blob: 741901d2e5dc00e3703789006d3e24172800a732 [file] [log] [blame]
Bryce Leeaf691c02017-03-20 14:20:22 -07001/*
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
17package com.android.server.am;
18
Bryce Lee18d51592017-10-25 10:22:19 -070019import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD;
20import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -070021import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
Bryce Leef3c6a472017-11-14 14:53:06 -080022import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -070023import static android.view.Display.DEFAULT_DISPLAY;
Bryce Leeaf691c02017-03-20 14:20:22 -070024import static org.mockito.Mockito.mock;
Bryce Lee2a3cc462017-10-27 10:57:35 -070025import static org.mockito.Mockito.doNothing;
Bryce Lee04ab3462017-04-10 15:06:33 -070026import static org.mockito.Mockito.doReturn;
27import static org.mockito.Mockito.any;
Bryce Lee2a3cc462017-10-27 10:57:35 -070028import static org.mockito.Mockito.anyBoolean;
29import static org.mockito.Mockito.anyInt;
Bryce Lee04ab3462017-04-10 15:06:33 -070030import static org.mockito.Mockito.doAnswer;
Bryce Lee4e4a3ec2017-09-27 08:25:03 -070031import static org.mockito.Mockito.spy;
Bryce Lee04ab3462017-04-10 15:06:33 -070032
Winson Chung59a47ded2018-01-25 17:46:06 +000033import com.android.server.wm.DisplayWindowController;
Adrian Roos3150dbf2018-03-28 18:06:52 +020034
35import org.junit.Rule;
Bryce Lee04ab3462017-04-10 15:06:33 -070036import org.mockito.invocation.InvocationOnMock;
Bryce Leeaf691c02017-03-20 14:20:22 -070037
Bryce Lee0bd8d422018-01-09 09:45:57 -080038import android.app.IApplicationThread;
Bryce Leeaf691c02017-03-20 14:20:22 -070039import android.content.ComponentName;
40import android.content.Context;
41import android.content.Intent;
42import android.content.pm.ActivityInfo;
43import android.content.pm.ApplicationInfo;
Bryce Leeba8f4422017-11-20 12:35:57 -080044import android.content.pm.IPackageManager;
Bryce Leeaf691c02017-03-20 14:20:22 -070045import android.content.res.Configuration;
46import android.graphics.Rect;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -070047import android.hardware.display.DisplayManager;
Bryce Lee3115bdf2017-04-05 08:39:40 -070048import android.os.HandlerThread;
Bryce Leeaf691c02017-03-20 14:20:22 -070049import android.os.Looper;
Bryce Lee93e7f792017-10-25 15:54:55 -070050import android.service.voice.IVoiceInteractionSession;
Bryce Leeaf691c02017-03-20 14:20:22 -070051import android.support.test.InstrumentationRegistry;
Adrian Roos3150dbf2018-03-28 18:06:52 +020052import android.testing.DexmakerShareClassLoaderRule;
53
Bryce Leeaf691c02017-03-20 14:20:22 -070054import com.android.server.AttributeCache;
55import com.android.server.wm.AppWindowContainerController;
Bryce Lee4e4a3ec2017-09-27 08:25:03 -070056import com.android.server.wm.PinnedStackWindowController;
Bryce Leeaf691c02017-03-20 14:20:22 -070057import com.android.server.wm.StackWindowController;
Bryce Lee04ab3462017-04-10 15:06:33 -070058import com.android.server.wm.TaskWindowContainerController;
Bryce Leeaf691c02017-03-20 14:20:22 -070059import com.android.server.wm.WindowManagerService;
60import com.android.server.wm.WindowTestUtils;
Bryce Lee3115bdf2017-04-05 08:39:40 -070061import org.junit.After;
Bryce Leeaf691c02017-03-20 14:20:22 -070062import org.junit.Before;
63import org.mockito.MockitoAnnotations;
64
65/**
66 * A base class to handle common operations in activity related unit tests.
67 */
68public class ActivityTestsBase {
Bryce Lee939a9a32017-10-23 10:01:21 -070069 private static boolean sOneTimeSetupDone = false;
70
Adrian Roos3150dbf2018-03-28 18:06:52 +020071 @Rule
72 public final DexmakerShareClassLoaderRule mDexmakerShareClassLoaderRule =
73 new DexmakerShareClassLoaderRule();
74
Bryce Leeaf691c02017-03-20 14:20:22 -070075 private final Context mContext = InstrumentationRegistry.getContext();
Bryce Lee3115bdf2017-04-05 08:39:40 -070076 private HandlerThread mHandlerThread;
Bryce Leeaf691c02017-03-20 14:20:22 -070077
Bryce Leefbd263b42018-03-07 10:33:55 -080078 // Default package name
79 static final String DEFAULT_COMPONENT_PACKAGE_NAME = "com.foo";
80
81 // Default base activity name
82 private static final String DEFAULT_COMPONENT_CLASS_NAME = ".BarActivity";
83
Bryce Leeaf691c02017-03-20 14:20:22 -070084 @Before
85 public void setUp() throws Exception {
Bryce Lee939a9a32017-10-23 10:01:21 -070086 if (!sOneTimeSetupDone) {
87 sOneTimeSetupDone = true;
Bryce Lee939a9a32017-10-23 10:01:21 -070088 MockitoAnnotations.initMocks(this);
89 }
Bryce Lee3115bdf2017-04-05 08:39:40 -070090 mHandlerThread = new HandlerThread("ActivityTestsBaseThread");
91 mHandlerThread.start();
92 }
Bryce Leeaf691c02017-03-20 14:20:22 -070093
Bryce Lee3115bdf2017-04-05 08:39:40 -070094 @After
95 public void tearDown() {
96 mHandlerThread.quitSafely();
Bryce Leeaf691c02017-03-20 14:20:22 -070097 }
98
99 protected ActivityManagerService createActivityManagerService() {
Bryce Lee93e7f792017-10-25 15:54:55 -0700100 final ActivityManagerService service =
101 setupActivityManagerService(new TestActivityManagerService(mContext));
102 AttributeCache.init(mContext);
103 return service;
Winson Chung3f0e59a2017-10-25 10:19:05 -0700104 }
105
106 protected ActivityManagerService setupActivityManagerService(ActivityManagerService service) {
107 service = spy(service);
Bryce Leeba8f4422017-11-20 12:35:57 -0800108 doReturn(mock(IPackageManager.class)).when(service).getPackageManager();
Bryce Leead5b8322018-03-08 14:28:52 -0800109 doNothing().when(service).grantEphemeralAccessLocked(anyInt(), any(), anyInt(), anyInt());
Bryce Lee4e4a3ec2017-09-27 08:25:03 -0700110 service.mWindowManager = prepareMockWindowManager();
Bryce Lee840c5662017-04-13 10:02:51 -0700111 return service;
Bryce Leeaf691c02017-03-20 14:20:22 -0700112 }
113
Bryce Lee18d51592017-10-25 10:22:19 -0700114 /**
115 * Builder for creating new activities.
116 */
117 protected static class ActivityBuilder {
118 // An id appended to the end of the component name to make it unique
119 private static int sCurrentActivityId = 0;
Bryce Lee9f6affd2017-09-01 09:18:35 -0700120
Bryce Leeaf691c02017-03-20 14:20:22 -0700121
Bryce Lee18d51592017-10-25 10:22:19 -0700122
123 private final ActivityManagerService mService;
124
125 private ComponentName mComponent;
126 private TaskRecord mTaskRecord;
127 private int mUid;
128 private boolean mCreateTask;
129 private ActivityStack mStack;
130
131 ActivityBuilder(ActivityManagerService service) {
132 mService = service;
Bryce Leeaf691c02017-03-20 14:20:22 -0700133 }
134
Bryce Lee18d51592017-10-25 10:22:19 -0700135 ActivityBuilder setComponent(ComponentName component) {
136 mComponent = component;
137 return this;
138 }
139
Bryce Leead5b8322018-03-08 14:28:52 -0800140 static ComponentName getDefaultComponent() {
141 return ComponentName.createRelative(DEFAULT_COMPONENT_PACKAGE_NAME,
142 DEFAULT_COMPONENT_PACKAGE_NAME);
143 }
144
Bryce Lee18d51592017-10-25 10:22:19 -0700145 ActivityBuilder setTask(TaskRecord task) {
146 mTaskRecord = task;
147 return this;
148 }
149
150 ActivityBuilder setStack(ActivityStack stack) {
151 mStack = stack;
152 return this;
153 }
154
155 ActivityBuilder setCreateTask(boolean createTask) {
156 mCreateTask = createTask;
157 return this;
158 }
159
160 ActivityBuilder setUid(int uid) {
161 mUid = uid;
162 return this;
163 }
164
165 ActivityRecord build() {
166 if (mComponent == null) {
167 final int id = sCurrentActivityId++;
Bryce Leefbd263b42018-03-07 10:33:55 -0800168 mComponent = ComponentName.createRelative(DEFAULT_COMPONENT_PACKAGE_NAME,
169 DEFAULT_COMPONENT_CLASS_NAME + id);
Bryce Lee18d51592017-10-25 10:22:19 -0700170 }
171
172 if (mCreateTask) {
173 mTaskRecord = new TaskBuilder(mService.mStackSupervisor)
174 .setComponent(mComponent)
175 .setStack(mStack).build();
176 }
177
178 Intent intent = new Intent();
179 intent.setComponent(mComponent);
180 final ActivityInfo aInfo = new ActivityInfo();
181 aInfo.applicationInfo = new ApplicationInfo();
182 aInfo.applicationInfo.packageName = mComponent.getPackageName();
183 aInfo.applicationInfo.uid = mUid;
Bryce Lee18d51592017-10-25 10:22:19 -0700184 final ActivityRecord activity = new ActivityRecord(mService, null /* caller */,
185 0 /* launchedFromPid */, 0, null, intent, null,
186 aInfo /*aInfo*/, new Configuration(), null /* resultTo */, null /* resultWho */,
187 0 /* reqCode */, false /*componentSpecified*/, false /* rootVoiceInteraction */,
188 mService.mStackSupervisor, null /* options */, null /* sourceRecord */);
189 activity.mWindowContainerController = mock(AppWindowContainerController.class);
190
191 if (mTaskRecord != null) {
192 mTaskRecord.addActivityToTop(activity);
193 }
194
Dianne Hackborne9d9b4b2018-03-28 13:51:46 -0700195 activity.setProcess(new ProcessRecord(null, null,
196 mService.mContext.getApplicationInfo(), "name", 12345));
Bryce Lee0bd8d422018-01-09 09:45:57 -0800197 activity.app.thread = mock(IApplicationThread.class);
198
Bryce Lee18d51592017-10-25 10:22:19 -0700199 return activity;
200 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700201 }
202
Bryce Lee18d51592017-10-25 10:22:19 -0700203 /**
204 * Builder for creating new tasks.
205 */
206 protected static class TaskBuilder {
Bryce Leefbd263b42018-03-07 10:33:55 -0800207 // Default package name
208 static final String DEFAULT_PACKAGE = "com.bar";
209
Bryce Lee18d51592017-10-25 10:22:19 -0700210 private final ActivityStackSupervisor mSupervisor;
Winson Chung1dbc8112017-09-28 18:05:31 -0700211
Bryce Lee18d51592017-10-25 10:22:19 -0700212 private ComponentName mComponent;
213 private String mPackage;
214 private int mFlags = 0;
215 private int mTaskId = 0;
Winson Chung0ec2a352017-10-26 11:38:30 -0700216 private int mUserId = 0;
Bryce Lee93e7f792017-10-25 15:54:55 -0700217 private IVoiceInteractionSession mVoiceSession;
Bryce Leeaf691c02017-03-20 14:20:22 -0700218
Bryce Lee18d51592017-10-25 10:22:19 -0700219 private ActivityStack mStack;
Bryce Leeaf691c02017-03-20 14:20:22 -0700220
Bryce Lee18d51592017-10-25 10:22:19 -0700221 TaskBuilder(ActivityStackSupervisor supervisor) {
222 mSupervisor = supervisor;
223 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700224
Bryce Lee18d51592017-10-25 10:22:19 -0700225 TaskBuilder setComponent(ComponentName component) {
226 mComponent = component;
227 return this;
228 }
229
230 TaskBuilder setPackage(String packageName) {
231 mPackage = packageName;
232 return this;
233 }
234
Bryce Lee93e7f792017-10-25 15:54:55 -0700235 TaskBuilder setVoiceSession(IVoiceInteractionSession session) {
236 mVoiceSession = session;
237 return this;
238 }
239
Bryce Lee18d51592017-10-25 10:22:19 -0700240 TaskBuilder setFlags(int flags) {
241 mFlags = flags;
242 return this;
243 }
244
245 TaskBuilder setTaskId(int taskId) {
246 mTaskId = taskId;
247 return this;
248 }
249
Winson Chung0ec2a352017-10-26 11:38:30 -0700250 TaskBuilder setUserId(int userId) {
251 mUserId = userId;
252 return this;
253 }
254
Bryce Lee18d51592017-10-25 10:22:19 -0700255 TaskBuilder setStack(ActivityStack stack) {
256 mStack = stack;
257 return this;
258 }
259
260 TaskRecord build() {
261 if (mStack == null) {
262 mStack = mSupervisor.getDefaultDisplay().createStack(
263 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, true /* onTop */);
264 }
265
266 final ActivityInfo aInfo = new ActivityInfo();
267 aInfo.applicationInfo = new ApplicationInfo();
268 aInfo.applicationInfo.packageName = mPackage;
269
270 Intent intent = new Intent();
Bryce Leefbd263b42018-03-07 10:33:55 -0800271 if (mComponent == null) {
272 mComponent = ComponentName.createRelative(DEFAULT_COMPONENT_PACKAGE_NAME,
273 DEFAULT_COMPONENT_CLASS_NAME);
274 }
275
Bryce Lee18d51592017-10-25 10:22:19 -0700276 intent.setComponent(mComponent);
277 intent.setFlags(mFlags);
278
279 final TaskRecord task = new TaskRecord(mSupervisor.mService, mTaskId, aInfo,
Bryce Lee93e7f792017-10-25 15:54:55 -0700280 intent /*intent*/, mVoiceSession, null /*_voiceInteractor*/);
Winson Chung0ec2a352017-10-26 11:38:30 -0700281 task.userId = mUserId;
Bryce Lee18d51592017-10-25 10:22:19 -0700282 mSupervisor.setFocusStackUnchecked("test", mStack);
283 mStack.addTask(task, true, "creating test task");
284 task.setStack(mStack);
285 task.setWindowContainerController(mock(TaskWindowContainerController.class));
Winson Chung0ec2a352017-10-26 11:38:30 -0700286 task.touchActiveTime();
Bryce Lee18d51592017-10-25 10:22:19 -0700287
288 return task;
289 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700290 }
291
292 /**
293 * An {@link ActivityManagerService} subclass which provides a test
294 * {@link ActivityStackSupervisor}.
295 */
296 protected static class TestActivityManagerService extends ActivityManagerService {
Bryce Leeb0f993f2018-03-02 15:38:01 -0800297 private ClientLifecycleManager mLifecycleManager;
298
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700299 TestActivityManagerService(Context context) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700300 super(context);
Bryce Lee04ab3462017-04-10 15:06:33 -0700301 mSupportsMultiWindow = true;
302 mSupportsMultiDisplay = true;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700303 mSupportsSplitScreenMultiWindow = true;
304 mSupportsFreeformWindowManagement = true;
305 mSupportsPictureInPicture = true;
Bryce Lee6a0754a2017-10-10 17:53:50 -0700306 mWindowManager = WindowTestUtils.getMockWindowManagerService();
Bryce Leeaf691c02017-03-20 14:20:22 -0700307 }
308
309 @Override
Bryce Leeb0f993f2018-03-02 15:38:01 -0800310 public ClientLifecycleManager getLifecycleManager() {
311 if (mLifecycleManager == null) {
312 return super.getLifecycleManager();
313 }
314 return mLifecycleManager;
315 }
316
317 void setLifecycleManager(ClientLifecycleManager manager) {
318 mLifecycleManager = manager;
319 }
320
321 @Override
Bryce Lee2a3cc462017-10-27 10:57:35 -0700322 final protected ActivityStackSupervisor createStackSupervisor() {
323 final ActivityStackSupervisor supervisor = spy(createTestSupervisor());
Bryce Lee271617a2018-03-15 10:39:12 -0700324 final KeyguardController keyguardController = mock(KeyguardController.class);
Bryce Lee2a3cc462017-10-27 10:57:35 -0700325
326 // No home stack is set.
327 doNothing().when(supervisor).moveHomeStackToFront(any());
328 doReturn(true).when(supervisor).moveHomeStackTaskToTop(any());
329 // Invoked during {@link ActivityStack} creation.
330 doNothing().when(supervisor).updateUIDsPresentOnDisplay();
331 // Always keep things awake.
332 doReturn(true).when(supervisor).hasAwakeDisplay();
333 // Called when moving activity to pinned stack.
334 doNothing().when(supervisor).ensureActivitiesVisibleLocked(any(), anyInt(), anyBoolean());
335 // Do not schedule idle timeouts
336 doNothing().when(supervisor).scheduleIdleTimeoutLocked(any());
Bryce Leefbd263b42018-03-07 10:33:55 -0800337 // unit test version does not handle launch wake lock
338 doNothing().when(supervisor).acquireLaunchWakelock();
Bryce Lee271617a2018-03-15 10:39:12 -0700339 doReturn(keyguardController).when(supervisor).getKeyguardController();
Bryce Lee2a3cc462017-10-27 10:57:35 -0700340
341 supervisor.initialize();
342
343 return supervisor;
344 }
345
346 protected ActivityStackSupervisor createTestSupervisor() {
Bryce Lee3115bdf2017-04-05 08:39:40 -0700347 return new TestActivityStackSupervisor(this, mHandlerThread.getLooper());
Bryce Leeaf691c02017-03-20 14:20:22 -0700348 }
Bryce Lee29a649d2017-08-18 13:52:31 -0700349
350 @Override
351 void updateUsageStats(ActivityRecord component, boolean resumed) {
352 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700353 }
354
355 /**
356 * An {@link ActivityStackSupervisor} which stubs out certain methods that depend on
357 * setup not available in the test environment. Also specifies an injector for
358 */
359 protected static class TestActivityStackSupervisor extends ActivityStackSupervisor {
Bryce Lee2a3cc462017-10-27 10:57:35 -0700360 private ActivityDisplay mDisplay;
Bryce Lee459c0622018-03-19 11:04:01 -0700361 private KeyguardController mKeyguardController;
Bryce Lee943ebe72017-05-04 10:19:07 -0700362
Bryce Leeaf691c02017-03-20 14:20:22 -0700363 public TestActivityStackSupervisor(ActivityManagerService service, Looper looper) {
364 super(service, looper);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700365 mDisplayManager =
366 (DisplayManager) mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
Bryce Lee04ab3462017-04-10 15:06:33 -0700367 mWindowManager = prepareMockWindowManager();
Bryce Lee459c0622018-03-19 11:04:01 -0700368 mKeyguardController = mock(KeyguardController.class);
Bryce Lee2a3cc462017-10-27 10:57:35 -0700369 }
370
371 @Override
372 public void initialize() {
373 super.initialize();
Bryce Lee459c0622018-03-19 11:04:01 -0700374 mDisplay = spy(new TestActivityDisplay(this, DEFAULT_DISPLAY));
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700375 attachDisplay(mDisplay);
Bryce Lee04ab3462017-04-10 15:06:33 -0700376 }
377
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700378 @Override
Bryce Lee459c0622018-03-19 11:04:01 -0700379 public KeyguardController getKeyguardController() {
380 return mKeyguardController;
381 }
382
383 @Override
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700384 ActivityDisplay getDefaultDisplay() {
385 return mDisplay;
386 }
387
Bryce Lee2a3cc462017-10-27 10:57:35 -0700388 // Just return the current front task. This is called internally so we cannot use spy to mock this out.
Bryce Lee04ab3462017-04-10 15:06:33 -0700389 @Override
Wale Ogunwalee1f68ce2018-03-09 08:58:54 -0800390 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus,
391 boolean ignoreCurrent) {
Bryce Lee04ab3462017-04-10 15:06:33 -0700392 return mFocusedStack;
393 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700394 }
395
Winson Chung59a47ded2018-01-25 17:46:06 +0000396 protected static class TestActivityDisplay extends ActivityDisplay {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700397
398 private final ActivityStackSupervisor mSupervisor;
399 TestActivityDisplay(ActivityStackSupervisor supervisor, int displayId) {
400 super(supervisor, displayId);
401 mSupervisor = supervisor;
Andrii Kulianb1cdb102017-07-13 15:33:06 -0700402 }
403
404 @Override
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700405 <T extends ActivityStack> T createStackUnchecked(int windowingMode, int activityType,
406 int stackId, boolean onTop) {
407 if (windowingMode == WINDOWING_MODE_PINNED) {
408 return (T) new PinnedActivityStack(this, stackId, mSupervisor, onTop) {
Andrii Kulianb1cdb102017-07-13 15:33:06 -0700409 @Override
410 Rect getDefaultPictureInPictureBounds(float aspectRatio) {
411 return new Rect(50, 50, 100, 100);
412 }
Bryce Lee4e4a3ec2017-09-27 08:25:03 -0700413
414 @Override
415 PinnedStackWindowController createStackWindowController(int displayId,
416 boolean onTop, Rect outBounds) {
417 return mock(PinnedStackWindowController.class);
418 }
Andrii Kulianb1cdb102017-07-13 15:33:06 -0700419 };
420 } else {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700421 return (T) new TestActivityStack(
422 this, stackId, mSupervisor, windowingMode, activityType, onTop);
Andrii Kulianb1cdb102017-07-13 15:33:06 -0700423 }
Bryce Lee04ab3462017-04-10 15:06:33 -0700424 }
Winson Chung59a47ded2018-01-25 17:46:06 +0000425
426 @Override
427 protected DisplayWindowController createWindowContainerController() {
428 return mock(DisplayWindowController.class);
429 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700430 }
431
Bryce Lee04ab3462017-04-10 15:06:33 -0700432 private static WindowManagerService prepareMockWindowManager() {
Bryce Lee4e4a3ec2017-09-27 08:25:03 -0700433 final WindowManagerService service = WindowTestUtils.getMockWindowManagerService();
Bryce Lee04ab3462017-04-10 15:06:33 -0700434
435 doAnswer((InvocationOnMock invocationOnMock) -> {
436 final Runnable runnable = invocationOnMock.<Runnable>getArgument(0);
437 if (runnable != null) {
438 runnable.run();
439 }
440 return null;
441 }).when(service).inSurfaceTransaction(any());
442
443 return service;
444 }
445
Bryce Leeaf691c02017-03-20 14:20:22 -0700446 /**
Bryce Lee4e4a3ec2017-09-27 08:25:03 -0700447 * Overrided of {@link ActivityStack} that tracks test metrics, such as the number of times a
Bryce Leeaf691c02017-03-20 14:20:22 -0700448 * method is called. Note that its functionality depends on the implementations of the
449 * construction arguments.
450 */
451 protected static class TestActivityStack<T extends StackWindowController>
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700452 extends ActivityStack<T> {
Bryce Leeaf691c02017-03-20 14:20:22 -0700453 private int mOnActivityRemovedFromStackCount = 0;
454 private T mContainerController;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700455
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700456 static final int IS_TRANSLUCENT_UNSET = 0;
457 static final int IS_TRANSLUCENT_FALSE = 1;
458 static final int IS_TRANSLUCENT_TRUE = 2;
459 private int mIsTranslucent = IS_TRANSLUCENT_UNSET;
460
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800461 static final int SUPPORTS_SPLIT_SCREEN_UNSET = 0;
462 static final int SUPPORTS_SPLIT_SCREEN_FALSE = 1;
463 static final int SUPPORTS_SPLIT_SCREEN_TRUE = 2;
464 private int mSupportsSplitScreen = SUPPORTS_SPLIT_SCREEN_UNSET;
465
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700466 TestActivityStack(ActivityDisplay display, int stackId, ActivityStackSupervisor supervisor,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700467 int windowingMode, int activityType, boolean onTop) {
468 super(display, stackId, supervisor, windowingMode, activityType, onTop);
Bryce Leeaf691c02017-03-20 14:20:22 -0700469 }
470
471 @Override
472 void onActivityRemovedFromStack(ActivityRecord r) {
473 mOnActivityRemovedFromStackCount++;
474 super.onActivityRemovedFromStack(r);
475 }
476
477 // Returns the number of times {@link #onActivityRemovedFromStack} has been called
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700478 int onActivityRemovedFromStackInvocationCount() {
Bryce Leeaf691c02017-03-20 14:20:22 -0700479 return mOnActivityRemovedFromStackCount;
480 }
481
482 @Override
Wale Ogunwale034a8ec2017-09-02 17:14:40 -0700483 protected T createStackWindowController(int displayId, boolean onTop, Rect outBounds) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700484 mContainerController = (T) WindowTestUtils.createMockStackWindowContainerController();
Bryce Leef3c6a472017-11-14 14:53:06 -0800485
486 // Primary pinned stacks require a non-empty out bounds to be set or else all tasks
487 // will be moved to the full screen stack.
488 if (getWindowingMode() == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
489 outBounds.set(0, 0, 100, 100);
490 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700491 return mContainerController;
492 }
493
494 @Override
495 T getWindowContainerController() {
496 return mContainerController;
497 }
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700498
499 void setIsTranslucent(boolean isTranslucent) {
500 mIsTranslucent = isTranslucent ? IS_TRANSLUCENT_TRUE : IS_TRANSLUCENT_FALSE;
501 }
502
503 @Override
Wale Ogunwale66e16852017-10-19 13:35:52 -0700504 boolean isStackTranslucent(ActivityRecord starting) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700505 switch (mIsTranslucent) {
506 case IS_TRANSLUCENT_TRUE:
507 return true;
508 case IS_TRANSLUCENT_FALSE:
509 return false;
510 case IS_TRANSLUCENT_UNSET:
511 default:
Wale Ogunwale66e16852017-10-19 13:35:52 -0700512 return super.isStackTranslucent(starting);
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700513 }
514 }
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800515
516 void setSupportsSplitScreen(boolean supportsSplitScreen) {
517 mSupportsSplitScreen = supportsSplitScreen
518 ? SUPPORTS_SPLIT_SCREEN_TRUE : SUPPORTS_SPLIT_SCREEN_FALSE;
519 }
520
521 @Override
522 public boolean supportsSplitScreenWindowingMode() {
523 switch (mSupportsSplitScreen) {
524 case SUPPORTS_SPLIT_SCREEN_TRUE:
525 return true;
526 case SUPPORTS_SPLIT_SCREEN_FALSE:
527 return false;
528 case SUPPORTS_SPLIT_SCREEN_UNSET:
529 default:
530 return super.supportsSplitScreenWindowingMode();
531 }
532 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700533 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700534}