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