Maurice Lam | 132710e | 2017-03-03 19:13:42 -0800 | [diff] [blame] | 1 | /* |
| 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 Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 14 | * limitations under the License |
Maurice Lam | 132710e | 2017-03-03 19:13:42 -0800 | [diff] [blame] | 15 | */ |
| 16 | |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 17 | package com.android.systemui.statusbar.notification.collection; |
Maurice Lam | 132710e | 2017-03-03 19:13:42 -0800 | [diff] [blame] | 18 | |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 19 | import static android.app.AppOpsManager.OP_ACCEPT_HANDOVER; |
| 20 | import static android.app.AppOpsManager.OP_CAMERA; |
Julia Reynolds | 25926af | 2018-05-01 17:05:33 -0400 | [diff] [blame] | 21 | import static android.app.Notification.CATEGORY_ALARM; |
| 22 | import static android.app.Notification.CATEGORY_CALL; |
| 23 | import static android.app.Notification.CATEGORY_EVENT; |
| 24 | import static android.app.Notification.CATEGORY_MESSAGE; |
| 25 | import static android.app.Notification.CATEGORY_REMINDER; |
Julia Reynolds | efcd8bd | 2019-02-06 08:56:15 -0500 | [diff] [blame] | 26 | import static android.app.NotificationManager.IMPORTANCE_LOW; |
| 27 | import static android.app.NotificationManager.IMPORTANCE_MIN; |
| 28 | |
| 29 | import static com.android.systemui.statusbar.notification.collection.NotificationDataTest.TestableNotificationData.OVERRIDE_CHANNEL; |
| 30 | import static com.android.systemui.statusbar.notification.collection.NotificationDataTest.TestableNotificationData.OVERRIDE_IMPORTANCE; |
| 31 | import static com.android.systemui.statusbar.notification.collection.NotificationDataTest.TestableNotificationData.OVERRIDE_VIS_EFFECTS; |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 32 | |
| 33 | import static junit.framework.Assert.assertEquals; |
| 34 | |
Maurice Lam | 132710e | 2017-03-03 19:13:42 -0800 | [diff] [blame] | 35 | import static org.junit.Assert.assertFalse; |
| 36 | import static org.junit.Assert.assertTrue; |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 37 | import static org.mockito.ArgumentMatchers.any; |
Maurice Lam | 132710e | 2017-03-03 19:13:42 -0800 | [diff] [blame] | 38 | import static org.mockito.Matchers.eq; |
| 39 | import static org.mockito.Mockito.mock; |
| 40 | import static org.mockito.Mockito.when; |
| 41 | |
| 42 | import android.Manifest; |
| 43 | import android.app.Notification; |
Selim Cinek | 608a57a | 2017-04-28 16:50:41 -0700 | [diff] [blame] | 44 | import android.app.NotificationChannel; |
Julia Reynolds | efcd8bd | 2019-02-06 08:56:15 -0500 | [diff] [blame] | 45 | import android.app.NotificationManager; |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 46 | import android.app.PendingIntent; |
Beverly | e98937a | 2018-11-15 10:18:57 -0500 | [diff] [blame] | 47 | import android.app.Person; |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 48 | import android.content.Intent; |
Maurice Lam | 132710e | 2017-03-03 19:13:42 -0800 | [diff] [blame] | 49 | import android.content.pm.IPackageManager; |
| 50 | import android.content.pm.PackageManager; |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 51 | import android.graphics.drawable.Icon; |
Julia Reynolds | 5bbb6da | 2018-03-28 10:48:37 -0400 | [diff] [blame] | 52 | import android.media.session.MediaSession; |
Maurice Lam | 132710e | 2017-03-03 19:13:42 -0800 | [diff] [blame] | 53 | import android.os.Bundle; |
Yohei Yukawa | 7276946 | 2019-01-20 09:28:08 -0800 | [diff] [blame] | 54 | import android.os.Process; |
Selim Cinek | 608a57a | 2017-04-28 16:50:41 -0700 | [diff] [blame] | 55 | import android.service.notification.NotificationListenerService; |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 56 | import android.service.notification.NotificationListenerService.Ranking; |
| 57 | import android.service.notification.SnoozeCriterion; |
Maurice Lam | 132710e | 2017-03-03 19:13:42 -0800 | [diff] [blame] | 58 | import android.service.notification.StatusBarNotification; |
Jason Monk | 6dceace | 2018-05-15 20:24:07 -0400 | [diff] [blame] | 59 | import android.testing.AndroidTestingRunner; |
Jason Monk | a716bac | 2018-12-05 15:48:21 -0500 | [diff] [blame] | 60 | import android.testing.TestableLooper; |
Jason Monk | 6dceace | 2018-05-15 20:24:07 -0400 | [diff] [blame] | 61 | import android.testing.TestableLooper.RunWithLooper; |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 62 | import android.util.ArraySet; |
Maurice Lam | 132710e | 2017-03-03 19:13:42 -0800 | [diff] [blame] | 63 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 64 | import androidx.test.filters.SmallTest; |
| 65 | |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 66 | import com.android.systemui.Dependency; |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 67 | import com.android.systemui.ForegroundServiceController; |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 68 | import com.android.systemui.InitController; |
Maurice Lam | 132710e | 2017-03-03 19:13:42 -0800 | [diff] [blame] | 69 | import com.android.systemui.SysuiTestCase; |
Rohan Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 70 | import com.android.systemui.statusbar.NotificationTestHelper; |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 71 | import com.android.systemui.statusbar.notification.collection.NotificationData; |
| 72 | import com.android.systemui.statusbar.notification.collection.NotificationData.KeyguardEnvironment; |
| 73 | import com.android.systemui.statusbar.notification.collection.NotificationEntry; |
Rohan Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 74 | import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow; |
Selim Cinek | 608a57a | 2017-04-28 16:50:41 -0700 | [diff] [blame] | 75 | import com.android.systemui.statusbar.phone.NotificationGroupManager; |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 76 | import com.android.systemui.statusbar.phone.ShadeController; |
Maurice Lam | 132710e | 2017-03-03 19:13:42 -0800 | [diff] [blame] | 77 | |
| 78 | import org.junit.Before; |
| 79 | import org.junit.Test; |
| 80 | import org.junit.runner.RunWith; |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 81 | import org.mockito.Mock; |
| 82 | import org.mockito.MockitoAnnotations; |
Maurice Lam | 132710e | 2017-03-03 19:13:42 -0800 | [diff] [blame] | 83 | |
Dieter Hsu | 36e1ebc | 2018-06-06 15:41:46 +0800 | [diff] [blame] | 84 | import java.util.ArrayList; |
Gustav Sennton | 1463d83 | 2018-11-06 16:12:48 +0000 | [diff] [blame] | 85 | import java.util.Collections; |
| 86 | import java.util.List; |
Dieter Hsu | 36e1ebc | 2018-06-06 15:41:46 +0800 | [diff] [blame] | 87 | |
Maurice Lam | 132710e | 2017-03-03 19:13:42 -0800 | [diff] [blame] | 88 | @SmallTest |
Jason Monk | 6dceace | 2018-05-15 20:24:07 -0400 | [diff] [blame] | 89 | @RunWith(AndroidTestingRunner.class) |
Jason Monk | a716bac | 2018-12-05 15:48:21 -0500 | [diff] [blame] | 90 | @RunWithLooper |
Maurice Lam | 132710e | 2017-03-03 19:13:42 -0800 | [diff] [blame] | 91 | public class NotificationDataTest extends SysuiTestCase { |
| 92 | |
| 93 | private static final int UID_NORMAL = 123; |
| 94 | private static final int UID_ALLOW_DURING_SETUP = 456; |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 95 | private static final NotificationChannel NOTIFICATION_CHANNEL = |
| 96 | new NotificationChannel("id", "name", NotificationChannel.USER_LOCKED_IMPORTANCE); |
Maurice Lam | 132710e | 2017-03-03 19:13:42 -0800 | [diff] [blame] | 97 | |
| 98 | private final StatusBarNotification mMockStatusBarNotification = |
| 99 | mock(StatusBarNotification.class); |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 100 | @Mock |
| 101 | ForegroundServiceController mFsc; |
| 102 | @Mock |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 103 | NotificationData.KeyguardEnvironment mEnvironment; |
Maurice Lam | 132710e | 2017-03-03 19:13:42 -0800 | [diff] [blame] | 104 | |
| 105 | private final IPackageManager mMockPackageManager = mock(IPackageManager.class); |
Julia Reynolds | efcd8bd | 2019-02-06 08:56:15 -0500 | [diff] [blame] | 106 | private TestableNotificationData mNotificationData; |
Selim Cinek | 608a57a | 2017-04-28 16:50:41 -0700 | [diff] [blame] | 107 | private ExpandableNotificationRow mRow; |
Maurice Lam | 132710e | 2017-03-03 19:13:42 -0800 | [diff] [blame] | 108 | |
| 109 | @Before |
| 110 | public void setUp() throws Exception { |
Jason Monk | a716bac | 2018-12-05 15:48:21 -0500 | [diff] [blame] | 111 | com.android.systemui.util.Assert.sMainLooper = TestableLooper.get(this).getLooper(); |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 112 | MockitoAnnotations.initMocks(this); |
Maurice Lam | 132710e | 2017-03-03 19:13:42 -0800 | [diff] [blame] | 113 | when(mMockStatusBarNotification.getUid()).thenReturn(UID_NORMAL); |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 114 | when(mMockStatusBarNotification.cloneLight()).thenReturn(mMockStatusBarNotification); |
Maurice Lam | 132710e | 2017-03-03 19:13:42 -0800 | [diff] [blame] | 115 | |
| 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 Cinek | 608a57a | 2017-04-28 16:50:41 -0700 | [diff] [blame] | 124 | |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 125 | mDependency.injectTestDependency(ForegroundServiceController.class, mFsc); |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 126 | mDependency.injectTestDependency(NotificationGroupManager.class, |
| 127 | new NotificationGroupManager()); |
| 128 | mDependency.injectMockDependency(ShadeController.class); |
| 129 | mDependency.injectTestDependency(KeyguardEnvironment.class, mEnvironment); |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 130 | when(mEnvironment.isDeviceProvisioned()).thenReturn(true); |
| 131 | when(mEnvironment.isNotificationForCurrentProfiles(any())).thenReturn(true); |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 132 | mNotificationData = new TestableNotificationData(); |
Selim Cinek | 608a57a | 2017-04-28 16:50:41 -0700 | [diff] [blame] | 133 | mNotificationData.updateRanking(mock(NotificationListenerService.RankingMap.class)); |
| 134 | mRow = new NotificationTestHelper(getContext()).createRow(); |
Tony Mak | 7d4b3a5 | 2018-11-27 17:29:36 +0000 | [diff] [blame] | 135 | Dependency.get(InitController.class).executePostInitTasks(); |
Maurice Lam | 132710e | 2017-03-03 19:13:42 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | @Test |
Selim Cinek | 608a57a | 2017-04-28 16:50:41 -0700 | [diff] [blame] | 139 | public void testChannelSetWhenAdded() { |
Julia Reynolds | efcd8bd | 2019-02-06 08:56:15 -0500 | [diff] [blame] | 140 | mNotificationData.rankingOverrides.putParcelable(OVERRIDE_CHANNEL, NOTIFICATION_CHANNEL); |
Selim Cinek | 608a57a | 2017-04-28 16:50:41 -0700 | [diff] [blame] | 141 | mNotificationData.add(mRow.getEntry()); |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 142 | assertEquals(NOTIFICATION_CHANNEL, mRow.getEntry().channel); |
Selim Cinek | 608a57a | 2017-04-28 16:50:41 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 145 | @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 Yukawa | 7276946 | 2019-01-20 09:28:08 -0800 | [diff] [blame] | 151 | new NotificationTestHelper(getContext()).createRow("pkg", 4000, |
| 152 | Process.myUserHandle()); |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 153 | 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 Reynolds | 9159006 | 2018-04-02 16:24:11 -0400 | [diff] [blame] | 161 | NotificationTestHelper.PKG, mRow.getEntry().key, true); |
| 162 | mNotificationData.updateAppOp(op, NotificationTestHelper.UID, |
| 163 | NotificationTestHelper.PKG, row2.getEntry().key, true); |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 164 | } |
| 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 Reynolds | 9159006 | 2018-04-02 16:24:11 -0400 | [diff] [blame] | 187 | NotificationTestHelper.PKG, row2.getEntry().key, true); |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | expectedOps.remove(OP_ACCEPT_HANDOVER); |
| 191 | mNotificationData.updateAppOp(OP_ACCEPT_HANDOVER, NotificationTestHelper.UID, |
Julia Reynolds | 9159006 | 2018-04-02 16:24:11 -0400 | [diff] [blame] | 192 | NotificationTestHelper.PKG, row2.getEntry().key, false); |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 193 | |
| 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 Hsu | 36e1ebc | 2018-06-06 15:41:46 +0800 | [diff] [blame] | 207 | 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 Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 217 | ArrayList<NotificationEntry> result = |
Dieter Hsu | 36e1ebc | 2018-06-06 15:41:46 +0800 | [diff] [blame] | 218 | mNotificationData.getNotificationsForCurrentUser(); |
| 219 | |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 220 | assertEquals(result.size(), 1); |
| 221 | junit.framework.Assert.assertEquals(result.get(0), row2.getEntry()); |
Dieter Hsu | 36e1ebc | 2018-06-06 15:41:46 +0800 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | @Test |
Julia Reynolds | 5bbb6da | 2018-03-28 10:48:37 -0400 | [diff] [blame] | 225 | public void testIsExemptFromDndVisualSuppression_foreground() { |
| 226 | initStatusBarNotification(false); |
Julia Reynolds | efcd8bd | 2019-02-06 08:56:15 -0500 | [diff] [blame] | 227 | |
Julia Reynolds | 5bbb6da | 2018-03-28 10:48:37 -0400 | [diff] [blame] | 228 | Notification n = mMockStatusBarNotification.getNotification(); |
| 229 | n.flags = Notification.FLAG_FOREGROUND_SERVICE; |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 230 | NotificationEntry entry = new NotificationEntry(mMockStatusBarNotification); |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 231 | mNotificationData.add(entry); |
Julia Reynolds | efcd8bd | 2019-02-06 08:56:15 -0500 | [diff] [blame] | 232 | mNotificationData.rankingOverrides.putInt(OVERRIDE_VIS_EFFECTS, 255); |
Julia Reynolds | 5bbb6da | 2018-03-28 10:48:37 -0400 | [diff] [blame] | 233 | |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 234 | assertTrue(entry.isExemptFromDndVisualSuppression()); |
| 235 | assertFalse(entry.shouldSuppressAmbient()); |
Julia Reynolds | 5bbb6da | 2018-03-28 10:48:37 -0400 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | @Test |
| 239 | public void testIsExemptFromDndVisualSuppression_media() { |
| 240 | initStatusBarNotification(false); |
Julia Reynolds | 5bbb6da | 2018-03-28 10:48:37 -0400 | [diff] [blame] | 241 | 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 Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 246 | NotificationEntry entry = new NotificationEntry(mMockStatusBarNotification); |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 247 | mNotificationData.add(entry); |
Julia Reynolds | efcd8bd | 2019-02-06 08:56:15 -0500 | [diff] [blame] | 248 | mNotificationData.rankingOverrides.putInt(OVERRIDE_VIS_EFFECTS, 255); |
Julia Reynolds | 5bbb6da | 2018-03-28 10:48:37 -0400 | [diff] [blame] | 249 | |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 250 | assertTrue(entry.isExemptFromDndVisualSuppression()); |
| 251 | assertFalse(entry.shouldSuppressAmbient()); |
Julia Reynolds | aa96cf3 | 2018-04-17 09:09:04 -0400 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | @Test |
| 255 | public void testIsExemptFromDndVisualSuppression_system() { |
| 256 | initStatusBarNotification(false); |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 257 | NotificationEntry entry = new NotificationEntry(mMockStatusBarNotification); |
Julia Reynolds | aa96cf3 | 2018-04-17 09:09:04 -0400 | [diff] [blame] | 258 | entry.mIsSystemNotification = true; |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 259 | mNotificationData.add(entry); |
Julia Reynolds | efcd8bd | 2019-02-06 08:56:15 -0500 | [diff] [blame] | 260 | mNotificationData.rankingOverrides.putInt(OVERRIDE_VIS_EFFECTS, 255); |
Julia Reynolds | aa96cf3 | 2018-04-17 09:09:04 -0400 | [diff] [blame] | 261 | |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 262 | assertTrue(entry.isExemptFromDndVisualSuppression()); |
| 263 | assertFalse(entry.shouldSuppressAmbient()); |
Julia Reynolds | 5bbb6da | 2018-03-28 10:48:37 -0400 | [diff] [blame] | 264 | } |
| 265 | |
Julia Reynolds | 25926af | 2018-05-01 17:05:33 -0400 | [diff] [blame] | 266 | @Test |
| 267 | public void testIsNotExemptFromDndVisualSuppression_hiddenCategories() { |
| 268 | initStatusBarNotification(false); |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 269 | NotificationEntry entry = new NotificationEntry(mMockStatusBarNotification); |
Julia Reynolds | 25926af | 2018-05-01 17:05:33 -0400 | [diff] [blame] | 270 | entry.mIsSystemNotification = true; |
Julia Reynolds | efcd8bd | 2019-02-06 08:56:15 -0500 | [diff] [blame] | 271 | mNotificationData.rankingOverrides.putInt(OVERRIDE_VIS_EFFECTS, |
| 272 | NotificationManager.Policy.SUPPRESSED_EFFECT_AMBIENT); |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 273 | mNotificationData.add(entry); |
| 274 | |
Julia Reynolds | 25926af | 2018-05-01 17:05:33 -0400 | [diff] [blame] | 275 | when(mMockStatusBarNotification.getNotification()).thenReturn( |
| 276 | new Notification.Builder(mContext, "").setCategory(CATEGORY_CALL).build()); |
| 277 | |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 278 | assertFalse(entry.isExemptFromDndVisualSuppression()); |
| 279 | assertTrue(entry.shouldSuppressAmbient()); |
Julia Reynolds | 25926af | 2018-05-01 17:05:33 -0400 | [diff] [blame] | 280 | |
| 281 | when(mMockStatusBarNotification.getNotification()).thenReturn( |
| 282 | new Notification.Builder(mContext, "").setCategory(CATEGORY_REMINDER).build()); |
| 283 | |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 284 | assertFalse(entry.isExemptFromDndVisualSuppression()); |
Julia Reynolds | 25926af | 2018-05-01 17:05:33 -0400 | [diff] [blame] | 285 | |
| 286 | when(mMockStatusBarNotification.getNotification()).thenReturn( |
| 287 | new Notification.Builder(mContext, "").setCategory(CATEGORY_ALARM).build()); |
| 288 | |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 289 | assertFalse(entry.isExemptFromDndVisualSuppression()); |
Julia Reynolds | 25926af | 2018-05-01 17:05:33 -0400 | [diff] [blame] | 290 | |
| 291 | when(mMockStatusBarNotification.getNotification()).thenReturn( |
| 292 | new Notification.Builder(mContext, "").setCategory(CATEGORY_EVENT).build()); |
| 293 | |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 294 | assertFalse(entry.isExemptFromDndVisualSuppression()); |
Julia Reynolds | 25926af | 2018-05-01 17:05:33 -0400 | [diff] [blame] | 295 | |
| 296 | when(mMockStatusBarNotification.getNotification()).thenReturn( |
| 297 | new Notification.Builder(mContext, "").setCategory(CATEGORY_MESSAGE).build()); |
| 298 | |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 299 | assertFalse(entry.isExemptFromDndVisualSuppression()); |
Julia Reynolds | 25926af | 2018-05-01 17:05:33 -0400 | [diff] [blame] | 300 | } |
| 301 | |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 302 | @Test |
| 303 | public void testCreateNotificationDataEntry_RankingUpdate() { |
| 304 | Ranking ranking = mock(Ranking.class); |
Gustav Sennton | 1463d83 | 2018-11-06 16:12:48 +0000 | [diff] [blame] | 305 | initStatusBarNotification(false); |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 306 | |
Gustav Sennton | 1463d83 | 2018-11-06 16:12:48 +0000 | [diff] [blame] | 307 | 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 Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 315 | |
| 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 Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 325 | NotificationEntry entry = |
| 326 | new NotificationEntry(mMockStatusBarNotification, ranking); |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 327 | |
Gustav Sennton | 1463d83 | 2018-11-06 16:12:48 +0000 | [diff] [blame] | 328 | assertEquals(systemGeneratedSmartActions, entry.systemGeneratedSmartActions); |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 329 | assertEquals(NOTIFICATION_CHANNEL, entry.channel); |
| 330 | assertEquals(Ranking.USER_SENTIMENT_NEGATIVE, entry.userSentiment); |
| 331 | assertEquals(snoozeCriterions, entry.snoozeCriteria); |
| 332 | } |
| 333 | |
Beverly | e98937a | 2018-11-15 10:18:57 -0500 | [diff] [blame] | 334 | @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 Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 355 | NotificationEntry entry = new NotificationEntry(sbn); |
Beverly | e98937a | 2018-11-15 10:18:57 -0500 | [diff] [blame] | 356 | entry.setHasSentReply(); |
| 357 | |
| 358 | assertTrue(entry.isLastMessageFromReply()); |
| 359 | } |
| 360 | |
Julia Reynolds | efcd8bd | 2019-02-06 08:56:15 -0500 | [diff] [blame] | 361 | @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 Lam | 132710e | 2017-03-03 19:13:42 -0800 | [diff] [blame] | 419 | 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 Cinek | 608a57a | 2017-04-28 16:50:41 -0700 | [diff] [blame] | 427 | |
Julia Reynolds | efcd8bd | 2019-02-06 08:56:15 -0500 | [diff] [blame] | 428 | public static class TestableNotificationData extends NotificationData { |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 429 | public TestableNotificationData() { |
| 430 | super(); |
Selim Cinek | 608a57a | 2017-04-28 16:50:41 -0700 | [diff] [blame] | 431 | } |
| 432 | |
Julia Reynolds | efcd8bd | 2019-02-06 08:56:15 -0500 | [diff] [blame] | 433 | 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 Cinek | 608a57a | 2017-04-28 16:50:41 -0700 | [diff] [blame] | 454 | @Override |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 455 | protected boolean getRanking(String key, Ranking outRanking) { |
Kensuke Matsui | efa1a74 | 2017-08-03 12:12:36 +0900 | [diff] [blame] | 456 | super.getRanking(key, outRanking); |
Julia Reynolds | efcd8bd | 2019-02-06 08:56:15 -0500 | [diff] [blame] | 457 | |
| 458 | ArrayList<String> currentAdditionalPeople = new ArrayList<>(); |
| 459 | if (outRanking.getAdditionalPeople() != null) { |
| 460 | currentAdditionalPeople.addAll(outRanking.getAdditionalPeople()); |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 461 | } |
Julia Reynolds | efcd8bd | 2019-02-06 08:56:15 -0500 | [diff] [blame] | 462 | |
| 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 Matsui | efa1a74 | 2017-08-03 12:12:36 +0900 | [diff] [blame] | 512 | return true; |
| 513 | } |
Selim Cinek | 608a57a | 2017-04-28 16:50:41 -0700 | [diff] [blame] | 514 | } |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 515 | |
Gustav Sennton | 1463d83 | 2018-11-06 16:12:48 +0000 | [diff] [blame] | 516 | private Notification.Action createContextualAction(String title) { |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 517 | return new Notification.Action.Builder( |
| 518 | Icon.createWithResource(getContext(), android.R.drawable.sym_def_app_icon), |
Gustav Sennton | 1463d83 | 2018-11-06 16:12:48 +0000 | [diff] [blame] | 519 | title, |
| 520 | PendingIntent.getBroadcast(getContext(), 0, new Intent("Action"), 0)) |
Gustav Sennton | 005d7a0 | 2019-01-04 13:41:32 +0000 | [diff] [blame] | 521 | .setContextual(true) |
Gustav Sennton | 1463d83 | 2018-11-06 16:12:48 +0000 | [diff] [blame] | 522 | .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 Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 529 | PendingIntent.getBroadcast(getContext(), 0, new Intent("Action"), 0)).build(); |
| 530 | } |
Maurice Lam | 132710e | 2017-03-03 19:13:42 -0800 | [diff] [blame] | 531 | } |