blob: 7918901f7e2db216726a34fa59c38c1d482c1152 [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
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080039import static android.app.AppOpsManager.OP_NONE;
Andrii Kulian367ff7f2017-01-25 19:45:34 -080040import static android.view.DisplayAdjustments.DEFAULT_DISPLAY_ADJUSTMENTS;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080041import static android.view.WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW;
Wale Ogunwale34247952017-02-19 11:57:53 -080042import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080043import 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 Ogunwale44fbdf52016-11-16 10:18:45 -080053import static org.mockito.Mockito.mock;
54
Jorim Jaggi9bafc712017-01-19 17:28:30 +010055import com.android.server.AttributeCache;
Wale Ogunwale17f175c2017-02-07 16:54:10 -080056
57import java.util.HashSet;
Wale Ogunwale34247952017-02-19 11:57:53 -080058import java.util.LinkedList;
Jorim Jaggi9bafc712017-01-19 17:28:30 +010059
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080060/**
61 * Common base class for window manager unit test classes.
62 */
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080063class WindowTestsBase {
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080064 static WindowManagerService sWm = null;
Wale Ogunwale11cc5162017-04-25 20:29:13 -070065 private static final IWindow sIWindow = new TestIWindow();
Bryce Leea5592862017-10-23 12:57:37 -070066 private static Session sMockSession;
Bryce Lee310de9e2017-03-15 10:18:21 -070067 // The default display is removed in {@link #setUp} and then we iterate over all displays to
68 // 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 -070069 // added display should be treated as default. This cannot be the default display
Wale Ogunwale11cc5162017-04-25 20:29:13 -070070 private static int sNextDisplayId = DEFAULT_DISPLAY + 1;
Wale Ogunwale44f036f2017-09-29 05:09:09 -070071 static int sNextStackId = 1000;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080072
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080073 private static boolean sOneTimeSetupDone = false;
Wale Ogunwale11cc5162017-04-25 20:29:13 -070074 DisplayContent mDisplayContent;
75 DisplayInfo mDisplayInfo = new DisplayInfo();
Wale Ogunwale11cc5162017-04-25 20:29:13 -070076 WindowState mWallpaperWindow;
77 WindowState mImeWindow;
78 WindowState mImeDialogWindow;
79 WindowState mStatusBarWindow;
80 WindowState mDockedDividerWindow;
81 WindowState mNavBarWindow;
82 WindowState mAppWindow;
83 WindowState mChildAppWindowAbove;
84 WindowState mChildAppWindowBelow;
85 HashSet<WindowState> mCommonWindows;
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080086
chaviw2fb06bc2018-01-19 17:09:15 -080087 private final SurfaceAnimationRunner mSurfaceAnimationRunner;
88
89 public WindowTestsBase() {
90 this(new SurfaceAnimationRunner());
91 }
92
93 public WindowTestsBase(SurfaceAnimationRunner surfaceAnimationRunner) {
94 mSurfaceAnimationRunner = surfaceAnimationRunner;
95 }
96
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080097 @Before
98 public void setUp() throws Exception {
Wale Ogunwale11cc5162017-04-25 20:29:13 -070099 if (!sOneTimeSetupDone) {
100 sOneTimeSetupDone = true;
Jorim Jaggi771d5b42017-10-20 15:07:41 +0200101
102 // Allows to mock package local classes and methods
103 System.setProperty("dexmaker.share_classloader", "true");
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700104 MockitoAnnotations.initMocks(this);
Bryce Leea5592862017-10-23 12:57:37 -0700105 sMockSession = mock(Session.class);
Wale Ogunwale3c1170d2016-12-02 14:44:52 -0800106 }
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700107
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800108 final Context context = InstrumentationRegistry.getTargetContext();
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100109 AttributeCache.init(context);
Robert Carrb1579c82017-09-05 14:54:47 -0700110
chaviw2fb06bc2018-01-19 17:09:15 -0800111 sWm = TestWindowManagerPolicy.getWindowManagerService(context, mSurfaceAnimationRunner);
Robert Carrb1579c82017-09-05 14:54:47 -0700112 beforeCreateDisplay();
Bryce Lee6272c7f2017-03-17 17:22:35 -0700113
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700114 context.getDisplay().getDisplayInfo(mDisplayInfo);
115 mDisplayContent = createNewDisplay();
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100116 sWm.mDisplayEnabled = true;
117 sWm.mDisplayReady = true;
Wale Ogunwale3c1170d2016-12-02 14:44:52 -0800118
119 // Set-up some common windows.
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700120 mCommonWindows = new HashSet();
121 mWallpaperWindow = createCommonWindow(null, TYPE_WALLPAPER, "wallpaperWindow");
122 mImeWindow = createCommonWindow(null, TYPE_INPUT_METHOD, "mImeWindow");
123 sWm.mInputMethodWindow = mImeWindow;
124 mImeDialogWindow = createCommonWindow(null, TYPE_INPUT_METHOD_DIALOG, "mImeDialogWindow");
125 mStatusBarWindow = createCommonWindow(null, TYPE_STATUS_BAR, "mStatusBarWindow");
126 mNavBarWindow = createCommonWindow(null, TYPE_NAVIGATION_BAR, "mNavBarWindow");
127 mDockedDividerWindow = createCommonWindow(null, TYPE_DOCK_DIVIDER, "mDockedDividerWindow");
128 mAppWindow = createCommonWindow(null, TYPE_BASE_APPLICATION, "mAppWindow");
129 mChildAppWindowAbove = createCommonWindow(mAppWindow, TYPE_APPLICATION_ATTACHED_DIALOG,
130 "mChildAppWindowAbove");
131 mChildAppWindowBelow = createCommonWindow(mAppWindow, TYPE_APPLICATION_MEDIA_OVERLAY,
132 "mChildAppWindowBelow");
Jorim Jaggi153badb2017-05-24 13:18:28 +0200133
134 // Adding a display will cause freezing the display. Make sure to wait until it's unfrozen
135 // to not run into race conditions with the tests.
136 waitUntilHandlersIdle();
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800137 }
138
Robert Carrb1579c82017-09-05 14:54:47 -0700139 void beforeCreateDisplay() {
140 // Called before display is created.
141 }
142
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800143 @After
144 public void tearDown() throws Exception {
Wale Ogunwale34247952017-02-19 11:57:53 -0800145 final LinkedList<WindowState> nonCommonWindows = new LinkedList();
Wale Ogunwale027f4752017-05-12 10:37:16 -0700146
147 synchronized (sWm.mWindowMap) {
148 sWm.mRoot.forAllWindows(w -> {
149 if (!mCommonWindows.contains(w)) {
150 nonCommonWindows.addLast(w);
151 }
152 }, true /* traverseTopToBottom */);
153
154 while (!nonCommonWindows.isEmpty()) {
155 nonCommonWindows.pollLast().removeImmediately();
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800156 }
Wale Ogunwale34247952017-02-19 11:57:53 -0800157
Wale Ogunwale027f4752017-05-12 10:37:16 -0700158 mDisplayContent.removeImmediately();
159 sWm.mInputMethodTarget = null;
Wale Ogunwale34247952017-02-19 11:57:53 -0800160 }
Jorim Jaggi153badb2017-05-24 13:18:28 +0200161
162 // Wait until everything is really cleaned up.
163 waitUntilHandlersIdle();
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800164 }
165
Robert Carrb1579c82017-09-05 14:54:47 -0700166 /**
167 * @return A SurfaceBuilderFactory to inject in to the WindowManagerService during
168 * set-up (or null).
169 */
170 SurfaceBuilderFactory getSurfaceBuilderFactory() {
171 return null;
172 }
173
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700174 private WindowState createCommonWindow(WindowState parent, int type, String name) {
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800175 final WindowState win = createWindow(parent, type, name);
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700176 mCommonWindows.add(win);
Wale Ogunwale34247952017-02-19 11:57:53 -0800177 // Prevent common windows from been IMe targets
178 win.mAttrs.flags |= FLAG_NOT_FOCUSABLE;
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800179 return win;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800180 }
181
182 /** Asserts that the first entry is greater than the second entry. */
183 void assertGreaterThan(int first, int second) throws Exception {
184 Assert.assertTrue("Excepted " + first + " to be greater than " + second, first > second);
185 }
186
Robert Carrb1579c82017-09-05 14:54:47 -0700187 /** Asserts that the first entry is greater than the second entry. */
188 void assertLessThan(int first, int second) throws Exception {
189 Assert.assertTrue("Excepted " + first + " to be less than " + second, first < second);
190 }
191
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100192 /**
193 * Waits until the main handler for WM has processed all messages.
194 */
Jorim Jaggied7993b2017-03-28 18:50:01 +0100195 void waitUntilHandlersIdle() {
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100196 sWm.mH.runWithScissors(() -> { }, 0);
Jorim Jaggied7993b2017-03-28 18:50:01 +0100197 sWm.mAnimationHandler.runWithScissors(() -> { }, 0);
Jorim Jaggi21c39a72017-10-20 15:47:51 +0200198 SurfaceAnimationThread.getHandler().runWithScissors(() -> { }, 0);
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100199 }
200
Wale Ogunwale68278562017-09-23 17:13:55 -0700201 private WindowToken createWindowToken(
202 DisplayContent dc, int windowingMode, int activityType, int type) {
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800203 if (type < FIRST_APPLICATION_WINDOW || type > LAST_APPLICATION_WINDOW) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700204 return new WindowTestUtils.TestWindowToken(type, dc);
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800205 }
206
Wale Ogunwale68278562017-09-23 17:13:55 -0700207 final TaskStack stack =
208 createStackControllerOnStackOnDisplay(windowingMode, activityType, dc).mContainer;
Andrii Kuliand2765632016-12-12 22:26:34 -0800209 final Task task = createTaskInStack(stack, 0 /* userId */);
Bryce Leeaf691c02017-03-20 14:20:22 -0700210 final WindowTestUtils.TestAppWindowToken token = new WindowTestUtils.TestAppWindowToken(dc);
Wale Ogunwale72919d22016-12-08 18:58:50 -0800211 task.addChild(token, 0);
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800212 return token;
213 }
214
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700215 WindowState createWindow(WindowState parent, int type, String name) {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -0800216 return (parent == null)
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700217 ? createWindow(parent, type, mDisplayContent, name)
Wale Ogunwale805d9ec2016-12-07 12:22:08 -0800218 : createWindow(parent, type, parent.mToken, name);
219 }
220
Wale Ogunwale68278562017-09-23 17:13:55 -0700221 WindowState createWindowOnStack(WindowState parent, int windowingMode, int activityType,
222 int type, DisplayContent dc, String name) {
223 final WindowToken token = createWindowToken(dc, windowingMode, activityType, type);
Winson Chung83471632016-12-13 11:02:12 -0800224 return createWindow(parent, type, token, name);
225 }
226
Jorim Jaggi02886a82016-12-06 09:10:06 -0800227 WindowState createAppWindow(Task task, int type, String name) {
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700228 final AppWindowToken token = new WindowTestUtils.TestAppWindowToken(mDisplayContent);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800229 task.addChild(token, 0);
230 return createWindow(null, type, token, name);
231 }
232
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700233 WindowState createWindow(WindowState parent, int type, DisplayContent dc, String name) {
Wale Ogunwale68278562017-09-23 17:13:55 -0700234 final WindowToken token = createWindowToken(
235 dc, WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, type);
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800236 return createWindow(parent, type, token, name);
237 }
238
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700239 WindowState createWindow(WindowState parent, int type, DisplayContent dc, String name,
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800240 boolean ownerCanAddInternalSystemWindow) {
Wale Ogunwale68278562017-09-23 17:13:55 -0700241 final WindowToken token = createWindowToken(
242 dc, WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, type);
Daichi Hironoda0748d2017-12-13 12:48:59 +0900243 return createWindow(parent, type, token, name, 0 /* ownerId */,
244 ownerCanAddInternalSystemWindow);
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800245 }
246
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100247 static WindowState createWindow(WindowState parent, int type, WindowToken token, String name) {
Daichi Hironoda0748d2017-12-13 12:48:59 +0900248 return createWindow(parent, type, token, name, 0 /* ownerId */,
249 false /* ownerCanAddInternalSystemWindow */);
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800250 }
251
252 static WindowState createWindow(WindowState parent, int type, WindowToken token, String name,
Daichi Hironoda0748d2017-12-13 12:48:59 +0900253 int ownerId, boolean ownerCanAddInternalSystemWindow) {
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800254 final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(type);
255 attrs.setTitle(name);
256
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -0800257 final WindowState w = new WindowState(sWm, sMockSession, sIWindow, token, parent, OP_NONE,
Daichi Hironoda0748d2017-12-13 12:48:59 +0900258 0, attrs, VISIBLE, ownerId, ownerCanAddInternalSystemWindow);
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800259 // TODO: Probably better to make this call in the WindowState ctor to avoid errors with
260 // adding it to the token...
261 token.addWindow(w);
262 return w;
263 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800264
Andrii Kuliand2765632016-12-12 22:26:34 -0800265 /** Creates a {@link TaskStack} and adds it to the specified {@link DisplayContent}. */
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700266 TaskStack createTaskStackOnDisplay(DisplayContent dc) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800267 return createStackControllerOnDisplay(dc).mContainer;
268 }
269
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700270 StackWindowController createStackControllerOnDisplay(DisplayContent dc) {
Wale Ogunwale68278562017-09-23 17:13:55 -0700271 return createStackControllerOnStackOnDisplay(
272 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, dc);
Winson Chung83471632016-12-13 11:02:12 -0800273 }
274
Wale Ogunwale68278562017-09-23 17:13:55 -0700275 StackWindowController createStackControllerOnStackOnDisplay(
276 int windowingMode, int activityType, DisplayContent dc) {
Wale Ogunwalef75962a2017-08-23 14:58:04 -0700277 final Configuration overrideConfig = new Configuration();
Wale Ogunwale68278562017-09-23 17:13:55 -0700278 overrideConfig.windowConfiguration.setWindowingMode(windowingMode);
279 overrideConfig.windowConfiguration.setActivityType(activityType);
280 final int stackId = ++sNextStackId;
Wale Ogunwale034a8ec2017-09-02 17:14:40 -0700281 final StackWindowController controller = new StackWindowController(stackId, null,
282 dc.getDisplayId(), true /* onTop */, new Rect(), sWm);
283 controller.onOverrideConfigurationChanged(overrideConfig);
284 return controller;
Andrii Kuliand2765632016-12-12 22:26:34 -0800285 }
286
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800287 /** Creates a {@link Task} and adds it to the specified {@link TaskStack}. */
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700288 Task createTaskInStack(TaskStack stack, int userId) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700289 return WindowTestUtils.createTaskInStack(sWm, stack, userId);
Andrii Kuliand2765632016-12-12 22:26:34 -0800290 }
291
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800292 /** Creates a {@link DisplayContent} and adds it to the system. */
293 DisplayContent createNewDisplay() {
294 final int displayId = sNextDisplayId++;
295 final Display display = new Display(DisplayManagerGlobal.getInstance(), displayId,
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700296 mDisplayInfo, DEFAULT_DISPLAY_ADJUSTMENTS);
Robert Carrb1579c82017-09-05 14:54:47 -0700297 return new DisplayContent(display, sWm, new WallpaperController(sWm));
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800298 }
299
Bryce Leeaf691c02017-03-20 14:20:22 -0700300 /** Creates a {@link com.android.server.wm.WindowTestUtils.TestWindowState} */
301 WindowTestUtils.TestWindowState createWindowState(WindowManager.LayoutParams attrs,
302 WindowToken token) {
303 return new WindowTestUtils.TestWindowState(sWm, sMockSession, sIWindow, attrs, token);
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800304 }
305
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800306}