blob: 40c79bbb183dababe57cac76378638fd68b9bbb9 [file] [log] [blame]
Bryce Leeaf691c02017-03-20 14:20:22 -07001/*
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
17package com.android.server.wm;
18
19import android.app.ActivityManager;
20import android.content.Context;
21import android.content.res.Configuration;
22import android.graphics.Rect;
23import android.os.Binder;
24import android.os.IBinder;
25import android.view.IApplicationToken;
26import android.view.IWindow;
27import android.view.WindowManager;
28
29import static android.app.AppOpsManager.OP_NONE;
30import static android.content.pm.ActivityInfo.RESIZE_MODE_UNRESIZEABLE;
31import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
32import static android.content.res.Configuration.EMPTY;
33import static com.android.server.wm.WindowContainer.POSITION_TOP;
34import static org.mockito.Mockito.mock;
35
36/**
37 * A collection of static functions that can be referenced by other test packages to provide access
38 * to WindowManager related test functionality.
39 */
40public class WindowTestUtils {
41 public static int sNextTaskId = 0;
42
43 /**
44 * Retrieves an instance of {@link WindowManagerService}, creating it if necessary.
45 */
46 public static WindowManagerService getWindowManagerService(Context context) {
47 return TestWindowManagerPolicy.getWindowManagerService(context);
48 }
49
50 /**
Bryce Lee04ab3462017-04-10 15:06:33 -070051 * Retrieves an instance of a mock {@link WindowManagerService}.
52 */
53 public static WindowManagerService getMockWindowManagerService() {
54 return mock(WindowManagerService.class);
55 }
56
57 /**
Bryce Leeaf691c02017-03-20 14:20:22 -070058 * Creates a mock instance of {@link StackWindowController}.
59 */
60 public static StackWindowController createMockStackWindowContainerController() {
61 StackWindowController controller = mock(StackWindowController.class);
62 controller.mContainer = mock(TestTaskStack.class);
63 return controller;
64 }
65
66 /** Creates a {@link Task} and adds it to the specified {@link TaskStack}. */
67 public static Task createTaskInStack(WindowManagerService service, TaskStack stack,
68 int userId) {
Wale Ogunwale2f569ed2017-05-08 09:15:49 -070069 final Task newTask = new Task(sNextTaskId++, stack, userId, service, null, EMPTY, 0, false,
Bryce Leeaf691c02017-03-20 14:20:22 -070070 false, new ActivityManager.TaskDescription(), null);
71 stack.addTask(newTask, POSITION_TOP);
72 return newTask;
73 }
74
75 /**
76 * An extension of {@link TestTaskStack}, which overrides package scoped methods that would not
77 * normally be mocked out.
78 */
79 public static class TestTaskStack extends TaskStack {
80 TestTaskStack(WindowManagerService service, int stackId) {
81 super(service, stackId);
82 }
83
84 @Override
85 void addTask(Task task, int position, boolean showForAllUsers, boolean moveParents) {
86 // Do nothing.
87 }
88 }
89
90 /** Used so we can gain access to some protected members of the {@link AppWindowToken} class. */
91 public static class TestAppWindowToken extends AppWindowToken {
Bryce Lee00d586d2017-07-28 20:48:43 -070092 boolean mOnTop = false;
Bryce Leeaf691c02017-03-20 14:20:22 -070093
94 TestAppWindowToken(DisplayContent dc) {
Steven Timotiusaf03df62017-07-18 16:56:43 -070095 super(dc.mService, new IApplicationToken.Stub() {
96 public String getName() {return null;}
97 }, false, dc, true /* fillsParent */,
Bryce Leeaf691c02017-03-20 14:20:22 -070098 null /* overrideConfig */, null /* bounds */);
99 }
100
101 TestAppWindowToken(WindowManagerService service, IApplicationToken token,
102 boolean voiceInteraction, DisplayContent dc, long inputDispatchingTimeoutNanos,
103 boolean fullscreen, boolean showForAllUsers, int targetSdk, int orientation,
104 int rotationAnimationHint, int configChanges, boolean launchTaskBehind,
105 boolean alwaysFocusable, AppWindowContainerController controller,
106 Configuration overrideConfig, Rect bounds) {
107 super(service, token, voiceInteraction, dc, inputDispatchingTimeoutNanos, fullscreen,
108 showForAllUsers, targetSdk, orientation, rotationAnimationHint, configChanges,
109 launchTaskBehind, alwaysFocusable, controller, overrideConfig, bounds);
110 }
111
112 int getWindowsCount() {
113 return mChildren.size();
114 }
115
116 boolean hasWindow(WindowState w) {
117 return mChildren.contains(w);
118 }
119
120 WindowState getFirstChild() {
Jorim Jaggi612bb882017-05-16 17:11:18 +0200121 return mChildren.peekFirst();
Bryce Leeaf691c02017-03-20 14:20:22 -0700122 }
123
124 WindowState getLastChild() {
Jorim Jaggi612bb882017-05-16 17:11:18 +0200125 return mChildren.peekLast();
Bryce Leeaf691c02017-03-20 14:20:22 -0700126 }
127
128 int positionInParent() {
129 return getParent().mChildren.indexOf(this);
130 }
Bryce Lee00d586d2017-07-28 20:48:43 -0700131
132 void setIsOnTop(boolean onTop) {
133 mOnTop = onTop;
134 }
135
136 @Override
137 boolean isOnTop() {
138 return mOnTop;
139 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700140 }
141
142 /* Used so we can gain access to some protected members of the {@link WindowToken} class */
143 public static class TestWindowToken extends WindowToken {
144 int adj = 0;
145
146 TestWindowToken(int type, DisplayContent dc) {
147 this(type, dc, false /* persistOnEmpty */);
148 }
149
150 TestWindowToken(int type, DisplayContent dc, boolean persistOnEmpty) {
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700151 super(dc.mService, mock(IBinder.class), type, persistOnEmpty, dc,
Bryce Leeaf691c02017-03-20 14:20:22 -0700152 false /* ownerCanManageAppTokens */);
153 }
154
155 int getWindowsCount() {
156 return mChildren.size();
157 }
158
159 boolean hasWindow(WindowState w) {
160 return mChildren.contains(w);
161 }
162
163 @Override
164 int getAnimLayerAdjustment() {
165 return adj;
166 }
167 }
168
169 /* Used so we can gain access to some protected members of the {@link Task} class */
170 public static class TestTask extends Task {
171 boolean mShouldDeferRemoval = false;
172 boolean mOnDisplayChangedCalled = false;
173 private boolean mUseLocalIsAnimating = false;
174 private boolean mIsAnimating = false;
175
176 TestTask(int taskId, TaskStack stack, int userId, WindowManagerService service, Rect bounds,
177 Configuration overrideConfig, int resizeMode, boolean supportsPictureInPicture,
178 boolean homeTask, TaskWindowContainerController controller) {
179 super(taskId, stack, userId, service, bounds, overrideConfig, resizeMode,
180 supportsPictureInPicture, homeTask, new ActivityManager.TaskDescription(),
181 controller);
182 }
183
184 boolean shouldDeferRemoval() {
185 return mShouldDeferRemoval;
186 }
187
188 int positionInParent() {
189 return getParent().mChildren.indexOf(this);
190 }
191
192 @Override
193 void onDisplayChanged(DisplayContent dc) {
194 super.onDisplayChanged(dc);
195 mOnDisplayChangedCalled = true;
196 }
197
198 @Override
199 boolean isAnimating() {
200 return mUseLocalIsAnimating ? mIsAnimating : super.isAnimating();
201 }
202
203 void setLocalIsAnimating(boolean isAnimating) {
204 mUseLocalIsAnimating = true;
205 mIsAnimating = isAnimating;
206 }
207 }
208
209 /**
210 * Used so we can gain access to some protected members of {@link TaskWindowContainerController}
211 * class.
212 */
213 public static class TestTaskWindowContainerController extends TaskWindowContainerController {
214
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700215 TestTaskWindowContainerController(WindowTestsBase testsBase) {
216 this(testsBase.createStackControllerOnDisplay(testsBase.mDisplayContent));
Bryce Leeaf691c02017-03-20 14:20:22 -0700217 }
218
219 TestTaskWindowContainerController(StackWindowController stackController) {
220 super(sNextTaskId++, new TaskWindowContainerListener() {
221 @Override
222 public void onSnapshotChanged(ActivityManager.TaskSnapshot snapshot) {
223
224 }
225
226 @Override
227 public void requestResize(Rect bounds, int resizeMode) {
228
229 }
230 }, stackController, 0 /* userId */, null /* bounds */,
231 EMPTY /* overrideConfig*/, RESIZE_MODE_UNRESIZEABLE,
232 false /* supportsPictureInPicture */, false /* homeTask*/, true /* toTop*/,
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700233 true /* showForAllUsers */, new ActivityManager.TaskDescription(),
234 stackController.mService);
Bryce Leeaf691c02017-03-20 14:20:22 -0700235 }
236
237 @Override
238 TestTask createTask(int taskId, TaskStack stack, int userId, Rect bounds,
239 Configuration overrideConfig, int resizeMode, boolean supportsPictureInPicture,
240 boolean homeTask, ActivityManager.TaskDescription taskDescription) {
241 return new TestTask(taskId, stack, userId, mService, bounds, overrideConfig, resizeMode,
242 supportsPictureInPicture, homeTask, this);
243 }
244 }
245
246 public static class TestAppWindowContainerController extends AppWindowContainerController {
247
248 final IApplicationToken mToken;
249
250 TestAppWindowContainerController(TestTaskWindowContainerController taskController) {
251 this(taskController, new TestIApplicationToken());
252 }
253
254 TestAppWindowContainerController(TestTaskWindowContainerController taskController,
255 IApplicationToken token) {
256 super(taskController, token, null /* listener */, 0 /* index */,
257 SCREEN_ORIENTATION_UNSPECIFIED, true /* fullscreen */,
258 true /* showForAllUsers */, 0 /* configChanges */, false /* voiceInteraction */,
259 false /* launchTaskBehind */, false /* alwaysFocusable */,
260 0 /* targetSdkVersion */, 0 /* rotationAnimationHint */,
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700261 0 /* inputDispatchingTimeoutNanos */, taskController.mService,
262 null /* overrideConfig */, null /* bounds */);
Bryce Leeaf691c02017-03-20 14:20:22 -0700263 mToken = token;
264 }
265
266 @Override
267 AppWindowToken createAppWindow(WindowManagerService service, IApplicationToken token,
268 boolean voiceInteraction, DisplayContent dc, long inputDispatchingTimeoutNanos,
269 boolean fullscreen, boolean showForAllUsers, int targetSdk, int orientation,
270 int rotationAnimationHint, int configChanges, boolean launchTaskBehind,
271 boolean alwaysFocusable, AppWindowContainerController controller,
272 Configuration overrideConfig, Rect bounds) {
273 return new TestAppWindowToken(service, token, voiceInteraction, dc,
274 inputDispatchingTimeoutNanos, fullscreen, showForAllUsers, targetSdk,
275 orientation,
276 rotationAnimationHint, configChanges, launchTaskBehind, alwaysFocusable,
277 controller, overrideConfig, bounds);
278 }
279
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700280 AppWindowToken getAppWindowToken(DisplayContent dc) {
281 return (AppWindowToken) dc.getWindowToken(mToken.asBinder());
Bryce Leeaf691c02017-03-20 14:20:22 -0700282 }
283 }
284
285 public static class TestIApplicationToken implements IApplicationToken {
286
287 private final Binder mBinder = new Binder();
288 @Override
289 public IBinder asBinder() {
290 return mBinder;
291 }
Steven Timotiusaf03df62017-07-18 16:56:43 -0700292 @Override
293 public String getName() {
294 return null;
295 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700296 }
297
298 /** Used to track resize reports. */
299 public static class TestWindowState extends WindowState {
300 boolean resizeReported;
301
302 TestWindowState(WindowManagerService service, Session session, IWindow window,
303 WindowManager.LayoutParams attrs, WindowToken token) {
304 super(service, session, window, token, null, OP_NONE, 0, attrs, 0, 0,
305 false /* ownerCanAddInternalSystemWindow */);
306 }
307
308 @Override
309 void reportResized() {
310 super.reportResized();
311 resizeReported = true;
312 }
313
314 @Override
315 public boolean isGoneForLayoutLw() {
316 return false;
317 }
318
319 @Override
320 void updateResizingWindowIfNeeded() {
321 // Used in AppWindowTokenTests#testLandscapeSeascapeRotationRelayout to deceive
322 // the system that it can actually update the window.
323 boolean hadSurface = mHasSurface;
324 mHasSurface = true;
325
326 super.updateResizingWindowIfNeeded();
327
328 mHasSurface = hadSurface;
329 }
330 }
331}