blob: c03a95789be481c66b4621fa79c91aa2659b6cda [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 }
Bryce Lee29a649d2017-08-18 13:52:31 -0700148
149 @Override
150 void updateUsageStats(ActivityRecord component, boolean resumed) {
151 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700152 }
153
154 /**
155 * An {@link ActivityStackSupervisor} which stubs out certain methods that depend on
156 * setup not available in the test environment. Also specifies an injector for
157 */
158 protected static class TestActivityStackSupervisor extends ActivityStackSupervisor {
Bryce Lee943ebe72017-05-04 10:19:07 -0700159 private final ActivityDisplay mDisplay;
160
Bryce Leeaf691c02017-03-20 14:20:22 -0700161 public TestActivityStackSupervisor(ActivityManagerService service, Looper looper) {
162 super(service, looper);
Bryce Lee04ab3462017-04-10 15:06:33 -0700163 mWindowManager = prepareMockWindowManager();
Bryce Lee943ebe72017-05-04 10:19:07 -0700164 mDisplay = new ActivityDisplay();
Bryce Lee04ab3462017-04-10 15:06:33 -0700165 }
166
167 // No home stack is set.
168 @Override
169 void moveHomeStackToFront(String reason) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700170 }
171
Bryce Lee3345c4e2017-04-25 07:40:41 -0700172 @Override
173 boolean moveHomeStackTaskToTop(String reason) {
174 return true;
175 }
176
Bryce Leeaf691c02017-03-20 14:20:22 -0700177 // Invoked during {@link ActivityStack} creation.
178 @Override
179 void updateUIDsPresentOnDisplay() {
180 }
181
Bryce Lee04ab3462017-04-10 15:06:33 -0700182 // Just return the current front task.
183 @Override
184 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) {
185 return mFocusedStack;
186 }
187
188 // Called when moving activity to pinned stack.
189 @Override
190 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
191 boolean preserveWindows) {
192 }
193
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700194 <T extends ActivityStack> T createTestStack(int stackId, boolean onTop) {
195 return (T) createStack(stackId, mDisplay, onTop);
196 }
197
198 @Override
199 ActivityStack createStack(int stackId, ActivityDisplay display, boolean onTop) {
200 final RecentTasks recents =
201 new RecentTasks(mService, mService.mStackSupervisor);
202 if (stackId == PINNED_STACK_ID) {
203 return new PinnedActivityStack(display, stackId, this, recents, onTop) {
204 @Override
205 Rect getDefaultPictureInPictureBounds(float aspectRatio) {
206 return new Rect(50, 50, 100, 100);
207 }
208 };
209 } else {
210 return new TestActivityStack(display, stackId, this, recents, onTop);
211 }
Bryce Lee04ab3462017-04-10 15:06:33 -0700212 }
213
214 @Override
215 protected <T extends ActivityStack> T getStack(int stackId,
216 boolean createStaticStackIfNeeded, boolean createOnTop) {
217 final T stack = super.getStack(stackId, createStaticStackIfNeeded, createOnTop);
218
219 if (stack != null || !createStaticStackIfNeeded) {
220 return stack;
221 }
222
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700223 return createTestStack(stackId, createOnTop);
Bryce Leeaf691c02017-03-20 14:20:22 -0700224 }
David Stevensf62360c2017-03-16 19:00:20 -0700225
226 // Always keep things awake
227 @Override
228 boolean hasAwakeDisplay() {
229 return true;
230 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700231 }
232
Bryce Lee04ab3462017-04-10 15:06:33 -0700233 private static WindowManagerService prepareMockWindowManager() {
234 final WindowManagerService service = mock(WindowManagerService.class);
235
236 doAnswer((InvocationOnMock invocationOnMock) -> {
237 final Runnable runnable = invocationOnMock.<Runnable>getArgument(0);
238 if (runnable != null) {
239 runnable.run();
240 }
241 return null;
242 }).when(service).inSurfaceTransaction(any());
243
244 return service;
245 }
246
247 protected interface ActivityStackReporter {
248 int onActivityRemovedFromStackInvocationCount();
249 }
250
Bryce Leeaf691c02017-03-20 14:20:22 -0700251 /**
252 * Override of {@link ActivityStack} that tracks test metrics, such as the number of times a
253 * method is called. Note that its functionality depends on the implementations of the
254 * construction arguments.
255 */
256 protected static class TestActivityStack<T extends StackWindowController>
Bryce Lee04ab3462017-04-10 15:06:33 -0700257 extends ActivityStack<T> implements ActivityStackReporter {
Bryce Leeaf691c02017-03-20 14:20:22 -0700258 private int mOnActivityRemovedFromStackCount = 0;
259 private T mContainerController;
Andrii Kulian94e82d9b02017-07-13 15:33:06 -0700260 TestActivityStack(ActivityStackSupervisor.ActivityDisplay display, int stackId,
261 ActivityStackSupervisor supervisor, RecentTasks recentTasks, boolean onTop) {
262 super(display, stackId, supervisor, recentTasks, onTop);
Bryce Leeaf691c02017-03-20 14:20:22 -0700263 }
264
265 @Override
266 void onActivityRemovedFromStack(ActivityRecord r) {
267 mOnActivityRemovedFromStackCount++;
268 super.onActivityRemovedFromStack(r);
269 }
270
271 // Returns the number of times {@link #onActivityRemovedFromStack} has been called
Bryce Lee04ab3462017-04-10 15:06:33 -0700272 @Override
Bryce Leeaf691c02017-03-20 14:20:22 -0700273 public int onActivityRemovedFromStackInvocationCount() {
274 return mOnActivityRemovedFromStackCount;
275 }
276
277 @Override
278 protected T createStackWindowController(int displayId, boolean onTop,
279 Rect outBounds) {
280 mContainerController = (T) WindowTestUtils.createMockStackWindowContainerController();
281 return mContainerController;
282 }
283
284 @Override
285 T getWindowContainerController() {
286 return mContainerController;
287 }
288 }
289
Bryce Lee04ab3462017-04-10 15:06:33 -0700290
Bryce Leeaf691c02017-03-20 14:20:22 -0700291 protected static class ActivityStackBuilder {
292 private boolean mOnTop = true;
293 private int mStackId = 0;
294 private int mDisplayId = 1;
295
296 private final ActivityManagerService mService;
297
298 public ActivityStackBuilder(ActivityManagerService ams) {
299 mService = ams;
300 }
301
302 public ActivityStackBuilder setOnTop(boolean onTop) {
303 mOnTop = onTop;
304 return this;
305 }
306
307 public ActivityStackBuilder setStackId(int id) {
308 mStackId = id;
309 return this;
310 }
311
312 public ActivityStackBuilder setDisplayId(int id) {
313 mDisplayId = id;
314 return this;
315 }
316
Bryce Lee04ab3462017-04-10 15:06:33 -0700317 public ActivityStack build() {
Bryce Leeaf691c02017-03-20 14:20:22 -0700318 return createActivityStack(mService, mStackId, mDisplayId, mOnTop);
319 }
320 }
321}