blob: cc8bd69eac3f5454b71c1c5891d374c4cc46d5eb [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
Wale Ogunwale04a05ac2017-09-17 21:35:02 -070019import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -070020import static android.view.Display.DEFAULT_DISPLAY;
Bryce Leeaf691c02017-03-20 14:20:22 -070021import static org.mockito.Mockito.mock;
Bryce Lee04ab3462017-04-10 15:06:33 -070022import static org.mockito.Mockito.doReturn;
23import static org.mockito.Mockito.any;
24import static org.mockito.Mockito.doAnswer;
Bryce Lee4e4a3ec2017-09-27 08:25:03 -070025import static org.mockito.Mockito.spy;
Bryce Lee04ab3462017-04-10 15:06:33 -070026
27import org.mockito.invocation.InvocationOnMock;
Bryce Leeaf691c02017-03-20 14:20:22 -070028
29import android.content.ComponentName;
30import android.content.Context;
31import android.content.Intent;
32import android.content.pm.ActivityInfo;
33import android.content.pm.ApplicationInfo;
34import android.content.res.Configuration;
35import android.graphics.Rect;
Wale Ogunwale9dcf9462017-09-19 15:13:01 -070036import android.hardware.display.DisplayManager;
Bryce Lee3115bdf2017-04-05 08:39:40 -070037import android.os.HandlerThread;
Bryce Leeaf691c02017-03-20 14:20:22 -070038import android.os.Looper;
39import android.support.test.InstrumentationRegistry;
40import com.android.server.AttributeCache;
41import com.android.server.wm.AppWindowContainerController;
Bryce Lee4e4a3ec2017-09-27 08:25:03 -070042import com.android.server.wm.PinnedStackWindowController;
Bryce Leeaf691c02017-03-20 14:20:22 -070043import com.android.server.wm.StackWindowController;
Bryce Lee04ab3462017-04-10 15:06:33 -070044import com.android.server.wm.TaskWindowContainerController;
Bryce Leeaf691c02017-03-20 14:20:22 -070045import com.android.server.wm.WindowManagerService;
46import com.android.server.wm.WindowTestUtils;
Bryce Lee3115bdf2017-04-05 08:39:40 -070047import org.junit.After;
Bryce Leeaf691c02017-03-20 14:20:22 -070048import org.junit.Before;
49import org.mockito.MockitoAnnotations;
50
51/**
52 * A base class to handle common operations in activity related unit tests.
53 */
54public class ActivityTestsBase {
55 private final Context mContext = InstrumentationRegistry.getContext();
Bryce Lee3115bdf2017-04-05 08:39:40 -070056 private HandlerThread mHandlerThread;
Bryce Leeaf691c02017-03-20 14:20:22 -070057
Bryce Leeaf691c02017-03-20 14:20:22 -070058 @Before
59 public void setUp() throws Exception {
60 MockitoAnnotations.initMocks(this);
Bryce Lee3115bdf2017-04-05 08:39:40 -070061 mHandlerThread = new HandlerThread("ActivityTestsBaseThread");
62 mHandlerThread.start();
63 }
Bryce Leeaf691c02017-03-20 14:20:22 -070064
Bryce Lee3115bdf2017-04-05 08:39:40 -070065 @After
66 public void tearDown() {
67 mHandlerThread.quitSafely();
Bryce Leeaf691c02017-03-20 14:20:22 -070068 }
69
70 protected ActivityManagerService createActivityManagerService() {
Bryce Lee4e4a3ec2017-09-27 08:25:03 -070071 final ActivityManagerService service = spy(new TestActivityManagerService(mContext));
72 service.mWindowManager = prepareMockWindowManager();
Bryce Lee840c5662017-04-13 10:02:51 -070073 return service;
Bryce Leeaf691c02017-03-20 14:20:22 -070074 }
75
Bryce Leeaf691c02017-03-20 14:20:22 -070076 protected static ActivityRecord createActivity(ActivityManagerService service,
77 ComponentName component, TaskRecord task) {
Bryce Lee9f6affd2017-09-01 09:18:35 -070078 return createActivity(service, component, task, 0 /* userId */);
79 }
80
81 protected static ActivityRecord createActivity(ActivityManagerService service,
82 ComponentName component, TaskRecord task, int uid) {
Bryce Leeaf691c02017-03-20 14:20:22 -070083 Intent intent = new Intent();
84 intent.setComponent(component);
85 final ActivityInfo aInfo = new ActivityInfo();
86 aInfo.applicationInfo = new ApplicationInfo();
87 aInfo.applicationInfo.packageName = component.getPackageName();
Bryce Lee9f6affd2017-09-01 09:18:35 -070088 aInfo.applicationInfo.uid = uid;
Bryce Leeaf691c02017-03-20 14:20:22 -070089 AttributeCache.init(service.mContext);
90 final ActivityRecord activity = new ActivityRecord(service, null /* caller */,
91 0 /* launchedFromPid */, 0, null, intent, null,
92 aInfo /*aInfo*/, new Configuration(), null /* resultTo */, null /* resultWho */,
93 0 /* reqCode */, false /*componentSpecified*/, false /* rootVoiceInteraction */,
Andrii Kulianb1cdb102017-07-13 15:33:06 -070094 service.mStackSupervisor, null /* options */, null /* sourceRecord */);
Bryce Leeaf691c02017-03-20 14:20:22 -070095 activity.mWindowContainerController = mock(AppWindowContainerController.class);
96
97 if (task != null) {
98 task.addActivityToTop(activity);
99 }
100
101 return activity;
102 }
103
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700104 protected static TaskRecord createTask(ActivityStackSupervisor supervisor,
105 ComponentName component, ActivityStack stack) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700106 final ActivityInfo aInfo = new ActivityInfo();
107 aInfo.applicationInfo = new ApplicationInfo();
108 aInfo.applicationInfo.packageName = component.getPackageName();
109
110 Intent intent = new Intent();
111 intent.setComponent(component);
112
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700113 final TaskRecord task = new TaskRecord(supervisor.mService, 0, aInfo, intent /*intent*/,
Jorim Jaggie7d2b852017-08-28 17:55:15 +0200114 null /*_taskDescription*/);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700115 supervisor.setFocusStackUnchecked("test", stack);
Bryce Leeaf691c02017-03-20 14:20:22 -0700116 stack.addTask(task, true, "creating test task");
117 task.setStack(stack);
Bryce Lee04ab3462017-04-10 15:06:33 -0700118 task.setWindowContainerController(mock(TaskWindowContainerController.class));
Bryce Leeaf691c02017-03-20 14:20:22 -0700119
120 return task;
121 }
122
123 /**
124 * An {@link ActivityManagerService} subclass which provides a test
125 * {@link ActivityStackSupervisor}.
126 */
127 protected static class TestActivityManagerService extends ActivityManagerService {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700128 TestActivityManagerService(Context context) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700129 super(context);
Bryce Lee04ab3462017-04-10 15:06:33 -0700130 mSupportsMultiWindow = true;
131 mSupportsMultiDisplay = true;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700132 mSupportsSplitScreenMultiWindow = true;
133 mSupportsFreeformWindowManagement = true;
134 mSupportsPictureInPicture = true;
Bryce Lee6a0754a2017-10-10 17:53:50 -0700135 mWindowManager = WindowTestUtils.getMockWindowManagerService();
Bryce Leeaf691c02017-03-20 14:20:22 -0700136 }
137
138 @Override
139 protected ActivityStackSupervisor createStackSupervisor() {
Bryce Lee3115bdf2017-04-05 08:39:40 -0700140 return new TestActivityStackSupervisor(this, mHandlerThread.getLooper());
Bryce Leeaf691c02017-03-20 14:20:22 -0700141 }
Bryce Lee29a649d2017-08-18 13:52:31 -0700142
143 @Override
144 void updateUsageStats(ActivityRecord component, boolean resumed) {
145 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700146 }
147
148 /**
149 * An {@link ActivityStackSupervisor} which stubs out certain methods that depend on
150 * setup not available in the test environment. Also specifies an injector for
151 */
152 protected static class TestActivityStackSupervisor extends ActivityStackSupervisor {
Bryce Lee943ebe72017-05-04 10:19:07 -0700153 private final ActivityDisplay mDisplay;
Bryce Lee1533b2b2017-09-14 17:06:41 -0700154 private boolean mLastResizeable;
Bryce Lee943ebe72017-05-04 10:19:07 -0700155
Bryce Leeaf691c02017-03-20 14:20:22 -0700156 public TestActivityStackSupervisor(ActivityManagerService service, Looper looper) {
157 super(service, looper);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700158 mDisplayManager =
159 (DisplayManager) mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
Bryce Lee04ab3462017-04-10 15:06:33 -0700160 mWindowManager = prepareMockWindowManager();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700161 mDisplay = new TestActivityDisplay(this, DEFAULT_DISPLAY);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700162 attachDisplay(mDisplay);
Bryce Lee04ab3462017-04-10 15:06:33 -0700163 }
164
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700165 @Override
166 ActivityDisplay getDefaultDisplay() {
167 return mDisplay;
168 }
169
Bryce Lee1533b2b2017-09-14 17:06:41 -0700170 // TODO: Use Mockito spy instead. Currently not possible due to TestActivityStackSupervisor
171 // access to ActivityDisplay
172 @Override
173 boolean canPlaceEntityOnDisplay(int displayId, boolean resizeable, int callingPid,
174 int callingUid, ActivityInfo activityInfo) {
175 mLastResizeable = resizeable;
176 return super.canPlaceEntityOnDisplay(displayId, resizeable, callingPid, callingUid,
177 activityInfo);
178 }
179
180 // TODO: remove and use Mockito verify once {@link #canPlaceEntityOnDisplay} override is
181 // removed.
182 public boolean getLastResizeableFromCanPlaceEntityOnDisplay() {
183 return mLastResizeable;
184 }
185
Bryce Lee04ab3462017-04-10 15:06:33 -0700186 // No home stack is set.
187 @Override
188 void moveHomeStackToFront(String reason) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700189 }
190
Bryce Lee3345c4e2017-04-25 07:40:41 -0700191 @Override
192 boolean moveHomeStackTaskToTop(String reason) {
193 return true;
194 }
195
Bryce Leeaf691c02017-03-20 14:20:22 -0700196 // Invoked during {@link ActivityStack} creation.
197 @Override
198 void updateUIDsPresentOnDisplay() {
199 }
200
Bryce Lee04ab3462017-04-10 15:06:33 -0700201 // Just return the current front task.
202 @Override
203 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) {
204 return mFocusedStack;
205 }
206
207 // Called when moving activity to pinned stack.
208 @Override
209 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
210 boolean preserveWindows) {
211 }
212
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700213 // Always keep things awake
214 @Override
215 boolean hasAwakeDisplay() {
216 return true;
217 }
218 }
219
220 private static class TestActivityDisplay extends ActivityDisplay {
221
222 private final ActivityStackSupervisor mSupervisor;
223 TestActivityDisplay(ActivityStackSupervisor supervisor, int displayId) {
224 super(supervisor, displayId);
225 mSupervisor = supervisor;
Andrii Kulianb1cdb102017-07-13 15:33:06 -0700226 }
227
228 @Override
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700229 <T extends ActivityStack> T createStackUnchecked(int windowingMode, int activityType,
230 int stackId, boolean onTop) {
231 if (windowingMode == WINDOWING_MODE_PINNED) {
232 return (T) new PinnedActivityStack(this, stackId, mSupervisor, onTop) {
Andrii Kulianb1cdb102017-07-13 15:33:06 -0700233 @Override
234 Rect getDefaultPictureInPictureBounds(float aspectRatio) {
235 return new Rect(50, 50, 100, 100);
236 }
Bryce Lee4e4a3ec2017-09-27 08:25:03 -0700237
238 @Override
239 PinnedStackWindowController createStackWindowController(int displayId,
240 boolean onTop, Rect outBounds) {
241 return mock(PinnedStackWindowController.class);
242 }
Andrii Kulianb1cdb102017-07-13 15:33:06 -0700243 };
244 } else {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700245 return (T) new TestActivityStack(
246 this, stackId, mSupervisor, windowingMode, activityType, onTop);
Andrii Kulianb1cdb102017-07-13 15:33:06 -0700247 }
Bryce Lee04ab3462017-04-10 15:06:33 -0700248 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700249 }
250
Bryce Lee04ab3462017-04-10 15:06:33 -0700251 private static WindowManagerService prepareMockWindowManager() {
Bryce Lee4e4a3ec2017-09-27 08:25:03 -0700252 final WindowManagerService service = WindowTestUtils.getMockWindowManagerService();
Bryce Lee04ab3462017-04-10 15:06:33 -0700253
254 doAnswer((InvocationOnMock invocationOnMock) -> {
255 final Runnable runnable = invocationOnMock.<Runnable>getArgument(0);
256 if (runnable != null) {
257 runnable.run();
258 }
259 return null;
260 }).when(service).inSurfaceTransaction(any());
261
262 return service;
263 }
264
265 protected interface ActivityStackReporter {
266 int onActivityRemovedFromStackInvocationCount();
267 }
268
Bryce Leeaf691c02017-03-20 14:20:22 -0700269 /**
Bryce Lee4e4a3ec2017-09-27 08:25:03 -0700270 * Overrided of {@link ActivityStack} that tracks test metrics, such as the number of times a
Bryce Leeaf691c02017-03-20 14:20:22 -0700271 * method is called. Note that its functionality depends on the implementations of the
272 * construction arguments.
273 */
274 protected static class TestActivityStack<T extends StackWindowController>
Bryce Lee04ab3462017-04-10 15:06:33 -0700275 extends ActivityStack<T> implements ActivityStackReporter {
Bryce Leeaf691c02017-03-20 14:20:22 -0700276 private int mOnActivityRemovedFromStackCount = 0;
277 private T mContainerController;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700278
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700279 TestActivityStack(ActivityDisplay display, int stackId, ActivityStackSupervisor supervisor,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700280 int windowingMode, int activityType, boolean onTop) {
281 super(display, stackId, supervisor, windowingMode, activityType, onTop);
Bryce Leeaf691c02017-03-20 14:20:22 -0700282 }
283
284 @Override
285 void onActivityRemovedFromStack(ActivityRecord r) {
286 mOnActivityRemovedFromStackCount++;
287 super.onActivityRemovedFromStack(r);
288 }
289
290 // Returns the number of times {@link #onActivityRemovedFromStack} has been called
Bryce Lee04ab3462017-04-10 15:06:33 -0700291 @Override
Bryce Leeaf691c02017-03-20 14:20:22 -0700292 public int onActivityRemovedFromStackInvocationCount() {
293 return mOnActivityRemovedFromStackCount;
294 }
295
296 @Override
Wale Ogunwale034a8ec2017-09-02 17:14:40 -0700297 protected T createStackWindowController(int displayId, boolean onTop, Rect outBounds) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700298 mContainerController = (T) WindowTestUtils.createMockStackWindowContainerController();
299 return mContainerController;
300 }
301
302 @Override
303 T getWindowContainerController() {
304 return mContainerController;
305 }
306 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700307}