blob: f21ce2780d40a1d8365f16fcba2bb0197006e79a [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
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
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
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
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
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 }
Kevina5ff1fa2018-08-21 16:35:48 -0700175
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
Kevine9e938c2018-09-06 13:38:11 -0700185 public void testSetShouldManageLifetime_setShouldManage() {
Kevina5ff1fa2018-08-21 16:35:48 -0700186 mAlertingNotificationManager.showNotification(mEntry);
187
Kevine9e938c2018-09-06 13:38:11 -0700188 mAlertingNotificationManager.setShouldManageLifetime(mEntry, true /* shouldManage */);
Kevina5ff1fa2018-08-21 16:35:48 -0700189
190 assertTrue(mAlertingNotificationManager.mExtendedLifetimeAlertEntries.contains(mEntry));
191 }
192
193 @Test
Kevine9e938c2018-09-06 13:38:11 -0700194 public void testSetShouldManageLifetime_setShouldNotManage() {
Kevina5ff1fa2018-08-21 16:35:48 -0700195 mAlertingNotificationManager.mExtendedLifetimeAlertEntries.add(mEntry);
196
Kevine9e938c2018-09-06 13:38:11 -0700197 mAlertingNotificationManager.setShouldManageLifetime(mEntry, false /* shouldManage */);
Kevina5ff1fa2018-08-21 16:35:48 -0700198
199 assertFalse(mAlertingNotificationManager.mExtendedLifetimeAlertEntries.contains(mEntry));
200 }
Kevind4f66a42018-08-03 13:12:51 -0700201}