Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.android.server.notification; |
| 17 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 18 | import static junit.framework.Assert.assertEquals; |
| 19 | import static junit.framework.Assert.assertFalse; |
| 20 | import static junit.framework.Assert.assertTrue; |
| 21 | |
| 22 | import static org.mockito.Matchers.any; |
| 23 | import static org.mockito.Matchers.anyInt; |
| 24 | import static org.mockito.Matchers.anyLong; |
| 25 | import static org.mockito.Mockito.never; |
| 26 | import static org.mockito.Mockito.times; |
| 27 | import static org.mockito.Mockito.verify; |
| 28 | import static org.mockito.Mockito.when; |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 29 | |
| 30 | import android.app.AlarmManager; |
| 31 | import android.app.Notification; |
| 32 | import android.app.NotificationChannel; |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 33 | import android.app.NotificationManager; |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 34 | import android.app.PendingIntent; |
Julia Reynolds | 5098977 | 2017-02-23 14:32:16 -0500 | [diff] [blame] | 35 | import android.os.SystemClock; |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 36 | import android.os.UserHandle; |
| 37 | import android.service.notification.StatusBarNotification; |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 38 | import android.test.suitebuilder.annotation.SmallTest; |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 39 | import android.util.IntArray; |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 40 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 41 | import androidx.test.runner.AndroidJUnit4; |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 42 | |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 43 | import com.android.server.UiServiceTestCase; |
| 44 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 45 | import org.junit.Before; |
| 46 | import org.junit.Test; |
| 47 | import org.junit.runner.RunWith; |
| 48 | import org.mockito.ArgumentCaptor; |
| 49 | import org.mockito.Mock; |
| 50 | import org.mockito.MockitoAnnotations; |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 51 | |
| 52 | @SmallTest |
| 53 | @RunWith(AndroidJUnit4.class) |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 54 | public class SnoozeHelperTest extends UiServiceTestCase { |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 55 | private static final String TEST_CHANNEL_ID = "test_channel_id"; |
| 56 | |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 57 | @Mock SnoozeHelper.Callback mCallback; |
| 58 | @Mock AlarmManager mAm; |
| 59 | @Mock ManagedServices.UserProfiles mUserProfiles; |
| 60 | |
| 61 | private SnoozeHelper mSnoozeHelper; |
| 62 | |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 63 | @Before |
| 64 | public void setUp() { |
| 65 | MockitoAnnotations.initMocks(this); |
| 66 | |
| 67 | mSnoozeHelper = new SnoozeHelper(getContext(), mCallback, mUserProfiles); |
| 68 | mSnoozeHelper.setAlarmManager(mAm); |
| 69 | } |
| 70 | |
| 71 | @Test |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 72 | public void testSnoozeForTime() throws Exception { |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 73 | NotificationRecord r = getNotificationRecord("pkg", 1, "one", UserHandle.SYSTEM); |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 74 | mSnoozeHelper.snooze(r, 1000); |
Julia Reynolds | 5098977 | 2017-02-23 14:32:16 -0500 | [diff] [blame] | 75 | ArgumentCaptor<Long> captor = ArgumentCaptor.forClass(Long.class); |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 76 | verify(mAm, times(1)).setExactAndAllowWhileIdle( |
Julia Reynolds | 5098977 | 2017-02-23 14:32:16 -0500 | [diff] [blame] | 77 | anyInt(), captor.capture(), any(PendingIntent.class)); |
| 78 | long actualSnoozedUntilDuration = captor.getValue() - SystemClock.elapsedRealtime(); |
Julia Reynolds | 0a091095 | 2018-12-19 10:44:20 -0500 | [diff] [blame] | 79 | assertTrue(Math.abs(actualSnoozedUntilDuration - 1000) < 250); |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 80 | assertTrue(mSnoozeHelper.isSnoozed( |
| 81 | UserHandle.USER_SYSTEM, r.sbn.getPackageName(), r.getKey())); |
| 82 | } |
| 83 | |
| 84 | @Test |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 85 | public void testSnooze() throws Exception { |
| 86 | NotificationRecord r = getNotificationRecord("pkg", 1, "one", UserHandle.SYSTEM); |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 87 | mSnoozeHelper.snooze(r); |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 88 | verify(mAm, never()).setExactAndAllowWhileIdle( |
| 89 | anyInt(), anyLong(), any(PendingIntent.class)); |
| 90 | assertTrue(mSnoozeHelper.isSnoozed( |
| 91 | UserHandle.USER_SYSTEM, r.sbn.getPackageName(), r.getKey())); |
| 92 | } |
| 93 | |
| 94 | @Test |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 95 | public void testCancelByApp() throws Exception { |
| 96 | NotificationRecord r = getNotificationRecord("pkg", 1, "one", UserHandle.SYSTEM); |
| 97 | NotificationRecord r2 = getNotificationRecord("pkg", 2, "two", UserHandle.SYSTEM); |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 98 | mSnoozeHelper.snooze(r, 1000); |
| 99 | mSnoozeHelper.snooze(r2 , 1000); |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 100 | assertTrue(mSnoozeHelper.isSnoozed( |
| 101 | UserHandle.USER_SYSTEM, r.sbn.getPackageName(), r.getKey())); |
| 102 | assertTrue(mSnoozeHelper.isSnoozed( |
| 103 | UserHandle.USER_SYSTEM, r2.sbn.getPackageName(), r2.getKey())); |
| 104 | |
| 105 | mSnoozeHelper.cancel(UserHandle.USER_SYSTEM, r.sbn.getPackageName(), "one", 1); |
Julia Reynolds | a8b766f | 2017-03-07 16:30:21 -0500 | [diff] [blame] | 106 | // 2 = one for each snooze, above, zero for the cancel. |
| 107 | verify(mAm, times(2)).cancel(any(PendingIntent.class)); |
| 108 | assertTrue(mSnoozeHelper.isSnoozed( |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 109 | UserHandle.USER_SYSTEM, r.sbn.getPackageName(), r.getKey())); |
| 110 | assertTrue(mSnoozeHelper.isSnoozed( |
| 111 | UserHandle.USER_SYSTEM, r2.sbn.getPackageName(), r2.getKey())); |
| 112 | } |
| 113 | |
| 114 | @Test |
| 115 | public void testCancelAllForUser() throws Exception { |
| 116 | NotificationRecord r = getNotificationRecord("pkg", 1, "one", UserHandle.SYSTEM); |
| 117 | NotificationRecord r2 = getNotificationRecord("pkg", 2, "two", UserHandle.SYSTEM); |
| 118 | NotificationRecord r3 = getNotificationRecord("pkg", 3, "three", UserHandle.ALL); |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 119 | mSnoozeHelper.snooze(r, 1000); |
| 120 | mSnoozeHelper.snooze(r2, 1000); |
| 121 | mSnoozeHelper.snooze(r3, 1000); |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 122 | assertTrue(mSnoozeHelper.isSnoozed( |
| 123 | UserHandle.USER_SYSTEM, r.sbn.getPackageName(), r.getKey())); |
| 124 | assertTrue(mSnoozeHelper.isSnoozed( |
| 125 | UserHandle.USER_SYSTEM, r2.sbn.getPackageName(), r2.getKey())); |
| 126 | assertTrue(mSnoozeHelper.isSnoozed( |
| 127 | UserHandle.USER_ALL, r3.sbn.getPackageName(), r3.getKey())); |
| 128 | |
| 129 | mSnoozeHelper.cancel(UserHandle.USER_SYSTEM, false); |
Julia Reynolds | a8b766f | 2017-03-07 16:30:21 -0500 | [diff] [blame] | 130 | // 3 = once for each snooze above (3), only. |
| 131 | verify(mAm, times(3)).cancel(any(PendingIntent.class)); |
| 132 | assertTrue(mSnoozeHelper.isSnoozed( |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 133 | UserHandle.USER_SYSTEM, r.sbn.getPackageName(), r.getKey())); |
Julia Reynolds | a8b766f | 2017-03-07 16:30:21 -0500 | [diff] [blame] | 134 | assertTrue(mSnoozeHelper.isSnoozed( |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 135 | UserHandle.USER_SYSTEM, r2.sbn.getPackageName(), r2.getKey())); |
| 136 | assertTrue(mSnoozeHelper.isSnoozed( |
| 137 | UserHandle.USER_ALL, r3.sbn.getPackageName(), r3.getKey())); |
| 138 | } |
| 139 | |
| 140 | @Test |
| 141 | public void testCancelAllByApp() throws Exception { |
| 142 | NotificationRecord r = getNotificationRecord("pkg", 1, "one", UserHandle.SYSTEM); |
| 143 | NotificationRecord r2 = getNotificationRecord("pkg", 2, "two", UserHandle.SYSTEM); |
| 144 | NotificationRecord r3 = getNotificationRecord("pkg2", 3, "three", UserHandle.SYSTEM); |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 145 | mSnoozeHelper.snooze(r, 1000); |
| 146 | mSnoozeHelper.snooze(r2, 1000); |
| 147 | mSnoozeHelper.snooze(r3, 1000); |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 148 | assertTrue(mSnoozeHelper.isSnoozed( |
| 149 | UserHandle.USER_SYSTEM, r.sbn.getPackageName(), r.getKey())); |
| 150 | assertTrue(mSnoozeHelper.isSnoozed( |
| 151 | UserHandle.USER_SYSTEM, r2.sbn.getPackageName(), r2.getKey())); |
| 152 | assertTrue(mSnoozeHelper.isSnoozed( |
| 153 | UserHandle.USER_SYSTEM, r3.sbn.getPackageName(), r3.getKey())); |
| 154 | |
| 155 | mSnoozeHelper.cancel(UserHandle.USER_SYSTEM, "pkg2"); |
Julia Reynolds | a8b766f | 2017-03-07 16:30:21 -0500 | [diff] [blame] | 156 | // 3 = once for each snooze above (3), only. |
| 157 | verify(mAm, times(3)).cancel(any(PendingIntent.class)); |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 158 | assertTrue(mSnoozeHelper.isSnoozed( |
| 159 | UserHandle.USER_SYSTEM, r.sbn.getPackageName(), r.getKey())); |
| 160 | assertTrue(mSnoozeHelper.isSnoozed( |
| 161 | UserHandle.USER_SYSTEM, r2.sbn.getPackageName(), r2.getKey())); |
Julia Reynolds | a8b766f | 2017-03-07 16:30:21 -0500 | [diff] [blame] | 162 | assertTrue(mSnoozeHelper.isSnoozed( |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 163 | UserHandle.USER_SYSTEM, r3.sbn.getPackageName(), r3.getKey())); |
| 164 | } |
| 165 | |
| 166 | @Test |
Julia Reynolds | a8b766f | 2017-03-07 16:30:21 -0500 | [diff] [blame] | 167 | public void testCancelDoesNotUnsnooze() throws Exception { |
| 168 | NotificationRecord r = getNotificationRecord("pkg", 1, "one", UserHandle.SYSTEM); |
| 169 | mSnoozeHelper.snooze(r, 1000); |
| 170 | assertTrue(mSnoozeHelper.isSnoozed( |
| 171 | UserHandle.USER_SYSTEM, r.sbn.getPackageName(), r.getKey())); |
| 172 | |
| 173 | mSnoozeHelper.cancel(UserHandle.USER_SYSTEM, r.sbn.getPackageName(), "one", 1); |
| 174 | |
| 175 | assertTrue(mSnoozeHelper.isSnoozed( |
| 176 | UserHandle.USER_SYSTEM, r.sbn.getPackageName(), r.getKey())); |
| 177 | } |
| 178 | |
| 179 | @Test |
| 180 | public void testCancelDoesNotRepost() throws Exception { |
| 181 | NotificationRecord r = getNotificationRecord("pkg", 1, "one", UserHandle.SYSTEM); |
| 182 | NotificationRecord r2 = getNotificationRecord("pkg", 2, "two", UserHandle.SYSTEM); |
| 183 | mSnoozeHelper.snooze(r, 1000); |
| 184 | mSnoozeHelper.snooze(r2 , 1000); |
| 185 | assertTrue(mSnoozeHelper.isSnoozed( |
| 186 | UserHandle.USER_SYSTEM, r.sbn.getPackageName(), r.getKey())); |
| 187 | assertTrue(mSnoozeHelper.isSnoozed( |
| 188 | UserHandle.USER_SYSTEM, r2.sbn.getPackageName(), r2.getKey())); |
| 189 | |
| 190 | mSnoozeHelper.cancel(UserHandle.USER_SYSTEM, r.sbn.getPackageName(), "one", 1); |
| 191 | |
| 192 | mSnoozeHelper.repost(r.getKey(), UserHandle.USER_SYSTEM); |
| 193 | verify(mCallback, never()).repost(UserHandle.USER_SYSTEM, r); |
| 194 | } |
| 195 | |
| 196 | @Test |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 197 | public void testRepost() throws Exception { |
| 198 | NotificationRecord r = getNotificationRecord("pkg", 1, "one", UserHandle.SYSTEM); |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 199 | mSnoozeHelper.snooze(r, 1000); |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 200 | NotificationRecord r2 = getNotificationRecord("pkg", 2, "one", UserHandle.ALL); |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 201 | mSnoozeHelper.snooze(r2, 1000); |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 202 | mSnoozeHelper.repost(r.getKey(), UserHandle.USER_SYSTEM); |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 203 | verify(mCallback, times(1)).repost(UserHandle.USER_SYSTEM, r); |
| 204 | } |
| 205 | |
| 206 | @Test |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 207 | public void testRepost_noUser() throws Exception { |
| 208 | NotificationRecord r = getNotificationRecord("pkg", 1, "one", UserHandle.SYSTEM); |
| 209 | mSnoozeHelper.snooze(r, 1000); |
| 210 | NotificationRecord r2 = getNotificationRecord("pkg", 2, "one", UserHandle.ALL); |
| 211 | mSnoozeHelper.snooze(r2, 1000); |
| 212 | mSnoozeHelper.repost(r.getKey()); |
| 213 | verify(mCallback, times(1)).repost(UserHandle.USER_SYSTEM, r); |
| 214 | } |
| 215 | |
| 216 | @Test |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 217 | public void testUpdate() throws Exception { |
| 218 | NotificationRecord r = getNotificationRecord("pkg", 1, "one", UserHandle.SYSTEM); |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 219 | mSnoozeHelper.snooze(r , 1000); |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 220 | r.getNotification().category = "NEW CATEGORY"; |
| 221 | |
| 222 | mSnoozeHelper.update(UserHandle.USER_SYSTEM, r); |
| 223 | verify(mCallback, never()).repost(anyInt(), any(NotificationRecord.class)); |
| 224 | |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 225 | mSnoozeHelper.repost(r.getKey(), UserHandle.USER_SYSTEM); |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 226 | verify(mCallback, times(1)).repost(UserHandle.USER_SYSTEM, r); |
| 227 | } |
| 228 | |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 229 | @Test |
| 230 | public void testGetSnoozedByUser() throws Exception { |
| 231 | NotificationRecord r = getNotificationRecord("pkg", 1, "one", UserHandle.SYSTEM); |
| 232 | NotificationRecord r2 = getNotificationRecord("pkg", 2, "two", UserHandle.SYSTEM); |
| 233 | NotificationRecord r3 = getNotificationRecord("pkg2", 3, "three", UserHandle.SYSTEM); |
| 234 | NotificationRecord r4 = getNotificationRecord("pkg2", 3, "three", UserHandle.CURRENT); |
| 235 | mSnoozeHelper.snooze(r, 1000); |
| 236 | mSnoozeHelper.snooze(r2, 1000); |
| 237 | mSnoozeHelper.snooze(r3, 1000); |
| 238 | mSnoozeHelper.snooze(r4, 1000); |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 239 | IntArray profileIds = new IntArray(); |
| 240 | profileIds.add(UserHandle.USER_SYSTEM); |
| 241 | when(mUserProfiles.getCurrentProfileIds()).thenReturn(profileIds); |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 242 | assertEquals(3, mSnoozeHelper.getSnoozed().size()); |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 243 | profileIds = new IntArray(); |
| 244 | profileIds.add(UserHandle.USER_CURRENT); |
| 245 | when(mUserProfiles.getCurrentProfileIds()).thenReturn(profileIds); |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 246 | assertEquals(1, mSnoozeHelper.getSnoozed().size()); |
| 247 | } |
| 248 | |
| 249 | @Test |
| 250 | public void testGetSnoozedByUser_managedProfiles() throws Exception { |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 251 | IntArray profileIds = new IntArray(); |
| 252 | profileIds.add(UserHandle.USER_CURRENT); |
| 253 | profileIds.add(UserHandle.USER_SYSTEM); |
| 254 | when(mUserProfiles.getCurrentProfileIds()).thenReturn(profileIds); |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 255 | NotificationRecord r = getNotificationRecord("pkg", 1, "one", UserHandle.SYSTEM); |
| 256 | NotificationRecord r2 = getNotificationRecord("pkg", 2, "two", UserHandle.SYSTEM); |
| 257 | NotificationRecord r3 = getNotificationRecord("pkg2", 3, "three", UserHandle.SYSTEM); |
| 258 | NotificationRecord r4 = getNotificationRecord("pkg2", 3, "three", UserHandle.CURRENT); |
| 259 | mSnoozeHelper.snooze(r, 1000); |
| 260 | mSnoozeHelper.snooze(r2, 1000); |
| 261 | mSnoozeHelper.snooze(r3, 1000); |
| 262 | mSnoozeHelper.snooze(r4, 1000); |
| 263 | assertEquals(4, mSnoozeHelper.getSnoozed().size()); |
| 264 | } |
| 265 | |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 266 | @Test |
| 267 | public void repostGroupSummary_onlyFellowGroupChildren() throws Exception { |
| 268 | NotificationRecord r = getNotificationRecord( |
| 269 | "pkg", 1, "one", UserHandle.SYSTEM, "group1", false); |
| 270 | NotificationRecord r2 = getNotificationRecord( |
| 271 | "pkg", 2, "two", UserHandle.SYSTEM, "group1", false); |
| 272 | mSnoozeHelper.snooze(r, 1000); |
| 273 | mSnoozeHelper.snooze(r2, 1000); |
| 274 | mSnoozeHelper.repostGroupSummary("pkg", UserHandle.USER_SYSTEM, "group1"); |
| 275 | |
| 276 | verify(mCallback, never()).repost(UserHandle.USER_SYSTEM, r); |
| 277 | } |
| 278 | |
| 279 | @Test |
| 280 | public void repostGroupSummary_repostsSummary() throws Exception { |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 281 | IntArray profileIds = new IntArray(); |
| 282 | profileIds.add(UserHandle.USER_SYSTEM); |
| 283 | when(mUserProfiles.getCurrentProfileIds()).thenReturn(profileIds); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 284 | NotificationRecord r = getNotificationRecord( |
| 285 | "pkg", 1, "one", UserHandle.SYSTEM, "group1", true); |
| 286 | NotificationRecord r2 = getNotificationRecord( |
| 287 | "pkg", 2, "two", UserHandle.SYSTEM, "group1", false); |
| 288 | mSnoozeHelper.snooze(r, 1000); |
| 289 | mSnoozeHelper.snooze(r2, 1000); |
| 290 | assertEquals(2, mSnoozeHelper.getSnoozed().size()); |
| 291 | assertEquals(2, mSnoozeHelper.getSnoozed(UserHandle.USER_SYSTEM, "pkg").size()); |
| 292 | |
| 293 | mSnoozeHelper.repostGroupSummary("pkg", UserHandle.USER_SYSTEM, r.getGroupKey()); |
| 294 | |
| 295 | verify(mCallback, times(1)).repost(UserHandle.USER_SYSTEM, r); |
| 296 | verify(mCallback, never()).repost(UserHandle.USER_SYSTEM, r2); |
| 297 | |
| 298 | assertEquals(1, mSnoozeHelper.getSnoozed().size()); |
| 299 | assertEquals(1, mSnoozeHelper.getSnoozed(UserHandle.USER_SYSTEM, "pkg").size()); |
| 300 | } |
| 301 | |
Julia Reynolds | 67c1e96 | 2019-01-04 14:01:10 -0500 | [diff] [blame] | 302 | @Test |
| 303 | public void testClearData() { |
| 304 | // snooze 2 from same package |
| 305 | NotificationRecord r = getNotificationRecord("pkg", 1, "one", UserHandle.SYSTEM); |
| 306 | NotificationRecord r2 = getNotificationRecord("pkg", 2, "two", UserHandle.SYSTEM); |
| 307 | mSnoozeHelper.snooze(r, 1000); |
| 308 | mSnoozeHelper.snooze(r2, 1000); |
| 309 | assertTrue(mSnoozeHelper.isSnoozed( |
| 310 | UserHandle.USER_SYSTEM, r.sbn.getPackageName(), r.getKey())); |
| 311 | assertTrue(mSnoozeHelper.isSnoozed( |
| 312 | UserHandle.USER_SYSTEM, r2.sbn.getPackageName(), r2.getKey())); |
| 313 | |
| 314 | // clear data |
| 315 | mSnoozeHelper.clearData(UserHandle.USER_SYSTEM, "pkg"); |
| 316 | |
| 317 | // nothing snoozed; alarms canceled |
| 318 | assertFalse(mSnoozeHelper.isSnoozed( |
| 319 | UserHandle.USER_SYSTEM, r.sbn.getPackageName(), r.getKey())); |
| 320 | assertFalse(mSnoozeHelper.isSnoozed( |
| 321 | UserHandle.USER_SYSTEM, r2.sbn.getPackageName(), r2.getKey())); |
| 322 | // twice for initial snooze, twice for canceling the snooze |
| 323 | verify(mAm, times(4)).cancel(any(PendingIntent.class)); |
| 324 | } |
| 325 | |
| 326 | @Test |
| 327 | public void testClearData_otherRecordsUntouched() { |
| 328 | // 2 packages, 2 users |
| 329 | NotificationRecord r = getNotificationRecord("pkg", 1, "one", UserHandle.SYSTEM); |
| 330 | NotificationRecord r2 = getNotificationRecord("pkg", 2, "two", UserHandle.ALL); |
| 331 | NotificationRecord r3 = getNotificationRecord("pkg2", 3, "three", UserHandle.SYSTEM); |
| 332 | mSnoozeHelper.snooze(r, 1000); |
| 333 | mSnoozeHelper.snooze(r2, 1000); |
| 334 | mSnoozeHelper.snooze(r3, 1000); |
| 335 | assertTrue(mSnoozeHelper.isSnoozed( |
| 336 | UserHandle.USER_SYSTEM, r.sbn.getPackageName(), r.getKey())); |
| 337 | assertTrue(mSnoozeHelper.isSnoozed( |
| 338 | UserHandle.USER_ALL, r2.sbn.getPackageName(), r2.getKey())); |
| 339 | assertTrue(mSnoozeHelper.isSnoozed( |
| 340 | UserHandle.USER_SYSTEM, r3.sbn.getPackageName(), r3.getKey())); |
| 341 | |
| 342 | // clear data |
| 343 | mSnoozeHelper.clearData(UserHandle.USER_SYSTEM, "pkg"); |
| 344 | |
| 345 | assertFalse(mSnoozeHelper.isSnoozed( |
| 346 | UserHandle.USER_SYSTEM, r.sbn.getPackageName(), r.getKey())); |
| 347 | assertTrue(mSnoozeHelper.isSnoozed( |
| 348 | UserHandle.USER_ALL, r2.sbn.getPackageName(), r2.getKey())); |
| 349 | assertTrue(mSnoozeHelper.isSnoozed( |
| 350 | UserHandle.USER_SYSTEM, r3.sbn.getPackageName(), r3.getKey())); |
| 351 | // once for each initial snooze, once for canceling one snooze |
| 352 | verify(mAm, times(4)).cancel(any(PendingIntent.class)); |
| 353 | } |
| 354 | |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 355 | private NotificationRecord getNotificationRecord(String pkg, int id, String tag, |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 356 | UserHandle user, String groupKey, boolean groupSummary) { |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 357 | Notification n = new Notification.Builder(getContext(), TEST_CHANNEL_ID) |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 358 | .setContentTitle("A") |
| 359 | .setGroup("G") |
| 360 | .setSortKey("A") |
| 361 | .setWhen(1205) |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 362 | .setGroup(groupKey) |
| 363 | .setGroupSummary(groupSummary) |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 364 | .build(); |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 365 | final NotificationChannel notificationChannel = new NotificationChannel( |
| 366 | TEST_CHANNEL_ID, "name", NotificationManager.IMPORTANCE_LOW); |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 367 | return new NotificationRecord(getContext(), new StatusBarNotification( |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 368 | pkg, pkg, id, tag, 0, 0, n, user, null, |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 369 | System.currentTimeMillis()), notificationChannel); |
Julia Reynolds | 423b9fc | 2016-11-09 09:51:08 -0500 | [diff] [blame] | 370 | } |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 371 | |
| 372 | private NotificationRecord getNotificationRecord(String pkg, int id, String tag, |
| 373 | UserHandle user) { |
| 374 | return getNotificationRecord(pkg, id, tag, user, null, false); |
| 375 | } |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 376 | } |