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