blob: ae344ddb38f32c702ad269808e15088065b2b860 [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
Jorim Jaggi829b9cd2017-01-23 16:20:53 +010019import android.app.ActivityManager.TaskDescription;
Jorim Jaggi9bafc712017-01-19 17:28:30 +010020import android.app.ActivityManagerInternal;
Wale Ogunwalec5cc3012017-01-13 13:26:16 -080021import android.content.res.Configuration;
22import android.graphics.Rect;
23import android.os.Binder;
24import android.view.IApplicationToken;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080025import org.junit.Assert;
26import org.junit.Before;
Jorim Jaggi9bafc712017-01-19 17:28:30 +010027import org.mockito.Mock;
28import org.mockito.Mockito;
29import org.mockito.MockitoAnnotations;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080030
31import android.content.Context;
32import android.os.IBinder;
33import android.support.test.InstrumentationRegistry;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080034import android.view.IWindow;
35import android.view.WindowManager;
36
37import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
38import static android.app.AppOpsManager.OP_NONE;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080039import static android.content.pm.ActivityInfo.RESIZE_MODE_UNRESIZEABLE;
Wale Ogunwalec5cc3012017-01-13 13:26:16 -080040import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080041import static android.content.res.Configuration.EMPTY;
42import static android.view.WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW;
43import static android.view.WindowManager.LayoutParams.LAST_APPLICATION_WINDOW;
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080044import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
45import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA_OVERLAY;
46import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
47import static android.view.WindowManager.LayoutParams.TYPE_DOCK_DIVIDER;
48import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
49import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
50import static android.view.WindowManager.LayoutParams.TYPE_NAVIGATION_BAR;
51import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
52import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080053import static com.android.server.wm.WindowContainer.POSITION_TOP;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080054import static org.mockito.Mockito.mock;
55
Jorim Jaggi9bafc712017-01-19 17:28:30 +010056import com.android.server.AttributeCache;
57import com.android.server.LocalServices;
58
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080059/**
60 * Common base class for window manager unit test classes.
61 */
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080062class WindowTestsBase {
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080063 static WindowManagerService sWm = null;
Jorim Jaggi9bafc712017-01-19 17:28:30 +010064 static TestWindowManagerPolicy sPolicy = null;
65 private final static IWindow sIWindow = new TestIWindow();
66 private final static Session sMockSession = mock(Session.class);
Wale Ogunwalec5cc3012017-01-13 13:26:16 -080067 static int sNextStackId = FIRST_DYNAMIC_STACK_ID;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080068 private static int sNextTaskId = 0;
69
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080070 private static boolean sOneTimeSetupDone = false;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080071 static DisplayContent sDisplayContent;
72 static WindowLayersController sLayersController;
73 static WindowState sWallpaperWindow;
74 static WindowState sImeWindow;
75 static WindowState sImeDialogWindow;
76 static WindowState sStatusBarWindow;
77 static WindowState sDockedDividerWindow;
78 static WindowState sNavBarWindow;
79 static WindowState sAppWindow;
80 static WindowState sChildAppWindowAbove;
81 static WindowState sChildAppWindowBelow;
Jorim Jaggi9bafc712017-01-19 17:28:30 +010082 static @Mock ActivityManagerInternal sMockAm;
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080083
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080084 @Before
85 public void setUp() throws Exception {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080086 if (sOneTimeSetupDone) {
Jorim Jaggi9bafc712017-01-19 17:28:30 +010087 Mockito.reset(sMockAm);
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080088 return;
89 }
90 sOneTimeSetupDone = true;
Jorim Jaggi9bafc712017-01-19 17:28:30 +010091 MockitoAnnotations.initMocks(this);
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080092 final Context context = InstrumentationRegistry.getTargetContext();
Jorim Jaggi9bafc712017-01-19 17:28:30 +010093 LocalServices.addService(ActivityManagerInternal.class, sMockAm);
94 AttributeCache.init(context);
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080095 sWm = TestWindowManagerPolicy.getWindowManagerService(context);
Jorim Jaggi9bafc712017-01-19 17:28:30 +010096 sPolicy = (TestWindowManagerPolicy) sWm.mPolicy;
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080097 sLayersController = new WindowLayersController(sWm);
98 sDisplayContent = new DisplayContent(context.getDisplay(), sWm, sLayersController,
99 new WallpaperController(sWm));
Andrii Kuliand2765632016-12-12 22:26:34 -0800100 sWm.mRoot.addChild(sDisplayContent, 0);
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100101 sWm.mDisplayEnabled = true;
102 sWm.mDisplayReady = true;
Wale Ogunwale3c1170d2016-12-02 14:44:52 -0800103
104 // Set-up some common windows.
105 sWallpaperWindow = createWindow(null, TYPE_WALLPAPER, sDisplayContent, "wallpaperWindow");
106 sImeWindow = createWindow(null, TYPE_INPUT_METHOD, sDisplayContent, "sImeWindow");
107 sImeDialogWindow =
108 createWindow(null, TYPE_INPUT_METHOD_DIALOG, sDisplayContent, "sImeDialogWindow");
109 sStatusBarWindow = createWindow(null, TYPE_STATUS_BAR, sDisplayContent, "sStatusBarWindow");
110 sNavBarWindow =
Wale Ogunwale6ce0fb82016-12-13 14:24:00 -0800111 createWindow(null, TYPE_NAVIGATION_BAR, sDisplayContent, "sNavBarWindow");
Wale Ogunwale3c1170d2016-12-02 14:44:52 -0800112 sDockedDividerWindow =
Wale Ogunwale6ce0fb82016-12-13 14:24:00 -0800113 createWindow(null, TYPE_DOCK_DIVIDER, sDisplayContent, "sDockedDividerWindow");
Wale Ogunwale3c1170d2016-12-02 14:44:52 -0800114 sAppWindow = createWindow(null, TYPE_BASE_APPLICATION, sDisplayContent, "sAppWindow");
115 sChildAppWindowAbove = createWindow(sAppWindow,
116 TYPE_APPLICATION_ATTACHED_DIALOG, sAppWindow.mToken, "sChildAppWindowAbove");
117 sChildAppWindowBelow = createWindow(sAppWindow,
118 TYPE_APPLICATION_MEDIA_OVERLAY, sAppWindow.mToken, "sChildAppWindowBelow");
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800119 }
120
121 /** Asserts that the first entry is greater than the second entry. */
122 void assertGreaterThan(int first, int second) throws Exception {
123 Assert.assertTrue("Excepted " + first + " to be greater than " + second, first > second);
124 }
125
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100126 /**
127 * Waits until the main handler for WM has processed all messages.
128 */
129 void waitUntilHandlerIdle() {
130 sWm.mH.runWithScissors(() -> { }, 0);
131 }
132
133 private static WindowToken createWindowToken(DisplayContent dc, int type) {
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800134 if (type < FIRST_APPLICATION_WINDOW || type > LAST_APPLICATION_WINDOW) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800135 return new TestWindowToken(type, dc);
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800136 }
137
Andrii Kuliand2765632016-12-12 22:26:34 -0800138 final TaskStack stack = createTaskStackOnDisplay(dc);
139 final Task task = createTaskInStack(stack, 0 /* userId */);
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800140 final TestAppWindowToken token = new TestAppWindowToken(dc);
Wale Ogunwale72919d22016-12-08 18:58:50 -0800141 task.addChild(token, 0);
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800142 return token;
143 }
144
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100145 static WindowState createWindow(WindowState parent, int type, String name) {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -0800146 return (parent == null)
147 ? createWindow(parent, type, sDisplayContent, name)
148 : createWindow(parent, type, parent.mToken, name);
149 }
150
Jorim Jaggi02886a82016-12-06 09:10:06 -0800151 WindowState createAppWindow(Task task, int type, String name) {
152 final AppWindowToken token = new TestAppWindowToken(sDisplayContent);
153 task.addChild(token, 0);
154 return createWindow(null, type, token, name);
155 }
156
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100157 static WindowState createWindow(WindowState parent, int type, DisplayContent dc, String name) {
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800158 final WindowToken token = createWindowToken(dc, type);
159 return createWindow(parent, type, token, name);
160 }
161
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100162 static WindowState createWindow(WindowState parent, int type, WindowToken token, String name) {
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800163 final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(type);
164 attrs.setTitle(name);
165
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100166 final WindowState w = new WindowState(sWm, sMockSession, sIWindow, token, parent, OP_NONE,
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800167 0, attrs, 0, 0);
168 // TODO: Probably better to make this call in the WindowState ctor to avoid errors with
169 // adding it to the token...
170 token.addWindow(w);
171 return w;
172 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800173
Andrii Kuliand2765632016-12-12 22:26:34 -0800174 /** Creates a {@link TaskStack} and adds it to the specified {@link DisplayContent}. */
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100175 static TaskStack createTaskStackOnDisplay(DisplayContent dc) {
Andrii Kuliand2765632016-12-12 22:26:34 -0800176 final int stackId = sNextStackId++;
177 dc.addStackToDisplay(stackId, true);
178 return sWm.mStackIdToStack.get(stackId);
179 }
180
181 /**Creates a {@link Task} and adds it to the specified {@link TaskStack}. */
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100182 static Task createTaskInStack(TaskStack stack, int userId) {
Andrii Kuliand2765632016-12-12 22:26:34 -0800183 final Task newTask = new Task(sNextTaskId++, stack, userId, sWm, null, EMPTY, false, 0,
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100184 false, new TaskDescription(), null);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800185 stack.addTask(newTask, POSITION_TOP);
Andrii Kuliand2765632016-12-12 22:26:34 -0800186 return newTask;
187 }
188
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800189 /* Used so we can gain access to some protected members of the {@link WindowToken} class */
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100190 static class TestWindowToken extends WindowToken {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800191
192 TestWindowToken(int type, DisplayContent dc) {
193 this(type, dc, false /* persistOnEmpty */);
194 }
195
196 TestWindowToken(int type, DisplayContent dc, boolean persistOnEmpty) {
197 super(sWm, mock(IBinder.class), type, persistOnEmpty, dc);
198 }
199
200 int getWindowsCount() {
201 return mChildren.size();
202 }
203
204 boolean hasWindow(WindowState w) {
205 return mChildren.contains(w);
206 }
207 }
208
Andrii Kulian4ede3e02017-01-12 11:52:31 -0800209 /** Used so we can gain access to some protected members of the {@link AppWindowToken} class. */
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100210 static class TestAppWindowToken extends AppWindowToken {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800211
212 TestAppWindowToken(DisplayContent dc) {
213 super(sWm, null, false, dc);
214 }
215
216 int getWindowsCount() {
217 return mChildren.size();
218 }
219
220 boolean hasWindow(WindowState w) {
221 return mChildren.contains(w);
222 }
223
224 WindowState getFirstChild() {
225 return mChildren.getFirst();
226 }
227
228 WindowState getLastChild() {
229 return mChildren.getLast();
230 }
231 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800232
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800233 /* Used so we can gain access to some protected members of the {@link Task} class */
234 class TestTask extends Task {
235
236 boolean mShouldDeferRemoval = false;
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -0800237 boolean mOnDisplayChangedCalled = false;
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800238
239 TestTask(int taskId, TaskStack stack, int userId, WindowManagerService service, Rect bounds,
240 Configuration overrideConfig, boolean isOnTopLauncher, int resizeMode,
241 boolean homeTask, TaskWindowContainerController controller) {
242 super(taskId, stack, userId, service, bounds, overrideConfig, isOnTopLauncher,
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100243 resizeMode, homeTask, new TaskDescription(), controller);
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800244 }
245
246 boolean shouldDeferRemoval() {
247 return mShouldDeferRemoval;
248 }
249
250 int positionInParent() {
251 return getParent().mChildren.indexOf(this);
252 }
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -0800253
254 @Override
255 void onDisplayChanged(DisplayContent dc) {
256 super.onDisplayChanged(dc);
257 mOnDisplayChangedCalled = true;
258 }
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800259 }
260
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800261 /**
262 * Used so we can gain access to some protected members of {@link TaskWindowContainerController}
263 * class.
264 */
265 class TestTaskWindowContainerController extends TaskWindowContainerController {
266
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800267 TestTaskWindowContainerController() {
268 this(createTaskStackOnDisplay(sDisplayContent).mStackId);
269 }
270
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800271 TestTaskWindowContainerController(int stackId) {
Jorim Jaggifb9d78a2017-01-05 18:57:12 +0100272 super(sNextTaskId++, snapshot -> {}, stackId, 0 /* userId */, null /* bounds */,
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800273 EMPTY /* overrideConfig*/, RESIZE_MODE_UNRESIZEABLE, false /* homeTask*/,
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100274 false /* isOnTopLauncher */, true /* toTop*/, true /* showForAllUsers */,
275 new TaskDescription());
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800276 }
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800277
278 @Override
279 TestTask createTask(int taskId, TaskStack stack, int userId, Rect bounds,
280 Configuration overrideConfig, int resizeMode, boolean homeTask,
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100281 boolean isOnTopLauncher, TaskDescription taskDescription) {
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800282 return new TestTask(taskId, stack, userId, mService, bounds, overrideConfig,
283 isOnTopLauncher, resizeMode, homeTask, this);
284 }
285 }
286
287 class TestAppWindowContainerController extends AppWindowContainerController {
288
289 final IApplicationToken mToken;
290
291 TestAppWindowContainerController(TestTaskWindowContainerController taskController) {
292 this(taskController, new TestIApplicationToken());
293 }
294
295 TestAppWindowContainerController(TestTaskWindowContainerController taskController,
296 IApplicationToken token) {
297 super(taskController, token, null /* listener */, 0 /* index */,
298 SCREEN_ORIENTATION_UNSPECIFIED, true /* fullscreen */,
299 true /* showForAllUsers */, 0 /* configChanges */, false /* voiceInteraction */,
300 false /* launchTaskBehind */, false /* alwaysFocusable */,
301 0 /* targetSdkVersion */, 0 /* rotationAnimationHint */,
302 0 /* inputDispatchingTimeoutNanos */, sWm);
303 mToken = token;
304 }
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100305
306 AppWindowToken getAppWindowToken() {
307 return (AppWindowToken) sDisplayContent.getWindowToken(mToken.asBinder());
308 }
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800309 }
310
311 class TestIApplicationToken implements IApplicationToken {
312
313 private final Binder mBinder = new Binder();
314 @Override
315 public IBinder asBinder() {
316 return mBinder;
317 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800318 }
Andrii Kulian4ede3e02017-01-12 11:52:31 -0800319
320 /** Used to track resize reports. */
321 class TestWindowState extends WindowState {
322 boolean resizeReported;
323
324 TestWindowState(WindowManager.LayoutParams attrs, WindowToken token) {
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100325 super(sWm, sMockSession, sIWindow, token, null, OP_NONE, 0, attrs, 0, 0);
Andrii Kulian4ede3e02017-01-12 11:52:31 -0800326 }
327
328 @Override
329 void reportResized() {
330 super.reportResized();
331 resizeReported = true;
332 }
333
334 @Override
335 public boolean isGoneForLayoutLw() {
336 return false;
337 }
338
339 @Override
340 void updateResizingWindowIfNeeded() {
341 // Used in AppWindowTokenTests#testLandscapeSeascapeRotationRelayout to deceive
342 // the system that it can actually update the window.
343 boolean hadSurface = mHasSurface;
344 mHasSurface = true;
345
346 super.updateResizingWindowIfNeeded();
347
348 mHasSurface = hadSurface;
349 }
350 }
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800351}