blob: 6cce9f088ee44beeedbc67c29e1f87a0ea74e98d [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;
Jorim Jaggi21c39a72017-10-20 15:47:51 +020052import org.junit.Test;
Jorim Jaggi21c39a72017-10-20 15:47:51 +020053import org.mockito.Mock;
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070054import org.mockito.MockitoAnnotations;
Jorim Jaggi21c39a72017-10-20 15:47:51 +020055
56import java.util.concurrent.CountDownLatch;
57
58/**
59 * Test class for {@link SurfaceAnimationRunner}.
60 *
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070061 * Build/Install/Run:
62 * atest FrameworksServicesTests:SurfaceAnimationRunnerTest
Jorim Jaggi21c39a72017-10-20 15:47:51 +020063 */
64@SmallTest
65@Presubmit
Jorim Jaggi21c39a72017-10-20 15:47:51 +020066public class SurfaceAnimationRunnerTest extends WindowTestsBase {
67
68 @Mock SurfaceControl mMockSurface;
69 @Mock Transaction mMockTransaction;
Jorim Jaggi6c7e5612017-12-12 02:17:10 +010070 @Mock AnimationSpec mMockAnimationSpec;
Jorim Jaggib6ba4332018-07-10 00:36:57 +020071 @Mock PowerManagerInternal mMockPowerManager;
Jorim Jaggi21c39a72017-10-20 15:47:51 +020072
73 private SurfaceAnimationRunner mSurfaceAnimationRunner;
74 private CountDownLatch mFinishCallbackLatch;
75
76 @Before
77 public void setUp() throws Exception {
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070078 MockitoAnnotations.initMocks(this);
79
Jorim Jaggi21c39a72017-10-20 15:47:51 +020080 mFinishCallbackLatch = new CountDownLatch(1);
Jorim Jaggi6c7e5612017-12-12 02:17:10 +010081 mSurfaceAnimationRunner = new SurfaceAnimationRunner(null /* callbackProvider */, null,
Jorim Jaggib6ba4332018-07-10 00:36:57 +020082 mMockTransaction, mMockPowerManager);
Jorim Jaggi21c39a72017-10-20 15:47:51 +020083 }
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. Takaokab6e148c2018-11-03 02:59:06 -0700112 public void testCancel_notStarted() {
Jorim Jaggi6c7e5612017-12-12 02:17:10 +0100113 mSurfaceAnimationRunner = new SurfaceAnimationRunner(new NoOpFrameCallbackProvider(), null,
Jorim Jaggib6ba4332018-07-10 00:36:57 +0200114 mMockTransaction, mMockPowerManager);
Jorim Jaggi21c39a72017-10-20 15:47:51 +0200115 mSurfaceAnimationRunner
116 .startAnimation(createTranslateAnimation(), mMockSurface, mMockTransaction,
117 this::finishedCallback);
118 mSurfaceAnimationRunner.onAnimationCancelled(mMockSurface);
119 waitUntilHandlersIdle();
120 assertTrue(mSurfaceAnimationRunner.mPendingAnimations.isEmpty());
121 assertFinishCallbackNotCalled();
Jorim Jaggi21c39a72017-10-20 15:47:51 +0200122 }
123
124 @Test
125 public void testCancel_running() throws Exception {
Jorim Jaggi6c7e5612017-12-12 02:17:10 +0100126 mSurfaceAnimationRunner = new SurfaceAnimationRunner(new NoOpFrameCallbackProvider(), null,
Jorim Jaggib6ba4332018-07-10 00:36:57 +0200127 mMockTransaction, mMockPowerManager);
Jorim Jaggi6c7e5612017-12-12 02:17:10 +0100128 mSurfaceAnimationRunner.startAnimation(createTranslateAnimation(), mMockSurface,
129 mMockTransaction, this::finishedCallback);
Jorim Jaggi21c39a72017-10-20 15:47:51 +0200130 waitUntilNextFrame();
131 assertFalse(mSurfaceAnimationRunner.mRunningAnimations.isEmpty());
132 mSurfaceAnimationRunner.onAnimationCancelled(mMockSurface);
133 assertTrue(mSurfaceAnimationRunner.mRunningAnimations.isEmpty());
134 waitUntilHandlersIdle();
135 assertFinishCallbackNotCalled();
Jorim Jaggi21c39a72017-10-20 15:47:51 +0200136 }
137
chaviw0fb741f2018-01-08 16:18:03 -0800138 @FlakyTest(bugId = 71719744)
Jorim Jaggi6c7e5612017-12-12 02:17:10 +0100139 @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 Jaggib6ba4332018-07-10 00:36:57 +0200157 }, mMockTransaction, mMockPowerManager);
Jorim Jaggi6c7e5612017-12-12 02:17:10 +0100158 when(mMockAnimationSpec.getDuration()).thenReturn(200L);
159 mSurfaceAnimationRunner.startAnimation(mMockAnimationSpec, mMockSurface, mMockTransaction,
160 this::finishedCallback);
Jorim Jaggi4130a682018-01-09 14:28:44 +0100161
162 // We need to wait for two frames: The first frame starts the animation, the second frame
163 // actually cancels the animation.
Jorim Jaggi6c7e5612017-12-12 02:17:10 +0100164 waitUntilNextFrame();
Jorim Jaggi4130a682018-01-09 14:28:44 +0100165 waitUntilNextFrame();
166 assertTrue(mSurfaceAnimationRunner.mRunningAnimations.isEmpty());
Jorim Jaggi6c7e5612017-12-12 02:17:10 +0100167 verify(mMockAnimationSpec, atLeastOnce()).apply(any(), any(), eq(0L));
168 }
169
Jorim Jaggid6d97162018-01-05 18:28:36 +0100170 @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 Jaggib6ba4332018-07-10 00:36:57 +0200184 @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 Jaggi21c39a72017-10-20 15:47:51 +0200197 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);
Jorim Jaggi2e3c31d2017-11-20 19:49:00 +0100216 return new WindowAnimationSpec(a, new Point(0, 0), false /* canSkipFirstFrame */);
Jorim Jaggi21c39a72017-10-20 15:47:51 +0200217 }
218
219 /**
220 * Callback provider that doesn't animate at all.
221 */
222 private static final class NoOpFrameCallbackProvider implements AnimationFrameCallbackProvider {
223
224 @Override
225 public void postFrameCallback(FrameCallback callback) {
226 }
227
228 @Override
229 public void postCommitCallback(Runnable runnable) {
230 }
231
232 @Override
233 public long getFrameTime() {
234 return 0;
235 }
236
237 @Override
238 public long getFrameDelay() {
239 return 0;
240 }
241
242 @Override
243 public void setFrameDelay(long delay) {
244 }
245 }
246}