Home | History | Annotate | Download | only in notification
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.settings.notification;
     18 
     19 import static android.app.NotificationManager.IMPORTANCE_LOW;
     20 import static android.app.NotificationManager.IMPORTANCE_NONE;
     21 import static junit.framework.Assert.assertFalse;
     22 import static junit.framework.Assert.assertTrue;
     23 import static org.junit.Assert.assertEquals;
     24 import static org.mockito.Mockito.mock;
     25 import static org.mockito.Mockito.spy;
     26 import static org.mockito.Mockito.when;
     27 
     28 import android.app.NotificationChannel;
     29 import android.app.NotificationChannelGroup;
     30 import android.app.NotificationManager;
     31 import android.content.Context;
     32 import android.os.UserManager;
     33 import android.support.v7.preference.Preference;
     34 
     35 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     36 
     37 import org.junit.Before;
     38 import org.junit.Test;
     39 import org.junit.runner.RunWith;
     40 import org.mockito.Mock;
     41 import org.mockito.MockitoAnnotations;
     42 import org.robolectric.RuntimeEnvironment;
     43 import org.robolectric.shadows.ShadowApplication;
     44 
     45 @RunWith(SettingsRobolectricTestRunner.class)
     46 public class DescriptionPreferenceControllerTest {
     47 
     48     private Context mContext;
     49     @Mock
     50     private NotificationManager mNm;
     51     @Mock
     52     private UserManager mUm;
     53 
     54     private DescriptionPreferenceController mController;
     55 
     56     @Before
     57     public void setUp() {
     58         MockitoAnnotations.initMocks(this);
     59         ShadowApplication shadowApplication = ShadowApplication.getInstance();
     60         shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNm);
     61         shadowApplication.setSystemService(Context.USER_SERVICE, mUm);
     62         mContext = shadowApplication.getApplicationContext();
     63         mController = spy(new DescriptionPreferenceController(mContext));
     64     }
     65 
     66     @Test
     67     public void testNoCrashIfNoOnResume() {
     68         mController.isAvailable();
     69         mController.updateState(mock(Preference.class));
     70     }
     71 
     72     @Test
     73     public void testIsAvailable_notIfNull() {
     74         mController.onResume(null, null, null, null);
     75         assertFalse(mController.isAvailable());
     76     }
     77 
     78     @Test
     79     public void testIsAvailable_notIfChannelGroupBlocked() {
     80         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
     81         NotificationChannelGroup group = mock(NotificationChannelGroup.class);
     82         mController.onResume(appRow, null, group, null);
     83         assertFalse(mController.isAvailable());
     84     }
     85 
     86     @Test
     87     public void testIsAvailable_notIfChannelBlocked() {
     88         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
     89         NotificationChannel channel = mock(NotificationChannel.class);
     90         when(channel.getImportance()).thenReturn(IMPORTANCE_NONE);
     91         mController.onResume(appRow, channel, null, null);
     92         assertFalse(mController.isAvailable());
     93     }
     94 
     95     @Test
     96     public void testIsAvailable_notIfNoChannelDesc() {
     97         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
     98         NotificationChannel channel = mock(NotificationChannel.class);
     99         when(channel.getImportance()).thenReturn(IMPORTANCE_LOW);
    100         mController.onResume(appRow, channel, null, null);
    101         assertFalse(mController.isAvailable());
    102     }
    103 
    104     @Test
    105     public void testIsAvailable_notIfNoChannelGroupDesc() {
    106         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
    107         NotificationChannelGroup group = mock(NotificationChannelGroup.class);
    108         mController.onResume(appRow, null, group, null);
    109         assertFalse(mController.isAvailable());
    110     }
    111 
    112     @Test
    113     public void testIsAvailable_channel() {
    114         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
    115         NotificationChannel channel = mock(NotificationChannel.class);
    116         when(channel.getImportance()).thenReturn(IMPORTANCE_LOW);
    117         when(channel.getDescription()).thenReturn("AAA");
    118         mController.onResume(appRow, channel, null, null);
    119         assertTrue(mController.isAvailable());
    120     }
    121 
    122     @Test
    123     public void testIsAvailable_channelGroup() {
    124         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
    125         NotificationChannelGroup group = mock(NotificationChannelGroup.class);
    126         when(group.getDescription()).thenReturn("something");
    127         when(group.isBlocked()).thenReturn(false);
    128         mController.onResume(appRow, null, group, null);
    129         assertTrue(mController.isAvailable());
    130     }
    131 
    132     @Test
    133     public void testUpdateState_channel() {
    134         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
    135         NotificationChannel channel = mock(NotificationChannel.class);
    136         when(channel.getImportance()).thenReturn(IMPORTANCE_LOW);
    137         when(channel.getDescription()).thenReturn("AAA");
    138         mController.onResume(appRow, channel, null, null);
    139 
    140         Preference pref = new Preference(RuntimeEnvironment.application);
    141         mController.updateState(pref);
    142 
    143         assertEquals("AAA", pref.getTitle());
    144         assertFalse(pref.isEnabled());
    145         assertFalse(pref.isSelectable());
    146     }
    147 
    148     @Test
    149     public void testUpdateState_channelGroup() {
    150         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
    151         NotificationChannelGroup group = mock(NotificationChannelGroup.class);
    152         when(group.getDescription()).thenReturn("something");
    153         mController.onResume(appRow, null, group, null);
    154 
    155         Preference pref = new Preference(RuntimeEnvironment.application);
    156         mController.updateState(pref);
    157 
    158         assertEquals("something", pref.getTitle());
    159         assertFalse(pref.isEnabled());
    160         assertFalse(pref.isSelectable());
    161     }
    162 }
    163