blob: f9933fb63f12297ff3bd12128d59d77bb269496a [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 Lee4e4a3ec2017-09-27 08:25:03 -070031import android.content.ComponentName;
Bryce Lee93e7f792017-10-25 15:54:55 -070032import android.content.Intent;
33import android.content.pm.ActivityInfo;
34import android.content.pm.ApplicationInfo;
35import android.content.pm.IPackageManager;
Bryce Lee4e4a3ec2017-09-27 08:25:03 -070036import android.graphics.Rect;
Bryce Lee93e7f792017-10-25 15:54:55 -070037import android.os.IBinder;
38import android.os.RemoteException;
Bryce Lee4e4a3ec2017-09-27 08:25:03 -070039import android.platform.test.annotations.Presubmit;
Bryce Lee93e7f792017-10-25 15:54:55 -070040import android.service.voice.IVoiceInteractionSession;
Bryce Lee4e4a3ec2017-09-27 08:25:03 -070041import android.support.test.filters.SmallTest;
42import android.support.test.runner.AndroidJUnit4;
43
44import org.junit.runner.RunWith;
45import org.junit.Test;
46
47import static com.android.server.am.ActivityManagerService.ANIMATE;
48
49import static org.junit.Assert.assertEquals;
50import static org.junit.Assert.assertTrue;
51import static org.mockito.Mockito.any;
52import static org.mockito.Mockito.anyBoolean;
53import static org.mockito.Mockito.anyInt;
Bryce Lee93e7f792017-10-25 15:54:55 -070054import static org.mockito.Mockito.anyObject;
55import static org.mockito.Mockito.doAnswer;
56import static org.mockito.Mockito.doReturn;
Bryce Lee4e4a3ec2017-09-27 08:25:03 -070057import static org.mockito.Mockito.eq;
Bryce Lee93e7f792017-10-25 15:54:55 -070058import static org.mockito.Mockito.mock;
59import static org.mockito.Mockito.spy;
Bryce Lee4e4a3ec2017-09-27 08:25:03 -070060import static org.mockito.Mockito.verify;
61import static org.mockito.Mockito.times;
62
Bryce Lee93e7f792017-10-25 15:54:55 -070063import static android.app.ActivityManager.START_PERMISSION_DENIED;
64import static android.app.ActivityManager.START_INTENT_NOT_RESOLVED;
65
66import com.android.internal.os.BatteryStatsImpl;
67
Bryce Lee4e4a3ec2017-09-27 08:25:03 -070068/**
69 * Tests for the {@link ActivityStack} class.
70 *
71 * Build/Install/Run:
72 * bit FrameworksServicesTests:com.android.server.am.ActivityStarterTests
73 */
74@SmallTest
75@Presubmit
76@RunWith(AndroidJUnit4.class)
77public class ActivityStarterTests extends ActivityTestsBase {
Bryce Lee4e4a3ec2017-09-27 08:25:03 -070078 private ActivityManagerService mService;
79 private ActivityStarter mStarter;
Bryce Lee93e7f792017-10-25 15:54:55 -070080 private IPackageManager mPackageManager;
81
82 private static final int PRECONDITION_NO_CALLER_APP = 1;
83 private static final int PRECONDITION_NO_INTENT_COMPONENT = 1 << 1;
84 private static final int PRECONDITION_NO_ACTIVITY_INFO = 1 << 2;
85 private static final int PRECONDITION_SOURCE_PRESENT = 1 << 3;
86 private static final int PRECONDITION_REQUEST_CODE = 1 << 4;
87 private static final int PRECONDITION_SOURCE_VOICE_SESSION = 1 << 5;
88 private static final int PRECONDITION_NO_VOICE_SESSION_SUPPORT = 1 << 6;
89 private static final int PRECONDITION_DIFFERENT_UID = 1 << 7;
90 private static final int PRECONDITION_ACTIVITY_SUPPORTS_INTENT_EXCEPTION = 1 << 8;
91 private static final int PRECONDITION_CANNOT_START_ANY_ACTIVITY = 1 << 9;
92 private static final int PRECONDITION_DISALLOW_APP_SWITCHING = 1 << 10;
Bryce Lee4e4a3ec2017-09-27 08:25:03 -070093
94 @Override
95 public void setUp() throws Exception {
96 super.setUp();
97 mService = createActivityManagerService();
Bryce Lee93e7f792017-10-25 15:54:55 -070098 mPackageManager = mock(IPackageManager.class);
99 mStarter = new ActivityStarter(mService, mPackageManager);
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);
112 assertEquals(task.mBounds, bounds);
113 assertEquals(task.getStack().mBounds, null);
114
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) {
128 assertEquals(task2.getStack().mBounds, bounds);
129 assertEquals(task2.mBounds, bounds);
130 } else {
131 assertEquals(task2.mBounds, null);
132 }
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);
181 final ActivityStarter starter = new ActivityStarter(service, packageManager);
182
183 final IApplicationThread caller = mock(IApplicationThread.class);
184
185 // If no caller app, return {@code null} {@link ProcessRecord}.
186 final ProcessRecord record = containsConditions(preconditions, PRECONDITION_NO_CALLER_APP)
187 ? null : new ProcessRecord(mock(BatteryStatsImpl.class),
188 mock(ApplicationInfo.class), null, 0);
189
190 doReturn(record).when(service).getRecordForAppLocked(anyObject());
191
192 final Intent intent = new Intent();
193 intent.setFlags(launchFlags);
194
195 final ActivityInfo aInfo = containsConditions(preconditions, PRECONDITION_NO_ACTIVITY_INFO)
196 ? null : new ActivityInfo();
197
198 if (aInfo != null) {
199 aInfo.applicationInfo = new ApplicationInfo();
200 aInfo.applicationInfo.packageName = ActivityBuilder.DEFAULT_PACKAGE;
201 }
202
203 IVoiceInteractionSession voiceSession =
204 containsConditions(preconditions, PRECONDITION_SOURCE_VOICE_SESSION)
205 ? mock(IVoiceInteractionSession.class) : null;
206
207 // Create source token
208 final ActivityBuilder builder = new ActivityBuilder(service).setTask(
209 new TaskBuilder(service.mStackSupervisor).setVoiceSession(voiceSession).build());
210
211 // Offset uid by one from {@link ActivityInfo} to simulate different uids.
212 if (containsConditions(preconditions, PRECONDITION_DIFFERENT_UID)) {
213 builder.setUid(aInfo.applicationInfo.uid + 1);
214 }
215
216 final ActivityRecord source = builder.build();
217
218 if (!containsConditions(preconditions, PRECONDITION_NO_INTENT_COMPONENT)) {
219 intent.setComponent(source.realActivity);
220 }
221
222 if (containsConditions(preconditions, PRECONDITION_DISALLOW_APP_SWITCHING)) {
223 doReturn(false).when(service).checkAppSwitchAllowedLocked(anyInt(), anyInt(), anyInt(),
224 anyInt(), any());
225 }
226
227 if (containsConditions(preconditions,PRECONDITION_CANNOT_START_ANY_ACTIVITY)) {
228 doReturn(false).when(service.mStackSupervisor).checkStartAnyActivityPermission(
229 any(), any(), any(), anyInt(), anyInt(), anyInt(), any(), anyBoolean(),
230 any(), any(), any(), any());
231 }
232
233 try {
234 if (containsConditions(preconditions,
235 PRECONDITION_ACTIVITY_SUPPORTS_INTENT_EXCEPTION)) {
236 doAnswer((inv) -> {
237 throw new RemoteException();
238 }).when(packageManager).activitySupportsIntent(eq(source.realActivity), eq(intent),
239 any());
240 } else {
241 doReturn(!containsConditions(preconditions, PRECONDITION_NO_VOICE_SESSION_SUPPORT))
242 .when(packageManager).activitySupportsIntent(eq(source.realActivity),
243 eq(intent), any());
244 }
245 } catch (RemoteException e) {
246 }
247
248 final IBinder resultTo = containsConditions(preconditions, PRECONDITION_SOURCE_PRESENT)
249 || containsConditions(preconditions, PRECONDITION_SOURCE_VOICE_SESSION)
250 ? source.appToken : null;
251
252 final int requestCode = containsConditions(preconditions, PRECONDITION_REQUEST_CODE)
253 ? 1 : 0;
254
255 final int result = starter.startActivityLocked(caller, intent,
256 null /*ephemeralIntent*/, null /*resolvedType*/, aInfo, null /*rInfo*/,
257 null /*voiceSession*/, null /*voiceInteractor*/, resultTo,
258 null /*resultWho*/, requestCode, 0 /*callingPid*/, 0 /*callingUid*/,
259 null /*callingPackage*/, 0 /*realCallingPid*/, 0 /*realCallingUid*/,
260 0 /*startFlags*/, null /*options*/, false /*ignoreTargetSecurity*/,
261 false /*componentSpecified*/, null /*outActivity*/,
262 null /*inTask*/, "testLaunchActivityPermissionDenied");
263
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) {
270 final ActivityOptions options = spy(ActivityOptions.makeBasic());
271 final int optionResult = starter.startActivityLocked(caller, intent,
272 null /*ephemeralIntent*/, null /*resolvedType*/, aInfo, null /*rInfo*/,
273 null /*voiceSession*/, null /*voiceInteractor*/, resultTo,
274 null /*resultWho*/, requestCode, 0 /*callingPid*/, 0 /*callingUid*/,
275 null /*callingPackage*/, 0 /*realCallingPid*/, 0 /*realCallingUid*/,
276 0 /*startFlags*/, options /*options*/, false /*ignoreTargetSecurity*/,
277 false /*componentSpecified*/, null /*outActivity*/,
278 null /*inTask*/, "testLaunchActivityPermissionDenied");
279 verify(options, times(1)).abort();
280 }
281 }
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700282}