blob: e6389c4adb769d439b55f4b55cf7a153c5a78cea [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 Monk340b0e52017-03-08 14:57:56 -050030import android.testing.AndroidTestingRunner;
31import android.testing.TestableLooper;
32import android.testing.TestableLooper.RunWithLooper;
33import android.testing.ViewUtils;
Julia Reynoldsb5867452018-02-28 16:31:35 -050034import android.view.ViewGroup;
35
Brett Chabot84151d92019-02-27 15:37:59 -080036import androidx.test.filters.SmallTest;
37
Mady Mellor95d743c2017-01-10 12:05:27 -080038import com.android.systemui.plugins.statusbar.NotificationMenuRowPlugin;
Ned Burnsf81c4c42019-01-07 14:10:43 -050039import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Jason Monkde850bb2017-02-01 19:26:30 -050040import com.android.systemui.utils.leaks.LeakCheckedTest;
41
42import org.junit.Before;
43import org.junit.Test;
Jason Monk28d5d222017-02-02 13:08:31 -050044import org.junit.runner.RunWith;
Aaron Heuckroth266cb342018-09-07 14:52:04 -040045import org.mockito.Mockito;
Jason Monkde850bb2017-02-01 19:26:30 -050046
Jason Monk340b0e52017-03-08 14:57:56 -050047@RunWith(AndroidTestingRunner.class)
Jason Monka716bac2018-12-05 15:48:21 -050048@RunWithLooper
Jason Monkfba8faf2017-05-23 10:42:59 -040049@SmallTest
Jason Monkde850bb2017-02-01 19:26:30 -050050public class NotificationMenuRowTest extends LeakCheckedTest {
51
Gus Prevas9b2348b2018-11-28 15:47:27 -050052 private ExpandableNotificationRow mRow;
53
Jason Monkde850bb2017-02-01 19:26:30 -050054 @Before
55 public void setup() {
56 injectLeakCheckedDependencies(ALL_SUPPORTED_CLASSES);
Gus Prevas9b2348b2018-11-28 15:47:27 -050057 mRow = mock(ExpandableNotificationRow.class);
Ned Burnsf81c4c42019-01-07 14:10:43 -050058 NotificationEntry entry = new NotificationEntry(
Gus Prevas9b2348b2018-11-28 15:47:27 -050059 mock(StatusBarNotification.class));
60 entry.channel = mock(NotificationChannel.class);
61 when(mRow.getEntry()).thenReturn(entry);
Jason Monkde850bb2017-02-01 19:26:30 -050062 }
63
64 @Test
65 public void testAttachDetach() {
Mady Mellor95d743c2017-01-10 12:05:27 -080066 NotificationMenuRowPlugin row = new NotificationMenuRow(mContext);
Gus Prevas9b2348b2018-11-28 15:47:27 -050067 row.createMenu(mRow, null);
Mady Mellor95d743c2017-01-10 12:05:27 -080068 ViewUtils.attachView(row.getMenuView());
Jason Monk28d5d222017-02-02 13:08:31 -050069 TestableLooper.get(this).processAllMessages();
Mady Mellor95d743c2017-01-10 12:05:27 -080070 ViewUtils.detachView(row.getMenuView());
Jason Monk28d5d222017-02-02 13:08:31 -050071 TestableLooper.get(this).processAllMessages();
Jason Monkde850bb2017-02-01 19:26:30 -050072 }
Mady Mellor0df26102017-04-03 11:50:44 -070073
74 @Test
75 public void testRecreateMenu() {
76 NotificationMenuRowPlugin row = new NotificationMenuRow(mContext);
Gus Prevas9b2348b2018-11-28 15:47:27 -050077 row.createMenu(mRow, null);
Mady Mellor4c197602017-04-10 17:57:52 -070078 assertTrue(row.getMenuView() != null);
Gus Prevas9b2348b2018-11-28 15:47:27 -050079 row.createMenu(mRow, null);
Mady Mellor4c197602017-04-10 17:57:52 -070080 assertTrue(row.getMenuView() != null);
Mady Mellor0df26102017-04-03 11:50:44 -070081 }
Mady Mellor0b3f0042017-06-20 17:18:53 -070082
83 @Test
84 public void testResetUncreatedMenu() {
85 NotificationMenuRowPlugin row = new NotificationMenuRow(mContext);
86 row.resetMenu();
87 }
Julia Reynoldsb5867452018-02-28 16:31:35 -050088
Aaron Heuckroth266cb342018-09-07 14:52:04 -040089
Julia Reynoldsb5867452018-02-28 16:31:35 -050090 @Test
91 public void testNoAppOpsInSlowSwipe() {
92 NotificationMenuRow row = new NotificationMenuRow(mContext);
Gus Prevas9b2348b2018-11-28 15:47:27 -050093 row.createMenu(mRow, null);
Julia Reynoldsb5867452018-02-28 16:31:35 -050094
95 ViewGroup container = (ViewGroup) row.getMenuView();
96 // one for snooze and one for noti blocking
97 assertEquals(2, container.getChildCount());
98 }
Aaron Heuckroth266cb342018-09-07 14:52:04 -040099
100 @Test
101 public void testIsSnappedAndOnSameSide() {
102 NotificationMenuRow row = Mockito.spy(new NotificationMenuRow((mContext)));
103
104 when(row.isMenuVisible()).thenReturn(true);
105 when(row.isMenuSnapped()).thenReturn(true);
106 when(row.isMenuOnLeft()).thenReturn(true);
107 when(row.isMenuSnappedOnLeft()).thenReturn(true);
108
109 assertTrue("Showing on left and on left", row.isSnappedAndOnSameSide());
110
111
112 when(row.isMenuOnLeft()).thenReturn(false);
113 when(row.isMenuSnappedOnLeft()).thenReturn(false);
114 assertTrue("Snapped to right and on right", row.isSnappedAndOnSameSide());
115
116 when(row.isMenuOnLeft()).thenReturn(true);
117 when(row.isMenuSnapped()).thenReturn(false);
118 assertFalse("Snapped to right and on left", row.isSnappedAndOnSameSide());
119
120 when(row.isMenuOnLeft()).thenReturn(true);
121 when(row.isMenuSnappedOnLeft()).thenReturn(true);
122 when(row.isMenuVisible()).thenReturn(false);
123 assertFalse("Snapped to left and on left, but menu not visible",
124 row.isSnappedAndOnSameSide());
125
126 when(row.isMenuVisible()).thenReturn(true);
127 when(row.isMenuSnapped()).thenReturn(false);
128 assertFalse("Snapped to left and on left, but not actually snapped to",
129 row.isSnappedAndOnSameSide());
130 }
131
132 @Test
133 public void testGetMenuSnapTarget() {
134 NotificationMenuRow row = Mockito.spy(new NotificationMenuRow((mContext)));
135 when(row.isMenuOnLeft()).thenReturn(true);
136 doReturn(30).when(row).getSpaceForMenu();
137
138 assertEquals("When on left, snap target is space for menu",
139 30, row.getMenuSnapTarget());
140
141 when(row.isMenuOnLeft()).thenReturn(false);
142 assertEquals("When on right, snap target is negative space for menu",
143 -30, row.getMenuSnapTarget());
144 }
145
146 @Test
147 public void testIsSwipedEnoughToShowMenu() {
148 NotificationMenuRow row = Mockito.spy(new NotificationMenuRow((mContext)));
149 when(row.isMenuVisible()).thenReturn(true);
150 when(row.isMenuOnLeft()).thenReturn(true);
151 doReturn(40f).when(row).getMinimumSwipeDistance();
152
153 when(row.getTranslation()).thenReturn(30f);
154 assertFalse("on left, translation is less than min", row.isSwipedEnoughToShowMenu());
155
156 when(row.getTranslation()).thenReturn(50f);
157 assertTrue("on left, translation is greater than min", row.isSwipedEnoughToShowMenu());
158
159 when(row.isMenuOnLeft()).thenReturn(false);
160 when(row.getTranslation()).thenReturn(-30f);
161 assertFalse("on right, translation is greater than -min", row.isSwipedEnoughToShowMenu());
162
163 when(row.getTranslation()).thenReturn(-50f);
164 assertTrue("on right, translation is less than -min", row.isSwipedEnoughToShowMenu());
165
166 when(row.isMenuVisible()).thenReturn(false);
167 when(row.getTranslation()).thenReturn(30f);
168 assertFalse("on left, translation greater than min, but not visible",
169 row.isSwipedEnoughToShowMenu());
170 }
171
172 @Test
173 public void testIsWithinSnapMenuThreshold() {
174 NotificationMenuRow row = Mockito.spy(new NotificationMenuRow((mContext)));
175 doReturn(30f).when(row).getSnapBackThreshold();
176 doReturn(50f).when(row).getDismissThreshold();
177
178 when(row.isMenuOnLeft()).thenReturn(true);
179 when(row.getTranslation()).thenReturn(40f);
180 assertTrue("When on left, translation is between min and max",
181 row.isWithinSnapMenuThreshold());
182
183 when(row.getTranslation()).thenReturn(20f);
184 assertFalse("When on left, translation is less than min",
185 row.isWithinSnapMenuThreshold());
186
187 when(row.getTranslation()).thenReturn(60f);
188 assertFalse("When on left, translation is greater than max",
189 row.isWithinSnapMenuThreshold());
190
191 when(row.isMenuOnLeft()).thenReturn(false);
192 when(row.getTranslation()).thenReturn(-40f);
193 assertTrue("When on right, translation is between -min and -max",
194 row.isWithinSnapMenuThreshold());
195
196 when(row.getTranslation()).thenReturn(-20f);
197 assertFalse("When on right, translation is greater than -min",
198 row.isWithinSnapMenuThreshold());
199
200 when(row.getTranslation()).thenReturn(-60f);
201 assertFalse("When on right, translation is less than -max",
202 row.isWithinSnapMenuThreshold());
203 }
204
205 @Test
206 public void testShouldSnapBack() {
207 NotificationMenuRow row = Mockito.spy(new NotificationMenuRow((mContext)));
208 doReturn(40f).when(row).getSnapBackThreshold();
209 when(row.isMenuVisible()).thenReturn(false);
210 when(row.isMenuOnLeft()).thenReturn(true);
211
212 when(row.getTranslation()).thenReturn(50f);
213 assertFalse("On left, translation greater than minimum target", row.shouldSnapBack());
214
215 when(row.getTranslation()).thenReturn(30f);
216 assertTrue("On left, translation less than minimum target", row.shouldSnapBack());
217
218 when(row.isMenuOnLeft()).thenReturn(false);
219 when(row.getTranslation()).thenReturn(-50f);
220 assertFalse("On right, translation less than minimum target", row.shouldSnapBack());
221
222 when(row.getTranslation()).thenReturn(-30f);
223 assertTrue("On right, translation greater than minimum target", row.shouldSnapBack());
224 }
225
226 @Test
227 public void testCanBeDismissed() {
228 NotificationMenuRow row = Mockito.spy(new NotificationMenuRow((mContext)));
229 ExpandableNotificationRow parent = mock(ExpandableNotificationRow.class);
230
231 when(row.getParent()).thenReturn(parent);
232 when(parent.canViewBeDismissed()).thenReturn(true);
233
234 assertTrue("Row can be dismissed if parent can be dismissed", row.canBeDismissed());
235
236 when(parent.canViewBeDismissed()).thenReturn(false);
237 assertFalse("Row cannot be dismissed if parent cannot be dismissed",
238 row.canBeDismissed());
239 }
240
241 @Test
242 public void testIsTowardsMenu() {
243 NotificationMenuRow row = Mockito.spy(new NotificationMenuRow((mContext)));
244 when(row.isMenuVisible()).thenReturn(true);
245 when(row.isMenuOnLeft()).thenReturn(true);
246
247 assertTrue("menu on left, movement is negative", row.isTowardsMenu(-30f));
248 assertFalse("menu on left, movement is positive", row.isTowardsMenu(30f));
249 assertTrue("menu on left, movement is 0", row.isTowardsMenu(0f));
250
251 when(row.isMenuOnLeft()).thenReturn(false);
252 assertTrue("menu on right, movement is positive", row.isTowardsMenu(30f));
253 assertFalse("menu on right, movement is negative", row.isTowardsMenu(-30f));
254 assertTrue("menu on right, movement is 0", row.isTowardsMenu(0f));
255
256 when(row.isMenuVisible()).thenReturn(false);
257 assertFalse("menu on left, movement is negative, but menu not visible",
258 row.isTowardsMenu(-30f));
259 }
260
261 @Test
262 public void onSnapBack() {
263 NotificationMenuRow row = Mockito.spy(new NotificationMenuRow((mContext)));
264 NotificationMenuRowPlugin.OnMenuEventListener listener = mock(NotificationMenuRowPlugin
265 .OnMenuEventListener.class);
266 row.setMenuClickListener(listener);
267 ExpandableNotificationRow parent = mock(ExpandableNotificationRow.class);
268 when(row.getParent()).thenReturn(parent);
269 doNothing().when(row).cancelDrag();
270
271 row.onSnapOpen();
272
273 assertTrue("before onSnapClosed, row is snapped to", row.isMenuSnapped());
274 assertFalse("before onSnapClosed, row is not snapping", row.isSnapping());
275
276 row.onSnapClosed();
277
278 assertFalse("after onSnapClosed, row is not snapped to", row.isMenuSnapped());
279 assertTrue("after onSnapClosed, row is snapping", row.isSnapping());
280 }
281
282 @Test
283 public void testOnSnap() {
284 NotificationMenuRow row = Mockito.spy(new NotificationMenuRow((mContext)));
285 when(row.isMenuOnLeft()).thenReturn(true);
286 NotificationMenuRowPlugin.OnMenuEventListener listener = mock(NotificationMenuRowPlugin
287 .OnMenuEventListener.class);
288 row.setMenuClickListener(listener);
289 ExpandableNotificationRow parent = mock(ExpandableNotificationRow.class);
290 when(row.getParent()).thenReturn(parent);
291
292 assertFalse("before onSnapOpen, row is not snapped to", row.isMenuSnapped());
293 assertFalse("before onSnapOpen, row is not snapped on left", row.isMenuSnappedOnLeft());
294
295 row.onSnapOpen();
296
297 assertTrue("after onSnapOpen, row is snapped to", row.isMenuSnapped());
298 assertTrue("after onSnapOpen, row is snapped on left", row.isMenuSnapped());
299 verify(listener, times(1)).onMenuShown(parent);
300 }
301
302 @Test
303 public void testOnDismiss() {
304 NotificationMenuRow row = Mockito.spy(new NotificationMenuRow((mContext)));
305 doNothing().when(row).cancelDrag();
306 row.onSnapOpen();
307
308 assertFalse("before onDismiss, row is not dismissing", row.isDismissing());
309 assertTrue("before onDismiss, row is showing", row.isMenuSnapped());
310
311 row.onDismiss();
312
313 verify(row, times(1)).cancelDrag();
314 assertTrue("after onDismiss, row is dismissing", row.isDismissing());
315 assertFalse("after onDismiss, row is not showing", row.isMenuSnapped());
316 }
317
318 @Test
319 public void testOnDown() {
320 NotificationMenuRow row = Mockito.spy(new NotificationMenuRow((mContext)));
321 doNothing().when(row).beginDrag();
322
323 row.onTouchStart();
324
325 verify(row, times(1)).beginDrag();
326 }
327
328 @Test
329 public void testOnUp() {
330 NotificationMenuRow row = Mockito.spy(new NotificationMenuRow((mContext)));
331 row.onTouchStart();
332
333 assertTrue("before onTouchEnd, isUserTouching is true", row.isUserTouching());
334
335 row.onTouchEnd();
336
337 assertFalse("after onTouchEnd, isUserTouching is false", row.isUserTouching());
338 }
339
340 @Test
341 public void testIsMenuVisible() {
342 NotificationMenuRow row = Mockito.spy(new NotificationMenuRow((mContext)));
343 row.setMenuAlpha(0);
344
345 assertFalse("when alpha is 0, menu is not visible", row.isMenuVisible());
346
347 row.setMenuAlpha(0.5f);
348 assertTrue("when alpha is .5, menu is visible", row.isMenuVisible());
349 }
Jason Monkde850bb2017-02-01 19:26:30 -0500350}