blob: 83e7ee711831ae32ca10a930fda686b3a51a33dd [file] [log] [blame]
Jorim Jaggi21c39a72017-10-20 15:47:51 +02001/*
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. Takaokab6e148c2018-11-03 02:59:06 -070014 * limitations under the License.
Jorim Jaggi21c39a72017-10-20 15:47:51 +020015 */
16
17package com.android.server.wm;
18
Tadashi G. Takaokabf0d57b2018-11-19 16:09:58 +090019import static com.android.dx.mockito.inline.extended.ExtendedMockito.any;
20import static com.android.dx.mockito.inline.extended.ExtendedMockito.atLeast;
21import static com.android.dx.mockito.inline.extended.ExtendedMockito.atLeastOnce;
22import static com.android.dx.mockito.inline.extended.ExtendedMockito.eq;
23import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify;
24import static com.android.dx.mockito.inline.extended.ExtendedMockito.when;
25
Jorim Jaggi21c39a72017-10-20 15:47:51 +020026import static org.junit.Assert.assertEquals;
27import static org.junit.Assert.assertFalse;
28import static org.junit.Assert.assertTrue;
Jorim Jaggib6ba4332018-07-10 00:36:57 +020029import static org.mockito.ArgumentMatchers.anyInt;
Jorim Jaggi21c39a72017-10-20 15:47:51 +020030
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070031import static java.util.concurrent.TimeUnit.SECONDS;
32
Jorim Jaggi21c39a72017-10-20 15:47:51 +020033import android.animation.AnimationHandler.AnimationFrameCallbackProvider;
Jorim Jaggi6c7e5612017-12-12 02:17:10 +010034import android.animation.ValueAnimator;
Jorim Jaggi21c39a72017-10-20 15:47:51 +020035import android.graphics.Matrix;
36import android.graphics.Point;
Jorim Jaggib6ba4332018-07-10 00:36:57 +020037import android.os.PowerManagerInternal;
Jorim Jaggi21c39a72017-10-20 15:47:51 +020038import android.platform.test.annotations.Presubmit;
Jorim Jaggi21c39a72017-10-20 15:47:51 +020039import android.view.Choreographer;
40import android.view.Choreographer.FrameCallback;
41import android.view.SurfaceControl;
42import android.view.SurfaceControl.Transaction;
43import android.view.animation.Animation;
44import android.view.animation.TranslateAnimation;
45
Brett Chabota26eda92018-07-23 13:08:30 -070046import androidx.test.filters.FlakyTest;
47import androidx.test.filters.SmallTest;
Brett Chabota26eda92018-07-23 13:08:30 -070048
Jorim Jaggi21c39a72017-10-20 15:47:51 +020049import com.android.server.wm.LocalAnimationAdapter.AnimationSpec;
50
51import org.junit.Before;
52import org.junit.Rule;
53import org.junit.Test;
Jorim Jaggi21c39a72017-10-20 15:47:51 +020054import org.mockito.Mock;
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070055import org.mockito.MockitoAnnotations;
Jorim Jaggi21c39a72017-10-20 15:47:51 +020056import org.mockito.junit.MockitoJUnit;
57import org.mockito.junit.MockitoRule;
58
59import java.util.concurrent.CountDownLatch;
60
61/**
62 * Test class for {@link SurfaceAnimationRunner}.
63 *
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070064 * Build/Install/Run:
65 * atest FrameworksServicesTests:SurfaceAnimationRunnerTest
Jorim Jaggi21c39a72017-10-20 15:47:51 +020066 */
67@SmallTest
68@Presubmit
Jorim Jaggi21c39a72017-10-20 15:47:51 +020069public class SurfaceAnimationRunnerTest extends WindowTestsBase {
70
71 @Mock SurfaceControl mMockSurface;
72 @Mock Transaction mMockTransaction;
Jorim Jaggi6c7e5612017-12-12 02:17:10 +010073 @Mock AnimationSpec mMockAnimationSpec;
Jorim Jaggib6ba4332018-07-10 00:36:57 +020074 @Mock PowerManagerInternal mMockPowerManager;
Jorim Jaggi21c39a72017-10-20 15:47:51 +020075 @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. Takaokab6e148c2018-11-03 02:59:06 -070082 MockitoAnnotations.initMocks(this);
83
Jorim Jaggi21c39a72017-10-20 15:47:51 +020084 mFinishCallbackLatch = new CountDownLatch(1);
Jorim Jaggi6c7e5612017-12-12 02:17:10 +010085 mSurfaceAnimationRunner = new SurfaceAnimationRunner(null /* callbackProvider */, null,
Jorim Jaggib6ba4332018-07-10 00:36:57 +020086 mMockTransaction, mMockPowerManager);
Jorim Jaggi21c39a72017-10-20 15:47:51 +020087 }
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. Takaokab6e148c2018-11-03 02:59:06 -0700116 public void testCancel_notStarted() {
Jorim Jaggi6c7e5612017-12-12 02:17:10 +0100117 mSurfaceAnimationRunner = new SurfaceAnimationRunner(new NoOpFrameCallbackProvider(), null,
Jorim Jaggib6ba4332018-07-10 00:36:57 +0200118 mMockTransaction, mMockPowerManager);
Jorim Jaggi21c39a72017-10-20 15:47:51 +0200119 mSurfaceAnimationRunner
120 .startAnimation(createTranslateAnimation(), mMockSurface, mMockTransaction,
121 this::finishedCallback);
122 mSurfaceAnimationRunner.onAnimationCancelled(mMockSurface);
123 waitUntilHandlersIdle();
124 assertTrue(mSurfaceAnimationRunner.mPendingAnimations.isEmpty());
125 assertFinishCallbackNotCalled();
Jorim Jaggi21c39a72017-10-20 15:47:51 +0200126 }
127
128 @Test
129 public void testCancel_running() throws Exception {
Jorim Jaggi6c7e5612017-12-12 02:17:10 +0100130 mSurfaceAnimationRunner = new SurfaceAnimationRunner(new NoOpFrameCallbackProvider(), null,
Jorim Jaggib6ba4332018-07-10 00:36:57 +0200131 mMockTransaction, mMockPowerManager);
Jorim Jaggi6c7e5612017-12-12 02:17:10 +0100132 mSurfaceAnimationRunner.startAnimation(createTranslateAnimation(), mMockSurface,
133 mMockTransaction, this::finishedCallback);
Jorim Jaggi21c39a72017-10-20 15:47:51 +0200134 waitUntilNextFrame();
135 assertFalse(mSurfaceAnimationRunner.mRunningAnimations.isEmpty());
136 mSurfaceAnimationRunner.onAnimationCancelled(mMockSurface);
137 assertTrue(mSurfaceAnimationRunner.mRunningAnimations.isEmpty());
138 waitUntilHandlersIdle();
139 assertFinishCallbackNotCalled();
Jorim Jaggi21c39a72017-10-20 15:47:51 +0200140 }
141
chaviw0fb741f2018-01-08 16:18:03 -0800142 @FlakyTest(bugId = 71719744)
Jorim Jaggi6c7e5612017-12-12 02:17:10 +0100143 @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 Jaggib6ba4332018-07-10 00:36:57 +0200161 }, mMockTransaction, mMockPowerManager);
Jorim Jaggi6c7e5612017-12-12 02:17:10 +0100162 when(mMockAnimationSpec.getDuration()).thenReturn(200L);
163 mSurfaceAnimationRunner.startAnimation(mMockAnimationSpec, mMockSurface, mMockTransaction,
164 this::finishedCallback);
Jorim Jaggi4130a682018-01-09 14:28:44 +0100165
166 // We need to wait for two frames: The first frame starts the animation, the second frame
167 // actually cancels the animation.
Jorim Jaggi6c7e5612017-12-12 02:17:10 +0100168 waitUntilNextFrame();
Jorim Jaggi4130a682018-01-09 14:28:44 +0100169 waitUntilNextFrame();
170 assertTrue(mSurfaceAnimationRunner.mRunningAnimations.isEmpty());
Jorim Jaggi6c7e5612017-12-12 02:17:10 +0100171 verify(mMockAnimationSpec, atLeastOnce()).apply(any(), any(), eq(0L));
172 }
173
Jorim Jaggid0a94372018-03-15 12:58:49 +0100174 @FlakyTest(bugId = 74780584)
Jorim Jaggid6d97162018-01-05 18:28:36 +0100175 @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 Jaggib6ba4332018-07-10 00:36:57 +0200189 @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 Jaggi21c39a72017-10-20 15:47:51 +0200202 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 Jaggi2e3c31d2017-11-20 19:49:00 +0100221 return new WindowAnimationSpec(a, new Point(0, 0), false /* canSkipFirstFrame */);
Jorim Jaggi21c39a72017-10-20 15:47:51 +0200222 }
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}