blob: f04a1151384ef6c64a7a60c50eb31f90ac2471cd [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;
37import com.android.systemui.statusbar.AlertingNotificationManager;
38import com.android.systemui.statusbar.notification.NotificationData;
39import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow;
40
41
42import org.junit.Before;
43import org.junit.Rule;
44import org.junit.Test;
45import org.junit.runner.RunWith;
46import org.mockito.Mock;
47import org.mockito.junit.MockitoJUnit;
48import org.mockito.junit.MockitoRule;
49
50@SmallTest
51@RunWith(AndroidTestingRunner.class)
52@TestableLooper.RunWithLooper
53public class AlertingNotificationManagerTest extends SysuiTestCase {
54 @Rule
55 public MockitoRule rule = MockitoJUnit.rule();
56
57 private static final String TEST_PACKAGE_NAME = "test";
58 private static final int TEST_UID = 0;
59
60 private static final int TEST_MINIMUM_DISPLAY_TIME = 200;
61 private static final int TEST_AUTO_DISMISS_TIME = 500;
62 // Number of notifications to use in tests requiring multiple notifications
63 private static final int TEST_NUM_NOTIFICATIONS = 4;
64 private static final int TEST_TIMEOUT_TIME = 10000;
65 private final Runnable TEST_TIMEOUT_RUNNABLE = () -> mTimedOut = true;
66
67 private AlertingNotificationManager mAlertingNotificationManager;
68
69 protected NotificationData.Entry mEntry;
70 protected Handler mTestHandler;
71 private StatusBarNotification mSbn;
72 private boolean mTimedOut = false;
73
74 @Mock protected ExpandableNotificationRow mRow;
75
76 private final class TestableAlertingNotificationManager extends AlertingNotificationManager {
77 private TestableAlertingNotificationManager() {
78 mMinimumDisplayTime = TEST_MINIMUM_DISPLAY_TIME;
79 mAutoDismissNotificationDecay = TEST_AUTO_DISMISS_TIME;
80 mHandler = mTestHandler;
81 }
82
83 @Override
84 protected void onAlertEntryAdded(AlertEntry alertEntry) {}
85
86 @Override
87 protected void onAlertEntryRemoved(AlertEntry alertEntry) {}
88 }
89
90 protected AlertingNotificationManager createAlertingNotificationManager() {
91 return new TestableAlertingNotificationManager();
92 }
93
94 private StatusBarNotification createNewNotification(int id) {
95 Notification.Builder n = new Notification.Builder(mContext, "")
96 .setSmallIcon(R.drawable.ic_person)
97 .setContentTitle("Title")
98 .setContentText("Text");
99 return new StatusBarNotification(
100 TEST_PACKAGE_NAME /* pkg */,
101 TEST_PACKAGE_NAME,
102 id,
103 null /* tag */,
104 TEST_UID,
105 0 /* initialPid */,
106 n.build(),
107 new UserHandle(ActivityManager.getCurrentUser()),
108 null /* overrideGroupKey */,
109 0 /* postTime */);
110 }
111
112 @Before
113 public void setUp() {
114 mTestHandler = Handler.createAsync(Looper.myLooper());
115 mSbn = createNewNotification(0 /* id */);
116 mEntry = new NotificationData.Entry(mSbn);
117 mEntry.row = mRow;
118
119 mAlertingNotificationManager = createAlertingNotificationManager();
120 }
121
122 @Test
123 public void testShowNotification_addsEntry() {
124 mAlertingNotificationManager.showNotification(mEntry);
125
126 assertTrue(mAlertingNotificationManager.contains(mEntry.key));
127 assertTrue(mAlertingNotificationManager.hasNotifications());
128 assertEquals(mEntry, mAlertingNotificationManager.getEntry(mEntry.key));
129 }
130
131 @Test
132 public void testShowNotification_autoDismisses() {
133 mAlertingNotificationManager.showNotification(mEntry);
134 mTestHandler.postDelayed(TEST_TIMEOUT_RUNNABLE, TEST_TIMEOUT_TIME);
135
136 // Wait for remove runnable and then process it immediately
137 TestableLooper.get(this).processMessages(1);
138
139 assertFalse("Test timed out", mTimedOut);
140 assertFalse(mAlertingNotificationManager.contains(mEntry.key));
141 }
142
143 @Test
144 public void testRemoveNotification_removeDeferred() {
145 mAlertingNotificationManager.showNotification(mEntry);
146
147 // Try to remove but defer, since the notification has not been shown long enough.
148 mAlertingNotificationManager.removeNotification(mEntry.key, false /* releaseImmediately */);
149
150 assertTrue(mAlertingNotificationManager.contains(mEntry.key));
151 }
152
153 @Test
154 public void testRemoveNotification_forceRemove() {
155 mAlertingNotificationManager.showNotification(mEntry);
156
157 //Remove forcibly with releaseImmediately = true.
158 mAlertingNotificationManager.removeNotification(mEntry.key, true /* releaseImmediately */);
159
160 assertFalse(mAlertingNotificationManager.contains(mEntry.key));
161 }
162
163 @Test
164 public void testReleaseAllImmediately() {
165 for (int i = 0; i < TEST_NUM_NOTIFICATIONS; i++) {
166 StatusBarNotification sbn = createNewNotification(i);
167 NotificationData.Entry entry = new NotificationData.Entry(sbn);
168 entry.row = mRow;
169 mAlertingNotificationManager.showNotification(entry);
170 }
171
172 mAlertingNotificationManager.releaseAllImmediately();
173
174 assertEquals(0, mAlertingNotificationManager.getAllEntries().count());
175 }
176}