blob: 8b41516044f2db5b26b6ea9805e8edc0110feb98 [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
20import static junit.framework.Assert.assertFalse;
21import static junit.framework.Assert.assertTrue;
22
23import static org.junit.Assert.assertEquals;
24
25import android.app.ActivityManager;
26import android.app.Notification;
27import android.os.Handler;
28import android.os.Looper;
29import android.os.UserHandle;
30import android.service.notification.StatusBarNotification;
31import android.support.test.filters.SmallTest;
32import android.testing.AndroidTestingRunner;
33import android.testing.TestableLooper;
34
35import com.android.systemui.R;
36import com.android.systemui.SysuiTestCase;
Kevind4f66a42018-08-03 13:12:51 -070037import com.android.systemui.statusbar.notification.NotificationData;
38import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow;
39
40
41import org.junit.Before;
42import org.junit.Rule;
43import org.junit.Test;
44import org.junit.runner.RunWith;
45import org.mockito.Mock;
46import org.mockito.junit.MockitoJUnit;
47import org.mockito.junit.MockitoRule;
48
49@SmallTest
50@RunWith(AndroidTestingRunner.class)
51@TestableLooper.RunWithLooper
52public 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
Kevina97ea052018-09-11 13:53:18 -070059 protected static final int TEST_MINIMUM_DISPLAY_TIME = 200;
60 protected static final int TEST_AUTO_DISMISS_TIME = 500;
Kevind4f66a42018-08-03 13:12:51 -070061 // Number of notifications to use in tests requiring multiple notifications
62 private static final int TEST_NUM_NOTIFICATIONS = 4;
Kevina97ea052018-09-11 13:53:18 -070063 protected static final int TEST_TIMEOUT_TIME = 10000;
64 protected final Runnable TEST_TIMEOUT_RUNNABLE = () -> mTimedOut = true;
Kevind4f66a42018-08-03 13:12:51 -070065
66 private AlertingNotificationManager mAlertingNotificationManager;
67
68 protected NotificationData.Entry mEntry;
69 protected Handler mTestHandler;
70 private StatusBarNotification mSbn;
Kevina97ea052018-09-11 13:53:18 -070071 protected boolean mTimedOut = false;
Kevind4f66a42018-08-03 13:12:51 -070072
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
Kevina5ff1fa2018-08-21 16:35:48 -070093 protected StatusBarNotification createNewNotification(int id) {
Kevind4f66a42018-08-03 13:12:51 -070094 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
Kevina97ea052018-09-11 13:53:18 -0700125 assertTrue(mAlertingNotificationManager.isAlerting(mEntry.key));
Kevind4f66a42018-08-03 13:12:51 -0700126 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);
Kevina97ea052018-09-11 13:53:18 -0700139 assertFalse(mAlertingNotificationManager.isAlerting(mEntry.key));
Kevind4f66a42018-08-03 13:12:51 -0700140 }
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
Kevina97ea052018-09-11 13:53:18 -0700149 assertTrue(mAlertingNotificationManager.isAlerting(mEntry.key));
Kevind4f66a42018-08-03 13:12:51 -0700150 }
151
152 @Test
153 public void testRemoveNotification_forceRemove() {
154 mAlertingNotificationManager.showNotification(mEntry);
155
Kevina5ff1fa2018-08-21 16:35:48 -0700156 // Remove forcibly with releaseImmediately = true.
Kevind4f66a42018-08-03 13:12:51 -0700157 mAlertingNotificationManager.removeNotification(mEntry.key, true /* releaseImmediately */);
158
Kevina97ea052018-09-11 13:53:18 -0700159 assertFalse(mAlertingNotificationManager.isAlerting(mEntry.key));
Kevind4f66a42018-08-03 13:12:51 -0700160 }
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 }
Kevina5ff1fa2018-08-21 16:35:48 -0700175
176 @Test
Kevina97ea052018-09-11 13:53:18 -0700177 public void testCanRemoveImmediately_notShownLongEnough() {
Kevina5ff1fa2018-08-21 16:35:48 -0700178 mAlertingNotificationManager.showNotification(mEntry);
179
Kevina97ea052018-09-11 13:53:18 -0700180 // The entry has just been added so we should not remove immediately.
181 assertFalse(mAlertingNotificationManager.canRemoveImmediately(mEntry.key));
182 }
183
184 @Test
185 public void testShouldExtendLifetime() {
186 mAlertingNotificationManager.showNotification(mEntry);
187
188 // While the entry is alerting, it should not be removable.
Kevina5ff1fa2018-08-21 16:35:48 -0700189 assertTrue(mAlertingNotificationManager.shouldExtendLifetime(mEntry));
190 }
191
192 @Test
Kevine9e938c2018-09-06 13:38:11 -0700193 public void testSetShouldManageLifetime_setShouldManage() {
Kevina5ff1fa2018-08-21 16:35:48 -0700194 mAlertingNotificationManager.showNotification(mEntry);
195
Kevine9e938c2018-09-06 13:38:11 -0700196 mAlertingNotificationManager.setShouldManageLifetime(mEntry, true /* shouldManage */);
Kevina5ff1fa2018-08-21 16:35:48 -0700197
198 assertTrue(mAlertingNotificationManager.mExtendedLifetimeAlertEntries.contains(mEntry));
199 }
200
201 @Test
Kevine9e938c2018-09-06 13:38:11 -0700202 public void testSetShouldManageLifetime_setShouldNotManage() {
Kevina5ff1fa2018-08-21 16:35:48 -0700203 mAlertingNotificationManager.mExtendedLifetimeAlertEntries.add(mEntry);
204
Kevine9e938c2018-09-06 13:38:11 -0700205 mAlertingNotificationManager.setShouldManageLifetime(mEntry, false /* shouldManage */);
Kevina5ff1fa2018-08-21 16:35:48 -0700206
207 assertFalse(mAlertingNotificationManager.mExtendedLifetimeAlertEntries.contains(mEntry));
208 }
Kevind4f66a42018-08-03 13:12:51 -0700209}