blob: 471726b2979a1af6d56254daa5a017777f1f6d2a [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
169 * {@link ActivityStarter#startActivityLocked} based on these preconditions and ensures the
170 * result matches the expected. It is important to note that the method also checks side effects
171 * of the start, such as ensuring {@link ActivityOptions#abort()} is called in the relevant
172 * scenarios.
173 * @param preconditions A bitmask representing the preconditions for the launch
174 * @param launchFlags The launch flags to be provided by the launch {@link Intent}.
175 * @param expectedResult The expected result from the launch.
176 */
177 private void verifyStartActivityPreconditions(int preconditions, int launchFlags,
178 int expectedResult) {
179 final ActivityManagerService service = createActivityManagerService();
180 final IPackageManager packageManager = mock(IPackageManager.class);
Bryce Leed3624e12017-11-30 08:51:45 -0800181 final ActivityStartController controller = mock(ActivityStartController.class);
Bryce Lee93e7f792017-10-25 15:54:55 -0700182
Bryce Leed3624e12017-11-30 08:51:45 -0800183 final ActivityStarter starter = new ActivityStarter(controller, service,
184 service.mStackSupervisor, mock(ActivityStartInterceptor.class));
Bryce Lee93e7f792017-10-25 15:54:55 -0700185 final IApplicationThread caller = mock(IApplicationThread.class);
186
187 // If no caller app, return {@code null} {@link ProcessRecord}.
188 final ProcessRecord record = containsConditions(preconditions, PRECONDITION_NO_CALLER_APP)
189 ? null : new ProcessRecord(mock(BatteryStatsImpl.class),
190 mock(ApplicationInfo.class), null, 0);
191
192 doReturn(record).when(service).getRecordForAppLocked(anyObject());
193
194 final Intent intent = new Intent();
195 intent.setFlags(launchFlags);
196
197 final ActivityInfo aInfo = containsConditions(preconditions, PRECONDITION_NO_ACTIVITY_INFO)
198 ? null : new ActivityInfo();
199
200 if (aInfo != null) {
201 aInfo.applicationInfo = new ApplicationInfo();
202 aInfo.applicationInfo.packageName = ActivityBuilder.DEFAULT_PACKAGE;
203 }
204
205 IVoiceInteractionSession voiceSession =
206 containsConditions(preconditions, PRECONDITION_SOURCE_VOICE_SESSION)
207 ? mock(IVoiceInteractionSession.class) : null;
208
209 // Create source token
210 final ActivityBuilder builder = new ActivityBuilder(service).setTask(
211 new TaskBuilder(service.mStackSupervisor).setVoiceSession(voiceSession).build());
212
213 // Offset uid by one from {@link ActivityInfo} to simulate different uids.
214 if (containsConditions(preconditions, PRECONDITION_DIFFERENT_UID)) {
215 builder.setUid(aInfo.applicationInfo.uid + 1);
216 }
217
218 final ActivityRecord source = builder.build();
219
220 if (!containsConditions(preconditions, PRECONDITION_NO_INTENT_COMPONENT)) {
221 intent.setComponent(source.realActivity);
222 }
223
224 if (containsConditions(preconditions, PRECONDITION_DISALLOW_APP_SWITCHING)) {
225 doReturn(false).when(service).checkAppSwitchAllowedLocked(anyInt(), anyInt(), anyInt(),
226 anyInt(), any());
227 }
228
229 if (containsConditions(preconditions,PRECONDITION_CANNOT_START_ANY_ACTIVITY)) {
230 doReturn(false).when(service.mStackSupervisor).checkStartAnyActivityPermission(
231 any(), any(), any(), anyInt(), anyInt(), anyInt(), any(), anyBoolean(),
232 any(), any(), any(), any());
233 }
234
235 try {
236 if (containsConditions(preconditions,
237 PRECONDITION_ACTIVITY_SUPPORTS_INTENT_EXCEPTION)) {
238 doAnswer((inv) -> {
239 throw new RemoteException();
240 }).when(packageManager).activitySupportsIntent(eq(source.realActivity), eq(intent),
241 any());
242 } else {
243 doReturn(!containsConditions(preconditions, PRECONDITION_NO_VOICE_SESSION_SUPPORT))
244 .when(packageManager).activitySupportsIntent(eq(source.realActivity),
245 eq(intent), any());
246 }
247 } catch (RemoteException e) {
248 }
249
250 final IBinder resultTo = containsConditions(preconditions, PRECONDITION_SOURCE_PRESENT)
251 || containsConditions(preconditions, PRECONDITION_SOURCE_VOICE_SESSION)
252 ? source.appToken : null;
253
254 final int requestCode = containsConditions(preconditions, PRECONDITION_REQUEST_CODE)
255 ? 1 : 0;
256
257 final int result = starter.startActivityLocked(caller, intent,
258 null /*ephemeralIntent*/, null /*resolvedType*/, aInfo, null /*rInfo*/,
259 null /*voiceSession*/, null /*voiceInteractor*/, resultTo,
260 null /*resultWho*/, requestCode, 0 /*callingPid*/, 0 /*callingUid*/,
261 null /*callingPackage*/, 0 /*realCallingPid*/, 0 /*realCallingUid*/,
262 0 /*startFlags*/, null /*options*/, false /*ignoreTargetSecurity*/,
263 false /*componentSpecified*/, null /*outActivity*/,
264 null /*inTask*/, "testLaunchActivityPermissionDenied");
265
266 // In some cases the expected result internally is different than the published result. We
267 // must use ActivityStarter#getExternalResult to translate.
268 assertEquals(ActivityStarter.getExternalResult(expectedResult), result);
269
270 // Ensure that {@link ActivityOptions} are aborted with unsuccessful result.
271 if (expectedResult != START_SUCCESS) {
272 final ActivityOptions options = spy(ActivityOptions.makeBasic());
273 final int optionResult = starter.startActivityLocked(caller, intent,
274 null /*ephemeralIntent*/, null /*resolvedType*/, aInfo, null /*rInfo*/,
275 null /*voiceSession*/, null /*voiceInteractor*/, resultTo,
276 null /*resultWho*/, requestCode, 0 /*callingPid*/, 0 /*callingUid*/,
277 null /*callingPackage*/, 0 /*realCallingPid*/, 0 /*realCallingUid*/,
278 0 /*startFlags*/, options /*options*/, false /*ignoreTargetSecurity*/,
279 false /*componentSpecified*/, null /*outActivity*/,
280 null /*inTask*/, "testLaunchActivityPermissionDenied");
281 verify(options, times(1)).abort();
282 }
283 }
Bryce Leeb802ea12017-11-15 21:25:03 -0800284
285// TODO(b/69270257): Add test to verify task layout is passed additional data such as activity and
286// source.
287// @Test
288// public void testCreateTaskLayout() {
289// }
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700290}