blob: 5a5b31753e22aef6874c4b07f3adb3e2c6a5d910 [file] [log] [blame]
Wale Ogunwale44fbdf52016-11-16 10:18:45 -08001/*
2 * Copyright (C) 2016 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
Wale Ogunwalec5cc3012017-01-13 13:26:16 -080019import android.content.res.Configuration;
20import android.graphics.Rect;
21import android.os.Binder;
22import android.view.IApplicationToken;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080023import org.junit.Assert;
24import org.junit.Before;
25
26import android.content.Context;
27import android.os.IBinder;
28import android.support.test.InstrumentationRegistry;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080029import android.view.IWindow;
30import android.view.WindowManager;
31
32import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
33import static android.app.AppOpsManager.OP_NONE;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080034import static android.content.pm.ActivityInfo.RESIZE_MODE_UNRESIZEABLE;
Wale Ogunwalec5cc3012017-01-13 13:26:16 -080035import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080036import static android.content.res.Configuration.EMPTY;
37import static android.view.WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW;
38import static android.view.WindowManager.LayoutParams.LAST_APPLICATION_WINDOW;
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080039import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
40import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA_OVERLAY;
41import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
42import static android.view.WindowManager.LayoutParams.TYPE_DOCK_DIVIDER;
43import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
44import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
45import static android.view.WindowManager.LayoutParams.TYPE_NAVIGATION_BAR;
46import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
47import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080048import static com.android.server.wm.WindowContainer.POSITION_TOP;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080049import static org.mockito.Mockito.mock;
50
51/**
52 * Common base class for window manager unit test classes.
53 */
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080054class WindowTestsBase {
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080055 static WindowManagerService sWm = null;
56 private final IWindow mIWindow = new TestIWindow();
57 private final Session mMockSession = mock(Session.class);
Wale Ogunwalec5cc3012017-01-13 13:26:16 -080058 static int sNextStackId = FIRST_DYNAMIC_STACK_ID;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080059 private static int sNextTaskId = 0;
60
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080061 private static boolean sOneTimeSetupDone = false;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080062 static DisplayContent sDisplayContent;
63 static WindowLayersController sLayersController;
64 static WindowState sWallpaperWindow;
65 static WindowState sImeWindow;
66 static WindowState sImeDialogWindow;
67 static WindowState sStatusBarWindow;
68 static WindowState sDockedDividerWindow;
69 static WindowState sNavBarWindow;
70 static WindowState sAppWindow;
71 static WindowState sChildAppWindowAbove;
72 static WindowState sChildAppWindowBelow;
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080073
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080074 @Before
75 public void setUp() throws Exception {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080076 if (sOneTimeSetupDone) {
77 return;
78 }
79 sOneTimeSetupDone = true;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080080 final Context context = InstrumentationRegistry.getTargetContext();
81 sWm = TestWindowManagerPolicy.getWindowManagerService(context);
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080082 sLayersController = new WindowLayersController(sWm);
83 sDisplayContent = new DisplayContent(context.getDisplay(), sWm, sLayersController,
84 new WallpaperController(sWm));
Andrii Kuliand2765632016-12-12 22:26:34 -080085 sWm.mRoot.addChild(sDisplayContent, 0);
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080086
87 // Set-up some common windows.
88 sWallpaperWindow = createWindow(null, TYPE_WALLPAPER, sDisplayContent, "wallpaperWindow");
89 sImeWindow = createWindow(null, TYPE_INPUT_METHOD, sDisplayContent, "sImeWindow");
90 sImeDialogWindow =
91 createWindow(null, TYPE_INPUT_METHOD_DIALOG, sDisplayContent, "sImeDialogWindow");
92 sStatusBarWindow = createWindow(null, TYPE_STATUS_BAR, sDisplayContent, "sStatusBarWindow");
93 sNavBarWindow =
Wale Ogunwale6ce0fb82016-12-13 14:24:00 -080094 createWindow(null, TYPE_NAVIGATION_BAR, sDisplayContent, "sNavBarWindow");
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080095 sDockedDividerWindow =
Wale Ogunwale6ce0fb82016-12-13 14:24:00 -080096 createWindow(null, TYPE_DOCK_DIVIDER, sDisplayContent, "sDockedDividerWindow");
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080097 sAppWindow = createWindow(null, TYPE_BASE_APPLICATION, sDisplayContent, "sAppWindow");
98 sChildAppWindowAbove = createWindow(sAppWindow,
99 TYPE_APPLICATION_ATTACHED_DIALOG, sAppWindow.mToken, "sChildAppWindowAbove");
100 sChildAppWindowBelow = createWindow(sAppWindow,
101 TYPE_APPLICATION_MEDIA_OVERLAY, sAppWindow.mToken, "sChildAppWindowBelow");
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800102 }
103
104 /** Asserts that the first entry is greater than the second entry. */
105 void assertGreaterThan(int first, int second) throws Exception {
106 Assert.assertTrue("Excepted " + first + " to be greater than " + second, first > second);
107 }
108
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800109 private WindowToken createWindowToken(DisplayContent dc, int type) {
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800110 if (type < FIRST_APPLICATION_WINDOW || type > LAST_APPLICATION_WINDOW) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800111 return new TestWindowToken(type, dc);
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800112 }
113
Andrii Kuliand2765632016-12-12 22:26:34 -0800114 final TaskStack stack = createTaskStackOnDisplay(dc);
115 final Task task = createTaskInStack(stack, 0 /* userId */);
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800116 final TestAppWindowToken token = new TestAppWindowToken(dc);
Wale Ogunwale72919d22016-12-08 18:58:50 -0800117 task.addChild(token, 0);
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800118 return token;
119 }
120
Wale Ogunwale805d9ec2016-12-07 12:22:08 -0800121 WindowState createWindow(WindowState parent, int type, String name) {
122 return (parent == null)
123 ? createWindow(parent, type, sDisplayContent, name)
124 : createWindow(parent, type, parent.mToken, name);
125 }
126
Jorim Jaggi02886a82016-12-06 09:10:06 -0800127 WindowState createAppWindow(Task task, int type, String name) {
128 final AppWindowToken token = new TestAppWindowToken(sDisplayContent);
129 task.addChild(token, 0);
130 return createWindow(null, type, token, name);
131 }
132
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800133 WindowState createWindow(WindowState parent, int type, DisplayContent dc, String name) {
134 final WindowToken token = createWindowToken(dc, type);
135 return createWindow(parent, type, token, name);
136 }
137
138 WindowState createWindow(WindowState parent, int type, WindowToken token, String name) {
139 final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(type);
140 attrs.setTitle(name);
141
142 final WindowState w = new WindowState(sWm, mMockSession, mIWindow, token, parent, OP_NONE,
143 0, attrs, 0, 0);
144 // TODO: Probably better to make this call in the WindowState ctor to avoid errors with
145 // adding it to the token...
146 token.addWindow(w);
147 return w;
148 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800149
Andrii Kuliand2765632016-12-12 22:26:34 -0800150 /** Creates a {@link TaskStack} and adds it to the specified {@link DisplayContent}. */
151 TaskStack createTaskStackOnDisplay(DisplayContent dc) {
152 final int stackId = sNextStackId++;
153 dc.addStackToDisplay(stackId, true);
154 return sWm.mStackIdToStack.get(stackId);
155 }
156
157 /**Creates a {@link Task} and adds it to the specified {@link TaskStack}. */
158 Task createTaskInStack(TaskStack stack, int userId) {
159 final Task newTask = new Task(sNextTaskId++, stack, userId, sWm, null, EMPTY, false, 0,
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800160 false, null);
161 stack.addTask(newTask, POSITION_TOP);
Andrii Kuliand2765632016-12-12 22:26:34 -0800162 return newTask;
163 }
164
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800165 /* Used so we can gain access to some protected members of the {@link WindowToken} class */
166 class TestWindowToken extends WindowToken {
167
168 TestWindowToken(int type, DisplayContent dc) {
169 this(type, dc, false /* persistOnEmpty */);
170 }
171
172 TestWindowToken(int type, DisplayContent dc, boolean persistOnEmpty) {
173 super(sWm, mock(IBinder.class), type, persistOnEmpty, dc);
174 }
175
176 int getWindowsCount() {
177 return mChildren.size();
178 }
179
180 boolean hasWindow(WindowState w) {
181 return mChildren.contains(w);
182 }
183 }
184
Andrii Kulian4ede3e02017-01-12 11:52:31 -0800185 /** Used so we can gain access to some protected members of the {@link AppWindowToken} class. */
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800186 class TestAppWindowToken extends AppWindowToken {
187
188 TestAppWindowToken(DisplayContent dc) {
189 super(sWm, null, false, dc);
190 }
191
192 int getWindowsCount() {
193 return mChildren.size();
194 }
195
196 boolean hasWindow(WindowState w) {
197 return mChildren.contains(w);
198 }
199
200 WindowState getFirstChild() {
201 return mChildren.getFirst();
202 }
203
204 WindowState getLastChild() {
205 return mChildren.getLast();
206 }
207 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800208
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800209 /* Used so we can gain access to some protected members of the {@link Task} class */
210 class TestTask extends Task {
211
212 boolean mShouldDeferRemoval = false;
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -0800213 boolean mOnDisplayChangedCalled = false;
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800214
215 TestTask(int taskId, TaskStack stack, int userId, WindowManagerService service, Rect bounds,
216 Configuration overrideConfig, boolean isOnTopLauncher, int resizeMode,
217 boolean homeTask, TaskWindowContainerController controller) {
218 super(taskId, stack, userId, service, bounds, overrideConfig, isOnTopLauncher,
219 resizeMode, homeTask, controller);
220 }
221
222 boolean shouldDeferRemoval() {
223 return mShouldDeferRemoval;
224 }
225
226 int positionInParent() {
227 return getParent().mChildren.indexOf(this);
228 }
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -0800229
230 @Override
231 void onDisplayChanged(DisplayContent dc) {
232 super.onDisplayChanged(dc);
233 mOnDisplayChangedCalled = true;
234 }
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800235 }
236
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800237 /**
238 * Used so we can gain access to some protected members of {@link TaskWindowContainerController}
239 * class.
240 */
241 class TestTaskWindowContainerController extends TaskWindowContainerController {
242
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800243 TestTaskWindowContainerController() {
244 this(createTaskStackOnDisplay(sDisplayContent).mStackId);
245 }
246
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800247 TestTaskWindowContainerController(int stackId) {
248 super(sNextTaskId++, stackId, 0 /* userId */, null /* bounds */,
249 EMPTY /* overrideConfig*/, RESIZE_MODE_UNRESIZEABLE, false /* homeTask*/,
250 false /* isOnTopLauncher */, true /* toTop*/, true /* showForAllUsers */);
251 }
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800252
253 @Override
254 TestTask createTask(int taskId, TaskStack stack, int userId, Rect bounds,
255 Configuration overrideConfig, int resizeMode, boolean homeTask,
256 boolean isOnTopLauncher) {
257 return new TestTask(taskId, stack, userId, mService, bounds, overrideConfig,
258 isOnTopLauncher, resizeMode, homeTask, this);
259 }
260 }
261
262 class TestAppWindowContainerController extends AppWindowContainerController {
263
264 final IApplicationToken mToken;
265
266 TestAppWindowContainerController(TestTaskWindowContainerController taskController) {
267 this(taskController, new TestIApplicationToken());
268 }
269
270 TestAppWindowContainerController(TestTaskWindowContainerController taskController,
271 IApplicationToken token) {
272 super(taskController, token, null /* listener */, 0 /* index */,
273 SCREEN_ORIENTATION_UNSPECIFIED, true /* fullscreen */,
274 true /* showForAllUsers */, 0 /* configChanges */, false /* voiceInteraction */,
275 false /* launchTaskBehind */, false /* alwaysFocusable */,
276 0 /* targetSdkVersion */, 0 /* rotationAnimationHint */,
277 0 /* inputDispatchingTimeoutNanos */, sWm);
278 mToken = token;
279 }
280 }
281
282 class TestIApplicationToken implements IApplicationToken {
283
284 private final Binder mBinder = new Binder();
285 @Override
286 public IBinder asBinder() {
287 return mBinder;
288 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800289 }
Andrii Kulian4ede3e02017-01-12 11:52:31 -0800290
291 /** Used to track resize reports. */
292 class TestWindowState extends WindowState {
293 boolean resizeReported;
294
295 TestWindowState(WindowManager.LayoutParams attrs, WindowToken token) {
296 super(sWm, mMockSession, mIWindow, token, null, OP_NONE, 0, attrs, 0, 0);
297 }
298
299 @Override
300 void reportResized() {
301 super.reportResized();
302 resizeReported = true;
303 }
304
305 @Override
306 public boolean isGoneForLayoutLw() {
307 return false;
308 }
309
310 @Override
311 void updateResizingWindowIfNeeded() {
312 // Used in AppWindowTokenTests#testLandscapeSeascapeRotationRelayout to deceive
313 // the system that it can actually update the window.
314 boolean hadSurface = mHasSurface;
315 mHasSurface = true;
316
317 super.updateResizingWindowIfNeeded();
318
319 mHasSurface = hadSurface;
320 }
321 }
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800322}