blob: 04d7509f760f9561d03ff5c7db53f0f58f3a183e [file] [log] [blame]
Jason Monkde850bb2017-02-01 19:26:30 -05001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11 * KIND, either express or implied. See the License for the specific language governing
12 * permissions and limitations under the License.
13 */
14
Rohan Shah20790b82018-07-02 17:21:04 -070015package com.android.systemui.statusbar.notification.row;
Jason Monkde850bb2017-02-01 19:26:30 -050016
Julia Reynoldsb5867452018-02-28 16:31:35 -050017import static junit.framework.Assert.assertEquals;
Aaron Heuckroth266cb342018-09-07 14:52:04 -040018import static junit.framework.Assert.assertFalse;
Mady Mellor4c197602017-04-10 17:57:52 -070019import static junit.framework.Assert.assertTrue;
20
Aaron Heuckroth96cd93f2018-11-20 11:14:40 -050021import static org.mockito.Mockito.doNothing;
Aaron Heuckroth266cb342018-09-07 14:52:04 -040022import static org.mockito.Mockito.doReturn;
Julia Reynoldsb5867452018-02-28 16:31:35 -050023import static org.mockito.Mockito.mock;
Aaron Heuckroth266cb342018-09-07 14:52:04 -040024import static org.mockito.Mockito.times;
25import static org.mockito.Mockito.verify;
Julia Reynoldsb5867452018-02-28 16:31:35 -050026import static org.mockito.Mockito.when;
27
Gus Prevas9b2348b2018-11-28 15:47:27 -050028import android.app.NotificationChannel;
Julia Reynoldsb5867452018-02-28 16:31:35 -050029import android.service.notification.StatusBarNotification;
Jason Monkfba8faf2017-05-23 10:42:59 -040030import android.support.test.filters.SmallTest;
Jason Monk340b0e52017-03-08 14:57:56 -050031import android.testing.AndroidTestingRunner;
32import android.testing.TestableLooper;
33import android.testing.TestableLooper.RunWithLooper;
34import android.testing.ViewUtils;
Julia Reynoldsb5867452018-02-28 16:31:35 -050035import android.view.ViewGroup;
36
Mady Mellor95d743c2017-01-10 12:05:27 -080037import com.android.systemui.plugins.statusbar.NotificationMenuRowPlugin;
Ned Burnsf81c4c42019-01-07 14:10:43 -050038import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Jason Monkde850bb2017-02-01 19:26:30 -050039import com.android.systemui.utils.leaks.LeakCheckedTest;
40
41import org.junit.Before;
42import org.junit.Test;
Jason Monk28d5d222017-02-02 13:08:31 -050043import org.junit.runner.RunWith;
Aaron Heuckroth266cb342018-09-07 14:52:04 -040044import org.mockito.Mockito;
Jason Monkde850bb2017-02-01 19:26:30 -050045
Jason Monk340b0e52017-03-08 14:57:56 -050046@RunWith(AndroidTestingRunner.class)
Jason Monka716bac2018-12-05 15:48:21 -050047@RunWithLooper
Jason Monkfba8faf2017-05-23 10:42:59 -040048@SmallTest
Jason Monkde850bb2017-02-01 19:26:30 -050049public class NotificationMenuRowTest extends LeakCheckedTest {
50
Gus Prevas9b2348b2018-11-28 15:47:27 -050051 private ExpandableNotificationRow mRow;
52
Jason Monkde850bb2017-02-01 19:26:30 -050053 @Before
54 public void setup() {
55 injectLeakCheckedDependencies(ALL_SUPPORTED_CLASSES);
Gus Prevas9b2348b2018-11-28 15:47:27 -050056 mRow = mock(ExpandableNotificationRow.class);
Ned Burnsf81c4c42019-01-07 14:10:43 -050057 NotificationEntry entry = new NotificationEntry(
Gus Prevas9b2348b2018-11-28 15:47:27 -050058 mock(StatusBarNotification.class));
59 entry.channel = mock(NotificationChannel.class);
60 when(mRow.getEntry()).thenReturn(entry);
Jason Monkde850bb2017-02-01 19:26:30 -050061 }
62
63 @Test
64 public void testAttachDetach() {
Mady Mellor95d743c2017-01-10 12:05:27 -080065 NotificationMenuRowPlugin row = new NotificationMenuRow(mContext);
Gus Prevas9b2348b2018-11-28 15:47:27 -050066 row.createMenu(mRow, null);
Mady Mellor95d743c2017-01-10 12:05:27 -080067 ViewUtils.attachView(row.getMenuView());
Jason Monk28d5d222017-02-02 13:08:31 -050068 TestableLooper.get(this).processAllMessages();
Mady Mellor95d743c2017-01-10 12:05:27 -080069 ViewUtils.detachView(row.getMenuView());
Jason Monk28d5d222017-02-02 13:08:31 -050070 TestableLooper.get(this).processAllMessages();
Jason Monkde850bb2017-02-01 19:26:30 -050071 }
Mady Mellor0df26102017-04-03 11:50:44 -070072
73 @Test
74 public void testRecreateMenu() {
75 NotificationMenuRowPlugin row = new NotificationMenuRow(mContext);
Gus Prevas9b2348b2018-11-28 15:47:27 -050076 row.createMenu(mRow, null);
Mady Mellor4c197602017-04-10 17:57:52 -070077 assertTrue(row.getMenuView() != null);
Gus Prevas9b2348b2018-11-28 15:47:27 -050078 row.createMenu(mRow, null);
Mady Mellor4c197602017-04-10 17:57:52 -070079 assertTrue(row.getMenuView() != null);
Mady Mellor0df26102017-04-03 11:50:44 -070080 }
Mady Mellor0b3f0042017-06-20 17:18:53 -070081
82 @Test
83 public void testResetUncreatedMenu() {
84 NotificationMenuRowPlugin row = new NotificationMenuRow(mContext);
85 row.resetMenu();
86 }
Julia Reynoldsb5867452018-02-28 16:31:35 -050087
Aaron Heuckroth266cb342018-09-07 14:52:04 -040088
Julia Reynoldsb5867452018-02-28 16:31:35 -050089 @Test
90 public void testNoAppOpsInSlowSwipe() {
91 NotificationMenuRow row = new NotificationMenuRow(mContext);
Gus Prevas9b2348b2018-11-28 15:47:27 -050092 row.createMenu(mRow, null);
Julia Reynoldsb5867452018-02-28 16:31:35 -050093
94 ViewGroup container = (ViewGroup) row.getMenuView();
95 // one for snooze and one for noti blocking
96 assertEquals(2, container.getChildCount());
97 }
Aaron Heuckroth266cb342018-09-07 14:52:04 -040098
99 @Test
100 public void testIsSnappedAndOnSameSide() {
101 NotificationMenuRow row = Mockito.spy(new NotificationMenuRow((mContext)));
102
103 when(row.isMenuVisible()).thenReturn(true);
104 when(row.isMenuSnapped()).thenReturn(true);
105 when(row.isMenuOnLeft()).thenReturn(true);
106 when(row.isMenuSnappedOnLeft()).thenReturn(true);
107
108 assertTrue("Showing on left and on left", row.isSnappedAndOnSameSide());
109
110
111 when(row.isMenuOnLeft()).thenReturn(false);
112 when(row.isMenuSnappedOnLeft()).thenReturn(false);
113 assertTrue("Snapped to right and on right", row.isSnappedAndOnSameSide());
114
115 when(row.isMenuOnLeft()).thenReturn(true);
116 when(row.isMenuSnapped()).thenReturn(false);
117 assertFalse("Snapped to right and on left", row.isSnappedAndOnSameSide());
118
119 when(row.isMenuOnLeft()).thenReturn(true);
120 when(row.isMenuSnappedOnLeft()).thenReturn(true);
121 when(row.isMenuVisible()).thenReturn(false);
122 assertFalse("Snapped to left and on left, but menu not visible",
123 row.isSnappedAndOnSameSide());
124
125 when(row.isMenuVisible()).thenReturn(true);
126 when(row.isMenuSnapped()).thenReturn(false);
127 assertFalse("Snapped to left and on left, but not actually snapped to",
128 row.isSnappedAndOnSameSide());
129 }
130
131 @Test
132 public void testGetMenuSnapTarget() {
133 NotificationMenuRow row = Mockito.spy(new NotificationMenuRow((mContext)));
134 when(row.isMenuOnLeft()).thenReturn(true);
135 doReturn(30).when(row).getSpaceForMenu();
136
137 assertEquals("When on left, snap target is space for menu",
138 30, row.getMenuSnapTarget());
139
140 when(row.isMenuOnLeft()).thenReturn(false);
141 assertEquals("When on right, snap target is negative space for menu",
142 -30, row.getMenuSnapTarget());
143 }
144
145 @Test
146 public void testIsSwipedEnoughToShowMenu() {
147 NotificationMenuRow row = Mockito.spy(new NotificationMenuRow((mContext)));
148 when(row.isMenuVisible()).thenReturn(true);
149 when(row.isMenuOnLeft()).thenReturn(true);
150 doReturn(40f).when(row).getMinimumSwipeDistance();
151
152 when(row.getTranslation()).thenReturn(30f);
153 assertFalse("on left, translation is less than min", row.isSwipedEnoughToShowMenu());
154
155 when(row.getTranslation()).thenReturn(50f);
156 assertTrue("on left, translation is greater than min", row.isSwipedEnoughToShowMenu());
157
158 when(row.isMenuOnLeft()).thenReturn(false);
159 when(row.getTranslation()).thenReturn(-30f);
160 assertFalse("on right, translation is greater than -min", row.isSwipedEnoughToShowMenu());
161
162 when(row.getTranslation()).thenReturn(-50f);
163 assertTrue("on right, translation is less than -min", row.isSwipedEnoughToShowMenu());
164
165 when(row.isMenuVisible()).thenReturn(false);
166 when(row.getTranslation()).thenReturn(30f);
167 assertFalse("on left, translation greater than min, but not visible",
168 row.isSwipedEnoughToShowMenu());
169 }
170
171 @Test
172 public void testIsWithinSnapMenuThreshold() {
173 NotificationMenuRow row = Mockito.spy(new NotificationMenuRow((mContext)));
174 doReturn(30f).when(row).getSnapBackThreshold();
175 doReturn(50f).when(row).getDismissThreshold();
176
177 when(row.isMenuOnLeft()).thenReturn(true);
178 when(row.getTranslation()).thenReturn(40f);
179 assertTrue("When on left, translation is between min and max",
180 row.isWithinSnapMenuThreshold());
181
182 when(row.getTranslation()).thenReturn(20f);
183 assertFalse("When on left, translation is less than min",
184 row.isWithinSnapMenuThreshold());
185
186 when(row.getTranslation()).thenReturn(60f);
187 assertFalse("When on left, translation is greater than max",
188 row.isWithinSnapMenuThreshold());
189
190 when(row.isMenuOnLeft()).thenReturn(false);
191 when(row.getTranslation()).thenReturn(-40f);
192 assertTrue("When on right, translation is between -min and -max",
193 row.isWithinSnapMenuThreshold());
194
195 when(row.getTranslation()).thenReturn(-20f);
196 assertFalse("When on right, translation is greater than -min",
197 row.isWithinSnapMenuThreshold());
198
199 when(row.getTranslation()).thenReturn(-60f);
200 assertFalse("When on right, translation is less than -max",
201 row.isWithinSnapMenuThreshold());
202 }
203
204 @Test
205 public void testShouldSnapBack() {
206 NotificationMenuRow row = Mockito.spy(new NotificationMenuRow((mContext)));
207 doReturn(40f).when(row).getSnapBackThreshold();
208 when(row.isMenuVisible()).thenReturn(false);
209 when(row.isMenuOnLeft()).thenReturn(true);
210
211 when(row.getTranslation()).thenReturn(50f);
212 assertFalse("On left, translation greater than minimum target", row.shouldSnapBack());
213
214 when(row.getTranslation()).thenReturn(30f);
215 assertTrue("On left, translation less than minimum target", row.shouldSnapBack());
216
217 when(row.isMenuOnLeft()).thenReturn(false);
218 when(row.getTranslation()).thenReturn(-50f);
219 assertFalse("On right, translation less than minimum target", row.shouldSnapBack());
220
221 when(row.getTranslation()).thenReturn(-30f);
222 assertTrue("On right, translation greater than minimum target", row.shouldSnapBack());
223 }
224
225 @Test
226 public void testCanBeDismissed() {
227 NotificationMenuRow row = Mockito.spy(new NotificationMenuRow((mContext)));
228 ExpandableNotificationRow parent = mock(ExpandableNotificationRow.class);
229
230 when(row.getParent()).thenReturn(parent);
231 when(parent.canViewBeDismissed()).thenReturn(true);
232
233 assertTrue("Row can be dismissed if parent can be dismissed", row.canBeDismissed());
234
235 when(parent.canViewBeDismissed()).thenReturn(false);
236 assertFalse("Row cannot be dismissed if parent cannot be dismissed",
237 row.canBeDismissed());
238 }
239
240 @Test
241 public void testIsTowardsMenu() {
242 NotificationMenuRow row = Mockito.spy(new NotificationMenuRow((mContext)));
243 when(row.isMenuVisible()).thenReturn(true);
244 when(row.isMenuOnLeft()).thenReturn(true);
245
246 assertTrue("menu on left, movement is negative", row.isTowardsMenu(-30f));
247 assertFalse("menu on left, movement is positive", row.isTowardsMenu(30f));
248 assertTrue("menu on left, movement is 0", row.isTowardsMenu(0f));
249
250 when(row.isMenuOnLeft()).thenReturn(false);
251 assertTrue("menu on right, movement is positive", row.isTowardsMenu(30f));
252 assertFalse("menu on right, movement is negative", row.isTowardsMenu(-30f));
253 assertTrue("menu on right, movement is 0", row.isTowardsMenu(0f));
254
255 when(row.isMenuVisible()).thenReturn(false);
256 assertFalse("menu on left, movement is negative, but menu not visible",
257 row.isTowardsMenu(-30f));
258 }
259
260 @Test
261 public void onSnapBack() {
262 NotificationMenuRow row = Mockito.spy(new NotificationMenuRow((mContext)));
263 NotificationMenuRowPlugin.OnMenuEventListener listener = mock(NotificationMenuRowPlugin
264 .OnMenuEventListener.class);
265 row.setMenuClickListener(listener);
266 ExpandableNotificationRow parent = mock(ExpandableNotificationRow.class);
267 when(row.getParent()).thenReturn(parent);
268 doNothing().when(row).cancelDrag();
269
270 row.onSnapOpen();
271
272 assertTrue("before onSnapClosed, row is snapped to", row.isMenuSnapped());
273 assertFalse("before onSnapClosed, row is not snapping", row.isSnapping());
274
275 row.onSnapClosed();
276
277 assertFalse("after onSnapClosed, row is not snapped to", row.isMenuSnapped());
278 assertTrue("after onSnapClosed, row is snapping", row.isSnapping());
279 }
280
281 @Test
282 public void testOnSnap() {
283 NotificationMenuRow row = Mockito.spy(new NotificationMenuRow((mContext)));
284 when(row.isMenuOnLeft()).thenReturn(true);
285 NotificationMenuRowPlugin.OnMenuEventListener listener = mock(NotificationMenuRowPlugin
286 .OnMenuEventListener.class);
287 row.setMenuClickListener(listener);
288 ExpandableNotificationRow parent = mock(ExpandableNotificationRow.class);
289 when(row.getParent()).thenReturn(parent);
290
291 assertFalse("before onSnapOpen, row is not snapped to", row.isMenuSnapped());
292 assertFalse("before onSnapOpen, row is not snapped on left", row.isMenuSnappedOnLeft());
293
294 row.onSnapOpen();
295
296 assertTrue("after onSnapOpen, row is snapped to", row.isMenuSnapped());
297 assertTrue("after onSnapOpen, row is snapped on left", row.isMenuSnapped());
298 verify(listener, times(1)).onMenuShown(parent);
299 }
300
301 @Test
302 public void testOnDismiss() {
303 NotificationMenuRow row = Mockito.spy(new NotificationMenuRow((mContext)));
304 doNothing().when(row).cancelDrag();
305 row.onSnapOpen();
306
307 assertFalse("before onDismiss, row is not dismissing", row.isDismissing());
308 assertTrue("before onDismiss, row is showing", row.isMenuSnapped());
309
310 row.onDismiss();
311
312 verify(row, times(1)).cancelDrag();
313 assertTrue("after onDismiss, row is dismissing", row.isDismissing());
314 assertFalse("after onDismiss, row is not showing", row.isMenuSnapped());
315 }
316
317 @Test
318 public void testOnDown() {
319 NotificationMenuRow row = Mockito.spy(new NotificationMenuRow((mContext)));
320 doNothing().when(row).beginDrag();
321
322 row.onTouchStart();
323
324 verify(row, times(1)).beginDrag();
325 }
326
327 @Test
328 public void testOnUp() {
329 NotificationMenuRow row = Mockito.spy(new NotificationMenuRow((mContext)));
330 row.onTouchStart();
331
332 assertTrue("before onTouchEnd, isUserTouching is true", row.isUserTouching());
333
334 row.onTouchEnd();
335
336 assertFalse("after onTouchEnd, isUserTouching is false", row.isUserTouching());
337 }
338
339 @Test
340 public void testIsMenuVisible() {
341 NotificationMenuRow row = Mockito.spy(new NotificationMenuRow((mContext)));
342 row.setMenuAlpha(0);
343
344 assertFalse("when alpha is 0, menu is not visible", row.isMenuVisible());
345
346 row.setMenuAlpha(0.5f);
347 assertTrue("when alpha is .5, menu is visible", row.isMenuVisible());
348 }
Jason Monkde850bb2017-02-01 19:26:30 -0500349}