Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 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 |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 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 |
| 15 | */ |
| 16 | |
| 17 | package com.android.systemui.statusbar.phone; |
| 18 | |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 19 | import static com.android.systemui.statusbar.phone.ScrimController.VISIBILITY_FULLY_OPAQUE; |
| 20 | import static com.android.systemui.statusbar.phone.ScrimController.VISIBILITY_FULLY_TRANSPARENT; |
| 21 | import static com.android.systemui.statusbar.phone.ScrimController.VISIBILITY_SEMI_TRANSPARENT; |
| 22 | |
| 23 | import static org.mockito.ArgumentMatchers.any; |
| 24 | import static org.mockito.ArgumentMatchers.anyInt; |
| 25 | import static org.mockito.ArgumentMatchers.anyLong; |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 26 | import static org.mockito.Mockito.mock; |
| 27 | import static org.mockito.Mockito.never; |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 28 | import static org.mockito.Mockito.reset; |
Lucas Dupin | 067136c | 2018-03-27 18:03:25 -0700 | [diff] [blame] | 29 | import static org.mockito.Mockito.spy; |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 30 | import static org.mockito.Mockito.verify; |
Lucas Dupin | eea53b3 | 2017-12-18 13:47:14 -0800 | [diff] [blame] | 31 | import static org.mockito.Mockito.verifyZeroInteractions; |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 32 | import static org.mockito.Mockito.when; |
| 33 | |
| 34 | import android.animation.Animator; |
Lucas Dupin | 80a3fcc | 2018-02-07 10:49:55 -0800 | [diff] [blame] | 35 | import android.animation.ValueAnimator; |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 36 | import android.app.AlarmManager; |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 37 | import android.graphics.Color; |
| 38 | import android.os.Handler; |
| 39 | import android.os.Looper; |
| 40 | import android.support.test.filters.SmallTest; |
| 41 | import android.testing.AndroidTestingRunner; |
| 42 | import android.testing.TestableLooper; |
| 43 | import android.view.View; |
| 44 | |
Lucas Dupin | 7517b5d | 2017-08-22 12:51:25 -0700 | [diff] [blame] | 45 | import com.android.keyguard.KeyguardUpdateMonitor; |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 46 | import com.android.systemui.SysuiTestCase; |
| 47 | import com.android.systemui.statusbar.ScrimView; |
| 48 | import com.android.systemui.util.wakelock.WakeLock; |
| 49 | import com.android.systemui.utils.os.FakeHandler; |
| 50 | |
| 51 | import org.junit.Assert; |
| 52 | import org.junit.Before; |
| 53 | import org.junit.Test; |
| 54 | import org.junit.runner.RunWith; |
| 55 | |
Lucas Dupin | 78949b8 | 2018-04-03 18:54:39 -0700 | [diff] [blame^] | 56 | import java.util.Arrays; |
| 57 | import java.util.HashSet; |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 58 | import java.util.function.Consumer; |
| 59 | |
| 60 | @RunWith(AndroidTestingRunner.class) |
| 61 | @TestableLooper.RunWithLooper |
| 62 | @SmallTest |
| 63 | public class ScrimControllerTest extends SysuiTestCase { |
| 64 | |
| 65 | private SynchronousScrimController mScrimController; |
| 66 | private ScrimView mScrimBehind; |
| 67 | private ScrimView mScrimInFront; |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 68 | private Consumer<Integer> mScrimVisibilityCallback; |
| 69 | private int mScrimVisibility; |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 70 | private LightBarController mLightBarController; |
| 71 | private DozeParameters mDozeParamenters; |
| 72 | private WakeLock mWakeLock; |
| 73 | private boolean mAlwaysOnEnabled; |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 74 | private AlarmManager mAlarmManager; |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 75 | |
| 76 | @Before |
| 77 | public void setup() { |
| 78 | mLightBarController = mock(LightBarController.class); |
Lucas Dupin | 067136c | 2018-03-27 18:03:25 -0700 | [diff] [blame] | 79 | mScrimBehind = spy(new ScrimView(getContext())); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 80 | mScrimInFront = new ScrimView(getContext()); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 81 | mWakeLock = mock(WakeLock.class); |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 82 | mAlarmManager = mock(AlarmManager.class); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 83 | mAlwaysOnEnabled = true; |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 84 | mScrimVisibilityCallback = (Integer visible) -> mScrimVisibility = visible; |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 85 | mDozeParamenters = mock(DozeParameters.class); |
| 86 | when(mDozeParamenters.getAlwaysOn()).thenAnswer(invocation -> mAlwaysOnEnabled); |
Lucas Dupin | 43d0d73 | 2017-11-16 11:23:49 -0800 | [diff] [blame] | 87 | when(mDozeParamenters.getDisplayNeedsBlanking()).thenReturn(true); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 88 | mScrimController = new SynchronousScrimController(mLightBarController, mScrimBehind, |
Selim Cinek | 99e9adf | 2018-03-15 09:17:47 -0700 | [diff] [blame] | 89 | mScrimInFront, mScrimVisibilityCallback, mDozeParamenters, |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 90 | mAlarmManager); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | @Test |
| 94 | public void initialState() { |
| 95 | Assert.assertEquals("ScrimController should start initialized", |
| 96 | mScrimController.getState(), ScrimState.UNINITIALIZED); |
| 97 | } |
| 98 | |
| 99 | @Test |
| 100 | public void transitionToKeyguard() { |
| 101 | mScrimController.transitionTo(ScrimState.KEYGUARD); |
| 102 | mScrimController.finishAnimationsImmediately(); |
| 103 | // Front scrim should be transparent |
| 104 | // Back scrim should be visible without tint |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 105 | assertScrimVisibility(VISIBILITY_FULLY_TRANSPARENT, VISIBILITY_SEMI_TRANSPARENT); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 106 | assertScrimTint(mScrimBehind, false /* tinted */); |
| 107 | } |
| 108 | |
| 109 | @Test |
Lucas Dupin | 7517b5d | 2017-08-22 12:51:25 -0700 | [diff] [blame] | 110 | public void transitionToAod_withRegularWallpaper() { |
| 111 | mScrimController.transitionTo(ScrimState.AOD); |
| 112 | mScrimController.finishAnimationsImmediately(); |
| 113 | // Front scrim should be transparent |
| 114 | // Back scrim should be visible with tint |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 115 | assertScrimVisibility(VISIBILITY_FULLY_TRANSPARENT, VISIBILITY_FULLY_OPAQUE); |
Lucas Dupin | 7517b5d | 2017-08-22 12:51:25 -0700 | [diff] [blame] | 116 | assertScrimTint(mScrimBehind, true /* tinted */); |
| 117 | assertScrimTint(mScrimInFront, true /* tinted */); |
| 118 | } |
| 119 | |
| 120 | @Test |
| 121 | public void transitionToAod_withAodWallpaper() { |
| 122 | mScrimController.setWallpaperSupportsAmbientMode(true); |
| 123 | mScrimController.transitionTo(ScrimState.AOD); |
| 124 | mScrimController.finishAnimationsImmediately(); |
| 125 | // Front scrim should be transparent |
| 126 | // Back scrim should be transparent |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 127 | assertScrimVisibility(VISIBILITY_FULLY_TRANSPARENT, VISIBILITY_FULLY_TRANSPARENT); |
| 128 | |
| 129 | // Move on to PULSING and check if the back scrim is still transparent |
| 130 | mScrimController.transitionTo(ScrimState.PULSING); |
| 131 | mScrimController.finishAnimationsImmediately(); |
| 132 | assertScrimVisibility(VISIBILITY_FULLY_TRANSPARENT, VISIBILITY_FULLY_TRANSPARENT); |
Lucas Dupin | 7517b5d | 2017-08-22 12:51:25 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | @Test |
| 136 | public void transitionToAod_withAodWallpaperAndLockScreenWallpaper() { |
| 137 | ScrimState.AOD.mKeyguardUpdateMonitor = new KeyguardUpdateMonitor(getContext()) { |
| 138 | @Override |
| 139 | public boolean hasLockscreenWallpaper() { |
| 140 | return true; |
| 141 | } |
| 142 | }; |
| 143 | mScrimController.setWallpaperSupportsAmbientMode(true); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 144 | mScrimController.transitionTo(ScrimState.AOD); |
| 145 | mScrimController.finishAnimationsImmediately(); |
| 146 | // Front scrim should be transparent |
| 147 | // Back scrim should be visible with tint |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 148 | assertScrimVisibility(VISIBILITY_FULLY_TRANSPARENT, VISIBILITY_FULLY_OPAQUE); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 149 | assertScrimTint(mScrimBehind, true /* tinted */); |
| 150 | assertScrimTint(mScrimInFront, true /* tinted */); |
| 151 | } |
| 152 | |
| 153 | @Test |
| 154 | public void transitionToPulsing() { |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 155 | // Pre-condition |
| 156 | // Need to go to AoD first because PULSING doesn't change |
| 157 | // the back scrim opacity - otherwise it would hide AoD wallpapers. |
| 158 | mScrimController.setWallpaperSupportsAmbientMode(false); |
| 159 | mScrimController.transitionTo(ScrimState.AOD); |
| 160 | mScrimController.finishAnimationsImmediately(); |
| 161 | assertScrimVisibility(VISIBILITY_FULLY_TRANSPARENT, VISIBILITY_FULLY_OPAQUE); |
| 162 | |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 163 | mScrimController.transitionTo(ScrimState.PULSING); |
| 164 | mScrimController.finishAnimationsImmediately(); |
| 165 | // Front scrim should be transparent |
| 166 | // Back scrim should be visible with tint |
| 167 | // Pulse callback should have been invoked |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 168 | assertScrimVisibility(VISIBILITY_FULLY_TRANSPARENT, VISIBILITY_FULLY_OPAQUE); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 169 | assertScrimTint(mScrimBehind, true /* tinted */); |
| 170 | } |
| 171 | |
| 172 | @Test |
Lucas Dupin | bc9aac1 | 2018-03-04 20:18:15 -0800 | [diff] [blame] | 173 | public void transitionToKeyguardBouncer() { |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 174 | mScrimController.transitionTo(ScrimState.BOUNCER); |
| 175 | mScrimController.finishAnimationsImmediately(); |
| 176 | // Front scrim should be transparent |
| 177 | // Back scrim should be visible without tint |
Lucas Dupin | bc9aac1 | 2018-03-04 20:18:15 -0800 | [diff] [blame] | 178 | assertScrimVisibility(VISIBILITY_FULLY_TRANSPARENT, VISIBILITY_SEMI_TRANSPARENT); |
| 179 | assertScrimTint(mScrimBehind, false /* tinted */); |
| 180 | } |
| 181 | |
| 182 | @Test |
| 183 | public void transitionToBouncer() { |
Lucas Dupin | 05726cd | 2018-03-13 14:00:24 -0700 | [diff] [blame] | 184 | mScrimController.transitionTo(ScrimState.BOUNCER_SCRIMMED); |
Lucas Dupin | bc9aac1 | 2018-03-04 20:18:15 -0800 | [diff] [blame] | 185 | mScrimController.finishAnimationsImmediately(); |
| 186 | // Front scrim should be transparent |
| 187 | // Back scrim should be visible without tint |
| 188 | assertScrimVisibility(VISIBILITY_SEMI_TRANSPARENT, VISIBILITY_FULLY_TRANSPARENT); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 189 | assertScrimTint(mScrimBehind, false /* tinted */); |
| 190 | } |
| 191 | |
| 192 | @Test |
| 193 | public void transitionToUnlocked() { |
Lucas Dupin | 80a3fcc | 2018-02-07 10:49:55 -0800 | [diff] [blame] | 194 | mScrimController.setPanelExpansion(0f); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 195 | mScrimController.transitionTo(ScrimState.UNLOCKED); |
| 196 | mScrimController.finishAnimationsImmediately(); |
| 197 | // Front scrim should be transparent |
| 198 | // Back scrim should be transparent |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 199 | assertScrimVisibility(VISIBILITY_FULLY_TRANSPARENT, VISIBILITY_FULLY_TRANSPARENT); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 200 | assertScrimTint(mScrimBehind, false /* tinted */); |
| 201 | assertScrimTint(mScrimInFront, false /* tinted */); |
| 202 | |
| 203 | // Back scrim should be visible after start dragging |
| 204 | mScrimController.setPanelExpansion(0.5f); |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 205 | assertScrimVisibility(VISIBILITY_FULLY_TRANSPARENT, VISIBILITY_SEMI_TRANSPARENT); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | @Test |
Lucas Dupin | 67f0263 | 2018-03-12 11:08:31 -0700 | [diff] [blame] | 209 | public void panelExpansionAffectsAlpha() { |
| 210 | mScrimController.setPanelExpansion(0f); |
| 211 | mScrimController.setPanelExpansion(0.5f); |
| 212 | mScrimController.transitionTo(ScrimState.UNLOCKED); |
| 213 | mScrimController.finishAnimationsImmediately(); |
| 214 | |
| 215 | final float scrimAlpha = mScrimBehind.getViewAlpha(); |
Lucas Dupin | 067136c | 2018-03-27 18:03:25 -0700 | [diff] [blame] | 216 | reset(mScrimBehind); |
Lucas Dupin | 67f0263 | 2018-03-12 11:08:31 -0700 | [diff] [blame] | 217 | mScrimController.setExpansionAffectsAlpha(false); |
| 218 | mScrimController.setPanelExpansion(0.8f); |
Lucas Dupin | 067136c | 2018-03-27 18:03:25 -0700 | [diff] [blame] | 219 | verifyZeroInteractions(mScrimBehind); |
Lucas Dupin | 67f0263 | 2018-03-12 11:08:31 -0700 | [diff] [blame] | 220 | Assert.assertEquals("Scrim opacity shouldn't change when setExpansionAffectsAlpha " |
| 221 | + "is false", scrimAlpha, mScrimBehind.getViewAlpha(), 0.01f); |
| 222 | |
| 223 | mScrimController.setExpansionAffectsAlpha(true); |
| 224 | mScrimController.setPanelExpansion(0.1f); |
| 225 | Assert.assertNotEquals("Scrim opacity should change when setExpansionAffectsAlpha " |
| 226 | + "is true", scrimAlpha, mScrimBehind.getViewAlpha(), 0.01f); |
| 227 | } |
| 228 | |
| 229 | @Test |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 230 | public void transitionToUnlockedFromAod() { |
| 231 | // Simulate unlock with fingerprint |
| 232 | mScrimController.transitionTo(ScrimState.AOD); |
Lucas Dupin | 80a3fcc | 2018-02-07 10:49:55 -0800 | [diff] [blame] | 233 | mScrimController.setPanelExpansion(0f); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 234 | mScrimController.finishAnimationsImmediately(); |
| 235 | mScrimController.transitionTo(ScrimState.UNLOCKED); |
| 236 | // Immediately tinted after the transition starts |
| 237 | assertScrimTint(mScrimInFront, true /* tinted */); |
| 238 | assertScrimTint(mScrimBehind, true /* tinted */); |
| 239 | mScrimController.finishAnimationsImmediately(); |
| 240 | // Front scrim should be transparent |
| 241 | // Back scrim should be transparent |
| 242 | // Neither scrims should be tinted anymore after the animation. |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 243 | assertScrimVisibility(VISIBILITY_FULLY_TRANSPARENT, VISIBILITY_FULLY_TRANSPARENT); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 244 | assertScrimTint(mScrimInFront, false /* tinted */); |
| 245 | assertScrimTint(mScrimBehind, false /* tinted */); |
| 246 | } |
| 247 | |
| 248 | @Test |
| 249 | public void scrimBlanksBeforeLeavingAoD() { |
| 250 | // Simulate unlock with fingerprint |
| 251 | mScrimController.transitionTo(ScrimState.AOD); |
| 252 | mScrimController.finishAnimationsImmediately(); |
| 253 | mScrimController.transitionTo(ScrimState.UNLOCKED, |
| 254 | new ScrimController.Callback() { |
| 255 | @Override |
| 256 | public void onDisplayBlanked() { |
| 257 | // Front scrim should be black in the middle of the transition |
| 258 | Assert.assertTrue("Scrim should be visible during transition. Alpha: " |
| 259 | + mScrimInFront.getViewAlpha(), mScrimInFront.getViewAlpha() > 0); |
| 260 | assertScrimTint(mScrimInFront, true /* tinted */); |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 261 | Assert.assertSame("Scrim should be visible during transition.", |
| 262 | mScrimVisibility, VISIBILITY_FULLY_OPAQUE); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 263 | } |
| 264 | }); |
| 265 | mScrimController.finishAnimationsImmediately(); |
| 266 | } |
| 267 | |
| 268 | @Test |
| 269 | public void testScrimCallback() { |
| 270 | int[] callOrder = {0, 0, 0}; |
| 271 | int[] currentCall = {0}; |
| 272 | mScrimController.transitionTo(ScrimState.AOD, new ScrimController.Callback() { |
| 273 | @Override |
| 274 | public void onStart() { |
| 275 | callOrder[0] = ++currentCall[0]; |
| 276 | } |
| 277 | |
| 278 | @Override |
| 279 | public void onDisplayBlanked() { |
| 280 | callOrder[1] = ++currentCall[0]; |
| 281 | } |
| 282 | |
| 283 | @Override |
| 284 | public void onFinished() { |
| 285 | callOrder[2] = ++currentCall[0]; |
| 286 | } |
| 287 | }); |
| 288 | mScrimController.finishAnimationsImmediately(); |
| 289 | Assert.assertEquals("onStart called in wrong order", 1, callOrder[0]); |
| 290 | Assert.assertEquals("onDisplayBlanked called in wrong order", 2, callOrder[1]); |
| 291 | Assert.assertEquals("onFinished called in wrong order", 3, callOrder[2]); |
| 292 | } |
| 293 | |
| 294 | @Test |
| 295 | public void testScrimCallbacksWithoutAmbientDisplay() { |
| 296 | mAlwaysOnEnabled = false; |
| 297 | testScrimCallback(); |
| 298 | } |
| 299 | |
| 300 | @Test |
| 301 | public void testScrimCallbackCancelled() { |
| 302 | boolean[] cancelledCalled = {false}; |
| 303 | mScrimController.transitionTo(ScrimState.AOD, new ScrimController.Callback() { |
| 304 | @Override |
| 305 | public void onCancelled() { |
| 306 | cancelledCalled[0] = true; |
| 307 | } |
| 308 | }); |
| 309 | mScrimController.transitionTo(ScrimState.PULSING); |
| 310 | Assert.assertTrue("onCancelled should have been called", cancelledCalled[0]); |
| 311 | } |
| 312 | |
| 313 | @Test |
Lucas Dupin | eea53b3 | 2017-12-18 13:47:14 -0800 | [diff] [blame] | 314 | public void testHoldsWakeLock_whenAOD() { |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 315 | mScrimController.transitionTo(ScrimState.AOD); |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 316 | verify(mWakeLock).acquire(); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 317 | verify(mWakeLock, never()).release(); |
| 318 | mScrimController.finishAnimationsImmediately(); |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 319 | verify(mWakeLock).release(); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 320 | } |
| 321 | |
Lucas Dupin | 19aba8e | 2017-12-11 12:42:26 -0800 | [diff] [blame] | 322 | @Test |
Lucas Dupin | eea53b3 | 2017-12-18 13:47:14 -0800 | [diff] [blame] | 323 | public void testDoesNotHoldWakeLock_whenUnlocking() { |
| 324 | mScrimController.transitionTo(ScrimState.UNLOCKED); |
| 325 | mScrimController.finishAnimationsImmediately(); |
| 326 | verifyZeroInteractions(mWakeLock); |
| 327 | } |
| 328 | |
| 329 | @Test |
Lucas Dupin | 19aba8e | 2017-12-11 12:42:26 -0800 | [diff] [blame] | 330 | public void testCallbackInvokedOnSameStateTransition() { |
| 331 | mScrimController.transitionTo(ScrimState.UNLOCKED); |
| 332 | mScrimController.finishAnimationsImmediately(); |
| 333 | ScrimController.Callback callback = mock(ScrimController.Callback.class); |
| 334 | mScrimController.transitionTo(ScrimState.UNLOCKED, callback); |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 335 | verify(callback).onFinished(); |
| 336 | } |
| 337 | |
| 338 | @Test |
| 339 | public void testHoldsAodWallpaperAnimationLock() { |
| 340 | // Pre-conditions |
| 341 | mScrimController.transitionTo(ScrimState.AOD); |
| 342 | mScrimController.finishAnimationsImmediately(); |
| 343 | reset(mWakeLock); |
| 344 | |
| 345 | mScrimController.onHideWallpaperTimeout(); |
| 346 | verify(mWakeLock).acquire(); |
| 347 | verify(mWakeLock, never()).release(); |
| 348 | mScrimController.finishAnimationsImmediately(); |
| 349 | verify(mWakeLock).release(); |
| 350 | } |
| 351 | |
| 352 | @Test |
Lucas Dupin | 16cfe45 | 2018-02-08 13:14:50 -0800 | [diff] [blame] | 353 | public void testWillHideAodWallpaper() { |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 354 | mScrimController.setWallpaperSupportsAmbientMode(true); |
| 355 | mScrimController.transitionTo(ScrimState.AOD); |
| 356 | verify(mAlarmManager).setExact(anyInt(), anyLong(), any(), any(), any()); |
| 357 | mScrimController.transitionTo(ScrimState.KEYGUARD); |
| 358 | verify(mAlarmManager).cancel(any(AlarmManager.OnAlarmListener.class)); |
Lucas Dupin | 19aba8e | 2017-12-11 12:42:26 -0800 | [diff] [blame] | 359 | } |
| 360 | |
Lucas Dupin | 80a3fcc | 2018-02-07 10:49:55 -0800 | [diff] [blame] | 361 | @Test |
| 362 | public void testConservesExpansionOpacityAfterTransition() { |
| 363 | mScrimController.transitionTo(ScrimState.UNLOCKED); |
| 364 | mScrimController.setPanelExpansion(0.5f); |
| 365 | mScrimController.finishAnimationsImmediately(); |
| 366 | |
| 367 | final float expandedAlpha = mScrimBehind.getViewAlpha(); |
| 368 | |
| 369 | mScrimController.transitionTo(ScrimState.BRIGHTNESS_MIRROR); |
| 370 | mScrimController.finishAnimationsImmediately(); |
| 371 | mScrimController.transitionTo(ScrimState.UNLOCKED); |
| 372 | mScrimController.finishAnimationsImmediately(); |
| 373 | |
| 374 | Assert.assertEquals("Scrim expansion opacity wasn't conserved when transitioning back", |
| 375 | expandedAlpha, mScrimBehind.getViewAlpha(), 0.01f); |
| 376 | } |
| 377 | |
| 378 | @Test |
| 379 | public void cancelsOldAnimationBeforeBlanking() { |
| 380 | mScrimController.transitionTo(ScrimState.AOD); |
| 381 | mScrimController.finishAnimationsImmediately(); |
| 382 | // Consume whatever value we had before |
| 383 | mScrimController.wasAnimationJustCancelled(); |
| 384 | |
| 385 | mScrimController.transitionTo(ScrimState.KEYGUARD); |
| 386 | mScrimController.finishAnimationsImmediately(); |
| 387 | Assert.assertTrue(mScrimController.wasAnimationJustCancelled()); |
| 388 | } |
| 389 | |
Lucas Dupin | b380c88 | 2018-02-25 21:57:17 -0800 | [diff] [blame] | 390 | /** |
| 391 | * Number of visible notifications affects scrim opacity. |
| 392 | */ |
| 393 | @Test |
| 394 | public void testNotificationDensity() { |
| 395 | mScrimController.transitionTo(ScrimState.KEYGUARD); |
| 396 | mScrimController.finishAnimationsImmediately(); |
| 397 | |
| 398 | mScrimController.setNotificationCount(0); |
| 399 | mScrimController.finishAnimationsImmediately(); |
| 400 | Assert.assertEquals("lower density when no notifications", |
| 401 | ScrimController.GRADIENT_SCRIM_ALPHA, mScrimBehind.getViewAlpha(), 0.01f); |
| 402 | |
| 403 | mScrimController.setNotificationCount(3); |
| 404 | mScrimController.finishAnimationsImmediately(); |
| 405 | Assert.assertEquals("stronger density when notifications are visible", |
| 406 | ScrimController.GRADIENT_SCRIM_ALPHA_BUSY, mScrimBehind.getViewAlpha(), 0.01f); |
| 407 | } |
| 408 | |
| 409 | /** |
| 410 | * Moving from/to states conserves old notification density. |
| 411 | */ |
| 412 | @Test |
| 413 | public void testConservesNotificationDensity() { |
| 414 | testConservesNotificationDensity(0 /* count */, ScrimController.GRADIENT_SCRIM_ALPHA); |
| 415 | testConservesNotificationDensity(3 /* count */, ScrimController.GRADIENT_SCRIM_ALPHA_BUSY); |
| 416 | } |
| 417 | |
Lucas Dupin | 3daf3b0 | 2018-02-27 17:23:41 -0800 | [diff] [blame] | 418 | @Test |
Lucas Dupin | 38962d7 | 2018-03-14 12:41:39 -0700 | [diff] [blame] | 419 | public void testScrimFocus() { |
| 420 | mScrimController.transitionTo(ScrimState.AOD); |
| 421 | Assert.assertFalse("Should not be focusable on AOD", mScrimBehind.isFocusable()); |
| 422 | Assert.assertFalse("Should not be focusable on AOD", mScrimInFront.isFocusable()); |
| 423 | |
| 424 | mScrimController.transitionTo(ScrimState.KEYGUARD); |
| 425 | Assert.assertTrue("Should be focusable on keyguard", mScrimBehind.isFocusable()); |
| 426 | Assert.assertTrue("Should be focusable on keyguard", mScrimInFront.isFocusable()); |
| 427 | } |
| 428 | |
Lucas Dupin | d510730 | 2018-03-19 15:30:29 -0700 | [diff] [blame] | 429 | @Test |
| 430 | public void testHidesShowWhenLockedActivity() { |
| 431 | mScrimController.setWallpaperSupportsAmbientMode(true); |
| 432 | mScrimController.setKeyguardOccluded(true); |
| 433 | mScrimController.transitionTo(ScrimState.AOD); |
| 434 | mScrimController.finishAnimationsImmediately(); |
| 435 | assertScrimVisibility(VISIBILITY_FULLY_TRANSPARENT, VISIBILITY_FULLY_OPAQUE); |
| 436 | |
| 437 | mScrimController.transitionTo(ScrimState.PULSING); |
| 438 | mScrimController.finishAnimationsImmediately(); |
| 439 | assertScrimVisibility(VISIBILITY_FULLY_TRANSPARENT, VISIBILITY_FULLY_OPAQUE); |
| 440 | } |
| 441 | |
Lucas Dupin | 78949b8 | 2018-04-03 18:54:39 -0700 | [diff] [blame^] | 442 | @Test |
| 443 | public void testEatsTouchEvent() { |
| 444 | HashSet<ScrimState> eatsTouches = |
| 445 | new HashSet<>(Arrays.asList(ScrimState.AOD, ScrimState.PULSING)); |
| 446 | for (ScrimState state : ScrimState.values()) { |
| 447 | if (state == ScrimState.UNINITIALIZED) { |
| 448 | continue; |
| 449 | } |
| 450 | mScrimController.transitionTo(state); |
| 451 | mScrimController.finishAnimationsImmediately(); |
| 452 | Assert.assertEquals("Should be clickable unless AOD or PULSING, was: " + state, |
| 453 | mScrimBehind.getViewAlpha() != 0 && !eatsTouches.contains(state), |
| 454 | mScrimBehind.isClickable()); |
| 455 | } |
| 456 | } |
| 457 | |
Lucas Dupin | b380c88 | 2018-02-25 21:57:17 -0800 | [diff] [blame] | 458 | /** |
| 459 | * Conserves old notification density after leaving state and coming back. |
| 460 | * |
| 461 | * @param count How many notification. |
| 462 | * @param expectedAlpha Expected alpha. |
| 463 | */ |
| 464 | private void testConservesNotificationDensity(int count, float expectedAlpha) { |
| 465 | mScrimController.setNotificationCount(count); |
| 466 | mScrimController.transitionTo(ScrimState.UNLOCKED); |
| 467 | mScrimController.finishAnimationsImmediately(); |
| 468 | |
| 469 | mScrimController.transitionTo(ScrimState.KEYGUARD); |
| 470 | mScrimController.finishAnimationsImmediately(); |
| 471 | |
| 472 | Assert.assertEquals("Doesn't respect notification busyness after transition", |
| 473 | expectedAlpha, mScrimBehind.getViewAlpha(), 0.01f); |
| 474 | } |
| 475 | |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 476 | private void assertScrimTint(ScrimView scrimView, boolean tinted) { |
| 477 | final boolean viewIsTinted = scrimView.getTint() != Color.TRANSPARENT; |
| 478 | final String name = scrimView == mScrimInFront ? "front" : "back"; |
| 479 | Assert.assertEquals("Tint test failed at state " + mScrimController.getState() |
| 480 | +" with scrim: " + name + " and tint: " + Integer.toHexString(scrimView.getTint()), |
| 481 | tinted, viewIsTinted); |
| 482 | } |
| 483 | |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 484 | private void assertScrimVisibility(int inFront, int behind) { |
| 485 | boolean inFrontVisible = inFront != ScrimController.VISIBILITY_FULLY_TRANSPARENT; |
| 486 | boolean behindVisible = behind != ScrimController.VISIBILITY_FULLY_TRANSPARENT; |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 487 | Assert.assertEquals("Unexpected front scrim visibility. Alpha is " |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 488 | + mScrimInFront.getViewAlpha(), inFrontVisible, mScrimInFront.getViewAlpha() > 0); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 489 | Assert.assertEquals("Unexpected back scrim visibility. Alpha is " |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 490 | + mScrimBehind.getViewAlpha(), behindVisible, mScrimBehind.getViewAlpha() > 0); |
| 491 | |
| 492 | final int visibility; |
| 493 | if (inFront == VISIBILITY_FULLY_OPAQUE || behind == VISIBILITY_FULLY_OPAQUE) { |
| 494 | visibility = VISIBILITY_FULLY_OPAQUE; |
| 495 | } else if (inFront > VISIBILITY_FULLY_TRANSPARENT || behind > VISIBILITY_FULLY_TRANSPARENT) { |
| 496 | visibility = VISIBILITY_SEMI_TRANSPARENT; |
| 497 | } else { |
| 498 | visibility = VISIBILITY_FULLY_TRANSPARENT; |
| 499 | } |
| 500 | Assert.assertEquals("Invalid visibility.", visibility, mScrimVisibility); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | /** |
| 504 | * Special version of ScrimController where animations have 0 duration for test purposes. |
| 505 | */ |
| 506 | private class SynchronousScrimController extends ScrimController { |
| 507 | |
| 508 | private FakeHandler mHandler; |
Lucas Dupin | 80a3fcc | 2018-02-07 10:49:55 -0800 | [diff] [blame] | 509 | private boolean mAnimationCancelled; |
Lucas Dupin | 16cfe45 | 2018-02-08 13:14:50 -0800 | [diff] [blame] | 510 | boolean mOnPreDrawCalled; |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 511 | |
Lucas Dupin | 3503c5f | 2018-03-02 19:04:00 -0800 | [diff] [blame] | 512 | SynchronousScrimController(LightBarController lightBarController, |
Selim Cinek | 99e9adf | 2018-03-15 09:17:47 -0700 | [diff] [blame] | 513 | ScrimView scrimBehind, ScrimView scrimInFront, |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 514 | Consumer<Integer> scrimVisibleListener, DozeParameters dozeParameters, |
| 515 | AlarmManager alarmManager) { |
Selim Cinek | 99e9adf | 2018-03-15 09:17:47 -0700 | [diff] [blame] | 516 | super(lightBarController, scrimBehind, scrimInFront, |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 517 | scrimVisibleListener, dozeParameters, alarmManager); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 518 | mHandler = new FakeHandler(Looper.myLooper()); |
| 519 | } |
| 520 | |
Lucas Dupin | 16cfe45 | 2018-02-08 13:14:50 -0800 | [diff] [blame] | 521 | @Override |
| 522 | public boolean onPreDraw() { |
| 523 | mOnPreDrawCalled = true; |
| 524 | return super.onPreDraw(); |
| 525 | } |
| 526 | |
Lucas Dupin | 3503c5f | 2018-03-02 19:04:00 -0800 | [diff] [blame] | 527 | void finishAnimationsImmediately() { |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 528 | boolean[] animationFinished = {false}; |
| 529 | setOnAnimationFinished(()-> animationFinished[0] = true); |
| 530 | |
| 531 | // Execute code that will trigger animations. |
| 532 | onPreDraw(); |
| 533 | |
| 534 | // Force finish screen blanking. |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 535 | mHandler.dispatchQueuedMessages(); |
| 536 | // Force finish all animations. |
| 537 | endAnimation(mScrimBehind, TAG_KEY_ANIM); |
| 538 | endAnimation(mScrimInFront, TAG_KEY_ANIM); |
| 539 | |
| 540 | if (!animationFinished[0]) { |
| 541 | throw new IllegalStateException("Animation never finished"); |
| 542 | } |
| 543 | } |
| 544 | |
Lucas Dupin | 3503c5f | 2018-03-02 19:04:00 -0800 | [diff] [blame] | 545 | boolean wasAnimationJustCancelled() { |
Lucas Dupin | 80a3fcc | 2018-02-07 10:49:55 -0800 | [diff] [blame] | 546 | final boolean wasCancelled = mAnimationCancelled; |
| 547 | mAnimationCancelled = false; |
| 548 | return wasCancelled; |
| 549 | } |
| 550 | |
Lucas Dupin | 3daf3b0 | 2018-02-27 17:23:41 -0800 | [diff] [blame] | 551 | private void endAnimation(View scrimView, int tag) { |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 552 | Animator animator = (Animator) scrimView.getTag(tag); |
| 553 | if (animator != null) { |
| 554 | animator.end(); |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | @Override |
Lucas Dupin | 80a3fcc | 2018-02-07 10:49:55 -0800 | [diff] [blame] | 559 | protected void cancelAnimator(ValueAnimator previousAnimator) { |
| 560 | super.cancelAnimator(previousAnimator); |
| 561 | mAnimationCancelled = true; |
| 562 | } |
| 563 | |
| 564 | @Override |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 565 | protected Handler getHandler() { |
| 566 | return mHandler; |
| 567 | } |
| 568 | |
| 569 | @Override |
| 570 | protected WakeLock createWakeLock() { |
| 571 | return mWakeLock; |
| 572 | } |
Lucas Dupin | 8c7cb02 | 2018-02-05 10:49:03 -0800 | [diff] [blame] | 573 | |
| 574 | /** |
| 575 | * Do not wait for a frame since we're in a test environment. |
| 576 | * @param callback What to execute. |
| 577 | */ |
| 578 | @Override |
Lucas Dupin | 0791d97 | 2018-03-26 13:32:16 -0700 | [diff] [blame] | 579 | protected void doOnTheNextFrame(Runnable callback) { |
| 580 | callback.run(); |
Lucas Dupin | 8c7cb02 | 2018-02-05 10:49:03 -0800 | [diff] [blame] | 581 | } |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | } |