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 | |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 20 | import static com.android.systemui.statusbar.notification.row.NotificationInflater.FLAG_CONTENT_VIEW_CONTRACTED; |
| 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; |
| 26 | |
| 27 | import android.app.ActivityManager; |
| 28 | import android.app.Notification; |
| 29 | import android.os.Handler; |
| 30 | import android.os.Looper; |
| 31 | import android.os.UserHandle; |
| 32 | import android.service.notification.StatusBarNotification; |
| 33 | import android.support.test.filters.SmallTest; |
| 34 | import android.testing.AndroidTestingRunner; |
| 35 | import android.testing.TestableLooper; |
| 36 | |
| 37 | import com.android.systemui.R; |
| 38 | import com.android.systemui.SysuiTestCase; |
Kevin | d4f66a4 | 2018-08-03 13:12:51 -0700 | [diff] [blame] | 39 | import com.android.systemui.statusbar.notification.NotificationData; |
| 40 | import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow; |
| 41 | |
| 42 | |
| 43 | import org.junit.Before; |
| 44 | import org.junit.Rule; |
| 45 | import org.junit.Test; |
| 46 | import org.junit.runner.RunWith; |
| 47 | import org.mockito.Mock; |
| 48 | import org.mockito.junit.MockitoJUnit; |
| 49 | import org.mockito.junit.MockitoRule; |
| 50 | |
| 51 | @SmallTest |
| 52 | @RunWith(AndroidTestingRunner.class) |
| 53 | @TestableLooper.RunWithLooper |
| 54 | public class AlertingNotificationManagerTest extends SysuiTestCase { |
| 55 | @Rule |
| 56 | public MockitoRule rule = MockitoJUnit.rule(); |
| 57 | |
| 58 | private static final String TEST_PACKAGE_NAME = "test"; |
| 59 | private static final int TEST_UID = 0; |
| 60 | |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 61 | protected static final int TEST_MINIMUM_DISPLAY_TIME = 200; |
| 62 | protected static final int TEST_AUTO_DISMISS_TIME = 500; |
Kevin | d4f66a4 | 2018-08-03 13:12:51 -0700 | [diff] [blame] | 63 | // Number of notifications to use in tests requiring multiple notifications |
| 64 | private static final int TEST_NUM_NOTIFICATIONS = 4; |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 65 | protected static final int TEST_TIMEOUT_TIME = 10000; |
| 66 | protected final Runnable TEST_TIMEOUT_RUNNABLE = () -> mTimedOut = true; |
Kevin | d4f66a4 | 2018-08-03 13:12:51 -0700 | [diff] [blame] | 67 | |
| 68 | private AlertingNotificationManager mAlertingNotificationManager; |
| 69 | |
| 70 | protected NotificationData.Entry mEntry; |
| 71 | protected Handler mTestHandler; |
| 72 | private StatusBarNotification mSbn; |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 73 | protected boolean mTimedOut = false; |
Kevin | d4f66a4 | 2018-08-03 13:12:51 -0700 | [diff] [blame] | 74 | |
| 75 | @Mock protected ExpandableNotificationRow mRow; |
| 76 | |
| 77 | private final class TestableAlertingNotificationManager extends AlertingNotificationManager { |
| 78 | private TestableAlertingNotificationManager() { |
| 79 | mMinimumDisplayTime = TEST_MINIMUM_DISPLAY_TIME; |
| 80 | mAutoDismissNotificationDecay = TEST_AUTO_DISMISS_TIME; |
| 81 | mHandler = mTestHandler; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | protected void onAlertEntryAdded(AlertEntry alertEntry) {} |
| 86 | |
| 87 | @Override |
| 88 | protected void onAlertEntryRemoved(AlertEntry alertEntry) {} |
Kevin | 01a53cb | 2018-11-09 18:19:54 -0800 | [diff] [blame] | 89 | |
| 90 | @Override |
| 91 | public int getContentFlag() { |
| 92 | return FLAG_CONTENT_VIEW_CONTRACTED; |
| 93 | } |
Kevin | d4f66a4 | 2018-08-03 13:12:51 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | protected AlertingNotificationManager createAlertingNotificationManager() { |
| 97 | return new TestableAlertingNotificationManager(); |
| 98 | } |
| 99 | |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 100 | protected StatusBarNotification createNewNotification(int id) { |
Kevin | d4f66a4 | 2018-08-03 13:12:51 -0700 | [diff] [blame] | 101 | Notification.Builder n = new Notification.Builder(mContext, "") |
| 102 | .setSmallIcon(R.drawable.ic_person) |
| 103 | .setContentTitle("Title") |
| 104 | .setContentText("Text"); |
| 105 | return new StatusBarNotification( |
| 106 | TEST_PACKAGE_NAME /* pkg */, |
| 107 | TEST_PACKAGE_NAME, |
| 108 | id, |
| 109 | null /* tag */, |
| 110 | TEST_UID, |
| 111 | 0 /* initialPid */, |
| 112 | n.build(), |
| 113 | new UserHandle(ActivityManager.getCurrentUser()), |
| 114 | null /* overrideGroupKey */, |
| 115 | 0 /* postTime */); |
| 116 | } |
| 117 | |
| 118 | @Before |
| 119 | public void setUp() { |
| 120 | mTestHandler = Handler.createAsync(Looper.myLooper()); |
| 121 | mSbn = createNewNotification(0 /* id */); |
| 122 | mEntry = new NotificationData.Entry(mSbn); |
| 123 | mEntry.row = mRow; |
| 124 | |
| 125 | mAlertingNotificationManager = createAlertingNotificationManager(); |
| 126 | } |
| 127 | |
| 128 | @Test |
| 129 | public void testShowNotification_addsEntry() { |
| 130 | mAlertingNotificationManager.showNotification(mEntry); |
| 131 | |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 132 | assertTrue(mAlertingNotificationManager.isAlerting(mEntry.key)); |
Kevin | d4f66a4 | 2018-08-03 13:12:51 -0700 | [diff] [blame] | 133 | assertTrue(mAlertingNotificationManager.hasNotifications()); |
| 134 | assertEquals(mEntry, mAlertingNotificationManager.getEntry(mEntry.key)); |
| 135 | } |
| 136 | |
| 137 | @Test |
| 138 | public void testShowNotification_autoDismisses() { |
| 139 | mAlertingNotificationManager.showNotification(mEntry); |
| 140 | mTestHandler.postDelayed(TEST_TIMEOUT_RUNNABLE, TEST_TIMEOUT_TIME); |
| 141 | |
| 142 | // Wait for remove runnable and then process it immediately |
| 143 | TestableLooper.get(this).processMessages(1); |
| 144 | |
| 145 | assertFalse("Test timed out", mTimedOut); |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 146 | assertFalse(mAlertingNotificationManager.isAlerting(mEntry.key)); |
Kevin | d4f66a4 | 2018-08-03 13:12:51 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | @Test |
| 150 | public void testRemoveNotification_removeDeferred() { |
| 151 | mAlertingNotificationManager.showNotification(mEntry); |
| 152 | |
| 153 | // Try to remove but defer, since the notification has not been shown long enough. |
| 154 | mAlertingNotificationManager.removeNotification(mEntry.key, false /* releaseImmediately */); |
| 155 | |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 156 | assertTrue(mAlertingNotificationManager.isAlerting(mEntry.key)); |
Kevin | d4f66a4 | 2018-08-03 13:12:51 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | @Test |
| 160 | public void testRemoveNotification_forceRemove() { |
| 161 | mAlertingNotificationManager.showNotification(mEntry); |
| 162 | |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 163 | // Remove forcibly with releaseImmediately = true. |
Kevin | d4f66a4 | 2018-08-03 13:12:51 -0700 | [diff] [blame] | 164 | mAlertingNotificationManager.removeNotification(mEntry.key, true /* releaseImmediately */); |
| 165 | |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 166 | assertFalse(mAlertingNotificationManager.isAlerting(mEntry.key)); |
Kevin | d4f66a4 | 2018-08-03 13:12:51 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | @Test |
| 170 | public void testReleaseAllImmediately() { |
| 171 | for (int i = 0; i < TEST_NUM_NOTIFICATIONS; i++) { |
| 172 | StatusBarNotification sbn = createNewNotification(i); |
| 173 | NotificationData.Entry entry = new NotificationData.Entry(sbn); |
| 174 | entry.row = mRow; |
| 175 | mAlertingNotificationManager.showNotification(entry); |
| 176 | } |
| 177 | |
| 178 | mAlertingNotificationManager.releaseAllImmediately(); |
| 179 | |
| 180 | assertEquals(0, mAlertingNotificationManager.getAllEntries().count()); |
| 181 | } |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 182 | |
| 183 | @Test |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 184 | public void testCanRemoveImmediately_notShownLongEnough() { |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 185 | mAlertingNotificationManager.showNotification(mEntry); |
| 186 | |
Kevin | a97ea05 | 2018-09-11 13:53:18 -0700 | [diff] [blame] | 187 | // The entry has just been added so we should not remove immediately. |
| 188 | assertFalse(mAlertingNotificationManager.canRemoveImmediately(mEntry.key)); |
| 189 | } |
| 190 | |
| 191 | @Test |
| 192 | public void testShouldExtendLifetime() { |
| 193 | mAlertingNotificationManager.showNotification(mEntry); |
| 194 | |
| 195 | // While the entry is alerting, it should not be removable. |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 196 | assertTrue(mAlertingNotificationManager.shouldExtendLifetime(mEntry)); |
| 197 | } |
| 198 | |
| 199 | @Test |
Kevin | e9e938c | 2018-09-06 13:38:11 -0700 | [diff] [blame] | 200 | public void testSetShouldManageLifetime_setShouldManage() { |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 201 | mAlertingNotificationManager.showNotification(mEntry); |
| 202 | |
Kevin | e9e938c | 2018-09-06 13:38:11 -0700 | [diff] [blame] | 203 | mAlertingNotificationManager.setShouldManageLifetime(mEntry, true /* shouldManage */); |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 204 | |
| 205 | assertTrue(mAlertingNotificationManager.mExtendedLifetimeAlertEntries.contains(mEntry)); |
| 206 | } |
| 207 | |
| 208 | @Test |
Kevin | e9e938c | 2018-09-06 13:38:11 -0700 | [diff] [blame] | 209 | public void testSetShouldManageLifetime_setShouldNotManage() { |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 210 | mAlertingNotificationManager.mExtendedLifetimeAlertEntries.add(mEntry); |
| 211 | |
Kevin | e9e938c | 2018-09-06 13:38:11 -0700 | [diff] [blame] | 212 | mAlertingNotificationManager.setShouldManageLifetime(mEntry, false /* shouldManage */); |
Kevin | a5ff1fa | 2018-08-21 16:35:48 -0700 | [diff] [blame] | 213 | |
| 214 | assertFalse(mAlertingNotificationManager.mExtendedLifetimeAlertEntries.contains(mEntry)); |
| 215 | } |
Kevin | d4f66a4 | 2018-08-03 13:12:51 -0700 | [diff] [blame] | 216 | } |