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_NONE;
     20 import static com.google.common.truth.Truth.assertThat;
     21 import static org.mockito.Mockito.mock;
     22 import static org.mockito.Mockito.spy;
     23 import static org.mockito.Mockito.when;
     24 
     25 import android.app.NotificationChannel;
     26 import android.app.NotificationChannelGroup;
     27 import android.app.NotificationManager;
     28 import android.content.Context;
     29 import android.os.UserManager;
     30 import android.support.v7.preference.Preference;
     31 
     32 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     33 
     34 import org.junit.Before;
     35 import org.junit.Test;
     36 import org.junit.runner.RunWith;
     37 import org.mockito.Mock;
     38 import org.mockito.MockitoAnnotations;
     39 import org.robolectric.RuntimeEnvironment;
     40 import org.robolectric.shadows.ShadowApplication;
     41 
     42 @RunWith(SettingsRobolectricTestRunner.class)
     43 public class NotificationsOffPreferenceControllerTest {
     44 
     45     @Mock
     46     private NotificationManager mNm;
     47     @Mock
     48     private UserManager mUm;
     49 
     50     private NotificationsOffPreferenceController mController;
     51 
     52     @Before
     53     public void setUp() {
     54         MockitoAnnotations.initMocks(this);
     55         ShadowApplication shadowApplication = ShadowApplication.getInstance();
     56         shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNm);
     57         shadowApplication.setSystemService(Context.USER_SERVICE, mUm);
     58         mController = spy(new NotificationsOffPreferenceController(RuntimeEnvironment.application));
     59     }
     60 
     61     @Test
     62     public void testNoCrashIfNoOnResume() {
     63         mController.isAvailable();
     64         mController.updateState(mock(Preference.class));
     65     }
     66 
     67     @Test
     68     public void testIsAvailable_yesIfAppBlocked() {
     69         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
     70         appRow.banned = true;
     71         mController.onResume(appRow, null, null, null);
     72         assertThat(mController.isAvailable()).isTrue();
     73     }
     74 
     75     @Test
     76     public void testIsAvailable_yesIfChannelGroupBlocked() {
     77         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
     78         NotificationChannelGroup group = mock(NotificationChannelGroup.class);
     79         when(group.isBlocked()).thenReturn(true);
     80         mController.onResume(appRow, null, group, null);
     81         assertThat(mController.isAvailable()).isTrue();
     82     }
     83 
     84     @Test
     85     public void testIsAvailable_yesIfChannelBlocked() {
     86         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
     87         NotificationChannel channel = mock(NotificationChannel.class);
     88         when(channel.getImportance()).thenReturn(IMPORTANCE_NONE);
     89         mController.onResume(appRow, channel, null, null);
     90         assertThat(mController.isAvailable()).isTrue();
     91     }
     92 
     93     @Test
     94     public void testUpdateState_channel() {
     95         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
     96         NotificationChannel channel = mock(NotificationChannel.class);
     97         when(channel.getImportance()).thenReturn(IMPORTANCE_NONE);
     98         mController.onResume(appRow, channel, null, null);
     99 
    100         Preference pref = new Preference(RuntimeEnvironment.application);
    101         mController.updateState(pref);
    102 
    103         assertThat(pref.getTitle().toString()).contains("category");
    104         assertThat(pref.isSelectable()).isFalse();
    105     }
    106 
    107     @Test
    108     public void testUpdateState_channelGroup() {
    109         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
    110         NotificationChannelGroup group = mock(NotificationChannelGroup.class);
    111         when(group.isBlocked()).thenReturn(true);
    112         mController.onResume(appRow, null, group, null);
    113 
    114         Preference pref = new Preference(RuntimeEnvironment.application);
    115         mController.updateState(pref);
    116 
    117         assertThat(pref.getTitle().toString()).contains("group");
    118         assertThat(pref.isSelectable()).isFalse();
    119     }
    120 
    121     @Test
    122     public void testUpdateState_app() {
    123         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
    124         appRow.banned = true;
    125         mController.onResume(appRow, null, null, null);
    126 
    127         Preference pref = new Preference(RuntimeEnvironment.application);
    128         mController.updateState(pref);
    129 
    130         assertThat(pref.getTitle().toString()).contains("app");
    131         assertThat(pref.isSelectable()).isFalse();
    132     }
    133 }
    134