blob: 73adf25cb3ec8d150441775f7834e858e0ded875 [file] [log] [blame]
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001/*
2 * Copyright (C) 2018 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 */
16package com.android.server.notification;
17
18import static android.app.NotificationManager.IMPORTANCE_DEFAULT;
19import static android.app.NotificationManager.IMPORTANCE_HIGH;
20import static android.app.NotificationManager.IMPORTANCE_LOW;
21import static android.app.NotificationManager.IMPORTANCE_MAX;
22import static android.app.NotificationManager.IMPORTANCE_NONE;
23import static android.app.NotificationManager.IMPORTANCE_UNSPECIFIED;
24
25import static junit.framework.Assert.assertNull;
26import static junit.framework.Assert.fail;
27
28import static org.junit.Assert.assertEquals;
29import static org.junit.Assert.assertFalse;
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -040030import static org.junit.Assert.assertNotEquals;
Aaron Heuckrothe5bec152018-07-09 16:26:09 -040031import static org.junit.Assert.assertNotNull;
32import static org.junit.Assert.assertTrue;
33import static org.mockito.ArgumentMatchers.any;
34import static org.mockito.Matchers.anyInt;
35import static org.mockito.Matchers.anyString;
36import static org.mockito.Matchers.eq;
37import static org.mockito.Mockito.mock;
38import static org.mockito.Mockito.never;
39import static org.mockito.Mockito.reset;
40import static org.mockito.Mockito.times;
41import static org.mockito.Mockito.verify;
42import static org.mockito.Mockito.when;
43
44import android.app.Notification;
45import android.app.NotificationChannel;
46import android.app.NotificationChannelGroup;
47import android.app.NotificationManager;
48import android.content.ContentProvider;
49import android.content.Context;
50import android.content.IContentProvider;
51import android.content.pm.ApplicationInfo;
52import android.content.pm.PackageInfo;
53import android.content.pm.PackageManager;
54import android.content.pm.Signature;
55import android.content.res.Resources;
56import android.graphics.Color;
57import android.media.AudioAttributes;
58import android.net.Uri;
59import android.os.Build;
60import android.os.UserHandle;
61import android.provider.Settings;
62import android.provider.Settings.Secure;
63import android.support.test.InstrumentationRegistry;
64import android.support.test.runner.AndroidJUnit4;
65import android.test.suitebuilder.annotation.SmallTest;
66import android.testing.TestableContentResolver;
67import android.util.ArrayMap;
68import android.util.Xml;
69
70import com.android.internal.util.FastXmlSerializer;
71import com.android.server.UiServiceTestCase;
72
73import org.json.JSONArray;
74import org.json.JSONObject;
75import org.junit.Before;
76import org.junit.Test;
77import org.junit.runner.RunWith;
78import org.mockito.Mock;
79import org.mockito.MockitoAnnotations;
80import org.xmlpull.v1.XmlPullParser;
81import org.xmlpull.v1.XmlSerializer;
82
83import java.io.BufferedInputStream;
84import java.io.BufferedOutputStream;
85import java.io.ByteArrayInputStream;
86import java.io.ByteArrayOutputStream;
87import java.util.Arrays;
88import java.util.HashMap;
89import java.util.List;
90import java.util.Map;
91import java.util.Objects;
92import java.util.concurrent.ThreadLocalRandom;
93
94@SmallTest
95@RunWith(AndroidJUnit4.class)
96public class PreferencesHelperTest extends UiServiceTestCase {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -040097 private static final int UID_N_MR1 = 0;
Aaron Heuckrothe5bec152018-07-09 16:26:09 -040098 private static final UserHandle USER = UserHandle.of(0);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -040099 private static final int UID_O = 1111;
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400100 private static final String SYSTEM_PKG = "android";
101 private static final int SYSTEM_UID= 1000;
102 private static final UserHandle USER2 = UserHandle.of(10);
103 private static final String TEST_CHANNEL_ID = "test_channel_id";
104 private static final String TEST_AUTHORITY = "test";
105 private static final Uri SOUND_URI =
106 Uri.parse("content://" + TEST_AUTHORITY + "/internal/audio/media/10");
107 private static final Uri CANONICAL_SOUND_URI =
108 Uri.parse("content://" + TEST_AUTHORITY
109 + "/internal/audio/media/10?title=Test&canonical=1");
110
111 @Mock NotificationUsageStats mUsageStats;
112 @Mock RankingHandler mHandler;
113 @Mock PackageManager mPm;
114 @Mock IContentProvider mTestIContentProvider;
115 @Mock Context mContext;
116 @Mock ZenModeHelper mMockZenModeHelper;
117
118 private NotificationManager.Policy mTestNotificationPolicy;
119
120 private PreferencesHelper mHelper;
121 private AudioAttributes mAudioAttributes;
122
123 @Before
124 public void setUp() throws Exception {
125 MockitoAnnotations.initMocks(this);
126 UserHandle user = UserHandle.ALL;
127
128 final ApplicationInfo legacy = new ApplicationInfo();
129 legacy.targetSdkVersion = Build.VERSION_CODES.N_MR1;
130 final ApplicationInfo upgrade = new ApplicationInfo();
131 upgrade.targetSdkVersion = Build.VERSION_CODES.O;
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400132 when(mPm.getApplicationInfoAsUser(eq(PKG_N_MR1), anyInt(), anyInt())).thenReturn(legacy);
133 when(mPm.getApplicationInfoAsUser(eq(PKG_O), anyInt(), anyInt())).thenReturn(upgrade);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400134 when(mPm.getApplicationInfoAsUser(eq(SYSTEM_PKG), anyInt(), anyInt())).thenReturn(upgrade);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400135 when(mPm.getPackageUidAsUser(eq(PKG_N_MR1), anyInt())).thenReturn(UID_N_MR1);
136 when(mPm.getPackageUidAsUser(eq(PKG_O), anyInt())).thenReturn(UID_O);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400137 when(mPm.getPackageUidAsUser(eq(SYSTEM_PKG), anyInt())).thenReturn(SYSTEM_UID);
138 PackageInfo info = mock(PackageInfo.class);
139 info.signatures = new Signature[] {mock(Signature.class)};
140 when(mPm.getPackageInfoAsUser(eq(SYSTEM_PKG), anyInt(), anyInt())).thenReturn(info);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400141 when(mPm.getPackageInfoAsUser(eq(PKG_N_MR1), anyInt(), anyInt()))
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400142 .thenReturn(mock(PackageInfo.class));
143 when(mContext.getResources()).thenReturn(
144 InstrumentationRegistry.getContext().getResources());
145 when(mContext.getContentResolver()).thenReturn(
146 InstrumentationRegistry.getContext().getContentResolver());
147 when(mContext.getPackageManager()).thenReturn(mPm);
148 when(mContext.getApplicationInfo()).thenReturn(legacy);
149 // most tests assume badging is enabled
150 TestableContentResolver contentResolver = getContext().getContentResolver();
151 contentResolver.setFallbackToExisting(false);
152 Secure.putIntForUser(contentResolver,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400153 Secure.NOTIFICATION_BADGING, 1, UserHandle.getUserId(UID_N_MR1));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400154
155 ContentProvider testContentProvider = mock(ContentProvider.class);
156 when(testContentProvider.getIContentProvider()).thenReturn(mTestIContentProvider);
157 contentResolver.addProvider(TEST_AUTHORITY, testContentProvider);
158
159 when(mTestIContentProvider.canonicalize(any(), eq(SOUND_URI)))
160 .thenReturn(CANONICAL_SOUND_URI);
161 when(mTestIContentProvider.canonicalize(any(), eq(CANONICAL_SOUND_URI)))
162 .thenReturn(CANONICAL_SOUND_URI);
163 when(mTestIContentProvider.uncanonicalize(any(), eq(CANONICAL_SOUND_URI)))
164 .thenReturn(SOUND_URI);
165
166 mTestNotificationPolicy = new NotificationManager.Policy(0, 0, 0, 0,
167 NotificationManager.Policy.STATE_CHANNELS_BYPASSING_DND);
168 when(mMockZenModeHelper.getNotificationPolicy()).thenReturn(mTestNotificationPolicy);
169 mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper);
170 resetZenModeHelper();
171
172 mAudioAttributes = new AudioAttributes.Builder()
173 .setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN)
174 .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
175 .setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED)
176 .build();
177 }
178
179 private NotificationChannel getDefaultChannel() {
180 return new NotificationChannel(NotificationChannel.DEFAULT_CHANNEL_ID, "name",
181 IMPORTANCE_LOW);
182 }
183
184 private ByteArrayOutputStream writeXmlAndPurge(String pkg, int uid, boolean forBackup,
185 String... channelIds)
186 throws Exception {
187 XmlSerializer serializer = new FastXmlSerializer();
188 ByteArrayOutputStream baos = new ByteArrayOutputStream();
189 serializer.setOutput(new BufferedOutputStream(baos), "utf-8");
190 serializer.startDocument(null, true);
191 mHelper.writeXml(serializer, forBackup);
192 serializer.endDocument();
193 serializer.flush();
194 for (String channelId : channelIds) {
195 mHelper.permanentlyDeleteNotificationChannel(pkg, uid, channelId);
196 }
197 return baos;
198 }
199
200 private void loadStreamXml(ByteArrayOutputStream stream, boolean forRestore) throws Exception {
201 loadByteArrayXml(stream.toByteArray(), forRestore);
202 }
203
204 private void loadByteArrayXml(byte[] byteArray, boolean forRestore) throws Exception {
205 XmlPullParser parser = Xml.newPullParser();
206 parser.setInput(new BufferedInputStream(new ByteArrayInputStream(byteArray)), null);
207 parser.nextTag();
208 mHelper.readXml(parser, forRestore);
209 }
210
211 private void compareChannels(NotificationChannel expected, NotificationChannel actual) {
212 assertEquals(expected.getId(), actual.getId());
213 assertEquals(expected.getName(), actual.getName());
214 assertEquals(expected.getDescription(), actual.getDescription());
215 assertEquals(expected.shouldVibrate(), actual.shouldVibrate());
216 assertEquals(expected.shouldShowLights(), actual.shouldShowLights());
217 assertEquals(expected.getImportance(), actual.getImportance());
218 assertEquals(expected.getLockscreenVisibility(), actual.getLockscreenVisibility());
219 assertEquals(expected.getSound(), actual.getSound());
220 assertEquals(expected.canBypassDnd(), actual.canBypassDnd());
221 assertTrue(Arrays.equals(expected.getVibrationPattern(), actual.getVibrationPattern()));
222 assertEquals(expected.getGroup(), actual.getGroup());
223 assertEquals(expected.getAudioAttributes(), actual.getAudioAttributes());
224 assertEquals(expected.getLightColor(), actual.getLightColor());
225 }
226
227 private void compareGroups(NotificationChannelGroup expected, NotificationChannelGroup actual) {
228 assertEquals(expected.getId(), actual.getId());
229 assertEquals(expected.getName(), actual.getName());
230 assertEquals(expected.getDescription(), actual.getDescription());
231 assertEquals(expected.isBlocked(), actual.isBlocked());
232 }
233
234 private NotificationChannel getChannel() {
235 return new NotificationChannel("id", "name", IMPORTANCE_LOW);
236 }
237
238 private NotificationChannel findChannel(List<NotificationChannel> channels, String id) {
239 for (NotificationChannel channel : channels) {
240 if (channel.getId().equals(id)) {
241 return channel;
242 }
243 }
244 return null;
245 }
246
247 private void resetZenModeHelper() {
248 reset(mMockZenModeHelper);
249 when(mMockZenModeHelper.getNotificationPolicy()).thenReturn(mTestNotificationPolicy);
250 }
251
252 @Test
253 public void testChannelXml() throws Exception {
254 NotificationChannelGroup ncg = new NotificationChannelGroup("1", "bye");
255 ncg.setBlocked(true);
256 ncg.setDescription("group desc");
257 NotificationChannelGroup ncg2 = new NotificationChannelGroup("2", "hello");
258 NotificationChannel channel1 =
259 new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH);
260 NotificationChannel channel2 =
261 new NotificationChannel("id2", "name2", IMPORTANCE_LOW);
262 channel2.setDescription("descriptions for all");
263 channel2.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes);
264 channel2.enableLights(true);
265 channel2.setBypassDnd(true);
266 channel2.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
267 channel2.enableVibration(true);
268 channel2.setGroup(ncg.getId());
269 channel2.setVibrationPattern(new long[]{100, 67, 145, 156});
270 channel2.setLightColor(Color.BLUE);
271
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400272 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg, true);
273 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg2, true);
274 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false);
275 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, false, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400276
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400277 mHelper.setShowBadge(PKG_N_MR1, UID_N_MR1, true);
278 mHelper.setAppImportanceLocked(PKG_N_MR1, UID_N_MR1);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400279
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400280 ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, false, channel1.getId(),
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400281 channel2.getId(), NotificationChannel.DEFAULT_CHANNEL_ID);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400282 mHelper.onPackagesChanged(true, UserHandle.myUserId(), new String[]{PKG_N_MR1}, new int[]{
283 UID_N_MR1});
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400284
285 loadStreamXml(baos, false);
286
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400287 assertTrue(mHelper.canShowBadge(PKG_N_MR1, UID_N_MR1));
288 assertTrue(mHelper.getIsAppImportanceLocked(PKG_N_MR1, UID_N_MR1));
289 assertEquals(channel1, mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1.getId(), false));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400290 compareChannels(channel2,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400291 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2.getId(), false));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400292
293 List<NotificationChannelGroup> actualGroups =
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400294 mHelper.getNotificationChannelGroups(PKG_N_MR1, UID_N_MR1, false, true).getList();
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400295 boolean foundNcg = false;
296 for (NotificationChannelGroup actual : actualGroups) {
297 if (ncg.getId().equals(actual.getId())) {
298 foundNcg = true;
299 compareGroups(ncg, actual);
300 } else if (ncg2.getId().equals(actual.getId())) {
301 compareGroups(ncg2, actual);
302 }
303 }
304 assertTrue(foundNcg);
305
306 boolean foundChannel2Group = false;
307 for (NotificationChannelGroup actual : actualGroups) {
308 if (channel2.getGroup().equals(actual.getChannels().get(0).getGroup())) {
309 foundChannel2Group = true;
310 break;
311 }
312 }
313 assertTrue(foundChannel2Group);
314 }
315
316 @Test
317 public void testChannelXmlForBackup() throws Exception {
318 NotificationChannelGroup ncg = new NotificationChannelGroup("1", "bye");
319 NotificationChannelGroup ncg2 = new NotificationChannelGroup("2", "hello");
320 NotificationChannel channel1 =
321 new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH);
322 NotificationChannel channel2 =
323 new NotificationChannel("id2", "name2", IMPORTANCE_LOW);
324 channel2.setDescription("descriptions for all");
325 channel2.setSound(SOUND_URI, mAudioAttributes);
326 channel2.enableLights(true);
327 channel2.setBypassDnd(true);
328 channel2.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
329 channel2.enableVibration(false);
330 channel2.setGroup(ncg.getId());
331 channel2.setLightColor(Color.BLUE);
332 NotificationChannel channel3 = new NotificationChannel("id3", "NAM3", IMPORTANCE_HIGH);
333 channel3.enableVibration(true);
334
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400335 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg, true);
336 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg2, true);
337 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false);
338 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, false, false);
339 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3, false, false);
340 mHelper.createNotificationChannel(PKG_O, UID_O, getChannel(), true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400341
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400342 mHelper.setShowBadge(PKG_N_MR1, UID_N_MR1, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400343
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400344 mHelper.setImportance(PKG_O, UID_O, IMPORTANCE_NONE);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400345
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400346 ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, true, channel1.getId(),
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400347 channel2.getId(), channel3.getId(), NotificationChannel.DEFAULT_CHANNEL_ID);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400348 mHelper.onPackagesChanged(true, UserHandle.myUserId(), new String[]{PKG_N_MR1, PKG_O},
349 new int[]{UID_N_MR1, UID_O});
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400350
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400351 mHelper.setShowBadge(PKG_O, UID_O, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400352
353 loadStreamXml(baos, true);
354
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400355 assertEquals(IMPORTANCE_NONE, mHelper.getImportance(PKG_O, UID_O));
356 assertTrue(mHelper.canShowBadge(PKG_N_MR1, UID_N_MR1));
357 assertEquals(channel1, mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1.getId(), false));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400358 compareChannels(channel2,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400359 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2.getId(), false));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400360 compareChannels(channel3,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400361 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3.getId(), false));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400362
363 List<NotificationChannelGroup> actualGroups =
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400364 mHelper.getNotificationChannelGroups(PKG_N_MR1, UID_N_MR1, false, true).getList();
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400365 boolean foundNcg = false;
366 for (NotificationChannelGroup actual : actualGroups) {
367 if (ncg.getId().equals(actual.getId())) {
368 foundNcg = true;
369 compareGroups(ncg, actual);
370 } else if (ncg2.getId().equals(actual.getId())) {
371 compareGroups(ncg2, actual);
372 }
373 }
374 assertTrue(foundNcg);
375
376 boolean foundChannel2Group = false;
377 for (NotificationChannelGroup actual : actualGroups) {
378 if (channel2.getGroup().equals(actual.getChannels().get(0).getGroup())) {
379 foundChannel2Group = true;
380 break;
381 }
382 }
383 assertTrue(foundChannel2Group);
384 }
385
386 @Test
387 public void testBackupXml_backupCanonicalizedSoundUri() throws Exception {
388 NotificationChannel channel =
389 new NotificationChannel("id", "name", IMPORTANCE_LOW);
390 channel.setSound(SOUND_URI, mAudioAttributes);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400391 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400392
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400393 ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, true, channel.getId());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400394
395 // Testing that in restore we are given the canonical version
396 loadStreamXml(baos, true);
397 verify(mTestIContentProvider).uncanonicalize(any(), eq(CANONICAL_SOUND_URI));
398 }
399
400 @Test
401 public void testRestoreXml_withExistentCanonicalizedSoundUri() throws Exception {
402 Uri localUri = Uri.parse("content://" + TEST_AUTHORITY + "/local/url");
403 Uri canonicalBasedOnLocal = localUri.buildUpon()
404 .appendQueryParameter("title", "Test")
405 .appendQueryParameter("canonical", "1")
406 .build();
407 when(mTestIContentProvider.canonicalize(any(), eq(CANONICAL_SOUND_URI)))
408 .thenReturn(canonicalBasedOnLocal);
409 when(mTestIContentProvider.uncanonicalize(any(), eq(CANONICAL_SOUND_URI)))
410 .thenReturn(localUri);
411 when(mTestIContentProvider.uncanonicalize(any(), eq(canonicalBasedOnLocal)))
412 .thenReturn(localUri);
413
414 NotificationChannel channel =
415 new NotificationChannel("id", "name", IMPORTANCE_LOW);
416 channel.setSound(SOUND_URI, mAudioAttributes);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400417 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false);
418 ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, true, channel.getId());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400419
420 loadStreamXml(baos, true);
421
422 NotificationChannel actualChannel = mHelper.getNotificationChannel(
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400423 PKG_N_MR1, UID_N_MR1, channel.getId(), false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400424 assertEquals(localUri, actualChannel.getSound());
425 }
426
427 @Test
428 public void testRestoreXml_withNonExistentCanonicalizedSoundUri() throws Exception {
429 Thread.sleep(3000);
430 when(mTestIContentProvider.canonicalize(any(), eq(CANONICAL_SOUND_URI)))
431 .thenReturn(null);
432 when(mTestIContentProvider.uncanonicalize(any(), eq(CANONICAL_SOUND_URI)))
433 .thenReturn(null);
434
435 NotificationChannel channel =
436 new NotificationChannel("id", "name", IMPORTANCE_LOW);
437 channel.setSound(SOUND_URI, mAudioAttributes);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400438 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false);
439 ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, true, channel.getId());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400440
441 loadStreamXml(baos, true);
442
443 NotificationChannel actualChannel = mHelper.getNotificationChannel(
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400444 PKG_N_MR1, UID_N_MR1, channel.getId(), false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400445 assertEquals(Settings.System.DEFAULT_NOTIFICATION_URI, actualChannel.getSound());
446 }
447
448
449 /**
450 * Although we don't make backups with uncanonicalized uris anymore, we used to, so we have to
451 * handle its restore properly.
452 */
453 @Test
454 public void testRestoreXml_withUncanonicalizedNonLocalSoundUri() throws Exception {
455 // Not a local uncanonicalized uri, simulating that it fails to exist locally
456 when(mTestIContentProvider.canonicalize(any(), eq(SOUND_URI))).thenReturn(null);
457 String id = "id";
458 String backupWithUncanonicalizedSoundUri = "<ranking version=\"1\">\n"
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400459 + "<package name=\"" + PKG_N_MR1 + "\" show_badge=\"true\">\n"
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400460 + "<channel id=\"" + id + "\" name=\"name\" importance=\"2\" "
461 + "sound=\"" + SOUND_URI + "\" "
462 + "usage=\"6\" content_type=\"0\" flags=\"1\" show_badge=\"true\" />\n"
463 + "<channel id=\"miscellaneous\" name=\"Uncategorized\" usage=\"5\" "
464 + "content_type=\"4\" flags=\"0\" show_badge=\"true\" />\n"
465 + "</package>\n"
466 + "</ranking>\n";
467
468 loadByteArrayXml(backupWithUncanonicalizedSoundUri.getBytes(), true);
469
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400470 NotificationChannel actualChannel = mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, id, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400471 assertEquals(Settings.System.DEFAULT_NOTIFICATION_URI, actualChannel.getSound());
472 }
473
474 @Test
475 public void testBackupRestoreXml_withNullSoundUri() throws Exception {
476 NotificationChannel channel =
477 new NotificationChannel("id", "name", IMPORTANCE_LOW);
478 channel.setSound(null, mAudioAttributes);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400479 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false);
480 ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, true, channel.getId());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400481
482 loadStreamXml(baos, true);
483
484 NotificationChannel actualChannel = mHelper.getNotificationChannel(
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400485 PKG_N_MR1, UID_N_MR1, channel.getId(), false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400486 assertEquals(null, actualChannel.getSound());
487 }
488
489 @Test
490 public void testChannelXml_backup() throws Exception {
491 NotificationChannelGroup ncg = new NotificationChannelGroup("1", "bye");
492 NotificationChannelGroup ncg2 = new NotificationChannelGroup("2", "hello");
493 NotificationChannel channel1 =
494 new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH);
495 NotificationChannel channel2 =
496 new NotificationChannel("id2", "name2", IMPORTANCE_LOW);
497 NotificationChannel channel3 =
498 new NotificationChannel("id3", "name3", IMPORTANCE_LOW);
499 channel3.setGroup(ncg.getId());
500
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400501 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg, true);
502 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg2, true);
503 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false);
504 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, false, false);
505 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400506
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400507 mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1.getId());
508 mHelper.deleteNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg.getId());
509 assertEquals(channel2, mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2.getId(), false));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400510
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400511 ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, true, channel1.getId(),
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400512 channel2.getId(), channel3.getId(), NotificationChannel.DEFAULT_CHANNEL_ID);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400513 mHelper.onPackagesChanged(true, UserHandle.myUserId(), new String[]{PKG_N_MR1}, new int[]{
514 UID_N_MR1});
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400515
516 XmlPullParser parser = Xml.newPullParser();
517 parser.setInput(new BufferedInputStream(new ByteArrayInputStream(baos.toByteArray())),
518 null);
519 parser.nextTag();
520 mHelper.readXml(parser, true);
521
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400522 assertNull(mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1.getId(), false));
523 assertNull(mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3.getId(), false));
524 assertNull(mHelper.getNotificationChannelGroup(ncg.getId(), PKG_N_MR1, UID_N_MR1));
525 //assertEquals(ncg2, mHelper.getNotificationChannelGroup(ncg2.getId(), PKG_N_MR1, UID_N_MR1));
526 assertEquals(channel2, mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2.getId(), false));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400527 }
528
529 @Test
530 public void testChannelXml_defaultChannelLegacyApp_noUserSettings() throws Exception {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400531 ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, false,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400532 NotificationChannel.DEFAULT_CHANNEL_ID);
533
534 loadStreamXml(baos, false);
535
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400536 final NotificationChannel updated = mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400537 NotificationChannel.DEFAULT_CHANNEL_ID, false);
538 assertEquals(NotificationManager.IMPORTANCE_UNSPECIFIED, updated.getImportance());
539 assertFalse(updated.canBypassDnd());
540 assertEquals(NotificationManager.VISIBILITY_NO_OVERRIDE, updated.getLockscreenVisibility());
541 assertEquals(0, updated.getUserLockedFields());
542 }
543
544 @Test
545 public void testChannelXml_defaultChannelUpdatedApp_userSettings() throws Exception {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400546 final NotificationChannel defaultChannel = mHelper.getNotificationChannel(PKG_N_MR1,
547 UID_N_MR1,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400548 NotificationChannel.DEFAULT_CHANNEL_ID, false);
549 defaultChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400550 mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, defaultChannel, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400551
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400552 ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, false,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400553 NotificationChannel.DEFAULT_CHANNEL_ID);
554
555 loadStreamXml(baos, false);
556
557 assertEquals(NotificationManager.IMPORTANCE_LOW, mHelper.getNotificationChannel(
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400558 PKG_N_MR1, UID_N_MR1, NotificationChannel.DEFAULT_CHANNEL_ID, false).getImportance());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400559 }
560
561 @Test
562 public void testChannelXml_upgradeCreateDefaultChannel() throws Exception {
563 final String preupgradeXml = "<ranking version=\"1\">\n"
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400564 + "<package name=\"" + PKG_N_MR1
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400565 + "\" importance=\"" + NotificationManager.IMPORTANCE_HIGH
566 + "\" priority=\"" + Notification.PRIORITY_MAX + "\" visibility=\""
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400567 + Notification.VISIBILITY_SECRET + "\"" +" uid=\"" + UID_N_MR1 + "\" />\n"
568 + "<package name=\"" + PKG_O + "\" uid=\"" + UID_O + "\" visibility=\""
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400569 + Notification.VISIBILITY_PRIVATE + "\" />\n"
570 + "</ranking>";
571 XmlPullParser parser = Xml.newPullParser();
572 parser.setInput(new BufferedInputStream(new ByteArrayInputStream(preupgradeXml.getBytes())),
573 null);
574 parser.nextTag();
575 mHelper.readXml(parser, false);
576
577 final NotificationChannel updated1 =
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400578 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, NotificationChannel.DEFAULT_CHANNEL_ID, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400579 assertEquals(NotificationManager.IMPORTANCE_HIGH, updated1.getImportance());
580 assertTrue(updated1.canBypassDnd());
581 assertEquals(Notification.VISIBILITY_SECRET, updated1.getLockscreenVisibility());
582 assertEquals(NotificationChannel.USER_LOCKED_IMPORTANCE
583 | NotificationChannel.USER_LOCKED_PRIORITY
584 | NotificationChannel.USER_LOCKED_VISIBILITY,
585 updated1.getUserLockedFields());
586
587 // No Default Channel created for updated packages
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400588 assertEquals(null, mHelper.getNotificationChannel(PKG_O, UID_O,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400589 NotificationChannel.DEFAULT_CHANNEL_ID, false));
590 }
591
592 @Test
593 public void testChannelXml_upgradeDeletesDefaultChannel() throws Exception {
594 final NotificationChannel defaultChannel = mHelper.getNotificationChannel(
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400595 PKG_N_MR1, UID_N_MR1, NotificationChannel.DEFAULT_CHANNEL_ID, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400596 assertTrue(defaultChannel != null);
597 ByteArrayOutputStream baos =
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400598 writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, false, NotificationChannel.DEFAULT_CHANNEL_ID);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400599 // Load package at higher sdk.
600 final ApplicationInfo upgraded = new ApplicationInfo();
601 upgraded.targetSdkVersion = Build.VERSION_CODES.N_MR1 + 1;
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400602 when(mPm.getApplicationInfoAsUser(eq(PKG_N_MR1), anyInt(), anyInt())).thenReturn(upgraded);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400603 loadStreamXml(baos, false);
604
605 // Default Channel should be gone.
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400606 assertEquals(null, mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400607 NotificationChannel.DEFAULT_CHANNEL_ID, false));
608 }
609
610 @Test
611 public void testDeletesDefaultChannelAfterChannelIsCreated() throws Exception {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400612 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400613 new NotificationChannel("bananas", "bananas", IMPORTANCE_LOW), true, false);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400614 ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, false,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400615 NotificationChannel.DEFAULT_CHANNEL_ID, "bananas");
616
617 // Load package at higher sdk.
618 final ApplicationInfo upgraded = new ApplicationInfo();
619 upgraded.targetSdkVersion = Build.VERSION_CODES.N_MR1 + 1;
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400620 when(mPm.getApplicationInfoAsUser(eq(PKG_N_MR1), anyInt(), anyInt())).thenReturn(upgraded);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400621 loadStreamXml(baos, false);
622
623 // Default Channel should be gone.
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400624 assertEquals(null, mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400625 NotificationChannel.DEFAULT_CHANNEL_ID, false));
626 }
627
628 @Test
629 public void testLoadingOldChannelsDoesNotDeleteNewlyCreatedChannels() throws Exception {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400630 ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, false,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400631 NotificationChannel.DEFAULT_CHANNEL_ID, "bananas");
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400632 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400633 new NotificationChannel("bananas", "bananas", IMPORTANCE_LOW), true, false);
634
635 loadStreamXml(baos, false);
636
637 // Should still have the newly created channel that wasn't in the xml.
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400638 assertTrue(mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, "bananas", false) != null);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400639 }
640
641 @Test
642 public void testCreateChannel_blocked() throws Exception {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400643 mHelper.setImportance(PKG_N_MR1, UID_N_MR1, IMPORTANCE_NONE);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400644
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400645 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400646 new NotificationChannel("bananas", "bananas", IMPORTANCE_LOW), true, false);
647 }
648
649 @Test
650 public void testCreateChannel_badImportance() throws Exception {
651 try {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400652 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400653 new NotificationChannel("bananas", "bananas", IMPORTANCE_NONE - 1),
654 true, false);
655 fail("Was allowed to create a channel with invalid importance");
656 } catch (IllegalArgumentException e) {
657 // yay
658 }
659 try {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400660 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400661 new NotificationChannel("bananas", "bananas", IMPORTANCE_UNSPECIFIED),
662 true, false);
663 fail("Was allowed to create a channel with invalid importance");
664 } catch (IllegalArgumentException e) {
665 // yay
666 }
667 try {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400668 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400669 new NotificationChannel("bananas", "bananas", IMPORTANCE_MAX + 1),
670 true, false);
671 fail("Was allowed to create a channel with invalid importance");
672 } catch (IllegalArgumentException e) {
673 // yay
674 }
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400675 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400676 new NotificationChannel("bananas", "bananas", IMPORTANCE_NONE), true, false);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400677 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400678 new NotificationChannel("bananas", "bananas", IMPORTANCE_MAX), true, false);
679 }
680
681
682 @Test
683 public void testUpdate() throws Exception {
684 // no fields locked by user
685 final NotificationChannel channel =
686 new NotificationChannel("id2", "name2", IMPORTANCE_LOW);
687 channel.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes);
688 channel.enableLights(true);
689 channel.setBypassDnd(true);
690 channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
691
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400692 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, false, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400693
694 // same id, try to update all fields
695 final NotificationChannel channel2 =
696 new NotificationChannel("id2", "name2", NotificationManager.IMPORTANCE_HIGH);
697 channel2.setSound(new Uri.Builder().scheme("test2").build(), mAudioAttributes);
698 channel2.enableLights(false);
699 channel2.setBypassDnd(false);
700 channel2.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
701
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400702 mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400703
704 // all fields should be changed
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400705 assertEquals(channel2, mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId(), false));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400706
707 verify(mHandler, times(1)).requestSort();
708 }
709
710 @Test
711 public void testUpdate_preUpgrade_updatesAppFields() throws Exception {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400712 mHelper.setImportance(PKG_N_MR1, UID_N_MR1, IMPORTANCE_UNSPECIFIED);
713 assertTrue(mHelper.canShowBadge(PKG_N_MR1, UID_N_MR1));
714 assertEquals(Notification.PRIORITY_DEFAULT, mHelper.getPackagePriority(PKG_N_MR1, UID_N_MR1));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400715 assertEquals(NotificationManager.VISIBILITY_NO_OVERRIDE,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400716 mHelper.getPackageVisibility(PKG_N_MR1, UID_N_MR1));
717 assertFalse(mHelper.getIsAppImportanceLocked(PKG_N_MR1, UID_N_MR1));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400718
719 NotificationChannel defaultChannel = mHelper.getNotificationChannel(
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400720 PKG_N_MR1, UID_N_MR1, NotificationChannel.DEFAULT_CHANNEL_ID, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400721
722 defaultChannel.setShowBadge(false);
723 defaultChannel.setImportance(IMPORTANCE_NONE);
724 defaultChannel.setBypassDnd(true);
725 defaultChannel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
726
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400727 mHelper.setAppImportanceLocked(PKG_N_MR1, UID_N_MR1);
728 mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, defaultChannel, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400729
730 // ensure app level fields are changed
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400731 assertFalse(mHelper.canShowBadge(PKG_N_MR1, UID_N_MR1));
732 assertEquals(Notification.PRIORITY_MAX, mHelper.getPackagePriority(PKG_N_MR1, UID_N_MR1));
733 assertEquals(Notification.VISIBILITY_SECRET, mHelper.getPackageVisibility(PKG_N_MR1,
734 UID_N_MR1));
735 assertEquals(IMPORTANCE_NONE, mHelper.getImportance(PKG_N_MR1, UID_N_MR1));
736 assertTrue(mHelper.getIsAppImportanceLocked(PKG_N_MR1, UID_N_MR1));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400737 }
738
739 @Test
740 public void testUpdate_postUpgrade_noUpdateAppFields() throws Exception {
741 final NotificationChannel channel = new NotificationChannel("id2", "name2", IMPORTANCE_LOW);
742
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400743 mHelper.createNotificationChannel(PKG_O, UID_O, channel, false, false);
744 assertTrue(mHelper.canShowBadge(PKG_O, UID_O));
745 assertEquals(Notification.PRIORITY_DEFAULT, mHelper.getPackagePriority(PKG_O, UID_O));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400746 assertEquals(NotificationManager.VISIBILITY_NO_OVERRIDE,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400747 mHelper.getPackageVisibility(PKG_O, UID_O));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400748
749 channel.setShowBadge(false);
750 channel.setImportance(IMPORTANCE_NONE);
751 channel.setBypassDnd(true);
752 channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
753
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400754 mHelper.updateNotificationChannel(PKG_O, UID_O, channel, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400755
756 // ensure app level fields are not changed
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400757 assertTrue(mHelper.canShowBadge(PKG_O, UID_O));
758 assertEquals(Notification.PRIORITY_DEFAULT, mHelper.getPackagePriority(PKG_O, UID_O));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400759 assertEquals(NotificationManager.VISIBILITY_NO_OVERRIDE,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400760 mHelper.getPackageVisibility(PKG_O, UID_O));
761 assertEquals(NotificationManager.IMPORTANCE_UNSPECIFIED, mHelper.getImportance(PKG_O,
762 UID_O));
763 assertFalse(mHelper.getIsAppImportanceLocked(PKG_O, UID_O));
764 }
765
766 @Test
767 public void testUpdate_preUpgrade_noUpdateAppFieldsWithMultipleChannels() throws Exception {
768 final NotificationChannel channel = new NotificationChannel("id2", "name2", IMPORTANCE_LOW);
769
770 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, false, false);
771 assertTrue(mHelper.canShowBadge(PKG_N_MR1, UID_N_MR1));
772 assertEquals(Notification.PRIORITY_DEFAULT, mHelper.getPackagePriority(PKG_N_MR1, UID_N_MR1));
773 assertEquals(NotificationManager.VISIBILITY_NO_OVERRIDE,
774 mHelper.getPackageVisibility(PKG_N_MR1, UID_N_MR1));
775
776 channel.setShowBadge(false);
777 channel.setImportance(IMPORTANCE_NONE);
778 channel.setBypassDnd(true);
779 channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
780
781 mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true);
782
783 NotificationChannel defaultChannel = mHelper.getNotificationChannel(
784 PKG_N_MR1, UID_N_MR1, NotificationChannel.DEFAULT_CHANNEL_ID, false);
785
786 defaultChannel.setShowBadge(false);
787 defaultChannel.setImportance(IMPORTANCE_NONE);
788 defaultChannel.setBypassDnd(true);
789 defaultChannel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
790
791 mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, defaultChannel, true);
792
793 // ensure app level fields are not changed
794 assertTrue(mHelper.canShowBadge(PKG_N_MR1, UID_N_MR1));
795 assertEquals(Notification.PRIORITY_DEFAULT, mHelper.getPackagePriority(PKG_N_MR1, UID_N_MR1));
796 assertEquals(NotificationManager.VISIBILITY_NO_OVERRIDE,
797 mHelper.getPackageVisibility(PKG_N_MR1, UID_N_MR1));
798 assertEquals(NotificationManager.IMPORTANCE_UNSPECIFIED, mHelper.getImportance(PKG_N_MR1,
799 UID_N_MR1));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400800 }
801
802 @Test
803 public void testGetNotificationChannel_ReturnsNullForUnknownChannel() throws Exception {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400804 assertEquals(null, mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, "garbage", false));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400805 }
806
807 @Test
808 public void testCreateChannel_CannotChangeHiddenFields() throws Exception {
809 final NotificationChannel channel =
810 new NotificationChannel("id2", "name2", IMPORTANCE_LOW);
811 channel.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes);
812 channel.enableLights(true);
813 channel.setBypassDnd(true);
814 channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
815 channel.setShowBadge(true);
816 int lockMask = 0;
817 for (int i = 0; i < NotificationChannel.LOCKABLE_FIELDS.length; i++) {
818 lockMask |= NotificationChannel.LOCKABLE_FIELDS[i];
819 }
820 channel.lockFields(lockMask);
821
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400822 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400823
824 NotificationChannel savedChannel =
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400825 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId(), false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400826
827 assertEquals(channel.getName(), savedChannel.getName());
828 assertEquals(channel.shouldShowLights(), savedChannel.shouldShowLights());
829 assertFalse(savedChannel.canBypassDnd());
830 assertFalse(Notification.VISIBILITY_SECRET == savedChannel.getLockscreenVisibility());
831 assertEquals(channel.canShowBadge(), savedChannel.canShowBadge());
832
833 verify(mHandler, never()).requestSort();
834 }
835
836 @Test
837 public void testCreateChannel_CannotChangeHiddenFieldsAssistant() throws Exception {
838 final NotificationChannel channel =
839 new NotificationChannel("id2", "name2", IMPORTANCE_LOW);
840 channel.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes);
841 channel.enableLights(true);
842 channel.setBypassDnd(true);
843 channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
844 channel.setShowBadge(true);
845 int lockMask = 0;
846 for (int i = 0; i < NotificationChannel.LOCKABLE_FIELDS.length; i++) {
847 lockMask |= NotificationChannel.LOCKABLE_FIELDS[i];
848 }
849 channel.lockFields(lockMask);
850
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400851 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400852
853 NotificationChannel savedChannel =
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400854 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId(), false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400855
856 assertEquals(channel.getName(), savedChannel.getName());
857 assertEquals(channel.shouldShowLights(), savedChannel.shouldShowLights());
858 assertFalse(savedChannel.canBypassDnd());
859 assertFalse(Notification.VISIBILITY_SECRET == savedChannel.getLockscreenVisibility());
860 assertEquals(channel.canShowBadge(), savedChannel.canShowBadge());
861 }
862
863 @Test
864 public void testClearLockedFields() throws Exception {
865 final NotificationChannel channel = getChannel();
866 mHelper.clearLockedFields(channel);
867 assertEquals(0, channel.getUserLockedFields());
868
869 channel.lockFields(NotificationChannel.USER_LOCKED_PRIORITY
870 | NotificationChannel.USER_LOCKED_IMPORTANCE);
871 mHelper.clearLockedFields(channel);
872 assertEquals(0, channel.getUserLockedFields());
873 }
874
875 @Test
876 public void testLockFields_soundAndVibration() throws Exception {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400877 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, getChannel(), true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400878
879 final NotificationChannel update1 = getChannel();
880 update1.setSound(new Uri.Builder().scheme("test").build(),
881 new AudioAttributes.Builder().build());
882 update1.lockFields(NotificationChannel.USER_LOCKED_PRIORITY);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400883 mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update1, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400884 assertEquals(NotificationChannel.USER_LOCKED_PRIORITY
885 | NotificationChannel.USER_LOCKED_SOUND,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400886 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update1.getId(), false)
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400887 .getUserLockedFields());
888
889 NotificationChannel update2 = getChannel();
890 update2.enableVibration(true);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400891 mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update2, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400892 assertEquals(NotificationChannel.USER_LOCKED_PRIORITY
893 | NotificationChannel.USER_LOCKED_SOUND
894 | NotificationChannel.USER_LOCKED_VIBRATION,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400895 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update2.getId(), false)
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400896 .getUserLockedFields());
897 }
898
899 @Test
900 public void testLockFields_vibrationAndLights() throws Exception {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400901 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, getChannel(), true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400902
903 final NotificationChannel update1 = getChannel();
904 update1.setVibrationPattern(new long[]{7945, 46 ,246});
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400905 mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update1, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400906 assertEquals(NotificationChannel.USER_LOCKED_VIBRATION,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400907 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update1.getId(), false)
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400908 .getUserLockedFields());
909
910 final NotificationChannel update2 = getChannel();
911 update2.enableLights(true);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400912 mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update2, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400913 assertEquals(NotificationChannel.USER_LOCKED_VIBRATION
914 | NotificationChannel.USER_LOCKED_LIGHTS,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400915 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update2.getId(), false)
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400916 .getUserLockedFields());
917 }
918
919 @Test
920 public void testLockFields_lightsAndImportance() throws Exception {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400921 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, getChannel(), true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400922
923 final NotificationChannel update1 = getChannel();
924 update1.setLightColor(Color.GREEN);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400925 mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update1, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400926 assertEquals(NotificationChannel.USER_LOCKED_LIGHTS,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400927 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update1.getId(), false)
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400928 .getUserLockedFields());
929
930 final NotificationChannel update2 = getChannel();
931 update2.setImportance(IMPORTANCE_DEFAULT);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400932 mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update2, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400933 assertEquals(NotificationChannel.USER_LOCKED_LIGHTS
934 | NotificationChannel.USER_LOCKED_IMPORTANCE,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400935 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update2.getId(), false)
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400936 .getUserLockedFields());
937 }
938
939 @Test
940 public void testLockFields_visibilityAndDndAndBadge() throws Exception {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400941 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, getChannel(), true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400942 assertEquals(0,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400943 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, getChannel().getId(), false)
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400944 .getUserLockedFields());
945
946 final NotificationChannel update1 = getChannel();
947 update1.setBypassDnd(true);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400948 mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update1, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400949 assertEquals(NotificationChannel.USER_LOCKED_PRIORITY,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400950 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update1.getId(), false)
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400951 .getUserLockedFields());
952
953 final NotificationChannel update2 = getChannel();
954 update2.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400955 mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update2, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400956 assertEquals(NotificationChannel.USER_LOCKED_PRIORITY
957 | NotificationChannel.USER_LOCKED_VISIBILITY,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400958 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update2.getId(), false)
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400959 .getUserLockedFields());
960
961 final NotificationChannel update3 = getChannel();
962 update3.setShowBadge(false);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400963 mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update3, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400964 assertEquals(NotificationChannel.USER_LOCKED_PRIORITY
965 | NotificationChannel.USER_LOCKED_VISIBILITY
966 | NotificationChannel.USER_LOCKED_SHOW_BADGE,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400967 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update3.getId(), false)
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400968 .getUserLockedFields());
969 }
970
971 @Test
972 public void testDeleteNonExistentChannel() throws Exception {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400973 mHelper.deleteNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, "does not exist");
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400974 }
975
976 @Test
977 public void testGetDeletedChannel() throws Exception {
978 NotificationChannel channel = getChannel();
979 channel.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes);
980 channel.enableLights(true);
981 channel.setBypassDnd(true);
982 channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
983 channel.enableVibration(true);
984 channel.setVibrationPattern(new long[]{100, 67, 145, 156});
985
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400986 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false);
987 mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400988
989 // Does not return deleted channel
990 NotificationChannel response =
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400991 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId(), false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400992 assertNull(response);
993
994 // Returns deleted channel
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400995 response = mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId(), true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400996 compareChannels(channel, response);
997 assertTrue(response.isDeleted());
998 }
999
1000 @Test
1001 public void testGetDeletedChannels() throws Exception {
1002 Map<String, NotificationChannel> channelMap = new HashMap<>();
1003 NotificationChannel channel =
1004 new NotificationChannel("id2", "name2", IMPORTANCE_LOW);
1005 channel.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes);
1006 channel.enableLights(true);
1007 channel.setBypassDnd(true);
1008 channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
1009 channel.enableVibration(true);
1010 channel.setVibrationPattern(new long[]{100, 67, 145, 156});
1011 channelMap.put(channel.getId(), channel);
1012 NotificationChannel channel2 =
1013 new NotificationChannel("id4", "a", NotificationManager.IMPORTANCE_HIGH);
1014 channelMap.put(channel2.getId(), channel2);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001015 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false);
1016 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001017
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001018 mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001019
1020 // Returns only non-deleted channels
1021 List<NotificationChannel> channels =
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001022 mHelper.getNotificationChannels(PKG_N_MR1, UID_N_MR1, false).getList();
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001023 assertEquals(2, channels.size()); // Default channel + non-deleted channel
1024 for (NotificationChannel nc : channels) {
1025 if (!NotificationChannel.DEFAULT_CHANNEL_ID.equals(nc.getId())) {
1026 compareChannels(channel2, nc);
1027 }
1028 }
1029
1030 // Returns deleted channels too
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001031 channels = mHelper.getNotificationChannels(PKG_N_MR1, UID_N_MR1, true).getList();
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001032 assertEquals(3, channels.size()); // Includes default channel
1033 for (NotificationChannel nc : channels) {
1034 if (!NotificationChannel.DEFAULT_CHANNEL_ID.equals(nc.getId())) {
1035 compareChannels(channelMap.get(nc.getId()), nc);
1036 }
1037 }
1038 }
1039
1040 @Test
1041 public void testGetDeletedChannelCount() throws Exception {
1042 NotificationChannel channel =
1043 new NotificationChannel("id2", "name2", IMPORTANCE_LOW);
1044 NotificationChannel channel2 =
1045 new NotificationChannel("id4", "a", NotificationManager.IMPORTANCE_HIGH);
1046 NotificationChannel channel3 =
1047 new NotificationChannel("id5", "a", NotificationManager.IMPORTANCE_HIGH);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001048 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false);
1049 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, true, false);
1050 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001051
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001052 mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId());
1053 mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3.getId());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001054
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001055 assertEquals(2, mHelper.getDeletedChannelCount(PKG_N_MR1, UID_N_MR1));
1056 assertEquals(0, mHelper.getDeletedChannelCount("pkg2", UID_O));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001057 }
1058
1059 @Test
1060 public void testGetBlockedChannelCount() throws Exception {
1061 NotificationChannel channel =
1062 new NotificationChannel("id2", "name2", IMPORTANCE_LOW);
1063 NotificationChannel channel2 =
1064 new NotificationChannel("id4", "a", NotificationManager.IMPORTANCE_NONE);
1065 NotificationChannel channel3 =
1066 new NotificationChannel("id5", "a", NotificationManager.IMPORTANCE_NONE);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001067 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false);
1068 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, true, false);
1069 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001070
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001071 mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3.getId());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001072
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001073 assertEquals(1, mHelper.getBlockedChannelCount(PKG_N_MR1, UID_N_MR1));
1074 assertEquals(0, mHelper.getBlockedChannelCount("pkg2", UID_O));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001075 }
1076
1077 @Test
1078 public void testCreateAndDeleteCanChannelsBypassDnd() throws Exception {
1079 // create notification channel that can't bypass dnd
1080 // expected result: areChannelsBypassingDnd = false
1081 // setNotificationPolicy isn't called since areChannelsBypassingDnd was already false
1082 NotificationChannel channel = new NotificationChannel("id1", "name1", IMPORTANCE_LOW);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001083 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001084 assertFalse(mHelper.areChannelsBypassingDnd());
1085 verify(mMockZenModeHelper, never()).setNotificationPolicy(any());
1086 resetZenModeHelper();
1087
1088 // create notification channel that can bypass dnd
1089 // expected result: areChannelsBypassingDnd = true
1090 NotificationChannel channel2 = new NotificationChannel("id2", "name2", IMPORTANCE_LOW);
1091 channel2.setBypassDnd(true);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001092 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, true, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001093 assertTrue(mHelper.areChannelsBypassingDnd());
1094 verify(mMockZenModeHelper, times(1)).setNotificationPolicy(any());
1095 resetZenModeHelper();
1096
1097 // delete channels
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001098 mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001099 assertTrue(mHelper.areChannelsBypassingDnd()); // channel2 can still bypass DND
1100 verify(mMockZenModeHelper, never()).setNotificationPolicy(any());
1101 resetZenModeHelper();
1102
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001103 mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2.getId());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001104 assertFalse(mHelper.areChannelsBypassingDnd());
1105 verify(mMockZenModeHelper, times(1)).setNotificationPolicy(any());
1106 resetZenModeHelper();
1107 }
1108
1109 @Test
1110 public void testUpdateCanChannelsBypassDnd() throws Exception {
1111 // create notification channel that can't bypass dnd
1112 // expected result: areChannelsBypassingDnd = false
1113 // setNotificationPolicy isn't called since areChannelsBypassingDnd was already false
1114 NotificationChannel channel = new NotificationChannel("id1", "name1", IMPORTANCE_LOW);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001115 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001116 assertFalse(mHelper.areChannelsBypassingDnd());
1117 verify(mMockZenModeHelper, never()).setNotificationPolicy(any());
1118 resetZenModeHelper();
1119
1120 // update channel so it CAN bypass dnd:
1121 // expected result: areChannelsBypassingDnd = true
1122 channel.setBypassDnd(true);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001123 mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001124 assertTrue(mHelper.areChannelsBypassingDnd());
1125 verify(mMockZenModeHelper, times(1)).setNotificationPolicy(any());
1126 resetZenModeHelper();
1127
1128 // update channel so it can't bypass dnd:
1129 // expected result: areChannelsBypassingDnd = false
1130 channel.setBypassDnd(false);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001131 mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001132 assertFalse(mHelper.areChannelsBypassingDnd());
1133 verify(mMockZenModeHelper, times(1)).setNotificationPolicy(any());
1134 resetZenModeHelper();
1135 }
1136
1137 @Test
1138 public void testSetupNewZenModeHelper_canBypass() {
1139 // start notification policy off with mAreChannelsBypassingDnd = true, but
1140 // RankingHelper should change to false
1141 mTestNotificationPolicy = new NotificationManager.Policy(0, 0, 0, 0,
1142 NotificationManager.Policy.STATE_CHANNELS_BYPASSING_DND);
1143 when(mMockZenModeHelper.getNotificationPolicy()).thenReturn(mTestNotificationPolicy);
1144 mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper);
1145 assertFalse(mHelper.areChannelsBypassingDnd());
1146 verify(mMockZenModeHelper, times(1)).setNotificationPolicy(any());
1147 resetZenModeHelper();
1148 }
1149
1150 @Test
1151 public void testSetupNewZenModeHelper_cannotBypass() {
1152 // start notification policy off with mAreChannelsBypassingDnd = false
1153 mTestNotificationPolicy = new NotificationManager.Policy(0, 0, 0, 0, 0);
1154 when(mMockZenModeHelper.getNotificationPolicy()).thenReturn(mTestNotificationPolicy);
1155 mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper);
1156 assertFalse(mHelper.areChannelsBypassingDnd());
1157 verify(mMockZenModeHelper, never()).setNotificationPolicy(any());
1158 resetZenModeHelper();
1159 }
1160
1161 @Test
1162 public void testCreateDeletedChannel() throws Exception {
1163 long[] vibration = new long[]{100, 67, 145, 156};
1164 NotificationChannel channel =
1165 new NotificationChannel("id2", "name2", IMPORTANCE_LOW);
1166 channel.setVibrationPattern(vibration);
1167
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001168 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false);
1169 mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001170
1171 NotificationChannel newChannel = new NotificationChannel(
1172 channel.getId(), channel.getName(), NotificationManager.IMPORTANCE_HIGH);
1173 newChannel.setVibrationPattern(new long[]{100});
1174
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001175 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, newChannel, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001176
1177 // No long deleted, using old settings
1178 compareChannels(channel,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001179 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, newChannel.getId(), false));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001180 }
1181
1182 @Test
1183 public void testOnlyHasDefaultChannel() throws Exception {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001184 assertTrue(mHelper.onlyHasDefaultChannel(PKG_N_MR1, UID_N_MR1));
1185 assertFalse(mHelper.onlyHasDefaultChannel(PKG_O, UID_O));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001186
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001187 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, getChannel(), true, false);
1188 assertFalse(mHelper.onlyHasDefaultChannel(PKG_N_MR1, UID_N_MR1));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001189 }
1190
1191 @Test
1192 public void testCreateChannel_defaultChannelId() throws Exception {
1193 try {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001194 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, new NotificationChannel(
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001195 NotificationChannel.DEFAULT_CHANNEL_ID, "ha", IMPORTANCE_HIGH), true, false);
1196 fail("Allowed to create default channel");
1197 } catch (IllegalArgumentException e) {
1198 // pass
1199 }
1200 }
1201
1202 @Test
1203 public void testCreateChannel_alreadyExists() throws Exception {
1204 long[] vibration = new long[]{100, 67, 145, 156};
1205 NotificationChannel channel =
1206 new NotificationChannel("id2", "name2", IMPORTANCE_LOW);
1207 channel.setVibrationPattern(vibration);
1208
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001209 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001210
1211 NotificationChannel newChannel = new NotificationChannel(
1212 channel.getId(), channel.getName(), NotificationManager.IMPORTANCE_HIGH);
1213 newChannel.setVibrationPattern(new long[]{100});
1214
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001215 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, newChannel, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001216
1217 // Old settings not overridden
1218 compareChannels(channel,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001219 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, newChannel.getId(), false));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001220 }
1221
1222 @Test
1223 public void testCreateChannel_noOverrideSound() throws Exception {
1224 Uri sound = new Uri.Builder().scheme("test").build();
1225 final NotificationChannel channel = new NotificationChannel("id2", "name2",
1226 NotificationManager.IMPORTANCE_DEFAULT);
1227 channel.setSound(sound, mAudioAttributes);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001228 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001229 assertEquals(sound, mHelper.getNotificationChannel(
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001230 PKG_N_MR1, UID_N_MR1, channel.getId(), false).getSound());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001231 }
1232
1233 @Test
1234 public void testPermanentlyDeleteChannels() throws Exception {
1235 NotificationChannel channel1 =
1236 new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH);
1237 NotificationChannel channel2 =
1238 new NotificationChannel("id2", "name2", IMPORTANCE_LOW);
1239
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001240 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false);
1241 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, false, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001242
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001243 mHelper.permanentlyDeleteNotificationChannels(PKG_N_MR1, UID_N_MR1);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001244
1245 // Only default channel remains
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001246 assertEquals(1, mHelper.getNotificationChannels(PKG_N_MR1, UID_N_MR1, true).getList().size());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001247 }
1248
1249 @Test
1250 public void testDeleteGroup() throws Exception {
1251 NotificationChannelGroup notDeleted = new NotificationChannelGroup("not", "deleted");
1252 NotificationChannelGroup deleted = new NotificationChannelGroup("totally", "deleted");
1253 NotificationChannel nonGroupedNonDeletedChannel =
1254 new NotificationChannel("no group", "so not deleted", IMPORTANCE_HIGH);
1255 NotificationChannel groupedButNotDeleted =
1256 new NotificationChannel("not deleted", "belongs to notDeleted", IMPORTANCE_DEFAULT);
1257 groupedButNotDeleted.setGroup("not");
1258 NotificationChannel groupedAndDeleted =
1259 new NotificationChannel("deleted", "belongs to deleted", IMPORTANCE_DEFAULT);
1260 groupedAndDeleted.setGroup("totally");
1261
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001262 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, notDeleted, true);
1263 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, deleted, true);
1264 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, nonGroupedNonDeletedChannel, true, false);
1265 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, groupedAndDeleted, true, false);
1266 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, groupedButNotDeleted, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001267
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001268 mHelper.deleteNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, deleted.getId());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001269
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001270 assertNull(mHelper.getNotificationChannelGroup(deleted.getId(), PKG_N_MR1, UID_N_MR1));
1271 assertNotNull(mHelper.getNotificationChannelGroup(notDeleted.getId(), PKG_N_MR1, UID_N_MR1));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001272
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001273 assertNull(mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, groupedAndDeleted.getId(), false));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001274 compareChannels(groupedAndDeleted,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001275 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, groupedAndDeleted.getId(), true));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001276
1277 compareChannels(groupedButNotDeleted,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001278 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, groupedButNotDeleted.getId(), false));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001279 compareChannels(nonGroupedNonDeletedChannel, mHelper.getNotificationChannel(
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001280 PKG_N_MR1, UID_N_MR1, nonGroupedNonDeletedChannel.getId(), false));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001281
1282 // notDeleted
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001283 assertEquals(1, mHelper.getNotificationChannelGroups(PKG_N_MR1, UID_N_MR1).size());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001284
1285 verify(mHandler, never()).requestSort();
1286 }
1287
1288 @Test
1289 public void testOnUserRemoved() throws Exception {
1290 int[] user0Uids = {98, 235, 16, 3782};
1291 int[] user1Uids = new int[user0Uids.length];
1292 for (int i = 0; i < user0Uids.length; i++) {
1293 user1Uids[i] = UserHandle.PER_USER_RANGE + user0Uids[i];
1294
1295 final ApplicationInfo legacy = new ApplicationInfo();
1296 legacy.targetSdkVersion = Build.VERSION_CODES.N_MR1;
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001297 when(mPm.getApplicationInfoAsUser(eq(PKG_N_MR1), anyInt(), anyInt())).thenReturn(legacy);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001298
1299 // create records with the default channel for all user 0 and user 1 uids
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001300 mHelper.getImportance(PKG_N_MR1, user0Uids[i]);
1301 mHelper.getImportance(PKG_N_MR1, user1Uids[i]);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001302 }
1303
1304 mHelper.onUserRemoved(1);
1305
1306 // user 0 records remain
1307 for (int i = 0; i < user0Uids.length; i++) {
1308 assertEquals(1,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001309 mHelper.getNotificationChannels(PKG_N_MR1, user0Uids[i], false).getList().size());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001310 }
1311 // user 1 records are gone
1312 for (int i = 0; i < user1Uids.length; i++) {
1313 assertEquals(0,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001314 mHelper.getNotificationChannels(PKG_N_MR1, user1Uids[i], false).getList().size());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001315 }
1316 }
1317
1318 @Test
1319 public void testOnPackageChanged_packageRemoval() throws Exception {
1320 // Deleted
1321 NotificationChannel channel1 =
1322 new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001323 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001324
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001325 mHelper.onPackagesChanged(true, UserHandle.USER_SYSTEM, new String[]{PKG_N_MR1}, new int[]{
1326 UID_N_MR1});
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001327
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001328 assertEquals(0, mHelper.getNotificationChannels(PKG_N_MR1, UID_N_MR1, true).getList().size());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001329
1330 // Not deleted
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001331 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001332
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001333 mHelper.onPackagesChanged(false, UserHandle.USER_SYSTEM, new String[]{PKG_N_MR1}, new int[]{
1334 UID_N_MR1});
1335 assertEquals(2, mHelper.getNotificationChannels(PKG_N_MR1, UID_N_MR1, false).getList().size());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001336 }
1337
1338 @Test
1339 public void testOnPackageChanged_packageRemoval_importance() throws Exception {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001340 mHelper.setImportance(PKG_N_MR1, UID_N_MR1, NotificationManager.IMPORTANCE_HIGH);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001341
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001342 mHelper.onPackagesChanged(true, UserHandle.USER_SYSTEM, new String[]{PKG_N_MR1}, new int[]{
1343 UID_N_MR1});
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001344
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001345 assertEquals(NotificationManager.IMPORTANCE_UNSPECIFIED, mHelper.getImportance(PKG_N_MR1,
1346 UID_N_MR1));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001347 }
1348
1349 @Test
1350 public void testOnPackageChanged_packageRemoval_groups() throws Exception {
1351 NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1");
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001352 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001353 NotificationChannelGroup ncg2 = new NotificationChannelGroup("group2", "name2");
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001354 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg2, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001355
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001356 mHelper.onPackagesChanged(true, UserHandle.USER_SYSTEM, new String[]{PKG_N_MR1}, new int[]{
1357 UID_N_MR1});
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001358
1359 assertEquals(0,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001360 mHelper.getNotificationChannelGroups(PKG_N_MR1, UID_N_MR1, true, true).getList().size());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001361 }
1362
1363 @Test
1364 public void testOnPackageChange_downgradeTargetSdk() throws Exception {
1365 // create channel as api 26
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001366 mHelper.createNotificationChannel(PKG_O, UID_O, getChannel(), true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001367
1368 // install new app version targeting 25
1369 final ApplicationInfo legacy = new ApplicationInfo();
1370 legacy.targetSdkVersion = Build.VERSION_CODES.N_MR1;
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001371 when(mPm.getApplicationInfoAsUser(eq(PKG_O), anyInt(), anyInt())).thenReturn(legacy);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001372 mHelper.onPackagesChanged(
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001373 false, UserHandle.USER_SYSTEM, new String[]{PKG_O}, new int[]{UID_O});
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001374
1375 // make sure the default channel was readded
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001376 //assertEquals(2, mHelper.getNotificationChannels(PKG_O, UID_O, false).getList().size());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001377 assertNotNull(mHelper.getNotificationChannel(
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001378 PKG_O, UID_O, NotificationChannel.DEFAULT_CHANNEL_ID, false));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001379 }
1380
1381 @Test
1382 public void testRecordDefaults() throws Exception {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001383 assertEquals(NotificationManager.IMPORTANCE_UNSPECIFIED, mHelper.getImportance(PKG_N_MR1,
1384 UID_N_MR1));
1385 assertEquals(true, mHelper.canShowBadge(PKG_N_MR1, UID_N_MR1));
1386 assertEquals(1, mHelper.getNotificationChannels(PKG_N_MR1, UID_N_MR1, false).getList().size());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001387 }
1388
1389 @Test
1390 public void testCreateGroup() throws Exception {
1391 NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1");
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001392 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg, true);
1393 assertEquals(ncg, mHelper.getNotificationChannelGroups(PKG_N_MR1, UID_N_MR1).iterator().next());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001394 verify(mHandler, never()).requestSort();
1395 }
1396
1397 @Test
1398 public void testCannotCreateChannel_badGroup() throws Exception {
1399 NotificationChannel channel1 =
1400 new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH);
1401 channel1.setGroup("garbage");
1402 try {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001403 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001404 fail("Created a channel with a bad group");
1405 } catch (IllegalArgumentException e) {
1406 }
1407 }
1408
1409 @Test
1410 public void testCannotCreateChannel_goodGroup() throws Exception {
1411 NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1");
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001412 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001413 NotificationChannel channel1 =
1414 new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH);
1415 channel1.setGroup(ncg.getId());
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001416 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001417
1418 assertEquals(ncg.getId(),
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001419 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1.getId(), false).getGroup());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001420 }
1421
1422 @Test
1423 public void testGetChannelGroups() throws Exception {
1424 NotificationChannelGroup unused = new NotificationChannelGroup("unused", "s");
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001425 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, unused, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001426 NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1");
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001427 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001428 NotificationChannelGroup ncg2 = new NotificationChannelGroup("group2", "name2");
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001429 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg2, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001430
1431 NotificationChannel channel1 =
1432 new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH);
1433 channel1.setGroup(ncg.getId());
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001434 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001435 NotificationChannel channel1a =
1436 new NotificationChannel("id1a", "name1", NotificationManager.IMPORTANCE_HIGH);
1437 channel1a.setGroup(ncg.getId());
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001438 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1a, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001439
1440 NotificationChannel channel2 =
1441 new NotificationChannel("id2", "name1", NotificationManager.IMPORTANCE_HIGH);
1442 channel2.setGroup(ncg2.getId());
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001443 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001444
1445 NotificationChannel channel3 =
1446 new NotificationChannel("id3", "name1", NotificationManager.IMPORTANCE_HIGH);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001447 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001448
1449 List<NotificationChannelGroup> actual =
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001450 mHelper.getNotificationChannelGroups(PKG_N_MR1, UID_N_MR1, true, true).getList();
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001451 assertEquals(3, actual.size());
1452 for (NotificationChannelGroup group : actual) {
1453 if (group.getId() == null) {
1454 assertEquals(2, group.getChannels().size()); // misc channel too
1455 assertTrue(channel3.getId().equals(group.getChannels().get(0).getId())
1456 || channel3.getId().equals(group.getChannels().get(1).getId()));
1457 } else if (group.getId().equals(ncg.getId())) {
1458 assertEquals(2, group.getChannels().size());
1459 if (group.getChannels().get(0).getId().equals(channel1.getId())) {
1460 assertTrue(group.getChannels().get(1).getId().equals(channel1a.getId()));
1461 } else if (group.getChannels().get(0).getId().equals(channel1a.getId())) {
1462 assertTrue(group.getChannels().get(1).getId().equals(channel1.getId()));
1463 } else {
1464 fail("expected channel not found");
1465 }
1466 } else if (group.getId().equals(ncg2.getId())) {
1467 assertEquals(1, group.getChannels().size());
1468 assertEquals(channel2.getId(), group.getChannels().get(0).getId());
1469 }
1470 }
1471 }
1472
1473 @Test
1474 public void testGetChannelGroups_noSideEffects() throws Exception {
1475 NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1");
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001476 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001477
1478 NotificationChannel channel1 =
1479 new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH);
1480 channel1.setGroup(ncg.getId());
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001481 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false);
1482 mHelper.getNotificationChannelGroups(PKG_N_MR1, UID_N_MR1, true, true).getList();
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001483
1484 channel1.setImportance(IMPORTANCE_LOW);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001485 mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001486
1487 List<NotificationChannelGroup> actual =
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001488 mHelper.getNotificationChannelGroups(PKG_N_MR1, UID_N_MR1, true, true).getList();
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001489
1490 assertEquals(2, actual.size());
1491 for (NotificationChannelGroup group : actual) {
1492 if (Objects.equals(group.getId(), ncg.getId())) {
1493 assertEquals(1, group.getChannels().size());
1494 }
1495 }
1496 }
1497
1498 @Test
1499 public void testCreateChannel_updateName() throws Exception {
1500 NotificationChannel nc = new NotificationChannel("id", "hello", IMPORTANCE_DEFAULT);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001501 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, nc, true, false);
1502 NotificationChannel actual = mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, "id", false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001503 assertEquals("hello", actual.getName());
1504
1505 nc = new NotificationChannel("id", "goodbye", IMPORTANCE_HIGH);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001506 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, nc, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001507
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001508 actual = mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, "id", false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001509 assertEquals("goodbye", actual.getName());
1510 assertEquals(IMPORTANCE_DEFAULT, actual.getImportance());
1511
1512 verify(mHandler, times(1)).requestSort();
1513 }
1514
1515 @Test
1516 public void testCreateChannel_addToGroup() throws Exception {
1517 NotificationChannelGroup group = new NotificationChannelGroup("group", "");
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001518 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, group, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001519 NotificationChannel nc = new NotificationChannel("id", "hello", IMPORTANCE_DEFAULT);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001520 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, nc, true, false);
1521 NotificationChannel actual = mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, "id", false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001522 assertNull(actual.getGroup());
1523
1524 nc = new NotificationChannel("id", "hello", IMPORTANCE_HIGH);
1525 nc.setGroup(group.getId());
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001526 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, nc, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001527
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001528 actual = mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, "id", false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001529 assertNotNull(actual.getGroup());
1530 assertEquals(IMPORTANCE_DEFAULT, actual.getImportance());
1531
1532 verify(mHandler, times(1)).requestSort();
1533 }
1534
1535 @Test
1536 public void testDumpChannelsJson() throws Exception {
1537 final ApplicationInfo upgrade = new ApplicationInfo();
1538 upgrade.targetSdkVersion = Build.VERSION_CODES.O;
1539 try {
1540 when(mPm.getApplicationInfoAsUser(
1541 anyString(), anyInt(), anyInt())).thenReturn(upgrade);
1542 } catch (PackageManager.NameNotFoundException e) {
1543 }
1544 ArrayMap<String, Integer> expectedChannels = new ArrayMap<>();
1545 int numPackages = ThreadLocalRandom.current().nextInt(1, 5);
1546 for (int i = 0; i < numPackages; i++) {
1547 String pkgName = "pkg" + i;
1548 int numChannels = ThreadLocalRandom.current().nextInt(1, 10);
1549 for (int j = 0; j < numChannels; j++) {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001550 mHelper.createNotificationChannel(pkgName, UID_N_MR1,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001551 new NotificationChannel("" + j, "a", IMPORTANCE_HIGH), true, false);
1552 }
1553 expectedChannels.put(pkgName, numChannels);
1554 }
1555
1556 // delete the first channel of the first package
1557 String pkg = expectedChannels.keyAt(0);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001558 mHelper.deleteNotificationChannel("pkg" + 0, UID_N_MR1, "0");
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001559 // dump should not include deleted channels
1560 int count = expectedChannels.get(pkg);
1561 expectedChannels.put(pkg, count - 1);
1562
1563 JSONArray actual = mHelper.dumpChannelsJson(new NotificationManagerService.DumpFilter());
1564 assertEquals(numPackages, actual.length());
1565 for (int i = 0; i < numPackages; i++) {
1566 JSONObject object = actual.getJSONObject(i);
1567 assertTrue(expectedChannels.containsKey(object.get("packageName")));
1568 assertEquals(expectedChannels.get(object.get("packageName")).intValue(),
1569 object.getInt("channelCount"));
1570 }
1571 }
1572
1573 @Test
1574 public void testBadgingOverrideTrue() throws Exception {
1575 Secure.putIntForUser(getContext().getContentResolver(),
1576 Secure.NOTIFICATION_BADGING, 1,
1577 USER.getIdentifier());
1578 mHelper.updateBadgingEnabled(); // would be called by settings observer
1579 assertTrue(mHelper.badgingEnabled(USER));
1580 }
1581
1582 @Test
1583 public void testBadgingOverrideFalse() throws Exception {
1584 Secure.putIntForUser(getContext().getContentResolver(),
1585 Secure.NOTIFICATION_BADGING, 0,
1586 USER.getIdentifier());
1587 mHelper.updateBadgingEnabled(); // would be called by settings observer
1588 assertFalse(mHelper.badgingEnabled(USER));
1589 }
1590
1591 @Test
1592 public void testBadgingForUserAll() throws Exception {
1593 try {
1594 mHelper.badgingEnabled(UserHandle.ALL);
1595 } catch (Exception e) {
1596 fail("just don't throw");
1597 }
1598 }
1599
1600 @Test
1601 public void testBadgingOverrideUserIsolation() throws Exception {
1602 Secure.putIntForUser(getContext().getContentResolver(),
1603 Secure.NOTIFICATION_BADGING, 0,
1604 USER.getIdentifier());
1605 Secure.putIntForUser(getContext().getContentResolver(),
1606 Secure.NOTIFICATION_BADGING, 1,
1607 USER2.getIdentifier());
1608 mHelper.updateBadgingEnabled(); // would be called by settings observer
1609 assertFalse(mHelper.badgingEnabled(USER));
1610 assertTrue(mHelper.badgingEnabled(USER2));
1611 }
1612
1613 @Test
1614 public void testOnLocaleChanged_updatesDefaultChannels() throws Exception {
1615 String newLabel = "bananas!";
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001616 final NotificationChannel defaultChannel = mHelper.getNotificationChannel(PKG_N_MR1,
1617 UID_N_MR1,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001618 NotificationChannel.DEFAULT_CHANNEL_ID, false);
1619 assertFalse(newLabel.equals(defaultChannel.getName()));
1620
1621 Resources res = mock(Resources.class);
1622 when(mContext.getResources()).thenReturn(res);
1623 when(res.getString(com.android.internal.R.string.default_notification_channel_label))
1624 .thenReturn(newLabel);
1625
1626 mHelper.onLocaleChanged(mContext, USER.getIdentifier());
1627
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001628 assertEquals(newLabel, mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001629 NotificationChannel.DEFAULT_CHANNEL_ID, false).getName());
1630 }
1631
1632 @Test
1633 public void testIsGroupBlocked_noGroup() throws Exception {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001634 assertFalse(mHelper.isGroupBlocked(PKG_N_MR1, UID_N_MR1, null));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001635
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001636 assertFalse(mHelper.isGroupBlocked(PKG_N_MR1, UID_N_MR1, "non existent group"));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001637 }
1638
1639 @Test
1640 public void testIsGroupBlocked_notBlocked() throws Exception {
1641 NotificationChannelGroup group = new NotificationChannelGroup("id", "name");
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001642 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, group, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001643
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001644 assertFalse(mHelper.isGroupBlocked(PKG_N_MR1, UID_N_MR1, group.getId()));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001645 }
1646
1647 @Test
1648 public void testIsGroupBlocked_blocked() throws Exception {
1649 NotificationChannelGroup group = new NotificationChannelGroup("id", "name");
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001650 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, group, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001651 group.setBlocked(true);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001652 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, group, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001653
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001654 assertTrue(mHelper.isGroupBlocked(PKG_N_MR1, UID_N_MR1, group.getId()));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001655 }
1656
1657 @Test
1658 public void testIsGroup_appCannotResetBlock() throws Exception {
1659 NotificationChannelGroup group = new NotificationChannelGroup("id", "name");
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001660 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, group, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001661 NotificationChannelGroup group2 = group.clone();
1662 group2.setBlocked(true);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001663 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, group2, false);
1664 assertTrue(mHelper.isGroupBlocked(PKG_N_MR1, UID_N_MR1, group.getId()));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001665
1666 NotificationChannelGroup group3 = group.clone();
1667 group3.setBlocked(false);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001668 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, group3, true);
1669 assertTrue(mHelper.isGroupBlocked(PKG_N_MR1, UID_N_MR1, group.getId()));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001670 }
1671
1672 @Test
1673 public void testGetNotificationChannelGroupWithChannels() throws Exception {
1674 NotificationChannelGroup group = new NotificationChannelGroup("group", "");
1675 NotificationChannelGroup other = new NotificationChannelGroup("something else", "");
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001676 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, group, true);
1677 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, other, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001678
1679 NotificationChannel a = new NotificationChannel("a", "a", IMPORTANCE_DEFAULT);
1680 a.setGroup(group.getId());
1681 NotificationChannel b = new NotificationChannel("b", "b", IMPORTANCE_DEFAULT);
1682 b.setGroup(other.getId());
1683 NotificationChannel c = new NotificationChannel("c", "c", IMPORTANCE_DEFAULT);
1684 c.setGroup(group.getId());
1685 NotificationChannel d = new NotificationChannel("d", "d", IMPORTANCE_DEFAULT);
1686
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001687 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, a, true, false);
1688 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, b, true, false);
1689 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, c, true, false);
1690 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, d, true, false);
1691 mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, c.getId());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001692
1693 NotificationChannelGroup retrieved = mHelper.getNotificationChannelGroupWithChannels(
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001694 PKG_N_MR1, UID_N_MR1, group.getId(), true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001695 assertEquals(2, retrieved.getChannels().size());
1696 compareChannels(a, findChannel(retrieved.getChannels(), a.getId()));
1697 compareChannels(c, findChannel(retrieved.getChannels(), c.getId()));
1698
1699 retrieved = mHelper.getNotificationChannelGroupWithChannels(
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001700 PKG_N_MR1, UID_N_MR1, group.getId(), false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001701 assertEquals(1, retrieved.getChannels().size());
1702 compareChannels(a, findChannel(retrieved.getChannels(), a.getId()));
1703 }
1704
1705 @Test
1706 public void testAndroidPkgCannotBypassDnd_creation() {
1707 NotificationChannel test = new NotificationChannel("A", "a", IMPORTANCE_LOW);
1708 test.setBypassDnd(true);
1709
1710 mHelper.createNotificationChannel(SYSTEM_PKG, SYSTEM_UID, test, true, false);
1711
1712 assertFalse(mHelper.getNotificationChannel(SYSTEM_PKG, SYSTEM_UID, "A", false)
1713 .canBypassDnd());
1714 }
1715
1716 @Test
1717 public void testDndPkgCanBypassDnd_creation() {
1718 NotificationChannel test = new NotificationChannel("A", "a", IMPORTANCE_LOW);
1719 test.setBypassDnd(true);
1720
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001721 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, test, true, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001722
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001723 assertTrue(mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, "A", false).canBypassDnd());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001724 }
1725
1726 @Test
1727 public void testNormalPkgCannotBypassDnd_creation() {
1728 NotificationChannel test = new NotificationChannel("A", "a", IMPORTANCE_LOW);
1729 test.setBypassDnd(true);
1730
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001731 mHelper.createNotificationChannel(PKG_N_MR1, 1000, test, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001732
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001733 assertFalse(mHelper.getNotificationChannel(PKG_N_MR1, 1000, "A", false).canBypassDnd());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001734 }
1735
1736 @Test
1737 public void testAndroidPkgCannotBypassDnd_update() throws Exception {
1738 NotificationChannel test = new NotificationChannel("A", "a", IMPORTANCE_LOW);
1739 mHelper.createNotificationChannel(SYSTEM_PKG, SYSTEM_UID, test, true, false);
1740
1741 NotificationChannel update = new NotificationChannel("A", "a", IMPORTANCE_LOW);
1742 update.setBypassDnd(true);
1743 mHelper.createNotificationChannel(SYSTEM_PKG, SYSTEM_UID, update, true, false);
1744
1745 assertFalse(mHelper.getNotificationChannel(SYSTEM_PKG, SYSTEM_UID, "A", false)
1746 .canBypassDnd());
1747 }
1748
1749 @Test
1750 public void testDndPkgCanBypassDnd_update() throws Exception {
1751 NotificationChannel test = new NotificationChannel("A", "a", IMPORTANCE_LOW);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001752 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, test, true, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001753
1754 NotificationChannel update = new NotificationChannel("A", "a", IMPORTANCE_LOW);
1755 update.setBypassDnd(true);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001756 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, update, true, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001757
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001758 assertTrue(mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, "A", false).canBypassDnd());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001759 }
1760
1761 @Test
1762 public void testNormalPkgCannotBypassDnd_update() {
1763 NotificationChannel test = new NotificationChannel("A", "a", IMPORTANCE_LOW);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001764 mHelper.createNotificationChannel(PKG_N_MR1, 1000, test, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001765 NotificationChannel update = new NotificationChannel("A", "a", IMPORTANCE_LOW);
1766 update.setBypassDnd(true);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001767 mHelper.createNotificationChannel(PKG_N_MR1, 1000, update, true, false);
1768 assertFalse(mHelper.getNotificationChannel(PKG_N_MR1, 1000, "A", false).canBypassDnd());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001769 }
1770
1771 @Test
1772 public void testGetBlockedAppCount_noApps() {
1773 assertEquals(0, mHelper.getBlockedAppCount(0));
1774 }
1775
1776 @Test
1777 public void testGetBlockedAppCount_noAppsForUserId() {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001778 mHelper.setEnabled(PKG_N_MR1, 100, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001779 assertEquals(0, mHelper.getBlockedAppCount(9));
1780 }
1781
1782 @Test
1783 public void testGetBlockedAppCount_appsForUserId() {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001784 mHelper.setEnabled(PKG_N_MR1, 1020, false);
1785 mHelper.setEnabled(PKG_N_MR1, 1030, false);
1786 mHelper.setEnabled(PKG_N_MR1, 1060, false);
1787 mHelper.setEnabled(PKG_N_MR1, 1000, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001788 assertEquals(3, mHelper.getBlockedAppCount(0));
1789 }
1790}