blob: 7ff1110e00f79f706bb0685acb4793995cc15ff7 [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) {
Wale Ogunwale2f569ed2017-05-08 09:15:49 -070095 super(dc.mService, new IApplicationToken.Stub() {}, false, dc, true /* fillsParent */,
Bryce Leeaf691c02017-03-20 14:20:22 -070096 null /* overrideConfig */, null /* bounds */);
97 }
98
99 TestAppWindowToken(WindowManagerService service, IApplicationToken token,
100 boolean voiceInteraction, DisplayContent dc, long inputDispatchingTimeoutNanos,
101 boolean fullscreen, boolean showForAllUsers, int targetSdk, int orientation,
102 int rotationAnimationHint, int configChanges, boolean launchTaskBehind,
103 boolean alwaysFocusable, AppWindowContainerController controller,
104 Configuration overrideConfig, Rect bounds) {
105 super(service, token, voiceInteraction, dc, inputDispatchingTimeoutNanos, fullscreen,
106 showForAllUsers, targetSdk, orientation, rotationAnimationHint, configChanges,
107 launchTaskBehind, alwaysFocusable, controller, overrideConfig, bounds);
108 }
109
110 int getWindowsCount() {
111 return mChildren.size();
112 }
113
114 boolean hasWindow(WindowState w) {
115 return mChildren.contains(w);
116 }
117
118 WindowState getFirstChild() {
Jorim Jaggi612bb882017-05-16 17:11:18 +0200119 return mChildren.peekFirst();
Bryce Leeaf691c02017-03-20 14:20:22 -0700120 }
121
122 WindowState getLastChild() {
Jorim Jaggi612bb882017-05-16 17:11:18 +0200123 return mChildren.peekLast();
Bryce Leeaf691c02017-03-20 14:20:22 -0700124 }
125
126 int positionInParent() {
127 return getParent().mChildren.indexOf(this);
128 }
Bryce Lee00d586d2017-07-28 20:48:43 -0700129
130 void setIsOnTop(boolean onTop) {
131 mOnTop = onTop;
132 }
133
134 @Override
135 boolean isOnTop() {
136 return mOnTop;
137 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700138 }
139
140 /* Used so we can gain access to some protected members of the {@link WindowToken} class */
141 public static class TestWindowToken extends WindowToken {
142 int adj = 0;
143
144 TestWindowToken(int type, DisplayContent dc) {
145 this(type, dc, false /* persistOnEmpty */);
146 }
147
148 TestWindowToken(int type, DisplayContent dc, boolean persistOnEmpty) {
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700149 super(dc.mService, mock(IBinder.class), type, persistOnEmpty, dc,
Bryce Leeaf691c02017-03-20 14:20:22 -0700150 false /* ownerCanManageAppTokens */);
151 }
152
153 int getWindowsCount() {
154 return mChildren.size();
155 }
156
157 boolean hasWindow(WindowState w) {
158 return mChildren.contains(w);
159 }
160
161 @Override
162 int getAnimLayerAdjustment() {
163 return adj;
164 }
165 }
166
167 /* Used so we can gain access to some protected members of the {@link Task} class */
168 public static class TestTask extends Task {
169 boolean mShouldDeferRemoval = false;
170 boolean mOnDisplayChangedCalled = false;
171 private boolean mUseLocalIsAnimating = false;
172 private boolean mIsAnimating = false;
173
174 TestTask(int taskId, TaskStack stack, int userId, WindowManagerService service, Rect bounds,
175 Configuration overrideConfig, int resizeMode, boolean supportsPictureInPicture,
176 boolean homeTask, TaskWindowContainerController controller) {
177 super(taskId, stack, userId, service, bounds, overrideConfig, resizeMode,
178 supportsPictureInPicture, homeTask, new ActivityManager.TaskDescription(),
179 controller);
180 }
181
182 boolean shouldDeferRemoval() {
183 return mShouldDeferRemoval;
184 }
185
186 int positionInParent() {
187 return getParent().mChildren.indexOf(this);
188 }
189
190 @Override
191 void onDisplayChanged(DisplayContent dc) {
192 super.onDisplayChanged(dc);
193 mOnDisplayChangedCalled = true;
194 }
195
196 @Override
197 boolean isAnimating() {
198 return mUseLocalIsAnimating ? mIsAnimating : super.isAnimating();
199 }
200
201 void setLocalIsAnimating(boolean isAnimating) {
202 mUseLocalIsAnimating = true;
203 mIsAnimating = isAnimating;
204 }
205 }
206
207 /**
208 * Used so we can gain access to some protected members of {@link TaskWindowContainerController}
209 * class.
210 */
211 public static class TestTaskWindowContainerController extends TaskWindowContainerController {
212
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700213 TestTaskWindowContainerController(WindowTestsBase testsBase) {
214 this(testsBase.createStackControllerOnDisplay(testsBase.mDisplayContent));
Bryce Leeaf691c02017-03-20 14:20:22 -0700215 }
216
217 TestTaskWindowContainerController(StackWindowController stackController) {
218 super(sNextTaskId++, new TaskWindowContainerListener() {
219 @Override
220 public void onSnapshotChanged(ActivityManager.TaskSnapshot snapshot) {
221
222 }
223
224 @Override
225 public void requestResize(Rect bounds, int resizeMode) {
226
227 }
228 }, stackController, 0 /* userId */, null /* bounds */,
229 EMPTY /* overrideConfig*/, RESIZE_MODE_UNRESIZEABLE,
230 false /* supportsPictureInPicture */, false /* homeTask*/, true /* toTop*/,
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700231 true /* showForAllUsers */, new ActivityManager.TaskDescription(),
232 stackController.mService);
Bryce Leeaf691c02017-03-20 14:20:22 -0700233 }
234
235 @Override
236 TestTask createTask(int taskId, TaskStack stack, int userId, Rect bounds,
237 Configuration overrideConfig, int resizeMode, boolean supportsPictureInPicture,
238 boolean homeTask, ActivityManager.TaskDescription taskDescription) {
239 return new TestTask(taskId, stack, userId, mService, bounds, overrideConfig, resizeMode,
240 supportsPictureInPicture, homeTask, this);
241 }
242 }
243
244 public static class TestAppWindowContainerController extends AppWindowContainerController {
245
246 final IApplicationToken mToken;
247
248 TestAppWindowContainerController(TestTaskWindowContainerController taskController) {
249 this(taskController, new TestIApplicationToken());
250 }
251
252 TestAppWindowContainerController(TestTaskWindowContainerController taskController,
253 IApplicationToken token) {
254 super(taskController, token, null /* listener */, 0 /* index */,
255 SCREEN_ORIENTATION_UNSPECIFIED, true /* fullscreen */,
256 true /* showForAllUsers */, 0 /* configChanges */, false /* voiceInteraction */,
257 false /* launchTaskBehind */, false /* alwaysFocusable */,
258 0 /* targetSdkVersion */, 0 /* rotationAnimationHint */,
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700259 0 /* inputDispatchingTimeoutNanos */, taskController.mService,
260 null /* overrideConfig */, null /* bounds */);
Bryce Leeaf691c02017-03-20 14:20:22 -0700261 mToken = token;
262 }
263
264 @Override
265 AppWindowToken createAppWindow(WindowManagerService service, IApplicationToken token,
266 boolean voiceInteraction, DisplayContent dc, long inputDispatchingTimeoutNanos,
267 boolean fullscreen, boolean showForAllUsers, int targetSdk, int orientation,
268 int rotationAnimationHint, int configChanges, boolean launchTaskBehind,
269 boolean alwaysFocusable, AppWindowContainerController controller,
270 Configuration overrideConfig, Rect bounds) {
271 return new TestAppWindowToken(service, token, voiceInteraction, dc,
272 inputDispatchingTimeoutNanos, fullscreen, showForAllUsers, targetSdk,
273 orientation,
274 rotationAnimationHint, configChanges, launchTaskBehind, alwaysFocusable,
275 controller, overrideConfig, bounds);
276 }
277
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700278 AppWindowToken getAppWindowToken(DisplayContent dc) {
279 return (AppWindowToken) dc.getWindowToken(mToken.asBinder());
Bryce Leeaf691c02017-03-20 14:20:22 -0700280 }
281 }
282
283 public static class TestIApplicationToken implements IApplicationToken {
284
285 private final Binder mBinder = new Binder();
286 @Override
287 public IBinder asBinder() {
288 return mBinder;
289 }
290 }
291
292 /** Used to track resize reports. */
293 public static class TestWindowState extends WindowState {
294 boolean resizeReported;
295
296 TestWindowState(WindowManagerService service, Session session, IWindow window,
297 WindowManager.LayoutParams attrs, WindowToken token) {
298 super(service, session, window, token, null, OP_NONE, 0, attrs, 0, 0,
299 false /* ownerCanAddInternalSystemWindow */);
300 }
301
302 @Override
303 void reportResized() {
304 super.reportResized();
305 resizeReported = true;
306 }
307
308 @Override
309 public boolean isGoneForLayoutLw() {
310 return false;
311 }
312
313 @Override
314 void updateResizingWindowIfNeeded() {
315 // Used in AppWindowTokenTests#testLandscapeSeascapeRotationRelayout to deceive
316 // the system that it can actually update the window.
317 boolean hadSurface = mHasSurface;
318 mHasSurface = true;
319
320 super.updateResizingWindowIfNeeded();
321
322 mHasSurface = hadSurface;
323 }
324 }
325}