Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.am; |
| 18 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 19 | import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD; |
| 20 | import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN; |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 21 | import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED; |
Bryce Lee | f3c6a47 | 2017-11-14 14:53:06 -0800 | [diff] [blame] | 22 | import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY; |
Wale Ogunwale | 9dcf946 | 2017-09-19 15:13:01 -0700 | [diff] [blame] | 23 | import static android.view.Display.DEFAULT_DISPLAY; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 24 | import static org.mockito.Mockito.mock; |
Bryce Lee | 2a3cc46 | 2017-10-27 10:57:35 -0700 | [diff] [blame] | 25 | import static org.mockito.Mockito.doNothing; |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 26 | import static org.mockito.Mockito.doReturn; |
| 27 | import static org.mockito.Mockito.any; |
Bryce Lee | 2a3cc46 | 2017-10-27 10:57:35 -0700 | [diff] [blame] | 28 | import static org.mockito.Mockito.anyBoolean; |
| 29 | import static org.mockito.Mockito.anyInt; |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 30 | import static org.mockito.Mockito.doAnswer; |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 31 | import static org.mockito.Mockito.spy; |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 32 | |
Winson Chung | 59a47ded | 2018-01-25 17:46:06 +0000 | [diff] [blame] | 33 | import com.android.server.wm.DisplayWindowController; |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 34 | import org.mockito.invocation.InvocationOnMock; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 35 | |
Bryce Lee | 0bd8d42 | 2018-01-09 09:45:57 -0800 | [diff] [blame] | 36 | import android.app.IApplicationThread; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 37 | import android.content.ComponentName; |
| 38 | import android.content.Context; |
| 39 | import android.content.Intent; |
| 40 | import android.content.pm.ActivityInfo; |
| 41 | import android.content.pm.ApplicationInfo; |
Bryce Lee | ba8f442 | 2017-11-20 12:35:57 -0800 | [diff] [blame] | 42 | import android.content.pm.IPackageManager; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 43 | import android.content.res.Configuration; |
| 44 | import android.graphics.Rect; |
Wale Ogunwale | 9dcf946 | 2017-09-19 15:13:01 -0700 | [diff] [blame] | 45 | import android.hardware.display.DisplayManager; |
Bryce Lee | 3115bdf | 2017-04-05 08:39:40 -0700 | [diff] [blame] | 46 | import android.os.HandlerThread; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 47 | import android.os.Looper; |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 48 | import android.service.voice.IVoiceInteractionSession; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 49 | import android.support.test.InstrumentationRegistry; |
| 50 | import com.android.server.AttributeCache; |
| 51 | import com.android.server.wm.AppWindowContainerController; |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 52 | import com.android.server.wm.PinnedStackWindowController; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 53 | import com.android.server.wm.StackWindowController; |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 54 | import com.android.server.wm.TaskWindowContainerController; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 55 | import com.android.server.wm.WindowManagerService; |
| 56 | import com.android.server.wm.WindowTestUtils; |
Bryce Lee | 3115bdf | 2017-04-05 08:39:40 -0700 | [diff] [blame] | 57 | import org.junit.After; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 58 | import org.junit.Before; |
| 59 | import org.mockito.MockitoAnnotations; |
| 60 | |
| 61 | /** |
| 62 | * A base class to handle common operations in activity related unit tests. |
| 63 | */ |
| 64 | public class ActivityTestsBase { |
Bryce Lee | 939a9a3 | 2017-10-23 10:01:21 -0700 | [diff] [blame] | 65 | private static boolean sOneTimeSetupDone = false; |
| 66 | |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 67 | private final Context mContext = InstrumentationRegistry.getContext(); |
Bryce Lee | 3115bdf | 2017-04-05 08:39:40 -0700 | [diff] [blame] | 68 | private HandlerThread mHandlerThread; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 69 | |
Bryce Lee | fbd263b4 | 2018-03-07 10:33:55 -0800 | [diff] [blame] | 70 | // Default package name |
| 71 | static final String DEFAULT_COMPONENT_PACKAGE_NAME = "com.foo"; |
| 72 | |
| 73 | // Default base activity name |
| 74 | private static final String DEFAULT_COMPONENT_CLASS_NAME = ".BarActivity"; |
| 75 | |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 76 | @Before |
| 77 | public void setUp() throws Exception { |
Bryce Lee | 939a9a3 | 2017-10-23 10:01:21 -0700 | [diff] [blame] | 78 | if (!sOneTimeSetupDone) { |
| 79 | sOneTimeSetupDone = true; |
| 80 | |
| 81 | // Allows to mock package local classes and methods |
| 82 | System.setProperty("dexmaker.share_classloader", "true"); |
| 83 | MockitoAnnotations.initMocks(this); |
| 84 | } |
Bryce Lee | 3115bdf | 2017-04-05 08:39:40 -0700 | [diff] [blame] | 85 | mHandlerThread = new HandlerThread("ActivityTestsBaseThread"); |
| 86 | mHandlerThread.start(); |
| 87 | } |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 88 | |
Bryce Lee | 3115bdf | 2017-04-05 08:39:40 -0700 | [diff] [blame] | 89 | @After |
| 90 | public void tearDown() { |
| 91 | mHandlerThread.quitSafely(); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | protected ActivityManagerService createActivityManagerService() { |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 95 | final ActivityManagerService service = |
| 96 | setupActivityManagerService(new TestActivityManagerService(mContext)); |
| 97 | AttributeCache.init(mContext); |
| 98 | return service; |
Winson Chung | 3f0e59a | 2017-10-25 10:19:05 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | protected ActivityManagerService setupActivityManagerService(ActivityManagerService service) { |
| 102 | service = spy(service); |
Bryce Lee | ba8f442 | 2017-11-20 12:35:57 -0800 | [diff] [blame] | 103 | doReturn(mock(IPackageManager.class)).when(service).getPackageManager(); |
Bryce Lee | ad5b832 | 2018-03-08 14:28:52 -0800 | [diff] [blame] | 104 | doNothing().when(service).grantEphemeralAccessLocked(anyInt(), any(), anyInt(), anyInt()); |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 105 | service.mWindowManager = prepareMockWindowManager(); |
Bryce Lee | 840c566 | 2017-04-13 10:02:51 -0700 | [diff] [blame] | 106 | return service; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 109 | /** |
| 110 | * Builder for creating new activities. |
| 111 | */ |
| 112 | protected static class ActivityBuilder { |
| 113 | // An id appended to the end of the component name to make it unique |
| 114 | private static int sCurrentActivityId = 0; |
Bryce Lee | 9f6affd | 2017-09-01 09:18:35 -0700 | [diff] [blame] | 115 | |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 116 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 117 | |
| 118 | private final ActivityManagerService mService; |
| 119 | |
| 120 | private ComponentName mComponent; |
| 121 | private TaskRecord mTaskRecord; |
| 122 | private int mUid; |
| 123 | private boolean mCreateTask; |
| 124 | private ActivityStack mStack; |
| 125 | |
| 126 | ActivityBuilder(ActivityManagerService service) { |
| 127 | mService = service; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 130 | ActivityBuilder setComponent(ComponentName component) { |
| 131 | mComponent = component; |
| 132 | return this; |
| 133 | } |
| 134 | |
Bryce Lee | ad5b832 | 2018-03-08 14:28:52 -0800 | [diff] [blame] | 135 | static ComponentName getDefaultComponent() { |
| 136 | return ComponentName.createRelative(DEFAULT_COMPONENT_PACKAGE_NAME, |
| 137 | DEFAULT_COMPONENT_PACKAGE_NAME); |
| 138 | } |
| 139 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 140 | ActivityBuilder setTask(TaskRecord task) { |
| 141 | mTaskRecord = task; |
| 142 | return this; |
| 143 | } |
| 144 | |
| 145 | ActivityBuilder setStack(ActivityStack stack) { |
| 146 | mStack = stack; |
| 147 | return this; |
| 148 | } |
| 149 | |
| 150 | ActivityBuilder setCreateTask(boolean createTask) { |
| 151 | mCreateTask = createTask; |
| 152 | return this; |
| 153 | } |
| 154 | |
| 155 | ActivityBuilder setUid(int uid) { |
| 156 | mUid = uid; |
| 157 | return this; |
| 158 | } |
| 159 | |
| 160 | ActivityRecord build() { |
| 161 | if (mComponent == null) { |
| 162 | final int id = sCurrentActivityId++; |
Bryce Lee | fbd263b4 | 2018-03-07 10:33:55 -0800 | [diff] [blame] | 163 | mComponent = ComponentName.createRelative(DEFAULT_COMPONENT_PACKAGE_NAME, |
| 164 | DEFAULT_COMPONENT_CLASS_NAME + id); |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | if (mCreateTask) { |
| 168 | mTaskRecord = new TaskBuilder(mService.mStackSupervisor) |
| 169 | .setComponent(mComponent) |
| 170 | .setStack(mStack).build(); |
| 171 | } |
| 172 | |
| 173 | Intent intent = new Intent(); |
| 174 | intent.setComponent(mComponent); |
| 175 | final ActivityInfo aInfo = new ActivityInfo(); |
| 176 | aInfo.applicationInfo = new ApplicationInfo(); |
| 177 | aInfo.applicationInfo.packageName = mComponent.getPackageName(); |
| 178 | aInfo.applicationInfo.uid = mUid; |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 179 | final ActivityRecord activity = new ActivityRecord(mService, null /* caller */, |
| 180 | 0 /* launchedFromPid */, 0, null, intent, null, |
| 181 | aInfo /*aInfo*/, new Configuration(), null /* resultTo */, null /* resultWho */, |
| 182 | 0 /* reqCode */, false /*componentSpecified*/, false /* rootVoiceInteraction */, |
| 183 | mService.mStackSupervisor, null /* options */, null /* sourceRecord */); |
| 184 | activity.mWindowContainerController = mock(AppWindowContainerController.class); |
| 185 | |
| 186 | if (mTaskRecord != null) { |
| 187 | mTaskRecord.addActivityToTop(activity); |
| 188 | } |
| 189 | |
Bryce Lee | 0bd8d42 | 2018-01-09 09:45:57 -0800 | [diff] [blame] | 190 | activity.setProcess(new ProcessRecord(null, mService.mContext.getApplicationInfo(), |
| 191 | "name", 12345)); |
| 192 | activity.app.thread = mock(IApplicationThread.class); |
| 193 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 194 | return activity; |
| 195 | } |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 198 | /** |
| 199 | * Builder for creating new tasks. |
| 200 | */ |
| 201 | protected static class TaskBuilder { |
Bryce Lee | fbd263b4 | 2018-03-07 10:33:55 -0800 | [diff] [blame] | 202 | // Default package name |
| 203 | static final String DEFAULT_PACKAGE = "com.bar"; |
| 204 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 205 | private final ActivityStackSupervisor mSupervisor; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 206 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 207 | private ComponentName mComponent; |
| 208 | private String mPackage; |
| 209 | private int mFlags = 0; |
| 210 | private int mTaskId = 0; |
Winson Chung | 0ec2a35 | 2017-10-26 11:38:30 -0700 | [diff] [blame] | 211 | private int mUserId = 0; |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 212 | private IVoiceInteractionSession mVoiceSession; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 213 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 214 | private ActivityStack mStack; |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 215 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 216 | TaskBuilder(ActivityStackSupervisor supervisor) { |
| 217 | mSupervisor = supervisor; |
| 218 | } |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 219 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 220 | TaskBuilder setComponent(ComponentName component) { |
| 221 | mComponent = component; |
| 222 | return this; |
| 223 | } |
| 224 | |
| 225 | TaskBuilder setPackage(String packageName) { |
| 226 | mPackage = packageName; |
| 227 | return this; |
| 228 | } |
| 229 | |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 230 | TaskBuilder setVoiceSession(IVoiceInteractionSession session) { |
| 231 | mVoiceSession = session; |
| 232 | return this; |
| 233 | } |
| 234 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 235 | TaskBuilder setFlags(int flags) { |
| 236 | mFlags = flags; |
| 237 | return this; |
| 238 | } |
| 239 | |
| 240 | TaskBuilder setTaskId(int taskId) { |
| 241 | mTaskId = taskId; |
| 242 | return this; |
| 243 | } |
| 244 | |
Winson Chung | 0ec2a35 | 2017-10-26 11:38:30 -0700 | [diff] [blame] | 245 | TaskBuilder setUserId(int userId) { |
| 246 | mUserId = userId; |
| 247 | return this; |
| 248 | } |
| 249 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 250 | TaskBuilder setStack(ActivityStack stack) { |
| 251 | mStack = stack; |
| 252 | return this; |
| 253 | } |
| 254 | |
| 255 | TaskRecord build() { |
| 256 | if (mStack == null) { |
| 257 | mStack = mSupervisor.getDefaultDisplay().createStack( |
| 258 | WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, true /* onTop */); |
| 259 | } |
| 260 | |
| 261 | final ActivityInfo aInfo = new ActivityInfo(); |
| 262 | aInfo.applicationInfo = new ApplicationInfo(); |
| 263 | aInfo.applicationInfo.packageName = mPackage; |
| 264 | |
| 265 | Intent intent = new Intent(); |
Bryce Lee | fbd263b4 | 2018-03-07 10:33:55 -0800 | [diff] [blame] | 266 | if (mComponent == null) { |
| 267 | mComponent = ComponentName.createRelative(DEFAULT_COMPONENT_PACKAGE_NAME, |
| 268 | DEFAULT_COMPONENT_CLASS_NAME); |
| 269 | } |
| 270 | |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 271 | intent.setComponent(mComponent); |
| 272 | intent.setFlags(mFlags); |
| 273 | |
| 274 | final TaskRecord task = new TaskRecord(mSupervisor.mService, mTaskId, aInfo, |
Bryce Lee | 93e7f79 | 2017-10-25 15:54:55 -0700 | [diff] [blame] | 275 | intent /*intent*/, mVoiceSession, null /*_voiceInteractor*/); |
Winson Chung | 0ec2a35 | 2017-10-26 11:38:30 -0700 | [diff] [blame] | 276 | task.userId = mUserId; |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 277 | mSupervisor.setFocusStackUnchecked("test", mStack); |
| 278 | mStack.addTask(task, true, "creating test task"); |
| 279 | task.setStack(mStack); |
| 280 | task.setWindowContainerController(mock(TaskWindowContainerController.class)); |
Winson Chung | 0ec2a35 | 2017-10-26 11:38:30 -0700 | [diff] [blame] | 281 | task.touchActiveTime(); |
Bryce Lee | 18d5159 | 2017-10-25 10:22:19 -0700 | [diff] [blame] | 282 | |
| 283 | return task; |
| 284 | } |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | /** |
| 288 | * An {@link ActivityManagerService} subclass which provides a test |
| 289 | * {@link ActivityStackSupervisor}. |
| 290 | */ |
| 291 | protected static class TestActivityManagerService extends ActivityManagerService { |
Bryce Lee | b0f993f | 2018-03-02 15:38:01 -0800 | [diff] [blame] | 292 | private ClientLifecycleManager mLifecycleManager; |
| 293 | |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 294 | TestActivityManagerService(Context context) { |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 295 | super(context); |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 296 | mSupportsMultiWindow = true; |
| 297 | mSupportsMultiDisplay = true; |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 298 | mSupportsSplitScreenMultiWindow = true; |
| 299 | mSupportsFreeformWindowManagement = true; |
| 300 | mSupportsPictureInPicture = true; |
Bryce Lee | 6a0754a | 2017-10-10 17:53:50 -0700 | [diff] [blame] | 301 | mWindowManager = WindowTestUtils.getMockWindowManagerService(); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | @Override |
Bryce Lee | b0f993f | 2018-03-02 15:38:01 -0800 | [diff] [blame] | 305 | public ClientLifecycleManager getLifecycleManager() { |
| 306 | if (mLifecycleManager == null) { |
| 307 | return super.getLifecycleManager(); |
| 308 | } |
| 309 | return mLifecycleManager; |
| 310 | } |
| 311 | |
| 312 | void setLifecycleManager(ClientLifecycleManager manager) { |
| 313 | mLifecycleManager = manager; |
| 314 | } |
| 315 | |
| 316 | @Override |
Bryce Lee | 2a3cc46 | 2017-10-27 10:57:35 -0700 | [diff] [blame] | 317 | final protected ActivityStackSupervisor createStackSupervisor() { |
| 318 | final ActivityStackSupervisor supervisor = spy(createTestSupervisor()); |
Bryce Lee | 271617a | 2018-03-15 10:39:12 -0700 | [diff] [blame] | 319 | final KeyguardController keyguardController = mock(KeyguardController.class); |
Bryce Lee | 2a3cc46 | 2017-10-27 10:57:35 -0700 | [diff] [blame] | 320 | |
| 321 | // No home stack is set. |
| 322 | doNothing().when(supervisor).moveHomeStackToFront(any()); |
| 323 | doReturn(true).when(supervisor).moveHomeStackTaskToTop(any()); |
| 324 | // Invoked during {@link ActivityStack} creation. |
| 325 | doNothing().when(supervisor).updateUIDsPresentOnDisplay(); |
| 326 | // Always keep things awake. |
| 327 | doReturn(true).when(supervisor).hasAwakeDisplay(); |
| 328 | // Called when moving activity to pinned stack. |
| 329 | doNothing().when(supervisor).ensureActivitiesVisibleLocked(any(), anyInt(), anyBoolean()); |
| 330 | // Do not schedule idle timeouts |
| 331 | doNothing().when(supervisor).scheduleIdleTimeoutLocked(any()); |
Bryce Lee | fbd263b4 | 2018-03-07 10:33:55 -0800 | [diff] [blame] | 332 | // unit test version does not handle launch wake lock |
| 333 | doNothing().when(supervisor).acquireLaunchWakelock(); |
Bryce Lee | 271617a | 2018-03-15 10:39:12 -0700 | [diff] [blame] | 334 | doReturn(keyguardController).when(supervisor).getKeyguardController(); |
Bryce Lee | 2a3cc46 | 2017-10-27 10:57:35 -0700 | [diff] [blame] | 335 | |
| 336 | supervisor.initialize(); |
| 337 | |
| 338 | return supervisor; |
| 339 | } |
| 340 | |
| 341 | protected ActivityStackSupervisor createTestSupervisor() { |
Bryce Lee | 3115bdf | 2017-04-05 08:39:40 -0700 | [diff] [blame] | 342 | return new TestActivityStackSupervisor(this, mHandlerThread.getLooper()); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 343 | } |
Bryce Lee | 29a649d | 2017-08-18 13:52:31 -0700 | [diff] [blame] | 344 | |
| 345 | @Override |
| 346 | void updateUsageStats(ActivityRecord component, boolean resumed) { |
| 347 | } |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | /** |
| 351 | * An {@link ActivityStackSupervisor} which stubs out certain methods that depend on |
| 352 | * setup not available in the test environment. Also specifies an injector for |
| 353 | */ |
| 354 | protected static class TestActivityStackSupervisor extends ActivityStackSupervisor { |
Bryce Lee | 2a3cc46 | 2017-10-27 10:57:35 -0700 | [diff] [blame] | 355 | private ActivityDisplay mDisplay; |
Bryce Lee | 459c062 | 2018-03-19 11:04:01 -0700 | [diff] [blame] | 356 | private KeyguardController mKeyguardController; |
Bryce Lee | 943ebe7 | 2017-05-04 10:19:07 -0700 | [diff] [blame] | 357 | |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 358 | public TestActivityStackSupervisor(ActivityManagerService service, Looper looper) { |
| 359 | super(service, looper); |
Wale Ogunwale | 9dcf946 | 2017-09-19 15:13:01 -0700 | [diff] [blame] | 360 | mDisplayManager = |
| 361 | (DisplayManager) mService.mContext.getSystemService(Context.DISPLAY_SERVICE); |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 362 | mWindowManager = prepareMockWindowManager(); |
Bryce Lee | 459c062 | 2018-03-19 11:04:01 -0700 | [diff] [blame] | 363 | mKeyguardController = mock(KeyguardController.class); |
Bryce Lee | 2a3cc46 | 2017-10-27 10:57:35 -0700 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | @Override |
| 367 | public void initialize() { |
| 368 | super.initialize(); |
Bryce Lee | 459c062 | 2018-03-19 11:04:01 -0700 | [diff] [blame] | 369 | mDisplay = spy(new TestActivityDisplay(this, DEFAULT_DISPLAY)); |
Wale Ogunwale | 9dcf946 | 2017-09-19 15:13:01 -0700 | [diff] [blame] | 370 | attachDisplay(mDisplay); |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 373 | @Override |
Bryce Lee | 459c062 | 2018-03-19 11:04:01 -0700 | [diff] [blame] | 374 | public KeyguardController getKeyguardController() { |
| 375 | return mKeyguardController; |
| 376 | } |
| 377 | |
| 378 | @Override |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 379 | ActivityDisplay getDefaultDisplay() { |
| 380 | return mDisplay; |
| 381 | } |
| 382 | |
Bryce Lee | 2a3cc46 | 2017-10-27 10:57:35 -0700 | [diff] [blame] | 383 | // Just return the current front task. This is called internally so we cannot use spy to mock this out. |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 384 | @Override |
Wale Ogunwale | e1f68ce | 2018-03-09 08:58:54 -0800 | [diff] [blame] | 385 | ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus, |
| 386 | boolean ignoreCurrent) { |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 387 | return mFocusedStack; |
| 388 | } |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Winson Chung | 59a47ded | 2018-01-25 17:46:06 +0000 | [diff] [blame] | 391 | protected static class TestActivityDisplay extends ActivityDisplay { |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 392 | |
| 393 | private final ActivityStackSupervisor mSupervisor; |
| 394 | TestActivityDisplay(ActivityStackSupervisor supervisor, int displayId) { |
| 395 | super(supervisor, displayId); |
| 396 | mSupervisor = supervisor; |
Andrii Kulian | b1cdb10 | 2017-07-13 15:33:06 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | @Override |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 400 | <T extends ActivityStack> T createStackUnchecked(int windowingMode, int activityType, |
| 401 | int stackId, boolean onTop) { |
| 402 | if (windowingMode == WINDOWING_MODE_PINNED) { |
| 403 | return (T) new PinnedActivityStack(this, stackId, mSupervisor, onTop) { |
Andrii Kulian | b1cdb10 | 2017-07-13 15:33:06 -0700 | [diff] [blame] | 404 | @Override |
| 405 | Rect getDefaultPictureInPictureBounds(float aspectRatio) { |
| 406 | return new Rect(50, 50, 100, 100); |
| 407 | } |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 408 | |
| 409 | @Override |
| 410 | PinnedStackWindowController createStackWindowController(int displayId, |
| 411 | boolean onTop, Rect outBounds) { |
| 412 | return mock(PinnedStackWindowController.class); |
| 413 | } |
Andrii Kulian | b1cdb10 | 2017-07-13 15:33:06 -0700 | [diff] [blame] | 414 | }; |
| 415 | } else { |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 416 | return (T) new TestActivityStack( |
| 417 | this, stackId, mSupervisor, windowingMode, activityType, onTop); |
Andrii Kulian | b1cdb10 | 2017-07-13 15:33:06 -0700 | [diff] [blame] | 418 | } |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 419 | } |
Winson Chung | 59a47ded | 2018-01-25 17:46:06 +0000 | [diff] [blame] | 420 | |
| 421 | @Override |
| 422 | protected DisplayWindowController createWindowContainerController() { |
| 423 | return mock(DisplayWindowController.class); |
| 424 | } |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 427 | private static WindowManagerService prepareMockWindowManager() { |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 428 | final WindowManagerService service = WindowTestUtils.getMockWindowManagerService(); |
Bryce Lee | 04ab346 | 2017-04-10 15:06:33 -0700 | [diff] [blame] | 429 | |
| 430 | doAnswer((InvocationOnMock invocationOnMock) -> { |
| 431 | final Runnable runnable = invocationOnMock.<Runnable>getArgument(0); |
| 432 | if (runnable != null) { |
| 433 | runnable.run(); |
| 434 | } |
| 435 | return null; |
| 436 | }).when(service).inSurfaceTransaction(any()); |
| 437 | |
| 438 | return service; |
| 439 | } |
| 440 | |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 441 | /** |
Bryce Lee | 4e4a3ec | 2017-09-27 08:25:03 -0700 | [diff] [blame] | 442 | * Overrided of {@link ActivityStack} that tracks test metrics, such as the number of times a |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 443 | * method is called. Note that its functionality depends on the implementations of the |
| 444 | * construction arguments. |
| 445 | */ |
| 446 | protected static class TestActivityStack<T extends StackWindowController> |
Wale Ogunwale | 7e1f5f5 | 2017-10-18 15:19:59 -0700 | [diff] [blame] | 447 | extends ActivityStack<T> { |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 448 | private int mOnActivityRemovedFromStackCount = 0; |
| 449 | private T mContainerController; |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 450 | |
Wale Ogunwale | 7e1f5f5 | 2017-10-18 15:19:59 -0700 | [diff] [blame] | 451 | static final int IS_TRANSLUCENT_UNSET = 0; |
| 452 | static final int IS_TRANSLUCENT_FALSE = 1; |
| 453 | static final int IS_TRANSLUCENT_TRUE = 2; |
| 454 | private int mIsTranslucent = IS_TRANSLUCENT_UNSET; |
| 455 | |
Wale Ogunwale | 30e441d | 2017-11-09 08:28:45 -0800 | [diff] [blame] | 456 | static final int SUPPORTS_SPLIT_SCREEN_UNSET = 0; |
| 457 | static final int SUPPORTS_SPLIT_SCREEN_FALSE = 1; |
| 458 | static final int SUPPORTS_SPLIT_SCREEN_TRUE = 2; |
| 459 | private int mSupportsSplitScreen = SUPPORTS_SPLIT_SCREEN_UNSET; |
| 460 | |
Wale Ogunwale | 9dcf946 | 2017-09-19 15:13:01 -0700 | [diff] [blame] | 461 | TestActivityStack(ActivityDisplay display, int stackId, ActivityStackSupervisor supervisor, |
Wale Ogunwale | 04a05ac | 2017-09-17 21:35:02 -0700 | [diff] [blame] | 462 | int windowingMode, int activityType, boolean onTop) { |
| 463 | super(display, stackId, supervisor, windowingMode, activityType, onTop); |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | @Override |
| 467 | void onActivityRemovedFromStack(ActivityRecord r) { |
| 468 | mOnActivityRemovedFromStackCount++; |
| 469 | super.onActivityRemovedFromStack(r); |
| 470 | } |
| 471 | |
| 472 | // Returns the number of times {@link #onActivityRemovedFromStack} has been called |
Wale Ogunwale | 7e1f5f5 | 2017-10-18 15:19:59 -0700 | [diff] [blame] | 473 | int onActivityRemovedFromStackInvocationCount() { |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 474 | return mOnActivityRemovedFromStackCount; |
| 475 | } |
| 476 | |
| 477 | @Override |
Wale Ogunwale | 034a8ec | 2017-09-02 17:14:40 -0700 | [diff] [blame] | 478 | protected T createStackWindowController(int displayId, boolean onTop, Rect outBounds) { |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 479 | mContainerController = (T) WindowTestUtils.createMockStackWindowContainerController(); |
Bryce Lee | f3c6a47 | 2017-11-14 14:53:06 -0800 | [diff] [blame] | 480 | |
| 481 | // Primary pinned stacks require a non-empty out bounds to be set or else all tasks |
| 482 | // will be moved to the full screen stack. |
| 483 | if (getWindowingMode() == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) { |
| 484 | outBounds.set(0, 0, 100, 100); |
| 485 | } |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 486 | return mContainerController; |
| 487 | } |
| 488 | |
| 489 | @Override |
| 490 | T getWindowContainerController() { |
| 491 | return mContainerController; |
| 492 | } |
Wale Ogunwale | 7e1f5f5 | 2017-10-18 15:19:59 -0700 | [diff] [blame] | 493 | |
| 494 | void setIsTranslucent(boolean isTranslucent) { |
| 495 | mIsTranslucent = isTranslucent ? IS_TRANSLUCENT_TRUE : IS_TRANSLUCENT_FALSE; |
| 496 | } |
| 497 | |
| 498 | @Override |
Wale Ogunwale | 66e1685 | 2017-10-19 13:35:52 -0700 | [diff] [blame] | 499 | boolean isStackTranslucent(ActivityRecord starting) { |
Wale Ogunwale | 7e1f5f5 | 2017-10-18 15:19:59 -0700 | [diff] [blame] | 500 | switch (mIsTranslucent) { |
| 501 | case IS_TRANSLUCENT_TRUE: |
| 502 | return true; |
| 503 | case IS_TRANSLUCENT_FALSE: |
| 504 | return false; |
| 505 | case IS_TRANSLUCENT_UNSET: |
| 506 | default: |
Wale Ogunwale | 66e1685 | 2017-10-19 13:35:52 -0700 | [diff] [blame] | 507 | return super.isStackTranslucent(starting); |
Wale Ogunwale | 7e1f5f5 | 2017-10-18 15:19:59 -0700 | [diff] [blame] | 508 | } |
| 509 | } |
Wale Ogunwale | 30e441d | 2017-11-09 08:28:45 -0800 | [diff] [blame] | 510 | |
| 511 | void setSupportsSplitScreen(boolean supportsSplitScreen) { |
| 512 | mSupportsSplitScreen = supportsSplitScreen |
| 513 | ? SUPPORTS_SPLIT_SCREEN_TRUE : SUPPORTS_SPLIT_SCREEN_FALSE; |
| 514 | } |
| 515 | |
| 516 | @Override |
| 517 | public boolean supportsSplitScreenWindowingMode() { |
| 518 | switch (mSupportsSplitScreen) { |
| 519 | case SUPPORTS_SPLIT_SCREEN_TRUE: |
| 520 | return true; |
| 521 | case SUPPORTS_SPLIT_SCREEN_FALSE: |
| 522 | return false; |
| 523 | case SUPPORTS_SPLIT_SCREEN_UNSET: |
| 524 | default: |
| 525 | return super.supportsSplitScreenWindowingMode(); |
| 526 | } |
| 527 | } |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 528 | } |
Bryce Lee | af691c0 | 2017-03-20 14:20:22 -0700 | [diff] [blame] | 529 | } |