blob: ad3fecf4b50e4a8aa39009d7af1997027a416c7b [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 Reynolds3eb3ffd2017-11-16 10:11:32 -050019import static android.app.NotificationManager.EXTRA_BLOCKED_STATE;
Julia Reynolds8617e4e2017-09-18 16:52:37 -040020import static android.app.NotificationManager.IMPORTANCE_HIGH;
Julia Reynolds73ed76b2017-04-04 17:04:38 -040021import static android.app.NotificationManager.IMPORTANCE_LOW;
Julia Reynolds3eb3ffd2017-11-16 10:11:32 -050022import static android.app.NotificationManager.IMPORTANCE_MAX;
Julia Reynolds4da79702017-06-01 11:06:10 -040023import static android.app.NotificationManager.IMPORTANCE_NONE;
Julia Reynolds8617e4e2017-09-18 16:52:37 -040024import static android.app.NotificationManager.IMPORTANCE_UNSPECIFIED;
Julia Reynoldse1816412017-10-24 10:39:11 -040025import static android.content.pm.PackageManager.FEATURE_WATCH;
Julia Reynolds4db59552017-06-30 13:34:01 -040026import static android.content.pm.PackageManager.PERMISSION_DENIED;
Julia Reynolds73ed76b2017-04-04 17:04:38 -040027
Geoffrey Pitsch03533712017-01-05 10:30:07 -050028import static junit.framework.Assert.assertEquals;
Julia Reynolds727a7282017-04-13 10:54:01 -040029import static junit.framework.Assert.assertFalse;
Julia Reynolds92febc32017-10-26 11:30:31 -040030import static junit.framework.Assert.assertNotNull;
Julia Reynolds8617e4e2017-09-18 16:52:37 -040031import static junit.framework.Assert.assertNull;
Julia Reynoldsbaff4002016-12-15 11:34:26 -050032import static junit.framework.Assert.assertTrue;
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -050033import static junit.framework.Assert.fail;
Julia Reynoldsbaff4002016-12-15 11:34:26 -050034
Julia Reynolds5f20e9f2017-01-30 08:54:53 -050035import static org.mockito.Matchers.anyBoolean;
Julia Reynoldsa78cdff2017-04-26 10:19:25 -040036import static org.mockito.Matchers.anyLong;
Julia Reynoldsbaff4002016-12-15 11:34:26 -050037import static org.mockito.Matchers.anyString;
38import static org.mockito.Matchers.eq;
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -050039import static org.mockito.Mockito.any;
40import static org.mockito.Mockito.anyInt;
Julia Reynoldseb3dca72017-07-11 10:39:58 -040041import static org.mockito.Mockito.doAnswer;
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -050042import static org.mockito.Mockito.mock;
Julia Reynolds73ed76b2017-04-04 17:04:38 -040043import static org.mockito.Mockito.never;
44import static org.mockito.Mockito.reset;
Julia Reynolds503ed942017-10-04 16:04:56 -040045import static org.mockito.Mockito.spy;
46import static org.mockito.Mockito.timeout;
Julia Reynoldsbaff4002016-12-15 11:34:26 -050047import static org.mockito.Mockito.times;
48import static org.mockito.Mockito.verify;
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -050049import static org.mockito.Mockito.when;
50
Julia Reynolds68263d12017-06-21 14:21:19 -040051import android.app.ActivityManager;
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -050052import android.app.INotificationManager;
Julia Reynoldsbaff4002016-12-15 11:34:26 -050053import android.app.Notification;
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -050054import android.app.NotificationChannel;
Julia Reynolds73ed76b2017-04-04 17:04:38 -040055import android.app.NotificationChannelGroup;
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -050056import android.app.NotificationManager;
Julia Reynolds73ed76b2017-04-04 17:04:38 -040057import android.companion.ICompanionDeviceManager;
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -050058import android.content.ComponentName;
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -050059import android.content.Context;
Beverlyd4f96492017-08-02 13:36:11 -040060import android.content.Intent;
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -050061import android.content.pm.ApplicationInfo;
62import android.content.pm.IPackageManager;
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -050063import android.content.pm.PackageManager;
Geoffrey Pitsch03533712017-01-05 10:30:07 -050064import android.content.pm.ParceledListSlice;
Julia Reynolds73ed76b2017-04-04 17:04:38 -040065import android.graphics.Color;
Julia Reynolds76c096d2017-06-19 08:16:04 -040066import android.media.AudioManager;
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -050067import android.os.Binder;
Julia Reynolds8617e4e2017-09-18 16:52:37 -040068import android.os.Build;
Julia Reynolds503ed942017-10-04 16:04:56 -040069import android.os.Bundle;
Julia Reynoldsf27d6b22017-04-13 15:48:16 -040070import android.os.Process;
Julia Reynoldsbaff4002016-12-15 11:34:26 -050071import android.os.UserHandle;
Chris Wren89aa2262017-05-05 18:05:56 -040072import android.provider.Settings.Secure;
Julia Reynolds503ed942017-10-04 16:04:56 -040073import android.service.notification.Adjustment;
Julia Reynolds73ed76b2017-04-04 17:04:38 -040074import android.service.notification.NotificationListenerService;
Julia Reynolds503ed942017-10-04 16:04:56 -040075import android.service.notification.NotificationStats;
Julia Reynoldsbaff4002016-12-15 11:34:26 -050076import android.service.notification.StatusBarNotification;
Geoffrey Pitsch8185d382017-05-19 18:41:32 -040077import android.test.suitebuilder.annotation.SmallTest;
Jason Monk745d0a82017-04-17 11:34:22 -040078import android.testing.AndroidTestingRunner;
Julia Reynolds92febc32017-10-26 11:30:31 -040079import android.testing.TestableContext;
Geoffrey Pitsch415e4542017-04-10 13:12:58 -040080import android.testing.TestableLooper;
Jason Monk745d0a82017-04-17 11:34:22 -040081import android.testing.TestableLooper.RunWithLooper;
Julia Reynoldseb3dca72017-07-11 10:39:58 -040082import android.util.ArrayMap;
Julia Reynoldsb852e562017-06-06 16:14:18 -040083import android.util.AtomicFile;
Julia Reynoldseb3dca72017-07-11 10:39:58 -040084
Julia Reynolds503ed942017-10-04 16:04:56 -040085import com.android.internal.statusbar.NotificationVisibility;
Jason Monk74f5e362017-12-06 08:56:33 -050086import com.android.server.UiServiceTestCase;
Julia Reynoldseb3dca72017-07-11 10:39:58 -040087import com.android.server.lights.Light;
88import com.android.server.lights.LightsManager;
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -040089import com.android.server.notification.NotificationManagerService.NotificationAssistants;
90import com.android.server.notification.NotificationManagerService.NotificationListeners;
Julia Reynoldseb3dca72017-07-11 10:39:58 -040091
92import org.junit.After;
93import org.junit.Before;
94import org.junit.Test;
95import org.junit.runner.RunWith;
Julia Reynolds40f00d72017-12-12 10:47:32 -050096import org.mockito.ArgumentCaptor;
97import org.mockito.Mock;
98import org.mockito.MockitoAnnotations;
Julia Reynoldseb3dca72017-07-11 10:39:58 -040099import org.mockito.stubbing.Answer;
Julia Reynolds5f20e9f2017-01-30 08:54:53 -0500100
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -0400101import java.io.BufferedInputStream;
102import java.io.ByteArrayInputStream;
Julia Reynoldsb852e562017-06-06 16:14:18 -0400103import java.io.File;
104import java.io.FileInputStream;
105import java.io.FileOutputStream;
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400106import java.util.ArrayList;
Geoffrey Pitsch03533712017-01-05 10:30:07 -0500107import java.util.Arrays;
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400108import java.util.List;
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400109import java.util.Map;
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -0500110
Geoffrey Pitsch8185d382017-05-19 18:41:32 -0400111@SmallTest
Jason Monk745d0a82017-04-17 11:34:22 -0400112@RunWith(AndroidTestingRunner.class)
113@RunWithLooper
Jason Monk74f5e362017-12-06 08:56:33 -0500114public class NotificationManagerServiceTest extends UiServiceTestCase {
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500115 private static final String TEST_CHANNEL_ID = "NotificationManagerServiceTestChannelId";
Geoffrey Pitsch07532c32017-07-18 11:44:06 -0400116 private final int mUid = Binder.getCallingUid();
Julia Reynolds503ed942017-10-04 16:04:56 -0400117 private NotificationManagerService mService;
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -0500118 private INotificationManager mBinderService;
Geoffrey Pitsch415e4542017-04-10 13:12:58 -0400119 private NotificationManagerInternal mInternalService;
Julia Reynoldsda781472017-04-12 09:41:16 -0400120 @Mock
121 private IPackageManager mPackageManager;
122 @Mock
123 private PackageManager mPackageManagerClient;
Julia Reynolds92febc32017-10-26 11:30:31 -0400124 private TestableContext mContext = spy(getContext());
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500125 private final String PKG = mContext.getPackageName();
Geoffrey Pitsch415e4542017-04-10 13:12:58 -0400126 private TestableLooper mTestableLooper;
Julia Reynoldsda781472017-04-12 09:41:16 -0400127 @Mock
128 private RankingHelper mRankingHelper;
Julia Reynoldsb852e562017-06-06 16:14:18 -0400129 AtomicFile mPolicyFile;
130 File mFile;
131 @Mock
Geoffrey Pitschd5bcf212017-06-01 15:45:35 -0400132 private NotificationUsageStats mUsageStats;
Julia Reynolds76c096d2017-06-19 08:16:04 -0400133 @Mock
134 private AudioManager mAudioManager;
Julia Reynolds68263d12017-06-21 14:21:19 -0400135 @Mock
136 ActivityManager mActivityManager;
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400137 NotificationManagerService.WorkerHandler mHandler;
Julia Reynolds3ff26d22017-06-19 08:16:04 -0400138
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500139 private NotificationChannel mTestNotificationChannel = new NotificationChannel(
140 TEST_CHANNEL_ID, TEST_CHANNEL_ID, NotificationManager.IMPORTANCE_DEFAULT);
Julia Reynoldsda781472017-04-12 09:41:16 -0400141 @Mock
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -0400142 private NotificationListeners mListeners;
143 @Mock private NotificationAssistants mAssistants;
Julia Reynoldsb852e562017-06-06 16:14:18 -0400144 @Mock private ConditionProviders mConditionProviders;
Julia Reynoldsda781472017-04-12 09:41:16 -0400145 private ManagedServices.ManagedServiceInfo mListener;
146 @Mock private ICompanionDeviceManager mCompanionMgr;
Julia Reynoldsa78cdff2017-04-26 10:19:25 -0400147 @Mock SnoozeHelper mSnoozeHelper;
Julia Reynolds8aebf352017-06-26 11:35:33 -0400148 @Mock GroupHelper mGroupHelper;
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -0500149
Geoffrey Pitsch415e4542017-04-10 13:12:58 -0400150 // Use a Testable subclass so we can simulate calls from the system without failing.
151 private static class TestableNotificationManagerService extends NotificationManagerService {
Amith Yamasani803eab692017-11-09 17:47:04 -0800152 public TestableNotificationManagerService(Context context) {
153 super(context);
154 }
Geoffrey Pitsch415e4542017-04-10 13:12:58 -0400155
156 @Override
Geoffrey Pitsch27684152017-05-02 11:41:31 -0400157 protected boolean isCallingUidSystem() {
158 return true;
159 }
160
161 @Override
162 protected boolean isCallerSystemOrPhone() {
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400163 return true;
164 }
Julia Reynolds727a7282017-04-13 10:54:01 -0400165
166 @Override
167 protected ICompanionDeviceManager getCompanionManager() {
168 return null;
169 }
Amith Yamasani803eab692017-11-09 17:47:04 -0800170
171 @Override
172 protected void reportSeen(NotificationRecord r) {
173 return;
174 }
Geoffrey Pitsch415e4542017-04-10 13:12:58 -0400175 }
176
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -0500177 @Before
178 public void setUp() throws Exception {
Julia Reynoldsda781472017-04-12 09:41:16 -0400179 MockitoAnnotations.initMocks(this);
Chris Wren89aa2262017-05-05 18:05:56 -0400180
181 // most tests assume badging is enabled
182 Secure.putIntForUser(getContext().getContentResolver(),
183 Secure.NOTIFICATION_BADGING, 1,
Geoffrey Pitsch07532c32017-07-18 11:44:06 -0400184 UserHandle.getUserHandleForUid(mUid).getIdentifier());
Chris Wren89aa2262017-05-05 18:05:56 -0400185
Julia Reynolds503ed942017-10-04 16:04:56 -0400186 mService = new TestableNotificationManagerService(mContext);
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -0500187
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400188 // Use this testable looper.
189 mTestableLooper = TestableLooper.get(this);
Julia Reynolds503ed942017-10-04 16:04:56 -0400190 mHandler = mService.new WorkerHandler(mTestableLooper.getLooper());
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -0500191 // MockPackageManager - default returns ApplicationInfo with matching calling UID
Julia Reynolds92febc32017-10-26 11:30:31 -0400192 mContext.setMockPackageManager(mPackageManagerClient);
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -0500193 final ApplicationInfo applicationInfo = new ApplicationInfo();
Geoffrey Pitsch07532c32017-07-18 11:44:06 -0400194 applicationInfo.uid = mUid;
Julia Reynolds73ed76b2017-04-04 17:04:38 -0400195 when(mPackageManager.getApplicationInfo(anyString(), anyInt(), anyInt()))
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -0500196 .thenReturn(applicationInfo);
Julia Reynolds5f20e9f2017-01-30 08:54:53 -0500197 when(mPackageManagerClient.getApplicationInfoAsUser(anyString(), anyInt(), anyInt()))
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500198 .thenReturn(applicationInfo);
Julia Reynolds92febc32017-10-26 11:30:31 -0400199 when(mPackageManagerClient.getPackageUidAsUser(any(), anyInt())).thenReturn(mUid);
Geoffrey Pitsch03533712017-01-05 10:30:07 -0500200 final LightsManager mockLightsManager = mock(LightsManager.class);
201 when(mockLightsManager.getLight(anyInt())).thenReturn(mock(Light.class));
Julia Reynolds76c096d2017-06-19 08:16:04 -0400202 when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_NORMAL);
Julia Reynoldse1816412017-10-24 10:39:11 -0400203 when(mPackageManagerClient.hasSystemFeature(FEATURE_WATCH)).thenReturn(false);
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500204
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -0400205 // write to a test file; the system file isn't readable from tests
Julia Reynoldsb852e562017-06-06 16:14:18 -0400206 mFile = new File(mContext.getCacheDir(), "test.xml");
207 mFile.createNewFile();
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -0400208 final String preupgradeXml = "<notification-policy></notification-policy>";
209 mPolicyFile = new AtomicFile(mFile);
210 FileOutputStream fos = mPolicyFile.startWrite();
211 fos.write(preupgradeXml.getBytes());
212 mPolicyFile.finishWrite(fos);
213 FileInputStream fStream = new FileInputStream(mFile);
Julia Reynoldsb852e562017-06-06 16:14:18 -0400214
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -0400215 // Setup managed services
216 mListener = mListeners.new ManagedServiceInfo(
Geoffrey Pitsch07532c32017-07-18 11:44:06 -0400217 null, new ComponentName(PKG, "test_class"), mUid, true, null, 0);
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -0400218 when(mListeners.checkServiceTokenLocked(any())).thenReturn(mListener);
219 ManagedServices.Config listenerConfig = new ManagedServices.Config();
220 listenerConfig.xmlTag = NotificationListeners.TAG_ENABLED_NOTIFICATION_LISTENERS;
221 when(mListeners.getConfig()).thenReturn(listenerConfig);
222 ManagedServices.Config assistantConfig = new ManagedServices.Config();
223 assistantConfig.xmlTag = NotificationAssistants.TAG_ENABLED_NOTIFICATION_ASSISTANTS;
224 when(mAssistants.getConfig()).thenReturn(assistantConfig);
225 ManagedServices.Config dndConfig = new ManagedServices.Config();
226 dndConfig.xmlTag = ConditionProviders.TAG_ENABLED_DND_APPS;
227 when(mConditionProviders.getConfig()).thenReturn(dndConfig);
228
Julia Reynoldsb852e562017-06-06 16:14:18 -0400229 try {
Julia Reynolds503ed942017-10-04 16:04:56 -0400230 mService.init(mTestableLooper.getLooper(),
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400231 mPackageManager, mPackageManagerClient, mockLightsManager,
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -0400232 mListeners, mAssistants, mConditionProviders,
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400233 mCompanionMgr, mSnoozeHelper, mUsageStats, mPolicyFile, mActivityManager,
234 mGroupHelper);
Julia Reynoldsb852e562017-06-06 16:14:18 -0400235 } catch (SecurityException e) {
236 if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) {
237 throw e;
238 }
239 }
Julia Reynolds503ed942017-10-04 16:04:56 -0400240 mService.setAudioManager(mAudioManager);
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -0500241
242 // Tests call directly into the Binder.
Julia Reynolds503ed942017-10-04 16:04:56 -0400243 mBinderService = mService.getBinderService();
244 mInternalService = mService.getInternalService();
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500245
246 mBinderService.createNotificationChannels(
247 PKG, new ParceledListSlice(Arrays.asList(mTestNotificationChannel)));
Julia Reynolds92febc32017-10-26 11:30:31 -0400248 assertNotNull(mBinderService.getNotificationChannel(PKG, TEST_CHANNEL_ID));
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -0500249 }
250
Julia Reynoldsb852e562017-06-06 16:14:18 -0400251 @After
252 public void tearDown() throws Exception {
253 mFile.delete();
254 }
255
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500256 public void waitForIdle() throws Exception {
Geoffrey Pitsch415e4542017-04-10 13:12:58 -0400257 mTestableLooper.processAllMessages();
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500258 }
259
Julia Reynoldsa78cdff2017-04-26 10:19:25 -0400260 private NotificationRecord generateNotificationRecord(NotificationChannel channel, int id,
261 String groupKey, boolean isSummary) {
262 Notification.Builder nb = new Notification.Builder(mContext, channel.getId())
263 .setContentTitle("foo")
264 .setSmallIcon(android.R.drawable.sym_def_app_icon)
265 .setGroup(groupKey)
266 .setGroupSummary(isSummary);
267
Geoffrey Pitsch07532c32017-07-18 11:44:06 -0400268 StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, id, "tag", mUid, 0,
269 nb.build(), new UserHandle(mUid), null, 0);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000270 return new NotificationRecord(mContext, sbn, channel);
Julia Reynoldsa78cdff2017-04-26 10:19:25 -0400271 }
Julia Reynolds8617e4e2017-09-18 16:52:37 -0400272
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500273 private NotificationRecord generateNotificationRecord(NotificationChannel channel) {
Julia Reynolds5f20e9f2017-01-30 08:54:53 -0500274 return generateNotificationRecord(channel, null);
275 }
276
277 private NotificationRecord generateNotificationRecord(NotificationChannel channel,
278 Notification.TvExtender extender) {
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500279 if (channel == null) {
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500280 channel = mTestNotificationChannel;
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500281 }
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500282 Notification.Builder nb = new Notification.Builder(mContext, channel.getId())
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500283 .setContentTitle("foo")
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500284 .setSmallIcon(android.R.drawable.sym_def_app_icon);
Julia Reynolds5f20e9f2017-01-30 08:54:53 -0500285 if (extender != null) {
286 nb.extend(extender);
287 }
Geoffrey Pitsch07532c32017-07-18 11:44:06 -0400288 StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, "tag", mUid, 0,
289 nb.build(), new UserHandle(mUid), null, 0);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000290 return new NotificationRecord(mContext, sbn, channel);
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500291 }
292
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400293 private Map<String, Answer> getSignalExtractorSideEffects() {
294 Map<String, Answer> answers = new ArrayMap<>();
295
296 answers.put("override group key", invocationOnMock -> {
297 ((NotificationRecord) invocationOnMock.getArguments()[0])
298 .setOverrideGroupKey("bananas");
299 return null;
300 });
301 answers.put("override people", invocationOnMock -> {
302 ((NotificationRecord) invocationOnMock.getArguments()[0])
303 .setPeopleOverride(new ArrayList<>());
304 return null;
305 });
306 answers.put("snooze criteria", invocationOnMock -> {
307 ((NotificationRecord) invocationOnMock.getArguments()[0])
308 .setSnoozeCriteria(new ArrayList<>());
309 return null;
310 });
311 answers.put("notification channel", invocationOnMock -> {
312 ((NotificationRecord) invocationOnMock.getArguments()[0])
313 .updateNotificationChannel(new NotificationChannel("a", "", IMPORTANCE_LOW));
314 return null;
315 });
316 answers.put("badging", invocationOnMock -> {
317 NotificationRecord r = (NotificationRecord) invocationOnMock.getArguments()[0];
318 r.setShowBadge(!r.canShowBadge());
319 return null;
320 });
321 answers.put("package visibility", invocationOnMock -> {
322 ((NotificationRecord) invocationOnMock.getArguments()[0]).setPackageVisibilityOverride(
323 Notification.VISIBILITY_SECRET);
324 return null;
325 });
326
327 return answers;
328 }
329
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500330 @Test
Geoffrey Pitsch03533712017-01-05 10:30:07 -0500331 public void testCreateNotificationChannels_SingleChannel() throws Exception {
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -0500332 final NotificationChannel channel =
333 new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_DEFAULT);
Geoffrey Pitsch07532c32017-07-18 11:44:06 -0400334 mBinderService.createNotificationChannels(PKG,
Geoffrey Pitsch03533712017-01-05 10:30:07 -0500335 new ParceledListSlice(Arrays.asList(channel)));
336 final NotificationChannel createdChannel =
Geoffrey Pitsch07532c32017-07-18 11:44:06 -0400337 mBinderService.getNotificationChannel(PKG, "id");
Geoffrey Pitsch03533712017-01-05 10:30:07 -0500338 assertTrue(createdChannel != null);
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -0500339 }
340
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500341 @Test
Geoffrey Pitsch03533712017-01-05 10:30:07 -0500342 public void testCreateNotificationChannels_NullChannelThrowsException() throws Exception {
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -0500343 try {
Geoffrey Pitsch07532c32017-07-18 11:44:06 -0400344 mBinderService.createNotificationChannels(PKG,
Geoffrey Pitsch03533712017-01-05 10:30:07 -0500345 new ParceledListSlice(Arrays.asList(null)));
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -0500346 fail("Exception should be thrown immediately.");
347 } catch (NullPointerException e) {
348 // pass
349 }
350 }
Julia Reynoldsbaff4002016-12-15 11:34:26 -0500351
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500352 @Test
Geoffrey Pitsch03533712017-01-05 10:30:07 -0500353 public void testCreateNotificationChannels_TwoChannels() throws Exception {
354 final NotificationChannel channel1 =
355 new NotificationChannel("id1", "name", NotificationManager.IMPORTANCE_DEFAULT);
356 final NotificationChannel channel2 =
357 new NotificationChannel("id2", "name", NotificationManager.IMPORTANCE_DEFAULT);
Geoffrey Pitsch07532c32017-07-18 11:44:06 -0400358 mBinderService.createNotificationChannels(PKG,
Geoffrey Pitsch03533712017-01-05 10:30:07 -0500359 new ParceledListSlice(Arrays.asList(channel1, channel2)));
Geoffrey Pitsch07532c32017-07-18 11:44:06 -0400360 assertTrue(mBinderService.getNotificationChannel(PKG, "id1") != null);
361 assertTrue(mBinderService.getNotificationChannel(PKG, "id2") != null);
Geoffrey Pitsch03533712017-01-05 10:30:07 -0500362 }
363
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500364 @Test
Geoffrey Pitsch76a3aa02017-07-26 15:07:34 -0400365 public void testCreateNotificationChannels_SecondCreateDoesNotChangeImportance()
366 throws Exception {
367 final NotificationChannel channel =
368 new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_DEFAULT);
369 mBinderService.createNotificationChannels(PKG,
370 new ParceledListSlice(Arrays.asList(channel)));
371
372 // Recreating the channel doesn't throw, but ignores importance.
373 final NotificationChannel dupeChannel =
Julia Reynolds8617e4e2017-09-18 16:52:37 -0400374 new NotificationChannel("id", "name", IMPORTANCE_HIGH);
Geoffrey Pitsch76a3aa02017-07-26 15:07:34 -0400375 mBinderService.createNotificationChannels(PKG,
376 new ParceledListSlice(Arrays.asList(dupeChannel)));
377 final NotificationChannel createdChannel =
378 mBinderService.getNotificationChannel(PKG, "id");
379 assertEquals(NotificationManager.IMPORTANCE_DEFAULT, createdChannel.getImportance());
380 }
381
382 @Test
383 public void testCreateNotificationChannels_SecondCreateAllowedToDowngradeImportance()
384 throws Exception {
385 final NotificationChannel channel =
386 new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_DEFAULT);
387 mBinderService.createNotificationChannels(PKG,
388 new ParceledListSlice(Arrays.asList(channel)));
389
390 // Recreating with a lower importance is allowed to modify the channel.
391 final NotificationChannel dupeChannel =
392 new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_LOW);
393 mBinderService.createNotificationChannels(PKG,
394 new ParceledListSlice(Arrays.asList(dupeChannel)));
395 final NotificationChannel createdChannel =
396 mBinderService.getNotificationChannel(PKG, "id");
397 assertEquals(NotificationManager.IMPORTANCE_LOW, createdChannel.getImportance());
398 }
399
400 @Test
Geoffrey Pitsch07532c32017-07-18 11:44:06 -0400401 public void testCreateNotificationChannels_CannotDowngradeImportanceIfAlreadyUpdated()
402 throws Exception {
403 final NotificationChannel channel =
404 new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_DEFAULT);
405 mBinderService.createNotificationChannels(PKG,
406 new ParceledListSlice(Arrays.asList(channel)));
407
408 // The user modifies importance directly, can no longer be changed by the app.
409 final NotificationChannel updatedChannel =
Julia Reynolds8617e4e2017-09-18 16:52:37 -0400410 new NotificationChannel("id", "name", IMPORTANCE_HIGH);
Geoffrey Pitsch07532c32017-07-18 11:44:06 -0400411 mBinderService.updateNotificationChannelForPackage(PKG, mUid, updatedChannel);
412
413 // Recreating with a lower importance leaves channel unchanged.
414 final NotificationChannel dupeChannel =
415 new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_LOW);
416 mBinderService.createNotificationChannels(PKG,
417 new ParceledListSlice(Arrays.asList(dupeChannel)));
418 final NotificationChannel createdChannel =
419 mBinderService.getNotificationChannel(PKG, "id");
Julia Reynolds8617e4e2017-09-18 16:52:37 -0400420 assertEquals(IMPORTANCE_HIGH, createdChannel.getImportance());
Geoffrey Pitsch07532c32017-07-18 11:44:06 -0400421 }
422
423 @Test
Geoffrey Pitsch03533712017-01-05 10:30:07 -0500424 public void testCreateNotificationChannels_IdenticalChannelsInListIgnoresSecond()
425 throws Exception {
426 final NotificationChannel channel1 =
427 new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_DEFAULT);
428 final NotificationChannel channel2 =
Julia Reynolds8617e4e2017-09-18 16:52:37 -0400429 new NotificationChannel("id", "name", IMPORTANCE_HIGH);
Geoffrey Pitsch07532c32017-07-18 11:44:06 -0400430 mBinderService.createNotificationChannels(PKG,
Geoffrey Pitsch03533712017-01-05 10:30:07 -0500431 new ParceledListSlice(Arrays.asList(channel1, channel2)));
432 final NotificationChannel createdChannel =
Geoffrey Pitsch07532c32017-07-18 11:44:06 -0400433 mBinderService.getNotificationChannel(PKG, "id");
Geoffrey Pitsch03533712017-01-05 10:30:07 -0500434 assertEquals(NotificationManager.IMPORTANCE_DEFAULT, createdChannel.getImportance());
435 }
436
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500437 @Test
Julia Reynoldsbaff4002016-12-15 11:34:26 -0500438 public void testBlockedNotifications_suspended() throws Exception {
Julia Reynoldsbaff4002016-12-15 11:34:26 -0500439 when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(true);
440
441 NotificationChannel channel = new NotificationChannel("id", "name",
Julia Reynolds8617e4e2017-09-18 16:52:37 -0400442 IMPORTANCE_HIGH);
Julia Reynoldsbaff4002016-12-15 11:34:26 -0500443 NotificationRecord r = generateNotificationRecord(channel);
Julia Reynolds503ed942017-10-04 16:04:56 -0400444 assertTrue(mService.isBlocked(r, mUsageStats));
Geoffrey Pitschd5bcf212017-06-01 15:45:35 -0400445 verify(mUsageStats, times(1)).registerSuspendedByAdmin(eq(r));
Julia Reynoldsbaff4002016-12-15 11:34:26 -0500446 }
447
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500448 @Test
Julia Reynoldsbaff4002016-12-15 11:34:26 -0500449 public void testBlockedNotifications_blockedChannel() throws Exception {
Julia Reynoldsbaff4002016-12-15 11:34:26 -0500450 when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false);
451
452 NotificationChannel channel = new NotificationChannel("id", "name",
Julia Reynolds8617e4e2017-09-18 16:52:37 -0400453 NotificationManager.IMPORTANCE_NONE);
Julia Reynoldsbaff4002016-12-15 11:34:26 -0500454 NotificationRecord r = generateNotificationRecord(channel);
Julia Reynolds503ed942017-10-04 16:04:56 -0400455 assertTrue(mService.isBlocked(r, mUsageStats));
Geoffrey Pitschd5bcf212017-06-01 15:45:35 -0400456 verify(mUsageStats, times(1)).registerBlocked(eq(r));
Julia Reynolds8617e4e2017-09-18 16:52:37 -0400457
458 mBinderService.createNotificationChannels(
459 PKG, new ParceledListSlice(Arrays.asList(channel)));
460 final StatusBarNotification sbn = generateNotificationRecord(channel).sbn;
461 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
462 sbn.getId(), sbn.getNotification(), sbn.getUserId());
463 waitForIdle();
464 assertEquals(0, mBinderService.getActiveNotifications(sbn.getPackageName()).length);
465 }
466
467 @Test
468 public void testEnqueuedBlockedNotifications_appBlockedChannelForegroundService()
469 throws Exception {
470 when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false);
471
472 NotificationChannel channel = new NotificationChannel("blocked", "name",
473 NotificationManager.IMPORTANCE_NONE);
474 mBinderService.createNotificationChannels(
475 PKG, new ParceledListSlice(Arrays.asList(channel)));
476
477 final StatusBarNotification sbn = generateNotificationRecord(channel).sbn;
478 sbn.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE;
479 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
480 sbn.getId(), sbn.getNotification(), sbn.getUserId());
481 waitForIdle();
482 assertEquals(1, mBinderService.getActiveNotifications(sbn.getPackageName()).length);
483 assertEquals(IMPORTANCE_LOW,
Julia Reynolds503ed942017-10-04 16:04:56 -0400484 mService.getNotificationRecord(sbn.getKey()).getImportance());
Julia Reynolds8617e4e2017-09-18 16:52:37 -0400485 assertEquals(IMPORTANCE_LOW,
486 mBinderService.getNotificationChannel(PKG, channel.getId()).getImportance());
487 }
488
489 @Test
490 public void testEnqueuedBlockedNotifications_userBlockedChannelForegroundService()
491 throws Exception {
492 when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false);
493
494 NotificationChannel channel =
495 new NotificationChannel("blockedbyuser", "name", IMPORTANCE_HIGH);
496 mBinderService.createNotificationChannels(
497 PKG, new ParceledListSlice(Arrays.asList(channel)));
498
499 NotificationChannel update =
500 new NotificationChannel("blockedbyuser", "name", IMPORTANCE_NONE);
501 mBinderService.updateNotificationChannelForPackage(PKG, mUid, update);
502 waitForIdle();
503 assertEquals(IMPORTANCE_NONE,
504 mBinderService.getNotificationChannel(PKG, channel.getId()).getImportance());
505
506 final StatusBarNotification sbn = generateNotificationRecord(channel).sbn;
507 sbn.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE;
508 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
509 sbn.getId(), sbn.getNotification(), sbn.getUserId());
510 waitForIdle();
511 assertEquals(0, mBinderService.getActiveNotifications(sbn.getPackageName()).length);
Julia Reynolds503ed942017-10-04 16:04:56 -0400512 assertNull(mService.getNotificationRecord(sbn.getKey()));
Julia Reynolds8617e4e2017-09-18 16:52:37 -0400513 assertEquals(IMPORTANCE_NONE,
514 mBinderService.getNotificationChannel(PKG, channel.getId()).getImportance());
Julia Reynoldsbaff4002016-12-15 11:34:26 -0500515 }
516
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500517 @Test
Julia Reynolds005c8b92017-08-24 10:35:53 -0400518 public void testBlockedNotifications_blockedChannelGroup() throws Exception {
519 when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false);
Julia Reynolds503ed942017-10-04 16:04:56 -0400520 mService.setRankingHelper(mRankingHelper);
Julia Reynolds005c8b92017-08-24 10:35:53 -0400521 when(mRankingHelper.isGroupBlocked(anyString(), anyInt(), anyString())).thenReturn(true);
522
523 NotificationChannel channel = new NotificationChannel("id", "name",
524 NotificationManager.IMPORTANCE_HIGH);
525 channel.setGroup("something");
526 NotificationRecord r = generateNotificationRecord(channel);
Julia Reynolds503ed942017-10-04 16:04:56 -0400527 assertTrue(mService.isBlocked(r, mUsageStats));
Julia Reynolds005c8b92017-08-24 10:35:53 -0400528 verify(mUsageStats, times(1)).registerBlocked(eq(r));
529 }
530
531 @Test
Julia Reynolds4da79702017-06-01 11:06:10 -0400532 public void testEnqueuedBlockedNotifications_blockedApp() throws Exception {
Julia Reynoldsbaff4002016-12-15 11:34:26 -0500533 when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false);
534
Geoffrey Pitsch07532c32017-07-18 11:44:06 -0400535 mBinderService.setNotificationsEnabledForPackage(PKG, mUid, false);
Julia Reynolds4da79702017-06-01 11:06:10 -0400536
537 final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
538 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
539 sbn.getId(), sbn.getNotification(), sbn.getUserId());
540 waitForIdle();
541 assertEquals(0, mBinderService.getActiveNotifications(sbn.getPackageName()).length);
Julia Reynoldsbaff4002016-12-15 11:34:26 -0500542 }
543
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500544 @Test
Julia Reynolds8617e4e2017-09-18 16:52:37 -0400545 public void testEnqueuedBlockedNotifications_blockedAppForegroundService() throws Exception {
546 when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false);
547
548 mBinderService.setNotificationsEnabledForPackage(PKG, mUid, false);
549
550 final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
551 sbn.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE;
552 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
553 sbn.getId(), sbn.getNotification(), sbn.getUserId());
554 waitForIdle();
555 assertEquals(0, mBinderService.getActiveNotifications(sbn.getPackageName()).length);
Julia Reynolds503ed942017-10-04 16:04:56 -0400556 assertNull(mService.getNotificationRecord(sbn.getKey()));
Julia Reynolds8617e4e2017-09-18 16:52:37 -0400557 }
558
559 @Test
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500560 public void testEnqueueNotificationWithTag_PopulatesGetActiveNotifications() throws Exception {
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500561 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0,
Julia Reynoldsfea6f7b2017-04-19 13:50:12 -0400562 generateNotificationRecord(null).getNotification(), 0);
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500563 waitForIdle();
Julia Reynolds080361e2017-07-13 11:23:12 -0400564 StatusBarNotification[] notifs = mBinderService.getActiveNotifications(PKG);
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500565 assertEquals(1, notifs.length);
Julia Reynolds503ed942017-10-04 16:04:56 -0400566 assertEquals(1, mService.getNotificationRecordCount());
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500567 }
568
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500569 @Test
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500570 public void testCancelNotificationImmediatelyAfterEnqueue() throws Exception {
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500571 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0,
Julia Reynoldsfea6f7b2017-04-19 13:50:12 -0400572 generateNotificationRecord(null).getNotification(), 0);
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500573 mBinderService.cancelNotificationWithTag(PKG, "tag", 0, 0);
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500574 waitForIdle();
575 StatusBarNotification[] notifs =
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500576 mBinderService.getActiveNotifications(PKG);
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500577 assertEquals(0, notifs.length);
Julia Reynolds503ed942017-10-04 16:04:56 -0400578 assertEquals(0, mService.getNotificationRecordCount());
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500579 }
580
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500581 @Test
Geoffrey Pitschccc0b972017-02-15 10:52:26 -0500582 public void testCancelNotificationWhilePostedAndEnqueued() throws Exception {
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500583 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0,
Julia Reynoldsfea6f7b2017-04-19 13:50:12 -0400584 generateNotificationRecord(null).getNotification(), 0);
Geoffrey Pitschccc0b972017-02-15 10:52:26 -0500585 waitForIdle();
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500586 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0,
Julia Reynoldsfea6f7b2017-04-19 13:50:12 -0400587 generateNotificationRecord(null).getNotification(), 0);
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500588 mBinderService.cancelNotificationWithTag(PKG, "tag", 0, 0);
Geoffrey Pitschccc0b972017-02-15 10:52:26 -0500589 waitForIdle();
590 StatusBarNotification[] notifs =
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500591 mBinderService.getActiveNotifications(PKG);
Geoffrey Pitschccc0b972017-02-15 10:52:26 -0500592 assertEquals(0, notifs.length);
Julia Reynolds503ed942017-10-04 16:04:56 -0400593 assertEquals(0, mService.getNotificationRecordCount());
594 ArgumentCaptor<NotificationStats> captor = ArgumentCaptor.forClass(NotificationStats.class);
595 verify(mListeners, times(1)).notifyRemovedLocked(any(), anyInt(), captor.capture());
596 assertEquals(NotificationStats.DISMISSAL_OTHER, captor.getValue().getDismissalSurface());
Geoffrey Pitschccc0b972017-02-15 10:52:26 -0500597 }
598
599 @Test
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500600 public void testCancelNotificationsFromListenerImmediatelyAfterEnqueue() throws Exception {
Julia Reynolds503ed942017-10-04 16:04:56 -0400601 NotificationRecord r = generateNotificationRecord(null);
602 final StatusBarNotification sbn = r.sbn;
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500603 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
Julia Reynoldsfea6f7b2017-04-19 13:50:12 -0400604 sbn.getId(), sbn.getNotification(), sbn.getUserId());
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500605 mBinderService.cancelNotificationsFromListener(null, null);
606 waitForIdle();
607 StatusBarNotification[] notifs =
608 mBinderService.getActiveNotifications(sbn.getPackageName());
609 assertEquals(0, notifs.length);
Julia Reynolds503ed942017-10-04 16:04:56 -0400610 assertEquals(0, mService.getNotificationRecordCount());
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500611 }
612
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500613 @Test
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500614 public void testCancelAllNotificationsImmediatelyAfterEnqueue() throws Exception {
615 final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500616 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
Julia Reynoldsfea6f7b2017-04-19 13:50:12 -0400617 sbn.getId(), sbn.getNotification(), sbn.getUserId());
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500618 mBinderService.cancelAllNotifications(PKG, sbn.getUserId());
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500619 waitForIdle();
620 StatusBarNotification[] notifs =
621 mBinderService.getActiveNotifications(sbn.getPackageName());
622 assertEquals(0, notifs.length);
Julia Reynolds503ed942017-10-04 16:04:56 -0400623 assertEquals(0, mService.getNotificationRecordCount());
Julia Reynolds080361e2017-07-13 11:23:12 -0400624 }
625
626 @Test
627 public void testUserInitiatedClearAll_noLeak() throws Exception {
628 final NotificationRecord n = generateNotificationRecord(
629 mTestNotificationChannel, 1, "group", true);
630
631 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
632 n.sbn.getId(), n.sbn.getNotification(), n.sbn.getUserId());
633 waitForIdle();
634
Julia Reynolds503ed942017-10-04 16:04:56 -0400635 mService.mNotificationDelegate.onClearAll(mUid, Binder.getCallingPid(),
Julia Reynolds080361e2017-07-13 11:23:12 -0400636 n.getUserId());
637 waitForIdle();
638 StatusBarNotification[] notifs =
639 mBinderService.getActiveNotifications(n.sbn.getPackageName());
640 assertEquals(0, notifs.length);
Julia Reynolds503ed942017-10-04 16:04:56 -0400641 assertEquals(0, mService.getNotificationRecordCount());
642 ArgumentCaptor<NotificationStats> captor = ArgumentCaptor.forClass(NotificationStats.class);
643 verify(mListeners, times(1)).notifyRemovedLocked(any(), anyInt(), captor.capture());
644 assertEquals(NotificationStats.DISMISSAL_OTHER, captor.getValue().getDismissalSurface());
Julia Reynolds080361e2017-07-13 11:23:12 -0400645 }
646
647 @Test
648 public void testCancelAllNotificationsCancelsChildren() throws Exception {
649 final NotificationRecord parent = generateNotificationRecord(
650 mTestNotificationChannel, 1, "group1", true);
651 final NotificationRecord child = generateNotificationRecord(
652 mTestNotificationChannel, 2, "group1", false);
653
654 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
655 parent.sbn.getId(), parent.sbn.getNotification(), parent.sbn.getUserId());
656 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
657 child.sbn.getId(), child.sbn.getNotification(), child.sbn.getUserId());
658 waitForIdle();
659
660 mBinderService.cancelAllNotifications(PKG, parent.sbn.getUserId());
661 waitForIdle();
Julia Reynolds503ed942017-10-04 16:04:56 -0400662 assertEquals(0, mService.getNotificationRecordCount());
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500663 }
664
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500665 @Test
Julia Reynolds0839c022017-06-15 15:24:01 -0400666 public void testCancelAllNotificationsMultipleEnqueuedDoesNotCrash() throws Exception {
667 final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
668 for (int i = 0; i < 10; i++) {
669 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
670 sbn.getId(), sbn.getNotification(), sbn.getUserId());
671 }
672 mBinderService.cancelAllNotifications(PKG, sbn.getUserId());
673 waitForIdle();
Julia Reynolds080361e2017-07-13 11:23:12 -0400674
Julia Reynolds503ed942017-10-04 16:04:56 -0400675 assertEquals(0, mService.getNotificationRecordCount());
Julia Reynolds0839c022017-06-15 15:24:01 -0400676 }
677
678 @Test
679 public void testCancelGroupSummaryMultipleEnqueuedChildrenDoesNotCrash() throws Exception {
680 final NotificationRecord parent = generateNotificationRecord(
681 mTestNotificationChannel, 1, "group1", true);
682 final NotificationRecord parentAsChild = generateNotificationRecord(
683 mTestNotificationChannel, 1, "group1", false);
684 final NotificationRecord child = generateNotificationRecord(
685 mTestNotificationChannel, 2, "group1", false);
686
687 // fully post parent notification
688 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
689 parent.sbn.getId(), parent.sbn.getNotification(), parent.sbn.getUserId());
690 waitForIdle();
691
692 // enqueue the child several times
693 for (int i = 0; i < 10; i++) {
694 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
695 child.sbn.getId(), child.sbn.getNotification(), child.sbn.getUserId());
696 }
697 // make the parent a child, which will cancel the child notification
698 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
699 parentAsChild.sbn.getId(), parentAsChild.sbn.getNotification(),
700 parentAsChild.sbn.getUserId());
701 waitForIdle();
Julia Reynolds080361e2017-07-13 11:23:12 -0400702
Julia Reynolds503ed942017-10-04 16:04:56 -0400703 assertEquals(0, mService.getNotificationRecordCount());
Julia Reynolds0839c022017-06-15 15:24:01 -0400704 }
705
706 @Test
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500707 public void testCancelAllNotifications_IgnoreForegroundService() throws Exception {
708 final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
709 sbn.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE;
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500710 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
Julia Reynoldsfea6f7b2017-04-19 13:50:12 -0400711 sbn.getId(), sbn.getNotification(), sbn.getUserId());
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500712 mBinderService.cancelAllNotifications(PKG, sbn.getUserId());
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500713 waitForIdle();
714 StatusBarNotification[] notifs =
715 mBinderService.getActiveNotifications(sbn.getPackageName());
716 assertEquals(1, notifs.length);
Julia Reynolds503ed942017-10-04 16:04:56 -0400717 assertEquals(1, mService.getNotificationRecordCount());
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500718 }
719
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500720 @Test
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500721 public void testCancelAllNotifications_IgnoreOtherPackages() throws Exception {
722 final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
723 sbn.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE;
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500724 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
Julia Reynoldsfea6f7b2017-04-19 13:50:12 -0400725 sbn.getId(), sbn.getNotification(), sbn.getUserId());
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500726 mBinderService.cancelAllNotifications("other_pkg_name", sbn.getUserId());
727 waitForIdle();
728 StatusBarNotification[] notifs =
729 mBinderService.getActiveNotifications(sbn.getPackageName());
730 assertEquals(1, notifs.length);
Julia Reynolds503ed942017-10-04 16:04:56 -0400731 assertEquals(1, mService.getNotificationRecordCount());
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500732 }
733
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500734 @Test
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500735 public void testCancelAllNotifications_NullPkgRemovesAll() throws Exception {
736 final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500737 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
Julia Reynoldsfea6f7b2017-04-19 13:50:12 -0400738 sbn.getId(), sbn.getNotification(), sbn.getUserId());
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500739 mBinderService.cancelAllNotifications(null, sbn.getUserId());
740 waitForIdle();
741 StatusBarNotification[] notifs =
742 mBinderService.getActiveNotifications(sbn.getPackageName());
743 assertEquals(0, notifs.length);
Julia Reynolds503ed942017-10-04 16:04:56 -0400744 assertEquals(0, mService.getNotificationRecordCount());
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500745 }
746
Geoffrey Pitsch16594462017-01-26 14:42:30 -0500747 @Test
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500748 public void testCancelAllNotifications_NullPkgIgnoresUserAllNotifications() throws Exception {
749 final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500750 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
Julia Reynoldsfea6f7b2017-04-19 13:50:12 -0400751 sbn.getId(), sbn.getNotification(), UserHandle.USER_ALL);
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500752 // Null pkg is how we signal a user switch.
753 mBinderService.cancelAllNotifications(null, sbn.getUserId());
754 waitForIdle();
755 StatusBarNotification[] notifs =
756 mBinderService.getActiveNotifications(sbn.getPackageName());
757 assertEquals(1, notifs.length);
Julia Reynolds503ed942017-10-04 16:04:56 -0400758 assertEquals(1, mService.getNotificationRecordCount());
Geoffrey Pitsch331a64d2017-01-17 14:00:47 -0500759 }
Julia Reynolds5f20e9f2017-01-30 08:54:53 -0500760
761 @Test
Beverly40239d92017-07-07 10:20:41 -0400762 public void testAppInitiatedCancelAllNotifications_CancelsNoClearFlag() throws Exception {
763 final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
764 sbn.getNotification().flags |= Notification.FLAG_NO_CLEAR;
765 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
766 sbn.getId(), sbn.getNotification(), sbn.getUserId());
767 mBinderService.cancelAllNotifications(PKG, sbn.getUserId());
768 waitForIdle();
769 StatusBarNotification[] notifs =
770 mBinderService.getActiveNotifications(sbn.getPackageName());
771 assertEquals(0, notifs.length);
772 }
773
774 @Test
775 public void testCancelAllNotifications_CancelsNoClearFlag() throws Exception {
776 final NotificationRecord notif = generateNotificationRecord(
777 mTestNotificationChannel, 1, "group", true);
778 notif.getNotification().flags |= Notification.FLAG_NO_CLEAR;
Julia Reynolds503ed942017-10-04 16:04:56 -0400779 mService.addNotification(notif);
780 mService.cancelAllNotificationsInt(mUid, 0, PKG, null, 0, 0, true,
Beverly40239d92017-07-07 10:20:41 -0400781 notif.getUserId(), 0, null);
782 waitForIdle();
783 StatusBarNotification[] notifs =
784 mBinderService.getActiveNotifications(notif.sbn.getPackageName());
785 assertEquals(0, notifs.length);
786 }
787
788 @Test
789 public void testUserInitiatedCancelAllOnClearAll_NoClearFlag() throws Exception {
790 final NotificationRecord notif = generateNotificationRecord(
791 mTestNotificationChannel, 1, "group", true);
792 notif.getNotification().flags |= Notification.FLAG_NO_CLEAR;
Julia Reynolds503ed942017-10-04 16:04:56 -0400793 mService.addNotification(notif);
Beverly40239d92017-07-07 10:20:41 -0400794
Julia Reynolds503ed942017-10-04 16:04:56 -0400795 mService.mNotificationDelegate.onClearAll(mUid, Binder.getCallingPid(),
Beverly40239d92017-07-07 10:20:41 -0400796 notif.getUserId());
797 waitForIdle();
798 StatusBarNotification[] notifs =
799 mBinderService.getActiveNotifications(notif.sbn.getPackageName());
800 assertEquals(1, notifs.length);
801 }
802
803 @Test
804 public void testCancelAllCancelNotificationsFromListener_NoClearFlag() throws Exception {
805 final NotificationRecord parent = generateNotificationRecord(
806 mTestNotificationChannel, 1, "group", true);
807 final NotificationRecord child = generateNotificationRecord(
808 mTestNotificationChannel, 2, "group", false);
809 final NotificationRecord child2 = generateNotificationRecord(
810 mTestNotificationChannel, 3, "group", false);
811 child2.getNotification().flags |= Notification.FLAG_NO_CLEAR;
812 final NotificationRecord newGroup = generateNotificationRecord(
813 mTestNotificationChannel, 4, "group2", false);
Julia Reynolds503ed942017-10-04 16:04:56 -0400814 mService.addNotification(parent);
815 mService.addNotification(child);
816 mService.addNotification(child2);
817 mService.addNotification(newGroup);
818 mService.getBinderService().cancelNotificationsFromListener(null, null);
Beverly40239d92017-07-07 10:20:41 -0400819 waitForIdle();
820 StatusBarNotification[] notifs =
821 mBinderService.getActiveNotifications(parent.sbn.getPackageName());
822 assertEquals(1, notifs.length);
823 }
824
825 @Test
826 public void testUserInitiatedCancelAllWithGroup_NoClearFlag() throws Exception {
827 final NotificationRecord parent = generateNotificationRecord(
828 mTestNotificationChannel, 1, "group", true);
829 final NotificationRecord child = generateNotificationRecord(
830 mTestNotificationChannel, 2, "group", false);
831 final NotificationRecord child2 = generateNotificationRecord(
832 mTestNotificationChannel, 3, "group", false);
833 child2.getNotification().flags |= Notification.FLAG_NO_CLEAR;
834 final NotificationRecord newGroup = generateNotificationRecord(
835 mTestNotificationChannel, 4, "group2", false);
Julia Reynolds503ed942017-10-04 16:04:56 -0400836 mService.addNotification(parent);
837 mService.addNotification(child);
838 mService.addNotification(child2);
839 mService.addNotification(newGroup);
840 mService.mNotificationDelegate.onClearAll(mUid, Binder.getCallingPid(),
Beverly40239d92017-07-07 10:20:41 -0400841 parent.getUserId());
842 waitForIdle();
843 StatusBarNotification[] notifs =
844 mBinderService.getActiveNotifications(parent.sbn.getPackageName());
845 assertEquals(1, notifs.length);
846 }
847
848 @Test
Geoffrey Pitsch415e4542017-04-10 13:12:58 -0400849 public void testRemoveForegroundServiceFlag_ImmediatelyAfterEnqueue() throws Exception {
850 final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
851 sbn.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE;
852 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", null,
Julia Reynoldsfea6f7b2017-04-19 13:50:12 -0400853 sbn.getId(), sbn.getNotification(), sbn.getUserId());
Geoffrey Pitsch415e4542017-04-10 13:12:58 -0400854 mInternalService.removeForegroundServiceFlagFromNotification(PKG, sbn.getId(),
855 sbn.getUserId());
856 waitForIdle();
857 StatusBarNotification[] notifs =
858 mBinderService.getActiveNotifications(sbn.getPackageName());
859 assertEquals(0, notifs[0].getNotification().flags & Notification.FLAG_FOREGROUND_SERVICE);
860 }
861
862 @Test
Geoffrey Pitsch27684152017-05-02 11:41:31 -0400863 public void testCancelAfterSecondEnqueueDoesNotSpecifyForegroundFlag() throws Exception {
864 final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
865 sbn.getNotification().flags =
866 Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE;
867 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
868 sbn.getId(), sbn.getNotification(), sbn.getUserId());
869 sbn.getNotification().flags = Notification.FLAG_ONGOING_EVENT;
870 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
871 sbn.getId(), sbn.getNotification(), sbn.getUserId());
872 mBinderService.cancelNotificationWithTag(PKG, "tag", sbn.getId(), sbn.getUserId());
873 waitForIdle();
874 assertEquals(0, mBinderService.getActiveNotifications(sbn.getPackageName()).length);
Julia Reynolds503ed942017-10-04 16:04:56 -0400875 assertEquals(0, mService.getNotificationRecordCount());
Geoffrey Pitsch27684152017-05-02 11:41:31 -0400876 }
877
878 @Test
Julia Reynolds40f00d72017-12-12 10:47:32 -0500879 public void testCancelAllCancelNotificationsFromListener_ForegroundServiceFlag()
880 throws Exception {
881 final NotificationRecord parent = generateNotificationRecord(
882 mTestNotificationChannel, 1, "group", true);
883 final NotificationRecord child = generateNotificationRecord(
884 mTestNotificationChannel, 2, "group", false);
885 final NotificationRecord child2 = generateNotificationRecord(
886 mTestNotificationChannel, 3, "group", false);
887 child2.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE;
888 final NotificationRecord newGroup = generateNotificationRecord(
889 mTestNotificationChannel, 4, "group2", false);
890 mService.addNotification(parent);
891 mService.addNotification(child);
892 mService.addNotification(child2);
893 mService.addNotification(newGroup);
894 mService.getBinderService().cancelNotificationsFromListener(null, null);
895 waitForIdle();
896 StatusBarNotification[] notifs =
897 mBinderService.getActiveNotifications(parent.sbn.getPackageName());
898 assertEquals(0, notifs.length);
899 }
900
901 @Test
902 public void testCancelAllCancelNotificationsFromListener_ForegroundServiceFlagWithParameter()
903 throws Exception {
904 final NotificationRecord parent = generateNotificationRecord(
905 mTestNotificationChannel, 1, "group", true);
906 final NotificationRecord child = generateNotificationRecord(
907 mTestNotificationChannel, 2, "group", false);
908 final NotificationRecord child2 = generateNotificationRecord(
909 mTestNotificationChannel, 3, "group", false);
910 child2.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE;
911 final NotificationRecord newGroup = generateNotificationRecord(
912 mTestNotificationChannel, 4, "group2", false);
913 mService.addNotification(parent);
914 mService.addNotification(child);
915 mService.addNotification(child2);
916 mService.addNotification(newGroup);
917 String[] keys = {parent.sbn.getKey(), child.sbn.getKey(),
918 child2.sbn.getKey(), newGroup.sbn.getKey()};
919 mService.getBinderService().cancelNotificationsFromListener(null, keys);
920 waitForIdle();
921 StatusBarNotification[] notifs =
922 mBinderService.getActiveNotifications(parent.sbn.getPackageName());
923 assertEquals(1, notifs.length);
924 }
925
926 @Test
927 public void testUserInitiatedCancelAllWithGroup_ForegroundServiceFlag() throws Exception {
928 final NotificationRecord parent = generateNotificationRecord(
929 mTestNotificationChannel, 1, "group", true);
930 final NotificationRecord child = generateNotificationRecord(
931 mTestNotificationChannel, 2, "group", false);
932 final NotificationRecord child2 = generateNotificationRecord(
933 mTestNotificationChannel, 3, "group", false);
934 child2.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE;
935 final NotificationRecord newGroup = generateNotificationRecord(
936 mTestNotificationChannel, 4, "group2", false);
937 mService.addNotification(parent);
938 mService.addNotification(child);
939 mService.addNotification(child2);
940 mService.addNotification(newGroup);
941 mService.mNotificationDelegate.onClearAll(mUid, Binder.getCallingPid(),
942 parent.getUserId());
943 waitForIdle();
944 StatusBarNotification[] notifs =
945 mBinderService.getActiveNotifications(parent.sbn.getPackageName());
946 assertEquals(0, notifs.length);
947 }
948
949 @Test
Julia Reynoldsa78cdff2017-04-26 10:19:25 -0400950 public void testFindGroupNotificationsLocked() throws Exception {
951 // make sure the same notification can be found in both lists and returned
952 final NotificationRecord group1 = generateNotificationRecord(
953 mTestNotificationChannel, 1, "group1", true);
Julia Reynolds503ed942017-10-04 16:04:56 -0400954 mService.addEnqueuedNotification(group1);
955 mService.addNotification(group1);
Julia Reynoldsa78cdff2017-04-26 10:19:25 -0400956
957 // should not be returned
958 final NotificationRecord group2 = generateNotificationRecord(
959 mTestNotificationChannel, 2, "group2", true);
960 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", null,
961 group2.sbn.getId(), group2.sbn.getNotification(), group2.sbn.getUserId());
962 waitForIdle();
963
964 // should not be returned
965 final NotificationRecord nonGroup = generateNotificationRecord(
966 mTestNotificationChannel, 3, null, false);
967 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", null,
968 nonGroup.sbn.getId(), nonGroup.sbn.getNotification(), nonGroup.sbn.getUserId());
969 waitForIdle();
970
971 // same group, child, should be returned
972 final NotificationRecord group1Child = generateNotificationRecord(
973 mTestNotificationChannel, 4, "group1", false);
974 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", null, group1Child.sbn.getId(),
975 group1Child.sbn.getNotification(), group1Child.sbn.getUserId());
976 waitForIdle();
977
978 List<NotificationRecord> inGroup1 =
Julia Reynolds503ed942017-10-04 16:04:56 -0400979 mService.findGroupNotificationsLocked(PKG, group1.getGroupKey(),
Julia Reynoldsa78cdff2017-04-26 10:19:25 -0400980 group1.sbn.getUserId());
981 assertEquals(3, inGroup1.size());
982 for (NotificationRecord record : inGroup1) {
983 assertTrue(record.getGroupKey().equals(group1.getGroupKey()));
984 assertTrue(record.sbn.getId() == 1 || record.sbn.getId() == 4);
985 }
986 }
987
Julia Reynoldsa78cdff2017-04-26 10:19:25 -0400988 @Test
Julia Reynolds40f00d72017-12-12 10:47:32 -0500989 public void testCancelAllNotifications_CancelsNoClearFlagOnGoing() throws Exception {
990 final NotificationRecord notif = generateNotificationRecord(
991 mTestNotificationChannel, 1, "group", true);
992 notif.getNotification().flags |= Notification.FLAG_NO_CLEAR;
993 mService.addNotification(notif);
994 mService.cancelAllNotificationsInt(mUid, 0, PKG, null, 0,
995 Notification.FLAG_ONGOING_EVENT, true, notif.getUserId(), 0, null);
996 waitForIdle();
997 StatusBarNotification[] notifs =
998 mBinderService.getActiveNotifications(notif.sbn.getPackageName());
999 assertEquals(0, notifs.length);
1000 }
1001
1002 @Test
1003 public void testCancelAllCancelNotificationsFromListener_NoClearFlagWithParameter()
1004 throws Exception {
1005 final NotificationRecord parent = generateNotificationRecord(
1006 mTestNotificationChannel, 1, "group", true);
1007 final NotificationRecord child = generateNotificationRecord(
1008 mTestNotificationChannel, 2, "group", false);
1009 final NotificationRecord child2 = generateNotificationRecord(
1010 mTestNotificationChannel, 3, "group", false);
1011 child2.getNotification().flags |= Notification.FLAG_NO_CLEAR;
1012 final NotificationRecord newGroup = generateNotificationRecord(
1013 mTestNotificationChannel, 4, "group2", false);
1014 mService.addNotification(parent);
1015 mService.addNotification(child);
1016 mService.addNotification(child2);
1017 mService.addNotification(newGroup);
1018 String[] keys = {parent.sbn.getKey(), child.sbn.getKey(),
1019 child2.sbn.getKey(), newGroup.sbn.getKey()};
1020 mService.getBinderService().cancelNotificationsFromListener(null, keys);
1021 waitForIdle();
1022 StatusBarNotification[] notifs =
1023 mBinderService.getActiveNotifications(parent.sbn.getPackageName());
1024 assertEquals(0, notifs.length);
1025 }
1026
1027 @Test
1028 public void testAppInitiatedCancelAllNotifications_CancelsOnGoingFlag() throws Exception {
1029 final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
1030 sbn.getNotification().flags |= Notification.FLAG_ONGOING_EVENT;
1031 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
1032 sbn.getId(), sbn.getNotification(), sbn.getUserId());
1033 mBinderService.cancelAllNotifications(PKG, sbn.getUserId());
1034 waitForIdle();
1035 StatusBarNotification[] notifs =
1036 mBinderService.getActiveNotifications(sbn.getPackageName());
1037 assertEquals(0, notifs.length);
1038 }
1039
1040 @Test
1041 public void testCancelAllNotifications_CancelsOnGoingFlag() throws Exception {
1042 final NotificationRecord notif = generateNotificationRecord(
1043 mTestNotificationChannel, 1, "group", true);
1044 notif.getNotification().flags |= Notification.FLAG_ONGOING_EVENT;
1045 mService.addNotification(notif);
1046 mService.cancelAllNotificationsInt(mUid, 0, PKG, null, 0, 0, true,
1047 notif.getUserId(), 0, null);
1048 waitForIdle();
1049 StatusBarNotification[] notifs =
1050 mBinderService.getActiveNotifications(notif.sbn.getPackageName());
1051 assertEquals(0, notifs.length);
1052 }
1053
1054 @Test
1055 public void testUserInitiatedCancelAllOnClearAll_OnGoingFlag() throws Exception {
1056 final NotificationRecord notif = generateNotificationRecord(
1057 mTestNotificationChannel, 1, "group", true);
1058 notif.getNotification().flags |= Notification.FLAG_ONGOING_EVENT;
1059 mService.addNotification(notif);
1060
1061 mService.mNotificationDelegate.onClearAll(mUid, Binder.getCallingPid(),
1062 notif.getUserId());
1063 waitForIdle();
1064 StatusBarNotification[] notifs =
1065 mBinderService.getActiveNotifications(notif.sbn.getPackageName());
1066 assertEquals(1, notifs.length);
1067 }
1068
1069 @Test
1070 public void testCancelAllCancelNotificationsFromListener_OnGoingFlag() throws Exception {
1071 final NotificationRecord parent = generateNotificationRecord(
1072 mTestNotificationChannel, 1, "group", true);
1073 final NotificationRecord child = generateNotificationRecord(
1074 mTestNotificationChannel, 2, "group", false);
1075 final NotificationRecord child2 = generateNotificationRecord(
1076 mTestNotificationChannel, 3, "group", false);
1077 child2.getNotification().flags |= Notification.FLAG_ONGOING_EVENT;
1078 final NotificationRecord newGroup = generateNotificationRecord(
1079 mTestNotificationChannel, 4, "group2", false);
1080 mService.addNotification(parent);
1081 mService.addNotification(child);
1082 mService.addNotification(child2);
1083 mService.addNotification(newGroup);
1084 mService.getBinderService().cancelNotificationsFromListener(null, null);
1085 waitForIdle();
1086 StatusBarNotification[] notifs =
1087 mBinderService.getActiveNotifications(parent.sbn.getPackageName());
1088 assertEquals(1, notifs.length);
1089 }
1090
1091 @Test
1092 public void testCancelAllCancelNotificationsFromListener_OnGoingFlagWithParameter()
1093 throws Exception {
1094 final NotificationRecord parent = generateNotificationRecord(
1095 mTestNotificationChannel, 1, "group", true);
1096 final NotificationRecord child = generateNotificationRecord(
1097 mTestNotificationChannel, 2, "group", false);
1098 final NotificationRecord child2 = generateNotificationRecord(
1099 mTestNotificationChannel, 3, "group", false);
1100 child2.getNotification().flags |= Notification.FLAG_ONGOING_EVENT;
1101 final NotificationRecord newGroup = generateNotificationRecord(
1102 mTestNotificationChannel, 4, "group2", false);
1103 mService.addNotification(parent);
1104 mService.addNotification(child);
1105 mService.addNotification(child2);
1106 mService.addNotification(newGroup);
1107 String[] keys = {parent.sbn.getKey(), child.sbn.getKey(),
1108 child2.sbn.getKey(), newGroup.sbn.getKey()};
1109 mService.getBinderService().cancelNotificationsFromListener(null, keys);
1110 waitForIdle();
1111 StatusBarNotification[] notifs =
1112 mBinderService.getActiveNotifications(parent.sbn.getPackageName());
1113 assertEquals(0, notifs.length);
1114 }
1115
1116 @Test
1117 public void testUserInitiatedCancelAllWithGroup_OnGoingFlag() throws Exception {
1118 final NotificationRecord parent = generateNotificationRecord(
1119 mTestNotificationChannel, 1, "group", true);
1120 final NotificationRecord child = generateNotificationRecord(
1121 mTestNotificationChannel, 2, "group", false);
1122 final NotificationRecord child2 = generateNotificationRecord(
1123 mTestNotificationChannel, 3, "group", false);
1124 child2.getNotification().flags |= Notification.FLAG_ONGOING_EVENT;
1125 final NotificationRecord newGroup = generateNotificationRecord(
1126 mTestNotificationChannel, 4, "group2", false);
1127 mService.addNotification(parent);
1128 mService.addNotification(child);
1129 mService.addNotification(child2);
1130 mService.addNotification(newGroup);
1131 mService.mNotificationDelegate.onClearAll(mUid, Binder.getCallingPid(),
1132 parent.getUserId());
1133 waitForIdle();
1134 StatusBarNotification[] notifs =
1135 mBinderService.getActiveNotifications(parent.sbn.getPackageName());
1136 assertEquals(1, notifs.length);
1137 }
1138
1139 @Test
Julia Reynolds5f20e9f2017-01-30 08:54:53 -05001140 public void testTvExtenderChannelOverride_onTv() throws Exception {
Julia Reynolds503ed942017-10-04 16:04:56 -04001141 mService.setIsTelevision(true);
1142 mService.setRankingHelper(mRankingHelper);
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001143 when(mRankingHelper.getNotificationChannel(
Julia Reynolds5f20e9f2017-01-30 08:54:53 -05001144 anyString(), anyInt(), eq("foo"), anyBoolean())).thenReturn(
Julia Reynolds8617e4e2017-09-18 16:52:37 -04001145 new NotificationChannel("foo", "foo", IMPORTANCE_HIGH));
Julia Reynolds5f20e9f2017-01-30 08:54:53 -05001146
Julia Reynoldsbad42972017-04-25 13:52:49 -04001147 Notification.TvExtender tv = new Notification.TvExtender().setChannelId("foo");
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001148 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0,
Julia Reynoldsfea6f7b2017-04-19 13:50:12 -04001149 generateNotificationRecord(null, tv).getNotification(), 0);
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001150 verify(mRankingHelper, times(1)).getNotificationChannel(
Julia Reynolds5f20e9f2017-01-30 08:54:53 -05001151 anyString(), anyInt(), eq("foo"), anyBoolean());
1152 }
1153
1154 @Test
Julia Reynolds5f20e9f2017-01-30 08:54:53 -05001155 public void testTvExtenderChannelOverride_notOnTv() throws Exception {
Julia Reynolds503ed942017-10-04 16:04:56 -04001156 mService.setIsTelevision(false);
1157 mService.setRankingHelper(mRankingHelper);
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001158 when(mRankingHelper.getNotificationChannel(
Julia Reynolds5f20e9f2017-01-30 08:54:53 -05001159 anyString(), anyInt(), anyString(), anyBoolean())).thenReturn(
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001160 mTestNotificationChannel);
Julia Reynolds5f20e9f2017-01-30 08:54:53 -05001161
Julia Reynoldsbad42972017-04-25 13:52:49 -04001162 Notification.TvExtender tv = new Notification.TvExtender().setChannelId("foo");
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001163 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0,
Julia Reynoldsfea6f7b2017-04-19 13:50:12 -04001164 generateNotificationRecord(null, tv).getNotification(), 0);
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001165 verify(mRankingHelper, times(1)).getNotificationChannel(
1166 anyString(), anyInt(), eq(mTestNotificationChannel.getId()), anyBoolean());
Julia Reynolds5f20e9f2017-01-30 08:54:53 -05001167 }
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001168
1169 @Test
Julia Reynolds3eb3ffd2017-11-16 10:11:32 -05001170 public void testUpdateChannelNotifyCreatorBlock() throws Exception {
1171 mService.setRankingHelper(mRankingHelper);
1172 when(mRankingHelper.getNotificationChannel(eq(PKG), anyInt(),
1173 eq(mTestNotificationChannel.getId()), anyBoolean()))
1174 .thenReturn(mTestNotificationChannel);
1175
1176 NotificationChannel updatedChannel =
1177 new NotificationChannel(mTestNotificationChannel.getId(),
1178 mTestNotificationChannel.getName(), IMPORTANCE_NONE);
1179
1180 mBinderService.updateNotificationChannelForPackage(PKG, 0, updatedChannel);
1181 ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
1182 verify(mContext, times(1)).sendBroadcastAsUser(captor.capture(), any(), eq(null));
1183
1184 assertEquals(NotificationManager.ACTION_NOTIFICATION_CHANNEL_BLOCK_STATE_CHANGED,
1185 captor.getValue().getAction());
1186 assertEquals(PKG, captor.getValue().getPackage());
1187 assertEquals(mTestNotificationChannel.getId(), captor.getValue().getStringExtra(
1188 NotificationManager.EXTRA_BLOCK_STATE_CHANGED_ID));
1189 assertTrue(captor.getValue().getBooleanExtra(EXTRA_BLOCKED_STATE, false));
1190 }
1191
1192 @Test
1193 public void testUpdateChannelNotifyCreatorUnblock() throws Exception {
1194 NotificationChannel existingChannel =
1195 new NotificationChannel(mTestNotificationChannel.getId(),
1196 mTestNotificationChannel.getName(), IMPORTANCE_NONE);
1197 mService.setRankingHelper(mRankingHelper);
1198 when(mRankingHelper.getNotificationChannel(eq(PKG), anyInt(),
1199 eq(mTestNotificationChannel.getId()), anyBoolean()))
1200 .thenReturn(existingChannel);
1201
1202 mBinderService.updateNotificationChannelForPackage(PKG, 0, mTestNotificationChannel);
1203 ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
1204 verify(mContext, times(1)).sendBroadcastAsUser(captor.capture(), any(), eq(null));
1205
1206 assertEquals(NotificationManager.ACTION_NOTIFICATION_CHANNEL_BLOCK_STATE_CHANGED,
1207 captor.getValue().getAction());
1208 assertEquals(PKG, captor.getValue().getPackage());
1209 assertEquals(mTestNotificationChannel.getId(), captor.getValue().getStringExtra(
1210 NotificationManager.EXTRA_BLOCK_STATE_CHANGED_ID));
1211 assertFalse(captor.getValue().getBooleanExtra(EXTRA_BLOCKED_STATE, false));
1212 }
1213
1214 @Test
1215 public void testUpdateChannelNoNotifyCreatorOtherChanges() throws Exception {
1216 NotificationChannel existingChannel =
1217 new NotificationChannel(mTestNotificationChannel.getId(),
1218 mTestNotificationChannel.getName(), IMPORTANCE_MAX);
1219 mService.setRankingHelper(mRankingHelper);
1220 when(mRankingHelper.getNotificationChannel(eq(PKG), anyInt(),
1221 eq(mTestNotificationChannel.getId()), anyBoolean()))
1222 .thenReturn(existingChannel);
1223
1224 mBinderService.updateNotificationChannelForPackage(PKG, 0, mTestNotificationChannel);
1225 verify(mContext, never()).sendBroadcastAsUser(any(), any(), eq(null));
1226 }
1227
1228 @Test
1229 public void testUpdateGroupNotifyCreatorBlock() throws Exception {
1230 NotificationChannelGroup existing = new NotificationChannelGroup("id", "name");
1231 mService.setRankingHelper(mRankingHelper);
1232 when(mRankingHelper.getNotificationChannelGroup(eq(existing.getId()), eq(PKG), anyInt()))
1233 .thenReturn(existing);
1234
1235 NotificationChannelGroup updated = new NotificationChannelGroup("id", "name");
1236 updated.setBlocked(true);
1237
1238 mBinderService.updateNotificationChannelGroupForPackage(PKG, 0, updated);
1239 ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
1240 verify(mContext, times(1)).sendBroadcastAsUser(captor.capture(), any(), eq(null));
1241
1242 assertEquals(NotificationManager.ACTION_NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED,
1243 captor.getValue().getAction());
1244 assertEquals(PKG, captor.getValue().getPackage());
1245 assertEquals(existing.getId(), captor.getValue().getStringExtra(
1246 NotificationManager.EXTRA_BLOCK_STATE_CHANGED_ID));
1247 assertTrue(captor.getValue().getBooleanExtra(EXTRA_BLOCKED_STATE, false));
1248 }
1249
1250 @Test
1251 public void testUpdateGroupNotifyCreatorUnblock() throws Exception {
1252 NotificationChannelGroup existing = new NotificationChannelGroup("id", "name");
1253 existing.setBlocked(true);
1254 mService.setRankingHelper(mRankingHelper);
1255 when(mRankingHelper.getNotificationChannelGroup(eq(existing.getId()), eq(PKG), anyInt()))
1256 .thenReturn(existing);
1257
1258 mBinderService.updateNotificationChannelGroupForPackage(
1259 PKG, 0, new NotificationChannelGroup("id", "name"));
1260 ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
1261 verify(mContext, times(1)).sendBroadcastAsUser(captor.capture(), any(), eq(null));
1262
1263 assertEquals(NotificationManager.ACTION_NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED,
1264 captor.getValue().getAction());
1265 assertEquals(PKG, captor.getValue().getPackage());
1266 assertEquals(existing.getId(), captor.getValue().getStringExtra(
1267 NotificationManager.EXTRA_BLOCK_STATE_CHANGED_ID));
1268 assertFalse(captor.getValue().getBooleanExtra(EXTRA_BLOCKED_STATE, false));
1269 }
1270
1271 @Test
1272 public void testUpdateGroupNoNotifyCreatorOtherChanges() throws Exception {
1273 NotificationChannelGroup existing = new NotificationChannelGroup("id", "name");
1274 mService.setRankingHelper(mRankingHelper);
1275 when(mRankingHelper.getNotificationChannelGroup(eq(existing.getId()), eq(PKG), anyInt()))
1276 .thenReturn(existing);
1277
1278 mBinderService.updateNotificationChannelGroupForPackage(
1279 PKG, 0, new NotificationChannelGroup("id", "new name"));
1280 verify(mContext, never()).sendBroadcastAsUser(any(), any(), eq(null));
1281 }
1282
1283 @Test
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001284 public void testCreateChannelNotifyListener() throws Exception {
1285 List<String> associations = new ArrayList<>();
1286 associations.add("a");
Geoffrey Pitsch07532c32017-07-18 11:44:06 -04001287 when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations);
Julia Reynolds503ed942017-10-04 16:04:56 -04001288 mService.setRankingHelper(mRankingHelper);
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001289 when(mRankingHelper.getNotificationChannel(eq(PKG), anyInt(),
1290 eq(mTestNotificationChannel.getId()), anyBoolean()))
1291 .thenReturn(mTestNotificationChannel);
1292 NotificationChannel channel2 = new NotificationChannel("a", "b", IMPORTANCE_LOW);
1293 when(mRankingHelper.getNotificationChannel(eq(PKG), anyInt(),
1294 eq(channel2.getId()), anyBoolean()))
1295 .thenReturn(channel2);
1296
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -04001297 reset(mListeners);
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001298 mBinderService.createNotificationChannels(PKG,
1299 new ParceledListSlice(Arrays.asList(mTestNotificationChannel, channel2)));
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -04001300 verify(mListeners, times(1)).notifyNotificationChannelChanged(eq(PKG),
Julia Reynoldsf27d6b22017-04-13 15:48:16 -04001301 eq(Process.myUserHandle()), eq(mTestNotificationChannel),
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001302 eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_ADDED));
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -04001303 verify(mListeners, times(1)).notifyNotificationChannelChanged(eq(PKG),
Julia Reynoldsf27d6b22017-04-13 15:48:16 -04001304 eq(Process.myUserHandle()), eq(channel2),
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001305 eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_ADDED));
1306 }
1307
1308 @Test
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001309 public void testCreateChannelGroupNotifyListener() throws Exception {
1310 List<String> associations = new ArrayList<>();
1311 associations.add("a");
Geoffrey Pitsch07532c32017-07-18 11:44:06 -04001312 when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations);
Julia Reynolds503ed942017-10-04 16:04:56 -04001313 mService.setRankingHelper(mRankingHelper);
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001314 NotificationChannelGroup group1 = new NotificationChannelGroup("a", "b");
1315 NotificationChannelGroup group2 = new NotificationChannelGroup("n", "m");
1316
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -04001317 reset(mListeners);
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001318 mBinderService.createNotificationChannelGroups(PKG,
1319 new ParceledListSlice(Arrays.asList(group1, group2)));
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -04001320 verify(mListeners, times(1)).notifyNotificationChannelGroupChanged(eq(PKG),
Julia Reynoldsf27d6b22017-04-13 15:48:16 -04001321 eq(Process.myUserHandle()), eq(group1),
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001322 eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_ADDED));
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -04001323 verify(mListeners, times(1)).notifyNotificationChannelGroupChanged(eq(PKG),
Julia Reynoldsf27d6b22017-04-13 15:48:16 -04001324 eq(Process.myUserHandle()), eq(group2),
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001325 eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_ADDED));
1326 }
1327
1328 @Test
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001329 public void testUpdateChannelNotifyListener() throws Exception {
1330 List<String> associations = new ArrayList<>();
1331 associations.add("a");
Geoffrey Pitsch07532c32017-07-18 11:44:06 -04001332 when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations);
Julia Reynolds503ed942017-10-04 16:04:56 -04001333 mService.setRankingHelper(mRankingHelper);
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001334 mTestNotificationChannel.setLightColor(Color.CYAN);
1335 when(mRankingHelper.getNotificationChannel(eq(PKG), anyInt(),
1336 eq(mTestNotificationChannel.getId()), anyBoolean()))
1337 .thenReturn(mTestNotificationChannel);
1338
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -04001339 reset(mListeners);
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001340 mBinderService.updateNotificationChannelForPackage(PKG, 0, mTestNotificationChannel);
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -04001341 verify(mListeners, times(1)).notifyNotificationChannelChanged(eq(PKG),
Julia Reynoldsf27d6b22017-04-13 15:48:16 -04001342 eq(Process.myUserHandle()), eq(mTestNotificationChannel),
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001343 eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_UPDATED));
1344 }
1345
1346 @Test
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001347 public void testDeleteChannelNotifyListener() throws Exception {
1348 List<String> associations = new ArrayList<>();
1349 associations.add("a");
Geoffrey Pitsch07532c32017-07-18 11:44:06 -04001350 when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations);
Julia Reynolds503ed942017-10-04 16:04:56 -04001351 mService.setRankingHelper(mRankingHelper);
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001352 when(mRankingHelper.getNotificationChannel(eq(PKG), anyInt(),
1353 eq(mTestNotificationChannel.getId()), anyBoolean()))
1354 .thenReturn(mTestNotificationChannel);
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -04001355 reset(mListeners);
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001356 mBinderService.deleteNotificationChannel(PKG, mTestNotificationChannel.getId());
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -04001357 verify(mListeners, times(1)).notifyNotificationChannelChanged(eq(PKG),
Julia Reynoldsf27d6b22017-04-13 15:48:16 -04001358 eq(Process.myUserHandle()), eq(mTestNotificationChannel),
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001359 eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_DELETED));
1360 }
1361
1362 @Test
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001363 public void testDeleteChannelGroupNotifyListener() throws Exception {
1364 List<String> associations = new ArrayList<>();
1365 associations.add("a");
Geoffrey Pitsch07532c32017-07-18 11:44:06 -04001366 when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations);
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001367 NotificationChannelGroup ncg = new NotificationChannelGroup("a", "b/c");
Julia Reynolds503ed942017-10-04 16:04:56 -04001368 mService.setRankingHelper(mRankingHelper);
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001369 when(mRankingHelper.getNotificationChannelGroup(eq(ncg.getId()), eq(PKG), anyInt()))
1370 .thenReturn(ncg);
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -04001371 reset(mListeners);
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001372 mBinderService.deleteNotificationChannelGroup(PKG, ncg.getId());
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -04001373 verify(mListeners, times(1)).notifyNotificationChannelGroupChanged(eq(PKG),
Julia Reynoldsf27d6b22017-04-13 15:48:16 -04001374 eq(Process.myUserHandle()), eq(ncg),
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001375 eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_DELETED));
1376 }
1377
1378 @Test
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001379 public void testUpdateNotificationChannelFromPrivilegedListener_success() throws Exception {
Julia Reynolds503ed942017-10-04 16:04:56 -04001380 mService.setRankingHelper(mRankingHelper);
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001381 List<String> associations = new ArrayList<>();
1382 associations.add("a");
Geoffrey Pitsch07532c32017-07-18 11:44:06 -04001383 when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations);
Julia Reynolds3eb3ffd2017-11-16 10:11:32 -05001384 when(mRankingHelper.getNotificationChannel(eq(PKG), anyInt(),
1385 eq(mTestNotificationChannel.getId()), anyBoolean()))
1386 .thenReturn(mTestNotificationChannel);
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001387
1388 mBinderService.updateNotificationChannelFromPrivilegedListener(
Julia Reynoldsf27d6b22017-04-13 15:48:16 -04001389 null, PKG, Process.myUserHandle(), mTestNotificationChannel);
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001390
Julia Reynolds8617e4e2017-09-18 16:52:37 -04001391 verify(mRankingHelper, times(1)).updateNotificationChannel(
1392 anyString(), anyInt(), any(), anyBoolean());
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001393
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -04001394 verify(mListeners, never()).notifyNotificationChannelChanged(eq(PKG),
Julia Reynoldsf27d6b22017-04-13 15:48:16 -04001395 eq(Process.myUserHandle()), eq(mTestNotificationChannel),
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001396 eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_UPDATED));
1397 }
1398
1399 @Test
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001400 public void testUpdateNotificationChannelFromPrivilegedListener_noAccess() throws Exception {
Julia Reynolds503ed942017-10-04 16:04:56 -04001401 mService.setRankingHelper(mRankingHelper);
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001402 List<String> associations = new ArrayList<>();
Geoffrey Pitsch07532c32017-07-18 11:44:06 -04001403 when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations);
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001404
1405 try {
1406 mBinderService.updateNotificationChannelFromPrivilegedListener(
Julia Reynoldsf27d6b22017-04-13 15:48:16 -04001407 null, PKG, Process.myUserHandle(), mTestNotificationChannel);
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001408 fail("listeners that don't have a companion device shouldn't be able to call this");
1409 } catch (SecurityException e) {
1410 // pass
1411 }
1412
Julia Reynolds8617e4e2017-09-18 16:52:37 -04001413 verify(mRankingHelper, never()).updateNotificationChannel(
1414 anyString(), anyInt(), any(), anyBoolean());
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001415
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -04001416 verify(mListeners, never()).notifyNotificationChannelChanged(eq(PKG),
Julia Reynoldsf27d6b22017-04-13 15:48:16 -04001417 eq(Process.myUserHandle()), eq(mTestNotificationChannel),
1418 eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_UPDATED));
1419 }
1420
1421 @Test
Julia Reynoldsf27d6b22017-04-13 15:48:16 -04001422 public void testUpdateNotificationChannelFromPrivilegedListener_badUser() throws Exception {
Julia Reynolds503ed942017-10-04 16:04:56 -04001423 mService.setRankingHelper(mRankingHelper);
Julia Reynoldsf27d6b22017-04-13 15:48:16 -04001424 List<String> associations = new ArrayList<>();
1425 associations.add("a");
Geoffrey Pitsch07532c32017-07-18 11:44:06 -04001426 when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations);
Julia Reynoldsf27d6b22017-04-13 15:48:16 -04001427 mListener = mock(ManagedServices.ManagedServiceInfo.class);
Julia Reynolds4da79702017-06-01 11:06:10 -04001428 mListener.component = new ComponentName(PKG, PKG);
Julia Reynoldsf27d6b22017-04-13 15:48:16 -04001429 when(mListener.enabledAndUserMatches(anyInt())).thenReturn(false);
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -04001430 when(mListeners.checkServiceTokenLocked(any())).thenReturn(mListener);
Julia Reynoldsf27d6b22017-04-13 15:48:16 -04001431
1432 try {
1433 mBinderService.updateNotificationChannelFromPrivilegedListener(
1434 null, PKG, UserHandle.ALL, mTestNotificationChannel);
1435 fail("incorrectly allowed a change to a user listener cannot see");
1436 } catch (SecurityException e) {
1437 // pass
1438 }
1439
Julia Reynolds8617e4e2017-09-18 16:52:37 -04001440 verify(mRankingHelper, never()).updateNotificationChannel(
1441 anyString(), anyInt(), any(), anyBoolean());
Julia Reynoldsf27d6b22017-04-13 15:48:16 -04001442
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -04001443 verify(mListeners, never()).notifyNotificationChannelChanged(eq(PKG),
Julia Reynoldsf27d6b22017-04-13 15:48:16 -04001444 eq(Process.myUserHandle()), eq(mTestNotificationChannel),
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001445 eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_UPDATED));
1446 }
1447
1448 @Test
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001449 public void testGetNotificationChannelFromPrivilegedListener_success() throws Exception {
Julia Reynolds503ed942017-10-04 16:04:56 -04001450 mService.setRankingHelper(mRankingHelper);
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001451 List<String> associations = new ArrayList<>();
1452 associations.add("a");
Geoffrey Pitsch07532c32017-07-18 11:44:06 -04001453 when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations);
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001454
Julia Reynoldsf27d6b22017-04-13 15:48:16 -04001455 mBinderService.getNotificationChannelsFromPrivilegedListener(
1456 null, PKG, Process.myUserHandle());
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001457
1458 verify(mRankingHelper, times(1)).getNotificationChannels(
1459 anyString(), anyInt(), anyBoolean());
1460 }
1461
1462 @Test
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001463 public void testGetNotificationChannelFromPrivilegedListener_noAccess() throws Exception {
Julia Reynolds503ed942017-10-04 16:04:56 -04001464 mService.setRankingHelper(mRankingHelper);
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001465 List<String> associations = new ArrayList<>();
Geoffrey Pitsch07532c32017-07-18 11:44:06 -04001466 when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations);
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001467
1468 try {
Julia Reynoldsf27d6b22017-04-13 15:48:16 -04001469 mBinderService.getNotificationChannelsFromPrivilegedListener(
1470 null, PKG, Process.myUserHandle());
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001471 fail("listeners that don't have a companion device shouldn't be able to call this");
1472 } catch (SecurityException e) {
1473 // pass
1474 }
1475
1476 verify(mRankingHelper, never()).getNotificationChannels(
1477 anyString(), anyInt(), anyBoolean());
1478 }
1479
1480 @Test
Julia Reynoldsf27d6b22017-04-13 15:48:16 -04001481 public void testGetNotificationChannelFromPrivilegedListener_badUser() throws Exception {
Julia Reynolds503ed942017-10-04 16:04:56 -04001482 mService.setRankingHelper(mRankingHelper);
Julia Reynoldsf27d6b22017-04-13 15:48:16 -04001483 List<String> associations = new ArrayList<>();
1484 associations.add("a");
Geoffrey Pitsch07532c32017-07-18 11:44:06 -04001485 when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations);
Julia Reynoldsf27d6b22017-04-13 15:48:16 -04001486 mListener = mock(ManagedServices.ManagedServiceInfo.class);
1487 when(mListener.enabledAndUserMatches(anyInt())).thenReturn(false);
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -04001488 when(mListeners.checkServiceTokenLocked(any())).thenReturn(mListener);
Julia Reynoldsf27d6b22017-04-13 15:48:16 -04001489
1490 try {
1491 mBinderService.getNotificationChannelsFromPrivilegedListener(
1492 null, PKG, Process.myUserHandle());
1493 fail("listener getting channels from a user they cannot see");
1494 } catch (SecurityException e) {
1495 // pass
1496 }
1497
1498 verify(mRankingHelper, never()).getNotificationChannels(
1499 anyString(), anyInt(), anyBoolean());
1500 }
1501
1502 @Test
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001503 public void testGetNotificationChannelGroupsFromPrivilegedListener_success() throws Exception {
Julia Reynolds503ed942017-10-04 16:04:56 -04001504 mService.setRankingHelper(mRankingHelper);
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001505 List<String> associations = new ArrayList<>();
1506 associations.add("a");
Geoffrey Pitsch07532c32017-07-18 11:44:06 -04001507 when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations);
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001508
Julia Reynoldsf27d6b22017-04-13 15:48:16 -04001509 mBinderService.getNotificationChannelGroupsFromPrivilegedListener(
1510 null, PKG, Process.myUserHandle());
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001511
1512 verify(mRankingHelper, times(1)).getNotificationChannelGroups(anyString(), anyInt());
1513 }
1514
1515 @Test
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001516 public void testGetNotificationChannelGroupsFromPrivilegedListener_noAccess() throws Exception {
Julia Reynolds503ed942017-10-04 16:04:56 -04001517 mService.setRankingHelper(mRankingHelper);
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001518 List<String> associations = new ArrayList<>();
Geoffrey Pitsch07532c32017-07-18 11:44:06 -04001519 when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations);
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001520
1521 try {
Julia Reynoldsf27d6b22017-04-13 15:48:16 -04001522 mBinderService.getNotificationChannelGroupsFromPrivilegedListener(
1523 null, PKG, Process.myUserHandle());
1524 fail("listeners that don't have a companion device shouldn't be able to call this");
1525 } catch (SecurityException e) {
1526 // pass
1527 }
1528
1529 verify(mRankingHelper, never()).getNotificationChannelGroups(anyString(), anyInt());
1530 }
1531
1532 @Test
Julia Reynoldsf27d6b22017-04-13 15:48:16 -04001533 public void testGetNotificationChannelGroupsFromPrivilegedListener_badUser() throws Exception {
Julia Reynolds503ed942017-10-04 16:04:56 -04001534 mService.setRankingHelper(mRankingHelper);
Julia Reynoldsf27d6b22017-04-13 15:48:16 -04001535 List<String> associations = new ArrayList<>();
Geoffrey Pitsch07532c32017-07-18 11:44:06 -04001536 when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations);
Julia Reynoldsf27d6b22017-04-13 15:48:16 -04001537 mListener = mock(ManagedServices.ManagedServiceInfo.class);
1538 when(mListener.enabledAndUserMatches(anyInt())).thenReturn(false);
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -04001539 when(mListeners.checkServiceTokenLocked(any())).thenReturn(mListener);
Julia Reynoldsf27d6b22017-04-13 15:48:16 -04001540
1541 try {
1542 mBinderService.getNotificationChannelGroupsFromPrivilegedListener(
1543 null, PKG, Process.myUserHandle());
Julia Reynolds73ed76b2017-04-04 17:04:38 -04001544 fail("listeners that don't have a companion device shouldn't be able to call this");
1545 } catch (SecurityException e) {
1546 // pass
1547 }
1548
1549 verify(mRankingHelper, never()).getNotificationChannelGroups(anyString(), anyInt());
1550 }
Julia Reynoldsda781472017-04-12 09:41:16 -04001551
1552 @Test
Julia Reynoldsda781472017-04-12 09:41:16 -04001553 public void testHasCompanionDevice_failure() throws Exception {
1554 when(mCompanionMgr.getAssociations(anyString(), anyInt())).thenThrow(
1555 new IllegalArgumentException());
Julia Reynolds503ed942017-10-04 16:04:56 -04001556 mService.hasCompanionDevice(mListener);
Julia Reynoldsda781472017-04-12 09:41:16 -04001557 }
Julia Reynolds727a7282017-04-13 10:54:01 -04001558
1559 @Test
Julia Reynolds727a7282017-04-13 10:54:01 -04001560 public void testHasCompanionDevice_noService() throws Exception {
Julia Reynolds503ed942017-10-04 16:04:56 -04001561 mService = new TestableNotificationManagerService(mContext);
Julia Reynolds727a7282017-04-13 10:54:01 -04001562
Julia Reynolds503ed942017-10-04 16:04:56 -04001563 assertFalse(mService.hasCompanionDevice(mListener));
Julia Reynolds727a7282017-04-13 10:54:01 -04001564 }
1565
Julia Reynoldsa78cdff2017-04-26 10:19:25 -04001566 @Test
1567 public void testSnoozeRunnable_snoozeNonGrouped() throws Exception {
1568 final NotificationRecord nonGrouped = generateNotificationRecord(
1569 mTestNotificationChannel, 1, null, false);
1570 final NotificationRecord grouped = generateNotificationRecord(
1571 mTestNotificationChannel, 2, "group", false);
Julia Reynolds503ed942017-10-04 16:04:56 -04001572 mService.addNotification(grouped);
1573 mService.addNotification(nonGrouped);
Julia Reynoldsa78cdff2017-04-26 10:19:25 -04001574
1575 NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
Julia Reynolds503ed942017-10-04 16:04:56 -04001576 mService.new SnoozeNotificationRunnable(
Julia Reynoldsa78cdff2017-04-26 10:19:25 -04001577 nonGrouped.getKey(), 100, null);
1578 snoozeNotificationRunnable.run();
1579
1580 // only snooze the one notification
1581 verify(mSnoozeHelper, times(1)).snooze(any(NotificationRecord.class), anyLong());
Julia Reynolds503ed942017-10-04 16:04:56 -04001582 assertTrue(nonGrouped.getStats().hasSnoozed());
Julia Reynoldsa78cdff2017-04-26 10:19:25 -04001583 }
1584
1585 @Test
1586 public void testSnoozeRunnable_snoozeSummary_withChildren() throws Exception {
1587 final NotificationRecord parent = generateNotificationRecord(
1588 mTestNotificationChannel, 1, "group", true);
1589 final NotificationRecord child = generateNotificationRecord(
1590 mTestNotificationChannel, 2, "group", false);
1591 final NotificationRecord child2 = generateNotificationRecord(
1592 mTestNotificationChannel, 3, "group", false);
Julia Reynolds503ed942017-10-04 16:04:56 -04001593 mService.addNotification(parent);
1594 mService.addNotification(child);
1595 mService.addNotification(child2);
Julia Reynoldsa78cdff2017-04-26 10:19:25 -04001596
1597 NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
Julia Reynolds503ed942017-10-04 16:04:56 -04001598 mService.new SnoozeNotificationRunnable(
Julia Reynoldsa78cdff2017-04-26 10:19:25 -04001599 parent.getKey(), 100, null);
1600 snoozeNotificationRunnable.run();
1601
1602 // snooze parent and children
1603 verify(mSnoozeHelper, times(3)).snooze(any(NotificationRecord.class), anyLong());
1604 }
1605
1606 @Test
1607 public void testSnoozeRunnable_snoozeGroupChild_fellowChildren() throws Exception {
1608 final NotificationRecord parent = generateNotificationRecord(
1609 mTestNotificationChannel, 1, "group", true);
1610 final NotificationRecord child = generateNotificationRecord(
1611 mTestNotificationChannel, 2, "group", false);
1612 final NotificationRecord child2 = generateNotificationRecord(
1613 mTestNotificationChannel, 3, "group", false);
Julia Reynolds503ed942017-10-04 16:04:56 -04001614 mService.addNotification(parent);
1615 mService.addNotification(child);
1616 mService.addNotification(child2);
Julia Reynoldsa78cdff2017-04-26 10:19:25 -04001617
1618 NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
Julia Reynolds503ed942017-10-04 16:04:56 -04001619 mService.new SnoozeNotificationRunnable(
Julia Reynoldsa78cdff2017-04-26 10:19:25 -04001620 child2.getKey(), 100, null);
1621 snoozeNotificationRunnable.run();
1622
1623 // only snooze the one child
1624 verify(mSnoozeHelper, times(1)).snooze(any(NotificationRecord.class), anyLong());
1625 }
1626
1627 @Test
1628 public void testSnoozeRunnable_snoozeGroupChild_onlyChildOfSummary() throws Exception {
1629 final NotificationRecord parent = generateNotificationRecord(
1630 mTestNotificationChannel, 1, "group", true);
1631 assertTrue(parent.sbn.getNotification().isGroupSummary());
1632 final NotificationRecord child = generateNotificationRecord(
1633 mTestNotificationChannel, 2, "group", false);
Julia Reynolds503ed942017-10-04 16:04:56 -04001634 mService.addNotification(parent);
1635 mService.addNotification(child);
Julia Reynoldsa78cdff2017-04-26 10:19:25 -04001636
1637 NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
Julia Reynolds503ed942017-10-04 16:04:56 -04001638 mService.new SnoozeNotificationRunnable(
Julia Reynoldsa78cdff2017-04-26 10:19:25 -04001639 child.getKey(), 100, null);
1640 snoozeNotificationRunnable.run();
1641
1642 // snooze child and summary
1643 verify(mSnoozeHelper, times(2)).snooze(any(NotificationRecord.class), anyLong());
1644 }
1645
1646 @Test
1647 public void testSnoozeRunnable_snoozeGroupChild_noOthersInGroup() throws Exception {
1648 final NotificationRecord child = generateNotificationRecord(
1649 mTestNotificationChannel, 2, "group", false);
Julia Reynolds503ed942017-10-04 16:04:56 -04001650 mService.addNotification(child);
Julia Reynoldsa78cdff2017-04-26 10:19:25 -04001651
1652 NotificationManagerService.SnoozeNotificationRunnable snoozeNotificationRunnable =
Julia Reynolds503ed942017-10-04 16:04:56 -04001653 mService.new SnoozeNotificationRunnable(
Julia Reynoldsa78cdff2017-04-26 10:19:25 -04001654 child.getKey(), 100, null);
1655 snoozeNotificationRunnable.run();
1656
1657 // snooze child only
1658 verify(mSnoozeHelper, times(1)).snooze(any(NotificationRecord.class), anyLong());
1659 }
1660
1661 @Test
1662 public void testPostGroupChild_unsnoozeParent() throws Exception {
1663 final NotificationRecord child = generateNotificationRecord(
1664 mTestNotificationChannel, 2, "group", false);
1665
1666 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", null,
1667 child.sbn.getId(), child.sbn.getNotification(), child.sbn.getUserId());
1668 waitForIdle();
1669
1670 verify(mSnoozeHelper, times(1)).repostGroupSummary(
1671 anyString(), anyInt(), eq(child.getGroupKey()));
1672 }
1673
1674 @Test
1675 public void testPostNonGroup_noUnsnoozing() throws Exception {
1676 final NotificationRecord record = generateNotificationRecord(
1677 mTestNotificationChannel, 2, null, false);
1678
1679 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", null,
1680 record.sbn.getId(), record.sbn.getNotification(), record.sbn.getUserId());
1681 waitForIdle();
1682
1683 verify(mSnoozeHelper, never()).repostGroupSummary(anyString(), anyInt(), anyString());
1684 }
1685
1686 @Test
1687 public void testPostGroupSummary_noUnsnoozing() throws Exception {
1688 final NotificationRecord parent = generateNotificationRecord(
1689 mTestNotificationChannel, 2, "group", true);
1690
1691 mBinderService.enqueueNotificationWithTag(PKG, "opPkg", null,
1692 parent.sbn.getId(), parent.sbn.getNotification(), parent.sbn.getUserId());
1693 waitForIdle();
1694
1695 verify(mSnoozeHelper, never()).repostGroupSummary(anyString(), anyInt(), anyString());
1696 }
Julia Reynoldsb852e562017-06-06 16:14:18 -04001697
1698 @Test
Julia Reynolds92febc32017-10-26 11:30:31 -04001699 public void testSetListenerAccessForUser() throws Exception {
1700 UserHandle user = UserHandle.of(10);
1701 ComponentName c = ComponentName.unflattenFromString("package/Component");
1702 try {
1703 mBinderService.setNotificationListenerAccessGrantedForUser(
1704 c, user.getIdentifier(), true);
1705 } catch (SecurityException e) {
1706 if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) {
1707 throw e;
1708 }
1709 }
1710
1711 verify(mContext, times(1)).sendBroadcastAsUser(any(), eq(user), any());
1712 verify(mListeners, times(1)).setPackageOrComponentEnabled(
1713 c.flattenToString(), user.getIdentifier(), true, true);
1714 verify(mConditionProviders, times(1)).setPackageOrComponentEnabled(
1715 c.flattenToString(), user.getIdentifier(), false, true);
1716 verify(mAssistants, never()).setPackageOrComponentEnabled(
1717 any(), anyInt(), anyBoolean(), anyBoolean());
1718 }
1719
1720 @Test
1721 public void testSetAssistantAccessForUser() throws Exception {
1722 UserHandle user = UserHandle.of(10);
1723 ComponentName c = ComponentName.unflattenFromString("package/Component");
1724 try {
1725 mBinderService.setNotificationAssistantAccessGrantedForUser(
1726 c, user.getIdentifier(), true);
1727 } catch (SecurityException e) {
1728 if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) {
1729 throw e;
1730 }
1731 }
1732
1733 verify(mContext, times(1)).sendBroadcastAsUser(any(), eq(user), any());
1734 verify(mAssistants, times(1)).setPackageOrComponentEnabled(
1735 c.flattenToString(), user.getIdentifier(), true, true);
1736 verify(mConditionProviders, times(1)).setPackageOrComponentEnabled(
1737 c.flattenToString(), user.getIdentifier(), false, true);
1738 verify(mListeners, never()).setPackageOrComponentEnabled(
1739 any(), anyInt(), anyBoolean(), anyBoolean());
1740 }
1741
1742 @Test
1743 public void testSetDndAccessForUser() throws Exception {
1744 UserHandle user = UserHandle.of(10);
1745 ComponentName c = ComponentName.unflattenFromString("package/Component");
1746 try {
1747 mBinderService.setNotificationPolicyAccessGrantedForUser(
1748 c.getPackageName(), user.getIdentifier(), true);
1749 } catch (SecurityException e) {
1750 if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) {
1751 throw e;
1752 }
1753 }
1754
1755 verify(mContext, times(1)).sendBroadcastAsUser(any(), eq(user), any());
1756 verify(mConditionProviders, times(1)).setPackageOrComponentEnabled(
1757 c.getPackageName(), user.getIdentifier(), true, true);
1758 verify(mAssistants, never()).setPackageOrComponentEnabled(
1759 any(), anyInt(), anyBoolean(), anyBoolean());
1760 verify(mListeners, never()).setPackageOrComponentEnabled(
1761 any(), anyInt(), anyBoolean(), anyBoolean());
1762 }
1763
1764 @Test
Julia Reynoldsb852e562017-06-06 16:14:18 -04001765 public void testSetListenerAccess() throws Exception {
1766 ComponentName c = ComponentName.unflattenFromString("package/Component");
1767 try {
1768 mBinderService.setNotificationListenerAccessGranted(c, true);
1769 } catch (SecurityException e) {
1770 if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) {
1771 throw e;
1772 }
1773 }
1774
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -04001775 verify(mListeners, times(1)).setPackageOrComponentEnabled(
Julia Reynoldsb852e562017-06-06 16:14:18 -04001776 c.flattenToString(), 0, true, true);
1777 verify(mConditionProviders, times(1)).setPackageOrComponentEnabled(
1778 c.flattenToString(), 0, false, true);
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -04001779 verify(mAssistants, never()).setPackageOrComponentEnabled(
Julia Reynoldsb852e562017-06-06 16:14:18 -04001780 any(), anyInt(), anyBoolean(), anyBoolean());
1781 }
1782
1783 @Test
1784 public void testSetAssistantAccess() throws Exception {
1785 ComponentName c = ComponentName.unflattenFromString("package/Component");
1786 try {
1787 mBinderService.setNotificationAssistantAccessGranted(c, true);
1788 } catch (SecurityException e) {
1789 if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) {
1790 throw e;
1791 }
1792 }
1793
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -04001794 verify(mAssistants, times(1)).setPackageOrComponentEnabled(
Julia Reynoldsb852e562017-06-06 16:14:18 -04001795 c.flattenToString(), 0, true, true);
1796 verify(mConditionProviders, times(1)).setPackageOrComponentEnabled(
1797 c.flattenToString(), 0, false, true);
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -04001798 verify(mListeners, never()).setPackageOrComponentEnabled(
Julia Reynoldsb852e562017-06-06 16:14:18 -04001799 any(), anyInt(), anyBoolean(), anyBoolean());
1800 }
1801
1802 @Test
1803 public void testSetDndAccess() throws Exception {
1804 ComponentName c = ComponentName.unflattenFromString("package/Component");
1805 try {
1806 mBinderService.setNotificationPolicyAccessGranted(c.getPackageName(), true);
1807 } catch (SecurityException e) {
1808 if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) {
1809 throw e;
1810 }
1811 }
1812
1813 verify(mConditionProviders, times(1)).setPackageOrComponentEnabled(
1814 c.getPackageName(), 0, true, true);
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -04001815 verify(mAssistants, never()).setPackageOrComponentEnabled(
Julia Reynoldsb852e562017-06-06 16:14:18 -04001816 any(), anyInt(), anyBoolean(), anyBoolean());
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -04001817 verify(mListeners, never()).setPackageOrComponentEnabled(
Julia Reynoldsb852e562017-06-06 16:14:18 -04001818 any(), anyInt(), anyBoolean(), anyBoolean());
1819 }
Julia Reynolds68263d12017-06-21 14:21:19 -04001820
1821 @Test
1822 public void testSetListenerAccess_doesNothingOnLowRam() throws Exception {
1823 when(mActivityManager.isLowRamDevice()).thenReturn(true);
1824 ComponentName c = ComponentName.unflattenFromString("package/Component");
1825 mBinderService.setNotificationListenerAccessGranted(c, true);
1826
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -04001827 verify(mListeners, never()).setPackageOrComponentEnabled(
Julia Reynoldse1816412017-10-24 10:39:11 -04001828 anyString(), anyInt(), anyBoolean(), anyBoolean());
Julia Reynolds68263d12017-06-21 14:21:19 -04001829 verify(mConditionProviders, never()).setPackageOrComponentEnabled(
Julia Reynoldse1816412017-10-24 10:39:11 -04001830 anyString(), anyInt(), anyBoolean(), anyBoolean());
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -04001831 verify(mAssistants, never()).setPackageOrComponentEnabled(
Julia Reynolds68263d12017-06-21 14:21:19 -04001832 any(), anyInt(), anyBoolean(), anyBoolean());
1833 }
1834
1835 @Test
1836 public void testSetAssistantAccess_doesNothingOnLowRam() throws Exception {
1837 when(mActivityManager.isLowRamDevice()).thenReturn(true);
1838 ComponentName c = ComponentName.unflattenFromString("package/Component");
1839 mBinderService.setNotificationAssistantAccessGranted(c, true);
1840
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -04001841 verify(mListeners, never()).setPackageOrComponentEnabled(
Julia Reynoldse1816412017-10-24 10:39:11 -04001842 anyString(), anyInt(), anyBoolean(), anyBoolean());
Julia Reynolds68263d12017-06-21 14:21:19 -04001843 verify(mConditionProviders, never()).setPackageOrComponentEnabled(
Julia Reynoldse1816412017-10-24 10:39:11 -04001844 anyString(), anyInt(), anyBoolean(), anyBoolean());
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -04001845 verify(mAssistants, never()).setPackageOrComponentEnabled(
Julia Reynolds68263d12017-06-21 14:21:19 -04001846 any(), anyInt(), anyBoolean(), anyBoolean());
1847 }
1848
1849 @Test
1850 public void testSetDndAccess_doesNothingOnLowRam() throws Exception {
1851 when(mActivityManager.isLowRamDevice()).thenReturn(true);
1852 ComponentName c = ComponentName.unflattenFromString("package/Component");
1853 mBinderService.setNotificationPolicyAccessGranted(c.getPackageName(), true);
1854
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -04001855 verify(mListeners, never()).setPackageOrComponentEnabled(
Julia Reynoldse1816412017-10-24 10:39:11 -04001856 anyString(), anyInt(), anyBoolean(), anyBoolean());
Julia Reynolds68263d12017-06-21 14:21:19 -04001857 verify(mConditionProviders, never()).setPackageOrComponentEnabled(
Julia Reynoldse1816412017-10-24 10:39:11 -04001858 anyString(), anyInt(), anyBoolean(), anyBoolean());
1859 verify(mAssistants, never()).setPackageOrComponentEnabled(
1860 any(), anyInt(), anyBoolean(), anyBoolean());
1861 }
1862
1863 @Test
1864 public void testSetListenerAccess_doesNothingOnLowRam_exceptWatch() throws Exception {
1865 when(mPackageManagerClient.hasSystemFeature(FEATURE_WATCH)).thenReturn(true);
1866 when(mActivityManager.isLowRamDevice()).thenReturn(true);
1867 ComponentName c = ComponentName.unflattenFromString("package/Component");
1868 try {
1869 mBinderService.setNotificationListenerAccessGranted(c, true);
1870 } catch (SecurityException e) {
1871 if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) {
1872 throw e;
1873 }
1874 }
1875
1876 verify(mListeners, times(1)).setPackageOrComponentEnabled(
1877 c.flattenToString(), 0, true, true);
1878 verify(mConditionProviders, times(1)).setPackageOrComponentEnabled(
Julia Reynolds68263d12017-06-21 14:21:19 -04001879 c.flattenToString(), 0, false, true);
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -04001880 verify(mAssistants, never()).setPackageOrComponentEnabled(
Julia Reynolds68263d12017-06-21 14:21:19 -04001881 any(), anyInt(), anyBoolean(), anyBoolean());
1882 }
Julia Reynolds8aebf352017-06-26 11:35:33 -04001883
1884 @Test
Julia Reynoldse1816412017-10-24 10:39:11 -04001885 public void testSetAssistantAccess_doesNothingOnLowRam_exceptWatch() throws Exception {
1886 when(mPackageManagerClient.hasSystemFeature(FEATURE_WATCH)).thenReturn(true);
1887 when(mActivityManager.isLowRamDevice()).thenReturn(true);
1888 ComponentName c = ComponentName.unflattenFromString("package/Component");
1889 try {
1890 mBinderService.setNotificationAssistantAccessGranted(c, true);
1891 } catch (SecurityException e) {
1892 if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) {
1893 throw e;
1894 }
1895 }
1896
1897 verify(mListeners, never()).setPackageOrComponentEnabled(
1898 anyString(), anyInt(), anyBoolean(), anyBoolean());
1899 verify(mConditionProviders, times(1)).setPackageOrComponentEnabled(
1900 c.flattenToString(), 0, false, true);
1901 verify(mAssistants, times(1)).setPackageOrComponentEnabled(
1902 c.flattenToString(), 0, true, true);
1903 }
1904
1905 @Test
1906 public void testSetDndAccess_doesNothingOnLowRam_exceptWatch() throws Exception {
1907 when(mPackageManagerClient.hasSystemFeature(FEATURE_WATCH)).thenReturn(true);
1908 when(mActivityManager.isLowRamDevice()).thenReturn(true);
1909 ComponentName c = ComponentName.unflattenFromString("package/Component");
1910 try {
1911 mBinderService.setNotificationPolicyAccessGranted(c.getPackageName(), true);
1912 } catch (SecurityException e) {
1913 if (!e.getMessage().contains("Permission Denial: not allowed to send broadcast")) {
1914 throw e;
1915 }
1916 }
1917
1918 verify(mListeners, never()).setPackageOrComponentEnabled(
1919 anyString(), anyInt(), anyBoolean(), anyBoolean());
1920 verify(mConditionProviders, times(1)).setPackageOrComponentEnabled(
1921 c.getPackageName(), 0, true, true);
1922 verify(mAssistants, never()).setPackageOrComponentEnabled(
1923 any(), anyInt(), anyBoolean(), anyBoolean());
1924 }
1925
1926 @Test
Julia Reynolds8aebf352017-06-26 11:35:33 -04001927 public void testOnlyAutogroupIfGroupChanged_noPriorNoti_autogroups() throws Exception {
1928 NotificationRecord r = generateNotificationRecord(mTestNotificationChannel, 0, null, false);
Julia Reynolds503ed942017-10-04 16:04:56 -04001929 mService.addEnqueuedNotification(r);
Julia Reynolds8aebf352017-06-26 11:35:33 -04001930 NotificationManagerService.PostNotificationRunnable runnable =
Julia Reynolds503ed942017-10-04 16:04:56 -04001931 mService.new PostNotificationRunnable(r.getKey());
Julia Reynolds8aebf352017-06-26 11:35:33 -04001932 runnable.run();
1933 waitForIdle();
1934
Julia Reynoldsa13b3e22017-08-10 16:58:54 -04001935 verify(mGroupHelper, times(1)).onNotificationPosted(any(), anyBoolean());
Julia Reynolds8aebf352017-06-26 11:35:33 -04001936 }
1937
1938 @Test
1939 public void testOnlyAutogroupIfGroupChanged_groupChanged_autogroups()
1940 throws Exception {
Julia Reynolds8617e4e2017-09-18 16:52:37 -04001941 NotificationRecord r =
1942 generateNotificationRecord(mTestNotificationChannel, 0, "group", false);
Julia Reynolds503ed942017-10-04 16:04:56 -04001943 mService.addNotification(r);
Julia Reynolds8aebf352017-06-26 11:35:33 -04001944
1945 r = generateNotificationRecord(mTestNotificationChannel, 0, null, false);
Julia Reynolds503ed942017-10-04 16:04:56 -04001946 mService.addEnqueuedNotification(r);
Julia Reynolds8aebf352017-06-26 11:35:33 -04001947 NotificationManagerService.PostNotificationRunnable runnable =
Julia Reynolds503ed942017-10-04 16:04:56 -04001948 mService.new PostNotificationRunnable(r.getKey());
Julia Reynolds8aebf352017-06-26 11:35:33 -04001949 runnable.run();
1950 waitForIdle();
1951
Julia Reynoldsa13b3e22017-08-10 16:58:54 -04001952 verify(mGroupHelper, times(1)).onNotificationPosted(any(), anyBoolean());
Julia Reynolds8aebf352017-06-26 11:35:33 -04001953 }
1954
1955 @Test
1956 public void testOnlyAutogroupIfGroupChanged_noGroupChanged_autogroups()
1957 throws Exception {
Julia Reynolds4db59552017-06-30 13:34:01 -04001958 NotificationRecord r = generateNotificationRecord(mTestNotificationChannel, 0, "group",
1959 false);
Julia Reynolds503ed942017-10-04 16:04:56 -04001960 mService.addNotification(r);
1961 mService.addEnqueuedNotification(r);
Julia Reynolds8aebf352017-06-26 11:35:33 -04001962
1963 NotificationManagerService.PostNotificationRunnable runnable =
Julia Reynolds503ed942017-10-04 16:04:56 -04001964 mService.new PostNotificationRunnable(r.getKey());
Julia Reynolds8aebf352017-06-26 11:35:33 -04001965 runnable.run();
1966 waitForIdle();
1967
Julia Reynoldsa13b3e22017-08-10 16:58:54 -04001968 verify(mGroupHelper, never()).onNotificationPosted(any(), anyBoolean());
Julia Reynolds8aebf352017-06-26 11:35:33 -04001969 }
Beverly40239d92017-07-07 10:20:41 -04001970
Julia Reynolds4db59552017-06-30 13:34:01 -04001971 @Test
1972 public void testNoFakeColorizedPermission() throws Exception {
1973 when(mPackageManagerClient.checkPermission(any(), any())).thenReturn(PERMISSION_DENIED);
1974 Notification.Builder nb = new Notification.Builder(mContext,
1975 mTestNotificationChannel.getId())
1976 .setContentTitle("foo")
1977 .setColorized(true)
1978 .setFlag(Notification.FLAG_CAN_COLORIZE, true)
1979 .setSmallIcon(android.R.drawable.sym_def_app_icon);
Geoffrey Pitsch07532c32017-07-18 11:44:06 -04001980 StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, "tag", mUid, 0,
1981 nb.build(), new UserHandle(mUid), null, 0);
Julia Reynolds4db59552017-06-30 13:34:01 -04001982 NotificationRecord nr = new NotificationRecord(mContext, sbn, mTestNotificationChannel);
1983
1984 mBinderService.enqueueNotificationWithTag(PKG, PKG, null,
1985 nr.sbn.getId(), nr.sbn.getNotification(), nr.sbn.getUserId());
1986 waitForIdle();
1987
Julia Reynolds503ed942017-10-04 16:04:56 -04001988 NotificationRecord posted = mService.findNotificationLocked(
Julia Reynolds4db59552017-06-30 13:34:01 -04001989 PKG, null, nr.sbn.getId(), nr.sbn.getUserId());
1990
1991 assertFalse(posted.getNotification().isColorized());
1992 }
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04001993
1994 @Test
1995 public void testGetNotificationCountLocked() throws Exception {
1996 for (int i = 0; i < 20; i++) {
Geoffrey Pitsch07532c32017-07-18 11:44:06 -04001997 NotificationRecord r =
1998 generateNotificationRecord(mTestNotificationChannel, i, null, false);
Julia Reynolds503ed942017-10-04 16:04:56 -04001999 mService.addEnqueuedNotification(r);
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04002000 }
2001 for (int i = 0; i < 20; i++) {
Geoffrey Pitsch07532c32017-07-18 11:44:06 -04002002 NotificationRecord r =
2003 generateNotificationRecord(mTestNotificationChannel, i, null, false);
Julia Reynolds503ed942017-10-04 16:04:56 -04002004 mService.addNotification(r);
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04002005 }
2006
2007 // another package
2008 Notification n =
2009 new Notification.Builder(mContext, mTestNotificationChannel.getId())
2010 .setSmallIcon(android.R.drawable.sym_def_app_icon)
2011 .build();
2012
Geoffrey Pitsch07532c32017-07-18 11:44:06 -04002013 StatusBarNotification sbn = new StatusBarNotification("a", "a", 0, "tag", mUid, 0,
2014 n, new UserHandle(mUid), null, 0);
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04002015 NotificationRecord otherPackage =
2016 new NotificationRecord(mContext, sbn, mTestNotificationChannel);
Julia Reynolds503ed942017-10-04 16:04:56 -04002017 mService.addEnqueuedNotification(otherPackage);
2018 mService.addNotification(otherPackage);
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04002019
2020 // Same notifications are enqueued as posted, everything counts b/c id and tag don't match
Geoffrey Pitsch07532c32017-07-18 11:44:06 -04002021 int userId = new UserHandle(mUid).getIdentifier();
Julia Reynolds8617e4e2017-09-18 16:52:37 -04002022 assertEquals(40,
Julia Reynolds503ed942017-10-04 16:04:56 -04002023 mService.getNotificationCountLocked(PKG, userId, 0, null));
Julia Reynolds8617e4e2017-09-18 16:52:37 -04002024 assertEquals(40,
Julia Reynolds503ed942017-10-04 16:04:56 -04002025 mService.getNotificationCountLocked(PKG, userId, 0, "tag2"));
Julia Reynolds8617e4e2017-09-18 16:52:37 -04002026 assertEquals(2,
Julia Reynolds503ed942017-10-04 16:04:56 -04002027 mService.getNotificationCountLocked("a", userId, 0, "banana"));
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04002028
2029 // exclude a known notification - it's excluded from only the posted list, not enqueued
Julia Reynolds8617e4e2017-09-18 16:52:37 -04002030 assertEquals(39,
Julia Reynolds503ed942017-10-04 16:04:56 -04002031 mService.getNotificationCountLocked(PKG, userId, 0, "tag"));
Julia Reynoldseb3dca72017-07-11 10:39:58 -04002032 }
2033
2034 @Test
Julia Reynolds51710712017-07-19 13:48:07 -04002035 public void testAddAutogroup_requestsSort() throws Exception {
Julia Reynoldseb3dca72017-07-11 10:39:58 -04002036 RankingHandler rh = mock(RankingHandler.class);
Julia Reynolds503ed942017-10-04 16:04:56 -04002037 mService.setRankingHandler(rh);
Julia Reynoldseb3dca72017-07-11 10:39:58 -04002038
2039 final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel);
Julia Reynolds503ed942017-10-04 16:04:56 -04002040 mService.addNotification(r);
2041 mService.addAutogroupKeyLocked(r.getKey());
Julia Reynolds51710712017-07-19 13:48:07 -04002042
2043 verify(rh, times(1)).requestSort();
2044 }
2045
2046 @Test
2047 public void testRemoveAutogroup_requestsSort() throws Exception {
2048 RankingHandler rh = mock(RankingHandler.class);
Julia Reynolds503ed942017-10-04 16:04:56 -04002049 mService.setRankingHandler(rh);
Julia Reynolds51710712017-07-19 13:48:07 -04002050
2051 final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel);
2052 r.setOverrideGroupKey("TEST");
Julia Reynolds503ed942017-10-04 16:04:56 -04002053 mService.addNotification(r);
2054 mService.removeAutogroupKeyLocked(r.getKey());
Julia Reynoldseb3dca72017-07-11 10:39:58 -04002055
Julia Reynolds51710712017-07-19 13:48:07 -04002056 verify(rh, times(1)).requestSort();
2057 }
2058
2059 @Test
2060 public void testReaddAutogroup_noSort() throws Exception {
2061 RankingHandler rh = mock(RankingHandler.class);
Julia Reynolds503ed942017-10-04 16:04:56 -04002062 mService.setRankingHandler(rh);
Julia Reynolds51710712017-07-19 13:48:07 -04002063
2064 final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel);
2065 r.setOverrideGroupKey("TEST");
Julia Reynolds503ed942017-10-04 16:04:56 -04002066 mService.addNotification(r);
2067 mService.addAutogroupKeyLocked(r.getKey());
Julia Reynolds51710712017-07-19 13:48:07 -04002068
2069 verify(rh, never()).requestSort();
Julia Reynoldseb3dca72017-07-11 10:39:58 -04002070 }
2071
2072 @Test
2073 public void testHandleRankingSort_sendsUpdateOnSignalExtractorChange() throws Exception {
Julia Reynolds503ed942017-10-04 16:04:56 -04002074 mService.setRankingHelper(mRankingHelper);
Julia Reynoldseb3dca72017-07-11 10:39:58 -04002075 NotificationManagerService.WorkerHandler handler = mock(
2076 NotificationManagerService.WorkerHandler.class);
Julia Reynolds503ed942017-10-04 16:04:56 -04002077 mService.setHandler(handler);
Julia Reynoldseb3dca72017-07-11 10:39:58 -04002078
2079 Map<String, Answer> answers = getSignalExtractorSideEffects();
2080 for (String message : answers.keySet()) {
Julia Reynolds503ed942017-10-04 16:04:56 -04002081 mService.clearNotifications();
Julia Reynoldseb3dca72017-07-11 10:39:58 -04002082 final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel);
Julia Reynolds503ed942017-10-04 16:04:56 -04002083 mService.addNotification(r);
Julia Reynoldseb3dca72017-07-11 10:39:58 -04002084
2085 doAnswer(answers.get(message)).when(mRankingHelper).extractSignals(r);
2086
Julia Reynolds503ed942017-10-04 16:04:56 -04002087 mService.handleRankingSort();
Julia Reynoldseb3dca72017-07-11 10:39:58 -04002088 }
2089 verify(handler, times(answers.size())).scheduleSendRankingUpdate();
2090 }
2091
2092 @Test
2093 public void testHandleRankingSort_noUpdateWhenNoSignalChange() throws Exception {
Julia Reynolds503ed942017-10-04 16:04:56 -04002094 mService.setRankingHelper(mRankingHelper);
Julia Reynoldseb3dca72017-07-11 10:39:58 -04002095 NotificationManagerService.WorkerHandler handler = mock(
2096 NotificationManagerService.WorkerHandler.class);
Julia Reynolds503ed942017-10-04 16:04:56 -04002097 mService.setHandler(handler);
Julia Reynoldseb3dca72017-07-11 10:39:58 -04002098
2099 final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel);
Julia Reynolds503ed942017-10-04 16:04:56 -04002100 mService.addNotification(r);
Julia Reynoldseb3dca72017-07-11 10:39:58 -04002101
Julia Reynolds503ed942017-10-04 16:04:56 -04002102 mService.handleRankingSort();
Julia Reynoldseb3dca72017-07-11 10:39:58 -04002103 verify(handler, never()).scheduleSendRankingUpdate();
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04002104 }
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -04002105
2106 @Test
2107 public void testReadPolicyXml_readApprovedServicesFromXml() throws Exception {
2108 final String preupgradeXml = "<notification-policy version=\"1\">"
2109 + "<zen></zen>"
2110 + "<ranking></ranking>"
2111 + "<enabled_listeners>"
2112 + "<service_listing approved=\"test\" user=\"0\" primary=\"true\" />"
2113 + "</enabled_listeners>"
2114 + "<enabled_assistants>"
2115 + "<service_listing approved=\"test\" user=\"0\" primary=\"true\" />"
2116 + "</enabled_assistants>"
2117 + "<dnd_apps>"
2118 + "<service_listing approved=\"test\" user=\"0\" primary=\"true\" />"
2119 + "</dnd_apps>"
2120 + "</notification-policy>";
Julia Reynolds503ed942017-10-04 16:04:56 -04002121 mService.readPolicyXml(
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -04002122 new BufferedInputStream(new ByteArrayInputStream(preupgradeXml.getBytes())), false);
2123 verify(mListeners, times(1)).readXml(any());
2124 verify(mConditionProviders, times(1)).readXml(any());
2125 verify(mAssistants, times(1)).readXml(any());
2126
2127 // numbers are inflated for setup
2128 verify(mListeners, times(1)).migrateToXml();
2129 verify(mConditionProviders, times(1)).migrateToXml();
2130 verify(mAssistants, times(1)).migrateToXml();
2131 }
2132
2133 @Test
2134 public void testReadPolicyXml_readApprovedServicesFromSettings() throws Exception {
2135 final String preupgradeXml = "<notification-policy version=\"1\">"
2136 + "<zen></zen>"
2137 + "<ranking></ranking>"
2138 + "</notification-policy>";
Julia Reynolds503ed942017-10-04 16:04:56 -04002139 mService.readPolicyXml(
Julia Reynoldsd1bf5f02017-07-11 10:39:58 -04002140 new BufferedInputStream(new ByteArrayInputStream(preupgradeXml.getBytes())), false);
2141 verify(mListeners, never()).readXml(any());
2142 verify(mConditionProviders, never()).readXml(any());
2143 verify(mAssistants, never()).readXml(any());
2144
2145 // numbers are inflated for setup
2146 verify(mListeners, times(2)).migrateToXml();
2147 verify(mConditionProviders, times(2)).migrateToXml();
2148 verify(mAssistants, times(2)).migrateToXml();
2149 }
2150
Beverlyd4f96492017-08-02 13:36:11 -04002151
2152 @Test
2153 public void testLocaleChangedCallsUpdateDefaultZenModeRules() throws Exception {
2154 ZenModeHelper mZenModeHelper = mock(ZenModeHelper.class);
Julia Reynolds503ed942017-10-04 16:04:56 -04002155 mService.mZenModeHelper = mZenModeHelper;
2156 mService.mLocaleChangeReceiver.onReceive(mContext,
Beverlyd4f96492017-08-02 13:36:11 -04002157 new Intent(Intent.ACTION_LOCALE_CHANGED));
2158
2159 verify(mZenModeHelper, times(1)).updateDefaultZenRules();
2160 }
Julia Reynolds8617e4e2017-09-18 16:52:37 -04002161
2162 @Test
2163 public void testBumpFGImportance_noChannelChangePreOApp() throws Exception {
2164 String preOPkg = "preO";
2165 int preOUid = 145;
2166 final ApplicationInfo legacy = new ApplicationInfo();
2167 legacy.targetSdkVersion = Build.VERSION_CODES.N_MR1;
2168 when(mPackageManagerClient.getApplicationInfoAsUser(eq(preOPkg), anyInt(), anyInt()))
2169 .thenReturn(legacy);
2170 when(mPackageManagerClient.getPackageUidAsUser(eq(preOPkg), anyInt())).thenReturn(preOUid);
2171 getContext().setMockPackageManager(mPackageManagerClient);
2172
2173 Notification.Builder nb = new Notification.Builder(mContext,
2174 NotificationChannel.DEFAULT_CHANNEL_ID)
2175 .setContentTitle("foo")
2176 .setSmallIcon(android.R.drawable.sym_def_app_icon)
2177 .setFlag(Notification.FLAG_FOREGROUND_SERVICE, true)
2178 .setPriority(Notification.PRIORITY_MIN);
2179
2180 StatusBarNotification sbn = new StatusBarNotification(preOPkg, preOPkg, 9, "tag", preOUid,
2181 0, nb.build(), new UserHandle(preOUid), null, 0);
2182
2183 mBinderService.enqueueNotificationWithTag(preOPkg, preOPkg, "tag",
2184 sbn.getId(), sbn.getNotification(), sbn.getUserId());
2185 waitForIdle();
2186 assertEquals(IMPORTANCE_LOW,
Julia Reynolds503ed942017-10-04 16:04:56 -04002187 mService.getNotificationRecord(sbn.getKey()).getImportance());
Julia Reynolds8617e4e2017-09-18 16:52:37 -04002188
2189 nb = new Notification.Builder(mContext)
2190 .setContentTitle("foo")
2191 .setSmallIcon(android.R.drawable.sym_def_app_icon)
2192 .setFlag(Notification.FLAG_FOREGROUND_SERVICE, true)
2193 .setPriority(Notification.PRIORITY_MIN);
2194
2195 sbn = new StatusBarNotification(preOPkg, preOPkg, 9, "tag", preOUid,
2196 0, nb.build(), new UserHandle(preOUid), null, 0);
2197
2198 mBinderService.enqueueNotificationWithTag(preOPkg, preOPkg, "tag",
2199 sbn.getId(), sbn.getNotification(), sbn.getUserId());
2200 waitForIdle();
2201 assertEquals(IMPORTANCE_LOW,
Julia Reynolds503ed942017-10-04 16:04:56 -04002202 mService.getNotificationRecord(sbn.getKey()).getImportance());
Julia Reynolds8617e4e2017-09-18 16:52:37 -04002203
2204 NotificationChannel defaultChannel = mBinderService.getNotificationChannel(
2205 preOPkg, NotificationChannel.DEFAULT_CHANNEL_ID);
2206 assertEquals(IMPORTANCE_UNSPECIFIED, defaultChannel.getImportance());
2207 }
Julia Reynolds503ed942017-10-04 16:04:56 -04002208
2209 @Test
2210 public void testStats_updatedOnDirectReply() throws Exception {
2211 final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel);
2212 mService.addNotification(r);
2213
2214 mService.mNotificationDelegate.onNotificationDirectReplied(r.getKey());
2215 assertTrue(mService.getNotificationRecord(r.getKey()).getStats().hasDirectReplied());
2216 }
2217
2218 @Test
Julia Reynolds84dc96b2017-11-14 09:51:01 -05002219 public void testStats_updatedOnUserExpansion() throws Exception {
2220 NotificationRecord r = generateNotificationRecord(mTestNotificationChannel);
Julia Reynolds503ed942017-10-04 16:04:56 -04002221 mService.addNotification(r);
2222
2223 mService.mNotificationDelegate.onNotificationExpansionChanged(r.getKey(), true, true);
2224 assertTrue(mService.getNotificationRecord(r.getKey()).getStats().hasExpanded());
2225 mService.mNotificationDelegate.onNotificationExpansionChanged(r.getKey(), true, false);
2226 assertTrue(mService.getNotificationRecord(r.getKey()).getStats().hasExpanded());
2227 }
2228
2229 @Test
Julia Reynolds84dc96b2017-11-14 09:51:01 -05002230 public void testStats_notUpdatedOnAutoExpansion() throws Exception {
2231 NotificationRecord r = generateNotificationRecord(mTestNotificationChannel);
2232 mService.addNotification(r);
2233
2234 mService.mNotificationDelegate.onNotificationExpansionChanged(r.getKey(), false, true);
2235 assertFalse(mService.getNotificationRecord(r.getKey()).getStats().hasExpanded());
2236 mService.mNotificationDelegate.onNotificationExpansionChanged(r.getKey(), false, false);
2237 assertFalse(mService.getNotificationRecord(r.getKey()).getStats().hasExpanded());
2238 }
2239
2240 @Test
Julia Reynolds503ed942017-10-04 16:04:56 -04002241 public void testStats_updatedOnViewSettings() throws Exception {
2242 final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel);
2243 mService.addNotification(r);
2244
2245 mService.mNotificationDelegate.onNotificationSettingsViewed(r.getKey());
2246 assertTrue(mService.getNotificationRecord(r.getKey()).getStats().hasViewedSettings());
2247 }
2248
2249 @Test
2250 public void testStats_updatedOnVisibilityChanged() throws Exception {
2251 final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel);
2252 mService.addNotification(r);
2253
2254 NotificationVisibility nv = NotificationVisibility.obtain(r.getKey(), 1, true);
2255 mService.mNotificationDelegate.onNotificationVisibilityChanged(
2256 new NotificationVisibility[] {nv}, new NotificationVisibility[]{});
2257 assertTrue(mService.getNotificationRecord(r.getKey()).getStats().hasSeen());
2258 mService.mNotificationDelegate.onNotificationVisibilityChanged(
2259 new NotificationVisibility[] {}, new NotificationVisibility[]{nv});
2260 assertTrue(mService.getNotificationRecord(r.getKey()).getStats().hasSeen());
2261 }
2262
2263 @Test
2264 public void testStats_dismissalSurface() throws Exception {
2265 final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel);
2266 mService.addNotification(r);
2267
2268 mService.mNotificationDelegate.onNotificationClear(mUid, 0, PKG, r.sbn.getTag(),
2269 r.sbn.getId(), r.getUserId(), r.getKey(), NotificationStats.DISMISSAL_AOD);
2270 waitForIdle();
2271
2272 assertEquals(NotificationStats.DISMISSAL_AOD, r.getStats().getDismissalSurface());
2273 }
2274
2275 @Test
2276 public void testUserSentimentChangeTriggersUpdate() throws Exception {
2277 final NotificationRecord r = generateNotificationRecord(mTestNotificationChannel);
2278 mService.addNotification(r);
2279 NotificationManagerService.WorkerHandler handler = mock(
2280 NotificationManagerService.WorkerHandler.class);
2281 mService.setHandler(handler);
2282
2283 Bundle signals = new Bundle();
2284 signals.putInt(Adjustment.KEY_USER_SENTIMENT,
2285 NotificationListenerService.Ranking.USER_SENTIMENT_NEGATIVE);
2286 Adjustment adjustment = new Adjustment(
2287 r.sbn.getPackageName(), r.getKey(), signals, "", r.getUser().getIdentifier());
2288 mBinderService.applyAdjustmentFromAssistant(null, adjustment);
2289
2290 waitForIdle();
2291
2292 verify(handler, timeout(300).times(1)).scheduleSendRankingUpdate();
2293 }
Geoffrey Pitsche75a66e2016-11-22 11:12:11 -05002294}