Wale Ogunwale | 44fbdf5 | 2016-11-16 10:18:45 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.wm; |
| 18 | |
Wale Ogunwale | c5cc301 | 2017-01-13 13:26:16 -0800 | [diff] [blame] | 19 | import android.content.res.Configuration; |
| 20 | import android.graphics.Rect; |
| 21 | import android.os.Binder; |
| 22 | import android.view.IApplicationToken; |
Wale Ogunwale | 44fbdf5 | 2016-11-16 10:18:45 -0800 | [diff] [blame] | 23 | import org.junit.Assert; |
| 24 | import org.junit.Before; |
| 25 | |
| 26 | import android.content.Context; |
| 27 | import android.os.IBinder; |
| 28 | import android.support.test.InstrumentationRegistry; |
Wale Ogunwale | 44fbdf5 | 2016-11-16 10:18:45 -0800 | [diff] [blame] | 29 | import android.view.IWindow; |
| 30 | import android.view.WindowManager; |
| 31 | |
| 32 | import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID; |
| 33 | import static android.app.AppOpsManager.OP_NONE; |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 34 | import static android.content.pm.ActivityInfo.RESIZE_MODE_UNRESIZEABLE; |
Wale Ogunwale | c5cc301 | 2017-01-13 13:26:16 -0800 | [diff] [blame] | 35 | import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED; |
Wale Ogunwale | 44fbdf5 | 2016-11-16 10:18:45 -0800 | [diff] [blame] | 36 | import static android.content.res.Configuration.EMPTY; |
| 37 | import static android.view.WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW; |
| 38 | import static android.view.WindowManager.LayoutParams.LAST_APPLICATION_WINDOW; |
Wale Ogunwale | 3c1170d | 2016-12-02 14:44:52 -0800 | [diff] [blame] | 39 | import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG; |
| 40 | import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA_OVERLAY; |
| 41 | import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION; |
| 42 | import static android.view.WindowManager.LayoutParams.TYPE_DOCK_DIVIDER; |
| 43 | import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD; |
| 44 | import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG; |
| 45 | import static android.view.WindowManager.LayoutParams.TYPE_NAVIGATION_BAR; |
| 46 | import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR; |
| 47 | import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER; |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 48 | import static com.android.server.wm.WindowContainer.POSITION_TOP; |
Wale Ogunwale | 44fbdf5 | 2016-11-16 10:18:45 -0800 | [diff] [blame] | 49 | import static org.mockito.Mockito.mock; |
| 50 | |
| 51 | /** |
| 52 | * Common base class for window manager unit test classes. |
| 53 | */ |
Wale Ogunwale | 26c0dfe | 2016-12-14 14:42:30 -0800 | [diff] [blame] | 54 | class WindowTestsBase { |
Wale Ogunwale | 44fbdf5 | 2016-11-16 10:18:45 -0800 | [diff] [blame] | 55 | static WindowManagerService sWm = null; |
| 56 | private final IWindow mIWindow = new TestIWindow(); |
| 57 | private final Session mMockSession = mock(Session.class); |
Wale Ogunwale | c5cc301 | 2017-01-13 13:26:16 -0800 | [diff] [blame] | 58 | static int sNextStackId = FIRST_DYNAMIC_STACK_ID; |
Wale Ogunwale | 44fbdf5 | 2016-11-16 10:18:45 -0800 | [diff] [blame] | 59 | private static int sNextTaskId = 0; |
| 60 | |
Wale Ogunwale | 3c1170d | 2016-12-02 14:44:52 -0800 | [diff] [blame] | 61 | private static boolean sOneTimeSetupDone = false; |
Wale Ogunwale | 26c0dfe | 2016-12-14 14:42:30 -0800 | [diff] [blame] | 62 | static DisplayContent sDisplayContent; |
| 63 | static WindowLayersController sLayersController; |
| 64 | static WindowState sWallpaperWindow; |
| 65 | static WindowState sImeWindow; |
| 66 | static WindowState sImeDialogWindow; |
| 67 | static WindowState sStatusBarWindow; |
| 68 | static WindowState sDockedDividerWindow; |
| 69 | static WindowState sNavBarWindow; |
| 70 | static WindowState sAppWindow; |
| 71 | static WindowState sChildAppWindowAbove; |
| 72 | static WindowState sChildAppWindowBelow; |
Wale Ogunwale | 3c1170d | 2016-12-02 14:44:52 -0800 | [diff] [blame] | 73 | |
Wale Ogunwale | 44fbdf5 | 2016-11-16 10:18:45 -0800 | [diff] [blame] | 74 | @Before |
| 75 | public void setUp() throws Exception { |
Wale Ogunwale | 3c1170d | 2016-12-02 14:44:52 -0800 | [diff] [blame] | 76 | if (sOneTimeSetupDone) { |
| 77 | return; |
| 78 | } |
| 79 | sOneTimeSetupDone = true; |
Wale Ogunwale | 44fbdf5 | 2016-11-16 10:18:45 -0800 | [diff] [blame] | 80 | final Context context = InstrumentationRegistry.getTargetContext(); |
| 81 | sWm = TestWindowManagerPolicy.getWindowManagerService(context); |
Wale Ogunwale | 3c1170d | 2016-12-02 14:44:52 -0800 | [diff] [blame] | 82 | sLayersController = new WindowLayersController(sWm); |
| 83 | sDisplayContent = new DisplayContent(context.getDisplay(), sWm, sLayersController, |
| 84 | new WallpaperController(sWm)); |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 85 | sWm.mRoot.addChild(sDisplayContent, 0); |
Wale Ogunwale | 3c1170d | 2016-12-02 14:44:52 -0800 | [diff] [blame] | 86 | |
| 87 | // Set-up some common windows. |
| 88 | sWallpaperWindow = createWindow(null, TYPE_WALLPAPER, sDisplayContent, "wallpaperWindow"); |
| 89 | sImeWindow = createWindow(null, TYPE_INPUT_METHOD, sDisplayContent, "sImeWindow"); |
| 90 | sImeDialogWindow = |
| 91 | createWindow(null, TYPE_INPUT_METHOD_DIALOG, sDisplayContent, "sImeDialogWindow"); |
| 92 | sStatusBarWindow = createWindow(null, TYPE_STATUS_BAR, sDisplayContent, "sStatusBarWindow"); |
| 93 | sNavBarWindow = |
Wale Ogunwale | 6ce0fb8 | 2016-12-13 14:24:00 -0800 | [diff] [blame] | 94 | createWindow(null, TYPE_NAVIGATION_BAR, sDisplayContent, "sNavBarWindow"); |
Wale Ogunwale | 3c1170d | 2016-12-02 14:44:52 -0800 | [diff] [blame] | 95 | sDockedDividerWindow = |
Wale Ogunwale | 6ce0fb8 | 2016-12-13 14:24:00 -0800 | [diff] [blame] | 96 | createWindow(null, TYPE_DOCK_DIVIDER, sDisplayContent, "sDockedDividerWindow"); |
Wale Ogunwale | 3c1170d | 2016-12-02 14:44:52 -0800 | [diff] [blame] | 97 | sAppWindow = createWindow(null, TYPE_BASE_APPLICATION, sDisplayContent, "sAppWindow"); |
| 98 | sChildAppWindowAbove = createWindow(sAppWindow, |
| 99 | TYPE_APPLICATION_ATTACHED_DIALOG, sAppWindow.mToken, "sChildAppWindowAbove"); |
| 100 | sChildAppWindowBelow = createWindow(sAppWindow, |
| 101 | TYPE_APPLICATION_MEDIA_OVERLAY, sAppWindow.mToken, "sChildAppWindowBelow"); |
Wale Ogunwale | 44fbdf5 | 2016-11-16 10:18:45 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /** Asserts that the first entry is greater than the second entry. */ |
| 105 | void assertGreaterThan(int first, int second) throws Exception { |
| 106 | Assert.assertTrue("Excepted " + first + " to be greater than " + second, first > second); |
| 107 | } |
| 108 | |
Wale Ogunwale | 3d0bfd9 | 2016-12-05 11:38:02 -0800 | [diff] [blame] | 109 | private WindowToken createWindowToken(DisplayContent dc, int type) { |
Wale Ogunwale | 44fbdf5 | 2016-11-16 10:18:45 -0800 | [diff] [blame] | 110 | if (type < FIRST_APPLICATION_WINDOW || type > LAST_APPLICATION_WINDOW) { |
Wale Ogunwale | 3d0bfd9 | 2016-12-05 11:38:02 -0800 | [diff] [blame] | 111 | return new TestWindowToken(type, dc); |
Wale Ogunwale | 44fbdf5 | 2016-11-16 10:18:45 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 114 | final TaskStack stack = createTaskStackOnDisplay(dc); |
| 115 | final Task task = createTaskInStack(stack, 0 /* userId */); |
Wale Ogunwale | 3d0bfd9 | 2016-12-05 11:38:02 -0800 | [diff] [blame] | 116 | final TestAppWindowToken token = new TestAppWindowToken(dc); |
Wale Ogunwale | 72919d2 | 2016-12-08 18:58:50 -0800 | [diff] [blame] | 117 | task.addChild(token, 0); |
Wale Ogunwale | 44fbdf5 | 2016-11-16 10:18:45 -0800 | [diff] [blame] | 118 | return token; |
| 119 | } |
| 120 | |
Wale Ogunwale | 805d9ec | 2016-12-07 12:22:08 -0800 | [diff] [blame] | 121 | WindowState createWindow(WindowState parent, int type, String name) { |
| 122 | return (parent == null) |
| 123 | ? createWindow(parent, type, sDisplayContent, name) |
| 124 | : createWindow(parent, type, parent.mToken, name); |
| 125 | } |
| 126 | |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 127 | WindowState createAppWindow(Task task, int type, String name) { |
| 128 | final AppWindowToken token = new TestAppWindowToken(sDisplayContent); |
| 129 | task.addChild(token, 0); |
| 130 | return createWindow(null, type, token, name); |
| 131 | } |
| 132 | |
Wale Ogunwale | 44fbdf5 | 2016-11-16 10:18:45 -0800 | [diff] [blame] | 133 | WindowState createWindow(WindowState parent, int type, DisplayContent dc, String name) { |
| 134 | final WindowToken token = createWindowToken(dc, type); |
| 135 | return createWindow(parent, type, token, name); |
| 136 | } |
| 137 | |
| 138 | WindowState createWindow(WindowState parent, int type, WindowToken token, String name) { |
| 139 | final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(type); |
| 140 | attrs.setTitle(name); |
| 141 | |
| 142 | final WindowState w = new WindowState(sWm, mMockSession, mIWindow, token, parent, OP_NONE, |
| 143 | 0, attrs, 0, 0); |
| 144 | // TODO: Probably better to make this call in the WindowState ctor to avoid errors with |
| 145 | // adding it to the token... |
| 146 | token.addWindow(w); |
| 147 | return w; |
| 148 | } |
Wale Ogunwale | 3d0bfd9 | 2016-12-05 11:38:02 -0800 | [diff] [blame] | 149 | |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 150 | /** Creates a {@link TaskStack} and adds it to the specified {@link DisplayContent}. */ |
| 151 | TaskStack createTaskStackOnDisplay(DisplayContent dc) { |
| 152 | final int stackId = sNextStackId++; |
| 153 | dc.addStackToDisplay(stackId, true); |
| 154 | return sWm.mStackIdToStack.get(stackId); |
| 155 | } |
| 156 | |
| 157 | /**Creates a {@link Task} and adds it to the specified {@link TaskStack}. */ |
| 158 | Task createTaskInStack(TaskStack stack, int userId) { |
| 159 | final Task newTask = new Task(sNextTaskId++, stack, userId, sWm, null, EMPTY, false, 0, |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 160 | false, null); |
| 161 | stack.addTask(newTask, POSITION_TOP); |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 162 | return newTask; |
| 163 | } |
| 164 | |
Wale Ogunwale | 3d0bfd9 | 2016-12-05 11:38:02 -0800 | [diff] [blame] | 165 | /* Used so we can gain access to some protected members of the {@link WindowToken} class */ |
| 166 | class TestWindowToken extends WindowToken { |
| 167 | |
| 168 | TestWindowToken(int type, DisplayContent dc) { |
| 169 | this(type, dc, false /* persistOnEmpty */); |
| 170 | } |
| 171 | |
| 172 | TestWindowToken(int type, DisplayContent dc, boolean persistOnEmpty) { |
| 173 | super(sWm, mock(IBinder.class), type, persistOnEmpty, dc); |
| 174 | } |
| 175 | |
| 176 | int getWindowsCount() { |
| 177 | return mChildren.size(); |
| 178 | } |
| 179 | |
| 180 | boolean hasWindow(WindowState w) { |
| 181 | return mChildren.contains(w); |
| 182 | } |
| 183 | } |
| 184 | |
Andrii Kulian | 4ede3e0 | 2017-01-12 11:52:31 -0800 | [diff] [blame] | 185 | /** Used so we can gain access to some protected members of the {@link AppWindowToken} class. */ |
Wale Ogunwale | 3d0bfd9 | 2016-12-05 11:38:02 -0800 | [diff] [blame] | 186 | class TestAppWindowToken extends AppWindowToken { |
| 187 | |
| 188 | TestAppWindowToken(DisplayContent dc) { |
| 189 | super(sWm, null, false, dc); |
| 190 | } |
| 191 | |
| 192 | int getWindowsCount() { |
| 193 | return mChildren.size(); |
| 194 | } |
| 195 | |
| 196 | boolean hasWindow(WindowState w) { |
| 197 | return mChildren.contains(w); |
| 198 | } |
| 199 | |
| 200 | WindowState getFirstChild() { |
| 201 | return mChildren.getFirst(); |
| 202 | } |
| 203 | |
| 204 | WindowState getLastChild() { |
| 205 | return mChildren.getLast(); |
| 206 | } |
| 207 | } |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 208 | |
Wale Ogunwale | c5cc301 | 2017-01-13 13:26:16 -0800 | [diff] [blame] | 209 | /* Used so we can gain access to some protected members of the {@link Task} class */ |
| 210 | class TestTask extends Task { |
| 211 | |
| 212 | boolean mShouldDeferRemoval = false; |
Andrii Kulian | 7cd7c2d | 2017-01-18 12:14:37 -0800 | [diff] [blame^] | 213 | boolean mOnDisplayChangedCalled = false; |
Wale Ogunwale | c5cc301 | 2017-01-13 13:26:16 -0800 | [diff] [blame] | 214 | |
| 215 | TestTask(int taskId, TaskStack stack, int userId, WindowManagerService service, Rect bounds, |
| 216 | Configuration overrideConfig, boolean isOnTopLauncher, int resizeMode, |
| 217 | boolean homeTask, TaskWindowContainerController controller) { |
| 218 | super(taskId, stack, userId, service, bounds, overrideConfig, isOnTopLauncher, |
| 219 | resizeMode, homeTask, controller); |
| 220 | } |
| 221 | |
| 222 | boolean shouldDeferRemoval() { |
| 223 | return mShouldDeferRemoval; |
| 224 | } |
| 225 | |
| 226 | int positionInParent() { |
| 227 | return getParent().mChildren.indexOf(this); |
| 228 | } |
Andrii Kulian | 7cd7c2d | 2017-01-18 12:14:37 -0800 | [diff] [blame^] | 229 | |
| 230 | @Override |
| 231 | void onDisplayChanged(DisplayContent dc) { |
| 232 | super.onDisplayChanged(dc); |
| 233 | mOnDisplayChangedCalled = true; |
| 234 | } |
Wale Ogunwale | c5cc301 | 2017-01-13 13:26:16 -0800 | [diff] [blame] | 235 | } |
| 236 | |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 237 | /** |
| 238 | * Used so we can gain access to some protected members of {@link TaskWindowContainerController} |
| 239 | * class. |
| 240 | */ |
| 241 | class TestTaskWindowContainerController extends TaskWindowContainerController { |
| 242 | |
Wale Ogunwale | c5cc301 | 2017-01-13 13:26:16 -0800 | [diff] [blame] | 243 | TestTaskWindowContainerController() { |
| 244 | this(createTaskStackOnDisplay(sDisplayContent).mStackId); |
| 245 | } |
| 246 | |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 247 | TestTaskWindowContainerController(int stackId) { |
| 248 | super(sNextTaskId++, stackId, 0 /* userId */, null /* bounds */, |
| 249 | EMPTY /* overrideConfig*/, RESIZE_MODE_UNRESIZEABLE, false /* homeTask*/, |
| 250 | false /* isOnTopLauncher */, true /* toTop*/, true /* showForAllUsers */); |
| 251 | } |
Wale Ogunwale | c5cc301 | 2017-01-13 13:26:16 -0800 | [diff] [blame] | 252 | |
| 253 | @Override |
| 254 | TestTask createTask(int taskId, TaskStack stack, int userId, Rect bounds, |
| 255 | Configuration overrideConfig, int resizeMode, boolean homeTask, |
| 256 | boolean isOnTopLauncher) { |
| 257 | return new TestTask(taskId, stack, userId, mService, bounds, overrideConfig, |
| 258 | isOnTopLauncher, resizeMode, homeTask, this); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | class TestAppWindowContainerController extends AppWindowContainerController { |
| 263 | |
| 264 | final IApplicationToken mToken; |
| 265 | |
| 266 | TestAppWindowContainerController(TestTaskWindowContainerController taskController) { |
| 267 | this(taskController, new TestIApplicationToken()); |
| 268 | } |
| 269 | |
| 270 | TestAppWindowContainerController(TestTaskWindowContainerController taskController, |
| 271 | IApplicationToken token) { |
| 272 | super(taskController, token, null /* listener */, 0 /* index */, |
| 273 | SCREEN_ORIENTATION_UNSPECIFIED, true /* fullscreen */, |
| 274 | true /* showForAllUsers */, 0 /* configChanges */, false /* voiceInteraction */, |
| 275 | false /* launchTaskBehind */, false /* alwaysFocusable */, |
| 276 | 0 /* targetSdkVersion */, 0 /* rotationAnimationHint */, |
| 277 | 0 /* inputDispatchingTimeoutNanos */, sWm); |
| 278 | mToken = token; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | class TestIApplicationToken implements IApplicationToken { |
| 283 | |
| 284 | private final Binder mBinder = new Binder(); |
| 285 | @Override |
| 286 | public IBinder asBinder() { |
| 287 | return mBinder; |
| 288 | } |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 289 | } |
Andrii Kulian | 4ede3e0 | 2017-01-12 11:52:31 -0800 | [diff] [blame] | 290 | |
| 291 | /** Used to track resize reports. */ |
| 292 | class TestWindowState extends WindowState { |
| 293 | boolean resizeReported; |
| 294 | |
| 295 | TestWindowState(WindowManager.LayoutParams attrs, WindowToken token) { |
| 296 | super(sWm, mMockSession, mIWindow, token, null, OP_NONE, 0, attrs, 0, 0); |
| 297 | } |
| 298 | |
| 299 | @Override |
| 300 | void reportResized() { |
| 301 | super.reportResized(); |
| 302 | resizeReported = true; |
| 303 | } |
| 304 | |
| 305 | @Override |
| 306 | public boolean isGoneForLayoutLw() { |
| 307 | return false; |
| 308 | } |
| 309 | |
| 310 | @Override |
| 311 | void updateResizingWindowIfNeeded() { |
| 312 | // Used in AppWindowTokenTests#testLandscapeSeascapeRotationRelayout to deceive |
| 313 | // the system that it can actually update the window. |
| 314 | boolean hadSurface = mHasSurface; |
| 315 | mHasSurface = true; |
| 316 | |
| 317 | super.updateResizingWindowIfNeeded(); |
| 318 | |
| 319 | mHasSurface = hadSurface; |
| 320 | } |
| 321 | } |
Wale Ogunwale | 44fbdf5 | 2016-11-16 10:18:45 -0800 | [diff] [blame] | 322 | } |