blob: b1c3c83e938e0d1fb912a2f8d74b7f2b143ed583 [file] [log] [blame]
Kevin01a53cb2018-11-09 18:19:54 -08001/*
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
17package com.android.systemui.statusbar.phone;
18
19import static org.junit.Assert.assertFalse;
20import static org.junit.Assert.assertTrue;
Kevin4b8bbda2018-11-19 14:36:31 -080021import static org.mockito.Mockito.doReturn;
Selim Cinekc3fec682019-06-06 18:11:07 -070022import static org.mockito.Mockito.mock;
Kevin4b8bbda2018-11-19 14:36:31 -080023import static org.mockito.Mockito.spy;
Kevin01a53cb2018-11-09 18:19:54 -080024import static org.mockito.Mockito.times;
25import static org.mockito.Mockito.verify;
26import static org.mockito.Mockito.when;
27
28import android.app.Notification;
Kevin4b8bbda2018-11-19 14:36:31 -080029import android.service.notification.StatusBarNotification;
Kevin01a53cb2018-11-09 18:19:54 -080030import android.testing.AndroidTestingRunner;
31import android.testing.TestableLooper;
32
Brett Chabot84151d92019-02-27 15:37:59 -080033import androidx.test.filters.SmallTest;
34
Kevin01a53cb2018-11-09 18:19:54 -080035import com.android.systemui.SysuiTestCase;
Selim Cinekc3fec682019-06-06 18:11:07 -070036import com.android.systemui.plugins.statusbar.StatusBarStateController;
Gus Prevasd65c2db2018-12-18 17:13:38 -050037import com.android.systemui.statusbar.notification.NotificationEntryListener;
Ned Burns3d6b3962018-12-07 21:26:00 -050038import com.android.systemui.statusbar.notification.NotificationEntryManager;
Ned Burnsf81c4c42019-01-07 14:10:43 -050039import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Kevin01a53cb2018-11-09 18:19:54 -080040import com.android.systemui.statusbar.policy.HeadsUpManager;
41
42import org.junit.Before;
43import org.junit.Rule;
44import org.junit.Test;
45import org.junit.runner.RunWith;
Ned Burns3d6b3962018-12-07 21:26:00 -050046import org.mockito.ArgumentCaptor;
47import org.mockito.Captor;
48import org.mockito.Mock;
Kevin01a53cb2018-11-09 18:19:54 -080049import org.mockito.junit.MockitoJUnit;
50import org.mockito.junit.MockitoRule;
51
52import java.util.HashMap;
53
54@SmallTest
55@RunWith(AndroidTestingRunner.class)
56@TestableLooper.RunWithLooper
57public class NotificationGroupAlertTransferHelperTest extends SysuiTestCase {
Ned Burns3d6b3962018-12-07 21:26:00 -050058 @Rule public MockitoRule rule = MockitoJUnit.rule();
Kevin01a53cb2018-11-09 18:19:54 -080059
60 private NotificationGroupAlertTransferHelper mGroupAlertTransferHelper;
61 private NotificationGroupManager mGroupManager;
Kevin01a53cb2018-11-09 18:19:54 -080062 private HeadsUpManager mHeadsUpManager;
Ned Burns3d6b3962018-12-07 21:26:00 -050063 @Mock private NotificationEntryManager mNotificationEntryManager;
Gus Prevasd65c2db2018-12-18 17:13:38 -050064 @Captor
65 private ArgumentCaptor<NotificationEntryListener> mListenerCaptor;
66 private NotificationEntryListener mNotificationEntryListener;
Ned Burnsf81c4c42019-01-07 14:10:43 -050067 private final HashMap<String, NotificationEntry> mPendingEntries = new HashMap<>();
Kevin01a53cb2018-11-09 18:19:54 -080068 private final NotificationGroupTestHelper mGroupTestHelper =
69 new NotificationGroupTestHelper(mContext);
70
71
72 @Before
73 public void setup() {
Kevin01a53cb2018-11-09 18:19:54 -080074 mHeadsUpManager = new HeadsUpManager(mContext) {};
75
Ned Burns3d6b3962018-12-07 21:26:00 -050076 when(mNotificationEntryManager.getPendingNotificationsIterator())
77 .thenReturn(mPendingEntries.values());
78
Selim Cinekc3fec682019-06-06 18:11:07 -070079 mGroupManager = new NotificationGroupManager(mock(StatusBarStateController.class));
Kevin01a53cb2018-11-09 18:19:54 -080080 mDependency.injectTestDependency(NotificationGroupManager.class, mGroupManager);
81 mGroupManager.setHeadsUpManager(mHeadsUpManager);
82
83 mGroupAlertTransferHelper = new NotificationGroupAlertTransferHelper();
84 mGroupAlertTransferHelper.setHeadsUpManager(mHeadsUpManager);
Kevin01a53cb2018-11-09 18:19:54 -080085
Ned Burns3d6b3962018-12-07 21:26:00 -050086 mGroupAlertTransferHelper.bind(mNotificationEntryManager, mGroupManager);
Gus Prevasd65c2db2018-12-18 17:13:38 -050087 verify(mNotificationEntryManager).addNotificationEntryListener(mListenerCaptor.capture());
88 mNotificationEntryListener = mListenerCaptor.getValue();
Kevin01a53cb2018-11-09 18:19:54 -080089 mHeadsUpManager.addListener(mGroupAlertTransferHelper);
Kevin01a53cb2018-11-09 18:19:54 -080090 }
91
92 @Test
93 public void testSuppressedSummaryHeadsUpTransfersToChild() {
Ned Burnsf81c4c42019-01-07 14:10:43 -050094 NotificationEntry summaryEntry = mGroupTestHelper.createSummaryNotification();
Kevin01a53cb2018-11-09 18:19:54 -080095 mHeadsUpManager.showNotification(summaryEntry);
Ned Burnsf81c4c42019-01-07 14:10:43 -050096 NotificationEntry childEntry = mGroupTestHelper.createChildNotification();
Kevin01a53cb2018-11-09 18:19:54 -080097
98 // Summary will be suppressed because there is only one child.
99 mGroupManager.onEntryAdded(summaryEntry);
100 mGroupManager.onEntryAdded(childEntry);
101
102 // A suppressed summary should transfer its alert state to the child.
103 assertFalse(mHeadsUpManager.isAlerting(summaryEntry.key));
104 assertTrue(mHeadsUpManager.isAlerting(childEntry.key));
105 }
106
107 @Test
108 public void testSuppressedSummaryHeadsUpTransfersToChildButBackAgain() {
Ned Burnsf81c4c42019-01-07 14:10:43 -0500109 NotificationEntry summaryEntry =
Kevin01a53cb2018-11-09 18:19:54 -0800110 mGroupTestHelper.createSummaryNotification(Notification.GROUP_ALERT_SUMMARY);
Ned Burnsf81c4c42019-01-07 14:10:43 -0500111 NotificationEntry childEntry =
Kevin01a53cb2018-11-09 18:19:54 -0800112 mGroupTestHelper.createChildNotification(Notification.GROUP_ALERT_SUMMARY);
Ned Burnsf81c4c42019-01-07 14:10:43 -0500113 NotificationEntry childEntry2 =
Kevin01a53cb2018-11-09 18:19:54 -0800114 mGroupTestHelper.createChildNotification(Notification.GROUP_ALERT_SUMMARY);
115 mHeadsUpManager.showNotification(summaryEntry);
116 // Trigger a transfer of alert state from summary to child.
117 mGroupManager.onEntryAdded(summaryEntry);
118 mGroupManager.onEntryAdded(childEntry);
119
120 // Add second child notification so that summary is no longer suppressed.
121 mPendingEntries.put(childEntry2.key, childEntry2);
Gus Prevasd65c2db2018-12-18 17:13:38 -0500122 mNotificationEntryListener.onPendingEntryAdded(childEntry2);
Kevin01a53cb2018-11-09 18:19:54 -0800123 mGroupManager.onEntryAdded(childEntry2);
124
125 // The alert state should transfer back to the summary as there is now more than one
126 // child and the summary should no longer be suppressed.
127 assertTrue(mHeadsUpManager.isAlerting(summaryEntry.key));
128 assertFalse(mHeadsUpManager.isAlerting(childEntry.key));
129 }
130
131 @Test
132 public void testSuppressedSummaryHeadsUpDoesntTransferBackOnDozingChanged() {
Ned Burnsf81c4c42019-01-07 14:10:43 -0500133 NotificationEntry summaryEntry =
Kevin01a53cb2018-11-09 18:19:54 -0800134 mGroupTestHelper.createSummaryNotification(Notification.GROUP_ALERT_SUMMARY);
Ned Burnsf81c4c42019-01-07 14:10:43 -0500135 NotificationEntry childEntry =
Kevin01a53cb2018-11-09 18:19:54 -0800136 mGroupTestHelper.createChildNotification(Notification.GROUP_ALERT_SUMMARY);
Ned Burnsf81c4c42019-01-07 14:10:43 -0500137 NotificationEntry childEntry2 =
Kevin01a53cb2018-11-09 18:19:54 -0800138 mGroupTestHelper.createChildNotification(Notification.GROUP_ALERT_SUMMARY);
139 mHeadsUpManager.showNotification(summaryEntry);
140 // Trigger a transfer of alert state from summary to child.
141 mGroupManager.onEntryAdded(summaryEntry);
142 mGroupManager.onEntryAdded(childEntry);
143
144 // Set dozing to true.
145 mGroupAlertTransferHelper.onDozingChanged(true);
146
147 // Add second child notification so that summary is no longer suppressed.
148 mPendingEntries.put(childEntry2.key, childEntry2);
Gus Prevasd65c2db2018-12-18 17:13:38 -0500149 mNotificationEntryListener.onPendingEntryAdded(childEntry2);
Kevin01a53cb2018-11-09 18:19:54 -0800150 mGroupManager.onEntryAdded(childEntry2);
151
152 // Dozing changed so no reason to re-alert summary.
153 assertFalse(mHeadsUpManager.isAlerting(summaryEntry.key));
154 }
155
156 @Test
157 public void testSuppressedSummaryHeadsUpTransferDoesNotAlertChildIfUninflated() {
Ned Burnsf81c4c42019-01-07 14:10:43 -0500158 NotificationEntry summaryEntry = mGroupTestHelper.createSummaryNotification();
Kevin01a53cb2018-11-09 18:19:54 -0800159 mHeadsUpManager.showNotification(summaryEntry);
Ned Burnsf81c4c42019-01-07 14:10:43 -0500160 NotificationEntry childEntry = mGroupTestHelper.createChildNotification();
Evan Laird94492852018-10-25 13:43:01 -0400161 when(childEntry.getRow().isInflationFlagSet(mHeadsUpManager.getContentFlag()))
162 .thenReturn(false);
Kevin01a53cb2018-11-09 18:19:54 -0800163
164 mGroupManager.onEntryAdded(summaryEntry);
165 mGroupManager.onEntryAdded(childEntry);
166
167 // Alert is immediately removed from summary, but we do not show child yet either as its
168 // content is not inflated.
169 assertFalse(mHeadsUpManager.isAlerting(summaryEntry.key));
170 assertFalse(mHeadsUpManager.isAlerting(childEntry.key));
Kevin4b8bbda2018-11-19 14:36:31 -0800171 assertTrue(mGroupAlertTransferHelper.isAlertTransferPending(childEntry));
Kevin01a53cb2018-11-09 18:19:54 -0800172 }
173
174 @Test
175 public void testSuppressedSummaryHeadsUpTransferAlertsChildOnInflation() {
Ned Burnsf81c4c42019-01-07 14:10:43 -0500176 NotificationEntry summaryEntry = mGroupTestHelper.createSummaryNotification();
Kevin01a53cb2018-11-09 18:19:54 -0800177 mHeadsUpManager.showNotification(summaryEntry);
Ned Burnsf81c4c42019-01-07 14:10:43 -0500178 NotificationEntry childEntry = mGroupTestHelper.createChildNotification();
Evan Laird94492852018-10-25 13:43:01 -0400179 when(childEntry.getRow().isInflationFlagSet(mHeadsUpManager.getContentFlag()))
180 .thenReturn(false);
Kevin01a53cb2018-11-09 18:19:54 -0800181
182 mGroupManager.onEntryAdded(summaryEntry);
183 mGroupManager.onEntryAdded(childEntry);
184
Evan Laird94492852018-10-25 13:43:01 -0400185 when(childEntry.getRow().isInflationFlagSet(mHeadsUpManager.getContentFlag()))
186 .thenReturn(true);
Gus Prevasd65c2db2018-12-18 17:13:38 -0500187 mNotificationEntryListener.onEntryReinflated(childEntry);
Kevin01a53cb2018-11-09 18:19:54 -0800188
189 // Alert is immediately removed from summary, and we show child as its content is inflated.
190 assertFalse(mHeadsUpManager.isAlerting(summaryEntry.key));
191 assertTrue(mHeadsUpManager.isAlerting(childEntry.key));
192 }
193
194 @Test
195 public void testSuppressedSummaryHeadsUpTransferBackAbortsChildInflation() {
Ned Burnsf81c4c42019-01-07 14:10:43 -0500196 NotificationEntry summaryEntry =
Kevin01a53cb2018-11-09 18:19:54 -0800197 mGroupTestHelper.createSummaryNotification(Notification.GROUP_ALERT_SUMMARY);
Ned Burnsf81c4c42019-01-07 14:10:43 -0500198 NotificationEntry childEntry =
Kevin01a53cb2018-11-09 18:19:54 -0800199 mGroupTestHelper.createChildNotification(Notification.GROUP_ALERT_SUMMARY);
Evan Laird94492852018-10-25 13:43:01 -0400200 when(childEntry.getRow().isInflationFlagSet(mHeadsUpManager.getContentFlag()))
201 .thenReturn(false);
Ned Burnsf81c4c42019-01-07 14:10:43 -0500202 NotificationEntry childEntry2 =
Kevin01a53cb2018-11-09 18:19:54 -0800203 mGroupTestHelper.createChildNotification(Notification.GROUP_ALERT_SUMMARY);
204 mHeadsUpManager.showNotification(summaryEntry);
205 // Trigger a transfer of alert state from summary to child.
206 mGroupManager.onEntryAdded(summaryEntry);
207 mGroupManager.onEntryAdded(childEntry);
208
209 // Add second child notification so that summary is no longer suppressed.
210 mPendingEntries.put(childEntry2.key, childEntry2);
Gus Prevasd65c2db2018-12-18 17:13:38 -0500211 mNotificationEntryListener.onPendingEntryAdded(childEntry2);
Kevin01a53cb2018-11-09 18:19:54 -0800212 mGroupManager.onEntryAdded(childEntry2);
213
214 // Child entry finishes its inflation.
Evan Laird94492852018-10-25 13:43:01 -0400215 when(childEntry.getRow().isInflationFlagSet(mHeadsUpManager.getContentFlag()))
216 .thenReturn(true);
Gus Prevasd65c2db2018-12-18 17:13:38 -0500217 mNotificationEntryListener.onEntryReinflated(childEntry);
Kevin01a53cb2018-11-09 18:19:54 -0800218
Evan Laird94492852018-10-25 13:43:01 -0400219 verify(childEntry.getRow(), times(1)).freeContentViewWhenSafe(mHeadsUpManager
220 .getContentFlag());
Kevin01a53cb2018-11-09 18:19:54 -0800221 assertFalse(mHeadsUpManager.isAlerting(childEntry.key));
222 }
Kevin4b8bbda2018-11-19 14:36:31 -0800223
224 @Test
225 public void testCleanUpPendingAlertInfo() {
Ned Burnsf81c4c42019-01-07 14:10:43 -0500226 NotificationEntry summaryEntry =
Kevin4b8bbda2018-11-19 14:36:31 -0800227 mGroupTestHelper.createSummaryNotification(Notification.GROUP_ALERT_SUMMARY);
Ned Burnsf81c4c42019-01-07 14:10:43 -0500228 NotificationEntry childEntry =
Kevin4b8bbda2018-11-19 14:36:31 -0800229 mGroupTestHelper.createChildNotification(Notification.GROUP_ALERT_SUMMARY);
Evan Laird94492852018-10-25 13:43:01 -0400230 when(childEntry.getRow().isInflationFlagSet(mHeadsUpManager.getContentFlag()))
231 .thenReturn(false);
Kevin4b8bbda2018-11-19 14:36:31 -0800232 mHeadsUpManager.showNotification(summaryEntry);
233 // Trigger a transfer of alert state from summary to child.
234 mGroupManager.onEntryAdded(summaryEntry);
235 mGroupManager.onEntryAdded(childEntry);
236
Ned Burnsef2ef6c2019-01-02 16:48:08 -0500237 mNotificationEntryListener.onEntryRemoved(childEntry, null, false);
Kevin4b8bbda2018-11-19 14:36:31 -0800238
239 assertFalse(mGroupAlertTransferHelper.isAlertTransferPending(childEntry));
240 }
241
242 @Test
243 public void testUpdateGroupChangeDoesNotTransfer() {
Ned Burnsf81c4c42019-01-07 14:10:43 -0500244 NotificationEntry summaryEntry =
Kevin4b8bbda2018-11-19 14:36:31 -0800245 mGroupTestHelper.createSummaryNotification(Notification.GROUP_ALERT_SUMMARY);
Ned Burnsf81c4c42019-01-07 14:10:43 -0500246 NotificationEntry childEntry =
Kevin4b8bbda2018-11-19 14:36:31 -0800247 mGroupTestHelper.createChildNotification(Notification.GROUP_ALERT_SUMMARY);
Evan Laird94492852018-10-25 13:43:01 -0400248 when(childEntry.getRow().isInflationFlagSet(mHeadsUpManager.getContentFlag()))
249 .thenReturn(false);
Kevin4b8bbda2018-11-19 14:36:31 -0800250 mHeadsUpManager.showNotification(summaryEntry);
251 // Trigger a transfer of alert state from summary to child.
252 mGroupManager.onEntryAdded(summaryEntry);
253 mGroupManager.onEntryAdded(childEntry);
254
255 // Notify that entry changed groups.
256 StatusBarNotification oldNotification = childEntry.notification;
257 StatusBarNotification newSbn = spy(childEntry.notification.clone());
258 doReturn("other_group").when(newSbn).getGroupKey();
259 childEntry.notification = newSbn;
260 mGroupManager.onEntryUpdated(childEntry, oldNotification);
261
262 assertFalse(mGroupAlertTransferHelper.isAlertTransferPending(childEntry));
263 }
264
265 @Test
266 public void testUpdateChildToSummaryDoesNotTransfer() {
Ned Burnsf81c4c42019-01-07 14:10:43 -0500267 NotificationEntry summaryEntry =
Kevin4b8bbda2018-11-19 14:36:31 -0800268 mGroupTestHelper.createSummaryNotification(Notification.GROUP_ALERT_SUMMARY);
Ned Burnsf81c4c42019-01-07 14:10:43 -0500269 NotificationEntry childEntry =
Kevin4b8bbda2018-11-19 14:36:31 -0800270 mGroupTestHelper.createChildNotification(Notification.GROUP_ALERT_SUMMARY);
Evan Laird94492852018-10-25 13:43:01 -0400271 when(childEntry.getRow().isInflationFlagSet(mHeadsUpManager.getContentFlag()))
272 .thenReturn(false);
Kevin4b8bbda2018-11-19 14:36:31 -0800273 mHeadsUpManager.showNotification(summaryEntry);
274 // Trigger a transfer of alert state from summary to child.
275 mGroupManager.onEntryAdded(summaryEntry);
276 mGroupManager.onEntryAdded(childEntry);
277
278 // Update that child to a summary.
279 StatusBarNotification oldNotification = childEntry.notification;
280 childEntry.notification = mGroupTestHelper.createSummaryNotification(
281 Notification.GROUP_ALERT_SUMMARY).notification;
282 mGroupManager.onEntryUpdated(childEntry, oldNotification);
283
284 assertFalse(mGroupAlertTransferHelper.isAlertTransferPending(childEntry));
285 }
Kevin01a53cb2018-11-09 18:19:54 -0800286}