blob: 9f36a1eb9943cd7f04b10cea7f228cdbef99a80e [file] [log] [blame]
Maurice Lam132710e2017-03-03 19:13:42 -08001/*
2 * Copyright (C) 2017 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
Rohan Shah20790b82018-07-02 17:21:04 -070014 * limitations under the License
Maurice Lam132710e2017-03-03 19:13:42 -080015 */
16
Ned Burnsf81c4c42019-01-07 14:10:43 -050017package com.android.systemui.statusbar.notification.collection;
Maurice Lam132710e2017-03-03 19:13:42 -080018
Julia Reynoldsfc640012018-02-21 12:25:27 -050019import static android.app.AppOpsManager.OP_ACCEPT_HANDOVER;
20import static android.app.AppOpsManager.OP_CAMERA;
Julia Reynolds25926af2018-05-01 17:05:33 -040021import static android.app.Notification.CATEGORY_ALARM;
22import static android.app.Notification.CATEGORY_CALL;
23import static android.app.Notification.CATEGORY_EVENT;
24import static android.app.Notification.CATEGORY_MESSAGE;
25import static android.app.Notification.CATEGORY_REMINDER;
Julia Reynoldsefcd8bd2019-02-06 08:56:15 -050026import static android.app.NotificationManager.IMPORTANCE_LOW;
27import static android.app.NotificationManager.IMPORTANCE_MIN;
28
29import static com.android.systemui.statusbar.notification.collection.NotificationDataTest.TestableNotificationData.OVERRIDE_CHANNEL;
30import static com.android.systemui.statusbar.notification.collection.NotificationDataTest.TestableNotificationData.OVERRIDE_IMPORTANCE;
31import static com.android.systemui.statusbar.notification.collection.NotificationDataTest.TestableNotificationData.OVERRIDE_VIS_EFFECTS;
Julia Reynoldsfc640012018-02-21 12:25:27 -050032
33import static junit.framework.Assert.assertEquals;
34
Maurice Lam132710e2017-03-03 19:13:42 -080035import static org.junit.Assert.assertFalse;
36import static org.junit.Assert.assertTrue;
Julia Reynoldsfc640012018-02-21 12:25:27 -050037import static org.mockito.ArgumentMatchers.any;
Julia Reynoldsefcd8bd2019-02-06 08:56:15 -050038import static org.mockito.ArgumentMatchers.anyString;
Maurice Lam132710e2017-03-03 19:13:42 -080039import static org.mockito.Matchers.eq;
40import static org.mockito.Mockito.mock;
41import static org.mockito.Mockito.when;
42
43import android.Manifest;
44import android.app.Notification;
Selim Cinek608a57a2017-04-28 16:50:41 -070045import android.app.NotificationChannel;
Julia Reynoldsefcd8bd2019-02-06 08:56:15 -050046import android.app.NotificationManager;
Tony Mak628cb932018-06-19 18:30:41 +010047import android.app.PendingIntent;
Beverlye98937a2018-11-15 10:18:57 -050048import android.app.Person;
Tony Mak628cb932018-06-19 18:30:41 +010049import android.content.Intent;
Maurice Lam132710e2017-03-03 19:13:42 -080050import android.content.pm.IPackageManager;
51import android.content.pm.PackageManager;
Tony Mak628cb932018-06-19 18:30:41 +010052import android.graphics.drawable.Icon;
Julia Reynolds5bbb6da2018-03-28 10:48:37 -040053import android.media.session.MediaSession;
Maurice Lam132710e2017-03-03 19:13:42 -080054import android.os.Bundle;
Yohei Yukawa72769462019-01-20 09:28:08 -080055import android.os.Process;
Selim Cinek608a57a2017-04-28 16:50:41 -070056import android.service.notification.NotificationListenerService;
Tony Mak628cb932018-06-19 18:30:41 +010057import android.service.notification.NotificationListenerService.Ranking;
58import android.service.notification.SnoozeCriterion;
Maurice Lam132710e2017-03-03 19:13:42 -080059import android.service.notification.StatusBarNotification;
Maurice Lam132710e2017-03-03 19:13:42 -080060import android.support.test.filters.SmallTest;
Jason Monk6dceace2018-05-15 20:24:07 -040061import android.testing.AndroidTestingRunner;
Jason Monka716bac2018-12-05 15:48:21 -050062import android.testing.TestableLooper;
Jason Monk6dceace2018-05-15 20:24:07 -040063import android.testing.TestableLooper.RunWithLooper;
Julia Reynoldsfc640012018-02-21 12:25:27 -050064import android.util.ArraySet;
Maurice Lam132710e2017-03-03 19:13:42 -080065
Jason Monk297c04e2018-08-23 17:16:59 -040066import com.android.systemui.Dependency;
Julia Reynoldsfc640012018-02-21 12:25:27 -050067import com.android.systemui.ForegroundServiceController;
Jason Monk297c04e2018-08-23 17:16:59 -040068import com.android.systemui.InitController;
Maurice Lam132710e2017-03-03 19:13:42 -080069import com.android.systemui.SysuiTestCase;
Rohan Shah20790b82018-07-02 17:21:04 -070070import com.android.systemui.statusbar.NotificationTestHelper;
Ned Burnsf81c4c42019-01-07 14:10:43 -050071import com.android.systemui.statusbar.notification.collection.NotificationData;
72import com.android.systemui.statusbar.notification.collection.NotificationData.KeyguardEnvironment;
73import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Rohan Shah20790b82018-07-02 17:21:04 -070074import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow;
Selim Cinek608a57a2017-04-28 16:50:41 -070075import com.android.systemui.statusbar.phone.NotificationGroupManager;
Jason Monk297c04e2018-08-23 17:16:59 -040076import com.android.systemui.statusbar.phone.ShadeController;
Maurice Lam132710e2017-03-03 19:13:42 -080077
78import org.junit.Before;
79import org.junit.Test;
80import org.junit.runner.RunWith;
Julia Reynoldsfc640012018-02-21 12:25:27 -050081import org.mockito.Mock;
82import org.mockito.MockitoAnnotations;
Maurice Lam132710e2017-03-03 19:13:42 -080083
Dieter Hsu36e1ebc2018-06-06 15:41:46 +080084import java.util.ArrayList;
Gustav Sennton1463d832018-11-06 16:12:48 +000085import java.util.Collections;
86import java.util.List;
Dieter Hsu36e1ebc2018-06-06 15:41:46 +080087
Maurice Lam132710e2017-03-03 19:13:42 -080088@SmallTest
Jason Monk6dceace2018-05-15 20:24:07 -040089@RunWith(AndroidTestingRunner.class)
Jason Monka716bac2018-12-05 15:48:21 -050090@RunWithLooper
Maurice Lam132710e2017-03-03 19:13:42 -080091public class NotificationDataTest extends SysuiTestCase {
92
93 private static final int UID_NORMAL = 123;
94 private static final int UID_ALLOW_DURING_SETUP = 456;
Tony Mak628cb932018-06-19 18:30:41 +010095 private static final NotificationChannel NOTIFICATION_CHANNEL =
96 new NotificationChannel("id", "name", NotificationChannel.USER_LOCKED_IMPORTANCE);
Maurice Lam132710e2017-03-03 19:13:42 -080097
98 private final StatusBarNotification mMockStatusBarNotification =
99 mock(StatusBarNotification.class);
Julia Reynoldsfc640012018-02-21 12:25:27 -0500100 @Mock
101 ForegroundServiceController mFsc;
102 @Mock
Jason Monk297c04e2018-08-23 17:16:59 -0400103 NotificationData.KeyguardEnvironment mEnvironment;
Maurice Lam132710e2017-03-03 19:13:42 -0800104
105 private final IPackageManager mMockPackageManager = mock(IPackageManager.class);
Julia Reynoldsefcd8bd2019-02-06 08:56:15 -0500106 private TestableNotificationData mNotificationData;
Selim Cinek608a57a2017-04-28 16:50:41 -0700107 private ExpandableNotificationRow mRow;
Maurice Lam132710e2017-03-03 19:13:42 -0800108
109 @Before
110 public void setUp() throws Exception {
Jason Monka716bac2018-12-05 15:48:21 -0500111 com.android.systemui.util.Assert.sMainLooper = TestableLooper.get(this).getLooper();
Julia Reynoldsfc640012018-02-21 12:25:27 -0500112 MockitoAnnotations.initMocks(this);
Maurice Lam132710e2017-03-03 19:13:42 -0800113 when(mMockStatusBarNotification.getUid()).thenReturn(UID_NORMAL);
Gus Prevasec9e1f02018-12-18 15:28:12 -0500114 when(mMockStatusBarNotification.cloneLight()).thenReturn(mMockStatusBarNotification);
Maurice Lam132710e2017-03-03 19:13:42 -0800115
116 when(mMockPackageManager.checkUidPermission(
117 eq(Manifest.permission.NOTIFICATION_DURING_SETUP),
118 eq(UID_NORMAL)))
119 .thenReturn(PackageManager.PERMISSION_DENIED);
120 when(mMockPackageManager.checkUidPermission(
121 eq(Manifest.permission.NOTIFICATION_DURING_SETUP),
122 eq(UID_ALLOW_DURING_SETUP)))
123 .thenReturn(PackageManager.PERMISSION_GRANTED);
Selim Cinek608a57a2017-04-28 16:50:41 -0700124
Julia Reynoldsfc640012018-02-21 12:25:27 -0500125 mDependency.injectTestDependency(ForegroundServiceController.class, mFsc);
Jason Monk297c04e2018-08-23 17:16:59 -0400126 mDependency.injectTestDependency(NotificationGroupManager.class,
127 new NotificationGroupManager());
128 mDependency.injectMockDependency(ShadeController.class);
129 mDependency.injectTestDependency(KeyguardEnvironment.class, mEnvironment);
Julia Reynoldsfc640012018-02-21 12:25:27 -0500130 when(mEnvironment.isDeviceProvisioned()).thenReturn(true);
131 when(mEnvironment.isNotificationForCurrentProfiles(any())).thenReturn(true);
Jason Monk297c04e2018-08-23 17:16:59 -0400132 mNotificationData = new TestableNotificationData();
Selim Cinek608a57a2017-04-28 16:50:41 -0700133 mNotificationData.updateRanking(mock(NotificationListenerService.RankingMap.class));
134 mRow = new NotificationTestHelper(getContext()).createRow();
Tony Mak7d4b3a52018-11-27 17:29:36 +0000135 Dependency.get(InitController.class).executePostInitTasks();
Maurice Lam132710e2017-03-03 19:13:42 -0800136 }
137
138 @Test
Selim Cinek608a57a2017-04-28 16:50:41 -0700139 public void testChannelSetWhenAdded() {
Julia Reynoldsefcd8bd2019-02-06 08:56:15 -0500140 mNotificationData.rankingOverrides.putParcelable(OVERRIDE_CHANNEL, NOTIFICATION_CHANNEL);
Selim Cinek608a57a2017-04-28 16:50:41 -0700141 mNotificationData.add(mRow.getEntry());
Tony Mak628cb932018-06-19 18:30:41 +0100142 assertEquals(NOTIFICATION_CHANNEL, mRow.getEntry().channel);
Selim Cinek608a57a2017-04-28 16:50:41 -0700143 }
144
Julia Reynoldsfc640012018-02-21 12:25:27 -0500145 @Test
146 public void testAllRelevantNotisTaggedWithAppOps() throws Exception {
147 mNotificationData.add(mRow.getEntry());
148 ExpandableNotificationRow row2 = new NotificationTestHelper(getContext()).createRow();
149 mNotificationData.add(row2.getEntry());
150 ExpandableNotificationRow diffPkg =
Yohei Yukawa72769462019-01-20 09:28:08 -0800151 new NotificationTestHelper(getContext()).createRow("pkg", 4000,
152 Process.myUserHandle());
Julia Reynoldsfc640012018-02-21 12:25:27 -0500153 mNotificationData.add(diffPkg.getEntry());
154
155 ArraySet<Integer> expectedOps = new ArraySet<>();
156 expectedOps.add(OP_CAMERA);
157 expectedOps.add(OP_ACCEPT_HANDOVER);
158
159 for (int op : expectedOps) {
160 mNotificationData.updateAppOp(op, NotificationTestHelper.UID,
Julia Reynolds91590062018-04-02 16:24:11 -0400161 NotificationTestHelper.PKG, mRow.getEntry().key, true);
162 mNotificationData.updateAppOp(op, NotificationTestHelper.UID,
163 NotificationTestHelper.PKG, row2.getEntry().key, true);
Julia Reynoldsfc640012018-02-21 12:25:27 -0500164 }
165 for (int op : expectedOps) {
166 assertTrue(mRow.getEntry().key + " doesn't have op " + op,
167 mNotificationData.get(mRow.getEntry().key).mActiveAppOps.contains(op));
168 assertTrue(row2.getEntry().key + " doesn't have op " + op,
169 mNotificationData.get(row2.getEntry().key).mActiveAppOps.contains(op));
170 assertFalse(diffPkg.getEntry().key + " has op " + op,
171 mNotificationData.get(diffPkg.getEntry().key).mActiveAppOps.contains(op));
172 }
173 }
174
175 @Test
176 public void testAppOpsRemoval() throws Exception {
177 mNotificationData.add(mRow.getEntry());
178 ExpandableNotificationRow row2 = new NotificationTestHelper(getContext()).createRow();
179 mNotificationData.add(row2.getEntry());
180
181 ArraySet<Integer> expectedOps = new ArraySet<>();
182 expectedOps.add(OP_CAMERA);
183 expectedOps.add(OP_ACCEPT_HANDOVER);
184
185 for (int op : expectedOps) {
186 mNotificationData.updateAppOp(op, NotificationTestHelper.UID,
Julia Reynolds91590062018-04-02 16:24:11 -0400187 NotificationTestHelper.PKG, row2.getEntry().key, true);
Julia Reynoldsfc640012018-02-21 12:25:27 -0500188 }
189
190 expectedOps.remove(OP_ACCEPT_HANDOVER);
191 mNotificationData.updateAppOp(OP_ACCEPT_HANDOVER, NotificationTestHelper.UID,
Julia Reynolds91590062018-04-02 16:24:11 -0400192 NotificationTestHelper.PKG, row2.getEntry().key, false);
Julia Reynoldsfc640012018-02-21 12:25:27 -0500193
194 assertTrue(mRow.getEntry().key + " doesn't have op " + OP_CAMERA,
195 mNotificationData.get(mRow.getEntry().key).mActiveAppOps.contains(OP_CAMERA));
196 assertTrue(row2.getEntry().key + " doesn't have op " + OP_CAMERA,
197 mNotificationData.get(row2.getEntry().key).mActiveAppOps.contains(OP_CAMERA));
198 assertFalse(mRow.getEntry().key + " has op " + OP_ACCEPT_HANDOVER,
199 mNotificationData.get(mRow.getEntry().key)
200 .mActiveAppOps.contains(OP_ACCEPT_HANDOVER));
201 assertFalse(row2.getEntry().key + " has op " + OP_ACCEPT_HANDOVER,
202 mNotificationData.get(row2.getEntry().key)
203 .mActiveAppOps.contains(OP_ACCEPT_HANDOVER));
204 }
205
206 @Test
Dieter Hsu36e1ebc2018-06-06 15:41:46 +0800207 public void testGetNotificationsForCurrentUser_shouldFilterNonCurrentUserNotifications()
208 throws Exception {
209 mNotificationData.add(mRow.getEntry());
210 ExpandableNotificationRow row2 = new NotificationTestHelper(getContext()).createRow();
211 mNotificationData.add(row2.getEntry());
212
213 when(mEnvironment.isNotificationForCurrentProfiles(
214 mRow.getEntry().notification)).thenReturn(false);
215 when(mEnvironment.isNotificationForCurrentProfiles(
216 row2.getEntry().notification)).thenReturn(true);
Ned Burnsf81c4c42019-01-07 14:10:43 -0500217 ArrayList<NotificationEntry> result =
Dieter Hsu36e1ebc2018-06-06 15:41:46 +0800218 mNotificationData.getNotificationsForCurrentUser();
219
Jason Monk297c04e2018-08-23 17:16:59 -0400220 assertEquals(result.size(), 1);
221 junit.framework.Assert.assertEquals(result.get(0), row2.getEntry());
Dieter Hsu36e1ebc2018-06-06 15:41:46 +0800222 }
223
224 @Test
Julia Reynolds5bbb6da2018-03-28 10:48:37 -0400225 public void testIsExemptFromDndVisualSuppression_foreground() {
226 initStatusBarNotification(false);
Julia Reynoldsefcd8bd2019-02-06 08:56:15 -0500227
Julia Reynolds5bbb6da2018-03-28 10:48:37 -0400228 Notification n = mMockStatusBarNotification.getNotification();
229 n.flags = Notification.FLAG_FOREGROUND_SERVICE;
Ned Burnsf81c4c42019-01-07 14:10:43 -0500230 NotificationEntry entry = new NotificationEntry(mMockStatusBarNotification);
Gus Prevasec9e1f02018-12-18 15:28:12 -0500231 mNotificationData.add(entry);
Julia Reynoldsefcd8bd2019-02-06 08:56:15 -0500232 mNotificationData.rankingOverrides.putInt(OVERRIDE_VIS_EFFECTS, 255);
Julia Reynolds5bbb6da2018-03-28 10:48:37 -0400233
Gus Prevasec9e1f02018-12-18 15:28:12 -0500234 assertTrue(entry.isExemptFromDndVisualSuppression());
235 assertFalse(entry.shouldSuppressAmbient());
Julia Reynolds5bbb6da2018-03-28 10:48:37 -0400236 }
237
238 @Test
239 public void testIsExemptFromDndVisualSuppression_media() {
240 initStatusBarNotification(false);
Julia Reynolds5bbb6da2018-03-28 10:48:37 -0400241 Notification n = mMockStatusBarNotification.getNotification();
242 Notification.Builder nb = Notification.Builder.recoverBuilder(mContext, n);
243 nb.setStyle(new Notification.MediaStyle().setMediaSession(mock(MediaSession.Token.class)));
244 n = nb.build();
245 when(mMockStatusBarNotification.getNotification()).thenReturn(n);
Ned Burnsf81c4c42019-01-07 14:10:43 -0500246 NotificationEntry entry = new NotificationEntry(mMockStatusBarNotification);
Gus Prevasec9e1f02018-12-18 15:28:12 -0500247 mNotificationData.add(entry);
Julia Reynoldsefcd8bd2019-02-06 08:56:15 -0500248 mNotificationData.rankingOverrides.putInt(OVERRIDE_VIS_EFFECTS, 255);
Julia Reynolds5bbb6da2018-03-28 10:48:37 -0400249
Gus Prevasec9e1f02018-12-18 15:28:12 -0500250 assertTrue(entry.isExemptFromDndVisualSuppression());
251 assertFalse(entry.shouldSuppressAmbient());
Julia Reynoldsaa96cf32018-04-17 09:09:04 -0400252 }
253
254 @Test
255 public void testIsExemptFromDndVisualSuppression_system() {
256 initStatusBarNotification(false);
Ned Burnsf81c4c42019-01-07 14:10:43 -0500257 NotificationEntry entry = new NotificationEntry(mMockStatusBarNotification);
Julia Reynoldsaa96cf32018-04-17 09:09:04 -0400258 entry.mIsSystemNotification = true;
Gus Prevasec9e1f02018-12-18 15:28:12 -0500259 mNotificationData.add(entry);
Julia Reynoldsefcd8bd2019-02-06 08:56:15 -0500260 mNotificationData.rankingOverrides.putInt(OVERRIDE_VIS_EFFECTS, 255);
Julia Reynoldsaa96cf32018-04-17 09:09:04 -0400261
Gus Prevasec9e1f02018-12-18 15:28:12 -0500262 assertTrue(entry.isExemptFromDndVisualSuppression());
263 assertFalse(entry.shouldSuppressAmbient());
Julia Reynolds5bbb6da2018-03-28 10:48:37 -0400264 }
265
Julia Reynolds25926af2018-05-01 17:05:33 -0400266 @Test
267 public void testIsNotExemptFromDndVisualSuppression_hiddenCategories() {
268 initStatusBarNotification(false);
Ned Burnsf81c4c42019-01-07 14:10:43 -0500269 NotificationEntry entry = new NotificationEntry(mMockStatusBarNotification);
Julia Reynolds25926af2018-05-01 17:05:33 -0400270 entry.mIsSystemNotification = true;
Julia Reynoldsefcd8bd2019-02-06 08:56:15 -0500271 mNotificationData.rankingOverrides.putInt(OVERRIDE_VIS_EFFECTS,
272 NotificationManager.Policy.SUPPRESSED_EFFECT_AMBIENT);
Gus Prevasec9e1f02018-12-18 15:28:12 -0500273 mNotificationData.add(entry);
274
Julia Reynolds25926af2018-05-01 17:05:33 -0400275 when(mMockStatusBarNotification.getNotification()).thenReturn(
276 new Notification.Builder(mContext, "").setCategory(CATEGORY_CALL).build());
277
Gus Prevasec9e1f02018-12-18 15:28:12 -0500278 assertFalse(entry.isExemptFromDndVisualSuppression());
279 assertTrue(entry.shouldSuppressAmbient());
Julia Reynolds25926af2018-05-01 17:05:33 -0400280
281 when(mMockStatusBarNotification.getNotification()).thenReturn(
282 new Notification.Builder(mContext, "").setCategory(CATEGORY_REMINDER).build());
283
Gus Prevasec9e1f02018-12-18 15:28:12 -0500284 assertFalse(entry.isExemptFromDndVisualSuppression());
Julia Reynolds25926af2018-05-01 17:05:33 -0400285
286 when(mMockStatusBarNotification.getNotification()).thenReturn(
287 new Notification.Builder(mContext, "").setCategory(CATEGORY_ALARM).build());
288
Gus Prevasec9e1f02018-12-18 15:28:12 -0500289 assertFalse(entry.isExemptFromDndVisualSuppression());
Julia Reynolds25926af2018-05-01 17:05:33 -0400290
291 when(mMockStatusBarNotification.getNotification()).thenReturn(
292 new Notification.Builder(mContext, "").setCategory(CATEGORY_EVENT).build());
293
Gus Prevasec9e1f02018-12-18 15:28:12 -0500294 assertFalse(entry.isExemptFromDndVisualSuppression());
Julia Reynolds25926af2018-05-01 17:05:33 -0400295
296 when(mMockStatusBarNotification.getNotification()).thenReturn(
297 new Notification.Builder(mContext, "").setCategory(CATEGORY_MESSAGE).build());
298
Gus Prevasec9e1f02018-12-18 15:28:12 -0500299 assertFalse(entry.isExemptFromDndVisualSuppression());
Julia Reynolds25926af2018-05-01 17:05:33 -0400300 }
301
Tony Mak628cb932018-06-19 18:30:41 +0100302 @Test
303 public void testCreateNotificationDataEntry_RankingUpdate() {
304 Ranking ranking = mock(Ranking.class);
Gustav Sennton1463d832018-11-06 16:12:48 +0000305 initStatusBarNotification(false);
Tony Mak628cb932018-06-19 18:30:41 +0100306
Gustav Sennton1463d832018-11-06 16:12:48 +0000307 List<Notification.Action> appGeneratedSmartActions =
308 Collections.singletonList(createContextualAction("appGeneratedAction"));
309 mMockStatusBarNotification.getNotification().actions =
310 appGeneratedSmartActions.toArray(new Notification.Action[0]);
311
312 List<Notification.Action> systemGeneratedSmartActions =
313 Collections.singletonList(createAction("systemGeneratedAction"));
314 when(ranking.getSmartActions()).thenReturn(systemGeneratedSmartActions);
Tony Mak628cb932018-06-19 18:30:41 +0100315
316 when(ranking.getChannel()).thenReturn(NOTIFICATION_CHANNEL);
317
318 when(ranking.getUserSentiment()).thenReturn(Ranking.USER_SENTIMENT_NEGATIVE);
319
320 SnoozeCriterion snoozeCriterion = new SnoozeCriterion("id", "explanation", "confirmation");
321 ArrayList<SnoozeCriterion> snoozeCriterions = new ArrayList<>();
322 snoozeCriterions.add(snoozeCriterion);
323 when(ranking.getSnoozeCriteria()).thenReturn(snoozeCriterions);
324
Ned Burnsf81c4c42019-01-07 14:10:43 -0500325 NotificationEntry entry =
326 new NotificationEntry(mMockStatusBarNotification, ranking);
Tony Mak628cb932018-06-19 18:30:41 +0100327
Gustav Sennton1463d832018-11-06 16:12:48 +0000328 assertEquals(systemGeneratedSmartActions, entry.systemGeneratedSmartActions);
Tony Mak628cb932018-06-19 18:30:41 +0100329 assertEquals(NOTIFICATION_CHANNEL, entry.channel);
330 assertEquals(Ranking.USER_SENTIMENT_NEGATIVE, entry.userSentiment);
331 assertEquals(snoozeCriterions, entry.snoozeCriteria);
332 }
333
Beverlye98937a2018-11-15 10:18:57 -0500334 @Test
335 public void notificationDataEntry_testIsLastMessageFromReply() {
336 Person.Builder person = new Person.Builder()
337 .setName("name")
338 .setKey("abc")
339 .setUri("uri")
340 .setBot(true);
341
342 // EXTRA_MESSAGING_PERSON is the same Person as the sender in last message in EXTRA_MESSAGES
343 Bundle bundle = new Bundle();
344 bundle.putParcelable(Notification.EXTRA_MESSAGING_PERSON, person.build());
345 Bundle[] messagesBundle = new Bundle[]{ new Notification.MessagingStyle.Message(
346 "text", 0, person.build()).toBundle() };
347 bundle.putParcelableArray(Notification.EXTRA_MESSAGES, messagesBundle);
348
349 Notification notification = new Notification.Builder(mContext, "test")
350 .addExtras(bundle)
351 .build();
352 StatusBarNotification sbn = new StatusBarNotification("pkg", "pkg", 0, "tag", 0, 0,
353 notification, mContext.getUser(), "", 0);
354
Ned Burnsf81c4c42019-01-07 14:10:43 -0500355 NotificationEntry entry = new NotificationEntry(sbn);
Beverlye98937a2018-11-15 10:18:57 -0500356 entry.setHasSentReply();
357
358 assertTrue(entry.isLastMessageFromReply());
359 }
360
Julia Reynoldsefcd8bd2019-02-06 08:56:15 -0500361 @Test
362 public void personHighPriority() {
363 Person person = new Person.Builder()
364 .setName("name")
365 .setKey("abc")
366 .setUri("uri")
367 .setBot(true)
368 .build();
369
370 Notification notification = new Notification.Builder(mContext, "test")
371 .addPerson(person)
372 .build();
373
374 StatusBarNotification sbn = new StatusBarNotification("pkg", "pkg", 0, "tag", 0, 0,
375 notification, mContext.getUser(), "", 0);
376
377 assertTrue(mNotificationData.isHighPriority(sbn));
378 }
379
380 @Test
381 public void messagingStyleHighPriority() {
382
383 Notification notification = new Notification.Builder(mContext, "test")
384 .setStyle(new Notification.MessagingStyle(""))
385 .build();
386
387 StatusBarNotification sbn = new StatusBarNotification("pkg", "pkg", 0, "tag", 0, 0,
388 notification, mContext.getUser(), "", 0);
389
390 assertTrue(mNotificationData.isHighPriority(sbn));
391 }
392
393 @Test
394 public void minForegroundNotHighPriority() {
395 Notification notification = mock(Notification.class);
396 when(notification.isForegroundService()).thenReturn(true);
397
398 mNotificationData.rankingOverrides.putInt(OVERRIDE_IMPORTANCE, IMPORTANCE_MIN);
399
400 StatusBarNotification sbn = new StatusBarNotification("pkg", "pkg", 0, "tag", 0, 0,
401 notification, mContext.getUser(), "", 0);
402
403 assertFalse(mNotificationData.isHighPriority(sbn));
404 }
405
406 @Test
407 public void lowForegroundHighPriority() {
408 Notification notification = mock(Notification.class);
409 when(notification.isForegroundService()).thenReturn(true);
410
411 mNotificationData.rankingOverrides.putInt(OVERRIDE_IMPORTANCE, IMPORTANCE_LOW);
412
413 StatusBarNotification sbn = new StatusBarNotification("pkg", "pkg", 0, "tag", 0, 0,
414 notification, mContext.getUser(), "", 0);
415
416 assertTrue(mNotificationData.isHighPriority(sbn));
417 }
418
Maurice Lam132710e2017-03-03 19:13:42 -0800419 private void initStatusBarNotification(boolean allowDuringSetup) {
420 Bundle bundle = new Bundle();
421 bundle.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, allowDuringSetup);
422 Notification notification = new Notification.Builder(mContext, "test")
423 .addExtras(bundle)
424 .build();
425 when(mMockStatusBarNotification.getNotification()).thenReturn(notification);
426 }
Selim Cinek608a57a2017-04-28 16:50:41 -0700427
Julia Reynoldsefcd8bd2019-02-06 08:56:15 -0500428 public static class TestableNotificationData extends NotificationData {
Jason Monk297c04e2018-08-23 17:16:59 -0400429 public TestableNotificationData() {
430 super();
Selim Cinek608a57a2017-04-28 16:50:41 -0700431 }
432
Julia Reynoldsefcd8bd2019-02-06 08:56:15 -0500433 public static final String OVERRIDE_RANK = "r";
434 public static final String OVERRIDE_DND = "dnd";
435 public static final String OVERRIDE_VIS_OVERRIDE = "vo";
436 public static final String OVERRIDE_VIS_EFFECTS = "ve";
437 public static final String OVERRIDE_IMPORTANCE = "i";
438 public static final String OVERRIDE_IMP_EXP = "ie";
439 public static final String OVERRIDE_GROUP = "g";
440 public static final String OVERRIDE_CHANNEL = "c";
441 public static final String OVERRIDE_PEOPLE = "p";
442 public static final String OVERRIDE_SNOOZE_CRITERIA = "sc";
443 public static final String OVERRIDE_BADGE = "b";
444 public static final String OVERRIDE_USER_SENTIMENT = "us";
445 public static final String OVERRIDE_HIDDEN = "h";
446 public static final String OVERRIDE_LAST_ALERTED = "la";
447 public static final String OVERRIDE_NOISY = "n";
448 public static final String OVERRIDE_SMART_ACTIONS = "sa";
449 public static final String OVERRIDE_SMART_REPLIES = "sr";
450 public static final String OVERRIDE_BUBBLE = "cb";
451
452 public Bundle rankingOverrides = new Bundle();
453
Selim Cinek608a57a2017-04-28 16:50:41 -0700454 @Override
Tony Mak628cb932018-06-19 18:30:41 +0100455 protected boolean getRanking(String key, Ranking outRanking) {
Kensuke Matsuiefa1a742017-08-03 12:12:36 +0900456 super.getRanking(key, outRanking);
Julia Reynoldsefcd8bd2019-02-06 08:56:15 -0500457
458 ArrayList<String> currentAdditionalPeople = new ArrayList<>();
459 if (outRanking.getAdditionalPeople() != null) {
460 currentAdditionalPeople.addAll(outRanking.getAdditionalPeople());
Beverly5a20a5e2018-03-06 15:02:44 -0500461 }
Julia Reynoldsefcd8bd2019-02-06 08:56:15 -0500462
463 ArrayList<SnoozeCriterion> currentSnooze = new ArrayList<>();
464 if (outRanking.getSnoozeCriteria() != null) {
465 currentSnooze.addAll(outRanking.getSnoozeCriteria());
466 }
467
468 ArrayList<Notification.Action> currentActions = new ArrayList<>();
469 if (outRanking.getSmartActions() != null) {
470 currentActions.addAll(outRanking.getSmartActions());
471 }
472
473 ArrayList<CharSequence> currentReplies = new ArrayList<>();
474 if (outRanking.getSmartReplies() != null) {
475 currentReplies.addAll(outRanking.getSmartReplies());
476 }
477
478 outRanking.populate(key,
479 rankingOverrides.getInt(OVERRIDE_RANK, outRanking.getRank()),
480 rankingOverrides.getBoolean(OVERRIDE_DND,
481 outRanking.matchesInterruptionFilter()),
482 rankingOverrides.getInt(OVERRIDE_VIS_OVERRIDE,
483 outRanking.getVisibilityOverride()),
484 rankingOverrides.getInt(OVERRIDE_VIS_EFFECTS,
485 outRanking.getSuppressedVisualEffects()),
486 rankingOverrides.getInt(OVERRIDE_IMPORTANCE, outRanking.getImportance()),
487 rankingOverrides.getCharSequence(OVERRIDE_IMP_EXP,
488 outRanking.getImportanceExplanation()),
489 rankingOverrides.getString(OVERRIDE_GROUP, outRanking.getOverrideGroupKey()),
490 rankingOverrides.containsKey(OVERRIDE_CHANNEL)
491 ? (NotificationChannel) rankingOverrides.getParcelable(OVERRIDE_CHANNEL)
492 : outRanking.getChannel(),
493 rankingOverrides.containsKey(OVERRIDE_PEOPLE)
494 ? rankingOverrides.getStringArrayList(OVERRIDE_PEOPLE)
495 : currentAdditionalPeople,
496 rankingOverrides.containsKey(OVERRIDE_SNOOZE_CRITERIA)
497 ? rankingOverrides.getParcelableArrayList(OVERRIDE_SNOOZE_CRITERIA)
498 : currentSnooze,
499 rankingOverrides.getBoolean(OVERRIDE_BADGE, outRanking.canShowBadge()),
500 rankingOverrides.getInt(OVERRIDE_USER_SENTIMENT, outRanking.getUserSentiment()),
501 rankingOverrides.getBoolean(OVERRIDE_HIDDEN, outRanking.isSuspended()),
502 rankingOverrides.getLong(OVERRIDE_LAST_ALERTED,
503 outRanking.getLastAudiblyAlertedMillis()),
504 rankingOverrides.getBoolean(OVERRIDE_NOISY, outRanking.isNoisy()),
505 rankingOverrides.containsKey(OVERRIDE_SMART_ACTIONS)
506 ? rankingOverrides.getParcelableArrayList(OVERRIDE_SMART_ACTIONS)
507 : currentActions,
508 rankingOverrides.containsKey(OVERRIDE_SMART_REPLIES)
509 ? rankingOverrides.getCharSequenceArrayList(OVERRIDE_SMART_REPLIES)
510 : currentReplies,
511 rankingOverrides.getBoolean(OVERRIDE_BUBBLE, outRanking.canBubble()));
Kensuke Matsuiefa1a742017-08-03 12:12:36 +0900512 return true;
513 }
Selim Cinek608a57a2017-04-28 16:50:41 -0700514 }
Tony Mak628cb932018-06-19 18:30:41 +0100515
Gustav Sennton1463d832018-11-06 16:12:48 +0000516 private Notification.Action createContextualAction(String title) {
Tony Mak628cb932018-06-19 18:30:41 +0100517 return new Notification.Action.Builder(
518 Icon.createWithResource(getContext(), android.R.drawable.sym_def_app_icon),
Gustav Sennton1463d832018-11-06 16:12:48 +0000519 title,
520 PendingIntent.getBroadcast(getContext(), 0, new Intent("Action"), 0))
Gustav Sennton005d7a02019-01-04 13:41:32 +0000521 .setContextual(true)
Gustav Sennton1463d832018-11-06 16:12:48 +0000522 .build();
523 }
524
525 private Notification.Action createAction(String title) {
526 return new Notification.Action.Builder(
527 Icon.createWithResource(getContext(), android.R.drawable.sym_def_app_icon),
528 title,
Tony Mak628cb932018-06-19 18:30:41 +0100529 PendingIntent.getBroadcast(getContext(), 0, new Intent("Action"), 0)).build();
530 }
Maurice Lam132710e2017-03-03 19:13:42 -0800531}