blob: bbc03c3aa8b7f7bfb00dc70adb18be944e7e001b [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;
Julia Reynolds4da79702017-06-01 11:06:10 -040020import static android.app.NotificationManager.IMPORTANCE_NONE;
Julia Reynolds4db59552017-06-30 13:34:01 -040021import static android.content.pm.PackageManager.PERMISSION_DENIED;
Julia Reynolds73ed76b2017-04-04 17:04:38 -040022
Julia Reynoldseb3dca72017-07-11 10:39:58 -040023import static com.android.server.notification.NotificationManagerService
24 .MESSAGE_SEND_RANKING_UPDATE;
25
Geoffrey Pitsch03533712017-01-05 10:30:07 -050026import static junit.framework.Assert.assertEquals;
Julia Reynolds727a7282017-04-13 10:54:01 -040027import static junit.framework.Assert.assertFalse;
Julia Reynoldsbaff4002016-12-15 11:34:26 -050028import static junit.framework.Assert.assertTrue;
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -050029import static junit.framework.Assert.fail;
Julia Reynoldsbaff4002016-12-15 11:34:26 -050030
Julia Reynolds5f20e9f2017-01-30 08:54:53 -050031import static org.mockito.Matchers.anyBoolean;
Julia Reynoldsa78cdff2017-04-26 10:19:25 -040032import static org.mockito.Matchers.anyLong;
Julia Reynoldsbaff4002016-12-15 11:34:26 -050033import static org.mockito.Matchers.anyString;
34import static org.mockito.Matchers.eq;
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -050035import static org.mockito.Mockito.any;
36import static org.mockito.Mockito.anyInt;
Julia Reynoldseb3dca72017-07-11 10:39:58 -040037import static org.mockito.Mockito.atLeastOnce;
38import static org.mockito.Mockito.doAnswer;
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -050039import static org.mockito.Mockito.mock;
Julia Reynolds73ed76b2017-04-04 17:04:38 -040040import static org.mockito.Mockito.never;
41import static org.mockito.Mockito.reset;
Julia Reynoldseb3dca72017-07-11 10:39:58 -040042import static org.mockito.Mockito.spy;
Julia Reynoldsbaff4002016-12-15 11:34:26 -050043import static org.mockito.Mockito.times;
44import static org.mockito.Mockito.verify;
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -050045import static org.mockito.Mockito.when;
46
Julia Reynolds68263d12017-06-21 14:21:19 -040047import android.app.ActivityManager;
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -050048import android.app.INotificationManager;
Julia Reynoldsbaff4002016-12-15 11:34:26 -050049import android.app.Notification;
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -050050import android.app.NotificationChannel;
Julia Reynolds73ed76b2017-04-04 17:04:38 -040051import android.app.NotificationChannelGroup;
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -050052import android.app.NotificationManager;
Julia Reynolds73ed76b2017-04-04 17:04:38 -040053import android.companion.ICompanionDeviceManager;
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -050054import android.content.ComponentName;
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -050055import android.content.Context;
56import android.content.pm.ApplicationInfo;
57import android.content.pm.IPackageManager;
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -050058import android.content.pm.PackageManager;
Geoffrey Pitsch03533712017-01-05 10:30:07 -050059import android.content.pm.ParceledListSlice;
Julia Reynolds73ed76b2017-04-04 17:04:38 -040060import android.graphics.Color;
Julia Reynolds76c096d2017-06-19 08:16:04 -040061import android.media.AudioManager;
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -050062import android.os.Binder;
Julia Reynoldseb3dca72017-07-11 10:39:58 -040063import android.os.Handler;
64import android.os.Looper;
65import android.os.Message;
Julia Reynoldsf27d6b22017-04-13 15:48:16 -040066import android.os.Process;
Julia Reynoldsbaff4002016-12-15 11:34:26 -050067import android.os.UserHandle;
Chris Wren89aa2262017-05-05 18:05:56 -040068import android.provider.Settings.Secure;
Julia Reynolds73ed76b2017-04-04 17:04:38 -040069import android.service.notification.NotificationListenerService;
Julia Reynoldsbaff4002016-12-15 11:34:26 -050070import android.service.notification.StatusBarNotification;
Geoffrey Pitsch8185d382017-05-19 18:41:32 -040071import android.test.suitebuilder.annotation.SmallTest;
Jason Monk745d0a82017-04-17 11:34:22 -040072import android.testing.AndroidTestingRunner;
Geoffrey Pitsch415e4542017-04-10 13:12:58 -040073import android.testing.TestableLooper;
Jason Monk745d0a82017-04-17 11:34:22 -040074import android.testing.TestableLooper.RunWithLooper;
Julia Reynoldseb3dca72017-07-11 10:39:58 -040075import android.util.ArrayMap;
Julia Reynoldsb852e562017-06-06 16:14:18 -040076import android.util.AtomicFile;
Julia Reynoldseb3dca72017-07-11 10:39:58 -040077import android.util.Log;
78import android.util.Slog;
79
80import com.android.server.lights.Light;
81import com.android.server.lights.LightsManager;
82
83import org.junit.After;
84import org.junit.Before;
85import org.junit.Test;
86import org.junit.runner.RunWith;
87import org.mockito.ArgumentCaptor;
88import org.mockito.Mock;
89import org.mockito.MockitoAnnotations;
90import org.mockito.stubbing.Answer;
Julia Reynolds5f20e9f2017-01-30 08:54:53 -050091
Julia Reynoldsb852e562017-06-06 16:14:18 -040092import java.io.File;
93import java.io.FileInputStream;
94import java.io.FileOutputStream;
Julia Reynolds73ed76b2017-04-04 17:04:38 -040095import java.util.ArrayList;
Geoffrey Pitsch03533712017-01-05 10:30:07 -050096import java.util.Arrays;
Julia Reynolds73ed76b2017-04-04 17:04:38 -040097import java.util.List;
Julia Reynoldseb3dca72017-07-11 10:39:58 -040098import java.util.Map;
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -050099
Geoffrey Pitsch8185d382017-05-19 18:41:32 -0400100@SmallTest
Jason Monk745d0a82017-04-17 11:34:22 -0400101@RunWith(AndroidTestingRunner.class)
102@RunWithLooper
Chris Wren89aa2262017-05-05 18:05:56 -0400103public class NotificationManagerServiceTest extends NotificationTestCase {
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500104 private static final String TEST_CHANNEL_ID = "NotificationManagerServiceTestChannelId";
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500105 private final int uid = Binder.getCallingUid();
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -0500106 private NotificationManagerService mNotificationManagerService;
107 private INotificationManager mBinderService;
Geoffrey Pitsch415e4542017-04-10 13:12:58 -0400108 private NotificationManagerInternal mInternalService;
Julia Reynoldsda781472017-04-12 09:41:16 -0400109 @Mock
110 private IPackageManager mPackageManager;
111 @Mock
112 private PackageManager mPackageManagerClient;
Chris Wren89aa2262017-05-05 18:05:56 -0400113 private Context mContext = getContext();
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500114 private final String PKG = mContext.getPackageName();
Geoffrey Pitsch415e4542017-04-10 13:12:58 -0400115 private TestableLooper mTestableLooper;
Julia Reynoldsda781472017-04-12 09:41:16 -0400116 @Mock
117 private RankingHelper mRankingHelper;
Geoffrey Pitschd5bcf212017-06-01 15:45:35 -0400118 @Mock
Julia Reynoldsb852e562017-06-06 16:14:18 -0400119 AtomicFile mPolicyFile;
120 File mFile;
121 @Mock
Geoffrey Pitschd5bcf212017-06-01 15:45:35 -0400122 private NotificationUsageStats mUsageStats;
Julia Reynolds76c096d2017-06-19 08:16:04 -0400123 @Mock
124 private AudioManager mAudioManager;
Julia Reynolds68263d12017-06-21 14:21:19 -0400125 @Mock
126 ActivityManager mActivityManager;
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400127 NotificationManagerService.WorkerHandler mHandler;
Julia Reynolds3ff26d22017-06-19 08:16:04 -0400128
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500129 private NotificationChannel mTestNotificationChannel = new NotificationChannel(
130 TEST_CHANNEL_ID, TEST_CHANNEL_ID, NotificationManager.IMPORTANCE_DEFAULT);
Julia Reynoldsda781472017-04-12 09:41:16 -0400131 @Mock
132 private NotificationManagerService.NotificationListeners mNotificationListeners;
Julia Reynoldsb852e562017-06-06 16:14:18 -0400133 @Mock private NotificationManagerService.NotificationAssistants mNotificationAssistants;
134 @Mock private ConditionProviders mConditionProviders;
Julia Reynoldsda781472017-04-12 09:41:16 -0400135 private ManagedServices.ManagedServiceInfo mListener;
136 @Mock private ICompanionDeviceManager mCompanionMgr;
Julia Reynoldsa78cdff2017-04-26 10:19:25 -0400137 @Mock SnoozeHelper mSnoozeHelper;
Julia Reynolds8aebf352017-06-26 11:35:33 -0400138 @Mock GroupHelper mGroupHelper;
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -0500139
Geoffrey Pitsch415e4542017-04-10 13:12:58 -0400140 // Use a Testable subclass so we can simulate calls from the system without failing.
141 private static class TestableNotificationManagerService extends NotificationManagerService {
142 public TestableNotificationManagerService(Context context) { super(context); }
143
144 @Override
Geoffrey Pitsch27684152017-05-02 11:41:31 -0400145 protected boolean isCallingUidSystem() {
146 return true;
147 }
148
149 @Override
150 protected boolean isCallerSystemOrPhone() {
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400151 return true;
152 }
Julia Reynolds727a7282017-04-13 10:54:01 -0400153
154 @Override
155 protected ICompanionDeviceManager getCompanionManager() {
156 return null;
157 }
Geoffrey Pitsch415e4542017-04-10 13:12:58 -0400158 }
159
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -0500160 @Before
161 public void setUp() throws Exception {
Julia Reynoldsda781472017-04-12 09:41:16 -0400162 MockitoAnnotations.initMocks(this);
Chris Wren89aa2262017-05-05 18:05:56 -0400163
164 // most tests assume badging is enabled
165 Secure.putIntForUser(getContext().getContentResolver(),
166 Secure.NOTIFICATION_BADGING, 1,
167 UserHandle.getUserHandleForUid(uid).getIdentifier());
168
Geoffrey Pitsch415e4542017-04-10 13:12:58 -0400169 mNotificationManagerService = new TestableNotificationManagerService(mContext);
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -0500170
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400171 // Use this testable looper.
172 mTestableLooper = TestableLooper.get(this);
173 mHandler = mNotificationManagerService.new WorkerHandler(mTestableLooper.getLooper());
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -0500174 // MockPackageManager - default returns ApplicationInfo with matching calling UID
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -0500175 final ApplicationInfo applicationInfo = new ApplicationInfo();
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500176 applicationInfo.uid = uid;
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400177 when(mPackageManager.getApplicationInfo(anyString(), anyInt(), anyInt()))
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -0500178 .thenReturn(applicationInfo);
Julia Reynolds5f20e9f2017-01-30 08:54:53 -0500179 when(mPackageManagerClient.getApplicationInfoAsUser(anyString(), anyInt(), anyInt()))
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500180 .thenReturn(applicationInfo);
Geoffrey Pitsch03533712017-01-05 10:30:07 -0500181 final LightsManager mockLightsManager = mock(LightsManager.class);
182 when(mockLightsManager.getLight(anyInt())).thenReturn(mock(Light.class));
Julia Reynolds76c096d2017-06-19 08:16:04 -0400183 when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_NORMAL);
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500184
Julia Reynoldsb852e562017-06-06 16:14:18 -0400185 mFile = new File(mContext.getCacheDir(), "test.xml");
186 mFile.createNewFile();
187 when(mPolicyFile.openRead()).thenReturn(new FileInputStream(mFile));
188 when(mPolicyFile.startWrite()).thenReturn(new FileOutputStream(mFile));
189
Julia Reynoldsda781472017-04-12 09:41:16 -0400190 mListener = mNotificationListeners.new ManagedServiceInfo(
191 null, new ComponentName(PKG, "test_class"), uid, true, null, 0);
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400192 when(mNotificationListeners.checkServiceTokenLocked(any())).thenReturn(mListener);
Julia Reynoldsb852e562017-06-06 16:14:18 -0400193 try {
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400194 mNotificationManagerService.init(mTestableLooper.getLooper(),
195 mPackageManager, mPackageManagerClient, mockLightsManager,
196 mNotificationListeners, mNotificationAssistants, mConditionProviders,
197 mCompanionMgr, mSnoozeHelper, mUsageStats, mPolicyFile, mActivityManager,
198 mGroupHelper);
Julia Reynoldsb852e562017-06-06 16:14:18 -0400199 } catch (SecurityException e) {
200 if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) {
201 throw e;
202 }
203 }
Julia Reynolds76c096d2017-06-19 08:16:04 -0400204 mNotificationManagerService.setAudioManager(mAudioManager);
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -0500205
206 // Tests call directly into the Binder.
207 mBinderService = mNotificationManagerService.getBinderService();
Geoffrey Pitsch415e4542017-04-10 13:12:58 -0400208 mInternalService = mNotificationManagerService.getInternalService();
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500209
210 mBinderService.createNotificationChannels(
211 PKG, new ParceledListSlice(Arrays.asList(mTestNotificationChannel)));
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -0500212 }
213
Julia Reynoldsb852e562017-06-06 16:14:18 -0400214 @After
215 public void tearDown() throws Exception {
216 mFile.delete();
217 }
218
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500219 public void waitForIdle() throws Exception {
Geoffrey Pitsch415e4542017-04-10 13:12:58 -0400220 mTestableLooper.processAllMessages();
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500221 }
222
Julia Reynoldsa78cdff2017-04-26 10:19:25 -0400223 private NotificationRecord generateNotificationRecord(NotificationChannel channel, int id,
224 String groupKey, boolean isSummary) {
225 Notification.Builder nb = new Notification.Builder(mContext, channel.getId())
226 .setContentTitle("foo")
227 .setSmallIcon(android.R.drawable.sym_def_app_icon)
228 .setGroup(groupKey)
229 .setGroupSummary(isSummary);
230
231 StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, id, "tag", uid, 0,
232 nb.build(), new UserHandle(uid), null, 0);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000233 return new NotificationRecord(mContext, sbn, channel);
Julia Reynoldsa78cdff2017-04-26 10:19:25 -0400234 }
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500235 private NotificationRecord generateNotificationRecord(NotificationChannel channel) {
Julia Reynolds5f20e9f2017-01-30 08:54:53 -0500236 return generateNotificationRecord(channel, null);
237 }
238
239 private NotificationRecord generateNotificationRecord(NotificationChannel channel,
240 Notification.TvExtender extender) {
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500241 if (channel == null) {
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500242 channel = mTestNotificationChannel;
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500243 }
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500244 Notification.Builder nb = new Notification.Builder(mContext, channel.getId())
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500245 .setContentTitle("foo")
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500246 .setSmallIcon(android.R.drawable.sym_def_app_icon);
Julia Reynolds5f20e9f2017-01-30 08:54:53 -0500247 if (extender != null) {
248 nb.extend(extender);
249 }
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500250 StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, "tag", uid, 0,
Julia Reynolds5f20e9f2017-01-30 08:54:53 -0500251 nb.build(), new UserHandle(uid), null, 0);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000252 return new NotificationRecord(mContext, sbn, channel);
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500253 }
254
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400255 private Map<String, Answer> getSignalExtractorSideEffects() {
256 Map<String, Answer> answers = new ArrayMap<>();
257
258 answers.put("override group key", invocationOnMock -> {
259 ((NotificationRecord) invocationOnMock.getArguments()[0])
260 .setOverrideGroupKey("bananas");
261 return null;
262 });
263 answers.put("override people", invocationOnMock -> {
264 ((NotificationRecord) invocationOnMock.getArguments()[0])
265 .setPeopleOverride(new ArrayList<>());
266 return null;
267 });
268 answers.put("snooze criteria", invocationOnMock -> {
269 ((NotificationRecord) invocationOnMock.getArguments()[0])
270 .setSnoozeCriteria(new ArrayList<>());
271 return null;
272 });
273 answers.put("notification channel", invocationOnMock -> {
274 ((NotificationRecord) invocationOnMock.getArguments()[0])
275 .updateNotificationChannel(new NotificationChannel("a", "", IMPORTANCE_LOW));
276 return null;
277 });
278 answers.put("badging", invocationOnMock -> {
279 NotificationRecord r = (NotificationRecord) invocationOnMock.getArguments()[0];
280 r.setShowBadge(!r.canShowBadge());
281 return null;
282 });
283 answers.put("package visibility", invocationOnMock -> {
284 ((NotificationRecord) invocationOnMock.getArguments()[0]).setPackageVisibilityOverride(
285 Notification.VISIBILITY_SECRET);
286 return null;
287 });
288
289 return answers;
290 }
291
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500292 @Test
Geoffrey Pitsch03533712017-01-05 10:30:07 -0500293 public void testCreateNotificationChannels_SingleChannel() throws Exception {
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -0500294 final NotificationChannel channel =
295 new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_DEFAULT);
Geoffrey Pitsch03533712017-01-05 10:30:07 -0500296 mBinderService.createNotificationChannels("test_pkg",
297 new ParceledListSlice(Arrays.asList(channel)));
298 final NotificationChannel createdChannel =
299 mBinderService.getNotificationChannel("test_pkg", "id");
300 assertTrue(createdChannel != null);
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -0500301 }
302
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500303 @Test
Geoffrey Pitsch03533712017-01-05 10:30:07 -0500304 public void testCreateNotificationChannels_NullChannelThrowsException() throws Exception {
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -0500305 try {
Geoffrey Pitsch03533712017-01-05 10:30:07 -0500306 mBinderService.createNotificationChannels("test_pkg",
307 new ParceledListSlice(Arrays.asList(null)));
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -0500308 fail("Exception should be thrown immediately.");
309 } catch (NullPointerException e) {
310 // pass
311 }
312 }
Julia Reynoldsbaff4002016-12-15 11:34:26 -0500313
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500314 @Test
Geoffrey Pitsch03533712017-01-05 10:30:07 -0500315 public void testCreateNotificationChannels_TwoChannels() throws Exception {
316 final NotificationChannel channel1 =
317 new NotificationChannel("id1", "name", NotificationManager.IMPORTANCE_DEFAULT);
318 final NotificationChannel channel2 =
319 new NotificationChannel("id2", "name", NotificationManager.IMPORTANCE_DEFAULT);
320 mBinderService.createNotificationChannels("test_pkg",
321 new ParceledListSlice(Arrays.asList(channel1, channel2)));
322 assertTrue(mBinderService.getNotificationChannel("test_pkg", "id1") != null);
323 assertTrue(mBinderService.getNotificationChannel("test_pkg", "id2") != null);
324 }
325
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500326 @Test
Geoffrey Pitsch03533712017-01-05 10:30:07 -0500327 public void testCreateNotificationChannels_SecondCreateDoesNotChangeImportance()
328 throws Exception {
329 final NotificationChannel channel =
330 new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_DEFAULT);
331 mBinderService.createNotificationChannels("test_pkg",
332 new ParceledListSlice(Arrays.asList(channel)));
333
334 // Recreating the channel doesn't throw, but ignores importance.
335 final NotificationChannel dupeChannel =
336 new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_HIGH);
337 mBinderService.createNotificationChannels("test_pkg",
338 new ParceledListSlice(Arrays.asList(dupeChannel)));
339 final NotificationChannel createdChannel =
340 mBinderService.getNotificationChannel("test_pkg", "id");
341 assertEquals(NotificationManager.IMPORTANCE_DEFAULT, createdChannel.getImportance());
342 }
343
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500344 @Test
Geoffrey Pitsch03533712017-01-05 10:30:07 -0500345 public void testCreateNotificationChannels_IdenticalChannelsInListIgnoresSecond()
346 throws Exception {
347 final NotificationChannel channel1 =
348 new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_DEFAULT);
349 final NotificationChannel channel2 =
350 new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_HIGH);
351 mBinderService.createNotificationChannels("test_pkg",
352 new ParceledListSlice(Arrays.asList(channel1, channel2)));
353 final NotificationChannel createdChannel =
354 mBinderService.getNotificationChannel("test_pkg", "id");
355 assertEquals(NotificationManager.IMPORTANCE_DEFAULT, createdChannel.getImportance());
356 }
357
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500358 @Test
Julia Reynoldsbaff4002016-12-15 11:34:26 -0500359 public void testBlockedNotifications_suspended() throws Exception {
Julia Reynoldsbaff4002016-12-15 11:34:26 -0500360 when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(true);
361
362 NotificationChannel channel = new NotificationChannel("id", "name",
363 NotificationManager.IMPORTANCE_HIGH);
364 NotificationRecord r = generateNotificationRecord(channel);
Geoffrey Pitschd5bcf212017-06-01 15:45:35 -0400365 assertTrue(mNotificationManagerService.isBlocked(r, mUsageStats));
366 verify(mUsageStats, times(1)).registerSuspendedByAdmin(eq(r));
Julia Reynoldsbaff4002016-12-15 11:34:26 -0500367 }
368
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500369 @Test
Julia Reynoldsbaff4002016-12-15 11:34:26 -0500370 public void testBlockedNotifications_blockedChannel() throws Exception {
Julia Reynoldsbaff4002016-12-15 11:34:26 -0500371 when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false);
372
373 NotificationChannel channel = new NotificationChannel("id", "name",
374 NotificationManager.IMPORTANCE_HIGH);
Julia Reynolds4db59552017-06-30 13:34:01 -0400375 channel.setImportance(IMPORTANCE_NONE);
Julia Reynoldsbaff4002016-12-15 11:34:26 -0500376 NotificationRecord r = generateNotificationRecord(channel);
Geoffrey Pitschd5bcf212017-06-01 15:45:35 -0400377 assertTrue(mNotificationManagerService.isBlocked(r, mUsageStats));
378 verify(mUsageStats, times(1)).registerBlocked(eq(r));
Julia Reynoldsbaff4002016-12-15 11:34:26 -0500379 }
380
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500381 @Test
Julia Reynolds4da79702017-06-01 11:06:10 -0400382 public void testEnqueuedBlockedNotifications_blockedApp() throws Exception {
Julia Reynoldsbaff4002016-12-15 11:34:26 -0500383 when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false);
384
Julia Reynolds4da79702017-06-01 11:06:10 -0400385 mBinderService.setNotificationsEnabledForPackage(PKG, uid, false);
386
387 final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
388 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
389 sbn.getId(), sbn.getNotification(), sbn.getUserId());
390 waitForIdle();
391 assertEquals(0, mBinderService.getActiveNotifications(sbn.getPackageName()).length);
Julia Reynoldsbaff4002016-12-15 11:34:26 -0500392 }
393
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500394 @Test
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500395 public void testEnqueueNotificationWithTag_PopulatesGetActiveNotifications() throws Exception {
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500396 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0,
Julia Reynoldsfea6f7b2017-04-19 13:50:12 -0400397 generateNotificationRecord(null).getNotification(), 0);
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500398 waitForIdle();
Julia Reynolds080361e2017-07-13 11:23:12 -0400399 StatusBarNotification[] notifs = mBinderService.getActiveNotifications(PKG);
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500400 assertEquals(1, notifs.length);
Julia Reynolds080361e2017-07-13 11:23:12 -0400401 assertEquals(1, mNotificationManagerService.getNotificationRecordCount());
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500402 }
403
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500404 @Test
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500405 public void testCancelNotificationImmediatelyAfterEnqueue() throws Exception {
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500406 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0,
Julia Reynoldsfea6f7b2017-04-19 13:50:12 -0400407 generateNotificationRecord(null).getNotification(), 0);
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500408 mBinderService.cancelNotificationWithTag(PKG, "tag", 0, 0);
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500409 waitForIdle();
410 StatusBarNotification[] notifs =
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500411 mBinderService.getActiveNotifications(PKG);
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500412 assertEquals(0, notifs.length);
Julia Reynolds080361e2017-07-13 11:23:12 -0400413 assertEquals(0, mNotificationManagerService.getNotificationRecordCount());
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500414 }
415
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500416 @Test
Geoffrey Pitschccc0b972017-02-15 10:52:26 -0500417 public void testCancelNotificationWhilePostedAndEnqueued() throws Exception {
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500418 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0,
Julia Reynoldsfea6f7b2017-04-19 13:50:12 -0400419 generateNotificationRecord(null).getNotification(), 0);
Geoffrey Pitschccc0b972017-02-15 10:52:26 -0500420 waitForIdle();
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500421 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0,
Julia Reynoldsfea6f7b2017-04-19 13:50:12 -0400422 generateNotificationRecord(null).getNotification(), 0);
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500423 mBinderService.cancelNotificationWithTag(PKG, "tag", 0, 0);
Geoffrey Pitschccc0b972017-02-15 10:52:26 -0500424 waitForIdle();
425 StatusBarNotification[] notifs =
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500426 mBinderService.getActiveNotifications(PKG);
Geoffrey Pitschccc0b972017-02-15 10:52:26 -0500427 assertEquals(0, notifs.length);
Julia Reynolds080361e2017-07-13 11:23:12 -0400428 assertEquals(0, mNotificationManagerService.getNotificationRecordCount());
Geoffrey Pitschccc0b972017-02-15 10:52:26 -0500429 }
430
431 @Test
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500432 public void testCancelNotificationsFromListenerImmediatelyAfterEnqueue() throws Exception {
433 final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500434 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
Julia Reynoldsfea6f7b2017-04-19 13:50:12 -0400435 sbn.getId(), sbn.getNotification(), sbn.getUserId());
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500436 mBinderService.cancelNotificationsFromListener(null, null);
437 waitForIdle();
438 StatusBarNotification[] notifs =
439 mBinderService.getActiveNotifications(sbn.getPackageName());
440 assertEquals(0, notifs.length);
Julia Reynolds080361e2017-07-13 11:23:12 -0400441 assertEquals(0, mNotificationManagerService.getNotificationRecordCount());
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500442 }
443
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500444 @Test
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500445 public void testCancelAllNotificationsImmediatelyAfterEnqueue() throws Exception {
446 final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500447 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
Julia Reynoldsfea6f7b2017-04-19 13:50:12 -0400448 sbn.getId(), sbn.getNotification(), sbn.getUserId());
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500449 mBinderService.cancelAllNotifications(PKG, sbn.getUserId());
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500450 waitForIdle();
451 StatusBarNotification[] notifs =
452 mBinderService.getActiveNotifications(sbn.getPackageName());
453 assertEquals(0, notifs.length);
Julia Reynolds080361e2017-07-13 11:23:12 -0400454 assertEquals(0, mNotificationManagerService.getNotificationRecordCount());
455 }
456
457 @Test
458 public void testUserInitiatedClearAll_noLeak() throws Exception {
459 final NotificationRecord n = generateNotificationRecord(
460 mTestNotificationChannel, 1, "group", true);
461
462 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
463 n.sbn.getId(), n.sbn.getNotification(), n.sbn.getUserId());
464 waitForIdle();
465
466 mNotificationManagerService.mNotificationDelegate.onClearAll(uid, Binder.getCallingPid(),
467 n.getUserId());
468 waitForIdle();
469 StatusBarNotification[] notifs =
470 mBinderService.getActiveNotifications(n.sbn.getPackageName());
471 assertEquals(0, notifs.length);
472 assertEquals(0, mNotificationManagerService.getNotificationRecordCount());
473 }
474
475 @Test
476 public void testCancelAllNotificationsCancelsChildren() throws Exception {
477 final NotificationRecord parent = generateNotificationRecord(
478 mTestNotificationChannel, 1, "group1", true);
479 final NotificationRecord child = generateNotificationRecord(
480 mTestNotificationChannel, 2, "group1", false);
481
482 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
483 parent.sbn.getId(), parent.sbn.getNotification(), parent.sbn.getUserId());
484 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
485 child.sbn.getId(), child.sbn.getNotification(), child.sbn.getUserId());
486 waitForIdle();
487
488 mBinderService.cancelAllNotifications(PKG, parent.sbn.getUserId());
489 waitForIdle();
490 assertEquals(0, mNotificationManagerService.getNotificationRecordCount());
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500491 }
492
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500493 @Test
Julia Reynolds0839c022017-06-15 15:24:01 -0400494 public void testCancelAllNotificationsMultipleEnqueuedDoesNotCrash() throws Exception {
495 final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
496 for (int i = 0; i < 10; i++) {
497 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
498 sbn.getId(), sbn.getNotification(), sbn.getUserId());
499 }
500 mBinderService.cancelAllNotifications(PKG, sbn.getUserId());
501 waitForIdle();
Julia Reynolds080361e2017-07-13 11:23:12 -0400502
503 assertEquals(0, mNotificationManagerService.getNotificationRecordCount());
Julia Reynolds0839c022017-06-15 15:24:01 -0400504 }
505
506 @Test
507 public void testCancelGroupSummaryMultipleEnqueuedChildrenDoesNotCrash() throws Exception {
508 final NotificationRecord parent = generateNotificationRecord(
509 mTestNotificationChannel, 1, "group1", true);
510 final NotificationRecord parentAsChild = generateNotificationRecord(
511 mTestNotificationChannel, 1, "group1", false);
512 final NotificationRecord child = generateNotificationRecord(
513 mTestNotificationChannel, 2, "group1", false);
514
515 // fully post parent notification
516 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
517 parent.sbn.getId(), parent.sbn.getNotification(), parent.sbn.getUserId());
518 waitForIdle();
519
520 // enqueue the child several times
521 for (int i = 0; i < 10; i++) {
522 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
523 child.sbn.getId(), child.sbn.getNotification(), child.sbn.getUserId());
524 }
525 // make the parent a child, which will cancel the child notification
526 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
527 parentAsChild.sbn.getId(), parentAsChild.sbn.getNotification(),
528 parentAsChild.sbn.getUserId());
529 waitForIdle();
Julia Reynolds080361e2017-07-13 11:23:12 -0400530
531 assertEquals(0, mNotificationManagerService.getNotificationRecordCount());
Julia Reynolds0839c022017-06-15 15:24:01 -0400532 }
533
534 @Test
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500535 public void testCancelAllNotifications_IgnoreForegroundService() throws Exception {
536 final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
537 sbn.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE;
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500538 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
Julia Reynoldsfea6f7b2017-04-19 13:50:12 -0400539 sbn.getId(), sbn.getNotification(), sbn.getUserId());
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500540 mBinderService.cancelAllNotifications(PKG, sbn.getUserId());
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500541 waitForIdle();
542 StatusBarNotification[] notifs =
543 mBinderService.getActiveNotifications(sbn.getPackageName());
544 assertEquals(1, notifs.length);
Julia Reynolds080361e2017-07-13 11:23:12 -0400545 assertEquals(1, mNotificationManagerService.getNotificationRecordCount());
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500546 }
547
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500548 @Test
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500549 public void testCancelAllNotifications_IgnoreOtherPackages() throws Exception {
550 final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
551 sbn.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE;
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500552 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
Julia Reynoldsfea6f7b2017-04-19 13:50:12 -0400553 sbn.getId(), sbn.getNotification(), sbn.getUserId());
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500554 mBinderService.cancelAllNotifications("other_pkg_name", sbn.getUserId());
555 waitForIdle();
556 StatusBarNotification[] notifs =
557 mBinderService.getActiveNotifications(sbn.getPackageName());
558 assertEquals(1, notifs.length);
Julia Reynolds080361e2017-07-13 11:23:12 -0400559 assertEquals(1, mNotificationManagerService.getNotificationRecordCount());
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500560 }
561
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500562 @Test
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500563 public void testCancelAllNotifications_NullPkgRemovesAll() throws Exception {
564 final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500565 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
Julia Reynoldsfea6f7b2017-04-19 13:50:12 -0400566 sbn.getId(), sbn.getNotification(), sbn.getUserId());
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500567 mBinderService.cancelAllNotifications(null, sbn.getUserId());
568 waitForIdle();
569 StatusBarNotification[] notifs =
570 mBinderService.getActiveNotifications(sbn.getPackageName());
571 assertEquals(0, notifs.length);
Julia Reynolds080361e2017-07-13 11:23:12 -0400572 assertEquals(0, mNotificationManagerService.getNotificationRecordCount());
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500573 }
574
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500575 @Test
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500576 public void testCancelAllNotifications_NullPkgIgnoresUserAllNotifications() throws Exception {
577 final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500578 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
Julia Reynoldsfea6f7b2017-04-19 13:50:12 -0400579 sbn.getId(), sbn.getNotification(), UserHandle.USER_ALL);
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500580 // Null pkg is how we signal a user switch.
581 mBinderService.cancelAllNotifications(null, sbn.getUserId());
582 waitForIdle();
583 StatusBarNotification[] notifs =
584 mBinderService.getActiveNotifications(sbn.getPackageName());
585 assertEquals(1, notifs.length);
Julia Reynolds080361e2017-07-13 11:23:12 -0400586 assertEquals(1, mNotificationManagerService.getNotificationRecordCount());
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500587 }
Julia Reynolds5f20e9f2017-01-30 08:54:53 -0500588
589 @Test
Beverly40239d92017-07-07 10:20:41 -0400590 public void testAppInitiatedCancelAllNotifications_CancelsNoClearFlag() throws Exception {
591 final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
592 sbn.getNotification().flags |= Notification.FLAG_NO_CLEAR;
593 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
594 sbn.getId(), sbn.getNotification(), sbn.getUserId());
595 mBinderService.cancelAllNotifications(PKG, sbn.getUserId());
596 waitForIdle();
597 StatusBarNotification[] notifs =
598 mBinderService.getActiveNotifications(sbn.getPackageName());
599 assertEquals(0, notifs.length);
600 }
601
602 @Test
603 public void testCancelAllNotifications_CancelsNoClearFlag() throws Exception {
604 final NotificationRecord notif = generateNotificationRecord(
605 mTestNotificationChannel, 1, "group", true);
606 notif.getNotification().flags |= Notification.FLAG_NO_CLEAR;
607 mNotificationManagerService.addNotification(notif);
608 mNotificationManagerService.cancelAllNotificationsInt(uid, 0, PKG, null, 0, 0, true,
609 notif.getUserId(), 0, null);
610 waitForIdle();
611 StatusBarNotification[] notifs =
612 mBinderService.getActiveNotifications(notif.sbn.getPackageName());
613 assertEquals(0, notifs.length);
614 }
615
616 @Test
617 public void testUserInitiatedCancelAllOnClearAll_NoClearFlag() throws Exception {
618 final NotificationRecord notif = generateNotificationRecord(
619 mTestNotificationChannel, 1, "group", true);
620 notif.getNotification().flags |= Notification.FLAG_NO_CLEAR;
621 mNotificationManagerService.addNotification(notif);
622
623 mNotificationManagerService.mNotificationDelegate.onClearAll(uid, Binder.getCallingPid(),
624 notif.getUserId());
625 waitForIdle();
626 StatusBarNotification[] notifs =
627 mBinderService.getActiveNotifications(notif.sbn.getPackageName());
628 assertEquals(1, notifs.length);
629 }
630
631 @Test
632 public void testCancelAllCancelNotificationsFromListener_NoClearFlag() throws Exception {
633 final NotificationRecord parent = generateNotificationRecord(
634 mTestNotificationChannel, 1, "group", true);
635 final NotificationRecord child = generateNotificationRecord(
636 mTestNotificationChannel, 2, "group", false);
637 final NotificationRecord child2 = generateNotificationRecord(
638 mTestNotificationChannel, 3, "group", false);
639 child2.getNotification().flags |= Notification.FLAG_NO_CLEAR;
640 final NotificationRecord newGroup = generateNotificationRecord(
641 mTestNotificationChannel, 4, "group2", false);
642 mNotificationManagerService.addNotification(parent);
643 mNotificationManagerService.addNotification(child);
644 mNotificationManagerService.addNotification(child2);
645 mNotificationManagerService.addNotification(newGroup);
646 mNotificationManagerService.getBinderService().cancelNotificationsFromListener(null, null);
647 waitForIdle();
648 StatusBarNotification[] notifs =
649 mBinderService.getActiveNotifications(parent.sbn.getPackageName());
650 assertEquals(1, notifs.length);
651 }
652
653 @Test
654 public void testUserInitiatedCancelAllWithGroup_NoClearFlag() throws Exception {
655 final NotificationRecord parent = generateNotificationRecord(
656 mTestNotificationChannel, 1, "group", true);
657 final NotificationRecord child = generateNotificationRecord(
658 mTestNotificationChannel, 2, "group", false);
659 final NotificationRecord child2 = generateNotificationRecord(
660 mTestNotificationChannel, 3, "group", false);
661 child2.getNotification().flags |= Notification.FLAG_NO_CLEAR;
662 final NotificationRecord newGroup = generateNotificationRecord(
663 mTestNotificationChannel, 4, "group2", false);
664 mNotificationManagerService.addNotification(parent);
665 mNotificationManagerService.addNotification(child);
666 mNotificationManagerService.addNotification(child2);
667 mNotificationManagerService.addNotification(newGroup);
668 mNotificationManagerService.mNotificationDelegate.onClearAll(uid, Binder.getCallingPid(),
669 parent.getUserId());
670 waitForIdle();
671 StatusBarNotification[] notifs =
672 mBinderService.getActiveNotifications(parent.sbn.getPackageName());
673 assertEquals(1, notifs.length);
674 }
675
676 @Test
Geoffrey Pitsch415e4542017-04-10 13:12:58 -0400677 public void testRemoveForegroundServiceFlag_ImmediatelyAfterEnqueue() throws Exception {
678 final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
679 sbn.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE;
680 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", null,
Julia Reynoldsfea6f7b2017-04-19 13:50:12 -0400681 sbn.getId(), sbn.getNotification(), sbn.getUserId());
Geoffrey Pitsch415e4542017-04-10 13:12:58 -0400682 mInternalService.removeForegroundServiceFlagFromNotification(PKG, sbn.getId(),
683 sbn.getUserId());
684 waitForIdle();
685 StatusBarNotification[] notifs =
686 mBinderService.getActiveNotifications(sbn.getPackageName());
687 assertEquals(0, notifs[0].getNotification().flags & Notification.FLAG_FOREGROUND_SERVICE);
688 }
689
690 @Test
Geoffrey Pitsch27684152017-05-02 11:41:31 -0400691 public void testCancelAfterSecondEnqueueDoesNotSpecifyForegroundFlag() throws Exception {
692 final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
693 sbn.getNotification().flags =
694 Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE;
695 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
696 sbn.getId(), sbn.getNotification(), sbn.getUserId());
697 sbn.getNotification().flags = Notification.FLAG_ONGOING_EVENT;
698 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
699 sbn.getId(), sbn.getNotification(), sbn.getUserId());
700 mBinderService.cancelNotificationWithTag(PKG, "tag", sbn.getId(), sbn.getUserId());
701 waitForIdle();
702 assertEquals(0, mBinderService.getActiveNotifications(sbn.getPackageName()).length);
Julia Reynolds080361e2017-07-13 11:23:12 -0400703 assertEquals(0, mNotificationManagerService.getNotificationRecordCount());
Geoffrey Pitsch27684152017-05-02 11:41:31 -0400704 }
705
706 @Test
Julia Reynoldsa78cdff2017-04-26 10:19:25 -0400707 public void testFindGroupNotificationsLocked() throws Exception {
708 // make sure the same notification can be found in both lists and returned
709 final NotificationRecord group1 = generateNotificationRecord(
710 mTestNotificationChannel, 1, "group1", true);
711 mNotificationManagerService.addEnqueuedNotification(group1);
712 mNotificationManagerService.addNotification(group1);
713
714 // should not be returned
715 final NotificationRecord group2 = generateNotificationRecord(
716 mTestNotificationChannel, 2, "group2", true);
717 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", null,
718 group2.sbn.getId(), group2.sbn.getNotification(), group2.sbn.getUserId());
719 waitForIdle();
720
721 // should not be returned
722 final NotificationRecord nonGroup = generateNotificationRecord(
723 mTestNotificationChannel, 3, null, false);
724 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", null,
725 nonGroup.sbn.getId(), nonGroup.sbn.getNotification(), nonGroup.sbn.getUserId());
726 waitForIdle();
727
728 // same group, child, should be returned
729 final NotificationRecord group1Child = generateNotificationRecord(
730 mTestNotificationChannel, 4, "group1", false);
731 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", null, group1Child.sbn.getId(),
732 group1Child.sbn.getNotification(), group1Child.sbn.getUserId());
733 waitForIdle();
734
735 List<NotificationRecord> inGroup1 =
736 mNotificationManagerService.findGroupNotificationsLocked(PKG, group1.getGroupKey(),
737 group1.sbn.getUserId());
738 assertEquals(3, inGroup1.size());
739 for (NotificationRecord record : inGroup1) {
740 assertTrue(record.getGroupKey().equals(group1.getGroupKey()));
741 assertTrue(record.sbn.getId() == 1 || record.sbn.getId() == 4);
742 }
743 }
744
Julia Reynoldsa78cdff2017-04-26 10:19:25 -0400745 @Test
Julia Reynolds5f20e9f2017-01-30 08:54:53 -0500746 public void testTvExtenderChannelOverride_onTv() throws Exception {
747 mNotificationManagerService.setIsTelevision(true);
748 mNotificationManagerService.setRankingHelper(mRankingHelper);
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500749 when(mRankingHelper.getNotificationChannel(
Julia Reynolds5f20e9f2017-01-30 08:54:53 -0500750 anyString(), anyInt(), eq("foo"), anyBoolean())).thenReturn(
751 new NotificationChannel("foo", "foo", NotificationManager.IMPORTANCE_HIGH));
752
Julia Reynoldsbad42972017-04-25 13:52:49 -0400753 Notification.TvExtender tv = new Notification.TvExtender().setChannelId("foo");
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500754 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0,
Julia Reynoldsfea6f7b2017-04-19 13:50:12 -0400755 generateNotificationRecord(null, tv).getNotification(), 0);
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500756 verify(mRankingHelper, times(1)).getNotificationChannel(
Julia Reynolds5f20e9f2017-01-30 08:54:53 -0500757 anyString(), anyInt(), eq("foo"), anyBoolean());
758 }
759
760 @Test
Julia Reynolds5f20e9f2017-01-30 08:54:53 -0500761 public void testTvExtenderChannelOverride_notOnTv() throws Exception {
762 mNotificationManagerService.setIsTelevision(false);
763 mNotificationManagerService.setRankingHelper(mRankingHelper);
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500764 when(mRankingHelper.getNotificationChannel(
Julia Reynolds5f20e9f2017-01-30 08:54:53 -0500765 anyString(), anyInt(), anyString(), anyBoolean())).thenReturn(
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500766 mTestNotificationChannel);
Julia Reynolds5f20e9f2017-01-30 08:54:53 -0500767
Julia Reynoldsbad42972017-04-25 13:52:49 -0400768 Notification.TvExtender tv = new Notification.TvExtender().setChannelId("foo");
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500769 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0,
Julia Reynoldsfea6f7b2017-04-19 13:50:12 -0400770 generateNotificationRecord(null, tv).getNotification(), 0);
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500771 verify(mRankingHelper, times(1)).getNotificationChannel(
772 anyString(), anyInt(), eq(mTestNotificationChannel.getId()), anyBoolean());
Julia Reynolds5f20e9f2017-01-30 08:54:53 -0500773 }
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400774
775 @Test
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400776 public void testCreateChannelNotifyListener() throws Exception {
777 List<String> associations = new ArrayList<>();
778 associations.add("a");
779 when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations);
780 mNotificationManagerService.setRankingHelper(mRankingHelper);
781 when(mRankingHelper.getNotificationChannel(eq(PKG), anyInt(),
782 eq(mTestNotificationChannel.getId()), anyBoolean()))
783 .thenReturn(mTestNotificationChannel);
784 NotificationChannel channel2 = new NotificationChannel("a", "b", IMPORTANCE_LOW);
785 when(mRankingHelper.getNotificationChannel(eq(PKG), anyInt(),
786 eq(channel2.getId()), anyBoolean()))
787 .thenReturn(channel2);
788
789 reset(mNotificationListeners);
790 mBinderService.createNotificationChannels(PKG,
791 new ParceledListSlice(Arrays.asList(mTestNotificationChannel, channel2)));
792 verify(mNotificationListeners, times(1)).notifyNotificationChannelChanged(eq(PKG),
Julia Reynoldsf27d6b22017-04-13 15:48:16 -0400793 eq(Process.myUserHandle()), eq(mTestNotificationChannel),
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400794 eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_ADDED));
795 verify(mNotificationListeners, times(1)).notifyNotificationChannelChanged(eq(PKG),
Julia Reynoldsf27d6b22017-04-13 15:48:16 -0400796 eq(Process.myUserHandle()), eq(channel2),
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400797 eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_ADDED));
798 }
799
800 @Test
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400801 public void testCreateChannelGroupNotifyListener() throws Exception {
802 List<String> associations = new ArrayList<>();
803 associations.add("a");
804 when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations);
805 mNotificationManagerService.setRankingHelper(mRankingHelper);
806 NotificationChannelGroup group1 = new NotificationChannelGroup("a", "b");
807 NotificationChannelGroup group2 = new NotificationChannelGroup("n", "m");
808
809 reset(mNotificationListeners);
810 mBinderService.createNotificationChannelGroups(PKG,
811 new ParceledListSlice(Arrays.asList(group1, group2)));
812 verify(mNotificationListeners, times(1)).notifyNotificationChannelGroupChanged(eq(PKG),
Julia Reynoldsf27d6b22017-04-13 15:48:16 -0400813 eq(Process.myUserHandle()), eq(group1),
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400814 eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_ADDED));
815 verify(mNotificationListeners, times(1)).notifyNotificationChannelGroupChanged(eq(PKG),
Julia Reynoldsf27d6b22017-04-13 15:48:16 -0400816 eq(Process.myUserHandle()), eq(group2),
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400817 eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_ADDED));
818 }
819
820 @Test
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400821 public void testUpdateChannelNotifyListener() throws Exception {
822 List<String> associations = new ArrayList<>();
823 associations.add("a");
824 when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations);
825 mNotificationManagerService.setRankingHelper(mRankingHelper);
826 mTestNotificationChannel.setLightColor(Color.CYAN);
827 when(mRankingHelper.getNotificationChannel(eq(PKG), anyInt(),
828 eq(mTestNotificationChannel.getId()), anyBoolean()))
829 .thenReturn(mTestNotificationChannel);
830
831 reset(mNotificationListeners);
832 mBinderService.updateNotificationChannelForPackage(PKG, 0, mTestNotificationChannel);
833 verify(mNotificationListeners, times(1)).notifyNotificationChannelChanged(eq(PKG),
Julia Reynoldsf27d6b22017-04-13 15:48:16 -0400834 eq(Process.myUserHandle()), eq(mTestNotificationChannel),
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400835 eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_UPDATED));
836 }
837
838 @Test
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400839 public void testDeleteChannelNotifyListener() throws Exception {
840 List<String> associations = new ArrayList<>();
841 associations.add("a");
842 when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations);
843 mNotificationManagerService.setRankingHelper(mRankingHelper);
844 when(mRankingHelper.getNotificationChannel(eq(PKG), anyInt(),
845 eq(mTestNotificationChannel.getId()), anyBoolean()))
846 .thenReturn(mTestNotificationChannel);
847 reset(mNotificationListeners);
848 mBinderService.deleteNotificationChannel(PKG, mTestNotificationChannel.getId());
849 verify(mNotificationListeners, times(1)).notifyNotificationChannelChanged(eq(PKG),
Julia Reynoldsf27d6b22017-04-13 15:48:16 -0400850 eq(Process.myUserHandle()), eq(mTestNotificationChannel),
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400851 eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_DELETED));
852 }
853
854 @Test
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400855 public void testDeleteChannelGroupNotifyListener() throws Exception {
856 List<String> associations = new ArrayList<>();
857 associations.add("a");
858 when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations);
859 NotificationChannelGroup ncg = new NotificationChannelGroup("a", "b/c");
860 mNotificationManagerService.setRankingHelper(mRankingHelper);
861 when(mRankingHelper.getNotificationChannelGroup(eq(ncg.getId()), eq(PKG), anyInt()))
862 .thenReturn(ncg);
863 reset(mNotificationListeners);
864 mBinderService.deleteNotificationChannelGroup(PKG, ncg.getId());
865 verify(mNotificationListeners, times(1)).notifyNotificationChannelGroupChanged(eq(PKG),
Julia Reynoldsf27d6b22017-04-13 15:48:16 -0400866 eq(Process.myUserHandle()), eq(ncg),
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400867 eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_DELETED));
868 }
869
870 @Test
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400871 public void testUpdateNotificationChannelFromPrivilegedListener_success() throws Exception {
872 mNotificationManagerService.setRankingHelper(mRankingHelper);
873 List<String> associations = new ArrayList<>();
874 associations.add("a");
875 when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations);
876
877 mBinderService.updateNotificationChannelFromPrivilegedListener(
Julia Reynoldsf27d6b22017-04-13 15:48:16 -0400878 null, PKG, Process.myUserHandle(), mTestNotificationChannel);
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400879
880 verify(mRankingHelper, times(1)).updateNotificationChannel(anyString(), anyInt(), any());
881
882 verify(mNotificationListeners, never()).notifyNotificationChannelChanged(eq(PKG),
Julia Reynoldsf27d6b22017-04-13 15:48:16 -0400883 eq(Process.myUserHandle()), eq(mTestNotificationChannel),
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400884 eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_UPDATED));
885 }
886
887 @Test
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400888 public void testUpdateNotificationChannelFromPrivilegedListener_noAccess() throws Exception {
889 mNotificationManagerService.setRankingHelper(mRankingHelper);
890 List<String> associations = new ArrayList<>();
891 when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations);
892
893 try {
894 mBinderService.updateNotificationChannelFromPrivilegedListener(
Julia Reynoldsf27d6b22017-04-13 15:48:16 -0400895 null, PKG, Process.myUserHandle(), mTestNotificationChannel);
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400896 fail("listeners that don't have a companion device shouldn't be able to call this");
897 } catch (SecurityException e) {
898 // pass
899 }
900
901 verify(mRankingHelper, never()).updateNotificationChannel(anyString(), anyInt(), any());
902
903 verify(mNotificationListeners, never()).notifyNotificationChannelChanged(eq(PKG),
Julia Reynoldsf27d6b22017-04-13 15:48:16 -0400904 eq(Process.myUserHandle()), eq(mTestNotificationChannel),
905 eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_UPDATED));
906 }
907
908 @Test
Julia Reynoldsf27d6b22017-04-13 15:48:16 -0400909 public void testUpdateNotificationChannelFromPrivilegedListener_badUser() throws Exception {
910 mNotificationManagerService.setRankingHelper(mRankingHelper);
911 List<String> associations = new ArrayList<>();
912 associations.add("a");
913 when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations);
914 mListener = mock(ManagedServices.ManagedServiceInfo.class);
Julia Reynolds4da79702017-06-01 11:06:10 -0400915 mListener.component = new ComponentName(PKG, PKG);
Julia Reynoldsf27d6b22017-04-13 15:48:16 -0400916 when(mListener.enabledAndUserMatches(anyInt())).thenReturn(false);
917 when(mNotificationListeners.checkServiceTokenLocked(any())).thenReturn(mListener);
918
919 try {
920 mBinderService.updateNotificationChannelFromPrivilegedListener(
921 null, PKG, UserHandle.ALL, mTestNotificationChannel);
922 fail("incorrectly allowed a change to a user listener cannot see");
923 } catch (SecurityException e) {
924 // pass
925 }
926
927 verify(mRankingHelper, never()).updateNotificationChannel(anyString(), anyInt(), any());
928
929 verify(mNotificationListeners, never()).notifyNotificationChannelChanged(eq(PKG),
930 eq(Process.myUserHandle()), eq(mTestNotificationChannel),
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400931 eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_UPDATED));
932 }
933
934 @Test
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400935 public void testGetNotificationChannelFromPrivilegedListener_success() throws Exception {
936 mNotificationManagerService.setRankingHelper(mRankingHelper);
937 List<String> associations = new ArrayList<>();
938 associations.add("a");
939 when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations);
940
Julia Reynoldsf27d6b22017-04-13 15:48:16 -0400941 mBinderService.getNotificationChannelsFromPrivilegedListener(
942 null, PKG, Process.myUserHandle());
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400943
944 verify(mRankingHelper, times(1)).getNotificationChannels(
945 anyString(), anyInt(), anyBoolean());
946 }
947
948 @Test
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400949 public void testGetNotificationChannelFromPrivilegedListener_noAccess() throws Exception {
950 mNotificationManagerService.setRankingHelper(mRankingHelper);
951 List<String> associations = new ArrayList<>();
952 when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations);
953
954 try {
Julia Reynoldsf27d6b22017-04-13 15:48:16 -0400955 mBinderService.getNotificationChannelsFromPrivilegedListener(
956 null, PKG, Process.myUserHandle());
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400957 fail("listeners that don't have a companion device shouldn't be able to call this");
958 } catch (SecurityException e) {
959 // pass
960 }
961
962 verify(mRankingHelper, never()).getNotificationChannels(
963 anyString(), anyInt(), anyBoolean());
964 }
965
966 @Test
Julia Reynoldsf27d6b22017-04-13 15:48:16 -0400967 public void testGetNotificationChannelFromPrivilegedListener_badUser() throws Exception {
968 mNotificationManagerService.setRankingHelper(mRankingHelper);
969 List<String> associations = new ArrayList<>();
970 associations.add("a");
971 when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations);
972 mListener = mock(ManagedServices.ManagedServiceInfo.class);
973 when(mListener.enabledAndUserMatches(anyInt())).thenReturn(false);
974 when(mNotificationListeners.checkServiceTokenLocked(any())).thenReturn(mListener);
975
976 try {
977 mBinderService.getNotificationChannelsFromPrivilegedListener(
978 null, PKG, Process.myUserHandle());
979 fail("listener getting channels from a user they cannot see");
980 } catch (SecurityException e) {
981 // pass
982 }
983
984 verify(mRankingHelper, never()).getNotificationChannels(
985 anyString(), anyInt(), anyBoolean());
986 }
987
988 @Test
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400989 public void testGetNotificationChannelGroupsFromPrivilegedListener_success() throws Exception {
990 mNotificationManagerService.setRankingHelper(mRankingHelper);
991 List<String> associations = new ArrayList<>();
992 associations.add("a");
993 when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations);
994
Julia Reynoldsf27d6b22017-04-13 15:48:16 -0400995 mBinderService.getNotificationChannelGroupsFromPrivilegedListener(
996 null, PKG, Process.myUserHandle());
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400997
998 verify(mRankingHelper, times(1)).getNotificationChannelGroups(anyString(), anyInt());
999 }
1000
1001 @Test
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001002 public void testGetNotificationChannelGroupsFromPrivilegedListener_noAccess() throws Exception {
1003 mNotificationManagerService.setRankingHelper(mRankingHelper);
1004 List<String> associations = new ArrayList<>();
1005 when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations);
1006
1007 try {
Julia Reynoldsf27d6b22017-04-13 15:48:16 -04001008 mBinderService.getNotificationChannelGroupsFromPrivilegedListener(
1009 null, PKG, Process.myUserHandle());
1010 fail("listeners that don't have a companion device shouldn't be able to call this");
1011 } catch (SecurityException e) {
1012 // pass
1013 }
1014
1015 verify(mRankingHelper, never()).getNotificationChannelGroups(anyString(), anyInt());
1016 }
1017
1018 @Test
Julia Reynoldsf27d6b22017-04-13 15:48:16 -04001019 public void testGetNotificationChannelGroupsFromPrivilegedListener_badUser() throws Exception {
1020 mNotificationManagerService.setRankingHelper(mRankingHelper);
1021 List<String> associations = new ArrayList<>();
1022 when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations);
1023 mListener = mock(ManagedServices.ManagedServiceInfo.class);
1024 when(mListener.enabledAndUserMatches(anyInt())).thenReturn(false);
1025 when(mNotificationListeners.checkServiceTokenLocked(any())).thenReturn(mListener);
1026
1027 try {
1028 mBinderService.getNotificationChannelGroupsFromPrivilegedListener(
1029 null, PKG, Process.myUserHandle());
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001030 fail("listeners that don't have a companion device shouldn't be able to call this");
1031 } catch (SecurityException e) {
1032 // pass
1033 }
1034
1035 verify(mRankingHelper, never()).getNotificationChannelGroups(anyString(), anyInt());
1036 }
Julia Reynoldsda781472017-04-12 09:41:16 -04001037
1038 @Test
Julia Reynoldsda781472017-04-12 09:41:16 -04001039 public void testHasCompanionDevice_failure() throws Exception {
1040 when(mCompanionMgr.getAssociations(anyString(), anyInt())).thenThrow(
1041 new IllegalArgumentException());
1042 mNotificationManagerService.hasCompanionDevice(mListener);
1043 }
Julia Reynolds727a7282017-04-13 10:54:01 -04001044
1045 @Test
Julia Reynolds727a7282017-04-13 10:54:01 -04001046 public void testHasCompanionDevice_noService() throws Exception {
1047 mNotificationManagerService = new TestableNotificationManagerService(mContext);
1048
1049 assertFalse(mNotificationManagerService.hasCompanionDevice(mListener));
1050 }
1051
Julia Reynoldsa78cdff2017-04-26 10:19:25 -04001052 @Test
1053 public void testSnoozeRunnable_snoozeNonGrouped() throws Exception {
1054 final NotificationRecord nonGrouped = generateNotificationRecord(
1055 mTestNotificationChannel, 1, null, false);
1056 final NotificationRecord grouped = generateNotificationRecord(
1057 mTestNotificationChannel, 2, "group", false);
1058 mNotificationManagerService.addNotification(grouped);
1059 mNotificationManagerService.addNotification(nonGrouped);
1060
1061 NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
1062 mNotificationManagerService.new SnoozeNotificationRunnable(
1063 nonGrouped.getKey(), 100, null);
1064 snoozeNotificationRunnable.run();
1065
1066 // only snooze the one notification
1067 verify(mSnoozeHelper, times(1)).snooze(any(NotificationRecord.class), anyLong());
1068 }
1069
1070 @Test
1071 public void testSnoozeRunnable_snoozeSummary_withChildren() throws Exception {
1072 final NotificationRecord parent = generateNotificationRecord(
1073 mTestNotificationChannel, 1, "group", true);
1074 final NotificationRecord child = generateNotificationRecord(
1075 mTestNotificationChannel, 2, "group", false);
1076 final NotificationRecord child2 = generateNotificationRecord(
1077 mTestNotificationChannel, 3, "group", false);
1078 mNotificationManagerService.addNotification(parent);
1079 mNotificationManagerService.addNotification(child);
1080 mNotificationManagerService.addNotification(child2);
1081
1082 NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
1083 mNotificationManagerService.new SnoozeNotificationRunnable(
1084 parent.getKey(), 100, null);
1085 snoozeNotificationRunnable.run();
1086
1087 // snooze parent and children
1088 verify(mSnoozeHelper, times(3)).snooze(any(NotificationRecord.class), anyLong());
1089 }
1090
1091 @Test
1092 public void testSnoozeRunnable_snoozeGroupChild_fellowChildren() throws Exception {
1093 final NotificationRecord parent = generateNotificationRecord(
1094 mTestNotificationChannel, 1, "group", true);
1095 final NotificationRecord child = generateNotificationRecord(
1096 mTestNotificationChannel, 2, "group", false);
1097 final NotificationRecord child2 = generateNotificationRecord(
1098 mTestNotificationChannel, 3, "group", false);
1099 mNotificationManagerService.addNotification(parent);
1100 mNotificationManagerService.addNotification(child);
1101 mNotificationManagerService.addNotification(child2);
1102
1103 NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
1104 mNotificationManagerService.new SnoozeNotificationRunnable(
1105 child2.getKey(), 100, null);
1106 snoozeNotificationRunnable.run();
1107
1108 // only snooze the one child
1109 verify(mSnoozeHelper, times(1)).snooze(any(NotificationRecord.class), anyLong());
1110 }
1111
1112 @Test
1113 public void testSnoozeRunnable_snoozeGroupChild_onlyChildOfSummary() throws Exception {
1114 final NotificationRecord parent = generateNotificationRecord(
1115 mTestNotificationChannel, 1, "group", true);
1116 assertTrue(parent.sbn.getNotification().isGroupSummary());
1117 final NotificationRecord child = generateNotificationRecord(
1118 mTestNotificationChannel, 2, "group", false);
1119 mNotificationManagerService.addNotification(parent);
1120 mNotificationManagerService.addNotification(child);
1121
1122 NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
1123 mNotificationManagerService.new SnoozeNotificationRunnable(
1124 child.getKey(), 100, null);
1125 snoozeNotificationRunnable.run();
1126
1127 // snooze child and summary
1128 verify(mSnoozeHelper, times(2)).snooze(any(NotificationRecord.class), anyLong());
1129 }
1130
1131 @Test
1132 public void testSnoozeRunnable_snoozeGroupChild_noOthersInGroup() throws Exception {
1133 final NotificationRecord child = generateNotificationRecord(
1134 mTestNotificationChannel, 2, "group", false);
1135 mNotificationManagerService.addNotification(child);
1136
1137 NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
1138 mNotificationManagerService.new SnoozeNotificationRunnable(
1139 child.getKey(), 100, null);
1140 snoozeNotificationRunnable.run();
1141
1142 // snooze child only
1143 verify(mSnoozeHelper, times(1)).snooze(any(NotificationRecord.class), anyLong());
1144 }
1145
1146 @Test
1147 public void testPostGroupChild_unsnoozeParent() throws Exception {
1148 final NotificationRecord child = generateNotificationRecord(
1149 mTestNotificationChannel, 2, "group", false);
1150
1151 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", null,
1152 child.sbn.getId(), child.sbn.getNotification(), child.sbn.getUserId());
1153 waitForIdle();
1154
1155 verify(mSnoozeHelper, times(1)).repostGroupSummary(
1156 anyString(), anyInt(), eq(child.getGroupKey()));
1157 }
1158
1159 @Test
1160 public void testPostNonGroup_noUnsnoozing() throws Exception {
1161 final NotificationRecord record = generateNotificationRecord(
1162 mTestNotificationChannel, 2, null, false);
1163
1164 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", null,
1165 record.sbn.getId(), record.sbn.getNotification(), record.sbn.getUserId());
1166 waitForIdle();
1167
1168 verify(mSnoozeHelper, never()).repostGroupSummary(anyString(), anyInt(), anyString());
1169 }
1170
1171 @Test
1172 public void testPostGroupSummary_noUnsnoozing() throws Exception {
1173 final NotificationRecord parent = generateNotificationRecord(
1174 mTestNotificationChannel, 2, "group", true);
1175
1176 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", null,
1177 parent.sbn.getId(), parent.sbn.getNotification(), parent.sbn.getUserId());
1178 waitForIdle();
1179
1180 verify(mSnoozeHelper, never()).repostGroupSummary(anyString(), anyInt(), anyString());
1181 }
Julia Reynoldsb852e562017-06-06 16:14:18 -04001182
1183 @Test
1184 public void testSetListenerAccess() throws Exception {
1185 ComponentName c = ComponentName.unflattenFromString("package/Component");
1186 try {
1187 mBinderService.setNotificationListenerAccessGranted(c, true);
1188 } catch (SecurityException e) {
1189 if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) {
1190 throw e;
1191 }
1192 }
1193
1194 verify(mNotificationListeners, times(1)).setPackageOrComponentEnabled(
1195 c.flattenToString(), 0, true, true);
1196 verify(mConditionProviders, times(1)).setPackageOrComponentEnabled(
1197 c.flattenToString(), 0, false, true);
1198 verify(mNotificationAssistants, never()).setPackageOrComponentEnabled(
1199 any(), anyInt(), anyBoolean(), anyBoolean());
1200 }
1201
1202 @Test
1203 public void testSetAssistantAccess() throws Exception {
1204 ComponentName c = ComponentName.unflattenFromString("package/Component");
1205 try {
1206 mBinderService.setNotificationAssistantAccessGranted(c, true);
1207 } catch (SecurityException e) {
1208 if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) {
1209 throw e;
1210 }
1211 }
1212
1213 verify(mNotificationAssistants, times(1)).setPackageOrComponentEnabled(
1214 c.flattenToString(), 0, true, true);
1215 verify(mConditionProviders, times(1)).setPackageOrComponentEnabled(
1216 c.flattenToString(), 0, false, true);
1217 verify(mNotificationListeners, never()).setPackageOrComponentEnabled(
1218 any(), anyInt(), anyBoolean(), anyBoolean());
1219 }
1220
1221 @Test
1222 public void testSetDndAccess() throws Exception {
1223 ComponentName c = ComponentName.unflattenFromString("package/Component");
1224 try {
1225 mBinderService.setNotificationPolicyAccessGranted(c.getPackageName(), true);
1226 } catch (SecurityException e) {
1227 if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) {
1228 throw e;
1229 }
1230 }
1231
1232 verify(mConditionProviders, times(1)).setPackageOrComponentEnabled(
1233 c.getPackageName(), 0, true, true);
1234 verify(mNotificationAssistants, never()).setPackageOrComponentEnabled(
1235 any(), anyInt(), anyBoolean(), anyBoolean());
1236 verify(mNotificationListeners, never()).setPackageOrComponentEnabled(
1237 any(), anyInt(), anyBoolean(), anyBoolean());
1238 }
Julia Reynolds68263d12017-06-21 14:21:19 -04001239
1240 @Test
1241 public void testSetListenerAccess_doesNothingOnLowRam() throws Exception {
1242 when(mActivityManager.isLowRamDevice()).thenReturn(true);
1243 ComponentName c = ComponentName.unflattenFromString("package/Component");
1244 mBinderService.setNotificationListenerAccessGranted(c, true);
1245
1246 verify(mNotificationListeners, never()).setPackageOrComponentEnabled(
1247 c.flattenToString(), 0, true, true);
1248 verify(mConditionProviders, never()).setPackageOrComponentEnabled(
1249 c.flattenToString(), 0, false, true);
1250 verify(mNotificationAssistants, never()).setPackageOrComponentEnabled(
1251 any(), anyInt(), anyBoolean(), anyBoolean());
1252 }
1253
1254 @Test
1255 public void testSetAssistantAccess_doesNothingOnLowRam() throws Exception {
1256 when(mActivityManager.isLowRamDevice()).thenReturn(true);
1257 ComponentName c = ComponentName.unflattenFromString("package/Component");
1258 mBinderService.setNotificationAssistantAccessGranted(c, true);
1259
1260
1261 verify(mNotificationListeners, never()).setPackageOrComponentEnabled(
1262 c.flattenToString(), 0, true, true);
1263 verify(mConditionProviders, never()).setPackageOrComponentEnabled(
1264 c.flattenToString(), 0, false, true);
1265 verify(mNotificationAssistants, never()).setPackageOrComponentEnabled(
1266 any(), anyInt(), anyBoolean(), anyBoolean());
1267 }
1268
1269 @Test
1270 public void testSetDndAccess_doesNothingOnLowRam() throws Exception {
1271 when(mActivityManager.isLowRamDevice()).thenReturn(true);
1272 ComponentName c = ComponentName.unflattenFromString("package/Component");
1273 mBinderService.setNotificationPolicyAccessGranted(c.getPackageName(), true);
1274
1275 verify(mNotificationListeners, never()).setPackageOrComponentEnabled(
1276 c.flattenToString(), 0, true, true);
1277 verify(mConditionProviders, never()).setPackageOrComponentEnabled(
1278 c.flattenToString(), 0, false, true);
1279 verify(mNotificationAssistants, never()).setPackageOrComponentEnabled(
1280 any(), anyInt(), anyBoolean(), anyBoolean());
1281 }
Julia Reynolds8aebf352017-06-26 11:35:33 -04001282
1283 @Test
1284 public void testOnlyAutogroupIfGroupChanged_noPriorNoti_autogroups() throws Exception {
1285 NotificationRecord r = generateNotificationRecord(mTestNotificationChannel, 0, null, false);
1286 mNotificationManagerService.addEnqueuedNotification(r);
1287 NotificationManagerService.PostNotificationRunnable runnable =
1288 mNotificationManagerService.new PostNotificationRunnable(r.getKey());
1289 runnable.run();
1290 waitForIdle();
1291
1292 verify(mGroupHelper, times(1)).onNotificationPosted(any());
1293 }
1294
1295 @Test
1296 public void testOnlyAutogroupIfGroupChanged_groupChanged_autogroups()
1297 throws Exception {
1298 NotificationRecord r = generateNotificationRecord(mTestNotificationChannel, 0, "group", false);
1299 mNotificationManagerService.addNotification(r);
1300
1301 r = generateNotificationRecord(mTestNotificationChannel, 0, null, false);
1302 mNotificationManagerService.addEnqueuedNotification(r);
1303 NotificationManagerService.PostNotificationRunnable runnable =
1304 mNotificationManagerService.new PostNotificationRunnable(r.getKey());
1305 runnable.run();
1306 waitForIdle();
1307
1308 verify(mGroupHelper, times(1)).onNotificationPosted(any());
1309 }
1310
1311 @Test
1312 public void testOnlyAutogroupIfGroupChanged_noGroupChanged_autogroups()
1313 throws Exception {
Julia Reynolds4db59552017-06-30 13:34:01 -04001314 NotificationRecord r = generateNotificationRecord(mTestNotificationChannel, 0, "group",
1315 false);
Julia Reynolds8aebf352017-06-26 11:35:33 -04001316 mNotificationManagerService.addNotification(r);
1317 mNotificationManagerService.addEnqueuedNotification(r);
1318
1319 NotificationManagerService.PostNotificationRunnable runnable =
1320 mNotificationManagerService.new PostNotificationRunnable(r.getKey());
1321 runnable.run();
1322 waitForIdle();
1323
1324 verify(mGroupHelper, never()).onNotificationPosted(any());
1325 }
Beverly40239d92017-07-07 10:20:41 -04001326
Julia Reynolds4db59552017-06-30 13:34:01 -04001327 @Test
1328 public void testNoFakeColorizedPermission() throws Exception {
1329 when(mPackageManagerClient.checkPermission(any(), any())).thenReturn(PERMISSION_DENIED);
1330 Notification.Builder nb = new Notification.Builder(mContext,
1331 mTestNotificationChannel.getId())
1332 .setContentTitle("foo")
1333 .setColorized(true)
1334 .setFlag(Notification.FLAG_CAN_COLORIZE, true)
1335 .setSmallIcon(android.R.drawable.sym_def_app_icon);
1336 StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, "tag", uid, 0,
1337 nb.build(), new UserHandle(uid), null, 0);
1338 NotificationRecord nr = new NotificationRecord(mContext, sbn, mTestNotificationChannel);
1339
1340 mBinderService.enqueueNotificationWithTag(PKG, PKG, null,
1341 nr.sbn.getId(), nr.sbn.getNotification(), nr.sbn.getUserId());
1342 waitForIdle();
1343
1344 NotificationRecord posted = mNotificationManagerService.findNotificationLocked(
1345 PKG, null, nr.sbn.getId(), nr.sbn.getUserId());
1346
1347 assertFalse(posted.getNotification().isColorized());
1348 }
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04001349
1350 @Test
1351 public void testGetNotificationCountLocked() throws Exception {
1352 for (int i = 0; i < 20; i++) {
1353 NotificationRecord r = generateNotificationRecord(mTestNotificationChannel, i, null, false);
1354 mNotificationManagerService.addEnqueuedNotification(r);
1355 }
1356 for (int i = 0; i < 20; i++) {
1357 NotificationRecord r = generateNotificationRecord(mTestNotificationChannel, i, null, false);
1358 mNotificationManagerService.addNotification(r);
1359 }
1360
1361 // another package
1362 Notification n =
1363 new Notification.Builder(mContext, mTestNotificationChannel.getId())
1364 .setSmallIcon(android.R.drawable.sym_def_app_icon)
1365 .build();
1366
1367 StatusBarNotification sbn = new StatusBarNotification("a", "a", 0, "tag", uid, 0,
1368 n, new UserHandle(uid), null, 0);
1369 NotificationRecord otherPackage =
1370 new NotificationRecord(mContext, sbn, mTestNotificationChannel);
1371 mNotificationManagerService.addEnqueuedNotification(otherPackage);
1372 mNotificationManagerService.addNotification(otherPackage);
1373
1374 // Same notifications are enqueued as posted, everything counts b/c id and tag don't match
Julia Reynoldseb3dca72017-07-11 10:39:58 -04001375 int userId = new UserHandle(uid).getIdentifier();
1376 assertEquals(40, mNotificationManagerService.getNotificationCountLocked(PKG, userId, 0, null));
1377 assertEquals(40, mNotificationManagerService.getNotificationCountLocked(PKG, userId, 0, "tag2"));
1378 assertEquals(2, mNotificationManagerService.getNotificationCountLocked("a", userId, 0, "banana"));
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04001379
1380 // exclude a known notification - it's excluded from only the posted list, not enqueued
Julia Reynoldseb3dca72017-07-11 10:39:58 -04001381 assertEquals(39, mNotificationManagerService.getNotificationCountLocked(PKG, userId, 0, "tag"));
1382 }
1383
1384 @Test
Julia Reynolds51710712017-07-19 13:48:07 -04001385 public void testAddAutogroup_requestsSort() throws Exception {
Julia Reynoldseb3dca72017-07-11 10:39:58 -04001386 RankingHandler rh = mock(RankingHandler.class);
1387 mNotificationManagerService.setRankingHandler(rh);
1388
1389 final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel);
1390 mNotificationManagerService.addNotification(r);
1391 mNotificationManagerService.addAutogroupKeyLocked(r.getKey());
Julia Reynolds51710712017-07-19 13:48:07 -04001392
1393 verify(rh, times(1)).requestSort();
1394 }
1395
1396 @Test
1397 public void testRemoveAutogroup_requestsSort() throws Exception {
1398 RankingHandler rh = mock(RankingHandler.class);
1399 mNotificationManagerService.setRankingHandler(rh);
1400
1401 final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel);
1402 r.setOverrideGroupKey("TEST");
1403 mNotificationManagerService.addNotification(r);
Julia Reynoldseb3dca72017-07-11 10:39:58 -04001404 mNotificationManagerService.removeAutogroupKeyLocked(r.getKey());
1405
Julia Reynolds51710712017-07-19 13:48:07 -04001406 verify(rh, times(1)).requestSort();
1407 }
1408
1409 @Test
1410 public void testReaddAutogroup_noSort() throws Exception {
1411 RankingHandler rh = mock(RankingHandler.class);
1412 mNotificationManagerService.setRankingHandler(rh);
1413
1414 final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel);
1415 r.setOverrideGroupKey("TEST");
1416 mNotificationManagerService.addNotification(r);
1417 mNotificationManagerService.addAutogroupKeyLocked(r.getKey());
1418
1419 verify(rh, never()).requestSort();
Julia Reynoldseb3dca72017-07-11 10:39:58 -04001420 }
1421
1422 @Test
1423 public void testHandleRankingSort_sendsUpdateOnSignalExtractorChange() throws Exception {
1424 mNotificationManagerService.setRankingHelper(mRankingHelper);
1425 NotificationManagerService.WorkerHandler handler = mock(
1426 NotificationManagerService.WorkerHandler.class);
1427 mNotificationManagerService.setHandler(handler);
1428
1429 Map<String, Answer> answers = getSignalExtractorSideEffects();
1430 for (String message : answers.keySet()) {
1431 mNotificationManagerService.clearNotifications();
1432 final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel);
1433 mNotificationManagerService.addNotification(r);
1434
1435 doAnswer(answers.get(message)).when(mRankingHelper).extractSignals(r);
1436
1437 mNotificationManagerService.handleRankingSort();
1438 }
1439 verify(handler, times(answers.size())).scheduleSendRankingUpdate();
1440 }
1441
1442 @Test
1443 public void testHandleRankingSort_noUpdateWhenNoSignalChange() throws Exception {
1444 mNotificationManagerService.setRankingHelper(mRankingHelper);
1445 NotificationManagerService.WorkerHandler handler = mock(
1446 NotificationManagerService.WorkerHandler.class);
1447 mNotificationManagerService.setHandler(handler);
1448
1449 final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel);
1450 mNotificationManagerService.addNotification(r);
1451
1452 mNotificationManagerService.handleRankingSort();
1453 verify(handler, never()).scheduleSendRankingUpdate();
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04001454 }
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -05001455}