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