blob: 9afb2d2b210cd7971d94244e4dff2246dc9646dd [file] [log] [blame]
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -05001/*
2 * Copyright (C) 2016 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
17package com.android.server.notification;
18
Julia Reynolds73ed76b2017-04-04 17:04:38 -040019import static android.app.NotificationManager.IMPORTANCE_LOW;
20
Geoffrey Pitsch03533712017-01-05 10:30:07 -050021import static junit.framework.Assert.assertEquals;
Julia Reynolds727a7282017-04-13 10:54:01 -040022import static junit.framework.Assert.assertFalse;
Julia Reynoldsbaff4002016-12-15 11:34:26 -050023import static junit.framework.Assert.assertTrue;
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -050024import static junit.framework.Assert.fail;
Julia Reynoldsbaff4002016-12-15 11:34:26 -050025
Julia Reynolds5f20e9f2017-01-30 08:54:53 -050026import static org.mockito.Matchers.anyBoolean;
Julia Reynoldsa78cdff2017-04-26 10:19:25 -040027import static org.mockito.Matchers.anyLong;
Julia Reynoldsbaff4002016-12-15 11:34:26 -050028import static org.mockito.Matchers.anyString;
29import static org.mockito.Matchers.eq;
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -050030import static org.mockito.Mockito.any;
31import static org.mockito.Mockito.anyInt;
32import static org.mockito.Mockito.mock;
Julia Reynolds73ed76b2017-04-04 17:04:38 -040033import static org.mockito.Mockito.never;
34import static org.mockito.Mockito.reset;
Julia Reynoldsbaff4002016-12-15 11:34:26 -050035import static org.mockito.Mockito.times;
36import static org.mockito.Mockito.verify;
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -050037import static org.mockito.Mockito.when;
38
39import android.app.INotificationManager;
Julia Reynoldsbaff4002016-12-15 11:34:26 -050040import android.app.Notification;
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -050041import android.app.NotificationChannel;
Julia Reynolds73ed76b2017-04-04 17:04:38 -040042import android.app.NotificationChannelGroup;
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -050043import android.app.NotificationManager;
Julia Reynolds73ed76b2017-04-04 17:04:38 -040044import android.companion.ICompanionDeviceManager;
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -050045import android.content.ComponentName;
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -050046import android.content.Context;
47import android.content.pm.ApplicationInfo;
48import android.content.pm.IPackageManager;
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -050049import android.content.pm.PackageManager;
Geoffrey Pitsch03533712017-01-05 10:30:07 -050050import android.content.pm.ParceledListSlice;
Julia Reynolds73ed76b2017-04-04 17:04:38 -040051import android.graphics.Color;
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -050052import android.os.Binder;
Julia Reynoldsf27d6b22017-04-13 15:48:16 -040053import android.os.Process;
Julia Reynoldsbaff4002016-12-15 11:34:26 -050054import android.os.UserHandle;
Chris Wren89aa2262017-05-05 18:05:56 -040055import android.provider.Settings.Secure;
Julia Reynolds73ed76b2017-04-04 17:04:38 -040056import android.service.notification.NotificationListenerService;
Julia Reynoldsbaff4002016-12-15 11:34:26 -050057import android.service.notification.StatusBarNotification;
Jason Monk745d0a82017-04-17 11:34:22 -040058import android.testing.AndroidTestingRunner;
Geoffrey Pitsch415e4542017-04-10 13:12:58 -040059import android.testing.TestableLooper;
Jason Monk745d0a82017-04-17 11:34:22 -040060import android.testing.TestableLooper.RunWithLooper;
Julia Reynolds5f20e9f2017-01-30 08:54:53 -050061
Julia Reynolds73ed76b2017-04-04 17:04:38 -040062import java.util.ArrayList;
Geoffrey Pitsch03533712017-01-05 10:30:07 -050063import java.util.Arrays;
Julia Reynolds73ed76b2017-04-04 17:04:38 -040064import java.util.List;
65
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -050066import org.junit.Before;
67import org.junit.Test;
Jason Monk745d0a82017-04-17 11:34:22 -040068import org.junit.runner.RunWith;
Julia Reynoldsda781472017-04-12 09:41:16 -040069import org.mockito.Mock;
70import org.mockito.Mockito;
71import org.mockito.MockitoAnnotations;
Julia Reynolds5f20e9f2017-01-30 08:54:53 -050072
Geoffrey Pitsch03533712017-01-05 10:30:07 -050073import com.android.server.lights.Light;
74import com.android.server.lights.LightsManager;
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -050075
Jason Monk745d0a82017-04-17 11:34:22 -040076@RunWith(AndroidTestingRunner.class)
77@RunWithLooper
Chris Wren89aa2262017-05-05 18:05:56 -040078public class NotificationManagerServiceTest extends NotificationTestCase {
Geoffrey Pitsch16594462017-01-26 14:42:30 -050079 private static final long WAIT_FOR_IDLE_TIMEOUT = 2;
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -050080 private static final String TEST_CHANNEL_ID = "NotificationManagerServiceTestChannelId";
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -050081 private final int uid = Binder.getCallingUid();
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -050082 private NotificationManagerService mNotificationManagerService;
83 private INotificationManager mBinderService;
Geoffrey Pitsch415e4542017-04-10 13:12:58 -040084 private NotificationManagerInternal mInternalService;
Julia Reynoldsda781472017-04-12 09:41:16 -040085 @Mock
86 private IPackageManager mPackageManager;
87 @Mock
88 private PackageManager mPackageManagerClient;
Chris Wren89aa2262017-05-05 18:05:56 -040089 private Context mContext = getContext();
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -050090 private final String PKG = mContext.getPackageName();
Geoffrey Pitsch415e4542017-04-10 13:12:58 -040091 private TestableLooper mTestableLooper;
Julia Reynoldsda781472017-04-12 09:41:16 -040092 @Mock
93 private RankingHelper mRankingHelper;
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -050094 private NotificationChannel mTestNotificationChannel = new NotificationChannel(
95 TEST_CHANNEL_ID, TEST_CHANNEL_ID, NotificationManager.IMPORTANCE_DEFAULT);
Julia Reynoldsda781472017-04-12 09:41:16 -040096 @Mock
97 private NotificationManagerService.NotificationListeners mNotificationListeners;
98 private ManagedServices.ManagedServiceInfo mListener;
99 @Mock private ICompanionDeviceManager mCompanionMgr;
Julia Reynoldsa78cdff2017-04-26 10:19:25 -0400100 @Mock SnoozeHelper mSnoozeHelper;
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -0500101
Geoffrey Pitsch415e4542017-04-10 13:12:58 -0400102 // Use a Testable subclass so we can simulate calls from the system without failing.
103 private static class TestableNotificationManagerService extends NotificationManagerService {
104 public TestableNotificationManagerService(Context context) { super(context); }
105
106 @Override
Geoffrey Pitsch27684152017-05-02 11:41:31 -0400107 protected boolean isCallingUidSystem() {
108 return true;
109 }
110
111 @Override
112 protected boolean isCallerSystemOrPhone() {
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400113 return true;
114 }
Julia Reynolds727a7282017-04-13 10:54:01 -0400115
116 @Override
117 protected ICompanionDeviceManager getCompanionManager() {
118 return null;
119 }
Geoffrey Pitsch415e4542017-04-10 13:12:58 -0400120 }
121
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -0500122 @Before
123 public void setUp() throws Exception {
Julia Reynoldsda781472017-04-12 09:41:16 -0400124 MockitoAnnotations.initMocks(this);
Chris Wren89aa2262017-05-05 18:05:56 -0400125
126 // most tests assume badging is enabled
127 Secure.putIntForUser(getContext().getContentResolver(),
128 Secure.NOTIFICATION_BADGING, 1,
129 UserHandle.getUserHandleForUid(uid).getIdentifier());
130
Geoffrey Pitsch415e4542017-04-10 13:12:58 -0400131 mNotificationManagerService = new TestableNotificationManagerService(mContext);
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -0500132
133 // MockPackageManager - default returns ApplicationInfo with matching calling UID
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -0500134 final ApplicationInfo applicationInfo = new ApplicationInfo();
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500135 applicationInfo.uid = uid;
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400136 when(mPackageManager.getApplicationInfo(anyString(), anyInt(), anyInt()))
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -0500137 .thenReturn(applicationInfo);
Julia Reynolds5f20e9f2017-01-30 08:54:53 -0500138 when(mPackageManagerClient.getApplicationInfoAsUser(anyString(), anyInt(), anyInt()))
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500139 .thenReturn(applicationInfo);
Geoffrey Pitsch03533712017-01-05 10:30:07 -0500140 final LightsManager mockLightsManager = mock(LightsManager.class);
141 when(mockLightsManager.getLight(anyInt())).thenReturn(mock(Light.class));
Geoffrey Pitsch415e4542017-04-10 13:12:58 -0400142 // Use this testable looper.
Jason Monk745d0a82017-04-17 11:34:22 -0400143 mTestableLooper = TestableLooper.get(this);
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500144
Julia Reynoldsda781472017-04-12 09:41:16 -0400145 mListener = mNotificationListeners.new ManagedServiceInfo(
146 null, new ComponentName(PKG, "test_class"), uid, true, null, 0);
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400147 when(mNotificationListeners.checkServiceTokenLocked(any())).thenReturn(mListener);
Geoffrey Pitsch415e4542017-04-10 13:12:58 -0400148 mNotificationManagerService.init(mTestableLooper.getLooper(), mPackageManager,
Julia Reynoldsa78cdff2017-04-26 10:19:25 -0400149 mPackageManagerClient, mockLightsManager, mNotificationListeners, mCompanionMgr,
150 mSnoozeHelper);
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -0500151
152 // Tests call directly into the Binder.
153 mBinderService = mNotificationManagerService.getBinderService();
Geoffrey Pitsch415e4542017-04-10 13:12:58 -0400154 mInternalService = mNotificationManagerService.getInternalService();
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500155
156 mBinderService.createNotificationChannels(
157 PKG, new ParceledListSlice(Arrays.asList(mTestNotificationChannel)));
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -0500158 }
159
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500160 public void waitForIdle() throws Exception {
Geoffrey Pitsch415e4542017-04-10 13:12:58 -0400161 mTestableLooper.processAllMessages();
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500162 }
163
Julia Reynoldsa78cdff2017-04-26 10:19:25 -0400164 private NotificationRecord generateNotificationRecord(NotificationChannel channel, int id,
165 String groupKey, boolean isSummary) {
166 Notification.Builder nb = new Notification.Builder(mContext, channel.getId())
167 .setContentTitle("foo")
168 .setSmallIcon(android.R.drawable.sym_def_app_icon)
169 .setGroup(groupKey)
170 .setGroupSummary(isSummary);
171
172 StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, id, "tag", uid, 0,
173 nb.build(), new UserHandle(uid), null, 0);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000174 return new NotificationRecord(mContext, sbn, channel);
Julia Reynoldsa78cdff2017-04-26 10:19:25 -0400175 }
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500176 private NotificationRecord generateNotificationRecord(NotificationChannel channel) {
Julia Reynolds5f20e9f2017-01-30 08:54:53 -0500177 return generateNotificationRecord(channel, null);
178 }
179
180 private NotificationRecord generateNotificationRecord(NotificationChannel channel,
181 Notification.TvExtender extender) {
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500182 if (channel == null) {
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500183 channel = mTestNotificationChannel;
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500184 }
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500185 Notification.Builder nb = new Notification.Builder(mContext, channel.getId())
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500186 .setContentTitle("foo")
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500187 .setSmallIcon(android.R.drawable.sym_def_app_icon);
Julia Reynolds5f20e9f2017-01-30 08:54:53 -0500188 if (extender != null) {
189 nb.extend(extender);
190 }
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500191 StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, "tag", uid, 0,
Julia Reynolds5f20e9f2017-01-30 08:54:53 -0500192 nb.build(), new UserHandle(uid), null, 0);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000193 return new NotificationRecord(mContext, sbn, channel);
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500194 }
195
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500196 @Test
Geoffrey Pitsch03533712017-01-05 10:30:07 -0500197 public void testCreateNotificationChannels_SingleChannel() throws Exception {
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -0500198 final NotificationChannel channel =
199 new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_DEFAULT);
Geoffrey Pitsch03533712017-01-05 10:30:07 -0500200 mBinderService.createNotificationChannels("test_pkg",
201 new ParceledListSlice(Arrays.asList(channel)));
202 final NotificationChannel createdChannel =
203 mBinderService.getNotificationChannel("test_pkg", "id");
204 assertTrue(createdChannel != null);
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -0500205 }
206
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500207 @Test
Geoffrey Pitsch03533712017-01-05 10:30:07 -0500208 public void testCreateNotificationChannels_NullChannelThrowsException() throws Exception {
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -0500209 try {
Geoffrey Pitsch03533712017-01-05 10:30:07 -0500210 mBinderService.createNotificationChannels("test_pkg",
211 new ParceledListSlice(Arrays.asList(null)));
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -0500212 fail("Exception should be thrown immediately.");
213 } catch (NullPointerException e) {
214 // pass
215 }
216 }
Julia Reynoldsbaff4002016-12-15 11:34:26 -0500217
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500218 @Test
Geoffrey Pitsch03533712017-01-05 10:30:07 -0500219 public void testCreateNotificationChannels_TwoChannels() throws Exception {
220 final NotificationChannel channel1 =
221 new NotificationChannel("id1", "name", NotificationManager.IMPORTANCE_DEFAULT);
222 final NotificationChannel channel2 =
223 new NotificationChannel("id2", "name", NotificationManager.IMPORTANCE_DEFAULT);
224 mBinderService.createNotificationChannels("test_pkg",
225 new ParceledListSlice(Arrays.asList(channel1, channel2)));
226 assertTrue(mBinderService.getNotificationChannel("test_pkg", "id1") != null);
227 assertTrue(mBinderService.getNotificationChannel("test_pkg", "id2") != null);
228 }
229
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500230 @Test
Geoffrey Pitsch03533712017-01-05 10:30:07 -0500231 public void testCreateNotificationChannels_SecondCreateDoesNotChangeImportance()
232 throws Exception {
233 final NotificationChannel channel =
234 new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_DEFAULT);
235 mBinderService.createNotificationChannels("test_pkg",
236 new ParceledListSlice(Arrays.asList(channel)));
237
238 // Recreating the channel doesn't throw, but ignores importance.
239 final NotificationChannel dupeChannel =
240 new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_HIGH);
241 mBinderService.createNotificationChannels("test_pkg",
242 new ParceledListSlice(Arrays.asList(dupeChannel)));
243 final NotificationChannel createdChannel =
244 mBinderService.getNotificationChannel("test_pkg", "id");
245 assertEquals(NotificationManager.IMPORTANCE_DEFAULT, createdChannel.getImportance());
246 }
247
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500248 @Test
Geoffrey Pitsch03533712017-01-05 10:30:07 -0500249 public void testCreateNotificationChannels_IdenticalChannelsInListIgnoresSecond()
250 throws Exception {
251 final NotificationChannel channel1 =
252 new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_DEFAULT);
253 final NotificationChannel channel2 =
254 new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_HIGH);
255 mBinderService.createNotificationChannels("test_pkg",
256 new ParceledListSlice(Arrays.asList(channel1, channel2)));
257 final NotificationChannel createdChannel =
258 mBinderService.getNotificationChannel("test_pkg", "id");
259 assertEquals(NotificationManager.IMPORTANCE_DEFAULT, createdChannel.getImportance());
260 }
261
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500262 @Test
Julia Reynoldsbaff4002016-12-15 11:34:26 -0500263 public void testBlockedNotifications_suspended() throws Exception {
264 NotificationUsageStats usageStats = mock(NotificationUsageStats.class);
265 when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(true);
266
267 NotificationChannel channel = new NotificationChannel("id", "name",
268 NotificationManager.IMPORTANCE_HIGH);
269 NotificationRecord r = generateNotificationRecord(channel);
Julia Reynoldsd94054f2017-02-01 11:11:06 -0500270 assertTrue(mNotificationManagerService.isBlocked(r, usageStats));
Julia Reynoldsbaff4002016-12-15 11:34:26 -0500271 verify(usageStats, times(1)).registerSuspendedByAdmin(eq(r));
272 }
273
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500274 @Test
Julia Reynoldsbaff4002016-12-15 11:34:26 -0500275 public void testBlockedNotifications_blockedChannel() throws Exception {
276 NotificationUsageStats usageStats = mock(NotificationUsageStats.class);
277 when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false);
278
279 NotificationChannel channel = new NotificationChannel("id", "name",
280 NotificationManager.IMPORTANCE_HIGH);
Julia Reynoldsaebfc4e2017-01-06 11:44:03 -0500281 channel.setImportance(NotificationManager.IMPORTANCE_NONE);
Julia Reynoldsbaff4002016-12-15 11:34:26 -0500282 NotificationRecord r = generateNotificationRecord(channel);
Julia Reynoldsd94054f2017-02-01 11:11:06 -0500283 assertTrue(mNotificationManagerService.isBlocked(r, usageStats));
Julia Reynoldsbaff4002016-12-15 11:34:26 -0500284 verify(usageStats, times(1)).registerBlocked(eq(r));
285 }
286
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500287 @Test
Julia Reynoldsbaff4002016-12-15 11:34:26 -0500288 public void testBlockedNotifications_blockedApp() throws Exception {
289 NotificationUsageStats usageStats = mock(NotificationUsageStats.class);
290 when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false);
291
292 NotificationChannel channel = new NotificationChannel("id", "name",
293 NotificationManager.IMPORTANCE_HIGH);
294 NotificationRecord r = generateNotificationRecord(channel);
295 r.setUserImportance(NotificationManager.IMPORTANCE_NONE);
Julia Reynoldsd94054f2017-02-01 11:11:06 -0500296 assertTrue(mNotificationManagerService.isBlocked(r, usageStats));
Julia Reynoldsbaff4002016-12-15 11:34:26 -0500297 verify(usageStats, times(1)).registerBlocked(eq(r));
298 }
299
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500300 @Test
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500301 public void testEnqueueNotificationWithTag_PopulatesGetActiveNotifications() throws Exception {
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500302 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0,
Julia Reynoldsfea6f7b2017-04-19 13:50:12 -0400303 generateNotificationRecord(null).getNotification(), 0);
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500304 waitForIdle();
305 StatusBarNotification[] notifs =
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500306 mBinderService.getActiveNotifications(PKG);
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500307 assertEquals(1, notifs.length);
308 }
309
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500310 @Test
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500311 public void testCancelNotificationImmediatelyAfterEnqueue() throws Exception {
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500312 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0,
Julia Reynoldsfea6f7b2017-04-19 13:50:12 -0400313 generateNotificationRecord(null).getNotification(), 0);
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500314 mBinderService.cancelNotificationWithTag(PKG, "tag", 0, 0);
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500315 waitForIdle();
316 StatusBarNotification[] notifs =
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500317 mBinderService.getActiveNotifications(PKG);
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500318 assertEquals(0, notifs.length);
319 }
320
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500321 @Test
Geoffrey Pitschccc0b972017-02-15 10:52:26 -0500322 public void testCancelNotificationWhilePostedAndEnqueued() throws Exception {
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500323 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0,
Julia Reynoldsfea6f7b2017-04-19 13:50:12 -0400324 generateNotificationRecord(null).getNotification(), 0);
Geoffrey Pitschccc0b972017-02-15 10:52:26 -0500325 waitForIdle();
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500326 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0,
Julia Reynoldsfea6f7b2017-04-19 13:50:12 -0400327 generateNotificationRecord(null).getNotification(), 0);
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500328 mBinderService.cancelNotificationWithTag(PKG, "tag", 0, 0);
Geoffrey Pitschccc0b972017-02-15 10:52:26 -0500329 waitForIdle();
330 StatusBarNotification[] notifs =
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500331 mBinderService.getActiveNotifications(PKG);
Geoffrey Pitschccc0b972017-02-15 10:52:26 -0500332 assertEquals(0, notifs.length);
333 }
334
335 @Test
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500336 public void testCancelNotificationsFromListenerImmediatelyAfterEnqueue() throws Exception {
337 final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500338 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
Julia Reynoldsfea6f7b2017-04-19 13:50:12 -0400339 sbn.getId(), sbn.getNotification(), sbn.getUserId());
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500340 mBinderService.cancelNotificationsFromListener(null, null);
341 waitForIdle();
342 StatusBarNotification[] notifs =
343 mBinderService.getActiveNotifications(sbn.getPackageName());
344 assertEquals(0, notifs.length);
345 }
346
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500347 @Test
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500348 public void testCancelAllNotificationsImmediatelyAfterEnqueue() throws Exception {
349 final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500350 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
Julia Reynoldsfea6f7b2017-04-19 13:50:12 -0400351 sbn.getId(), sbn.getNotification(), sbn.getUserId());
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500352 mBinderService.cancelAllNotifications(PKG, sbn.getUserId());
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500353 waitForIdle();
354 StatusBarNotification[] notifs =
355 mBinderService.getActiveNotifications(sbn.getPackageName());
356 assertEquals(0, notifs.length);
357 }
358
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500359 @Test
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500360 public void testCancelAllNotifications_IgnoreForegroundService() throws Exception {
361 final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
362 sbn.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE;
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500363 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
Julia Reynoldsfea6f7b2017-04-19 13:50:12 -0400364 sbn.getId(), sbn.getNotification(), sbn.getUserId());
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500365 mBinderService.cancelAllNotifications(PKG, sbn.getUserId());
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500366 waitForIdle();
367 StatusBarNotification[] notifs =
368 mBinderService.getActiveNotifications(sbn.getPackageName());
369 assertEquals(1, notifs.length);
370 }
371
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500372 @Test
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500373 public void testCancelAllNotifications_IgnoreOtherPackages() throws Exception {
374 final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
375 sbn.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE;
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500376 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
Julia Reynoldsfea6f7b2017-04-19 13:50:12 -0400377 sbn.getId(), sbn.getNotification(), sbn.getUserId());
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500378 mBinderService.cancelAllNotifications("other_pkg_name", sbn.getUserId());
379 waitForIdle();
380 StatusBarNotification[] notifs =
381 mBinderService.getActiveNotifications(sbn.getPackageName());
382 assertEquals(1, notifs.length);
383 }
384
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500385 @Test
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500386 public void testCancelAllNotifications_NullPkgRemovesAll() throws Exception {
387 final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500388 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
Julia Reynoldsfea6f7b2017-04-19 13:50:12 -0400389 sbn.getId(), sbn.getNotification(), sbn.getUserId());
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500390 mBinderService.cancelAllNotifications(null, sbn.getUserId());
391 waitForIdle();
392 StatusBarNotification[] notifs =
393 mBinderService.getActiveNotifications(sbn.getPackageName());
394 assertEquals(0, notifs.length);
395 }
396
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500397 @Test
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500398 public void testCancelAllNotifications_NullPkgIgnoresUserAllNotifications() throws Exception {
399 final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500400 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
Julia Reynoldsfea6f7b2017-04-19 13:50:12 -0400401 sbn.getId(), sbn.getNotification(), UserHandle.USER_ALL);
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500402 // Null pkg is how we signal a user switch.
403 mBinderService.cancelAllNotifications(null, sbn.getUserId());
404 waitForIdle();
405 StatusBarNotification[] notifs =
406 mBinderService.getActiveNotifications(sbn.getPackageName());
407 assertEquals(1, notifs.length);
408 }
Julia Reynolds5f20e9f2017-01-30 08:54:53 -0500409
410 @Test
Geoffrey Pitsch415e4542017-04-10 13:12:58 -0400411 public void testRemoveForegroundServiceFlag_ImmediatelyAfterEnqueue() throws Exception {
412 final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
413 sbn.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE;
414 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", null,
Julia Reynoldsfea6f7b2017-04-19 13:50:12 -0400415 sbn.getId(), sbn.getNotification(), sbn.getUserId());
Geoffrey Pitsch415e4542017-04-10 13:12:58 -0400416 mInternalService.removeForegroundServiceFlagFromNotification(PKG, sbn.getId(),
417 sbn.getUserId());
418 waitForIdle();
419 StatusBarNotification[] notifs =
420 mBinderService.getActiveNotifications(sbn.getPackageName());
421 assertEquals(0, notifs[0].getNotification().flags & Notification.FLAG_FOREGROUND_SERVICE);
422 }
423
424 @Test
Geoffrey Pitsch27684152017-05-02 11:41:31 -0400425 public void testCancelAfterSecondEnqueueDoesNotSpecifyForegroundFlag() throws Exception {
426 final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
427 sbn.getNotification().flags =
428 Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE;
429 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
430 sbn.getId(), sbn.getNotification(), sbn.getUserId());
431 sbn.getNotification().flags = Notification.FLAG_ONGOING_EVENT;
432 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
433 sbn.getId(), sbn.getNotification(), sbn.getUserId());
434 mBinderService.cancelNotificationWithTag(PKG, "tag", sbn.getId(), sbn.getUserId());
435 waitForIdle();
436 assertEquals(0, mBinderService.getActiveNotifications(sbn.getPackageName()).length);
437 }
438
439 @Test
Julia Reynoldsa78cdff2017-04-26 10:19:25 -0400440 public void testFindGroupNotificationsLocked() throws Exception {
441 // make sure the same notification can be found in both lists and returned
442 final NotificationRecord group1 = generateNotificationRecord(
443 mTestNotificationChannel, 1, "group1", true);
444 mNotificationManagerService.addEnqueuedNotification(group1);
445 mNotificationManagerService.addNotification(group1);
446
447 // should not be returned
448 final NotificationRecord group2 = generateNotificationRecord(
449 mTestNotificationChannel, 2, "group2", true);
450 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", null,
451 group2.sbn.getId(), group2.sbn.getNotification(), group2.sbn.getUserId());
452 waitForIdle();
453
454 // should not be returned
455 final NotificationRecord nonGroup = generateNotificationRecord(
456 mTestNotificationChannel, 3, null, false);
457 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", null,
458 nonGroup.sbn.getId(), nonGroup.sbn.getNotification(), nonGroup.sbn.getUserId());
459 waitForIdle();
460
461 // same group, child, should be returned
462 final NotificationRecord group1Child = generateNotificationRecord(
463 mTestNotificationChannel, 4, "group1", false);
464 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", null, group1Child.sbn.getId(),
465 group1Child.sbn.getNotification(), group1Child.sbn.getUserId());
466 waitForIdle();
467
468 List<NotificationRecord> inGroup1 =
469 mNotificationManagerService.findGroupNotificationsLocked(PKG, group1.getGroupKey(),
470 group1.sbn.getUserId());
471 assertEquals(3, inGroup1.size());
472 for (NotificationRecord record : inGroup1) {
473 assertTrue(record.getGroupKey().equals(group1.getGroupKey()));
474 assertTrue(record.sbn.getId() == 1 || record.sbn.getId() == 4);
475 }
476 }
477
Julia Reynoldsa78cdff2017-04-26 10:19:25 -0400478 @Test
Julia Reynolds5f20e9f2017-01-30 08:54:53 -0500479 public void testTvExtenderChannelOverride_onTv() throws Exception {
480 mNotificationManagerService.setIsTelevision(true);
481 mNotificationManagerService.setRankingHelper(mRankingHelper);
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500482 when(mRankingHelper.getNotificationChannel(
Julia Reynolds5f20e9f2017-01-30 08:54:53 -0500483 anyString(), anyInt(), eq("foo"), anyBoolean())).thenReturn(
484 new NotificationChannel("foo", "foo", NotificationManager.IMPORTANCE_HIGH));
485
Julia Reynoldsbad42972017-04-25 13:52:49 -0400486 Notification.TvExtender tv = new Notification.TvExtender().setChannelId("foo");
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500487 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0,
Julia Reynoldsfea6f7b2017-04-19 13:50:12 -0400488 generateNotificationRecord(null, tv).getNotification(), 0);
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500489 verify(mRankingHelper, times(1)).getNotificationChannel(
Julia Reynolds5f20e9f2017-01-30 08:54:53 -0500490 anyString(), anyInt(), eq("foo"), anyBoolean());
491 }
492
493 @Test
Julia Reynolds5f20e9f2017-01-30 08:54:53 -0500494 public void testTvExtenderChannelOverride_notOnTv() throws Exception {
495 mNotificationManagerService.setIsTelevision(false);
496 mNotificationManagerService.setRankingHelper(mRankingHelper);
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500497 when(mRankingHelper.getNotificationChannel(
Julia Reynolds5f20e9f2017-01-30 08:54:53 -0500498 anyString(), anyInt(), anyString(), anyBoolean())).thenReturn(
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500499 mTestNotificationChannel);
Julia Reynolds5f20e9f2017-01-30 08:54:53 -0500500
Julia Reynoldsbad42972017-04-25 13:52:49 -0400501 Notification.TvExtender tv = new Notification.TvExtender().setChannelId("foo");
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500502 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0,
Julia Reynoldsfea6f7b2017-04-19 13:50:12 -0400503 generateNotificationRecord(null, tv).getNotification(), 0);
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500504 verify(mRankingHelper, times(1)).getNotificationChannel(
505 anyString(), anyInt(), eq(mTestNotificationChannel.getId()), anyBoolean());
Julia Reynolds5f20e9f2017-01-30 08:54:53 -0500506 }
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400507
508 @Test
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400509 public void testCreateChannelNotifyListener() throws Exception {
510 List<String> associations = new ArrayList<>();
511 associations.add("a");
512 when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations);
513 mNotificationManagerService.setRankingHelper(mRankingHelper);
514 when(mRankingHelper.getNotificationChannel(eq(PKG), anyInt(),
515 eq(mTestNotificationChannel.getId()), anyBoolean()))
516 .thenReturn(mTestNotificationChannel);
517 NotificationChannel channel2 = new NotificationChannel("a", "b", IMPORTANCE_LOW);
518 when(mRankingHelper.getNotificationChannel(eq(PKG), anyInt(),
519 eq(channel2.getId()), anyBoolean()))
520 .thenReturn(channel2);
521
522 reset(mNotificationListeners);
523 mBinderService.createNotificationChannels(PKG,
524 new ParceledListSlice(Arrays.asList(mTestNotificationChannel, channel2)));
525 verify(mNotificationListeners, times(1)).notifyNotificationChannelChanged(eq(PKG),
Julia Reynoldsf27d6b22017-04-13 15:48:16 -0400526 eq(Process.myUserHandle()), eq(mTestNotificationChannel),
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400527 eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_ADDED));
528 verify(mNotificationListeners, times(1)).notifyNotificationChannelChanged(eq(PKG),
Julia Reynoldsf27d6b22017-04-13 15:48:16 -0400529 eq(Process.myUserHandle()), eq(channel2),
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400530 eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_ADDED));
531 }
532
533 @Test
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400534 public void testCreateChannelGroupNotifyListener() throws Exception {
535 List<String> associations = new ArrayList<>();
536 associations.add("a");
537 when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations);
538 mNotificationManagerService.setRankingHelper(mRankingHelper);
539 NotificationChannelGroup group1 = new NotificationChannelGroup("a", "b");
540 NotificationChannelGroup group2 = new NotificationChannelGroup("n", "m");
541
542 reset(mNotificationListeners);
543 mBinderService.createNotificationChannelGroups(PKG,
544 new ParceledListSlice(Arrays.asList(group1, group2)));
545 verify(mNotificationListeners, times(1)).notifyNotificationChannelGroupChanged(eq(PKG),
Julia Reynoldsf27d6b22017-04-13 15:48:16 -0400546 eq(Process.myUserHandle()), eq(group1),
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400547 eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_ADDED));
548 verify(mNotificationListeners, times(1)).notifyNotificationChannelGroupChanged(eq(PKG),
Julia Reynoldsf27d6b22017-04-13 15:48:16 -0400549 eq(Process.myUserHandle()), eq(group2),
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400550 eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_ADDED));
551 }
552
553 @Test
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400554 public void testUpdateChannelNotifyListener() throws Exception {
555 List<String> associations = new ArrayList<>();
556 associations.add("a");
557 when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations);
558 mNotificationManagerService.setRankingHelper(mRankingHelper);
559 mTestNotificationChannel.setLightColor(Color.CYAN);
560 when(mRankingHelper.getNotificationChannel(eq(PKG), anyInt(),
561 eq(mTestNotificationChannel.getId()), anyBoolean()))
562 .thenReturn(mTestNotificationChannel);
563
564 reset(mNotificationListeners);
565 mBinderService.updateNotificationChannelForPackage(PKG, 0, mTestNotificationChannel);
566 verify(mNotificationListeners, times(1)).notifyNotificationChannelChanged(eq(PKG),
Julia Reynoldsf27d6b22017-04-13 15:48:16 -0400567 eq(Process.myUserHandle()), eq(mTestNotificationChannel),
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400568 eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_UPDATED));
569 }
570
571 @Test
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400572 public void testDeleteChannelNotifyListener() throws Exception {
573 List<String> associations = new ArrayList<>();
574 associations.add("a");
575 when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations);
576 mNotificationManagerService.setRankingHelper(mRankingHelper);
577 when(mRankingHelper.getNotificationChannel(eq(PKG), anyInt(),
578 eq(mTestNotificationChannel.getId()), anyBoolean()))
579 .thenReturn(mTestNotificationChannel);
580 reset(mNotificationListeners);
581 mBinderService.deleteNotificationChannel(PKG, mTestNotificationChannel.getId());
582 verify(mNotificationListeners, times(1)).notifyNotificationChannelChanged(eq(PKG),
Julia Reynoldsf27d6b22017-04-13 15:48:16 -0400583 eq(Process.myUserHandle()), eq(mTestNotificationChannel),
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400584 eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_DELETED));
585 }
586
587 @Test
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400588 public void testDeleteChannelGroupNotifyListener() throws Exception {
589 List<String> associations = new ArrayList<>();
590 associations.add("a");
591 when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations);
592 NotificationChannelGroup ncg = new NotificationChannelGroup("a", "b/c");
593 mNotificationManagerService.setRankingHelper(mRankingHelper);
594 when(mRankingHelper.getNotificationChannelGroup(eq(ncg.getId()), eq(PKG), anyInt()))
595 .thenReturn(ncg);
596 reset(mNotificationListeners);
597 mBinderService.deleteNotificationChannelGroup(PKG, ncg.getId());
598 verify(mNotificationListeners, times(1)).notifyNotificationChannelGroupChanged(eq(PKG),
Julia Reynoldsf27d6b22017-04-13 15:48:16 -0400599 eq(Process.myUserHandle()), eq(ncg),
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400600 eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_DELETED));
601 }
602
603 @Test
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400604 public void testUpdateNotificationChannelFromPrivilegedListener_success() throws Exception {
605 mNotificationManagerService.setRankingHelper(mRankingHelper);
606 List<String> associations = new ArrayList<>();
607 associations.add("a");
608 when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations);
609
610 mBinderService.updateNotificationChannelFromPrivilegedListener(
Julia Reynoldsf27d6b22017-04-13 15:48:16 -0400611 null, PKG, Process.myUserHandle(), mTestNotificationChannel);
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400612
613 verify(mRankingHelper, times(1)).updateNotificationChannel(anyString(), anyInt(), any());
614
615 verify(mNotificationListeners, never()).notifyNotificationChannelChanged(eq(PKG),
Julia Reynoldsf27d6b22017-04-13 15:48:16 -0400616 eq(Process.myUserHandle()), eq(mTestNotificationChannel),
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400617 eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_UPDATED));
618 }
619
620 @Test
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400621 public void testUpdateNotificationChannelFromPrivilegedListener_noAccess() throws Exception {
622 mNotificationManagerService.setRankingHelper(mRankingHelper);
623 List<String> associations = new ArrayList<>();
624 when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations);
625
626 try {
627 mBinderService.updateNotificationChannelFromPrivilegedListener(
Julia Reynoldsf27d6b22017-04-13 15:48:16 -0400628 null, PKG, Process.myUserHandle(), mTestNotificationChannel);
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400629 fail("listeners that don't have a companion device shouldn't be able to call this");
630 } catch (SecurityException e) {
631 // pass
632 }
633
634 verify(mRankingHelper, never()).updateNotificationChannel(anyString(), anyInt(), any());
635
636 verify(mNotificationListeners, never()).notifyNotificationChannelChanged(eq(PKG),
Julia Reynoldsf27d6b22017-04-13 15:48:16 -0400637 eq(Process.myUserHandle()), eq(mTestNotificationChannel),
638 eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_UPDATED));
639 }
640
641 @Test
Julia Reynoldsf27d6b22017-04-13 15:48:16 -0400642 public void testUpdateNotificationChannelFromPrivilegedListener_badUser() throws Exception {
643 mNotificationManagerService.setRankingHelper(mRankingHelper);
644 List<String> associations = new ArrayList<>();
645 associations.add("a");
646 when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations);
647 mListener = mock(ManagedServices.ManagedServiceInfo.class);
648 when(mListener.enabledAndUserMatches(anyInt())).thenReturn(false);
649 when(mNotificationListeners.checkServiceTokenLocked(any())).thenReturn(mListener);
650
651 try {
652 mBinderService.updateNotificationChannelFromPrivilegedListener(
653 null, PKG, UserHandle.ALL, mTestNotificationChannel);
654 fail("incorrectly allowed a change to a user listener cannot see");
655 } catch (SecurityException e) {
656 // pass
657 }
658
659 verify(mRankingHelper, never()).updateNotificationChannel(anyString(), anyInt(), any());
660
661 verify(mNotificationListeners, never()).notifyNotificationChannelChanged(eq(PKG),
662 eq(Process.myUserHandle()), eq(mTestNotificationChannel),
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400663 eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_UPDATED));
664 }
665
666 @Test
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400667 public void testGetNotificationChannelFromPrivilegedListener_success() throws Exception {
668 mNotificationManagerService.setRankingHelper(mRankingHelper);
669 List<String> associations = new ArrayList<>();
670 associations.add("a");
671 when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations);
672
Julia Reynoldsf27d6b22017-04-13 15:48:16 -0400673 mBinderService.getNotificationChannelsFromPrivilegedListener(
674 null, PKG, Process.myUserHandle());
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400675
676 verify(mRankingHelper, times(1)).getNotificationChannels(
677 anyString(), anyInt(), anyBoolean());
678 }
679
680 @Test
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400681 public void testGetNotificationChannelFromPrivilegedListener_noAccess() throws Exception {
682 mNotificationManagerService.setRankingHelper(mRankingHelper);
683 List<String> associations = new ArrayList<>();
684 when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations);
685
686 try {
Julia Reynoldsf27d6b22017-04-13 15:48:16 -0400687 mBinderService.getNotificationChannelsFromPrivilegedListener(
688 null, PKG, Process.myUserHandle());
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400689 fail("listeners that don't have a companion device shouldn't be able to call this");
690 } catch (SecurityException e) {
691 // pass
692 }
693
694 verify(mRankingHelper, never()).getNotificationChannels(
695 anyString(), anyInt(), anyBoolean());
696 }
697
698 @Test
Julia Reynoldsf27d6b22017-04-13 15:48:16 -0400699 public void testGetNotificationChannelFromPrivilegedListener_badUser() throws Exception {
700 mNotificationManagerService.setRankingHelper(mRankingHelper);
701 List<String> associations = new ArrayList<>();
702 associations.add("a");
703 when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations);
704 mListener = mock(ManagedServices.ManagedServiceInfo.class);
705 when(mListener.enabledAndUserMatches(anyInt())).thenReturn(false);
706 when(mNotificationListeners.checkServiceTokenLocked(any())).thenReturn(mListener);
707
708 try {
709 mBinderService.getNotificationChannelsFromPrivilegedListener(
710 null, PKG, Process.myUserHandle());
711 fail("listener getting channels from a user they cannot see");
712 } catch (SecurityException e) {
713 // pass
714 }
715
716 verify(mRankingHelper, never()).getNotificationChannels(
717 anyString(), anyInt(), anyBoolean());
718 }
719
720 @Test
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400721 public void testGetNotificationChannelGroupsFromPrivilegedListener_success() throws Exception {
722 mNotificationManagerService.setRankingHelper(mRankingHelper);
723 List<String> associations = new ArrayList<>();
724 associations.add("a");
725 when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations);
726
Julia Reynoldsf27d6b22017-04-13 15:48:16 -0400727 mBinderService.getNotificationChannelGroupsFromPrivilegedListener(
728 null, PKG, Process.myUserHandle());
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400729
730 verify(mRankingHelper, times(1)).getNotificationChannelGroups(anyString(), anyInt());
731 }
732
733 @Test
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400734 public void testGetNotificationChannelGroupsFromPrivilegedListener_noAccess() throws Exception {
735 mNotificationManagerService.setRankingHelper(mRankingHelper);
736 List<String> associations = new ArrayList<>();
737 when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations);
738
739 try {
Julia Reynoldsf27d6b22017-04-13 15:48:16 -0400740 mBinderService.getNotificationChannelGroupsFromPrivilegedListener(
741 null, PKG, Process.myUserHandle());
742 fail("listeners that don't have a companion device shouldn't be able to call this");
743 } catch (SecurityException e) {
744 // pass
745 }
746
747 verify(mRankingHelper, never()).getNotificationChannelGroups(anyString(), anyInt());
748 }
749
750 @Test
Julia Reynoldsf27d6b22017-04-13 15:48:16 -0400751 public void testGetNotificationChannelGroupsFromPrivilegedListener_badUser() throws Exception {
752 mNotificationManagerService.setRankingHelper(mRankingHelper);
753 List<String> associations = new ArrayList<>();
754 when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations);
755 mListener = mock(ManagedServices.ManagedServiceInfo.class);
756 when(mListener.enabledAndUserMatches(anyInt())).thenReturn(false);
757 when(mNotificationListeners.checkServiceTokenLocked(any())).thenReturn(mListener);
758
759 try {
760 mBinderService.getNotificationChannelGroupsFromPrivilegedListener(
761 null, PKG, Process.myUserHandle());
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400762 fail("listeners that don't have a companion device shouldn't be able to call this");
763 } catch (SecurityException e) {
764 // pass
765 }
766
767 verify(mRankingHelper, never()).getNotificationChannelGroups(anyString(), anyInt());
768 }
Julia Reynoldsda781472017-04-12 09:41:16 -0400769
770 @Test
Julia Reynoldsda781472017-04-12 09:41:16 -0400771 public void testHasCompanionDevice_failure() throws Exception {
772 when(mCompanionMgr.getAssociations(anyString(), anyInt())).thenThrow(
773 new IllegalArgumentException());
774 mNotificationManagerService.hasCompanionDevice(mListener);
775 }
Julia Reynolds727a7282017-04-13 10:54:01 -0400776
777 @Test
Julia Reynolds727a7282017-04-13 10:54:01 -0400778 public void testHasCompanionDevice_noService() throws Exception {
779 mNotificationManagerService = new TestableNotificationManagerService(mContext);
780
781 assertFalse(mNotificationManagerService.hasCompanionDevice(mListener));
782 }
783
Julia Reynoldsa78cdff2017-04-26 10:19:25 -0400784 @Test
785 public void testSnoozeRunnable_snoozeNonGrouped() throws Exception {
786 final NotificationRecord nonGrouped = generateNotificationRecord(
787 mTestNotificationChannel, 1, null, false);
788 final NotificationRecord grouped = generateNotificationRecord(
789 mTestNotificationChannel, 2, "group", false);
790 mNotificationManagerService.addNotification(grouped);
791 mNotificationManagerService.addNotification(nonGrouped);
792
793 NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
794 mNotificationManagerService.new SnoozeNotificationRunnable(
795 nonGrouped.getKey(), 100, null);
796 snoozeNotificationRunnable.run();
797
798 // only snooze the one notification
799 verify(mSnoozeHelper, times(1)).snooze(any(NotificationRecord.class), anyLong());
800 }
801
802 @Test
803 public void testSnoozeRunnable_snoozeSummary_withChildren() throws Exception {
804 final NotificationRecord parent = generateNotificationRecord(
805 mTestNotificationChannel, 1, "group", true);
806 final NotificationRecord child = generateNotificationRecord(
807 mTestNotificationChannel, 2, "group", false);
808 final NotificationRecord child2 = generateNotificationRecord(
809 mTestNotificationChannel, 3, "group", false);
810 mNotificationManagerService.addNotification(parent);
811 mNotificationManagerService.addNotification(child);
812 mNotificationManagerService.addNotification(child2);
813
814 NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
815 mNotificationManagerService.new SnoozeNotificationRunnable(
816 parent.getKey(), 100, null);
817 snoozeNotificationRunnable.run();
818
819 // snooze parent and children
820 verify(mSnoozeHelper, times(3)).snooze(any(NotificationRecord.class), anyLong());
821 }
822
823 @Test
824 public void testSnoozeRunnable_snoozeGroupChild_fellowChildren() throws Exception {
825 final NotificationRecord parent = generateNotificationRecord(
826 mTestNotificationChannel, 1, "group", true);
827 final NotificationRecord child = generateNotificationRecord(
828 mTestNotificationChannel, 2, "group", false);
829 final NotificationRecord child2 = generateNotificationRecord(
830 mTestNotificationChannel, 3, "group", false);
831 mNotificationManagerService.addNotification(parent);
832 mNotificationManagerService.addNotification(child);
833 mNotificationManagerService.addNotification(child2);
834
835 NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
836 mNotificationManagerService.new SnoozeNotificationRunnable(
837 child2.getKey(), 100, null);
838 snoozeNotificationRunnable.run();
839
840 // only snooze the one child
841 verify(mSnoozeHelper, times(1)).snooze(any(NotificationRecord.class), anyLong());
842 }
843
844 @Test
845 public void testSnoozeRunnable_snoozeGroupChild_onlyChildOfSummary() throws Exception {
846 final NotificationRecord parent = generateNotificationRecord(
847 mTestNotificationChannel, 1, "group", true);
848 assertTrue(parent.sbn.getNotification().isGroupSummary());
849 final NotificationRecord child = generateNotificationRecord(
850 mTestNotificationChannel, 2, "group", false);
851 mNotificationManagerService.addNotification(parent);
852 mNotificationManagerService.addNotification(child);
853
854 NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
855 mNotificationManagerService.new SnoozeNotificationRunnable(
856 child.getKey(), 100, null);
857 snoozeNotificationRunnable.run();
858
859 // snooze child and summary
860 verify(mSnoozeHelper, times(2)).snooze(any(NotificationRecord.class), anyLong());
861 }
862
863 @Test
864 public void testSnoozeRunnable_snoozeGroupChild_noOthersInGroup() throws Exception {
865 final NotificationRecord child = generateNotificationRecord(
866 mTestNotificationChannel, 2, "group", false);
867 mNotificationManagerService.addNotification(child);
868
869 NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
870 mNotificationManagerService.new SnoozeNotificationRunnable(
871 child.getKey(), 100, null);
872 snoozeNotificationRunnable.run();
873
874 // snooze child only
875 verify(mSnoozeHelper, times(1)).snooze(any(NotificationRecord.class), anyLong());
876 }
877
878 @Test
879 public void testPostGroupChild_unsnoozeParent() throws Exception {
880 final NotificationRecord child = generateNotificationRecord(
881 mTestNotificationChannel, 2, "group", false);
882
883 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", null,
884 child.sbn.getId(), child.sbn.getNotification(), child.sbn.getUserId());
885 waitForIdle();
886
887 verify(mSnoozeHelper, times(1)).repostGroupSummary(
888 anyString(), anyInt(), eq(child.getGroupKey()));
889 }
890
891 @Test
892 public void testPostNonGroup_noUnsnoozing() throws Exception {
893 final NotificationRecord record = generateNotificationRecord(
894 mTestNotificationChannel, 2, null, false);
895
896 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", null,
897 record.sbn.getId(), record.sbn.getNotification(), record.sbn.getUserId());
898 waitForIdle();
899
900 verify(mSnoozeHelper, never()).repostGroupSummary(anyString(), anyInt(), anyString());
901 }
902
903 @Test
904 public void testPostGroupSummary_noUnsnoozing() throws Exception {
905 final NotificationRecord parent = generateNotificationRecord(
906 mTestNotificationChannel, 2, "group", true);
907
908 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", null,
909 parent.sbn.getId(), parent.sbn.getNotification(), parent.sbn.getUserId());
910 waitForIdle();
911
912 verify(mSnoozeHelper, never()).repostGroupSummary(anyString(), anyInt(), anyString());
913 }
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -0500914}