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