• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_DEFAULT;
20 import static android.app.NotificationManager.IMPORTANCE_HIGH;
21 import static android.app.NotificationManager.IMPORTANCE_LOW;
22 import static android.app.NotificationManager.IMPORTANCE_MIN;
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.assertTrue;
27 import static org.junit.Assert.assertEquals;
28 import static org.mockito.ArgumentMatchers.anyInt;
29 import static org.mockito.Mockito.any;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.times;
32 import static org.mockito.Mockito.verify;
33 import static org.mockito.Mockito.when;
34 
35 import android.app.NotificationChannel;
36 import android.app.NotificationChannelGroup;
37 import android.app.NotificationManager;
38 import android.content.Context;
39 import android.os.UserManager;
40 import android.support.v7.preference.Preference;
41 
42 import com.android.settings.testutils.SettingsRobolectricTestRunner;
43 import com.android.settingslib.RestrictedLockUtils;
44 
45 import org.junit.Before;
46 import org.junit.Test;
47 import org.junit.runner.RunWith;
48 import org.mockito.Answers;
49 import org.mockito.Mock;
50 import org.mockito.MockitoAnnotations;
51 import org.robolectric.shadows.ShadowApplication;
52 
53 @RunWith(SettingsRobolectricTestRunner.class)
54 public class NotificationPreferenceControllerTest {
55 
56     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
57     private Context mContext;
58     @Mock
59     private NotificationBackend mBackend;
60     @Mock
61     private NotificationManager mNm;
62     @Mock
63     private UserManager mUm;
64 
65     private TestPreferenceController mController;
66 
67     @Before
setUp()68     public void setUp() {
69         MockitoAnnotations.initMocks(this);
70         ShadowApplication shadowApplication = ShadowApplication.getInstance();
71         shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNm);
72         shadowApplication.setSystemService(Context.USER_SERVICE, mUm);
73         mContext = shadowApplication.getApplicationContext();
74         mController = new TestPreferenceController(mContext, mBackend);
75     }
76 
77     @Test
noCrashIfNoOnResume()78     public void noCrashIfNoOnResume() {
79         mController.isAvailable();
80         mController.updateState(mock(Preference.class));
81         assertFalse(mController.checkCanBeVisible(IMPORTANCE_UNSPECIFIED));
82         mController.saveChannel();
83         assertFalse(mController.isChannelConfigurable());
84         assertFalse(mController.isChannelBlockable());
85         assertFalse(mController.isChannelGroupBlockable());
86     }
87 
88     @Test
isAvailable_notIfNull()89     public void isAvailable_notIfNull() {
90         mController.onResume(null, null, null, null);
91         assertFalse(mController.isAvailable());
92     }
93 
94     @Test
isAvailable_notIfAppBlocked()95     public void isAvailable_notIfAppBlocked() {
96         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
97         appRow.banned = true;
98         mController.onResume(appRow, mock(NotificationChannel.class),
99                 mock(NotificationChannelGroup.class), null);
100         assertFalse(mController.isAvailable());
101     }
102 
103     @Test
isAvailable_notIfChannelBlocked()104     public void isAvailable_notIfChannelBlocked() {
105         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
106         NotificationChannel channel = mock(NotificationChannel.class);
107         when(channel.getImportance()).thenReturn(IMPORTANCE_NONE);
108 
109         mController.onResume(appRow, channel, null, null);
110         assertFalse(mController.isAvailable());
111     }
112 
113     @Test
isAvailable_notIfChannelGroupBlocked()114     public void isAvailable_notIfChannelGroupBlocked() {
115         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
116         NotificationChannel channel = mock(NotificationChannel.class);
117         NotificationChannelGroup group = mock(NotificationChannelGroup.class);
118 
119         mController.onResume(appRow, channel, group, null);
120         when(group.isBlocked()).thenReturn(true);
121         assertFalse(mController.isAvailable());
122     }
123 
124     @Test
isAvailable()125     public void isAvailable() {
126         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
127         NotificationChannel channel = mock(NotificationChannel.class);
128         when(channel.getImportance()).thenReturn(IMPORTANCE_DEFAULT);
129         NotificationChannelGroup group = mock(NotificationChannelGroup.class);
130         when(group.isBlocked()).thenReturn(false);
131 
132         mController.onResume(appRow, channel, group, null);
133         assertTrue(mController.isAvailable());
134     }
135 
136     @Test
testOnResume()137     public void testOnResume() {
138         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
139         NotificationChannel channel = mock(NotificationChannel.class);
140         NotificationChannelGroup group = mock(NotificationChannelGroup.class);
141         RestrictedLockUtils.EnforcedAdmin admin = mock(RestrictedLockUtils.EnforcedAdmin.class);
142 
143         mController.onResume(appRow, channel, group, admin);
144 
145         assertEquals(appRow, mController.mAppRow);
146         assertEquals(channel, mController.mChannel);
147         assertEquals(group, mController.mChannelGroup);
148         assertEquals(admin, mController.mAdmin);
149     }
150 
151     @Test
testCanBeVisible_unspecified()152     public void testCanBeVisible_unspecified() {
153         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
154         NotificationChannel channel = mock(NotificationChannel.class);
155         when(channel.getImportance()).thenReturn(IMPORTANCE_UNSPECIFIED);
156 
157         mController.onResume(appRow, channel, null, null);
158         assertTrue(mController.checkCanBeVisible(IMPORTANCE_MIN));
159     }
160 
161     @Test
testCanBeVisible_sameImportance()162     public void testCanBeVisible_sameImportance() {
163         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
164         NotificationChannel channel = mock(NotificationChannel.class);
165         when(channel.getImportance()).thenReturn(IMPORTANCE_LOW);
166 
167         mController.onResume(appRow, channel, null, null);
168         assertTrue(mController.checkCanBeVisible(IMPORTANCE_LOW));
169     }
170 
171     @Test
testCanBeVisible_greaterImportance()172     public void testCanBeVisible_greaterImportance() {
173         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
174         NotificationChannel channel = mock(NotificationChannel.class);
175         when(channel.getImportance()).thenReturn(IMPORTANCE_LOW);
176 
177         mController.onResume(appRow, channel, null, null);
178         assertTrue(mController.checkCanBeVisible(IMPORTANCE_MIN));
179     }
180 
181     @Test
testCanBeVisible_lesserImportance()182     public void testCanBeVisible_lesserImportance() {
183         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
184         NotificationChannel channel = mock(NotificationChannel.class);
185         when(channel.getImportance()).thenReturn(IMPORTANCE_LOW);
186 
187         mController.onResume(appRow, channel, null, null);
188         assertFalse(mController.checkCanBeVisible(IMPORTANCE_DEFAULT));
189     }
190 
191     @Test
testSaveImportance()192     public void testSaveImportance() {
193         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
194         NotificationChannel channel = mock(NotificationChannel.class);
195         when(channel.getImportance()).thenReturn(IMPORTANCE_DEFAULT);
196 
197         mController.onResume(appRow, channel, null, null);
198         mController.saveChannel();
199         verify(mBackend, times(1)).updateChannel(any(), anyInt(), any());
200     }
201 
202     @Test
testIsConfigurable()203     public void testIsConfigurable() {
204         String sameId = "bananas";
205         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
206         appRow.lockedChannelId = sameId;
207         NotificationChannel channel = mock(NotificationChannel.class);
208         when(channel.getId()).thenReturn(sameId);
209 
210         mController.onResume(appRow, channel, null, null);
211         assertFalse(mController.isChannelConfigurable());
212 
213         when(channel.getId()).thenReturn("something new");
214         mController.onResume(appRow, channel, null, null);
215         assertTrue(mController.isChannelConfigurable());
216     }
217 
218     @Test
testIsChannelBlockable_nonSystemAppsBlockable()219     public void testIsChannelBlockable_nonSystemAppsBlockable() {
220         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
221         appRow.systemApp = false;
222         NotificationChannel channel = mock(NotificationChannel.class);
223         when(channel.isBlockableSystem()).thenReturn(false);
224 
225         mController.onResume(appRow, channel, null, null);
226         assertTrue(mController.isChannelBlockable());
227     }
228 
229     @Test
testIsChannelBlockable_mostSystemAppsNotBlockable()230     public void testIsChannelBlockable_mostSystemAppsNotBlockable() {
231         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
232         appRow.systemApp = true;
233         NotificationChannel channel = mock(NotificationChannel.class);
234         when(channel.isBlockableSystem()).thenReturn(false);
235         when(channel.getImportance()).thenReturn(IMPORTANCE_HIGH);
236 
237         mController.onResume(appRow, channel, null, null);
238         assertFalse(mController.isChannelBlockable());
239     }
240 
241     @Test
testIsChannelBlockable_someSystemAppsAreBlockable()242     public void testIsChannelBlockable_someSystemAppsAreBlockable() {
243         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
244         appRow.systemApp = true;
245         NotificationChannel channel = mock(NotificationChannel.class);
246         when(channel.isBlockableSystem()).thenReturn(true);
247 
248         mController.onResume(appRow, channel, null, null);
249         assertTrue(mController.isChannelBlockable());
250     }
251 
252     @Test
testIsChannelBlockable_canUndoSystemBlock()253     public void testIsChannelBlockable_canUndoSystemBlock() {
254         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
255         appRow.systemApp = true;
256         NotificationChannel channel = mock(NotificationChannel.class);
257         when(channel.isBlockableSystem()).thenReturn(false);
258         when(channel.getImportance()).thenReturn(IMPORTANCE_NONE);
259 
260         mController.onResume(appRow, channel, null, null);
261         assertTrue(mController.isChannelBlockable());
262     }
263 
264     @Test
testIsChannelGroupBlockable_nonSystemBlockable()265     public void testIsChannelGroupBlockable_nonSystemBlockable() {
266         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
267         appRow.systemApp = false;
268         NotificationChannelGroup group = mock(NotificationChannelGroup.class);
269         when(group.isBlocked()).thenReturn(false);
270 
271         mController.onResume(appRow, null, group, null);
272         assertTrue(mController.isChannelGroupBlockable());
273     }
274 
275     @Test
testIsChannelGroupBlockable_SystemNotBlockable()276     public void testIsChannelGroupBlockable_SystemNotBlockable() {
277         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
278         appRow.systemApp = true;
279         NotificationChannelGroup group = mock(NotificationChannelGroup.class);
280         when(group.isBlocked()).thenReturn(false);
281 
282         mController.onResume(appRow, null, group, null);
283         assertFalse(mController.isChannelGroupBlockable());
284     }
285 
286     @Test
testIsChannelGroupBlockable_canUndoSystemBlock()287     public void testIsChannelGroupBlockable_canUndoSystemBlock() {
288         NotificationBackend.AppRow appRow = new NotificationBackend.AppRow();
289         appRow.systemApp = true;
290         NotificationChannelGroup group = mock(NotificationChannelGroup.class);
291         when(group.isBlocked()).thenReturn(true);
292 
293         mController.onResume(appRow, null, group, null);
294         assertTrue(mController.isChannelGroupBlockable());
295     }
296 
297     @Test
testIsDefaultChannel_noChannel()298     public void testIsDefaultChannel_noChannel() {
299         mController.onResume(mock(NotificationBackend.AppRow.class), null, null, null);
300 
301         assertFalse(mController.isDefaultChannel());
302     }
303 
304     @Test
testIsDefaultChannel_nonDefaultChannel()305     public void testIsDefaultChannel_nonDefaultChannel() {
306         NotificationChannel channel = mock(NotificationChannel.class);
307         mController.onResume(mock(NotificationBackend.AppRow.class), channel, null, null);
308 
309         assertFalse(mController.isDefaultChannel());
310     }
311 
312     @Test
testIsDefaultChannel()313     public void testIsDefaultChannel() {
314         NotificationChannel channel = mock(NotificationChannel.class);
315         when(channel.getId()).thenReturn(NotificationChannel.DEFAULT_CHANNEL_ID);
316         mController.onResume(mock(NotificationBackend.AppRow.class), channel, null, null);
317 
318         assertTrue(mController.isDefaultChannel());
319     }
320 
321     private final class TestPreferenceController extends NotificationPreferenceController {
322 
TestPreferenceController(Context context, NotificationBackend backend)323         private TestPreferenceController(Context context, NotificationBackend backend) {
324             super(context, backend);
325         }
326 
327         @Override
getPreferenceKey()328         public String getPreferenceKey() {
329             return null;
330         }
331     }
332 }
333