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 | |
shawnlin | 317db37 | 2018-04-09 19:49:48 +0800 | [diff] [blame] | 45 | import com.android.internal.colorextraction.ColorExtractor.GradientColors; |
Yohei Yukawa | 795f010 | 2018-04-13 14:55:30 -0700 | [diff] [blame] | 46 | import com.android.internal.util.function.TriConsumer; |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 47 | import com.android.systemui.SysuiTestCase; |
| 48 | import com.android.systemui.statusbar.ScrimView; |
| 49 | import com.android.systemui.util.wakelock.WakeLock; |
| 50 | import com.android.systemui.utils.os.FakeHandler; |
| 51 | |
| 52 | import org.junit.Assert; |
| 53 | import org.junit.Before; |
| 54 | import org.junit.Test; |
| 55 | import org.junit.runner.RunWith; |
| 56 | |
Lucas Dupin | 78949b8 | 2018-04-03 18:54:39 -0700 | [diff] [blame] | 57 | import java.util.Arrays; |
| 58 | import java.util.HashSet; |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 59 | import java.util.function.Consumer; |
| 60 | |
| 61 | @RunWith(AndroidTestingRunner.class) |
| 62 | @TestableLooper.RunWithLooper |
| 63 | @SmallTest |
| 64 | public class ScrimControllerTest extends SysuiTestCase { |
| 65 | |
| 66 | private SynchronousScrimController mScrimController; |
| 67 | private ScrimView mScrimBehind; |
| 68 | private ScrimView mScrimInFront; |
Yohei Yukawa | 795f010 | 2018-04-13 14:55:30 -0700 | [diff] [blame] | 69 | private ScrimState mScrimState; |
shawnlin | 317db37 | 2018-04-09 19:49:48 +0800 | [diff] [blame] | 70 | private float mScrimBehindAlpha; |
| 71 | private GradientColors mScrimInFrontColor; |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 72 | private int mScrimVisibility; |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 73 | private DozeParameters mDozeParamenters; |
| 74 | private WakeLock mWakeLock; |
| 75 | private boolean mAlwaysOnEnabled; |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 76 | private AlarmManager mAlarmManager; |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 77 | |
Yohei Yukawa | 795f010 | 2018-04-13 14:55:30 -0700 | [diff] [blame] | 78 | |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 79 | @Before |
| 80 | public void setup() { |
Lucas Dupin | 067136c | 2018-03-27 18:03:25 -0700 | [diff] [blame] | 81 | mScrimBehind = spy(new ScrimView(getContext())); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 82 | mScrimInFront = new ScrimView(getContext()); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 83 | mWakeLock = mock(WakeLock.class); |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 84 | mAlarmManager = mock(AlarmManager.class); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 85 | mAlwaysOnEnabled = true; |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 86 | mDozeParamenters = mock(DozeParameters.class); |
| 87 | when(mDozeParamenters.getAlwaysOn()).thenAnswer(invocation -> mAlwaysOnEnabled); |
Lucas Dupin | 43d0d73 | 2017-11-16 11:23:49 -0800 | [diff] [blame] | 88 | when(mDozeParamenters.getDisplayNeedsBlanking()).thenReturn(true); |
shawnlin | 317db37 | 2018-04-09 19:49:48 +0800 | [diff] [blame] | 89 | mScrimController = new SynchronousScrimController(mScrimBehind, mScrimInFront, |
Yohei Yukawa | 795f010 | 2018-04-13 14:55:30 -0700 | [diff] [blame] | 90 | (scrimState, scrimBehindAlpha, scrimInFrontColor) -> { |
| 91 | mScrimState = scrimState; |
| 92 | mScrimBehindAlpha = scrimBehindAlpha; |
| 93 | mScrimInFrontColor = scrimInFrontColor; |
| 94 | }, |
| 95 | visible -> mScrimVisibility = visible, mDozeParamenters, mAlarmManager); |
Lucas Dupin | f8463ee | 2018-06-11 16:18:15 -0700 | [diff] [blame] | 96 | mScrimController.setHasBackdrop(false); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @Test |
| 100 | public void initialState() { |
| 101 | Assert.assertEquals("ScrimController should start initialized", |
| 102 | mScrimController.getState(), ScrimState.UNINITIALIZED); |
| 103 | } |
| 104 | |
| 105 | @Test |
| 106 | public void transitionToKeyguard() { |
| 107 | mScrimController.transitionTo(ScrimState.KEYGUARD); |
| 108 | mScrimController.finishAnimationsImmediately(); |
| 109 | // Front scrim should be transparent |
| 110 | // Back scrim should be visible without tint |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 111 | assertScrimVisibility(VISIBILITY_FULLY_TRANSPARENT, VISIBILITY_SEMI_TRANSPARENT); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 112 | assertScrimTint(mScrimBehind, false /* tinted */); |
| 113 | } |
| 114 | |
| 115 | @Test |
Lucas Dupin | 7517b5d | 2017-08-22 12:51:25 -0700 | [diff] [blame] | 116 | public void transitionToAod_withRegularWallpaper() { |
| 117 | mScrimController.transitionTo(ScrimState.AOD); |
| 118 | mScrimController.finishAnimationsImmediately(); |
| 119 | // Front scrim should be transparent |
| 120 | // Back scrim should be visible with tint |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 121 | assertScrimVisibility(VISIBILITY_FULLY_TRANSPARENT, VISIBILITY_FULLY_OPAQUE); |
Lucas Dupin | 7517b5d | 2017-08-22 12:51:25 -0700 | [diff] [blame] | 122 | assertScrimTint(mScrimBehind, true /* tinted */); |
| 123 | assertScrimTint(mScrimInFront, true /* tinted */); |
| 124 | } |
| 125 | |
| 126 | @Test |
| 127 | public void transitionToAod_withAodWallpaper() { |
| 128 | mScrimController.setWallpaperSupportsAmbientMode(true); |
| 129 | mScrimController.transitionTo(ScrimState.AOD); |
| 130 | mScrimController.finishAnimationsImmediately(); |
| 131 | // Front scrim should be transparent |
| 132 | // Back scrim should be transparent |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 133 | assertScrimVisibility(VISIBILITY_FULLY_TRANSPARENT, VISIBILITY_FULLY_TRANSPARENT); |
| 134 | |
| 135 | // Move on to PULSING and check if the back scrim is still transparent |
| 136 | mScrimController.transitionTo(ScrimState.PULSING); |
| 137 | mScrimController.finishAnimationsImmediately(); |
| 138 | assertScrimVisibility(VISIBILITY_FULLY_TRANSPARENT, VISIBILITY_FULLY_TRANSPARENT); |
Lucas Dupin | 7517b5d | 2017-08-22 12:51:25 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | @Test |
| 142 | public void transitionToAod_withAodWallpaperAndLockScreenWallpaper() { |
Lucas Dupin | f8463ee | 2018-06-11 16:18:15 -0700 | [diff] [blame] | 143 | mScrimController.setHasBackdrop(true); |
Lucas Dupin | 7517b5d | 2017-08-22 12:51:25 -0700 | [diff] [blame] | 144 | mScrimController.setWallpaperSupportsAmbientMode(true); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 145 | mScrimController.transitionTo(ScrimState.AOD); |
| 146 | mScrimController.finishAnimationsImmediately(); |
| 147 | // Front scrim should be transparent |
| 148 | // Back scrim should be visible with tint |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 149 | assertScrimVisibility(VISIBILITY_FULLY_TRANSPARENT, VISIBILITY_FULLY_OPAQUE); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 150 | assertScrimTint(mScrimBehind, true /* tinted */); |
| 151 | assertScrimTint(mScrimInFront, true /* tinted */); |
| 152 | } |
| 153 | |
| 154 | @Test |
Lucas Dupin | 218a0cb | 2018-07-09 14:32:53 -0700 | [diff] [blame] | 155 | public void setHasBackdrop_withAodWallpaperAndAlbumArt() { |
| 156 | mScrimController.setWallpaperSupportsAmbientMode(true); |
| 157 | mScrimController.transitionTo(ScrimState.AOD); |
| 158 | mScrimController.finishAnimationsImmediately(); |
| 159 | mScrimController.setHasBackdrop(true); |
| 160 | mScrimController.finishAnimationsImmediately(); |
| 161 | // Front scrim should be transparent |
| 162 | // Back scrim should be visible with tint |
| 163 | assertScrimVisibility(VISIBILITY_FULLY_TRANSPARENT, VISIBILITY_FULLY_OPAQUE); |
| 164 | assertScrimTint(mScrimBehind, true /* tinted */); |
| 165 | assertScrimTint(mScrimInFront, true /* tinted */); |
| 166 | } |
| 167 | |
| 168 | @Test |
Michael Wright | cae8875 | 2018-04-16 23:13:54 +0100 | [diff] [blame] | 169 | public void transitionToAod_withFrontAlphaUpdates() { |
| 170 | // Assert that setting the AOD front scrim alpha doesn't take effect in a non-AOD state. |
| 171 | mScrimController.transitionTo(ScrimState.KEYGUARD); |
| 172 | mScrimController.setAodFrontScrimAlpha(0.5f); |
| 173 | mScrimController.finishAnimationsImmediately(); |
| 174 | // Front scrim should be transparent |
| 175 | // Back scrim should be visible without tint |
| 176 | assertScrimVisibility(VISIBILITY_FULLY_TRANSPARENT, VISIBILITY_SEMI_TRANSPARENT); |
| 177 | |
| 178 | // ... but that it does take effect once we enter the AOD state. |
| 179 | mScrimController.transitionTo(ScrimState.AOD); |
| 180 | mScrimController.finishAnimationsImmediately(); |
| 181 | // Front scrim should be semi-transparent |
| 182 | // Back scrim should be visible |
| 183 | assertScrimVisibility(VISIBILITY_SEMI_TRANSPARENT, VISIBILITY_FULLY_OPAQUE); |
| 184 | |
| 185 | // ... and that if we set it while we're in AOD, it does take immediate effect. |
| 186 | mScrimController.setAodFrontScrimAlpha(1f); |
| 187 | assertScrimVisibility(VISIBILITY_FULLY_OPAQUE, VISIBILITY_FULLY_OPAQUE); |
| 188 | |
| 189 | // ... and make sure we recall the previous front scrim alpha even if we transition away |
| 190 | // for a bit. |
| 191 | mScrimController.transitionTo(ScrimState.UNLOCKED); |
| 192 | mScrimController.transitionTo(ScrimState.AOD); |
| 193 | mScrimController.finishAnimationsImmediately(); |
| 194 | assertScrimVisibility(VISIBILITY_FULLY_OPAQUE, VISIBILITY_FULLY_OPAQUE); |
| 195 | |
Lucas Dupin | 69bda60 | 2018-05-18 17:24:52 -0700 | [diff] [blame] | 196 | // ... and alpha updates should be completely ignored if always_on is off. |
| 197 | // Passing it forward would mess up the wake-up transition. |
| 198 | mAlwaysOnEnabled = false; |
| 199 | mScrimController.transitionTo(ScrimState.UNLOCKED); |
| 200 | mScrimController.transitionTo(ScrimState.AOD); |
| 201 | mScrimController.finishAnimationsImmediately(); |
| 202 | mScrimController.setAodFrontScrimAlpha(0.3f); |
| 203 | Assert.assertEquals(ScrimState.AOD.getFrontAlpha(), mScrimInFront.getViewAlpha(), 0.001f); |
| 204 | Assert.assertNotEquals(0.3f, mScrimInFront.getViewAlpha(), 0.001f); |
| 205 | |
Michael Wright | cae8875 | 2018-04-16 23:13:54 +0100 | [diff] [blame] | 206 | // Reset value since enums are static. |
| 207 | mScrimController.setAodFrontScrimAlpha(0f); |
| 208 | } |
| 209 | |
| 210 | @Test |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 211 | public void transitionToPulsing() { |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 212 | // Pre-condition |
| 213 | // Need to go to AoD first because PULSING doesn't change |
| 214 | // the back scrim opacity - otherwise it would hide AoD wallpapers. |
| 215 | mScrimController.setWallpaperSupportsAmbientMode(false); |
| 216 | mScrimController.transitionTo(ScrimState.AOD); |
| 217 | mScrimController.finishAnimationsImmediately(); |
| 218 | assertScrimVisibility(VISIBILITY_FULLY_TRANSPARENT, VISIBILITY_FULLY_OPAQUE); |
| 219 | |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 220 | mScrimController.transitionTo(ScrimState.PULSING); |
| 221 | mScrimController.finishAnimationsImmediately(); |
| 222 | // Front scrim should be transparent |
| 223 | // Back scrim should be visible with tint |
| 224 | // Pulse callback should have been invoked |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 225 | assertScrimVisibility(VISIBILITY_FULLY_TRANSPARENT, VISIBILITY_FULLY_OPAQUE); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 226 | assertScrimTint(mScrimBehind, true /* tinted */); |
| 227 | } |
| 228 | |
| 229 | @Test |
Lucas Dupin | bc9aac1 | 2018-03-04 20:18:15 -0800 | [diff] [blame] | 230 | public void transitionToKeyguardBouncer() { |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 231 | mScrimController.transitionTo(ScrimState.BOUNCER); |
| 232 | mScrimController.finishAnimationsImmediately(); |
| 233 | // Front scrim should be transparent |
| 234 | // Back scrim should be visible without tint |
Lucas Dupin | bc9aac1 | 2018-03-04 20:18:15 -0800 | [diff] [blame] | 235 | assertScrimVisibility(VISIBILITY_FULLY_TRANSPARENT, VISIBILITY_SEMI_TRANSPARENT); |
| 236 | assertScrimTint(mScrimBehind, false /* tinted */); |
| 237 | } |
| 238 | |
| 239 | @Test |
| 240 | public void transitionToBouncer() { |
Lucas Dupin | 05726cd | 2018-03-13 14:00:24 -0700 | [diff] [blame] | 241 | mScrimController.transitionTo(ScrimState.BOUNCER_SCRIMMED); |
Lucas Dupin | bc9aac1 | 2018-03-04 20:18:15 -0800 | [diff] [blame] | 242 | mScrimController.finishAnimationsImmediately(); |
| 243 | // Front scrim should be transparent |
| 244 | // Back scrim should be visible without tint |
| 245 | assertScrimVisibility(VISIBILITY_SEMI_TRANSPARENT, VISIBILITY_FULLY_TRANSPARENT); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 246 | assertScrimTint(mScrimBehind, false /* tinted */); |
| 247 | } |
| 248 | |
| 249 | @Test |
| 250 | public void transitionToUnlocked() { |
Lucas Dupin | 80a3fcc | 2018-02-07 10:49:55 -0800 | [diff] [blame] | 251 | mScrimController.setPanelExpansion(0f); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 252 | mScrimController.transitionTo(ScrimState.UNLOCKED); |
| 253 | mScrimController.finishAnimationsImmediately(); |
| 254 | // Front scrim should be transparent |
| 255 | // Back scrim should be transparent |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 256 | assertScrimVisibility(VISIBILITY_FULLY_TRANSPARENT, VISIBILITY_FULLY_TRANSPARENT); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 257 | assertScrimTint(mScrimBehind, false /* tinted */); |
| 258 | assertScrimTint(mScrimInFront, false /* tinted */); |
| 259 | |
| 260 | // Back scrim should be visible after start dragging |
| 261 | mScrimController.setPanelExpansion(0.5f); |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 262 | assertScrimVisibility(VISIBILITY_FULLY_TRANSPARENT, VISIBILITY_SEMI_TRANSPARENT); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | @Test |
Yohei Yukawa | 795f010 | 2018-04-13 14:55:30 -0700 | [diff] [blame] | 266 | public void scrimStateCallback() { |
| 267 | mScrimController.transitionTo(ScrimState.UNLOCKED); |
| 268 | mScrimController.finishAnimationsImmediately(); |
| 269 | Assert.assertEquals(mScrimState, ScrimState.UNLOCKED); |
| 270 | |
| 271 | mScrimController.transitionTo(ScrimState.BOUNCER); |
| 272 | mScrimController.finishAnimationsImmediately(); |
| 273 | Assert.assertEquals(mScrimState, ScrimState.BOUNCER); |
| 274 | |
| 275 | mScrimController.transitionTo(ScrimState.BOUNCER_SCRIMMED); |
| 276 | mScrimController.finishAnimationsImmediately(); |
| 277 | Assert.assertEquals(mScrimState, ScrimState.BOUNCER_SCRIMMED); |
| 278 | } |
| 279 | |
| 280 | @Test |
shawnlin | 317db37 | 2018-04-09 19:49:48 +0800 | [diff] [blame] | 281 | public void panelExpansion() { |
| 282 | mScrimController.setPanelExpansion(0f); |
| 283 | mScrimController.setPanelExpansion(0.5f); |
| 284 | mScrimController.transitionTo(ScrimState.UNLOCKED); |
| 285 | mScrimController.finishAnimationsImmediately(); |
| 286 | |
| 287 | reset(mScrimBehind); |
| 288 | mScrimController.setPanelExpansion(0f); |
| 289 | mScrimController.setPanelExpansion(1.0f); |
| 290 | mScrimController.onPreDraw(); |
| 291 | |
| 292 | Assert.assertEquals("Scrim alpha should change after setPanelExpansion", |
| 293 | mScrimBehindAlpha, mScrimBehind.getViewAlpha(), 0.01f); |
| 294 | |
| 295 | mScrimController.setPanelExpansion(0f); |
| 296 | mScrimController.onPreDraw(); |
| 297 | |
| 298 | Assert.assertEquals("Scrim alpha should change after setPanelExpansion", |
| 299 | mScrimBehindAlpha, mScrimBehind.getViewAlpha(), 0.01f); |
| 300 | } |
| 301 | |
| 302 | @Test |
Lucas Dupin | 67f0263 | 2018-03-12 11:08:31 -0700 | [diff] [blame] | 303 | public void panelExpansionAffectsAlpha() { |
| 304 | mScrimController.setPanelExpansion(0f); |
| 305 | mScrimController.setPanelExpansion(0.5f); |
| 306 | mScrimController.transitionTo(ScrimState.UNLOCKED); |
| 307 | mScrimController.finishAnimationsImmediately(); |
| 308 | |
| 309 | final float scrimAlpha = mScrimBehind.getViewAlpha(); |
Lucas Dupin | 067136c | 2018-03-27 18:03:25 -0700 | [diff] [blame] | 310 | reset(mScrimBehind); |
Lucas Dupin | 67f0263 | 2018-03-12 11:08:31 -0700 | [diff] [blame] | 311 | mScrimController.setExpansionAffectsAlpha(false); |
| 312 | mScrimController.setPanelExpansion(0.8f); |
Lucas Dupin | 067136c | 2018-03-27 18:03:25 -0700 | [diff] [blame] | 313 | verifyZeroInteractions(mScrimBehind); |
Lucas Dupin | 67f0263 | 2018-03-12 11:08:31 -0700 | [diff] [blame] | 314 | Assert.assertEquals("Scrim opacity shouldn't change when setExpansionAffectsAlpha " |
| 315 | + "is false", scrimAlpha, mScrimBehind.getViewAlpha(), 0.01f); |
| 316 | |
| 317 | mScrimController.setExpansionAffectsAlpha(true); |
| 318 | mScrimController.setPanelExpansion(0.1f); |
| 319 | Assert.assertNotEquals("Scrim opacity should change when setExpansionAffectsAlpha " |
| 320 | + "is true", scrimAlpha, mScrimBehind.getViewAlpha(), 0.01f); |
| 321 | } |
| 322 | |
| 323 | @Test |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 324 | public void transitionToUnlockedFromAod() { |
| 325 | // Simulate unlock with fingerprint |
| 326 | mScrimController.transitionTo(ScrimState.AOD); |
Lucas Dupin | 80a3fcc | 2018-02-07 10:49:55 -0800 | [diff] [blame] | 327 | mScrimController.setPanelExpansion(0f); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 328 | mScrimController.finishAnimationsImmediately(); |
| 329 | mScrimController.transitionTo(ScrimState.UNLOCKED); |
| 330 | // Immediately tinted after the transition starts |
| 331 | assertScrimTint(mScrimInFront, true /* tinted */); |
| 332 | assertScrimTint(mScrimBehind, true /* tinted */); |
| 333 | mScrimController.finishAnimationsImmediately(); |
| 334 | // Front scrim should be transparent |
| 335 | // Back scrim should be transparent |
| 336 | // Neither scrims should be tinted anymore after the animation. |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 337 | assertScrimVisibility(VISIBILITY_FULLY_TRANSPARENT, VISIBILITY_FULLY_TRANSPARENT); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 338 | assertScrimTint(mScrimInFront, false /* tinted */); |
| 339 | assertScrimTint(mScrimBehind, false /* tinted */); |
| 340 | } |
| 341 | |
| 342 | @Test |
Lucas Dupin | ea0116e | 2018-04-05 10:09:29 -0700 | [diff] [blame] | 343 | public void scrimBlanksBeforeLeavingAod() { |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 344 | // Simulate unlock with fingerprint |
| 345 | mScrimController.transitionTo(ScrimState.AOD); |
| 346 | mScrimController.finishAnimationsImmediately(); |
| 347 | mScrimController.transitionTo(ScrimState.UNLOCKED, |
| 348 | new ScrimController.Callback() { |
| 349 | @Override |
| 350 | public void onDisplayBlanked() { |
| 351 | // Front scrim should be black in the middle of the transition |
| 352 | Assert.assertTrue("Scrim should be visible during transition. Alpha: " |
| 353 | + mScrimInFront.getViewAlpha(), mScrimInFront.getViewAlpha() > 0); |
| 354 | assertScrimTint(mScrimInFront, true /* tinted */); |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 355 | Assert.assertSame("Scrim should be visible during transition.", |
| 356 | mScrimVisibility, VISIBILITY_FULLY_OPAQUE); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 357 | } |
| 358 | }); |
| 359 | mScrimController.finishAnimationsImmediately(); |
| 360 | } |
| 361 | |
| 362 | @Test |
Lucas Dupin | eb840ea | 2018-06-01 00:28:58 -0700 | [diff] [blame] | 363 | public void scrimBlanksWhenUnlockingFromPulse() { |
| 364 | boolean[] blanked = {false}; |
| 365 | // Simulate unlock with fingerprint |
| 366 | mScrimController.transitionTo(ScrimState.PULSING); |
| 367 | mScrimController.finishAnimationsImmediately(); |
| 368 | mScrimController.transitionTo(ScrimState.UNLOCKED, |
| 369 | new ScrimController.Callback() { |
| 370 | @Override |
| 371 | public void onDisplayBlanked() { |
| 372 | blanked[0] = true; |
| 373 | } |
| 374 | }); |
| 375 | mScrimController.finishAnimationsImmediately(); |
| 376 | Assert.assertTrue("Scrim should blank when unlocking from pulse.", blanked[0]); |
| 377 | } |
| 378 | |
| 379 | @Test |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 380 | public void testScrimCallback() { |
| 381 | int[] callOrder = {0, 0, 0}; |
| 382 | int[] currentCall = {0}; |
| 383 | mScrimController.transitionTo(ScrimState.AOD, new ScrimController.Callback() { |
| 384 | @Override |
| 385 | public void onStart() { |
| 386 | callOrder[0] = ++currentCall[0]; |
| 387 | } |
| 388 | |
| 389 | @Override |
| 390 | public void onDisplayBlanked() { |
| 391 | callOrder[1] = ++currentCall[0]; |
| 392 | } |
| 393 | |
| 394 | @Override |
| 395 | public void onFinished() { |
| 396 | callOrder[2] = ++currentCall[0]; |
| 397 | } |
| 398 | }); |
| 399 | mScrimController.finishAnimationsImmediately(); |
| 400 | Assert.assertEquals("onStart called in wrong order", 1, callOrder[0]); |
| 401 | Assert.assertEquals("onDisplayBlanked called in wrong order", 2, callOrder[1]); |
| 402 | Assert.assertEquals("onFinished called in wrong order", 3, callOrder[2]); |
| 403 | } |
| 404 | |
| 405 | @Test |
| 406 | public void testScrimCallbacksWithoutAmbientDisplay() { |
| 407 | mAlwaysOnEnabled = false; |
| 408 | testScrimCallback(); |
| 409 | } |
| 410 | |
| 411 | @Test |
| 412 | public void testScrimCallbackCancelled() { |
| 413 | boolean[] cancelledCalled = {false}; |
| 414 | mScrimController.transitionTo(ScrimState.AOD, new ScrimController.Callback() { |
| 415 | @Override |
| 416 | public void onCancelled() { |
| 417 | cancelledCalled[0] = true; |
| 418 | } |
| 419 | }); |
| 420 | mScrimController.transitionTo(ScrimState.PULSING); |
| 421 | Assert.assertTrue("onCancelled should have been called", cancelledCalled[0]); |
| 422 | } |
| 423 | |
| 424 | @Test |
Lucas Dupin | eea53b3 | 2017-12-18 13:47:14 -0800 | [diff] [blame] | 425 | public void testHoldsWakeLock_whenAOD() { |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 426 | mScrimController.transitionTo(ScrimState.AOD); |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 427 | verify(mWakeLock).acquire(); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 428 | verify(mWakeLock, never()).release(); |
| 429 | mScrimController.finishAnimationsImmediately(); |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 430 | verify(mWakeLock).release(); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 431 | } |
| 432 | |
Lucas Dupin | 19aba8e | 2017-12-11 12:42:26 -0800 | [diff] [blame] | 433 | @Test |
Lucas Dupin | eea53b3 | 2017-12-18 13:47:14 -0800 | [diff] [blame] | 434 | public void testDoesNotHoldWakeLock_whenUnlocking() { |
| 435 | mScrimController.transitionTo(ScrimState.UNLOCKED); |
| 436 | mScrimController.finishAnimationsImmediately(); |
| 437 | verifyZeroInteractions(mWakeLock); |
| 438 | } |
| 439 | |
| 440 | @Test |
Lucas Dupin | 19aba8e | 2017-12-11 12:42:26 -0800 | [diff] [blame] | 441 | public void testCallbackInvokedOnSameStateTransition() { |
| 442 | mScrimController.transitionTo(ScrimState.UNLOCKED); |
| 443 | mScrimController.finishAnimationsImmediately(); |
| 444 | ScrimController.Callback callback = mock(ScrimController.Callback.class); |
| 445 | mScrimController.transitionTo(ScrimState.UNLOCKED, callback); |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 446 | verify(callback).onFinished(); |
| 447 | } |
| 448 | |
| 449 | @Test |
| 450 | public void testHoldsAodWallpaperAnimationLock() { |
| 451 | // Pre-conditions |
| 452 | mScrimController.transitionTo(ScrimState.AOD); |
| 453 | mScrimController.finishAnimationsImmediately(); |
| 454 | reset(mWakeLock); |
| 455 | |
| 456 | mScrimController.onHideWallpaperTimeout(); |
| 457 | verify(mWakeLock).acquire(); |
| 458 | verify(mWakeLock, never()).release(); |
| 459 | mScrimController.finishAnimationsImmediately(); |
| 460 | verify(mWakeLock).release(); |
| 461 | } |
| 462 | |
| 463 | @Test |
Lucas Dupin | 16cfe45 | 2018-02-08 13:14:50 -0800 | [diff] [blame] | 464 | public void testWillHideAodWallpaper() { |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 465 | mScrimController.setWallpaperSupportsAmbientMode(true); |
| 466 | mScrimController.transitionTo(ScrimState.AOD); |
| 467 | verify(mAlarmManager).setExact(anyInt(), anyLong(), any(), any(), any()); |
| 468 | mScrimController.transitionTo(ScrimState.KEYGUARD); |
| 469 | verify(mAlarmManager).cancel(any(AlarmManager.OnAlarmListener.class)); |
Lucas Dupin | 19aba8e | 2017-12-11 12:42:26 -0800 | [diff] [blame] | 470 | } |
| 471 | |
Lucas Dupin | 80a3fcc | 2018-02-07 10:49:55 -0800 | [diff] [blame] | 472 | @Test |
| 473 | public void testConservesExpansionOpacityAfterTransition() { |
| 474 | mScrimController.transitionTo(ScrimState.UNLOCKED); |
| 475 | mScrimController.setPanelExpansion(0.5f); |
| 476 | mScrimController.finishAnimationsImmediately(); |
| 477 | |
| 478 | final float expandedAlpha = mScrimBehind.getViewAlpha(); |
| 479 | |
| 480 | mScrimController.transitionTo(ScrimState.BRIGHTNESS_MIRROR); |
| 481 | mScrimController.finishAnimationsImmediately(); |
| 482 | mScrimController.transitionTo(ScrimState.UNLOCKED); |
| 483 | mScrimController.finishAnimationsImmediately(); |
| 484 | |
| 485 | Assert.assertEquals("Scrim expansion opacity wasn't conserved when transitioning back", |
| 486 | expandedAlpha, mScrimBehind.getViewAlpha(), 0.01f); |
| 487 | } |
| 488 | |
| 489 | @Test |
| 490 | public void cancelsOldAnimationBeforeBlanking() { |
| 491 | mScrimController.transitionTo(ScrimState.AOD); |
| 492 | mScrimController.finishAnimationsImmediately(); |
| 493 | // Consume whatever value we had before |
| 494 | mScrimController.wasAnimationJustCancelled(); |
| 495 | |
| 496 | mScrimController.transitionTo(ScrimState.KEYGUARD); |
| 497 | mScrimController.finishAnimationsImmediately(); |
| 498 | Assert.assertTrue(mScrimController.wasAnimationJustCancelled()); |
| 499 | } |
| 500 | |
Lucas Dupin | b380c88 | 2018-02-25 21:57:17 -0800 | [diff] [blame] | 501 | /** |
| 502 | * Number of visible notifications affects scrim opacity. |
| 503 | */ |
| 504 | @Test |
| 505 | public void testNotificationDensity() { |
| 506 | mScrimController.transitionTo(ScrimState.KEYGUARD); |
| 507 | mScrimController.finishAnimationsImmediately(); |
| 508 | |
| 509 | mScrimController.setNotificationCount(0); |
| 510 | mScrimController.finishAnimationsImmediately(); |
| 511 | Assert.assertEquals("lower density when no notifications", |
| 512 | ScrimController.GRADIENT_SCRIM_ALPHA, mScrimBehind.getViewAlpha(), 0.01f); |
| 513 | |
| 514 | mScrimController.setNotificationCount(3); |
| 515 | mScrimController.finishAnimationsImmediately(); |
| 516 | Assert.assertEquals("stronger density when notifications are visible", |
| 517 | ScrimController.GRADIENT_SCRIM_ALPHA_BUSY, mScrimBehind.getViewAlpha(), 0.01f); |
| 518 | } |
| 519 | |
| 520 | /** |
| 521 | * Moving from/to states conserves old notification density. |
| 522 | */ |
| 523 | @Test |
| 524 | public void testConservesNotificationDensity() { |
| 525 | testConservesNotificationDensity(0 /* count */, ScrimController.GRADIENT_SCRIM_ALPHA); |
| 526 | testConservesNotificationDensity(3 /* count */, ScrimController.GRADIENT_SCRIM_ALPHA_BUSY); |
| 527 | } |
| 528 | |
Lucas Dupin | 3daf3b0 | 2018-02-27 17:23:41 -0800 | [diff] [blame] | 529 | @Test |
Lucas Dupin | 38962d7 | 2018-03-14 12:41:39 -0700 | [diff] [blame] | 530 | public void testScrimFocus() { |
| 531 | mScrimController.transitionTo(ScrimState.AOD); |
| 532 | Assert.assertFalse("Should not be focusable on AOD", mScrimBehind.isFocusable()); |
| 533 | Assert.assertFalse("Should not be focusable on AOD", mScrimInFront.isFocusable()); |
| 534 | |
| 535 | mScrimController.transitionTo(ScrimState.KEYGUARD); |
| 536 | Assert.assertTrue("Should be focusable on keyguard", mScrimBehind.isFocusable()); |
| 537 | Assert.assertTrue("Should be focusable on keyguard", mScrimInFront.isFocusable()); |
| 538 | } |
| 539 | |
Lucas Dupin | d510730 | 2018-03-19 15:30:29 -0700 | [diff] [blame] | 540 | @Test |
| 541 | public void testHidesShowWhenLockedActivity() { |
| 542 | mScrimController.setWallpaperSupportsAmbientMode(true); |
| 543 | mScrimController.setKeyguardOccluded(true); |
| 544 | mScrimController.transitionTo(ScrimState.AOD); |
| 545 | mScrimController.finishAnimationsImmediately(); |
| 546 | assertScrimVisibility(VISIBILITY_FULLY_TRANSPARENT, VISIBILITY_FULLY_OPAQUE); |
| 547 | |
| 548 | mScrimController.transitionTo(ScrimState.PULSING); |
| 549 | mScrimController.finishAnimationsImmediately(); |
| 550 | assertScrimVisibility(VISIBILITY_FULLY_TRANSPARENT, VISIBILITY_FULLY_OPAQUE); |
| 551 | } |
| 552 | |
Lucas Dupin | 78949b8 | 2018-04-03 18:54:39 -0700 | [diff] [blame] | 553 | @Test |
Lucas Dupin | 63d7217 | 2018-06-06 11:42:55 -0700 | [diff] [blame] | 554 | public void testHidesShowWhenLockedActivity_whenAlreadyInAod() { |
| 555 | mScrimController.setWallpaperSupportsAmbientMode(true); |
| 556 | mScrimController.transitionTo(ScrimState.AOD); |
| 557 | mScrimController.finishAnimationsImmediately(); |
| 558 | assertScrimVisibility(VISIBILITY_FULLY_TRANSPARENT, VISIBILITY_FULLY_TRANSPARENT); |
| 559 | |
| 560 | mScrimController.setKeyguardOccluded(true); |
| 561 | mScrimController.finishAnimationsImmediately(); |
| 562 | assertScrimVisibility(VISIBILITY_FULLY_TRANSPARENT, VISIBILITY_FULLY_OPAQUE); |
| 563 | } |
| 564 | |
| 565 | @Test |
Lucas Dupin | 78949b8 | 2018-04-03 18:54:39 -0700 | [diff] [blame] | 566 | public void testEatsTouchEvent() { |
| 567 | HashSet<ScrimState> eatsTouches = |
| 568 | new HashSet<>(Arrays.asList(ScrimState.AOD, ScrimState.PULSING)); |
| 569 | for (ScrimState state : ScrimState.values()) { |
| 570 | if (state == ScrimState.UNINITIALIZED) { |
| 571 | continue; |
| 572 | } |
| 573 | mScrimController.transitionTo(state); |
| 574 | mScrimController.finishAnimationsImmediately(); |
| 575 | Assert.assertEquals("Should be clickable unless AOD or PULSING, was: " + state, |
| 576 | mScrimBehind.getViewAlpha() != 0 && !eatsTouches.contains(state), |
| 577 | mScrimBehind.isClickable()); |
| 578 | } |
| 579 | } |
| 580 | |
Lucas Dupin | ea0116e | 2018-04-05 10:09:29 -0700 | [diff] [blame] | 581 | @Test |
| 582 | public void testAnimatesTransitionToAod() { |
| 583 | when(mDozeParamenters.shouldControlScreenOff()).thenReturn(false); |
| 584 | ScrimState.AOD.prepare(ScrimState.KEYGUARD); |
| 585 | Assert.assertFalse("No animation when ColorFade kicks in", |
| 586 | ScrimState.AOD.getAnimateChange()); |
| 587 | |
| 588 | reset(mDozeParamenters); |
| 589 | when(mDozeParamenters.shouldControlScreenOff()).thenReturn(true); |
| 590 | ScrimState.AOD.prepare(ScrimState.KEYGUARD); |
| 591 | Assert.assertTrue("Animate scrims when ColorFade won't be triggered", |
| 592 | ScrimState.AOD.getAnimateChange()); |
| 593 | } |
| 594 | |
Lucas Dupin | 373356b | 2018-04-07 10:50:25 -0700 | [diff] [blame] | 595 | @Test |
| 596 | public void testViewsDontHaveFocusHighlight() { |
| 597 | Assert.assertFalse("Scrim shouldn't have focus highlight", |
| 598 | mScrimInFront.getDefaultFocusHighlightEnabled()); |
| 599 | Assert.assertFalse("Scrim shouldn't have focus highlight", |
| 600 | mScrimBehind.getDefaultFocusHighlightEnabled()); |
| 601 | } |
| 602 | |
Lucas Dupin | b380c88 | 2018-02-25 21:57:17 -0800 | [diff] [blame] | 603 | /** |
| 604 | * Conserves old notification density after leaving state and coming back. |
| 605 | * |
| 606 | * @param count How many notification. |
| 607 | * @param expectedAlpha Expected alpha. |
| 608 | */ |
| 609 | private void testConservesNotificationDensity(int count, float expectedAlpha) { |
| 610 | mScrimController.setNotificationCount(count); |
| 611 | mScrimController.transitionTo(ScrimState.UNLOCKED); |
| 612 | mScrimController.finishAnimationsImmediately(); |
| 613 | |
| 614 | mScrimController.transitionTo(ScrimState.KEYGUARD); |
| 615 | mScrimController.finishAnimationsImmediately(); |
| 616 | |
| 617 | Assert.assertEquals("Doesn't respect notification busyness after transition", |
| 618 | expectedAlpha, mScrimBehind.getViewAlpha(), 0.01f); |
| 619 | } |
| 620 | |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 621 | private void assertScrimTint(ScrimView scrimView, boolean tinted) { |
| 622 | final boolean viewIsTinted = scrimView.getTint() != Color.TRANSPARENT; |
| 623 | final String name = scrimView == mScrimInFront ? "front" : "back"; |
| 624 | Assert.assertEquals("Tint test failed at state " + mScrimController.getState() |
| 625 | +" with scrim: " + name + " and tint: " + Integer.toHexString(scrimView.getTint()), |
| 626 | tinted, viewIsTinted); |
| 627 | } |
| 628 | |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 629 | private void assertScrimVisibility(int inFront, int behind) { |
| 630 | boolean inFrontVisible = inFront != ScrimController.VISIBILITY_FULLY_TRANSPARENT; |
| 631 | boolean behindVisible = behind != ScrimController.VISIBILITY_FULLY_TRANSPARENT; |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 632 | Assert.assertEquals("Unexpected front scrim visibility. Alpha is " |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 633 | + mScrimInFront.getViewAlpha(), inFrontVisible, mScrimInFront.getViewAlpha() > 0); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 634 | Assert.assertEquals("Unexpected back scrim visibility. Alpha is " |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 635 | + mScrimBehind.getViewAlpha(), behindVisible, mScrimBehind.getViewAlpha() > 0); |
| 636 | |
| 637 | final int visibility; |
| 638 | if (inFront == VISIBILITY_FULLY_OPAQUE || behind == VISIBILITY_FULLY_OPAQUE) { |
| 639 | visibility = VISIBILITY_FULLY_OPAQUE; |
| 640 | } else if (inFront > VISIBILITY_FULLY_TRANSPARENT || behind > VISIBILITY_FULLY_TRANSPARENT) { |
| 641 | visibility = VISIBILITY_SEMI_TRANSPARENT; |
| 642 | } else { |
| 643 | visibility = VISIBILITY_FULLY_TRANSPARENT; |
| 644 | } |
| 645 | Assert.assertEquals("Invalid visibility.", visibility, mScrimVisibility); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | /** |
| 649 | * Special version of ScrimController where animations have 0 duration for test purposes. |
| 650 | */ |
| 651 | private class SynchronousScrimController extends ScrimController { |
| 652 | |
| 653 | private FakeHandler mHandler; |
Lucas Dupin | 80a3fcc | 2018-02-07 10:49:55 -0800 | [diff] [blame] | 654 | private boolean mAnimationCancelled; |
Lucas Dupin | 16cfe45 | 2018-02-08 13:14:50 -0800 | [diff] [blame] | 655 | boolean mOnPreDrawCalled; |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 656 | |
shawnlin | 317db37 | 2018-04-09 19:49:48 +0800 | [diff] [blame] | 657 | SynchronousScrimController(ScrimView scrimBehind, ScrimView scrimInFront, |
Yohei Yukawa | 795f010 | 2018-04-13 14:55:30 -0700 | [diff] [blame] | 658 | TriConsumer<ScrimState, Float, GradientColors> scrimStateListener, |
Lucas Dupin | 82aa163 | 2017-12-13 00:13:57 -0800 | [diff] [blame] | 659 | Consumer<Integer> scrimVisibleListener, DozeParameters dozeParameters, |
| 660 | AlarmManager alarmManager) { |
Yohei Yukawa | 795f010 | 2018-04-13 14:55:30 -0700 | [diff] [blame] | 661 | super(scrimBehind, scrimInFront, scrimStateListener, scrimVisibleListener, |
| 662 | dozeParameters, alarmManager); |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 663 | mHandler = new FakeHandler(Looper.myLooper()); |
| 664 | } |
| 665 | |
Lucas Dupin | 16cfe45 | 2018-02-08 13:14:50 -0800 | [diff] [blame] | 666 | @Override |
| 667 | public boolean onPreDraw() { |
| 668 | mOnPreDrawCalled = true; |
| 669 | return super.onPreDraw(); |
| 670 | } |
| 671 | |
Lucas Dupin | 3503c5f | 2018-03-02 19:04:00 -0800 | [diff] [blame] | 672 | void finishAnimationsImmediately() { |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 673 | boolean[] animationFinished = {false}; |
| 674 | setOnAnimationFinished(()-> animationFinished[0] = true); |
| 675 | |
| 676 | // Execute code that will trigger animations. |
| 677 | onPreDraw(); |
| 678 | |
| 679 | // Force finish screen blanking. |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 680 | mHandler.dispatchQueuedMessages(); |
| 681 | // Force finish all animations. |
| 682 | endAnimation(mScrimBehind, TAG_KEY_ANIM); |
| 683 | endAnimation(mScrimInFront, TAG_KEY_ANIM); |
| 684 | |
| 685 | if (!animationFinished[0]) { |
| 686 | throw new IllegalStateException("Animation never finished"); |
| 687 | } |
| 688 | } |
| 689 | |
Lucas Dupin | 3503c5f | 2018-03-02 19:04:00 -0800 | [diff] [blame] | 690 | boolean wasAnimationJustCancelled() { |
Lucas Dupin | 80a3fcc | 2018-02-07 10:49:55 -0800 | [diff] [blame] | 691 | final boolean wasCancelled = mAnimationCancelled; |
| 692 | mAnimationCancelled = false; |
| 693 | return wasCancelled; |
| 694 | } |
| 695 | |
Lucas Dupin | 3daf3b0 | 2018-02-27 17:23:41 -0800 | [diff] [blame] | 696 | private void endAnimation(View scrimView, int tag) { |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 697 | Animator animator = (Animator) scrimView.getTag(tag); |
| 698 | if (animator != null) { |
| 699 | animator.end(); |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | @Override |
Lucas Dupin | 80a3fcc | 2018-02-07 10:49:55 -0800 | [diff] [blame] | 704 | protected void cancelAnimator(ValueAnimator previousAnimator) { |
| 705 | super.cancelAnimator(previousAnimator); |
| 706 | mAnimationCancelled = true; |
| 707 | } |
| 708 | |
| 709 | @Override |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 710 | protected Handler getHandler() { |
| 711 | return mHandler; |
| 712 | } |
| 713 | |
| 714 | @Override |
| 715 | protected WakeLock createWakeLock() { |
| 716 | return mWakeLock; |
| 717 | } |
Lucas Dupin | 8c7cb02 | 2018-02-05 10:49:03 -0800 | [diff] [blame] | 718 | |
| 719 | /** |
| 720 | * Do not wait for a frame since we're in a test environment. |
| 721 | * @param callback What to execute. |
| 722 | */ |
| 723 | @Override |
Lucas Dupin | 0791d97 | 2018-03-26 13:32:16 -0700 | [diff] [blame] | 724 | protected void doOnTheNextFrame(Runnable callback) { |
| 725 | callback.run(); |
Lucas Dupin | 8c7cb02 | 2018-02-05 10:49:03 -0800 | [diff] [blame] | 726 | } |
Lucas Dupin | 9e3fa10 | 2017-11-08 17:16:55 -0800 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | } |