blob: cefd4acbfb5ac86e6724d0617589f9fd3fd313c9 [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.support.test.filters.SmallTest;
30import android.testing.AndroidTestingRunner;
31import android.testing.TestableLooper;
32
33import com.android.systemui.SysuiTestCase;
34import com.android.systemui.statusbar.AmbientPulseManager;
Gus Prevasd65c2db2018-12-18 17:13:38 -050035import com.android.systemui.statusbar.notification.NotificationEntryListener;
Ned Burns3d6b3962018-12-07 21:26:00 -050036import com.android.systemui.statusbar.notification.NotificationEntryManager;
Ned Burnsf81c4c42019-01-07 14:10:43 -050037import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Kevin01a53cb2018-11-09 18:19:54 -080038import com.android.systemui.statusbar.policy.HeadsUpManager;
39
40import org.junit.Before;
41import org.junit.Rule;
42import org.junit.Test;
43import org.junit.runner.RunWith;
Ned Burns3d6b3962018-12-07 21:26:00 -050044import org.mockito.ArgumentCaptor;
45import org.mockito.Captor;
46import org.mockito.Mock;
Kevin01a53cb2018-11-09 18:19:54 -080047import org.mockito.junit.MockitoJUnit;
48import org.mockito.junit.MockitoRule;
49
50import java.util.HashMap;
51
52@SmallTest
53@RunWith(AndroidTestingRunner.class)
54@TestableLooper.RunWithLooper
55public class NotificationGroupAlertTransferHelperTest extends SysuiTestCase {
Ned Burns3d6b3962018-12-07 21:26:00 -050056 @Rule public MockitoRule rule = MockitoJUnit.rule();
Kevin01a53cb2018-11-09 18:19:54 -080057
58 private NotificationGroupAlertTransferHelper mGroupAlertTransferHelper;
59 private NotificationGroupManager mGroupManager;
60 private AmbientPulseManager mAmbientPulseManager;
61 private HeadsUpManager mHeadsUpManager;
Ned Burns3d6b3962018-12-07 21:26:00 -050062 @Mock private NotificationEntryManager mNotificationEntryManager;
Gus Prevasd65c2db2018-12-18 17:13:38 -050063 @Captor
64 private ArgumentCaptor<NotificationEntryListener> mListenerCaptor;
65 private NotificationEntryListener mNotificationEntryListener;
Ned Burnsf81c4c42019-01-07 14:10:43 -050066 private final HashMap<String, NotificationEntry> mPendingEntries = new HashMap<>();
Kevin01a53cb2018-11-09 18:19:54 -080067 private final NotificationGroupTestHelper mGroupTestHelper =
68 new NotificationGroupTestHelper(mContext);
69
70
71 @Before
72 public void setup() {
73 mAmbientPulseManager = new AmbientPulseManager(mContext);
74 mDependency.injectTestDependency(AmbientPulseManager.class, mAmbientPulseManager);
75 mHeadsUpManager = new HeadsUpManager(mContext) {};
76
Ned Burns3d6b3962018-12-07 21:26:00 -050077 when(mNotificationEntryManager.getPendingNotificationsIterator())
78 .thenReturn(mPendingEntries.values());
79
Kevin01a53cb2018-11-09 18:19:54 -080080 mGroupManager = new NotificationGroupManager();
81 mDependency.injectTestDependency(NotificationGroupManager.class, mGroupManager);
82 mGroupManager.setHeadsUpManager(mHeadsUpManager);
83
84 mGroupAlertTransferHelper = new NotificationGroupAlertTransferHelper();
85 mGroupAlertTransferHelper.setHeadsUpManager(mHeadsUpManager);
Kevin01a53cb2018-11-09 18:19:54 -080086
Ned Burns3d6b3962018-12-07 21:26:00 -050087 mGroupAlertTransferHelper.bind(mNotificationEntryManager, mGroupManager);
Gus Prevasd65c2db2018-12-18 17:13:38 -050088 verify(mNotificationEntryManager).addNotificationEntryListener(mListenerCaptor.capture());
89 mNotificationEntryListener = mListenerCaptor.getValue();
Kevin01a53cb2018-11-09 18:19:54 -080090 mHeadsUpManager.addListener(mGroupAlertTransferHelper);
91 mAmbientPulseManager.addListener(mGroupAlertTransferHelper);
92 }
93
94 @Test
95 public void testSuppressedSummaryHeadsUpTransfersToChild() {
Ned Burnsf81c4c42019-01-07 14:10:43 -050096 NotificationEntry summaryEntry = mGroupTestHelper.createSummaryNotification();
Kevin01a53cb2018-11-09 18:19:54 -080097 mHeadsUpManager.showNotification(summaryEntry);
Ned Burnsf81c4c42019-01-07 14:10:43 -050098 NotificationEntry childEntry = mGroupTestHelper.createChildNotification();
Kevin01a53cb2018-11-09 18:19:54 -080099
100 // Summary will be suppressed because there is only one child.
101 mGroupManager.onEntryAdded(summaryEntry);
102 mGroupManager.onEntryAdded(childEntry);
103
104 // A suppressed summary should transfer its alert state to the child.
105 assertFalse(mHeadsUpManager.isAlerting(summaryEntry.key));
106 assertTrue(mHeadsUpManager.isAlerting(childEntry.key));
107 }
108
109 @Test
110 public void testSuppressedSummaryHeadsUpTransfersToChildButBackAgain() {
Ned Burnsf81c4c42019-01-07 14:10:43 -0500111 NotificationEntry summaryEntry =
Kevin01a53cb2018-11-09 18:19:54 -0800112 mGroupTestHelper.createSummaryNotification(Notification.GROUP_ALERT_SUMMARY);
Ned Burnsf81c4c42019-01-07 14:10:43 -0500113 NotificationEntry childEntry =
Kevin01a53cb2018-11-09 18:19:54 -0800114 mGroupTestHelper.createChildNotification(Notification.GROUP_ALERT_SUMMARY);
Ned Burnsf81c4c42019-01-07 14:10:43 -0500115 NotificationEntry childEntry2 =
Kevin01a53cb2018-11-09 18:19:54 -0800116 mGroupTestHelper.createChildNotification(Notification.GROUP_ALERT_SUMMARY);
117 mHeadsUpManager.showNotification(summaryEntry);
118 // Trigger a transfer of alert state from summary to child.
119 mGroupManager.onEntryAdded(summaryEntry);
120 mGroupManager.onEntryAdded(childEntry);
121
122 // Add second child notification so that summary is no longer suppressed.
123 mPendingEntries.put(childEntry2.key, childEntry2);
Gus Prevasd65c2db2018-12-18 17:13:38 -0500124 mNotificationEntryListener.onPendingEntryAdded(childEntry2);
Kevin01a53cb2018-11-09 18:19:54 -0800125 mGroupManager.onEntryAdded(childEntry2);
126
127 // The alert state should transfer back to the summary as there is now more than one
128 // child and the summary should no longer be suppressed.
129 assertTrue(mHeadsUpManager.isAlerting(summaryEntry.key));
130 assertFalse(mHeadsUpManager.isAlerting(childEntry.key));
131 }
132
133 @Test
134 public void testSuppressedSummaryHeadsUpDoesntTransferBackOnDozingChanged() {
Ned Burnsf81c4c42019-01-07 14:10:43 -0500135 NotificationEntry summaryEntry =
Kevin01a53cb2018-11-09 18:19:54 -0800136 mGroupTestHelper.createSummaryNotification(Notification.GROUP_ALERT_SUMMARY);
Ned Burnsf81c4c42019-01-07 14:10:43 -0500137 NotificationEntry childEntry =
Kevin01a53cb2018-11-09 18:19:54 -0800138 mGroupTestHelper.createChildNotification(Notification.GROUP_ALERT_SUMMARY);
Ned Burnsf81c4c42019-01-07 14:10:43 -0500139 NotificationEntry childEntry2 =
Kevin01a53cb2018-11-09 18:19:54 -0800140 mGroupTestHelper.createChildNotification(Notification.GROUP_ALERT_SUMMARY);
141 mHeadsUpManager.showNotification(summaryEntry);
142 // Trigger a transfer of alert state from summary to child.
143 mGroupManager.onEntryAdded(summaryEntry);
144 mGroupManager.onEntryAdded(childEntry);
145
146 // Set dozing to true.
147 mGroupAlertTransferHelper.onDozingChanged(true);
148
149 // Add second child notification so that summary is no longer suppressed.
150 mPendingEntries.put(childEntry2.key, childEntry2);
Gus Prevasd65c2db2018-12-18 17:13:38 -0500151 mNotificationEntryListener.onPendingEntryAdded(childEntry2);
Kevin01a53cb2018-11-09 18:19:54 -0800152 mGroupManager.onEntryAdded(childEntry2);
153
154 // Dozing changed so no reason to re-alert summary.
155 assertFalse(mHeadsUpManager.isAlerting(summaryEntry.key));
156 }
157
158 @Test
159 public void testSuppressedSummaryHeadsUpTransferDoesNotAlertChildIfUninflated() {
Ned Burnsf81c4c42019-01-07 14:10:43 -0500160 NotificationEntry summaryEntry = mGroupTestHelper.createSummaryNotification();
Kevin01a53cb2018-11-09 18:19:54 -0800161 mHeadsUpManager.showNotification(summaryEntry);
Ned Burnsf81c4c42019-01-07 14:10:43 -0500162 NotificationEntry childEntry = mGroupTestHelper.createChildNotification();
Evan Laird94492852018-10-25 13:43:01 -0400163 when(childEntry.getRow().isInflationFlagSet(mHeadsUpManager.getContentFlag()))
164 .thenReturn(false);
Kevin01a53cb2018-11-09 18:19:54 -0800165
166 mGroupManager.onEntryAdded(summaryEntry);
167 mGroupManager.onEntryAdded(childEntry);
168
169 // Alert is immediately removed from summary, but we do not show child yet either as its
170 // content is not inflated.
171 assertFalse(mHeadsUpManager.isAlerting(summaryEntry.key));
172 assertFalse(mHeadsUpManager.isAlerting(childEntry.key));
Kevin4b8bbda2018-11-19 14:36:31 -0800173 assertTrue(mGroupAlertTransferHelper.isAlertTransferPending(childEntry));
Kevin01a53cb2018-11-09 18:19:54 -0800174 }
175
176 @Test
177 public void testSuppressedSummaryHeadsUpTransferAlertsChildOnInflation() {
Ned Burnsf81c4c42019-01-07 14:10:43 -0500178 NotificationEntry summaryEntry = mGroupTestHelper.createSummaryNotification();
Kevin01a53cb2018-11-09 18:19:54 -0800179 mHeadsUpManager.showNotification(summaryEntry);
Ned Burnsf81c4c42019-01-07 14:10:43 -0500180 NotificationEntry childEntry = mGroupTestHelper.createChildNotification();
Evan Laird94492852018-10-25 13:43:01 -0400181 when(childEntry.getRow().isInflationFlagSet(mHeadsUpManager.getContentFlag()))
182 .thenReturn(false);
Kevin01a53cb2018-11-09 18:19:54 -0800183
184 mGroupManager.onEntryAdded(summaryEntry);
185 mGroupManager.onEntryAdded(childEntry);
186
Evan Laird94492852018-10-25 13:43:01 -0400187 when(childEntry.getRow().isInflationFlagSet(mHeadsUpManager.getContentFlag()))
188 .thenReturn(true);
Gus Prevasd65c2db2018-12-18 17:13:38 -0500189 mNotificationEntryListener.onEntryReinflated(childEntry);
Kevin01a53cb2018-11-09 18:19:54 -0800190
191 // Alert is immediately removed from summary, and we show child as its content is inflated.
192 assertFalse(mHeadsUpManager.isAlerting(summaryEntry.key));
193 assertTrue(mHeadsUpManager.isAlerting(childEntry.key));
194 }
195
196 @Test
197 public void testSuppressedSummaryHeadsUpTransferBackAbortsChildInflation() {
Ned Burnsf81c4c42019-01-07 14:10:43 -0500198 NotificationEntry summaryEntry =
Kevin01a53cb2018-11-09 18:19:54 -0800199 mGroupTestHelper.createSummaryNotification(Notification.GROUP_ALERT_SUMMARY);
Ned Burnsf81c4c42019-01-07 14:10:43 -0500200 NotificationEntry childEntry =
Kevin01a53cb2018-11-09 18:19:54 -0800201 mGroupTestHelper.createChildNotification(Notification.GROUP_ALERT_SUMMARY);
Evan Laird94492852018-10-25 13:43:01 -0400202 when(childEntry.getRow().isInflationFlagSet(mHeadsUpManager.getContentFlag()))
203 .thenReturn(false);
Ned Burnsf81c4c42019-01-07 14:10:43 -0500204 NotificationEntry childEntry2 =
Kevin01a53cb2018-11-09 18:19:54 -0800205 mGroupTestHelper.createChildNotification(Notification.GROUP_ALERT_SUMMARY);
206 mHeadsUpManager.showNotification(summaryEntry);
207 // Trigger a transfer of alert state from summary to child.
208 mGroupManager.onEntryAdded(summaryEntry);
209 mGroupManager.onEntryAdded(childEntry);
210
211 // Add second child notification so that summary is no longer suppressed.
212 mPendingEntries.put(childEntry2.key, childEntry2);
Gus Prevasd65c2db2018-12-18 17:13:38 -0500213 mNotificationEntryListener.onPendingEntryAdded(childEntry2);
Kevin01a53cb2018-11-09 18:19:54 -0800214 mGroupManager.onEntryAdded(childEntry2);
215
216 // Child entry finishes its inflation.
Evan Laird94492852018-10-25 13:43:01 -0400217 when(childEntry.getRow().isInflationFlagSet(mHeadsUpManager.getContentFlag()))
218 .thenReturn(true);
Gus Prevasd65c2db2018-12-18 17:13:38 -0500219 mNotificationEntryListener.onEntryReinflated(childEntry);
Kevin01a53cb2018-11-09 18:19:54 -0800220
Evan Laird94492852018-10-25 13:43:01 -0400221 verify(childEntry.getRow(), times(1)).freeContentViewWhenSafe(mHeadsUpManager
222 .getContentFlag());
Kevin01a53cb2018-11-09 18:19:54 -0800223 assertFalse(mHeadsUpManager.isAlerting(childEntry.key));
224 }
Kevin4b8bbda2018-11-19 14:36:31 -0800225
226 @Test
227 public void testCleanUpPendingAlertInfo() {
Ned Burnsf81c4c42019-01-07 14:10:43 -0500228 NotificationEntry summaryEntry =
Kevin4b8bbda2018-11-19 14:36:31 -0800229 mGroupTestHelper.createSummaryNotification(Notification.GROUP_ALERT_SUMMARY);
Ned Burnsf81c4c42019-01-07 14:10:43 -0500230 NotificationEntry childEntry =
Kevin4b8bbda2018-11-19 14:36:31 -0800231 mGroupTestHelper.createChildNotification(Notification.GROUP_ALERT_SUMMARY);
Evan Laird94492852018-10-25 13:43:01 -0400232 when(childEntry.getRow().isInflationFlagSet(mHeadsUpManager.getContentFlag()))
233 .thenReturn(false);
Kevin4b8bbda2018-11-19 14:36:31 -0800234 mHeadsUpManager.showNotification(summaryEntry);
235 // Trigger a transfer of alert state from summary to child.
236 mGroupManager.onEntryAdded(summaryEntry);
237 mGroupManager.onEntryAdded(childEntry);
238
Ned Burnsef2ef6c2019-01-02 16:48:08 -0500239 mNotificationEntryListener.onEntryRemoved(childEntry, null, false);
Kevin4b8bbda2018-11-19 14:36:31 -0800240
241 assertFalse(mGroupAlertTransferHelper.isAlertTransferPending(childEntry));
242 }
243
244 @Test
245 public void testUpdateGroupChangeDoesNotTransfer() {
Ned Burnsf81c4c42019-01-07 14:10:43 -0500246 NotificationEntry summaryEntry =
Kevin4b8bbda2018-11-19 14:36:31 -0800247 mGroupTestHelper.createSummaryNotification(Notification.GROUP_ALERT_SUMMARY);
Ned Burnsf81c4c42019-01-07 14:10:43 -0500248 NotificationEntry childEntry =
Kevin4b8bbda2018-11-19 14:36:31 -0800249 mGroupTestHelper.createChildNotification(Notification.GROUP_ALERT_SUMMARY);
Evan Laird94492852018-10-25 13:43:01 -0400250 when(childEntry.getRow().isInflationFlagSet(mHeadsUpManager.getContentFlag()))
251 .thenReturn(false);
Kevin4b8bbda2018-11-19 14:36:31 -0800252 mHeadsUpManager.showNotification(summaryEntry);
253 // Trigger a transfer of alert state from summary to child.
254 mGroupManager.onEntryAdded(summaryEntry);
255 mGroupManager.onEntryAdded(childEntry);
256
257 // Notify that entry changed groups.
258 StatusBarNotification oldNotification = childEntry.notification;
259 StatusBarNotification newSbn = spy(childEntry.notification.clone());
260 doReturn("other_group").when(newSbn).getGroupKey();
261 childEntry.notification = newSbn;
262 mGroupManager.onEntryUpdated(childEntry, oldNotification);
263
264 assertFalse(mGroupAlertTransferHelper.isAlertTransferPending(childEntry));
265 }
266
267 @Test
268 public void testUpdateChildToSummaryDoesNotTransfer() {
Ned Burnsf81c4c42019-01-07 14:10:43 -0500269 NotificationEntry summaryEntry =
Kevin4b8bbda2018-11-19 14:36:31 -0800270 mGroupTestHelper.createSummaryNotification(Notification.GROUP_ALERT_SUMMARY);
Ned Burnsf81c4c42019-01-07 14:10:43 -0500271 NotificationEntry childEntry =
Kevin4b8bbda2018-11-19 14:36:31 -0800272 mGroupTestHelper.createChildNotification(Notification.GROUP_ALERT_SUMMARY);
Evan Laird94492852018-10-25 13:43:01 -0400273 when(childEntry.getRow().isInflationFlagSet(mHeadsUpManager.getContentFlag()))
274 .thenReturn(false);
Kevin4b8bbda2018-11-19 14:36:31 -0800275 mHeadsUpManager.showNotification(summaryEntry);
276 // Trigger a transfer of alert state from summary to child.
277 mGroupManager.onEntryAdded(summaryEntry);
278 mGroupManager.onEntryAdded(childEntry);
279
280 // Update that child to a summary.
281 StatusBarNotification oldNotification = childEntry.notification;
282 childEntry.notification = mGroupTestHelper.createSummaryNotification(
283 Notification.GROUP_ALERT_SUMMARY).notification;
284 mGroupManager.onEntryUpdated(childEntry, oldNotification);
285
286 assertFalse(mGroupAlertTransferHelper.isAlertTransferPending(childEntry));
287 }
Kevin01a53cb2018-11-09 18:19:54 -0800288}