blob: bda6b8a50a3daf25a081a57b6fcfbeb9133ae4db [file] [log] [blame]
Chris Wren1031c972014-07-23 13:11:45 +00001/*
2 * Copyright (C) 2014 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
Julia Reynoldsd373d782017-03-03 13:32:57 -050018import static android.app.NotificationManager.IMPORTANCE_DEFAULT;
Julia Reynolds03fa85d2017-03-06 15:14:50 -050019import static android.app.NotificationManager.IMPORTANCE_HIGH;
Julia Reynolds74856c42017-02-08 14:47:23 -050020import static android.app.NotificationManager.IMPORTANCE_LOW;
Julia Reynolds3e50bf62017-05-24 16:33:32 -040021import static android.app.NotificationManager.IMPORTANCE_MAX;
Julia Reynoldsd8c0ac42017-04-23 13:11:40 -040022import static android.app.NotificationManager.IMPORTANCE_NONE;
Julia Reynoldsfa04e142017-04-23 13:32:01 -040023import static android.app.NotificationManager.IMPORTANCE_UNSPECIFIED;
Julia Reynolds74856c42017-02-08 14:47:23 -050024
Julia Reynolds4036e8d2017-01-13 09:50:05 -050025import static junit.framework.Assert.assertNull;
Julia Reynolds32c97ef2016-11-28 10:47:18 -050026import static junit.framework.Assert.fail;
27
Julia Reynolds3d5b3c72017-11-07 09:04:58 -050028import 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.times;
39import static org.mockito.Mockito.verify;
40import static org.mockito.Mockito.when;
41
Chris Wren1031c972014-07-23 13:11:45 +000042import android.app.Notification;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -040043import android.app.NotificationChannel;
Bernardo Rufinoc27bb6a2017-10-03 13:55:10 +010044import android.app.NotificationChannelGroup;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -040045import android.app.NotificationManager;
Bernardo Rufinoc27bb6a2017-10-03 13:55:10 +010046import android.content.ContentProvider;
47import android.content.Context;
48import android.content.IContentProvider;
Julia Reynolds85769912016-10-25 09:08:57 -040049import android.content.pm.ApplicationInfo;
Julia Reynoldsbcc62ea2018-03-19 10:26:32 -040050import android.content.pm.PackageInfo;
Julia Reynolds85769912016-10-25 09:08:57 -040051import android.content.pm.PackageManager;
Julia Reynoldsbcc62ea2018-03-19 10:26:32 -040052import android.content.pm.Signature;
Julia Reynolds816797a2017-08-11 15:47:09 -040053import android.content.res.Resources;
Julia Reynolds529e3322017-02-06 08:33:01 -050054import android.graphics.Color;
Julia Reynolds619a69f2017-01-27 15:11:38 -050055import android.media.AudioAttributes;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -040056import android.net.Uri;
Julia Reynolds85769912016-10-25 09:08:57 -040057import android.os.Build;
Chris Wren1031c972014-07-23 13:11:45 +000058import android.os.UserHandle;
Bernardo Rufinoc27bb6a2017-10-03 13:55:10 +010059import android.provider.Settings;
Chris Wren89aa2262017-05-05 18:05:56 -040060import android.provider.Settings.Secure;
Chris Wren1031c972014-07-23 13:11:45 +000061import android.service.notification.StatusBarNotification;
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040062import android.support.test.InstrumentationRegistry;
63import android.support.test.runner.AndroidJUnit4;
Chris Wren1031c972014-07-23 13:11:45 +000064import android.test.suitebuilder.annotation.SmallTest;
Bernardo Rufinoc27bb6a2017-10-03 13:55:10 +010065import android.testing.TestableContentResolver;
Julia Reynoldsd373d782017-03-03 13:32:57 -050066import android.util.ArrayMap;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -040067import android.util.Xml;
Chris Wren1031c972014-07-23 13:11:45 +000068
Bernardo Rufinoc27bb6a2017-10-03 13:55:10 +010069import com.android.internal.util.FastXmlSerializer;
Jason Monk74f5e362017-12-06 08:56:33 -050070import com.android.server.UiServiceTestCase;
Bernardo Rufinoc27bb6a2017-10-03 13:55:10 +010071
72import org.json.JSONArray;
73import org.json.JSONObject;
74import org.junit.Before;
75import org.junit.Test;
76import org.junit.runner.RunWith;
77import org.mockito.Mock;
78import org.mockito.MockitoAnnotations;
79import org.xmlpull.v1.XmlPullParser;
80import org.xmlpull.v1.XmlSerializer;
81
Julia Reynoldsb5e44b72016-08-16 15:00:25 -040082import java.io.BufferedInputStream;
83import java.io.BufferedOutputStream;
84import java.io.ByteArrayInputStream;
85import java.io.ByteArrayOutputStream;
Chris Wren1031c972014-07-23 13:11:45 +000086import java.util.ArrayList;
Julia Reynoldsf57de462016-11-23 11:31:46 -050087import java.util.Arrays;
Julia Reynolds4036e8d2017-01-13 09:50:05 -050088import java.util.HashMap;
89import java.util.List;
90import java.util.Map;
Julia Reynolds74856c42017-02-08 14:47:23 -050091import java.util.Objects;
Julia Reynoldsd373d782017-03-03 13:32:57 -050092import java.util.concurrent.ThreadLocalRandom;
Chris Wren1031c972014-07-23 13:11:45 +000093
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -040094@SmallTest
95@RunWith(AndroidJUnit4.class)
Jason Monk74f5e362017-12-06 08:56:33 -050096public class RankingHelperTest extends UiServiceTestCase {
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -050097 private static final String PKG = "com.android.server.notification";
98 private static final int UID = 0;
Julia Reynolds5fe2eae2017-05-22 08:45:27 -040099 private static final UserHandle USER = UserHandle.of(0);
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500100 private static final String UPDATED_PKG = "updatedPkg";
Julia Reynolds5fe2eae2017-05-22 08:45:27 -0400101 private static final int UID2 = 1111;
Julia Reynoldsbcc62ea2018-03-19 10:26:32 -0400102 private static final String SYSTEM_PKG = "android";
103 private static final int SYSTEM_UID= 1000;
Julia Reynolds5fe2eae2017-05-22 08:45:27 -0400104 private static final UserHandle USER2 = UserHandle.of(10);
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500105 private static final String TEST_CHANNEL_ID = "test_channel_id";
Bernardo Rufinoc27bb6a2017-10-03 13:55:10 +0100106 private static final String TEST_AUTHORITY = "test";
107 private static final Uri SOUND_URI =
108 Uri.parse("content://" + TEST_AUTHORITY + "/internal/audio/media/10");
109 private static final Uri CANONICAL_SOUND_URI =
110 Uri.parse("content://" + TEST_AUTHORITY
111 + "/internal/audio/media/10?title=Test&canonical=1");
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500112
113 @Mock NotificationUsageStats mUsageStats;
114 @Mock RankingHandler mHandler;
115 @Mock PackageManager mPm;
Bernardo Rufinoc27bb6a2017-10-03 13:55:10 +0100116 @Mock IContentProvider mTestIContentProvider;
Julia Reynoldse0b25742017-05-08 12:55:24 -0400117 @Mock Context mContext;
Chris Wren1031c972014-07-23 13:11:45 +0000118
119 private Notification mNotiGroupGSortA;
120 private Notification mNotiGroupGSortB;
121 private Notification mNotiNoGroup;
122 private Notification mNotiNoGroup2;
123 private Notification mNotiNoGroupSortA;
124 private NotificationRecord mRecordGroupGSortA;
125 private NotificationRecord mRecordGroupGSortB;
126 private NotificationRecord mRecordNoGroup;
127 private NotificationRecord mRecordNoGroup2;
128 private NotificationRecord mRecordNoGroupSortA;
129 private RankingHelper mHelper;
Julia Reynolds619a69f2017-01-27 15:11:38 -0500130 private AudioAttributes mAudioAttributes;
Chris Wren1031c972014-07-23 13:11:45 +0000131
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400132 @Before
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500133 public void setUp() throws Exception {
Chris Wren5eab2b72015-06-16 13:56:22 -0400134 MockitoAnnotations.initMocks(this);
Chris Wren1031c972014-07-23 13:11:45 +0000135 UserHandle user = UserHandle.ALL;
136
Julia Reynolds85769912016-10-25 09:08:57 -0400137 final ApplicationInfo legacy = new ApplicationInfo();
138 legacy.targetSdkVersion = Build.VERSION_CODES.N_MR1;
Julia Reynolds1864ff52016-11-02 09:54:47 -0400139 final ApplicationInfo upgrade = new ApplicationInfo();
Dan Sandlere103b782017-05-17 16:07:56 -0700140 upgrade.targetSdkVersion = Build.VERSION_CODES.O;
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500141 when(mPm.getApplicationInfoAsUser(eq(PKG), anyInt(), anyInt())).thenReturn(legacy);
142 when(mPm.getApplicationInfoAsUser(eq(UPDATED_PKG), anyInt(), anyInt())).thenReturn(upgrade);
Julia Reynoldsbcc62ea2018-03-19 10:26:32 -0400143 when(mPm.getApplicationInfoAsUser(eq(SYSTEM_PKG), anyInt(), anyInt())).thenReturn(upgrade);
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500144 when(mPm.getPackageUidAsUser(eq(PKG), anyInt())).thenReturn(UID);
Julia Reynoldsf26eb912017-05-22 15:47:06 -0400145 when(mPm.getPackageUidAsUser(eq(UPDATED_PKG), anyInt())).thenReturn(UID2);
Julia Reynoldsbcc62ea2018-03-19 10:26:32 -0400146 when(mPm.getPackageUidAsUser(eq(SYSTEM_PKG), anyInt())).thenReturn(SYSTEM_UID);
147 PackageInfo info = mock(PackageInfo.class);
148 info.signatures = new Signature[] {mock(Signature.class)};
149 when(mPm.getPackageInfoAsUser(eq(SYSTEM_PKG), anyInt(), anyInt())).thenReturn(info);
150 when(mPm.getPackageInfoAsUser(eq(PKG), anyInt(), anyInt()))
151 .thenReturn(mock(PackageInfo.class));
Julia Reynoldse0b25742017-05-08 12:55:24 -0400152 when(mContext.getResources()).thenReturn(
153 InstrumentationRegistry.getContext().getResources());
Julia Reynolds816797a2017-08-11 15:47:09 -0400154 when(mContext.getContentResolver()).thenReturn(
155 InstrumentationRegistry.getContext().getContentResolver());
Julia Reynoldse0b25742017-05-08 12:55:24 -0400156 when(mContext.getPackageManager()).thenReturn(mPm);
157 when(mContext.getApplicationInfo()).thenReturn(legacy);
Chris Wren89aa2262017-05-05 18:05:56 -0400158 // most tests assume badging is enabled
Bernardo Rufinoc27bb6a2017-10-03 13:55:10 +0100159 TestableContentResolver contentResolver = getContext().getContentResolver();
160 contentResolver.setFallbackToExisting(false);
161 Secure.putIntForUser(contentResolver,
Chris Wren89aa2262017-05-05 18:05:56 -0400162 Secure.NOTIFICATION_BADGING, 1, UserHandle.getUserId(UID));
Julia Reynoldse0b25742017-05-08 12:55:24 -0400163
Bernardo Rufinoc27bb6a2017-10-03 13:55:10 +0100164 ContentProvider testContentProvider = mock(ContentProvider.class);
165 when(testContentProvider.getIContentProvider()).thenReturn(mTestIContentProvider);
166 contentResolver.addProvider(TEST_AUTHORITY, testContentProvider);
167
168 when(mTestIContentProvider.canonicalize(any(), eq(SOUND_URI)))
169 .thenReturn(CANONICAL_SOUND_URI);
170 when(mTestIContentProvider.canonicalize(any(), eq(CANONICAL_SOUND_URI)))
171 .thenReturn(CANONICAL_SOUND_URI);
172 when(mTestIContentProvider.uncanonicalize(any(), eq(CANONICAL_SOUND_URI)))
173 .thenReturn(SOUND_URI);
174
Julia Reynoldsc861a3d2018-02-15 10:34:49 -0500175 mHelper = new RankingHelper(getContext(), mPm, mHandler, mock(ZenModeHelper.class),
176 mUsageStats, new String[] {ImportanceExtractor.class.getName()});
Julia Reynoldse0b25742017-05-08 12:55:24 -0400177
178 mNotiGroupGSortA = new Notification.Builder(mContext, TEST_CHANNEL_ID)
179 .setContentTitle("A")
180 .setGroup("G")
181 .setSortKey("A")
182 .setWhen(1205)
183 .build();
184 mRecordGroupGSortA = new NotificationRecord(mContext, new StatusBarNotification(
185 PKG, PKG, 1, null, 0, 0, mNotiGroupGSortA, user,
186 null, System.currentTimeMillis()), getDefaultChannel());
187
188 mNotiGroupGSortB = new Notification.Builder(mContext, TEST_CHANNEL_ID)
189 .setContentTitle("B")
190 .setGroup("G")
191 .setSortKey("B")
192 .setWhen(1200)
193 .build();
194 mRecordGroupGSortB = new NotificationRecord(mContext, new StatusBarNotification(
195 PKG, PKG, 1, null, 0, 0, mNotiGroupGSortB, user,
196 null, System.currentTimeMillis()), getDefaultChannel());
197
198 mNotiNoGroup = new Notification.Builder(mContext, TEST_CHANNEL_ID)
199 .setContentTitle("C")
200 .setWhen(1201)
201 .build();
202 mRecordNoGroup = new NotificationRecord(mContext, new StatusBarNotification(
203 PKG, PKG, 1, null, 0, 0, mNotiNoGroup, user,
204 null, System.currentTimeMillis()), getDefaultChannel());
205
206 mNotiNoGroup2 = new Notification.Builder(mContext, TEST_CHANNEL_ID)
207 .setContentTitle("D")
208 .setWhen(1202)
209 .build();
210 mRecordNoGroup2 = new NotificationRecord(mContext, new StatusBarNotification(
211 PKG, PKG, 1, null, 0, 0, mNotiNoGroup2, user,
212 null, System.currentTimeMillis()), getDefaultChannel());
213
214 mNotiNoGroupSortA = new Notification.Builder(mContext, TEST_CHANNEL_ID)
215 .setContentTitle("E")
216 .setWhen(1201)
217 .setSortKey("A")
218 .build();
219 mRecordNoGroupSortA = new NotificationRecord(mContext, new StatusBarNotification(
220 PKG, PKG, 1, null, 0, 0, mNotiNoGroupSortA, user,
221 null, System.currentTimeMillis()), getDefaultChannel());
222
223 mAudioAttributes = new AudioAttributes.Builder()
224 .setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN)
225 .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
226 .setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED)
227 .build();
Chris Wren1031c972014-07-23 13:11:45 +0000228 }
229
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400230 private NotificationChannel getDefaultChannel() {
Julia Reynolds85769912016-10-25 09:08:57 -0400231 return new NotificationChannel(NotificationChannel.DEFAULT_CHANNEL_ID, "name",
Julia Reynolds74856c42017-02-08 14:47:23 -0500232 IMPORTANCE_LOW);
Julia Reynolds85769912016-10-25 09:08:57 -0400233 }
234
Julia Reynoldsd0a5b162017-03-18 13:42:09 -0400235 private ByteArrayOutputStream writeXmlAndPurge(String pkg, int uid, boolean forBackup,
236 String... channelIds)
Julia Reynolds85769912016-10-25 09:08:57 -0400237 throws Exception {
238 XmlSerializer serializer = new FastXmlSerializer();
239 ByteArrayOutputStream baos = new ByteArrayOutputStream();
240 serializer.setOutput(new BufferedOutputStream(baos), "utf-8");
241 serializer.startDocument(null, true);
Julia Reynoldsd0a5b162017-03-18 13:42:09 -0400242 mHelper.writeXml(serializer, forBackup);
Julia Reynolds85769912016-10-25 09:08:57 -0400243 serializer.endDocument();
244 serializer.flush();
Julia Reynolds85769912016-10-25 09:08:57 -0400245 for (String channelId : channelIds) {
Julia Reynolds4036e8d2017-01-13 09:50:05 -0500246 mHelper.permanentlyDeleteNotificationChannel(pkg, uid, channelId);
Julia Reynolds85769912016-10-25 09:08:57 -0400247 }
248 return baos;
249 }
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400250
Julia Reynolds5fe2eae2017-05-22 08:45:27 -0400251 private void loadStreamXml(ByteArrayOutputStream stream, boolean forRestore) throws Exception {
Bernardo Rufinoc27bb6a2017-10-03 13:55:10 +0100252 loadByteArrayXml(stream.toByteArray(), forRestore);
253 }
254
255 private void loadByteArrayXml(byte[] byteArray, boolean forRestore) throws Exception {
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500256 XmlPullParser parser = Xml.newPullParser();
Bernardo Rufinoc27bb6a2017-10-03 13:55:10 +0100257 parser.setInput(new BufferedInputStream(new ByteArrayInputStream(byteArray)), null);
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500258 parser.nextTag();
Julia Reynolds5fe2eae2017-05-22 08:45:27 -0400259 mHelper.readXml(parser, forRestore);
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500260 }
261
Julia Reynoldsf57de462016-11-23 11:31:46 -0500262 private void compareChannels(NotificationChannel expected, NotificationChannel actual) {
263 assertEquals(expected.getId(), actual.getId());
264 assertEquals(expected.getName(), actual.getName());
Julia Reynolds2c891c92017-03-17 14:23:47 -0400265 assertEquals(expected.getDescription(), actual.getDescription());
Julia Reynoldsf57de462016-11-23 11:31:46 -0500266 assertEquals(expected.shouldVibrate(), actual.shouldVibrate());
267 assertEquals(expected.shouldShowLights(), actual.shouldShowLights());
268 assertEquals(expected.getImportance(), actual.getImportance());
269 assertEquals(expected.getLockscreenVisibility(), actual.getLockscreenVisibility());
270 assertEquals(expected.getSound(), actual.getSound());
271 assertEquals(expected.canBypassDnd(), actual.canBypassDnd());
272 assertTrue(Arrays.equals(expected.getVibrationPattern(), actual.getVibrationPattern()));
Julia Reynolds59e152e2017-01-25 17:42:53 -0500273 assertEquals(expected.getGroup(), actual.getGroup());
Julia Reynolds619a69f2017-01-27 15:11:38 -0500274 assertEquals(expected.getAudioAttributes(), actual.getAudioAttributes());
Julia Reynolds529e3322017-02-06 08:33:01 -0500275 assertEquals(expected.getLightColor(), actual.getLightColor());
Julia Reynoldsf57de462016-11-23 11:31:46 -0500276 }
277
Julia Reynoldsd5286842017-03-02 15:37:10 -0500278 private void compareGroups(NotificationChannelGroup expected, NotificationChannelGroup actual) {
279 assertEquals(expected.getId(), actual.getId());
280 assertEquals(expected.getName(), actual.getName());
Julia Reynolds005c8b92017-08-24 10:35:53 -0400281 assertEquals(expected.getDescription(), actual.getDescription());
282 assertEquals(expected.isBlocked(), actual.isBlocked());
Julia Reynoldsd5286842017-03-02 15:37:10 -0500283 }
284
Julia Reynoldse0b25742017-05-08 12:55:24 -0400285 private NotificationChannel getChannel() {
286 return new NotificationChannel("id", "name", IMPORTANCE_LOW);
287 }
288
Julia Reynolds005c8b92017-08-24 10:35:53 -0400289 private NotificationChannel findChannel(List<NotificationChannel> channels, String id) {
290 for (NotificationChannel channel : channels) {
291 if (channel.getId().equals(id)) {
292 return channel;
293 }
294 }
295 return null;
296 }
297
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400298 @Test
Chris Wren1031c972014-07-23 13:11:45 +0000299 public void testFindAfterRankingWithASplitGroup() throws Exception {
300 ArrayList<NotificationRecord> notificationList = new ArrayList<NotificationRecord>(3);
301 notificationList.add(mRecordGroupGSortA);
302 notificationList.add(mRecordGroupGSortB);
303 notificationList.add(mRecordNoGroup);
304 notificationList.add(mRecordNoGroupSortA);
305 mHelper.sort(notificationList);
306 assertTrue(mHelper.indexOf(notificationList, mRecordGroupGSortA) >= 0);
307 assertTrue(mHelper.indexOf(notificationList, mRecordGroupGSortB) >= 0);
308 assertTrue(mHelper.indexOf(notificationList, mRecordNoGroup) >= 0);
309 assertTrue(mHelper.indexOf(notificationList, mRecordNoGroupSortA) >= 0);
310 }
311
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400312 @Test
Chris Wren1031c972014-07-23 13:11:45 +0000313 public void testSortShouldNotThrowWithPlainNotifications() throws Exception {
314 ArrayList<NotificationRecord> notificationList = new ArrayList<NotificationRecord>(2);
315 notificationList.add(mRecordNoGroup);
316 notificationList.add(mRecordNoGroup2);
317 mHelper.sort(notificationList);
318 }
319
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400320 @Test
Chris Wren1031c972014-07-23 13:11:45 +0000321 public void testSortShouldNotThrowOneSorted() throws Exception {
322 ArrayList<NotificationRecord> notificationList = new ArrayList<NotificationRecord>(2);
323 notificationList.add(mRecordNoGroup);
324 notificationList.add(mRecordNoGroupSortA);
325 mHelper.sort(notificationList);
326 }
327
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400328 @Test
Chris Wren1031c972014-07-23 13:11:45 +0000329 public void testSortShouldNotThrowOneNotification() throws Exception {
330 ArrayList<NotificationRecord> notificationList = new ArrayList<NotificationRecord>(1);
331 notificationList.add(mRecordNoGroup);
332 mHelper.sort(notificationList);
333 }
334
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400335 @Test
Chris Wren1031c972014-07-23 13:11:45 +0000336 public void testSortShouldNotThrowOneSortKey() throws Exception {
337 ArrayList<NotificationRecord> notificationList = new ArrayList<NotificationRecord>(1);
338 notificationList.add(mRecordGroupGSortB);
339 mHelper.sort(notificationList);
340 }
341
Geoffrey Pitsch0ffe7552016-08-29 10:07:40 -0400342 @Test
Chris Wren1031c972014-07-23 13:11:45 +0000343 public void testSortShouldNotThrowOnEmptyList() throws Exception {
344 ArrayList<NotificationRecord> notificationList = new ArrayList<NotificationRecord>();
345 mHelper.sort(notificationList);
346 }
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400347
348 @Test
349 public void testChannelXml() throws Exception {
Julia Reynolds1d97e6a2017-03-13 15:05:40 -0400350 NotificationChannelGroup ncg = new NotificationChannelGroup("1", "bye");
Julia Reynolds005c8b92017-08-24 10:35:53 -0400351 ncg.setBlocked(true);
352 ncg.setDescription("group desc");
Julia Reynoldsd5286842017-03-02 15:37:10 -0500353 NotificationChannelGroup ncg2 = new NotificationChannelGroup("2", "hello");
Julia Reynolds85769912016-10-25 09:08:57 -0400354 NotificationChannel channel1 =
355 new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH);
356 NotificationChannel channel2 =
Julia Reynolds1d97e6a2017-03-13 15:05:40 -0400357 new NotificationChannel("id2", "name2", IMPORTANCE_LOW);
Julia Reynolds2c891c92017-03-17 14:23:47 -0400358 channel2.setDescription("descriptions for all");
Julia Reynolds619a69f2017-01-27 15:11:38 -0500359 channel2.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes);
Julia Reynolds529e3322017-02-06 08:33:01 -0500360 channel2.enableLights(true);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400361 channel2.setBypassDnd(true);
362 channel2.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
Julia Reynoldsf57de462016-11-23 11:31:46 -0500363 channel2.enableVibration(true);
Julia Reynolds59e152e2017-01-25 17:42:53 -0500364 channel2.setGroup(ncg.getId());
Julia Reynolds619a69f2017-01-27 15:11:38 -0500365 channel2.setVibrationPattern(new long[]{100, 67, 145, 156});
Julia Reynolds529e3322017-02-06 08:33:01 -0500366 channel2.setLightColor(Color.BLUE);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400367
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500368 mHelper.createNotificationChannelGroup(PKG, UID, ncg, true);
369 mHelper.createNotificationChannelGroup(PKG, UID, ncg2, true);
Julia Reynolds1fe10942018-03-28 12:46:51 -0400370 mHelper.createNotificationChannel(PKG, UID, channel1, true, false);
371 mHelper.createNotificationChannel(PKG, UID, channel2, false, false);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400372
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500373 mHelper.setShowBadge(PKG, UID, true);
Rohan Shah590e1b22018-04-10 23:48:47 -0400374 mHelper.setAppImportanceLocked(PKG, UID);
Julia Reynolds924eed12017-01-19 09:52:07 -0500375
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500376 ByteArrayOutputStream baos = writeXmlAndPurge(PKG, UID, false, channel1.getId(),
Julia Reynoldsd0a5b162017-03-18 13:42:09 -0400377 channel2.getId(), NotificationChannel.DEFAULT_CHANNEL_ID);
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500378 mHelper.onPackagesChanged(true, UserHandle.myUserId(), new String[]{PKG}, new int[]{UID});
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400379
Julia Reynolds5fe2eae2017-05-22 08:45:27 -0400380 loadStreamXml(baos, false);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400381
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500382 assertTrue(mHelper.canShowBadge(PKG, UID));
Rohan Shah590e1b22018-04-10 23:48:47 -0400383 assertTrue(mHelper.getIsAppImportanceLocked(PKG, UID));
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500384 assertEquals(channel1, mHelper.getNotificationChannel(PKG, UID, channel1.getId(), false));
Julia Reynolds4036e8d2017-01-13 09:50:05 -0500385 compareChannels(channel2,
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500386 mHelper.getNotificationChannel(PKG, UID, channel2.getId(), false));
Julia Reynolds74856c42017-02-08 14:47:23 -0500387
388 List<NotificationChannelGroup> actualGroups =
Julia Reynolds3eb3ffd2017-11-16 10:11:32 -0500389 mHelper.getNotificationChannelGroups(PKG, UID, false, true).getList();
Julia Reynolds74856c42017-02-08 14:47:23 -0500390 boolean foundNcg = false;
391 for (NotificationChannelGroup actual : actualGroups) {
392 if (ncg.getId().equals(actual.getId())) {
393 foundNcg = true;
Julia Reynoldsd5286842017-03-02 15:37:10 -0500394 compareGroups(ncg, actual);
395 } else if (ncg2.getId().equals(actual.getId())) {
396 compareGroups(ncg2, actual);
Julia Reynolds74856c42017-02-08 14:47:23 -0500397 }
398 }
399 assertTrue(foundNcg);
400
401 boolean foundChannel2Group = false;
402 for (NotificationChannelGroup actual : actualGroups) {
403 if (channel2.getGroup().equals(actual.getChannels().get(0).getGroup())) {
404 foundChannel2Group = true;
405 break;
406 }
407 }
408 assertTrue(foundChannel2Group);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400409 }
Julia Reynolds85769912016-10-25 09:08:57 -0400410
411 @Test
Julia Reynolds5fe2eae2017-05-22 08:45:27 -0400412 public void testChannelXmlForBackup() throws Exception {
413 NotificationChannelGroup ncg = new NotificationChannelGroup("1", "bye");
414 NotificationChannelGroup ncg2 = new NotificationChannelGroup("2", "hello");
415 NotificationChannel channel1 =
416 new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH);
417 NotificationChannel channel2 =
418 new NotificationChannel("id2", "name2", IMPORTANCE_LOW);
419 channel2.setDescription("descriptions for all");
Bernardo Rufinoc27bb6a2017-10-03 13:55:10 +0100420 channel2.setSound(SOUND_URI, mAudioAttributes);
Julia Reynolds5fe2eae2017-05-22 08:45:27 -0400421 channel2.enableLights(true);
422 channel2.setBypassDnd(true);
423 channel2.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
424 channel2.enableVibration(false);
425 channel2.setGroup(ncg.getId());
426 channel2.setLightColor(Color.BLUE);
Julia Reynolds745c1542017-05-26 14:43:47 -0400427 NotificationChannel channel3 = new NotificationChannel("id3", "NAM3", IMPORTANCE_HIGH);
428 channel3.enableVibration(true);
Julia Reynolds5fe2eae2017-05-22 08:45:27 -0400429
430 mHelper.createNotificationChannelGroup(PKG, UID, ncg, true);
431 mHelper.createNotificationChannelGroup(PKG, UID, ncg2, true);
Julia Reynolds1fe10942018-03-28 12:46:51 -0400432 mHelper.createNotificationChannel(PKG, UID, channel1, true, false);
433 mHelper.createNotificationChannel(PKG, UID, channel2, false, false);
434 mHelper.createNotificationChannel(PKG, UID, channel3, false, false);
435 mHelper.createNotificationChannel(UPDATED_PKG, UID2, getChannel(), true, false);
Julia Reynolds5fe2eae2017-05-22 08:45:27 -0400436
437 mHelper.setShowBadge(PKG, UID, true);
438
439 mHelper.setImportance(UPDATED_PKG, UID2, IMPORTANCE_NONE);
440
441 ByteArrayOutputStream baos = writeXmlAndPurge(PKG, UID, true, channel1.getId(),
Julia Reynolds745c1542017-05-26 14:43:47 -0400442 channel2.getId(), channel3.getId(), NotificationChannel.DEFAULT_CHANNEL_ID);
443 mHelper.onPackagesChanged(true, UserHandle.myUserId(), new String[]{PKG, UPDATED_PKG},
444 new int[]{UID, UID2});
Julia Reynolds5fe2eae2017-05-22 08:45:27 -0400445
446 mHelper.setShowBadge(UPDATED_PKG, UID2, true);
447
448 loadStreamXml(baos, true);
449
450 assertEquals(IMPORTANCE_NONE, mHelper.getImportance(UPDATED_PKG, UID2));
451 assertTrue(mHelper.canShowBadge(PKG, UID));
452 assertEquals(channel1, mHelper.getNotificationChannel(PKG, UID, channel1.getId(), false));
453 compareChannels(channel2,
454 mHelper.getNotificationChannel(PKG, UID, channel2.getId(), false));
Julia Reynolds745c1542017-05-26 14:43:47 -0400455 compareChannels(channel3,
456 mHelper.getNotificationChannel(PKG, UID, channel3.getId(), false));
Julia Reynolds5fe2eae2017-05-22 08:45:27 -0400457
458 List<NotificationChannelGroup> actualGroups =
Julia Reynolds3eb3ffd2017-11-16 10:11:32 -0500459 mHelper.getNotificationChannelGroups(PKG, UID, false, true).getList();
Julia Reynolds5fe2eae2017-05-22 08:45:27 -0400460 boolean foundNcg = false;
461 for (NotificationChannelGroup actual : actualGroups) {
462 if (ncg.getId().equals(actual.getId())) {
463 foundNcg = true;
464 compareGroups(ncg, actual);
465 } else if (ncg2.getId().equals(actual.getId())) {
466 compareGroups(ncg2, actual);
467 }
468 }
469 assertTrue(foundNcg);
470
471 boolean foundChannel2Group = false;
472 for (NotificationChannelGroup actual : actualGroups) {
473 if (channel2.getGroup().equals(actual.getChannels().get(0).getGroup())) {
474 foundChannel2Group = true;
475 break;
476 }
477 }
478 assertTrue(foundChannel2Group);
479 }
480
481 @Test
Bernardo Rufinoc27bb6a2017-10-03 13:55:10 +0100482 public void testBackupXml_backupCanonicalizedSoundUri() throws Exception {
483 NotificationChannel channel =
484 new NotificationChannel("id", "name", IMPORTANCE_LOW);
485 channel.setSound(SOUND_URI, mAudioAttributes);
Julia Reynolds1fe10942018-03-28 12:46:51 -0400486 mHelper.createNotificationChannel(PKG, UID, channel, true, false);
Bernardo Rufinoc27bb6a2017-10-03 13:55:10 +0100487
488 ByteArrayOutputStream baos = writeXmlAndPurge(PKG, UID, true, channel.getId());
489
490 // Testing that in restore we are given the canonical version
491 loadStreamXml(baos, true);
492 verify(mTestIContentProvider).uncanonicalize(any(), eq(CANONICAL_SOUND_URI));
493 }
494
495 @Test
496 public void testRestoreXml_withExistentCanonicalizedSoundUri() throws Exception {
497 Uri localUri = Uri.parse("content://" + TEST_AUTHORITY + "/local/url");
498 Uri canonicalBasedOnLocal = localUri.buildUpon()
499 .appendQueryParameter("title", "Test")
500 .appendQueryParameter("canonical", "1")
501 .build();
502 when(mTestIContentProvider.canonicalize(any(), eq(CANONICAL_SOUND_URI)))
503 .thenReturn(canonicalBasedOnLocal);
504 when(mTestIContentProvider.uncanonicalize(any(), eq(CANONICAL_SOUND_URI)))
505 .thenReturn(localUri);
506 when(mTestIContentProvider.uncanonicalize(any(), eq(canonicalBasedOnLocal)))
507 .thenReturn(localUri);
508
509 NotificationChannel channel =
510 new NotificationChannel("id", "name", IMPORTANCE_LOW);
511 channel.setSound(SOUND_URI, mAudioAttributes);
Julia Reynolds1fe10942018-03-28 12:46:51 -0400512 mHelper.createNotificationChannel(PKG, UID, channel, true, false);
Bernardo Rufinoc27bb6a2017-10-03 13:55:10 +0100513 ByteArrayOutputStream baos = writeXmlAndPurge(PKG, UID, true, channel.getId());
514
515 loadStreamXml(baos, true);
516
517 NotificationChannel actualChannel = mHelper.getNotificationChannel(
518 PKG, UID, channel.getId(), false);
519 assertEquals(localUri, actualChannel.getSound());
520 }
521
522 @Test
523 public void testRestoreXml_withNonExistentCanonicalizedSoundUri() throws Exception {
524 Thread.sleep(3000);
525 when(mTestIContentProvider.canonicalize(any(), eq(CANONICAL_SOUND_URI)))
526 .thenReturn(null);
527 when(mTestIContentProvider.uncanonicalize(any(), eq(CANONICAL_SOUND_URI)))
528 .thenReturn(null);
529
530 NotificationChannel channel =
531 new NotificationChannel("id", "name", IMPORTANCE_LOW);
532 channel.setSound(SOUND_URI, mAudioAttributes);
Julia Reynolds1fe10942018-03-28 12:46:51 -0400533 mHelper.createNotificationChannel(PKG, UID, channel, true, false);
Bernardo Rufinoc27bb6a2017-10-03 13:55:10 +0100534 ByteArrayOutputStream baos = writeXmlAndPurge(PKG, UID, true, channel.getId());
535
536 loadStreamXml(baos, true);
537
538 NotificationChannel actualChannel = mHelper.getNotificationChannel(
539 PKG, UID, channel.getId(), false);
540 assertEquals(Settings.System.DEFAULT_NOTIFICATION_URI, actualChannel.getSound());
541 }
542
543
544 /**
545 * Although we don't make backups with uncanonicalized uris anymore, we used to, so we have to
546 * handle its restore properly.
547 */
548 @Test
549 public void testRestoreXml_withUncanonicalizedNonLocalSoundUri() throws Exception {
550 // Not a local uncanonicalized uri, simulating that it fails to exist locally
551 when(mTestIContentProvider.canonicalize(any(), eq(SOUND_URI))).thenReturn(null);
552 String id = "id";
553 String backupWithUncanonicalizedSoundUri = "<ranking version=\"1\">\n"
554 + "<package name=\"com.android.server.notification\" show_badge=\"true\">\n"
555 + "<channel id=\"" + id + "\" name=\"name\" importance=\"2\" "
556 + "sound=\"" + SOUND_URI + "\" "
557 + "usage=\"6\" content_type=\"0\" flags=\"1\" show_badge=\"true\" />\n"
558 + "<channel id=\"miscellaneous\" name=\"Uncategorized\" usage=\"5\" "
559 + "content_type=\"4\" flags=\"0\" show_badge=\"true\" />\n"
560 + "</package>\n"
561 + "</ranking>\n";
562
563 loadByteArrayXml(backupWithUncanonicalizedSoundUri.getBytes(), true);
564
565 NotificationChannel actualChannel = mHelper.getNotificationChannel(PKG, UID, id, false);
566 assertEquals(Settings.System.DEFAULT_NOTIFICATION_URI, actualChannel.getSound());
567 }
568
569 @Test
570 public void testBackupRestoreXml_withNullSoundUri() throws Exception {
571 NotificationChannel channel =
572 new NotificationChannel("id", "name", IMPORTANCE_LOW);
573 channel.setSound(null, mAudioAttributes);
Julia Reynolds1fe10942018-03-28 12:46:51 -0400574 mHelper.createNotificationChannel(PKG, UID, channel, true, false);
Bernardo Rufinoc27bb6a2017-10-03 13:55:10 +0100575 ByteArrayOutputStream baos = writeXmlAndPurge(PKG, UID, true, channel.getId());
576
577 loadStreamXml(baos, true);
578
579 NotificationChannel actualChannel = mHelper.getNotificationChannel(
580 PKG, UID, channel.getId(), false);
581 assertEquals(null, actualChannel.getSound());
582 }
583
584 @Test
Julia Reynoldsd0a5b162017-03-18 13:42:09 -0400585 public void testChannelXml_backup() throws Exception {
586 NotificationChannelGroup ncg = new NotificationChannelGroup("1", "bye");
587 NotificationChannelGroup ncg2 = new NotificationChannelGroup("2", "hello");
588 NotificationChannel channel1 =
589 new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH);
590 NotificationChannel channel2 =
591 new NotificationChannel("id2", "name2", IMPORTANCE_LOW);
592 NotificationChannel channel3 =
593 new NotificationChannel("id3", "name3", IMPORTANCE_LOW);
594 channel3.setGroup(ncg.getId());
595
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500596 mHelper.createNotificationChannelGroup(PKG, UID, ncg, true);
597 mHelper.createNotificationChannelGroup(PKG, UID, ncg2, true);
Julia Reynolds1fe10942018-03-28 12:46:51 -0400598 mHelper.createNotificationChannel(PKG, UID, channel1, true, false);
599 mHelper.createNotificationChannel(PKG, UID, channel2, false, false);
600 mHelper.createNotificationChannel(PKG, UID, channel3, true, false);
Julia Reynoldsd0a5b162017-03-18 13:42:09 -0400601
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500602 mHelper.deleteNotificationChannel(PKG, UID, channel1.getId());
603 mHelper.deleteNotificationChannelGroup(PKG, UID, ncg.getId());
604 assertEquals(channel2, mHelper.getNotificationChannel(PKG, UID, channel2.getId(), false));
Julia Reynoldsd0a5b162017-03-18 13:42:09 -0400605
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500606 ByteArrayOutputStream baos = writeXmlAndPurge(PKG, UID, true, channel1.getId(),
Julia Reynoldsd0a5b162017-03-18 13:42:09 -0400607 channel2.getId(), channel3.getId(), NotificationChannel.DEFAULT_CHANNEL_ID);
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500608 mHelper.onPackagesChanged(true, UserHandle.myUserId(), new String[]{PKG}, new int[]{UID});
Julia Reynoldsd0a5b162017-03-18 13:42:09 -0400609
610 XmlPullParser parser = Xml.newPullParser();
611 parser.setInput(new BufferedInputStream(new ByteArrayInputStream(baos.toByteArray())),
612 null);
613 parser.nextTag();
614 mHelper.readXml(parser, true);
615
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500616 assertNull(mHelper.getNotificationChannel(PKG, UID, channel1.getId(), false));
617 assertNull(mHelper.getNotificationChannel(PKG, UID, channel3.getId(), false));
618 assertNull(mHelper.getNotificationChannelGroup(ncg.getId(), PKG, UID));
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000619 //assertEquals(ncg2, mHelper.getNotificationChannelGroup(ncg2.getId(), PKG, UID));
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500620 assertEquals(channel2, mHelper.getNotificationChannel(PKG, UID, channel2.getId(), false));
Julia Reynoldsd0a5b162017-03-18 13:42:09 -0400621 }
622
623 @Test
Julia Reynolds85769912016-10-25 09:08:57 -0400624 public void testChannelXml_defaultChannelLegacyApp_noUserSettings() throws Exception {
Geoffrey Pitsch5644ccd2017-04-17 11:33:24 -0400625 ByteArrayOutputStream baos = writeXmlAndPurge(PKG, UID, false,
Julia Reynolds85769912016-10-25 09:08:57 -0400626 NotificationChannel.DEFAULT_CHANNEL_ID);
627
Julia Reynolds5fe2eae2017-05-22 08:45:27 -0400628 loadStreamXml(baos, false);
Julia Reynolds85769912016-10-25 09:08:57 -0400629
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500630 final NotificationChannel updated = mHelper.getNotificationChannel(PKG, UID,
631 NotificationChannel.DEFAULT_CHANNEL_ID, false);
Julia Reynolds85769912016-10-25 09:08:57 -0400632 assertEquals(NotificationManager.IMPORTANCE_UNSPECIFIED, updated.getImportance());
633 assertFalse(updated.canBypassDnd());
Julia Reynolds619a69f2017-01-27 15:11:38 -0500634 assertEquals(NotificationManager.VISIBILITY_NO_OVERRIDE, updated.getLockscreenVisibility());
Julia Reynolds85769912016-10-25 09:08:57 -0400635 assertEquals(0, updated.getUserLockedFields());
636 }
637
638 @Test
Julia Reynoldsb3a04f02016-11-04 09:08:52 -0400639 public void testChannelXml_defaultChannelUpdatedApp_userSettings() throws Exception {
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500640 final NotificationChannel defaultChannel = mHelper.getNotificationChannel(PKG, UID,
641 NotificationChannel.DEFAULT_CHANNEL_ID, false);
642 defaultChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
Julia Reynolds8617e4e2017-09-18 16:52:37 -0400643 mHelper.updateNotificationChannel(PKG, UID, defaultChannel, true);
Julia Reynoldsb3a04f02016-11-04 09:08:52 -0400644
Geoffrey Pitsch5644ccd2017-04-17 11:33:24 -0400645 ByteArrayOutputStream baos = writeXmlAndPurge(PKG, UID, false,
Julia Reynolds85769912016-10-25 09:08:57 -0400646 NotificationChannel.DEFAULT_CHANNEL_ID);
647
Julia Reynolds5fe2eae2017-05-22 08:45:27 -0400648 loadStreamXml(baos, false);
Julia Reynolds85769912016-10-25 09:08:57 -0400649
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500650 assertEquals(NotificationManager.IMPORTANCE_LOW, mHelper.getNotificationChannel(
651 PKG, UID, NotificationChannel.DEFAULT_CHANNEL_ID, false).getImportance());
Julia Reynolds85769912016-10-25 09:08:57 -0400652 }
653
Julia Reynolds85769912016-10-25 09:08:57 -0400654 @Test
655 public void testChannelXml_upgradeCreateDefaultChannel() throws Exception {
Julia Reynolds1864ff52016-11-02 09:54:47 -0400656 final String preupgradeXml = "<ranking version=\"1\">\n"
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500657 + "<package name=\"" + PKG
658 + "\" importance=\"" + NotificationManager.IMPORTANCE_HIGH
Julia Reynolds619a69f2017-01-27 15:11:38 -0500659 + "\" priority=\"" + Notification.PRIORITY_MAX + "\" visibility=\""
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500660 + Notification.VISIBILITY_SECRET + "\"" +" uid=\"" + UID + "\" />\n"
661 + "<package name=\"" + UPDATED_PKG + "\" uid=\"" + UID2 + "\" visibility=\""
Julia Reynolds619a69f2017-01-27 15:11:38 -0500662 + Notification.VISIBILITY_PRIVATE + "\" />\n"
663 + "</ranking>";
Julia Reynolds85769912016-10-25 09:08:57 -0400664 XmlPullParser parser = Xml.newPullParser();
Julia Reynolds1864ff52016-11-02 09:54:47 -0400665 parser.setInput(new BufferedInputStream(new ByteArrayInputStream(preupgradeXml.getBytes())),
Julia Reynolds619a69f2017-01-27 15:11:38 -0500666 null);
Julia Reynolds85769912016-10-25 09:08:57 -0400667 parser.nextTag();
668 mHelper.readXml(parser, false);
669
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500670 final NotificationChannel updated1 =
671 mHelper.getNotificationChannel(PKG, UID, NotificationChannel.DEFAULT_CHANNEL_ID, false);
Julia Reynolds1864ff52016-11-02 09:54:47 -0400672 assertEquals(NotificationManager.IMPORTANCE_HIGH, updated1.getImportance());
673 assertTrue(updated1.canBypassDnd());
674 assertEquals(Notification.VISIBILITY_SECRET, updated1.getLockscreenVisibility());
Julia Reynolds85769912016-10-25 09:08:57 -0400675 assertEquals(NotificationChannel.USER_LOCKED_IMPORTANCE
Julia Reynolds619a69f2017-01-27 15:11:38 -0500676 | NotificationChannel.USER_LOCKED_PRIORITY
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500677 | NotificationChannel.USER_LOCKED_VISIBILITY,
678 updated1.getUserLockedFields());
Julia Reynolds1864ff52016-11-02 09:54:47 -0400679
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500680 // No Default Channel created for updated packages
Geoffrey Pitsch5644ccd2017-04-17 11:33:24 -0400681 assertEquals(null, mHelper.getNotificationChannel(UPDATED_PKG, UID2,
682 NotificationChannel.DEFAULT_CHANNEL_ID, false));
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500683 }
684
685 @Test
686 public void testChannelXml_upgradeDeletesDefaultChannel() throws Exception {
687 final NotificationChannel defaultChannel = mHelper.getNotificationChannel(
688 PKG, UID, NotificationChannel.DEFAULT_CHANNEL_ID, false);
689 assertTrue(defaultChannel != null);
690 ByteArrayOutputStream baos =
691 writeXmlAndPurge(PKG, UID, false, NotificationChannel.DEFAULT_CHANNEL_ID);
692 // Load package at higher sdk.
693 final ApplicationInfo upgraded = new ApplicationInfo();
694 upgraded.targetSdkVersion = Build.VERSION_CODES.N_MR1 + 1;
695 when(mPm.getApplicationInfoAsUser(eq(PKG), anyInt(), anyInt())).thenReturn(upgraded);
Julia Reynolds5fe2eae2017-05-22 08:45:27 -0400696 loadStreamXml(baos, false);
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500697
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500698 // Default Channel should be gone.
Geoffrey Pitsch5644ccd2017-04-17 11:33:24 -0400699 assertEquals(null, mHelper.getNotificationChannel(PKG, UID,
700 NotificationChannel.DEFAULT_CHANNEL_ID, false));
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500701 }
702
703 @Test
704 public void testDeletesDefaultChannelAfterChannelIsCreated() throws Exception {
705 mHelper.createNotificationChannel(PKG, UID,
Julia Reynolds1fe10942018-03-28 12:46:51 -0400706 new NotificationChannel("bananas", "bananas", IMPORTANCE_LOW), true, false);
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500707 ByteArrayOutputStream baos = writeXmlAndPurge(PKG, UID, false,
708 NotificationChannel.DEFAULT_CHANNEL_ID, "bananas");
709
710 // Load package at higher sdk.
711 final ApplicationInfo upgraded = new ApplicationInfo();
712 upgraded.targetSdkVersion = Build.VERSION_CODES.N_MR1 + 1;
713 when(mPm.getApplicationInfoAsUser(eq(PKG), anyInt(), anyInt())).thenReturn(upgraded);
Julia Reynolds5fe2eae2017-05-22 08:45:27 -0400714 loadStreamXml(baos, false);
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500715
716 // Default Channel should be gone.
717 assertEquals(null, mHelper.getNotificationChannel(PKG, UID,
718 NotificationChannel.DEFAULT_CHANNEL_ID, false));
Julia Reynolds85769912016-10-25 09:08:57 -0400719 }
720
Julia Reynolds85769912016-10-25 09:08:57 -0400721 @Test
Geoffrey Pitsch6eccf002017-04-05 12:33:59 -0400722 public void testLoadingOldChannelsDoesNotDeleteNewlyCreatedChannels() throws Exception {
723 ByteArrayOutputStream baos = writeXmlAndPurge(PKG, UID, false,
724 NotificationChannel.DEFAULT_CHANNEL_ID, "bananas");
725 mHelper.createNotificationChannel(PKG, UID,
Julia Reynolds1fe10942018-03-28 12:46:51 -0400726 new NotificationChannel("bananas", "bananas", IMPORTANCE_LOW), true, false);
Geoffrey Pitsch6eccf002017-04-05 12:33:59 -0400727
Julia Reynolds5fe2eae2017-05-22 08:45:27 -0400728 loadStreamXml(baos, false);
Geoffrey Pitsch6eccf002017-04-05 12:33:59 -0400729
730 // Should still have the newly created channel that wasn't in the xml.
731 assertTrue(mHelper.getNotificationChannel(PKG, UID, "bananas", false) != null);
732 }
733
734 @Test
Julia Reynolds32c97ef2016-11-28 10:47:18 -0500735 public void testCreateChannel_blocked() throws Exception {
Julia Reynoldsd8c0ac42017-04-23 13:11:40 -0400736 mHelper.setImportance(PKG, UID, IMPORTANCE_NONE);
Julia Reynolds32c97ef2016-11-28 10:47:18 -0500737
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500738 mHelper.createNotificationChannel(PKG, UID,
Julia Reynolds1fe10942018-03-28 12:46:51 -0400739 new NotificationChannel("bananas", "bananas", IMPORTANCE_LOW), true, false);
Julia Reynolds32c97ef2016-11-28 10:47:18 -0500740 }
741
Julia Reynoldsd8c0ac42017-04-23 13:11:40 -0400742 @Test
Julia Reynolds3e50bf62017-05-24 16:33:32 -0400743 public void testCreateChannel_badImportance() throws Exception {
Julia Reynoldsd8c0ac42017-04-23 13:11:40 -0400744 try {
745 mHelper.createNotificationChannel(PKG, UID,
Julia Reynolds1fe10942018-03-28 12:46:51 -0400746 new NotificationChannel("bananas", "bananas", IMPORTANCE_NONE - 1),
747 true, false);
Julia Reynolds3e50bf62017-05-24 16:33:32 -0400748 fail("Was allowed to create a channel with invalid importance");
Julia Reynoldsd8c0ac42017-04-23 13:11:40 -0400749 } catch (IllegalArgumentException e) {
750 // yay
751 }
Julia Reynolds3e50bf62017-05-24 16:33:32 -0400752 try {
753 mHelper.createNotificationChannel(PKG, UID,
Julia Reynolds1fe10942018-03-28 12:46:51 -0400754 new NotificationChannel("bananas", "bananas", IMPORTANCE_UNSPECIFIED),
755 true, false);
Julia Reynolds3e50bf62017-05-24 16:33:32 -0400756 fail("Was allowed to create a channel with invalid importance");
757 } catch (IllegalArgumentException e) {
758 // yay
759 }
760 try {
761 mHelper.createNotificationChannel(PKG, UID,
Julia Reynolds1fe10942018-03-28 12:46:51 -0400762 new NotificationChannel("bananas", "bananas", IMPORTANCE_MAX + 1),
763 true, false);
Julia Reynolds3e50bf62017-05-24 16:33:32 -0400764 fail("Was allowed to create a channel with invalid importance");
765 } catch (IllegalArgumentException e) {
766 // yay
767 }
768 mHelper.createNotificationChannel(PKG, UID,
Julia Reynolds1fe10942018-03-28 12:46:51 -0400769 new NotificationChannel("bananas", "bananas", IMPORTANCE_NONE), true, false);
Julia Reynolds3e50bf62017-05-24 16:33:32 -0400770 mHelper.createNotificationChannel(PKG, UID,
Julia Reynolds1fe10942018-03-28 12:46:51 -0400771 new NotificationChannel("bananas", "bananas", IMPORTANCE_MAX), true, false);
Julia Reynoldsd8c0ac42017-04-23 13:11:40 -0400772 }
773
Julia Reynoldsbaff4002016-12-15 11:34:26 -0500774
775 @Test
Julia Reynolds85769912016-10-25 09:08:57 -0400776 public void testUpdate() throws Exception {
777 // no fields locked by user
778 final NotificationChannel channel =
Julia Reynolds74856c42017-02-08 14:47:23 -0500779 new NotificationChannel("id2", "name2", IMPORTANCE_LOW);
Julia Reynolds619a69f2017-01-27 15:11:38 -0500780 channel.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes);
Julia Reynolds529e3322017-02-06 08:33:01 -0500781 channel.enableLights(true);
Julia Reynolds85769912016-10-25 09:08:57 -0400782 channel.setBypassDnd(true);
783 channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
784
Julia Reynolds1fe10942018-03-28 12:46:51 -0400785 mHelper.createNotificationChannel(PKG, UID, channel, false, false);
Julia Reynolds85769912016-10-25 09:08:57 -0400786
787 // same id, try to update all fields
788 final NotificationChannel channel2 =
789 new NotificationChannel("id2", "name2", NotificationManager.IMPORTANCE_HIGH);
Julia Reynolds619a69f2017-01-27 15:11:38 -0500790 channel2.setSound(new Uri.Builder().scheme("test2").build(), mAudioAttributes);
Julia Reynolds529e3322017-02-06 08:33:01 -0500791 channel2.enableLights(false);
Julia Reynolds85769912016-10-25 09:08:57 -0400792 channel2.setBypassDnd(false);
793 channel2.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
794
Julia Reynolds8617e4e2017-09-18 16:52:37 -0400795 mHelper.updateNotificationChannel(PKG, UID, channel2, true);
Julia Reynolds85769912016-10-25 09:08:57 -0400796
797 // all fields should be changed
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500798 assertEquals(channel2, mHelper.getNotificationChannel(PKG, UID, channel.getId(), false));
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400799
800 verify(mHandler, times(1)).requestSort();
Julia Reynolds85769912016-10-25 09:08:57 -0400801 }
802
803 @Test
Julia Reynoldsfa04e142017-04-23 13:32:01 -0400804 public void testUpdate_preUpgrade_updatesAppFields() throws Exception {
805 mHelper.setImportance(PKG, UID, IMPORTANCE_UNSPECIFIED);
806 assertTrue(mHelper.canShowBadge(PKG, UID));
807 assertEquals(Notification.PRIORITY_DEFAULT, mHelper.getPackagePriority(PKG, UID));
808 assertEquals(NotificationManager.VISIBILITY_NO_OVERRIDE,
809 mHelper.getPackageVisibility(PKG, UID));
Rohan Shah590e1b22018-04-10 23:48:47 -0400810 assertFalse(mHelper.getIsAppImportanceLocked(PKG, UID));
Julia Reynoldsfa04e142017-04-23 13:32:01 -0400811
812 NotificationChannel defaultChannel = mHelper.getNotificationChannel(
813 PKG, UID, NotificationChannel.DEFAULT_CHANNEL_ID, false);
814
815 defaultChannel.setShowBadge(false);
816 defaultChannel.setImportance(IMPORTANCE_NONE);
817 defaultChannel.setBypassDnd(true);
818 defaultChannel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
819
Rohan Shah590e1b22018-04-10 23:48:47 -0400820 mHelper.setAppImportanceLocked(PKG, UID);
Julia Reynolds8617e4e2017-09-18 16:52:37 -0400821 mHelper.updateNotificationChannel(PKG, UID, defaultChannel, true);
Julia Reynoldsfa04e142017-04-23 13:32:01 -0400822
823 // ensure app level fields are changed
824 assertFalse(mHelper.canShowBadge(PKG, UID));
825 assertEquals(Notification.PRIORITY_MAX, mHelper.getPackagePriority(PKG, UID));
826 assertEquals(Notification.VISIBILITY_SECRET, mHelper.getPackageVisibility(PKG, UID));
827 assertEquals(IMPORTANCE_NONE, mHelper.getImportance(PKG, UID));
Rohan Shah590e1b22018-04-10 23:48:47 -0400828 assertTrue(mHelper.getIsAppImportanceLocked(PKG, UID));
Julia Reynoldsfa04e142017-04-23 13:32:01 -0400829 }
830
831 @Test
832 public void testUpdate_postUpgrade_noUpdateAppFields() throws Exception {
833 final NotificationChannel channel = new NotificationChannel("id2", "name2", IMPORTANCE_LOW);
834
Julia Reynolds1fe10942018-03-28 12:46:51 -0400835 mHelper.createNotificationChannel(PKG, UID, channel, false, false);
Julia Reynoldsfa04e142017-04-23 13:32:01 -0400836 assertTrue(mHelper.canShowBadge(PKG, UID));
837 assertEquals(Notification.PRIORITY_DEFAULT, mHelper.getPackagePriority(PKG, UID));
838 assertEquals(NotificationManager.VISIBILITY_NO_OVERRIDE,
839 mHelper.getPackageVisibility(PKG, UID));
840
841 channel.setShowBadge(false);
842 channel.setImportance(IMPORTANCE_NONE);
843 channel.setBypassDnd(true);
844 channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
845
Julia Reynolds8617e4e2017-09-18 16:52:37 -0400846 mHelper.updateNotificationChannel(PKG, UID, channel, true);
Julia Reynoldsfa04e142017-04-23 13:32:01 -0400847
848 // ensure app level fields are not changed
849 assertTrue(mHelper.canShowBadge(PKG, UID));
850 assertEquals(Notification.PRIORITY_DEFAULT, mHelper.getPackagePriority(PKG, UID));
851 assertEquals(NotificationManager.VISIBILITY_NO_OVERRIDE,
852 mHelper.getPackageVisibility(PKG, UID));
853 assertEquals(NotificationManager.IMPORTANCE_UNSPECIFIED, mHelper.getImportance(PKG, UID));
854 }
855
856 @Test
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500857 public void testGetNotificationChannel_ReturnsNullForUnknownChannel() throws Exception {
858 assertEquals(null, mHelper.getNotificationChannel(PKG, UID, "garbage", false));
Julia Reynolds85769912016-10-25 09:08:57 -0400859 }
Julia Reynoldsbaff4002016-12-15 11:34:26 -0500860
861 @Test
862 public void testCreateChannel_CannotChangeHiddenFields() throws Exception {
863 final NotificationChannel channel =
Julia Reynolds74856c42017-02-08 14:47:23 -0500864 new NotificationChannel("id2", "name2", IMPORTANCE_LOW);
Julia Reynolds619a69f2017-01-27 15:11:38 -0500865 channel.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes);
Julia Reynolds529e3322017-02-06 08:33:01 -0500866 channel.enableLights(true);
Julia Reynoldsbaff4002016-12-15 11:34:26 -0500867 channel.setBypassDnd(true);
868 channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
869 channel.setShowBadge(true);
Julia Reynoldsbaff4002016-12-15 11:34:26 -0500870 int lockMask = 0;
871 for (int i = 0; i < NotificationChannel.LOCKABLE_FIELDS.length; i++) {
872 lockMask |= NotificationChannel.LOCKABLE_FIELDS[i];
873 }
874 channel.lockFields(lockMask);
875
Julia Reynolds1fe10942018-03-28 12:46:51 -0400876 mHelper.createNotificationChannel(PKG, UID, channel, true, false);
Julia Reynoldsbaff4002016-12-15 11:34:26 -0500877
878 NotificationChannel savedChannel =
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500879 mHelper.getNotificationChannel(PKG, UID, channel.getId(), false);
Julia Reynoldsbaff4002016-12-15 11:34:26 -0500880
881 assertEquals(channel.getName(), savedChannel.getName());
882 assertEquals(channel.shouldShowLights(), savedChannel.shouldShowLights());
883 assertFalse(savedChannel.canBypassDnd());
884 assertFalse(Notification.VISIBILITY_SECRET == savedChannel.getLockscreenVisibility());
Julia Reynoldsf35e3972017-01-05 15:41:04 -0500885 assertEquals(channel.canShowBadge(), savedChannel.canShowBadge());
Julia Reynoldseb3dca72017-07-11 10:39:58 -0400886
887 verify(mHandler, never()).requestSort();
Julia Reynoldsbaff4002016-12-15 11:34:26 -0500888 }
889
890 @Test
891 public void testCreateChannel_CannotChangeHiddenFieldsAssistant() throws Exception {
892 final NotificationChannel channel =
Julia Reynolds74856c42017-02-08 14:47:23 -0500893 new NotificationChannel("id2", "name2", IMPORTANCE_LOW);
Julia Reynolds619a69f2017-01-27 15:11:38 -0500894 channel.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes);
Julia Reynolds529e3322017-02-06 08:33:01 -0500895 channel.enableLights(true);
Julia Reynoldsbaff4002016-12-15 11:34:26 -0500896 channel.setBypassDnd(true);
897 channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
898 channel.setShowBadge(true);
Julia Reynoldsbaff4002016-12-15 11:34:26 -0500899 int lockMask = 0;
900 for (int i = 0; i < NotificationChannel.LOCKABLE_FIELDS.length; i++) {
901 lockMask |= NotificationChannel.LOCKABLE_FIELDS[i];
902 }
903 channel.lockFields(lockMask);
904
Julia Reynolds1fe10942018-03-28 12:46:51 -0400905 mHelper.createNotificationChannel(PKG, UID, channel, true, false);
Julia Reynoldsbaff4002016-12-15 11:34:26 -0500906
907 NotificationChannel savedChannel =
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -0500908 mHelper.getNotificationChannel(PKG, UID, channel.getId(), false);
Julia Reynoldsbaff4002016-12-15 11:34:26 -0500909
910 assertEquals(channel.getName(), savedChannel.getName());
911 assertEquals(channel.shouldShowLights(), savedChannel.shouldShowLights());
912 assertFalse(savedChannel.canBypassDnd());
913 assertFalse(Notification.VISIBILITY_SECRET == savedChannel.getLockscreenVisibility());
Julia Reynoldsf35e3972017-01-05 15:41:04 -0500914 assertEquals(channel.canShowBadge(), savedChannel.canShowBadge());
Julia Reynoldsbaff4002016-12-15 11:34:26 -0500915 }
Julia Reynolds4036e8d2017-01-13 09:50:05 -0500916
917 @Test
Julia Reynoldse0b25742017-05-08 12:55:24 -0400918 public void testClearLockedFields() throws Exception {
919 final NotificationChannel channel = getChannel();
920 mHelper.clearLockedFields(channel);
921 assertEquals(0, channel.getUserLockedFields());
922
923 channel.lockFields(NotificationChannel.USER_LOCKED_PRIORITY
924 | NotificationChannel.USER_LOCKED_IMPORTANCE);
925 mHelper.clearLockedFields(channel);
926 assertEquals(0, channel.getUserLockedFields());
927 }
928
929 @Test
930 public void testLockFields_soundAndVibration() throws Exception {
Julia Reynolds1fe10942018-03-28 12:46:51 -0400931 mHelper.createNotificationChannel(PKG, UID, getChannel(), true, false);
Julia Reynoldse0b25742017-05-08 12:55:24 -0400932
933 final NotificationChannel update1 = getChannel();
934 update1.setSound(new Uri.Builder().scheme("test").build(),
935 new AudioAttributes.Builder().build());
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500936 update1.lockFields(NotificationChannel.USER_LOCKED_PRIORITY);
Julia Reynolds8617e4e2017-09-18 16:52:37 -0400937 mHelper.updateNotificationChannel(PKG, UID, update1, true);
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500938 assertEquals(NotificationChannel.USER_LOCKED_PRIORITY
939 | NotificationChannel.USER_LOCKED_SOUND,
Julia Reynoldse0b25742017-05-08 12:55:24 -0400940 mHelper.getNotificationChannel(PKG, UID, update1.getId(), false)
941 .getUserLockedFields());
942
943 NotificationChannel update2 = getChannel();
944 update2.enableVibration(true);
Julia Reynolds8617e4e2017-09-18 16:52:37 -0400945 mHelper.updateNotificationChannel(PKG, UID, update2, true);
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500946 assertEquals(NotificationChannel.USER_LOCKED_PRIORITY
947 | NotificationChannel.USER_LOCKED_SOUND
Julia Reynoldse0b25742017-05-08 12:55:24 -0400948 | NotificationChannel.USER_LOCKED_VIBRATION,
949 mHelper.getNotificationChannel(PKG, UID, update2.getId(), false)
950 .getUserLockedFields());
951 }
952
953 @Test
954 public void testLockFields_vibrationAndLights() throws Exception {
Julia Reynolds1fe10942018-03-28 12:46:51 -0400955 mHelper.createNotificationChannel(PKG, UID, getChannel(), true, false);
Julia Reynoldse0b25742017-05-08 12:55:24 -0400956
957 final NotificationChannel update1 = getChannel();
958 update1.setVibrationPattern(new long[]{7945, 46 ,246});
Julia Reynolds8617e4e2017-09-18 16:52:37 -0400959 mHelper.updateNotificationChannel(PKG, UID, update1, true);
Julia Reynoldse0b25742017-05-08 12:55:24 -0400960 assertEquals(NotificationChannel.USER_LOCKED_VIBRATION,
961 mHelper.getNotificationChannel(PKG, UID, update1.getId(), false)
962 .getUserLockedFields());
963
964 final NotificationChannel update2 = getChannel();
965 update2.enableLights(true);
Julia Reynolds8617e4e2017-09-18 16:52:37 -0400966 mHelper.updateNotificationChannel(PKG, UID, update2, true);
Julia Reynoldse0b25742017-05-08 12:55:24 -0400967 assertEquals(NotificationChannel.USER_LOCKED_VIBRATION
968 | NotificationChannel.USER_LOCKED_LIGHTS,
969 mHelper.getNotificationChannel(PKG, UID, update2.getId(), false)
970 .getUserLockedFields());
971 }
972
973 @Test
974 public void testLockFields_lightsAndImportance() throws Exception {
Julia Reynolds1fe10942018-03-28 12:46:51 -0400975 mHelper.createNotificationChannel(PKG, UID, getChannel(), true, false);
Julia Reynoldse0b25742017-05-08 12:55:24 -0400976
977 final NotificationChannel update1 = getChannel();
978 update1.setLightColor(Color.GREEN);
Julia Reynolds8617e4e2017-09-18 16:52:37 -0400979 mHelper.updateNotificationChannel(PKG, UID, update1, true);
Julia Reynoldse0b25742017-05-08 12:55:24 -0400980 assertEquals(NotificationChannel.USER_LOCKED_LIGHTS,
981 mHelper.getNotificationChannel(PKG, UID, update1.getId(), false)
982 .getUserLockedFields());
983
984 final NotificationChannel update2 = getChannel();
985 update2.setImportance(IMPORTANCE_DEFAULT);
Julia Reynolds8617e4e2017-09-18 16:52:37 -0400986 mHelper.updateNotificationChannel(PKG, UID, update2, true);
Julia Reynoldse0b25742017-05-08 12:55:24 -0400987 assertEquals(NotificationChannel.USER_LOCKED_LIGHTS
988 | NotificationChannel.USER_LOCKED_IMPORTANCE,
989 mHelper.getNotificationChannel(PKG, UID, update2.getId(), false)
990 .getUserLockedFields());
991 }
992
993 @Test
994 public void testLockFields_visibilityAndDndAndBadge() throws Exception {
Julia Reynolds1fe10942018-03-28 12:46:51 -0400995 mHelper.createNotificationChannel(PKG, UID, getChannel(), true, false);
Julia Reynoldse0b25742017-05-08 12:55:24 -0400996 assertEquals(0,
997 mHelper.getNotificationChannel(PKG, UID, getChannel().getId(), false)
998 .getUserLockedFields());
999
1000 final NotificationChannel update1 = getChannel();
1001 update1.setBypassDnd(true);
Julia Reynolds8617e4e2017-09-18 16:52:37 -04001002 mHelper.updateNotificationChannel(PKG, UID, update1, true);
Julia Reynoldse0b25742017-05-08 12:55:24 -04001003 assertEquals(NotificationChannel.USER_LOCKED_PRIORITY,
1004 mHelper.getNotificationChannel(PKG, UID, update1.getId(), false)
1005 .getUserLockedFields());
1006
1007 final NotificationChannel update2 = getChannel();
1008 update2.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
Julia Reynolds8617e4e2017-09-18 16:52:37 -04001009 mHelper.updateNotificationChannel(PKG, UID, update2, true);
Julia Reynoldse0b25742017-05-08 12:55:24 -04001010 assertEquals(NotificationChannel.USER_LOCKED_PRIORITY
1011 | NotificationChannel.USER_LOCKED_VISIBILITY,
1012 mHelper.getNotificationChannel(PKG, UID, update2.getId(), false)
1013 .getUserLockedFields());
1014
1015 final NotificationChannel update3 = getChannel();
1016 update3.setShowBadge(false);
Julia Reynolds8617e4e2017-09-18 16:52:37 -04001017 mHelper.updateNotificationChannel(PKG, UID, update3, true);
Julia Reynoldse0b25742017-05-08 12:55:24 -04001018 assertEquals(NotificationChannel.USER_LOCKED_PRIORITY
1019 | NotificationChannel.USER_LOCKED_VISIBILITY
1020 | NotificationChannel.USER_LOCKED_SHOW_BADGE,
1021 mHelper.getNotificationChannel(PKG, UID, update3.getId(), false)
1022 .getUserLockedFields());
1023 }
1024
1025 @Test
Julia Reynolds8e0eb372017-03-21 15:04:50 -04001026 public void testDeleteNonExistentChannel() throws Exception {
1027 mHelper.deleteNotificationChannelGroup(PKG, UID, "does not exist");
1028 }
1029
1030 @Test
Julia Reynolds4036e8d2017-01-13 09:50:05 -05001031 public void testGetDeletedChannel() throws Exception {
Julia Reynoldse0b25742017-05-08 12:55:24 -04001032 NotificationChannel channel = getChannel();
Julia Reynolds619a69f2017-01-27 15:11:38 -05001033 channel.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes);
Julia Reynolds529e3322017-02-06 08:33:01 -05001034 channel.enableLights(true);
Julia Reynolds4036e8d2017-01-13 09:50:05 -05001035 channel.setBypassDnd(true);
1036 channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
1037 channel.enableVibration(true);
Julia Reynolds619a69f2017-01-27 15:11:38 -05001038 channel.setVibrationPattern(new long[]{100, 67, 145, 156});
Julia Reynolds4036e8d2017-01-13 09:50:05 -05001039
Julia Reynolds1fe10942018-03-28 12:46:51 -04001040 mHelper.createNotificationChannel(PKG, UID, channel, true, false);
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001041 mHelper.deleteNotificationChannel(PKG, UID, channel.getId());
Julia Reynolds4036e8d2017-01-13 09:50:05 -05001042
1043 // Does not return deleted channel
1044 NotificationChannel response =
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001045 mHelper.getNotificationChannel(PKG, UID, channel.getId(), false);
Julia Reynolds4036e8d2017-01-13 09:50:05 -05001046 assertNull(response);
1047
1048 // Returns deleted channel
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001049 response = mHelper.getNotificationChannel(PKG, UID, channel.getId(), true);
Julia Reynolds4036e8d2017-01-13 09:50:05 -05001050 compareChannels(channel, response);
1051 assertTrue(response.isDeleted());
1052 }
1053
1054 @Test
1055 public void testGetDeletedChannels() throws Exception {
1056 Map<String, NotificationChannel> channelMap = new HashMap<>();
1057 NotificationChannel channel =
Julia Reynolds74856c42017-02-08 14:47:23 -05001058 new NotificationChannel("id2", "name2", IMPORTANCE_LOW);
Julia Reynolds619a69f2017-01-27 15:11:38 -05001059 channel.setSound(new Uri.Builder().scheme("test").build(), mAudioAttributes);
Julia Reynolds529e3322017-02-06 08:33:01 -05001060 channel.enableLights(true);
Julia Reynolds4036e8d2017-01-13 09:50:05 -05001061 channel.setBypassDnd(true);
1062 channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
1063 channel.enableVibration(true);
Julia Reynolds619a69f2017-01-27 15:11:38 -05001064 channel.setVibrationPattern(new long[]{100, 67, 145, 156});
Julia Reynolds4036e8d2017-01-13 09:50:05 -05001065 channelMap.put(channel.getId(), channel);
1066 NotificationChannel channel2 =
1067 new NotificationChannel("id4", "a", NotificationManager.IMPORTANCE_HIGH);
1068 channelMap.put(channel2.getId(), channel2);
Julia Reynolds1fe10942018-03-28 12:46:51 -04001069 mHelper.createNotificationChannel(PKG, UID, channel, true, false);
1070 mHelper.createNotificationChannel(PKG, UID, channel2, true, false);
Julia Reynolds4036e8d2017-01-13 09:50:05 -05001071
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001072 mHelper.deleteNotificationChannel(PKG, UID, channel.getId());
Julia Reynolds4036e8d2017-01-13 09:50:05 -05001073
1074 // Returns only non-deleted channels
1075 List<NotificationChannel> channels =
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001076 mHelper.getNotificationChannels(PKG, UID, false).getList();
Geoffrey Pitscha22f6442017-05-05 16:47:38 +00001077 assertEquals(2, channels.size()); // Default channel + non-deleted channel
1078 for (NotificationChannel nc : channels) {
1079 if (!NotificationChannel.DEFAULT_CHANNEL_ID.equals(nc.getId())) {
1080 compareChannels(channel2, nc);
1081 }
1082 }
Julia Reynolds4036e8d2017-01-13 09:50:05 -05001083
1084 // Returns deleted channels too
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001085 channels = mHelper.getNotificationChannels(PKG, UID, true).getList();
Geoffrey Pitscha22f6442017-05-05 16:47:38 +00001086 assertEquals(3, channels.size()); // Includes default channel
Julia Reynolds4036e8d2017-01-13 09:50:05 -05001087 for (NotificationChannel nc : channels) {
Geoffrey Pitscha22f6442017-05-05 16:47:38 +00001088 if (!NotificationChannel.DEFAULT_CHANNEL_ID.equals(nc.getId())) {
1089 compareChannels(channelMap.get(nc.getId()), nc);
1090 }
Julia Reynolds4036e8d2017-01-13 09:50:05 -05001091 }
1092 }
1093
1094 @Test
Julia Reynolds41103f42017-03-15 11:36:35 -04001095 public void testGetDeletedChannelCount() throws Exception {
1096 NotificationChannel channel =
1097 new NotificationChannel("id2", "name2", IMPORTANCE_LOW);
1098 NotificationChannel channel2 =
1099 new NotificationChannel("id4", "a", NotificationManager.IMPORTANCE_HIGH);
1100 NotificationChannel channel3 =
Julia Reynoldsf2e499d2018-03-30 10:36:42 -04001101 new NotificationChannel("id5", "a", NotificationManager.IMPORTANCE_HIGH);
Julia Reynolds1fe10942018-03-28 12:46:51 -04001102 mHelper.createNotificationChannel(PKG, UID, channel, true, false);
1103 mHelper.createNotificationChannel(PKG, UID, channel2, true, false);
1104 mHelper.createNotificationChannel(PKG, UID, channel3, true, false);
Julia Reynolds41103f42017-03-15 11:36:35 -04001105
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001106 mHelper.deleteNotificationChannel(PKG, UID, channel.getId());
1107 mHelper.deleteNotificationChannel(PKG, UID, channel3.getId());
Julia Reynolds41103f42017-03-15 11:36:35 -04001108
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001109 assertEquals(2, mHelper.getDeletedChannelCount(PKG, UID));
1110 assertEquals(0, mHelper.getDeletedChannelCount("pkg2", UID2));
Julia Reynolds41103f42017-03-15 11:36:35 -04001111 }
1112
1113 @Test
Julia Reynoldsf2e499d2018-03-30 10:36:42 -04001114 public void testGetBlockedChannelCount() throws Exception {
1115 NotificationChannel channel =
1116 new NotificationChannel("id2", "name2", IMPORTANCE_LOW);
1117 NotificationChannel channel2 =
1118 new NotificationChannel("id4", "a", NotificationManager.IMPORTANCE_NONE);
1119 NotificationChannel channel3 =
1120 new NotificationChannel("id5", "a", NotificationManager.IMPORTANCE_NONE);
1121 mHelper.createNotificationChannel(PKG, UID, channel, true, false);
1122 mHelper.createNotificationChannel(PKG, UID, channel2, true, false);
1123 mHelper.createNotificationChannel(PKG, UID, channel3, true, false);
1124
1125 mHelper.deleteNotificationChannel(PKG, UID, channel3.getId());
1126
1127 assertEquals(1, mHelper.getBlockedChannelCount(PKG, UID));
1128 assertEquals(0, mHelper.getBlockedChannelCount("pkg2", UID2));
1129 }
1130
1131 @Test
Julia Reynolds4036e8d2017-01-13 09:50:05 -05001132 public void testCreateDeletedChannel() throws Exception {
Julia Reynolds619a69f2017-01-27 15:11:38 -05001133 long[] vibration = new long[]{100, 67, 145, 156};
Julia Reynolds4036e8d2017-01-13 09:50:05 -05001134 NotificationChannel channel =
Julia Reynolds74856c42017-02-08 14:47:23 -05001135 new NotificationChannel("id2", "name2", IMPORTANCE_LOW);
Julia Reynolds4036e8d2017-01-13 09:50:05 -05001136 channel.setVibrationPattern(vibration);
1137
Julia Reynolds1fe10942018-03-28 12:46:51 -04001138 mHelper.createNotificationChannel(PKG, UID, channel, true, false);
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001139 mHelper.deleteNotificationChannel(PKG, UID, channel.getId());
Julia Reynolds4036e8d2017-01-13 09:50:05 -05001140
1141 NotificationChannel newChannel = new NotificationChannel(
1142 channel.getId(), channel.getName(), NotificationManager.IMPORTANCE_HIGH);
Julia Reynolds619a69f2017-01-27 15:11:38 -05001143 newChannel.setVibrationPattern(new long[]{100});
Julia Reynolds4036e8d2017-01-13 09:50:05 -05001144
Julia Reynolds1fe10942018-03-28 12:46:51 -04001145 mHelper.createNotificationChannel(PKG, UID, newChannel, true, false);
Julia Reynolds4036e8d2017-01-13 09:50:05 -05001146
1147 // No long deleted, using old settings
1148 compareChannels(channel,
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001149 mHelper.getNotificationChannel(PKG, UID, newChannel.getId(), false));
Julia Reynolds4036e8d2017-01-13 09:50:05 -05001150 }
1151
1152 @Test
Julia Reynolds17717f52017-05-09 11:46:06 -04001153 public void testOnlyHasDefaultChannel() throws Exception {
1154 assertTrue(mHelper.onlyHasDefaultChannel(PKG, UID));
1155 assertFalse(mHelper.onlyHasDefaultChannel(UPDATED_PKG, UID2));
1156
Julia Reynolds1fe10942018-03-28 12:46:51 -04001157 mHelper.createNotificationChannel(PKG, UID, getChannel(), true, false);
Julia Reynolds17717f52017-05-09 11:46:06 -04001158 assertFalse(mHelper.onlyHasDefaultChannel(PKG, UID));
1159 }
1160
1161 @Test
Julia Reynolds03fa85d2017-03-06 15:14:50 -05001162 public void testCreateChannel_defaultChannelId() throws Exception {
1163 try {
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001164 mHelper.createNotificationChannel(PKG, UID, new NotificationChannel(
Julia Reynolds1fe10942018-03-28 12:46:51 -04001165 NotificationChannel.DEFAULT_CHANNEL_ID, "ha", IMPORTANCE_HIGH), true, false);
Julia Reynolds03fa85d2017-03-06 15:14:50 -05001166 fail("Allowed to create default channel");
1167 } catch (IllegalArgumentException e) {
1168 // pass
1169 }
1170 }
1171
1172 @Test
Julia Reynolds4036e8d2017-01-13 09:50:05 -05001173 public void testCreateChannel_alreadyExists() throws Exception {
Julia Reynolds619a69f2017-01-27 15:11:38 -05001174 long[] vibration = new long[]{100, 67, 145, 156};
Julia Reynolds4036e8d2017-01-13 09:50:05 -05001175 NotificationChannel channel =
Julia Reynolds74856c42017-02-08 14:47:23 -05001176 new NotificationChannel("id2", "name2", IMPORTANCE_LOW);
Julia Reynolds4036e8d2017-01-13 09:50:05 -05001177 channel.setVibrationPattern(vibration);
1178
Julia Reynolds1fe10942018-03-28 12:46:51 -04001179 mHelper.createNotificationChannel(PKG, UID, channel, true, false);
Julia Reynolds4036e8d2017-01-13 09:50:05 -05001180
1181 NotificationChannel newChannel = new NotificationChannel(
1182 channel.getId(), channel.getName(), NotificationManager.IMPORTANCE_HIGH);
Julia Reynolds619a69f2017-01-27 15:11:38 -05001183 newChannel.setVibrationPattern(new long[]{100});
Julia Reynolds4036e8d2017-01-13 09:50:05 -05001184
Julia Reynolds1fe10942018-03-28 12:46:51 -04001185 mHelper.createNotificationChannel(PKG, UID, newChannel, true, false);
Julia Reynolds4036e8d2017-01-13 09:50:05 -05001186
1187 // Old settings not overridden
1188 compareChannels(channel,
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001189 mHelper.getNotificationChannel(PKG, UID, newChannel.getId(), false));
Julia Reynolds4036e8d2017-01-13 09:50:05 -05001190 }
1191
1192 @Test
Julia Reynolds31b2baa2017-02-22 12:18:45 -05001193 public void testCreateChannel_noOverrideSound() throws Exception {
1194 Uri sound = new Uri.Builder().scheme("test").build();
1195 final NotificationChannel channel = new NotificationChannel("id2", "name2",
1196 NotificationManager.IMPORTANCE_DEFAULT);
1197 channel.setSound(sound, mAudioAttributes);
Julia Reynolds1fe10942018-03-28 12:46:51 -04001198 mHelper.createNotificationChannel(PKG, UID, channel, true, false);
Julia Reynolds31b2baa2017-02-22 12:18:45 -05001199 assertEquals(sound, mHelper.getNotificationChannel(
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001200 PKG, UID, channel.getId(), false).getSound());
Julia Reynolds31b2baa2017-02-22 12:18:45 -05001201 }
1202
1203 @Test
Julia Reynolds4036e8d2017-01-13 09:50:05 -05001204 public void testPermanentlyDeleteChannels() throws Exception {
1205 NotificationChannel channel1 =
1206 new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH);
1207 NotificationChannel channel2 =
Julia Reynolds74856c42017-02-08 14:47:23 -05001208 new NotificationChannel("id2", "name2", IMPORTANCE_LOW);
Julia Reynolds4036e8d2017-01-13 09:50:05 -05001209
Julia Reynolds1fe10942018-03-28 12:46:51 -04001210 mHelper.createNotificationChannel(PKG, UID, channel1, true, false);
1211 mHelper.createNotificationChannel(PKG, UID, channel2, false, false);
Julia Reynolds4036e8d2017-01-13 09:50:05 -05001212
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001213 mHelper.permanentlyDeleteNotificationChannels(PKG, UID);
Julia Reynolds4036e8d2017-01-13 09:50:05 -05001214
Geoffrey Pitscha22f6442017-05-05 16:47:38 +00001215 // Only default channel remains
1216 assertEquals(1, mHelper.getNotificationChannels(PKG, UID, true).getList().size());
Julia Reynolds4036e8d2017-01-13 09:50:05 -05001217 }
1218
1219 @Test
Julia Reynolds9bfba592017-03-15 14:03:55 -04001220 public void testDeleteGroup() throws Exception {
1221 NotificationChannelGroup notDeleted = new NotificationChannelGroup("not", "deleted");
1222 NotificationChannelGroup deleted = new NotificationChannelGroup("totally", "deleted");
1223 NotificationChannel nonGroupedNonDeletedChannel =
1224 new NotificationChannel("no group", "so not deleted", IMPORTANCE_HIGH);
1225 NotificationChannel groupedButNotDeleted =
1226 new NotificationChannel("not deleted", "belongs to notDeleted", IMPORTANCE_DEFAULT);
1227 groupedButNotDeleted.setGroup("not");
1228 NotificationChannel groupedAndDeleted =
1229 new NotificationChannel("deleted", "belongs to deleted", IMPORTANCE_DEFAULT);
1230 groupedAndDeleted.setGroup("totally");
1231
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001232 mHelper.createNotificationChannelGroup(PKG, UID, notDeleted, true);
1233 mHelper.createNotificationChannelGroup(PKG, UID, deleted, true);
Julia Reynolds1fe10942018-03-28 12:46:51 -04001234 mHelper.createNotificationChannel(PKG, UID, nonGroupedNonDeletedChannel, true, false);
1235 mHelper.createNotificationChannel(PKG, UID, groupedAndDeleted, true, false);
1236 mHelper.createNotificationChannel(PKG, UID, groupedButNotDeleted, true, false);
Julia Reynolds9bfba592017-03-15 14:03:55 -04001237
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001238 mHelper.deleteNotificationChannelGroup(PKG, UID, deleted.getId());
Julia Reynolds9bfba592017-03-15 14:03:55 -04001239
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001240 assertNull(mHelper.getNotificationChannelGroup(deleted.getId(), PKG, UID));
1241 assertNotNull(mHelper.getNotificationChannelGroup(notDeleted.getId(), PKG, UID));
Julia Reynolds9bfba592017-03-15 14:03:55 -04001242
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001243 assertNull(mHelper.getNotificationChannel(PKG, UID, groupedAndDeleted.getId(), false));
Julia Reynolds9bfba592017-03-15 14:03:55 -04001244 compareChannels(groupedAndDeleted,
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001245 mHelper.getNotificationChannel(PKG, UID, groupedAndDeleted.getId(), true));
Julia Reynolds9bfba592017-03-15 14:03:55 -04001246
1247 compareChannels(groupedButNotDeleted,
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001248 mHelper.getNotificationChannel(PKG, UID, groupedButNotDeleted.getId(), false));
Julia Reynolds9bfba592017-03-15 14:03:55 -04001249 compareChannels(nonGroupedNonDeletedChannel, mHelper.getNotificationChannel(
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001250 PKG, UID, nonGroupedNonDeletedChannel.getId(), false));
Julia Reynolds9bfba592017-03-15 14:03:55 -04001251
1252 // notDeleted
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001253 assertEquals(1, mHelper.getNotificationChannelGroups(PKG, UID).size());
Julia Reynoldseb3dca72017-07-11 10:39:58 -04001254
1255 verify(mHandler, never()).requestSort();
Julia Reynolds9bfba592017-03-15 14:03:55 -04001256 }
1257
1258 @Test
Julia Reynolds2e9bf5f2017-05-03 13:23:30 -04001259 public void testOnUserRemoved() throws Exception {
1260 int[] user0Uids = {98, 235, 16, 3782};
1261 int[] user1Uids = new int[user0Uids.length];
1262 for (int i = 0; i < user0Uids.length; i++) {
1263 user1Uids[i] = UserHandle.PER_USER_RANGE + user0Uids[i];
1264
1265 final ApplicationInfo legacy = new ApplicationInfo();
1266 legacy.targetSdkVersion = Build.VERSION_CODES.N_MR1;
1267 when(mPm.getApplicationInfoAsUser(eq(PKG), anyInt(), anyInt())).thenReturn(legacy);
1268
1269 // create records with the default channel for all user 0 and user 1 uids
1270 mHelper.getImportance(PKG, user0Uids[i]);
1271 mHelper.getImportance(PKG, user1Uids[i]);
1272 }
1273
1274 mHelper.onUserRemoved(1);
1275
1276 // user 0 records remain
1277 for (int i = 0; i < user0Uids.length; i++) {
1278 assertEquals(1,
1279 mHelper.getNotificationChannels(PKG, user0Uids[i], false).getList().size());
1280 }
1281 // user 1 records are gone
1282 for (int i = 0; i < user1Uids.length; i++) {
1283 assertEquals(0,
1284 mHelper.getNotificationChannels(PKG, user1Uids[i], false).getList().size());
1285 }
1286 }
1287
1288 @Test
Julia Reynolds4036e8d2017-01-13 09:50:05 -05001289 public void testOnPackageChanged_packageRemoval() throws Exception {
1290 // Deleted
1291 NotificationChannel channel1 =
1292 new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH);
Julia Reynolds1fe10942018-03-28 12:46:51 -04001293 mHelper.createNotificationChannel(PKG, UID, channel1, true, false);
Julia Reynolds4036e8d2017-01-13 09:50:05 -05001294
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001295 mHelper.onPackagesChanged(true, UserHandle.USER_SYSTEM, new String[]{PKG}, new int[]{UID});
Julia Reynolds4036e8d2017-01-13 09:50:05 -05001296
Geoffrey Pitscha22f6442017-05-05 16:47:38 +00001297 assertEquals(0, mHelper.getNotificationChannels(PKG, UID, true).getList().size());
Julia Reynolds4036e8d2017-01-13 09:50:05 -05001298
1299 // Not deleted
Julia Reynolds1fe10942018-03-28 12:46:51 -04001300 mHelper.createNotificationChannel(PKG, UID, channel1, true, false);
Geoffrey Pitscha22f6442017-05-05 16:47:38 +00001301
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001302 mHelper.onPackagesChanged(false, UserHandle.USER_SYSTEM, new String[]{PKG}, new int[]{UID});
Geoffrey Pitscha22f6442017-05-05 16:47:38 +00001303 assertEquals(2, mHelper.getNotificationChannels(PKG, UID, false).getList().size());
Julia Reynolds4036e8d2017-01-13 09:50:05 -05001304 }
Julia Reynolds924eed12017-01-19 09:52:07 -05001305
1306 @Test
Julia Reynolds59e152e2017-01-25 17:42:53 -05001307 public void testOnPackageChanged_packageRemoval_importance() throws Exception {
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001308 mHelper.setImportance(PKG, UID, NotificationManager.IMPORTANCE_HIGH);
Julia Reynolds59e152e2017-01-25 17:42:53 -05001309
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001310 mHelper.onPackagesChanged(true, UserHandle.USER_SYSTEM, new String[]{PKG}, new int[]{UID});
Julia Reynolds59e152e2017-01-25 17:42:53 -05001311
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001312 assertEquals(NotificationManager.IMPORTANCE_UNSPECIFIED, mHelper.getImportance(PKG, UID));
Julia Reynolds59e152e2017-01-25 17:42:53 -05001313 }
1314
1315 @Test
1316 public void testOnPackageChanged_packageRemoval_groups() throws Exception {
1317 NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1");
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001318 mHelper.createNotificationChannelGroup(PKG, UID, ncg, true);
Julia Reynolds59e152e2017-01-25 17:42:53 -05001319 NotificationChannelGroup ncg2 = new NotificationChannelGroup("group2", "name2");
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001320 mHelper.createNotificationChannelGroup(PKG, UID, ncg2, true);
Julia Reynolds59e152e2017-01-25 17:42:53 -05001321
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001322 mHelper.onPackagesChanged(true, UserHandle.USER_SYSTEM, new String[]{PKG}, new int[]{UID});
Julia Reynolds59e152e2017-01-25 17:42:53 -05001323
Julia Reynolds3eb3ffd2017-11-16 10:11:32 -05001324 assertEquals(0,
1325 mHelper.getNotificationChannelGroups(PKG, UID, true, true).getList().size());
Julia Reynolds59e152e2017-01-25 17:42:53 -05001326 }
1327
1328 @Test
Julia Reynoldsf26eb912017-05-22 15:47:06 -04001329 public void testOnPackageChange_downgradeTargetSdk() throws Exception {
1330 // create channel as api 26
Julia Reynolds1fe10942018-03-28 12:46:51 -04001331 mHelper.createNotificationChannel(UPDATED_PKG, UID2, getChannel(), true, false);
Julia Reynoldsf26eb912017-05-22 15:47:06 -04001332
1333 // install new app version targeting 25
1334 final ApplicationInfo legacy = new ApplicationInfo();
1335 legacy.targetSdkVersion = Build.VERSION_CODES.N_MR1;
1336 when(mPm.getApplicationInfoAsUser(eq(UPDATED_PKG), anyInt(), anyInt())).thenReturn(legacy);
1337 mHelper.onPackagesChanged(
1338 false, UserHandle.USER_SYSTEM, new String[]{UPDATED_PKG}, new int[]{UID2});
1339
1340 // make sure the default channel was readded
1341 //assertEquals(2, mHelper.getNotificationChannels(UPDATED_PKG, UID2, false).getList().size());
1342 assertNotNull(mHelper.getNotificationChannel(
1343 UPDATED_PKG, UID2, NotificationChannel.DEFAULT_CHANNEL_ID, false));
1344 }
1345
1346 @Test
Julia Reynolds924eed12017-01-19 09:52:07 -05001347 public void testRecordDefaults() throws Exception {
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001348 assertEquals(NotificationManager.IMPORTANCE_UNSPECIFIED, mHelper.getImportance(PKG, UID));
1349 assertEquals(true, mHelper.canShowBadge(PKG, UID));
1350 assertEquals(1, mHelper.getNotificationChannels(PKG, UID, false).getList().size());
Julia Reynolds924eed12017-01-19 09:52:07 -05001351 }
Julia Reynolds59e152e2017-01-25 17:42:53 -05001352
1353 @Test
1354 public void testCreateGroup() throws Exception {
1355 NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1");
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001356 mHelper.createNotificationChannelGroup(PKG, UID, ncg, true);
1357 assertEquals(ncg, mHelper.getNotificationChannelGroups(PKG, UID).iterator().next());
Julia Reynoldseb3dca72017-07-11 10:39:58 -04001358 verify(mHandler, never()).requestSort();
Julia Reynolds59e152e2017-01-25 17:42:53 -05001359 }
1360
1361 @Test
1362 public void testCannotCreateChannel_badGroup() throws Exception {
1363 NotificationChannel channel1 =
1364 new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH);
1365 channel1.setGroup("garbage");
1366 try {
Julia Reynolds1fe10942018-03-28 12:46:51 -04001367 mHelper.createNotificationChannel(PKG, UID, channel1, true, false);
Julia Reynolds59e152e2017-01-25 17:42:53 -05001368 fail("Created a channel with a bad group");
Julia Reynolds619a69f2017-01-27 15:11:38 -05001369 } catch (IllegalArgumentException e) {
1370 }
Julia Reynolds59e152e2017-01-25 17:42:53 -05001371 }
1372
1373 @Test
1374 public void testCannotCreateChannel_goodGroup() throws Exception {
1375 NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1");
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001376 mHelper.createNotificationChannelGroup(PKG, UID, ncg, true);
Julia Reynolds59e152e2017-01-25 17:42:53 -05001377 NotificationChannel channel1 =
1378 new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH);
1379 channel1.setGroup(ncg.getId());
Julia Reynolds1fe10942018-03-28 12:46:51 -04001380 mHelper.createNotificationChannel(PKG, UID, channel1, true, false);
Julia Reynolds59e152e2017-01-25 17:42:53 -05001381
1382 assertEquals(ncg.getId(),
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001383 mHelper.getNotificationChannel(PKG, UID, channel1.getId(), false).getGroup());
Julia Reynolds59e152e2017-01-25 17:42:53 -05001384 }
1385
1386 @Test
1387 public void testGetChannelGroups() throws Exception {
Julia Reynoldsf02562a2017-01-26 13:33:56 -05001388 NotificationChannelGroup unused = new NotificationChannelGroup("unused", "s");
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001389 mHelper.createNotificationChannelGroup(PKG, UID, unused, true);
Julia Reynolds59e152e2017-01-25 17:42:53 -05001390 NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1");
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001391 mHelper.createNotificationChannelGroup(PKG, UID, ncg, true);
Julia Reynolds59e152e2017-01-25 17:42:53 -05001392 NotificationChannelGroup ncg2 = new NotificationChannelGroup("group2", "name2");
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001393 mHelper.createNotificationChannelGroup(PKG, UID, ncg2, true);
Julia Reynolds59e152e2017-01-25 17:42:53 -05001394
1395 NotificationChannel channel1 =
1396 new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH);
1397 channel1.setGroup(ncg.getId());
Julia Reynolds1fe10942018-03-28 12:46:51 -04001398 mHelper.createNotificationChannel(PKG, UID, channel1, true, false);
Julia Reynolds59e152e2017-01-25 17:42:53 -05001399 NotificationChannel channel1a =
1400 new NotificationChannel("id1a", "name1", NotificationManager.IMPORTANCE_HIGH);
1401 channel1a.setGroup(ncg.getId());
Julia Reynolds1fe10942018-03-28 12:46:51 -04001402 mHelper.createNotificationChannel(PKG, UID, channel1a, true, false);
Julia Reynolds59e152e2017-01-25 17:42:53 -05001403
1404 NotificationChannel channel2 =
1405 new NotificationChannel("id2", "name1", NotificationManager.IMPORTANCE_HIGH);
1406 channel2.setGroup(ncg2.getId());
Julia Reynolds1fe10942018-03-28 12:46:51 -04001407 mHelper.createNotificationChannel(PKG, UID, channel2, true, false);
Julia Reynolds59e152e2017-01-25 17:42:53 -05001408
1409 NotificationChannel channel3 =
1410 new NotificationChannel("id3", "name1", NotificationManager.IMPORTANCE_HIGH);
Julia Reynolds1fe10942018-03-28 12:46:51 -04001411 mHelper.createNotificationChannel(PKG, UID, channel3, true, false);
Julia Reynolds59e152e2017-01-25 17:42:53 -05001412
1413 List<NotificationChannelGroup> actual =
Julia Reynolds3eb3ffd2017-11-16 10:11:32 -05001414 mHelper.getNotificationChannelGroups(PKG, UID, true, true).getList();
Julia Reynolds59e152e2017-01-25 17:42:53 -05001415 assertEquals(3, actual.size());
Julia Reynolds619a69f2017-01-27 15:11:38 -05001416 for (NotificationChannelGroup group : actual) {
Julia Reynolds59e152e2017-01-25 17:42:53 -05001417 if (group.getId() == null) {
Geoffrey Pitscha22f6442017-05-05 16:47:38 +00001418 assertEquals(2, group.getChannels().size()); // misc channel too
1419 assertTrue(channel3.getId().equals(group.getChannels().get(0).getId())
1420 || channel3.getId().equals(group.getChannels().get(1).getId()));
Julia Reynolds59e152e2017-01-25 17:42:53 -05001421 } else if (group.getId().equals(ncg.getId())) {
1422 assertEquals(2, group.getChannels().size());
1423 if (group.getChannels().get(0).getId().equals(channel1.getId())) {
1424 assertTrue(group.getChannels().get(1).getId().equals(channel1a.getId()));
1425 } else if (group.getChannels().get(0).getId().equals(channel1a.getId())) {
1426 assertTrue(group.getChannels().get(1).getId().equals(channel1.getId()));
1427 } else {
1428 fail("expected channel not found");
1429 }
1430 } else if (group.getId().equals(ncg2.getId())) {
1431 assertEquals(1, group.getChannels().size());
1432 assertEquals(channel2.getId(), group.getChannels().get(0).getId());
1433 }
1434 }
1435 }
Julia Reynolds74856c42017-02-08 14:47:23 -05001436
1437 @Test
1438 public void testGetChannelGroups_noSideEffects() throws Exception {
1439 NotificationChannelGroup ncg = new NotificationChannelGroup("group1", "name1");
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001440 mHelper.createNotificationChannelGroup(PKG, UID, ncg, true);
Julia Reynolds74856c42017-02-08 14:47:23 -05001441
1442 NotificationChannel channel1 =
1443 new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH);
1444 channel1.setGroup(ncg.getId());
Julia Reynolds1fe10942018-03-28 12:46:51 -04001445 mHelper.createNotificationChannel(PKG, UID, channel1, true, false);
Julia Reynolds3eb3ffd2017-11-16 10:11:32 -05001446 mHelper.getNotificationChannelGroups(PKG, UID, true, true).getList();
Julia Reynolds74856c42017-02-08 14:47:23 -05001447
1448 channel1.setImportance(IMPORTANCE_LOW);
Julia Reynolds8617e4e2017-09-18 16:52:37 -04001449 mHelper.updateNotificationChannel(PKG, UID, channel1, true);
Julia Reynolds74856c42017-02-08 14:47:23 -05001450
1451 List<NotificationChannelGroup> actual =
Julia Reynolds3eb3ffd2017-11-16 10:11:32 -05001452 mHelper.getNotificationChannelGroups(PKG, UID, true, true).getList();
Julia Reynolds74856c42017-02-08 14:47:23 -05001453
Geoffrey Pitscha22f6442017-05-05 16:47:38 +00001454 assertEquals(2, actual.size());
1455 for (NotificationChannelGroup group : actual) {
1456 if (Objects.equals(group.getId(), ncg.getId())) {
1457 assertEquals(1, group.getChannels().size());
1458 }
1459 }
Julia Reynolds74856c42017-02-08 14:47:23 -05001460 }
Julia Reynoldsd373d782017-03-03 13:32:57 -05001461
1462 @Test
Julia Reynolds1d97e6a2017-03-13 15:05:40 -04001463 public void testCreateChannel_updateName() throws Exception {
1464 NotificationChannel nc = new NotificationChannel("id", "hello", IMPORTANCE_DEFAULT);
Julia Reynolds1fe10942018-03-28 12:46:51 -04001465 mHelper.createNotificationChannel(PKG, UID, nc, true, false);
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001466 NotificationChannel actual = mHelper.getNotificationChannel(PKG, UID, "id", false);
Julia Reynolds1d97e6a2017-03-13 15:05:40 -04001467 assertEquals("hello", actual.getName());
1468
1469 nc = new NotificationChannel("id", "goodbye", IMPORTANCE_HIGH);
Julia Reynolds1fe10942018-03-28 12:46:51 -04001470 mHelper.createNotificationChannel(PKG, UID, nc, true, false);
Julia Reynoldsd373d782017-03-03 13:32:57 -05001471
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001472 actual = mHelper.getNotificationChannel(PKG, UID, "id", false);
Julia Reynolds1d97e6a2017-03-13 15:05:40 -04001473 assertEquals("goodbye", actual.getName());
1474 assertEquals(IMPORTANCE_DEFAULT, actual.getImportance());
Julia Reynoldseb3dca72017-07-11 10:39:58 -04001475
1476 verify(mHandler, times(1)).requestSort();
Julia Reynoldsd373d782017-03-03 13:32:57 -05001477 }
1478
1479 @Test
Julia Reynolds005c8b92017-08-24 10:35:53 -04001480 public void testCreateChannel_addToGroup() throws Exception {
1481 NotificationChannelGroup group = new NotificationChannelGroup("group", "");
1482 mHelper.createNotificationChannelGroup(PKG, UID, group, true);
1483 NotificationChannel nc = new NotificationChannel("id", "hello", IMPORTANCE_DEFAULT);
Julia Reynolds1fe10942018-03-28 12:46:51 -04001484 mHelper.createNotificationChannel(PKG, UID, nc, true, false);
Julia Reynolds005c8b92017-08-24 10:35:53 -04001485 NotificationChannel actual = mHelper.getNotificationChannel(PKG, UID, "id", false);
1486 assertNull(actual.getGroup());
1487
1488 nc = new NotificationChannel("id", "hello", IMPORTANCE_HIGH);
1489 nc.setGroup(group.getId());
Julia Reynolds1fe10942018-03-28 12:46:51 -04001490 mHelper.createNotificationChannel(PKG, UID, nc, true, false);
Julia Reynolds005c8b92017-08-24 10:35:53 -04001491
1492 actual = mHelper.getNotificationChannel(PKG, UID, "id", false);
1493 assertNotNull(actual.getGroup());
1494 assertEquals(IMPORTANCE_DEFAULT, actual.getImportance());
1495
1496 verify(mHandler, times(1)).requestSort();
1497 }
1498
1499 @Test
Julia Reynoldsd373d782017-03-03 13:32:57 -05001500 public void testDumpChannelsJson() throws Exception {
1501 final ApplicationInfo upgrade = new ApplicationInfo();
1502 upgrade.targetSdkVersion = Build.VERSION_CODES.O;
1503 try {
1504 when(mPm.getApplicationInfoAsUser(
1505 anyString(), anyInt(), anyInt())).thenReturn(upgrade);
1506 } catch (PackageManager.NameNotFoundException e) {
1507 }
1508 ArrayMap<String, Integer> expectedChannels = new ArrayMap<>();
1509 int numPackages = ThreadLocalRandom.current().nextInt(1, 5);
1510 for (int i = 0; i < numPackages; i++) {
1511 String pkgName = "pkg" + i;
1512 int numChannels = ThreadLocalRandom.current().nextInt(1, 10);
1513 for (int j = 0; j < numChannels; j++) {
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001514 mHelper.createNotificationChannel(pkgName, UID,
Julia Reynolds1fe10942018-03-28 12:46:51 -04001515 new NotificationChannel("" + j, "a", IMPORTANCE_HIGH), true, false);
Julia Reynoldsd373d782017-03-03 13:32:57 -05001516 }
1517 expectedChannels.put(pkgName, numChannels);
1518 }
1519
1520 // delete the first channel of the first package
1521 String pkg = expectedChannels.keyAt(0);
Geoffrey Pitsch1f17e022017-01-03 16:44:20 -05001522 mHelper.deleteNotificationChannel("pkg" + 0, UID, "0");
Julia Reynoldsd373d782017-03-03 13:32:57 -05001523 // dump should not include deleted channels
1524 int count = expectedChannels.get(pkg);
1525 expectedChannels.put(pkg, count - 1);
1526
1527 JSONArray actual = mHelper.dumpChannelsJson(new NotificationManagerService.DumpFilter());
1528 assertEquals(numPackages, actual.length());
1529 for (int i = 0; i < numPackages; i++) {
1530 JSONObject object = actual.getJSONObject(i);
1531 assertTrue(expectedChannels.containsKey(object.get("packageName")));
Geoffrey Pitsch5644ccd2017-04-17 11:33:24 -04001532 assertEquals(expectedChannels.get(object.get("packageName")).intValue(),
Julia Reynoldsd373d782017-03-03 13:32:57 -05001533 object.getInt("channelCount"));
1534 }
1535 }
Chris Wren89aa2262017-05-05 18:05:56 -04001536
1537 @Test
1538 public void testBadgingOverrideTrue() throws Exception {
1539 Secure.putIntForUser(getContext().getContentResolver(),
1540 Secure.NOTIFICATION_BADGING, 1,
1541 USER.getIdentifier());
1542 mHelper.updateBadgingEnabled(); // would be called by settings observer
1543 assertTrue(mHelper.badgingEnabled(USER));
1544 }
1545
1546 @Test
1547 public void testBadgingOverrideFalse() throws Exception {
1548 Secure.putIntForUser(getContext().getContentResolver(),
1549 Secure.NOTIFICATION_BADGING, 0,
1550 USER.getIdentifier());
1551 mHelper.updateBadgingEnabled(); // would be called by settings observer
1552 assertFalse(mHelper.badgingEnabled(USER));
1553 }
1554
1555 @Test
Chris Wren13f157f2017-05-12 15:02:06 -04001556 public void testBadgingForUserAll() throws Exception {
1557 try {
1558 mHelper.badgingEnabled(UserHandle.ALL);
1559 } catch (Exception e) {
1560 fail("just don't throw");
1561 }
1562 }
1563
1564 @Test
Chris Wren89aa2262017-05-05 18:05:56 -04001565 public void testBadgingOverrideUserIsolation() throws Exception {
1566 Secure.putIntForUser(getContext().getContentResolver(),
1567 Secure.NOTIFICATION_BADGING, 0,
1568 USER.getIdentifier());
1569 Secure.putIntForUser(getContext().getContentResolver(),
1570 Secure.NOTIFICATION_BADGING, 1,
1571 USER2.getIdentifier());
1572 mHelper.updateBadgingEnabled(); // would be called by settings observer
1573 assertFalse(mHelper.badgingEnabled(USER));
1574 assertTrue(mHelper.badgingEnabled(USER2));
1575 }
Julia Reynolds816797a2017-08-11 15:47:09 -04001576
1577 @Test
1578 public void testOnLocaleChanged_updatesDefaultChannels() throws Exception {
1579 String newLabel = "bananas!";
1580 final NotificationChannel defaultChannel = mHelper.getNotificationChannel(PKG, UID,
1581 NotificationChannel.DEFAULT_CHANNEL_ID, false);
1582 assertFalse(newLabel.equals(defaultChannel.getName()));
1583
1584 Resources res = mock(Resources.class);
1585 when(mContext.getResources()).thenReturn(res);
1586 when(res.getString(com.android.internal.R.string.default_notification_channel_label))
1587 .thenReturn(newLabel);
1588
1589 mHelper.onLocaleChanged(mContext, USER.getIdentifier());
1590
1591 assertEquals(newLabel, mHelper.getNotificationChannel(PKG, UID,
1592 NotificationChannel.DEFAULT_CHANNEL_ID, false).getName());
1593 }
Julia Reynolds005c8b92017-08-24 10:35:53 -04001594
1595 @Test
1596 public void testIsGroupBlocked_noGroup() throws Exception {
1597 assertFalse(mHelper.isGroupBlocked(PKG, UID, null));
1598
1599 assertFalse(mHelper.isGroupBlocked(PKG, UID, "non existent group"));
1600 }
1601
1602 @Test
1603 public void testIsGroupBlocked_notBlocked() throws Exception {
1604 NotificationChannelGroup group = new NotificationChannelGroup("id", "name");
1605 mHelper.createNotificationChannelGroup(PKG, UID, group, true);
1606
1607 assertFalse(mHelper.isGroupBlocked(PKG, UID, group.getId()));
1608 }
1609
1610 @Test
1611 public void testIsGroupBlocked_blocked() throws Exception {
1612 NotificationChannelGroup group = new NotificationChannelGroup("id", "name");
1613 mHelper.createNotificationChannelGroup(PKG, UID, group, true);
1614 group.setBlocked(true);
1615 mHelper.createNotificationChannelGroup(PKG, UID, group, false);
1616
1617 assertTrue(mHelper.isGroupBlocked(PKG, UID, group.getId()));
1618 }
1619
1620 @Test
1621 public void testIsGroup_appCannotResetBlock() throws Exception {
1622 NotificationChannelGroup group = new NotificationChannelGroup("id", "name");
1623 mHelper.createNotificationChannelGroup(PKG, UID, group, true);
1624 NotificationChannelGroup group2 = group.clone();
1625 group2.setBlocked(true);
1626 mHelper.createNotificationChannelGroup(PKG, UID, group2, false);
1627 assertTrue(mHelper.isGroupBlocked(PKG, UID, group.getId()));
1628
1629 NotificationChannelGroup group3 = group.clone();
1630 group3.setBlocked(false);
1631 mHelper.createNotificationChannelGroup(PKG, UID, group3, true);
1632 assertTrue(mHelper.isGroupBlocked(PKG, UID, group.getId()));
1633 }
1634
1635 @Test
1636 public void testGetNotificationChannelGroupWithChannels() throws Exception {
1637 NotificationChannelGroup group = new NotificationChannelGroup("group", "");
1638 NotificationChannelGroup other = new NotificationChannelGroup("something else", "");
1639 mHelper.createNotificationChannelGroup(PKG, UID, group, true);
1640 mHelper.createNotificationChannelGroup(PKG, UID, other, true);
1641
1642 NotificationChannel a = new NotificationChannel("a", "a", IMPORTANCE_DEFAULT);
1643 a.setGroup(group.getId());
1644 NotificationChannel b = new NotificationChannel("b", "b", IMPORTANCE_DEFAULT);
1645 b.setGroup(other.getId());
1646 NotificationChannel c = new NotificationChannel("c", "c", IMPORTANCE_DEFAULT);
1647 c.setGroup(group.getId());
1648 NotificationChannel d = new NotificationChannel("d", "d", IMPORTANCE_DEFAULT);
1649
Julia Reynolds1fe10942018-03-28 12:46:51 -04001650 mHelper.createNotificationChannel(PKG, UID, a, true, false);
1651 mHelper.createNotificationChannel(PKG, UID, b, true, false);
1652 mHelper.createNotificationChannel(PKG, UID, c, true, false);
1653 mHelper.createNotificationChannel(PKG, UID, d, true, false);
Julia Reynolds005c8b92017-08-24 10:35:53 -04001654 mHelper.deleteNotificationChannel(PKG, UID, c.getId());
1655
1656 NotificationChannelGroup retrieved = mHelper.getNotificationChannelGroupWithChannels(
1657 PKG, UID, group.getId(), true);
1658 assertEquals(2, retrieved.getChannels().size());
1659 compareChannels(a, findChannel(retrieved.getChannels(), a.getId()));
1660 compareChannels(c, findChannel(retrieved.getChannels(), c.getId()));
1661
1662 retrieved = mHelper.getNotificationChannelGroupWithChannels(
1663 PKG, UID, group.getId(), false);
1664 assertEquals(1, retrieved.getChannels().size());
1665 compareChannels(a, findChannel(retrieved.getChannels(), a.getId()));
1666 }
Julia Reynoldsbcc62ea2018-03-19 10:26:32 -04001667
1668 @Test
1669 public void testAndroidPkgCanBypassDnd_creation() {
Julia Reynoldsbcc62ea2018-03-19 10:26:32 -04001670 NotificationChannel test = new NotificationChannel("A", "a", IMPORTANCE_LOW);
1671 test.setBypassDnd(true);
1672
Julia Reynolds1fe10942018-03-28 12:46:51 -04001673 mHelper.createNotificationChannel(SYSTEM_PKG, SYSTEM_UID, test, true, false);
Julia Reynoldsbcc62ea2018-03-19 10:26:32 -04001674
1675 assertTrue(mHelper.getNotificationChannel(SYSTEM_PKG, SYSTEM_UID, "A", false)
1676 .canBypassDnd());
1677 }
1678
1679 @Test
Julia Reynolds1fe10942018-03-28 12:46:51 -04001680 public void testDndPkgCanBypassDnd_creation() {
1681 NotificationChannel test = new NotificationChannel("A", "a", IMPORTANCE_LOW);
1682 test.setBypassDnd(true);
1683
1684 mHelper.createNotificationChannel(PKG, UID, test, true, true);
1685
1686 assertTrue(mHelper.getNotificationChannel(PKG, UID, "A", false).canBypassDnd());
1687 }
1688
1689 @Test
Julia Reynoldsbcc62ea2018-03-19 10:26:32 -04001690 public void testNormalPkgCannotBypassDnd_creation() {
1691 NotificationChannel test = new NotificationChannel("A", "a", IMPORTANCE_LOW);
1692 test.setBypassDnd(true);
1693
Julia Reynolds1fe10942018-03-28 12:46:51 -04001694 mHelper.createNotificationChannel(PKG, 1000, test, true, false);
Julia Reynoldsbcc62ea2018-03-19 10:26:32 -04001695
1696 assertFalse(mHelper.getNotificationChannel(PKG, 1000, "A", false).canBypassDnd());
1697 }
1698
1699 @Test
1700 public void testAndroidPkgCanBypassDnd_update() throws Exception {
1701 NotificationChannel test = new NotificationChannel("A", "a", IMPORTANCE_LOW);
Julia Reynolds1fe10942018-03-28 12:46:51 -04001702 mHelper.createNotificationChannel(SYSTEM_PKG, SYSTEM_UID, test, true, false);
Julia Reynoldsbcc62ea2018-03-19 10:26:32 -04001703
1704 NotificationChannel update = new NotificationChannel("A", "a", IMPORTANCE_LOW);
1705 update.setBypassDnd(true);
Julia Reynolds1fe10942018-03-28 12:46:51 -04001706 mHelper.createNotificationChannel(SYSTEM_PKG, SYSTEM_UID, update, true, false);
Julia Reynoldsbcc62ea2018-03-19 10:26:32 -04001707
1708 assertTrue(mHelper.getNotificationChannel(SYSTEM_PKG, SYSTEM_UID, "A", false)
1709 .canBypassDnd());
1710
1711 // setup + 1st check
1712 verify(mPm, times(2)).getPackageInfoAsUser(any(), anyInt(), anyInt());
1713 }
1714
1715 @Test
Julia Reynolds1fe10942018-03-28 12:46:51 -04001716 public void testDndPkgCanBypassDnd_update() throws Exception {
Julia Reynoldsbcc62ea2018-03-19 10:26:32 -04001717 NotificationChannel test = new NotificationChannel("A", "a", IMPORTANCE_LOW);
Julia Reynolds1fe10942018-03-28 12:46:51 -04001718 mHelper.createNotificationChannel(PKG, UID, test, true, true);
1719
Julia Reynoldsbcc62ea2018-03-19 10:26:32 -04001720 NotificationChannel update = new NotificationChannel("A", "a", IMPORTANCE_LOW);
1721 update.setBypassDnd(true);
Julia Reynolds1fe10942018-03-28 12:46:51 -04001722 mHelper.createNotificationChannel(PKG, UID, update, true, true);
1723
1724 assertTrue(mHelper.getNotificationChannel(PKG, UID, "A", false).canBypassDnd());
1725 }
1726
1727 @Test
1728 public void testNormalPkgCannotBypassDnd_update() {
1729 NotificationChannel test = new NotificationChannel("A", "a", IMPORTANCE_LOW);
1730 mHelper.createNotificationChannel(PKG, 1000, test, true, false);
1731 NotificationChannel update = new NotificationChannel("A", "a", IMPORTANCE_LOW);
1732 update.setBypassDnd(true);
1733 mHelper.createNotificationChannel(PKG, 1000, update, true, false);
Julia Reynoldsbcc62ea2018-03-19 10:26:32 -04001734 assertFalse(mHelper.getNotificationChannel(PKG, 1000, "A", false).canBypassDnd());
1735 }
Julia Reynoldse273f082018-04-12 13:48:49 -04001736
1737 @Test
1738 public void testGetBlockedAppCount_noApps() {
1739 assertEquals(0, mHelper.getBlockedAppCount(0));
1740 }
1741
1742 @Test
1743 public void testGetBlockedAppCount_noAppsForUserId() {
1744 mHelper.setEnabled(PKG, 100, false);
1745 assertEquals(0, mHelper.getBlockedAppCount(9));
1746 }
1747
1748 @Test
1749 public void testGetBlockedAppCount_appsForUserId() {
1750 mHelper.setEnabled(PKG, 1020, false);
1751 mHelper.setEnabled(PKG, 1030, false);
1752 mHelper.setEnabled(PKG, 1060, false);
1753 mHelper.setEnabled(PKG, 1000, true);
1754 assertEquals(3, mHelper.getBlockedAppCount(0));
1755 }
Chris Wren1031c972014-07-23 13:11:45 +00001756}