Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 1 | /* |
Selim Cinek | a7679b6 | 2017-05-10 16:33:25 -0700 | [diff] [blame] | 2 | * Copyright (C) 2017 The Android Open Source Project |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 3 | * |
Selim Cinek | a7679b6 | 2017-05-10 16:33:25 -0700 | [diff] [blame] | 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 |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
Selim Cinek | a7679b6 | 2017-05-10 16:33:25 -0700 | [diff] [blame] | 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 |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 15 | */ |
| 16 | |
Selim Cinek | a7679b6 | 2017-05-10 16:33:25 -0700 | [diff] [blame] | 17 | package com.android.systemui.statusbar.notification; |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 18 | |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 19 | import static org.junit.Assert.assertFalse; |
| 20 | import static org.junit.Assert.assertTrue; |
| 21 | import static org.mockito.ArgumentMatchers.any; |
Gus Prevas | 5b9098dc | 2018-12-21 17:07:15 -0500 | [diff] [blame] | 22 | import static org.mockito.Mockito.mock; |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 23 | import static org.mockito.Mockito.never; |
Gus Prevas | 5b9098dc | 2018-12-21 17:07:15 -0500 | [diff] [blame] | 24 | import static org.mockito.Mockito.verify; |
| 25 | import static org.mockito.Mockito.when; |
| 26 | |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 27 | import android.os.Handler; |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 28 | import android.service.notification.StatusBarNotification; |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 29 | import android.testing.AndroidTestingRunner; |
| 30 | import android.testing.TestableLooper; |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 31 | |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 32 | import androidx.test.filters.SmallTest; |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 33 | |
Jason Monk | fba8faf | 2017-05-23 10:42:59 -0400 | [diff] [blame] | 34 | import com.android.systemui.SysuiTestCase; |
Gus Prevas | 5b9098dc | 2018-12-21 17:07:15 -0500 | [diff] [blame] | 35 | import com.android.systemui.statusbar.NotificationPresenter; |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 36 | import com.android.systemui.statusbar.notification.collection.NotificationEntry; |
Rohan Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 37 | import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow; |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 38 | |
| 39 | import org.junit.Before; |
| 40 | import org.junit.Test; |
| 41 | import org.junit.runner.RunWith; |
| 42 | |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 43 | @SmallTest |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 44 | @RunWith(AndroidTestingRunner.class) |
| 45 | @TestableLooper.RunWithLooper() |
Jason Monk | fba8faf | 2017-05-23 10:42:59 -0400 | [diff] [blame] | 46 | public class VisualStabilityManagerTest extends SysuiTestCase { |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 47 | |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 48 | private TestableLooper mTestableLooper; |
| 49 | |
| 50 | private VisualStabilityManager mVisualStabilityManager; |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 51 | private VisualStabilityManager.Callback mCallback = mock(VisualStabilityManager.Callback.class); |
| 52 | private VisibilityLocationProvider mLocationProvider = mock(VisibilityLocationProvider.class); |
| 53 | private ExpandableNotificationRow mRow = mock(ExpandableNotificationRow.class); |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 54 | private NotificationEntry mEntry; |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 55 | |
| 56 | @Before |
| 57 | public void setUp() { |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 58 | mTestableLooper = TestableLooper.get(this); |
| 59 | mVisualStabilityManager = new VisualStabilityManager( |
| 60 | mock(NotificationEntryManager.class), |
| 61 | new Handler(mTestableLooper.getLooper())); |
| 62 | |
Gus Prevas | 5b9098dc | 2018-12-21 17:07:15 -0500 | [diff] [blame] | 63 | mVisualStabilityManager.setUpWithPresenter(mock(NotificationPresenter.class)); |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 64 | mVisualStabilityManager.setVisibilityLocationProvider(mLocationProvider); |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 65 | mEntry = new NotificationEntry(mock(StatusBarNotification.class)); |
Evan Laird | 9449285 | 2018-10-25 13:43:01 -0400 | [diff] [blame] | 66 | mEntry.setRow(mRow); |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 67 | |
| 68 | when(mRow.getEntry()).thenReturn(mEntry); |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Test |
| 72 | public void testPanelExpansion() { |
| 73 | mVisualStabilityManager.setPanelExpanded(true); |
| 74 | mVisualStabilityManager.setScreenOn(true); |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 75 | assertFalse(mVisualStabilityManager.canReorderNotification(mRow)); |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 76 | mVisualStabilityManager.setPanelExpanded(false); |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 77 | assertTrue(mVisualStabilityManager.canReorderNotification(mRow)); |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Test |
| 81 | public void testScreenOn() { |
| 82 | mVisualStabilityManager.setPanelExpanded(true); |
| 83 | mVisualStabilityManager.setScreenOn(true); |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 84 | assertFalse(mVisualStabilityManager.canReorderNotification(mRow)); |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 85 | mVisualStabilityManager.setScreenOn(false); |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 86 | assertTrue(mVisualStabilityManager.canReorderNotification(mRow)); |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | @Test |
| 90 | public void testReorderingAllowedChangesScreenOn() { |
| 91 | mVisualStabilityManager.setPanelExpanded(true); |
| 92 | mVisualStabilityManager.setScreenOn(true); |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 93 | assertFalse(mVisualStabilityManager.isReorderingAllowed()); |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 94 | mVisualStabilityManager.setScreenOn(false); |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 95 | assertTrue(mVisualStabilityManager.isReorderingAllowed()); |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | @Test |
| 99 | public void testReorderingAllowedChangesPanel() { |
| 100 | mVisualStabilityManager.setPanelExpanded(true); |
| 101 | mVisualStabilityManager.setScreenOn(true); |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 102 | assertFalse(mVisualStabilityManager.isReorderingAllowed()); |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 103 | mVisualStabilityManager.setPanelExpanded(false); |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 104 | assertTrue(mVisualStabilityManager.isReorderingAllowed()); |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | @Test |
| 108 | public void testCallBackCalledScreenOn() { |
| 109 | mVisualStabilityManager.setPanelExpanded(true); |
| 110 | mVisualStabilityManager.setScreenOn(true); |
| 111 | mVisualStabilityManager.addReorderingAllowedCallback(mCallback); |
| 112 | mVisualStabilityManager.setScreenOn(false); |
| 113 | verify(mCallback).onReorderingAllowed(); |
| 114 | } |
| 115 | |
| 116 | @Test |
| 117 | public void testCallBackCalledPanelExpanded() { |
| 118 | mVisualStabilityManager.setPanelExpanded(true); |
| 119 | mVisualStabilityManager.setScreenOn(true); |
| 120 | mVisualStabilityManager.addReorderingAllowedCallback(mCallback); |
| 121 | mVisualStabilityManager.setPanelExpanded(false); |
| 122 | verify(mCallback).onReorderingAllowed(); |
| 123 | } |
| 124 | |
| 125 | @Test |
| 126 | public void testCallBackExactlyOnce() { |
| 127 | mVisualStabilityManager.setPanelExpanded(true); |
| 128 | mVisualStabilityManager.setScreenOn(true); |
| 129 | mVisualStabilityManager.addReorderingAllowedCallback(mCallback); |
| 130 | mVisualStabilityManager.setScreenOn(false); |
| 131 | mVisualStabilityManager.setScreenOn(true); |
| 132 | mVisualStabilityManager.setScreenOn(false); |
| 133 | verify(mCallback).onReorderingAllowed(); |
| 134 | } |
| 135 | |
| 136 | @Test |
| 137 | public void testAddedCanReorder() { |
| 138 | mVisualStabilityManager.setPanelExpanded(true); |
| 139 | mVisualStabilityManager.setScreenOn(true); |
| 140 | mVisualStabilityManager.notifyViewAddition(mRow); |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 141 | assertTrue(mVisualStabilityManager.canReorderNotification(mRow)); |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | @Test |
| 145 | public void testReorderingVisibleHeadsUpNotAllowed() { |
| 146 | mVisualStabilityManager.setPanelExpanded(true); |
| 147 | mVisualStabilityManager.setScreenOn(true); |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 148 | when(mLocationProvider.isInVisibleLocation(any(NotificationEntry.class))).thenReturn(true); |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 149 | mVisualStabilityManager.onHeadsUpStateChanged(mEntry, true); |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 150 | assertFalse(mVisualStabilityManager.canReorderNotification(mRow)); |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | @Test |
| 154 | public void testReorderingVisibleHeadsUpAllowed() { |
| 155 | mVisualStabilityManager.setPanelExpanded(true); |
| 156 | mVisualStabilityManager.setScreenOn(true); |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 157 | when(mLocationProvider.isInVisibleLocation(any(NotificationEntry.class))).thenReturn(false); |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 158 | mVisualStabilityManager.onHeadsUpStateChanged(mEntry, true); |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 159 | assertTrue(mVisualStabilityManager.canReorderNotification(mRow)); |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | @Test |
| 163 | public void testReorderingVisibleHeadsUpAllowedOnce() { |
| 164 | mVisualStabilityManager.setPanelExpanded(true); |
| 165 | mVisualStabilityManager.setScreenOn(true); |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 166 | when(mLocationProvider.isInVisibleLocation(any(NotificationEntry.class))).thenReturn(false); |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 167 | mVisualStabilityManager.onHeadsUpStateChanged(mEntry, true); |
| 168 | mVisualStabilityManager.onReorderingFinished(); |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 169 | assertFalse(mVisualStabilityManager.canReorderNotification(mRow)); |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 170 | } |
Adrian Roos | 4b820e4 | 2017-01-13 17:40:43 -0800 | [diff] [blame] | 171 | |
| 172 | @Test |
| 173 | public void testPulsing() { |
| 174 | mVisualStabilityManager.setPulsing(true); |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 175 | assertFalse(mVisualStabilityManager.canReorderNotification(mRow)); |
Adrian Roos | 4b820e4 | 2017-01-13 17:40:43 -0800 | [diff] [blame] | 176 | mVisualStabilityManager.setPulsing(false); |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 177 | assertTrue(mVisualStabilityManager.canReorderNotification(mRow)); |
Adrian Roos | 4b820e4 | 2017-01-13 17:40:43 -0800 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | @Test |
| 181 | public void testReorderingAllowedChanges_Pulsing() { |
| 182 | mVisualStabilityManager.setPulsing(true); |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 183 | assertFalse(mVisualStabilityManager.isReorderingAllowed()); |
Adrian Roos | 4b820e4 | 2017-01-13 17:40:43 -0800 | [diff] [blame] | 184 | mVisualStabilityManager.setPulsing(false); |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 185 | assertTrue(mVisualStabilityManager.isReorderingAllowed()); |
Adrian Roos | 4b820e4 | 2017-01-13 17:40:43 -0800 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | @Test |
| 189 | public void testCallBackCalled_Pulsing() { |
| 190 | mVisualStabilityManager.setPulsing(true); |
| 191 | mVisualStabilityManager.addReorderingAllowedCallback(mCallback); |
| 192 | mVisualStabilityManager.setPulsing(false); |
| 193 | verify(mCallback).onReorderingAllowed(); |
| 194 | } |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 195 | |
| 196 | @Test |
| 197 | public void testTemporarilyAllowReorderingNotifiesCallbacks() { |
| 198 | // GIVEN having the panel open (which would block reordering) |
| 199 | mVisualStabilityManager.setScreenOn(true); |
| 200 | mVisualStabilityManager.setPanelExpanded(true); |
| 201 | mVisualStabilityManager.addReorderingAllowedCallback(mCallback); |
| 202 | |
| 203 | // WHEN we temprarily allow reordering |
| 204 | mVisualStabilityManager.temporarilyAllowReordering(); |
| 205 | |
| 206 | // THEN callbacks are notified that reordering is allowed |
| 207 | verify(mCallback).onReorderingAllowed(); |
| 208 | assertTrue(mVisualStabilityManager.isReorderingAllowed()); |
| 209 | } |
| 210 | |
| 211 | @Test |
| 212 | public void testTemporarilyAllowReorderingDoesntOverridePulsing() { |
| 213 | // GIVEN we are in a pulsing state |
| 214 | mVisualStabilityManager.setPulsing(true); |
| 215 | mVisualStabilityManager.addReorderingAllowedCallback(mCallback); |
| 216 | |
| 217 | // WHEN we temprarily allow reordering |
| 218 | mVisualStabilityManager.temporarilyAllowReordering(); |
| 219 | |
| 220 | // THEN reordering is still not allowed |
| 221 | verify(mCallback, never()).onReorderingAllowed(); |
| 222 | assertFalse(mVisualStabilityManager.isReorderingAllowed()); |
| 223 | } |
| 224 | |
| 225 | @Test |
| 226 | public void testTemporarilyAllowReorderingExpires() { |
| 227 | // GIVEN having the panel open (which would block reordering) |
| 228 | mVisualStabilityManager.setScreenOn(true); |
| 229 | mVisualStabilityManager.setPanelExpanded(true); |
| 230 | mVisualStabilityManager.addReorderingAllowedCallback(mCallback); |
| 231 | |
| 232 | // WHEN we temprarily allow reordering and then wait until the window expires |
| 233 | mVisualStabilityManager.temporarilyAllowReordering(); |
| 234 | assertTrue(mVisualStabilityManager.isReorderingAllowed()); |
| 235 | mTestableLooper.processMessages(1); |
| 236 | |
| 237 | // THEN reordering is no longer allowed |
| 238 | assertFalse(mVisualStabilityManager.isReorderingAllowed()); |
| 239 | } |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 240 | } |