blob: 4ad92c7ec14eb9c4ee9a8a00784aced2dd0f44c0 [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
Andrii Kulian94e82d9b02017-07-13 15:33:06 -070019import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Bryce Leeaf691c02017-03-20 14:20:22 -070020import static org.mockito.Mockito.mock;
Bryce Lee04ab3462017-04-10 15:06:33 -070021import static org.mockito.Mockito.doReturn;
22import static org.mockito.Mockito.any;
23import static org.mockito.Mockito.doAnswer;
24
25import org.mockito.invocation.InvocationOnMock;
Bryce Leeaf691c02017-03-20 14:20:22 -070026
Bryce Lee840c5662017-04-13 10:02:51 -070027import android.app.ActivityManager;
Bryce Leeaf691c02017-03-20 14:20:22 -070028import android.content.ComponentName;
29import android.content.Context;
30import android.content.Intent;
31import android.content.pm.ActivityInfo;
32import android.content.pm.ApplicationInfo;
33import android.content.res.Configuration;
34import android.graphics.Rect;
Bryce Lee3115bdf2017-04-05 08:39:40 -070035import android.os.HandlerThread;
Bryce Leeaf691c02017-03-20 14:20:22 -070036import android.os.Looper;
37import android.support.test.InstrumentationRegistry;
38import com.android.server.AttributeCache;
39import com.android.server.wm.AppWindowContainerController;
40import com.android.server.wm.StackWindowController;
41
Bryce Lee04ab3462017-04-10 15:06:33 -070042import com.android.server.wm.TaskWindowContainerController;
Bryce Leeaf691c02017-03-20 14:20:22 -070043import com.android.server.wm.WindowManagerService;
44import com.android.server.wm.WindowTestUtils;
Bryce Lee3115bdf2017-04-05 08:39:40 -070045import org.junit.After;
Bryce Leeaf691c02017-03-20 14:20:22 -070046import org.junit.Before;
47import org.mockito.MockitoAnnotations;
48
49/**
50 * A base class to handle common operations in activity related unit tests.
51 */
52public class ActivityTestsBase {
53 private final Context mContext = InstrumentationRegistry.getContext();
Bryce Lee3115bdf2017-04-05 08:39:40 -070054 private HandlerThread mHandlerThread;
Bryce Leeaf691c02017-03-20 14:20:22 -070055
56 // Grabbing an instance of {@link WindowManagerService} creates it if not present so this must
57 // be called at before any tests.
58 private final WindowManagerService mWms = WindowTestUtils.getWindowManagerService(mContext);
59
60 @Before
61 public void setUp() throws Exception {
62 MockitoAnnotations.initMocks(this);
Bryce Lee3115bdf2017-04-05 08:39:40 -070063 mHandlerThread = new HandlerThread("ActivityTestsBaseThread");
64 mHandlerThread.start();
65 }
Bryce Leeaf691c02017-03-20 14:20:22 -070066
Bryce Lee3115bdf2017-04-05 08:39:40 -070067 @After
68 public void tearDown() {
69 mHandlerThread.quitSafely();
Bryce Leeaf691c02017-03-20 14:20:22 -070070 }
71
72 protected ActivityManagerService createActivityManagerService() {
Bryce Lee840c5662017-04-13 10:02:51 -070073 final ActivityManagerService service = new TestActivityManagerService(mContext);
Bryce Lee04ab3462017-04-10 15:06:33 -070074 service.mWindowManager = WindowTestUtils.getMockWindowManagerService();
Bryce Lee840c5662017-04-13 10:02:51 -070075 return service;
Bryce Leeaf691c02017-03-20 14:20:22 -070076 }
77
Bryce Lee04ab3462017-04-10 15:06:33 -070078 protected static ActivityStack createActivityStack(ActivityManagerService service,
Bryce Leeaf691c02017-03-20 14:20:22 -070079 int stackId, int displayId, boolean onTop) {
80 if (service.mStackSupervisor instanceof TestActivityStackSupervisor) {
Bryce Lee04ab3462017-04-10 15:06:33 -070081 return ((TestActivityStackSupervisor) service.mStackSupervisor)
Andrii Kulian94e82d9b02017-07-13 15:33:06 -070082 .createTestStack(stackId, onTop);
Bryce Leeaf691c02017-03-20 14:20:22 -070083 }
84
85 return null;
86 }
87
88 protected static ActivityRecord createActivity(ActivityManagerService service,
89 ComponentName component, TaskRecord task) {
90 Intent intent = new Intent();
91 intent.setComponent(component);
92 final ActivityInfo aInfo = new ActivityInfo();
93 aInfo.applicationInfo = new ApplicationInfo();
94 aInfo.applicationInfo.packageName = component.getPackageName();
95 AttributeCache.init(service.mContext);
96 final ActivityRecord activity = new ActivityRecord(service, null /* caller */,
97 0 /* launchedFromPid */, 0, null, intent, null,
98 aInfo /*aInfo*/, new Configuration(), null /* resultTo */, null /* resultWho */,
99 0 /* reqCode */, false /*componentSpecified*/, false /* rootVoiceInteraction */,
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700100 service.mStackSupervisor, null /* options */, null /* sourceRecord */);
Bryce Leeaf691c02017-03-20 14:20:22 -0700101 activity.mWindowContainerController = mock(AppWindowContainerController.class);
102
103 if (task != null) {
104 task.addActivityToTop(activity);
105 }
106
107 return activity;
108 }
109
110 protected static TaskRecord createTask(ActivityManagerService service,
Bryce Lee04ab3462017-04-10 15:06:33 -0700111 ComponentName component, int stackId) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700112 final ActivityInfo aInfo = new ActivityInfo();
113 aInfo.applicationInfo = new ApplicationInfo();
114 aInfo.applicationInfo.packageName = component.getPackageName();
115
116 Intent intent = new Intent();
117 intent.setComponent(component);
118
119 final TaskRecord task = new TaskRecord(service, 0, aInfo, intent /*intent*/,
Bryce Lee840c5662017-04-13 10:02:51 -0700120 null /*_taskDescription*/, new ActivityManager.TaskThumbnailInfo());
Bryce Lee04ab3462017-04-10 15:06:33 -0700121 final ActivityStack stack = service.mStackSupervisor.getStack(stackId,
122 true /*createStaticStackIfNeeded*/, true /*onTop*/);
Andrii Kulian3a1619d2017-07-07 14:38:09 -0700123 service.mStackSupervisor.setFocusStackUnchecked("test", stack);
Bryce Leeaf691c02017-03-20 14:20:22 -0700124 stack.addTask(task, true, "creating test task");
125 task.setStack(stack);
Bryce Lee04ab3462017-04-10 15:06:33 -0700126 task.setWindowContainerController(mock(TaskWindowContainerController.class));
Bryce Leeaf691c02017-03-20 14:20:22 -0700127
128 return task;
129 }
130
Bryce Lee04ab3462017-04-10 15:06:33 -0700131
Bryce Leeaf691c02017-03-20 14:20:22 -0700132 /**
133 * An {@link ActivityManagerService} subclass which provides a test
134 * {@link ActivityStackSupervisor}.
135 */
136 protected static class TestActivityManagerService extends ActivityManagerService {
137 public TestActivityManagerService(Context context) {
138 super(context);
Bryce Lee04ab3462017-04-10 15:06:33 -0700139 mSupportsMultiWindow = true;
140 mSupportsMultiDisplay = true;
141 mWindowManager = WindowTestUtils.getWindowManagerService(context);
Bryce Leeaf691c02017-03-20 14:20:22 -0700142 }
143
144 @Override
145 protected ActivityStackSupervisor createStackSupervisor() {
Bryce Lee3115bdf2017-04-05 08:39:40 -0700146 return new TestActivityStackSupervisor(this, mHandlerThread.getLooper());
Bryce Leeaf691c02017-03-20 14:20:22 -0700147 }
148 }
149
150 /**
151 * An {@link ActivityStackSupervisor} which stubs out certain methods that depend on
152 * setup not available in the test environment. Also specifies an injector for
153 */
154 protected static class TestActivityStackSupervisor extends ActivityStackSupervisor {
Bryce Lee943ebe72017-05-04 10:19:07 -0700155 private final ActivityDisplay mDisplay;
156
Bryce Leeaf691c02017-03-20 14:20:22 -0700157 public TestActivityStackSupervisor(ActivityManagerService service, Looper looper) {
158 super(service, looper);
Bryce Lee04ab3462017-04-10 15:06:33 -0700159 mWindowManager = prepareMockWindowManager();
Bryce Lee943ebe72017-05-04 10:19:07 -0700160 mDisplay = new ActivityDisplay();
Bryce Lee04ab3462017-04-10 15:06:33 -0700161 }
162
163 // No home stack is set.
164 @Override
165 void moveHomeStackToFront(String reason) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700166 }
167
Bryce Lee3345c4e2017-04-25 07:40:41 -0700168 @Override
169 boolean moveHomeStackTaskToTop(String reason) {
170 return true;
171 }
172
Bryce Leeaf691c02017-03-20 14:20:22 -0700173 // Invoked during {@link ActivityStack} creation.
174 @Override
175 void updateUIDsPresentOnDisplay() {
176 }
177
Bryce Lee04ab3462017-04-10 15:06:33 -0700178 // Just return the current front task.
179 @Override
180 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) {
181 return mFocusedStack;
182 }
183
184 // Called when moving activity to pinned stack.
185 @Override
186 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
187 boolean preserveWindows) {
188 }
189
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700190 <T extends ActivityStack> T createTestStack(int stackId, boolean onTop) {
191 return (T) createStack(stackId, mDisplay, onTop);
192 }
193
194 @Override
195 ActivityStack createStack(int stackId, ActivityDisplay display, boolean onTop) {
196 final RecentTasks recents =
197 new RecentTasks(mService, mService.mStackSupervisor);
198 if (stackId == PINNED_STACK_ID) {
199 return new PinnedActivityStack(display, stackId, this, recents, onTop) {
200 @Override
201 Rect getDefaultPictureInPictureBounds(float aspectRatio) {
202 return new Rect(50, 50, 100, 100);
203 }
204 };
205 } else {
206 return new TestActivityStack(display, stackId, this, recents, onTop);
207 }
Bryce Lee04ab3462017-04-10 15:06:33 -0700208 }
209
210 @Override
211 protected <T extends ActivityStack> T getStack(int stackId,
212 boolean createStaticStackIfNeeded, boolean createOnTop) {
213 final T stack = super.getStack(stackId, createStaticStackIfNeeded, createOnTop);
214
215 if (stack != null || !createStaticStackIfNeeded) {
216 return stack;
217 }
218
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700219 return createTestStack(stackId, createOnTop);
Bryce Leeaf691c02017-03-20 14:20:22 -0700220 }
David Stevensf62360c2017-03-16 19:00:20 -0700221
222 // Always keep things awake
223 @Override
224 boolean hasAwakeDisplay() {
225 return true;
226 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700227 }
228
Bryce Lee04ab3462017-04-10 15:06:33 -0700229 private static WindowManagerService prepareMockWindowManager() {
230 final WindowManagerService service = mock(WindowManagerService.class);
231
232 doAnswer((InvocationOnMock invocationOnMock) -> {
233 final Runnable runnable = invocationOnMock.<Runnable>getArgument(0);
234 if (runnable != null) {
235 runnable.run();
236 }
237 return null;
238 }).when(service).inSurfaceTransaction(any());
239
240 return service;
241 }
242
243 protected interface ActivityStackReporter {
244 int onActivityRemovedFromStackInvocationCount();
245 }
246
Bryce Leeaf691c02017-03-20 14:20:22 -0700247 /**
248 * Override of {@link ActivityStack} that tracks test metrics, such as the number of times a
249 * method is called. Note that its functionality depends on the implementations of the
250 * construction arguments.
251 */
252 protected static class TestActivityStack<T extends StackWindowController>
Bryce Lee04ab3462017-04-10 15:06:33 -0700253 extends ActivityStack<T> implements ActivityStackReporter {
Bryce Leeaf691c02017-03-20 14:20:22 -0700254 private int mOnActivityRemovedFromStackCount = 0;
255 private T mContainerController;
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700256 TestActivityStack(ActivityStackSupervisor.ActivityDisplay display, int stackId,
257 ActivityStackSupervisor supervisor, RecentTasks recentTasks, boolean onTop) {
258 super(display, stackId, supervisor, recentTasks, onTop);
Bryce Leeaf691c02017-03-20 14:20:22 -0700259 }
260
261 @Override
262 void onActivityRemovedFromStack(ActivityRecord r) {
263 mOnActivityRemovedFromStackCount++;
264 super.onActivityRemovedFromStack(r);
265 }
266
267 // Returns the number of times {@link #onActivityRemovedFromStack} has been called
Bryce Lee04ab3462017-04-10 15:06:33 -0700268 @Override
Bryce Leeaf691c02017-03-20 14:20:22 -0700269 public int onActivityRemovedFromStackInvocationCount() {
270 return mOnActivityRemovedFromStackCount;
271 }
272
273 @Override
274 protected T createStackWindowController(int displayId, boolean onTop,
275 Rect outBounds) {
276 mContainerController = (T) WindowTestUtils.createMockStackWindowContainerController();
277 return mContainerController;
278 }
279
280 @Override
281 T getWindowContainerController() {
282 return mContainerController;
283 }
284 }
285
Bryce Lee04ab3462017-04-10 15:06:33 -0700286
Bryce Leeaf691c02017-03-20 14:20:22 -0700287 protected static class ActivityStackBuilder {
288 private boolean mOnTop = true;
289 private int mStackId = 0;
290 private int mDisplayId = 1;
291
292 private final ActivityManagerService mService;
293
294 public ActivityStackBuilder(ActivityManagerService ams) {
295 mService = ams;
296 }
297
298 public ActivityStackBuilder setOnTop(boolean onTop) {
299 mOnTop = onTop;
300 return this;
301 }
302
303 public ActivityStackBuilder setStackId(int id) {
304 mStackId = id;
305 return this;
306 }
307
308 public ActivityStackBuilder setDisplayId(int id) {
309 mDisplayId = id;
310 return this;
311 }
312
Bryce Lee04ab3462017-04-10 15:06:33 -0700313 public ActivityStack build() {
Bryce Leeaf691c02017-03-20 14:20:22 -0700314 return createActivityStack(mService, mStackId, mDisplayId, mOnTop);
315 }
316 }
317}