blob: 1cec0d93cabe8f1cc027a9b67757e99418652a9f [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/**
68 * Tests for the {@link ActivityStack} class.
69 *
70 * Build/Install/Run:
71 * bit FrameworksServicesTests:com.android.server.am.ActivityStarterTests
72 */
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 Lee93e7f792017-10-25 15:54:55 -070079 private IPackageManager mPackageManager;
80
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 Leeba8f4422017-11-20 12:35:57 -080097 mStarter = new ActivityStarter(mService);
Bryce Lee4e4a3ec2017-09-27 08:25:03 -070098 }
99
100 @Test
101 public void testUpdateLaunchBounds() throws Exception {
102 // When in a non-resizeable stack, the task bounds should be updated.
Bryce Lee18d51592017-10-25 10:22:19 -0700103 final TaskRecord task = new TaskBuilder(mService.mStackSupervisor)
104 .setStack(mService.mStackSupervisor.getDefaultDisplay().createStack(
105 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, true /* onTop */))
106 .build();
Bryce Lee4e4a3ec2017-09-27 08:25:03 -0700107 final Rect bounds = new Rect(10, 10, 100, 100);
108
109 mStarter.updateBounds(task, bounds);
Bryce Leef3c6a472017-11-14 14:53:06 -0800110 assertEquals(task.getOverrideBounds(), bounds);
111 assertEquals(new Rect(), task.getStack().getOverrideBounds());
Bryce Lee4e4a3ec2017-09-27 08:25:03 -0700112
113 // When in a resizeable stack, the stack bounds should be updated as well.
Bryce Lee18d51592017-10-25 10:22:19 -0700114 final TaskRecord task2 = new TaskBuilder(mService.mStackSupervisor)
115 .setStack(mService.mStackSupervisor.getDefaultDisplay().createStack(
116 WINDOWING_MODE_PINNED, ACTIVITY_TYPE_STANDARD, true /* onTop */))
117 .build();
Bryce Lee4e4a3ec2017-09-27 08:25:03 -0700118 assertTrue(task2.getStack() instanceof PinnedActivityStack);
119 mStarter.updateBounds(task2, bounds);
120
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700121 verify(mService, times(1)).resizeStack(eq(task2.getStack().mStackId),
Bryce Lee4e4a3ec2017-09-27 08:25:03 -0700122 eq(bounds), anyBoolean(), anyBoolean(), anyBoolean(), anyInt());
123
124 // In the case of no animation, the stack and task bounds should be set immediately.
125 if (!ANIMATE) {
Bryce Leef3c6a472017-11-14 14:53:06 -0800126 assertEquals(task2.getStack().getOverrideBounds(), bounds);
127 assertEquals(task2.getOverrideBounds(), bounds);
Bryce Lee4e4a3ec2017-09-27 08:25:03 -0700128 } else {
Bryce Leef3c6a472017-11-14 14:53:06 -0800129 assertEquals(task2.getOverrideBounds(), new Rect());
Bryce Lee4e4a3ec2017-09-27 08:25:03 -0700130 }
131 }
Bryce Lee93e7f792017-10-25 15:54:55 -0700132
133 @Test
134 public void testStartActivityPreconditions() throws Exception {
135 verifyStartActivityPreconditions(PRECONDITION_NO_CALLER_APP, START_PERMISSION_DENIED);
136 verifyStartActivityPreconditions(PRECONDITION_NO_INTENT_COMPONENT,
137 START_INTENT_NOT_RESOLVED);
138 verifyStartActivityPreconditions(PRECONDITION_NO_ACTIVITY_INFO, START_CLASS_NOT_FOUND);
139 verifyStartActivityPreconditions(PRECONDITION_SOURCE_PRESENT | PRECONDITION_REQUEST_CODE,
140 Intent.FLAG_ACTIVITY_FORWARD_RESULT, START_FORWARD_AND_REQUEST_CONFLICT);
141 verifyStartActivityPreconditions(
142 PRECONDITION_SOURCE_PRESENT | PRECONDITION_NO_VOICE_SESSION_SUPPORT
143 | PRECONDITION_SOURCE_VOICE_SESSION | PRECONDITION_DIFFERENT_UID,
144 START_NOT_VOICE_COMPATIBLE);
145 verifyStartActivityPreconditions(
146 PRECONDITION_SOURCE_PRESENT | PRECONDITION_NO_VOICE_SESSION_SUPPORT
147 | PRECONDITION_SOURCE_VOICE_SESSION | PRECONDITION_DIFFERENT_UID
148 | PRECONDITION_ACTIVITY_SUPPORTS_INTENT_EXCEPTION,
149 START_NOT_VOICE_COMPATIBLE);
150 verifyStartActivityPreconditions(PRECONDITION_CANNOT_START_ANY_ACTIVITY, START_ABORTED);
151 verifyStartActivityPreconditions(PRECONDITION_DISALLOW_APP_SWITCHING,
152 START_SWITCHES_CANCELED);
153 }
154
155 private static boolean containsConditions(int preconditions, int mask) {
156 return (preconditions & mask) == mask;
157 }
158
159 private void verifyStartActivityPreconditions(int preconditions, int expectedResult) {
160 verifyStartActivityPreconditions(preconditions, 0 /*launchFlags*/, expectedResult);
161 }
162
163 /**
164 * Excercises how the {@link ActivityStarter} reacts to various preconditions. The caller
165 * provides a bitmask of all the set conditions (such as {@link #PRECONDITION_NO_CALLER_APP})
166 * and the launch flags specified in the intent. The method constructs a call to
167 * {@link ActivityStarter#startActivityLocked} based on these preconditions and ensures the
168 * result matches the expected. It is important to note that the method also checks side effects
169 * of the start, such as ensuring {@link ActivityOptions#abort()} is called in the relevant
170 * scenarios.
171 * @param preconditions A bitmask representing the preconditions for the launch
172 * @param launchFlags The launch flags to be provided by the launch {@link Intent}.
173 * @param expectedResult The expected result from the launch.
174 */
175 private void verifyStartActivityPreconditions(int preconditions, int launchFlags,
176 int expectedResult) {
177 final ActivityManagerService service = createActivityManagerService();
178 final IPackageManager packageManager = mock(IPackageManager.class);
Bryce Leeba8f4422017-11-20 12:35:57 -0800179 final ActivityStarter starter = new ActivityStarter(service);
Bryce Lee93e7f792017-10-25 15:54:55 -0700180
181 final IApplicationThread caller = mock(IApplicationThread.class);
182
183 // If no caller app, return {@code null} {@link ProcessRecord}.
184 final ProcessRecord record = containsConditions(preconditions, PRECONDITION_NO_CALLER_APP)
185 ? null : new ProcessRecord(mock(BatteryStatsImpl.class),
186 mock(ApplicationInfo.class), null, 0);
187
188 doReturn(record).when(service).getRecordForAppLocked(anyObject());
189
190 final Intent intent = new Intent();
191 intent.setFlags(launchFlags);
192
193 final ActivityInfo aInfo = containsConditions(preconditions, PRECONDITION_NO_ACTIVITY_INFO)
194 ? null : new ActivityInfo();
195
196 if (aInfo != null) {
197 aInfo.applicationInfo = new ApplicationInfo();
198 aInfo.applicationInfo.packageName = ActivityBuilder.DEFAULT_PACKAGE;
199 }
200
201 IVoiceInteractionSession voiceSession =
202 containsConditions(preconditions, PRECONDITION_SOURCE_VOICE_SESSION)
203 ? mock(IVoiceInteractionSession.class) : null;
204
205 // Create source token
206 final ActivityBuilder builder = new ActivityBuilder(service).setTask(
207 new TaskBuilder(service.mStackSupervisor).setVoiceSession(voiceSession).build());
208
209 // Offset uid by one from {@link ActivityInfo} to simulate different uids.
210 if (containsConditions(preconditions, PRECONDITION_DIFFERENT_UID)) {
211 builder.setUid(aInfo.applicationInfo.uid + 1);
212 }
213
214 final ActivityRecord source = builder.build();
215
216 if (!containsConditions(preconditions, PRECONDITION_NO_INTENT_COMPONENT)) {
217 intent.setComponent(source.realActivity);
218 }
219
220 if (containsConditions(preconditions, PRECONDITION_DISALLOW_APP_SWITCHING)) {
221 doReturn(false).when(service).checkAppSwitchAllowedLocked(anyInt(), anyInt(), anyInt(),
222 anyInt(), any());
223 }
224
225 if (containsConditions(preconditions,PRECONDITION_CANNOT_START_ANY_ACTIVITY)) {
226 doReturn(false).when(service.mStackSupervisor).checkStartAnyActivityPermission(
227 any(), any(), any(), anyInt(), anyInt(), anyInt(), any(), anyBoolean(),
228 any(), any(), any(), any());
229 }
230
231 try {
232 if (containsConditions(preconditions,
233 PRECONDITION_ACTIVITY_SUPPORTS_INTENT_EXCEPTION)) {
234 doAnswer((inv) -> {
235 throw new RemoteException();
236 }).when(packageManager).activitySupportsIntent(eq(source.realActivity), eq(intent),
237 any());
238 } else {
239 doReturn(!containsConditions(preconditions, PRECONDITION_NO_VOICE_SESSION_SUPPORT))
240 .when(packageManager).activitySupportsIntent(eq(source.realActivity),
241 eq(intent), any());
242 }
243 } catch (RemoteException e) {
244 }
245
246 final IBinder resultTo = containsConditions(preconditions, PRECONDITION_SOURCE_PRESENT)
247 || containsConditions(preconditions, PRECONDITION_SOURCE_VOICE_SESSION)
248 ? source.appToken : null;
249
250 final int requestCode = containsConditions(preconditions, PRECONDITION_REQUEST_CODE)
251 ? 1 : 0;
252
253 final int result = starter.startActivityLocked(caller, intent,
254 null /*ephemeralIntent*/, null /*resolvedType*/, aInfo, null /*rInfo*/,
255 null /*voiceSession*/, null /*voiceInteractor*/, resultTo,
256 null /*resultWho*/, requestCode, 0 /*callingPid*/, 0 /*callingUid*/,
257 null /*callingPackage*/, 0 /*realCallingPid*/, 0 /*realCallingUid*/,
258 0 /*startFlags*/, null /*options*/, false /*ignoreTargetSecurity*/,
259 false /*componentSpecified*/, null /*outActivity*/,
260 null /*inTask*/, "testLaunchActivityPermissionDenied");
261
262 // In some cases the expected result internally is different than the published result. We
263 // must use ActivityStarter#getExternalResult to translate.
264 assertEquals(ActivityStarter.getExternalResult(expectedResult), result);
265
266 // Ensure that {@link ActivityOptions} are aborted with unsuccessful result.
267 if (expectedResult != START_SUCCESS) {
268 final ActivityOptions options = spy(ActivityOptions.makeBasic());
269 final int optionResult = starter.startActivityLocked(caller, intent,
270 null /*ephemeralIntent*/, null /*resolvedType*/, aInfo, null /*rInfo*/,
271 null /*voiceSession*/, null /*voiceInteractor*/, resultTo,
272 null /*resultWho*/, requestCode, 0 /*callingPid*/, 0 /*callingUid*/,
273 null /*callingPackage*/, 0 /*realCallingPid*/, 0 /*realCallingUid*/,
274 0 /*startFlags*/, options /*options*/, false /*ignoreTargetSecurity*/,
275 false /*componentSpecified*/, null /*outActivity*/,
276 null /*inTask*/, "testLaunchActivityPermissionDenied");
277 verify(options, times(1)).abort();
278 }
279 }
Bryce Leeb802ea12017-11-15 21:25:03 -0800280
281// TODO(b/69270257): Add test to verify task layout is passed additional data such as activity and
282// source.
283// @Test
284// public void testCreateTaskLayout() {
285// }
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700286}