blob: 7b1253af12c69ae833484f0a16db40e2221627f4 [file] [log] [blame]
Kevind4f66a42018-08-03 13:12:51 -07001/*
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
18package com.android.systemui.statusbar;
19
Ned Burns1a5e22f2019-02-14 15:11:52 -050020import static com.android.systemui.statusbar.notification.row.NotificationContentInflater.FLAG_CONTENT_VIEW_CONTRACTED;
Kevin01a53cb2018-11-09 18:19:54 -080021
Kevind4f66a42018-08-03 13:12:51 -070022import static junit.framework.Assert.assertFalse;
23import static junit.framework.Assert.assertTrue;
24
25import static org.junit.Assert.assertEquals;
Selim Cinekbd559092019-01-28 19:38:58 -080026import static org.mockito.Mockito.spy;
27import static org.mockito.Mockito.verify;
Kevind4f66a42018-08-03 13:12:51 -070028
29import android.app.ActivityManager;
30import android.app.Notification;
31import android.os.Handler;
32import android.os.Looper;
33import android.os.UserHandle;
34import android.service.notification.StatusBarNotification;
35import android.support.test.filters.SmallTest;
36import android.testing.AndroidTestingRunner;
37import android.testing.TestableLooper;
38
39import com.android.systemui.R;
40import com.android.systemui.SysuiTestCase;
Ned Burnsf81c4c42019-01-07 14:10:43 -050041import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Kevind4f66a42018-08-03 13:12:51 -070042import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow;
43
44
45import org.junit.Before;
46import org.junit.Rule;
47import org.junit.Test;
48import org.junit.runner.RunWith;
49import org.mockito.Mock;
50import org.mockito.junit.MockitoJUnit;
51import org.mockito.junit.MockitoRule;
52
53@SmallTest
54@RunWith(AndroidTestingRunner.class)
55@TestableLooper.RunWithLooper
56public 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
Kevina97ea052018-09-11 13:53:18 -070063 protected static final int TEST_MINIMUM_DISPLAY_TIME = 200;
64 protected static final int TEST_AUTO_DISMISS_TIME = 500;
Kevind4f66a42018-08-03 13:12:51 -070065 // Number of notifications to use in tests requiring multiple notifications
66 private static final int TEST_NUM_NOTIFICATIONS = 4;
Kevina97ea052018-09-11 13:53:18 -070067 protected static final int TEST_TIMEOUT_TIME = 10000;
68 protected final Runnable TEST_TIMEOUT_RUNNABLE = () -> mTimedOut = true;
Kevind4f66a42018-08-03 13:12:51 -070069
70 private AlertingNotificationManager mAlertingNotificationManager;
71
Ned Burnsf81c4c42019-01-07 14:10:43 -050072 protected NotificationEntry mEntry;
Kevind4f66a42018-08-03 13:12:51 -070073 protected Handler mTestHandler;
74 private StatusBarNotification mSbn;
Kevina97ea052018-09-11 13:53:18 -070075 protected boolean mTimedOut = false;
Kevind4f66a42018-08-03 13:12:51 -070076
77 @Mock protected ExpandableNotificationRow mRow;
78
79 private final class TestableAlertingNotificationManager extends AlertingNotificationManager {
Selim Cinekbd559092019-01-28 19:38:58 -080080 private AlertEntry mLastCreatedEntry;
81
Kevind4f66a42018-08-03 13:12:51 -070082 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) {}
Kevin01a53cb2018-11-09 18:19:54 -080093
94 @Override
Selim Cinekbd559092019-01-28 19:38:58 -080095 protected AlertEntry createAlertEntry() {
96 mLastCreatedEntry = spy(super.createAlertEntry());
97 return mLastCreatedEntry;
98 }
99
100 @Override
Kevin01a53cb2018-11-09 18:19:54 -0800101 public int getContentFlag() {
102 return FLAG_CONTENT_VIEW_CONTRACTED;
103 }
Kevind4f66a42018-08-03 13:12:51 -0700104 }
105
106 protected AlertingNotificationManager createAlertingNotificationManager() {
107 return new TestableAlertingNotificationManager();
108 }
109
Kevina5ff1fa2018-08-21 16:35:48 -0700110 protected StatusBarNotification createNewNotification(int id) {
Kevind4f66a42018-08-03 13:12:51 -0700111 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 Burnsf81c4c42019-01-07 14:10:43 -0500132 mEntry = new NotificationEntry(mSbn);
Evan Laird94492852018-10-25 13:43:01 -0400133 mEntry.setRow(mRow);
Kevind4f66a42018-08-03 13:12:51 -0700134
135 mAlertingNotificationManager = createAlertingNotificationManager();
136 }
137
138 @Test
139 public void testShowNotification_addsEntry() {
140 mAlertingNotificationManager.showNotification(mEntry);
141
Kevina97ea052018-09-11 13:53:18 -0700142 assertTrue(mAlertingNotificationManager.isAlerting(mEntry.key));
Kevind4f66a42018-08-03 13:12:51 -0700143 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);
Kevina97ea052018-09-11 13:53:18 -0700156 assertFalse(mAlertingNotificationManager.isAlerting(mEntry.key));
Kevind4f66a42018-08-03 13:12:51 -0700157 }
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
Kevina97ea052018-09-11 13:53:18 -0700166 assertTrue(mAlertingNotificationManager.isAlerting(mEntry.key));
Kevind4f66a42018-08-03 13:12:51 -0700167 }
168
169 @Test
170 public void testRemoveNotification_forceRemove() {
171 mAlertingNotificationManager.showNotification(mEntry);
172
Kevina5ff1fa2018-08-21 16:35:48 -0700173 // Remove forcibly with releaseImmediately = true.
Kevind4f66a42018-08-03 13:12:51 -0700174 mAlertingNotificationManager.removeNotification(mEntry.key, true /* releaseImmediately */);
175
Kevina97ea052018-09-11 13:53:18 -0700176 assertFalse(mAlertingNotificationManager.isAlerting(mEntry.key));
Kevind4f66a42018-08-03 13:12:51 -0700177 }
178
179 @Test
180 public void testReleaseAllImmediately() {
181 for (int i = 0; i < TEST_NUM_NOTIFICATIONS; i++) {
182 StatusBarNotification sbn = createNewNotification(i);
Ned Burnsf81c4c42019-01-07 14:10:43 -0500183 NotificationEntry entry = new NotificationEntry(sbn);
Evan Laird94492852018-10-25 13:43:01 -0400184 entry.setRow(mRow);
Kevind4f66a42018-08-03 13:12:51 -0700185 mAlertingNotificationManager.showNotification(entry);
186 }
187
188 mAlertingNotificationManager.releaseAllImmediately();
189
190 assertEquals(0, mAlertingNotificationManager.getAllEntries().count());
191 }
Kevina5ff1fa2018-08-21 16:35:48 -0700192
193 @Test
Kevina97ea052018-09-11 13:53:18 -0700194 public void testCanRemoveImmediately_notShownLongEnough() {
Kevina5ff1fa2018-08-21 16:35:48 -0700195 mAlertingNotificationManager.showNotification(mEntry);
196
Kevina97ea052018-09-11 13:53:18 -0700197 // 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.
Kevina5ff1fa2018-08-21 16:35:48 -0700206 assertTrue(mAlertingNotificationManager.shouldExtendLifetime(mEntry));
207 }
208
209 @Test
Kevine9e938c2018-09-06 13:38:11 -0700210 public void testSetShouldManageLifetime_setShouldManage() {
Kevina5ff1fa2018-08-21 16:35:48 -0700211 mAlertingNotificationManager.showNotification(mEntry);
212
Kevine9e938c2018-09-06 13:38:11 -0700213 mAlertingNotificationManager.setShouldManageLifetime(mEntry, true /* shouldManage */);
Kevina5ff1fa2018-08-21 16:35:48 -0700214
215 assertTrue(mAlertingNotificationManager.mExtendedLifetimeAlertEntries.contains(mEntry));
216 }
217
218 @Test
Selim Cinekbd559092019-01-28 19:38:58 -0800219 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
Kevine9e938c2018-09-06 13:38:11 -0700230 public void testSetShouldManageLifetime_setShouldNotManage() {
Kevina5ff1fa2018-08-21 16:35:48 -0700231 mAlertingNotificationManager.mExtendedLifetimeAlertEntries.add(mEntry);
232
Kevine9e938c2018-09-06 13:38:11 -0700233 mAlertingNotificationManager.setShouldManageLifetime(mEntry, false /* shouldManage */);
Kevina5ff1fa2018-08-21 16:35:48 -0700234
235 assertFalse(mAlertingNotificationManager.mExtendedLifetimeAlertEntries.contains(mEntry));
236 }
Kevind4f66a42018-08-03 13:12:51 -0700237}