Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 1 | /* |
| 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 Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 15 | package com.android.systemui.statusbar.notification.row; |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 16 | |
Julia Reynolds | b586745 | 2018-02-28 16:31:35 -0500 | [diff] [blame] | 17 | import static junit.framework.Assert.assertEquals; |
Aaron Heuckroth | 266cb34 | 2018-09-07 14:52:04 -0400 | [diff] [blame] | 18 | import static junit.framework.Assert.assertFalse; |
Mady Mellor | 4c19760 | 2017-04-10 17:57:52 -0700 | [diff] [blame] | 19 | import static junit.framework.Assert.assertTrue; |
| 20 | |
Aaron Heuckroth | 96cd93f | 2018-11-20 11:14:40 -0500 | [diff] [blame] | 21 | import static org.mockito.Mockito.doNothing; |
Aaron Heuckroth | 266cb34 | 2018-09-07 14:52:04 -0400 | [diff] [blame] | 22 | import static org.mockito.Mockito.doReturn; |
Julia Reynolds | b586745 | 2018-02-28 16:31:35 -0500 | [diff] [blame] | 23 | import static org.mockito.Mockito.mock; |
Aaron Heuckroth | 266cb34 | 2018-09-07 14:52:04 -0400 | [diff] [blame] | 24 | import static org.mockito.Mockito.times; |
| 25 | import static org.mockito.Mockito.verify; |
Julia Reynolds | b586745 | 2018-02-28 16:31:35 -0500 | [diff] [blame] | 26 | import static org.mockito.Mockito.when; |
| 27 | |
Gus Prevas | 9b2348b | 2018-11-28 15:47:27 -0500 | [diff] [blame] | 28 | import android.app.NotificationChannel; |
Julia Reynolds | b586745 | 2018-02-28 16:31:35 -0500 | [diff] [blame] | 29 | import android.service.notification.StatusBarNotification; |
Jason Monk | fba8faf | 2017-05-23 10:42:59 -0400 | [diff] [blame] | 30 | import android.support.test.filters.SmallTest; |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 31 | import android.testing.AndroidTestingRunner; |
| 32 | import android.testing.TestableLooper; |
| 33 | import android.testing.TestableLooper.RunWithLooper; |
| 34 | import android.testing.ViewUtils; |
Julia Reynolds | b586745 | 2018-02-28 16:31:35 -0500 | [diff] [blame] | 35 | import android.view.ViewGroup; |
| 36 | |
Mady Mellor | 95d743c | 2017-01-10 12:05:27 -0800 | [diff] [blame] | 37 | import com.android.systemui.plugins.statusbar.NotificationMenuRowPlugin; |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 38 | import com.android.systemui.statusbar.notification.collection.NotificationEntry; |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 39 | import com.android.systemui.utils.leaks.LeakCheckedTest; |
| 40 | |
| 41 | import org.junit.Before; |
| 42 | import org.junit.Test; |
Jason Monk | 28d5d22 | 2017-02-02 13:08:31 -0500 | [diff] [blame] | 43 | import org.junit.runner.RunWith; |
Aaron Heuckroth | 266cb34 | 2018-09-07 14:52:04 -0400 | [diff] [blame] | 44 | import org.mockito.Mockito; |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 45 | |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 46 | @RunWith(AndroidTestingRunner.class) |
Jason Monk | a716bac | 2018-12-05 15:48:21 -0500 | [diff] [blame] | 47 | @RunWithLooper |
Jason Monk | fba8faf | 2017-05-23 10:42:59 -0400 | [diff] [blame] | 48 | @SmallTest |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 49 | public class NotificationMenuRowTest extends LeakCheckedTest { |
| 50 | |
Gus Prevas | 9b2348b | 2018-11-28 15:47:27 -0500 | [diff] [blame] | 51 | private ExpandableNotificationRow mRow; |
| 52 | |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 53 | @Before |
| 54 | public void setup() { |
| 55 | injectLeakCheckedDependencies(ALL_SUPPORTED_CLASSES); |
Gus Prevas | 9b2348b | 2018-11-28 15:47:27 -0500 | [diff] [blame] | 56 | mRow = mock(ExpandableNotificationRow.class); |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 57 | NotificationEntry entry = new NotificationEntry( |
Gus Prevas | 9b2348b | 2018-11-28 15:47:27 -0500 | [diff] [blame] | 58 | mock(StatusBarNotification.class)); |
| 59 | entry.channel = mock(NotificationChannel.class); |
| 60 | when(mRow.getEntry()).thenReturn(entry); |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | @Test |
| 64 | public void testAttachDetach() { |
Mady Mellor | 95d743c | 2017-01-10 12:05:27 -0800 | [diff] [blame] | 65 | NotificationMenuRowPlugin row = new NotificationMenuRow(mContext); |
Gus Prevas | 9b2348b | 2018-11-28 15:47:27 -0500 | [diff] [blame] | 66 | row.createMenu(mRow, null); |
Mady Mellor | 95d743c | 2017-01-10 12:05:27 -0800 | [diff] [blame] | 67 | ViewUtils.attachView(row.getMenuView()); |
Jason Monk | 28d5d22 | 2017-02-02 13:08:31 -0500 | [diff] [blame] | 68 | TestableLooper.get(this).processAllMessages(); |
Mady Mellor | 95d743c | 2017-01-10 12:05:27 -0800 | [diff] [blame] | 69 | ViewUtils.detachView(row.getMenuView()); |
Jason Monk | 28d5d22 | 2017-02-02 13:08:31 -0500 | [diff] [blame] | 70 | TestableLooper.get(this).processAllMessages(); |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 71 | } |
Mady Mellor | 0df2610 | 2017-04-03 11:50:44 -0700 | [diff] [blame] | 72 | |
| 73 | @Test |
| 74 | public void testRecreateMenu() { |
| 75 | NotificationMenuRowPlugin row = new NotificationMenuRow(mContext); |
Gus Prevas | 9b2348b | 2018-11-28 15:47:27 -0500 | [diff] [blame] | 76 | row.createMenu(mRow, null); |
Mady Mellor | 4c19760 | 2017-04-10 17:57:52 -0700 | [diff] [blame] | 77 | assertTrue(row.getMenuView() != null); |
Gus Prevas | 9b2348b | 2018-11-28 15:47:27 -0500 | [diff] [blame] | 78 | row.createMenu(mRow, null); |
Mady Mellor | 4c19760 | 2017-04-10 17:57:52 -0700 | [diff] [blame] | 79 | assertTrue(row.getMenuView() != null); |
Mady Mellor | 0df2610 | 2017-04-03 11:50:44 -0700 | [diff] [blame] | 80 | } |
Mady Mellor | 0b3f004 | 2017-06-20 17:18:53 -0700 | [diff] [blame] | 81 | |
| 82 | @Test |
| 83 | public void testResetUncreatedMenu() { |
| 84 | NotificationMenuRowPlugin row = new NotificationMenuRow(mContext); |
| 85 | row.resetMenu(); |
| 86 | } |
Julia Reynolds | b586745 | 2018-02-28 16:31:35 -0500 | [diff] [blame] | 87 | |
Aaron Heuckroth | 266cb34 | 2018-09-07 14:52:04 -0400 | [diff] [blame] | 88 | |
Julia Reynolds | b586745 | 2018-02-28 16:31:35 -0500 | [diff] [blame] | 89 | @Test |
| 90 | public void testNoAppOpsInSlowSwipe() { |
| 91 | NotificationMenuRow row = new NotificationMenuRow(mContext); |
Gus Prevas | 9b2348b | 2018-11-28 15:47:27 -0500 | [diff] [blame] | 92 | row.createMenu(mRow, null); |
Julia Reynolds | b586745 | 2018-02-28 16:31:35 -0500 | [diff] [blame] | 93 | |
| 94 | ViewGroup container = (ViewGroup) row.getMenuView(); |
| 95 | // one for snooze and one for noti blocking |
| 96 | assertEquals(2, container.getChildCount()); |
| 97 | } |
Aaron Heuckroth | 266cb34 | 2018-09-07 14:52:04 -0400 | [diff] [blame] | 98 | |
| 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 Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 349 | } |