blob: 4ca1647ef415c591c1c0ab918e22041a5e98aa87 [file] [log] [blame]
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001/*
2 * Copyright (C) 2016 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 */
16package com.android.server.pm;
17
Makoto Onukid0010c52017-03-30 14:17:35 -070018import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.anyOrNull;
19import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.anyStringOrNull;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070020import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.cloneShortcutList;
21import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.hashSet;
22import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.list;
23import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.makeBundle;
24import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.set;
25
26import static org.mockito.Matchers.any;
Makoto Onuki157b1622016-06-02 16:13:10 -070027import static org.mockito.Matchers.anyInt;
28import static org.mockito.Matchers.anyString;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070029import static org.mockito.Matchers.eq;
30import static org.mockito.Mockito.doAnswer;
31import static org.mockito.Mockito.mock;
32import static org.mockito.Mockito.reset;
33import static org.mockito.Mockito.spy;
Makoto Onukiff14f732016-06-30 17:07:25 -070034import static org.mockito.Mockito.times;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070035import static org.mockito.Mockito.verify;
36import static org.mockito.Mockito.when;
37
38import android.annotation.NonNull;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070039import android.annotation.UserIdInt;
40import android.app.Activity;
Makoto Onuki33525d22016-08-03 15:45:24 -070041import android.app.ActivityManager;
Makoto Onukiea11db12016-06-24 15:17:44 -070042import android.app.ActivityManagerInternal;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070043import android.app.IUidObserver;
Makoto Onukiac042502016-05-20 16:39:42 -070044import android.app.usage.UsageStatsManagerInternal;
Makoto Onuki440a1ea2016-07-20 14:21:18 -070045import android.content.ActivityNotFoundException;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070046import android.content.BroadcastReceiver;
47import android.content.ComponentName;
48import android.content.Context;
49import android.content.Intent;
50import android.content.IntentFilter;
Makoto Onuki2d895c32016-12-02 15:48:40 -080051import android.content.IntentSender;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070052import android.content.pm.ActivityInfo;
53import android.content.pm.ApplicationInfo;
54import android.content.pm.ILauncherApps;
55import android.content.pm.LauncherApps;
56import android.content.pm.LauncherApps.ShortcutQuery;
57import android.content.pm.PackageInfo;
58import android.content.pm.PackageManager;
59import android.content.pm.PackageManagerInternal;
Makoto Onukib08790c2016-06-23 14:05:46 -070060import android.content.pm.ResolveInfo;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070061import android.content.pm.ShortcutInfo;
62import android.content.pm.ShortcutManager;
63import android.content.pm.ShortcutServiceInternal;
64import android.content.pm.Signature;
65import android.content.pm.UserInfo;
66import android.content.res.Resources;
67import android.content.res.XmlResourceParser;
68import android.graphics.drawable.Icon;
69import android.net.Uri;
70import android.os.Bundle;
71import android.os.FileUtils;
72import android.os.Handler;
73import android.os.Looper;
Makoto Onukib5a012f2016-06-21 11:13:53 -070074import android.os.PersistableBundle;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070075import android.os.Process;
Makoto Onukia01f4f02016-12-15 15:58:41 -080076import android.os.RemoteException;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070077import android.os.UserHandle;
78import android.os.UserManager;
79import android.test.InstrumentationTestCase;
80import android.test.mock.MockContext;
Makoto Onuki2d895c32016-12-02 15:48:40 -080081import android.util.ArrayMap;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070082import android.util.Log;
83import android.util.Pair;
84
85import com.android.internal.util.Preconditions;
86import com.android.server.LocalServices;
87import com.android.server.SystemService;
88import com.android.server.pm.LauncherAppsService.LauncherAppsImpl;
89import com.android.server.pm.ShortcutUser.PackageWithUser;
90
91import org.junit.Assert;
92import org.mockito.ArgumentCaptor;
93import org.mockito.invocation.InvocationOnMock;
94import org.mockito.stubbing.Answer;
95
96import java.io.BufferedReader;
97import java.io.ByteArrayOutputStream;
98import java.io.File;
99import java.io.FileReader;
Makoto Onuki76269922016-07-15 14:58:54 -0700100import java.io.IOException;
101import java.io.InputStreamReader;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700102import java.io.PrintWriter;
103import java.util.ArrayList;
104import java.util.HashMap;
105import java.util.HashSet;
106import java.util.LinkedHashMap;
107import java.util.List;
Makoto Onuki157b1622016-06-02 16:13:10 -0700108import java.util.Locale;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700109import java.util.Map;
110import java.util.Set;
Makoto Onukib08790c2016-06-23 14:05:46 -0700111import java.util.function.BiFunction;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700112import java.util.function.BiPredicate;
113import java.util.function.Consumer;
Makoto Onukia2241832016-07-06 13:28:37 -0700114import java.util.function.Function;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700115
116public abstract class BaseShortcutManagerTest extends InstrumentationTestCase {
117 protected static final String TAG = "ShortcutManagerTest";
118
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700119 protected static final boolean DUMP_IN_TEARDOWN = false; // DO NOT SUBMIT WITH true
120
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700121 /**
122 * Whether to enable dump or not. Should be only true when debugging to avoid bugs where
123 * dump affecting the behavior.
124 */
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700125 protected static final boolean ENABLE_DUMP = false // DO NOT SUBMIT WITH true
126 || DUMP_IN_TEARDOWN || ShortcutService.DEBUG;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700127
128 protected static final String[] EMPTY_STRINGS = new String[0]; // Just for readability.
129
Makoto Onukib08790c2016-06-23 14:05:46 -0700130 protected static final String MAIN_ACTIVITY_CLASS = "MainActivity";
Makoto Onuki2d895c32016-12-02 15:48:40 -0800131 protected static final String PIN_CONFIRM_ACTIVITY_CLASS = "PinConfirmActivity";
Makoto Onukib08790c2016-06-23 14:05:46 -0700132
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700133 // public for mockito
134 public class BaseContext extends MockContext {
135 @Override
136 public Object getSystemService(String name) {
137 switch (name) {
138 case Context.USER_SERVICE:
139 return mMockUserManager;
140 }
141 throw new UnsupportedOperationException();
142 }
143
144 @Override
145 public String getSystemServiceName(Class<?> serviceClass) {
146 return getTestContext().getSystemServiceName(serviceClass);
147 }
148
149 @Override
150 public PackageManager getPackageManager() {
151 return mMockPackageManager;
152 }
153
154 @Override
155 public Resources getResources() {
156 return getTestContext().getResources();
157 }
158
159 @Override
160 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
161 IntentFilter filter, String broadcastPermission, Handler scheduler) {
162 // ignore.
163 return null;
164 }
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700165
166 @Override
167 public void unregisterReceiver(BroadcastReceiver receiver) {
168 // ignore.
169 }
Makoto Onuki2d895c32016-12-02 15:48:40 -0800170
171 @Override
172 public void startActivityAsUser(Intent intent, UserHandle user) {
173 // ignore, use spy to intercept it.
174 }
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700175 }
176
177 /** Context used in the client side */
178 public class ClientContext extends BaseContext {
179 @Override
180 public String getPackageName() {
181 return mInjectedClientPackage;
182 }
183
184 @Override
185 public int getUserId() {
186 return getCallingUserId();
187 }
188 }
189
190 /** Context used in the service side */
191 public class ServiceContext extends BaseContext {
192 long injectClearCallingIdentity() {
193 final int prevCallingUid = mInjectedCallingUid;
194 mInjectedCallingUid = Process.SYSTEM_UID;
195 return prevCallingUid;
196 }
197
198 void injectRestoreCallingIdentity(long token) {
199 mInjectedCallingUid = (int) token;
200 }
201
202 @Override
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700203 public int getUserId() {
204 return UserHandle.USER_SYSTEM;
205 }
206
207 public PackageInfo injectGetActivitiesWithMetadata(
208 String packageName, @UserIdInt int userId) {
209 return BaseShortcutManagerTest.this.injectGetActivitiesWithMetadata(packageName, userId);
210 }
211
212 public XmlResourceParser injectXmlMetaData(ActivityInfo activityInfo, String key) {
213 return BaseShortcutManagerTest.this.injectXmlMetaData(activityInfo, key);
214 }
Makoto Onuki2d895c32016-12-02 15:48:40 -0800215
216 public void sendIntentSender(IntentSender intent) {
217 // Placeholder for spying.
218 }
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700219 }
220
221 /** ShortcutService with injection override methods. */
222 protected final class ShortcutServiceTestable extends ShortcutService {
223 final ServiceContext mContext;
224 IUidObserver mUidObserver;
225
226 public ShortcutServiceTestable(ServiceContext context, Looper looper) {
Makoto Onukiee6b6e42016-06-29 17:34:02 -0700227 super(context, looper, /* onyForPackageManagerApis */ false);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700228 mContext = context;
229 }
230
231 @Override
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700232 public String injectGetLocaleTagsForUser(@UserIdInt int userId) {
233 return mInjectedLocale.toLanguageTag();
234 }
235
236 @Override
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700237 boolean injectShouldPerformVerification() {
238 return true; // Always verify during unit tests.
239 }
240
241 @Override
242 String injectShortcutManagerConstants() {
243 return ConfigConstants.KEY_RESET_INTERVAL_SEC + "=" + (INTERVAL / 1000) + ","
244 + ConfigConstants.KEY_MAX_SHORTCUTS + "=" + MAX_SHORTCUTS + ","
245 + ConfigConstants.KEY_MAX_UPDATES_PER_INTERVAL + "="
246 + MAX_UPDATES_PER_INTERVAL + ","
247 + ConfigConstants.KEY_MAX_ICON_DIMENSION_DP + "=" + MAX_ICON_DIMENSION + ","
248 + ConfigConstants.KEY_MAX_ICON_DIMENSION_DP_LOWRAM + "="
249 + MAX_ICON_DIMENSION_LOWRAM + ","
250 + ConfigConstants.KEY_ICON_FORMAT + "=PNG,"
251 + ConfigConstants.KEY_ICON_QUALITY + "=100";
252 }
253
254 @Override
255 long injectClearCallingIdentity() {
256 return mContext.injectClearCallingIdentity();
257 }
258
259 @Override
260 void injectRestoreCallingIdentity(long token) {
261 mContext.injectRestoreCallingIdentity(token);
262 }
263
264 @Override
265 int injectDipToPixel(int dip) {
266 return dip;
267 }
268
269 @Override
270 long injectCurrentTimeMillis() {
271 return mInjectedCurrentTimeMillis;
272 }
273
274 @Override
275 long injectElapsedRealtime() {
276 // TODO This should be kept separately from mInjectedCurrentTimeMillis, since
277 // this should increase even if we rewind mInjectedCurrentTimeMillis in some tests.
278 return mInjectedCurrentTimeMillis - START_TIME;
279 }
280
281 @Override
Makoto Onuki475c3652017-05-08 14:29:03 -0700282 long injectUptimeMillis() {
283 return mInjectedCurrentTimeMillis - START_TIME - mDeepSleepTime;
284 }
285
286 @Override
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700287 int injectBinderCallingUid() {
288 return mInjectedCallingUid;
289 }
290
291 @Override
Makoto Onuki7d0fa812018-02-21 11:24:43 -0800292 int injectBinderCallingPid() {
293 // Note it's not used in tests, so just return a "random" value.
294 return mInjectedCallingUid * 123;
295 }
296
297 @Override
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700298 int injectGetPackageUid(String packageName, int userId) {
299 return getInjectedPackageInfo(packageName, userId, false).applicationInfo.uid;
300 }
301
302 @Override
303 File injectSystemDataPath() {
304 return new File(mInjectedFilePathRoot, "system");
305 }
306
307 @Override
308 File injectUserDataPath(@UserIdInt int userId) {
309 return new File(mInjectedFilePathRoot, "user-" + userId);
310 }
311
312 @Override
313 void injectValidateIconResPackage(ShortcutInfo shortcut, Icon icon) {
314 // Can't check
315 }
316
317 @Override
318 boolean injectIsLowRamDevice() {
319 return mInjectedIsLowRamDevice;
320 }
321
322 @Override
323 void injectRegisterUidObserver(IUidObserver observer, int which) {
324 mUidObserver = observer;
325 }
326
327 @Override
Makoto Onuki634cecb2017-10-13 17:10:48 -0700328 boolean hasShortcutHostPermission(@NonNull String callingPackage, int userId,
329 int callingPid, int callingUid) {
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700330 return mDefaultLauncherChecker.test(callingPackage, userId);
331 }
332
333 @Override
Makoto Onuki7d0fa812018-02-21 11:24:43 -0800334 boolean injectHasUnlimitedShortcutsApiCallsPermission(int callingPid, int callingUid) {
335 return mInjectHasUnlimitedShortcutsApiCallsPermission;
336 }
337
338 @Override
Makoto Onuki2d895c32016-12-02 15:48:40 -0800339 ComponentName getDefaultLauncher(@UserIdInt int userId) {
340 final ComponentName activity = mDefaultLauncher.get(userId);
341 if (activity != null) {
342 return activity;
343 }
344 return super.getDefaultLauncher(userId);
345 }
346
347 @Override
Makoto Onukiee6b6e42016-06-29 17:34:02 -0700348 PackageInfo injectPackageInfoWithUninstalled(String packageName, @UserIdInt int userId,
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700349 boolean getSignatures) {
350 return getInjectedPackageInfo(packageName, userId, getSignatures);
351 }
352
353 @Override
Makoto Onukiee6b6e42016-06-29 17:34:02 -0700354 ApplicationInfo injectApplicationInfoWithUninstalled(
355 String packageName, @UserIdInt int userId) {
356 PackageInfo pi = injectPackageInfoWithUninstalled(
357 packageName, userId, /* getSignatures= */ false);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700358 return pi != null ? pi.applicationInfo : null;
359 }
360
361 @Override
Makoto Onukiee6b6e42016-06-29 17:34:02 -0700362 List<PackageInfo> injectGetPackagesWithUninstalled(@UserIdInt int userId) {
363 return BaseShortcutManagerTest.this.getInstalledPackagesWithUninstalled(userId);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700364 }
365
366 @Override
Makoto Onukiee6b6e42016-06-29 17:34:02 -0700367 ActivityInfo injectGetActivityInfoWithMetadataWithUninstalled(ComponentName activity,
Makoto Onukib08790c2016-06-23 14:05:46 -0700368 @UserIdInt int userId) {
369 final PackageInfo pi = mContext.injectGetActivitiesWithMetadata(
370 activity.getPackageName(), userId);
371 if (pi == null || pi.activities == null) {
372 return null;
373 }
374 for (ActivityInfo ai : pi.activities) {
375 if (!mEnabledActivityChecker.test(ai.getComponentName(), userId)) {
376 continue;
377 }
378 if (activity.equals(ai.getComponentName())) {
379 return ai;
380 }
381 }
382 return null;
383 }
384
385 @Override
386 boolean injectIsMainActivity(@NonNull ComponentName activity, int userId) {
387 if (!mEnabledActivityChecker.test(activity, userId)) {
388 return false;
389 }
390 return mMainActivityChecker.test(activity, userId);
391 }
392
393 @Override
394 List<ResolveInfo> injectGetMainActivities(@NonNull String packageName, int userId) {
395 final PackageInfo pi = mContext.injectGetActivitiesWithMetadata(
396 packageName, userId);
397 if (pi == null || pi.activities == null) {
398 return null;
399 }
400 final ArrayList<ResolveInfo> ret = new ArrayList<>(pi.activities.length);
401 for (int i = 0; i < pi.activities.length; i++) {
402 if (!mEnabledActivityChecker.test(pi.activities[i].getComponentName(), userId)) {
403 continue;
404 }
405 final ResolveInfo ri = new ResolveInfo();
406 ri.activityInfo = pi.activities[i];
407 ret.add(ri);
408 }
409
410 return ret;
411 }
412
413 @Override
414 ComponentName injectGetDefaultMainActivity(@NonNull String packageName, int userId) {
415 return mMainActivityFetcher.apply(packageName, userId);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700416 }
417
418 @Override
Makoto Onuki2d895c32016-12-02 15:48:40 -0800419 ComponentName injectGetPinConfirmationActivity(@NonNull String launcherPackageName,
Sunny Goyal7f7372a2017-01-24 11:53:54 -0800420 int launcherUserId, int requestType) {
Makoto Onuki2d895c32016-12-02 15:48:40 -0800421 return mPinConfirmActivityFetcher.apply(launcherPackageName, launcherUserId);
422 }
423
424 @Override
Makoto Onukiee6b6e42016-06-29 17:34:02 -0700425 boolean injectIsActivityEnabledAndExported(ComponentName activity, @UserIdInt int userId) {
426 return mEnabledActivityChecker.test(activity, userId);
427 }
428
429 @Override
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700430 XmlResourceParser injectXmlMetaData(ActivityInfo activityInfo, String key) {
431 return mContext.injectXmlMetaData(activityInfo, key);
432 }
433
434 @Override
Makoto Onuki157b1622016-06-02 16:13:10 -0700435 void injectPostToHandler(Runnable r) {
Makoto Onukia2241832016-07-06 13:28:37 -0700436 runOnHandler(r);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700437 }
438
439 @Override
Makoto Onuki085a05c2016-08-19 11:39:29 -0700440 void injectRunOnNewThread(Runnable r) {
441 runOnHandler(r);
442 }
443
444 @Override
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700445 void injectEnforceCallingPermission(String permission, String message) {
446 if (!mCallerPermissions.contains(permission)) {
447 throw new SecurityException("Missing permission: " + permission);
448 }
449 }
450
451 @Override
Makoto Onukib08790c2016-06-23 14:05:46 -0700452 boolean injectIsSafeModeEnabled() {
453 return mSafeMode;
454 }
455
456 @Override
Makoto Onuki33663282016-08-22 16:19:04 -0700457 String injectBuildFingerprint() {
458 return mInjectedBuildFingerprint;
459 }
460
461 @Override
Sunny Goyal87a563e2017-01-01 19:42:45 -0800462 void injectSendIntentSender(IntentSender intent, Intent extras) {
Makoto Onuki2d895c32016-12-02 15:48:40 -0800463 mContext.sendIntentSender(intent);
464 }
465
466 @Override
Makoto Onuki35559d62017-11-06 16:26:32 -0800467 boolean injectHasAccessShortcutsPermission(int callingPid, int callingUid) {
Makoto Onuki634cecb2017-10-13 17:10:48 -0700468 return mInjectCheckAccessShortcutsPermission;
469 }
470
471 @Override
Makoto Onukia2241832016-07-06 13:28:37 -0700472 void wtf(String message, Throwable th) {
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700473 // During tests, WTF is fatal.
Makoto Onukia2241832016-07-06 13:28:37 -0700474 fail(message + " exception: " + th + "\n" + Log.getStackTraceString(th));
475 }
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700476 }
477
478 /** ShortcutManager with injection override methods. */
479 protected class ShortcutManagerTestable extends ShortcutManager {
480 public ShortcutManagerTestable(Context context, ShortcutServiceTestable service) {
481 super(context, service);
482 }
483
484 @Override
485 protected int injectMyUserId() {
486 return UserHandle.getUserId(mInjectedCallingUid);
487 }
488
489 @Override
490 public boolean setDynamicShortcuts(@NonNull List<ShortcutInfo> shortcutInfoList) {
491 // Note to simulate the binder RPC, we need to clone the incoming arguments.
492 // Otherwise bad things will happen because they're mutable.
493 return super.setDynamicShortcuts(cloneShortcutList(shortcutInfoList));
494 }
495
496 @Override
497 public boolean addDynamicShortcuts(@NonNull List<ShortcutInfo> shortcutInfoList) {
498 // Note to simulate the binder RPC, we need to clone the incoming arguments.
499 return super.addDynamicShortcuts(cloneShortcutList(shortcutInfoList));
500 }
501
502 @Override
503 public boolean updateShortcuts(List<ShortcutInfo> shortcutInfoList) {
504 // Note to simulate the binder RPC, we need to clone the incoming arguments.
505 return super.updateShortcuts(cloneShortcutList(shortcutInfoList));
506 }
507 }
508
509 protected class LauncherAppImplTestable extends LauncherAppsImpl {
510 final ServiceContext mContext;
511
512 public LauncherAppImplTestable(ServiceContext context) {
513 super(context);
514 mContext = context;
515 }
516
517 @Override
518 public void verifyCallingPackage(String callingPackage) {
519 // SKIP
520 }
521
522 @Override
523 void postToPackageMonitorHandler(Runnable r) {
Makoto Onukia2241832016-07-06 13:28:37 -0700524 runOnHandler(r);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700525 }
526
527 @Override
528 int injectBinderCallingUid() {
529 return mInjectedCallingUid;
530 }
531
532 @Override
Makoto Onuki7d0fa812018-02-21 11:24:43 -0800533 int injectBinderCallingPid() {
534 // Note it's not used in tests, so just return a "random" value.
535 return mInjectedCallingUid * 123;
536 }
537
538 @Override
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700539 long injectClearCallingIdentity() {
540 final int prevCallingUid = mInjectedCallingUid;
541 mInjectedCallingUid = Process.SYSTEM_UID;
542 return prevCallingUid;
543 }
544
545 @Override
546 void injectRestoreCallingIdentity(long token) {
547 mInjectedCallingUid = (int) token;
548 }
549 }
550
551 protected class LauncherAppsTestable extends LauncherApps {
552 public LauncherAppsTestable(Context context, ILauncherApps service) {
553 super(context, service);
554 }
555 }
556
557 public static class ShortcutActivity extends Activity {
558 }
559
560 public static class ShortcutActivity2 extends Activity {
561 }
562
563 public static class ShortcutActivity3 extends Activity {
564 }
565
Makoto Onukia2241832016-07-06 13:28:37 -0700566 protected Looper mLooper;
567 protected Handler mHandler;
568
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700569 protected ServiceContext mServiceContext;
570 protected ClientContext mClientContext;
571
572 protected ShortcutServiceTestable mService;
573 protected ShortcutManagerTestable mManager;
574 protected ShortcutServiceInternal mInternal;
575
576 protected LauncherAppImplTestable mLauncherAppImpl;
577
578 // LauncherApps has per-instace state, so we need a differnt instance for each launcher.
579 protected final Map<Pair<Integer, String>, LauncherAppsTestable>
580 mLauncherAppsMap = new HashMap<>();
581 protected LauncherAppsTestable mLauncherApps; // Current one
582
583 protected File mInjectedFilePathRoot;
584
Makoto Onukib08790c2016-06-23 14:05:46 -0700585 protected boolean mSafeMode;
586
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700587 protected long mInjectedCurrentTimeMillis;
Makoto Onuki475c3652017-05-08 14:29:03 -0700588 protected long mDeepSleepTime; // Used to calculate "uptimeMillis".
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700589
590 protected boolean mInjectedIsLowRamDevice;
591
Makoto Onuki157b1622016-06-02 16:13:10 -0700592 protected Locale mInjectedLocale = Locale.ENGLISH;
593
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700594 protected int mInjectedCallingUid;
595 protected String mInjectedClientPackage;
596
597 protected Map<String, PackageInfo> mInjectedPackages;
598
599 protected Set<PackageWithUser> mUninstalledPackages;
Makoto Onuki82fb2eb2017-03-31 16:58:26 -0700600 protected Set<PackageWithUser> mDisabledPackages;
Makoto Onuki66e4a2b2017-01-23 11:37:45 -0800601 protected Set<PackageWithUser> mEphemeralPackages;
Makoto Onuki33663282016-08-22 16:19:04 -0700602 protected Set<String> mSystemPackages;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700603
604 protected PackageManager mMockPackageManager;
605 protected PackageManagerInternal mMockPackageManagerInternal;
606 protected UserManager mMockUserManager;
Makoto Onukiac042502016-05-20 16:39:42 -0700607 protected UsageStatsManagerInternal mMockUsageStatsManagerInternal;
Makoto Onukiea11db12016-06-24 15:17:44 -0700608 protected ActivityManagerInternal mMockActivityManagerInternal;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700609
610 protected static final String CALLING_PACKAGE_1 = "com.android.test.1";
611 protected static final int CALLING_UID_1 = 10001;
612
613 protected static final String CALLING_PACKAGE_2 = "com.android.test.2";
614 protected static final int CALLING_UID_2 = 10002;
615
616 protected static final String CALLING_PACKAGE_3 = "com.android.test.3";
617 protected static final int CALLING_UID_3 = 10003;
618
619 protected static final String CALLING_PACKAGE_4 = "com.android.test.4";
620 protected static final int CALLING_UID_4 = 10004;
621
622 protected static final String LAUNCHER_1 = "com.android.launcher.1";
623 protected static final int LAUNCHER_UID_1 = 10011;
624
625 protected static final String LAUNCHER_2 = "com.android.launcher.2";
626 protected static final int LAUNCHER_UID_2 = 10012;
627
628 protected static final String LAUNCHER_3 = "com.android.launcher.3";
629 protected static final int LAUNCHER_UID_3 = 10013;
630
631 protected static final String LAUNCHER_4 = "com.android.launcher.4";
632 protected static final int LAUNCHER_UID_4 = 10014;
633
634 protected static final int USER_0 = UserHandle.USER_SYSTEM;
635 protected static final int USER_10 = 10;
636 protected static final int USER_11 = 11;
Makoto Onukide3c16c2017-01-26 11:39:31 -0800637 protected static final int USER_P0 = 20; // profile of user 0 (MANAGED_PROFILE *not* set)
638 protected static final int USER_P1 = 21; // another profile of user 0 (MANAGED_PROFILE set)
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700639
640 protected static final UserHandle HANDLE_USER_0 = UserHandle.of(USER_0);
641 protected static final UserHandle HANDLE_USER_10 = UserHandle.of(USER_10);
642 protected static final UserHandle HANDLE_USER_11 = UserHandle.of(USER_11);
643 protected static final UserHandle HANDLE_USER_P0 = UserHandle.of(USER_P0);
Makoto Onukide3c16c2017-01-26 11:39:31 -0800644 protected static final UserHandle HANDLE_USER_P1 = UserHandle.of(USER_P1);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700645
646 protected static final UserInfo USER_INFO_0 = withProfileGroupId(
647 new UserInfo(USER_0, "user0",
Makoto Onuki2d895c32016-12-02 15:48:40 -0800648 UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY | UserInfo.FLAG_INITIALIZED), 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700649
650 protected static final UserInfo USER_INFO_10 =
651 new UserInfo(USER_10, "user10", UserInfo.FLAG_INITIALIZED);
652
653 protected static final UserInfo USER_INFO_11 =
654 new UserInfo(USER_11, "user11", UserInfo.FLAG_INITIALIZED);
655
Makoto Onukiaecbd032017-01-19 12:11:11 -0800656 /*
657 * Cheat: USER_P0 is a sub profile of USER_0, but it doesn't have the MANAGED_PROFILE flag set.
658 * Due to a change made to LauncherApps (b/34340531), work profile apps a no longer able
659 * to see the main profile, which would break tons of unit tests. We avoid it by not setting
660 * MANAGED_PROFILE for P0.
661 * We cover this negative case in CTS. (i.e. CTS has tests to make sure maanged profile
662 * can't access main profile's shortcuts.)
663 */
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700664 protected static final UserInfo USER_INFO_P0 = withProfileGroupId(
Makoto Onukiaecbd032017-01-19 12:11:11 -0800665 new UserInfo(USER_P0, "userP0", UserInfo.FLAG_INITIALIZED), 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700666
Makoto Onukide3c16c2017-01-26 11:39:31 -0800667 protected static final UserInfo USER_INFO_P1 = withProfileGroupId(
668 new UserInfo(USER_P1, "userP1",
669 UserInfo.FLAG_INITIALIZED | UserInfo.FLAG_MANAGED_PROFILE), 0);
670
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700671 protected BiPredicate<String, Integer> mDefaultLauncherChecker =
672 (callingPackage, userId) ->
673 LAUNCHER_1.equals(callingPackage) || LAUNCHER_2.equals(callingPackage)
674 || LAUNCHER_3.equals(callingPackage) || LAUNCHER_4.equals(callingPackage);
675
Makoto Onuki2d895c32016-12-02 15:48:40 -0800676 private final Map<Integer, ComponentName> mDefaultLauncher = new ArrayMap<>();
677
Makoto Onukib08790c2016-06-23 14:05:46 -0700678 protected BiPredicate<ComponentName, Integer> mMainActivityChecker =
679 (activity, userId) -> true;
680
681 protected BiFunction<String, Integer, ComponentName> mMainActivityFetcher =
682 (packageName, userId) -> new ComponentName(packageName, MAIN_ACTIVITY_CLASS);
683
Makoto Onuki2d895c32016-12-02 15:48:40 -0800684 protected BiFunction<String, Integer, ComponentName> mPinConfirmActivityFetcher =
685 (packageName, userId) -> new ComponentName(packageName, PIN_CONFIRM_ACTIVITY_CLASS);
686
Makoto Onukib08790c2016-06-23 14:05:46 -0700687 protected BiPredicate<ComponentName, Integer> mEnabledActivityChecker
688 = (activity, userId) -> true; // all activities are enabled.
689
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700690 protected static final long START_TIME = 1440000000101L;
691
692 protected static final long INTERVAL = 10000;
693
694 protected static final int MAX_SHORTCUTS = 10;
695
696 protected static final int MAX_UPDATES_PER_INTERVAL = 3;
697
698 protected static final int MAX_ICON_DIMENSION = 128;
699
700 protected static final int MAX_ICON_DIMENSION_LOWRAM = 32;
701
702 protected static final ShortcutQuery QUERY_ALL = new ShortcutQuery();
703
704 protected final ArrayList<String> mCallerPermissions = new ArrayList<>();
705
706 protected final HashMap<String, LinkedHashMap<ComponentName, Integer>> mActivityMetadataResId
707 = new HashMap<>();
708
Makoto Onukia2241832016-07-06 13:28:37 -0700709 protected final Map<Integer, UserInfo> mUserInfos = new HashMap<>();
710 protected final Map<Integer, Boolean> mRunningUsers = new HashMap<>();
711 protected final Map<Integer, Boolean> mUnlockedUsers = new HashMap<>();
712
Makoto Onuki0b9d1db2016-07-18 14:16:41 -0700713 protected static final String PACKAGE_SYSTEM_LAUNCHER = "com.android.systemlauncher";
714 protected static final String PACKAGE_SYSTEM_LAUNCHER_NAME = "systemlauncher_name";
715 protected static final int PACKAGE_SYSTEM_LAUNCHER_PRIORITY = 0;
716
717 protected static final String PACKAGE_FALLBACK_LAUNCHER = "com.android.settings";
718 protected static final String PACKAGE_FALLBACK_LAUNCHER_NAME = "fallback";
719 protected static final int PACKAGE_FALLBACK_LAUNCHER_PRIORITY = -999;
720
Makoto Onuki33663282016-08-22 16:19:04 -0700721 protected String mInjectedBuildFingerprint = "build1";
722
Makoto Onuki634cecb2017-10-13 17:10:48 -0700723 protected boolean mInjectCheckAccessShortcutsPermission = false;
724
Makoto Onuki7d0fa812018-02-21 11:24:43 -0800725 protected boolean mInjectHasUnlimitedShortcutsApiCallsPermission = false;
726
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700727 static {
728 QUERY_ALL.setQueryFlags(
Makoto Onuki4d6b87f2016-06-17 13:47:40 -0700729 ShortcutQuery.FLAG_GET_ALL_KINDS);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700730 }
731
732 @Override
733 protected void setUp() throws Exception {
734 super.setUp();
735
Makoto Onukia2241832016-07-06 13:28:37 -0700736 mLooper = Looper.getMainLooper();
737 mHandler = new Handler(mLooper);
738
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700739 mServiceContext = spy(new ServiceContext());
740 mClientContext = new ClientContext();
741
742 mMockPackageManager = mock(PackageManager.class);
743 mMockPackageManagerInternal = mock(PackageManagerInternal.class);
744 mMockUserManager = mock(UserManager.class);
Makoto Onukiac042502016-05-20 16:39:42 -0700745 mMockUsageStatsManagerInternal = mock(UsageStatsManagerInternal.class);
Makoto Onukiea11db12016-06-24 15:17:44 -0700746 mMockActivityManagerInternal = mock(ActivityManagerInternal.class);
Makoto Onukiac042502016-05-20 16:39:42 -0700747
748 LocalServices.removeServiceForTest(PackageManagerInternal.class);
749 LocalServices.addService(PackageManagerInternal.class, mMockPackageManagerInternal);
750 LocalServices.removeServiceForTest(UsageStatsManagerInternal.class);
751 LocalServices.addService(UsageStatsManagerInternal.class, mMockUsageStatsManagerInternal);
Makoto Onukiea11db12016-06-24 15:17:44 -0700752 LocalServices.removeServiceForTest(ActivityManagerInternal.class);
753 LocalServices.addService(ActivityManagerInternal.class, mMockActivityManagerInternal);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700754
755 // Prepare injection values.
756
757 mInjectedCurrentTimeMillis = START_TIME;
758
759 mInjectedPackages = new HashMap<>();
760 addPackage(CALLING_PACKAGE_1, CALLING_UID_1, 1);
761 addPackage(CALLING_PACKAGE_2, CALLING_UID_2, 2);
762 addPackage(CALLING_PACKAGE_3, CALLING_UID_3, 3);
763 addPackage(CALLING_PACKAGE_4, CALLING_UID_4, 10);
764 addPackage(LAUNCHER_1, LAUNCHER_UID_1, 4);
765 addPackage(LAUNCHER_2, LAUNCHER_UID_2, 5);
766 addPackage(LAUNCHER_3, LAUNCHER_UID_3, 6);
767 addPackage(LAUNCHER_4, LAUNCHER_UID_4, 10);
768
769 // CALLING_PACKAGE_3 / LAUNCHER_3 are not backup target.
770 updatePackageInfo(CALLING_PACKAGE_3,
771 pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP);
772 updatePackageInfo(LAUNCHER_3,
773 pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP);
774
775 mUninstalledPackages = new HashSet<>();
Makoto Onuki82fb2eb2017-03-31 16:58:26 -0700776 mDisabledPackages = new HashSet<>();
Makoto Onuki33663282016-08-22 16:19:04 -0700777 mSystemPackages = new HashSet<>();
Makoto Onuki66e4a2b2017-01-23 11:37:45 -0800778 mEphemeralPackages = new HashSet<>();
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700779
780 mInjectedFilePathRoot = new File(getTestContext().getCacheDir(), "test-files");
781
782 deleteAllSavedFiles();
783
784 // Set up users.
Makoto Onukia2241832016-07-06 13:28:37 -0700785 when(mMockUserManager.getUserInfo(anyInt())).thenAnswer(new AnswerWithSystemCheck<>(
786 inv -> mUserInfos.get((Integer) inv.getArguments()[0])));
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700787
Makoto Onukia2241832016-07-06 13:28:37 -0700788 mUserInfos.put(USER_0, USER_INFO_0);
789 mUserInfos.put(USER_10, USER_INFO_10);
790 mUserInfos.put(USER_11, USER_INFO_11);
791 mUserInfos.put(USER_P0, USER_INFO_P0);
Makoto Onukide3c16c2017-01-26 11:39:31 -0800792 mUserInfos.put(USER_P1, USER_INFO_P1);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700793
Makoto Onukia2241832016-07-06 13:28:37 -0700794 // Set up isUserRunning and isUserUnlocked.
795 when(mMockUserManager.isUserRunning(anyInt())).thenAnswer(new AnswerWithSystemCheck<>(
Makoto Onuki9c850012016-07-26 15:50:50 -0700796 inv -> b(mRunningUsers.get((Integer) inv.getArguments()[0]))));
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700797
Makoto Onuki9c850012016-07-26 15:50:50 -0700798 when(mMockUserManager.isUserUnlocked(anyInt()))
799 .thenAnswer(new AnswerWithSystemCheck<>(inv -> {
800 final int userId = (Integer) inv.getArguments()[0];
801 return b(mRunningUsers.get(userId)) && b(mUnlockedUsers.get(userId));
802 }));
803 // isUserUnlockingOrUnlocked() return the same value as isUserUnlocked().
804 when(mMockUserManager.isUserUnlockingOrUnlocked(anyInt()))
805 .thenAnswer(new AnswerWithSystemCheck<>(inv -> {
806 final int userId = (Integer) inv.getArguments()[0];
807 return b(mRunningUsers.get(userId)) && b(mUnlockedUsers.get(userId));
808 }));
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700809
Makoto Onuki2d895c32016-12-02 15:48:40 -0800810 when(mMockUserManager.getProfileParent(anyInt()))
811 .thenAnswer(new AnswerWithSystemCheck<>(inv -> {
812 final int userId = (Integer) inv.getArguments()[0];
813 final UserInfo ui = mUserInfos.get(userId);
814 assertNotNull(ui);
815 if (ui.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID) {
816 return null;
817 }
818 final UserInfo parent = mUserInfos.get(ui.profileGroupId);
819 assertNotNull(parent);
820 return parent;
821 }));
Makoto Onukide3c16c2017-01-26 11:39:31 -0800822 when(mMockUserManager.isManagedProfile(anyInt()))
823 .thenAnswer(new AnswerWithSystemCheck<>(inv -> {
824 final int userId = (Integer) inv.getArguments()[0];
825 final UserInfo ui = mUserInfos.get(userId);
826 assertNotNull(ui);
827 return ui.isManagedProfile();
828 }));
Makoto Onuki2d895c32016-12-02 15:48:40 -0800829
Makoto Onuki33525d22016-08-03 15:45:24 -0700830 when(mMockActivityManagerInternal.getUidProcessState(anyInt())).thenReturn(
831 ActivityManager.PROCESS_STATE_CACHED_EMPTY);
832
Makoto Onuki9c850012016-07-26 15:50:50 -0700833 // User 0 and P0 are always running
Makoto Onukia2241832016-07-06 13:28:37 -0700834 mRunningUsers.put(USER_0, true);
835 mRunningUsers.put(USER_10, false);
836 mRunningUsers.put(USER_11, false);
Makoto Onuki9c850012016-07-26 15:50:50 -0700837 mRunningUsers.put(USER_P0, true);
Makoto Onukide3c16c2017-01-26 11:39:31 -0800838 mRunningUsers.put(USER_P1, true);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700839
Makoto Onukia2241832016-07-06 13:28:37 -0700840 // Unlock all users by default.
841 mUnlockedUsers.put(USER_0, true);
842 mUnlockedUsers.put(USER_10, true);
843 mUnlockedUsers.put(USER_11, true);
844 mUnlockedUsers.put(USER_P0, true);
Makoto Onukide3c16c2017-01-26 11:39:31 -0800845 mUnlockedUsers.put(USER_P1, true);
Makoto Onukia2241832016-07-06 13:28:37 -0700846
847 // Set up resources
Makoto Onuki157b1622016-06-02 16:13:10 -0700848 setUpAppResources();
849
850 // Start the service.
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700851 initService();
852 setCaller(CALLING_PACKAGE_1);
Makoto Onuki248a0ef2016-11-03 15:59:01 -0700853
854 if (ENABLE_DUMP) {
855 Log.d(TAG, "setUp done");
856 }
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700857 }
858
Makoto Onuki9c850012016-07-26 15:50:50 -0700859 private static boolean b(Boolean value) {
860 return (value != null && value);
861 }
862
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700863 /**
864 * Returns a boolean but also checks if the current UID is SYSTEM_UID.
865 */
Makoto Onukia2241832016-07-06 13:28:37 -0700866 protected class AnswerWithSystemCheck<T> implements Answer<T> {
867 private final Function<InvocationOnMock, T> mChecker;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700868
Makoto Onukia2241832016-07-06 13:28:37 -0700869 public AnswerWithSystemCheck(Function<InvocationOnMock, T> checker) {
870 mChecker = checker;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700871 }
872
873 @Override
Makoto Onukia2241832016-07-06 13:28:37 -0700874 public T answer(InvocationOnMock invocation) throws Throwable {
875 assertEquals("Must be called on SYSTEM UID.",
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700876 Process.SYSTEM_UID, mInjectedCallingUid);
Makoto Onukia2241832016-07-06 13:28:37 -0700877 return mChecker.apply(invocation);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700878 }
879 }
880
Makoto Onuki157b1622016-06-02 16:13:10 -0700881 protected void setUpAppResources() throws Exception {
882 setUpAppResources(/* offset = */ 0);
883 }
884
885 protected void setUpAppResources(int ressIdOffset) throws Exception {
886 // ressIdOffset is used to adjust resource IDs to emulate the case where an updated app
887 // has resource IDs changed.
888
889 doAnswer(pmInvocation -> {
890 assertEquals(Process.SYSTEM_UID, mInjectedCallingUid);
891
892 final String packageName = (String) pmInvocation.getArguments()[0];
893 final int userId = (Integer) pmInvocation.getArguments()[1];
894
895 final Resources res = mock(Resources.class);
896
897 doAnswer(resInvocation -> {
898 final int argResId = (Integer) resInvocation.getArguments()[0];
899
900 return "string-" + packageName + "-user:" + userId + "-res:" + argResId
901 + "/" + mInjectedLocale;
902 }).when(res).getString(anyInt());
903
904 doAnswer(resInvocation -> {
905 final int resId = (Integer) resInvocation.getArguments()[0];
906
907 // Always use the "string" resource type. The type doesn't matter during the test.
908 return packageName + ":string/r" + resId;
909 }).when(res).getResourceName(anyInt());
910
911 doAnswer(resInvocation -> {
912 final String argResName = (String) resInvocation.getArguments()[0];
913 final String argType = (String) resInvocation.getArguments()[1];
914 final String argPackageName = (String) resInvocation.getArguments()[2];
915
916 // See the above code. getResourceName() will just use "r" + res ID as the entry
917 // name.
918 String entryName = argResName;
919 if (entryName.contains("/")) {
920 entryName = ShortcutInfo.getResourceEntryName(entryName);
921 }
922 return Integer.parseInt(entryName.substring(1)) + ressIdOffset;
Makoto Onukid0010c52017-03-30 14:17:35 -0700923 }).when(res).getIdentifier(anyStringOrNull(), anyStringOrNull(), anyStringOrNull());
Makoto Onuki157b1622016-06-02 16:13:10 -0700924 return res;
925 }).when(mMockPackageManager).getResourcesForApplicationAsUser(anyString(), anyInt());
926 }
927
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700928 protected static UserInfo withProfileGroupId(UserInfo in, int groupId) {
929 in.profileGroupId = groupId;
930 return in;
931 }
932
933 @Override
934 protected void tearDown() throws Exception {
935 if (DUMP_IN_TEARDOWN) dumpsysOnLogcat("Teardown");
936
937 shutdownServices();
938
939 super.tearDown();
940 }
941
942 protected Context getTestContext() {
943 return getInstrumentation().getContext();
944 }
945
Makoto Onukia1d38b32016-06-10 15:32:26 -0700946 protected ShortcutManager getManager() {
947 return mManager;
948 }
949
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700950 protected void deleteAllSavedFiles() {
951 // Empty the data directory.
952 if (mInjectedFilePathRoot.exists()) {
953 Assert.assertTrue("failed to delete dir",
954 FileUtils.deleteContents(mInjectedFilePathRoot));
955 }
956 mInjectedFilePathRoot.mkdirs();
957 }
958
959 /** (Re-) init the manager and the service. */
960 protected void initService() {
961 shutdownServices();
962
963 LocalServices.removeServiceForTest(ShortcutServiceInternal.class);
964
965 // Instantiate targets.
Makoto Onukia2241832016-07-06 13:28:37 -0700966 mService = new ShortcutServiceTestable(mServiceContext, mLooper);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700967 mManager = new ShortcutManagerTestable(mClientContext, mService);
968
969 mInternal = LocalServices.getService(ShortcutServiceInternal.class);
970
971 mLauncherAppImpl = new LauncherAppImplTestable(mServiceContext);
972 mLauncherApps = null;
973 mLauncherAppsMap.clear();
974
Makoto Onuki157b1622016-06-02 16:13:10 -0700975 // Send boot sequence events.
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700976 mService.onBootPhase(SystemService.PHASE_LOCK_SETTINGS_READY);
Makoto Onuki157b1622016-06-02 16:13:10 -0700977
Makoto Onuki157b1622016-06-02 16:13:10 -0700978 mService.onBootPhase(SystemService.PHASE_BOOT_COMPLETED);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700979 }
980
981 protected void shutdownServices() {
982 if (mService != null) {
983 // Flush all the unsaved data from the previous instance.
984 mService.saveDirtyInfo();
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700985
986 // Make sure everything is consistent.
987 mService.verifyStates();
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700988 }
989 LocalServices.removeServiceForTest(ShortcutServiceInternal.class);
990
991 mService = null;
992 mManager = null;
993 mInternal = null;
994 mLauncherAppImpl = null;
995 mLauncherApps = null;
996 mLauncherAppsMap.clear();
997 }
998
Makoto Onukia2241832016-07-06 13:28:37 -0700999 protected void runOnHandler(Runnable r) {
1000 final long token = mServiceContext.injectClearCallingIdentity();
1001 try {
1002 r.run();
1003 } finally {
1004 mServiceContext.injectRestoreCallingIdentity(token);
1005 }
1006 }
1007
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001008 protected void addPackage(String packageName, int uid, int version) {
1009 addPackage(packageName, uid, version, packageName);
1010 }
1011
1012 protected Signature[] genSignatures(String... signatures) {
1013 final Signature[] sigs = new Signature[signatures.length];
1014 for (int i = 0; i < signatures.length; i++){
1015 sigs[i] = new Signature(signatures[i].getBytes());
1016 }
1017 return sigs;
1018 }
1019
1020 protected PackageInfo genPackage(String packageName, int uid, int version, String... signatures) {
1021 final PackageInfo pi = new PackageInfo();
1022 pi.packageName = packageName;
1023 pi.applicationInfo = new ApplicationInfo();
1024 pi.applicationInfo.uid = uid;
1025 pi.applicationInfo.flags = ApplicationInfo.FLAG_INSTALLED
1026 | ApplicationInfo.FLAG_ALLOW_BACKUP;
1027 pi.versionCode = version;
1028 pi.applicationInfo.versionCode = version;
1029 pi.signatures = genSignatures(signatures);
1030
1031 return pi;
1032 }
1033
1034 protected void addPackage(String packageName, int uid, int version, String... signatures) {
1035 mInjectedPackages.put(packageName, genPackage(packageName, uid, version, signatures));
1036 }
1037
1038 protected void updatePackageInfo(String packageName, Consumer<PackageInfo> c) {
1039 c.accept(mInjectedPackages.get(packageName));
1040 }
1041
1042 protected void updatePackageVersion(String packageName, int increment) {
1043 updatePackageInfo(packageName, pi -> {
1044 pi.versionCode += increment;
1045 pi.applicationInfo.versionCode += increment;
1046 });
1047 }
1048
1049 protected void updatePackageLastUpdateTime(String packageName, long increment) {
1050 updatePackageInfo(packageName, pi -> {
1051 pi.lastUpdateTime += increment;
1052 });
1053 }
1054
Makoto Onuki085a05c2016-08-19 11:39:29 -07001055 protected void setPackageLastUpdateTime(String packageName, long value) {
1056 updatePackageInfo(packageName, pi -> {
1057 pi.lastUpdateTime = value;
1058 });
1059 }
1060
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001061 protected void uninstallPackage(int userId, String packageName) {
1062 if (ENABLE_DUMP) {
Makoto Onuki82fb2eb2017-03-31 16:58:26 -07001063 Log.v(TAG, "Uninstall package " + packageName + " / " + userId);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001064 }
1065 mUninstalledPackages.add(PackageWithUser.of(userId, packageName));
1066 }
1067
1068 protected void installPackage(int userId, String packageName) {
1069 if (ENABLE_DUMP) {
1070 Log.v(TAG, "Install package " + packageName + " / " + userId);
1071 }
1072 mUninstalledPackages.remove(PackageWithUser.of(userId, packageName));
1073 }
1074
Makoto Onuki82fb2eb2017-03-31 16:58:26 -07001075 protected void disablePackage(int userId, String packageName) {
1076 if (ENABLE_DUMP) {
1077 Log.v(TAG, "Disable package " + packageName + " / " + userId);
1078 }
1079 mDisabledPackages.add(PackageWithUser.of(userId, packageName));
1080 }
1081
1082 protected void enablePackage(int userId, String packageName) {
1083 if (ENABLE_DUMP) {
1084 Log.v(TAG, "Enable package " + packageName + " / " + userId);
1085 }
1086 mDisabledPackages.remove(PackageWithUser.of(userId, packageName));
1087 }
1088
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001089 PackageInfo getInjectedPackageInfo(String packageName, @UserIdInt int userId,
1090 boolean getSignatures) {
1091 final PackageInfo pi = mInjectedPackages.get(packageName);
1092 if (pi == null) return null;
1093
1094 final PackageInfo ret = new PackageInfo();
1095 ret.packageName = pi.packageName;
1096 ret.versionCode = pi.versionCode;
Dianne Hackborn3accca02013-09-20 09:32:11 -07001097 ret.versionCodeMajor = pi.versionCodeMajor;
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001098 ret.lastUpdateTime = pi.lastUpdateTime;
1099
1100 ret.applicationInfo = new ApplicationInfo(pi.applicationInfo);
1101 ret.applicationInfo.uid = UserHandle.getUid(userId, pi.applicationInfo.uid);
1102 ret.applicationInfo.packageName = pi.packageName;
1103
1104 if (mUninstalledPackages.contains(PackageWithUser.of(userId, packageName))) {
1105 ret.applicationInfo.flags &= ~ApplicationInfo.FLAG_INSTALLED;
1106 }
Makoto Onuki66e4a2b2017-01-23 11:37:45 -08001107 if (mEphemeralPackages.contains(PackageWithUser.of(userId, packageName))) {
Todd Kennedybe0b8892017-02-15 14:13:52 -08001108 ret.applicationInfo.privateFlags |= ApplicationInfo.PRIVATE_FLAG_INSTANT;
Makoto Onuki66e4a2b2017-01-23 11:37:45 -08001109 }
Makoto Onuki33663282016-08-22 16:19:04 -07001110 if (mSystemPackages.contains(packageName)) {
1111 ret.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
1112 }
Makoto Onuki82fb2eb2017-03-31 16:58:26 -07001113 ret.applicationInfo.enabled =
1114 !mDisabledPackages.contains(PackageWithUser.of(userId, packageName));
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001115
1116 if (getSignatures) {
1117 ret.signatures = pi.signatures;
1118 }
1119
1120 return ret;
1121 }
1122
1123 protected void addApplicationInfo(PackageInfo pi, List<ApplicationInfo> list) {
1124 if (pi != null && pi.applicationInfo != null) {
1125 list.add(pi.applicationInfo);
1126 }
1127 }
1128
1129 protected List<ApplicationInfo> getInstalledApplications(int userId) {
1130 final ArrayList<ApplicationInfo> ret = new ArrayList<>();
1131
1132 addApplicationInfo(getInjectedPackageInfo(CALLING_PACKAGE_1, userId, false), ret);
1133 addApplicationInfo(getInjectedPackageInfo(CALLING_PACKAGE_2, userId, false), ret);
1134 addApplicationInfo(getInjectedPackageInfo(CALLING_PACKAGE_3, userId, false), ret);
1135 addApplicationInfo(getInjectedPackageInfo(CALLING_PACKAGE_4, userId, false), ret);
1136 addApplicationInfo(getInjectedPackageInfo(LAUNCHER_1, userId, false), ret);
1137 addApplicationInfo(getInjectedPackageInfo(LAUNCHER_2, userId, false), ret);
1138 addApplicationInfo(getInjectedPackageInfo(LAUNCHER_3, userId, false), ret);
1139 addApplicationInfo(getInjectedPackageInfo(LAUNCHER_4, userId, false), ret);
1140
1141 return ret;
1142 }
1143
Makoto Onuki6dd9fb72016-06-01 13:55:54 -07001144 private void addPackageInfo(PackageInfo pi, List<PackageInfo> list) {
1145 if (pi != null) {
1146 list.add(pi);
1147 }
1148 }
1149
Makoto Onukiee6b6e42016-06-29 17:34:02 -07001150 private List<PackageInfo> getInstalledPackagesWithUninstalled(int userId) {
Makoto Onuki6dd9fb72016-06-01 13:55:54 -07001151 final ArrayList<PackageInfo> ret = new ArrayList<>();
1152
1153 addPackageInfo(getInjectedPackageInfo(CALLING_PACKAGE_1, userId, false), ret);
1154 addPackageInfo(getInjectedPackageInfo(CALLING_PACKAGE_2, userId, false), ret);
1155 addPackageInfo(getInjectedPackageInfo(CALLING_PACKAGE_3, userId, false), ret);
1156 addPackageInfo(getInjectedPackageInfo(CALLING_PACKAGE_4, userId, false), ret);
1157 addPackageInfo(getInjectedPackageInfo(LAUNCHER_1, userId, false), ret);
1158 addPackageInfo(getInjectedPackageInfo(LAUNCHER_2, userId, false), ret);
1159 addPackageInfo(getInjectedPackageInfo(LAUNCHER_3, userId, false), ret);
1160 addPackageInfo(getInjectedPackageInfo(LAUNCHER_4, userId, false), ret);
1161
1162 return ret;
1163 }
1164
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001165 protected void addManifestShortcutResource(ComponentName activity, int resId) {
1166 final String packageName = activity.getPackageName();
1167 LinkedHashMap<ComponentName, Integer> map = mActivityMetadataResId.get(packageName);
1168 if (map == null) {
1169 map = new LinkedHashMap<>();
1170 mActivityMetadataResId.put(packageName, map);
1171 }
1172 map.put(activity, resId);
1173 }
1174
1175 protected PackageInfo injectGetActivitiesWithMetadata(String packageName, @UserIdInt int userId) {
1176 final PackageInfo ret = getInjectedPackageInfo(packageName, userId,
1177 /* getSignatures=*/ false);
1178
1179 final HashMap<ComponentName, Integer> activities = mActivityMetadataResId.get(packageName);
1180 if (activities != null) {
1181 final ArrayList<ActivityInfo> list = new ArrayList<>();
1182
1183 for (ComponentName cn : activities.keySet()) {
1184 ActivityInfo ai = new ActivityInfo();
1185 ai.packageName = cn.getPackageName();
1186 ai.name = cn.getClassName();
1187 ai.metaData = new Bundle();
1188 ai.metaData.putInt(ShortcutParser.METADATA_KEY, activities.get(cn));
Makoto Onukiee6b6e42016-06-29 17:34:02 -07001189 ai.applicationInfo = ret.applicationInfo;
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001190 list.add(ai);
1191 }
1192 ret.activities = list.toArray(new ActivityInfo[list.size()]);
1193 }
1194 return ret;
1195 }
1196
1197 protected XmlResourceParser injectXmlMetaData(ActivityInfo activityInfo, String key) {
1198 if (!ShortcutParser.METADATA_KEY.equals(key) || activityInfo.metaData == null) {
1199 return null;
1200 }
1201 final int resId = activityInfo.metaData.getInt(key);
1202 return getTestContext().getResources().getXml(resId);
1203 }
1204
1205 /** Replace the current calling package */
1206 protected void setCaller(String packageName, int userId) {
1207 mInjectedClientPackage = packageName;
1208 mInjectedCallingUid =
1209 Preconditions.checkNotNull(getInjectedPackageInfo(packageName, userId, false),
1210 "Unknown package").applicationInfo.uid;
1211
1212 // Set up LauncherApps for this caller.
1213 final Pair<Integer, String> key = Pair.create(userId, packageName);
1214 if (!mLauncherAppsMap.containsKey(key)) {
1215 mLauncherAppsMap.put(key, new LauncherAppsTestable(mClientContext, mLauncherAppImpl));
1216 }
1217 mLauncherApps = mLauncherAppsMap.get(key);
1218 }
1219
1220 protected void setCaller(String packageName) {
1221 setCaller(packageName, UserHandle.USER_SYSTEM);
1222 }
1223
1224 protected String getCallingPackage() {
1225 return mInjectedClientPackage;
1226 }
1227
Makoto Onuki2d895c32016-12-02 15:48:40 -08001228 /**
Makoto Onuki7d0fa812018-02-21 11:24:43 -08001229 * This controls {@link ShortcutService#hasShortcutHostPermission}, but
Makoto Onuki2d895c32016-12-02 15:48:40 -08001230 * not {@link ShortcutService#getDefaultLauncher(int)}. To control the later, use
1231 * {@link #setDefaultLauncher(int, ComponentName)}.
1232 */
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001233 protected void setDefaultLauncherChecker(BiPredicate<String, Integer> p) {
1234 mDefaultLauncherChecker = p;
1235 }
1236
Makoto Onuki2d895c32016-12-02 15:48:40 -08001237 /**
1238 * Set the default launcher. This will update {@link #mDefaultLauncherChecker} set by
1239 * {@link #setDefaultLauncherChecker} too.
1240 */
1241 protected void setDefaultLauncher(int userId, ComponentName launcherActivity) {
1242 mDefaultLauncher.put(userId, launcherActivity);
1243
1244 final BiPredicate<String, Integer> oldChecker = mDefaultLauncherChecker;
1245 mDefaultLauncherChecker = (checkPackageName, checkUserId) -> {
1246 if ((checkUserId == userId) && (launcherActivity != null)) {
1247 return launcherActivity.getPackageName().equals(checkPackageName);
1248 }
1249 return oldChecker.test(checkPackageName, checkUserId);
1250 };
1251 }
1252
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001253 protected void runWithCaller(String packageName, int userId, Runnable r) {
1254 final String previousPackage = mInjectedClientPackage;
1255 final int previousUserId = UserHandle.getUserId(mInjectedCallingUid);
1256
1257 setCaller(packageName, userId);
1258
1259 r.run();
1260
1261 setCaller(previousPackage, previousUserId);
1262 }
1263
Makoto Onuki157b1622016-06-02 16:13:10 -07001264 protected void runWithSystemUid(Runnable r) {
1265 final int origUid = mInjectedCallingUid;
1266 mInjectedCallingUid = Process.SYSTEM_UID;
1267 r.run();
1268 mInjectedCallingUid = origUid;
1269 }
1270
1271 protected void lookupAndFillInResourceNames(ShortcutInfo si) {
1272 runWithSystemUid(() -> si.lookupAndFillInResourceNames(
1273 mService.injectGetResourcesForApplicationAsUser(si.getPackage(), si.getUserId())));
1274 }
1275
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001276 protected int getCallingUserId() {
1277 return UserHandle.getUserId(mInjectedCallingUid);
1278 }
1279
1280 protected UserHandle getCallingUser() {
1281 return UserHandle.of(getCallingUserId());
1282 }
1283
1284 /** For debugging */
1285 protected void dumpsysOnLogcat() {
1286 dumpsysOnLogcat("");
1287 }
1288
1289 protected void dumpsysOnLogcat(String message) {
1290 dumpsysOnLogcat(message, false);
1291 }
1292
1293 protected void dumpsysOnLogcat(String message, boolean force) {
1294 if (force || !ENABLE_DUMP) return;
1295
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001296 Log.v(TAG, "Dumping ShortcutService: " + message);
Makoto Onuki50a320e2017-05-31 14:38:42 -07001297 for (String line : dumpsys("-u").split("\n")) {
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001298 Log.v(TAG, line);
1299 }
1300 }
1301
Makoto Onuki76269922016-07-15 14:58:54 -07001302 protected String dumpCheckin() {
Makoto Onuki50a320e2017-05-31 14:38:42 -07001303 return dumpsys("--checkin");
Makoto Onuki76269922016-07-15 14:58:54 -07001304 }
1305
Makoto Onuki50a320e2017-05-31 14:38:42 -07001306 protected String dumpsys(String... args) {
Makoto Onuki76269922016-07-15 14:58:54 -07001307 final ArrayList<String> origPermissions = new ArrayList<>(mCallerPermissions);
1308 mCallerPermissions.add(android.Manifest.permission.DUMP);
1309 try {
1310 final ByteArrayOutputStream out = new ByteArrayOutputStream();
1311 final PrintWriter pw = new PrintWriter(out);
Makoto Onukic4361e32017-04-03 11:24:25 -07001312 mService.dumpNoCheck(/* fd */ null, pw, args);
Makoto Onuki76269922016-07-15 14:58:54 -07001313 pw.close();
1314
1315 return out.toString();
1316 } finally {
1317 mCallerPermissions.clear();
1318 mCallerPermissions.addAll(origPermissions);
1319 }
1320 }
1321
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001322 /**
1323 * For debugging, dump arbitrary file on logcat.
1324 */
1325 protected void dumpFileOnLogcat(String path) {
1326 dumpFileOnLogcat(path, "");
1327 }
1328
1329 protected void dumpFileOnLogcat(String path, String message) {
1330 if (!ENABLE_DUMP) return;
1331
1332 Log.v(TAG, "Dumping file: " + path + " " + message);
1333 final StringBuilder sb = new StringBuilder();
1334 try (BufferedReader br = new BufferedReader(new FileReader(path))) {
1335 String line;
1336 while ((line = br.readLine()) != null) {
1337 Log.v(TAG, line);
1338 }
1339 } catch (Exception e) {
1340 Log.e(TAG, "Couldn't read file", e);
1341 fail("Exception " + e);
1342 }
1343 }
1344
1345 /**
1346 * For debugging, dump the main state file on logcat.
1347 */
1348 protected void dumpBaseStateFile() {
1349 mService.saveDirtyInfo();
1350 dumpFileOnLogcat(mInjectedFilePathRoot.getAbsolutePath()
1351 + "/system/" + ShortcutService.FILENAME_BASE_STATE);
1352 }
1353
1354 /**
1355 * For debugging, dump per-user state file on logcat.
1356 */
1357 protected void dumpUserFile(int userId) {
1358 dumpUserFile(userId, "");
1359 }
1360
1361 protected void dumpUserFile(int userId, String message) {
1362 mService.saveDirtyInfo();
1363 dumpFileOnLogcat(mInjectedFilePathRoot.getAbsolutePath()
1364 + "/user-" + userId
1365 + "/" + ShortcutService.FILENAME_USER_PACKAGES, message);
1366 }
1367
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001368 /**
Makoto Onukia01f4f02016-12-15 15:58:41 -08001369 * Make a shortcut with an ID only.
1370 */
1371 protected ShortcutInfo makeShortcutIdOnly(String id) {
1372 return new ShortcutInfo.Builder(mClientContext, id).build();
1373 }
1374
1375 /**
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001376 * Make a shortcut with an ID.
1377 */
1378 protected ShortcutInfo makeShortcut(String id) {
1379 return makeShortcut(
1380 id, "Title-" + id, /* activity =*/ null, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001381 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001382 }
1383
Makoto Onukia01f4f02016-12-15 15:58:41 -08001384 @Deprecated // Title was renamed to short label.
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001385 protected ShortcutInfo makeShortcutWithTitle(String id, String title) {
1386 return makeShortcut(
1387 id, title, /* activity =*/ null, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001388 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001389 }
1390
Makoto Onukia01f4f02016-12-15 15:58:41 -08001391 protected ShortcutInfo makeShortcutWithShortLabel(String id, String shortLabel) {
1392 return makeShortcut(
1393 id, shortLabel, /* activity =*/ null, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001394 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onukia01f4f02016-12-15 15:58:41 -08001395 }
1396
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001397 /**
1398 * Make a shortcut with an ID and timestamp.
1399 */
1400 protected ShortcutInfo makeShortcutWithTimestamp(String id, long timestamp) {
1401 final ShortcutInfo s = makeShortcut(
1402 id, "Title-" + id, /* activity =*/ null, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001403 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001404 s.setTimestamp(timestamp);
1405 return s;
1406 }
1407
1408 /**
1409 * Make a shortcut with an ID, a timestamp and an activity component
1410 */
1411 protected ShortcutInfo makeShortcutWithTimestampWithActivity(String id, long timestamp,
1412 ComponentName activity) {
1413 final ShortcutInfo s = makeShortcut(
1414 id, "Title-" + id, activity, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001415 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001416 s.setTimestamp(timestamp);
1417 return s;
1418 }
1419
1420 /**
1421 * Make a shortcut with an ID and icon.
1422 */
1423 protected ShortcutInfo makeShortcutWithIcon(String id, Icon icon) {
1424 return makeShortcut(
1425 id, "Title-" + id, /* activity =*/ null, icon,
Makoto Onuki99302b52017-03-29 12:42:26 -07001426 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001427 }
1428
1429 protected ShortcutInfo makePackageShortcut(String packageName, String id) {
1430 String origCaller = getCallingPackage();
1431
1432 setCaller(packageName);
1433 ShortcutInfo s = makeShortcut(
1434 id, "Title-" + id, /* activity =*/ null, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001435 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001436 setCaller(origCaller); // restore the caller
1437
1438 return s;
1439 }
1440
1441 /**
1442 * Make multiple shortcuts with IDs.
1443 */
1444 protected List<ShortcutInfo> makeShortcuts(String... ids) {
1445 final ArrayList<ShortcutInfo> ret = new ArrayList();
1446 for (String id : ids) {
1447 ret.add(makeShortcut(id));
1448 }
1449 return ret;
1450 }
1451
1452 protected ShortcutInfo.Builder makeShortcutBuilder() {
1453 return new ShortcutInfo.Builder(mClientContext);
1454 }
1455
1456 protected ShortcutInfo makeShortcutWithActivity(String id, ComponentName activity) {
1457 return makeShortcut(
1458 id, "Title-" + id, activity, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001459 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001460 }
1461
Makoto Onukib5a012f2016-06-21 11:13:53 -07001462 protected ShortcutInfo makeShortcutWithIntent(String id, Intent intent) {
1463 return makeShortcut(
1464 id, "Title-" + id, /* activity =*/ null, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001465 intent, /* rank =*/ 0);
Makoto Onukib5a012f2016-06-21 11:13:53 -07001466 }
1467
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001468 protected ShortcutInfo makeShortcutWithActivityAndTitle(String id, ComponentName activity,
1469 String title) {
1470 return makeShortcut(
1471 id, title, activity, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001472 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001473 }
1474
1475 protected ShortcutInfo makeShortcutWithActivityAndRank(String id, ComponentName activity,
1476 int rank) {
1477 return makeShortcut(
1478 id, "Title-" + id, activity, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001479 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), rank);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001480 }
1481
1482 /**
1483 * Make a shortcut with details.
1484 */
1485 protected ShortcutInfo makeShortcut(String id, String title, ComponentName activity,
Makoto Onuki99302b52017-03-29 12:42:26 -07001486 Icon icon, Intent intent, int rank) {
Makoto Onukib5a012f2016-06-21 11:13:53 -07001487 final ShortcutInfo.Builder b = new ShortcutInfo.Builder(mClientContext, id)
Makoto Onuki255461f2017-01-10 11:47:25 -08001488 .setActivity(new ComponentName(mClientContext.getPackageName(), "main"))
Makoto Onukib5a012f2016-06-21 11:13:53 -07001489 .setShortLabel(title)
Makoto Onuki99302b52017-03-29 12:42:26 -07001490 .setRank(rank)
1491 .setIntent(intent);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001492 if (icon != null) {
1493 b.setIcon(icon);
1494 }
1495 if (activity != null) {
1496 b.setActivity(activity);
1497 }
1498 final ShortcutInfo s = b.build();
1499
1500 s.setTimestamp(mInjectedCurrentTimeMillis); // HACK
1501
1502 return s;
1503 }
1504
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001505 protected ShortcutInfo makeShortcutWithIntents(String id, Intent... intents) {
1506 return makeShortcut(
1507 id, "Title-" + id, /* activity =*/ null, /* icon =*/ null,
1508 intents, /* rank =*/ 0);
1509 }
1510
1511 /**
1512 * Make a shortcut with details.
1513 */
1514 protected ShortcutInfo makeShortcut(String id, String title, ComponentName activity,
1515 Icon icon, Intent[] intents, int rank) {
1516 final ShortcutInfo.Builder b = new ShortcutInfo.Builder(mClientContext, id)
Makoto Onuki255461f2017-01-10 11:47:25 -08001517 .setActivity(new ComponentName(mClientContext.getPackageName(), "main"))
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001518 .setShortLabel(title)
1519 .setRank(rank)
1520 .setIntents(intents);
1521 if (icon != null) {
1522 b.setIcon(icon);
1523 }
1524 if (activity != null) {
1525 b.setActivity(activity);
1526 }
1527 final ShortcutInfo s = b.build();
1528
1529 s.setTimestamp(mInjectedCurrentTimeMillis); // HACK
1530
1531 return s;
1532 }
1533
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001534 /**
Makoto Onukib5a012f2016-06-21 11:13:53 -07001535 * Make a shortcut with details.
1536 */
1537 protected ShortcutInfo makeShortcutWithExtras(String id, Intent intent,
1538 PersistableBundle extras) {
1539 final ShortcutInfo.Builder b = new ShortcutInfo.Builder(mClientContext, id)
Makoto Onuki255461f2017-01-10 11:47:25 -08001540 .setActivity(new ComponentName(mClientContext.getPackageName(), "main"))
Makoto Onukib5a012f2016-06-21 11:13:53 -07001541 .setShortLabel("title-" + id)
1542 .setExtras(extras)
1543 .setIntent(intent);
1544 final ShortcutInfo s = b.build();
1545
1546 s.setTimestamp(mInjectedCurrentTimeMillis); // HACK
1547
1548 return s;
1549 }
1550
1551 /**
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001552 * Make an intent.
1553 */
1554 protected Intent makeIntent(String action, Class<?> clazz, Object... bundleKeysAndValues) {
1555 final Intent intent = new Intent(action);
1556 intent.setComponent(makeComponent(clazz));
1557 intent.replaceExtras(makeBundle(bundleKeysAndValues));
1558 return intent;
1559 }
1560
1561 /**
1562 * Make an component name, with the client context.
1563 */
1564 @NonNull
1565 protected ComponentName makeComponent(Class<?> clazz) {
1566 return new ComponentName(mClientContext, clazz);
1567 }
1568
1569 @NonNull
1570 protected ShortcutInfo findById(List<ShortcutInfo> list, String id) {
1571 for (ShortcutInfo s : list) {
1572 if (s.getId().equals(id)) {
1573 return s;
1574 }
1575 }
1576 fail("Shortcut with id " + id + " not found");
1577 return null;
1578 }
1579
1580 protected void assertSystem() {
1581 assertEquals("Caller must be system", Process.SYSTEM_UID, mInjectedCallingUid);
1582 }
1583
1584 protected void assertResetTimes(long expectedLastResetTime, long expectedNextResetTime) {
1585 assertEquals(expectedLastResetTime, mService.getLastResetTimeLocked());
1586 assertEquals(expectedNextResetTime, mService.getNextResetTimeLocked());
1587 }
1588
1589 public static List<ShortcutInfo> assertAllNotHaveIcon(
1590 List<ShortcutInfo> actualShortcuts) {
1591 for (ShortcutInfo s : actualShortcuts) {
1592 assertNull("ID " + s.getId(), s.getIcon());
1593 }
1594 return actualShortcuts;
1595 }
1596
1597 @NonNull
1598 protected List<ShortcutInfo> assertAllHaveFlags(@NonNull List<ShortcutInfo> actualShortcuts,
1599 int shortcutFlags) {
1600 for (ShortcutInfo s : actualShortcuts) {
1601 assertTrue("ID " + s.getId() + " doesn't have flags " + shortcutFlags,
1602 s.hasFlags(shortcutFlags));
1603 }
1604 return actualShortcuts;
1605 }
1606
1607 protected ShortcutInfo getPackageShortcut(String packageName, String shortcutId, int userId) {
1608 return mService.getPackageShortcutForTest(packageName, shortcutId, userId);
1609 }
1610
1611 protected void assertShortcutExists(String packageName, String shortcutId, int userId) {
1612 assertTrue(getPackageShortcut(packageName, shortcutId, userId) != null);
1613 }
1614
1615 protected void assertShortcutNotExists(String packageName, String shortcutId, int userId) {
1616 assertTrue(getPackageShortcut(packageName, shortcutId, userId) == null);
1617 }
1618
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001619 protected Intent[] launchShortcutAndGetIntentsInner(Runnable shortcutStarter,
1620 @NonNull String packageName, @NonNull String shortcutId, int userId) {
1621 reset(mMockActivityManagerInternal);
1622 shortcutStarter.run();
1623
1624 final ArgumentCaptor<Intent[]> intentsCaptor = ArgumentCaptor.forClass(Intent[].class);
1625 verify(mMockActivityManagerInternal).startActivitiesAsPackage(
1626 eq(packageName),
1627 eq(userId),
1628 intentsCaptor.capture(),
Makoto Onukid0010c52017-03-30 14:17:35 -07001629 anyOrNull(Bundle.class));
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001630 return intentsCaptor.getValue();
1631 }
1632
1633 protected Intent[] launchShortcutAndGetIntents(
1634 @NonNull String packageName, @NonNull String shortcutId, int userId) {
1635 return launchShortcutAndGetIntentsInner(
1636 () -> {
1637 mLauncherApps.startShortcut(packageName, shortcutId, null, null,
1638 UserHandle.of(userId));
1639 }, packageName, shortcutId, userId
1640 );
1641 }
1642
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001643 protected Intent launchShortcutAndGetIntent(
1644 @NonNull String packageName, @NonNull String shortcutId, int userId) {
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001645 final Intent[] intents = launchShortcutAndGetIntents(packageName, shortcutId, userId);
1646 assertEquals(1, intents.length);
1647 return intents[0];
1648 }
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001649
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001650 protected Intent[] launchShortcutAndGetIntents_withShortcutInfo(
1651 @NonNull String packageName, @NonNull String shortcutId, int userId) {
1652 return launchShortcutAndGetIntentsInner(
1653 () -> {
1654 mLauncherApps.startShortcut(
1655 getShortcutInfoAsLauncher(packageName, shortcutId, userId), null, null);
1656 }, packageName, shortcutId, userId
1657 );
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001658 }
1659
1660 protected Intent launchShortcutAndGetIntent_withShortcutInfo(
1661 @NonNull String packageName, @NonNull String shortcutId, int userId) {
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001662 final Intent[] intents = launchShortcutAndGetIntents_withShortcutInfo(
1663 packageName, shortcutId, userId);
1664 assertEquals(1, intents.length);
1665 return intents[0];
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001666 }
1667
1668 protected void assertShortcutLaunchable(@NonNull String packageName, @NonNull String shortcutId,
1669 int userId) {
1670 assertNotNull(launchShortcutAndGetIntent(packageName, shortcutId, userId));
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001671 }
1672
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001673 protected void assertShortcutNotLaunched(@NonNull String packageName,
1674 @NonNull String shortcutId, int userId) {
1675 reset(mMockActivityManagerInternal);
1676 try {
1677 mLauncherApps.startShortcut(packageName, shortcutId, null, null,
1678 UserHandle.of(userId));
1679 fail("ActivityNotFoundException was not thrown");
1680 } catch (ActivityNotFoundException expected) {
1681 }
1682 // This shouldn't have been called.
1683 verify(mMockActivityManagerInternal, times(0)).startActivitiesAsPackage(
1684 anyString(),
1685 anyInt(),
1686 any(Intent[].class),
Makoto Onukid0010c52017-03-30 14:17:35 -07001687 anyOrNull(Bundle.class));
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001688 }
1689
1690 protected void assertStartShortcutThrowsException(@NonNull String packageName,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07001691 @NonNull String shortcutId, int userId, Class<?> expectedException) {
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07001692 Exception thrown = null;
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001693 try {
Makoto Onukid6880792016-06-29 13:37:43 -07001694 mLauncherApps.startShortcut(packageName, shortcutId, null, null,
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001695 UserHandle.of(userId));
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07001696 } catch (Exception e) {
1697 thrown = e;
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001698 }
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07001699 assertNotNull("Exception was not thrown", thrown);
1700 assertEquals("Exception type different", expectedException, thrown.getClass());
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001701 }
1702
1703 protected void assertBitmapDirectories(int userId, String... expectedDirectories) {
1704 final Set<String> expected = hashSet(set(expectedDirectories));
1705
1706 final Set<String> actual = new HashSet<>();
1707
1708 final File[] files = mService.getUserBitmapFilePath(userId).listFiles();
1709 if (files != null) {
1710 for (File child : files) {
1711 if (child.isDirectory()) {
1712 actual.add(child.getName());
1713 }
1714 }
1715 }
1716
1717 assertEquals(expected, actual);
1718 }
1719
1720 protected void assertBitmapFiles(int userId, String packageName, String... expectedFiles) {
1721 final Set<String> expected = hashSet(set(expectedFiles));
1722
1723 final Set<String> actual = new HashSet<>();
1724
1725 final File[] files = new File(mService.getUserBitmapFilePath(userId), packageName)
1726 .listFiles();
1727 if (files != null) {
1728 for (File child : files) {
1729 if (child.isFile()) {
1730 actual.add(child.getName());
1731 }
1732 }
1733 }
1734
1735 assertEquals(expected, actual);
1736 }
1737
1738 protected String getBitmapFilename(int userId, String packageName, String shortcutId) {
1739 final ShortcutInfo si = mService.getPackageShortcutForTest(packageName, shortcutId, userId);
1740 if (si == null) {
1741 return null;
1742 }
Makoto Onuki475c3652017-05-08 14:29:03 -07001743 mService.waitForBitmapSavesForTest();
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001744 return new File(si.getBitmapPath()).getName();
1745 }
1746
Makoto Onuki475c3652017-05-08 14:29:03 -07001747 protected String getBitmapAbsPath(int userId, String packageName, String shortcutId) {
1748 final ShortcutInfo si = mService.getPackageShortcutForTest(packageName, shortcutId, userId);
1749 if (si == null) {
1750 return null;
1751 }
1752 mService.waitForBitmapSavesForTest();
1753 return new File(si.getBitmapPath()).getAbsolutePath();
1754 }
1755
Makoto Onukif3ba2e02016-07-12 09:18:50 -07001756 /**
1757 * @return all shortcuts stored internally for the caller. This reflects the *internal* view
1758 * of shortcuts, which may be different from what {@link #getCallerVisibleShortcuts} would
1759 * return, because getCallerVisibleShortcuts() will get shortcuts from the proper "front door"
1760 * which performs some extra checks, like {@link ShortcutPackage#onRestored}.
1761 */
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001762 protected List<ShortcutInfo> getCallerShortcuts() {
1763 final ShortcutPackage p = mService.getPackageShortcutForTest(
1764 getCallingPackage(), getCallingUserId());
1765 return p == null ? null : p.getAllShortcutsForTest();
1766 }
1767
Makoto Onukif3ba2e02016-07-12 09:18:50 -07001768 /**
1769 * @return all shortcuts owned by caller that are actually visible via ShortcutManager.
1770 * See also {@link #getCallerShortcuts}.
1771 */
1772 protected List<ShortcutInfo> getCallerVisibleShortcuts() {
1773 final ArrayList<ShortcutInfo> ret = new ArrayList<>();
1774 ret.addAll(mManager.getDynamicShortcuts());
1775 ret.addAll(mManager.getPinnedShortcuts());
1776 ret.addAll(mManager.getManifestShortcuts());
1777 return ret;
1778 }
1779
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001780 protected ShortcutInfo getCallerShortcut(String shortcutId) {
1781 return getPackageShortcut(getCallingPackage(), shortcutId, getCallingUserId());
1782 }
1783
1784 protected List<ShortcutInfo> getLauncherShortcuts(String launcher, int userId, int queryFlags) {
1785 final List<ShortcutInfo>[] ret = new List[1];
1786 runWithCaller(launcher, userId, () -> {
1787 final ShortcutQuery q = new ShortcutQuery();
1788 q.setQueryFlags(queryFlags);
1789 ret[0] = mLauncherApps.getShortcuts(q, UserHandle.of(userId));
1790 });
1791 return ret[0];
1792 }
1793
1794 protected List<ShortcutInfo> getLauncherPinnedShortcuts(String launcher, int userId) {
1795 return getLauncherShortcuts(launcher, userId, ShortcutQuery.FLAG_GET_PINNED);
1796 }
1797
Makoto Onukia01f4f02016-12-15 15:58:41 -08001798 protected List<ShortcutInfo> getShortcutAsLauncher(int targetUserId) {
1799 final ShortcutQuery q = new ShortcutQuery();
1800 q.setQueryFlags(ShortcutQuery.FLAG_MATCH_DYNAMIC | ShortcutQuery.FLAG_MATCH_DYNAMIC
1801 | ShortcutQuery.FLAG_MATCH_PINNED);
1802 return mLauncherApps.getShortcuts(q, UserHandle.of(targetUserId));
1803 }
1804
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001805 protected ShortcutInfo getShortcutInfoAsLauncher(String packageName, String shortcutId,
1806 int userId) {
1807 final List<ShortcutInfo> infoList =
1808 mLauncherApps.getShortcutInfo(packageName, list(shortcutId),
1809 UserHandle.of(userId));
1810 assertEquals("No shortcutInfo found (or too many of them)", 1, infoList.size());
1811 return infoList.get(0);
1812 }
1813
Makoto Onukiee6b6e42016-06-29 17:34:02 -07001814 protected Intent genPackageAddIntent(String packageName, int userId) {
1815 installPackage(userId, packageName);
1816
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001817 Intent i = new Intent(Intent.ACTION_PACKAGE_ADDED);
Makoto Onukiee6b6e42016-06-29 17:34:02 -07001818 i.setData(Uri.parse("package:" + packageName));
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001819 i.putExtra(Intent.EXTRA_USER_HANDLE, userId);
1820 return i;
1821 }
1822
1823 protected Intent genPackageDeleteIntent(String pakcageName, int userId) {
Makoto Onukiee6b6e42016-06-29 17:34:02 -07001824 uninstallPackage(userId, pakcageName);
1825
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001826 Intent i = new Intent(Intent.ACTION_PACKAGE_REMOVED);
1827 i.setData(Uri.parse("package:" + pakcageName));
1828 i.putExtra(Intent.EXTRA_USER_HANDLE, userId);
1829 return i;
1830 }
1831
1832 protected Intent genPackageUpdateIntent(String pakcageName, int userId) {
Makoto Onukiee6b6e42016-06-29 17:34:02 -07001833 installPackage(userId, pakcageName);
1834
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001835 Intent i = new Intent(Intent.ACTION_PACKAGE_ADDED);
1836 i.setData(Uri.parse("package:" + pakcageName));
1837 i.putExtra(Intent.EXTRA_USER_HANDLE, userId);
1838 i.putExtra(Intent.EXTRA_REPLACING, true);
1839 return i;
1840 }
1841
Makoto Onukib08790c2016-06-23 14:05:46 -07001842 protected Intent genPackageChangedIntent(String pakcageName, int userId) {
1843 Intent i = new Intent(Intent.ACTION_PACKAGE_CHANGED);
1844 i.setData(Uri.parse("package:" + pakcageName));
1845 i.putExtra(Intent.EXTRA_USER_HANDLE, userId);
1846 return i;
1847 }
1848
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001849 protected Intent genPackageDataClear(String packageName, int userId) {
1850 Intent i = new Intent(Intent.ACTION_PACKAGE_DATA_CLEARED);
1851 i.setData(Uri.parse("package:" + packageName));
1852 i.putExtra(Intent.EXTRA_USER_HANDLE, userId);
1853 return i;
1854 }
1855
1856 protected void assertExistsAndShadow(ShortcutPackageItem spi) {
1857 assertNotNull(spi);
1858 assertTrue(spi.getPackageInfo().isShadow());
1859 }
1860
1861 protected File makeFile(File baseDirectory, String... paths) {
1862 File ret = baseDirectory;
1863
1864 for (String path : paths) {
1865 ret = new File(ret, path);
1866 }
1867
1868 return ret;
1869 }
1870
1871 protected boolean bitmapDirectoryExists(String packageName, int userId) {
Makoto Onuki475c3652017-05-08 14:29:03 -07001872 mService.waitForBitmapSavesForTest();
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001873 final File path = new File(mService.getUserBitmapFilePath(userId), packageName);
1874 return path.isDirectory();
1875 }
1876 protected static ShortcutQuery buildQuery(long changedSince,
1877 String packageName, ComponentName componentName,
1878 /* @ShortcutQuery.QueryFlags */ int flags) {
1879 return buildQuery(changedSince, packageName, null, componentName, flags);
1880 }
1881
1882 protected static ShortcutQuery buildQuery(long changedSince,
1883 String packageName, List<String> shortcutIds, ComponentName componentName,
1884 /* @ShortcutQuery.QueryFlags */ int flags) {
1885 final ShortcutQuery q = new ShortcutQuery();
1886 q.setChangedSince(changedSince);
1887 q.setPackage(packageName);
1888 q.setShortcutIds(shortcutIds);
1889 q.setActivity(componentName);
1890 q.setQueryFlags(flags);
1891 return q;
1892 }
1893
1894 protected static ShortcutQuery buildAllQuery(String packageName) {
1895 final ShortcutQuery q = new ShortcutQuery();
1896 q.setPackage(packageName);
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07001897 q.setQueryFlags(ShortcutQuery.FLAG_GET_ALL_KINDS);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001898 return q;
1899 }
1900
1901 protected static ShortcutQuery buildPinnedQuery(String packageName) {
1902 final ShortcutQuery q = new ShortcutQuery();
1903 q.setPackage(packageName);
1904 q.setQueryFlags(ShortcutQuery.FLAG_GET_PINNED);
1905 return q;
1906 }
1907
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07001908 protected static ShortcutQuery buildQueryWithFlags(int queryFlags) {
1909 final ShortcutQuery q = new ShortcutQuery();
1910 q.setQueryFlags(queryFlags);
1911 return q;
1912 }
1913
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001914 protected void backupAndRestore() {
1915 int prevUid = mInjectedCallingUid;
1916
1917 mInjectedCallingUid = Process.SYSTEM_UID; // Only system can call it.
1918
1919 dumpsysOnLogcat("Before backup");
1920
1921 final byte[] payload = mService.getBackupPayload(USER_0);
1922 if (ENABLE_DUMP) {
1923 final String xml = new String(payload);
1924 Log.v(TAG, "Backup payload:");
1925 for (String line : xml.split("\n")) {
1926 Log.v(TAG, line);
1927 }
1928 }
1929
1930 // Before doing anything else, uninstall all packages.
1931 for (int userId : list(USER_0, USER_P0)) {
1932 for (String pkg : list(CALLING_PACKAGE_1, CALLING_PACKAGE_2, CALLING_PACKAGE_3,
1933 LAUNCHER_1, LAUNCHER_2, LAUNCHER_3)) {
1934 uninstallPackage(userId, pkg);
1935 }
1936 }
1937
1938 shutdownServices();
1939
1940 deleteAllSavedFiles();
1941
1942 initService();
1943 mService.applyRestore(payload, USER_0);
1944
1945 // handleUnlockUser will perform the gone package check, but it shouldn't remove
1946 // shadow information.
1947 mService.handleUnlockUser(USER_0);
1948
1949 dumpsysOnLogcat("After restore");
1950
1951 mInjectedCallingUid = prevUid;
1952 }
1953
1954 protected void prepareCrossProfileDataSet() {
Makoto Onuki9c850012016-07-26 15:50:50 -07001955 mRunningUsers.put(USER_10, true); // this test needs user 10.
1956
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001957 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1958 assertTrue(mManager.setDynamicShortcuts(list(
1959 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"),
1960 makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6"))));
1961 });
1962 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
1963 assertTrue(mManager.setDynamicShortcuts(list(
1964 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"),
1965 makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6"))));
1966 });
1967 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
1968 assertTrue(mManager.setDynamicShortcuts(list(
1969 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"),
1970 makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6"))));
1971 });
1972 runWithCaller(CALLING_PACKAGE_4, USER_0, () -> {
1973 assertTrue(mManager.setDynamicShortcuts(list()));
1974 });
1975 runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
1976 assertTrue(mManager.setDynamicShortcuts(list(
1977 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"),
1978 makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6"))));
1979 });
1980 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
1981 assertTrue(mManager.setDynamicShortcuts(list(
1982 makeShortcut("x1"), makeShortcut("x2"), makeShortcut("x3"),
1983 makeShortcut("x4"), makeShortcut("x5"), makeShortcut("x6"))));
1984 });
1985
1986 runWithCaller(LAUNCHER_1, USER_0, () -> {
1987 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s1"), HANDLE_USER_0);
1988 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s1", "s2"), HANDLE_USER_0);
1989 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("s1", "s2", "s3"), HANDLE_USER_0);
1990
1991 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s1", "s4"), HANDLE_USER_P0);
1992 });
1993 runWithCaller(LAUNCHER_2, USER_0, () -> {
1994 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s2"), HANDLE_USER_0);
1995 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s2", "s3"), HANDLE_USER_0);
1996 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("s2", "s3", "s4"), HANDLE_USER_0);
1997
1998 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s2", "s5"), HANDLE_USER_P0);
1999 });
Makoto Onukif3ba2e02016-07-12 09:18:50 -07002000
2001 // Note LAUNCHER_3 has allowBackup=false.
Makoto Onuki51ab2b32016-06-02 11:03:51 -07002002 runWithCaller(LAUNCHER_3, USER_0, () -> {
2003 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s3"), HANDLE_USER_0);
2004 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s3", "s4"), HANDLE_USER_0);
2005 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("s3", "s4", "s5"), HANDLE_USER_0);
2006
2007 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s3", "s6"), HANDLE_USER_P0);
2008 });
2009 runWithCaller(LAUNCHER_4, USER_0, () -> {
2010 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list(), HANDLE_USER_0);
2011 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list(), HANDLE_USER_0);
2012 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list(), HANDLE_USER_0);
2013 mLauncherApps.pinShortcuts(CALLING_PACKAGE_4, list(), HANDLE_USER_0);
2014 });
2015
2016 // Launcher on a managed profile is referring ot user 0!
2017 runWithCaller(LAUNCHER_1, USER_P0, () -> {
2018 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s3", "s4"), HANDLE_USER_0);
2019 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s3", "s4", "s5"), HANDLE_USER_0);
2020 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("s3", "s4", "s5", "s6"),
2021 HANDLE_USER_0);
2022
2023 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s4", "s1"), HANDLE_USER_P0);
2024 });
2025 runWithCaller(LAUNCHER_1, USER_10, () -> {
2026 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("x4", "x5"), HANDLE_USER_10);
2027 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("x4", "x5", "x6"), HANDLE_USER_10);
2028 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("x4", "x5", "x6", "x1"),
2029 HANDLE_USER_10);
2030 });
2031
2032 // Then remove some dynamic shortcuts.
2033 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2034 assertTrue(mManager.setDynamicShortcuts(list(
2035 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2036 });
2037 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
2038 assertTrue(mManager.setDynamicShortcuts(list(
2039 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2040 });
2041 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
2042 assertTrue(mManager.setDynamicShortcuts(list(
2043 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2044 });
2045 runWithCaller(CALLING_PACKAGE_4, USER_0, () -> {
2046 assertTrue(mManager.setDynamicShortcuts(list()));
2047 });
2048 runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
2049 assertTrue(mManager.setDynamicShortcuts(list(
2050 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2051 });
2052 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
2053 assertTrue(mManager.setDynamicShortcuts(list(
2054 makeShortcut("x1"), makeShortcut("x2"), makeShortcut("x3"))));
2055 });
2056 }
Makoto Onukib5a012f2016-06-21 11:13:53 -07002057
2058 public static List<ShortcutInfo> assertAllHaveIconResId(
2059 List<ShortcutInfo> actualShortcuts) {
2060 for (ShortcutInfo s : actualShortcuts) {
2061 assertTrue("ID " + s.getId() + " not have icon res ID", s.hasIconResource());
2062 assertFalse("ID " + s.getId() + " shouldn't have icon FD", s.hasIconFile());
2063 }
2064 return actualShortcuts;
2065 }
2066
2067 public static List<ShortcutInfo> assertAllHaveIconFile(
2068 List<ShortcutInfo> actualShortcuts) {
2069 for (ShortcutInfo s : actualShortcuts) {
2070 assertFalse("ID " + s.getId() + " shouldn't have icon res ID", s.hasIconResource());
2071 assertTrue("ID " + s.getId() + " not have icon FD", s.hasIconFile());
2072 }
2073 return actualShortcuts;
2074 }
2075
2076 public static List<ShortcutInfo> assertAllHaveIcon(
2077 List<ShortcutInfo> actualShortcuts) {
2078 for (ShortcutInfo s : actualShortcuts) {
Makoto Onukia01f4f02016-12-15 15:58:41 -08002079 assertTrue("ID " + s.getId() + " has no icon ",
2080 s.hasIconFile() || s.hasIconResource() || s.getIcon() != null);
Makoto Onukib5a012f2016-06-21 11:13:53 -07002081 }
2082 return actualShortcuts;
2083 }
2084
2085 public static List<ShortcutInfo> assertAllStringsResolved(
2086 List<ShortcutInfo> actualShortcuts) {
2087 for (ShortcutInfo s : actualShortcuts) {
2088 assertTrue("ID " + s.getId(), s.hasStringResourcesResolved());
2089 }
2090 return actualShortcuts;
2091 }
Makoto Onuki76269922016-07-15 14:58:54 -07002092
2093 public String readTestAsset(String assetPath) throws IOException {
2094 final StringBuilder sb = new StringBuilder();
2095 try (BufferedReader br = new BufferedReader(
2096 new InputStreamReader(
2097 getTestContext().getResources().getAssets().open(assetPath)))) {
2098 String line;
2099 while ((line = br.readLine()) != null) {
2100 sb.append(line);
2101 sb.append(System.lineSeparator());
2102 }
2103 }
2104 return sb.toString();
2105 }
Makoto Onuki0b9d1db2016-07-18 14:16:41 -07002106
2107 protected void prepareGetHomeActivitiesAsUser(ComponentName preferred,
2108 List<ResolveInfo> candidates, int userId) {
2109 doAnswer(inv -> {
2110 ((List) inv.getArguments()[0]).addAll(candidates);
2111 return preferred;
2112 }).when(mMockPackageManagerInternal).getHomeActivitiesAsUser(any(List.class), eq(userId));
2113 }
2114
2115 protected static ComponentName cn(String packageName, String name) {
2116 return new ComponentName(packageName, name);
2117 }
2118
2119 protected static ResolveInfo ri(String packageName, String name, boolean isSystem, int priority) {
2120 final ResolveInfo ri = new ResolveInfo();
2121 ri.activityInfo = new ActivityInfo();
2122 ri.activityInfo.applicationInfo = new ApplicationInfo();
2123
2124 ri.activityInfo.packageName = packageName;
2125 ri.activityInfo.name = name;
2126 if (isSystem) {
2127 ri.activityInfo.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
2128 }
2129 ri.priority = priority;
2130 return ri;
2131 }
2132
2133 protected static ResolveInfo getSystemLauncher() {
2134 return ri(PACKAGE_SYSTEM_LAUNCHER, PACKAGE_SYSTEM_LAUNCHER_NAME, true,
2135 PACKAGE_SYSTEM_LAUNCHER_PRIORITY);
2136 }
2137
2138 protected static ResolveInfo getFallbackLauncher() {
2139 return ri(PACKAGE_FALLBACK_LAUNCHER, PACKAGE_FALLBACK_LAUNCHER_NAME, true,
2140 PACKAGE_FALLBACK_LAUNCHER_PRIORITY);
2141 }
Makoto Onukia01f4f02016-12-15 15:58:41 -08002142
Tony Maked6ef622017-12-07 16:36:16 +00002143 protected void makeUidForeground(int uid) {
Makoto Onukia01f4f02016-12-15 15:58:41 -08002144 try {
2145 mService.mUidObserver.onUidStateChanged(
Tony Maked6ef622017-12-07 16:36:16 +00002146 uid, ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE, 0);
2147 } catch (RemoteException e) {
2148 e.rethrowAsRuntimeException();
2149 }
2150 }
2151
2152 protected void makeCallerForeground() {
2153 makeUidForeground(mInjectedCallingUid);
2154 }
2155
2156 protected void makeUidBackground(int uid) {
2157 try {
2158 mService.mUidObserver.onUidStateChanged(
2159 uid, ActivityManager.PROCESS_STATE_TOP_SLEEPING, 0);
Makoto Onukia01f4f02016-12-15 15:58:41 -08002160 } catch (RemoteException e) {
2161 e.rethrowAsRuntimeException();
2162 }
2163 }
2164
2165 protected void makeCallerBackground() {
Tony Maked6ef622017-12-07 16:36:16 +00002166 makeUidBackground(mInjectedCallingUid);
Makoto Onukia01f4f02016-12-15 15:58:41 -08002167 }
2168
2169 protected void publishManifestShortcutsAsCaller(int resId) {
2170 addManifestShortcutResource(
2171 new ComponentName(getCallingPackage(), ShortcutActivity.class.getName()),
2172 resId);
2173 updatePackageVersion(getCallingPackage(), 1);
2174 mService.mPackageMonitor.onReceive(getTestContext(),
2175 genPackageAddIntent(getCallingPackage(), getCallingUserId()));
2176 }
Makoto Onuki50a320e2017-05-31 14:38:42 -07002177
2178 protected void assertFileNotExists(String path) {
2179 final File f = new File(mInjectedFilePathRoot, path);
2180 assertFalse("File shouldn't exist: " + f.getAbsolutePath(), f.exists());
2181 }
2182
2183 protected void assertFileExistsWithContent(String path) {
2184 final File f = new File(mInjectedFilePathRoot, path);
2185 assertTrue("File should exist: " + f.getAbsolutePath(), f.exists());
2186 assertTrue("File should be larger than 0b: " + f.getAbsolutePath(), f.length() > 0);
2187 }
Makoto Onuki51ab2b32016-06-02 11:03:51 -07002188}