blob: 4e810f2c3973503103a09b342ade93f6c9bbce7d [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 Ogunwale68278562017-09-23 17:13:55 -070019import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD;
20import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
Wale Ogunwale11cc5162017-04-25 20:29:13 -070021import static android.view.Display.DEFAULT_DISPLAY;
David Stevens46939562017-03-24 13:04:00 -070022import static android.view.View.VISIBLE;
23
Wale Ogunwale687b4272017-07-27 02:56:23 -070024import android.content.res.Configuration;
Wale Ogunwalec5cc3012017-01-13 13:26:16 -080025import android.graphics.Rect;
Andrii Kulian367ff7f2017-01-25 19:45:34 -080026import android.hardware.display.DisplayManagerGlobal;
Andrii Kulian367ff7f2017-01-25 19:45:34 -080027import android.view.Display;
28import android.view.DisplayInfo;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080029import org.junit.Assert;
Wale Ogunwale17f175c2017-02-07 16:54:10 -080030import org.junit.After;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080031import org.junit.Before;
Jorim Jaggi9bafc712017-01-19 17:28:30 +010032import org.mockito.MockitoAnnotations;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080033
34import android.content.Context;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080035import android.support.test.InstrumentationRegistry;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080036import android.view.IWindow;
37import android.view.WindowManager;
38
39import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
Winson Chung83471632016-12-13 11:02:12 -080040import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080041import static android.app.AppOpsManager.OP_NONE;
Andrii Kulian367ff7f2017-01-25 19:45:34 -080042import static android.view.DisplayAdjustments.DEFAULT_DISPLAY_ADJUSTMENTS;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080043import static android.view.WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW;
Wale Ogunwale34247952017-02-19 11:57:53 -080044import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080045import static android.view.WindowManager.LayoutParams.LAST_APPLICATION_WINDOW;
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080046import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
47import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA_OVERLAY;
48import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
49import static android.view.WindowManager.LayoutParams.TYPE_DOCK_DIVIDER;
50import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
51import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
52import static android.view.WindowManager.LayoutParams.TYPE_NAVIGATION_BAR;
53import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
54import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080055import static org.mockito.Mockito.mock;
56
Jorim Jaggi9bafc712017-01-19 17:28:30 +010057import com.android.server.AttributeCache;
Wale Ogunwale17f175c2017-02-07 16:54:10 -080058
59import java.util.HashSet;
Wale Ogunwale34247952017-02-19 11:57:53 -080060import java.util.LinkedList;
Jorim Jaggi9bafc712017-01-19 17:28:30 +010061
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080062/**
63 * Common base class for window manager unit test classes.
64 */
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080065class WindowTestsBase {
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080066 static WindowManagerService sWm = null;
Wale Ogunwale11cc5162017-04-25 20:29:13 -070067 private static final IWindow sIWindow = new TestIWindow();
68 private static final Session sMockSession = mock(Session.class);
Bryce Lee310de9e2017-03-15 10:18:21 -070069 // The default display is removed in {@link #setUp} and then we iterate over all displays to
70 // make sure we don't collide with any existing display. If we run into no other display, the
Bryce Lee6272c7f2017-03-17 17:22:35 -070071 // added display should be treated as default. This cannot be the default display
Wale Ogunwale11cc5162017-04-25 20:29:13 -070072 private static int sNextDisplayId = DEFAULT_DISPLAY + 1;
Bryce Leeaf691c02017-03-20 14:20:22 -070073 private static int sNextStackId = FIRST_DYNAMIC_STACK_ID;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080074
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080075 private static boolean sOneTimeSetupDone = false;
Wale Ogunwale11cc5162017-04-25 20:29:13 -070076 DisplayContent mDisplayContent;
77 DisplayInfo mDisplayInfo = new DisplayInfo();
78 WindowLayersController mLayersController;
79 WindowState mWallpaperWindow;
80 WindowState mImeWindow;
81 WindowState mImeDialogWindow;
82 WindowState mStatusBarWindow;
83 WindowState mDockedDividerWindow;
84 WindowState mNavBarWindow;
85 WindowState mAppWindow;
86 WindowState mChildAppWindowAbove;
87 WindowState mChildAppWindowBelow;
88 HashSet<WindowState> mCommonWindows;
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080089
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080090 @Before
91 public void setUp() throws Exception {
Wale Ogunwale11cc5162017-04-25 20:29:13 -070092 if (!sOneTimeSetupDone) {
93 sOneTimeSetupDone = true;
94 MockitoAnnotations.initMocks(this);
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080095 }
Wale Ogunwale11cc5162017-04-25 20:29:13 -070096
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080097 final Context context = InstrumentationRegistry.getTargetContext();
Jorim Jaggi9bafc712017-01-19 17:28:30 +010098 AttributeCache.init(context);
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080099 sWm = TestWindowManagerPolicy.getWindowManagerService(context);
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700100 mLayersController = new WindowLayersController(sWm);
Bryce Lee6272c7f2017-03-17 17:22:35 -0700101
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700102 context.getDisplay().getDisplayInfo(mDisplayInfo);
103 mDisplayContent = createNewDisplay();
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100104 sWm.mDisplayEnabled = true;
105 sWm.mDisplayReady = true;
Wale Ogunwale3c1170d2016-12-02 14:44:52 -0800106
107 // Set-up some common windows.
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700108 mCommonWindows = new HashSet();
109 mWallpaperWindow = createCommonWindow(null, TYPE_WALLPAPER, "wallpaperWindow");
110 mImeWindow = createCommonWindow(null, TYPE_INPUT_METHOD, "mImeWindow");
111 sWm.mInputMethodWindow = mImeWindow;
112 mImeDialogWindow = createCommonWindow(null, TYPE_INPUT_METHOD_DIALOG, "mImeDialogWindow");
113 mStatusBarWindow = createCommonWindow(null, TYPE_STATUS_BAR, "mStatusBarWindow");
114 mNavBarWindow = createCommonWindow(null, TYPE_NAVIGATION_BAR, "mNavBarWindow");
115 mDockedDividerWindow = createCommonWindow(null, TYPE_DOCK_DIVIDER, "mDockedDividerWindow");
116 mAppWindow = createCommonWindow(null, TYPE_BASE_APPLICATION, "mAppWindow");
117 mChildAppWindowAbove = createCommonWindow(mAppWindow, TYPE_APPLICATION_ATTACHED_DIALOG,
118 "mChildAppWindowAbove");
119 mChildAppWindowBelow = createCommonWindow(mAppWindow, TYPE_APPLICATION_MEDIA_OVERLAY,
120 "mChildAppWindowBelow");
Jorim Jaggi153badb2017-05-24 13:18:28 +0200121
122 // Adding a display will cause freezing the display. Make sure to wait until it's unfrozen
123 // to not run into race conditions with the tests.
124 waitUntilHandlersIdle();
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800125 }
126
127 @After
128 public void tearDown() throws Exception {
Wale Ogunwale34247952017-02-19 11:57:53 -0800129 final LinkedList<WindowState> nonCommonWindows = new LinkedList();
Wale Ogunwale027f4752017-05-12 10:37:16 -0700130
131 synchronized (sWm.mWindowMap) {
132 sWm.mRoot.forAllWindows(w -> {
133 if (!mCommonWindows.contains(w)) {
134 nonCommonWindows.addLast(w);
135 }
136 }, true /* traverseTopToBottom */);
137
138 while (!nonCommonWindows.isEmpty()) {
139 nonCommonWindows.pollLast().removeImmediately();
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800140 }
Wale Ogunwale34247952017-02-19 11:57:53 -0800141
Wale Ogunwale027f4752017-05-12 10:37:16 -0700142 mDisplayContent.removeImmediately();
143 sWm.mInputMethodTarget = null;
Wale Ogunwale34247952017-02-19 11:57:53 -0800144 }
Jorim Jaggi153badb2017-05-24 13:18:28 +0200145
146 // Wait until everything is really cleaned up.
147 waitUntilHandlersIdle();
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800148 }
149
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700150 private WindowState createCommonWindow(WindowState parent, int type, String name) {
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800151 final WindowState win = createWindow(parent, type, name);
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700152 mCommonWindows.add(win);
Wale Ogunwale34247952017-02-19 11:57:53 -0800153 // Prevent common windows from been IMe targets
154 win.mAttrs.flags |= FLAG_NOT_FOCUSABLE;
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800155 return win;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800156 }
157
158 /** Asserts that the first entry is greater than the second entry. */
159 void assertGreaterThan(int first, int second) throws Exception {
160 Assert.assertTrue("Excepted " + first + " to be greater than " + second, first > second);
161 }
162
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100163 /**
164 * Waits until the main handler for WM has processed all messages.
165 */
Jorim Jaggied7993b2017-03-28 18:50:01 +0100166 void waitUntilHandlersIdle() {
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100167 sWm.mH.runWithScissors(() -> { }, 0);
Jorim Jaggied7993b2017-03-28 18:50:01 +0100168 sWm.mAnimationHandler.runWithScissors(() -> { }, 0);
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100169 }
170
Wale Ogunwale68278562017-09-23 17:13:55 -0700171 private WindowToken createWindowToken(
172 DisplayContent dc, int windowingMode, int activityType, int type) {
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800173 if (type < FIRST_APPLICATION_WINDOW || type > LAST_APPLICATION_WINDOW) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700174 return new WindowTestUtils.TestWindowToken(type, dc);
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800175 }
176
Wale Ogunwale68278562017-09-23 17:13:55 -0700177 final TaskStack stack =
178 createStackControllerOnStackOnDisplay(windowingMode, activityType, dc).mContainer;
Andrii Kuliand2765632016-12-12 22:26:34 -0800179 final Task task = createTaskInStack(stack, 0 /* userId */);
Bryce Leeaf691c02017-03-20 14:20:22 -0700180 final WindowTestUtils.TestAppWindowToken token = new WindowTestUtils.TestAppWindowToken(dc);
Wale Ogunwale72919d22016-12-08 18:58:50 -0800181 task.addChild(token, 0);
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800182 return token;
183 }
184
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700185 WindowState createWindow(WindowState parent, int type, String name) {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -0800186 return (parent == null)
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700187 ? createWindow(parent, type, mDisplayContent, name)
Wale Ogunwale805d9ec2016-12-07 12:22:08 -0800188 : createWindow(parent, type, parent.mToken, name);
189 }
190
Wale Ogunwale68278562017-09-23 17:13:55 -0700191 WindowState createWindowOnStack(WindowState parent, int windowingMode, int activityType,
192 int type, DisplayContent dc, String name) {
193 final WindowToken token = createWindowToken(dc, windowingMode, activityType, type);
Winson Chung83471632016-12-13 11:02:12 -0800194 return createWindow(parent, type, token, name);
195 }
196
Jorim Jaggi02886a82016-12-06 09:10:06 -0800197 WindowState createAppWindow(Task task, int type, String name) {
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700198 final AppWindowToken token = new WindowTestUtils.TestAppWindowToken(mDisplayContent);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800199 task.addChild(token, 0);
200 return createWindow(null, type, token, name);
201 }
202
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700203 WindowState createWindow(WindowState parent, int type, DisplayContent dc, String name) {
Wale Ogunwale68278562017-09-23 17:13:55 -0700204 final WindowToken token = createWindowToken(
205 dc, WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, type);
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800206 return createWindow(parent, type, token, name);
207 }
208
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700209 WindowState createWindow(WindowState parent, int type, DisplayContent dc, String name,
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800210 boolean ownerCanAddInternalSystemWindow) {
Wale Ogunwale68278562017-09-23 17:13:55 -0700211 final WindowToken token = createWindowToken(
212 dc, WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, type);
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800213 return createWindow(parent, type, token, name, ownerCanAddInternalSystemWindow);
214 }
215
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100216 static WindowState createWindow(WindowState parent, int type, WindowToken token, String name) {
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800217 return createWindow(parent, type, token, name, false /* ownerCanAddInternalSystemWindow */);
218 }
219
220 static WindowState createWindow(WindowState parent, int type, WindowToken token, String name,
221 boolean ownerCanAddInternalSystemWindow) {
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800222 final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(type);
223 attrs.setTitle(name);
224
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -0800225 final WindowState w = new WindowState(sWm, sMockSession, sIWindow, token, parent, OP_NONE,
David Stevens46939562017-03-24 13:04:00 -0700226 0, attrs, VISIBLE, 0, ownerCanAddInternalSystemWindow);
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800227 // TODO: Probably better to make this call in the WindowState ctor to avoid errors with
228 // adding it to the token...
229 token.addWindow(w);
230 return w;
231 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800232
Andrii Kuliand2765632016-12-12 22:26:34 -0800233 /** Creates a {@link TaskStack} and adds it to the specified {@link DisplayContent}. */
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700234 TaskStack createTaskStackOnDisplay(DisplayContent dc) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800235 return createStackControllerOnDisplay(dc).mContainer;
236 }
237
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700238 StackWindowController createStackControllerOnDisplay(DisplayContent dc) {
Wale Ogunwale68278562017-09-23 17:13:55 -0700239 return createStackControllerOnStackOnDisplay(
240 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, dc);
Winson Chung83471632016-12-13 11:02:12 -0800241 }
242
Wale Ogunwale68278562017-09-23 17:13:55 -0700243 StackWindowController createStackControllerOnStackOnDisplay(
244 int windowingMode, int activityType, DisplayContent dc) {
Wale Ogunwalef75962a2017-08-23 14:58:04 -0700245 final Configuration overrideConfig = new Configuration();
Wale Ogunwale68278562017-09-23 17:13:55 -0700246 overrideConfig.windowConfiguration.setWindowingMode(windowingMode);
247 overrideConfig.windowConfiguration.setActivityType(activityType);
248 final int stackId = ++sNextStackId;
Wale Ogunwale034a8ec2017-09-02 17:14:40 -0700249 final StackWindowController controller = new StackWindowController(stackId, null,
250 dc.getDisplayId(), true /* onTop */, new Rect(), sWm);
251 controller.onOverrideConfigurationChanged(overrideConfig);
252 return controller;
Andrii Kuliand2765632016-12-12 22:26:34 -0800253 }
254
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800255 /** Creates a {@link Task} and adds it to the specified {@link TaskStack}. */
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700256 Task createTaskInStack(TaskStack stack, int userId) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700257 return WindowTestUtils.createTaskInStack(sWm, stack, userId);
Andrii Kuliand2765632016-12-12 22:26:34 -0800258 }
259
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800260 /** Creates a {@link DisplayContent} and adds it to the system. */
261 DisplayContent createNewDisplay() {
262 final int displayId = sNextDisplayId++;
263 final Display display = new Display(DisplayManagerGlobal.getInstance(), displayId,
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700264 mDisplayInfo, DEFAULT_DISPLAY_ADJUSTMENTS);
265 return new DisplayContent(display, sWm, mLayersController, new WallpaperController(sWm));
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800266 }
267
Bryce Leeaf691c02017-03-20 14:20:22 -0700268 /** Creates a {@link com.android.server.wm.WindowTestUtils.TestWindowState} */
269 WindowTestUtils.TestWindowState createWindowState(WindowManager.LayoutParams attrs,
270 WindowToken token) {
271 return new WindowTestUtils.TestWindowState(sWm, sMockSession, sIWindow, attrs, token);
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800272 }
273
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800274}