Kevin | d4f66a4 | 2018-08-03 13:12:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | |
| 18 | package com.android.systemui.statusbar; |
| 19 | |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 20 | import static com.android.systemui.statusbar.notification.row.NotificationContentInflater.FLAG_CONTENT_VIEW_CONTRACTED; |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 21 | |
Kevin | d4f66a4 | 2018-08-03 13:12:51 -0700 | [diff] [blame] | 22 | import static junit.framework.Assert.assertFalse; |
| 23 | import static junit.framework.Assert.assertTrue; |
| 24 | |
| 25 | import static org.junit.Assert.assertEquals; |
Selim Cinek | bd55909 | 2019-01-28 19:38:58 -0800 | [diff] [blame] | 26 | import static org.mockito.Mockito.spy; |
| 27 | import static org.mockito.Mockito.verify; |
Kevin | d4f66a4 | 2018-08-03 13:12:51 -0700 | [diff] [blame] | 28 | |
| 29 | import android.app.ActivityManager; |
| 30 | import android.app.Notification; |
| 31 | import android.os.Handler; |
| 32 | import android.os.Looper; |
| 33 | import android.os.UserHandle; |
| 34 | import android.service.notification.StatusBarNotification; |
Kevin | d4f66a4 | 2018-08-03 13:12:51 -0700 | [diff] [blame] | 35 | import android.testing.AndroidTestingRunner; |
| 36 | import android.testing.TestableLooper; |
| 37 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 38 | import androidx.test.filters.SmallTest; |
| 39 | |
Kevin | d4f66a4 | 2018-08-03 13:12:51 -0700 | [diff] [blame] | 40 | import com.android.systemui.R; |
| 41 | import com.android.systemui.SysuiTestCase; |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 42 | import com.android.systemui.statusbar.notification.collection.NotificationEntry; |
Kevin | d4f66a4 | 2018-08-03 13:12:51 -0700 | [diff] [blame] | 43 | import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow; |
| 44 | |
Kevin | d4f66a4 | 2018-08-03 13:12:51 -0700 | [diff] [blame] | 45 | import org.junit.Before; |
| 46 | import org.junit.Rule; |
| 47 | import org.junit.Test; |
| 48 | import org.junit.runner.RunWith; |
| 49 | import org.mockito.Mock; |
| 50 | import org.mockito.junit.MockitoJUnit; |
| 51 | import org.mockito.junit.MockitoRule; |
| 52 | |
| 53 | @SmallTest |
| 54 | @RunWith(AndroidTestingRunner.class) |
| 55 | @TestableLooper.RunWithLooper |
| 56 | public class AlertingNotificationManagerTest extends SysuiTestCase { |
| 57 | @Rule |
| 58 | public MockitoRule rule = MockitoJUnit.rule(); |
| 59 | |
| 60 | private static final String TEST_PACKAGE_NAME = "test"; |
| 61 | private static final int TEST_UID = 0; |
| 62 | |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 63 | protected static final int TEST_MINIMUM_DISPLAY_TIME = 200; |
| 64 | protected static final int TEST_AUTO_DISMISS_TIME = 500; |
Kevin | d4f66a4 | 2018-08-03 13:12:51 -0700 | [diff] [blame] | 65 | // Number of notifications to use in tests requiring multiple notifications |
| 66 | private static final int TEST_NUM_NOTIFICATIONS = 4; |
Lucas Dupin | d534c12 | 2019-07-16 18:48:59 -0700 | [diff] [blame] | 67 | protected static final int TEST_TIMEOUT_TIME = 15000; |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 68 | protected final Runnable TEST_TIMEOUT_RUNNABLE = () -> mTimedOut = true; |
Kevin | d4f66a4 | 2018-08-03 13:12:51 -0700 | [diff] [blame] | 69 | |
| 70 | private AlertingNotificationManager mAlertingNotificationManager; |
| 71 | |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 72 | protected NotificationEntry mEntry; |
Kevin | d4f66a4 | 2018-08-03 13:12:51 -0700 | [diff] [blame] | 73 | protected Handler mTestHandler; |
| 74 | private StatusBarNotification mSbn; |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 75 | protected boolean mTimedOut = false; |
Kevin | d4f66a4 | 2018-08-03 13:12:51 -0700 | [diff] [blame] | 76 | |
| 77 | @Mock protected ExpandableNotificationRow mRow; |
| 78 | |
| 79 | private final class TestableAlertingNotificationManager extends AlertingNotificationManager { |
Selim Cinek | bd55909 | 2019-01-28 19:38:58 -0800 | [diff] [blame] | 80 | private AlertEntry mLastCreatedEntry; |
| 81 | |
Kevin | d4f66a4 | 2018-08-03 13:12:51 -0700 | [diff] [blame] | 82 | private TestableAlertingNotificationManager() { |
| 83 | mMinimumDisplayTime = TEST_MINIMUM_DISPLAY_TIME; |
| 84 | mAutoDismissNotificationDecay = TEST_AUTO_DISMISS_TIME; |
| 85 | mHandler = mTestHandler; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | protected void onAlertEntryAdded(AlertEntry alertEntry) {} |
| 90 | |
| 91 | @Override |
| 92 | protected void onAlertEntryRemoved(AlertEntry alertEntry) {} |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 93 | |
| 94 | @Override |
Selim Cinek | bd55909 | 2019-01-28 19:38:58 -0800 | [diff] [blame] | 95 | protected AlertEntry createAlertEntry() { |
| 96 | mLastCreatedEntry = spy(super.createAlertEntry()); |
| 97 | return mLastCreatedEntry; |
| 98 | } |
| 99 | |
| 100 | @Override |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 101 | public int getContentFlag() { |
| 102 | return FLAG_CONTENT_VIEW_CONTRACTED; |
| 103 | } |
Kevin | d4f66a4 | 2018-08-03 13:12:51 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | protected AlertingNotificationManager createAlertingNotificationManager() { |
| 107 | return new TestableAlertingNotificationManager(); |
| 108 | } |
| 109 | |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 110 | protected StatusBarNotification createNewNotification(int id) { |
Kevin | d4f66a4 | 2018-08-03 13:12:51 -0700 | [diff] [blame] | 111 | Notification.Builder n = new Notification.Builder(mContext, "") |
| 112 | .setSmallIcon(R.drawable.ic_person) |
| 113 | .setContentTitle("Title") |
| 114 | .setContentText("Text"); |
| 115 | return new StatusBarNotification( |
| 116 | TEST_PACKAGE_NAME /* pkg */, |
| 117 | TEST_PACKAGE_NAME, |
| 118 | id, |
| 119 | null /* tag */, |
| 120 | TEST_UID, |
| 121 | 0 /* initialPid */, |
| 122 | n.build(), |
| 123 | new UserHandle(ActivityManager.getCurrentUser()), |
| 124 | null /* overrideGroupKey */, |
| 125 | 0 /* postTime */); |
| 126 | } |
| 127 | |
| 128 | @Before |
| 129 | public void setUp() { |
| 130 | mTestHandler = Handler.createAsync(Looper.myLooper()); |
| 131 | mSbn = createNewNotification(0 /* id */); |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 132 | mEntry = new NotificationEntry(mSbn); |
Evan Laird | 9449285 | 2018-10-25 13:43:01 -0400 | [diff] [blame] | 133 | mEntry.setRow(mRow); |
Kevin | d4f66a4 | 2018-08-03 13:12:51 -0700 | [diff] [blame] | 134 | |
| 135 | mAlertingNotificationManager = createAlertingNotificationManager(); |
| 136 | } |
| 137 | |
| 138 | @Test |
| 139 | public void testShowNotification_addsEntry() { |
| 140 | mAlertingNotificationManager.showNotification(mEntry); |
| 141 | |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 142 | assertTrue(mAlertingNotificationManager.isAlerting(mEntry.key)); |
Kevin | d4f66a4 | 2018-08-03 13:12:51 -0700 | [diff] [blame] | 143 | assertTrue(mAlertingNotificationManager.hasNotifications()); |
| 144 | assertEquals(mEntry, mAlertingNotificationManager.getEntry(mEntry.key)); |
| 145 | } |
| 146 | |
| 147 | @Test |
| 148 | public void testShowNotification_autoDismisses() { |
| 149 | mAlertingNotificationManager.showNotification(mEntry); |
| 150 | mTestHandler.postDelayed(TEST_TIMEOUT_RUNNABLE, TEST_TIMEOUT_TIME); |
| 151 | |
| 152 | // Wait for remove runnable and then process it immediately |
| 153 | TestableLooper.get(this).processMessages(1); |
| 154 | |
| 155 | assertFalse("Test timed out", mTimedOut); |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 156 | assertFalse(mAlertingNotificationManager.isAlerting(mEntry.key)); |
Kevin | d4f66a4 | 2018-08-03 13:12:51 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | @Test |
| 160 | public void testRemoveNotification_removeDeferred() { |
| 161 | mAlertingNotificationManager.showNotification(mEntry); |
| 162 | |
| 163 | // Try to remove but defer, since the notification has not been shown long enough. |
| 164 | mAlertingNotificationManager.removeNotification(mEntry.key, false /* releaseImmediately */); |
| 165 | |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 166 | assertTrue(mAlertingNotificationManager.isAlerting(mEntry.key)); |
Kevin | d4f66a4 | 2018-08-03 13:12:51 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | @Test |
| 170 | public void testRemoveNotification_forceRemove() { |
| 171 | mAlertingNotificationManager.showNotification(mEntry); |
| 172 | |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 173 | // Remove forcibly with releaseImmediately = true. |
Kevin | d4f66a4 | 2018-08-03 13:12:51 -0700 | [diff] [blame] | 174 | mAlertingNotificationManager.removeNotification(mEntry.key, true /* releaseImmediately */); |
| 175 | |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 176 | assertFalse(mAlertingNotificationManager.isAlerting(mEntry.key)); |
Kevin | d4f66a4 | 2018-08-03 13:12:51 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | @Test |
| 180 | public void testReleaseAllImmediately() { |
| 181 | for (int i = 0; i < TEST_NUM_NOTIFICATIONS; i++) { |
| 182 | StatusBarNotification sbn = createNewNotification(i); |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 183 | NotificationEntry entry = new NotificationEntry(sbn); |
Evan Laird | 9449285 | 2018-10-25 13:43:01 -0400 | [diff] [blame] | 184 | entry.setRow(mRow); |
Kevin | d4f66a4 | 2018-08-03 13:12:51 -0700 | [diff] [blame] | 185 | mAlertingNotificationManager.showNotification(entry); |
| 186 | } |
| 187 | |
| 188 | mAlertingNotificationManager.releaseAllImmediately(); |
| 189 | |
| 190 | assertEquals(0, mAlertingNotificationManager.getAllEntries().count()); |
| 191 | } |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 192 | |
| 193 | @Test |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 194 | public void testCanRemoveImmediately_notShownLongEnough() { |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 195 | mAlertingNotificationManager.showNotification(mEntry); |
| 196 | |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 197 | // The entry has just been added so we should not remove immediately. |
| 198 | assertFalse(mAlertingNotificationManager.canRemoveImmediately(mEntry.key)); |
| 199 | } |
| 200 | |
| 201 | @Test |
| 202 | public void testShouldExtendLifetime() { |
| 203 | mAlertingNotificationManager.showNotification(mEntry); |
| 204 | |
| 205 | // While the entry is alerting, it should not be removable. |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 206 | assertTrue(mAlertingNotificationManager.shouldExtendLifetime(mEntry)); |
| 207 | } |
| 208 | |
| 209 | @Test |
Kevin | e9e938c | 2018-09-06 13:38:11 -0700 | [diff] [blame] | 210 | public void testSetShouldManageLifetime_setShouldManage() { |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 211 | mAlertingNotificationManager.showNotification(mEntry); |
| 212 | |
Kevin | e9e938c | 2018-09-06 13:38:11 -0700 | [diff] [blame] | 213 | mAlertingNotificationManager.setShouldManageLifetime(mEntry, true /* shouldManage */); |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 214 | |
| 215 | assertTrue(mAlertingNotificationManager.mExtendedLifetimeAlertEntries.contains(mEntry)); |
| 216 | } |
| 217 | |
| 218 | @Test |
Selim Cinek | bd55909 | 2019-01-28 19:38:58 -0800 | [diff] [blame] | 219 | public void testSetShouldManageLifetime_setShouldManageCallsRemoval() { |
| 220 | mAlertingNotificationManager.showNotification(mEntry); |
| 221 | mAlertingNotificationManager.setShouldManageLifetime(mEntry, true /* shouldManage */); |
| 222 | if (mAlertingNotificationManager instanceof TestableAlertingNotificationManager) { |
| 223 | TestableAlertingNotificationManager testableManager = |
| 224 | (TestableAlertingNotificationManager) mAlertingNotificationManager; |
| 225 | verify(testableManager.mLastCreatedEntry).removeAsSoonAsPossible(); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | @Test |
Kevin | e9e938c | 2018-09-06 13:38:11 -0700 | [diff] [blame] | 230 | public void testSetShouldManageLifetime_setShouldNotManage() { |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 231 | mAlertingNotificationManager.mExtendedLifetimeAlertEntries.add(mEntry); |
| 232 | |
Kevin | e9e938c | 2018-09-06 13:38:11 -0700 | [diff] [blame] | 233 | mAlertingNotificationManager.setShouldManageLifetime(mEntry, false /* shouldManage */); |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 234 | |
| 235 | assertFalse(mAlertingNotificationManager.mExtendedLifetimeAlertEntries.contains(mEntry)); |
| 236 | } |
Kevin | d4f66a4 | 2018-08-03 13:12:51 -0700 | [diff] [blame] | 237 | } |