blob: ca72602f2c2b750a71ce506dec89e60d5dfbba05 [file] [log] [blame]
Mady Mellorebdbbb92018-11-15 14:36:48 -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.bubbles;
18
Mady Melloredd4ee12019-01-18 10:45:11 -080019import static org.junit.Assert.assertEquals;
Mady Mellorebdbbb92018-11-15 14:36:48 -080020import static org.junit.Assert.assertFalse;
21import static org.junit.Assert.assertTrue;
Ned Burns01e38212019-01-03 16:32:52 -050022import static org.mockito.Mockito.atLeastOnce;
Ned Burns01e38212019-01-03 16:32:52 -050023import static org.mockito.Mockito.verify;
24import static org.mockito.Mockito.when;
Mady Mellorebdbbb92018-11-15 14:36:48 -080025
26import android.app.IActivityManager;
27import android.content.Context;
28import android.support.test.filters.SmallTest;
29import android.testing.AndroidTestingRunner;
30import android.testing.TestableLooper;
31import android.view.WindowManager;
32import android.widget.FrameLayout;
33
34import com.android.systemui.SysuiTestCase;
35import com.android.systemui.statusbar.NotificationTestHelper;
Ned Burns01e38212019-01-03 16:32:52 -050036import com.android.systemui.statusbar.notification.NotificationEntryListener;
37import com.android.systemui.statusbar.notification.NotificationEntryManager;
Ned Burnsf81c4c42019-01-07 14:10:43 -050038import com.android.systemui.statusbar.notification.collection.NotificationData;
Mady Mellor9bad2242019-01-28 11:21:51 -080039import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Mady Mellorebdbbb92018-11-15 14:36:48 -080040import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow;
41import com.android.systemui.statusbar.phone.DozeParameters;
42import com.android.systemui.statusbar.phone.StatusBarWindowController;
43
44import org.junit.Before;
45import org.junit.Test;
46import org.junit.runner.RunWith;
Ned Burns01e38212019-01-03 16:32:52 -050047import org.mockito.ArgumentCaptor;
48import org.mockito.Captor;
Mady Mellorebdbbb92018-11-15 14:36:48 -080049import org.mockito.Mock;
50import org.mockito.MockitoAnnotations;
51
52@SmallTest
53@RunWith(AndroidTestingRunner.class)
54@TestableLooper.RunWithLooper(setAsMainLooper = true)
55public class BubbleControllerTest extends SysuiTestCase {
56
57 @Mock
Ned Burns01e38212019-01-03 16:32:52 -050058 private NotificationEntryManager mNotificationEntryManager;
59 @Mock
Mady Mellorebdbbb92018-11-15 14:36:48 -080060 private WindowManager mWindowManager;
61 @Mock
62 private IActivityManager mActivityManager;
63 @Mock
64 private DozeParameters mDozeParameters;
Mady Mellorebdbbb92018-11-15 14:36:48 -080065 private FrameLayout mStatusBarView;
Ned Burns01e38212019-01-03 16:32:52 -050066 @Captor
67 private ArgumentCaptor<NotificationEntryListener> mEntryListenerCaptor;
Mady Mellorebdbbb92018-11-15 14:36:48 -080068
69 private TestableBubbleController mBubbleController;
70 private StatusBarWindowController mStatusBarWindowController;
Ned Burns01e38212019-01-03 16:32:52 -050071 private NotificationEntryListener mEntryListener;
Mady Mellorebdbbb92018-11-15 14:36:48 -080072
73 private NotificationTestHelper mNotificationTestHelper;
74 private ExpandableNotificationRow mRow;
75 private ExpandableNotificationRow mRow2;
Mady Mellorc18ba962019-01-29 11:11:56 -080076 private ExpandableNotificationRow mNoChannelRow;
Mady Mellorebdbbb92018-11-15 14:36:48 -080077
Mady Mellorb4991e62019-01-10 15:14:51 -080078 @Mock
79 private NotificationData mNotificationData;
Mady Melloracb12152019-01-29 15:24:48 -080080 @Mock
81 private BubbleController.BubbleStateChangeListener mBubbleStateChangeListener;
82 @Mock
83 private BubbleController.BubbleExpandListener mBubbleExpandListener;
Ned Burns01e38212019-01-03 16:32:52 -050084
Mady Mellorcfd06c12019-02-13 14:32:12 -080085 private BubbleData mBubbleData;
86
Mady Mellorebdbbb92018-11-15 14:36:48 -080087 @Before
88 public void setUp() throws Exception {
89 MockitoAnnotations.initMocks(this);
Joshua Tsujib1a796b2019-01-16 15:43:12 -080090 mStatusBarView = new FrameLayout(mContext);
Ned Burns01e38212019-01-03 16:32:52 -050091 mDependency.injectTestDependency(NotificationEntryManager.class, mNotificationEntryManager);
Mady Mellorebdbbb92018-11-15 14:36:48 -080092
93 // Bubbles get added to status bar window view
94 mStatusBarWindowController = new StatusBarWindowController(mContext, mWindowManager,
95 mActivityManager, mDozeParameters);
96 mStatusBarWindowController.add(mStatusBarView, 120 /* height */);
97
98 // Need notifications for bubbles
99 mNotificationTestHelper = new NotificationTestHelper(mContext);
100 mRow = mNotificationTestHelper.createBubble();
101 mRow2 = mNotificationTestHelper.createBubble();
Mady Mellorc18ba962019-01-29 11:11:56 -0800102 mNoChannelRow = mNotificationTestHelper.createBubble();
Mady Mellorebdbbb92018-11-15 14:36:48 -0800103
Ned Burns01e38212019-01-03 16:32:52 -0500104 // Return non-null notification data from the NEM
105 when(mNotificationEntryManager.getNotificationData()).thenReturn(mNotificationData);
Mady Mellorb4991e62019-01-10 15:14:51 -0800106 when(mNotificationData.getChannel(mRow.getEntry().key)).thenReturn(mRow.getEntry().channel);
Mady Mellorc18ba962019-01-29 11:11:56 -0800107 when(mNotificationData.getChannel(mNoChannelRow.getEntry().key)).thenReturn(null);
Ned Burns01e38212019-01-03 16:32:52 -0500108
Mady Mellorcfd06c12019-02-13 14:32:12 -0800109 mBubbleData = new BubbleData();
110 mBubbleController = new TestableBubbleController(mContext, mStatusBarWindowController,
111 mBubbleData);
Mady Melloracb12152019-01-29 15:24:48 -0800112 mBubbleController.setBubbleStateChangeListener(mBubbleStateChangeListener);
113 mBubbleController.setExpandListener(mBubbleExpandListener);
Ned Burns01e38212019-01-03 16:32:52 -0500114
115 // Get a reference to the BubbleController's entry listener
116 verify(mNotificationEntryManager, atLeastOnce())
117 .addNotificationEntryListener(mEntryListenerCaptor.capture());
118 mEntryListener = mEntryListenerCaptor.getValue();
Mady Mellorebdbbb92018-11-15 14:36:48 -0800119 }
120
121 @Test
Mady Mellorebdbbb92018-11-15 14:36:48 -0800122 public void testAddBubble() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800123 mBubbleController.updateBubble(mRow.getEntry(), true /* updatePosition */);
Mady Mellorebdbbb92018-11-15 14:36:48 -0800124 assertTrue(mBubbleController.hasBubbles());
Mady Melloracb12152019-01-29 15:24:48 -0800125
126 verify(mBubbleStateChangeListener).onHasBubblesChanged(true);
Mady Mellorebdbbb92018-11-15 14:36:48 -0800127 }
128
129 @Test
130 public void testHasBubbles() {
131 assertFalse(mBubbleController.hasBubbles());
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800132 mBubbleController.updateBubble(mRow.getEntry(), true /* updatePosition */);
Mady Mellorebdbbb92018-11-15 14:36:48 -0800133 assertTrue(mBubbleController.hasBubbles());
134 }
135
136 @Test
137 public void testRemoveBubble() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800138 mBubbleController.updateBubble(mRow.getEntry(), true /* updatePosition */);
Mady Mellorebdbbb92018-11-15 14:36:48 -0800139 assertTrue(mBubbleController.hasBubbles());
140
Mady Melloracb12152019-01-29 15:24:48 -0800141 verify(mBubbleStateChangeListener).onHasBubblesChanged(true);
142
Mady Mellorebdbbb92018-11-15 14:36:48 -0800143 mBubbleController.removeBubble(mRow.getEntry().key);
144 assertFalse(mStatusBarWindowController.getBubblesShowing());
Ned Burns01e38212019-01-03 16:32:52 -0500145 assertTrue(mRow.getEntry().isBubbleDismissed());
146 verify(mNotificationEntryManager).updateNotifications();
Mady Melloracb12152019-01-29 15:24:48 -0800147
148 verify(mBubbleStateChangeListener).onHasBubblesChanged(false);
Mady Mellorebdbbb92018-11-15 14:36:48 -0800149 }
150
151 @Test
152 public void testDismissStack() {
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800153 mBubbleController.updateBubble(mRow.getEntry(), true /* updatePosition */);
154 mBubbleController.updateBubble(mRow2.getEntry(), true /* updatePosition */);
Mady Mellorebdbbb92018-11-15 14:36:48 -0800155 assertTrue(mBubbleController.hasBubbles());
156
157 mBubbleController.dismissStack();
158 assertFalse(mStatusBarWindowController.getBubblesShowing());
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800159 verify(mNotificationEntryManager).updateNotifications();
Mady Melloracb12152019-01-29 15:24:48 -0800160 assertTrue(mRow.getEntry().isBubbleDismissed());
161 assertTrue(mRow2.getEntry().isBubbleDismissed());
Mady Mellorebdbbb92018-11-15 14:36:48 -0800162 }
163
164 @Test
Mady Melloracb12152019-01-29 15:24:48 -0800165 public void testExpandCollapseStack() {
Mady Mellorebdbbb92018-11-15 14:36:48 -0800166 assertFalse(mBubbleController.isStackExpanded());
Mady Melloracb12152019-01-29 15:24:48 -0800167
168 // Mark it as a bubble and add it explicitly
169 mEntryListener.onPendingEntryAdded(mRow.getEntry());
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800170 mBubbleController.updateBubble(mRow.getEntry(), true /* updatePosition */);
Mady Mellorebdbbb92018-11-15 14:36:48 -0800171
Mady Melloracb12152019-01-29 15:24:48 -0800172 // We should have bubbles & their notifs should show in the shade
173 assertTrue(mBubbleController.hasBubbles());
174 assertTrue(mRow.getEntry().showInShadeWhenBubble());
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100175 assertFalse(mStatusBarWindowController.getBubbleExpanded());
Mady Melloracb12152019-01-29 15:24:48 -0800176
177 // Expand the stack
Mady Mellorebdbbb92018-11-15 14:36:48 -0800178 BubbleStackView stackView = mBubbleController.getStackView();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800179 stackView.expandStack();
Mady Mellorebdbbb92018-11-15 14:36:48 -0800180 assertTrue(mBubbleController.isStackExpanded());
Mady Melloracb12152019-01-29 15:24:48 -0800181 verify(mBubbleExpandListener).onBubbleExpandChanged(true, mRow.getEntry().key);
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100182 assertTrue(mStatusBarWindowController.getBubbleExpanded());
Mady Mellorebdbbb92018-11-15 14:36:48 -0800183
Mady Melloracb12152019-01-29 15:24:48 -0800184 // Make sure it's no longer in the shade
185 assertFalse(mRow.getEntry().showInShadeWhenBubble());
186
187 // Collapse
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800188 stackView.collapseStack();
Mady Melloracb12152019-01-29 15:24:48 -0800189 verify(mBubbleExpandListener).onBubbleExpandChanged(false, mRow.getEntry().key);
Mady Mellorebdbbb92018-11-15 14:36:48 -0800190 assertFalse(mBubbleController.isStackExpanded());
Issei Suzukiac9fcb72019-02-04 17:45:57 +0100191 assertFalse(mStatusBarWindowController.getBubbleExpanded());
Mady Mellorebdbbb92018-11-15 14:36:48 -0800192 }
193
194 @Test
Mady Melloracb12152019-01-29 15:24:48 -0800195 public void testCollapseAfterChangingExpandedBubble() {
196 // Mark it as a bubble and add it explicitly
197 mEntryListener.onPendingEntryAdded(mRow.getEntry());
198 mEntryListener.onPendingEntryAdded(mRow2.getEntry());
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800199 mBubbleController.updateBubble(mRow.getEntry(), true /* updatePosition */);
200 mBubbleController.updateBubble(mRow2.getEntry(), true /* updatePosition */);
Mady Mellorebdbbb92018-11-15 14:36:48 -0800201
Mady Melloracb12152019-01-29 15:24:48 -0800202 // We should have bubbles & their notifs should show in the shade
203 assertTrue(mBubbleController.hasBubbles());
204 assertTrue(mRow.getEntry().showInShadeWhenBubble());
205 assertTrue(mRow2.getEntry().showInShadeWhenBubble());
206
207 // Expand
Mady Mellorebdbbb92018-11-15 14:36:48 -0800208 BubbleStackView stackView = mBubbleController.getStackView();
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800209 stackView.expandStack();
Mady Mellorebdbbb92018-11-15 14:36:48 -0800210 assertTrue(mBubbleController.isStackExpanded());
Mady Melloracb12152019-01-29 15:24:48 -0800211 verify(mBubbleExpandListener).onBubbleExpandChanged(true, mRow2.getEntry().key);
Mady Mellorebdbbb92018-11-15 14:36:48 -0800212
Mady Melloracb12152019-01-29 15:24:48 -0800213 // Last added is the one that is expanded
Mady Mellor3dff9e62019-02-05 18:12:53 -0800214 assertEquals(mRow2.getEntry(), stackView.getExpandedBubbleView().getEntry());
Mady Melloracb12152019-01-29 15:24:48 -0800215 assertFalse(mRow2.getEntry().showInShadeWhenBubble());
216
217 // Switch which bubble is expanded
Mady Melloredd4ee12019-01-18 10:45:11 -0800218 stackView.setExpandedBubble(mRow.getEntry());
Mady Mellor3dff9e62019-02-05 18:12:53 -0800219 assertEquals(mRow.getEntry(), stackView.getExpandedBubbleView().getEntry());
Mady Melloracb12152019-01-29 15:24:48 -0800220 assertFalse(mRow.getEntry().showInShadeWhenBubble());
Mady Melloredd4ee12019-01-18 10:45:11 -0800221
Mady Melloracb12152019-01-29 15:24:48 -0800222 // collapse for previous bubble
223 verify(mBubbleExpandListener).onBubbleExpandChanged(false, mRow2.getEntry().key);
224 // expand for selected bubble
225 verify(mBubbleExpandListener).onBubbleExpandChanged(true, mRow.getEntry().key);
Mady Melloredd4ee12019-01-18 10:45:11 -0800226
Mady Melloracb12152019-01-29 15:24:48 -0800227 // Collapse
Mady Mellorebdbbb92018-11-15 14:36:48 -0800228 mBubbleController.collapseStack();
229 assertFalse(mBubbleController.isStackExpanded());
230 }
231
Ned Burns01e38212019-01-03 16:32:52 -0500232 @Test
Mady Melloracb12152019-01-29 15:24:48 -0800233 public void testExpansionRemovesShowInShade() {
234 // Mark it as a bubble and add it explicitly
235 mEntryListener.onPendingEntryAdded(mRow.getEntry());
236 mBubbleController.updateBubble(mRow.getEntry(), true /* updatePosition */);
237
238 // We should have bubbles & their notifs should show in the shade
239 assertTrue(mBubbleController.hasBubbles());
240 assertTrue(mRow.getEntry().showInShadeWhenBubble());
241
242 // Expand
243 BubbleStackView stackView = mBubbleController.getStackView();
244 stackView.expandStack();
245 assertTrue(mBubbleController.isStackExpanded());
246 verify(mBubbleExpandListener).onBubbleExpandChanged(true, mRow.getEntry().key);
247
248 // No longer show shade in notif after expansion
249 assertFalse(mRow.getEntry().showInShadeWhenBubble());
250 }
251
252 @Test
253 public void testRemoveLastExpandedCollapses() {
254 // Mark it as a bubble and add it explicitly
255 mEntryListener.onPendingEntryAdded(mRow.getEntry());
256 mEntryListener.onPendingEntryAdded(mRow2.getEntry());
257 mBubbleController.updateBubble(mRow.getEntry(), true /* updatePosition */);
258 mBubbleController.updateBubble(mRow2.getEntry(), true /* updatePosition */);
259 verify(mBubbleStateChangeListener).onHasBubblesChanged(true);
260
261 // Expand
262 BubbleStackView stackView = mBubbleController.getStackView();
263 stackView.expandStack();
264
265 assertTrue(mBubbleController.isStackExpanded());
266 verify(mBubbleExpandListener).onBubbleExpandChanged(true, mRow2.getEntry().key);
267
268 // Last added is the one that is expanded
Mady Mellor3dff9e62019-02-05 18:12:53 -0800269 assertEquals(mRow2.getEntry(), stackView.getExpandedBubbleView().getEntry());
Mady Melloracb12152019-01-29 15:24:48 -0800270 assertFalse(mRow2.getEntry().showInShadeWhenBubble());
271
272 // Dismiss currently expanded
Mady Mellor3dff9e62019-02-05 18:12:53 -0800273 mBubbleController.removeBubble(stackView.getExpandedBubbleView().getKey());
Mady Melloracb12152019-01-29 15:24:48 -0800274 verify(mBubbleExpandListener).onBubbleExpandChanged(false, mRow2.getEntry().key);
275
276 // Make sure next bubble is selected
Mady Mellor3dff9e62019-02-05 18:12:53 -0800277 assertEquals(mRow.getEntry(), stackView.getExpandedBubbleView().getEntry());
Mady Melloracb12152019-01-29 15:24:48 -0800278 verify(mBubbleExpandListener).onBubbleExpandChanged(true, mRow.getEntry().key);
279
280 // Dismiss that one
Mady Mellor3dff9e62019-02-05 18:12:53 -0800281 mBubbleController.removeBubble(stackView.getExpandedBubbleView().getKey());
Mady Melloracb12152019-01-29 15:24:48 -0800282
283 // Make sure state changes and collapse happens
284 verify(mBubbleExpandListener).onBubbleExpandChanged(false, mRow.getEntry().key);
285 verify(mBubbleStateChangeListener).onHasBubblesChanged(false);
286 assertFalse(mBubbleController.hasBubbles());
287 }
288
289 @Test
Ned Burns01e38212019-01-03 16:32:52 -0500290 public void testMarkNewNotificationAsBubble() {
291 mEntryListener.onPendingEntryAdded(mRow.getEntry());
292 assertTrue(mRow.getEntry().isBubble());
293 }
294
Mady Mellor3f2efdb2018-11-21 11:30:45 -0800295 @Test
296 public void testMarkNewNotificationAsShowInShade() {
297 mEntryListener.onPendingEntryAdded(mRow.getEntry());
298 assertTrue(mRow.getEntry().showInShadeWhenBubble());
299 }
300
Mady Mellorebdbbb92018-11-15 14:36:48 -0800301 static class TestableBubbleController extends BubbleController {
302
303 TestableBubbleController(Context context,
Mady Mellorcfd06c12019-02-13 14:32:12 -0800304 StatusBarWindowController statusBarWindowController, BubbleData data) {
305 super(context, statusBarWindowController, data);
Mady Mellorebdbbb92018-11-15 14:36:48 -0800306 }
Mady Mellor9bad2242019-01-28 11:21:51 -0800307
308 @Override
Mady Mellorf6e3ac02019-01-29 10:37:52 -0800309 public boolean shouldAutoBubbleForFlags(Context c, NotificationEntry entry) {
Mady Mellor9bad2242019-01-28 11:21:51 -0800310 return entry.notification.getNotification().getBubbleMetadata() != null;
311 }
Mady Mellorebdbbb92018-11-15 14:36:48 -0800312 }
313}