blob: 32eee84200bae0ca0cf12ba565fa6ed494822d0d [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 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;
Wale Ogunwale11cc5162017-04-25 20:29:13 -070064 private static final IWindow sIWindow = new TestIWindow();
65 private static final Session sMockSession = mock(Session.class);
Bryce Lee310de9e2017-03-15 10:18:21 -070066 // The default display is removed in {@link #setUp} and then we iterate over all displays to
67 // 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 -070068 // added display should be treated as default. This cannot be the default display
Wale Ogunwale11cc5162017-04-25 20:29:13 -070069 private static int sNextDisplayId = DEFAULT_DISPLAY + 1;
Bryce Leeaf691c02017-03-20 14:20:22 -070070 private static int sNextStackId = FIRST_DYNAMIC_STACK_ID;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080071
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080072 private static boolean sOneTimeSetupDone = false;
Wale Ogunwale11cc5162017-04-25 20:29:13 -070073 DisplayContent mDisplayContent;
74 DisplayInfo mDisplayInfo = new DisplayInfo();
75 WindowLayersController mLayersController;
76 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
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080087 @Before
88 public void setUp() throws Exception {
Wale Ogunwale11cc5162017-04-25 20:29:13 -070089 if (!sOneTimeSetupDone) {
90 sOneTimeSetupDone = true;
91 MockitoAnnotations.initMocks(this);
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080092 }
Wale Ogunwale11cc5162017-04-25 20:29:13 -070093
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080094 final Context context = InstrumentationRegistry.getTargetContext();
Jorim Jaggi9bafc712017-01-19 17:28:30 +010095 AttributeCache.init(context);
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080096 sWm = TestWindowManagerPolicy.getWindowManagerService(context);
Wale Ogunwale11cc5162017-04-25 20:29:13 -070097 mLayersController = new WindowLayersController(sWm);
Bryce Lee6272c7f2017-03-17 17:22:35 -070098
Wale Ogunwale11cc5162017-04-25 20:29:13 -070099 context.getDisplay().getDisplayInfo(mDisplayInfo);
100 mDisplayContent = createNewDisplay();
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.
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700105 mCommonWindows = new HashSet();
106 mWallpaperWindow = createCommonWindow(null, TYPE_WALLPAPER, "wallpaperWindow");
107 mImeWindow = createCommonWindow(null, TYPE_INPUT_METHOD, "mImeWindow");
108 sWm.mInputMethodWindow = mImeWindow;
109 mImeDialogWindow = createCommonWindow(null, TYPE_INPUT_METHOD_DIALOG, "mImeDialogWindow");
110 mStatusBarWindow = createCommonWindow(null, TYPE_STATUS_BAR, "mStatusBarWindow");
111 mNavBarWindow = createCommonWindow(null, TYPE_NAVIGATION_BAR, "mNavBarWindow");
112 mDockedDividerWindow = createCommonWindow(null, TYPE_DOCK_DIVIDER, "mDockedDividerWindow");
113 mAppWindow = createCommonWindow(null, TYPE_BASE_APPLICATION, "mAppWindow");
114 mChildAppWindowAbove = createCommonWindow(mAppWindow, TYPE_APPLICATION_ATTACHED_DIALOG,
115 "mChildAppWindowAbove");
116 mChildAppWindowBelow = createCommonWindow(mAppWindow, TYPE_APPLICATION_MEDIA_OVERLAY,
117 "mChildAppWindowBelow");
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800118 }
119
120 @After
121 public void tearDown() throws Exception {
Wale Ogunwale34247952017-02-19 11:57:53 -0800122 final LinkedList<WindowState> nonCommonWindows = new LinkedList();
Wale Ogunwale027f4752017-05-12 10:37:16 -0700123
124 synchronized (sWm.mWindowMap) {
125 sWm.mRoot.forAllWindows(w -> {
126 if (!mCommonWindows.contains(w)) {
127 nonCommonWindows.addLast(w);
128 }
129 }, true /* traverseTopToBottom */);
130
131 while (!nonCommonWindows.isEmpty()) {
132 nonCommonWindows.pollLast().removeImmediately();
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800133 }
Wale Ogunwale34247952017-02-19 11:57:53 -0800134
Wale Ogunwale027f4752017-05-12 10:37:16 -0700135 mDisplayContent.removeImmediately();
136 sWm.mInputMethodTarget = null;
Wale Ogunwale34247952017-02-19 11:57:53 -0800137 }
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800138 }
139
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700140 private WindowState createCommonWindow(WindowState parent, int type, String name) {
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800141 final WindowState win = createWindow(parent, type, name);
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700142 mCommonWindows.add(win);
Wale Ogunwale34247952017-02-19 11:57:53 -0800143 // Prevent common windows from been IMe targets
144 win.mAttrs.flags |= FLAG_NOT_FOCUSABLE;
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800145 return win;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800146 }
147
148 /** Asserts that the first entry is greater than the second entry. */
149 void assertGreaterThan(int first, int second) throws Exception {
150 Assert.assertTrue("Excepted " + first + " to be greater than " + second, first > second);
151 }
152
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100153 /**
154 * Waits until the main handler for WM has processed all messages.
155 */
Jorim Jaggied7993b2017-03-28 18:50:01 +0100156 void waitUntilHandlersIdle() {
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100157 sWm.mH.runWithScissors(() -> { }, 0);
Jorim Jaggied7993b2017-03-28 18:50:01 +0100158 sWm.mAnimationHandler.runWithScissors(() -> { }, 0);
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100159 }
160
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700161 private WindowToken createWindowToken(DisplayContent dc, int stackId, int type) {
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800162 if (type < FIRST_APPLICATION_WINDOW || type > LAST_APPLICATION_WINDOW) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700163 return new WindowTestUtils.TestWindowToken(type, dc);
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800164 }
165
Winson Chung83471632016-12-13 11:02:12 -0800166 final TaskStack stack = stackId == INVALID_STACK_ID
167 ? createTaskStackOnDisplay(dc)
168 : createStackControllerOnStackOnDisplay(stackId, dc).mContainer;
Andrii Kuliand2765632016-12-12 22:26:34 -0800169 final Task task = createTaskInStack(stack, 0 /* userId */);
Bryce Leeaf691c02017-03-20 14:20:22 -0700170 final WindowTestUtils.TestAppWindowToken token = new WindowTestUtils.TestAppWindowToken(dc);
Wale Ogunwale72919d22016-12-08 18:58:50 -0800171 task.addChild(token, 0);
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800172 return token;
173 }
174
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700175 WindowState createWindow(WindowState parent, int type, String name) {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -0800176 return (parent == null)
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700177 ? createWindow(parent, type, mDisplayContent, name)
Wale Ogunwale805d9ec2016-12-07 12:22:08 -0800178 : createWindow(parent, type, parent.mToken, name);
179 }
180
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700181 WindowState createWindowOnStack(WindowState parent, int stackId, int type,
Winson Chung83471632016-12-13 11:02:12 -0800182 DisplayContent dc, String name) {
183 final WindowToken token = createWindowToken(dc, stackId, type);
184 return createWindow(parent, type, token, name);
185 }
186
Jorim Jaggi02886a82016-12-06 09:10:06 -0800187 WindowState createAppWindow(Task task, int type, String name) {
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700188 final AppWindowToken token = new WindowTestUtils.TestAppWindowToken(mDisplayContent);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800189 task.addChild(token, 0);
190 return createWindow(null, type, token, name);
191 }
192
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700193 WindowState createWindow(WindowState parent, int type, DisplayContent dc, String name) {
Winson Chung83471632016-12-13 11:02:12 -0800194 final WindowToken token = createWindowToken(dc, INVALID_STACK_ID, type);
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800195 return createWindow(parent, type, token, name);
196 }
197
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700198 WindowState createWindow(WindowState parent, int type, DisplayContent dc, String name,
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800199 boolean ownerCanAddInternalSystemWindow) {
Winson Chung83471632016-12-13 11:02:12 -0800200 final WindowToken token = createWindowToken(dc, INVALID_STACK_ID, type);
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800201 return createWindow(parent, type, token, name, ownerCanAddInternalSystemWindow);
202 }
203
Jorim Jaggi9bafc712017-01-19 17:28:30 +0100204 static WindowState createWindow(WindowState parent, int type, WindowToken token, String name) {
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800205 return createWindow(parent, type, token, name, false /* ownerCanAddInternalSystemWindow */);
206 }
207
208 static WindowState createWindow(WindowState parent, int type, WindowToken token, String name,
209 boolean ownerCanAddInternalSystemWindow) {
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800210 final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(type);
211 attrs.setTitle(name);
212
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -0800213 final WindowState w = new WindowState(sWm, sMockSession, sIWindow, token, parent, OP_NONE,
David Stevens46939562017-03-24 13:04:00 -0700214 0, attrs, VISIBLE, 0, ownerCanAddInternalSystemWindow);
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800215 // TODO: Probably better to make this call in the WindowState ctor to avoid errors with
216 // adding it to the token...
217 token.addWindow(w);
218 return w;
219 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800220
Andrii Kuliand2765632016-12-12 22:26:34 -0800221 /** Creates a {@link TaskStack} and adds it to the specified {@link DisplayContent}. */
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700222 TaskStack createTaskStackOnDisplay(DisplayContent dc) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800223 return createStackControllerOnDisplay(dc).mContainer;
224 }
225
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700226 StackWindowController createStackControllerOnDisplay(DisplayContent dc) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800227 final int stackId = ++sNextStackId;
Winson Chung83471632016-12-13 11:02:12 -0800228 return createStackControllerOnStackOnDisplay(stackId, dc);
229 }
230
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700231 StackWindowController createStackControllerOnStackOnDisplay(int stackId,
Winson Chung83471632016-12-13 11:02:12 -0800232 DisplayContent dc) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800233 return new StackWindowController(stackId, null, dc.getDisplayId(),
234 true /* onTop */, new Rect(), sWm);
Andrii Kuliand2765632016-12-12 22:26:34 -0800235 }
236
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800237 /** Creates a {@link Task} and adds it to the specified {@link TaskStack}. */
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700238 Task createTaskInStack(TaskStack stack, int userId) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700239 return WindowTestUtils.createTaskInStack(sWm, stack, userId);
Andrii Kuliand2765632016-12-12 22:26:34 -0800240 }
241
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800242 /** Creates a {@link DisplayContent} and adds it to the system. */
243 DisplayContent createNewDisplay() {
244 final int displayId = sNextDisplayId++;
245 final Display display = new Display(DisplayManagerGlobal.getInstance(), displayId,
Wale Ogunwale11cc5162017-04-25 20:29:13 -0700246 mDisplayInfo, DEFAULT_DISPLAY_ADJUSTMENTS);
247 return new DisplayContent(display, sWm, mLayersController, new WallpaperController(sWm));
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800248 }
249
Bryce Leeaf691c02017-03-20 14:20:22 -0700250 /** Creates a {@link com.android.server.wm.WindowTestUtils.TestWindowState} */
251 WindowTestUtils.TestWindowState createWindowState(WindowManager.LayoutParams attrs,
252 WindowToken token) {
253 return new WindowTestUtils.TestWindowState(sWm, sMockSession, sIWindow, attrs, token);
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800254 }
255
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800256}