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 |
| 14 | * limitations under the License |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.wm; |
| 18 | |
| 19 | import static java.util.concurrent.TimeUnit.SECONDS; |
| 20 | import static org.junit.Assert.assertEquals; |
| 21 | import static org.junit.Assert.assertFalse; |
| 22 | import static org.junit.Assert.assertTrue; |
| 23 | import static org.mockito.Mockito.any; |
| 24 | import static org.mockito.Mockito.atLeast; |
| 25 | import static org.mockito.Mockito.atLeastOnce; |
| 26 | import static org.mockito.Mockito.eq; |
Jorim Jaggi | 6c7e561 | 2017-12-12 02:17:10 +0100 | [diff] [blame] | 27 | import static org.mockito.Mockito.times; |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 28 | import static org.mockito.Mockito.verify; |
Jorim Jaggi | 6c7e561 | 2017-12-12 02:17:10 +0100 | [diff] [blame] | 29 | import static org.mockito.Mockito.when; |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 30 | |
Jorim Jaggi | 6c7e561 | 2017-12-12 02:17:10 +0100 | [diff] [blame] | 31 | import android.animation.AnimationHandler; |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 32 | import android.animation.AnimationHandler.AnimationFrameCallbackProvider; |
Jorim Jaggi | 6c7e561 | 2017-12-12 02:17:10 +0100 | [diff] [blame] | 33 | import android.animation.ValueAnimator; |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 34 | import android.graphics.Matrix; |
| 35 | import android.graphics.Point; |
| 36 | import android.platform.test.annotations.Presubmit; |
| 37 | import android.support.test.filters.SmallTest; |
| 38 | import android.support.test.runner.AndroidJUnit4; |
| 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 | |
| 46 | import com.android.server.wm.LocalAnimationAdapter.AnimationSpec; |
Jorim Jaggi | 6c7e561 | 2017-12-12 02:17:10 +0100 | [diff] [blame] | 47 | import com.android.server.wm.SurfaceAnimationRunner.AnimatorFactory; |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 48 | |
| 49 | import org.junit.Before; |
| 50 | import org.junit.Rule; |
| 51 | import org.junit.Test; |
| 52 | import org.junit.runner.RunWith; |
| 53 | import org.mockito.Mock; |
| 54 | import org.mockito.junit.MockitoJUnit; |
| 55 | import org.mockito.junit.MockitoRule; |
| 56 | |
| 57 | import java.util.concurrent.CountDownLatch; |
| 58 | |
| 59 | /** |
| 60 | * Test class for {@link SurfaceAnimationRunner}. |
| 61 | * |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 62 | * atest FrameworksServicesTests:com.android.server.wm.SurfaceAnimationRunnerTest |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 63 | */ |
| 64 | @SmallTest |
| 65 | @Presubmit |
| 66 | @RunWith(AndroidJUnit4.class) |
| 67 | public class SurfaceAnimationRunnerTest extends WindowTestsBase { |
| 68 | |
| 69 | @Mock SurfaceControl mMockSurface; |
| 70 | @Mock Transaction mMockTransaction; |
Jorim Jaggi | 6c7e561 | 2017-12-12 02:17:10 +0100 | [diff] [blame] | 71 | @Mock AnimationSpec mMockAnimationSpec; |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 72 | @Rule public MockitoRule mMockitoRule = MockitoJUnit.rule(); |
| 73 | |
| 74 | private SurfaceAnimationRunner mSurfaceAnimationRunner; |
| 75 | private CountDownLatch mFinishCallbackLatch; |
| 76 | |
| 77 | @Before |
| 78 | public void setUp() throws Exception { |
| 79 | super.setUp(); |
| 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 | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 82 | mMockTransaction); |
| 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 |
| 112 | public void testCancel_notStarted() throws Exception { |
Jorim Jaggi | 6c7e561 | 2017-12-12 02:17:10 +0100 | [diff] [blame] | 113 | mSurfaceAnimationRunner = new SurfaceAnimationRunner(new NoOpFrameCallbackProvider(), null, |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 114 | mMockTransaction); |
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 | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 127 | mMockTransaction); |
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 | |
Jorim Jaggi | 6c7e561 | 2017-12-12 02:17:10 +0100 | [diff] [blame] | 138 | @Test |
| 139 | public void testCancel_sneakyCancelBeforeUpdate() throws Exception { |
| 140 | mSurfaceAnimationRunner = new SurfaceAnimationRunner(null, () -> new ValueAnimator() { |
| 141 | { |
| 142 | setFloatValues(0f, 1f); |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public void addUpdateListener(AnimatorUpdateListener listener) { |
| 147 | super.addUpdateListener(animation -> { |
| 148 | // Sneaky test cancels animation just before applying frame to simulate |
| 149 | // interleaving of multiple threads. Muahahaha |
| 150 | if (animation.getCurrentPlayTime() > 0) { |
| 151 | mSurfaceAnimationRunner.onAnimationCancelled(mMockSurface); |
| 152 | } |
| 153 | listener.onAnimationUpdate(animation); |
| 154 | }); |
| 155 | } |
| 156 | }, mMockTransaction); |
| 157 | when(mMockAnimationSpec.getDuration()).thenReturn(200L); |
| 158 | mSurfaceAnimationRunner.startAnimation(mMockAnimationSpec, mMockSurface, mMockTransaction, |
| 159 | this::finishedCallback); |
| 160 | waitUntilNextFrame(); |
| 161 | assertFalse(mSurfaceAnimationRunner.mRunningAnimations.isEmpty()); |
| 162 | verify(mMockAnimationSpec, atLeastOnce()).apply(any(), any(), eq(0L)); |
| 163 | } |
| 164 | |
Jorim Jaggi | d6d9716 | 2018-01-05 18:28:36 +0100 | [diff] [blame] | 165 | @Test |
| 166 | public void testDeferStartingAnimations() throws Exception { |
| 167 | mSurfaceAnimationRunner.deferStartingAnimations(); |
| 168 | mSurfaceAnimationRunner.startAnimation(createTranslateAnimation(), mMockSurface, |
| 169 | mMockTransaction, this::finishedCallback); |
| 170 | waitUntilNextFrame(); |
| 171 | assertTrue(mSurfaceAnimationRunner.mRunningAnimations.isEmpty()); |
| 172 | mSurfaceAnimationRunner.continueStartingAnimations(); |
| 173 | waitUntilNextFrame(); |
| 174 | assertFalse(mSurfaceAnimationRunner.mRunningAnimations.isEmpty()); |
| 175 | mFinishCallbackLatch.await(1, SECONDS); |
| 176 | assertFinishCallbackCalled(); |
| 177 | } |
| 178 | |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 179 | private void waitUntilNextFrame() throws Exception { |
| 180 | final CountDownLatch latch = new CountDownLatch(1); |
| 181 | mSurfaceAnimationRunner.mChoreographer.postCallback(Choreographer.CALLBACK_COMMIT, |
| 182 | latch::countDown, null /* token */); |
| 183 | latch.await(); |
| 184 | } |
| 185 | |
| 186 | private void assertFinishCallbackCalled() { |
| 187 | assertEquals(0, mFinishCallbackLatch.getCount()); |
| 188 | } |
| 189 | |
| 190 | private void assertFinishCallbackNotCalled() { |
| 191 | assertEquals(1, mFinishCallbackLatch.getCount()); |
| 192 | } |
| 193 | |
| 194 | private AnimationSpec createTranslateAnimation() { |
| 195 | final Animation a = new TranslateAnimation(-10, 10, 0, 0); |
| 196 | a.initialize(0, 0, 0, 0); |
| 197 | a.setDuration(50); |
Jorim Jaggi | 2e3c31d | 2017-11-20 19:49:00 +0100 | [diff] [blame] | 198 | return new WindowAnimationSpec(a, new Point(0, 0), false /* canSkipFirstFrame */); |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Callback provider that doesn't animate at all. |
| 203 | */ |
| 204 | private static final class NoOpFrameCallbackProvider implements AnimationFrameCallbackProvider { |
| 205 | |
| 206 | @Override |
| 207 | public void postFrameCallback(FrameCallback callback) { |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public void postCommitCallback(Runnable runnable) { |
| 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public long getFrameTime() { |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public long getFrameDelay() { |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | @Override |
| 225 | public void setFrameDelay(long delay) { |
| 226 | } |
| 227 | } |
| 228 | } |