blob: e8194dd8e7777971b5ea26ea1b9785e3a55a2244 [file] [log] [blame]
Bryce Lee4e4a3ec2017-09-27 08:25:03 -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
Bryce Lee93e7f792017-10-25 15:54:55 -070019import static android.app.ActivityManager.START_ABORTED;
20import static android.app.ActivityManager.START_CLASS_NOT_FOUND;
21import static android.app.ActivityManager.START_FORWARD_AND_REQUEST_CONFLICT;
22import static android.app.ActivityManager.START_NOT_VOICE_COMPATIBLE;
23import static android.app.ActivityManager.START_SUCCESS;
24import static android.app.ActivityManager.START_SWITCHES_CANCELED;
Bryce Lee4e4a3ec2017-09-27 08:25:03 -070025import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD;
26import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
27import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
28
Bryce Lee93e7f792017-10-25 15:54:55 -070029import android.app.ActivityOptions;
30import android.app.IApplicationThread;
Bryce Lee93e7f792017-10-25 15:54:55 -070031import android.content.Intent;
32import android.content.pm.ActivityInfo;
33import android.content.pm.ApplicationInfo;
34import android.content.pm.IPackageManager;
Bryce Lee4e4a3ec2017-09-27 08:25:03 -070035import android.graphics.Rect;
Bryce Lee93e7f792017-10-25 15:54:55 -070036import android.os.IBinder;
37import android.os.RemoteException;
Bryce Lee4e4a3ec2017-09-27 08:25:03 -070038import android.platform.test.annotations.Presubmit;
Bryce Lee93e7f792017-10-25 15:54:55 -070039import android.service.voice.IVoiceInteractionSession;
Bryce Lee4e4a3ec2017-09-27 08:25:03 -070040import android.support.test.filters.SmallTest;
41import android.support.test.runner.AndroidJUnit4;
42
43import org.junit.runner.RunWith;
44import org.junit.Test;
45
46import static com.android.server.am.ActivityManagerService.ANIMATE;
47
48import static org.junit.Assert.assertEquals;
49import static org.junit.Assert.assertTrue;
50import static org.mockito.Mockito.any;
51import static org.mockito.Mockito.anyBoolean;
52import static org.mockito.Mockito.anyInt;
Bryce Lee93e7f792017-10-25 15:54:55 -070053import static org.mockito.Mockito.anyObject;
54import static org.mockito.Mockito.doAnswer;
55import static org.mockito.Mockito.doReturn;
Bryce Lee4e4a3ec2017-09-27 08:25:03 -070056import static org.mockito.Mockito.eq;
Bryce Lee93e7f792017-10-25 15:54:55 -070057import static org.mockito.Mockito.mock;
58import static org.mockito.Mockito.spy;
Bryce Lee4e4a3ec2017-09-27 08:25:03 -070059import static org.mockito.Mockito.verify;
60import static org.mockito.Mockito.times;
61
Bryce Lee93e7f792017-10-25 15:54:55 -070062import static android.app.ActivityManager.START_PERMISSION_DENIED;
63import static android.app.ActivityManager.START_INTENT_NOT_RESOLVED;
64
65import com.android.internal.os.BatteryStatsImpl;
66
Bryce Lee4e4a3ec2017-09-27 08:25:03 -070067/**
Bryce Leed3624e12017-11-30 08:51:45 -080068 * Tests for the {@link ActivityStarter} class.
Bryce Lee4e4a3ec2017-09-27 08:25:03 -070069 *
70 * Build/Install/Run:
Bryce Leed3624e12017-11-30 08:51:45 -080071 * atest FrameworksServicesTests:ActivityStarterTests
Bryce Lee4e4a3ec2017-09-27 08:25:03 -070072 */
73@SmallTest
74@Presubmit
75@RunWith(AndroidJUnit4.class)
76public class ActivityStarterTests extends ActivityTestsBase {
Bryce Lee4e4a3ec2017-09-27 08:25:03 -070077 private ActivityManagerService mService;
78 private ActivityStarter mStarter;
Bryce Leed3624e12017-11-30 08:51:45 -080079 private ActivityStartController mController;
Bryce Lee93e7f792017-10-25 15:54:55 -070080
81 private static final int PRECONDITION_NO_CALLER_APP = 1;
82 private static final int PRECONDITION_NO_INTENT_COMPONENT = 1 << 1;
83 private static final int PRECONDITION_NO_ACTIVITY_INFO = 1 << 2;
84 private static final int PRECONDITION_SOURCE_PRESENT = 1 << 3;
85 private static final int PRECONDITION_REQUEST_CODE = 1 << 4;
86 private static final int PRECONDITION_SOURCE_VOICE_SESSION = 1 << 5;
87 private static final int PRECONDITION_NO_VOICE_SESSION_SUPPORT = 1 << 6;
88 private static final int PRECONDITION_DIFFERENT_UID = 1 << 7;
89 private static final int PRECONDITION_ACTIVITY_SUPPORTS_INTENT_EXCEPTION = 1 << 8;
90 private static final int PRECONDITION_CANNOT_START_ANY_ACTIVITY = 1 << 9;
91 private static final int PRECONDITION_DISALLOW_APP_SWITCHING = 1 << 10;
Bryce Lee4e4a3ec2017-09-27 08:25:03 -070092
93 @Override
94 public void setUp() throws Exception {
95 super.setUp();
96 mService = createActivityManagerService();
Bryce Leed3624e12017-11-30 08:51:45 -080097 mController = mock(ActivityStartController.class);
98 mStarter = new ActivityStarter(mController, mService, mService.mStackSupervisor,
99 mock(ActivityStartInterceptor.class));
Bryce Lee4e4a3ec2017-09-27 08:25:03 -0700100 }
101
102 @Test
103 public void testUpdateLaunchBounds() throws Exception {
104 // When in a non-resizeable stack, the task bounds should be updated.
Bryce Lee18d51592017-10-25 10:22:19 -0700105 final TaskRecord task = new TaskBuilder(mService.mStackSupervisor)
106 .setStack(mService.mStackSupervisor.getDefaultDisplay().createStack(
107 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, true /* onTop */))
108 .build();
Bryce Lee4e4a3ec2017-09-27 08:25:03 -0700109 final Rect bounds = new Rect(10, 10, 100, 100);
110
111 mStarter.updateBounds(task, bounds);
Bryce Leef3c6a472017-11-14 14:53:06 -0800112 assertEquals(task.getOverrideBounds(), bounds);
113 assertEquals(new Rect(), task.getStack().getOverrideBounds());
Bryce Lee4e4a3ec2017-09-27 08:25:03 -0700114
115 // When in a resizeable stack, the stack bounds should be updated as well.
Bryce Lee18d51592017-10-25 10:22:19 -0700116 final TaskRecord task2 = new TaskBuilder(mService.mStackSupervisor)
117 .setStack(mService.mStackSupervisor.getDefaultDisplay().createStack(
118 WINDOWING_MODE_PINNED, ACTIVITY_TYPE_STANDARD, true /* onTop */))
119 .build();
Bryce Lee4e4a3ec2017-09-27 08:25:03 -0700120 assertTrue(task2.getStack() instanceof PinnedActivityStack);
121 mStarter.updateBounds(task2, bounds);
122
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700123 verify(mService, times(1)).resizeStack(eq(task2.getStack().mStackId),
Bryce Lee4e4a3ec2017-09-27 08:25:03 -0700124 eq(bounds), anyBoolean(), anyBoolean(), anyBoolean(), anyInt());
125
126 // In the case of no animation, the stack and task bounds should be set immediately.
127 if (!ANIMATE) {
Bryce Leef3c6a472017-11-14 14:53:06 -0800128 assertEquals(task2.getStack().getOverrideBounds(), bounds);
129 assertEquals(task2.getOverrideBounds(), bounds);
Bryce Lee4e4a3ec2017-09-27 08:25:03 -0700130 } else {
Bryce Leef3c6a472017-11-14 14:53:06 -0800131 assertEquals(task2.getOverrideBounds(), new Rect());
Bryce Lee4e4a3ec2017-09-27 08:25:03 -0700132 }
133 }
Bryce Lee93e7f792017-10-25 15:54:55 -0700134
135 @Test
136 public void testStartActivityPreconditions() throws Exception {
137 verifyStartActivityPreconditions(PRECONDITION_NO_CALLER_APP, START_PERMISSION_DENIED);
138 verifyStartActivityPreconditions(PRECONDITION_NO_INTENT_COMPONENT,
139 START_INTENT_NOT_RESOLVED);
140 verifyStartActivityPreconditions(PRECONDITION_NO_ACTIVITY_INFO, START_CLASS_NOT_FOUND);
141 verifyStartActivityPreconditions(PRECONDITION_SOURCE_PRESENT | PRECONDITION_REQUEST_CODE,
142 Intent.FLAG_ACTIVITY_FORWARD_RESULT, START_FORWARD_AND_REQUEST_CONFLICT);
143 verifyStartActivityPreconditions(
144 PRECONDITION_SOURCE_PRESENT | PRECONDITION_NO_VOICE_SESSION_SUPPORT
145 | PRECONDITION_SOURCE_VOICE_SESSION | PRECONDITION_DIFFERENT_UID,
146 START_NOT_VOICE_COMPATIBLE);
147 verifyStartActivityPreconditions(
148 PRECONDITION_SOURCE_PRESENT | PRECONDITION_NO_VOICE_SESSION_SUPPORT
149 | PRECONDITION_SOURCE_VOICE_SESSION | PRECONDITION_DIFFERENT_UID
150 | PRECONDITION_ACTIVITY_SUPPORTS_INTENT_EXCEPTION,
151 START_NOT_VOICE_COMPATIBLE);
152 verifyStartActivityPreconditions(PRECONDITION_CANNOT_START_ANY_ACTIVITY, START_ABORTED);
153 verifyStartActivityPreconditions(PRECONDITION_DISALLOW_APP_SWITCHING,
154 START_SWITCHES_CANCELED);
155 }
156
157 private static boolean containsConditions(int preconditions, int mask) {
158 return (preconditions & mask) == mask;
159 }
160
161 private void verifyStartActivityPreconditions(int preconditions, int expectedResult) {
162 verifyStartActivityPreconditions(preconditions, 0 /*launchFlags*/, expectedResult);
163 }
164
165 /**
166 * Excercises how the {@link ActivityStarter} reacts to various preconditions. The caller
167 * provides a bitmask of all the set conditions (such as {@link #PRECONDITION_NO_CALLER_APP})
168 * and the launch flags specified in the intent. The method constructs a call to
Bryce Lee4c9a5972017-12-01 22:14:24 -0800169 * {@link ActivityStarter#execute} based on these preconditions and ensures the result matches
170 * the expected. It is important to note that the method also checks side effects of the start,
171 * such as ensuring {@link ActivityOptions#abort()} is called in the relevant scenarios.
Bryce Lee93e7f792017-10-25 15:54:55 -0700172 * @param preconditions A bitmask representing the preconditions for the launch
173 * @param launchFlags The launch flags to be provided by the launch {@link Intent}.
174 * @param expectedResult The expected result from the launch.
175 */
176 private void verifyStartActivityPreconditions(int preconditions, int launchFlags,
177 int expectedResult) {
178 final ActivityManagerService service = createActivityManagerService();
179 final IPackageManager packageManager = mock(IPackageManager.class);
Bryce Leed3624e12017-11-30 08:51:45 -0800180 final ActivityStartController controller = mock(ActivityStartController.class);
Bryce Lee93e7f792017-10-25 15:54:55 -0700181
Bryce Leed3624e12017-11-30 08:51:45 -0800182 final ActivityStarter starter = new ActivityStarter(controller, service,
183 service.mStackSupervisor, mock(ActivityStartInterceptor.class));
Bryce Lee93e7f792017-10-25 15:54:55 -0700184 final IApplicationThread caller = mock(IApplicationThread.class);
185
186 // If no caller app, return {@code null} {@link ProcessRecord}.
187 final ProcessRecord record = containsConditions(preconditions, PRECONDITION_NO_CALLER_APP)
188 ? null : new ProcessRecord(mock(BatteryStatsImpl.class),
189 mock(ApplicationInfo.class), null, 0);
190
191 doReturn(record).when(service).getRecordForAppLocked(anyObject());
192
193 final Intent intent = new Intent();
194 intent.setFlags(launchFlags);
195
196 final ActivityInfo aInfo = containsConditions(preconditions, PRECONDITION_NO_ACTIVITY_INFO)
197 ? null : new ActivityInfo();
198
199 if (aInfo != null) {
200 aInfo.applicationInfo = new ApplicationInfo();
201 aInfo.applicationInfo.packageName = ActivityBuilder.DEFAULT_PACKAGE;
202 }
203
204 IVoiceInteractionSession voiceSession =
205 containsConditions(preconditions, PRECONDITION_SOURCE_VOICE_SESSION)
206 ? mock(IVoiceInteractionSession.class) : null;
207
208 // Create source token
209 final ActivityBuilder builder = new ActivityBuilder(service).setTask(
210 new TaskBuilder(service.mStackSupervisor).setVoiceSession(voiceSession).build());
211
212 // Offset uid by one from {@link ActivityInfo} to simulate different uids.
213 if (containsConditions(preconditions, PRECONDITION_DIFFERENT_UID)) {
214 builder.setUid(aInfo.applicationInfo.uid + 1);
215 }
216
217 final ActivityRecord source = builder.build();
218
219 if (!containsConditions(preconditions, PRECONDITION_NO_INTENT_COMPONENT)) {
220 intent.setComponent(source.realActivity);
221 }
222
223 if (containsConditions(preconditions, PRECONDITION_DISALLOW_APP_SWITCHING)) {
224 doReturn(false).when(service).checkAppSwitchAllowedLocked(anyInt(), anyInt(), anyInt(),
225 anyInt(), any());
226 }
227
228 if (containsConditions(preconditions,PRECONDITION_CANNOT_START_ANY_ACTIVITY)) {
229 doReturn(false).when(service.mStackSupervisor).checkStartAnyActivityPermission(
230 any(), any(), any(), anyInt(), anyInt(), anyInt(), any(), anyBoolean(),
231 any(), any(), any(), any());
232 }
233
234 try {
235 if (containsConditions(preconditions,
236 PRECONDITION_ACTIVITY_SUPPORTS_INTENT_EXCEPTION)) {
237 doAnswer((inv) -> {
238 throw new RemoteException();
239 }).when(packageManager).activitySupportsIntent(eq(source.realActivity), eq(intent),
240 any());
241 } else {
242 doReturn(!containsConditions(preconditions, PRECONDITION_NO_VOICE_SESSION_SUPPORT))
243 .when(packageManager).activitySupportsIntent(eq(source.realActivity),
244 eq(intent), any());
245 }
246 } catch (RemoteException e) {
247 }
248
249 final IBinder resultTo = containsConditions(preconditions, PRECONDITION_SOURCE_PRESENT)
250 || containsConditions(preconditions, PRECONDITION_SOURCE_VOICE_SESSION)
251 ? source.appToken : null;
252
253 final int requestCode = containsConditions(preconditions, PRECONDITION_REQUEST_CODE)
254 ? 1 : 0;
255
Bryce Lee4c9a5972017-12-01 22:14:24 -0800256 final int result = starter.setCaller(caller)
257 .setIntent(intent)
258 .setActivityInfo(aInfo)
259 .setResultTo(resultTo)
260 .setRequestCode(requestCode)
261 .setReason("testLaunchActivityPermissionDenied")
262 .execute();
Bryce Lee93e7f792017-10-25 15:54:55 -0700263
264 // In some cases the expected result internally is different than the published result. We
265 // must use ActivityStarter#getExternalResult to translate.
266 assertEquals(ActivityStarter.getExternalResult(expectedResult), result);
267
268 // Ensure that {@link ActivityOptions} are aborted with unsuccessful result.
269 if (expectedResult != START_SUCCESS) {
Bryce Lee4c9a5972017-12-01 22:14:24 -0800270 final ActivityStarter optionStarter = new ActivityStarter(mController, mService,
271 mService.mStackSupervisor, mock(ActivityStartInterceptor.class));
Bryce Lee93e7f792017-10-25 15:54:55 -0700272 final ActivityOptions options = spy(ActivityOptions.makeBasic());
Bryce Lee4c9a5972017-12-01 22:14:24 -0800273
274 final int optionResult = optionStarter.setCaller(caller)
275 .setIntent(intent)
276 .setActivityInfo(aInfo)
277 .setResultTo(resultTo)
278 .setRequestCode(requestCode)
279 .setReason("testLaunchActivityPermissionDenied")
280 .setActivityOptions(options)
281 .execute();
Bryce Lee93e7f792017-10-25 15:54:55 -0700282 verify(options, times(1)).abort();
283 }
284 }
Bryce Leeb802ea12017-11-15 21:25:03 -0800285
286// TODO(b/69270257): Add test to verify task layout is passed additional data such as activity and
287// source.
288// @Test
289// public void testCreateTaskLayout() {
290// }
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700291}