blob: 6b2e05b23ca7603ee83382c2156d23632c761362 [file] [log] [blame]
Eliot Courtney2b4c3a02017-11-27 13:27:46 +09001/*
2 * Copyright (C) 2017 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;
18
19import static junit.framework.Assert.assertTrue;
20
21import static org.junit.Assert.assertEquals;
Julia Reynoldsfc640012018-02-21 12:25:27 -050022import static org.mockito.ArgumentMatchers.any;
Jason Monk09f4d372018-12-20 15:30:54 -050023import static org.mockito.Mockito.mock;
Julia Reynoldsfc640012018-02-21 12:25:27 -050024import static org.mockito.Mockito.spy;
25import static org.mockito.Mockito.times;
Eliot Courtney2b4c3a02017-11-27 13:27:46 +090026import static org.mockito.Mockito.verify;
27import static org.mockito.Mockito.when;
28
Eliot Courtney2b4c3a02017-11-27 13:27:46 +090029import android.testing.AndroidTestingRunner;
30import android.testing.TestableLooper;
31import android.view.View;
32import android.view.ViewGroup;
33import android.widget.LinearLayout;
34
Brett Chabot84151d92019-02-27 15:37:59 -080035import androidx.test.filters.SmallTest;
36
Jason Monk297c04e2018-08-23 17:16:59 -040037import com.android.systemui.Dependency;
38import com.android.systemui.InitController;
Eliot Courtney2b4c3a02017-11-27 13:27:46 +090039import com.android.systemui.SysuiTestCase;
40import com.android.systemui.plugins.statusbar.NotificationSwipeActionHelper;
Rohan Shah20790b82018-07-02 17:21:04 -070041import com.android.systemui.statusbar.notification.NotificationEntryManager;
Eliot Courtney2b4c3a02017-11-27 13:27:46 +090042import com.android.systemui.statusbar.notification.VisualStabilityManager;
Ned Burnsf81c4c42019-01-07 14:10:43 -050043import com.android.systemui.statusbar.notification.collection.NotificationData;
44import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Rohan Shah20790b82018-07-02 17:21:04 -070045import com.android.systemui.statusbar.notification.logging.NotificationLogger;
46import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow;
47import com.android.systemui.statusbar.notification.row.ExpandableView;
48import com.android.systemui.statusbar.notification.stack.NotificationListContainer;
Eliot Courtney2b4c3a02017-11-27 13:27:46 +090049import com.android.systemui.statusbar.phone.NotificationGroupManager;
Jason Monk297c04e2018-08-23 17:16:59 -040050import com.android.systemui.statusbar.phone.ShadeController;
Jason Monka716bac2018-12-05 15:48:21 -050051import com.android.systemui.util.Assert;
Eliot Courtney2b4c3a02017-11-27 13:27:46 +090052
53import com.google.android.collect.Lists;
54
55import org.junit.Before;
56import org.junit.Test;
57import org.junit.runner.RunWith;
58import org.mockito.Mock;
59import org.mockito.MockitoAnnotations;
60import org.mockito.Spy;
61
62import java.util.List;
63
64@SmallTest
65@RunWith(AndroidTestingRunner.class)
Jason Monka716bac2018-12-05 15:48:21 -050066@TestableLooper.RunWithLooper
Eliot Courtney2b4c3a02017-11-27 13:27:46 +090067public class NotificationViewHierarchyManagerTest extends SysuiTestCase {
68 @Mock private NotificationPresenter mPresenter;
Eliot Courtney8f56b0e2017-12-14 18:54:28 +090069 @Mock private NotificationData mNotificationData;
70 @Spy private FakeListContainer mListContainer = new FakeListContainer();
71
72 // Dependency mocks:
Eliot Courtney2b4c3a02017-11-27 13:27:46 +090073 @Mock private NotificationEntryManager mEntryManager;
74 @Mock private NotificationLockscreenUserManager mLockscreenUserManager;
75 @Mock private NotificationGroupManager mGroupManager;
76 @Mock private VisualStabilityManager mVisualStabilityManager;
Jason Monk297c04e2018-08-23 17:16:59 -040077 @Mock private ShadeController mShadeController;
Eliot Courtney2b4c3a02017-11-27 13:27:46 +090078
79 private NotificationViewHierarchyManager mViewHierarchyManager;
Ned Burns01e38212019-01-03 16:32:52 -050080 private NotificationTestHelper mHelper;
Eliot Courtney2b4c3a02017-11-27 13:27:46 +090081
82 @Before
83 public void setUp() {
84 MockitoAnnotations.initMocks(this);
Jason Monka716bac2018-12-05 15:48:21 -050085 Assert.sMainLooper = TestableLooper.get(this).getLooper();
Eliot Courtney8f56b0e2017-12-14 18:54:28 +090086 mDependency.injectTestDependency(NotificationEntryManager.class, mEntryManager);
87 mDependency.injectTestDependency(NotificationLockscreenUserManager.class,
88 mLockscreenUserManager);
89 mDependency.injectTestDependency(NotificationGroupManager.class, mGroupManager);
90 mDependency.injectTestDependency(VisualStabilityManager.class, mVisualStabilityManager);
Jason Monk297c04e2018-08-23 17:16:59 -040091 mDependency.injectTestDependency(ShadeController.class, mShadeController);
Eliot Courtney2b4c3a02017-11-27 13:27:46 +090092
Ned Burns01e38212019-01-03 16:32:52 -050093 mHelper = new NotificationTestHelper(mContext);
94
Eliot Courtney2b4c3a02017-11-27 13:27:46 +090095 when(mEntryManager.getNotificationData()).thenReturn(mNotificationData);
96
Jason Monk09f4d372018-12-20 15:30:54 -050097 mViewHierarchyManager = new NotificationViewHierarchyManager(mContext,
98 mLockscreenUserManager, mGroupManager, mVisualStabilityManager,
Beverly8fdb5332019-02-04 14:29:49 -050099 mock(StatusBarStateControllerImpl.class), mEntryManager,
Jason Monk09f4d372018-12-20 15:30:54 -0500100 () -> mShadeController);
Jason Monk297c04e2018-08-23 17:16:59 -0400101 Dependency.get(InitController.class).executePostInitTasks();
102 mViewHierarchyManager.setUpWithPresenter(mPresenter, mListContainer);
Eliot Courtney2b4c3a02017-11-27 13:27:46 +0900103 }
104
Ned Burnsf81c4c42019-01-07 14:10:43 -0500105 private NotificationEntry createEntry() throws Exception {
Eliot Courtney2b4c3a02017-11-27 13:27:46 +0900106 ExpandableNotificationRow row = mHelper.createRow();
Ned Burnsf81c4c42019-01-07 14:10:43 -0500107 NotificationEntry entry = new NotificationEntry(row.getStatusBarNotification());
Evan Laird94492852018-10-25 13:43:01 -0400108 entry.setRow(row);
Eliot Courtney2b4c3a02017-11-27 13:27:46 +0900109 return entry;
110 }
111
112 @Test
113 public void testNotificationsBecomingBundled() throws Exception {
114 // Tests 3 top level notifications becoming a single bundled notification with |entry0| as
115 // the summary.
Ned Burnsf81c4c42019-01-07 14:10:43 -0500116 NotificationEntry entry0 = createEntry();
117 NotificationEntry entry1 = createEntry();
118 NotificationEntry entry2 = createEntry();
Eliot Courtney2b4c3a02017-11-27 13:27:46 +0900119
120 // Set up the prior state to look like three top level notifications.
Evan Laird94492852018-10-25 13:43:01 -0400121 mListContainer.addContainerView(entry0.getRow());
122 mListContainer.addContainerView(entry1.getRow());
123 mListContainer.addContainerView(entry2.getRow());
Eliot Courtney2b4c3a02017-11-27 13:27:46 +0900124 when(mNotificationData.getActiveNotifications()).thenReturn(
125 Lists.newArrayList(entry0, entry1, entry2));
126
127 // Set up group manager to report that they should be bundled now.
128 when(mGroupManager.isChildInGroupWithSummary(entry0.notification)).thenReturn(false);
129 when(mGroupManager.isChildInGroupWithSummary(entry1.notification)).thenReturn(true);
130 when(mGroupManager.isChildInGroupWithSummary(entry2.notification)).thenReturn(true);
Evan Laird94492852018-10-25 13:43:01 -0400131 when(mGroupManager.getGroupSummary(entry1.notification)).thenReturn(entry0);
132 when(mGroupManager.getGroupSummary(entry2.notification)).thenReturn(entry0);
Eliot Courtney2b4c3a02017-11-27 13:27:46 +0900133
134 // Run updateNotifications - the view hierarchy should be reorganized.
135 mViewHierarchyManager.updateNotificationViews();
136
Evan Laird94492852018-10-25 13:43:01 -0400137 verify(mListContainer).notifyGroupChildAdded(entry1.getRow());
138 verify(mListContainer).notifyGroupChildAdded(entry2.getRow());
139 assertTrue(Lists.newArrayList(entry0.getRow()).equals(mListContainer.mRows));
Eliot Courtney2b4c3a02017-11-27 13:27:46 +0900140 }
141
142 @Test
143 public void testNotificationsBecomingUnbundled() throws Exception {
144 // Tests a bundled notification becoming three top level notifications.
Ned Burnsf81c4c42019-01-07 14:10:43 -0500145 NotificationEntry entry0 = createEntry();
146 NotificationEntry entry1 = createEntry();
147 NotificationEntry entry2 = createEntry();
Evan Laird94492852018-10-25 13:43:01 -0400148 entry0.getRow().addChildNotification(entry1.getRow());
149 entry0.getRow().addChildNotification(entry2.getRow());
Eliot Courtney2b4c3a02017-11-27 13:27:46 +0900150
151 // Set up the prior state to look like one top level notification.
Evan Laird94492852018-10-25 13:43:01 -0400152 mListContainer.addContainerView(entry0.getRow());
Eliot Courtney2b4c3a02017-11-27 13:27:46 +0900153 when(mNotificationData.getActiveNotifications()).thenReturn(
154 Lists.newArrayList(entry0, entry1, entry2));
155
156 // Set up group manager to report that they should not be bundled now.
157 when(mGroupManager.isChildInGroupWithSummary(entry0.notification)).thenReturn(false);
158 when(mGroupManager.isChildInGroupWithSummary(entry1.notification)).thenReturn(false);
159 when(mGroupManager.isChildInGroupWithSummary(entry2.notification)).thenReturn(false);
160
161 // Run updateNotifications - the view hierarchy should be reorganized.
162 mViewHierarchyManager.updateNotificationViews();
163
164 verify(mListContainer).notifyGroupChildRemoved(
Evan Laird94492852018-10-25 13:43:01 -0400165 entry1.getRow(), entry0.getRow().getChildrenContainer());
Eliot Courtney2b4c3a02017-11-27 13:27:46 +0900166 verify(mListContainer).notifyGroupChildRemoved(
Evan Laird94492852018-10-25 13:43:01 -0400167 entry2.getRow(), entry0.getRow().getChildrenContainer());
168 assertTrue(
169 Lists.newArrayList(entry0.getRow(), entry1.getRow(), entry2.getRow())
170 .equals(mListContainer.mRows));
Eliot Courtney2b4c3a02017-11-27 13:27:46 +0900171 }
172
173 @Test
174 public void testNotificationsBecomingSuppressed() throws Exception {
175 // Tests two top level notifications becoming a suppressed summary and a child.
Ned Burnsf81c4c42019-01-07 14:10:43 -0500176 NotificationEntry entry0 = createEntry();
177 NotificationEntry entry1 = createEntry();
Evan Laird94492852018-10-25 13:43:01 -0400178 entry0.getRow().addChildNotification(entry1.getRow());
Eliot Courtney2b4c3a02017-11-27 13:27:46 +0900179
180 // Set up the prior state to look like a top level notification.
Evan Laird94492852018-10-25 13:43:01 -0400181 mListContainer.addContainerView(entry0.getRow());
Eliot Courtney2b4c3a02017-11-27 13:27:46 +0900182 when(mNotificationData.getActiveNotifications()).thenReturn(
183 Lists.newArrayList(entry0, entry1));
184
185 // Set up group manager to report a suppressed summary now.
186 when(mGroupManager.isChildInGroupWithSummary(entry0.notification)).thenReturn(false);
187 when(mGroupManager.isChildInGroupWithSummary(entry1.notification)).thenReturn(false);
188 when(mGroupManager.isSummaryOfSuppressedGroup(entry0.notification)).thenReturn(true);
189
190 // Run updateNotifications - the view hierarchy should be reorganized.
191 mViewHierarchyManager.updateNotificationViews();
192
193 verify(mListContainer).notifyGroupChildRemoved(
Evan Laird94492852018-10-25 13:43:01 -0400194 entry1.getRow(), entry0.getRow().getChildrenContainer());
195 assertTrue(Lists.newArrayList(entry0.getRow(), entry1.getRow()).equals(mListContainer.mRows));
196 assertEquals(View.GONE, entry0.getRow().getVisibility());
197 assertEquals(View.VISIBLE, entry1.getRow().getVisibility());
Eliot Courtney2b4c3a02017-11-27 13:27:46 +0900198 }
199
Julia Reynoldsfc640012018-02-21 12:25:27 -0500200 @Test
201 public void testUpdateNotificationViews_appOps() throws Exception {
Ned Burnsf81c4c42019-01-07 14:10:43 -0500202 NotificationEntry entry0 = createEntry();
Evan Laird94492852018-10-25 13:43:01 -0400203 entry0.setRow(spy(entry0.getRow()));
Julia Reynoldsfc640012018-02-21 12:25:27 -0500204 when(mNotificationData.getActiveNotifications()).thenReturn(
205 Lists.newArrayList(entry0));
Evan Laird94492852018-10-25 13:43:01 -0400206 mListContainer.addContainerView(entry0.getRow());
Julia Reynoldsfc640012018-02-21 12:25:27 -0500207
208 mViewHierarchyManager.updateNotificationViews();
209
Evan Laird94492852018-10-25 13:43:01 -0400210 verify(entry0.getRow(), times(1)).showAppOpsIcons(any());
Julia Reynoldsfc640012018-02-21 12:25:27 -0500211 }
212
Eliot Courtney2b4c3a02017-11-27 13:27:46 +0900213 private class FakeListContainer implements NotificationListContainer {
214 final LinearLayout mLayout = new LinearLayout(mContext);
215 final List<View> mRows = Lists.newArrayList();
216
217 @Override
218 public void setChildTransferInProgress(boolean childTransferInProgress) {}
219
220 @Override
Dave Mankoffa4d195d2018-11-16 13:33:27 -0500221 public void changeViewPosition(ExpandableView child, int newIndex) {
Eliot Courtney2b4c3a02017-11-27 13:27:46 +0900222 mRows.remove(child);
223 mRows.add(newIndex, child);
224 }
225
226 @Override
Dave Mankoffa4d195d2018-11-16 13:33:27 -0500227 public void notifyGroupChildAdded(ExpandableView row) {}
Eliot Courtney2b4c3a02017-11-27 13:27:46 +0900228
229 @Override
Dave Mankoffa4d195d2018-11-16 13:33:27 -0500230 public void notifyGroupChildRemoved(ExpandableView row, ViewGroup childrenContainer) {}
Eliot Courtney2b4c3a02017-11-27 13:27:46 +0900231
232 @Override
Dave Mankoffa4d195d2018-11-16 13:33:27 -0500233 public void generateAddAnimation(ExpandableView child, boolean fromMoreCard) {}
Eliot Courtney2b4c3a02017-11-27 13:27:46 +0900234
235 @Override
236 public void generateChildOrderChangedEvent() {}
237
238 @Override
Aaron Heuckrothcd944dc2018-10-01 16:31:08 -0400239 public void onReset(ExpandableView view) {}
240
241 @Override
Eliot Courtney2b4c3a02017-11-27 13:27:46 +0900242 public int getContainerChildCount() {
243 return mRows.size();
244 }
245
246 @Override
247 public View getContainerChildAt(int i) {
248 return mRows.get(i);
249 }
250
251 @Override
252 public void removeContainerView(View v) {
253 mLayout.removeView(v);
254 mRows.remove(v);
255 }
256
257 @Override
258 public void addContainerView(View v) {
259 mLayout.addView(v);
260 mRows.add(v);
261 }
262
263 @Override
264 public void setMaxDisplayedNotifications(int maxNotifications) {}
265
266 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500267 public ViewGroup getViewParentForNotification(NotificationEntry entry) {
Eliot Courtney2b4c3a02017-11-27 13:27:46 +0900268 return null;
269 }
270
271 @Override
272 public void onHeightChanged(ExpandableView view, boolean animate) {}
273
274 @Override
275 public void resetExposedMenuView(boolean animate, boolean force) {}
276
277 @Override
278 public NotificationSwipeActionHelper getSwipeActionHelper() {
279 return null;
280 }
281
282 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500283 public void cleanUpViewStateForEntry(NotificationEntry entry) { }
Eliot Courtney2b4c3a02017-11-27 13:27:46 +0900284
285 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500286 public boolean isInVisibleLocation(NotificationEntry entry) {
Eliot Courtney2b4c3a02017-11-27 13:27:46 +0900287 return true;
288 }
289
290 @Override
291 public void setChildLocationsChangedListener(
292 NotificationLogger.OnChildLocationsChangedListener listener) {}
293
294 @Override
295 public boolean hasPulsingNotifications() {
296 return false;
297 }
Evan Laird94492852018-10-25 13:43:01 -0400298
Eliot Courtney2b4c3a02017-11-27 13:27:46 +0900299 }
300}