blob: fa4b79ffda6f7272a53c683a9a8b92f94115a47c [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
19import org.junit.Assert;
20import org.junit.Before;
21
22import android.content.Context;
23import android.os.IBinder;
24import android.support.test.InstrumentationRegistry;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080025import android.view.IWindow;
26import android.view.WindowManager;
27
28import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID;
29import static android.app.AppOpsManager.OP_NONE;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080030import static android.content.pm.ActivityInfo.RESIZE_MODE_UNRESIZEABLE;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080031import static android.content.res.Configuration.EMPTY;
32import static android.view.WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW;
33import static android.view.WindowManager.LayoutParams.LAST_APPLICATION_WINDOW;
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080034import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
35import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA_OVERLAY;
36import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
37import static android.view.WindowManager.LayoutParams.TYPE_DOCK_DIVIDER;
38import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
39import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
40import static android.view.WindowManager.LayoutParams.TYPE_NAVIGATION_BAR;
41import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
42import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080043import static com.android.server.wm.WindowContainer.POSITION_TOP;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080044import static org.mockito.Mockito.mock;
45
46/**
47 * Common base class for window manager unit test classes.
48 */
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080049class WindowTestsBase {
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080050 static WindowManagerService sWm = null;
51 private final IWindow mIWindow = new TestIWindow();
52 private final Session mMockSession = mock(Session.class);
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080053 private static int sNextStackId = FIRST_DYNAMIC_STACK_ID;
54 private static int sNextTaskId = 0;
55
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080056 private static boolean sOneTimeSetupDone = false;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080057 static DisplayContent sDisplayContent;
58 static WindowLayersController sLayersController;
59 static WindowState sWallpaperWindow;
60 static WindowState sImeWindow;
61 static WindowState sImeDialogWindow;
62 static WindowState sStatusBarWindow;
63 static WindowState sDockedDividerWindow;
64 static WindowState sNavBarWindow;
65 static WindowState sAppWindow;
66 static WindowState sChildAppWindowAbove;
67 static WindowState sChildAppWindowBelow;
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080068
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080069 @Before
70 public void setUp() throws Exception {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080071 if (sOneTimeSetupDone) {
72 return;
73 }
74 sOneTimeSetupDone = true;
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080075 final Context context = InstrumentationRegistry.getTargetContext();
76 sWm = TestWindowManagerPolicy.getWindowManagerService(context);
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080077 sLayersController = new WindowLayersController(sWm);
78 sDisplayContent = new DisplayContent(context.getDisplay(), sWm, sLayersController,
79 new WallpaperController(sWm));
Andrii Kuliand2765632016-12-12 22:26:34 -080080 sWm.mRoot.addChild(sDisplayContent, 0);
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080081
82 // Set-up some common windows.
83 sWallpaperWindow = createWindow(null, TYPE_WALLPAPER, sDisplayContent, "wallpaperWindow");
84 sImeWindow = createWindow(null, TYPE_INPUT_METHOD, sDisplayContent, "sImeWindow");
85 sImeDialogWindow =
86 createWindow(null, TYPE_INPUT_METHOD_DIALOG, sDisplayContent, "sImeDialogWindow");
87 sStatusBarWindow = createWindow(null, TYPE_STATUS_BAR, sDisplayContent, "sStatusBarWindow");
88 sNavBarWindow =
Wale Ogunwale6ce0fb82016-12-13 14:24:00 -080089 createWindow(null, TYPE_NAVIGATION_BAR, sDisplayContent, "sNavBarWindow");
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080090 sDockedDividerWindow =
Wale Ogunwale6ce0fb82016-12-13 14:24:00 -080091 createWindow(null, TYPE_DOCK_DIVIDER, sDisplayContent, "sDockedDividerWindow");
Wale Ogunwale3c1170d2016-12-02 14:44:52 -080092 sAppWindow = createWindow(null, TYPE_BASE_APPLICATION, sDisplayContent, "sAppWindow");
93 sChildAppWindowAbove = createWindow(sAppWindow,
94 TYPE_APPLICATION_ATTACHED_DIALOG, sAppWindow.mToken, "sChildAppWindowAbove");
95 sChildAppWindowBelow = createWindow(sAppWindow,
96 TYPE_APPLICATION_MEDIA_OVERLAY, sAppWindow.mToken, "sChildAppWindowBelow");
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080097 }
98
99 /** Asserts that the first entry is greater than the second entry. */
100 void assertGreaterThan(int first, int second) throws Exception {
101 Assert.assertTrue("Excepted " + first + " to be greater than " + second, first > second);
102 }
103
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800104 private WindowToken createWindowToken(DisplayContent dc, int type) {
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800105 if (type < FIRST_APPLICATION_WINDOW || type > LAST_APPLICATION_WINDOW) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800106 return new TestWindowToken(type, dc);
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800107 }
108
Andrii Kuliand2765632016-12-12 22:26:34 -0800109 final TaskStack stack = createTaskStackOnDisplay(dc);
110 final Task task = createTaskInStack(stack, 0 /* userId */);
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800111 final TestAppWindowToken token = new TestAppWindowToken(dc);
Wale Ogunwale72919d22016-12-08 18:58:50 -0800112 task.addChild(token, 0);
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800113 return token;
114 }
115
Wale Ogunwale805d9ec2016-12-07 12:22:08 -0800116 WindowState createWindow(WindowState parent, int type, String name) {
117 return (parent == null)
118 ? createWindow(parent, type, sDisplayContent, name)
119 : createWindow(parent, type, parent.mToken, name);
120 }
121
Jorim Jaggi02886a82016-12-06 09:10:06 -0800122 WindowState createAppWindow(Task task, int type, String name) {
123 final AppWindowToken token = new TestAppWindowToken(sDisplayContent);
124 task.addChild(token, 0);
125 return createWindow(null, type, token, name);
126 }
127
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800128 WindowState createWindow(WindowState parent, int type, DisplayContent dc, String name) {
129 final WindowToken token = createWindowToken(dc, type);
130 return createWindow(parent, type, token, name);
131 }
132
133 WindowState createWindow(WindowState parent, int type, WindowToken token, String name) {
134 final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(type);
135 attrs.setTitle(name);
136
137 final WindowState w = new WindowState(sWm, mMockSession, mIWindow, token, parent, OP_NONE,
138 0, attrs, 0, 0);
139 // TODO: Probably better to make this call in the WindowState ctor to avoid errors with
140 // adding it to the token...
141 token.addWindow(w);
142 return w;
143 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800144
Andrii Kuliand2765632016-12-12 22:26:34 -0800145 /** Creates a {@link TaskStack} and adds it to the specified {@link DisplayContent}. */
146 TaskStack createTaskStackOnDisplay(DisplayContent dc) {
147 final int stackId = sNextStackId++;
148 dc.addStackToDisplay(stackId, true);
149 return sWm.mStackIdToStack.get(stackId);
150 }
151
152 /**Creates a {@link Task} and adds it to the specified {@link TaskStack}. */
153 Task createTaskInStack(TaskStack stack, int userId) {
154 final Task newTask = new Task(sNextTaskId++, stack, userId, sWm, null, EMPTY, false, 0,
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800155 false, null);
156 stack.addTask(newTask, POSITION_TOP);
Andrii Kuliand2765632016-12-12 22:26:34 -0800157 return newTask;
158 }
159
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800160 /* Used so we can gain access to some protected members of the {@link WindowToken} class */
161 class TestWindowToken extends WindowToken {
162
163 TestWindowToken(int type, DisplayContent dc) {
164 this(type, dc, false /* persistOnEmpty */);
165 }
166
167 TestWindowToken(int type, DisplayContent dc, boolean persistOnEmpty) {
168 super(sWm, mock(IBinder.class), type, persistOnEmpty, dc);
169 }
170
171 int getWindowsCount() {
172 return mChildren.size();
173 }
174
175 boolean hasWindow(WindowState w) {
176 return mChildren.contains(w);
177 }
178 }
179
Andrii Kulian4ede3e02017-01-12 11:52:31 -0800180 /** Used so we can gain access to some protected members of the {@link AppWindowToken} class. */
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800181 class TestAppWindowToken extends AppWindowToken {
182
183 TestAppWindowToken(DisplayContent dc) {
184 super(sWm, null, false, dc);
185 }
186
187 int getWindowsCount() {
188 return mChildren.size();
189 }
190
191 boolean hasWindow(WindowState w) {
192 return mChildren.contains(w);
193 }
194
195 WindowState getFirstChild() {
196 return mChildren.getFirst();
197 }
198
199 WindowState getLastChild() {
200 return mChildren.getLast();
201 }
202 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800203
204 /**
205 * Used so we can gain access to some protected members of {@link TaskWindowContainerController}
206 * class.
207 */
208 class TestTaskWindowContainerController extends TaskWindowContainerController {
209
210 TestTaskWindowContainerController(int stackId) {
211 super(sNextTaskId++, stackId, 0 /* userId */, null /* bounds */,
212 EMPTY /* overrideConfig*/, RESIZE_MODE_UNRESIZEABLE, false /* homeTask*/,
213 false /* isOnTopLauncher */, true /* toTop*/, true /* showForAllUsers */);
214 }
215 }
Andrii Kulian4ede3e02017-01-12 11:52:31 -0800216
217 /** Used to track resize reports. */
218 class TestWindowState extends WindowState {
219 boolean resizeReported;
220
221 TestWindowState(WindowManager.LayoutParams attrs, WindowToken token) {
222 super(sWm, mMockSession, mIWindow, token, null, OP_NONE, 0, attrs, 0, 0);
223 }
224
225 @Override
226 void reportResized() {
227 super.reportResized();
228 resizeReported = true;
229 }
230
231 @Override
232 public boolean isGoneForLayoutLw() {
233 return false;
234 }
235
236 @Override
237 void updateResizingWindowIfNeeded() {
238 // Used in AppWindowTokenTests#testLandscapeSeascapeRotationRelayout to deceive
239 // the system that it can actually update the window.
240 boolean hadSurface = mHasSurface;
241 mHasSurface = true;
242
243 super.updateResizingWindowIfNeeded();
244
245 mHasSurface = hadSurface;
246 }
247 }
Wale Ogunwale44fbdf52016-11-16 10:18:45 -0800248}