blob: f5e61a1db341bb4039beb0b9e90c344a75fc3398 [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
Bryce Lee2b8e0372018-04-05 17:01:37 -070033import android.app.ActivityOptions;
Winson Chung59a47ded2018-01-25 17:46:06 +000034import com.android.server.wm.DisplayWindowController;
Adrian Roos3150dbf2018-03-28 18:06:52 +020035
36import org.junit.Rule;
Bryce Lee04ab3462017-04-10 15:06:33 -070037import org.mockito.invocation.InvocationOnMock;
Bryce Leeaf691c02017-03-20 14:20:22 -070038
Bryce Lee0bd8d422018-01-09 09:45:57 -080039import android.app.IApplicationThread;
Bryce Leeaf691c02017-03-20 14:20:22 -070040import android.content.ComponentName;
41import android.content.Context;
42import android.content.Intent;
43import android.content.pm.ActivityInfo;
44import android.content.pm.ApplicationInfo;
Bryce Leeba8f4422017-11-20 12:35:57 -080045import android.content.pm.IPackageManager;
Bryce Leeaf691c02017-03-20 14:20:22 -070046import android.content.res.Configuration;
47import android.graphics.Rect;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -070048import android.hardware.display.DisplayManager;
Bryce Lee3115bdf2017-04-05 08:39:40 -070049import android.os.HandlerThread;
Bryce Leeaf691c02017-03-20 14:20:22 -070050import android.os.Looper;
Bryce Lee93e7f792017-10-25 15:54:55 -070051import android.service.voice.IVoiceInteractionSession;
Bryce Leeaf691c02017-03-20 14:20:22 -070052import android.support.test.InstrumentationRegistry;
Adrian Roos3150dbf2018-03-28 18:06:52 +020053import android.testing.DexmakerShareClassLoaderRule;
54
Bryce Lee2b8e0372018-04-05 17:01:37 -070055
56import com.android.internal.app.IVoiceInteractor;
57
Bryce Leeaf691c02017-03-20 14:20:22 -070058import com.android.server.AttributeCache;
59import com.android.server.wm.AppWindowContainerController;
Bryce Lee4e4a3ec2017-09-27 08:25:03 -070060import com.android.server.wm.PinnedStackWindowController;
Bryce Leeaf691c02017-03-20 14:20:22 -070061import com.android.server.wm.StackWindowController;
Bryce Lee04ab3462017-04-10 15:06:33 -070062import com.android.server.wm.TaskWindowContainerController;
Bryce Leeaf691c02017-03-20 14:20:22 -070063import com.android.server.wm.WindowManagerService;
64import com.android.server.wm.WindowTestUtils;
Bryce Lee3115bdf2017-04-05 08:39:40 -070065import org.junit.After;
Bryce Leeaf691c02017-03-20 14:20:22 -070066import org.junit.Before;
67import org.mockito.MockitoAnnotations;
68
Bryce Lee2b8e0372018-04-05 17:01:37 -070069
Bryce Leeaf691c02017-03-20 14:20:22 -070070/**
71 * A base class to handle common operations in activity related unit tests.
72 */
73public class ActivityTestsBase {
Bryce Lee939a9a32017-10-23 10:01:21 -070074 private static boolean sOneTimeSetupDone = false;
75
Adrian Roos3150dbf2018-03-28 18:06:52 +020076 @Rule
77 public final DexmakerShareClassLoaderRule mDexmakerShareClassLoaderRule =
78 new DexmakerShareClassLoaderRule();
79
Bryce Leeaf691c02017-03-20 14:20:22 -070080 private final Context mContext = InstrumentationRegistry.getContext();
Bryce Lee3115bdf2017-04-05 08:39:40 -070081 private HandlerThread mHandlerThread;
Bryce Leeaf691c02017-03-20 14:20:22 -070082
Bryce Leefbd263b42018-03-07 10:33:55 -080083 // Default package name
84 static final String DEFAULT_COMPONENT_PACKAGE_NAME = "com.foo";
85
86 // Default base activity name
87 private static final String DEFAULT_COMPONENT_CLASS_NAME = ".BarActivity";
88
Bryce Leeaf691c02017-03-20 14:20:22 -070089 @Before
90 public void setUp() throws Exception {
Bryce Lee939a9a32017-10-23 10:01:21 -070091 if (!sOneTimeSetupDone) {
92 sOneTimeSetupDone = true;
Bryce Lee939a9a32017-10-23 10:01:21 -070093 MockitoAnnotations.initMocks(this);
94 }
Bryce Lee3115bdf2017-04-05 08:39:40 -070095 mHandlerThread = new HandlerThread("ActivityTestsBaseThread");
96 mHandlerThread.start();
97 }
Bryce Leeaf691c02017-03-20 14:20:22 -070098
Bryce Lee3115bdf2017-04-05 08:39:40 -070099 @After
100 public void tearDown() {
101 mHandlerThread.quitSafely();
Bryce Leeaf691c02017-03-20 14:20:22 -0700102 }
103
104 protected ActivityManagerService createActivityManagerService() {
Bryce Lee93e7f792017-10-25 15:54:55 -0700105 final ActivityManagerService service =
106 setupActivityManagerService(new TestActivityManagerService(mContext));
107 AttributeCache.init(mContext);
108 return service;
Winson Chung3f0e59a2017-10-25 10:19:05 -0700109 }
110
111 protected ActivityManagerService setupActivityManagerService(ActivityManagerService service) {
112 service = spy(service);
Bryce Leeba8f4422017-11-20 12:35:57 -0800113 doReturn(mock(IPackageManager.class)).when(service).getPackageManager();
Bryce Leead5b8322018-03-08 14:28:52 -0800114 doNothing().when(service).grantEphemeralAccessLocked(anyInt(), any(), anyInt(), anyInt());
Bryce Lee4e4a3ec2017-09-27 08:25:03 -0700115 service.mWindowManager = prepareMockWindowManager();
Bryce Lee840c5662017-04-13 10:02:51 -0700116 return service;
Bryce Leeaf691c02017-03-20 14:20:22 -0700117 }
118
Bryce Lee18d51592017-10-25 10:22:19 -0700119 /**
120 * Builder for creating new activities.
121 */
122 protected static class ActivityBuilder {
123 // An id appended to the end of the component name to make it unique
124 private static int sCurrentActivityId = 0;
Bryce Lee9f6affd2017-09-01 09:18:35 -0700125
Bryce Leeaf691c02017-03-20 14:20:22 -0700126
Bryce Lee18d51592017-10-25 10:22:19 -0700127
128 private final ActivityManagerService mService;
129
130 private ComponentName mComponent;
131 private TaskRecord mTaskRecord;
132 private int mUid;
133 private boolean mCreateTask;
134 private ActivityStack mStack;
135
136 ActivityBuilder(ActivityManagerService service) {
137 mService = service;
Bryce Leeaf691c02017-03-20 14:20:22 -0700138 }
139
Bryce Lee18d51592017-10-25 10:22:19 -0700140 ActivityBuilder setComponent(ComponentName component) {
141 mComponent = component;
142 return this;
143 }
144
Bryce Leead5b8322018-03-08 14:28:52 -0800145 static ComponentName getDefaultComponent() {
146 return ComponentName.createRelative(DEFAULT_COMPONENT_PACKAGE_NAME,
147 DEFAULT_COMPONENT_PACKAGE_NAME);
148 }
149
Bryce Lee18d51592017-10-25 10:22:19 -0700150 ActivityBuilder setTask(TaskRecord task) {
151 mTaskRecord = task;
152 return this;
153 }
154
155 ActivityBuilder setStack(ActivityStack stack) {
156 mStack = stack;
157 return this;
158 }
159
160 ActivityBuilder setCreateTask(boolean createTask) {
161 mCreateTask = createTask;
162 return this;
163 }
164
165 ActivityBuilder setUid(int uid) {
166 mUid = uid;
167 return this;
168 }
169
170 ActivityRecord build() {
171 if (mComponent == null) {
172 final int id = sCurrentActivityId++;
Bryce Leefbd263b42018-03-07 10:33:55 -0800173 mComponent = ComponentName.createRelative(DEFAULT_COMPONENT_PACKAGE_NAME,
174 DEFAULT_COMPONENT_CLASS_NAME + id);
Bryce Lee18d51592017-10-25 10:22:19 -0700175 }
176
177 if (mCreateTask) {
178 mTaskRecord = new TaskBuilder(mService.mStackSupervisor)
179 .setComponent(mComponent)
180 .setStack(mStack).build();
181 }
182
183 Intent intent = new Intent();
184 intent.setComponent(mComponent);
185 final ActivityInfo aInfo = new ActivityInfo();
186 aInfo.applicationInfo = new ApplicationInfo();
187 aInfo.applicationInfo.packageName = mComponent.getPackageName();
188 aInfo.applicationInfo.uid = mUid;
Bryce Lee18d51592017-10-25 10:22:19 -0700189 final ActivityRecord activity = new ActivityRecord(mService, null /* caller */,
190 0 /* launchedFromPid */, 0, null, intent, null,
191 aInfo /*aInfo*/, new Configuration(), null /* resultTo */, null /* resultWho */,
192 0 /* reqCode */, false /*componentSpecified*/, false /* rootVoiceInteraction */,
193 mService.mStackSupervisor, null /* options */, null /* sourceRecord */);
194 activity.mWindowContainerController = mock(AppWindowContainerController.class);
195
196 if (mTaskRecord != null) {
197 mTaskRecord.addActivityToTop(activity);
198 }
199
Dianne Hackborne9d9b4b2018-03-28 13:51:46 -0700200 activity.setProcess(new ProcessRecord(null, null,
201 mService.mContext.getApplicationInfo(), "name", 12345));
Bryce Lee0bd8d422018-01-09 09:45:57 -0800202 activity.app.thread = mock(IApplicationThread.class);
203
Bryce Lee18d51592017-10-25 10:22:19 -0700204 return activity;
205 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700206 }
207
Bryce Lee18d51592017-10-25 10:22:19 -0700208 /**
209 * Builder for creating new tasks.
210 */
211 protected static class TaskBuilder {
Bryce Leefbd263b42018-03-07 10:33:55 -0800212 // Default package name
213 static final String DEFAULT_PACKAGE = "com.bar";
214
Bryce Lee18d51592017-10-25 10:22:19 -0700215 private final ActivityStackSupervisor mSupervisor;
Winson Chung1dbc8112017-09-28 18:05:31 -0700216
Bryce Lee18d51592017-10-25 10:22:19 -0700217 private ComponentName mComponent;
218 private String mPackage;
219 private int mFlags = 0;
220 private int mTaskId = 0;
Winson Chung0ec2a352017-10-26 11:38:30 -0700221 private int mUserId = 0;
Bryce Lee93e7f792017-10-25 15:54:55 -0700222 private IVoiceInteractionSession mVoiceSession;
Bryce Lee2b8e0372018-04-05 17:01:37 -0700223 private boolean mCreateStack = true;
Bryce Leeaf691c02017-03-20 14:20:22 -0700224
Bryce Lee18d51592017-10-25 10:22:19 -0700225 private ActivityStack mStack;
Bryce Leeaf691c02017-03-20 14:20:22 -0700226
Bryce Lee18d51592017-10-25 10:22:19 -0700227 TaskBuilder(ActivityStackSupervisor supervisor) {
228 mSupervisor = supervisor;
229 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700230
Bryce Lee18d51592017-10-25 10:22:19 -0700231 TaskBuilder setComponent(ComponentName component) {
232 mComponent = component;
233 return this;
234 }
235
236 TaskBuilder setPackage(String packageName) {
237 mPackage = packageName;
238 return this;
239 }
240
Bryce Lee2b8e0372018-04-05 17:01:37 -0700241 /**
242 * Set to {@code true} by default, set to {@code false} to prevent the task from
243 * automatically creating a parent stack.
244 */
245 TaskBuilder setCreateStack(boolean createStack) {
246 mCreateStack = createStack;
247 return this;
248 }
249
Bryce Lee93e7f792017-10-25 15:54:55 -0700250 TaskBuilder setVoiceSession(IVoiceInteractionSession session) {
251 mVoiceSession = session;
252 return this;
253 }
254
Bryce Lee18d51592017-10-25 10:22:19 -0700255 TaskBuilder setFlags(int flags) {
256 mFlags = flags;
257 return this;
258 }
259
260 TaskBuilder setTaskId(int taskId) {
261 mTaskId = taskId;
262 return this;
263 }
264
Winson Chung0ec2a352017-10-26 11:38:30 -0700265 TaskBuilder setUserId(int userId) {
266 mUserId = userId;
267 return this;
268 }
269
Bryce Lee18d51592017-10-25 10:22:19 -0700270 TaskBuilder setStack(ActivityStack stack) {
271 mStack = stack;
272 return this;
273 }
274
275 TaskRecord build() {
Bryce Lee2b8e0372018-04-05 17:01:37 -0700276 if (mStack == null && mCreateStack) {
Bryce Lee18d51592017-10-25 10:22:19 -0700277 mStack = mSupervisor.getDefaultDisplay().createStack(
278 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, true /* onTop */);
279 }
280
281 final ActivityInfo aInfo = new ActivityInfo();
282 aInfo.applicationInfo = new ApplicationInfo();
283 aInfo.applicationInfo.packageName = mPackage;
284
285 Intent intent = new Intent();
Bryce Leefbd263b42018-03-07 10:33:55 -0800286 if (mComponent == null) {
287 mComponent = ComponentName.createRelative(DEFAULT_COMPONENT_PACKAGE_NAME,
288 DEFAULT_COMPONENT_CLASS_NAME);
289 }
290
Bryce Lee18d51592017-10-25 10:22:19 -0700291 intent.setComponent(mComponent);
292 intent.setFlags(mFlags);
293
Bryce Lee2b8e0372018-04-05 17:01:37 -0700294 final TestTaskRecord task = new TestTaskRecord(mSupervisor.mService, mTaskId, aInfo,
Bryce Lee93e7f792017-10-25 15:54:55 -0700295 intent /*intent*/, mVoiceSession, null /*_voiceInteractor*/);
Winson Chung0ec2a352017-10-26 11:38:30 -0700296 task.userId = mUserId;
Bryce Lee2b8e0372018-04-05 17:01:37 -0700297
298 if (mStack != null) {
299 mSupervisor.setFocusStackUnchecked("test", mStack);
300 mStack.addTask(task, true, "creating test task");
301 task.setStack(mStack);
302 task.setWindowContainerController();
303 }
304
Winson Chung0ec2a352017-10-26 11:38:30 -0700305 task.touchActiveTime();
Bryce Lee18d51592017-10-25 10:22:19 -0700306
307 return task;
308 }
Bryce Lee2b8e0372018-04-05 17:01:37 -0700309
310 private static class TestTaskRecord extends TaskRecord {
311 TestTaskRecord(ActivityManagerService service, int _taskId, ActivityInfo info,
312 Intent _intent, IVoiceInteractionSession _voiceSession,
313 IVoiceInteractor _voiceInteractor) {
314 super(service, _taskId, info, _intent, _voiceSession, _voiceInteractor);
315 }
316
317 @Override
318 void createWindowContainer(boolean onTop, boolean showForAllUsers) {
319 setWindowContainerController();
320 }
321
322 private void setWindowContainerController() {
323 setWindowContainerController(mock(TaskWindowContainerController.class));
324 }
325 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700326 }
327
328 /**
329 * An {@link ActivityManagerService} subclass which provides a test
330 * {@link ActivityStackSupervisor}.
331 */
332 protected static class TestActivityManagerService extends ActivityManagerService {
Bryce Leeb0f993f2018-03-02 15:38:01 -0800333 private ClientLifecycleManager mLifecycleManager;
Bryce Lee2b8e0372018-04-05 17:01:37 -0700334 private LockTaskController mLockTaskController;
Bryce Leeb0f993f2018-03-02 15:38:01 -0800335
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700336 TestActivityManagerService(Context context) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700337 super(context);
Bryce Lee04ab3462017-04-10 15:06:33 -0700338 mSupportsMultiWindow = true;
339 mSupportsMultiDisplay = true;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700340 mSupportsSplitScreenMultiWindow = true;
341 mSupportsFreeformWindowManagement = true;
342 mSupportsPictureInPicture = true;
Bryce Lee6a0754a2017-10-10 17:53:50 -0700343 mWindowManager = WindowTestUtils.getMockWindowManagerService();
Bryce Leeaf691c02017-03-20 14:20:22 -0700344 }
345
346 @Override
Bryce Leeb0f993f2018-03-02 15:38:01 -0800347 public ClientLifecycleManager getLifecycleManager() {
348 if (mLifecycleManager == null) {
349 return super.getLifecycleManager();
350 }
351 return mLifecycleManager;
352 }
353
Bryce Lee2b8e0372018-04-05 17:01:37 -0700354 public LockTaskController getLockTaskController() {
355 if (mLockTaskController == null) {
356 mLockTaskController = spy(super.getLockTaskController());
357 }
358
359 return mLockTaskController;
360 }
361
Bryce Leeb0f993f2018-03-02 15:38:01 -0800362 void setLifecycleManager(ClientLifecycleManager manager) {
363 mLifecycleManager = manager;
364 }
365
366 @Override
Bryce Lee2a3cc462017-10-27 10:57:35 -0700367 final protected ActivityStackSupervisor createStackSupervisor() {
368 final ActivityStackSupervisor supervisor = spy(createTestSupervisor());
Bryce Lee271617a2018-03-15 10:39:12 -0700369 final KeyguardController keyguardController = mock(KeyguardController.class);
Bryce Lee2a3cc462017-10-27 10:57:35 -0700370
371 // No home stack is set.
372 doNothing().when(supervisor).moveHomeStackToFront(any());
373 doReturn(true).when(supervisor).moveHomeStackTaskToTop(any());
374 // Invoked during {@link ActivityStack} creation.
375 doNothing().when(supervisor).updateUIDsPresentOnDisplay();
376 // Always keep things awake.
377 doReturn(true).when(supervisor).hasAwakeDisplay();
378 // Called when moving activity to pinned stack.
379 doNothing().when(supervisor).ensureActivitiesVisibleLocked(any(), anyInt(), anyBoolean());
380 // Do not schedule idle timeouts
381 doNothing().when(supervisor).scheduleIdleTimeoutLocked(any());
Bryce Leefbd263b42018-03-07 10:33:55 -0800382 // unit test version does not handle launch wake lock
383 doNothing().when(supervisor).acquireLaunchWakelock();
Bryce Lee271617a2018-03-15 10:39:12 -0700384 doReturn(keyguardController).when(supervisor).getKeyguardController();
Bryce Lee2a3cc462017-10-27 10:57:35 -0700385
386 supervisor.initialize();
387
388 return supervisor;
389 }
390
391 protected ActivityStackSupervisor createTestSupervisor() {
Bryce Lee3115bdf2017-04-05 08:39:40 -0700392 return new TestActivityStackSupervisor(this, mHandlerThread.getLooper());
Bryce Leeaf691c02017-03-20 14:20:22 -0700393 }
Bryce Lee29a649d2017-08-18 13:52:31 -0700394
395 @Override
396 void updateUsageStats(ActivityRecord component, boolean resumed) {
397 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700398 }
399
400 /**
401 * An {@link ActivityStackSupervisor} which stubs out certain methods that depend on
402 * setup not available in the test environment. Also specifies an injector for
403 */
404 protected static class TestActivityStackSupervisor extends ActivityStackSupervisor {
Bryce Lee2a3cc462017-10-27 10:57:35 -0700405 private ActivityDisplay mDisplay;
Bryce Lee459c0622018-03-19 11:04:01 -0700406 private KeyguardController mKeyguardController;
Bryce Lee943ebe72017-05-04 10:19:07 -0700407
Bryce Leeaf691c02017-03-20 14:20:22 -0700408 public TestActivityStackSupervisor(ActivityManagerService service, Looper looper) {
409 super(service, looper);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700410 mDisplayManager =
411 (DisplayManager) mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
Bryce Lee04ab3462017-04-10 15:06:33 -0700412 mWindowManager = prepareMockWindowManager();
Bryce Lee459c0622018-03-19 11:04:01 -0700413 mKeyguardController = mock(KeyguardController.class);
Bryce Lee2a3cc462017-10-27 10:57:35 -0700414 }
415
416 @Override
417 public void initialize() {
418 super.initialize();
Bryce Lee459c0622018-03-19 11:04:01 -0700419 mDisplay = spy(new TestActivityDisplay(this, DEFAULT_DISPLAY));
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700420 attachDisplay(mDisplay);
Bryce Lee04ab3462017-04-10 15:06:33 -0700421 }
422
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700423 @Override
Bryce Lee459c0622018-03-19 11:04:01 -0700424 public KeyguardController getKeyguardController() {
425 return mKeyguardController;
426 }
427
428 @Override
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700429 ActivityDisplay getDefaultDisplay() {
430 return mDisplay;
431 }
432
Bryce Lee2a3cc462017-10-27 10:57:35 -0700433 // 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 -0700434 @Override
Wale Ogunwalee1f68ce2018-03-09 08:58:54 -0800435 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus,
436 boolean ignoreCurrent) {
Bryce Lee04ab3462017-04-10 15:06:33 -0700437 return mFocusedStack;
438 }
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700439 }
440
Winson Chung59a47ded2018-01-25 17:46:06 +0000441 protected static class TestActivityDisplay extends ActivityDisplay {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700442
443 private final ActivityStackSupervisor mSupervisor;
444 TestActivityDisplay(ActivityStackSupervisor supervisor, int displayId) {
445 super(supervisor, displayId);
446 mSupervisor = supervisor;
Andrii Kulianb1cdb102017-07-13 15:33:06 -0700447 }
448
449 @Override
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700450 <T extends ActivityStack> T createStackUnchecked(int windowingMode, int activityType,
451 int stackId, boolean onTop) {
452 if (windowingMode == WINDOWING_MODE_PINNED) {
453 return (T) new PinnedActivityStack(this, stackId, mSupervisor, onTop) {
Andrii Kulianb1cdb102017-07-13 15:33:06 -0700454 @Override
455 Rect getDefaultPictureInPictureBounds(float aspectRatio) {
456 return new Rect(50, 50, 100, 100);
457 }
Bryce Lee4e4a3ec2017-09-27 08:25:03 -0700458
459 @Override
460 PinnedStackWindowController createStackWindowController(int displayId,
461 boolean onTop, Rect outBounds) {
462 return mock(PinnedStackWindowController.class);
463 }
Andrii Kulianb1cdb102017-07-13 15:33:06 -0700464 };
465 } else {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700466 return (T) new TestActivityStack(
467 this, stackId, mSupervisor, windowingMode, activityType, onTop);
Andrii Kulianb1cdb102017-07-13 15:33:06 -0700468 }
Bryce Lee04ab3462017-04-10 15:06:33 -0700469 }
Winson Chung59a47ded2018-01-25 17:46:06 +0000470
471 @Override
472 protected DisplayWindowController createWindowContainerController() {
473 return mock(DisplayWindowController.class);
474 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700475 }
476
Bryce Lee04ab3462017-04-10 15:06:33 -0700477 private static WindowManagerService prepareMockWindowManager() {
Bryce Lee4e4a3ec2017-09-27 08:25:03 -0700478 final WindowManagerService service = WindowTestUtils.getMockWindowManagerService();
Bryce Lee04ab3462017-04-10 15:06:33 -0700479
480 doAnswer((InvocationOnMock invocationOnMock) -> {
481 final Runnable runnable = invocationOnMock.<Runnable>getArgument(0);
482 if (runnable != null) {
483 runnable.run();
484 }
485 return null;
486 }).when(service).inSurfaceTransaction(any());
487
488 return service;
489 }
490
Bryce Leeaf691c02017-03-20 14:20:22 -0700491 /**
Bryce Lee2b8e0372018-04-05 17:01:37 -0700492 * Overridden {@link ActivityStack} that tracks test metrics, such as the number of times a
Bryce Leeaf691c02017-03-20 14:20:22 -0700493 * method is called. Note that its functionality depends on the implementations of the
494 * construction arguments.
495 */
496 protected static class TestActivityStack<T extends StackWindowController>
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700497 extends ActivityStack<T> {
Bryce Leeaf691c02017-03-20 14:20:22 -0700498 private int mOnActivityRemovedFromStackCount = 0;
499 private T mContainerController;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700500
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700501 static final int IS_TRANSLUCENT_UNSET = 0;
502 static final int IS_TRANSLUCENT_FALSE = 1;
503 static final int IS_TRANSLUCENT_TRUE = 2;
504 private int mIsTranslucent = IS_TRANSLUCENT_UNSET;
505
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800506 static final int SUPPORTS_SPLIT_SCREEN_UNSET = 0;
507 static final int SUPPORTS_SPLIT_SCREEN_FALSE = 1;
508 static final int SUPPORTS_SPLIT_SCREEN_TRUE = 2;
509 private int mSupportsSplitScreen = SUPPORTS_SPLIT_SCREEN_UNSET;
510
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700511 TestActivityStack(ActivityDisplay display, int stackId, ActivityStackSupervisor supervisor,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700512 int windowingMode, int activityType, boolean onTop) {
513 super(display, stackId, supervisor, windowingMode, activityType, onTop);
Bryce Leeaf691c02017-03-20 14:20:22 -0700514 }
515
516 @Override
517 void onActivityRemovedFromStack(ActivityRecord r) {
518 mOnActivityRemovedFromStackCount++;
519 super.onActivityRemovedFromStack(r);
520 }
521
522 // Returns the number of times {@link #onActivityRemovedFromStack} has been called
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700523 int onActivityRemovedFromStackInvocationCount() {
Bryce Leeaf691c02017-03-20 14:20:22 -0700524 return mOnActivityRemovedFromStackCount;
525 }
526
527 @Override
Wale Ogunwale034a8ec2017-09-02 17:14:40 -0700528 protected T createStackWindowController(int displayId, boolean onTop, Rect outBounds) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700529 mContainerController = (T) WindowTestUtils.createMockStackWindowContainerController();
Bryce Leef3c6a472017-11-14 14:53:06 -0800530
531 // Primary pinned stacks require a non-empty out bounds to be set or else all tasks
532 // will be moved to the full screen stack.
533 if (getWindowingMode() == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
534 outBounds.set(0, 0, 100, 100);
535 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700536 return mContainerController;
537 }
538
539 @Override
540 T getWindowContainerController() {
541 return mContainerController;
542 }
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700543
544 void setIsTranslucent(boolean isTranslucent) {
545 mIsTranslucent = isTranslucent ? IS_TRANSLUCENT_TRUE : IS_TRANSLUCENT_FALSE;
546 }
547
548 @Override
Wale Ogunwale66e16852017-10-19 13:35:52 -0700549 boolean isStackTranslucent(ActivityRecord starting) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700550 switch (mIsTranslucent) {
551 case IS_TRANSLUCENT_TRUE:
552 return true;
553 case IS_TRANSLUCENT_FALSE:
554 return false;
555 case IS_TRANSLUCENT_UNSET:
556 default:
Wale Ogunwale66e16852017-10-19 13:35:52 -0700557 return super.isStackTranslucent(starting);
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700558 }
559 }
Wale Ogunwale30e441d2017-11-09 08:28:45 -0800560
561 void setSupportsSplitScreen(boolean supportsSplitScreen) {
562 mSupportsSplitScreen = supportsSplitScreen
563 ? SUPPORTS_SPLIT_SCREEN_TRUE : SUPPORTS_SPLIT_SCREEN_FALSE;
564 }
565
566 @Override
567 public boolean supportsSplitScreenWindowingMode() {
568 switch (mSupportsSplitScreen) {
569 case SUPPORTS_SPLIT_SCREEN_TRUE:
570 return true;
571 case SUPPORTS_SPLIT_SCREEN_FALSE:
572 return false;
573 case SUPPORTS_SPLIT_SCREEN_UNSET:
574 default:
575 return super.supportsSplitScreenWindowingMode();
576 }
577 }
Bryce Lee2b8e0372018-04-05 17:01:37 -0700578
579 @Override
580 void startActivityLocked(ActivityRecord r, ActivityRecord focusedTopActivity,
581 boolean newTask, boolean keepCurTransition,
582 ActivityOptions options) {
583 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700584 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700585}