blob: 337c35c39ca1a8d4078d2494e9f0f1e98af312c3 [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;
22import static org.mockito.Mockito.spy;
Kevin01a53cb2018-11-09 18:19:54 -080023import static org.mockito.Mockito.times;
24import static org.mockito.Mockito.verify;
25import static org.mockito.Mockito.when;
26
27import android.app.Notification;
Kevin4b8bbda2018-11-19 14:36:31 -080028import android.service.notification.StatusBarNotification;
Kevin01a53cb2018-11-09 18:19:54 -080029import android.testing.AndroidTestingRunner;
30import android.testing.TestableLooper;
31
Brett Chabot84151d92019-02-27 15:37:59 -080032import androidx.test.filters.SmallTest;
33
Kevin01a53cb2018-11-09 18:19:54 -080034import com.android.systemui.SysuiTestCase;
35import com.android.systemui.statusbar.AmbientPulseManager;
Gus Prevasd65c2db2018-12-18 17:13:38 -050036import com.android.systemui.statusbar.notification.NotificationEntryListener;
Ned Burns3d6b3962018-12-07 21:26:00 -050037import com.android.systemui.statusbar.notification.NotificationEntryManager;
Ned Burnsf81c4c42019-01-07 14:10:43 -050038import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Kevin01a53cb2018-11-09 18:19:54 -080039import com.android.systemui.statusbar.policy.HeadsUpManager;
40
41import org.junit.Before;
42import org.junit.Rule;
43import org.junit.Test;
44import org.junit.runner.RunWith;
Ned Burns3d6b3962018-12-07 21:26:00 -050045import org.mockito.ArgumentCaptor;
46import org.mockito.Captor;
47import org.mockito.Mock;
Kevin01a53cb2018-11-09 18:19:54 -080048import org.mockito.junit.MockitoJUnit;
49import org.mockito.junit.MockitoRule;
50
51import java.util.HashMap;
52
53@SmallTest
54@RunWith(AndroidTestingRunner.class)
55@TestableLooper.RunWithLooper
56public class NotificationGroupAlertTransferHelperTest extends SysuiTestCase {
Ned Burns3d6b3962018-12-07 21:26:00 -050057 @Rule public MockitoRule rule = MockitoJUnit.rule();
Kevin01a53cb2018-11-09 18:19:54 -080058
59 private NotificationGroupAlertTransferHelper mGroupAlertTransferHelper;
60 private NotificationGroupManager mGroupManager;
61 private AmbientPulseManager mAmbientPulseManager;
62 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() {
74 mAmbientPulseManager = new AmbientPulseManager(mContext);
75 mDependency.injectTestDependency(AmbientPulseManager.class, mAmbientPulseManager);
76 mHeadsUpManager = new HeadsUpManager(mContext) {};
77
Ned Burns3d6b3962018-12-07 21:26:00 -050078 when(mNotificationEntryManager.getPendingNotificationsIterator())
79 .thenReturn(mPendingEntries.values());
80
Kevin01a53cb2018-11-09 18:19:54 -080081 mGroupManager = new NotificationGroupManager();
82 mDependency.injectTestDependency(NotificationGroupManager.class, mGroupManager);
83 mGroupManager.setHeadsUpManager(mHeadsUpManager);
84
85 mGroupAlertTransferHelper = new NotificationGroupAlertTransferHelper();
86 mGroupAlertTransferHelper.setHeadsUpManager(mHeadsUpManager);
Kevin01a53cb2018-11-09 18:19:54 -080087
Ned Burns3d6b3962018-12-07 21:26:00 -050088 mGroupAlertTransferHelper.bind(mNotificationEntryManager, mGroupManager);
Gus Prevasd65c2db2018-12-18 17:13:38 -050089 verify(mNotificationEntryManager).addNotificationEntryListener(mListenerCaptor.capture());
90 mNotificationEntryListener = mListenerCaptor.getValue();
Kevin01a53cb2018-11-09 18:19:54 -080091 mHeadsUpManager.addListener(mGroupAlertTransferHelper);
92 mAmbientPulseManager.addListener(mGroupAlertTransferHelper);
93 }
94
95 @Test
96 public void testSuppressedSummaryHeadsUpTransfersToChild() {
Ned Burnsf81c4c42019-01-07 14:10:43 -050097 NotificationEntry summaryEntry = mGroupTestHelper.createSummaryNotification();
Kevin01a53cb2018-11-09 18:19:54 -080098 mHeadsUpManager.showNotification(summaryEntry);
Ned Burnsf81c4c42019-01-07 14:10:43 -050099 NotificationEntry childEntry = mGroupTestHelper.createChildNotification();
Kevin01a53cb2018-11-09 18:19:54 -0800100
101 // Summary will be suppressed because there is only one child.
102 mGroupManager.onEntryAdded(summaryEntry);
103 mGroupManager.onEntryAdded(childEntry);
104
105 // A suppressed summary should transfer its alert state to the child.
106 assertFalse(mHeadsUpManager.isAlerting(summaryEntry.key));
107 assertTrue(mHeadsUpManager.isAlerting(childEntry.key));
108 }
109
110 @Test
111 public void testSuppressedSummaryHeadsUpTransfersToChildButBackAgain() {
Ned Burnsf81c4c42019-01-07 14:10:43 -0500112 NotificationEntry summaryEntry =
Kevin01a53cb2018-11-09 18:19:54 -0800113 mGroupTestHelper.createSummaryNotification(Notification.GROUP_ALERT_SUMMARY);
Ned Burnsf81c4c42019-01-07 14:10:43 -0500114 NotificationEntry childEntry =
Kevin01a53cb2018-11-09 18:19:54 -0800115 mGroupTestHelper.createChildNotification(Notification.GROUP_ALERT_SUMMARY);
Ned Burnsf81c4c42019-01-07 14:10:43 -0500116 NotificationEntry childEntry2 =
Kevin01a53cb2018-11-09 18:19:54 -0800117 mGroupTestHelper.createChildNotification(Notification.GROUP_ALERT_SUMMARY);
118 mHeadsUpManager.showNotification(summaryEntry);
119 // Trigger a transfer of alert state from summary to child.
120 mGroupManager.onEntryAdded(summaryEntry);
121 mGroupManager.onEntryAdded(childEntry);
122
123 // Add second child notification so that summary is no longer suppressed.
124 mPendingEntries.put(childEntry2.key, childEntry2);
Gus Prevasd65c2db2018-12-18 17:13:38 -0500125 mNotificationEntryListener.onPendingEntryAdded(childEntry2);
Kevin01a53cb2018-11-09 18:19:54 -0800126 mGroupManager.onEntryAdded(childEntry2);
127
128 // The alert state should transfer back to the summary as there is now more than one
129 // child and the summary should no longer be suppressed.
130 assertTrue(mHeadsUpManager.isAlerting(summaryEntry.key));
131 assertFalse(mHeadsUpManager.isAlerting(childEntry.key));
132 }
133
134 @Test
135 public void testSuppressedSummaryHeadsUpDoesntTransferBackOnDozingChanged() {
Ned Burnsf81c4c42019-01-07 14:10:43 -0500136 NotificationEntry summaryEntry =
Kevin01a53cb2018-11-09 18:19:54 -0800137 mGroupTestHelper.createSummaryNotification(Notification.GROUP_ALERT_SUMMARY);
Ned Burnsf81c4c42019-01-07 14:10:43 -0500138 NotificationEntry childEntry =
Kevin01a53cb2018-11-09 18:19:54 -0800139 mGroupTestHelper.createChildNotification(Notification.GROUP_ALERT_SUMMARY);
Ned Burnsf81c4c42019-01-07 14:10:43 -0500140 NotificationEntry childEntry2 =
Kevin01a53cb2018-11-09 18:19:54 -0800141 mGroupTestHelper.createChildNotification(Notification.GROUP_ALERT_SUMMARY);
142 mHeadsUpManager.showNotification(summaryEntry);
143 // Trigger a transfer of alert state from summary to child.
144 mGroupManager.onEntryAdded(summaryEntry);
145 mGroupManager.onEntryAdded(childEntry);
146
147 // Set dozing to true.
148 mGroupAlertTransferHelper.onDozingChanged(true);
149
150 // Add second child notification so that summary is no longer suppressed.
151 mPendingEntries.put(childEntry2.key, childEntry2);
Gus Prevasd65c2db2018-12-18 17:13:38 -0500152 mNotificationEntryListener.onPendingEntryAdded(childEntry2);
Kevin01a53cb2018-11-09 18:19:54 -0800153 mGroupManager.onEntryAdded(childEntry2);
154
155 // Dozing changed so no reason to re-alert summary.
156 assertFalse(mHeadsUpManager.isAlerting(summaryEntry.key));
157 }
158
159 @Test
160 public void testSuppressedSummaryHeadsUpTransferDoesNotAlertChildIfUninflated() {
Ned Burnsf81c4c42019-01-07 14:10:43 -0500161 NotificationEntry summaryEntry = mGroupTestHelper.createSummaryNotification();
Kevin01a53cb2018-11-09 18:19:54 -0800162 mHeadsUpManager.showNotification(summaryEntry);
Ned Burnsf81c4c42019-01-07 14:10:43 -0500163 NotificationEntry childEntry = mGroupTestHelper.createChildNotification();
Evan Laird94492852018-10-25 13:43:01 -0400164 when(childEntry.getRow().isInflationFlagSet(mHeadsUpManager.getContentFlag()))
165 .thenReturn(false);
Kevin01a53cb2018-11-09 18:19:54 -0800166
167 mGroupManager.onEntryAdded(summaryEntry);
168 mGroupManager.onEntryAdded(childEntry);
169
170 // Alert is immediately removed from summary, but we do not show child yet either as its
171 // content is not inflated.
172 assertFalse(mHeadsUpManager.isAlerting(summaryEntry.key));
173 assertFalse(mHeadsUpManager.isAlerting(childEntry.key));
Kevin4b8bbda2018-11-19 14:36:31 -0800174 assertTrue(mGroupAlertTransferHelper.isAlertTransferPending(childEntry));
Kevin01a53cb2018-11-09 18:19:54 -0800175 }
176
177 @Test
178 public void testSuppressedSummaryHeadsUpTransferAlertsChildOnInflation() {
Ned Burnsf81c4c42019-01-07 14:10:43 -0500179 NotificationEntry summaryEntry = mGroupTestHelper.createSummaryNotification();
Kevin01a53cb2018-11-09 18:19:54 -0800180 mHeadsUpManager.showNotification(summaryEntry);
Ned Burnsf81c4c42019-01-07 14:10:43 -0500181 NotificationEntry childEntry = mGroupTestHelper.createChildNotification();
Evan Laird94492852018-10-25 13:43:01 -0400182 when(childEntry.getRow().isInflationFlagSet(mHeadsUpManager.getContentFlag()))
183 .thenReturn(false);
Kevin01a53cb2018-11-09 18:19:54 -0800184
185 mGroupManager.onEntryAdded(summaryEntry);
186 mGroupManager.onEntryAdded(childEntry);
187
Evan Laird94492852018-10-25 13:43:01 -0400188 when(childEntry.getRow().isInflationFlagSet(mHeadsUpManager.getContentFlag()))
189 .thenReturn(true);
Gus Prevasd65c2db2018-12-18 17:13:38 -0500190 mNotificationEntryListener.onEntryReinflated(childEntry);
Kevin01a53cb2018-11-09 18:19:54 -0800191
192 // Alert is immediately removed from summary, and we show child as its content is inflated.
193 assertFalse(mHeadsUpManager.isAlerting(summaryEntry.key));
194 assertTrue(mHeadsUpManager.isAlerting(childEntry.key));
195 }
196
197 @Test
198 public void testSuppressedSummaryHeadsUpTransferBackAbortsChildInflation() {
Ned Burnsf81c4c42019-01-07 14:10:43 -0500199 NotificationEntry summaryEntry =
Kevin01a53cb2018-11-09 18:19:54 -0800200 mGroupTestHelper.createSummaryNotification(Notification.GROUP_ALERT_SUMMARY);
Ned Burnsf81c4c42019-01-07 14:10:43 -0500201 NotificationEntry childEntry =
Kevin01a53cb2018-11-09 18:19:54 -0800202 mGroupTestHelper.createChildNotification(Notification.GROUP_ALERT_SUMMARY);
Evan Laird94492852018-10-25 13:43:01 -0400203 when(childEntry.getRow().isInflationFlagSet(mHeadsUpManager.getContentFlag()))
204 .thenReturn(false);
Ned Burnsf81c4c42019-01-07 14:10:43 -0500205 NotificationEntry childEntry2 =
Kevin01a53cb2018-11-09 18:19:54 -0800206 mGroupTestHelper.createChildNotification(Notification.GROUP_ALERT_SUMMARY);
207 mHeadsUpManager.showNotification(summaryEntry);
208 // Trigger a transfer of alert state from summary to child.
209 mGroupManager.onEntryAdded(summaryEntry);
210 mGroupManager.onEntryAdded(childEntry);
211
212 // Add second child notification so that summary is no longer suppressed.
213 mPendingEntries.put(childEntry2.key, childEntry2);
Gus Prevasd65c2db2018-12-18 17:13:38 -0500214 mNotificationEntryListener.onPendingEntryAdded(childEntry2);
Kevin01a53cb2018-11-09 18:19:54 -0800215 mGroupManager.onEntryAdded(childEntry2);
216
217 // Child entry finishes its inflation.
Evan Laird94492852018-10-25 13:43:01 -0400218 when(childEntry.getRow().isInflationFlagSet(mHeadsUpManager.getContentFlag()))
219 .thenReturn(true);
Gus Prevasd65c2db2018-12-18 17:13:38 -0500220 mNotificationEntryListener.onEntryReinflated(childEntry);
Kevin01a53cb2018-11-09 18:19:54 -0800221
Evan Laird94492852018-10-25 13:43:01 -0400222 verify(childEntry.getRow(), times(1)).freeContentViewWhenSafe(mHeadsUpManager
223 .getContentFlag());
Kevin01a53cb2018-11-09 18:19:54 -0800224 assertFalse(mHeadsUpManager.isAlerting(childEntry.key));
225 }
Kevin4b8bbda2018-11-19 14:36:31 -0800226
227 @Test
228 public void testCleanUpPendingAlertInfo() {
Ned Burnsf81c4c42019-01-07 14:10:43 -0500229 NotificationEntry summaryEntry =
Kevin4b8bbda2018-11-19 14:36:31 -0800230 mGroupTestHelper.createSummaryNotification(Notification.GROUP_ALERT_SUMMARY);
Ned Burnsf81c4c42019-01-07 14:10:43 -0500231 NotificationEntry childEntry =
Kevin4b8bbda2018-11-19 14:36:31 -0800232 mGroupTestHelper.createChildNotification(Notification.GROUP_ALERT_SUMMARY);
Evan Laird94492852018-10-25 13:43:01 -0400233 when(childEntry.getRow().isInflationFlagSet(mHeadsUpManager.getContentFlag()))
234 .thenReturn(false);
Kevin4b8bbda2018-11-19 14:36:31 -0800235 mHeadsUpManager.showNotification(summaryEntry);
236 // Trigger a transfer of alert state from summary to child.
237 mGroupManager.onEntryAdded(summaryEntry);
238 mGroupManager.onEntryAdded(childEntry);
239
Ned Burnsef2ef6c2019-01-02 16:48:08 -0500240 mNotificationEntryListener.onEntryRemoved(childEntry, null, false);
Kevin4b8bbda2018-11-19 14:36:31 -0800241
242 assertFalse(mGroupAlertTransferHelper.isAlertTransferPending(childEntry));
243 }
244
245 @Test
246 public void testUpdateGroupChangeDoesNotTransfer() {
Ned Burnsf81c4c42019-01-07 14:10:43 -0500247 NotificationEntry summaryEntry =
Kevin4b8bbda2018-11-19 14:36:31 -0800248 mGroupTestHelper.createSummaryNotification(Notification.GROUP_ALERT_SUMMARY);
Ned Burnsf81c4c42019-01-07 14:10:43 -0500249 NotificationEntry childEntry =
Kevin4b8bbda2018-11-19 14:36:31 -0800250 mGroupTestHelper.createChildNotification(Notification.GROUP_ALERT_SUMMARY);
Evan Laird94492852018-10-25 13:43:01 -0400251 when(childEntry.getRow().isInflationFlagSet(mHeadsUpManager.getContentFlag()))
252 .thenReturn(false);
Kevin4b8bbda2018-11-19 14:36:31 -0800253 mHeadsUpManager.showNotification(summaryEntry);
254 // Trigger a transfer of alert state from summary to child.
255 mGroupManager.onEntryAdded(summaryEntry);
256 mGroupManager.onEntryAdded(childEntry);
257
258 // Notify that entry changed groups.
259 StatusBarNotification oldNotification = childEntry.notification;
260 StatusBarNotification newSbn = spy(childEntry.notification.clone());
261 doReturn("other_group").when(newSbn).getGroupKey();
262 childEntry.notification = newSbn;
263 mGroupManager.onEntryUpdated(childEntry, oldNotification);
264
265 assertFalse(mGroupAlertTransferHelper.isAlertTransferPending(childEntry));
266 }
267
268 @Test
269 public void testUpdateChildToSummaryDoesNotTransfer() {
Ned Burnsf81c4c42019-01-07 14:10:43 -0500270 NotificationEntry summaryEntry =
Kevin4b8bbda2018-11-19 14:36:31 -0800271 mGroupTestHelper.createSummaryNotification(Notification.GROUP_ALERT_SUMMARY);
Ned Burnsf81c4c42019-01-07 14:10:43 -0500272 NotificationEntry childEntry =
Kevin4b8bbda2018-11-19 14:36:31 -0800273 mGroupTestHelper.createChildNotification(Notification.GROUP_ALERT_SUMMARY);
Evan Laird94492852018-10-25 13:43:01 -0400274 when(childEntry.getRow().isInflationFlagSet(mHeadsUpManager.getContentFlag()))
275 .thenReturn(false);
Kevin4b8bbda2018-11-19 14:36:31 -0800276 mHeadsUpManager.showNotification(summaryEntry);
277 // Trigger a transfer of alert state from summary to child.
278 mGroupManager.onEntryAdded(summaryEntry);
279 mGroupManager.onEntryAdded(childEntry);
280
281 // Update that child to a summary.
282 StatusBarNotification oldNotification = childEntry.notification;
283 childEntry.notification = mGroupTestHelper.createSummaryNotification(
284 Notification.GROUP_ALERT_SUMMARY).notification;
285 mGroupManager.onEntryUpdated(childEntry, oldNotification);
286
287 assertFalse(mGroupAlertTransferHelper.isAlertTransferPending(childEntry));
288 }
Kevin01a53cb2018-11-09 18:19:54 -0800289}