Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [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 |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 15 | */ |
| 16 | |
Rohan Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 17 | package com.android.systemui.statusbar.notification; |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 18 | |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 19 | import static android.app.NotificationManager.IMPORTANCE_DEFAULT; |
Mady Mellor | c2ff011 | 2019-03-28 14:18:06 -0700 | [diff] [blame] | 20 | import static android.service.notification.NotificationListenerService.REASON_CANCEL; |
| 21 | |
Mady Mellor | 1a4e86f | 2019-05-03 16:07:23 -0700 | [diff] [blame] | 22 | import static com.android.systemui.statusbar.notification.NotificationEntryManager.UNDEFINED_DISMISS_REASON; |
| 23 | |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 24 | import static junit.framework.Assert.assertNotNull; |
| 25 | import static junit.framework.Assert.assertNull; |
| 26 | import static junit.framework.Assert.assertTrue; |
| 27 | |
| 28 | import static org.junit.Assert.assertEquals; |
Beverly | 8ca664b | 2020-05-07 14:46:50 -0400 | [diff] [blame] | 29 | import static org.junit.Assert.assertFalse; |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 30 | import static org.mockito.ArgumentMatchers.any; |
Mady Mellor | c2ff011 | 2019-03-28 14:18:06 -0700 | [diff] [blame] | 31 | import static org.mockito.ArgumentMatchers.anyBoolean; |
| 32 | import static org.mockito.ArgumentMatchers.anyInt; |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 33 | import static org.mockito.ArgumentMatchers.eq; |
Mady Mellor | c2ff011 | 2019-03-28 14:18:06 -0700 | [diff] [blame] | 34 | import static org.mockito.Mockito.atLeastOnce; |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 35 | import static org.mockito.Mockito.doAnswer; |
Mady Mellor | 0ad5b9d | 2019-01-08 14:59:55 -0800 | [diff] [blame] | 36 | import static org.mockito.Mockito.inOrder; |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 37 | import static org.mockito.Mockito.mock; |
| 38 | import static org.mockito.Mockito.never; |
| 39 | import static org.mockito.Mockito.verify; |
| 40 | import static org.mockito.Mockito.when; |
| 41 | |
| 42 | import android.app.ActivityManager; |
| 43 | import android.app.Notification; |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 44 | import android.app.NotificationChannel; |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 45 | import android.app.PendingIntent; |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 46 | import android.content.Intent; |
| 47 | import android.graphics.drawable.Icon; |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 48 | import android.os.Handler; |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 49 | import android.os.UserHandle; |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 50 | import android.service.notification.NotificationListenerService.Ranking; |
| 51 | import android.service.notification.NotificationListenerService.RankingMap; |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 52 | import android.service.notification.StatusBarNotification; |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 53 | import android.testing.AndroidTestingRunner; |
| 54 | import android.testing.TestableLooper; |
Ned Burns | a5dad55 | 2019-01-29 17:59:10 -0500 | [diff] [blame] | 55 | import android.util.ArraySet; |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 56 | |
Ned Burns | a5dad55 | 2019-01-29 17:59:10 -0500 | [diff] [blame] | 57 | import androidx.annotation.NonNull; |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 58 | import androidx.test.filters.SmallTest; |
Ned Burns | a5dad55 | 2019-01-29 17:59:10 -0500 | [diff] [blame] | 59 | |
Mady Mellor | c2ff011 | 2019-03-28 14:18:06 -0700 | [diff] [blame] | 60 | import com.android.internal.statusbar.NotificationVisibility; |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 61 | import com.android.systemui.Dependency; |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 62 | import com.android.systemui.R; |
| 63 | import com.android.systemui.SysuiTestCase; |
Beverly | 8b493df | 2019-12-18 14:16:50 -0500 | [diff] [blame] | 64 | import com.android.systemui.statusbar.FeatureFlags; |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 65 | import com.android.systemui.statusbar.NotificationLifetimeExtender; |
Dave Mankoff | 48091ed | 2019-10-04 16:33:14 -0400 | [diff] [blame] | 66 | import com.android.systemui.statusbar.NotificationMediaManager; |
Rohan Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 67 | import com.android.systemui.statusbar.NotificationPresenter; |
| 68 | import com.android.systemui.statusbar.NotificationRemoteInputManager; |
Mady Mellor | c2ff011 | 2019-03-28 14:18:06 -0700 | [diff] [blame] | 69 | import com.android.systemui.statusbar.NotificationRemoveInterceptor; |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 70 | import com.android.systemui.statusbar.RankingBuilder; |
Rohan Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 71 | import com.android.systemui.statusbar.SmartReplyController; |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 72 | import com.android.systemui.statusbar.notification.NotificationEntryManager.KeyguardEnvironment; |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 73 | import com.android.systemui.statusbar.notification.collection.NotificationEntry; |
Beverly | 79c89ec | 2019-12-13 10:33:01 -0500 | [diff] [blame] | 74 | import com.android.systemui.statusbar.notification.collection.NotificationEntryBuilder; |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 75 | import com.android.systemui.statusbar.notification.collection.NotificationRankingManager; |
Kevin Han | 225b712 | 2020-01-31 15:42:42 -0800 | [diff] [blame] | 76 | import com.android.systemui.statusbar.notification.collection.inflation.NotificationRowBinder; |
Beverly | e558f1d | 2020-01-07 16:28:58 -0500 | [diff] [blame] | 77 | import com.android.systemui.statusbar.notification.collection.provider.HighPriorityProvider; |
Steve Elliott | 960f4ce | 2019-12-04 15:13:52 -0500 | [diff] [blame] | 78 | import com.android.systemui.statusbar.notification.people.PeopleNotificationIdentifier; |
Rohan Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 79 | import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow; |
Kevin Han | 225b712 | 2020-01-31 15:42:42 -0800 | [diff] [blame] | 80 | import com.android.systemui.statusbar.notification.row.NotificationEntryManagerInflationTest; |
Rohan Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 81 | import com.android.systemui.statusbar.notification.row.RowInflaterTask; |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 82 | import com.android.systemui.statusbar.phone.NotificationGroupManager; |
| 83 | import com.android.systemui.statusbar.policy.DeviceProvisionedController; |
| 84 | import com.android.systemui.statusbar.policy.HeadsUpManager; |
Dave Mankoff | 02dcaf5 | 2020-01-08 15:42:06 -0500 | [diff] [blame] | 85 | import com.android.systemui.util.leak.LeakDetector; |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 86 | |
| 87 | import org.junit.Before; |
| 88 | import org.junit.Test; |
| 89 | import org.junit.runner.RunWith; |
| 90 | import org.mockito.ArgumentCaptor; |
Mady Mellor | 0ad5b9d | 2019-01-08 14:59:55 -0800 | [diff] [blame] | 91 | import org.mockito.InOrder; |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 92 | import org.mockito.Mock; |
| 93 | import org.mockito.MockitoAnnotations; |
| 94 | |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 95 | import java.util.ArrayList; |
| 96 | import java.util.Arrays; |
Beverly | 8ca664b | 2020-05-07 14:46:50 -0400 | [diff] [blame] | 97 | import java.util.Collection; |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 98 | import java.util.List; |
Ned Burns | a5dad55 | 2019-01-29 17:59:10 -0500 | [diff] [blame] | 99 | import java.util.Set; |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 100 | |
Kevin Han | 225b712 | 2020-01-31 15:42:42 -0800 | [diff] [blame] | 101 | /** |
| 102 | * Unit tests for {@link NotificationEntryManager}. This test will not test any interactions with |
| 103 | * inflation. Instead, for functional inflation tests, see |
| 104 | * {@link NotificationEntryManagerInflationTest}. |
| 105 | */ |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 106 | @SmallTest |
| 107 | @RunWith(AndroidTestingRunner.class) |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 108 | @TestableLooper.RunWithLooper() |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 109 | public class NotificationEntryManagerTest extends SysuiTestCase { |
| 110 | private static final String TEST_PACKAGE_NAME = "test"; |
| 111 | private static final int TEST_UID = 0; |
| 112 | |
| 113 | @Mock private NotificationPresenter mPresenter; |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 114 | @Mock private KeyguardEnvironment mEnvironment; |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 115 | @Mock private ExpandableNotificationRow mRow; |
Ned Burns | c586467 | 2019-02-20 12:57:29 -0500 | [diff] [blame] | 116 | @Mock private NotificationEntryListener mEntryListener; |
Mady Mellor | c2ff011 | 2019-03-28 14:18:06 -0700 | [diff] [blame] | 117 | @Mock private NotificationRemoveInterceptor mRemoveInterceptor; |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 118 | @Mock private HeadsUpManager mHeadsUpManager; |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 119 | @Mock private RankingMap mRankingMap; |
Eliot Courtney | 8f56b0e | 2017-12-14 18:54:28 +0900 | [diff] [blame] | 120 | @Mock private NotificationGroupManager mGroupManager; |
Eliot Courtney | 8f56b0e | 2017-12-14 18:54:28 +0900 | [diff] [blame] | 121 | @Mock private NotificationRemoteInputManager mRemoteInputManager; |
Eliot Courtney | 8f56b0e | 2017-12-14 18:54:28 +0900 | [diff] [blame] | 122 | @Mock private DeviceProvisionedController mDeviceProvisionedController; |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 123 | @Mock private RowInflaterTask mAsyncInflationTask; |
Ned Burns | afe77bc | 2020-01-30 20:45:07 -0500 | [diff] [blame] | 124 | @Mock private NotificationEntryManagerLogger mLogger; |
Beverly | 8b493df | 2019-12-18 14:16:50 -0500 | [diff] [blame] | 125 | @Mock private FeatureFlags mFeatureFlags; |
Dave Mankoff | 02dcaf5 | 2020-01-08 15:42:06 -0500 | [diff] [blame] | 126 | @Mock private LeakDetector mLeakDetector; |
Dave Mankoff | c0211ff | 2020-02-07 15:36:12 -0500 | [diff] [blame] | 127 | @Mock private NotificationMediaManager mNotificationMediaManager; |
Kevin Han | 225b712 | 2020-01-31 15:42:42 -0800 | [diff] [blame] | 128 | @Mock private NotificationRowBinder mNotificationRowBinder; |
Eliot Courtney | 8f56b0e | 2017-12-14 18:54:28 +0900 | [diff] [blame] | 129 | |
Beverly | 201cdd5 | 2019-10-18 14:30:46 -0400 | [diff] [blame] | 130 | private int mId; |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 131 | private NotificationEntry mEntry; |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 132 | private StatusBarNotification mSbn; |
Kevin Han | 225b712 | 2020-01-31 15:42:42 -0800 | [diff] [blame] | 133 | private NotificationEntryManager mEntryManager; |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 134 | |
Dan Sandler | 1d958f8 | 2018-01-09 21:10:26 -0500 | [diff] [blame] | 135 | private void setUserSentiment(String key, int sentiment) { |
| 136 | doAnswer(invocationOnMock -> { |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 137 | Ranking ranking = (Ranking) |
Dan Sandler | 1d958f8 | 2018-01-09 21:10:26 -0500 | [diff] [blame] | 138 | invocationOnMock.getArguments()[1]; |
| 139 | ranking.populate( |
| 140 | key, |
| 141 | 0, |
| 142 | false, |
| 143 | 0, |
| 144 | 0, |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 145 | IMPORTANCE_DEFAULT, |
Dan Sandler | 1d958f8 | 2018-01-09 21:10:26 -0500 | [diff] [blame] | 146 | null, null, |
Danning Chen | 10326cf | 2020-01-16 13:29:13 -0800 | [diff] [blame] | 147 | null, null, null, true, sentiment, false, -1, false, null, null, false, false, |
Mady Mellor | 56515c4 | 2020-02-18 17:58:36 -0800 | [diff] [blame] | 148 | false, null, false); |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 149 | return true; |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 150 | }).when(mRankingMap).getRanking(eq(key), any(Ranking.class)); |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | private void setSmartActions(String key, ArrayList<Notification.Action> smartActions) { |
| 154 | doAnswer(invocationOnMock -> { |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 155 | Ranking ranking = (Ranking) |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 156 | invocationOnMock.getArguments()[1]; |
| 157 | ranking.populate( |
| 158 | key, |
| 159 | 0, |
| 160 | false, |
| 161 | 0, |
| 162 | 0, |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 163 | IMPORTANCE_DEFAULT, |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 164 | null, null, |
| 165 | null, null, null, true, |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 166 | Ranking.USER_SENTIMENT_NEUTRAL, false, -1, |
Mady Mellor | 56515c4 | 2020-02-18 17:58:36 -0800 | [diff] [blame] | 167 | false, smartActions, null, false, false, false, null, false); |
Dan Sandler | 1d958f8 | 2018-01-09 21:10:26 -0500 | [diff] [blame] | 168 | return true; |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 169 | }).when(mRankingMap).getRanking(eq(key), any(Ranking.class)); |
Dan Sandler | 1d958f8 | 2018-01-09 21:10:26 -0500 | [diff] [blame] | 170 | } |
| 171 | |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 172 | @Before |
| 173 | public void setUp() { |
| 174 | MockitoAnnotations.initMocks(this); |
Beverly | 433923a | 2020-01-22 11:23:24 -0500 | [diff] [blame] | 175 | mDependency.injectMockDependency(SmartReplyController.class); |
Eliot Courtney | 8f56b0e | 2017-12-14 18:54:28 +0900 | [diff] [blame] | 176 | |
Beverly | 1467c9e | 2020-02-18 13:31:29 -0500 | [diff] [blame] | 177 | allowTestableLooperAsMainThread(); |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 178 | mDependency.injectTestDependency(Dependency.MAIN_HANDLER, |
Beverly | 433923a | 2020-01-22 11:23:24 -0500 | [diff] [blame] | 179 | Handler.createAsync(TestableLooper.get(this).getLooper())); |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 180 | |
Beverly | 201cdd5 | 2019-10-18 14:30:46 -0400 | [diff] [blame] | 181 | mEntry = createNotification(); |
Evan Laird | 9afe766 | 2019-10-16 17:16:39 -0400 | [diff] [blame] | 182 | mSbn = mEntry.getSbn(); |
Ned Burns | 60e9459 | 2019-09-06 14:47:25 -0400 | [diff] [blame] | 183 | |
Beverly | 8b493df | 2019-12-18 14:16:50 -0500 | [diff] [blame] | 184 | when(mFeatureFlags.isNewNotifPipelineEnabled()).thenReturn(false); |
| 185 | when(mFeatureFlags.isNewNotifPipelineRenderingEnabled()).thenReturn(false); |
Kevin Han | 225b712 | 2020-01-31 15:42:42 -0800 | [diff] [blame] | 186 | mEntryManager = new NotificationEntryManager( |
Ned Burns | afe77bc | 2020-01-30 20:45:07 -0500 | [diff] [blame] | 187 | mLogger, |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 188 | mGroupManager, |
| 189 | new NotificationRankingManager( |
Dave Mankoff | c0211ff | 2020-02-07 15:36:12 -0500 | [diff] [blame] | 190 | () -> mNotificationMediaManager, |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 191 | mGroupManager, |
| 192 | mHeadsUpManager, |
| 193 | mock(NotificationFilter.class), |
Ned Burns | afe77bc | 2020-01-30 20:45:07 -0500 | [diff] [blame] | 194 | mLogger, |
Steve Elliott | 960f4ce | 2019-12-04 15:13:52 -0500 | [diff] [blame] | 195 | mock(NotificationSectionsFeatureManager.class), |
Beverly | e558f1d | 2020-01-07 16:28:58 -0500 | [diff] [blame] | 196 | mock(PeopleNotificationIdentifier.class), |
| 197 | mock(HighPriorityProvider.class)), |
Beverly | 8b493df | 2019-12-18 14:16:50 -0500 | [diff] [blame] | 198 | mEnvironment, |
Dave Mankoff | 02dcaf5 | 2020-01-08 15:42:06 -0500 | [diff] [blame] | 199 | mFeatureFlags, |
Kevin Han | 225b712 | 2020-01-31 15:42:42 -0800 | [diff] [blame] | 200 | () -> mNotificationRowBinder, |
Dave Mankoff | 02dcaf5 | 2020-01-08 15:42:06 -0500 | [diff] [blame] | 201 | () -> mRemoteInputManager, |
Evan Laird | 0437366 | 2020-01-24 17:37:39 -0500 | [diff] [blame] | 202 | mLeakDetector, |
| 203 | mock(ForegroundServiceDismissalFeatureController.class) |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 204 | ); |
Beverly | 95a0802ac | 2020-02-10 15:27:40 -0500 | [diff] [blame] | 205 | mEntryManager.setUpWithPresenter(mPresenter); |
Gus Prevas | 8621bd2 | 2018-12-20 15:04:25 -0500 | [diff] [blame] | 206 | mEntryManager.addNotificationEntryListener(mEntryListener); |
Evan Laird | 0437366 | 2020-01-24 17:37:39 -0500 | [diff] [blame] | 207 | mEntryManager.addNotificationRemoveInterceptor(mRemoveInterceptor); |
Gus Prevas | 8ba88a8 | 2018-12-18 11:13:44 -0500 | [diff] [blame] | 208 | |
Kevin Han | 225b712 | 2020-01-31 15:42:42 -0800 | [diff] [blame] | 209 | setUserSentiment(mSbn.getKey(), Ranking.USER_SENTIMENT_NEUTRAL); |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | @Test |
Kevin Han | 225b712 | 2020-01-31 15:42:42 -0800 | [diff] [blame] | 213 | public void testAddNotification_setsUserSentiment() { |
Jason Monk | 6dceace | 2018-05-15 20:24:07 -0400 | [diff] [blame] | 214 | mEntryManager.addNotification(mSbn, mRankingMap); |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 215 | |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 216 | ArgumentCaptor<NotificationEntry> entryCaptor = ArgumentCaptor.forClass( |
| 217 | NotificationEntry.class); |
Kevin Han | 225b712 | 2020-01-31 15:42:42 -0800 | [diff] [blame] | 218 | verify(mEntryListener).onPendingEntryAdded(entryCaptor.capture()); |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 219 | NotificationEntry entry = entryCaptor.getValue(); |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 220 | |
Kevin Han | 225b712 | 2020-01-31 15:42:42 -0800 | [diff] [blame] | 221 | assertEquals(entry.getUserSentiment(), Ranking.USER_SENTIMENT_NEUTRAL); |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | @Test |
Kevin Han | 225b712 | 2020-01-31 15:42:42 -0800 | [diff] [blame] | 225 | public void testUpdateNotification_updatesUserSentiment() { |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 226 | mEntryManager.addActiveNotificationForTest(mEntry); |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 227 | setUserSentiment( |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 228 | mEntry.getKey(), Ranking.USER_SENTIMENT_NEGATIVE); |
Dan Sandler | 1d958f8 | 2018-01-09 21:10:26 -0500 | [diff] [blame] | 229 | |
Jason Monk | 6dceace | 2018-05-15 20:24:07 -0400 | [diff] [blame] | 230 | mEntryManager.updateNotification(mSbn, mRankingMap); |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 231 | |
Kevin Han | 225b712 | 2020-01-31 15:42:42 -0800 | [diff] [blame] | 232 | assertEquals(Ranking.USER_SENTIMENT_NEGATIVE, mEntry.getUserSentiment()); |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | @Test |
Mady Mellor | 0ad5b9d | 2019-01-08 14:59:55 -0800 | [diff] [blame] | 236 | public void testUpdateNotification_prePostEntryOrder() throws Exception { |
Mady Mellor | 0ad5b9d | 2019-01-08 14:59:55 -0800 | [diff] [blame] | 237 | TestableLooper.get(this).processAllMessages(); |
| 238 | |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 239 | mEntryManager.addActiveNotificationForTest(mEntry); |
Mady Mellor | 0ad5b9d | 2019-01-08 14:59:55 -0800 | [diff] [blame] | 240 | |
| 241 | mEntryManager.updateNotification(mSbn, mRankingMap); |
Mady Mellor | 0ad5b9d | 2019-01-08 14:59:55 -0800 | [diff] [blame] | 242 | |
| 243 | // Ensure that update callbacks happen in correct order |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 244 | InOrder order = inOrder(mEntryListener, mPresenter, mEntryListener); |
Mady Mellor | 0ad5b9d | 2019-01-08 14:59:55 -0800 | [diff] [blame] | 245 | order.verify(mEntryListener).onPreEntryUpdated(mEntry); |
Mady Mellor | 0ad5b9d | 2019-01-08 14:59:55 -0800 | [diff] [blame] | 246 | order.verify(mPresenter).updateNotificationViews(); |
| 247 | order.verify(mEntryListener).onPostEntryUpdated(mEntry); |
Mady Mellor | 0ad5b9d | 2019-01-08 14:59:55 -0800 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | @Test |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 251 | public void testRemoveNotification() { |
Evan Laird | 9449285 | 2018-10-25 13:43:01 -0400 | [diff] [blame] | 252 | mEntry.setRow(mRow); |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 253 | mEntryManager.addActiveNotificationForTest(mEntry); |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 254 | |
Mady Mellor | 1a4e86f | 2019-05-03 16:07:23 -0700 | [diff] [blame] | 255 | mEntryManager.removeNotification(mSbn.getKey(), mRankingMap, UNDEFINED_DISMISS_REASON); |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 256 | |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 257 | verify(mPresenter).updateNotificationViews(); |
Ned Burns | ef2ef6c | 2019-01-02 16:48:08 -0500 | [diff] [blame] | 258 | verify(mEntryListener).onEntryRemoved( |
Julia Reynolds | 138111f | 2020-02-26 11:17:39 -0500 | [diff] [blame] | 259 | eq(mEntry), any(), eq(false) /* removedByUser */, eq(UNDEFINED_DISMISS_REASON)); |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 260 | verify(mRow).setRemoved(); |
| 261 | |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 262 | assertNull(mEntryManager.getActiveNotificationUnfiltered(mSbn.getKey())); |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 263 | } |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 264 | |
| 265 | @Test |
Beverly | 8ca664b | 2020-05-07 14:46:50 -0400 | [diff] [blame] | 266 | public void testRemoveUninflatedNotification_removesNotificationFromAllNotifsList() { |
| 267 | // GIVEN an uninflated entry is added |
| 268 | mEntryManager.addNotification(mSbn, mRankingMap); |
| 269 | assertTrue(entriesContainKey(mEntryManager.getAllNotifs(), mSbn.getKey())); |
| 270 | |
| 271 | // WHEN the uninflated entry is removed |
| 272 | mEntryManager.performRemoveNotification(mSbn, UNDEFINED_DISMISS_REASON); |
| 273 | |
| 274 | // THEN the entry is still removed from the allNotifications list |
| 275 | assertFalse(entriesContainKey(mEntryManager.getAllNotifs(), mSbn.getKey())); |
| 276 | } |
| 277 | |
| 278 | @Test |
Ned Burns | ef2ef6c | 2019-01-02 16:48:08 -0500 | [diff] [blame] | 279 | public void testRemoveNotification_onEntryRemoveNotFiredIfEntryDoesntExist() { |
Ned Burns | ef2ef6c | 2019-01-02 16:48:08 -0500 | [diff] [blame] | 280 | |
Mady Mellor | 1a4e86f | 2019-05-03 16:07:23 -0700 | [diff] [blame] | 281 | mEntryManager.removeNotification("not_a_real_key", mRankingMap, UNDEFINED_DISMISS_REASON); |
Ned Burns | ef2ef6c | 2019-01-02 16:48:08 -0500 | [diff] [blame] | 282 | |
| 283 | verify(mEntryListener, never()).onEntryRemoved( |
Julia Reynolds | 138111f | 2020-02-26 11:17:39 -0500 | [diff] [blame] | 284 | eq(mEntry), any(), eq(false) /* removedByUser */, eq(UNDEFINED_DISMISS_REASON)); |
Ned Burns | ef2ef6c | 2019-01-02 16:48:08 -0500 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | @Test |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 288 | public void testRemoveNotification_whilePending() { |
Ned Burns | ef2ef6c | 2019-01-02 16:48:08 -0500 | [diff] [blame] | 289 | mEntryManager.addNotification(mSbn, mRankingMap); |
Mady Mellor | 1a4e86f | 2019-05-03 16:07:23 -0700 | [diff] [blame] | 290 | mEntryManager.removeNotification(mSbn.getKey(), mRankingMap, UNDEFINED_DISMISS_REASON); |
Ned Burns | ef2ef6c | 2019-01-02 16:48:08 -0500 | [diff] [blame] | 291 | |
| 292 | verify(mEntryListener, never()).onEntryRemoved( |
Julia Reynolds | 138111f | 2020-02-26 11:17:39 -0500 | [diff] [blame] | 293 | eq(mEntry), any(), eq(false /* removedByUser */), eq(UNDEFINED_DISMISS_REASON)); |
Kenny Guy | 8cc15d2 | 2018-05-09 09:50:55 +0100 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | @Test |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 297 | public void testUpdateNotificationRanking() { |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 298 | when(mDeviceProvisionedController.isDeviceProvisioned()).thenReturn(true); |
| 299 | when(mEnvironment.isDeviceProvisioned()).thenReturn(true); |
| 300 | when(mEnvironment.isNotificationForCurrentProfiles(any())).thenReturn(true); |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 301 | |
Evan Laird | 9449285 | 2018-10-25 13:43:01 -0400 | [diff] [blame] | 302 | mEntry.setRow(mRow); |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 303 | mEntry.setInflationTask(mAsyncInflationTask); |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 304 | mEntryManager.addActiveNotificationForTest(mEntry); |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 305 | setSmartActions(mEntry.getKey(), new ArrayList<>(Arrays.asList(createAction()))); |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 306 | |
| 307 | mEntryManager.updateNotificationRanking(mRankingMap); |
Ned Burns | 5a9e3520 | 2019-09-06 22:26:33 -0400 | [diff] [blame] | 308 | assertEquals(1, mEntry.getSmartActions().size()); |
| 309 | assertEquals("action", mEntry.getSmartActions().get(0).title); |
Mady Mellor | 6847869 | 2019-05-23 14:40:54 -0700 | [diff] [blame] | 310 | verify(mEntryListener).onNotificationRankingUpdated(mRankingMap); |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | @Test |
| 314 | public void testUpdateNotificationRanking_noChange() { |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 315 | when(mDeviceProvisionedController.isDeviceProvisioned()).thenReturn(true); |
| 316 | when(mEnvironment.isNotificationForCurrentProfiles(any())).thenReturn(true); |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 317 | |
Evan Laird | 9449285 | 2018-10-25 13:43:01 -0400 | [diff] [blame] | 318 | mEntry.setRow(mRow); |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 319 | mEntryManager.addActiveNotificationForTest(mEntry); |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 320 | setSmartActions(mEntry.getKey(), null); |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 321 | |
| 322 | mEntryManager.updateNotificationRanking(mRankingMap); |
Kevin | d4660b2 | 2018-09-27 10:57:35 -0700 | [diff] [blame] | 323 | verify(mRow, never()).setEntry(eq(mEntry)); |
Ned Burns | 5a9e3520 | 2019-09-06 22:26:33 -0400 | [diff] [blame] | 324 | assertNull(mEntry.getSmartActions()); |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | @Test |
| 328 | public void testUpdateNotificationRanking_rowNotInflatedYet() { |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 329 | when(mDeviceProvisionedController.isDeviceProvisioned()).thenReturn(true); |
| 330 | when(mEnvironment.isNotificationForCurrentProfiles(any())).thenReturn(true); |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 331 | |
Evan Laird | 9449285 | 2018-10-25 13:43:01 -0400 | [diff] [blame] | 332 | mEntry.setRow(null); |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 333 | mEntryManager.addActiveNotificationForTest(mEntry); |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 334 | setSmartActions(mEntry.getKey(), new ArrayList<>(Arrays.asList(createAction()))); |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 335 | |
| 336 | mEntryManager.updateNotificationRanking(mRankingMap); |
Kevin | d4660b2 | 2018-09-27 10:57:35 -0700 | [diff] [blame] | 337 | verify(mRow, never()).setEntry(eq(mEntry)); |
Ned Burns | 5a9e3520 | 2019-09-06 22:26:33 -0400 | [diff] [blame] | 338 | assertEquals(1, mEntry.getSmartActions().size()); |
| 339 | assertEquals("action", mEntry.getSmartActions().get(0).title); |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | @Test |
| 343 | public void testUpdateNotificationRanking_pendingNotification() { |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 344 | when(mDeviceProvisionedController.isDeviceProvisioned()).thenReturn(true); |
| 345 | when(mEnvironment.isNotificationForCurrentProfiles(any())).thenReturn(true); |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 346 | |
Evan Laird | 9449285 | 2018-10-25 13:43:01 -0400 | [diff] [blame] | 347 | mEntry.setRow(null); |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 348 | mEntryManager.mPendingNotifications.put(mEntry.getKey(), mEntry); |
| 349 | setSmartActions(mEntry.getKey(), new ArrayList<>(Arrays.asList(createAction()))); |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 350 | |
| 351 | mEntryManager.updateNotificationRanking(mRankingMap); |
Kevin | d4660b2 | 2018-09-27 10:57:35 -0700 | [diff] [blame] | 352 | verify(mRow, never()).setEntry(eq(mEntry)); |
Ned Burns | 5a9e3520 | 2019-09-06 22:26:33 -0400 | [diff] [blame] | 353 | assertEquals(1, mEntry.getSmartActions().size()); |
| 354 | assertEquals("action", mEntry.getSmartActions().get(0).title); |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 355 | } |
| 356 | |
Ned Burns | a5dad55 | 2019-01-29 17:59:10 -0500 | [diff] [blame] | 357 | @Test |
| 358 | public void testLifetimeExtenders_ifNotificationIsRetainedItIsntRemoved() { |
| 359 | // GIVEN an entry manager with a notification |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 360 | mEntryManager.addActiveNotificationForTest(mEntry); |
Ned Burns | a5dad55 | 2019-01-29 17:59:10 -0500 | [diff] [blame] | 361 | |
| 362 | // GIVEN a lifetime extender that always tries to extend lifetime |
| 363 | NotificationLifetimeExtender extender = mock(NotificationLifetimeExtender.class); |
| 364 | when(extender.shouldExtendLifetime(mEntry)).thenReturn(true); |
| 365 | mEntryManager.addNotificationLifetimeExtender(extender); |
| 366 | |
| 367 | // WHEN the notification is removed |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 368 | mEntryManager.removeNotification(mEntry.getKey(), mRankingMap, UNDEFINED_DISMISS_REASON); |
Ned Burns | a5dad55 | 2019-01-29 17:59:10 -0500 | [diff] [blame] | 369 | |
| 370 | // THEN the extender is asked to manage the lifetime |
| 371 | verify(extender).setShouldManageLifetime(mEntry, true); |
| 372 | // THEN the notification is retained |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 373 | assertNotNull(mEntryManager.getActiveNotificationUnfiltered(mSbn.getKey())); |
Julia Reynolds | 138111f | 2020-02-26 11:17:39 -0500 | [diff] [blame] | 374 | verify(mEntryListener, never()).onEntryRemoved( |
| 375 | eq(mEntry), any(), eq(false), eq(UNDEFINED_DISMISS_REASON)); |
Ned Burns | a5dad55 | 2019-01-29 17:59:10 -0500 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | @Test |
| 379 | public void testLifetimeExtenders_whenRetentionEndsNotificationIsRemoved() { |
| 380 | // GIVEN an entry manager with a notification whose life has been extended |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 381 | mEntryManager.addActiveNotificationForTest(mEntry); |
Ned Burns | a5dad55 | 2019-01-29 17:59:10 -0500 | [diff] [blame] | 382 | final FakeNotificationLifetimeExtender extender = new FakeNotificationLifetimeExtender(); |
| 383 | mEntryManager.addNotificationLifetimeExtender(extender); |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 384 | mEntryManager.removeNotification(mEntry.getKey(), mRankingMap, UNDEFINED_DISMISS_REASON); |
| 385 | assertTrue(extender.isManaging(mEntry.getKey())); |
Ned Burns | a5dad55 | 2019-01-29 17:59:10 -0500 | [diff] [blame] | 386 | |
| 387 | // WHEN the extender finishes its extension |
| 388 | extender.setExtendLifetimes(false); |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 389 | extender.getCallback().onSafeToRemove(mEntry.getKey()); |
Ned Burns | a5dad55 | 2019-01-29 17:59:10 -0500 | [diff] [blame] | 390 | |
| 391 | // THEN the notification is removed |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 392 | assertNull(mEntryManager.getActiveNotificationUnfiltered(mSbn.getKey())); |
Julia Reynolds | 138111f | 2020-02-26 11:17:39 -0500 | [diff] [blame] | 393 | verify(mEntryListener).onEntryRemoved( |
| 394 | eq(mEntry), any(), eq(false), eq(UNDEFINED_DISMISS_REASON)); |
Ned Burns | a5dad55 | 2019-01-29 17:59:10 -0500 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | @Test |
| 398 | public void testLifetimeExtenders_whenNotificationUpdatedRetainersAreCanceled() { |
| 399 | // GIVEN an entry manager with a notification whose life has been extended |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 400 | mEntryManager.addActiveNotificationForTest(mEntry); |
Ned Burns | a5dad55 | 2019-01-29 17:59:10 -0500 | [diff] [blame] | 401 | NotificationLifetimeExtender extender = mock(NotificationLifetimeExtender.class); |
| 402 | when(extender.shouldExtendLifetime(mEntry)).thenReturn(true); |
| 403 | mEntryManager.addNotificationLifetimeExtender(extender); |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 404 | mEntryManager.removeNotification(mEntry.getKey(), mRankingMap, UNDEFINED_DISMISS_REASON); |
Ned Burns | a5dad55 | 2019-01-29 17:59:10 -0500 | [diff] [blame] | 405 | |
| 406 | // WHEN the notification is updated |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 407 | mEntryManager.updateNotification(mEntry.getSbn(), mRankingMap); |
Ned Burns | a5dad55 | 2019-01-29 17:59:10 -0500 | [diff] [blame] | 408 | |
| 409 | // THEN the lifetime extension is canceled |
| 410 | verify(extender).setShouldManageLifetime(mEntry, false); |
| 411 | } |
| 412 | |
| 413 | @Test |
| 414 | public void testLifetimeExtenders_whenNewExtenderTakesPrecedenceOldExtenderIsCanceled() { |
| 415 | // GIVEN an entry manager with a notification |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 416 | mEntryManager.addActiveNotificationForTest(mEntry); |
Ned Burns | a5dad55 | 2019-01-29 17:59:10 -0500 | [diff] [blame] | 417 | |
| 418 | // GIVEN two lifetime extenders, the first which never extends and the second which |
| 419 | // always extends |
| 420 | NotificationLifetimeExtender extender1 = mock(NotificationLifetimeExtender.class); |
| 421 | when(extender1.shouldExtendLifetime(mEntry)).thenReturn(false); |
| 422 | NotificationLifetimeExtender extender2 = mock(NotificationLifetimeExtender.class); |
| 423 | when(extender2.shouldExtendLifetime(mEntry)).thenReturn(true); |
| 424 | mEntryManager.addNotificationLifetimeExtender(extender1); |
| 425 | mEntryManager.addNotificationLifetimeExtender(extender2); |
| 426 | |
| 427 | // GIVEN a notification was lifetime-extended and extender2 is managing it |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 428 | mEntryManager.removeNotification(mEntry.getKey(), mRankingMap, UNDEFINED_DISMISS_REASON); |
Ned Burns | a5dad55 | 2019-01-29 17:59:10 -0500 | [diff] [blame] | 429 | verify(extender1, never()).setShouldManageLifetime(mEntry, true); |
| 430 | verify(extender2).setShouldManageLifetime(mEntry, true); |
| 431 | |
| 432 | // WHEN the extender1 changes its mind and wants to extend the lifetime of the notif |
| 433 | when(extender1.shouldExtendLifetime(mEntry)).thenReturn(true); |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 434 | mEntryManager.removeNotification(mEntry.getKey(), mRankingMap, UNDEFINED_DISMISS_REASON); |
Ned Burns | a5dad55 | 2019-01-29 17:59:10 -0500 | [diff] [blame] | 435 | |
| 436 | // THEN extender2 stops managing the notif and extender1 starts managing it |
| 437 | verify(extender1).setShouldManageLifetime(mEntry, true); |
| 438 | verify(extender2).setShouldManageLifetime(mEntry, false); |
| 439 | } |
| 440 | |
Gustav Sennton | 744e7e1 | 2019-01-31 14:45:03 +0000 | [diff] [blame] | 441 | /** |
| 442 | * Ensure that calling NotificationEntryManager.performRemoveNotification() doesn't crash when |
| 443 | * given a notification that has already been removed from NotificationData. |
| 444 | */ |
| 445 | @Test |
| 446 | public void testPerformRemoveNotification_removedEntry() { |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 447 | mEntryManager.removeNotification(mSbn.getKey(), null, 0); |
Mady Mellor | c2ff011 | 2019-03-28 14:18:06 -0700 | [diff] [blame] | 448 | mEntryManager.performRemoveNotification(mSbn, REASON_CANCEL); |
| 449 | } |
| 450 | |
| 451 | @Test |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 452 | public void testRemoveInterceptor_interceptsDontGetRemoved() throws InterruptedException { |
Mady Mellor | c2ff011 | 2019-03-28 14:18:06 -0700 | [diff] [blame] | 453 | // GIVEN an entry manager with a notification |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 454 | mEntryManager.addActiveNotificationForTest(mEntry); |
Mady Mellor | c2ff011 | 2019-03-28 14:18:06 -0700 | [diff] [blame] | 455 | |
| 456 | // GIVEN interceptor that intercepts that entry |
Evan Laird | 0437366 | 2020-01-24 17:37:39 -0500 | [diff] [blame] | 457 | when(mRemoveInterceptor.onNotificationRemoveRequested( |
| 458 | eq(mEntry.getKey()), eq(mEntry), anyInt())) |
Mady Mellor | c2ff011 | 2019-03-28 14:18:06 -0700 | [diff] [blame] | 459 | .thenReturn(true); |
| 460 | |
| 461 | // WHEN the notification is removed |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 462 | mEntryManager.removeNotification(mEntry.getKey(), mRankingMap, UNDEFINED_DISMISS_REASON); |
Mady Mellor | c2ff011 | 2019-03-28 14:18:06 -0700 | [diff] [blame] | 463 | |
| 464 | // THEN the interceptor intercepts & the entry is not removed & no listeners are called |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 465 | assertNotNull(mEntryManager.getActiveNotificationUnfiltered(mSbn.getKey())); |
Mady Mellor | c2ff011 | 2019-03-28 14:18:06 -0700 | [diff] [blame] | 466 | verify(mEntryListener, never()).onEntryRemoved(eq(mEntry), |
Julia Reynolds | 138111f | 2020-02-26 11:17:39 -0500 | [diff] [blame] | 467 | any(NotificationVisibility.class), anyBoolean(), eq(UNDEFINED_DISMISS_REASON)); |
Mady Mellor | c2ff011 | 2019-03-28 14:18:06 -0700 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | @Test |
| 471 | public void testRemoveInterceptor_notInterceptedGetsRemoved() { |
| 472 | // GIVEN an entry manager with a notification |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 473 | mEntryManager.addActiveNotificationForTest(mEntry); |
Mady Mellor | c2ff011 | 2019-03-28 14:18:06 -0700 | [diff] [blame] | 474 | |
| 475 | // GIVEN interceptor that doesn't intercept |
Evan Laird | 0437366 | 2020-01-24 17:37:39 -0500 | [diff] [blame] | 476 | when(mRemoveInterceptor.onNotificationRemoveRequested( |
| 477 | eq(mEntry.getKey()), eq(mEntry), anyInt())) |
Mady Mellor | c2ff011 | 2019-03-28 14:18:06 -0700 | [diff] [blame] | 478 | .thenReturn(false); |
| 479 | |
| 480 | // WHEN the notification is removed |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 481 | mEntryManager.removeNotification(mEntry.getKey(), mRankingMap, UNDEFINED_DISMISS_REASON); |
Mady Mellor | c2ff011 | 2019-03-28 14:18:06 -0700 | [diff] [blame] | 482 | |
| 483 | // THEN the interceptor intercepts & the entry is not removed & no listeners are called |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 484 | assertNull(mEntryManager.getActiveNotificationUnfiltered(mSbn.getKey())); |
Mady Mellor | c2ff011 | 2019-03-28 14:18:06 -0700 | [diff] [blame] | 485 | verify(mEntryListener, atLeastOnce()).onEntryRemoved(eq(mEntry), |
Julia Reynolds | 138111f | 2020-02-26 11:17:39 -0500 | [diff] [blame] | 486 | any(NotificationVisibility.class), anyBoolean(), eq(UNDEFINED_DISMISS_REASON)); |
Gustav Sennton | 744e7e1 | 2019-01-31 14:45:03 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Beverly | 201cdd5 | 2019-10-18 14:30:46 -0400 | [diff] [blame] | 489 | private NotificationEntry createNotification() { |
Julia Reynolds | dcd70d6 | 2020-01-15 10:33:43 -0500 | [diff] [blame] | 490 | Notification.Builder n = new Notification.Builder(mContext, "id") |
Beverly | 201cdd5 | 2019-10-18 14:30:46 -0400 | [diff] [blame] | 491 | .setSmallIcon(R.drawable.ic_person) |
| 492 | .setContentTitle("Title") |
| 493 | .setContentText("Text"); |
| 494 | |
| 495 | return new NotificationEntryBuilder() |
| 496 | .setPkg(TEST_PACKAGE_NAME) |
| 497 | .setOpPkg(TEST_PACKAGE_NAME) |
| 498 | .setUid(TEST_UID) |
| 499 | .setId(mId++) |
| 500 | .setNotification(n.build()) |
Julia Reynolds | dcd70d6 | 2020-01-15 10:33:43 -0500 | [diff] [blame] | 501 | .setChannel(new NotificationChannel("id", "", IMPORTANCE_DEFAULT)) |
Beverly | 201cdd5 | 2019-10-18 14:30:46 -0400 | [diff] [blame] | 502 | .setUser(new UserHandle(ActivityManager.getCurrentUser())) |
| 503 | .build(); |
| 504 | } |
| 505 | |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 506 | /* Tests annexed from NotificationDataTest go here */ |
| 507 | |
| 508 | @Test |
| 509 | public void testChannelIsSetWhenAdded() { |
| 510 | NotificationChannel nc = new NotificationChannel( |
| 511 | "testId", |
| 512 | "testName", |
| 513 | IMPORTANCE_DEFAULT); |
| 514 | |
| 515 | Ranking r = new RankingBuilder() |
| 516 | .setKey(mEntry.getKey()) |
| 517 | .setChannel(nc) |
| 518 | .build(); |
| 519 | |
| 520 | RankingMap rm = new RankingMap(new Ranking[] { r }); |
| 521 | |
| 522 | // GIVEN: a notification is added, and the ranking updated |
| 523 | mEntryManager.addActiveNotificationForTest(mEntry); |
| 524 | mEntryManager.updateRanking(rm, "testReason"); |
| 525 | |
| 526 | // THEN the notification entry better have a channel on it |
| 527 | assertEquals( |
| 528 | "Channel must be set when adding a notification", |
| 529 | nc.getName(), |
| 530 | mEntry.getChannel().getName()); |
| 531 | } |
| 532 | |
| 533 | @Test |
| 534 | public void testGetNotificationsForCurrentUser_shouldFilterNonCurrentUserNotifications() { |
Julia Reynolds | dcd70d6 | 2020-01-15 10:33:43 -0500 | [diff] [blame] | 535 | Notification.Builder n = new Notification.Builder(mContext, "di") |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 536 | .setSmallIcon(R.drawable.ic_person) |
| 537 | .setContentTitle("Title") |
| 538 | .setContentText("Text"); |
| 539 | |
| 540 | NotificationEntry e2 = new NotificationEntryBuilder() |
| 541 | .setPkg(TEST_PACKAGE_NAME) |
| 542 | .setOpPkg(TEST_PACKAGE_NAME) |
| 543 | .setUid(TEST_UID) |
| 544 | .setId(mId++) |
| 545 | .setNotification(n.build()) |
| 546 | .setUser(new UserHandle(ActivityManager.getCurrentUser())) |
Julia Reynolds | dcd70d6 | 2020-01-15 10:33:43 -0500 | [diff] [blame] | 547 | .setChannel(new NotificationChannel("id", "", IMPORTANCE_DEFAULT)) |
Evan Laird | 181de62 | 2019-10-24 09:53:02 -0400 | [diff] [blame] | 548 | .build(); |
| 549 | |
| 550 | mEntryManager.addActiveNotificationForTest(mEntry); |
| 551 | mEntryManager.addActiveNotificationForTest(e2); |
| 552 | |
| 553 | when(mEnvironment.isNotificationForCurrentProfiles(mEntry.getSbn())).thenReturn(false); |
| 554 | when(mEnvironment.isNotificationForCurrentProfiles(e2.getSbn())).thenReturn(true); |
| 555 | |
| 556 | List<NotificationEntry> result = mEntryManager.getActiveNotificationsForCurrentUser(); |
| 557 | assertEquals(result.size(), 1); |
| 558 | junit.framework.Assert.assertEquals(result.get(0), e2); |
| 559 | } |
| 560 | |
| 561 | /* End annex */ |
| 562 | |
Beverly | 8ca664b | 2020-05-07 14:46:50 -0400 | [diff] [blame] | 563 | private boolean entriesContainKey(Collection<NotificationEntry> entries, String key) { |
| 564 | for (NotificationEntry entry : entries) { |
| 565 | if (entry.getSbn().getKey().equals(key)) { |
| 566 | return true; |
| 567 | } |
| 568 | } |
| 569 | return false; |
| 570 | } |
| 571 | |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 572 | private Notification.Action createAction() { |
| 573 | return new Notification.Action.Builder( |
| 574 | Icon.createWithResource(getContext(), android.R.drawable.sym_def_app_icon), |
| 575 | "action", |
| 576 | PendingIntent.getBroadcast(getContext(), 0, new Intent("Action"), 0)).build(); |
| 577 | } |
Ned Burns | a5dad55 | 2019-01-29 17:59:10 -0500 | [diff] [blame] | 578 | |
| 579 | private static class FakeNotificationLifetimeExtender implements NotificationLifetimeExtender { |
| 580 | private NotificationSafeToRemoveCallback mCallback; |
| 581 | private boolean mExtendLifetimes = true; |
| 582 | private Set<String> mManagedNotifs = new ArraySet<>(); |
| 583 | |
| 584 | @Override |
| 585 | public void setCallback(@NonNull NotificationSafeToRemoveCallback callback) { |
| 586 | mCallback = callback; |
| 587 | } |
| 588 | |
| 589 | @Override |
| 590 | public boolean shouldExtendLifetime(@NonNull NotificationEntry entry) { |
| 591 | return mExtendLifetimes; |
| 592 | } |
| 593 | |
| 594 | @Override |
| 595 | public void setShouldManageLifetime( |
| 596 | @NonNull NotificationEntry entry, |
| 597 | boolean shouldManage) { |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 598 | final boolean hasEntry = mManagedNotifs.contains(entry.getKey()); |
Ned Burns | a5dad55 | 2019-01-29 17:59:10 -0500 | [diff] [blame] | 599 | if (shouldManage) { |
| 600 | if (hasEntry) { |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 601 | throw new RuntimeException("Already managing this entry: " + entry.getKey()); |
Ned Burns | a5dad55 | 2019-01-29 17:59:10 -0500 | [diff] [blame] | 602 | } |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 603 | mManagedNotifs.add(entry.getKey()); |
Ned Burns | a5dad55 | 2019-01-29 17:59:10 -0500 | [diff] [blame] | 604 | } else { |
| 605 | if (!hasEntry) { |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 606 | throw new RuntimeException("Not managing this entry: " + entry.getKey()); |
Ned Burns | a5dad55 | 2019-01-29 17:59:10 -0500 | [diff] [blame] | 607 | } |
Ned Burns | 00b4b2d | 2019-10-17 22:09:27 -0400 | [diff] [blame] | 608 | mManagedNotifs.remove(entry.getKey()); |
Ned Burns | a5dad55 | 2019-01-29 17:59:10 -0500 | [diff] [blame] | 609 | } |
| 610 | } |
| 611 | |
| 612 | public void setExtendLifetimes(boolean extendLifetimes) { |
| 613 | mExtendLifetimes = extendLifetimes; |
| 614 | } |
| 615 | |
| 616 | public NotificationSafeToRemoveCallback getCallback() { |
| 617 | return mCallback; |
| 618 | } |
| 619 | |
| 620 | public boolean isManaging(String notificationKey) { |
| 621 | return mManagedNotifs.contains(notificationKey); |
| 622 | } |
| 623 | } |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 624 | } |