Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.am; |
| 18 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 19 | import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD; |
| 20 | import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN; |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 21 | import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED; |
Wale Ogunwale | 9dcf946 | 2017-09-19 15:13:01 -0700 | [diff] [blame] | 22 | import static android.view.Display.DEFAULT_DISPLAY; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 23 | import static org.mockito.Mockito.mock; |
Bryce Lee | 2a3cc46 | 2017-10-27 10:57:35 -0700 | [diff] [blame^] | 24 | import static org.mockito.Mockito.doNothing; |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 25 | import static org.mockito.Mockito.doReturn; |
| 26 | import static org.mockito.Mockito.any; |
Bryce Lee | 2a3cc46 | 2017-10-27 10:57:35 -0700 | [diff] [blame^] | 27 | import static org.mockito.Mockito.anyBoolean; |
| 28 | import static org.mockito.Mockito.anyInt; |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 29 | import static org.mockito.Mockito.doAnswer; |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 30 | import static org.mockito.Mockito.spy; |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 31 | |
| 32 | import org.mockito.invocation.InvocationOnMock; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 33 | |
| 34 | import android.content.ComponentName; |
| 35 | import android.content.Context; |
| 36 | import android.content.Intent; |
| 37 | import android.content.pm.ActivityInfo; |
| 38 | import android.content.pm.ApplicationInfo; |
| 39 | import android.content.res.Configuration; |
| 40 | import android.graphics.Rect; |
Wale Ogunwale | 9dcf946 | 2017-09-19 15:13:01 -0700 | [diff] [blame] | 41 | import android.hardware.display.DisplayManager; |
Bryce Lee | 3115bdf | 2017-04-05 08:39:40 -0700 | [diff] [blame] | 42 | import android.os.HandlerThread; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 43 | import android.os.Looper; |
| 44 | import android.support.test.InstrumentationRegistry; |
| 45 | import com.android.server.AttributeCache; |
| 46 | import com.android.server.wm.AppWindowContainerController; |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 47 | import com.android.server.wm.PinnedStackWindowController; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 48 | import com.android.server.wm.StackWindowController; |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 49 | import com.android.server.wm.TaskWindowContainerController; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 50 | import com.android.server.wm.WindowManagerService; |
| 51 | import com.android.server.wm.WindowTestUtils; |
Bryce Lee | 3115bdf | 2017-04-05 08:39:40 -0700 | [diff] [blame] | 52 | import org.junit.After; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 53 | import org.junit.Before; |
| 54 | import org.mockito.MockitoAnnotations; |
| 55 | |
| 56 | /** |
| 57 | * A base class to handle common operations in activity related unit tests. |
| 58 | */ |
| 59 | public class ActivityTestsBase { |
Bryce Lee | 939a9a3 | 2017-10-23 10:01:21 -0700 | [diff] [blame] | 60 | private static boolean sOneTimeSetupDone = false; |
| 61 | |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 62 | private final Context mContext = InstrumentationRegistry.getContext(); |
Bryce Lee | 3115bdf | 2017-04-05 08:39:40 -0700 | [diff] [blame] | 63 | private HandlerThread mHandlerThread; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 64 | |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 65 | @Before |
| 66 | public void setUp() throws Exception { |
Bryce Lee | 939a9a3 | 2017-10-23 10:01:21 -0700 | [diff] [blame] | 67 | if (!sOneTimeSetupDone) { |
| 68 | sOneTimeSetupDone = true; |
| 69 | |
| 70 | // Allows to mock package local classes and methods |
| 71 | System.setProperty("dexmaker.share_classloader", "true"); |
| 72 | MockitoAnnotations.initMocks(this); |
| 73 | } |
Bryce Lee | 3115bdf | 2017-04-05 08:39:40 -0700 | [diff] [blame] | 74 | mHandlerThread = new HandlerThread("ActivityTestsBaseThread"); |
| 75 | mHandlerThread.start(); |
| 76 | } |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 77 | |
Bryce Lee | 3115bdf | 2017-04-05 08:39:40 -0700 | [diff] [blame] | 78 | @After |
| 79 | public void tearDown() { |
| 80 | mHandlerThread.quitSafely(); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | protected ActivityManagerService createActivityManagerService() { |
Winson Chung | 3f0e59a | 2017-10-25 10:19:05 -0700 | [diff] [blame] | 84 | return setupActivityManagerService(new TestActivityManagerService(mContext)); |
| 85 | } |
| 86 | |
| 87 | protected ActivityManagerService setupActivityManagerService(ActivityManagerService service) { |
| 88 | service = spy(service); |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 89 | service.mWindowManager = prepareMockWindowManager(); |
Bryce Lee | 840c566 | 2017-04-13 10:02:51 -0700 | [diff] [blame] | 90 | return service; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 93 | /** |
| 94 | * Builder for creating new activities. |
| 95 | */ |
| 96 | protected static class ActivityBuilder { |
| 97 | // An id appended to the end of the component name to make it unique |
| 98 | private static int sCurrentActivityId = 0; |
Bryce Lee | 9f6affd | 2017-09-01 09:18:35 -0700 | [diff] [blame] | 99 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 100 | // Default package name |
| 101 | private static final String DEFAULT_PACKAGE = "com.foo"; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 102 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 103 | // Default base activity name |
| 104 | private static final String DEFAULT_BASE_ACTIVITY_NAME = ".BarActivity"; |
| 105 | |
| 106 | private final ActivityManagerService mService; |
| 107 | |
| 108 | private ComponentName mComponent; |
| 109 | private TaskRecord mTaskRecord; |
| 110 | private int mUid; |
| 111 | private boolean mCreateTask; |
| 112 | private ActivityStack mStack; |
| 113 | |
| 114 | ActivityBuilder(ActivityManagerService service) { |
| 115 | mService = service; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 118 | ActivityBuilder setComponent(ComponentName component) { |
| 119 | mComponent = component; |
| 120 | return this; |
| 121 | } |
| 122 | |
| 123 | ActivityBuilder setTask(TaskRecord task) { |
| 124 | mTaskRecord = task; |
| 125 | return this; |
| 126 | } |
| 127 | |
| 128 | ActivityBuilder setStack(ActivityStack stack) { |
| 129 | mStack = stack; |
| 130 | return this; |
| 131 | } |
| 132 | |
| 133 | ActivityBuilder setCreateTask(boolean createTask) { |
| 134 | mCreateTask = createTask; |
| 135 | return this; |
| 136 | } |
| 137 | |
| 138 | ActivityBuilder setUid(int uid) { |
| 139 | mUid = uid; |
| 140 | return this; |
| 141 | } |
| 142 | |
| 143 | ActivityRecord build() { |
| 144 | if (mComponent == null) { |
| 145 | final int id = sCurrentActivityId++; |
| 146 | mComponent = ComponentName.createRelative(DEFAULT_PACKAGE, |
| 147 | DEFAULT_BASE_ACTIVITY_NAME + id); |
| 148 | } |
| 149 | |
| 150 | if (mCreateTask) { |
| 151 | mTaskRecord = new TaskBuilder(mService.mStackSupervisor) |
| 152 | .setComponent(mComponent) |
| 153 | .setStack(mStack).build(); |
| 154 | } |
| 155 | |
| 156 | Intent intent = new Intent(); |
| 157 | intent.setComponent(mComponent); |
| 158 | final ActivityInfo aInfo = new ActivityInfo(); |
| 159 | aInfo.applicationInfo = new ApplicationInfo(); |
| 160 | aInfo.applicationInfo.packageName = mComponent.getPackageName(); |
| 161 | aInfo.applicationInfo.uid = mUid; |
| 162 | AttributeCache.init(mService.mContext); |
| 163 | final ActivityRecord activity = new ActivityRecord(mService, null /* caller */, |
| 164 | 0 /* launchedFromPid */, 0, null, intent, null, |
| 165 | aInfo /*aInfo*/, new Configuration(), null /* resultTo */, null /* resultWho */, |
| 166 | 0 /* reqCode */, false /*componentSpecified*/, false /* rootVoiceInteraction */, |
| 167 | mService.mStackSupervisor, null /* options */, null /* sourceRecord */); |
| 168 | activity.mWindowContainerController = mock(AppWindowContainerController.class); |
| 169 | |
| 170 | if (mTaskRecord != null) { |
| 171 | mTaskRecord.addActivityToTop(activity); |
| 172 | } |
| 173 | |
| 174 | return activity; |
| 175 | } |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 178 | /** |
| 179 | * Builder for creating new tasks. |
| 180 | */ |
| 181 | protected static class TaskBuilder { |
| 182 | private final ActivityStackSupervisor mSupervisor; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 183 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 184 | private ComponentName mComponent; |
| 185 | private String mPackage; |
| 186 | private int mFlags = 0; |
| 187 | private int mTaskId = 0; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 188 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 189 | private ActivityStack mStack; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 190 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 191 | TaskBuilder(ActivityStackSupervisor supervisor) { |
| 192 | mSupervisor = supervisor; |
| 193 | } |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 194 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 195 | TaskBuilder setComponent(ComponentName component) { |
| 196 | mComponent = component; |
| 197 | return this; |
| 198 | } |
| 199 | |
| 200 | TaskBuilder setPackage(String packageName) { |
| 201 | mPackage = packageName; |
| 202 | return this; |
| 203 | } |
| 204 | |
| 205 | TaskBuilder setFlags(int flags) { |
| 206 | mFlags = flags; |
| 207 | return this; |
| 208 | } |
| 209 | |
| 210 | TaskBuilder setTaskId(int taskId) { |
| 211 | mTaskId = taskId; |
| 212 | return this; |
| 213 | } |
| 214 | |
| 215 | TaskBuilder setStack(ActivityStack stack) { |
| 216 | mStack = stack; |
| 217 | return this; |
| 218 | } |
| 219 | |
| 220 | TaskRecord build() { |
| 221 | if (mStack == null) { |
| 222 | mStack = mSupervisor.getDefaultDisplay().createStack( |
| 223 | WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, true /* onTop */); |
| 224 | } |
| 225 | |
| 226 | final ActivityInfo aInfo = new ActivityInfo(); |
| 227 | aInfo.applicationInfo = new ApplicationInfo(); |
| 228 | aInfo.applicationInfo.packageName = mPackage; |
| 229 | |
| 230 | Intent intent = new Intent(); |
| 231 | intent.setComponent(mComponent); |
| 232 | intent.setFlags(mFlags); |
| 233 | |
| 234 | final TaskRecord task = new TaskRecord(mSupervisor.mService, mTaskId, aInfo, |
| 235 | intent /*intent*/, null /*_taskDescription*/); |
| 236 | mSupervisor.setFocusStackUnchecked("test", mStack); |
| 237 | mStack.addTask(task, true, "creating test task"); |
| 238 | task.setStack(mStack); |
| 239 | task.setWindowContainerController(mock(TaskWindowContainerController.class)); |
| 240 | |
| 241 | return task; |
| 242 | } |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | /** |
| 246 | * An {@link ActivityManagerService} subclass which provides a test |
| 247 | * {@link ActivityStackSupervisor}. |
| 248 | */ |
| 249 | protected static class TestActivityManagerService extends ActivityManagerService { |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 250 | TestActivityManagerService(Context context) { |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 251 | super(context); |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 252 | mSupportsMultiWindow = true; |
| 253 | mSupportsMultiDisplay = true; |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 254 | mSupportsSplitScreenMultiWindow = true; |
| 255 | mSupportsFreeformWindowManagement = true; |
| 256 | mSupportsPictureInPicture = true; |
Bryce Lee | 6a0754a | 2017-10-10 17:53:50 -0700 | [diff] [blame] | 257 | mWindowManager = WindowTestUtils.getMockWindowManagerService(); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | @Override |
Bryce Lee | 2a3cc46 | 2017-10-27 10:57:35 -0700 | [diff] [blame^] | 261 | final protected ActivityStackSupervisor createStackSupervisor() { |
| 262 | final ActivityStackSupervisor supervisor = spy(createTestSupervisor()); |
| 263 | |
| 264 | // No home stack is set. |
| 265 | doNothing().when(supervisor).moveHomeStackToFront(any()); |
| 266 | doReturn(true).when(supervisor).moveHomeStackTaskToTop(any()); |
| 267 | // Invoked during {@link ActivityStack} creation. |
| 268 | doNothing().when(supervisor).updateUIDsPresentOnDisplay(); |
| 269 | // Always keep things awake. |
| 270 | doReturn(true).when(supervisor).hasAwakeDisplay(); |
| 271 | // Called when moving activity to pinned stack. |
| 272 | doNothing().when(supervisor).ensureActivitiesVisibleLocked(any(), anyInt(), anyBoolean()); |
| 273 | // Do not schedule idle timeouts |
| 274 | doNothing().when(supervisor).scheduleIdleTimeoutLocked(any()); |
| 275 | |
| 276 | supervisor.initialize(); |
| 277 | |
| 278 | return supervisor; |
| 279 | } |
| 280 | |
| 281 | protected ActivityStackSupervisor createTestSupervisor() { |
Bryce Lee | 3115bdf | 2017-04-05 08:39:40 -0700 | [diff] [blame] | 282 | return new TestActivityStackSupervisor(this, mHandlerThread.getLooper()); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 283 | } |
Bryce Lee | 29a649d | 2017-08-18 13:52:31 -0700 | [diff] [blame] | 284 | |
| 285 | @Override |
| 286 | void updateUsageStats(ActivityRecord component, boolean resumed) { |
| 287 | } |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | /** |
| 291 | * An {@link ActivityStackSupervisor} which stubs out certain methods that depend on |
| 292 | * setup not available in the test environment. Also specifies an injector for |
| 293 | */ |
| 294 | protected static class TestActivityStackSupervisor extends ActivityStackSupervisor { |
Bryce Lee | 2a3cc46 | 2017-10-27 10:57:35 -0700 | [diff] [blame^] | 295 | private ActivityDisplay mDisplay; |
Bryce Lee | 943ebe7 | 2017-05-04 10:19:07 -0700 | [diff] [blame] | 296 | |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 297 | public TestActivityStackSupervisor(ActivityManagerService service, Looper looper) { |
| 298 | super(service, looper); |
Wale Ogunwale | 9dcf946 | 2017-09-19 15:13:01 -0700 | [diff] [blame] | 299 | mDisplayManager = |
| 300 | (DisplayManager) mService.mContext.getSystemService(Context.DISPLAY_SERVICE); |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 301 | mWindowManager = prepareMockWindowManager(); |
Bryce Lee | 2a3cc46 | 2017-10-27 10:57:35 -0700 | [diff] [blame^] | 302 | } |
| 303 | |
| 304 | @Override |
| 305 | public void initialize() { |
| 306 | super.initialize(); |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 307 | mDisplay = new TestActivityDisplay(this, DEFAULT_DISPLAY); |
Wale Ogunwale | 9dcf946 | 2017-09-19 15:13:01 -0700 | [diff] [blame] | 308 | attachDisplay(mDisplay); |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 311 | @Override |
| 312 | ActivityDisplay getDefaultDisplay() { |
| 313 | return mDisplay; |
| 314 | } |
| 315 | |
Bryce Lee | 2a3cc46 | 2017-10-27 10:57:35 -0700 | [diff] [blame^] | 316 | // Just return the current front task. This is called internally so we cannot use spy to mock this out. |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 317 | @Override |
| 318 | ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) { |
| 319 | return mFocusedStack; |
| 320 | } |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | private static class TestActivityDisplay extends ActivityDisplay { |
| 324 | |
| 325 | private final ActivityStackSupervisor mSupervisor; |
| 326 | TestActivityDisplay(ActivityStackSupervisor supervisor, int displayId) { |
| 327 | super(supervisor, displayId); |
| 328 | mSupervisor = supervisor; |
Andrii Kulian | b1cdb10 | 2017-07-13 15:33:06 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | @Override |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 332 | <T extends ActivityStack> T createStackUnchecked(int windowingMode, int activityType, |
| 333 | int stackId, boolean onTop) { |
| 334 | if (windowingMode == WINDOWING_MODE_PINNED) { |
| 335 | return (T) new PinnedActivityStack(this, stackId, mSupervisor, onTop) { |
Andrii Kulian | b1cdb10 | 2017-07-13 15:33:06 -0700 | [diff] [blame] | 336 | @Override |
| 337 | Rect getDefaultPictureInPictureBounds(float aspectRatio) { |
| 338 | return new Rect(50, 50, 100, 100); |
| 339 | } |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 340 | |
| 341 | @Override |
| 342 | PinnedStackWindowController createStackWindowController(int displayId, |
| 343 | boolean onTop, Rect outBounds) { |
| 344 | return mock(PinnedStackWindowController.class); |
| 345 | } |
Andrii Kulian | b1cdb10 | 2017-07-13 15:33:06 -0700 | [diff] [blame] | 346 | }; |
| 347 | } else { |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 348 | return (T) new TestActivityStack( |
| 349 | this, stackId, mSupervisor, windowingMode, activityType, onTop); |
Andrii Kulian | b1cdb10 | 2017-07-13 15:33:06 -0700 | [diff] [blame] | 350 | } |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 351 | } |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 354 | private static WindowManagerService prepareMockWindowManager() { |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 355 | final WindowManagerService service = WindowTestUtils.getMockWindowManagerService(); |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 356 | |
| 357 | doAnswer((InvocationOnMock invocationOnMock) -> { |
| 358 | final Runnable runnable = invocationOnMock.<Runnable>getArgument(0); |
| 359 | if (runnable != null) { |
| 360 | runnable.run(); |
| 361 | } |
| 362 | return null; |
| 363 | }).when(service).inSurfaceTransaction(any()); |
| 364 | |
| 365 | return service; |
| 366 | } |
| 367 | |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 368 | /** |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 369 | * Overrided of {@link ActivityStack} that tracks test metrics, such as the number of times a |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 370 | * method is called. Note that its functionality depends on the implementations of the |
| 371 | * construction arguments. |
| 372 | */ |
| 373 | protected static class TestActivityStack<T extends StackWindowController> |
Wale Ogunwale | 7e1f5f5 | 2017-10-18 15:19:59 -0700 | [diff] [blame] | 374 | extends ActivityStack<T> { |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 375 | private int mOnActivityRemovedFromStackCount = 0; |
| 376 | private T mContainerController; |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 377 | |
Wale Ogunwale | 7e1f5f5 | 2017-10-18 15:19:59 -0700 | [diff] [blame] | 378 | static final int IS_TRANSLUCENT_UNSET = 0; |
| 379 | static final int IS_TRANSLUCENT_FALSE = 1; |
| 380 | static final int IS_TRANSLUCENT_TRUE = 2; |
| 381 | private int mIsTranslucent = IS_TRANSLUCENT_UNSET; |
| 382 | |
Wale Ogunwale | 9dcf946 | 2017-09-19 15:13:01 -0700 | [diff] [blame] | 383 | TestActivityStack(ActivityDisplay display, int stackId, ActivityStackSupervisor supervisor, |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 384 | int windowingMode, int activityType, boolean onTop) { |
| 385 | super(display, stackId, supervisor, windowingMode, activityType, onTop); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | @Override |
| 389 | void onActivityRemovedFromStack(ActivityRecord r) { |
| 390 | mOnActivityRemovedFromStackCount++; |
| 391 | super.onActivityRemovedFromStack(r); |
| 392 | } |
| 393 | |
| 394 | // Returns the number of times {@link #onActivityRemovedFromStack} has been called |
Wale Ogunwale | 7e1f5f5 | 2017-10-18 15:19:59 -0700 | [diff] [blame] | 395 | int onActivityRemovedFromStackInvocationCount() { |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 396 | return mOnActivityRemovedFromStackCount; |
| 397 | } |
| 398 | |
| 399 | @Override |
Wale Ogunwale | 034a8ec | 2017-09-02 17:14:40 -0700 | [diff] [blame] | 400 | protected T createStackWindowController(int displayId, boolean onTop, Rect outBounds) { |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 401 | mContainerController = (T) WindowTestUtils.createMockStackWindowContainerController(); |
| 402 | return mContainerController; |
| 403 | } |
| 404 | |
| 405 | @Override |
| 406 | T getWindowContainerController() { |
| 407 | return mContainerController; |
| 408 | } |
Wale Ogunwale | 7e1f5f5 | 2017-10-18 15:19:59 -0700 | [diff] [blame] | 409 | |
| 410 | void setIsTranslucent(boolean isTranslucent) { |
| 411 | mIsTranslucent = isTranslucent ? IS_TRANSLUCENT_TRUE : IS_TRANSLUCENT_FALSE; |
| 412 | } |
| 413 | |
| 414 | @Override |
Wale Ogunwale | 66e1685 | 2017-10-19 13:35:52 -0700 | [diff] [blame] | 415 | boolean isStackTranslucent(ActivityRecord starting) { |
Wale Ogunwale | 7e1f5f5 | 2017-10-18 15:19:59 -0700 | [diff] [blame] | 416 | switch (mIsTranslucent) { |
| 417 | case IS_TRANSLUCENT_TRUE: |
| 418 | return true; |
| 419 | case IS_TRANSLUCENT_FALSE: |
| 420 | return false; |
| 421 | case IS_TRANSLUCENT_UNSET: |
| 422 | default: |
Wale Ogunwale | 66e1685 | 2017-10-19 13:35:52 -0700 | [diff] [blame] | 423 | return super.isStackTranslucent(starting); |
Wale Ogunwale | 7e1f5f5 | 2017-10-18 15:19:59 -0700 | [diff] [blame] | 424 | } |
| 425 | } |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 426 | } |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 427 | } |