blob: 1f6dda191d911f393423c64a1117a0b68efd7fbd [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
58 // Grabbing an instance of {@link WindowManagerService} creates it if not present so this must
59 // be called at before any tests.
60 private final WindowManagerService mWms = WindowTestUtils.getWindowManagerService(mContext);
61
62 @Before
63 public void setUp() throws Exception {
64 MockitoAnnotations.initMocks(this);
Bryce Lee3115bdf2017-04-05 08:39:40 -070065 mHandlerThread = new HandlerThread("ActivityTestsBaseThread");
66 mHandlerThread.start();
67 }
Bryce Leeaf691c02017-03-20 14:20:22 -070068
Bryce Lee3115bdf2017-04-05 08:39:40 -070069 @After
70 public void tearDown() {
71 mHandlerThread.quitSafely();
Bryce Leeaf691c02017-03-20 14:20:22 -070072 }
73
74 protected ActivityManagerService createActivityManagerService() {
Bryce Lee4e4a3ec2017-09-27 08:25:03 -070075 final ActivityManagerService service = spy(new TestActivityManagerService(mContext));
76 service.mWindowManager = prepareMockWindowManager();
Bryce Lee840c5662017-04-13 10:02:51 -070077 return service;
Bryce Leeaf691c02017-03-20 14:20:22 -070078 }
79
Bryce Leeaf691c02017-03-20 14:20:22 -070080 protected static ActivityRecord createActivity(ActivityManagerService service,
81 ComponentName component, TaskRecord task) {
Bryce Lee9f6affd2017-09-01 09:18:35 -070082 return createActivity(service, component, task, 0 /* userId */);
83 }
84
85 protected static ActivityRecord createActivity(ActivityManagerService service,
86 ComponentName component, TaskRecord task, int uid) {
Bryce Leeaf691c02017-03-20 14:20:22 -070087 Intent intent = new Intent();
88 intent.setComponent(component);
89 final ActivityInfo aInfo = new ActivityInfo();
90 aInfo.applicationInfo = new ApplicationInfo();
91 aInfo.applicationInfo.packageName = component.getPackageName();
Bryce Lee9f6affd2017-09-01 09:18:35 -070092 aInfo.applicationInfo.uid = uid;
Bryce Leeaf691c02017-03-20 14:20:22 -070093 AttributeCache.init(service.mContext);
94 final ActivityRecord activity = new ActivityRecord(service, null /* caller */,
95 0 /* launchedFromPid */, 0, null, intent, null,
96 aInfo /*aInfo*/, new Configuration(), null /* resultTo */, null /* resultWho */,
97 0 /* reqCode */, false /*componentSpecified*/, false /* rootVoiceInteraction */,
Andrii Kulianb1cdb102017-07-13 15:33:06 -070098 service.mStackSupervisor, null /* options */, null /* sourceRecord */);
Bryce Leeaf691c02017-03-20 14:20:22 -070099 activity.mWindowContainerController = mock(AppWindowContainerController.class);
100
101 if (task != null) {
102 task.addActivityToTop(activity);
103 }
104
105 return activity;
106 }
107
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700108 protected static TaskRecord createTask(ActivityStackSupervisor supervisor,
109 ComponentName component, ActivityStack stack) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700110 final ActivityInfo aInfo = new ActivityInfo();
111 aInfo.applicationInfo = new ApplicationInfo();
112 aInfo.applicationInfo.packageName = component.getPackageName();
113
114 Intent intent = new Intent();
115 intent.setComponent(component);
116
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700117 final TaskRecord task = new TaskRecord(supervisor.mService, 0, aInfo, intent /*intent*/,
Jorim Jaggie7d2b852017-08-28 17:55:15 +0200118 null /*_taskDescription*/);
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700119 supervisor.setFocusStackUnchecked("test", stack);
Bryce Leeaf691c02017-03-20 14:20:22 -0700120 stack.addTask(task, true, "creating test task");
121 task.setStack(stack);
Bryce Lee04ab3462017-04-10 15:06:33 -0700122 task.setWindowContainerController(mock(TaskWindowContainerController.class));
Bryce Leeaf691c02017-03-20 14:20:22 -0700123
124 return task;
125 }
126
127 /**
128 * An {@link ActivityManagerService} subclass which provides a test
129 * {@link ActivityStackSupervisor}.
130 */
131 protected static class TestActivityManagerService extends ActivityManagerService {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700132 TestActivityManagerService(Context context) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700133 super(context);
Bryce Lee04ab3462017-04-10 15:06:33 -0700134 mSupportsMultiWindow = true;
135 mSupportsMultiDisplay = true;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700136 mSupportsSplitScreenMultiWindow = true;
137 mSupportsFreeformWindowManagement = true;
138 mSupportsPictureInPicture = true;
Bryce Lee04ab3462017-04-10 15:06:33 -0700139 mWindowManager = WindowTestUtils.getWindowManagerService(context);
Bryce Leeaf691c02017-03-20 14:20:22 -0700140 }
141
142 @Override
143 protected ActivityStackSupervisor createStackSupervisor() {
Bryce Lee3115bdf2017-04-05 08:39:40 -0700144 return new TestActivityStackSupervisor(this, mHandlerThread.getLooper());
Bryce Leeaf691c02017-03-20 14:20:22 -0700145 }
Bryce Lee29a649d2017-08-18 13:52:31 -0700146
147 @Override
148 void updateUsageStats(ActivityRecord component, boolean resumed) {
149 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700150 }
151
152 /**
153 * An {@link ActivityStackSupervisor} which stubs out certain methods that depend on
154 * setup not available in the test environment. Also specifies an injector for
155 */
156 protected static class TestActivityStackSupervisor extends ActivityStackSupervisor {
Bryce Lee943ebe72017-05-04 10:19:07 -0700157 private final ActivityDisplay mDisplay;
Bryce Lee1533b2b2017-09-14 17:06:41 -0700158 private boolean mLastResizeable;
Bryce Lee943ebe72017-05-04 10:19:07 -0700159
Bryce Leeaf691c02017-03-20 14:20:22 -0700160 public TestActivityStackSupervisor(ActivityManagerService service, Looper looper) {
161 super(service, looper);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700162 mDisplayManager =
163 (DisplayManager) mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
Bryce Lee04ab3462017-04-10 15:06:33 -0700164 mWindowManager = prepareMockWindowManager();
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700165 mDisplay = new TestActivityDisplay(this, DEFAULT_DISPLAY);
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700166 attachDisplay(mDisplay);
Bryce Lee04ab3462017-04-10 15:06:33 -0700167 }
168
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700169 @Override
170 ActivityDisplay getDefaultDisplay() {
171 return mDisplay;
172 }
173
Bryce Lee1533b2b2017-09-14 17:06:41 -0700174 // TODO: Use Mockito spy instead. Currently not possible due to TestActivityStackSupervisor
175 // access to ActivityDisplay
176 @Override
177 boolean canPlaceEntityOnDisplay(int displayId, boolean resizeable, int callingPid,
178 int callingUid, ActivityInfo activityInfo) {
179 mLastResizeable = resizeable;
180 return super.canPlaceEntityOnDisplay(displayId, resizeable, callingPid, callingUid,
181 activityInfo);
182 }
183
184 // TODO: remove and use Mockito verify once {@link #canPlaceEntityOnDisplay} override is
185 // removed.
186 public boolean getLastResizeableFromCanPlaceEntityOnDisplay() {
187 return mLastResizeable;
188 }
189
Bryce Lee04ab3462017-04-10 15:06:33 -0700190 // No home stack is set.
191 @Override
192 void moveHomeStackToFront(String reason) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700193 }
194
Bryce Lee3345c4e2017-04-25 07:40:41 -0700195 @Override
196 boolean moveHomeStackTaskToTop(String reason) {
197 return true;
198 }
199
Bryce Leeaf691c02017-03-20 14:20:22 -0700200 // Invoked during {@link ActivityStack} creation.
201 @Override
202 void updateUIDsPresentOnDisplay() {
203 }
204
Bryce Lee04ab3462017-04-10 15:06:33 -0700205 // Just return the current front task.
206 @Override
207 ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) {
208 return mFocusedStack;
209 }
210
211 // Called when moving activity to pinned stack.
212 @Override
213 void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
214 boolean preserveWindows) {
215 }
216
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700217 // Always keep things awake
218 @Override
219 boolean hasAwakeDisplay() {
220 return true;
221 }
222 }
223
224 private static class TestActivityDisplay extends ActivityDisplay {
225
226 private final ActivityStackSupervisor mSupervisor;
227 TestActivityDisplay(ActivityStackSupervisor supervisor, int displayId) {
228 super(supervisor, displayId);
229 mSupervisor = supervisor;
Andrii Kulianb1cdb102017-07-13 15:33:06 -0700230 }
231
232 @Override
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700233 <T extends ActivityStack> T createStackUnchecked(int windowingMode, int activityType,
234 int stackId, boolean onTop) {
235 if (windowingMode == WINDOWING_MODE_PINNED) {
236 return (T) new PinnedActivityStack(this, stackId, mSupervisor, onTop) {
Andrii Kulianb1cdb102017-07-13 15:33:06 -0700237 @Override
238 Rect getDefaultPictureInPictureBounds(float aspectRatio) {
239 return new Rect(50, 50, 100, 100);
240 }
Bryce Lee4e4a3ec2017-09-27 08:25:03 -0700241
242 @Override
243 PinnedStackWindowController createStackWindowController(int displayId,
244 boolean onTop, Rect outBounds) {
245 return mock(PinnedStackWindowController.class);
246 }
Andrii Kulianb1cdb102017-07-13 15:33:06 -0700247 };
248 } else {
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700249 return (T) new TestActivityStack(
250 this, stackId, mSupervisor, windowingMode, activityType, onTop);
Andrii Kulianb1cdb102017-07-13 15:33:06 -0700251 }
Bryce Lee04ab3462017-04-10 15:06:33 -0700252 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700253 }
254
Bryce Lee04ab3462017-04-10 15:06:33 -0700255 private static WindowManagerService prepareMockWindowManager() {
Bryce Lee4e4a3ec2017-09-27 08:25:03 -0700256 final WindowManagerService service = WindowTestUtils.getMockWindowManagerService();
Bryce Lee04ab3462017-04-10 15:06:33 -0700257
258 doAnswer((InvocationOnMock invocationOnMock) -> {
259 final Runnable runnable = invocationOnMock.<Runnable>getArgument(0);
260 if (runnable != null) {
261 runnable.run();
262 }
263 return null;
264 }).when(service).inSurfaceTransaction(any());
265
266 return service;
267 }
268
269 protected interface ActivityStackReporter {
270 int onActivityRemovedFromStackInvocationCount();
271 }
272
Bryce Leeaf691c02017-03-20 14:20:22 -0700273 /**
Bryce Lee4e4a3ec2017-09-27 08:25:03 -0700274 * Overrided of {@link ActivityStack} that tracks test metrics, such as the number of times a
Bryce Leeaf691c02017-03-20 14:20:22 -0700275 * method is called. Note that its functionality depends on the implementations of the
276 * construction arguments.
277 */
278 protected static class TestActivityStack<T extends StackWindowController>
Bryce Lee04ab3462017-04-10 15:06:33 -0700279 extends ActivityStack<T> implements ActivityStackReporter {
Bryce Leeaf691c02017-03-20 14:20:22 -0700280 private int mOnActivityRemovedFromStackCount = 0;
281 private T mContainerController;
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700282
Wale Ogunwale9dcf9462017-09-19 15:13:01 -0700283 TestActivityStack(ActivityDisplay display, int stackId, ActivityStackSupervisor supervisor,
Wale Ogunwale04a05ac2017-09-17 21:35:02 -0700284 int windowingMode, int activityType, boolean onTop) {
285 super(display, stackId, supervisor, windowingMode, activityType, onTop);
Bryce Leeaf691c02017-03-20 14:20:22 -0700286 }
287
288 @Override
289 void onActivityRemovedFromStack(ActivityRecord r) {
290 mOnActivityRemovedFromStackCount++;
291 super.onActivityRemovedFromStack(r);
292 }
293
294 // Returns the number of times {@link #onActivityRemovedFromStack} has been called
Bryce Lee04ab3462017-04-10 15:06:33 -0700295 @Override
Bryce Leeaf691c02017-03-20 14:20:22 -0700296 public int onActivityRemovedFromStackInvocationCount() {
297 return mOnActivityRemovedFromStackCount;
298 }
299
300 @Override
Wale Ogunwale034a8ec2017-09-02 17:14:40 -0700301 protected T createStackWindowController(int displayId, boolean onTop, Rect outBounds) {
Bryce Leeaf691c02017-03-20 14:20:22 -0700302 mContainerController = (T) WindowTestUtils.createMockStackWindowContainerController();
303 return mContainerController;
304 }
305
306 @Override
307 T getWindowContainerController() {
308 return mContainerController;
309 }
310 }
Bryce Leeaf691c02017-03-20 14:20:22 -0700311}