blob: eaf4ac4baf4f39b5974a994b64a6a9905b47b661 [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
David Stevens46939562017-03-24 13:04:00 -070019import static android.view.View.VISIBLE;
20
Jorim Jaggi829b9cd2017-01-23 16:20:53 +010021import android.app.ActivityManager.TaskDescription;
Wale Ogunwalec5cc3012017-01-13 13:26:16 -080022import android.graphics.Rect;
Andrii Kulian367ff7f2017-01-25 19:45:34 -080023import android.hardware.display.DisplayManagerGlobal;
Andrii Kulian367ff7f2017-01-25 19:45:34 -080024import android.view.Display;
25import android.view.DisplayInfo;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080026import org.junit.Assert;
Wale Ogunwale17f175c2017-02-07 16:54:10 -080027import org.junit.After;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080028import org.junit.Before;
Jorim Jaggi9bafc712017-01-19 17:28:30 +010029import org.mockito.MockitoAnnotations;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080030
31import android.content.Context;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080032import android.support.test.InstrumentationRegistry;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080033import android.view.IWindow;
34import android.view.WindowManager;
35
36import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
Winson Chung83471632016-12-13 11:02:12 -080037import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080038import static android.app.AppOpsManager.OP_NONE;
Andrii Kulian367ff7f2017-01-25 19:45:34 -080039import static android.view.DisplayAdjustments.DEFAULT_DISPLAY_ADJUSTMENTS;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080040import static android.view.WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW;
Wale Ogunwale34247952017-02-19 11:57:53 -080041import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080042import static android.view.WindowManager.LayoutParams.LAST_APPLICATION_WINDOW;
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080043import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
44import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA_OVERLAY;
45import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
46import static android.view.WindowManager.LayoutParams.TYPE_DOCK_DIVIDER;
47import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
48import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
49import static android.view.WindowManager.LayoutParams.TYPE_NAVIGATION_BAR;
50import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
51import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080052import static org.mockito.Mockito.mock;
53
Jorim Jaggi9bafc712017-01-19 17:28:30 +010054import com.android.server.AttributeCache;
Wale Ogunwale17f175c2017-02-07 16:54:10 -080055
56import java.util.HashSet;
Wale Ogunwale34247952017-02-19 11:57:53 -080057import java.util.LinkedList;
Jorim Jaggi9bafc712017-01-19 17:28:30 +010058
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;
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -080065 private final static IWindow sIWindow = new TestIWindow();
Jorim Jaggi9bafc712017-01-19 17:28:30 +010066 private final static 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
70 private static int sNextDisplayId = Display.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 Ogunwale26c0dfe2016-12-14 14:42:30 -080074 static DisplayContent sDisplayContent;
Andrii Kulian367ff7f2017-01-25 19:45:34 -080075 static DisplayInfo sDisplayInfo = new DisplayInfo();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080076 static WindowLayersController sLayersController;
77 static WindowState sWallpaperWindow;
78 static WindowState sImeWindow;
79 static WindowState sImeDialogWindow;
80 static WindowState sStatusBarWindow;
81 static WindowState sDockedDividerWindow;
82 static WindowState sNavBarWindow;
83 static WindowState sAppWindow;
84 static WindowState sChildAppWindowAbove;
85 static WindowState sChildAppWindowBelow;
Wale Ogunwale17f175c2017-02-07 16:54:10 -080086 static HashSet<WindowState> sCommonWindows;
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080087
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080088 @Before
89 public void setUp() throws Exception {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080090 if (sOneTimeSetupDone) {
91 return;
92 }
93 sOneTimeSetupDone = true;
Jorim Jaggi9bafc712017-01-19 17:28:30 +010094 MockitoAnnotations.initMocks(this);
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);
Jorim Jaggi9bafc712017-01-19 17:28:30 +010098 sPolicy = (TestWindowManagerPolicy) sWm.mPolicy;
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080099 sLayersController = new WindowLayersController(sWm);
Bryce Lee6272c7f2017-03-17 17:22:35 -0700100
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800101 // Make sure that display ids don't overlap, so there won't be several displays with same
102 // ids among RootWindowContainer children.
103 for (DisplayContent dc : sWm.mRoot.mChildren) {
104 if (dc.getDisplayId() >= sNextDisplayId) {
105 sNextDisplayId = dc.getDisplayId() + 1;
106 }
Bryce Lee6272c7f2017-03-17 17:22:35 -0700107
108 // The default display must be preserved as some tests require it to function
109 // (such as policy rotation).
110 if (dc.getDisplayId() != Display.DEFAULT_DISPLAY) {
111 // It is safe to remove these displays as new displays will always be created with
112 // new ids.
113 dc.removeImmediately();
114 }
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800115 }
Bryce Lee6272c7f2017-03-17 17:22:35 -0700116
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800117 context.getDisplay().getDisplayInfo(sDisplayInfo);
118 sDisplayContent = createNewDisplay();
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100119 sWm.mDisplayEnabled = true;
120 sWm.mDisplayReady = true;
Wale Ogunwale3c1170d2016-12-02 14:44:52 -0800121
122 // Set-up some common windows.
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800123 sCommonWindows = new HashSet();
124 sWallpaperWindow = createCommonWindow(null, TYPE_WALLPAPER, "wallpaperWindow");
125 sImeWindow = createCommonWindow(null, TYPE_INPUT_METHOD, "sImeWindow");
Wale Ogunwale34247952017-02-19 11:57:53 -0800126 sWm.mInputMethodWindow = sImeWindow;
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800127 sImeDialogWindow = createCommonWindow(null, TYPE_INPUT_METHOD_DIALOG, "sImeDialogWindow");
128 sStatusBarWindow = createCommonWindow(null, TYPE_STATUS_BAR, "sStatusBarWindow");
129 sNavBarWindow = createCommonWindow(null, TYPE_NAVIGATION_BAR, "sNavBarWindow");
130 sDockedDividerWindow = createCommonWindow(null, TYPE_DOCK_DIVIDER, "sDockedDividerWindow");
131 sAppWindow = createCommonWindow(null, TYPE_BASE_APPLICATION, "sAppWindow");
132 sChildAppWindowAbove = createCommonWindow(sAppWindow, TYPE_APPLICATION_ATTACHED_DIALOG,
133 "sChildAppWindowAbove");
134 sChildAppWindowBelow = createCommonWindow(sAppWindow, TYPE_APPLICATION_MEDIA_OVERLAY,
135 "sChildAppWindowBelow");
136 }
137
138 @After
139 public void tearDown() throws Exception {
Wale Ogunwale34247952017-02-19 11:57:53 -0800140 final LinkedList<WindowState> nonCommonWindows = new LinkedList();
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800141 sWm.mRoot.forAllWindows(w -> {
142 if (!sCommonWindows.contains(w)) {
Wale Ogunwale34247952017-02-19 11:57:53 -0800143 nonCommonWindows.addLast(w);
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800144 }
145 }, true /* traverseTopToBottom */);
Wale Ogunwale34247952017-02-19 11:57:53 -0800146
147 while (!nonCommonWindows.isEmpty()) {
148 nonCommonWindows.pollLast().removeImmediately();
149 }
150
151 sWm.mInputMethodTarget = null;
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800152 }
153
154 private static WindowState createCommonWindow(WindowState parent, int type, String name) {
155 final WindowState win = createWindow(parent, type, name);
156 sCommonWindows.add(win);
Wale Ogunwale34247952017-02-19 11:57:53 -0800157 // Prevent common windows from been IMe targets
158 win.mAttrs.flags |= FLAG_NOT_FOCUSABLE;
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800159 return win;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800160 }
161
162 /** Asserts that the first entry is greater than the second entry. */
163 void assertGreaterThan(int first, int second) throws Exception {
164 Assert.assertTrue("Excepted " + first + " to be greater than " + second, first > second);
165 }
166
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100167 /**
168 * Waits until the main handler for WM has processed all messages.
169 */
170 void waitUntilHandlerIdle() {
171 sWm.mH.runWithScissors(() -> { }, 0);
172 }
173
Winson Chung83471632016-12-13 11:02:12 -0800174 private static WindowToken createWindowToken(DisplayContent dc, int stackId, int type) {
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800175 if (type < FIRST_APPLICATION_WINDOW || type > LAST_APPLICATION_WINDOW) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700176 return new WindowTestUtils.TestWindowToken(type, dc);
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800177 }
178
Winson Chung83471632016-12-13 11:02:12 -0800179 final TaskStack stack = stackId == INVALID_STACK_ID
180 ? createTaskStackOnDisplay(dc)
181 : createStackControllerOnStackOnDisplay(stackId, dc).mContainer;
Andrii Kuliand2765632016-12-12 22:26:34 -0800182 final Task task = createTaskInStack(stack, 0 /* userId */);
Bryce Leeaf691c02017-03-20 14:20:22 -0700183 final WindowTestUtils.TestAppWindowToken token = new WindowTestUtils.TestAppWindowToken(dc);
Wale Ogunwale72919d22016-12-08 18:58:50 -0800184 task.addChild(token, 0);
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800185 return token;
186 }
187
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100188 static WindowState createWindow(WindowState parent, int type, String name) {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -0800189 return (parent == null)
190 ? createWindow(parent, type, sDisplayContent, name)
191 : createWindow(parent, type, parent.mToken, name);
192 }
193
Winson Chung83471632016-12-13 11:02:12 -0800194 static WindowState createWindowOnStack(WindowState parent, int stackId, int type,
195 DisplayContent dc, String name) {
196 final WindowToken token = createWindowToken(dc, stackId, type);
197 return createWindow(parent, type, token, name);
198 }
199
Jorim Jaggi02886a82016-12-06 09:10:06 -0800200 WindowState createAppWindow(Task task, int type, String name) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700201 final AppWindowToken token = new WindowTestUtils.TestAppWindowToken(sDisplayContent);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800202 task.addChild(token, 0);
203 return createWindow(null, type, token, name);
204 }
205
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100206 static WindowState createWindow(WindowState parent, int type, DisplayContent dc, String name) {
Winson Chung83471632016-12-13 11:02:12 -0800207 final WindowToken token = createWindowToken(dc, INVALID_STACK_ID, type);
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800208 return createWindow(parent, type, token, name);
209 }
210
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800211 static WindowState createWindow(WindowState parent, int type, DisplayContent dc, String name,
212 boolean ownerCanAddInternalSystemWindow) {
Winson Chung83471632016-12-13 11:02:12 -0800213 final WindowToken token = createWindowToken(dc, INVALID_STACK_ID, type);
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800214 return createWindow(parent, type, token, name, ownerCanAddInternalSystemWindow);
215 }
216
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100217 static WindowState createWindow(WindowState parent, int type, WindowToken token, String name) {
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800218 return createWindow(parent, type, token, name, false /* ownerCanAddInternalSystemWindow */);
219 }
220
221 static WindowState createWindow(WindowState parent, int type, WindowToken token, String name,
222 boolean ownerCanAddInternalSystemWindow) {
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800223 final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(type);
224 attrs.setTitle(name);
225
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -0800226 final WindowState w = new WindowState(sWm, sMockSession, sIWindow, token, parent, OP_NONE,
David Stevens46939562017-03-24 13:04:00 -0700227 0, attrs, VISIBLE, 0, ownerCanAddInternalSystemWindow);
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800228 // TODO: Probably better to make this call in the WindowState ctor to avoid errors with
229 // adding it to the token...
230 token.addWindow(w);
231 return w;
232 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800233
Andrii Kuliand2765632016-12-12 22:26:34 -0800234 /** Creates a {@link TaskStack} and adds it to the specified {@link DisplayContent}. */
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100235 static TaskStack createTaskStackOnDisplay(DisplayContent dc) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800236 return createStackControllerOnDisplay(dc).mContainer;
237 }
238
239 static StackWindowController createStackControllerOnDisplay(DisplayContent dc) {
240 final int stackId = ++sNextStackId;
Winson Chung83471632016-12-13 11:02:12 -0800241 return createStackControllerOnStackOnDisplay(stackId, dc);
242 }
243
244 static StackWindowController createStackControllerOnStackOnDisplay(int stackId,
245 DisplayContent dc) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800246 return new StackWindowController(stackId, null, dc.getDisplayId(),
247 true /* onTop */, new Rect(), sWm);
Andrii Kuliand2765632016-12-12 22:26:34 -0800248 }
249
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800250 /** Creates a {@link Task} and adds it to the specified {@link TaskStack}. */
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100251 static Task createTaskInStack(TaskStack stack, int userId) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700252 return WindowTestUtils.createTaskInStack(sWm, stack, userId);
Andrii Kuliand2765632016-12-12 22:26:34 -0800253 }
254
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800255 /** Creates a {@link DisplayContent} and adds it to the system. */
256 DisplayContent createNewDisplay() {
257 final int displayId = sNextDisplayId++;
258 final Display display = new Display(DisplayManagerGlobal.getInstance(), displayId,
259 sDisplayInfo, DEFAULT_DISPLAY_ADJUSTMENTS);
260 return new DisplayContent(display, sWm, sLayersController, new WallpaperController(sWm));
261 }
262
Bryce Leeaf691c02017-03-20 14:20:22 -0700263 /** Creates a {@link com.android.server.wm.WindowTestUtils.TestWindowState} */
264 WindowTestUtils.TestWindowState createWindowState(WindowManager.LayoutParams attrs,
265 WindowToken token) {
266 return new WindowTestUtils.TestWindowState(sWm, sMockSession, sIWindow, attrs, token);
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800267 }
268
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800269}