Jorim Jaggi | 33a701a | 2017-12-01 14:58:18 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION; |
Adrian Roos | 842e788 | 2018-03-26 17:34:06 +0200 | [diff] [blame^] | 20 | |
Jorim Jaggi | 33a701a | 2017-12-01 14:58:18 +0100 | [diff] [blame] | 21 | import static org.junit.Assert.assertEquals; |
| 22 | import static org.mockito.ArgumentMatchers.eq; |
Adrian Roos | 842e788 | 2018-03-26 17:34:06 +0200 | [diff] [blame^] | 23 | import static org.mockito.Mockito.atLeast; |
Jorim Jaggi | a19d781 | 2018-02-01 15:03:59 +0100 | [diff] [blame] | 24 | import static org.mockito.Mockito.never; |
Jorim Jaggi | 33a701a | 2017-12-01 14:58:18 +0100 | [diff] [blame] | 25 | import static org.mockito.Mockito.verify; |
Adrian Roos | 842e788 | 2018-03-26 17:34:06 +0200 | [diff] [blame^] | 26 | import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 27 | import static org.mockito.Mockito.when; |
Jorim Jaggi | 33a701a | 2017-12-01 14:58:18 +0100 | [diff] [blame] | 28 | |
| 29 | import android.graphics.Point; |
| 30 | import android.graphics.Rect; |
Adrian Roos | 842e788 | 2018-03-26 17:34:06 +0200 | [diff] [blame^] | 31 | import android.os.Binder; |
| 32 | import android.os.IInterface; |
| 33 | import android.platform.test.annotations.Presubmit; |
Jorim Jaggi | 33a701a | 2017-12-01 14:58:18 +0100 | [diff] [blame] | 34 | import android.support.test.filters.FlakyTest; |
| 35 | import android.support.test.filters.SmallTest; |
| 36 | import android.support.test.runner.AndroidJUnit4; |
| 37 | import android.view.IRemoteAnimationFinishedCallback; |
| 38 | import android.view.IRemoteAnimationRunner; |
| 39 | import android.view.RemoteAnimationAdapter; |
| 40 | import android.view.RemoteAnimationTarget; |
| 41 | import android.view.SurfaceControl; |
| 42 | import android.view.SurfaceControl.Transaction; |
| 43 | |
| 44 | import com.android.server.testutils.OffsettableClock; |
| 45 | import com.android.server.testutils.TestHandler; |
| 46 | import com.android.server.wm.SurfaceAnimator.OnAnimationFinishedCallback; |
| 47 | |
| 48 | import org.junit.Before; |
| 49 | import org.junit.Test; |
| 50 | import org.junit.runner.RunWith; |
| 51 | import org.mockito.ArgumentCaptor; |
| 52 | import org.mockito.Mock; |
| 53 | import org.mockito.MockitoAnnotations; |
| 54 | |
| 55 | /** |
| 56 | * atest FrameworksServicesTests:com.android.server.wm.RemoteAnimationControllerTest |
| 57 | */ |
| 58 | @SmallTest |
Adrian Roos | 842e788 | 2018-03-26 17:34:06 +0200 | [diff] [blame^] | 59 | @Presubmit |
Jorim Jaggi | 33a701a | 2017-12-01 14:58:18 +0100 | [diff] [blame] | 60 | @RunWith(AndroidJUnit4.class) |
| 61 | public class RemoteAnimationControllerTest extends WindowTestsBase { |
| 62 | |
| 63 | @Mock SurfaceControl mMockLeash; |
| 64 | @Mock Transaction mMockTransaction; |
| 65 | @Mock OnAnimationFinishedCallback mFinishedCallback; |
| 66 | @Mock IRemoteAnimationRunner mMockRunner; |
| 67 | private RemoteAnimationAdapter mAdapter; |
| 68 | private RemoteAnimationController mController; |
| 69 | private final OffsettableClock mClock = new OffsettableClock.Stopped(); |
| 70 | private TestHandler mHandler; |
| 71 | |
| 72 | @Before |
| 73 | public void setUp() throws Exception { |
| 74 | super.setUp(); |
| 75 | MockitoAnnotations.initMocks(this); |
Adrian Roos | 842e788 | 2018-03-26 17:34:06 +0200 | [diff] [blame^] | 76 | when(mMockRunner.asBinder()).thenReturn(new Binder()); |
Jorim Jaggi | 33a701a | 2017-12-01 14:58:18 +0100 | [diff] [blame] | 77 | mAdapter = new RemoteAnimationAdapter(mMockRunner, 100, 50); |
Jorim Jaggi | c4d29f2 | 2018-03-22 16:30:56 +0100 | [diff] [blame] | 78 | mAdapter.setCallingPid(123); |
Jorim Jaggi | 33a701a | 2017-12-01 14:58:18 +0100 | [diff] [blame] | 79 | sWm.mH.runWithScissors(() -> { |
| 80 | mHandler = new TestHandler(null, mClock); |
| 81 | }, 0); |
| 82 | mController = new RemoteAnimationController(sWm, mAdapter, mHandler); |
| 83 | } |
| 84 | |
| 85 | @Test |
| 86 | public void testRun() throws Exception { |
| 87 | final WindowState win = createWindow(null /* parent */, TYPE_BASE_APPLICATION, "testWin"); |
| 88 | sWm.mOpeningApps.add(win.mAppToken); |
| 89 | try { |
| 90 | final AnimationAdapter adapter = mController.createAnimationAdapter(win.mAppToken, |
| 91 | new Point(50, 100), new Rect(50, 100, 150, 150)); |
| 92 | adapter.startAnimation(mMockLeash, mMockTransaction, mFinishedCallback); |
| 93 | mController.goodToGo(); |
Jorim Jaggi | c4d29f2 | 2018-03-22 16:30:56 +0100 | [diff] [blame] | 94 | sWm.mAnimator.executeAfterPrepareSurfacesRunnables(); |
Jorim Jaggi | 33a701a | 2017-12-01 14:58:18 +0100 | [diff] [blame] | 95 | final ArgumentCaptor<RemoteAnimationTarget[]> appsCaptor = |
| 96 | ArgumentCaptor.forClass(RemoteAnimationTarget[].class); |
| 97 | final ArgumentCaptor<IRemoteAnimationFinishedCallback> finishedCaptor = |
| 98 | ArgumentCaptor.forClass(IRemoteAnimationFinishedCallback.class); |
| 99 | verify(mMockRunner).onAnimationStart(appsCaptor.capture(), finishedCaptor.capture()); |
| 100 | assertEquals(1, appsCaptor.getValue().length); |
| 101 | final RemoteAnimationTarget app = appsCaptor.getValue()[0]; |
| 102 | assertEquals(new Point(50, 100), app.position); |
| 103 | assertEquals(new Rect(50, 100, 150, 150), app.sourceContainerBounds); |
| 104 | assertEquals(win.mAppToken.getPrefixOrderIndex(), app.prefixOrderIndex); |
| 105 | assertEquals(win.mAppToken.getTask().mTaskId, app.taskId); |
| 106 | assertEquals(mMockLeash, app.leash); |
| 107 | assertEquals(win.mWinAnimator.mLastClipRect, app.clipRect); |
| 108 | assertEquals(false, app.isTranslucent); |
| 109 | verify(mMockTransaction).setLayer(mMockLeash, app.prefixOrderIndex); |
| 110 | verify(mMockTransaction).setPosition(mMockLeash, app.position.x, app.position.y); |
| 111 | verify(mMockTransaction).setWindowCrop(mMockLeash, new Rect(0, 0, 100, 50)); |
| 112 | |
| 113 | finishedCaptor.getValue().onAnimationFinished(); |
| 114 | verify(mFinishedCallback).onAnimationFinished(eq(adapter)); |
| 115 | } finally { |
| 116 | sWm.mOpeningApps.clear(); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | @Test |
| 121 | public void testCancel() throws Exception { |
| 122 | final WindowState win = createWindow(null /* parent */, TYPE_BASE_APPLICATION, "testWin"); |
| 123 | final AnimationAdapter adapter = mController.createAnimationAdapter(win.mAppToken, |
| 124 | new Point(50, 100), new Rect(50, 100, 150, 150)); |
| 125 | adapter.startAnimation(mMockLeash, mMockTransaction, mFinishedCallback); |
| 126 | mController.goodToGo(); |
| 127 | |
| 128 | adapter.onAnimationCancelled(mMockLeash); |
| 129 | verify(mMockRunner).onAnimationCancelled(); |
| 130 | } |
| 131 | |
| 132 | @Test |
| 133 | public void testTimeout() throws Exception { |
| 134 | final WindowState win = createWindow(null /* parent */, TYPE_BASE_APPLICATION, "testWin"); |
| 135 | final AnimationAdapter adapter = mController.createAnimationAdapter(win.mAppToken, |
| 136 | new Point(50, 100), new Rect(50, 100, 150, 150)); |
| 137 | adapter.startAnimation(mMockLeash, mMockTransaction, mFinishedCallback); |
| 138 | mController.goodToGo(); |
| 139 | |
| 140 | mClock.fastForward(2500); |
| 141 | mHandler.timeAdvance(); |
| 142 | |
| 143 | verify(mMockRunner).onAnimationCancelled(); |
| 144 | verify(mFinishedCallback).onAnimationFinished(eq(adapter)); |
| 145 | } |
Jorim Jaggi | 93f9fe3 | 2018-01-25 15:06:13 +0100 | [diff] [blame] | 146 | |
| 147 | @Test |
Jorim Jaggi | a19d781 | 2018-02-01 15:03:59 +0100 | [diff] [blame] | 148 | public void testTimeout_scaled() throws Exception { |
| 149 | sWm.setAnimationScale(2, 5.0f); |
| 150 | try{ |
| 151 | final WindowState win = createWindow(null /* parent */, TYPE_BASE_APPLICATION, "testWin"); |
| 152 | final AnimationAdapter adapter = mController.createAnimationAdapter(win.mAppToken, |
| 153 | new Point(50, 100), new Rect(50, 100, 150, 150)); |
| 154 | adapter.startAnimation(mMockLeash, mMockTransaction, mFinishedCallback); |
| 155 | mController.goodToGo(); |
| 156 | |
| 157 | mClock.fastForward(2500); |
| 158 | mHandler.timeAdvance(); |
| 159 | |
| 160 | verify(mMockRunner, never()).onAnimationCancelled(); |
| 161 | |
| 162 | mClock.fastForward(10000); |
| 163 | mHandler.timeAdvance(); |
| 164 | |
| 165 | verify(mMockRunner).onAnimationCancelled(); |
| 166 | verify(mFinishedCallback).onAnimationFinished(eq(adapter)); |
| 167 | } finally { |
| 168 | sWm.setAnimationScale(2, 1.0f); |
| 169 | } |
| 170 | |
| 171 | } |
| 172 | |
| 173 | @Test |
Jorim Jaggi | 93f9fe3 | 2018-01-25 15:06:13 +0100 | [diff] [blame] | 174 | public void testZeroAnimations() throws Exception { |
| 175 | mController.goodToGo(); |
Adrian Roos | 842e788 | 2018-03-26 17:34:06 +0200 | [diff] [blame^] | 176 | verifyNoMoreInteractionsExceptAsBinder(mMockRunner); |
Jorim Jaggi | 93f9fe3 | 2018-01-25 15:06:13 +0100 | [diff] [blame] | 177 | } |
Jorim Jaggi | c4d29f2 | 2018-03-22 16:30:56 +0100 | [diff] [blame] | 178 | |
| 179 | @Test |
| 180 | public void testNotReallyStarted() throws Exception { |
| 181 | final WindowState win = createWindow(null /* parent */, TYPE_BASE_APPLICATION, "testWin"); |
| 182 | mController.createAnimationAdapter(win.mAppToken, |
| 183 | new Point(50, 100), new Rect(50, 100, 150, 150)); |
| 184 | mController.goodToGo(); |
Adrian Roos | 842e788 | 2018-03-26 17:34:06 +0200 | [diff] [blame^] | 185 | verifyNoMoreInteractionsExceptAsBinder(mMockRunner); |
Jorim Jaggi | c4d29f2 | 2018-03-22 16:30:56 +0100 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | @Test |
| 189 | public void testOneNotStarted() throws Exception { |
| 190 | final WindowState win1 = createWindow(null /* parent */, TYPE_BASE_APPLICATION, "testWin1"); |
| 191 | final WindowState win2 = createWindow(null /* parent */, TYPE_BASE_APPLICATION, "testWin2"); |
| 192 | mController.createAnimationAdapter(win1.mAppToken, |
| 193 | new Point(50, 100), new Rect(50, 100, 150, 150)); |
| 194 | final AnimationAdapter adapter = mController.createAnimationAdapter(win2.mAppToken, |
| 195 | new Point(50, 100), new Rect(50, 100, 150, 150)); |
| 196 | adapter.startAnimation(mMockLeash, mMockTransaction, mFinishedCallback); |
| 197 | mController.goodToGo(); |
| 198 | sWm.mAnimator.executeAfterPrepareSurfacesRunnables(); |
| 199 | final ArgumentCaptor<RemoteAnimationTarget[]> appsCaptor = |
| 200 | ArgumentCaptor.forClass(RemoteAnimationTarget[].class); |
| 201 | final ArgumentCaptor<IRemoteAnimationFinishedCallback> finishedCaptor = |
| 202 | ArgumentCaptor.forClass(IRemoteAnimationFinishedCallback.class); |
| 203 | verify(mMockRunner).onAnimationStart(appsCaptor.capture(), finishedCaptor.capture()); |
| 204 | assertEquals(1, appsCaptor.getValue().length); |
| 205 | assertEquals(mMockLeash, appsCaptor.getValue()[0].leash); |
| 206 | } |
Jorim Jaggi | b8a9cbe | 2018-03-27 18:02:18 +0200 | [diff] [blame] | 207 | |
| 208 | @Test |
| 209 | public void testRemovedBeforeStarted() throws Exception { |
| 210 | final WindowState win = createWindow(null /* parent */, TYPE_BASE_APPLICATION, "testWin"); |
| 211 | final AnimationAdapter adapter = mController.createAnimationAdapter(win.mAppToken, |
| 212 | new Point(50, 100), new Rect(50, 100, 150, 150)); |
| 213 | adapter.startAnimation(mMockLeash, mMockTransaction, mFinishedCallback); |
| 214 | win.mAppToken.removeImmediately(); |
| 215 | mController.goodToGo(); |
Adrian Roos | 842e788 | 2018-03-26 17:34:06 +0200 | [diff] [blame^] | 216 | verifyNoMoreInteractionsExceptAsBinder(mMockRunner); |
Jorim Jaggi | b8a9cbe | 2018-03-27 18:02:18 +0200 | [diff] [blame] | 217 | verify(mFinishedCallback).onAnimationFinished(eq(adapter)); |
| 218 | } |
Adrian Roos | 842e788 | 2018-03-26 17:34:06 +0200 | [diff] [blame^] | 219 | |
| 220 | private static void verifyNoMoreInteractionsExceptAsBinder(IInterface binder) { |
| 221 | verify(binder, atLeast(0)).asBinder(); |
| 222 | verifyNoMoreInteractions(binder); |
| 223 | } |
Jorim Jaggi | 33a701a | 2017-12-01 14:58:18 +0100 | [diff] [blame] | 224 | } |