blob: eb8cf9160d5b3088b8a69e51cceae4d1fdc5880c [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 Ogunwale11cc5162017-04-25 20:29:13 -070019import static android.view.Display.DEFAULT_DISPLAY;
David Stevens46939562017-03-24 13:04:00 -070020import static android.view.View.VISIBLE;
21
Wale Ogunwale687b4272017-07-27 02:56:23 -070022import android.content.res.Configuration;
Wale Ogunwalec5cc3012017-01-13 13:26:16 -080023import android.graphics.Rect;
Andrii Kulian367ff7f2017-01-25 19:45:34 -080024import android.hardware.display.DisplayManagerGlobal;
Andrii Kulian367ff7f2017-01-25 19:45:34 -080025import android.view.Display;
26import android.view.DisplayInfo;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080027import org.junit.Assert;
Wale Ogunwale17f175c2017-02-07 16:54:10 -080028import org.junit.After;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080029import org.junit.Before;
Jorim Jaggi9bafc712017-01-19 17:28:30 +010030import org.mockito.MockitoAnnotations;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080031
32import android.content.Context;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080033import 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;
Winson Chung83471632016-12-13 11:02:12 -080038import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
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();
66 private static final Session sMockSession = mock(Session.class);
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;
Bryce Leeaf691c02017-03-20 14:20:22 -070071 private static int sNextStackId = FIRST_DYNAMIC_STACK_ID;
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();
76 WindowLayersController mLayersController;
77 WindowState mWallpaperWindow;
78 WindowState mImeWindow;
79 WindowState mImeDialogWindow;
80 WindowState mStatusBarWindow;
81 WindowState mDockedDividerWindow;
82 WindowState mNavBarWindow;
83 WindowState mAppWindow;
84 WindowState mChildAppWindowAbove;
85 WindowState mChildAppWindowBelow;
86 HashSet<WindowState> mCommonWindows;
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080087
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080088 @Before
89 public void setUp() throws Exception {
Wale Ogunwale11cc5162017-04-25 20:29:13 -070090 if (!sOneTimeSetupDone) {
91 sOneTimeSetupDone = true;
92 MockitoAnnotations.initMocks(this);
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080093 }
Wale Ogunwale11cc5162017-04-25 20:29:13 -070094
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080095 final Context context = InstrumentationRegistry.getTargetContext();
Jorim Jaggi9bafc712017-01-19 17:28:30 +010096 AttributeCache.init(context);
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080097 sWm = TestWindowManagerPolicy.getWindowManagerService(context);
Wale Ogunwale11cc5162017-04-25 20:29:13 -070098 mLayersController = new WindowLayersController(sWm);
Bryce Lee6272c7f2017-03-17 17:22:35 -070099
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700100 context.getDisplay().getDisplayInfo(mDisplayInfo);
101 mDisplayContent = createNewDisplay();
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100102 sWm.mDisplayEnabled = true;
103 sWm.mDisplayReady = true;
Wale Ogunwale3c1170d2016-12-02 14:44:52 -0800104
105 // Set-up some common windows.
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700106 mCommonWindows = new HashSet();
107 mWallpaperWindow = createCommonWindow(null, TYPE_WALLPAPER, "wallpaperWindow");
108 mImeWindow = createCommonWindow(null, TYPE_INPUT_METHOD, "mImeWindow");
109 sWm.mInputMethodWindow = mImeWindow;
110 mImeDialogWindow = createCommonWindow(null, TYPE_INPUT_METHOD_DIALOG, "mImeDialogWindow");
111 mStatusBarWindow = createCommonWindow(null, TYPE_STATUS_BAR, "mStatusBarWindow");
112 mNavBarWindow = createCommonWindow(null, TYPE_NAVIGATION_BAR, "mNavBarWindow");
113 mDockedDividerWindow = createCommonWindow(null, TYPE_DOCK_DIVIDER, "mDockedDividerWindow");
114 mAppWindow = createCommonWindow(null, TYPE_BASE_APPLICATION, "mAppWindow");
115 mChildAppWindowAbove = createCommonWindow(mAppWindow, TYPE_APPLICATION_ATTACHED_DIALOG,
116 "mChildAppWindowAbove");
117 mChildAppWindowBelow = createCommonWindow(mAppWindow, TYPE_APPLICATION_MEDIA_OVERLAY,
118 "mChildAppWindowBelow");
Jorim Jaggi153badb2017-05-24 13:18:28 +0200119
120 // Adding a display will cause freezing the display. Make sure to wait until it's unfrozen
121 // to not run into race conditions with the tests.
122 waitUntilHandlersIdle();
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800123 }
124
125 @After
126 public void tearDown() throws Exception {
Wale Ogunwale34247952017-02-19 11:57:53 -0800127 final LinkedList<WindowState> nonCommonWindows = new LinkedList();
Wale Ogunwale027f4752017-05-12 10:37:16 -0700128
129 synchronized (sWm.mWindowMap) {
130 sWm.mRoot.forAllWindows(w -> {
131 if (!mCommonWindows.contains(w)) {
132 nonCommonWindows.addLast(w);
133 }
134 }, true /* traverseTopToBottom */);
135
136 while (!nonCommonWindows.isEmpty()) {
137 nonCommonWindows.pollLast().removeImmediately();
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800138 }
Wale Ogunwale34247952017-02-19 11:57:53 -0800139
Wale Ogunwale027f4752017-05-12 10:37:16 -0700140 mDisplayContent.removeImmediately();
141 sWm.mInputMethodTarget = null;
Wale Ogunwale34247952017-02-19 11:57:53 -0800142 }
Jorim Jaggi153badb2017-05-24 13:18:28 +0200143
144 // Wait until everything is really cleaned up.
145 waitUntilHandlersIdle();
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800146 }
147
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700148 private WindowState createCommonWindow(WindowState parent, int type, String name) {
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800149 final WindowState win = createWindow(parent, type, name);
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700150 mCommonWindows.add(win);
Wale Ogunwale34247952017-02-19 11:57:53 -0800151 // Prevent common windows from been IMe targets
152 win.mAttrs.flags |= FLAG_NOT_FOCUSABLE;
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800153 return win;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800154 }
155
156 /** Asserts that the first entry is greater than the second entry. */
157 void assertGreaterThan(int first, int second) throws Exception {
158 Assert.assertTrue("Excepted " + first + " to be greater than " + second, first > second);
159 }
160
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100161 /**
162 * Waits until the main handler for WM has processed all messages.
163 */
Jorim Jaggied7993b2017-03-28 18:50:01 +0100164 void waitUntilHandlersIdle() {
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100165 sWm.mH.runWithScissors(() -> { }, 0);
Jorim Jaggied7993b2017-03-28 18:50:01 +0100166 sWm.mAnimationHandler.runWithScissors(() -> { }, 0);
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100167 }
168
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700169 private WindowToken createWindowToken(DisplayContent dc, int stackId, int type) {
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800170 if (type < FIRST_APPLICATION_WINDOW || type > LAST_APPLICATION_WINDOW) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700171 return new WindowTestUtils.TestWindowToken(type, dc);
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800172 }
173
Winson Chung83471632016-12-13 11:02:12 -0800174 final TaskStack stack = stackId == INVALID_STACK_ID
175 ? createTaskStackOnDisplay(dc)
176 : createStackControllerOnStackOnDisplay(stackId, dc).mContainer;
Andrii Kuliand2765632016-12-12 22:26:34 -0800177 final Task task = createTaskInStack(stack, 0 /* userId */);
Bryce Leeaf691c02017-03-20 14:20:22 -0700178 final WindowTestUtils.TestAppWindowToken token = new WindowTestUtils.TestAppWindowToken(dc);
Wale Ogunwale72919d22016-12-08 18:58:50 -0800179 task.addChild(token, 0);
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800180 return token;
181 }
182
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700183 WindowState createWindow(WindowState parent, int type, String name) {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -0800184 return (parent == null)
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700185 ? createWindow(parent, type, mDisplayContent, name)
Wale Ogunwale805d9ec2016-12-07 12:22:08 -0800186 : createWindow(parent, type, parent.mToken, name);
187 }
188
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700189 WindowState createWindowOnStack(WindowState parent, int stackId, int type,
Winson Chung83471632016-12-13 11:02:12 -0800190 DisplayContent dc, String name) {
191 final WindowToken token = createWindowToken(dc, stackId, type);
192 return createWindow(parent, type, token, name);
193 }
194
Jorim Jaggi02886a82016-12-06 09:10:06 -0800195 WindowState createAppWindow(Task task, int type, String name) {
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700196 final AppWindowToken token = new WindowTestUtils.TestAppWindowToken(mDisplayContent);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800197 task.addChild(token, 0);
198 return createWindow(null, type, token, name);
199 }
200
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700201 WindowState createWindow(WindowState parent, int type, DisplayContent dc, String name) {
Winson Chung83471632016-12-13 11:02:12 -0800202 final WindowToken token = createWindowToken(dc, INVALID_STACK_ID, type);
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800203 return createWindow(parent, type, token, name);
204 }
205
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700206 WindowState createWindow(WindowState parent, int type, DisplayContent dc, String name,
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800207 boolean ownerCanAddInternalSystemWindow) {
Winson Chung83471632016-12-13 11:02:12 -0800208 final WindowToken token = createWindowToken(dc, INVALID_STACK_ID, type);
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800209 return createWindow(parent, type, token, name, ownerCanAddInternalSystemWindow);
210 }
211
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100212 static WindowState createWindow(WindowState parent, int type, WindowToken token, String name) {
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800213 return createWindow(parent, type, token, name, false /* ownerCanAddInternalSystemWindow */);
214 }
215
216 static WindowState createWindow(WindowState parent, int type, WindowToken token, String name,
217 boolean ownerCanAddInternalSystemWindow) {
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800218 final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(type);
219 attrs.setTitle(name);
220
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -0800221 final WindowState w = new WindowState(sWm, sMockSession, sIWindow, token, parent, OP_NONE,
David Stevens46939562017-03-24 13:04:00 -0700222 0, attrs, VISIBLE, 0, ownerCanAddInternalSystemWindow);
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800223 // TODO: Probably better to make this call in the WindowState ctor to avoid errors with
224 // adding it to the token...
225 token.addWindow(w);
226 return w;
227 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800228
Andrii Kuliand2765632016-12-12 22:26:34 -0800229 /** Creates a {@link TaskStack} and adds it to the specified {@link DisplayContent}. */
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700230 TaskStack createTaskStackOnDisplay(DisplayContent dc) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800231 return createStackControllerOnDisplay(dc).mContainer;
232 }
233
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700234 StackWindowController createStackControllerOnDisplay(DisplayContent dc) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800235 final int stackId = ++sNextStackId;
Winson Chung83471632016-12-13 11:02:12 -0800236 return createStackControllerOnStackOnDisplay(stackId, dc);
237 }
238
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700239 StackWindowController createStackControllerOnStackOnDisplay(int stackId,
Winson Chung83471632016-12-13 11:02:12 -0800240 DisplayContent dc) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800241 return new StackWindowController(stackId, null, dc.getDisplayId(),
Wale Ogunwale687b4272017-07-27 02:56:23 -0700242 true /* onTop */, new Rect(), new Configuration(), sWm);
Andrii Kuliand2765632016-12-12 22:26:34 -0800243 }
244
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800245 /** Creates a {@link Task} and adds it to the specified {@link TaskStack}. */
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700246 Task createTaskInStack(TaskStack stack, int userId) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700247 return WindowTestUtils.createTaskInStack(sWm, stack, userId);
Andrii Kuliand2765632016-12-12 22:26:34 -0800248 }
249
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800250 /** Creates a {@link DisplayContent} and adds it to the system. */
251 DisplayContent createNewDisplay() {
252 final int displayId = sNextDisplayId++;
253 final Display display = new Display(DisplayManagerGlobal.getInstance(), displayId,
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700254 mDisplayInfo, DEFAULT_DISPLAY_ADJUSTMENTS);
255 return new DisplayContent(display, sWm, mLayersController, new WallpaperController(sWm));
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800256 }
257
Bryce Leeaf691c02017-03-20 14:20:22 -0700258 /** Creates a {@link com.android.server.wm.WindowTestUtils.TestWindowState} */
259 WindowTestUtils.TestWindowState createWindowState(WindowManager.LayoutParams attrs,
260 WindowToken token) {
261 return new WindowTestUtils.TestWindowState(sWm, sMockSession, sIWindow, attrs, token);
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800262 }
263
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800264}