blob: 37b9835089c264990ea8be884f96e13e39badfad [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 Kulianb1cdb102017-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 Kulianb1cdb102017-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 Kulianb1cdb102017-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*/);
Bryce Leeaf691c02017-03-20 14:20:22 -0700123 stack.addTask(task, true, "creating test task");
124 task.setStack(stack);
Bryce Lee04ab3462017-04-10 15:06:33 -0700125 task.setWindowContainerController(mock(TaskWindowContainerController.class));
Bryce Leeaf691c02017-03-20 14:20:22 -0700126
127 return task;
128 }
129
Bryce Lee04ab3462017-04-10 15:06:33 -0700130
Bryce Leeaf691c02017-03-20 14:20:22 -0700131 /**
132 * An {@link ActivityManagerService} subclass which provides a test
133 * {@link ActivityStackSupervisor}.
134 */
135 protected static class TestActivityManagerService extends ActivityManagerService {
136 public TestActivityManagerService(Context context) {
137 super(context);
Bryce Lee04ab3462017-04-10 15:06:33 -0700138 mSupportsMultiWindow = true;
139 mSupportsMultiDisplay = true;
140 mWindowManager = WindowTestUtils.getWindowManagerService(context);
Bryce Leeaf691c02017-03-20 14:20:22 -0700141 }
142
143 @Override
144 protected ActivityStackSupervisor createStackSupervisor() {
Bryce Lee3115bdf2017-04-05 08:39:40 -0700145 return new TestActivityStackSupervisor(this, mHandlerThread.getLooper());
Bryce Leeaf691c02017-03-20 14:20:22 -0700146 }
147 }
148
149 /**
150 * An {@link ActivityStackSupervisor} which stubs out certain methods that depend on
151 * setup not available in the test environment. Also specifies an injector for
152 */
153 protected static class TestActivityStackSupervisor extends ActivityStackSupervisor {
Bryce Lee943ebe72017-05-04 10:19:07 -0700154 private final ActivityDisplay mDisplay;
155
Bryce Leeaf691c02017-03-20 14:20:22 -0700156 public TestActivityStackSupervisor(ActivityManagerService service, Looper looper) {
157 super(service, looper);
Bryce Lee04ab3462017-04-10 15:06:33 -0700158 mWindowManager = prepareMockWindowManager();
Bryce Lee943ebe72017-05-04 10:19:07 -0700159 mDisplay = new ActivityDisplay();
Bryce Lee04ab3462017-04-10 15:06:33 -0700160 }
161
162 // No home stack is set.
163 @Override
164 void moveHomeStackToFront(String reason) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700165 }
166
Bryce Lee3345c4e2017-04-25 07:40:41 -0700167 @Override
168 boolean moveHomeStackTaskToTop(String reason) {
169 return true;
170 }
171
Bryce Leeaf691c02017-03-20 14:20:22 -0700172 // Invoked during {@link ActivityStack} creation.
173 @Override
174 void updateUIDsPresentOnDisplay() {
175 }
176
Bryce Lee04ab3462017-04-10 15:06:33 -0700177 // Just return the current front task.
178 @Override
179 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) {
180 return mFocusedStack;
181 }
182
183 // Called when moving activity to pinned stack.
184 @Override
185 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
186 boolean preserveWindows) {
187 }
188
Andrii Kulianb1cdb102017-07-13 15:33:06 -0700189 <T extends ActivityStack> T createTestStack(int stackId, boolean onTop) {
190 return (T) createStack(stackId, mDisplay, onTop);
191 }
192
193 @Override
194 ActivityStack createStack(int stackId, ActivityDisplay display, boolean onTop) {
195 final RecentTasks recents =
196 new RecentTasks(mService, mService.mStackSupervisor);
197 if (stackId == PINNED_STACK_ID) {
198 return new PinnedActivityStack(display, stackId, this, recents, onTop) {
199 @Override
200 Rect getDefaultPictureInPictureBounds(float aspectRatio) {
201 return new Rect(50, 50, 100, 100);
202 }
203 };
204 } else {
205 return new TestActivityStack(display, stackId, this, recents, onTop);
206 }
Bryce Lee04ab3462017-04-10 15:06:33 -0700207 }
208
209 @Override
210 protected <T extends ActivityStack> T getStack(int stackId,
211 boolean createStaticStackIfNeeded, boolean createOnTop) {
212 final T stack = super.getStack(stackId, createStaticStackIfNeeded, createOnTop);
213
214 if (stack != null || !createStaticStackIfNeeded) {
215 return stack;
216 }
217
Andrii Kulianb1cdb102017-07-13 15:33:06 -0700218 return createTestStack(stackId, createOnTop);
Bryce Leeaf691c02017-03-20 14:20:22 -0700219 }
220 }
221
Bryce Lee04ab3462017-04-10 15:06:33 -0700222 private static WindowManagerService prepareMockWindowManager() {
223 final WindowManagerService service = mock(WindowManagerService.class);
224
225 doAnswer((InvocationOnMock invocationOnMock) -> {
226 final Runnable runnable = invocationOnMock.<Runnable>getArgument(0);
227 if (runnable != null) {
228 runnable.run();
229 }
230 return null;
231 }).when(service).inSurfaceTransaction(any());
232
233 return service;
234 }
235
236 protected interface ActivityStackReporter {
237 int onActivityRemovedFromStackInvocationCount();
238 }
239
Bryce Leeaf691c02017-03-20 14:20:22 -0700240 /**
241 * Override of {@link ActivityStack} that tracks test metrics, such as the number of times a
242 * method is called. Note that its functionality depends on the implementations of the
243 * construction arguments.
244 */
245 protected static class TestActivityStack<T extends StackWindowController>
Bryce Lee04ab3462017-04-10 15:06:33 -0700246 extends ActivityStack<T> implements ActivityStackReporter {
Bryce Leeaf691c02017-03-20 14:20:22 -0700247 private int mOnActivityRemovedFromStackCount = 0;
248 private T mContainerController;
Andrii Kulianb1cdb102017-07-13 15:33:06 -0700249 TestActivityStack(ActivityStackSupervisor.ActivityDisplay display, int stackId,
250 ActivityStackSupervisor supervisor, RecentTasks recentTasks, boolean onTop) {
251 super(display, stackId, supervisor, recentTasks, onTop);
Bryce Leeaf691c02017-03-20 14:20:22 -0700252 }
253
254 @Override
255 void onActivityRemovedFromStack(ActivityRecord r) {
256 mOnActivityRemovedFromStackCount++;
257 super.onActivityRemovedFromStack(r);
258 }
259
260 // Returns the number of times {@link #onActivityRemovedFromStack} has been called
Bryce Lee04ab3462017-04-10 15:06:33 -0700261 @Override
Bryce Leeaf691c02017-03-20 14:20:22 -0700262 public int onActivityRemovedFromStackInvocationCount() {
263 return mOnActivityRemovedFromStackCount;
264 }
265
266 @Override
267 protected T createStackWindowController(int displayId, boolean onTop,
268 Rect outBounds) {
269 mContainerController = (T) WindowTestUtils.createMockStackWindowContainerController();
270 return mContainerController;
271 }
272
273 @Override
274 T getWindowContainerController() {
275 return mContainerController;
276 }
277 }
278
Bryce Lee04ab3462017-04-10 15:06:33 -0700279
Bryce Leeaf691c02017-03-20 14:20:22 -0700280 protected static class ActivityStackBuilder {
281 private boolean mOnTop = true;
282 private int mStackId = 0;
283 private int mDisplayId = 1;
284
285 private final ActivityManagerService mService;
286
287 public ActivityStackBuilder(ActivityManagerService ams) {
288 mService = ams;
289 }
290
291 public ActivityStackBuilder setOnTop(boolean onTop) {
292 mOnTop = onTop;
293 return this;
294 }
295
296 public ActivityStackBuilder setStackId(int id) {
297 mStackId = id;
298 return this;
299 }
300
301 public ActivityStackBuilder setDisplayId(int id) {
302 mDisplayId = id;
303 return this;
304 }
305
Bryce Lee04ab3462017-04-10 15:06:33 -0700306 public ActivityStack build() {
Bryce Leeaf691c02017-03-20 14:20:22 -0700307 return createActivityStack(mService, mStackId, mDisplayId, mOnTop);
308 }
309 }
310}