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 | |
| 19 | import org.junit.Assert; |
| 20 | import org.junit.Before; |
| 21 | |
| 22 | import android.content.Context; |
| 23 | import android.os.IBinder; |
| 24 | import android.support.test.InstrumentationRegistry; |
Wale Ogunwale | 44fbdf5 | 2016-11-16 10:18:45 -0800 | [diff] [blame] | 25 | import android.view.IWindow; |
| 26 | import android.view.WindowManager; |
| 27 | |
| 28 | import static android.app.ActivityManager.StackId.FIRST_DYNAMIC_STACK_ID; |
| 29 | import static android.app.AppOpsManager.OP_NONE; |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame^] | 30 | import static android.content.pm.ActivityInfo.RESIZE_MODE_UNRESIZEABLE; |
Wale Ogunwale | 44fbdf5 | 2016-11-16 10:18:45 -0800 | [diff] [blame] | 31 | import static android.content.res.Configuration.EMPTY; |
| 32 | import static android.view.WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW; |
| 33 | import static android.view.WindowManager.LayoutParams.LAST_APPLICATION_WINDOW; |
Wale Ogunwale | 3c1170d | 2016-12-02 14:44:52 -0800 | [diff] [blame] | 34 | import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG; |
| 35 | import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA_OVERLAY; |
| 36 | import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION; |
| 37 | import static android.view.WindowManager.LayoutParams.TYPE_DOCK_DIVIDER; |
| 38 | import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD; |
| 39 | import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG; |
| 40 | import static android.view.WindowManager.LayoutParams.TYPE_NAVIGATION_BAR; |
| 41 | import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR; |
| 42 | import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER; |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame^] | 43 | import static com.android.server.wm.WindowContainer.POSITION_TOP; |
Wale Ogunwale | 44fbdf5 | 2016-11-16 10:18:45 -0800 | [diff] [blame] | 44 | import static org.mockito.Mockito.mock; |
| 45 | |
| 46 | /** |
| 47 | * Common base class for window manager unit test classes. |
| 48 | */ |
Wale Ogunwale | 26c0dfe | 2016-12-14 14:42:30 -0800 | [diff] [blame] | 49 | class WindowTestsBase { |
Wale Ogunwale | 44fbdf5 | 2016-11-16 10:18:45 -0800 | [diff] [blame] | 50 | static WindowManagerService sWm = null; |
| 51 | private final IWindow mIWindow = new TestIWindow(); |
| 52 | private final Session mMockSession = mock(Session.class); |
Wale Ogunwale | 44fbdf5 | 2016-11-16 10:18:45 -0800 | [diff] [blame] | 53 | private static int sNextStackId = FIRST_DYNAMIC_STACK_ID; |
| 54 | private static int sNextTaskId = 0; |
| 55 | |
Wale Ogunwale | 3c1170d | 2016-12-02 14:44:52 -0800 | [diff] [blame] | 56 | private static boolean sOneTimeSetupDone = false; |
Wale Ogunwale | 26c0dfe | 2016-12-14 14:42:30 -0800 | [diff] [blame] | 57 | 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 Ogunwale | 3c1170d | 2016-12-02 14:44:52 -0800 | [diff] [blame] | 68 | |
Wale Ogunwale | 44fbdf5 | 2016-11-16 10:18:45 -0800 | [diff] [blame] | 69 | @Before |
| 70 | public void setUp() throws Exception { |
Wale Ogunwale | 3c1170d | 2016-12-02 14:44:52 -0800 | [diff] [blame] | 71 | if (sOneTimeSetupDone) { |
| 72 | return; |
| 73 | } |
| 74 | sOneTimeSetupDone = true; |
Wale Ogunwale | 44fbdf5 | 2016-11-16 10:18:45 -0800 | [diff] [blame] | 75 | final Context context = InstrumentationRegistry.getTargetContext(); |
| 76 | sWm = TestWindowManagerPolicy.getWindowManagerService(context); |
Wale Ogunwale | 3c1170d | 2016-12-02 14:44:52 -0800 | [diff] [blame] | 77 | sLayersController = new WindowLayersController(sWm); |
| 78 | sDisplayContent = new DisplayContent(context.getDisplay(), sWm, sLayersController, |
| 79 | new WallpaperController(sWm)); |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 80 | sWm.mRoot.addChild(sDisplayContent, 0); |
Wale Ogunwale | 3c1170d | 2016-12-02 14:44:52 -0800 | [diff] [blame] | 81 | |
| 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 Ogunwale | 6ce0fb8 | 2016-12-13 14:24:00 -0800 | [diff] [blame] | 89 | createWindow(null, TYPE_NAVIGATION_BAR, sDisplayContent, "sNavBarWindow"); |
Wale Ogunwale | 3c1170d | 2016-12-02 14:44:52 -0800 | [diff] [blame] | 90 | sDockedDividerWindow = |
Wale Ogunwale | 6ce0fb8 | 2016-12-13 14:24:00 -0800 | [diff] [blame] | 91 | createWindow(null, TYPE_DOCK_DIVIDER, sDisplayContent, "sDockedDividerWindow"); |
Wale Ogunwale | 3c1170d | 2016-12-02 14:44:52 -0800 | [diff] [blame] | 92 | 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 Ogunwale | 44fbdf5 | 2016-11-16 10:18:45 -0800 | [diff] [blame] | 97 | } |
| 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 Ogunwale | 3d0bfd9 | 2016-12-05 11:38:02 -0800 | [diff] [blame] | 104 | private WindowToken createWindowToken(DisplayContent dc, int type) { |
Wale Ogunwale | 44fbdf5 | 2016-11-16 10:18:45 -0800 | [diff] [blame] | 105 | if (type < FIRST_APPLICATION_WINDOW || type > LAST_APPLICATION_WINDOW) { |
Wale Ogunwale | 3d0bfd9 | 2016-12-05 11:38:02 -0800 | [diff] [blame] | 106 | return new TestWindowToken(type, dc); |
Wale Ogunwale | 44fbdf5 | 2016-11-16 10:18:45 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 109 | final TaskStack stack = createTaskStackOnDisplay(dc); |
| 110 | final Task task = createTaskInStack(stack, 0 /* userId */); |
Wale Ogunwale | 3d0bfd9 | 2016-12-05 11:38:02 -0800 | [diff] [blame] | 111 | final TestAppWindowToken token = new TestAppWindowToken(dc); |
Wale Ogunwale | 72919d2 | 2016-12-08 18:58:50 -0800 | [diff] [blame] | 112 | task.addChild(token, 0); |
Wale Ogunwale | 44fbdf5 | 2016-11-16 10:18:45 -0800 | [diff] [blame] | 113 | return token; |
| 114 | } |
| 115 | |
Wale Ogunwale | 805d9ec | 2016-12-07 12:22:08 -0800 | [diff] [blame] | 116 | 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 | |
Wale Ogunwale | 44fbdf5 | 2016-11-16 10:18:45 -0800 | [diff] [blame] | 122 | WindowState createWindow(WindowState parent, int type, DisplayContent dc, String name) { |
| 123 | final WindowToken token = createWindowToken(dc, type); |
| 124 | return createWindow(parent, type, token, name); |
| 125 | } |
| 126 | |
| 127 | WindowState createWindow(WindowState parent, int type, WindowToken token, String name) { |
| 128 | final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(type); |
| 129 | attrs.setTitle(name); |
| 130 | |
| 131 | final WindowState w = new WindowState(sWm, mMockSession, mIWindow, token, parent, OP_NONE, |
| 132 | 0, attrs, 0, 0); |
| 133 | // TODO: Probably better to make this call in the WindowState ctor to avoid errors with |
| 134 | // adding it to the token... |
| 135 | token.addWindow(w); |
| 136 | return w; |
| 137 | } |
Wale Ogunwale | 3d0bfd9 | 2016-12-05 11:38:02 -0800 | [diff] [blame] | 138 | |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 139 | /** Creates a {@link TaskStack} and adds it to the specified {@link DisplayContent}. */ |
| 140 | TaskStack createTaskStackOnDisplay(DisplayContent dc) { |
| 141 | final int stackId = sNextStackId++; |
| 142 | dc.addStackToDisplay(stackId, true); |
| 143 | return sWm.mStackIdToStack.get(stackId); |
| 144 | } |
| 145 | |
| 146 | /**Creates a {@link Task} and adds it to the specified {@link TaskStack}. */ |
| 147 | Task createTaskInStack(TaskStack stack, int userId) { |
| 148 | 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^] | 149 | false, null); |
| 150 | stack.addTask(newTask, POSITION_TOP); |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 151 | return newTask; |
| 152 | } |
| 153 | |
Wale Ogunwale | 3d0bfd9 | 2016-12-05 11:38:02 -0800 | [diff] [blame] | 154 | /* Used so we can gain access to some protected members of the {@link WindowToken} class */ |
| 155 | class TestWindowToken extends WindowToken { |
| 156 | |
| 157 | TestWindowToken(int type, DisplayContent dc) { |
| 158 | this(type, dc, false /* persistOnEmpty */); |
| 159 | } |
| 160 | |
| 161 | TestWindowToken(int type, DisplayContent dc, boolean persistOnEmpty) { |
| 162 | super(sWm, mock(IBinder.class), type, persistOnEmpty, dc); |
| 163 | } |
| 164 | |
| 165 | int getWindowsCount() { |
| 166 | return mChildren.size(); |
| 167 | } |
| 168 | |
| 169 | boolean hasWindow(WindowState w) { |
| 170 | return mChildren.contains(w); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /* Used so we can gain access to some protected members of the {@link AppWindowToken} class */ |
| 175 | class TestAppWindowToken extends AppWindowToken { |
| 176 | |
| 177 | TestAppWindowToken(DisplayContent dc) { |
| 178 | super(sWm, null, false, dc); |
| 179 | } |
| 180 | |
| 181 | int getWindowsCount() { |
| 182 | return mChildren.size(); |
| 183 | } |
| 184 | |
| 185 | boolean hasWindow(WindowState w) { |
| 186 | return mChildren.contains(w); |
| 187 | } |
| 188 | |
| 189 | WindowState getFirstChild() { |
| 190 | return mChildren.getFirst(); |
| 191 | } |
| 192 | |
| 193 | WindowState getLastChild() { |
| 194 | return mChildren.getLast(); |
| 195 | } |
| 196 | } |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame^] | 197 | |
| 198 | /** |
| 199 | * Used so we can gain access to some protected members of {@link TaskWindowContainerController} |
| 200 | * class. |
| 201 | */ |
| 202 | class TestTaskWindowContainerController extends TaskWindowContainerController { |
| 203 | |
| 204 | TestTaskWindowContainerController(int stackId) { |
| 205 | super(sNextTaskId++, stackId, 0 /* userId */, null /* bounds */, |
| 206 | EMPTY /* overrideConfig*/, RESIZE_MODE_UNRESIZEABLE, false /* homeTask*/, |
| 207 | false /* isOnTopLauncher */, true /* toTop*/, true /* showForAllUsers */); |
| 208 | } |
| 209 | } |
Wale Ogunwale | 44fbdf5 | 2016-11-16 10:18:45 -0800 | [diff] [blame] | 210 | } |