blob: f49c5b47deda3723eeec4532ed41688d21069a05 [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
Kevin01a53cb2018-11-09 18:19:54 -080020import static com.android.systemui.statusbar.notification.row.NotificationInflater.FLAG_CONTENT_VIEW_CONTRACTED;
21
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;
26
27import android.app.ActivityManager;
28import android.app.Notification;
29import android.os.Handler;
30import android.os.Looper;
31import android.os.UserHandle;
32import android.service.notification.StatusBarNotification;
33import android.support.test.filters.SmallTest;
34import android.testing.AndroidTestingRunner;
35import android.testing.TestableLooper;
36
37import com.android.systemui.R;
38import com.android.systemui.SysuiTestCase;
Kevind4f66a42018-08-03 13:12:51 -070039import com.android.systemui.statusbar.notification.NotificationData;
40import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow;
41
42
43import org.junit.Before;
44import org.junit.Rule;
45import org.junit.Test;
46import org.junit.runner.RunWith;
47import org.mockito.Mock;
48import org.mockito.junit.MockitoJUnit;
49import org.mockito.junit.MockitoRule;
50
51@SmallTest
52@RunWith(AndroidTestingRunner.class)
53@TestableLooper.RunWithLooper
54public 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
Kevina97ea052018-09-11 13:53:18 -070061 protected static final int TEST_MINIMUM_DISPLAY_TIME = 200;
62 protected static final int TEST_AUTO_DISMISS_TIME = 500;
Kevind4f66a42018-08-03 13:12:51 -070063 // Number of notifications to use in tests requiring multiple notifications
64 private static final int TEST_NUM_NOTIFICATIONS = 4;
Kevina97ea052018-09-11 13:53:18 -070065 protected static final int TEST_TIMEOUT_TIME = 10000;
66 protected final Runnable TEST_TIMEOUT_RUNNABLE = () -> mTimedOut = true;
Kevind4f66a42018-08-03 13:12:51 -070067
68 private AlertingNotificationManager mAlertingNotificationManager;
69
70 protected NotificationData.Entry mEntry;
71 protected Handler mTestHandler;
72 private StatusBarNotification mSbn;
Kevina97ea052018-09-11 13:53:18 -070073 protected boolean mTimedOut = false;
Kevind4f66a42018-08-03 13:12:51 -070074
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) {}
Kevin01a53cb2018-11-09 18:19:54 -080089
90 @Override
91 public int getContentFlag() {
92 return FLAG_CONTENT_VIEW_CONTRACTED;
93 }
Kevind4f66a42018-08-03 13:12:51 -070094 }
95
96 protected AlertingNotificationManager createAlertingNotificationManager() {
97 return new TestableAlertingNotificationManager();
98 }
99
Kevina5ff1fa2018-08-21 16:35:48 -0700100 protected StatusBarNotification createNewNotification(int id) {
Kevind4f66a42018-08-03 13:12:51 -0700101 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
Kevina97ea052018-09-11 13:53:18 -0700132 assertTrue(mAlertingNotificationManager.isAlerting(mEntry.key));
Kevind4f66a42018-08-03 13:12:51 -0700133 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);
Kevina97ea052018-09-11 13:53:18 -0700146 assertFalse(mAlertingNotificationManager.isAlerting(mEntry.key));
Kevind4f66a42018-08-03 13:12:51 -0700147 }
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
Kevina97ea052018-09-11 13:53:18 -0700156 assertTrue(mAlertingNotificationManager.isAlerting(mEntry.key));
Kevind4f66a42018-08-03 13:12:51 -0700157 }
158
159 @Test
160 public void testRemoveNotification_forceRemove() {
161 mAlertingNotificationManager.showNotification(mEntry);
162
Kevina5ff1fa2018-08-21 16:35:48 -0700163 // Remove forcibly with releaseImmediately = true.
Kevind4f66a42018-08-03 13:12:51 -0700164 mAlertingNotificationManager.removeNotification(mEntry.key, true /* releaseImmediately */);
165
Kevina97ea052018-09-11 13:53:18 -0700166 assertFalse(mAlertingNotificationManager.isAlerting(mEntry.key));
Kevind4f66a42018-08-03 13:12:51 -0700167 }
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 }
Kevina5ff1fa2018-08-21 16:35:48 -0700182
183 @Test
Kevina97ea052018-09-11 13:53:18 -0700184 public void testCanRemoveImmediately_notShownLongEnough() {
Kevina5ff1fa2018-08-21 16:35:48 -0700185 mAlertingNotificationManager.showNotification(mEntry);
186
Kevina97ea052018-09-11 13:53:18 -0700187 // 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.
Kevina5ff1fa2018-08-21 16:35:48 -0700196 assertTrue(mAlertingNotificationManager.shouldExtendLifetime(mEntry));
197 }
198
199 @Test
Kevine9e938c2018-09-06 13:38:11 -0700200 public void testSetShouldManageLifetime_setShouldManage() {
Kevina5ff1fa2018-08-21 16:35:48 -0700201 mAlertingNotificationManager.showNotification(mEntry);
202
Kevine9e938c2018-09-06 13:38:11 -0700203 mAlertingNotificationManager.setShouldManageLifetime(mEntry, true /* shouldManage */);
Kevina5ff1fa2018-08-21 16:35:48 -0700204
205 assertTrue(mAlertingNotificationManager.mExtendedLifetimeAlertEntries.contains(mEntry));
206 }
207
208 @Test
Kevine9e938c2018-09-06 13:38:11 -0700209 public void testSetShouldManageLifetime_setShouldNotManage() {
Kevina5ff1fa2018-08-21 16:35:48 -0700210 mAlertingNotificationManager.mExtendedLifetimeAlertEntries.add(mEntry);
211
Kevine9e938c2018-09-06 13:38:11 -0700212 mAlertingNotificationManager.setShouldManageLifetime(mEntry, false /* shouldManage */);
Kevina5ff1fa2018-08-21 16:35:48 -0700213
214 assertFalse(mAlertingNotificationManager.mExtendedLifetimeAlertEntries.contains(mEntry));
215 }
Kevind4f66a42018-08-03 13:12:51 -0700216}