Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [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 |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 14 | * limitations under the License. |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | package com.android.server.wm; |
| 18 | |
Tadashi G. Takaoka | bf0d57b | 2018-11-19 16:09:58 +0900 | [diff] [blame^] | 19 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.any; |
| 20 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.atLeast; |
| 21 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.atLeastOnce; |
| 22 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.eq; |
| 23 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify; |
| 24 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.when; |
| 25 | |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 26 | import static org.junit.Assert.assertEquals; |
| 27 | import static org.junit.Assert.assertFalse; |
| 28 | import static org.junit.Assert.assertTrue; |
Jorim Jaggi | b6ba433 | 2018-07-10 00:36:57 +0200 | [diff] [blame] | 29 | import static org.mockito.ArgumentMatchers.anyInt; |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 30 | |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 31 | import static java.util.concurrent.TimeUnit.SECONDS; |
| 32 | |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 33 | import android.animation.AnimationHandler.AnimationFrameCallbackProvider; |
Jorim Jaggi | 6c7e561 | 2017-12-12 02:17:10 +0100 | [diff] [blame] | 34 | import android.animation.ValueAnimator; |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 35 | import android.graphics.Matrix; |
| 36 | import android.graphics.Point; |
Jorim Jaggi | b6ba433 | 2018-07-10 00:36:57 +0200 | [diff] [blame] | 37 | import android.os.PowerManagerInternal; |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 38 | import android.platform.test.annotations.Presubmit; |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 39 | import android.view.Choreographer; |
| 40 | import android.view.Choreographer.FrameCallback; |
| 41 | import android.view.SurfaceControl; |
| 42 | import android.view.SurfaceControl.Transaction; |
| 43 | import android.view.animation.Animation; |
| 44 | import android.view.animation.TranslateAnimation; |
| 45 | |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 46 | import androidx.test.filters.FlakyTest; |
| 47 | import androidx.test.filters.SmallTest; |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 48 | |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 49 | import com.android.server.wm.LocalAnimationAdapter.AnimationSpec; |
| 50 | |
| 51 | import org.junit.Before; |
| 52 | import org.junit.Rule; |
| 53 | import org.junit.Test; |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 54 | import org.mockito.Mock; |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 55 | import org.mockito.MockitoAnnotations; |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 56 | import org.mockito.junit.MockitoJUnit; |
| 57 | import org.mockito.junit.MockitoRule; |
| 58 | |
| 59 | import java.util.concurrent.CountDownLatch; |
| 60 | |
| 61 | /** |
| 62 | * Test class for {@link SurfaceAnimationRunner}. |
| 63 | * |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 64 | * Build/Install/Run: |
| 65 | * atest FrameworksServicesTests:SurfaceAnimationRunnerTest |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 66 | */ |
| 67 | @SmallTest |
| 68 | @Presubmit |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 69 | public class SurfaceAnimationRunnerTest extends WindowTestsBase { |
| 70 | |
| 71 | @Mock SurfaceControl mMockSurface; |
| 72 | @Mock Transaction mMockTransaction; |
Jorim Jaggi | 6c7e561 | 2017-12-12 02:17:10 +0100 | [diff] [blame] | 73 | @Mock AnimationSpec mMockAnimationSpec; |
Jorim Jaggi | b6ba433 | 2018-07-10 00:36:57 +0200 | [diff] [blame] | 74 | @Mock PowerManagerInternal mMockPowerManager; |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 75 | @Rule public MockitoRule mMockitoRule = MockitoJUnit.rule(); |
| 76 | |
| 77 | private SurfaceAnimationRunner mSurfaceAnimationRunner; |
| 78 | private CountDownLatch mFinishCallbackLatch; |
| 79 | |
| 80 | @Before |
| 81 | public void setUp() throws Exception { |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 82 | MockitoAnnotations.initMocks(this); |
| 83 | |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 84 | mFinishCallbackLatch = new CountDownLatch(1); |
Jorim Jaggi | 6c7e561 | 2017-12-12 02:17:10 +0100 | [diff] [blame] | 85 | mSurfaceAnimationRunner = new SurfaceAnimationRunner(null /* callbackProvider */, null, |
Jorim Jaggi | b6ba433 | 2018-07-10 00:36:57 +0200 | [diff] [blame] | 86 | mMockTransaction, mMockPowerManager); |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | private void finishedCallback() { |
| 90 | mFinishCallbackLatch.countDown(); |
| 91 | } |
| 92 | |
| 93 | @Test |
| 94 | public void testAnimation() throws Exception { |
| 95 | mSurfaceAnimationRunner |
| 96 | .startAnimation(createTranslateAnimation(), mMockSurface, mMockTransaction, |
| 97 | this::finishedCallback); |
| 98 | |
| 99 | // Ensure that the initial transformation has been applied. |
| 100 | final Matrix m = new Matrix(); |
| 101 | m.setTranslate(-10, 0); |
| 102 | verify(mMockTransaction, atLeastOnce()).setMatrix(eq(mMockSurface), eq(m), any()); |
| 103 | verify(mMockTransaction, atLeastOnce()).setAlpha(eq(mMockSurface), eq(1.0f)); |
| 104 | |
| 105 | mFinishCallbackLatch.await(1, SECONDS); |
| 106 | assertFinishCallbackCalled(); |
| 107 | |
| 108 | m.setTranslate(10, 0); |
| 109 | verify(mMockTransaction, atLeastOnce()).setMatrix(eq(mMockSurface), eq(m), any()); |
| 110 | |
| 111 | // At least 3 times: After initialization, first frame, last frame. |
| 112 | verify(mMockTransaction, atLeast(3)).setAlpha(eq(mMockSurface), eq(1.0f)); |
| 113 | } |
| 114 | |
| 115 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 116 | public void testCancel_notStarted() { |
Jorim Jaggi | 6c7e561 | 2017-12-12 02:17:10 +0100 | [diff] [blame] | 117 | mSurfaceAnimationRunner = new SurfaceAnimationRunner(new NoOpFrameCallbackProvider(), null, |
Jorim Jaggi | b6ba433 | 2018-07-10 00:36:57 +0200 | [diff] [blame] | 118 | mMockTransaction, mMockPowerManager); |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 119 | mSurfaceAnimationRunner |
| 120 | .startAnimation(createTranslateAnimation(), mMockSurface, mMockTransaction, |
| 121 | this::finishedCallback); |
| 122 | mSurfaceAnimationRunner.onAnimationCancelled(mMockSurface); |
| 123 | waitUntilHandlersIdle(); |
| 124 | assertTrue(mSurfaceAnimationRunner.mPendingAnimations.isEmpty()); |
| 125 | assertFinishCallbackNotCalled(); |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | @Test |
| 129 | public void testCancel_running() throws Exception { |
Jorim Jaggi | 6c7e561 | 2017-12-12 02:17:10 +0100 | [diff] [blame] | 130 | mSurfaceAnimationRunner = new SurfaceAnimationRunner(new NoOpFrameCallbackProvider(), null, |
Jorim Jaggi | b6ba433 | 2018-07-10 00:36:57 +0200 | [diff] [blame] | 131 | mMockTransaction, mMockPowerManager); |
Jorim Jaggi | 6c7e561 | 2017-12-12 02:17:10 +0100 | [diff] [blame] | 132 | mSurfaceAnimationRunner.startAnimation(createTranslateAnimation(), mMockSurface, |
| 133 | mMockTransaction, this::finishedCallback); |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 134 | waitUntilNextFrame(); |
| 135 | assertFalse(mSurfaceAnimationRunner.mRunningAnimations.isEmpty()); |
| 136 | mSurfaceAnimationRunner.onAnimationCancelled(mMockSurface); |
| 137 | assertTrue(mSurfaceAnimationRunner.mRunningAnimations.isEmpty()); |
| 138 | waitUntilHandlersIdle(); |
| 139 | assertFinishCallbackNotCalled(); |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 140 | } |
| 141 | |
chaviw | 0fb741f | 2018-01-08 16:18:03 -0800 | [diff] [blame] | 142 | @FlakyTest(bugId = 71719744) |
Jorim Jaggi | 6c7e561 | 2017-12-12 02:17:10 +0100 | [diff] [blame] | 143 | @Test |
| 144 | public void testCancel_sneakyCancelBeforeUpdate() throws Exception { |
| 145 | mSurfaceAnimationRunner = new SurfaceAnimationRunner(null, () -> new ValueAnimator() { |
| 146 | { |
| 147 | setFloatValues(0f, 1f); |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public void addUpdateListener(AnimatorUpdateListener listener) { |
| 152 | super.addUpdateListener(animation -> { |
| 153 | // Sneaky test cancels animation just before applying frame to simulate |
| 154 | // interleaving of multiple threads. Muahahaha |
| 155 | if (animation.getCurrentPlayTime() > 0) { |
| 156 | mSurfaceAnimationRunner.onAnimationCancelled(mMockSurface); |
| 157 | } |
| 158 | listener.onAnimationUpdate(animation); |
| 159 | }); |
| 160 | } |
Jorim Jaggi | b6ba433 | 2018-07-10 00:36:57 +0200 | [diff] [blame] | 161 | }, mMockTransaction, mMockPowerManager); |
Jorim Jaggi | 6c7e561 | 2017-12-12 02:17:10 +0100 | [diff] [blame] | 162 | when(mMockAnimationSpec.getDuration()).thenReturn(200L); |
| 163 | mSurfaceAnimationRunner.startAnimation(mMockAnimationSpec, mMockSurface, mMockTransaction, |
| 164 | this::finishedCallback); |
Jorim Jaggi | 4130a68 | 2018-01-09 14:28:44 +0100 | [diff] [blame] | 165 | |
| 166 | // We need to wait for two frames: The first frame starts the animation, the second frame |
| 167 | // actually cancels the animation. |
Jorim Jaggi | 6c7e561 | 2017-12-12 02:17:10 +0100 | [diff] [blame] | 168 | waitUntilNextFrame(); |
Jorim Jaggi | 4130a68 | 2018-01-09 14:28:44 +0100 | [diff] [blame] | 169 | waitUntilNextFrame(); |
| 170 | assertTrue(mSurfaceAnimationRunner.mRunningAnimations.isEmpty()); |
Jorim Jaggi | 6c7e561 | 2017-12-12 02:17:10 +0100 | [diff] [blame] | 171 | verify(mMockAnimationSpec, atLeastOnce()).apply(any(), any(), eq(0L)); |
| 172 | } |
| 173 | |
Jorim Jaggi | d0a9437 | 2018-03-15 12:58:49 +0100 | [diff] [blame] | 174 | @FlakyTest(bugId = 74780584) |
Jorim Jaggi | d6d9716 | 2018-01-05 18:28:36 +0100 | [diff] [blame] | 175 | @Test |
| 176 | public void testDeferStartingAnimations() throws Exception { |
| 177 | mSurfaceAnimationRunner.deferStartingAnimations(); |
| 178 | mSurfaceAnimationRunner.startAnimation(createTranslateAnimation(), mMockSurface, |
| 179 | mMockTransaction, this::finishedCallback); |
| 180 | waitUntilNextFrame(); |
| 181 | assertTrue(mSurfaceAnimationRunner.mRunningAnimations.isEmpty()); |
| 182 | mSurfaceAnimationRunner.continueStartingAnimations(); |
| 183 | waitUntilNextFrame(); |
| 184 | assertFalse(mSurfaceAnimationRunner.mRunningAnimations.isEmpty()); |
| 185 | mFinishCallbackLatch.await(1, SECONDS); |
| 186 | assertFinishCallbackCalled(); |
| 187 | } |
| 188 | |
Jorim Jaggi | b6ba433 | 2018-07-10 00:36:57 +0200 | [diff] [blame] | 189 | @Test |
| 190 | public void testPowerHint() throws Exception { |
| 191 | mSurfaceAnimationRunner = new SurfaceAnimationRunner(new NoOpFrameCallbackProvider(), null, |
| 192 | mMockTransaction, mMockPowerManager); |
| 193 | mSurfaceAnimationRunner.startAnimation(createTranslateAnimation(), mMockSurface, |
| 194 | mMockTransaction, this::finishedCallback); |
| 195 | waitUntilNextFrame(); |
| 196 | |
| 197 | // TODO: For some reason we don't have access to PowerHint definition from the tests. For |
| 198 | // now let's just verify that we got some kind of hint. |
| 199 | verify(mMockPowerManager).powerHint(anyInt(), anyInt()); |
| 200 | } |
| 201 | |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 202 | private void waitUntilNextFrame() throws Exception { |
| 203 | final CountDownLatch latch = new CountDownLatch(1); |
| 204 | mSurfaceAnimationRunner.mChoreographer.postCallback(Choreographer.CALLBACK_COMMIT, |
| 205 | latch::countDown, null /* token */); |
| 206 | latch.await(); |
| 207 | } |
| 208 | |
| 209 | private void assertFinishCallbackCalled() { |
| 210 | assertEquals(0, mFinishCallbackLatch.getCount()); |
| 211 | } |
| 212 | |
| 213 | private void assertFinishCallbackNotCalled() { |
| 214 | assertEquals(1, mFinishCallbackLatch.getCount()); |
| 215 | } |
| 216 | |
| 217 | private AnimationSpec createTranslateAnimation() { |
| 218 | final Animation a = new TranslateAnimation(-10, 10, 0, 0); |
| 219 | a.initialize(0, 0, 0, 0); |
| 220 | a.setDuration(50); |
Jorim Jaggi | 2e3c31d | 2017-11-20 19:49:00 +0100 | [diff] [blame] | 221 | return new WindowAnimationSpec(a, new Point(0, 0), false /* canSkipFirstFrame */); |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Callback provider that doesn't animate at all. |
| 226 | */ |
| 227 | private static final class NoOpFrameCallbackProvider implements AnimationFrameCallbackProvider { |
| 228 | |
| 229 | @Override |
| 230 | public void postFrameCallback(FrameCallback callback) { |
| 231 | } |
| 232 | |
| 233 | @Override |
| 234 | public void postCommitCallback(Runnable runnable) { |
| 235 | } |
| 236 | |
| 237 | @Override |
| 238 | public long getFrameTime() { |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public long getFrameDelay() { |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | @Override |
| 248 | public void setFrameDelay(long delay) { |
| 249 | } |
| 250 | } |
| 251 | } |