blob: 07b21fbc8f5f66beab0d72bf868ac9c5e3f39f78 [file] [log] [blame]
Julia Reynolds72f1cbb2016-09-19 14:57:31 -04001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.server.notification;
17
18import org.junit.Before;
19import org.junit.Test;
20import org.junit.runner.RunWith;
Julia Reynolds50989772017-02-23 14:32:16 -050021import org.mockito.ArgumentCaptor;
Julia Reynolds72f1cbb2016-09-19 14:57:31 -040022import org.mockito.Mock;
23import org.mockito.MockitoAnnotations;
24
25import android.app.AlarmManager;
26import android.app.Notification;
27import android.app.NotificationChannel;
Julia Reynolds85769912016-10-25 09:08:57 -040028import android.app.NotificationManager;
Julia Reynolds72f1cbb2016-09-19 14:57:31 -040029import android.app.PendingIntent;
Julia Reynolds50989772017-02-23 14:32:16 -050030import android.os.SystemClock;
Julia Reynolds72f1cbb2016-09-19 14:57:31 -040031import android.os.UserHandle;
32import android.service.notification.StatusBarNotification;
Julia Reynolds72f1cbb2016-09-19 14:57:31 -040033import android.support.test.runner.AndroidJUnit4;
34import android.test.suitebuilder.annotation.SmallTest;
Julia Reynoldsa78cdff2017-04-26 10:19:25 -040035import android.util.Slog;
Julia Reynolds72f1cbb2016-09-19 14:57:31 -040036
Julia Reynoldscf63ff12017-01-24 13:55:48 -050037import static junit.framework.Assert.assertEquals;
Julia Reynolds72f1cbb2016-09-19 14:57:31 -040038import static junit.framework.Assert.assertFalse;
39import static junit.framework.Assert.assertTrue;
40import static org.mockito.Matchers.any;
41import static org.mockito.Matchers.anyInt;
Julia Reynoldsb6c1f992016-11-22 09:26:46 -050042import static org.mockito.Matchers.anyLong;
Julia Reynolds72f1cbb2016-09-19 14:57:31 -040043import static org.mockito.Matchers.eq;
44import static org.mockito.Mockito.never;
45import static org.mockito.Mockito.times;
46import static org.mockito.Mockito.verify;
Julia Reynoldscf63ff12017-01-24 13:55:48 -050047import static org.mockito.Mockito.when;
Julia Reynolds72f1cbb2016-09-19 14:57:31 -040048
49
50@SmallTest
51@RunWith(AndroidJUnit4.class)
Chris Wren89aa2262017-05-05 18:05:56 -040052public class SnoozeHelperTest extends NotificationTestCase {
Geoffrey Pitschaf759c52017-02-15 09:35:38 -050053 private static final String TEST_CHANNEL_ID = "test_channel_id";
54
Julia Reynolds72f1cbb2016-09-19 14:57:31 -040055 @Mock SnoozeHelper.Callback mCallback;
56 @Mock AlarmManager mAm;
57 @Mock ManagedServices.UserProfiles mUserProfiles;
58
59 private SnoozeHelper mSnoozeHelper;
60
Julia Reynolds72f1cbb2016-09-19 14:57:31 -040061 @Before
62 public void setUp() {
63 MockitoAnnotations.initMocks(this);
64
65 mSnoozeHelper = new SnoozeHelper(getContext(), mCallback, mUserProfiles);
66 mSnoozeHelper.setAlarmManager(mAm);
67 }
68
69 @Test
Julia Reynoldsb6c1f992016-11-22 09:26:46 -050070 public void testSnoozeForTime() throws Exception {
Julia Reynolds72f1cbb2016-09-19 14:57:31 -040071 NotificationRecord r = getNotificationRecord("pkg", 1, "one", UserHandle.SYSTEM);
Julia Reynolds79672302017-01-12 08:30:16 -050072 mSnoozeHelper.snooze(r, 1000);
Julia Reynolds50989772017-02-23 14:32:16 -050073 ArgumentCaptor<Long> captor = ArgumentCaptor.forClass(Long.class);
Julia Reynolds72f1cbb2016-09-19 14:57:31 -040074 verify(mAm, times(1)).setExactAndAllowWhileIdle(
Julia Reynolds50989772017-02-23 14:32:16 -050075 anyInt(), captor.capture(), any(PendingIntent.class));
76 long actualSnoozedUntilDuration = captor.getValue() - SystemClock.elapsedRealtime();
Julia Reynolds4024b662017-03-01 09:33:41 -050077 assertTrue(Math.abs(actualSnoozedUntilDuration - 1000) < 25);
Julia Reynolds72f1cbb2016-09-19 14:57:31 -040078 assertTrue(mSnoozeHelper.isSnoozed(
79 UserHandle.USER_SYSTEM, r.sbn.getPackageName(), r.getKey()));
80 }
81
82 @Test
Julia Reynoldsb6c1f992016-11-22 09:26:46 -050083 public void testSnooze() throws Exception {
84 NotificationRecord r = getNotificationRecord("pkg", 1, "one", UserHandle.SYSTEM);
Julia Reynolds79672302017-01-12 08:30:16 -050085 mSnoozeHelper.snooze(r);
Julia Reynoldsb6c1f992016-11-22 09:26:46 -050086 verify(mAm, never()).setExactAndAllowWhileIdle(
87 anyInt(), anyLong(), any(PendingIntent.class));
88 assertTrue(mSnoozeHelper.isSnoozed(
89 UserHandle.USER_SYSTEM, r.sbn.getPackageName(), r.getKey()));
90 }
91
92 @Test
Julia Reynolds72f1cbb2016-09-19 14:57:31 -040093 public void testCancelByApp() throws Exception {
94 NotificationRecord r = getNotificationRecord("pkg", 1, "one", UserHandle.SYSTEM);
95 NotificationRecord r2 = getNotificationRecord("pkg", 2, "two", UserHandle.SYSTEM);
Julia Reynolds79672302017-01-12 08:30:16 -050096 mSnoozeHelper.snooze(r, 1000);
97 mSnoozeHelper.snooze(r2 , 1000);
Julia Reynolds72f1cbb2016-09-19 14:57:31 -040098 assertTrue(mSnoozeHelper.isSnoozed(
99 UserHandle.USER_SYSTEM, r.sbn.getPackageName(), r.getKey()));
100 assertTrue(mSnoozeHelper.isSnoozed(
101 UserHandle.USER_SYSTEM, r2.sbn.getPackageName(), r2.getKey()));
102
103 mSnoozeHelper.cancel(UserHandle.USER_SYSTEM, r.sbn.getPackageName(), "one", 1);
Julia Reynoldsa8b766f2017-03-07 16:30:21 -0500104 // 2 = one for each snooze, above, zero for the cancel.
105 verify(mAm, times(2)).cancel(any(PendingIntent.class));
106 assertTrue(mSnoozeHelper.isSnoozed(
Julia Reynolds72f1cbb2016-09-19 14:57:31 -0400107 UserHandle.USER_SYSTEM, r.sbn.getPackageName(), r.getKey()));
108 assertTrue(mSnoozeHelper.isSnoozed(
109 UserHandle.USER_SYSTEM, r2.sbn.getPackageName(), r2.getKey()));
110 }
111
112 @Test
113 public void testCancelAllForUser() throws Exception {
114 NotificationRecord r = getNotificationRecord("pkg", 1, "one", UserHandle.SYSTEM);
115 NotificationRecord r2 = getNotificationRecord("pkg", 2, "two", UserHandle.SYSTEM);
116 NotificationRecord r3 = getNotificationRecord("pkg", 3, "three", UserHandle.ALL);
Julia Reynolds79672302017-01-12 08:30:16 -0500117 mSnoozeHelper.snooze(r, 1000);
118 mSnoozeHelper.snooze(r2, 1000);
119 mSnoozeHelper.snooze(r3, 1000);
Julia Reynolds72f1cbb2016-09-19 14:57:31 -0400120 assertTrue(mSnoozeHelper.isSnoozed(
121 UserHandle.USER_SYSTEM, r.sbn.getPackageName(), r.getKey()));
122 assertTrue(mSnoozeHelper.isSnoozed(
123 UserHandle.USER_SYSTEM, r2.sbn.getPackageName(), r2.getKey()));
124 assertTrue(mSnoozeHelper.isSnoozed(
125 UserHandle.USER_ALL, r3.sbn.getPackageName(), r3.getKey()));
126
127 mSnoozeHelper.cancel(UserHandle.USER_SYSTEM, false);
Julia Reynoldsa8b766f2017-03-07 16:30:21 -0500128 // 3 = once for each snooze above (3), only.
129 verify(mAm, times(3)).cancel(any(PendingIntent.class));
130 assertTrue(mSnoozeHelper.isSnoozed(
Julia Reynolds72f1cbb2016-09-19 14:57:31 -0400131 UserHandle.USER_SYSTEM, r.sbn.getPackageName(), r.getKey()));
Julia Reynoldsa8b766f2017-03-07 16:30:21 -0500132 assertTrue(mSnoozeHelper.isSnoozed(
Julia Reynolds72f1cbb2016-09-19 14:57:31 -0400133 UserHandle.USER_SYSTEM, r2.sbn.getPackageName(), r2.getKey()));
134 assertTrue(mSnoozeHelper.isSnoozed(
135 UserHandle.USER_ALL, r3.sbn.getPackageName(), r3.getKey()));
136 }
137
138 @Test
139 public void testCancelAllByApp() throws Exception {
140 NotificationRecord r = getNotificationRecord("pkg", 1, "one", UserHandle.SYSTEM);
141 NotificationRecord r2 = getNotificationRecord("pkg", 2, "two", UserHandle.SYSTEM);
142 NotificationRecord r3 = getNotificationRecord("pkg2", 3, "three", UserHandle.SYSTEM);
Julia Reynolds79672302017-01-12 08:30:16 -0500143 mSnoozeHelper.snooze(r, 1000);
144 mSnoozeHelper.snooze(r2, 1000);
145 mSnoozeHelper.snooze(r3, 1000);
Julia Reynolds72f1cbb2016-09-19 14:57:31 -0400146 assertTrue(mSnoozeHelper.isSnoozed(
147 UserHandle.USER_SYSTEM, r.sbn.getPackageName(), r.getKey()));
148 assertTrue(mSnoozeHelper.isSnoozed(
149 UserHandle.USER_SYSTEM, r2.sbn.getPackageName(), r2.getKey()));
150 assertTrue(mSnoozeHelper.isSnoozed(
151 UserHandle.USER_SYSTEM, r3.sbn.getPackageName(), r3.getKey()));
152
153 mSnoozeHelper.cancel(UserHandle.USER_SYSTEM, "pkg2");
Julia Reynoldsa8b766f2017-03-07 16:30:21 -0500154 // 3 = once for each snooze above (3), only.
155 verify(mAm, times(3)).cancel(any(PendingIntent.class));
Julia Reynolds72f1cbb2016-09-19 14:57:31 -0400156 assertTrue(mSnoozeHelper.isSnoozed(
157 UserHandle.USER_SYSTEM, r.sbn.getPackageName(), r.getKey()));
158 assertTrue(mSnoozeHelper.isSnoozed(
159 UserHandle.USER_SYSTEM, r2.sbn.getPackageName(), r2.getKey()));
Julia Reynoldsa8b766f2017-03-07 16:30:21 -0500160 assertTrue(mSnoozeHelper.isSnoozed(
Julia Reynolds72f1cbb2016-09-19 14:57:31 -0400161 UserHandle.USER_SYSTEM, r3.sbn.getPackageName(), r3.getKey()));
162 }
163
164 @Test
Julia Reynoldsa8b766f2017-03-07 16:30:21 -0500165 public void testCancelDoesNotUnsnooze() throws Exception {
166 NotificationRecord r = getNotificationRecord("pkg", 1, "one", UserHandle.SYSTEM);
167 mSnoozeHelper.snooze(r, 1000);
168 assertTrue(mSnoozeHelper.isSnoozed(
169 UserHandle.USER_SYSTEM, r.sbn.getPackageName(), r.getKey()));
170
171 mSnoozeHelper.cancel(UserHandle.USER_SYSTEM, r.sbn.getPackageName(), "one", 1);
172
173 assertTrue(mSnoozeHelper.isSnoozed(
174 UserHandle.USER_SYSTEM, r.sbn.getPackageName(), r.getKey()));
175 }
176
177 @Test
178 public void testCancelDoesNotRepost() throws Exception {
179 NotificationRecord r = getNotificationRecord("pkg", 1, "one", UserHandle.SYSTEM);
180 NotificationRecord r2 = getNotificationRecord("pkg", 2, "two", UserHandle.SYSTEM);
181 mSnoozeHelper.snooze(r, 1000);
182 mSnoozeHelper.snooze(r2 , 1000);
183 assertTrue(mSnoozeHelper.isSnoozed(
184 UserHandle.USER_SYSTEM, r.sbn.getPackageName(), r.getKey()));
185 assertTrue(mSnoozeHelper.isSnoozed(
186 UserHandle.USER_SYSTEM, r2.sbn.getPackageName(), r2.getKey()));
187
188 mSnoozeHelper.cancel(UserHandle.USER_SYSTEM, r.sbn.getPackageName(), "one", 1);
189
190 mSnoozeHelper.repost(r.getKey(), UserHandle.USER_SYSTEM);
191 verify(mCallback, never()).repost(UserHandle.USER_SYSTEM, r);
192 }
193
194 @Test
Julia Reynolds72f1cbb2016-09-19 14:57:31 -0400195 public void testRepost() throws Exception {
196 NotificationRecord r = getNotificationRecord("pkg", 1, "one", UserHandle.SYSTEM);
Julia Reynolds79672302017-01-12 08:30:16 -0500197 mSnoozeHelper.snooze(r, 1000);
Julia Reynolds72f1cbb2016-09-19 14:57:31 -0400198 NotificationRecord r2 = getNotificationRecord("pkg", 2, "one", UserHandle.ALL);
Julia Reynolds79672302017-01-12 08:30:16 -0500199 mSnoozeHelper.snooze(r2, 1000);
Julia Reynoldsb6c1f992016-11-22 09:26:46 -0500200 mSnoozeHelper.repost(r.getKey(), UserHandle.USER_SYSTEM);
Julia Reynolds72f1cbb2016-09-19 14:57:31 -0400201 verify(mCallback, times(1)).repost(UserHandle.USER_SYSTEM, r);
202 }
203
204 @Test
Julia Reynolds79672302017-01-12 08:30:16 -0500205 public void testRepost_noUser() throws Exception {
206 NotificationRecord r = getNotificationRecord("pkg", 1, "one", UserHandle.SYSTEM);
207 mSnoozeHelper.snooze(r, 1000);
208 NotificationRecord r2 = getNotificationRecord("pkg", 2, "one", UserHandle.ALL);
209 mSnoozeHelper.snooze(r2, 1000);
210 mSnoozeHelper.repost(r.getKey());
211 verify(mCallback, times(1)).repost(UserHandle.USER_SYSTEM, r);
212 }
213
214 @Test
Julia Reynolds72f1cbb2016-09-19 14:57:31 -0400215 public void testUpdate() throws Exception {
216 NotificationRecord r = getNotificationRecord("pkg", 1, "one", UserHandle.SYSTEM);
Julia Reynolds79672302017-01-12 08:30:16 -0500217 mSnoozeHelper.snooze(r , 1000);
Julia Reynolds72f1cbb2016-09-19 14:57:31 -0400218 r.getNotification().category = "NEW CATEGORY";
219
220 mSnoozeHelper.update(UserHandle.USER_SYSTEM, r);
221 verify(mCallback, never()).repost(anyInt(), any(NotificationRecord.class));
222
Julia Reynoldsb6c1f992016-11-22 09:26:46 -0500223 mSnoozeHelper.repost(r.getKey(), UserHandle.USER_SYSTEM);
Julia Reynolds72f1cbb2016-09-19 14:57:31 -0400224 verify(mCallback, times(1)).repost(UserHandle.USER_SYSTEM, r);
225 }
226
Julia Reynoldscf63ff12017-01-24 13:55:48 -0500227 @Test
228 public void testGetSnoozedByUser() throws Exception {
229 NotificationRecord r = getNotificationRecord("pkg", 1, "one", UserHandle.SYSTEM);
230 NotificationRecord r2 = getNotificationRecord("pkg", 2, "two", UserHandle.SYSTEM);
231 NotificationRecord r3 = getNotificationRecord("pkg2", 3, "three", UserHandle.SYSTEM);
232 NotificationRecord r4 = getNotificationRecord("pkg2", 3, "three", UserHandle.CURRENT);
233 mSnoozeHelper.snooze(r, 1000);
234 mSnoozeHelper.snooze(r2, 1000);
235 mSnoozeHelper.snooze(r3, 1000);
236 mSnoozeHelper.snooze(r4, 1000);
237 when(mUserProfiles.getCurrentProfileIds()).thenReturn(
238 new int[] {UserHandle.USER_SYSTEM});
239 assertEquals(3, mSnoozeHelper.getSnoozed().size());
240 when(mUserProfiles.getCurrentProfileIds()).thenReturn(
241 new int[] {UserHandle.USER_CURRENT});
242 assertEquals(1, mSnoozeHelper.getSnoozed().size());
243 }
244
245 @Test
246 public void testGetSnoozedByUser_managedProfiles() throws Exception {
247 when(mUserProfiles.getCurrentProfileIds()).thenReturn(
248 new int[] {UserHandle.USER_SYSTEM, UserHandle.USER_CURRENT});
249 NotificationRecord r = getNotificationRecord("pkg", 1, "one", UserHandle.SYSTEM);
250 NotificationRecord r2 = getNotificationRecord("pkg", 2, "two", UserHandle.SYSTEM);
251 NotificationRecord r3 = getNotificationRecord("pkg2", 3, "three", UserHandle.SYSTEM);
252 NotificationRecord r4 = getNotificationRecord("pkg2", 3, "three", UserHandle.CURRENT);
253 mSnoozeHelper.snooze(r, 1000);
254 mSnoozeHelper.snooze(r2, 1000);
255 mSnoozeHelper.snooze(r3, 1000);
256 mSnoozeHelper.snooze(r4, 1000);
257 assertEquals(4, mSnoozeHelper.getSnoozed().size());
258 }
259
Julia Reynoldsa78cdff2017-04-26 10:19:25 -0400260 @Test
261 public void repostGroupSummary_onlyFellowGroupChildren() throws Exception {
262 NotificationRecord r = getNotificationRecord(
263 "pkg", 1, "one", UserHandle.SYSTEM, "group1", false);
264 NotificationRecord r2 = getNotificationRecord(
265 "pkg", 2, "two", UserHandle.SYSTEM, "group1", false);
266 mSnoozeHelper.snooze(r, 1000);
267 mSnoozeHelper.snooze(r2, 1000);
268 mSnoozeHelper.repostGroupSummary("pkg", UserHandle.USER_SYSTEM, "group1");
269
270 verify(mCallback, never()).repost(UserHandle.USER_SYSTEM, r);
271 }
272
273 @Test
274 public void repostGroupSummary_repostsSummary() throws Exception {
275 when(mUserProfiles.getCurrentProfileIds()).thenReturn(
276 new int[] {UserHandle.USER_SYSTEM});
277 NotificationRecord r = getNotificationRecord(
278 "pkg", 1, "one", UserHandle.SYSTEM, "group1", true);
279 NotificationRecord r2 = getNotificationRecord(
280 "pkg", 2, "two", UserHandle.SYSTEM, "group1", false);
281 mSnoozeHelper.snooze(r, 1000);
282 mSnoozeHelper.snooze(r2, 1000);
283 assertEquals(2, mSnoozeHelper.getSnoozed().size());
284 assertEquals(2, mSnoozeHelper.getSnoozed(UserHandle.USER_SYSTEM, "pkg").size());
285
286 mSnoozeHelper.repostGroupSummary("pkg", UserHandle.USER_SYSTEM, r.getGroupKey());
287
288 verify(mCallback, times(1)).repost(UserHandle.USER_SYSTEM, r);
289 verify(mCallback, never()).repost(UserHandle.USER_SYSTEM, r2);
290
291 assertEquals(1, mSnoozeHelper.getSnoozed().size());
292 assertEquals(1, mSnoozeHelper.getSnoozed(UserHandle.USER_SYSTEM, "pkg").size());
293 }
294
Julia Reynolds72f1cbb2016-09-19 14:57:31 -0400295 private NotificationRecord getNotificationRecord(String pkg, int id, String tag,
Julia Reynoldsa78cdff2017-04-26 10:19:25 -0400296 UserHandle user, String groupKey, boolean groupSummary) {
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500297 Notification n = new Notification.Builder(getContext(), TEST_CHANNEL_ID)
Julia Reynolds72f1cbb2016-09-19 14:57:31 -0400298 .setContentTitle("A")
299 .setGroup("G")
300 .setSortKey("A")
301 .setWhen(1205)
Julia Reynoldsa78cdff2017-04-26 10:19:25 -0400302 .setGroup(groupKey)
303 .setGroupSummary(groupSummary)
Julia Reynolds72f1cbb2016-09-19 14:57:31 -0400304 .build();
Geoffrey Pitschaf759c52017-02-15 09:35:38 -0500305 final NotificationChannel notificationChannel = new NotificationChannel(
306 TEST_CHANNEL_ID, "name", NotificationManager.IMPORTANCE_LOW);
Julia Reynolds72f1cbb2016-09-19 14:57:31 -0400307 return new NotificationRecord(getContext(), new StatusBarNotification(
Julia Reynolds924eed12017-01-19 09:52:07 -0500308 pkg, pkg, id, tag, 0, 0, n, user, null,
Geoffrey Pitscha22f6442017-05-05 16:47:38 +0000309 System.currentTimeMillis()), notificationChannel);
Julia Reynolds423b9fc2016-11-09 09:51:08 -0500310 }
Julia Reynoldsa78cdff2017-04-26 10:19:25 -0400311
312 private NotificationRecord getNotificationRecord(String pkg, int id, String tag,
313 UserHandle user) {
314 return getNotificationRecord(pkg, id, tag, user, null, false);
315 }
Julia Reynolds72f1cbb2016-09-19 14:57:31 -0400316}