blob: 3d5d87c1359ab4dc289474862606e3b18500a0ff [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;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -070022import static android.view.Display.DEFAULT_DISPLAY;
Bryce Leeaf691c02017-03-20 14:20:22 -070023import static org.mockito.Mockito.mock;
Bryce Lee04ab3462017-04-10 15:06:33 -070024import static org.mockito.Mockito.doReturn;
25import static org.mockito.Mockito.any;
26import static org.mockito.Mockito.doAnswer;
Bryce Lee4e4a3ec2017-09-27 08:25:03 -070027import static org.mockito.Mockito.spy;
Bryce Lee04ab3462017-04-10 15:06:33 -070028
29import org.mockito.invocation.InvocationOnMock;
Bryce Leeaf691c02017-03-20 14:20:22 -070030
31import android.content.ComponentName;
32import android.content.Context;
33import android.content.Intent;
34import android.content.pm.ActivityInfo;
35import android.content.pm.ApplicationInfo;
36import android.content.res.Configuration;
37import android.graphics.Rect;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -070038import android.hardware.display.DisplayManager;
Bryce Lee3115bdf2017-04-05 08:39:40 -070039import android.os.HandlerThread;
Bryce Leeaf691c02017-03-20 14:20:22 -070040import android.os.Looper;
41import android.support.test.InstrumentationRegistry;
42import com.android.server.AttributeCache;
43import com.android.server.wm.AppWindowContainerController;
Bryce Lee4e4a3ec2017-09-27 08:25:03 -070044import com.android.server.wm.PinnedStackWindowController;
Bryce Leeaf691c02017-03-20 14:20:22 -070045import com.android.server.wm.StackWindowController;
Bryce Lee04ab3462017-04-10 15:06:33 -070046import com.android.server.wm.TaskWindowContainerController;
Bryce Leeaf691c02017-03-20 14:20:22 -070047import com.android.server.wm.WindowManagerService;
48import com.android.server.wm.WindowTestUtils;
Bryce Lee3115bdf2017-04-05 08:39:40 -070049import org.junit.After;
Bryce Leeaf691c02017-03-20 14:20:22 -070050import org.junit.Before;
51import org.mockito.MockitoAnnotations;
52
53/**
54 * A base class to handle common operations in activity related unit tests.
55 */
56public class ActivityTestsBase {
Bryce Lee939a9a32017-10-23 10:01:21 -070057 private static boolean sOneTimeSetupDone = false;
58
Bryce Leeaf691c02017-03-20 14:20:22 -070059 private final Context mContext = InstrumentationRegistry.getContext();
Bryce Lee3115bdf2017-04-05 08:39:40 -070060 private HandlerThread mHandlerThread;
Bryce Leeaf691c02017-03-20 14:20:22 -070061
Bryce Leeaf691c02017-03-20 14:20:22 -070062 @Before
63 public void setUp() throws Exception {
Bryce Lee939a9a32017-10-23 10:01:21 -070064 if (!sOneTimeSetupDone) {
65 sOneTimeSetupDone = true;
66
67 // Allows to mock package local classes and methods
68 System.setProperty("dexmaker.share_classloader", "true");
69 MockitoAnnotations.initMocks(this);
70 }
Bryce Lee3115bdf2017-04-05 08:39:40 -070071 mHandlerThread = new HandlerThread("ActivityTestsBaseThread");
72 mHandlerThread.start();
73 }
Bryce Leeaf691c02017-03-20 14:20:22 -070074
Bryce Lee3115bdf2017-04-05 08:39:40 -070075 @After
76 public void tearDown() {
77 mHandlerThread.quitSafely();
Bryce Leeaf691c02017-03-20 14:20:22 -070078 }
79
80 protected ActivityManagerService createActivityManagerService() {
Bryce Lee4e4a3ec2017-09-27 08:25:03 -070081 final ActivityManagerService service = spy(new TestActivityManagerService(mContext));
82 service.mWindowManager = prepareMockWindowManager();
Bryce Lee840c5662017-04-13 10:02:51 -070083 return service;
Bryce Leeaf691c02017-03-20 14:20:22 -070084 }
85
Bryce Lee18d51592017-10-25 10:22:19 -070086 /**
87 * Builder for creating new activities.
88 */
89 protected static class ActivityBuilder {
90 // An id appended to the end of the component name to make it unique
91 private static int sCurrentActivityId = 0;
Bryce Lee9f6affd2017-09-01 09:18:35 -070092
Bryce Lee18d51592017-10-25 10:22:19 -070093 // Default package name
94 private static final String DEFAULT_PACKAGE = "com.foo";
Bryce Leeaf691c02017-03-20 14:20:22 -070095
Bryce Lee18d51592017-10-25 10:22:19 -070096 // Default base activity name
97 private static final String DEFAULT_BASE_ACTIVITY_NAME = ".BarActivity";
98
99 private final ActivityManagerService mService;
100
101 private ComponentName mComponent;
102 private TaskRecord mTaskRecord;
103 private int mUid;
104 private boolean mCreateTask;
105 private ActivityStack mStack;
106
107 ActivityBuilder(ActivityManagerService service) {
108 mService = service;
Bryce Leeaf691c02017-03-20 14:20:22 -0700109 }
110
Bryce Lee18d51592017-10-25 10:22:19 -0700111 ActivityBuilder setComponent(ComponentName component) {
112 mComponent = component;
113 return this;
114 }
115
116 ActivityBuilder setTask(TaskRecord task) {
117 mTaskRecord = task;
118 return this;
119 }
120
121 ActivityBuilder setStack(ActivityStack stack) {
122 mStack = stack;
123 return this;
124 }
125
126 ActivityBuilder setCreateTask(boolean createTask) {
127 mCreateTask = createTask;
128 return this;
129 }
130
131 ActivityBuilder setUid(int uid) {
132 mUid = uid;
133 return this;
134 }
135
136 ActivityRecord build() {
137 if (mComponent == null) {
138 final int id = sCurrentActivityId++;
139 mComponent = ComponentName.createRelative(DEFAULT_PACKAGE,
140 DEFAULT_BASE_ACTIVITY_NAME + id);
141 }
142
143 if (mCreateTask) {
144 mTaskRecord = new TaskBuilder(mService.mStackSupervisor)
145 .setComponent(mComponent)
146 .setStack(mStack).build();
147 }
148
149 Intent intent = new Intent();
150 intent.setComponent(mComponent);
151 final ActivityInfo aInfo = new ActivityInfo();
152 aInfo.applicationInfo = new ApplicationInfo();
153 aInfo.applicationInfo.packageName = mComponent.getPackageName();
154 aInfo.applicationInfo.uid = mUid;
155 AttributeCache.init(mService.mContext);
156 final ActivityRecord activity = new ActivityRecord(mService, null /* caller */,
157 0 /* launchedFromPid */, 0, null, intent, null,
158 aInfo /*aInfo*/, new Configuration(), null /* resultTo */, null /* resultWho */,
159 0 /* reqCode */, false /*componentSpecified*/, false /* rootVoiceInteraction */,
160 mService.mStackSupervisor, null /* options */, null /* sourceRecord */);
161 activity.mWindowContainerController = mock(AppWindowContainerController.class);
162
163 if (mTaskRecord != null) {
164 mTaskRecord.addActivityToTop(activity);
165 }
166
167 return activity;
168 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700169 }
170
Bryce Lee18d51592017-10-25 10:22:19 -0700171 /**
172 * Builder for creating new tasks.
173 */
174 protected static class TaskBuilder {
175 private final ActivityStackSupervisor mSupervisor;
Winson Chung1dbc8112017-09-28 18:05:31 -0700176
Bryce Lee18d51592017-10-25 10:22:19 -0700177 private ComponentName mComponent;
178 private String mPackage;
179 private int mFlags = 0;
180 private int mTaskId = 0;
Bryce Leeaf691c02017-03-20 14:20:22 -0700181
Bryce Lee18d51592017-10-25 10:22:19 -0700182 private ActivityStack mStack;
Bryce Leeaf691c02017-03-20 14:20:22 -0700183
Bryce Lee18d51592017-10-25 10:22:19 -0700184 TaskBuilder(ActivityStackSupervisor supervisor) {
185 mSupervisor = supervisor;
186 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700187
Bryce Lee18d51592017-10-25 10:22:19 -0700188 TaskBuilder setComponent(ComponentName component) {
189 mComponent = component;
190 return this;
191 }
192
193 TaskBuilder setPackage(String packageName) {
194 mPackage = packageName;
195 return this;
196 }
197
198 TaskBuilder setFlags(int flags) {
199 mFlags = flags;
200 return this;
201 }
202
203 TaskBuilder setTaskId(int taskId) {
204 mTaskId = taskId;
205 return this;
206 }
207
208 TaskBuilder setStack(ActivityStack stack) {
209 mStack = stack;
210 return this;
211 }
212
213 TaskRecord build() {
214 if (mStack == null) {
215 mStack = mSupervisor.getDefaultDisplay().createStack(
216 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, true /* onTop */);
217 }
218
219 final ActivityInfo aInfo = new ActivityInfo();
220 aInfo.applicationInfo = new ApplicationInfo();
221 aInfo.applicationInfo.packageName = mPackage;
222
223 Intent intent = new Intent();
224 intent.setComponent(mComponent);
225 intent.setFlags(mFlags);
226
227 final TaskRecord task = new TaskRecord(mSupervisor.mService, mTaskId, aInfo,
228 intent /*intent*/, null /*_taskDescription*/);
229 mSupervisor.setFocusStackUnchecked("test", mStack);
230 mStack.addTask(task, true, "creating test task");
231 task.setStack(mStack);
232 task.setWindowContainerController(mock(TaskWindowContainerController.class));
233
234 return task;
235 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700236 }
237
238 /**
239 * An {@link ActivityManagerService} subclass which provides a test
240 * {@link ActivityStackSupervisor}.
241 */
242 protected static class TestActivityManagerService extends ActivityManagerService {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700243 TestActivityManagerService(Context context) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700244 super(context);
Bryce Lee04ab3462017-04-10 15:06:33 -0700245 mSupportsMultiWindow = true;
246 mSupportsMultiDisplay = true;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700247 mSupportsSplitScreenMultiWindow = true;
248 mSupportsFreeformWindowManagement = true;
249 mSupportsPictureInPicture = true;
Bryce Lee6a0754a2017-10-10 17:53:50 -0700250 mWindowManager = WindowTestUtils.getMockWindowManagerService();
Bryce Leeaf691c02017-03-20 14:20:22 -0700251 }
252
253 @Override
254 protected ActivityStackSupervisor createStackSupervisor() {
Bryce Lee3115bdf2017-04-05 08:39:40 -0700255 return new TestActivityStackSupervisor(this, mHandlerThread.getLooper());
Bryce Leeaf691c02017-03-20 14:20:22 -0700256 }
Bryce Lee29a649d2017-08-18 13:52:31 -0700257
258 @Override
259 void updateUsageStats(ActivityRecord component, boolean resumed) {
260 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700261 }
262
263 /**
264 * An {@link ActivityStackSupervisor} which stubs out certain methods that depend on
265 * setup not available in the test environment. Also specifies an injector for
266 */
267 protected static class TestActivityStackSupervisor extends ActivityStackSupervisor {
Bryce Lee943ebe72017-05-04 10:19:07 -0700268 private final ActivityDisplay mDisplay;
Bryce Lee1533b2b2017-09-14 17:06:41 -0700269 private boolean mLastResizeable;
Bryce Lee943ebe72017-05-04 10:19:07 -0700270
Bryce Leeaf691c02017-03-20 14:20:22 -0700271 public TestActivityStackSupervisor(ActivityManagerService service, Looper looper) {
272 super(service, looper);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700273 mDisplayManager =
274 (DisplayManager) mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
Bryce Lee04ab3462017-04-10 15:06:33 -0700275 mWindowManager = prepareMockWindowManager();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700276 mDisplay = new TestActivityDisplay(this, DEFAULT_DISPLAY);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700277 attachDisplay(mDisplay);
Bryce Lee04ab3462017-04-10 15:06:33 -0700278 }
279
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700280 @Override
281 ActivityDisplay getDefaultDisplay() {
282 return mDisplay;
283 }
284
Bryce Lee1533b2b2017-09-14 17:06:41 -0700285 // TODO: Use Mockito spy instead. Currently not possible due to TestActivityStackSupervisor
286 // access to ActivityDisplay
287 @Override
288 boolean canPlaceEntityOnDisplay(int displayId, boolean resizeable, int callingPid,
289 int callingUid, ActivityInfo activityInfo) {
290 mLastResizeable = resizeable;
291 return super.canPlaceEntityOnDisplay(displayId, resizeable, callingPid, callingUid,
292 activityInfo);
293 }
294
295 // TODO: remove and use Mockito verify once {@link #canPlaceEntityOnDisplay} override is
296 // removed.
297 public boolean getLastResizeableFromCanPlaceEntityOnDisplay() {
298 return mLastResizeable;
299 }
300
Bryce Lee04ab3462017-04-10 15:06:33 -0700301 // No home stack is set.
302 @Override
303 void moveHomeStackToFront(String reason) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700304 }
305
Bryce Lee3345c4e2017-04-25 07:40:41 -0700306 @Override
307 boolean moveHomeStackTaskToTop(String reason) {
308 return true;
309 }
310
Bryce Leeaf691c02017-03-20 14:20:22 -0700311 // Invoked during {@link ActivityStack} creation.
312 @Override
313 void updateUIDsPresentOnDisplay() {
314 }
315
Bryce Lee04ab3462017-04-10 15:06:33 -0700316 // Just return the current front task.
317 @Override
318 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) {
319 return mFocusedStack;
320 }
321
322 // Called when moving activity to pinned stack.
323 @Override
324 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
325 boolean preserveWindows) {
326 }
327
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700328 // Always keep things awake
329 @Override
330 boolean hasAwakeDisplay() {
331 return true;
332 }
333 }
334
335 private static class TestActivityDisplay extends ActivityDisplay {
336
337 private final ActivityStackSupervisor mSupervisor;
338 TestActivityDisplay(ActivityStackSupervisor supervisor, int displayId) {
339 super(supervisor, displayId);
340 mSupervisor = supervisor;
Andrii Kulianb1cdb102017-07-13 15:33:06 -0700341 }
342
343 @Override
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700344 <T extends ActivityStack> T createStackUnchecked(int windowingMode, int activityType,
345 int stackId, boolean onTop) {
346 if (windowingMode == WINDOWING_MODE_PINNED) {
347 return (T) new PinnedActivityStack(this, stackId, mSupervisor, onTop) {
Andrii Kulianb1cdb102017-07-13 15:33:06 -0700348 @Override
349 Rect getDefaultPictureInPictureBounds(float aspectRatio) {
350 return new Rect(50, 50, 100, 100);
351 }
Bryce Lee4e4a3ec2017-09-27 08:25:03 -0700352
353 @Override
354 PinnedStackWindowController createStackWindowController(int displayId,
355 boolean onTop, Rect outBounds) {
356 return mock(PinnedStackWindowController.class);
357 }
Andrii Kulianb1cdb102017-07-13 15:33:06 -0700358 };
359 } else {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700360 return (T) new TestActivityStack(
361 this, stackId, mSupervisor, windowingMode, activityType, onTop);
Andrii Kulianb1cdb102017-07-13 15:33:06 -0700362 }
Bryce Lee04ab3462017-04-10 15:06:33 -0700363 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700364 }
365
Bryce Lee04ab3462017-04-10 15:06:33 -0700366 private static WindowManagerService prepareMockWindowManager() {
Bryce Lee4e4a3ec2017-09-27 08:25:03 -0700367 final WindowManagerService service = WindowTestUtils.getMockWindowManagerService();
Bryce Lee04ab3462017-04-10 15:06:33 -0700368
369 doAnswer((InvocationOnMock invocationOnMock) -> {
370 final Runnable runnable = invocationOnMock.<Runnable>getArgument(0);
371 if (runnable != null) {
372 runnable.run();
373 }
374 return null;
375 }).when(service).inSurfaceTransaction(any());
376
377 return service;
378 }
379
Bryce Leeaf691c02017-03-20 14:20:22 -0700380 /**
Bryce Lee4e4a3ec2017-09-27 08:25:03 -0700381 * Overrided of {@link ActivityStack} that tracks test metrics, such as the number of times a
Bryce Leeaf691c02017-03-20 14:20:22 -0700382 * method is called. Note that its functionality depends on the implementations of the
383 * construction arguments.
384 */
385 protected static class TestActivityStack<T extends StackWindowController>
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700386 extends ActivityStack<T> {
Bryce Leeaf691c02017-03-20 14:20:22 -0700387 private int mOnActivityRemovedFromStackCount = 0;
388 private T mContainerController;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700389
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700390 static final int IS_TRANSLUCENT_UNSET = 0;
391 static final int IS_TRANSLUCENT_FALSE = 1;
392 static final int IS_TRANSLUCENT_TRUE = 2;
393 private int mIsTranslucent = IS_TRANSLUCENT_UNSET;
394
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700395 TestActivityStack(ActivityDisplay display, int stackId, ActivityStackSupervisor supervisor,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700396 int windowingMode, int activityType, boolean onTop) {
397 super(display, stackId, supervisor, windowingMode, activityType, onTop);
Bryce Leeaf691c02017-03-20 14:20:22 -0700398 }
399
400 @Override
401 void onActivityRemovedFromStack(ActivityRecord r) {
402 mOnActivityRemovedFromStackCount++;
403 super.onActivityRemovedFromStack(r);
404 }
405
406 // Returns the number of times {@link #onActivityRemovedFromStack} has been called
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700407 int onActivityRemovedFromStackInvocationCount() {
Bryce Leeaf691c02017-03-20 14:20:22 -0700408 return mOnActivityRemovedFromStackCount;
409 }
410
411 @Override
Wale Ogunwale034a8ec2017-09-02 17:14:40 -0700412 protected T createStackWindowController(int displayId, boolean onTop, Rect outBounds) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700413 mContainerController = (T) WindowTestUtils.createMockStackWindowContainerController();
414 return mContainerController;
415 }
416
417 @Override
418 T getWindowContainerController() {
419 return mContainerController;
420 }
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700421
422 void setIsTranslucent(boolean isTranslucent) {
423 mIsTranslucent = isTranslucent ? IS_TRANSLUCENT_TRUE : IS_TRANSLUCENT_FALSE;
424 }
425
426 @Override
Wale Ogunwale66e16852017-10-19 13:35:52 -0700427 boolean isStackTranslucent(ActivityRecord starting) {
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700428 switch (mIsTranslucent) {
429 case IS_TRANSLUCENT_TRUE:
430 return true;
431 case IS_TRANSLUCENT_FALSE:
432 return false;
433 case IS_TRANSLUCENT_UNSET:
434 default:
Wale Ogunwale66e16852017-10-19 13:35:52 -0700435 return super.isStackTranslucent(starting);
Wale Ogunwale7e1f5f52017-10-18 15:19:59 -0700436 }
437 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700438 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700439}