blob: 39e47ecb1b22d63d780dae5b76fd28ae244e6eee [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;
30import static org.junit.Assert.assertNotNull;
31import static org.junit.Assert.assertTrue;
32import static org.mockito.ArgumentMatchers.any;
33import static org.mockito.Matchers.anyInt;
34import static org.mockito.Matchers.anyString;
35import static org.mockito.Matchers.eq;
36import static org.mockito.Mockito.mock;
37import static org.mockito.Mockito.never;
38import static org.mockito.Mockito.reset;
39import static org.mockito.Mockito.times;
40import static org.mockito.Mockito.verify;
41import static org.mockito.Mockito.when;
42
43import android.app.Notification;
44import android.app.NotificationChannel;
45import android.app.NotificationChannelGroup;
46import android.app.NotificationManager;
47import android.content.ContentProvider;
48import android.content.Context;
49import android.content.IContentProvider;
50import android.content.pm.ApplicationInfo;
51import android.content.pm.PackageInfo;
52import android.content.pm.PackageManager;
53import android.content.pm.Signature;
54import android.content.res.Resources;
55import android.graphics.Color;
56import android.media.AudioAttributes;
57import android.net.Uri;
58import android.os.Build;
59import android.os.UserHandle;
60import android.provider.Settings;
61import android.provider.Settings.Secure;
Aaron Heuckrothe5bec152018-07-09 16:26:09 -040062import android.test.suitebuilder.annotation.SmallTest;
63import android.testing.TestableContentResolver;
64import android.util.ArrayMap;
65import android.util.Xml;
66
Brett Chabot84151d92019-02-27 15:37:59 -080067import androidx.test.InstrumentationRegistry;
68import androidx.test.runner.AndroidJUnit4;
69
Aaron Heuckrothe5bec152018-07-09 16:26:09 -040070import 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";
Beverly0479cde22018-11-09 11:05:34 -0500101 private static final int SYSTEM_UID = 1000;
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400102 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);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400126
127 final ApplicationInfo legacy = new ApplicationInfo();
128 legacy.targetSdkVersion = Build.VERSION_CODES.N_MR1;
129 final ApplicationInfo upgrade = new ApplicationInfo();
130 upgrade.targetSdkVersion = Build.VERSION_CODES.O;
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400131 when(mPm.getApplicationInfoAsUser(eq(PKG_N_MR1), anyInt(), anyInt())).thenReturn(legacy);
132 when(mPm.getApplicationInfoAsUser(eq(PKG_O), anyInt(), anyInt())).thenReturn(upgrade);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400133 when(mPm.getApplicationInfoAsUser(eq(SYSTEM_PKG), anyInt(), anyInt())).thenReturn(upgrade);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400134 when(mPm.getPackageUidAsUser(eq(PKG_N_MR1), anyInt())).thenReturn(UID_N_MR1);
135 when(mPm.getPackageUidAsUser(eq(PKG_O), anyInt())).thenReturn(UID_O);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400136 when(mPm.getPackageUidAsUser(eq(SYSTEM_PKG), anyInt())).thenReturn(SYSTEM_UID);
137 PackageInfo info = mock(PackageInfo.class);
138 info.signatures = new Signature[] {mock(Signature.class)};
139 when(mPm.getPackageInfoAsUser(eq(SYSTEM_PKG), anyInt(), anyInt())).thenReturn(info);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400140 when(mPm.getPackageInfoAsUser(eq(PKG_N_MR1), anyInt(), anyInt()))
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400141 .thenReturn(mock(PackageInfo.class));
142 when(mContext.getResources()).thenReturn(
143 InstrumentationRegistry.getContext().getResources());
144 when(mContext.getContentResolver()).thenReturn(
145 InstrumentationRegistry.getContext().getContentResolver());
146 when(mContext.getPackageManager()).thenReturn(mPm);
147 when(mContext.getApplicationInfo()).thenReturn(legacy);
148 // most tests assume badging is enabled
149 TestableContentResolver contentResolver = getContext().getContentResolver();
150 contentResolver.setFallbackToExisting(false);
151 Secure.putIntForUser(contentResolver,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400152 Secure.NOTIFICATION_BADGING, 1, UserHandle.getUserId(UID_N_MR1));
Julia Reynolds4509ce72019-01-31 13:12:43 -0500153 Secure.putIntForUser(contentResolver,
154 Secure.NOTIFICATION_BUBBLES, 1, UserHandle.getUserId(UID_N_MR1));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400155
156 ContentProvider testContentProvider = mock(ContentProvider.class);
157 when(testContentProvider.getIContentProvider()).thenReturn(mTestIContentProvider);
158 contentResolver.addProvider(TEST_AUTHORITY, testContentProvider);
159
160 when(mTestIContentProvider.canonicalize(any(), eq(SOUND_URI)))
161 .thenReturn(CANONICAL_SOUND_URI);
162 when(mTestIContentProvider.canonicalize(any(), eq(CANONICAL_SOUND_URI)))
163 .thenReturn(CANONICAL_SOUND_URI);
164 when(mTestIContentProvider.uncanonicalize(any(), eq(CANONICAL_SOUND_URI)))
165 .thenReturn(SOUND_URI);
166
167 mTestNotificationPolicy = new NotificationManager.Policy(0, 0, 0, 0,
168 NotificationManager.Policy.STATE_CHANNELS_BYPASSING_DND);
169 when(mMockZenModeHelper.getNotificationPolicy()).thenReturn(mTestNotificationPolicy);
170 mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper);
171 resetZenModeHelper();
172
173 mAudioAttributes = new AudioAttributes.Builder()
174 .setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN)
175 .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
176 .setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED)
177 .build();
178 }
179
Annie Meng8b646fd2019-02-01 18:46:42 +0000180 private ByteArrayOutputStream writeXmlAndPurge(
181 String pkg, int uid, boolean forBackup, int userId, String... channelIds)
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400182 throws Exception {
183 XmlSerializer serializer = new FastXmlSerializer();
184 ByteArrayOutputStream baos = new ByteArrayOutputStream();
185 serializer.setOutput(new BufferedOutputStream(baos), "utf-8");
186 serializer.startDocument(null, true);
Annie Meng8b646fd2019-02-01 18:46:42 +0000187 mHelper.writeXml(serializer, forBackup, userId);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400188 serializer.endDocument();
189 serializer.flush();
190 for (String channelId : channelIds) {
191 mHelper.permanentlyDeleteNotificationChannel(pkg, uid, channelId);
192 }
193 return baos;
194 }
195
Annie Meng8b646fd2019-02-01 18:46:42 +0000196 private void loadStreamXml(ByteArrayOutputStream stream, boolean forRestore, int userId)
197 throws Exception {
198 loadByteArrayXml(stream.toByteArray(), forRestore, userId);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400199 }
200
Annie Meng8b646fd2019-02-01 18:46:42 +0000201 private void loadByteArrayXml(byte[] byteArray, boolean forRestore, int userId)
202 throws Exception {
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400203 XmlPullParser parser = Xml.newPullParser();
204 parser.setInput(new BufferedInputStream(new ByteArrayInputStream(byteArray)), null);
205 parser.nextTag();
Annie Meng8b646fd2019-02-01 18:46:42 +0000206 mHelper.readXml(parser, forRestore, userId);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400207 }
208
209 private void compareChannels(NotificationChannel expected, NotificationChannel actual) {
210 assertEquals(expected.getId(), actual.getId());
211 assertEquals(expected.getName(), actual.getName());
212 assertEquals(expected.getDescription(), actual.getDescription());
213 assertEquals(expected.shouldVibrate(), actual.shouldVibrate());
214 assertEquals(expected.shouldShowLights(), actual.shouldShowLights());
215 assertEquals(expected.getImportance(), actual.getImportance());
216 assertEquals(expected.getLockscreenVisibility(), actual.getLockscreenVisibility());
217 assertEquals(expected.getSound(), actual.getSound());
218 assertEquals(expected.canBypassDnd(), actual.canBypassDnd());
219 assertTrue(Arrays.equals(expected.getVibrationPattern(), actual.getVibrationPattern()));
220 assertEquals(expected.getGroup(), actual.getGroup());
221 assertEquals(expected.getAudioAttributes(), actual.getAudioAttributes());
222 assertEquals(expected.getLightColor(), actual.getLightColor());
223 }
224
225 private void compareGroups(NotificationChannelGroup expected, NotificationChannelGroup actual) {
226 assertEquals(expected.getId(), actual.getId());
227 assertEquals(expected.getName(), actual.getName());
228 assertEquals(expected.getDescription(), actual.getDescription());
229 assertEquals(expected.isBlocked(), actual.isBlocked());
230 }
231
232 private NotificationChannel getChannel() {
233 return new NotificationChannel("id", "name", IMPORTANCE_LOW);
234 }
235
236 private NotificationChannel findChannel(List<NotificationChannel> channels, String id) {
237 for (NotificationChannel channel : channels) {
238 if (channel.getId().equals(id)) {
239 return channel;
240 }
241 }
242 return null;
243 }
244
245 private void resetZenModeHelper() {
246 reset(mMockZenModeHelper);
247 when(mMockZenModeHelper.getNotificationPolicy()).thenReturn(mTestNotificationPolicy);
248 }
249
Annie Meng8b646fd2019-02-01 18:46:42 +0000250 private void setUpPackageWithUid(String packageName, int uid) throws Exception {
251 when(mPm.getApplicationInfoAsUser(eq(packageName), anyInt(), anyInt()))
252 .thenReturn(new ApplicationInfo());
253 when(mPm.getPackageUidAsUser(eq(packageName), anyInt())).thenReturn(uid);
254 }
255
256 @Test
257 public void testWriteXml_onlyBackupsTargetUser() throws Exception {
258 // Setup package notifications.
259 String package0 = "test.package.user0";
260 int uid0 = 1001;
261 setUpPackageWithUid(package0, uid0);
262 NotificationChannel channel0 = new NotificationChannel("id0", "name0", IMPORTANCE_HIGH);
263 mHelper.createNotificationChannel(package0, uid0, channel0, true, false);
264
265 String package10 = "test.package.user10";
266 int uid10 = 1001001;
267 setUpPackageWithUid(package10, uid10);
268 NotificationChannel channel10 = new NotificationChannel("id10", "name10", IMPORTANCE_HIGH);
269 mHelper.createNotificationChannel(package10, uid10, channel10, true, false);
270
271 ByteArrayOutputStream baos = writeXmlAndPurge(package10, uid10, true, 10);
272
273 // Reset state.
274 mHelper.onPackagesChanged(true, 0, new String[] {package0}, new int[] {uid0});
275 mHelper.onPackagesChanged(true, 10, new String[] {package10}, new int[] {uid10});
276
277 // Parse backup data.
278 loadStreamXml(baos, true, 0);
279 loadStreamXml(baos, true, 10);
280
281 assertEquals(
282 channel10,
283 mHelper.getNotificationChannel(package10, uid10, channel10.getId(), false));
284 assertNull(mHelper.getNotificationChannel(package0, uid0, channel0.getId(), false));
285 }
286
287 @Test
288 public void testReadXml_onlyRestoresTargetUser() throws Exception {
289 // Setup package in user 0.
290 String package0 = "test.package.user0";
291 int uid0 = 1001;
292 setUpPackageWithUid(package0, uid0);
293 NotificationChannel channel0 = new NotificationChannel("id0", "name0", IMPORTANCE_HIGH);
294 mHelper.createNotificationChannel(package0, uid0, channel0, true, false);
295
296 ByteArrayOutputStream baos = writeXmlAndPurge(package0, uid0, true, 0);
297
298 // Reset state.
299 mHelper.onPackagesChanged(true, 0, new String[] {package0}, new int[] {uid0});
300
301 // Restore should convert the uid according to the target user.
302 int expectedUid = 1001001;
303 setUpPackageWithUid(package0, expectedUid);
304 // Parse backup data.
305 loadStreamXml(baos, true, 10);
306
307 assertEquals(
308 channel0,
309 mHelper.getNotificationChannel(package0, expectedUid, channel0.getId(), false));
310 assertNull(mHelper.getNotificationChannel(package0, uid0, channel0.getId(), false));
311 }
312
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400313 @Test
314 public void testChannelXml() throws Exception {
315 NotificationChannelGroup ncg = new NotificationChannelGroup("1", "bye");
316 ncg.setBlocked(true);
317 ncg.setDescription("group desc");
318 NotificationChannelGroup ncg2 = new NotificationChannelGroup("2", "hello");
319 NotificationChannel channel1 =
320 new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH);
321 NotificationChannel channel2 =
322 new NotificationChannel("id2", "name2", IMPORTANCE_LOW);
323 channel2.setDescription("descriptions for all");
324 channel2.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes);
325 channel2.enableLights(true);
326 channel2.setBypassDnd(true);
327 channel2.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
328 channel2.enableVibration(true);
329 channel2.setGroup(ncg.getId());
330 channel2.setVibrationPattern(new long[]{100, 67, 145, 156});
331 channel2.setLightColor(Color.BLUE);
332
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400333 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg, true);
334 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg2, true);
335 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false);
336 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, false, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400337
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400338 mHelper.setShowBadge(PKG_N_MR1, UID_N_MR1, true);
339 mHelper.setAppImportanceLocked(PKG_N_MR1, UID_N_MR1);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400340
Annie Meng8b646fd2019-02-01 18:46:42 +0000341 ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, false,
342 UserHandle.USER_ALL, channel1.getId(), channel2.getId(),
343 NotificationChannel.DEFAULT_CHANNEL_ID);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400344 mHelper.onPackagesChanged(true, UserHandle.myUserId(), new String[]{PKG_N_MR1}, new int[]{
345 UID_N_MR1});
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400346
Annie Meng8b646fd2019-02-01 18:46:42 +0000347 loadStreamXml(baos, false, UserHandle.USER_ALL);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400348
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400349 assertTrue(mHelper.canShowBadge(PKG_N_MR1, UID_N_MR1));
350 assertTrue(mHelper.getIsAppImportanceLocked(PKG_N_MR1, UID_N_MR1));
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -0400351 assertEquals(channel1,
352 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1.getId(), false));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400353 compareChannels(channel2,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400354 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2.getId(), false));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400355
Julia Reynolds13ed28b2018-09-21 15:20:13 -0400356 List<NotificationChannelGroup> actualGroups = mHelper.getNotificationChannelGroups(
357 PKG_N_MR1, UID_N_MR1, false, true, false).getList();
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400358 boolean foundNcg = false;
359 for (NotificationChannelGroup actual : actualGroups) {
360 if (ncg.getId().equals(actual.getId())) {
361 foundNcg = true;
362 compareGroups(ncg, actual);
363 } else if (ncg2.getId().equals(actual.getId())) {
364 compareGroups(ncg2, actual);
365 }
366 }
367 assertTrue(foundNcg);
368
369 boolean foundChannel2Group = false;
370 for (NotificationChannelGroup actual : actualGroups) {
371 if (channel2.getGroup().equals(actual.getChannels().get(0).getGroup())) {
372 foundChannel2Group = true;
373 break;
374 }
375 }
376 assertTrue(foundChannel2Group);
377 }
378
379 @Test
380 public void testChannelXmlForBackup() throws Exception {
381 NotificationChannelGroup ncg = new NotificationChannelGroup("1", "bye");
382 NotificationChannelGroup ncg2 = new NotificationChannelGroup("2", "hello");
383 NotificationChannel channel1 =
384 new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH);
385 NotificationChannel channel2 =
386 new NotificationChannel("id2", "name2", IMPORTANCE_LOW);
387 channel2.setDescription("descriptions for all");
388 channel2.setSound(SOUND_URI, mAudioAttributes);
389 channel2.enableLights(true);
390 channel2.setBypassDnd(true);
391 channel2.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
392 channel2.enableVibration(false);
393 channel2.setGroup(ncg.getId());
394 channel2.setLightColor(Color.BLUE);
395 NotificationChannel channel3 = new NotificationChannel("id3", "NAM3", IMPORTANCE_HIGH);
396 channel3.enableVibration(true);
397
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400398 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg, true);
399 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg2, true);
400 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false);
401 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, false, false);
402 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3, false, false);
403 mHelper.createNotificationChannel(PKG_O, UID_O, getChannel(), true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400404
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400405 mHelper.setShowBadge(PKG_N_MR1, UID_N_MR1, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400406
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400407 mHelper.setImportance(PKG_O, UID_O, IMPORTANCE_NONE);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400408
Annie Meng8b646fd2019-02-01 18:46:42 +0000409 ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, true,
410 UserHandle.USER_SYSTEM, channel1.getId(), channel2.getId(), channel3.getId(),
411 NotificationChannel.DEFAULT_CHANNEL_ID);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400412 mHelper.onPackagesChanged(true, UserHandle.myUserId(), new String[]{PKG_N_MR1, PKG_O},
413 new int[]{UID_N_MR1, UID_O});
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400414
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400415 mHelper.setShowBadge(PKG_O, UID_O, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400416
Annie Meng8b646fd2019-02-01 18:46:42 +0000417 loadStreamXml(baos, true, UserHandle.USER_SYSTEM);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400418
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400419 assertEquals(IMPORTANCE_NONE, mHelper.getImportance(PKG_O, UID_O));
420 assertTrue(mHelper.canShowBadge(PKG_N_MR1, UID_N_MR1));
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -0400421 assertEquals(channel1,
422 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1.getId(), false));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400423 compareChannels(channel2,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400424 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2.getId(), false));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400425 compareChannels(channel3,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400426 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3.getId(), false));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400427
Julia Reynolds13ed28b2018-09-21 15:20:13 -0400428 List<NotificationChannelGroup> actualGroups = mHelper.getNotificationChannelGroups(
429 PKG_N_MR1, UID_N_MR1, false, true, false).getList();
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400430 boolean foundNcg = false;
431 for (NotificationChannelGroup actual : actualGroups) {
432 if (ncg.getId().equals(actual.getId())) {
433 foundNcg = true;
434 compareGroups(ncg, actual);
435 } else if (ncg2.getId().equals(actual.getId())) {
436 compareGroups(ncg2, actual);
437 }
438 }
439 assertTrue(foundNcg);
440
441 boolean foundChannel2Group = false;
442 for (NotificationChannelGroup actual : actualGroups) {
443 if (channel2.getGroup().equals(actual.getChannels().get(0).getGroup())) {
444 foundChannel2Group = true;
445 break;
446 }
447 }
448 assertTrue(foundChannel2Group);
449 }
450
451 @Test
452 public void testBackupXml_backupCanonicalizedSoundUri() throws Exception {
453 NotificationChannel channel =
454 new NotificationChannel("id", "name", IMPORTANCE_LOW);
455 channel.setSound(SOUND_URI, mAudioAttributes);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400456 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400457
Annie Meng8b646fd2019-02-01 18:46:42 +0000458 ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, true,
459 UserHandle.USER_SYSTEM, channel.getId());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400460
461 // Testing that in restore we are given the canonical version
Annie Meng8b646fd2019-02-01 18:46:42 +0000462 loadStreamXml(baos, true, UserHandle.USER_SYSTEM);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400463 verify(mTestIContentProvider).uncanonicalize(any(), eq(CANONICAL_SOUND_URI));
464 }
465
466 @Test
467 public void testRestoreXml_withExistentCanonicalizedSoundUri() throws Exception {
468 Uri localUri = Uri.parse("content://" + TEST_AUTHORITY + "/local/url");
469 Uri canonicalBasedOnLocal = localUri.buildUpon()
470 .appendQueryParameter("title", "Test")
471 .appendQueryParameter("canonical", "1")
472 .build();
473 when(mTestIContentProvider.canonicalize(any(), eq(CANONICAL_SOUND_URI)))
474 .thenReturn(canonicalBasedOnLocal);
475 when(mTestIContentProvider.uncanonicalize(any(), eq(CANONICAL_SOUND_URI)))
476 .thenReturn(localUri);
477 when(mTestIContentProvider.uncanonicalize(any(), eq(canonicalBasedOnLocal)))
478 .thenReturn(localUri);
479
480 NotificationChannel channel =
481 new NotificationChannel("id", "name", IMPORTANCE_LOW);
482 channel.setSound(SOUND_URI, mAudioAttributes);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400483 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false);
Annie Meng8b646fd2019-02-01 18:46:42 +0000484 ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, true,
485 UserHandle.USER_SYSTEM, channel.getId());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400486
Annie Meng8b646fd2019-02-01 18:46:42 +0000487 loadStreamXml(baos, true, UserHandle.USER_SYSTEM);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400488
489 NotificationChannel actualChannel = mHelper.getNotificationChannel(
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400490 PKG_N_MR1, UID_N_MR1, channel.getId(), false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400491 assertEquals(localUri, actualChannel.getSound());
492 }
493
494 @Test
495 public void testRestoreXml_withNonExistentCanonicalizedSoundUri() throws Exception {
496 Thread.sleep(3000);
497 when(mTestIContentProvider.canonicalize(any(), eq(CANONICAL_SOUND_URI)))
498 .thenReturn(null);
499 when(mTestIContentProvider.uncanonicalize(any(), eq(CANONICAL_SOUND_URI)))
500 .thenReturn(null);
501
502 NotificationChannel channel =
503 new NotificationChannel("id", "name", IMPORTANCE_LOW);
504 channel.setSound(SOUND_URI, mAudioAttributes);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400505 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false);
Annie Meng8b646fd2019-02-01 18:46:42 +0000506 ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, true,
507 UserHandle.USER_SYSTEM, channel.getId());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400508
Annie Meng8b646fd2019-02-01 18:46:42 +0000509 loadStreamXml(baos, true, UserHandle.USER_SYSTEM);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400510
511 NotificationChannel actualChannel = mHelper.getNotificationChannel(
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400512 PKG_N_MR1, UID_N_MR1, channel.getId(), false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400513 assertEquals(Settings.System.DEFAULT_NOTIFICATION_URI, actualChannel.getSound());
514 }
515
516
517 /**
518 * Although we don't make backups with uncanonicalized uris anymore, we used to, so we have to
519 * handle its restore properly.
520 */
521 @Test
522 public void testRestoreXml_withUncanonicalizedNonLocalSoundUri() throws Exception {
523 // Not a local uncanonicalized uri, simulating that it fails to exist locally
524 when(mTestIContentProvider.canonicalize(any(), eq(SOUND_URI))).thenReturn(null);
525 String id = "id";
526 String backupWithUncanonicalizedSoundUri = "<ranking version=\"1\">\n"
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400527 + "<package name=\"" + PKG_N_MR1 + "\" show_badge=\"true\">\n"
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400528 + "<channel id=\"" + id + "\" name=\"name\" importance=\"2\" "
529 + "sound=\"" + SOUND_URI + "\" "
530 + "usage=\"6\" content_type=\"0\" flags=\"1\" show_badge=\"true\" />\n"
531 + "<channel id=\"miscellaneous\" name=\"Uncategorized\" usage=\"5\" "
532 + "content_type=\"4\" flags=\"0\" show_badge=\"true\" />\n"
533 + "</package>\n"
534 + "</ranking>\n";
535
Annie Meng8b646fd2019-02-01 18:46:42 +0000536 loadByteArrayXml(
537 backupWithUncanonicalizedSoundUri.getBytes(), true, UserHandle.USER_SYSTEM);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400538
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400539 NotificationChannel actualChannel = mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, id, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400540 assertEquals(Settings.System.DEFAULT_NOTIFICATION_URI, actualChannel.getSound());
541 }
542
543 @Test
544 public void testBackupRestoreXml_withNullSoundUri() throws Exception {
545 NotificationChannel channel =
546 new NotificationChannel("id", "name", IMPORTANCE_LOW);
547 channel.setSound(null, mAudioAttributes);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400548 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false);
Annie Meng8b646fd2019-02-01 18:46:42 +0000549 ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, true,
550 UserHandle.USER_SYSTEM, channel.getId());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400551
Annie Meng8b646fd2019-02-01 18:46:42 +0000552 loadStreamXml(baos, true, UserHandle.USER_SYSTEM);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400553
554 NotificationChannel actualChannel = mHelper.getNotificationChannel(
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400555 PKG_N_MR1, UID_N_MR1, channel.getId(), false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400556 assertEquals(null, actualChannel.getSound());
557 }
558
559 @Test
560 public void testChannelXml_backup() throws Exception {
561 NotificationChannelGroup ncg = new NotificationChannelGroup("1", "bye");
562 NotificationChannelGroup ncg2 = new NotificationChannelGroup("2", "hello");
563 NotificationChannel channel1 =
564 new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH);
565 NotificationChannel channel2 =
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -0400566 new NotificationChannel("id2", "name2", IMPORTANCE_HIGH);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400567 NotificationChannel channel3 =
568 new NotificationChannel("id3", "name3", IMPORTANCE_LOW);
569 channel3.setGroup(ncg.getId());
570
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400571 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg, true);
572 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg2, true);
573 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false);
574 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, false, false);
575 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400576
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400577 mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1.getId());
578 mHelper.deleteNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg.getId());
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -0400579 assertEquals(channel2,
580 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2.getId(), false));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400581
Annie Meng8b646fd2019-02-01 18:46:42 +0000582 ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, true,
583 UserHandle.USER_SYSTEM, channel1.getId(), channel2.getId(), channel3.getId(),
584 NotificationChannel.DEFAULT_CHANNEL_ID);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400585 mHelper.onPackagesChanged(true, UserHandle.myUserId(), new String[]{PKG_N_MR1}, new int[]{
586 UID_N_MR1});
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400587
588 XmlPullParser parser = Xml.newPullParser();
589 parser.setInput(new BufferedInputStream(new ByteArrayInputStream(baos.toByteArray())),
590 null);
591 parser.nextTag();
Annie Meng8b646fd2019-02-01 18:46:42 +0000592 mHelper.readXml(parser, true, UserHandle.USER_SYSTEM);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400593
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400594 assertNull(mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1.getId(), false));
595 assertNull(mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3.getId(), false));
596 assertNull(mHelper.getNotificationChannelGroup(ncg.getId(), PKG_N_MR1, UID_N_MR1));
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -0400597 assertEquals(channel2,
598 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2.getId(), false));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400599 }
600
601 @Test
602 public void testChannelXml_defaultChannelLegacyApp_noUserSettings() throws Exception {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400603 ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, false,
Annie Meng8b646fd2019-02-01 18:46:42 +0000604 UserHandle.USER_ALL, NotificationChannel.DEFAULT_CHANNEL_ID);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400605
Annie Meng8b646fd2019-02-01 18:46:42 +0000606 loadStreamXml(baos, false, UserHandle.USER_ALL);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400607
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400608 final NotificationChannel updated = mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400609 NotificationChannel.DEFAULT_CHANNEL_ID, false);
610 assertEquals(NotificationManager.IMPORTANCE_UNSPECIFIED, updated.getImportance());
611 assertFalse(updated.canBypassDnd());
612 assertEquals(NotificationManager.VISIBILITY_NO_OVERRIDE, updated.getLockscreenVisibility());
613 assertEquals(0, updated.getUserLockedFields());
614 }
615
616 @Test
617 public void testChannelXml_defaultChannelUpdatedApp_userSettings() throws Exception {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400618 final NotificationChannel defaultChannel = mHelper.getNotificationChannel(PKG_N_MR1,
619 UID_N_MR1,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400620 NotificationChannel.DEFAULT_CHANNEL_ID, false);
621 defaultChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400622 mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, defaultChannel, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400623
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400624 ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, false,
Annie Meng8b646fd2019-02-01 18:46:42 +0000625 UserHandle.USER_ALL, NotificationChannel.DEFAULT_CHANNEL_ID);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400626
Annie Meng8b646fd2019-02-01 18:46:42 +0000627 loadStreamXml(baos, false, UserHandle.USER_ALL);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400628
629 assertEquals(NotificationManager.IMPORTANCE_LOW, mHelper.getNotificationChannel(
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400630 PKG_N_MR1, UID_N_MR1, NotificationChannel.DEFAULT_CHANNEL_ID, false).getImportance());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400631 }
632
633 @Test
634 public void testChannelXml_upgradeCreateDefaultChannel() throws Exception {
635 final String preupgradeXml = "<ranking version=\"1\">\n"
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400636 + "<package name=\"" + PKG_N_MR1
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400637 + "\" importance=\"" + NotificationManager.IMPORTANCE_HIGH
638 + "\" priority=\"" + Notification.PRIORITY_MAX + "\" visibility=\""
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400639 + Notification.VISIBILITY_SECRET + "\"" +" uid=\"" + UID_N_MR1 + "\" />\n"
640 + "<package name=\"" + PKG_O + "\" uid=\"" + UID_O + "\" visibility=\""
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400641 + Notification.VISIBILITY_PRIVATE + "\" />\n"
642 + "</ranking>";
643 XmlPullParser parser = Xml.newPullParser();
644 parser.setInput(new BufferedInputStream(new ByteArrayInputStream(preupgradeXml.getBytes())),
645 null);
646 parser.nextTag();
Annie Meng8b646fd2019-02-01 18:46:42 +0000647 mHelper.readXml(parser, false, UserHandle.USER_ALL);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400648
649 final NotificationChannel updated1 =
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400650 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, NotificationChannel.DEFAULT_CHANNEL_ID, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400651 assertEquals(NotificationManager.IMPORTANCE_HIGH, updated1.getImportance());
652 assertTrue(updated1.canBypassDnd());
653 assertEquals(Notification.VISIBILITY_SECRET, updated1.getLockscreenVisibility());
654 assertEquals(NotificationChannel.USER_LOCKED_IMPORTANCE
655 | NotificationChannel.USER_LOCKED_PRIORITY
656 | NotificationChannel.USER_LOCKED_VISIBILITY,
657 updated1.getUserLockedFields());
658
659 // No Default Channel created for updated packages
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400660 assertEquals(null, mHelper.getNotificationChannel(PKG_O, UID_O,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400661 NotificationChannel.DEFAULT_CHANNEL_ID, false));
662 }
663
664 @Test
665 public void testChannelXml_upgradeDeletesDefaultChannel() throws Exception {
666 final NotificationChannel defaultChannel = mHelper.getNotificationChannel(
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400667 PKG_N_MR1, UID_N_MR1, NotificationChannel.DEFAULT_CHANNEL_ID, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400668 assertTrue(defaultChannel != null);
Annie Meng8b646fd2019-02-01 18:46:42 +0000669 ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, false,
670 UserHandle.USER_ALL, NotificationChannel.DEFAULT_CHANNEL_ID);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400671 // Load package at higher sdk.
672 final ApplicationInfo upgraded = new ApplicationInfo();
673 upgraded.targetSdkVersion = Build.VERSION_CODES.N_MR1 + 1;
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400674 when(mPm.getApplicationInfoAsUser(eq(PKG_N_MR1), anyInt(), anyInt())).thenReturn(upgraded);
Annie Meng8b646fd2019-02-01 18:46:42 +0000675 loadStreamXml(baos, false, UserHandle.USER_ALL);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400676
677 // Default Channel should be gone.
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400678 assertEquals(null, mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400679 NotificationChannel.DEFAULT_CHANNEL_ID, false));
680 }
681
682 @Test
683 public void testDeletesDefaultChannelAfterChannelIsCreated() throws Exception {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400684 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400685 new NotificationChannel("bananas", "bananas", IMPORTANCE_LOW), true, false);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400686 ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, false,
Annie Meng8b646fd2019-02-01 18:46:42 +0000687 UserHandle.USER_ALL, NotificationChannel.DEFAULT_CHANNEL_ID, "bananas");
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400688
689 // Load package at higher sdk.
690 final ApplicationInfo upgraded = new ApplicationInfo();
691 upgraded.targetSdkVersion = Build.VERSION_CODES.N_MR1 + 1;
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400692 when(mPm.getApplicationInfoAsUser(eq(PKG_N_MR1), anyInt(), anyInt())).thenReturn(upgraded);
Annie Meng8b646fd2019-02-01 18:46:42 +0000693 loadStreamXml(baos, false, UserHandle.USER_ALL);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400694
695 // Default Channel should be gone.
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400696 assertEquals(null, mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400697 NotificationChannel.DEFAULT_CHANNEL_ID, false));
698 }
699
700 @Test
701 public void testLoadingOldChannelsDoesNotDeleteNewlyCreatedChannels() throws Exception {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400702 ByteArrayOutputStream baos = writeXmlAndPurge(PKG_N_MR1, UID_N_MR1, false,
Annie Meng8b646fd2019-02-01 18:46:42 +0000703 UserHandle.USER_ALL, NotificationChannel.DEFAULT_CHANNEL_ID, "bananas");
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400704 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400705 new NotificationChannel("bananas", "bananas", IMPORTANCE_LOW), true, false);
706
Annie Meng8b646fd2019-02-01 18:46:42 +0000707 loadStreamXml(baos, false, UserHandle.USER_ALL);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400708
709 // Should still have the newly created channel that wasn't in the xml.
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400710 assertTrue(mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, "bananas", false) != null);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400711 }
712
713 @Test
714 public void testCreateChannel_blocked() throws Exception {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400715 mHelper.setImportance(PKG_N_MR1, UID_N_MR1, IMPORTANCE_NONE);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400716
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400717 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400718 new NotificationChannel("bananas", "bananas", IMPORTANCE_LOW), true, false);
719 }
720
721 @Test
722 public void testCreateChannel_badImportance() throws Exception {
723 try {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400724 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400725 new NotificationChannel("bananas", "bananas", IMPORTANCE_NONE - 1),
726 true, false);
727 fail("Was allowed to create a channel with invalid importance");
728 } catch (IllegalArgumentException e) {
729 // yay
730 }
731 try {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400732 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400733 new NotificationChannel("bananas", "bananas", IMPORTANCE_UNSPECIFIED),
734 true, false);
735 fail("Was allowed to create a channel with invalid importance");
736 } catch (IllegalArgumentException e) {
737 // yay
738 }
739 try {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400740 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400741 new NotificationChannel("bananas", "bananas", IMPORTANCE_MAX + 1),
742 true, false);
743 fail("Was allowed to create a channel with invalid importance");
744 } catch (IllegalArgumentException e) {
745 // yay
746 }
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400747 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400748 new NotificationChannel("bananas", "bananas", IMPORTANCE_NONE), true, false);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400749 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400750 new NotificationChannel("bananas", "bananas", IMPORTANCE_MAX), true, false);
751 }
752
753
754 @Test
755 public void testUpdate() throws Exception {
756 // no fields locked by user
757 final NotificationChannel channel =
758 new NotificationChannel("id2", "name2", IMPORTANCE_LOW);
759 channel.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes);
760 channel.enableLights(true);
761 channel.setBypassDnd(true);
762 channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
763
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400764 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, false, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400765
766 // same id, try to update all fields
767 final NotificationChannel channel2 =
768 new NotificationChannel("id2", "name2", NotificationManager.IMPORTANCE_HIGH);
769 channel2.setSound(new Uri.Builder().scheme("test2").build(), mAudioAttributes);
770 channel2.enableLights(false);
771 channel2.setBypassDnd(false);
772 channel2.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
773
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400774 mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400775
776 // all fields should be changed
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400777 assertEquals(channel2, mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId(), false));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400778
779 verify(mHandler, times(1)).requestSort();
780 }
781
782 @Test
783 public void testUpdate_preUpgrade_updatesAppFields() throws Exception {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400784 mHelper.setImportance(PKG_N_MR1, UID_N_MR1, IMPORTANCE_UNSPECIFIED);
785 assertTrue(mHelper.canShowBadge(PKG_N_MR1, UID_N_MR1));
786 assertEquals(Notification.PRIORITY_DEFAULT, mHelper.getPackagePriority(PKG_N_MR1, UID_N_MR1));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400787 assertEquals(NotificationManager.VISIBILITY_NO_OVERRIDE,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400788 mHelper.getPackageVisibility(PKG_N_MR1, UID_N_MR1));
789 assertFalse(mHelper.getIsAppImportanceLocked(PKG_N_MR1, UID_N_MR1));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400790
791 NotificationChannel defaultChannel = mHelper.getNotificationChannel(
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400792 PKG_N_MR1, UID_N_MR1, NotificationChannel.DEFAULT_CHANNEL_ID, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400793
794 defaultChannel.setShowBadge(false);
795 defaultChannel.setImportance(IMPORTANCE_NONE);
796 defaultChannel.setBypassDnd(true);
797 defaultChannel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
798
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400799 mHelper.setAppImportanceLocked(PKG_N_MR1, UID_N_MR1);
800 mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, defaultChannel, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400801
802 // ensure app level fields are changed
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400803 assertFalse(mHelper.canShowBadge(PKG_N_MR1, UID_N_MR1));
804 assertEquals(Notification.PRIORITY_MAX, mHelper.getPackagePriority(PKG_N_MR1, UID_N_MR1));
805 assertEquals(Notification.VISIBILITY_SECRET, mHelper.getPackageVisibility(PKG_N_MR1,
806 UID_N_MR1));
807 assertEquals(IMPORTANCE_NONE, mHelper.getImportance(PKG_N_MR1, UID_N_MR1));
808 assertTrue(mHelper.getIsAppImportanceLocked(PKG_N_MR1, UID_N_MR1));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400809 }
810
811 @Test
812 public void testUpdate_postUpgrade_noUpdateAppFields() throws Exception {
813 final NotificationChannel channel = new NotificationChannel("id2", "name2", IMPORTANCE_LOW);
814
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400815 mHelper.createNotificationChannel(PKG_O, UID_O, channel, false, false);
816 assertTrue(mHelper.canShowBadge(PKG_O, UID_O));
817 assertEquals(Notification.PRIORITY_DEFAULT, mHelper.getPackagePriority(PKG_O, UID_O));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400818 assertEquals(NotificationManager.VISIBILITY_NO_OVERRIDE,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400819 mHelper.getPackageVisibility(PKG_O, UID_O));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400820
821 channel.setShowBadge(false);
822 channel.setImportance(IMPORTANCE_NONE);
823 channel.setBypassDnd(true);
824 channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
825
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400826 mHelper.updateNotificationChannel(PKG_O, UID_O, channel, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400827
828 // ensure app level fields are not changed
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400829 assertTrue(mHelper.canShowBadge(PKG_O, UID_O));
830 assertEquals(Notification.PRIORITY_DEFAULT, mHelper.getPackagePriority(PKG_O, UID_O));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400831 assertEquals(NotificationManager.VISIBILITY_NO_OVERRIDE,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400832 mHelper.getPackageVisibility(PKG_O, UID_O));
833 assertEquals(NotificationManager.IMPORTANCE_UNSPECIFIED, mHelper.getImportance(PKG_O,
834 UID_O));
835 assertFalse(mHelper.getIsAppImportanceLocked(PKG_O, UID_O));
836 }
837
838 @Test
839 public void testUpdate_preUpgrade_noUpdateAppFieldsWithMultipleChannels() throws Exception {
840 final NotificationChannel channel = new NotificationChannel("id2", "name2", IMPORTANCE_LOW);
841
842 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, false, false);
843 assertTrue(mHelper.canShowBadge(PKG_N_MR1, UID_N_MR1));
844 assertEquals(Notification.PRIORITY_DEFAULT, mHelper.getPackagePriority(PKG_N_MR1, UID_N_MR1));
845 assertEquals(NotificationManager.VISIBILITY_NO_OVERRIDE,
846 mHelper.getPackageVisibility(PKG_N_MR1, UID_N_MR1));
847
848 channel.setShowBadge(false);
849 channel.setImportance(IMPORTANCE_NONE);
850 channel.setBypassDnd(true);
851 channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
852
853 mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true);
854
855 NotificationChannel defaultChannel = mHelper.getNotificationChannel(
856 PKG_N_MR1, UID_N_MR1, NotificationChannel.DEFAULT_CHANNEL_ID, false);
857
858 defaultChannel.setShowBadge(false);
859 defaultChannel.setImportance(IMPORTANCE_NONE);
860 defaultChannel.setBypassDnd(true);
861 defaultChannel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
862
863 mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, defaultChannel, true);
864
865 // ensure app level fields are not changed
866 assertTrue(mHelper.canShowBadge(PKG_N_MR1, UID_N_MR1));
867 assertEquals(Notification.PRIORITY_DEFAULT, mHelper.getPackagePriority(PKG_N_MR1, UID_N_MR1));
868 assertEquals(NotificationManager.VISIBILITY_NO_OVERRIDE,
869 mHelper.getPackageVisibility(PKG_N_MR1, UID_N_MR1));
870 assertEquals(NotificationManager.IMPORTANCE_UNSPECIFIED, mHelper.getImportance(PKG_N_MR1,
871 UID_N_MR1));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400872 }
873
874 @Test
875 public void testGetNotificationChannel_ReturnsNullForUnknownChannel() throws Exception {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400876 assertEquals(null, mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, "garbage", false));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400877 }
878
879 @Test
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -0400880 public void testCreateChannel_CannotChangeHiddenFields() {
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400881 final NotificationChannel channel =
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -0400882 new NotificationChannel("id2", "name2", IMPORTANCE_HIGH);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400883 channel.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes);
884 channel.enableLights(true);
885 channel.setBypassDnd(true);
886 channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
887 channel.setShowBadge(true);
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800888 channel.setAllowBubbles(false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400889 int lockMask = 0;
890 for (int i = 0; i < NotificationChannel.LOCKABLE_FIELDS.length; i++) {
891 lockMask |= NotificationChannel.LOCKABLE_FIELDS[i];
892 }
893 channel.lockFields(lockMask);
894
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400895 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400896
897 NotificationChannel savedChannel =
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400898 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId(), false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400899
900 assertEquals(channel.getName(), savedChannel.getName());
901 assertEquals(channel.shouldShowLights(), savedChannel.shouldShowLights());
902 assertFalse(savedChannel.canBypassDnd());
903 assertFalse(Notification.VISIBILITY_SECRET == savedChannel.getLockscreenVisibility());
904 assertEquals(channel.canShowBadge(), savedChannel.canShowBadge());
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800905 assertEquals(channel.canBubble(), savedChannel.canBubble());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400906
907 verify(mHandler, never()).requestSort();
908 }
909
910 @Test
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -0400911 public void testCreateChannel_CannotChangeHiddenFieldsAssistant() {
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400912 final NotificationChannel channel =
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -0400913 new NotificationChannel("id2", "name2", IMPORTANCE_HIGH);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400914 channel.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes);
915 channel.enableLights(true);
916 channel.setBypassDnd(true);
917 channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
918 channel.setShowBadge(true);
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800919 channel.setAllowBubbles(false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400920 int lockMask = 0;
921 for (int i = 0; i < NotificationChannel.LOCKABLE_FIELDS.length; i++) {
922 lockMask |= NotificationChannel.LOCKABLE_FIELDS[i];
923 }
924 channel.lockFields(lockMask);
925
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400926 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400927
928 NotificationChannel savedChannel =
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400929 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId(), false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400930
931 assertEquals(channel.getName(), savedChannel.getName());
932 assertEquals(channel.shouldShowLights(), savedChannel.shouldShowLights());
933 assertFalse(savedChannel.canBypassDnd());
934 assertFalse(Notification.VISIBILITY_SECRET == savedChannel.getLockscreenVisibility());
935 assertEquals(channel.canShowBadge(), savedChannel.canShowBadge());
Mady Mellorc39b4ae2019-01-09 17:11:37 -0800936 assertEquals(channel.canBubble(), savedChannel.canBubble());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400937 }
938
939 @Test
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -0400940 public void testClearLockedFields() {
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400941 final NotificationChannel channel = getChannel();
942 mHelper.clearLockedFields(channel);
943 assertEquals(0, channel.getUserLockedFields());
944
945 channel.lockFields(NotificationChannel.USER_LOCKED_PRIORITY
946 | NotificationChannel.USER_LOCKED_IMPORTANCE);
947 mHelper.clearLockedFields(channel);
948 assertEquals(0, channel.getUserLockedFields());
949 }
950
951 @Test
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -0400952 public void testLockFields_soundAndVibration() {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400953 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, getChannel(), true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400954
955 final NotificationChannel update1 = getChannel();
956 update1.setSound(new Uri.Builder().scheme("test").build(),
957 new AudioAttributes.Builder().build());
958 update1.lockFields(NotificationChannel.USER_LOCKED_PRIORITY);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400959 mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update1, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400960 assertEquals(NotificationChannel.USER_LOCKED_PRIORITY
961 | NotificationChannel.USER_LOCKED_SOUND,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400962 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update1.getId(), false)
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400963 .getUserLockedFields());
964
965 NotificationChannel update2 = getChannel();
966 update2.enableVibration(true);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400967 mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update2, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400968 assertEquals(NotificationChannel.USER_LOCKED_PRIORITY
969 | NotificationChannel.USER_LOCKED_SOUND
970 | NotificationChannel.USER_LOCKED_VIBRATION,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400971 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update2.getId(), false)
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400972 .getUserLockedFields());
973 }
974
975 @Test
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -0400976 public void testLockFields_vibrationAndLights() {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400977 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, getChannel(), true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400978
979 final NotificationChannel update1 = getChannel();
980 update1.setVibrationPattern(new long[]{7945, 46 ,246});
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400981 mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update1, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400982 assertEquals(NotificationChannel.USER_LOCKED_VIBRATION,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400983 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update1.getId(), false)
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400984 .getUserLockedFields());
985
986 final NotificationChannel update2 = getChannel();
987 update2.enableLights(true);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400988 mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update2, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400989 assertEquals(NotificationChannel.USER_LOCKED_VIBRATION
990 | NotificationChannel.USER_LOCKED_LIGHTS,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400991 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update2.getId(), false)
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400992 .getUserLockedFields());
993 }
994
995 @Test
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -0400996 public void testLockFields_lightsAndImportance() {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -0400997 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, getChannel(), true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -0400998
999 final NotificationChannel update1 = getChannel();
1000 update1.setLightColor(Color.GREEN);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001001 mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update1, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001002 assertEquals(NotificationChannel.USER_LOCKED_LIGHTS,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001003 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update1.getId(), false)
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001004 .getUserLockedFields());
1005
1006 final NotificationChannel update2 = getChannel();
1007 update2.setImportance(IMPORTANCE_DEFAULT);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001008 mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update2, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001009 assertEquals(NotificationChannel.USER_LOCKED_LIGHTS
1010 | NotificationChannel.USER_LOCKED_IMPORTANCE,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001011 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update2.getId(), false)
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001012 .getUserLockedFields());
1013 }
1014
1015 @Test
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04001016 public void testLockFields_visibilityAndDndAndBadge() {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001017 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, getChannel(), true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001018 assertEquals(0,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001019 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, getChannel().getId(), false)
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001020 .getUserLockedFields());
1021
1022 final NotificationChannel update1 = getChannel();
1023 update1.setBypassDnd(true);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001024 mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update1, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001025 assertEquals(NotificationChannel.USER_LOCKED_PRIORITY,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001026 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update1.getId(), false)
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001027 .getUserLockedFields());
1028
1029 final NotificationChannel update2 = getChannel();
1030 update2.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001031 mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update2, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001032 assertEquals(NotificationChannel.USER_LOCKED_PRIORITY
1033 | NotificationChannel.USER_LOCKED_VISIBILITY,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001034 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update2.getId(), false)
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001035 .getUserLockedFields());
1036
1037 final NotificationChannel update3 = getChannel();
1038 update3.setShowBadge(false);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001039 mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update3, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001040 assertEquals(NotificationChannel.USER_LOCKED_PRIORITY
1041 | NotificationChannel.USER_LOCKED_VISIBILITY
1042 | NotificationChannel.USER_LOCKED_SHOW_BADGE,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001043 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update3.getId(), false)
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001044 .getUserLockedFields());
1045 }
1046
1047 @Test
Mady Mellorc39b4ae2019-01-09 17:11:37 -08001048 public void testLockFields_allowBubble() {
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04001049 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, getChannel(), true, false);
1050 assertEquals(0,
1051 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, getChannel().getId(), false)
1052 .getUserLockedFields());
1053
1054 final NotificationChannel update = getChannel();
Mady Mellorc39b4ae2019-01-09 17:11:37 -08001055 update.setAllowBubbles(false);
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04001056 mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, update, true);
Mady Mellorc39b4ae2019-01-09 17:11:37 -08001057 assertEquals(NotificationChannel.USER_LOCKED_ALLOW_BUBBLE,
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04001058 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, update.getId(), false)
1059 .getUserLockedFields());
1060 }
1061
1062 @Test
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001063 public void testDeleteNonExistentChannel() throws Exception {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001064 mHelper.deleteNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, "does not exist");
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001065 }
1066
1067 @Test
1068 public void testGetDeletedChannel() throws Exception {
1069 NotificationChannel channel = getChannel();
1070 channel.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes);
1071 channel.enableLights(true);
1072 channel.setBypassDnd(true);
1073 channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
1074 channel.enableVibration(true);
1075 channel.setVibrationPattern(new long[]{100, 67, 145, 156});
1076
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001077 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false);
1078 mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001079
1080 // Does not return deleted channel
1081 NotificationChannel response =
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001082 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId(), false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001083 assertNull(response);
1084
1085 // Returns deleted channel
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001086 response = mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId(), true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001087 compareChannels(channel, response);
1088 assertTrue(response.isDeleted());
1089 }
1090
1091 @Test
1092 public void testGetDeletedChannels() throws Exception {
1093 Map<String, NotificationChannel> channelMap = new HashMap<>();
1094 NotificationChannel channel =
1095 new NotificationChannel("id2", "name2", IMPORTANCE_LOW);
1096 channel.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes);
1097 channel.enableLights(true);
1098 channel.setBypassDnd(true);
1099 channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
1100 channel.enableVibration(true);
1101 channel.setVibrationPattern(new long[]{100, 67, 145, 156});
1102 channelMap.put(channel.getId(), channel);
1103 NotificationChannel channel2 =
1104 new NotificationChannel("id4", "a", NotificationManager.IMPORTANCE_HIGH);
1105 channelMap.put(channel2.getId(), channel2);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001106 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false);
1107 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001108
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001109 mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001110
1111 // Returns only non-deleted channels
1112 List<NotificationChannel> channels =
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001113 mHelper.getNotificationChannels(PKG_N_MR1, UID_N_MR1, false).getList();
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001114 assertEquals(2, channels.size()); // Default channel + non-deleted channel
1115 for (NotificationChannel nc : channels) {
1116 if (!NotificationChannel.DEFAULT_CHANNEL_ID.equals(nc.getId())) {
1117 compareChannels(channel2, nc);
1118 }
1119 }
1120
1121 // Returns deleted channels too
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001122 channels = mHelper.getNotificationChannels(PKG_N_MR1, UID_N_MR1, true).getList();
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001123 assertEquals(3, channels.size()); // Includes default channel
1124 for (NotificationChannel nc : channels) {
1125 if (!NotificationChannel.DEFAULT_CHANNEL_ID.equals(nc.getId())) {
1126 compareChannels(channelMap.get(nc.getId()), nc);
1127 }
1128 }
1129 }
1130
1131 @Test
1132 public void testGetDeletedChannelCount() throws Exception {
1133 NotificationChannel channel =
1134 new NotificationChannel("id2", "name2", IMPORTANCE_LOW);
1135 NotificationChannel channel2 =
1136 new NotificationChannel("id4", "a", NotificationManager.IMPORTANCE_HIGH);
1137 NotificationChannel channel3 =
1138 new NotificationChannel("id5", "a", NotificationManager.IMPORTANCE_HIGH);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001139 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false);
1140 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, true, false);
1141 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001142
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001143 mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId());
1144 mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3.getId());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001145
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001146 assertEquals(2, mHelper.getDeletedChannelCount(PKG_N_MR1, UID_N_MR1));
1147 assertEquals(0, mHelper.getDeletedChannelCount("pkg2", UID_O));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001148 }
1149
1150 @Test
1151 public void testGetBlockedChannelCount() throws Exception {
1152 NotificationChannel channel =
1153 new NotificationChannel("id2", "name2", IMPORTANCE_LOW);
1154 NotificationChannel channel2 =
1155 new NotificationChannel("id4", "a", NotificationManager.IMPORTANCE_NONE);
1156 NotificationChannel channel3 =
1157 new NotificationChannel("id5", "a", NotificationManager.IMPORTANCE_NONE);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001158 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false);
1159 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, true, false);
1160 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001161
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001162 mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3.getId());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001163
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001164 assertEquals(1, mHelper.getBlockedChannelCount(PKG_N_MR1, UID_N_MR1));
1165 assertEquals(0, mHelper.getBlockedChannelCount("pkg2", UID_O));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001166 }
1167
1168 @Test
Beverly4f7b53d2018-11-20 09:56:31 -05001169 public void testUpdateChannelsBypassingDnd_onUserSwitch_onUserUnlocked() throws Exception {
1170 int user = USER.getIdentifier();
1171 NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1");
1172 NotificationChannel channel1 = new NotificationChannel("id1", "name1",
1173 NotificationManager.IMPORTANCE_MAX);
1174 channel1.setBypassDnd(true);
1175 channel1.setGroup(ncg.getId());
1176
1177 // channel is associated with a group, then group is deleted
1178 mHelper.createNotificationChannelGroup(PKG_N_MR1, user, ncg, /* fromTargetApp */ true);
1179 mHelper.createNotificationChannel(PKG_N_MR1, user, channel1, true, /*has DND access*/ true);
1180 mHelper.deleteNotificationChannelGroup(PKG_N_MR1, user, ncg.getId());
1181
1182 mHelper.onUserSwitched(user);
1183 mHelper.onUserUnlocked(user);
1184 }
1185
1186 @Test
Beverly0479cde22018-11-09 11:05:34 -05001187 public void testGetChannelsBypassingDndCount_noChannelsBypassing() throws Exception {
1188 assertEquals(0, mHelper.getNotificationChannelsBypassingDnd(PKG_N_MR1,
1189 USER.getIdentifier()).getList().size());
1190 }
1191
1192 @Test
1193 public void testGetChannelsBypassingDnd_noChannelsForUserIdBypassing()
1194 throws Exception {
1195 int user = 9;
1196 NotificationChannel channel = new NotificationChannel("id", "name",
1197 NotificationManager.IMPORTANCE_MAX);
1198 channel.setBypassDnd(true);
1199 mHelper.createNotificationChannel(PKG_N_MR1, 111, channel, true, true);
1200
1201 assertEquals(0, mHelper.getNotificationChannelsBypassingDnd(PKG_N_MR1,
1202 user).getList().size());
1203 }
1204
1205 @Test
1206 public void testGetChannelsBypassingDndCount_oneChannelBypassing_groupBlocked() {
1207 int user = USER.getIdentifier();
1208 NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1");
1209 NotificationChannel channel1 = new NotificationChannel("id1", "name1",
1210 NotificationManager.IMPORTANCE_MAX);
1211 channel1.setBypassDnd(true);
1212 channel1.setGroup(ncg.getId());
1213 mHelper.createNotificationChannelGroup(PKG_N_MR1, user, ncg, /* fromTargetApp */ true);
1214 mHelper.createNotificationChannel(PKG_N_MR1, user, channel1, true, /*has DND access*/ true);
1215
1216 assertEquals(1, mHelper.getNotificationChannelsBypassingDnd(PKG_N_MR1,
1217 user).getList().size());
1218
1219 // disable group
1220 ncg.setBlocked(true);
1221 mHelper.createNotificationChannelGroup(PKG_N_MR1, user, ncg, /* fromTargetApp */ false);
1222 assertEquals(0, mHelper.getNotificationChannelsBypassingDnd(PKG_N_MR1,
1223 user).getList().size());
1224 }
1225
1226 @Test
1227 public void testGetChannelsBypassingDndCount_multipleChannelsBypassing() {
1228 int user = USER.getIdentifier();
1229 NotificationChannel channel1 = new NotificationChannel("id1", "name1",
1230 NotificationManager.IMPORTANCE_MAX);
1231 NotificationChannel channel2 = new NotificationChannel("id2", "name2",
1232 NotificationManager.IMPORTANCE_MAX);
1233 NotificationChannel channel3 = new NotificationChannel("id3", "name3",
1234 NotificationManager.IMPORTANCE_MAX);
1235 channel1.setBypassDnd(true);
1236 channel2.setBypassDnd(true);
1237 channel3.setBypassDnd(true);
1238 // has DND access, so can set bypassDnd attribute
1239 mHelper.createNotificationChannel(PKG_N_MR1, user, channel1, true, /*has DND access*/ true);
1240 mHelper.createNotificationChannel(PKG_N_MR1, user, channel2, true, true);
1241 mHelper.createNotificationChannel(PKG_N_MR1, user, channel3, true, true);
1242 assertEquals(3, mHelper.getNotificationChannelsBypassingDnd(PKG_N_MR1,
1243 user).getList().size());
1244
1245 // block notifications from this app
1246 mHelper.setEnabled(PKG_N_MR1, user, false);
1247 assertEquals(0, mHelper.getNotificationChannelsBypassingDnd(PKG_N_MR1,
1248 user).getList().size());
1249
1250 // re-enable notifications from this app
1251 mHelper.setEnabled(PKG_N_MR1, user, true);
1252 assertEquals(3, mHelper.getNotificationChannelsBypassingDnd(PKG_N_MR1,
1253 user).getList().size());
1254
1255 // setBypassDnd false for some channels
1256 channel1.setBypassDnd(false);
1257 channel2.setBypassDnd(false);
1258 assertEquals(1, mHelper.getNotificationChannelsBypassingDnd(PKG_N_MR1,
1259 user).getList().size());
1260
1261 // setBypassDnd false for rest of the channels
1262 channel3.setBypassDnd(false);
1263 assertEquals(0, mHelper.getNotificationChannelsBypassingDnd(PKG_N_MR1,
1264 user).getList().size());
1265 }
1266
1267 @Test
1268 public void testGetAppsBypassingDndCount_noAppsBypassing() throws Exception {
1269 assertEquals(0, mHelper.getAppsBypassingDndCount(USER.getIdentifier()));
1270 }
1271
1272 @Test
1273 public void testGetAppsBypassingDndCount_noAppsForUserIdBypassing() throws Exception {
1274 int user = 9;
1275 NotificationChannel channel = new NotificationChannel("id", "name",
1276 NotificationManager.IMPORTANCE_MAX);
1277 channel.setBypassDnd(true);
1278 mHelper.createNotificationChannel(PKG_N_MR1, 111, channel, true, true);
1279
1280 assertEquals(0, mHelper.getAppsBypassingDndCount(user));
1281 }
1282
1283 @Test
1284 public void testGetAppsBypassingDndCount_oneChannelBypassing_groupBlocked() {
1285 int user = USER.getIdentifier();
1286 NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1");
1287 NotificationChannel channel1 = new NotificationChannel("id1", "name1",
1288 NotificationManager.IMPORTANCE_MAX);
1289 channel1.setBypassDnd(true);
1290 channel1.setGroup(ncg.getId());
1291 mHelper.createNotificationChannelGroup(PKG_N_MR1, user, ncg, /* fromTargetApp */ true);
1292 mHelper.createNotificationChannel(PKG_N_MR1, user, channel1, true, /*has DND access*/ true);
1293
1294 assertEquals(1, mHelper.getAppsBypassingDndCount(user));
1295
1296 // disable group
1297 ncg.setBlocked(true);
1298 mHelper.createNotificationChannelGroup(PKG_N_MR1, user, ncg, /* fromTargetApp */ false);
1299 assertEquals(0, mHelper.getAppsBypassingDndCount(user));
1300 }
1301
1302 @Test
1303 public void testGetAppsBypassingDndCount_oneAppBypassing() {
1304 int user = USER.getIdentifier();
1305 NotificationChannel channel1 = new NotificationChannel("id1", "name1",
1306 NotificationManager.IMPORTANCE_MAX);
1307 NotificationChannel channel2 = new NotificationChannel("id2", "name2",
1308 NotificationManager.IMPORTANCE_MAX);
1309 NotificationChannel channel3 = new NotificationChannel("id3", "name3",
1310 NotificationManager.IMPORTANCE_MAX);
1311 channel1.setBypassDnd(true);
1312 channel2.setBypassDnd(true);
1313 channel3.setBypassDnd(true);
1314 // has DND access, so can set bypassDnd attribute
1315 mHelper.createNotificationChannel(PKG_N_MR1, user, channel1, true, /*has DND access*/ true);
1316 mHelper.createNotificationChannel(PKG_N_MR1, user, channel2, true, true);
1317 mHelper.createNotificationChannel(PKG_N_MR1, user, channel3, true, true);
1318 assertEquals(1, mHelper.getAppsBypassingDndCount(user));
1319
1320 // block notifications from this app
1321 mHelper.setEnabled(PKG_N_MR1, user, false);
1322 assertEquals(0, mHelper.getAppsBypassingDndCount(user)); // no apps can bypass dnd
1323
1324 // re-enable notifications from this app
1325 mHelper.setEnabled(PKG_N_MR1, user, true);
1326 assertEquals(1, mHelper.getAppsBypassingDndCount(user));
1327
1328 // setBypassDnd false for some channels
1329 channel1.setBypassDnd(false);
1330 channel2.setBypassDnd(false);
1331 assertEquals(1, mHelper.getAppsBypassingDndCount(user));
1332
1333 // setBypassDnd false for rest of the channels
1334 channel3.setBypassDnd(false);
1335 assertEquals(0, mHelper.getAppsBypassingDndCount(user));
1336 }
1337
1338 @Test
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001339 public void testCreateAndDeleteCanChannelsBypassDnd() throws Exception {
1340 // create notification channel that can't bypass dnd
1341 // expected result: areChannelsBypassingDnd = false
1342 // setNotificationPolicy isn't called since areChannelsBypassingDnd was already false
1343 NotificationChannel channel = new NotificationChannel("id1", "name1", IMPORTANCE_LOW);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001344 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001345 assertFalse(mHelper.areChannelsBypassingDnd());
1346 verify(mMockZenModeHelper, never()).setNotificationPolicy(any());
1347 resetZenModeHelper();
1348
1349 // create notification channel that can bypass dnd
1350 // expected result: areChannelsBypassingDnd = true
1351 NotificationChannel channel2 = new NotificationChannel("id2", "name2", IMPORTANCE_LOW);
1352 channel2.setBypassDnd(true);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001353 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, true, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001354 assertTrue(mHelper.areChannelsBypassingDnd());
1355 verify(mMockZenModeHelper, times(1)).setNotificationPolicy(any());
1356 resetZenModeHelper();
1357
1358 // delete channels
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001359 mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001360 assertTrue(mHelper.areChannelsBypassingDnd()); // channel2 can still bypass DND
1361 verify(mMockZenModeHelper, never()).setNotificationPolicy(any());
1362 resetZenModeHelper();
1363
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001364 mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2.getId());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001365 assertFalse(mHelper.areChannelsBypassingDnd());
1366 verify(mMockZenModeHelper, times(1)).setNotificationPolicy(any());
1367 resetZenModeHelper();
1368 }
1369
1370 @Test
1371 public void testUpdateCanChannelsBypassDnd() throws Exception {
1372 // create notification channel that can't bypass dnd
1373 // expected result: areChannelsBypassingDnd = false
1374 // setNotificationPolicy isn't called since areChannelsBypassingDnd was already false
1375 NotificationChannel channel = new NotificationChannel("id1", "name1", IMPORTANCE_LOW);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001376 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001377 assertFalse(mHelper.areChannelsBypassingDnd());
1378 verify(mMockZenModeHelper, never()).setNotificationPolicy(any());
1379 resetZenModeHelper();
1380
1381 // update channel so it CAN bypass dnd:
1382 // expected result: areChannelsBypassingDnd = true
1383 channel.setBypassDnd(true);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001384 mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001385 assertTrue(mHelper.areChannelsBypassingDnd());
1386 verify(mMockZenModeHelper, times(1)).setNotificationPolicy(any());
1387 resetZenModeHelper();
1388
1389 // update channel so it can't bypass dnd:
1390 // expected result: areChannelsBypassingDnd = false
1391 channel.setBypassDnd(false);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001392 mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001393 assertFalse(mHelper.areChannelsBypassingDnd());
1394 verify(mMockZenModeHelper, times(1)).setNotificationPolicy(any());
1395 resetZenModeHelper();
1396 }
1397
1398 @Test
1399 public void testSetupNewZenModeHelper_canBypass() {
1400 // start notification policy off with mAreChannelsBypassingDnd = true, but
1401 // RankingHelper should change to false
1402 mTestNotificationPolicy = new NotificationManager.Policy(0, 0, 0, 0,
1403 NotificationManager.Policy.STATE_CHANNELS_BYPASSING_DND);
1404 when(mMockZenModeHelper.getNotificationPolicy()).thenReturn(mTestNotificationPolicy);
1405 mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper);
1406 assertFalse(mHelper.areChannelsBypassingDnd());
1407 verify(mMockZenModeHelper, times(1)).setNotificationPolicy(any());
1408 resetZenModeHelper();
1409 }
1410
1411 @Test
1412 public void testSetupNewZenModeHelper_cannotBypass() {
1413 // start notification policy off with mAreChannelsBypassingDnd = false
1414 mTestNotificationPolicy = new NotificationManager.Policy(0, 0, 0, 0, 0);
1415 when(mMockZenModeHelper.getNotificationPolicy()).thenReturn(mTestNotificationPolicy);
1416 mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper);
1417 assertFalse(mHelper.areChannelsBypassingDnd());
1418 verify(mMockZenModeHelper, never()).setNotificationPolicy(any());
1419 resetZenModeHelper();
1420 }
1421
1422 @Test
1423 public void testCreateDeletedChannel() throws Exception {
1424 long[] vibration = new long[]{100, 67, 145, 156};
1425 NotificationChannel channel =
1426 new NotificationChannel("id2", "name2", IMPORTANCE_LOW);
1427 channel.setVibrationPattern(vibration);
1428
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001429 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false);
1430 mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001431
1432 NotificationChannel newChannel = new NotificationChannel(
1433 channel.getId(), channel.getName(), NotificationManager.IMPORTANCE_HIGH);
1434 newChannel.setVibrationPattern(new long[]{100});
1435
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001436 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, newChannel, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001437
1438 // No long deleted, using old settings
1439 compareChannels(channel,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001440 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, newChannel.getId(), false));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001441 }
1442
1443 @Test
1444 public void testOnlyHasDefaultChannel() throws Exception {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001445 assertTrue(mHelper.onlyHasDefaultChannel(PKG_N_MR1, UID_N_MR1));
1446 assertFalse(mHelper.onlyHasDefaultChannel(PKG_O, UID_O));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001447
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001448 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, getChannel(), true, false);
1449 assertFalse(mHelper.onlyHasDefaultChannel(PKG_N_MR1, UID_N_MR1));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001450 }
1451
1452 @Test
1453 public void testCreateChannel_defaultChannelId() throws Exception {
1454 try {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001455 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, new NotificationChannel(
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001456 NotificationChannel.DEFAULT_CHANNEL_ID, "ha", IMPORTANCE_HIGH), true, false);
1457 fail("Allowed to create default channel");
1458 } catch (IllegalArgumentException e) {
1459 // pass
1460 }
1461 }
1462
1463 @Test
1464 public void testCreateChannel_alreadyExists() throws Exception {
1465 long[] vibration = new long[]{100, 67, 145, 156};
1466 NotificationChannel channel =
1467 new NotificationChannel("id2", "name2", IMPORTANCE_LOW);
1468 channel.setVibrationPattern(vibration);
1469
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001470 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001471
1472 NotificationChannel newChannel = new NotificationChannel(
1473 channel.getId(), channel.getName(), NotificationManager.IMPORTANCE_HIGH);
1474 newChannel.setVibrationPattern(new long[]{100});
1475
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001476 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, newChannel, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001477
1478 // Old settings not overridden
1479 compareChannels(channel,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001480 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, newChannel.getId(), false));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001481 }
1482
1483 @Test
1484 public void testCreateChannel_noOverrideSound() throws Exception {
1485 Uri sound = new Uri.Builder().scheme("test").build();
1486 final NotificationChannel channel = new NotificationChannel("id2", "name2",
1487 NotificationManager.IMPORTANCE_DEFAULT);
1488 channel.setSound(sound, mAudioAttributes);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001489 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001490 assertEquals(sound, mHelper.getNotificationChannel(
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001491 PKG_N_MR1, UID_N_MR1, channel.getId(), false).getSound());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001492 }
1493
1494 @Test
1495 public void testPermanentlyDeleteChannels() throws Exception {
1496 NotificationChannel channel1 =
1497 new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH);
1498 NotificationChannel channel2 =
1499 new NotificationChannel("id2", "name2", IMPORTANCE_LOW);
1500
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001501 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false);
1502 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, false, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001503
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001504 mHelper.permanentlyDeleteNotificationChannels(PKG_N_MR1, UID_N_MR1);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001505
1506 // Only default channel remains
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001507 assertEquals(1, mHelper.getNotificationChannels(PKG_N_MR1, UID_N_MR1, true).getList().size());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001508 }
1509
1510 @Test
1511 public void testDeleteGroup() throws Exception {
1512 NotificationChannelGroup notDeleted = new NotificationChannelGroup("not", "deleted");
1513 NotificationChannelGroup deleted = new NotificationChannelGroup("totally", "deleted");
1514 NotificationChannel nonGroupedNonDeletedChannel =
1515 new NotificationChannel("no group", "so not deleted", IMPORTANCE_HIGH);
1516 NotificationChannel groupedButNotDeleted =
1517 new NotificationChannel("not deleted", "belongs to notDeleted", IMPORTANCE_DEFAULT);
1518 groupedButNotDeleted.setGroup("not");
1519 NotificationChannel groupedAndDeleted =
1520 new NotificationChannel("deleted", "belongs to deleted", IMPORTANCE_DEFAULT);
1521 groupedAndDeleted.setGroup("totally");
1522
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001523 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, notDeleted, true);
1524 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, deleted, true);
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04001525 mHelper.createNotificationChannel(
1526 PKG_N_MR1, UID_N_MR1, nonGroupedNonDeletedChannel, true, false);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001527 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, groupedAndDeleted, true, false);
1528 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, groupedButNotDeleted, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001529
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001530 mHelper.deleteNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, deleted.getId());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001531
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001532 assertNull(mHelper.getNotificationChannelGroup(deleted.getId(), PKG_N_MR1, UID_N_MR1));
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04001533 assertNotNull(
1534 mHelper.getNotificationChannelGroup(notDeleted.getId(), PKG_N_MR1, UID_N_MR1));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001535
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04001536 assertNull(mHelper.getNotificationChannel(
1537 PKG_N_MR1, UID_N_MR1, groupedAndDeleted.getId(), false));
1538 compareChannels(groupedAndDeleted, mHelper.getNotificationChannel(
1539 PKG_N_MR1, UID_N_MR1, groupedAndDeleted.getId(), true));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001540
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04001541 compareChannels(groupedButNotDeleted, mHelper.getNotificationChannel(
1542 PKG_N_MR1, UID_N_MR1, groupedButNotDeleted.getId(), false));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001543 compareChannels(nonGroupedNonDeletedChannel, mHelper.getNotificationChannel(
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001544 PKG_N_MR1, UID_N_MR1, nonGroupedNonDeletedChannel.getId(), false));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001545
1546 // notDeleted
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001547 assertEquals(1, mHelper.getNotificationChannelGroups(PKG_N_MR1, UID_N_MR1).size());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001548
1549 verify(mHandler, never()).requestSort();
1550 }
1551
1552 @Test
1553 public void testOnUserRemoved() throws Exception {
1554 int[] user0Uids = {98, 235, 16, 3782};
1555 int[] user1Uids = new int[user0Uids.length];
1556 for (int i = 0; i < user0Uids.length; i++) {
1557 user1Uids[i] = UserHandle.PER_USER_RANGE + user0Uids[i];
1558
1559 final ApplicationInfo legacy = new ApplicationInfo();
1560 legacy.targetSdkVersion = Build.VERSION_CODES.N_MR1;
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001561 when(mPm.getApplicationInfoAsUser(eq(PKG_N_MR1), anyInt(), anyInt())).thenReturn(legacy);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001562
1563 // create records with the default channel for all user 0 and user 1 uids
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001564 mHelper.getImportance(PKG_N_MR1, user0Uids[i]);
1565 mHelper.getImportance(PKG_N_MR1, user1Uids[i]);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001566 }
1567
1568 mHelper.onUserRemoved(1);
1569
1570 // user 0 records remain
1571 for (int i = 0; i < user0Uids.length; i++) {
1572 assertEquals(1,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001573 mHelper.getNotificationChannels(PKG_N_MR1, user0Uids[i], false).getList().size());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001574 }
1575 // user 1 records are gone
1576 for (int i = 0; i < user1Uids.length; i++) {
1577 assertEquals(0,
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001578 mHelper.getNotificationChannels(PKG_N_MR1, user1Uids[i], false).getList().size());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001579 }
1580 }
1581
1582 @Test
1583 public void testOnPackageChanged_packageRemoval() throws Exception {
1584 // Deleted
1585 NotificationChannel channel1 =
1586 new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001587 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001588
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001589 mHelper.onPackagesChanged(true, UserHandle.USER_SYSTEM, new String[]{PKG_N_MR1}, new int[]{
1590 UID_N_MR1});
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001591
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001592 assertEquals(0, mHelper.getNotificationChannels(PKG_N_MR1, UID_N_MR1, true).getList().size());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001593
1594 // Not deleted
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001595 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001596
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001597 mHelper.onPackagesChanged(false, UserHandle.USER_SYSTEM, new String[]{PKG_N_MR1}, new int[]{
1598 UID_N_MR1});
1599 assertEquals(2, mHelper.getNotificationChannels(PKG_N_MR1, UID_N_MR1, false).getList().size());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001600 }
1601
1602 @Test
1603 public void testOnPackageChanged_packageRemoval_importance() throws Exception {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001604 mHelper.setImportance(PKG_N_MR1, UID_N_MR1, NotificationManager.IMPORTANCE_HIGH);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001605
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001606 mHelper.onPackagesChanged(true, UserHandle.USER_SYSTEM, new String[]{PKG_N_MR1}, new int[]{
1607 UID_N_MR1});
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001608
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001609 assertEquals(NotificationManager.IMPORTANCE_UNSPECIFIED, mHelper.getImportance(PKG_N_MR1,
1610 UID_N_MR1));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001611 }
1612
1613 @Test
1614 public void testOnPackageChanged_packageRemoval_groups() throws Exception {
1615 NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1");
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001616 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001617 NotificationChannelGroup ncg2 = new NotificationChannelGroup("group2", "name2");
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001618 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg2, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001619
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001620 mHelper.onPackagesChanged(true, UserHandle.USER_SYSTEM, new String[]{PKG_N_MR1}, new int[]{
1621 UID_N_MR1});
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001622
Julia Reynolds13ed28b2018-09-21 15:20:13 -04001623 assertEquals(0, mHelper.getNotificationChannelGroups(
1624 PKG_N_MR1, UID_N_MR1, true, true, false).getList().size());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001625 }
1626
1627 @Test
1628 public void testOnPackageChange_downgradeTargetSdk() throws Exception {
1629 // create channel as api 26
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001630 mHelper.createNotificationChannel(PKG_O, UID_O, getChannel(), true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001631
1632 // install new app version targeting 25
1633 final ApplicationInfo legacy = new ApplicationInfo();
1634 legacy.targetSdkVersion = Build.VERSION_CODES.N_MR1;
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001635 when(mPm.getApplicationInfoAsUser(eq(PKG_O), anyInt(), anyInt())).thenReturn(legacy);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001636 mHelper.onPackagesChanged(
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001637 false, UserHandle.USER_SYSTEM, new String[]{PKG_O}, new int[]{UID_O});
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001638
1639 // make sure the default channel was readded
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001640 //assertEquals(2, mHelper.getNotificationChannels(PKG_O, UID_O, false).getList().size());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001641 assertNotNull(mHelper.getNotificationChannel(
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001642 PKG_O, UID_O, NotificationChannel.DEFAULT_CHANNEL_ID, false));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001643 }
1644
1645 @Test
1646 public void testRecordDefaults() throws Exception {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001647 assertEquals(NotificationManager.IMPORTANCE_UNSPECIFIED, mHelper.getImportance(PKG_N_MR1,
1648 UID_N_MR1));
1649 assertEquals(true, mHelper.canShowBadge(PKG_N_MR1, UID_N_MR1));
1650 assertEquals(1, mHelper.getNotificationChannels(PKG_N_MR1, UID_N_MR1, false).getList().size());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001651 }
1652
1653 @Test
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04001654 public void testCreateGroup() {
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001655 NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1");
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001656 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg, true);
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04001657 assertEquals(ncg,
1658 mHelper.getNotificationChannelGroups(PKG_N_MR1, UID_N_MR1).iterator().next());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001659 verify(mHandler, never()).requestSort();
1660 }
1661
1662 @Test
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04001663 public void testCannotCreateChannel_badGroup() {
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001664 NotificationChannel channel1 =
1665 new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH);
1666 channel1.setGroup("garbage");
1667 try {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001668 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001669 fail("Created a channel with a bad group");
1670 } catch (IllegalArgumentException e) {
1671 }
1672 }
1673
1674 @Test
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04001675 public void testCannotCreateChannel_goodGroup() {
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001676 NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1");
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001677 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001678 NotificationChannel channel1 =
1679 new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH);
1680 channel1.setGroup(ncg.getId());
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001681 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001682
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04001683 assertEquals(ncg.getId(), mHelper.getNotificationChannel(
1684 PKG_N_MR1, UID_N_MR1, channel1.getId(), false).getGroup());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001685 }
1686
1687 @Test
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04001688 public void testGetChannelGroups() {
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001689 NotificationChannelGroup unused = new NotificationChannelGroup("unused", "s");
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001690 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, unused, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001691 NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1");
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001692 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001693 NotificationChannelGroup ncg2 = new NotificationChannelGroup("group2", "name2");
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001694 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg2, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001695
1696 NotificationChannel channel1 =
1697 new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH);
1698 channel1.setGroup(ncg.getId());
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001699 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001700 NotificationChannel channel1a =
1701 new NotificationChannel("id1a", "name1", NotificationManager.IMPORTANCE_HIGH);
1702 channel1a.setGroup(ncg.getId());
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001703 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1a, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001704
1705 NotificationChannel channel2 =
1706 new NotificationChannel("id2", "name1", NotificationManager.IMPORTANCE_HIGH);
1707 channel2.setGroup(ncg2.getId());
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001708 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel2, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001709
1710 NotificationChannel channel3 =
1711 new NotificationChannel("id3", "name1", NotificationManager.IMPORTANCE_HIGH);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001712 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel3, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001713
Julia Reynolds13ed28b2018-09-21 15:20:13 -04001714 List<NotificationChannelGroup> actual = mHelper.getNotificationChannelGroups(
1715 PKG_N_MR1, UID_N_MR1, true, true, false).getList();
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001716 assertEquals(3, actual.size());
1717 for (NotificationChannelGroup group : actual) {
1718 if (group.getId() == null) {
1719 assertEquals(2, group.getChannels().size()); // misc channel too
1720 assertTrue(channel3.getId().equals(group.getChannels().get(0).getId())
1721 || channel3.getId().equals(group.getChannels().get(1).getId()));
1722 } else if (group.getId().equals(ncg.getId())) {
1723 assertEquals(2, group.getChannels().size());
1724 if (group.getChannels().get(0).getId().equals(channel1.getId())) {
1725 assertTrue(group.getChannels().get(1).getId().equals(channel1a.getId()));
1726 } else if (group.getChannels().get(0).getId().equals(channel1a.getId())) {
1727 assertTrue(group.getChannels().get(1).getId().equals(channel1.getId()));
1728 } else {
1729 fail("expected channel not found");
1730 }
1731 } else if (group.getId().equals(ncg2.getId())) {
1732 assertEquals(1, group.getChannels().size());
1733 assertEquals(channel2.getId(), group.getChannels().get(0).getId());
1734 }
1735 }
1736 }
1737
1738 @Test
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04001739 public void testGetChannelGroups_noSideEffects() {
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001740 NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1");
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001741 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001742
1743 NotificationChannel channel1 =
1744 new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH);
1745 channel1.setGroup(ncg.getId());
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001746 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false);
Julia Reynolds13ed28b2018-09-21 15:20:13 -04001747 mHelper.getNotificationChannelGroups(PKG_N_MR1, UID_N_MR1, true, true, false).getList();
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001748
1749 channel1.setImportance(IMPORTANCE_LOW);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001750 mHelper.updateNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001751
Julia Reynolds13ed28b2018-09-21 15:20:13 -04001752 List<NotificationChannelGroup> actual = mHelper.getNotificationChannelGroups(
1753 PKG_N_MR1, UID_N_MR1, true, true, false).getList();
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001754
1755 assertEquals(2, actual.size());
1756 for (NotificationChannelGroup group : actual) {
1757 if (Objects.equals(group.getId(), ncg.getId())) {
1758 assertEquals(1, group.getChannels().size());
1759 }
1760 }
1761 }
1762
1763 @Test
Julia Reynolds13ed28b2018-09-21 15:20:13 -04001764 public void testGetChannelGroups_includeEmptyGroups() {
1765 NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1");
1766 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncg, true);
1767 NotificationChannelGroup ncgEmpty = new NotificationChannelGroup("group2", "name2");
1768 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, ncgEmpty, true);
1769
1770 NotificationChannel channel1 =
1771 new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH);
1772 channel1.setGroup(ncg.getId());
1773 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel1, true, false);
1774
1775 List<NotificationChannelGroup> actual = mHelper.getNotificationChannelGroups(
1776 PKG_N_MR1, UID_N_MR1, false, false, true).getList();
1777
1778 assertEquals(2, actual.size());
1779 for (NotificationChannelGroup group : actual) {
1780 if (Objects.equals(group.getId(), ncg.getId())) {
1781 assertEquals(1, group.getChannels().size());
1782 }
1783 if (Objects.equals(group.getId(), ncgEmpty.getId())) {
1784 assertEquals(0, group.getChannels().size());
1785 }
1786 }
1787 }
1788
1789 @Test
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04001790 public void testCreateChannel_updateName() {
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001791 NotificationChannel nc = new NotificationChannel("id", "hello", IMPORTANCE_DEFAULT);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001792 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, nc, true, false);
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04001793 NotificationChannel actual =
1794 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, "id", false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001795 assertEquals("hello", actual.getName());
1796
1797 nc = new NotificationChannel("id", "goodbye", IMPORTANCE_HIGH);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001798 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, nc, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001799
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001800 actual = mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, "id", false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001801 assertEquals("goodbye", actual.getName());
1802 assertEquals(IMPORTANCE_DEFAULT, actual.getImportance());
1803
1804 verify(mHandler, times(1)).requestSort();
1805 }
1806
1807 @Test
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04001808 public void testCreateChannel_addToGroup() {
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001809 NotificationChannelGroup group = new NotificationChannelGroup("group", "");
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001810 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, group, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001811 NotificationChannel nc = new NotificationChannel("id", "hello", IMPORTANCE_DEFAULT);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001812 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, nc, true, false);
Julia Reynoldsb6bd93d2018-10-24 09:22:38 -04001813 NotificationChannel actual =
1814 mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, "id", false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001815 assertNull(actual.getGroup());
1816
1817 nc = new NotificationChannel("id", "hello", IMPORTANCE_HIGH);
1818 nc.setGroup(group.getId());
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001819 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, nc, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001820
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001821 actual = mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, "id", false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001822 assertNotNull(actual.getGroup());
1823 assertEquals(IMPORTANCE_DEFAULT, actual.getImportance());
1824
1825 verify(mHandler, times(1)).requestSort();
1826 }
1827
1828 @Test
1829 public void testDumpChannelsJson() throws Exception {
1830 final ApplicationInfo upgrade = new ApplicationInfo();
1831 upgrade.targetSdkVersion = Build.VERSION_CODES.O;
1832 try {
1833 when(mPm.getApplicationInfoAsUser(
1834 anyString(), anyInt(), anyInt())).thenReturn(upgrade);
1835 } catch (PackageManager.NameNotFoundException e) {
1836 }
1837 ArrayMap<String, Integer> expectedChannels = new ArrayMap<>();
1838 int numPackages = ThreadLocalRandom.current().nextInt(1, 5);
1839 for (int i = 0; i < numPackages; i++) {
1840 String pkgName = "pkg" + i;
1841 int numChannels = ThreadLocalRandom.current().nextInt(1, 10);
1842 for (int j = 0; j < numChannels; j++) {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001843 mHelper.createNotificationChannel(pkgName, UID_N_MR1,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001844 new NotificationChannel("" + j, "a", IMPORTANCE_HIGH), true, false);
1845 }
1846 expectedChannels.put(pkgName, numChannels);
1847 }
1848
1849 // delete the first channel of the first package
1850 String pkg = expectedChannels.keyAt(0);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001851 mHelper.deleteNotificationChannel("pkg" + 0, UID_N_MR1, "0");
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001852 // dump should not include deleted channels
1853 int count = expectedChannels.get(pkg);
1854 expectedChannels.put(pkg, count - 1);
1855
1856 JSONArray actual = mHelper.dumpChannelsJson(new NotificationManagerService.DumpFilter());
1857 assertEquals(numPackages, actual.length());
1858 for (int i = 0; i < numPackages; i++) {
1859 JSONObject object = actual.getJSONObject(i);
1860 assertTrue(expectedChannels.containsKey(object.get("packageName")));
1861 assertEquals(expectedChannels.get(object.get("packageName")).intValue(),
1862 object.getInt("channelCount"));
1863 }
1864 }
1865
1866 @Test
1867 public void testBadgingOverrideTrue() throws Exception {
1868 Secure.putIntForUser(getContext().getContentResolver(),
1869 Secure.NOTIFICATION_BADGING, 1,
1870 USER.getIdentifier());
1871 mHelper.updateBadgingEnabled(); // would be called by settings observer
1872 assertTrue(mHelper.badgingEnabled(USER));
1873 }
1874
1875 @Test
1876 public void testBadgingOverrideFalse() throws Exception {
1877 Secure.putIntForUser(getContext().getContentResolver(),
1878 Secure.NOTIFICATION_BADGING, 0,
1879 USER.getIdentifier());
1880 mHelper.updateBadgingEnabled(); // would be called by settings observer
1881 assertFalse(mHelper.badgingEnabled(USER));
1882 }
1883
1884 @Test
1885 public void testBadgingForUserAll() throws Exception {
1886 try {
1887 mHelper.badgingEnabled(UserHandle.ALL);
1888 } catch (Exception e) {
1889 fail("just don't throw");
1890 }
1891 }
1892
1893 @Test
1894 public void testBadgingOverrideUserIsolation() throws Exception {
1895 Secure.putIntForUser(getContext().getContentResolver(),
1896 Secure.NOTIFICATION_BADGING, 0,
1897 USER.getIdentifier());
1898 Secure.putIntForUser(getContext().getContentResolver(),
1899 Secure.NOTIFICATION_BADGING, 1,
1900 USER2.getIdentifier());
1901 mHelper.updateBadgingEnabled(); // would be called by settings observer
1902 assertFalse(mHelper.badgingEnabled(USER));
1903 assertTrue(mHelper.badgingEnabled(USER2));
1904 }
1905
1906 @Test
Julia Reynolds4509ce72019-01-31 13:12:43 -05001907 public void testBubblesOverrideTrue() {
1908 Secure.putIntForUser(getContext().getContentResolver(),
1909 Secure.NOTIFICATION_BUBBLES, 1,
1910 USER.getIdentifier());
1911 mHelper.updateBubblesEnabled(); // would be called by settings observer
1912 assertTrue(mHelper.bubblesEnabled(USER));
1913 }
1914
1915 @Test
1916 public void testBubblesOverrideFalse() {
1917 Secure.putIntForUser(getContext().getContentResolver(),
1918 Secure.NOTIFICATION_BUBBLES, 0,
1919 USER.getIdentifier());
1920 mHelper.updateBubblesEnabled(); // would be called by settings observer
1921 assertFalse(mHelper.bubblesEnabled(USER));
1922 }
1923
1924 @Test
1925 public void testBubblesForUserAll() {
1926 try {
1927 mHelper.bubblesEnabled(UserHandle.ALL);
1928 } catch (Exception e) {
1929 fail("just don't throw");
1930 }
1931 }
1932
1933 @Test
1934 public void testBubblesOverrideUserIsolation() {
1935 Secure.putIntForUser(getContext().getContentResolver(),
1936 Secure.NOTIFICATION_BUBBLES, 0,
1937 USER.getIdentifier());
1938 Secure.putIntForUser(getContext().getContentResolver(),
1939 Secure.NOTIFICATION_BUBBLES, 1,
1940 USER2.getIdentifier());
1941 mHelper.updateBubblesEnabled(); // would be called by settings observer
1942 assertFalse(mHelper.bubblesEnabled(USER));
1943 assertTrue(mHelper.bubblesEnabled(USER2));
1944 }
1945
1946 @Test
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001947 public void testOnLocaleChanged_updatesDefaultChannels() throws Exception {
1948 String newLabel = "bananas!";
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001949 final NotificationChannel defaultChannel = mHelper.getNotificationChannel(PKG_N_MR1,
1950 UID_N_MR1,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001951 NotificationChannel.DEFAULT_CHANNEL_ID, false);
1952 assertFalse(newLabel.equals(defaultChannel.getName()));
1953
1954 Resources res = mock(Resources.class);
1955 when(mContext.getResources()).thenReturn(res);
1956 when(res.getString(com.android.internal.R.string.default_notification_channel_label))
1957 .thenReturn(newLabel);
1958
1959 mHelper.onLocaleChanged(mContext, USER.getIdentifier());
1960
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001961 assertEquals(newLabel, mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1,
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001962 NotificationChannel.DEFAULT_CHANNEL_ID, false).getName());
1963 }
1964
1965 @Test
1966 public void testIsGroupBlocked_noGroup() throws Exception {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001967 assertFalse(mHelper.isGroupBlocked(PKG_N_MR1, UID_N_MR1, null));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001968
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001969 assertFalse(mHelper.isGroupBlocked(PKG_N_MR1, UID_N_MR1, "non existent group"));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001970 }
1971
1972 @Test
1973 public void testIsGroupBlocked_notBlocked() throws Exception {
1974 NotificationChannelGroup group = new NotificationChannelGroup("id", "name");
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001975 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, group, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001976
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001977 assertFalse(mHelper.isGroupBlocked(PKG_N_MR1, UID_N_MR1, group.getId()));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001978 }
1979
1980 @Test
1981 public void testIsGroupBlocked_blocked() throws Exception {
1982 NotificationChannelGroup group = new NotificationChannelGroup("id", "name");
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001983 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, group, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001984 group.setBlocked(true);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001985 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, group, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001986
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001987 assertTrue(mHelper.isGroupBlocked(PKG_N_MR1, UID_N_MR1, group.getId()));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001988 }
1989
1990 @Test
1991 public void testIsGroup_appCannotResetBlock() throws Exception {
1992 NotificationChannelGroup group = new NotificationChannelGroup("id", "name");
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001993 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, group, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001994 NotificationChannelGroup group2 = group.clone();
1995 group2.setBlocked(true);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04001996 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, group2, false);
1997 assertTrue(mHelper.isGroupBlocked(PKG_N_MR1, UID_N_MR1, group.getId()));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04001998
1999 NotificationChannelGroup group3 = group.clone();
2000 group3.setBlocked(false);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04002001 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, group3, true);
2002 assertTrue(mHelper.isGroupBlocked(PKG_N_MR1, UID_N_MR1, group.getId()));
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04002003 }
2004
2005 @Test
2006 public void testGetNotificationChannelGroupWithChannels() throws Exception {
2007 NotificationChannelGroup group = new NotificationChannelGroup("group", "");
2008 NotificationChannelGroup other = new NotificationChannelGroup("something else", "");
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04002009 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, group, true);
2010 mHelper.createNotificationChannelGroup(PKG_N_MR1, UID_N_MR1, other, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04002011
2012 NotificationChannel a = new NotificationChannel("a", "a", IMPORTANCE_DEFAULT);
2013 a.setGroup(group.getId());
2014 NotificationChannel b = new NotificationChannel("b", "b", IMPORTANCE_DEFAULT);
2015 b.setGroup(other.getId());
2016 NotificationChannel c = new NotificationChannel("c", "c", IMPORTANCE_DEFAULT);
2017 c.setGroup(group.getId());
2018 NotificationChannel d = new NotificationChannel("d", "d", IMPORTANCE_DEFAULT);
2019
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04002020 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, a, true, false);
2021 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, b, true, false);
2022 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, c, true, false);
2023 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, d, true, false);
2024 mHelper.deleteNotificationChannel(PKG_N_MR1, UID_N_MR1, c.getId());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04002025
2026 NotificationChannelGroup retrieved = mHelper.getNotificationChannelGroupWithChannels(
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04002027 PKG_N_MR1, UID_N_MR1, group.getId(), true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04002028 assertEquals(2, retrieved.getChannels().size());
2029 compareChannels(a, findChannel(retrieved.getChannels(), a.getId()));
2030 compareChannels(c, findChannel(retrieved.getChannels(), c.getId()));
2031
2032 retrieved = mHelper.getNotificationChannelGroupWithChannels(
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04002033 PKG_N_MR1, UID_N_MR1, group.getId(), false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04002034 assertEquals(1, retrieved.getChannels().size());
2035 compareChannels(a, findChannel(retrieved.getChannels(), a.getId()));
2036 }
2037
2038 @Test
2039 public void testAndroidPkgCannotBypassDnd_creation() {
2040 NotificationChannel test = new NotificationChannel("A", "a", IMPORTANCE_LOW);
2041 test.setBypassDnd(true);
2042
2043 mHelper.createNotificationChannel(SYSTEM_PKG, SYSTEM_UID, test, true, false);
2044
2045 assertFalse(mHelper.getNotificationChannel(SYSTEM_PKG, SYSTEM_UID, "A", false)
2046 .canBypassDnd());
2047 }
2048
2049 @Test
2050 public void testDndPkgCanBypassDnd_creation() {
2051 NotificationChannel test = new NotificationChannel("A", "a", IMPORTANCE_LOW);
2052 test.setBypassDnd(true);
2053
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04002054 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, test, true, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04002055
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04002056 assertTrue(mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, "A", false).canBypassDnd());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04002057 }
2058
2059 @Test
2060 public void testNormalPkgCannotBypassDnd_creation() {
2061 NotificationChannel test = new NotificationChannel("A", "a", IMPORTANCE_LOW);
2062 test.setBypassDnd(true);
2063
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04002064 mHelper.createNotificationChannel(PKG_N_MR1, 1000, test, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04002065
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04002066 assertFalse(mHelper.getNotificationChannel(PKG_N_MR1, 1000, "A", false).canBypassDnd());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04002067 }
2068
2069 @Test
2070 public void testAndroidPkgCannotBypassDnd_update() throws Exception {
2071 NotificationChannel test = new NotificationChannel("A", "a", IMPORTANCE_LOW);
2072 mHelper.createNotificationChannel(SYSTEM_PKG, SYSTEM_UID, test, true, false);
2073
2074 NotificationChannel update = new NotificationChannel("A", "a", IMPORTANCE_LOW);
2075 update.setBypassDnd(true);
2076 mHelper.createNotificationChannel(SYSTEM_PKG, SYSTEM_UID, update, true, false);
2077
2078 assertFalse(mHelper.getNotificationChannel(SYSTEM_PKG, SYSTEM_UID, "A", false)
2079 .canBypassDnd());
2080 }
2081
2082 @Test
2083 public void testDndPkgCanBypassDnd_update() throws Exception {
2084 NotificationChannel test = new NotificationChannel("A", "a", IMPORTANCE_LOW);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04002085 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, test, true, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04002086
2087 NotificationChannel update = new NotificationChannel("A", "a", IMPORTANCE_LOW);
2088 update.setBypassDnd(true);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04002089 mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, update, true, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04002090
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04002091 assertTrue(mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, "A", false).canBypassDnd());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04002092 }
2093
2094 @Test
2095 public void testNormalPkgCannotBypassDnd_update() {
2096 NotificationChannel test = new NotificationChannel("A", "a", IMPORTANCE_LOW);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04002097 mHelper.createNotificationChannel(PKG_N_MR1, 1000, test, true, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04002098 NotificationChannel update = new NotificationChannel("A", "a", IMPORTANCE_LOW);
2099 update.setBypassDnd(true);
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04002100 mHelper.createNotificationChannel(PKG_N_MR1, 1000, update, true, false);
2101 assertFalse(mHelper.getNotificationChannel(PKG_N_MR1, 1000, "A", false).canBypassDnd());
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04002102 }
2103
2104 @Test
2105 public void testGetBlockedAppCount_noApps() {
2106 assertEquals(0, mHelper.getBlockedAppCount(0));
2107 }
2108
2109 @Test
2110 public void testGetBlockedAppCount_noAppsForUserId() {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04002111 mHelper.setEnabled(PKG_N_MR1, 100, false);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04002112 assertEquals(0, mHelper.getBlockedAppCount(9));
2113 }
2114
2115 @Test
2116 public void testGetBlockedAppCount_appsForUserId() {
Aaron Heuckroth9ae59f82018-07-13 12:23:36 -04002117 mHelper.setEnabled(PKG_N_MR1, 1020, false);
2118 mHelper.setEnabled(PKG_N_MR1, 1030, false);
2119 mHelper.setEnabled(PKG_N_MR1, 1060, false);
2120 mHelper.setEnabled(PKG_N_MR1, 1000, true);
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04002121 assertEquals(3, mHelper.getBlockedAppCount(0));
2122 }
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04002123
2124 @Test
Julia Reynolds12ad7ca2019-01-28 09:29:16 -05002125 public void testXml_statusBarIcons_default() throws Exception {
Annie Meng8b646fd2019-02-01 18:46:42 +00002126 ByteArrayOutputStream baos = writeXmlAndPurge(PKG_O, UID_O, false, UserHandle.USER_ALL);
Julia Reynolds12ad7ca2019-01-28 09:29:16 -05002127 mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper);
Annie Meng8b646fd2019-02-01 18:46:42 +00002128 loadStreamXml(baos, false, UserHandle.USER_ALL);
Julia Reynolds12ad7ca2019-01-28 09:29:16 -05002129
2130 assertEquals(PreferencesHelper.DEFAULT_HIDE_SILENT_STATUS_BAR_ICONS,
2131 mHelper.shouldHideSilentStatusIcons());
2132 }
2133
2134 @Test
2135 public void testXml_statusBarIcons() throws Exception {
2136 mHelper.setHideSilentStatusIcons(!PreferencesHelper.DEFAULT_HIDE_SILENT_STATUS_BAR_ICONS);
2137
Annie Meng8b646fd2019-02-01 18:46:42 +00002138 ByteArrayOutputStream baos = writeXmlAndPurge(PKG_O, UID_O, false, UserHandle.USER_ALL);
Julia Reynolds12ad7ca2019-01-28 09:29:16 -05002139 mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper);
Annie Meng8b646fd2019-02-01 18:46:42 +00002140 loadStreamXml(baos, false, UserHandle.USER_ALL);
Julia Reynolds12ad7ca2019-01-28 09:29:16 -05002141
2142 assertEquals(!PreferencesHelper.DEFAULT_HIDE_SILENT_STATUS_BAR_ICONS,
2143 mHelper.shouldHideSilentStatusIcons());
2144 }
2145
2146 @Test
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04002147 public void testSetNotificationDelegate() {
2148 mHelper.setNotificationDelegate(PKG_O, UID_O, "other", 53);
2149 assertEquals("other", mHelper.getNotificationDelegate(PKG_O, UID_O));
2150 }
2151
2152 @Test
2153 public void testRevokeNotificationDelegate() {
2154 mHelper.setNotificationDelegate(PKG_O, UID_O, "other", 53);
2155 mHelper.revokeNotificationDelegate(PKG_O, UID_O);
2156
2157 assertNull(mHelper.getNotificationDelegate(PKG_O, UID_O));
2158 }
2159
2160 @Test
2161 public void testRevokeNotificationDelegate_noDelegateExistsNoCrash() {
2162 mHelper.revokeNotificationDelegate(PKG_O, UID_O);
2163
2164 assertNull(mHelper.getNotificationDelegate(PKG_O, UID_O));
2165 }
2166
2167 @Test
2168 public void testToggleNotificationDelegate() {
2169 mHelper.setNotificationDelegate(PKG_O, UID_O, "other", 53);
2170 mHelper.toggleNotificationDelegate(PKG_O, UID_O, false);
2171
2172 assertNull(mHelper.getNotificationDelegate(PKG_O, UID_O));
2173
2174 mHelper.toggleNotificationDelegate(PKG_O, UID_O, true);
2175 assertEquals("other", mHelper.getNotificationDelegate(PKG_O, UID_O));
2176 }
2177
2178 @Test
2179 public void testToggleNotificationDelegate_noDelegateExistsNoCrash() {
2180 mHelper.toggleNotificationDelegate(PKG_O, UID_O, false);
2181 assertNull(mHelper.getNotificationDelegate(PKG_O, UID_O));
2182
2183 mHelper.toggleNotificationDelegate(PKG_O, UID_O, true);
2184 assertNull(mHelper.getNotificationDelegate(PKG_O, UID_O));
2185 }
2186
2187 @Test
2188 public void testIsDelegateAllowed_noSource() {
2189 assertFalse(mHelper.isDelegateAllowed("does not exist", -1, "whatever", 0));
2190 }
2191
2192 @Test
2193 public void testIsDelegateAllowed_noDelegate() {
2194 mHelper.setImportance(PKG_O, UID_O, IMPORTANCE_UNSPECIFIED);
2195
2196 assertFalse(mHelper.isDelegateAllowed(PKG_O, UID_O, "whatever", 0));
2197 }
2198
2199 @Test
2200 public void testIsDelegateAllowed_delegateDisabledByApp() {
2201 mHelper.setNotificationDelegate(PKG_O, UID_O, "other", 53);
2202 mHelper.revokeNotificationDelegate(PKG_O, UID_O);
2203
2204 assertFalse(mHelper.isDelegateAllowed(PKG_O, UID_O, "other", 53));
2205 }
2206
2207 @Test
2208 public void testIsDelegateAllowed_wrongDelegate() {
2209 mHelper.setNotificationDelegate(PKG_O, UID_O, "other", 53);
2210 mHelper.revokeNotificationDelegate(PKG_O, UID_O);
2211
2212 assertFalse(mHelper.isDelegateAllowed(PKG_O, UID_O, "banana", 27));
2213 }
2214
2215 @Test
2216 public void testIsDelegateAllowed_delegateDisabledByUser() {
2217 mHelper.setNotificationDelegate(PKG_O, UID_O, "other", 53);
2218 mHelper.toggleNotificationDelegate(PKG_O, UID_O, false);
2219
2220 assertFalse(mHelper.isDelegateAllowed(PKG_O, UID_O, "other", 53));
2221 }
2222
2223 @Test
2224 public void testIsDelegateAllowed() {
2225 mHelper.setNotificationDelegate(PKG_O, UID_O, "other", 53);
2226
2227 assertTrue(mHelper.isDelegateAllowed(PKG_O, UID_O, "other", 53));
2228 }
2229
2230 @Test
2231 public void testDelegateXml_noDelegate() throws Exception {
2232 mHelper.setImportance(PKG_O, UID_O, IMPORTANCE_UNSPECIFIED);
2233
Annie Meng8b646fd2019-02-01 18:46:42 +00002234 ByteArrayOutputStream baos = writeXmlAndPurge(PKG_O, UID_O, false, UserHandle.USER_ALL);
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04002235 mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper);
Annie Meng8b646fd2019-02-01 18:46:42 +00002236 loadStreamXml(baos, false, UserHandle.USER_ALL);
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04002237
2238 assertNull(mHelper.getNotificationDelegate(PKG_O, UID_O));
2239 }
2240
2241 @Test
2242 public void testDelegateXml_delegate() throws Exception {
2243 mHelper.setNotificationDelegate(PKG_O, UID_O, "other", 53);
2244
Annie Meng8b646fd2019-02-01 18:46:42 +00002245 ByteArrayOutputStream baos = writeXmlAndPurge(PKG_O, UID_O, false, UserHandle.USER_ALL);
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04002246 mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper);
Annie Meng8b646fd2019-02-01 18:46:42 +00002247 loadStreamXml(baos, false, UserHandle.USER_ALL);
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04002248
2249 assertEquals("other", mHelper.getNotificationDelegate(PKG_O, UID_O));
2250 }
2251
2252 @Test
2253 public void testDelegateXml_disabledDelegate() throws Exception {
2254 mHelper.setNotificationDelegate(PKG_O, UID_O, "other", 53);
2255 mHelper.revokeNotificationDelegate(PKG_O, UID_O);
2256
Annie Meng8b646fd2019-02-01 18:46:42 +00002257 ByteArrayOutputStream baos = writeXmlAndPurge(PKG_O, UID_O, false, UserHandle.USER_ALL);
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04002258 mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper);
Annie Meng8b646fd2019-02-01 18:46:42 +00002259 loadStreamXml(baos, false, UserHandle.USER_ALL);
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04002260
2261 assertNull(mHelper.getNotificationDelegate(PKG_O, UID_O));
2262 }
2263
2264 @Test
2265 public void testDelegateXml_userDisabledDelegate() throws Exception {
2266 mHelper.setNotificationDelegate(PKG_O, UID_O, "other", 53);
2267 mHelper.toggleNotificationDelegate(PKG_O, UID_O, false);
2268
Annie Meng8b646fd2019-02-01 18:46:42 +00002269 ByteArrayOutputStream baos = writeXmlAndPurge(PKG_O, UID_O, false, UserHandle.USER_ALL);
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04002270 mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper);
Annie Meng8b646fd2019-02-01 18:46:42 +00002271 loadStreamXml(baos, false, UserHandle.USER_ALL);
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04002272
2273 // appears disabled
2274 assertNull(mHelper.getNotificationDelegate(PKG_O, UID_O));
2275
2276 // but was loaded and can be toggled back on
2277 mHelper.toggleNotificationDelegate(PKG_O, UID_O, true);
2278 assertEquals("other", mHelper.getNotificationDelegate(PKG_O, UID_O));
2279 }
2280
2281 @Test
2282 public void testDelegateXml_entirelyDisabledDelegate() throws Exception {
2283 mHelper.setNotificationDelegate(PKG_O, UID_O, "other", 53);
2284 mHelper.toggleNotificationDelegate(PKG_O, UID_O, false);
2285 mHelper.revokeNotificationDelegate(PKG_O, UID_O);
2286
Annie Meng8b646fd2019-02-01 18:46:42 +00002287 ByteArrayOutputStream baos = writeXmlAndPurge(PKG_O, UID_O, false, UserHandle.USER_ALL);
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04002288 mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper);
Annie Meng8b646fd2019-02-01 18:46:42 +00002289 loadStreamXml(baos, false, UserHandle.USER_ALL);
Julia Reynoldsa7ba45a2018-08-29 09:07:52 -04002290
2291 // appears disabled
2292 assertNull(mHelper.getNotificationDelegate(PKG_O, UID_O));
2293
2294 mHelper.setNotificationDelegate(PKG_O, UID_O, "other", 53);
2295 assertNull(mHelper.getNotificationDelegate(PKG_O, UID_O));
2296
2297 mHelper.toggleNotificationDelegate(PKG_O, UID_O, true);
2298 assertEquals("other", mHelper.getNotificationDelegate(PKG_O, UID_O));
2299 }
Julia Reynolds33ab8a02018-12-17 16:19:52 -05002300
2301 @Test
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002302 public void testAllowBubbles_defaults() throws Exception {
Mady Mellor9db685a2019-01-23 13:23:37 -08002303 assertTrue(mHelper.areBubblesAllowed(PKG_O, UID_O));
Julia Reynolds33ab8a02018-12-17 16:19:52 -05002304
Annie Meng8b646fd2019-02-01 18:46:42 +00002305 ByteArrayOutputStream baos = writeXmlAndPurge(PKG_O, UID_O, false, UserHandle.USER_ALL);
Julia Reynolds33ab8a02018-12-17 16:19:52 -05002306 mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper);
Annie Meng8b646fd2019-02-01 18:46:42 +00002307 loadStreamXml(baos, false, UserHandle.USER_ALL);
Julia Reynolds33ab8a02018-12-17 16:19:52 -05002308
Mady Mellor9db685a2019-01-23 13:23:37 -08002309 assertTrue(mHelper.areBubblesAllowed(PKG_O, UID_O));
Julia Reynolds33ab8a02018-12-17 16:19:52 -05002310 assertEquals(0, mHelper.getAppLockedFields(PKG_O, UID_O));
2311 }
2312
2313 @Test
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002314 public void testAllowBubbles_xml() throws Exception {
2315 mHelper.setBubblesAllowed(PKG_O, UID_O, false);
Mady Mellor9db685a2019-01-23 13:23:37 -08002316 assertFalse(mHelper.areBubblesAllowed(PKG_O, UID_O));
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002317 assertEquals(PreferencesHelper.LockableAppFields.USER_LOCKED_BUBBLE,
Julia Reynolds33ab8a02018-12-17 16:19:52 -05002318 mHelper.getAppLockedFields(PKG_O, UID_O));
2319
Annie Meng8b646fd2019-02-01 18:46:42 +00002320 ByteArrayOutputStream baos = writeXmlAndPurge(PKG_O, UID_O, false, UserHandle.USER_ALL);
Julia Reynolds33ab8a02018-12-17 16:19:52 -05002321 mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper);
Annie Meng8b646fd2019-02-01 18:46:42 +00002322 loadStreamXml(baos, false, UserHandle.USER_ALL);
Julia Reynolds33ab8a02018-12-17 16:19:52 -05002323
Mady Mellor9db685a2019-01-23 13:23:37 -08002324 assertFalse(mHelper.areBubblesAllowed(PKG_O, UID_O));
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002325 assertEquals(PreferencesHelper.LockableAppFields.USER_LOCKED_BUBBLE,
Julia Reynolds33ab8a02018-12-17 16:19:52 -05002326 mHelper.getAppLockedFields(PKG_O, UID_O));
2327 }
Julia Reynolds413ba842019-01-11 10:38:08 -05002328
2329 @Test
2330 public void testLockChannelsForOEM_emptyList() {
2331 mHelper.lockChannelsForOEM(null);
2332 mHelper.lockChannelsForOEM(new String[0]);
2333 // no exception
2334 }
2335
2336 @Test
2337 public void testLockChannelsForOEM_appWide() {
2338 NotificationChannel a = new NotificationChannel("a", "a", IMPORTANCE_HIGH);
2339 NotificationChannel b = new NotificationChannel("b", "b", IMPORTANCE_LOW);
2340 NotificationChannel c = new NotificationChannel("c", "c", IMPORTANCE_DEFAULT);
2341 // different uids, same package
2342 mHelper.createNotificationChannel(PKG_O, 3, a, true, false);
2343 mHelper.createNotificationChannel(PKG_O, 3, b, false, false);
2344 mHelper.createNotificationChannel(PKG_O, 30, c, true, true);
2345
2346 mHelper.lockChannelsForOEM(new String[] {PKG_O});
2347
2348 assertTrue(mHelper.getNotificationChannel(PKG_O, 3, a.getId(), false)
2349 .isImportanceLockedByOEM());
2350 assertTrue(mHelper.getNotificationChannel(PKG_O, 3, b.getId(), false)
2351 .isImportanceLockedByOEM());
2352 assertTrue(mHelper.getNotificationChannel(PKG_O, 30, c.getId(), false)
2353 .isImportanceLockedByOEM());
2354 }
2355
2356 @Test
2357 public void testLockChannelsForOEM_onlyGivenPkg() {
2358 NotificationChannel a = new NotificationChannel("a", "a", IMPORTANCE_HIGH);
2359 NotificationChannel b = new NotificationChannel("b", "b", IMPORTANCE_LOW);
2360 mHelper.createNotificationChannel(PKG_O, 3, a, true, false);
2361 mHelper.createNotificationChannel(PKG_N_MR1, 30, b, false, false);
2362
2363 mHelper.lockChannelsForOEM(new String[] {PKG_O});
2364
2365 assertTrue(mHelper.getNotificationChannel(PKG_O, 3, a.getId(), false)
2366 .isImportanceLockedByOEM());
2367 assertFalse(mHelper.getNotificationChannel(PKG_N_MR1, 30, b.getId(), false)
2368 .isImportanceLockedByOEM());
2369 }
2370
2371 @Test
2372 public void testLockChannelsForOEM_channelSpecific() {
2373 NotificationChannel a = new NotificationChannel("a", "a", IMPORTANCE_HIGH);
2374 NotificationChannel b = new NotificationChannel("b", "b", IMPORTANCE_LOW);
2375 NotificationChannel c = new NotificationChannel("c", "c", IMPORTANCE_DEFAULT);
2376 // different uids, same package
2377 mHelper.createNotificationChannel(PKG_O, 3, a, true, false);
2378 mHelper.createNotificationChannel(PKG_O, 3, b, false, false);
2379 mHelper.createNotificationChannel(PKG_O, 30, c, true, true);
2380
2381 mHelper.lockChannelsForOEM(new String[] {PKG_O + ":b", PKG_O + ":c"});
2382
2383 assertFalse(mHelper.getNotificationChannel(PKG_O, 3, a.getId(), false)
2384 .isImportanceLockedByOEM());
2385 assertTrue(mHelper.getNotificationChannel(PKG_O, 3, b.getId(), false)
2386 .isImportanceLockedByOEM());
2387 assertTrue(mHelper.getNotificationChannel(PKG_O, 30, c.getId(), false)
2388 .isImportanceLockedByOEM());
2389 }
2390
2391 @Test
2392 public void testLockChannelsForOEM_channelDoesNotExistYet_appWide() {
2393 NotificationChannel a = new NotificationChannel("a", "a", IMPORTANCE_HIGH);
2394 NotificationChannel b = new NotificationChannel("b", "b", IMPORTANCE_LOW);
2395 mHelper.createNotificationChannel(PKG_O, 3, a, true, false);
2396
2397 mHelper.lockChannelsForOEM(new String[] {PKG_O});
2398
2399 assertTrue(mHelper.getNotificationChannel(PKG_O, 3, a.getId(), false)
2400 .isImportanceLockedByOEM());
2401
2402 mHelper.createNotificationChannel(PKG_O, 3, b, true, false);
2403 assertTrue(mHelper.getNotificationChannel(PKG_O, 3, b.getId(), false)
2404 .isImportanceLockedByOEM());
2405 }
2406
2407 @Test
2408 public void testLockChannelsForOEM_channelDoesNotExistYet_channelSpecific() {
2409 NotificationChannel a = new NotificationChannel("a", "a", IMPORTANCE_HIGH);
2410 NotificationChannel b = new NotificationChannel("b", "b", IMPORTANCE_LOW);
2411 mHelper.createNotificationChannel(PKG_O, UID_O, a, true, false);
2412
2413 mHelper.lockChannelsForOEM(new String[] {PKG_O + ":a", PKG_O + ":b"});
2414
2415 assertTrue(mHelper.getNotificationChannel(PKG_O, UID_O, a.getId(), false)
2416 .isImportanceLockedByOEM());
2417
2418 mHelper.createNotificationChannel(PKG_O, UID_O, b, true, false);
2419 assertTrue(mHelper.getNotificationChannel(PKG_O, UID_O, b.getId(), false)
2420 .isImportanceLockedByOEM());
2421 }
2422
2423 @Test
2424 public void testUpdateNotificationChannel_oemLockedImportance() {
2425 NotificationChannel a = new NotificationChannel("a", "a", IMPORTANCE_HIGH);
2426 mHelper.createNotificationChannel(PKG_O, UID_O, a, true, false);
2427
2428 mHelper.lockChannelsForOEM(new String[] {PKG_O});
2429
2430 NotificationChannel update = new NotificationChannel("a", "a", IMPORTANCE_NONE);
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002431 update.setAllowBubbles(false);
Julia Reynolds413ba842019-01-11 10:38:08 -05002432
2433 mHelper.updateNotificationChannel(PKG_O, UID_O, update, true);
2434
2435 assertEquals(IMPORTANCE_HIGH,
2436 mHelper.getNotificationChannel(PKG_O, UID_O, a.getId(), false).getImportance());
2437 assertEquals(false,
Mady Mellorc39b4ae2019-01-09 17:11:37 -08002438 mHelper.getNotificationChannel(PKG_O, UID_O, a.getId(), false).canBubble());
Julia Reynolds413ba842019-01-11 10:38:08 -05002439
2440 mHelper.updateNotificationChannel(PKG_O, UID_O, update, true);
2441
2442 assertEquals(IMPORTANCE_HIGH,
2443 mHelper.getNotificationChannel(PKG_O, UID_O, a.getId(), false).getImportance());
2444 }
Aaron Heuckrothe5bec152018-07-09 16:26:09 -04002445}