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.NotificationChannel.DEFAULT_CHANNEL_ID;
     20 import static android.app.NotificationManager.IMPORTANCE_DEFAULT;
     21 import static android.app.NotificationManager.IMPORTANCE_HIGH;
     22 import static android.app.NotificationManager.IMPORTANCE_LOW;
     23 import static android.app.NotificationManager.IMPORTANCE_NONE;
     24 import static android.app.NotificationManager.IMPORTANCE_UNSPECIFIED;
     25 import static junit.framework.Assert.assertFalse;
     26 import static junit.framework.Assert.assertNotNull;
     27 import static junit.framework.Assert.assertTrue;
     28 import static org.junit.Assert.assertEquals;
     29 import static org.mockito.ArgumentMatchers.any;
     30 import static org.mockito.ArgumentMatchers.anyBoolean;
     31 import static org.mockito.ArgumentMatchers.anyInt;
     32 import static org.mockito.ArgumentMatchers.anyString;
     33 import static org.mockito.Mockito.mock;
     34 import static org.mockito.Mockito.spy;
     35 import static org.mockito.Mockito.times;
     36 import static org.mockito.Mockito.verify;
     37 import static org.mockito.Mockito.when;
     38 
     39 import android.app.NotificationChannel;
     40 import android.app.NotificationChannelGroup;
     41 import android.app.NotificationManager;
     42 import android.content.Context;
     43 import android.os.UserManager;
     44 
     45 import com.android.settings.R;
     46 import com.android.settings.applications.LayoutPreference;
     47 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     48 import com.android.settings.widget.SwitchBar;
     49 
     50 import org.junit.Before;
     51 import org.junit.Test;
     52 import org.junit.runner.RunWith;
     53 import org.mockito.Mock;
     54 import org.mockito.MockitoAnnotations;
     55 import org.robolectric.shadows.ShadowApplication;
     56 
     57 @RunWith(SettingsRobolectricTestRunner.class)
     58 public class BlockPreferenceControllerTest {
     59 
     60     private Context mContext;
     61     @Mock
     62     private NotificationBackend mBackend;
     63     @Mock
     64     private NotificationManager mNm;
     65     @Mock
     66     private UserManager mUm;
     67 
     68     @Mock
     69     private NotificationSettingsBase.ImportanceListener mImportanceListener;
     70 
     71     private BlockPreferenceController mController;
     72     @Mock
     73     private LayoutPreference mPreference;
     74     private SwitchBar mSwitch;
     75 
     76     @Before
     77     public void setUp() {
     78         MockitoAnnotations.initMocks(this);
     79         ShadowApplication shadowApplication = ShadowApplication.getInstance();
     80         shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNm);
     81         shadowApplication.setSystemService(Context.USER_SERVICE, mUm);
     82         mContext = shadowApplication.getApplicationContext();
     83         mController = spy(new BlockPreferenceController(mContext, mImportanceListener, mBackend));
     84         mSwitch = new SwitchBar(mContext);
     85         when(mPreference.findViewById(R.id.switch_bar)).thenReturn(mSwitch);
     86     }
     87 
     88     @Test
     89     public void testNoCrashIfNoOnResume() {
     90         mController.isAvailable();
     91         mController.updateState(mock(LayoutPreference.class));
     92         mController.onSwitchChanged(null, false);
     93     }
     94 
     95     @Test
     96     public void testIsAvailable_notIfNull() {
     97         mController.onResume(null, null, null, null);
     98         assertFalse(mController.isAvailable());
     99     }
    100 
    101     @Test
    102     public void testIsAvailable_notIfChannelNotBlockable() {
    103         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
    104         appRow.systemApp = true;
    105         NotificationChannel channel = mock(NotificationChannel.class);
    106         when(channel.getImportance()).thenReturn(IMPORTANCE_HIGH);
    107         mController.onResume(appRow, channel, null, null);
    108         assertFalse(mController.isAvailable());
    109     }
    110 
    111     @Test
    112     public void testIsAvailable_notIfGroupNotBlockable() {
    113         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
    114         appRow.systemApp = true;
    115         mController.onResume(appRow, null, mock(NotificationChannelGroup.class), null);
    116         assertFalse(mController.isAvailable());
    117     }
    118 
    119     @Test
    120     public void testIsAvailable_notIfAppNotBlockable() {
    121         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
    122         appRow.systemApp = true;
    123         mController.onResume(appRow, null, null, null);
    124         assertFalse(mController.isAvailable());
    125     }
    126 
    127     @Test
    128     public void testIsAvailable_systemApp() {
    129         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
    130         appRow.systemApp = true;
    131         NotificationChannel channel = mock(NotificationChannel.class);
    132         when(channel.getImportance()).thenReturn(IMPORTANCE_NONE);
    133         mController.onResume(appRow, channel, null, null);
    134         assertTrue(mController.isAvailable());
    135     }
    136 
    137     @Test
    138     public void testIsAvailable_nonSystemApp() {
    139         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
    140         appRow.systemApp = false;
    141         NotificationChannel channel = mock(NotificationChannel.class);
    142         when(channel.getImportance()).thenReturn(IMPORTANCE_HIGH);
    143         mController.onResume(appRow, channel, null, null);
    144         assertTrue(mController.isAvailable());
    145     }
    146 
    147     @Test
    148     public void testUpdateState_app() {
    149         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
    150         appRow.banned = true;
    151         mController.onResume(appRow, null, null, null);
    152         mController.updateState(mPreference);
    153 
    154         assertNotNull(mPreference.findViewById(R.id.switch_bar));
    155 
    156         assertFalse(mSwitch.isChecked());
    157 
    158         appRow.banned = false;
    159         mController.onResume(appRow, null, null, null);
    160         mController.updateState(mPreference);
    161 
    162         assertTrue(mSwitch.isChecked());
    163     }
    164 
    165     @Test
    166     public void testUpdateState_group() {
    167         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
    168         NotificationChannelGroup group = mock(NotificationChannelGroup.class);
    169         when(group.isBlocked()).thenReturn(true);
    170         mController.onResume(appRow, null, group, null);
    171         mController.updateState(mPreference);
    172 
    173         assertFalse(mSwitch.isChecked());
    174 
    175         appRow.banned = true;
    176         mController.onResume(appRow, null, group, null);
    177         when(group.isBlocked()).thenReturn(true);
    178         mController.updateState(mPreference);
    179 
    180         assertFalse(mSwitch.isChecked());
    181 
    182         appRow.banned = false;
    183         mController.onResume(appRow, null, group, null);
    184         when(group.isBlocked()).thenReturn(false);
    185         mController.updateState(mPreference);
    186 
    187         assertTrue(mSwitch.isChecked());
    188     }
    189 
    190     @Test
    191     public void testUpdateState_channelBlocked() {
    192         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
    193         NotificationChannel channel = new NotificationChannel("a", "a", IMPORTANCE_NONE);
    194         mController.onResume(appRow, channel, null, null);
    195         mController.updateState(mPreference);
    196 
    197         assertFalse(mSwitch.isChecked());
    198 
    199         appRow.banned = true;
    200         channel = new NotificationChannel("a", "a", IMPORTANCE_HIGH);
    201         mController.onResume(appRow, channel, null, null);
    202         mController.updateState(mPreference);
    203 
    204         assertFalse(mSwitch.isChecked());
    205 
    206         appRow.banned = false;
    207         channel = new NotificationChannel("a", "a", IMPORTANCE_HIGH);
    208         mController.onResume(appRow, channel, null, null);
    209         mController.updateState(mPreference);
    210 
    211         assertTrue(mSwitch.isChecked());
    212     }
    213 
    214     @Test
    215     public void testUpdateState_noCrashIfCalledTwice() {
    216         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
    217         NotificationChannel channel = new NotificationChannel("a", "a", IMPORTANCE_LOW);
    218         mController.onResume(appRow, channel, null, null);
    219         mController.updateState(mPreference);
    220         mController.updateState(mPreference);
    221     }
    222 
    223     @Test
    224     public void testUpdateState_doesNotResetImportance() {
    225         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
    226         NotificationChannel channel = new NotificationChannel("a", "a", IMPORTANCE_LOW);
    227         mController.onResume(appRow, channel, null, null);
    228         mController.updateState(mPreference);
    229 
    230         assertEquals(IMPORTANCE_LOW, channel.getImportance());
    231     }
    232 
    233     @Test
    234     public void testOnSwitchChanged_channel_default() {
    235         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
    236         appRow.pkg = "pkg";
    237         NotificationChannel channel =
    238                 new NotificationChannel(DEFAULT_CHANNEL_ID, "a", IMPORTANCE_UNSPECIFIED);
    239         when(mBackend.onlyHasDefaultChannel(anyString(), anyInt())).thenReturn(true);
    240         mController.onResume(appRow, channel, null, null);
    241         mController.updateState(mPreference);
    242 
    243         mController.onSwitchChanged(null, false);
    244         assertEquals(IMPORTANCE_NONE, channel.getImportance());
    245         assertTrue(appRow.banned);
    246 
    247         mController.onSwitchChanged(null, true);
    248         assertEquals(IMPORTANCE_UNSPECIFIED, channel.getImportance());
    249         assertFalse(appRow.banned);
    250 
    251         verify(mBackend, times(2)).updateChannel(any(), anyInt(), any());
    252 
    253         verify(mBackend, times(2)).setNotificationsEnabledForPackage(
    254                 anyString(), anyInt(), anyBoolean());
    255     }
    256 
    257     @Test
    258     public void testOnSwitchChanged_channel_nonDefault() {
    259         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
    260         NotificationChannel channel = new NotificationChannel("a", "a", IMPORTANCE_HIGH);
    261         mController.onResume(appRow, channel, null, null);
    262         mController.updateState(mPreference);
    263 
    264         mController.onSwitchChanged(null, false);
    265         assertEquals(IMPORTANCE_NONE, channel.getImportance());
    266 
    267         mController.onSwitchChanged(null, true);
    268         assertEquals(IMPORTANCE_DEFAULT, channel.getImportance());
    269 
    270         verify(mBackend, times(2)).updateChannel(any(), anyInt(), any());
    271     }
    272 }
    273