BaseShortcutManagerTest.java revision 248a0ef3aa119f92858a2bb95d20595824b8d07b
1/*
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
18import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.cloneShortcutList;
19import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.hashSet;
20import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.list;
21import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.makeBundle;
22import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.set;
23
24import static org.mockito.Matchers.any;
25import static org.mockito.Matchers.anyInt;
26import static org.mockito.Matchers.anyString;
27import static org.mockito.Matchers.eq;
28import static org.mockito.Mockito.doAnswer;
29import static org.mockito.Mockito.mock;
30import static org.mockito.Mockito.reset;
31import static org.mockito.Mockito.spy;
32import static org.mockito.Mockito.times;
33import static org.mockito.Mockito.verify;
34import static org.mockito.Mockito.when;
35
36import android.annotation.NonNull;
37import android.annotation.UserIdInt;
38import android.app.Activity;
39import android.app.ActivityManager;
40import android.app.ActivityManagerInternal;
41import android.app.IUidObserver;
42import android.app.usage.UsageStatsManagerInternal;
43import android.content.ActivityNotFoundException;
44import android.content.BroadcastReceiver;
45import android.content.ComponentName;
46import android.content.Context;
47import android.content.Intent;
48import android.content.IntentFilter;
49import android.content.pm.ActivityInfo;
50import android.content.pm.ApplicationInfo;
51import android.content.pm.ILauncherApps;
52import android.content.pm.LauncherApps;
53import android.content.pm.LauncherApps.ShortcutQuery;
54import android.content.pm.PackageInfo;
55import android.content.pm.PackageManager;
56import android.content.pm.PackageManagerInternal;
57import android.content.pm.ResolveInfo;
58import android.content.pm.ShortcutInfo;
59import android.content.pm.ShortcutManager;
60import android.content.pm.ShortcutServiceInternal;
61import android.content.pm.Signature;
62import android.content.pm.UserInfo;
63import android.content.res.Resources;
64import android.content.res.XmlResourceParser;
65import android.graphics.drawable.Icon;
66import android.net.Uri;
67import android.os.Bundle;
68import android.os.FileUtils;
69import android.os.Handler;
70import android.os.Looper;
71import android.os.PersistableBundle;
72import android.os.Process;
73import android.os.UserHandle;
74import android.os.UserManager;
75import android.test.InstrumentationTestCase;
76import android.test.mock.MockContext;
77import android.util.Log;
78import android.util.Pair;
79
80import com.android.internal.util.Preconditions;
81import com.android.server.LocalServices;
82import com.android.server.SystemService;
83import com.android.server.pm.LauncherAppsService.LauncherAppsImpl;
84import com.android.server.pm.ShortcutUser.PackageWithUser;
85
86import org.junit.Assert;
87import org.mockito.ArgumentCaptor;
88import org.mockito.invocation.InvocationOnMock;
89import org.mockito.stubbing.Answer;
90
91import java.io.BufferedReader;
92import java.io.ByteArrayOutputStream;
93import java.io.File;
94import java.io.FileReader;
95import java.io.IOException;
96import java.io.InputStreamReader;
97import java.io.PrintWriter;
98import java.util.ArrayList;
99import java.util.HashMap;
100import java.util.HashSet;
101import java.util.LinkedHashMap;
102import java.util.List;
103import java.util.Locale;
104import java.util.Map;
105import java.util.Set;
106import java.util.function.BiFunction;
107import java.util.function.BiPredicate;
108import java.util.function.Consumer;
109import java.util.function.Function;
110
111public abstract class BaseShortcutManagerTest extends InstrumentationTestCase {
112    protected static final String TAG = "ShortcutManagerTest";
113
114    protected static final boolean DUMP_IN_TEARDOWN = false; // DO NOT SUBMIT WITH true
115
116    /**
117     * Whether to enable dump or not.  Should be only true when debugging to avoid bugs where
118     * dump affecting the behavior.
119     */
120    protected static final boolean ENABLE_DUMP = false // DO NOT SUBMIT WITH true
121            || DUMP_IN_TEARDOWN || ShortcutService.DEBUG;
122
123    protected static final String[] EMPTY_STRINGS = new String[0]; // Just for readability.
124
125    protected static final String MAIN_ACTIVITY_CLASS = "MainActivity";
126
127    // public for mockito
128    public class BaseContext extends MockContext {
129        @Override
130        public Object getSystemService(String name) {
131            switch (name) {
132                case Context.USER_SERVICE:
133                    return mMockUserManager;
134            }
135            throw new UnsupportedOperationException();
136        }
137
138        @Override
139        public String getSystemServiceName(Class<?> serviceClass) {
140            return getTestContext().getSystemServiceName(serviceClass);
141        }
142
143        @Override
144        public PackageManager getPackageManager() {
145            return mMockPackageManager;
146        }
147
148        @Override
149        public Resources getResources() {
150            return getTestContext().getResources();
151        }
152
153        @Override
154        public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
155                IntentFilter filter, String broadcastPermission, Handler scheduler) {
156            // ignore.
157            return null;
158        }
159
160        @Override
161        public void unregisterReceiver(BroadcastReceiver receiver) {
162            // ignore.
163        }
164    }
165
166    /** Context used in the client side */
167    public class ClientContext extends BaseContext {
168        @Override
169        public String getPackageName() {
170            return mInjectedClientPackage;
171        }
172
173        @Override
174        public int getUserId() {
175            return getCallingUserId();
176        }
177    }
178
179    /** Context used in the service side */
180    public class ServiceContext extends BaseContext {
181        long injectClearCallingIdentity() {
182            final int prevCallingUid = mInjectedCallingUid;
183            mInjectedCallingUid = Process.SYSTEM_UID;
184            return prevCallingUid;
185        }
186
187        void injectRestoreCallingIdentity(long token) {
188            mInjectedCallingUid = (int) token;
189        }
190
191        @Override
192        public int getUserId() {
193            return UserHandle.USER_SYSTEM;
194        }
195
196        public PackageInfo injectGetActivitiesWithMetadata(
197                String packageName, @UserIdInt int userId) {
198            return BaseShortcutManagerTest.this.injectGetActivitiesWithMetadata(packageName, userId);
199        }
200
201        public XmlResourceParser injectXmlMetaData(ActivityInfo activityInfo, String key) {
202            return BaseShortcutManagerTest.this.injectXmlMetaData(activityInfo, key);
203        }
204    }
205
206    /** ShortcutService with injection override methods. */
207    protected final class ShortcutServiceTestable extends ShortcutService {
208        final ServiceContext mContext;
209        IUidObserver mUidObserver;
210
211        public ShortcutServiceTestable(ServiceContext context, Looper looper) {
212            super(context, looper, /* onyForPackageManagerApis */ false);
213            mContext = context;
214        }
215
216        @Override
217        public String injectGetLocaleTagsForUser(@UserIdInt int userId) {
218            return mInjectedLocale.toLanguageTag();
219        }
220
221        @Override
222        boolean injectShouldPerformVerification() {
223            return true; // Always verify during unit tests.
224        }
225
226        @Override
227        String injectShortcutManagerConstants() {
228            return ConfigConstants.KEY_RESET_INTERVAL_SEC + "=" + (INTERVAL / 1000) + ","
229                    + ConfigConstants.KEY_MAX_SHORTCUTS + "=" + MAX_SHORTCUTS + ","
230                    + ConfigConstants.KEY_MAX_UPDATES_PER_INTERVAL + "="
231                    + MAX_UPDATES_PER_INTERVAL + ","
232                    + ConfigConstants.KEY_MAX_ICON_DIMENSION_DP + "=" + MAX_ICON_DIMENSION + ","
233                    + ConfigConstants.KEY_MAX_ICON_DIMENSION_DP_LOWRAM + "="
234                    + MAX_ICON_DIMENSION_LOWRAM + ","
235                    + ConfigConstants.KEY_ICON_FORMAT + "=PNG,"
236                    + ConfigConstants.KEY_ICON_QUALITY + "=100";
237        }
238
239        @Override
240        long injectClearCallingIdentity() {
241            return mContext.injectClearCallingIdentity();
242        }
243
244        @Override
245        void injectRestoreCallingIdentity(long token) {
246            mContext.injectRestoreCallingIdentity(token);
247        }
248
249        @Override
250        int injectDipToPixel(int dip) {
251            return dip;
252        }
253
254        @Override
255        long injectCurrentTimeMillis() {
256            return mInjectedCurrentTimeMillis;
257        }
258
259        @Override
260        long injectElapsedRealtime() {
261            // TODO This should be kept separately from mInjectedCurrentTimeMillis, since
262            // this should increase even if we rewind mInjectedCurrentTimeMillis in some tests.
263            return mInjectedCurrentTimeMillis - START_TIME;
264        }
265
266        @Override
267        int injectBinderCallingUid() {
268            return mInjectedCallingUid;
269        }
270
271        @Override
272        int injectGetPackageUid(String packageName, int userId) {
273            return getInjectedPackageInfo(packageName, userId, false).applicationInfo.uid;
274        }
275
276        @Override
277        File injectSystemDataPath() {
278            return new File(mInjectedFilePathRoot, "system");
279        }
280
281        @Override
282        File injectUserDataPath(@UserIdInt int userId) {
283            return new File(mInjectedFilePathRoot, "user-" + userId);
284        }
285
286        @Override
287        void injectValidateIconResPackage(ShortcutInfo shortcut, Icon icon) {
288            // Can't check
289        }
290
291        @Override
292        boolean injectIsLowRamDevice() {
293            return mInjectedIsLowRamDevice;
294        }
295
296        @Override
297        void injectRegisterUidObserver(IUidObserver observer, int which) {
298            mUidObserver = observer;
299        }
300
301        @Override
302        boolean hasShortcutHostPermission(@NonNull String callingPackage, int userId) {
303            return mDefaultLauncherChecker.test(callingPackage, userId);
304        }
305
306        @Override
307        PackageInfo injectPackageInfoWithUninstalled(String packageName, @UserIdInt int userId,
308                boolean getSignatures) {
309            return getInjectedPackageInfo(packageName, userId, getSignatures);
310        }
311
312        @Override
313        ApplicationInfo injectApplicationInfoWithUninstalled(
314                String packageName, @UserIdInt int userId) {
315            PackageInfo pi = injectPackageInfoWithUninstalled(
316                    packageName, userId, /* getSignatures= */ false);
317            return pi != null ? pi.applicationInfo : null;
318        }
319
320        @Override
321        List<PackageInfo> injectGetPackagesWithUninstalled(@UserIdInt int userId) {
322            return BaseShortcutManagerTest.this.getInstalledPackagesWithUninstalled(userId);
323        }
324
325        @Override
326        ActivityInfo injectGetActivityInfoWithMetadataWithUninstalled(ComponentName activity,
327                @UserIdInt int userId) {
328            final PackageInfo pi = mContext.injectGetActivitiesWithMetadata(
329                    activity.getPackageName(), userId);
330            if (pi == null || pi.activities == null) {
331                return null;
332            }
333            for (ActivityInfo ai : pi.activities) {
334                if (!mEnabledActivityChecker.test(ai.getComponentName(), userId)) {
335                    continue;
336                }
337                if (activity.equals(ai.getComponentName())) {
338                    return ai;
339                }
340            }
341            return null;
342        }
343
344        @Override
345        boolean injectIsMainActivity(@NonNull ComponentName activity, int userId) {
346            if (!mEnabledActivityChecker.test(activity, userId)) {
347                return false;
348            }
349            return mMainActivityChecker.test(activity, userId);
350        }
351
352        @Override
353        List<ResolveInfo> injectGetMainActivities(@NonNull String packageName, int userId) {
354            final PackageInfo pi = mContext.injectGetActivitiesWithMetadata(
355                    packageName, userId);
356            if (pi == null || pi.activities == null) {
357                return null;
358            }
359            final ArrayList<ResolveInfo> ret = new ArrayList<>(pi.activities.length);
360            for (int i = 0; i < pi.activities.length; i++) {
361                if (!mEnabledActivityChecker.test(pi.activities[i].getComponentName(), userId)) {
362                    continue;
363                }
364                final ResolveInfo ri = new ResolveInfo();
365                ri.activityInfo = pi.activities[i];
366                ret.add(ri);
367            }
368
369            return ret;
370        }
371
372        @Override
373        ComponentName injectGetDefaultMainActivity(@NonNull String packageName, int userId) {
374            return mMainActivityFetcher.apply(packageName, userId);
375        }
376
377        @Override
378        boolean injectIsActivityEnabledAndExported(ComponentName activity, @UserIdInt int userId) {
379            return mEnabledActivityChecker.test(activity, userId);
380        }
381
382        @Override
383        XmlResourceParser injectXmlMetaData(ActivityInfo activityInfo, String key) {
384            return mContext.injectXmlMetaData(activityInfo, key);
385        }
386
387        @Override
388        void injectPostToHandler(Runnable r) {
389            runOnHandler(r);
390        }
391
392        @Override
393        void injectRunOnNewThread(Runnable r) {
394            runOnHandler(r);
395        }
396
397        @Override
398        void injectEnforceCallingPermission(String permission, String message) {
399            if (!mCallerPermissions.contains(permission)) {
400                throw new SecurityException("Missing permission: " + permission);
401            }
402        }
403
404        @Override
405        boolean injectIsSafeModeEnabled() {
406            return mSafeMode;
407        }
408
409        @Override
410        String injectBuildFingerprint() {
411            return mInjectedBuildFingerprint;
412        }
413
414        @Override
415        void wtf(String message, Throwable th) {
416            // During tests, WTF is fatal.
417            fail(message + "  exception: " + th + "\n" + Log.getStackTraceString(th));
418        }
419    }
420
421    /** ShortcutManager with injection override methods. */
422    protected class ShortcutManagerTestable extends ShortcutManager {
423        public ShortcutManagerTestable(Context context, ShortcutServiceTestable service) {
424            super(context, service);
425        }
426
427        @Override
428        protected int injectMyUserId() {
429            return UserHandle.getUserId(mInjectedCallingUid);
430        }
431
432        @Override
433        public boolean setDynamicShortcuts(@NonNull List<ShortcutInfo> shortcutInfoList) {
434            // Note to simulate the binder RPC, we need to clone the incoming arguments.
435            // Otherwise bad things will happen because they're mutable.
436            return super.setDynamicShortcuts(cloneShortcutList(shortcutInfoList));
437        }
438
439        @Override
440        public boolean addDynamicShortcuts(@NonNull List<ShortcutInfo> shortcutInfoList) {
441            // Note to simulate the binder RPC, we need to clone the incoming arguments.
442            return super.addDynamicShortcuts(cloneShortcutList(shortcutInfoList));
443        }
444
445        @Override
446        public boolean updateShortcuts(List<ShortcutInfo> shortcutInfoList) {
447            // Note to simulate the binder RPC, we need to clone the incoming arguments.
448            return super.updateShortcuts(cloneShortcutList(shortcutInfoList));
449        }
450    }
451
452    protected class LauncherAppImplTestable extends LauncherAppsImpl {
453        final ServiceContext mContext;
454
455        public LauncherAppImplTestable(ServiceContext context) {
456            super(context);
457            mContext = context;
458        }
459
460        @Override
461        public void verifyCallingPackage(String callingPackage) {
462            // SKIP
463        }
464
465        @Override
466        void postToPackageMonitorHandler(Runnable r) {
467            runOnHandler(r);
468        }
469
470        @Override
471        int injectBinderCallingUid() {
472            return mInjectedCallingUid;
473        }
474
475        @Override
476        long injectClearCallingIdentity() {
477            final int prevCallingUid = mInjectedCallingUid;
478            mInjectedCallingUid = Process.SYSTEM_UID;
479            return prevCallingUid;
480        }
481
482        @Override
483        void injectRestoreCallingIdentity(long token) {
484            mInjectedCallingUid = (int) token;
485        }
486    }
487
488    protected class LauncherAppsTestable extends LauncherApps {
489        public LauncherAppsTestable(Context context, ILauncherApps service) {
490            super(context, service);
491        }
492    }
493
494    public static class ShortcutActivity extends Activity {
495    }
496
497    public static class ShortcutActivity2 extends Activity {
498    }
499
500    public static class ShortcutActivity3 extends Activity {
501    }
502
503    protected Looper mLooper;
504    protected Handler mHandler;
505
506    protected ServiceContext mServiceContext;
507    protected ClientContext mClientContext;
508
509    protected ShortcutServiceTestable mService;
510    protected ShortcutManagerTestable mManager;
511    protected ShortcutServiceInternal mInternal;
512
513    protected LauncherAppImplTestable mLauncherAppImpl;
514
515    // LauncherApps has per-instace state, so we need a differnt instance for each launcher.
516    protected final Map<Pair<Integer, String>, LauncherAppsTestable>
517            mLauncherAppsMap = new HashMap<>();
518    protected LauncherAppsTestable mLauncherApps; // Current one
519
520    protected File mInjectedFilePathRoot;
521
522    protected boolean mSafeMode;
523
524    protected long mInjectedCurrentTimeMillis;
525
526    protected boolean mInjectedIsLowRamDevice;
527
528    protected Locale mInjectedLocale = Locale.ENGLISH;
529
530    protected int mInjectedCallingUid;
531    protected String mInjectedClientPackage;
532
533    protected Map<String, PackageInfo> mInjectedPackages;
534
535    protected Set<PackageWithUser> mUninstalledPackages;
536    protected Set<String> mSystemPackages;
537
538    protected PackageManager mMockPackageManager;
539    protected PackageManagerInternal mMockPackageManagerInternal;
540    protected UserManager mMockUserManager;
541    protected UsageStatsManagerInternal mMockUsageStatsManagerInternal;
542    protected ActivityManagerInternal mMockActivityManagerInternal;
543
544    protected static final String CALLING_PACKAGE_1 = "com.android.test.1";
545    protected static final int CALLING_UID_1 = 10001;
546
547    protected static final String CALLING_PACKAGE_2 = "com.android.test.2";
548    protected static final int CALLING_UID_2 = 10002;
549
550    protected static final String CALLING_PACKAGE_3 = "com.android.test.3";
551    protected static final int CALLING_UID_3 = 10003;
552
553    protected static final String CALLING_PACKAGE_4 = "com.android.test.4";
554    protected static final int CALLING_UID_4 = 10004;
555
556    protected static final String LAUNCHER_1 = "com.android.launcher.1";
557    protected static final int LAUNCHER_UID_1 = 10011;
558
559    protected static final String LAUNCHER_2 = "com.android.launcher.2";
560    protected static final int LAUNCHER_UID_2 = 10012;
561
562    protected static final String LAUNCHER_3 = "com.android.launcher.3";
563    protected static final int LAUNCHER_UID_3 = 10013;
564
565    protected static final String LAUNCHER_4 = "com.android.launcher.4";
566    protected static final int LAUNCHER_UID_4 = 10014;
567
568    protected static final int USER_0 = UserHandle.USER_SYSTEM;
569    protected static final int USER_10 = 10;
570    protected static final int USER_11 = 11;
571    protected static final int USER_P0 = 20; // profile of user 0
572
573    protected static final UserHandle HANDLE_USER_0 = UserHandle.of(USER_0);
574    protected static final UserHandle HANDLE_USER_10 = UserHandle.of(USER_10);
575    protected static final UserHandle HANDLE_USER_11 = UserHandle.of(USER_11);
576    protected static final UserHandle HANDLE_USER_P0 = UserHandle.of(USER_P0);
577
578    protected static final UserInfo USER_INFO_0 = withProfileGroupId(
579            new UserInfo(USER_0, "user0",
580                    UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY | UserInfo.FLAG_INITIALIZED), 10);
581
582    protected static final UserInfo USER_INFO_10 =
583            new UserInfo(USER_10, "user10", UserInfo.FLAG_INITIALIZED);
584
585    protected static final UserInfo USER_INFO_11 =
586            new UserInfo(USER_11, "user11", UserInfo.FLAG_INITIALIZED);
587
588    protected static final UserInfo USER_INFO_P0 = withProfileGroupId(
589            new UserInfo(USER_P0, "userP0",
590                    UserInfo.FLAG_MANAGED_PROFILE), 10);
591
592    protected BiPredicate<String, Integer> mDefaultLauncherChecker =
593            (callingPackage, userId) ->
594            LAUNCHER_1.equals(callingPackage) || LAUNCHER_2.equals(callingPackage)
595            || LAUNCHER_3.equals(callingPackage) || LAUNCHER_4.equals(callingPackage);
596
597    protected BiPredicate<ComponentName, Integer> mMainActivityChecker =
598            (activity, userId) -> true;
599
600    protected BiFunction<String, Integer, ComponentName> mMainActivityFetcher =
601            (packageName, userId) -> new ComponentName(packageName, MAIN_ACTIVITY_CLASS);
602
603    protected BiPredicate<ComponentName, Integer> mEnabledActivityChecker
604            = (activity, userId) -> true; // all activities are enabled.
605
606    protected static final long START_TIME = 1440000000101L;
607
608    protected static final long INTERVAL = 10000;
609
610    protected static final int MAX_SHORTCUTS = 10;
611
612    protected static final int MAX_UPDATES_PER_INTERVAL = 3;
613
614    protected static final int MAX_ICON_DIMENSION = 128;
615
616    protected static final int MAX_ICON_DIMENSION_LOWRAM = 32;
617
618    protected static final ShortcutQuery QUERY_ALL = new ShortcutQuery();
619
620    protected final ArrayList<String> mCallerPermissions = new ArrayList<>();
621
622    protected final HashMap<String, LinkedHashMap<ComponentName, Integer>> mActivityMetadataResId
623            = new HashMap<>();
624
625    protected final Map<Integer, UserInfo> mUserInfos = new HashMap<>();
626    protected final Map<Integer, Boolean> mRunningUsers = new HashMap<>();
627    protected final Map<Integer, Boolean> mUnlockedUsers = new HashMap<>();
628
629    protected static final String PACKAGE_SYSTEM_LAUNCHER = "com.android.systemlauncher";
630    protected static final String PACKAGE_SYSTEM_LAUNCHER_NAME = "systemlauncher_name";
631    protected static final int PACKAGE_SYSTEM_LAUNCHER_PRIORITY = 0;
632
633    protected static final String PACKAGE_FALLBACK_LAUNCHER = "com.android.settings";
634    protected static final String PACKAGE_FALLBACK_LAUNCHER_NAME = "fallback";
635    protected static final int PACKAGE_FALLBACK_LAUNCHER_PRIORITY = -999;
636
637    protected String mInjectedBuildFingerprint = "build1";
638
639    static {
640        QUERY_ALL.setQueryFlags(
641                ShortcutQuery.FLAG_GET_ALL_KINDS);
642    }
643
644    @Override
645    protected void setUp() throws Exception {
646        super.setUp();
647
648        mLooper = Looper.getMainLooper();
649        mHandler = new Handler(mLooper);
650
651        mServiceContext = spy(new ServiceContext());
652        mClientContext = new ClientContext();
653
654        mMockPackageManager = mock(PackageManager.class);
655        mMockPackageManagerInternal = mock(PackageManagerInternal.class);
656        mMockUserManager = mock(UserManager.class);
657        mMockUsageStatsManagerInternal = mock(UsageStatsManagerInternal.class);
658        mMockActivityManagerInternal = mock(ActivityManagerInternal.class);
659
660        LocalServices.removeServiceForTest(PackageManagerInternal.class);
661        LocalServices.addService(PackageManagerInternal.class, mMockPackageManagerInternal);
662        LocalServices.removeServiceForTest(UsageStatsManagerInternal.class);
663        LocalServices.addService(UsageStatsManagerInternal.class, mMockUsageStatsManagerInternal);
664        LocalServices.removeServiceForTest(ActivityManagerInternal.class);
665        LocalServices.addService(ActivityManagerInternal.class, mMockActivityManagerInternal);
666
667        // Prepare injection values.
668
669        mInjectedCurrentTimeMillis = START_TIME;
670
671        mInjectedPackages = new HashMap<>();
672        addPackage(CALLING_PACKAGE_1, CALLING_UID_1, 1);
673        addPackage(CALLING_PACKAGE_2, CALLING_UID_2, 2);
674        addPackage(CALLING_PACKAGE_3, CALLING_UID_3, 3);
675        addPackage(CALLING_PACKAGE_4, CALLING_UID_4, 10);
676        addPackage(LAUNCHER_1, LAUNCHER_UID_1, 4);
677        addPackage(LAUNCHER_2, LAUNCHER_UID_2, 5);
678        addPackage(LAUNCHER_3, LAUNCHER_UID_3, 6);
679        addPackage(LAUNCHER_4, LAUNCHER_UID_4, 10);
680
681        // CALLING_PACKAGE_3 / LAUNCHER_3 are not backup target.
682        updatePackageInfo(CALLING_PACKAGE_3,
683                pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP);
684        updatePackageInfo(LAUNCHER_3,
685                pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP);
686
687        mUninstalledPackages = new HashSet<>();
688        mSystemPackages = new HashSet<>();
689
690        mInjectedFilePathRoot = new File(getTestContext().getCacheDir(), "test-files");
691
692        deleteAllSavedFiles();
693
694        // Set up users.
695        when(mMockUserManager.getUserInfo(anyInt())).thenAnswer(new AnswerWithSystemCheck<>(
696                inv -> mUserInfos.get((Integer) inv.getArguments()[0])));
697
698        mUserInfos.put(USER_0, USER_INFO_0);
699        mUserInfos.put(USER_10, USER_INFO_10);
700        mUserInfos.put(USER_11, USER_INFO_11);
701        mUserInfos.put(USER_P0, USER_INFO_P0);
702
703        // Set up isUserRunning and isUserUnlocked.
704        when(mMockUserManager.isUserRunning(anyInt())).thenAnswer(new AnswerWithSystemCheck<>(
705                        inv -> b(mRunningUsers.get((Integer) inv.getArguments()[0]))));
706
707        when(mMockUserManager.isUserUnlocked(anyInt()))
708                .thenAnswer(new AnswerWithSystemCheck<>(inv -> {
709                    final int userId = (Integer) inv.getArguments()[0];
710                    return b(mRunningUsers.get(userId)) && b(mUnlockedUsers.get(userId));
711                }));
712        // isUserUnlockingOrUnlocked() return the same value as isUserUnlocked().
713        when(mMockUserManager.isUserUnlockingOrUnlocked(anyInt()))
714                .thenAnswer(new AnswerWithSystemCheck<>(inv -> {
715                    final int userId = (Integer) inv.getArguments()[0];
716                    return b(mRunningUsers.get(userId)) && b(mUnlockedUsers.get(userId));
717                }));
718
719        when(mMockActivityManagerInternal.getUidProcessState(anyInt())).thenReturn(
720                ActivityManager.PROCESS_STATE_CACHED_EMPTY);
721
722        // User 0 and P0 are always running
723        mRunningUsers.put(USER_0, true);
724        mRunningUsers.put(USER_10, false);
725        mRunningUsers.put(USER_11, false);
726        mRunningUsers.put(USER_P0, true);
727
728        // Unlock all users by default.
729        mUnlockedUsers.put(USER_0, true);
730        mUnlockedUsers.put(USER_10, true);
731        mUnlockedUsers.put(USER_11, true);
732        mUnlockedUsers.put(USER_P0, true);
733
734        // Set up resources
735        setUpAppResources();
736
737        // Start the service.
738        initService();
739        setCaller(CALLING_PACKAGE_1);
740
741        if (ENABLE_DUMP) {
742            Log.d(TAG, "setUp done");
743        }
744    }
745
746    private static boolean b(Boolean value) {
747        return (value != null && value);
748    }
749
750    /**
751     * Returns a boolean but also checks if the current UID is SYSTEM_UID.
752     */
753    protected class AnswerWithSystemCheck<T> implements Answer<T> {
754        private final Function<InvocationOnMock, T> mChecker;
755
756        public AnswerWithSystemCheck(Function<InvocationOnMock, T> checker) {
757            mChecker = checker;
758        }
759
760        @Override
761        public T answer(InvocationOnMock invocation) throws Throwable {
762            assertEquals("Must be called on SYSTEM UID.",
763                    Process.SYSTEM_UID, mInjectedCallingUid);
764            return mChecker.apply(invocation);
765        }
766    }
767
768    protected void setUpAppResources() throws Exception {
769        setUpAppResources(/* offset = */ 0);
770    }
771
772    protected void setUpAppResources(int ressIdOffset) throws Exception {
773        // ressIdOffset is used to adjust resource IDs to emulate the case where an updated app
774        // has resource IDs changed.
775
776        doAnswer(pmInvocation -> {
777            assertEquals(Process.SYSTEM_UID, mInjectedCallingUid);
778
779            final String packageName = (String) pmInvocation.getArguments()[0];
780            final int userId = (Integer) pmInvocation.getArguments()[1];
781
782            final Resources res = mock(Resources.class);
783
784            doAnswer(resInvocation -> {
785                final int argResId = (Integer) resInvocation.getArguments()[0];
786
787                return "string-" + packageName + "-user:" + userId + "-res:" + argResId
788                        + "/" + mInjectedLocale;
789            }).when(res).getString(anyInt());
790
791            doAnswer(resInvocation -> {
792                final int resId = (Integer) resInvocation.getArguments()[0];
793
794                // Always use the "string" resource type.  The type doesn't matter during the test.
795                return packageName + ":string/r" + resId;
796            }).when(res).getResourceName(anyInt());
797
798            doAnswer(resInvocation -> {
799                final String argResName = (String) resInvocation.getArguments()[0];
800                final String argType = (String) resInvocation.getArguments()[1];
801                final String argPackageName = (String) resInvocation.getArguments()[2];
802
803                // See the above code.  getResourceName() will just use "r" + res ID as the entry
804                // name.
805                String entryName = argResName;
806                if (entryName.contains("/")) {
807                    entryName = ShortcutInfo.getResourceEntryName(entryName);
808                }
809                return Integer.parseInt(entryName.substring(1)) + ressIdOffset;
810            }).when(res).getIdentifier(anyString(), anyString(), anyString());
811            return res;
812        }).when(mMockPackageManager).getResourcesForApplicationAsUser(anyString(), anyInt());
813    }
814
815    protected static UserInfo withProfileGroupId(UserInfo in, int groupId) {
816        in.profileGroupId = groupId;
817        return in;
818    }
819
820    @Override
821    protected void tearDown() throws Exception {
822        if (DUMP_IN_TEARDOWN) dumpsysOnLogcat("Teardown");
823
824        shutdownServices();
825
826        super.tearDown();
827    }
828
829    protected Context getTestContext() {
830        return getInstrumentation().getContext();
831    }
832
833    protected ShortcutManager getManager() {
834        return mManager;
835    }
836
837    protected void deleteAllSavedFiles() {
838        // Empty the data directory.
839        if (mInjectedFilePathRoot.exists()) {
840            Assert.assertTrue("failed to delete dir",
841                    FileUtils.deleteContents(mInjectedFilePathRoot));
842        }
843        mInjectedFilePathRoot.mkdirs();
844    }
845
846    /** (Re-) init the manager and the service. */
847    protected void initService() {
848        shutdownServices();
849
850        LocalServices.removeServiceForTest(ShortcutServiceInternal.class);
851
852        // Instantiate targets.
853        mService = new ShortcutServiceTestable(mServiceContext, mLooper);
854        mManager = new ShortcutManagerTestable(mClientContext, mService);
855
856        mInternal = LocalServices.getService(ShortcutServiceInternal.class);
857
858        mLauncherAppImpl = new LauncherAppImplTestable(mServiceContext);
859        mLauncherApps = null;
860        mLauncherAppsMap.clear();
861
862        // Send boot sequence events.
863        mService.onBootPhase(SystemService.PHASE_LOCK_SETTINGS_READY);
864
865        mService.onBootPhase(SystemService.PHASE_BOOT_COMPLETED);
866    }
867
868    protected void shutdownServices() {
869        if (mService != null) {
870            // Flush all the unsaved data from the previous instance.
871            mService.saveDirtyInfo();
872
873            // Make sure everything is consistent.
874            mService.verifyStates();
875        }
876        LocalServices.removeServiceForTest(ShortcutServiceInternal.class);
877
878        mService = null;
879        mManager = null;
880        mInternal = null;
881        mLauncherAppImpl = null;
882        mLauncherApps = null;
883        mLauncherAppsMap.clear();
884    }
885
886    protected void runOnHandler(Runnable r) {
887        final long token = mServiceContext.injectClearCallingIdentity();
888        try {
889            r.run();
890        } finally {
891            mServiceContext.injectRestoreCallingIdentity(token);
892        }
893    }
894
895    protected void addPackage(String packageName, int uid, int version) {
896        addPackage(packageName, uid, version, packageName);
897    }
898
899    protected Signature[] genSignatures(String... signatures) {
900        final Signature[] sigs = new Signature[signatures.length];
901        for (int i = 0; i < signatures.length; i++){
902            sigs[i] = new Signature(signatures[i].getBytes());
903        }
904        return sigs;
905    }
906
907    protected PackageInfo genPackage(String packageName, int uid, int version, String... signatures) {
908        final PackageInfo pi = new PackageInfo();
909        pi.packageName = packageName;
910        pi.applicationInfo = new ApplicationInfo();
911        pi.applicationInfo.uid = uid;
912        pi.applicationInfo.flags = ApplicationInfo.FLAG_INSTALLED
913                | ApplicationInfo.FLAG_ALLOW_BACKUP;
914        pi.versionCode = version;
915        pi.applicationInfo.versionCode = version;
916        pi.signatures = genSignatures(signatures);
917
918        return pi;
919    }
920
921    protected void addPackage(String packageName, int uid, int version, String... signatures) {
922        mInjectedPackages.put(packageName, genPackage(packageName, uid, version, signatures));
923    }
924
925    protected void updatePackageInfo(String packageName, Consumer<PackageInfo> c) {
926        c.accept(mInjectedPackages.get(packageName));
927    }
928
929    protected void updatePackageVersion(String packageName, int increment) {
930        updatePackageInfo(packageName, pi -> {
931            pi.versionCode += increment;
932            pi.applicationInfo.versionCode += increment;
933        });
934    }
935
936    protected void updatePackageLastUpdateTime(String packageName, long increment) {
937        updatePackageInfo(packageName, pi -> {
938            pi.lastUpdateTime += increment;
939        });
940    }
941
942    protected void setPackageLastUpdateTime(String packageName, long value) {
943        updatePackageInfo(packageName, pi -> {
944            pi.lastUpdateTime = value;
945        });
946    }
947
948    protected void uninstallPackage(int userId, String packageName) {
949        if (ENABLE_DUMP) {
950            Log.v(TAG, "Unnstall package " + packageName + " / " + userId);
951        }
952        mUninstalledPackages.add(PackageWithUser.of(userId, packageName));
953    }
954
955    protected void installPackage(int userId, String packageName) {
956        if (ENABLE_DUMP) {
957            Log.v(TAG, "Install package " + packageName + " / " + userId);
958        }
959        mUninstalledPackages.remove(PackageWithUser.of(userId, packageName));
960    }
961
962    PackageInfo getInjectedPackageInfo(String packageName, @UserIdInt int userId,
963            boolean getSignatures) {
964        final PackageInfo pi = mInjectedPackages.get(packageName);
965        if (pi == null) return null;
966
967        final PackageInfo ret = new PackageInfo();
968        ret.packageName = pi.packageName;
969        ret.versionCode = pi.versionCode;
970        ret.lastUpdateTime = pi.lastUpdateTime;
971
972        ret.applicationInfo = new ApplicationInfo(pi.applicationInfo);
973        ret.applicationInfo.uid = UserHandle.getUid(userId, pi.applicationInfo.uid);
974        ret.applicationInfo.packageName = pi.packageName;
975
976        if (mUninstalledPackages.contains(PackageWithUser.of(userId, packageName))) {
977            ret.applicationInfo.flags &= ~ApplicationInfo.FLAG_INSTALLED;
978        }
979        if (mSystemPackages.contains(packageName)) {
980            ret.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
981        }
982
983        if (getSignatures) {
984            ret.signatures = pi.signatures;
985        }
986
987        return ret;
988    }
989
990    protected void addApplicationInfo(PackageInfo pi, List<ApplicationInfo> list) {
991        if (pi != null && pi.applicationInfo != null) {
992            list.add(pi.applicationInfo);
993        }
994    }
995
996    protected List<ApplicationInfo> getInstalledApplications(int userId) {
997        final ArrayList<ApplicationInfo> ret = new ArrayList<>();
998
999        addApplicationInfo(getInjectedPackageInfo(CALLING_PACKAGE_1, userId, false), ret);
1000        addApplicationInfo(getInjectedPackageInfo(CALLING_PACKAGE_2, userId, false), ret);
1001        addApplicationInfo(getInjectedPackageInfo(CALLING_PACKAGE_3, userId, false), ret);
1002        addApplicationInfo(getInjectedPackageInfo(CALLING_PACKAGE_4, userId, false), ret);
1003        addApplicationInfo(getInjectedPackageInfo(LAUNCHER_1, userId, false), ret);
1004        addApplicationInfo(getInjectedPackageInfo(LAUNCHER_2, userId, false), ret);
1005        addApplicationInfo(getInjectedPackageInfo(LAUNCHER_3, userId, false), ret);
1006        addApplicationInfo(getInjectedPackageInfo(LAUNCHER_4, userId, false), ret);
1007
1008        return ret;
1009    }
1010
1011    private void addPackageInfo(PackageInfo pi, List<PackageInfo> list) {
1012        if (pi != null) {
1013            list.add(pi);
1014        }
1015    }
1016
1017    private List<PackageInfo> getInstalledPackagesWithUninstalled(int userId) {
1018        final ArrayList<PackageInfo> ret = new ArrayList<>();
1019
1020        addPackageInfo(getInjectedPackageInfo(CALLING_PACKAGE_1, userId, false), ret);
1021        addPackageInfo(getInjectedPackageInfo(CALLING_PACKAGE_2, userId, false), ret);
1022        addPackageInfo(getInjectedPackageInfo(CALLING_PACKAGE_3, userId, false), ret);
1023        addPackageInfo(getInjectedPackageInfo(CALLING_PACKAGE_4, userId, false), ret);
1024        addPackageInfo(getInjectedPackageInfo(LAUNCHER_1, userId, false), ret);
1025        addPackageInfo(getInjectedPackageInfo(LAUNCHER_2, userId, false), ret);
1026        addPackageInfo(getInjectedPackageInfo(LAUNCHER_3, userId, false), ret);
1027        addPackageInfo(getInjectedPackageInfo(LAUNCHER_4, userId, false), ret);
1028
1029        return ret;
1030    }
1031
1032    protected void addManifestShortcutResource(ComponentName activity, int resId) {
1033        final String packageName = activity.getPackageName();
1034        LinkedHashMap<ComponentName, Integer> map = mActivityMetadataResId.get(packageName);
1035        if (map == null) {
1036            map = new LinkedHashMap<>();
1037            mActivityMetadataResId.put(packageName, map);
1038        }
1039        map.put(activity, resId);
1040    }
1041
1042    protected PackageInfo injectGetActivitiesWithMetadata(String packageName, @UserIdInt int userId) {
1043        final PackageInfo ret = getInjectedPackageInfo(packageName, userId,
1044                /* getSignatures=*/ false);
1045
1046        final HashMap<ComponentName, Integer> activities = mActivityMetadataResId.get(packageName);
1047        if (activities != null) {
1048            final ArrayList<ActivityInfo> list = new ArrayList<>();
1049
1050            for (ComponentName cn : activities.keySet()) {
1051                ActivityInfo ai = new ActivityInfo();
1052                ai.packageName = cn.getPackageName();
1053                ai.name = cn.getClassName();
1054                ai.metaData = new Bundle();
1055                ai.metaData.putInt(ShortcutParser.METADATA_KEY, activities.get(cn));
1056                ai.applicationInfo = ret.applicationInfo;
1057                list.add(ai);
1058            }
1059            ret.activities = list.toArray(new ActivityInfo[list.size()]);
1060        }
1061        return ret;
1062    }
1063
1064    protected XmlResourceParser injectXmlMetaData(ActivityInfo activityInfo, String key) {
1065        if (!ShortcutParser.METADATA_KEY.equals(key) || activityInfo.metaData == null) {
1066            return null;
1067        }
1068        final int resId = activityInfo.metaData.getInt(key);
1069        return getTestContext().getResources().getXml(resId);
1070    }
1071
1072    /** Replace the current calling package */
1073    protected void setCaller(String packageName, int userId) {
1074        mInjectedClientPackage = packageName;
1075        mInjectedCallingUid =
1076                Preconditions.checkNotNull(getInjectedPackageInfo(packageName, userId, false),
1077                        "Unknown package").applicationInfo.uid;
1078
1079        // Set up LauncherApps for this caller.
1080        final Pair<Integer, String> key = Pair.create(userId, packageName);
1081        if (!mLauncherAppsMap.containsKey(key)) {
1082            mLauncherAppsMap.put(key, new LauncherAppsTestable(mClientContext, mLauncherAppImpl));
1083        }
1084        mLauncherApps = mLauncherAppsMap.get(key);
1085    }
1086
1087    protected void setCaller(String packageName) {
1088        setCaller(packageName, UserHandle.USER_SYSTEM);
1089    }
1090
1091    protected String getCallingPackage() {
1092        return mInjectedClientPackage;
1093    }
1094
1095    protected void setDefaultLauncherChecker(BiPredicate<String, Integer> p) {
1096        mDefaultLauncherChecker = p;
1097    }
1098
1099    protected void runWithCaller(String packageName, int userId, Runnable r) {
1100        final String previousPackage = mInjectedClientPackage;
1101        final int previousUserId = UserHandle.getUserId(mInjectedCallingUid);
1102
1103        setCaller(packageName, userId);
1104
1105        r.run();
1106
1107        setCaller(previousPackage, previousUserId);
1108    }
1109
1110    protected void runWithSystemUid(Runnable r) {
1111        final int origUid = mInjectedCallingUid;
1112        mInjectedCallingUid = Process.SYSTEM_UID;
1113        r.run();
1114        mInjectedCallingUid = origUid;
1115    }
1116
1117    protected void lookupAndFillInResourceNames(ShortcutInfo si) {
1118        runWithSystemUid(() -> si.lookupAndFillInResourceNames(
1119                mService.injectGetResourcesForApplicationAsUser(si.getPackage(), si.getUserId())));
1120    }
1121
1122    protected int getCallingUserId() {
1123        return UserHandle.getUserId(mInjectedCallingUid);
1124    }
1125
1126    protected UserHandle getCallingUser() {
1127        return UserHandle.of(getCallingUserId());
1128    }
1129
1130    /** For debugging */
1131    protected void dumpsysOnLogcat() {
1132        dumpsysOnLogcat("");
1133    }
1134
1135    protected void dumpsysOnLogcat(String message) {
1136        dumpsysOnLogcat(message, false);
1137    }
1138
1139    protected void dumpsysOnLogcat(String message, boolean force) {
1140        if (force || !ENABLE_DUMP) return;
1141
1142        Log.v(TAG, "Dumping ShortcutService: " + message);
1143        for (String line : dumpsys(null).split("\n")) {
1144            Log.v(TAG, line);
1145        }
1146    }
1147
1148    protected String dumpCheckin() {
1149        return dumpsys(new String[]{"--checkin"});
1150    }
1151
1152    private String dumpsys(String[] args) {
1153        final ArrayList<String> origPermissions = new ArrayList<>(mCallerPermissions);
1154        mCallerPermissions.add(android.Manifest.permission.DUMP);
1155        try {
1156            final ByteArrayOutputStream out = new ByteArrayOutputStream();
1157            final PrintWriter pw = new PrintWriter(out);
1158            mService.dump(/* fd */ null, pw, args);
1159            pw.close();
1160
1161            return out.toString();
1162        } finally {
1163            mCallerPermissions.clear();
1164            mCallerPermissions.addAll(origPermissions);
1165        }
1166    }
1167
1168    /**
1169     * For debugging, dump arbitrary file on logcat.
1170     */
1171    protected void dumpFileOnLogcat(String path) {
1172        dumpFileOnLogcat(path, "");
1173    }
1174
1175    protected void dumpFileOnLogcat(String path, String message) {
1176        if (!ENABLE_DUMP) return;
1177
1178        Log.v(TAG, "Dumping file: " + path + " " + message);
1179        final StringBuilder sb = new StringBuilder();
1180        try (BufferedReader br = new BufferedReader(new FileReader(path))) {
1181            String line;
1182            while ((line = br.readLine()) != null) {
1183                Log.v(TAG, line);
1184            }
1185        } catch (Exception e) {
1186            Log.e(TAG, "Couldn't read file", e);
1187            fail("Exception " + e);
1188        }
1189    }
1190
1191    /**
1192     * For debugging, dump the main state file on logcat.
1193     */
1194    protected void dumpBaseStateFile() {
1195        mService.saveDirtyInfo();
1196        dumpFileOnLogcat(mInjectedFilePathRoot.getAbsolutePath()
1197                + "/system/" + ShortcutService.FILENAME_BASE_STATE);
1198    }
1199
1200    /**
1201     * For debugging, dump per-user state file on logcat.
1202     */
1203    protected void dumpUserFile(int userId) {
1204        dumpUserFile(userId, "");
1205    }
1206
1207    protected void dumpUserFile(int userId, String message) {
1208        mService.saveDirtyInfo();
1209        dumpFileOnLogcat(mInjectedFilePathRoot.getAbsolutePath()
1210                + "/user-" + userId
1211                + "/" + ShortcutService.FILENAME_USER_PACKAGES, message);
1212    }
1213
1214    /**
1215     * Make a shortcut with an ID.
1216     */
1217    protected ShortcutInfo makeShortcut(String id) {
1218        return makeShortcut(
1219                id, "Title-" + id, /* activity =*/ null, /* icon =*/ null,
1220                makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
1221    }
1222
1223    protected ShortcutInfo makeShortcutWithTitle(String id, String title) {
1224        return makeShortcut(
1225                id, title, /* activity =*/ null, /* icon =*/ null,
1226                makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
1227    }
1228
1229    /**
1230     * Make a shortcut with an ID and timestamp.
1231     */
1232    protected ShortcutInfo makeShortcutWithTimestamp(String id, long timestamp) {
1233        final ShortcutInfo s = makeShortcut(
1234                id, "Title-" + id, /* activity =*/ null, /* icon =*/ null,
1235                makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
1236        s.setTimestamp(timestamp);
1237        return s;
1238    }
1239
1240    /**
1241     * Make a shortcut with an ID, a timestamp and an activity component
1242     */
1243    protected ShortcutInfo makeShortcutWithTimestampWithActivity(String id, long timestamp,
1244            ComponentName activity) {
1245        final ShortcutInfo s = makeShortcut(
1246                id, "Title-" + id, activity, /* icon =*/ null,
1247                makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
1248        s.setTimestamp(timestamp);
1249        return s;
1250    }
1251
1252    /**
1253     * Make a shortcut with an ID and icon.
1254     */
1255    protected ShortcutInfo makeShortcutWithIcon(String id, Icon icon) {
1256        return makeShortcut(
1257                id, "Title-" + id, /* activity =*/ null, icon,
1258                makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
1259    }
1260
1261    protected ShortcutInfo makePackageShortcut(String packageName, String id) {
1262        String origCaller = getCallingPackage();
1263
1264        setCaller(packageName);
1265        ShortcutInfo s = makeShortcut(
1266                id, "Title-" + id, /* activity =*/ null, /* icon =*/ null,
1267                makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
1268        setCaller(origCaller); // restore the caller
1269
1270        return s;
1271    }
1272
1273    /**
1274     * Make multiple shortcuts with IDs.
1275     */
1276    protected List<ShortcutInfo> makeShortcuts(String... ids) {
1277        final ArrayList<ShortcutInfo> ret = new ArrayList();
1278        for (String id : ids) {
1279            ret.add(makeShortcut(id));
1280        }
1281        return ret;
1282    }
1283
1284    protected ShortcutInfo.Builder makeShortcutBuilder() {
1285        return new ShortcutInfo.Builder(mClientContext);
1286    }
1287
1288    protected ShortcutInfo makeShortcutWithActivity(String id, ComponentName activity) {
1289        return makeShortcut(
1290                id, "Title-" + id, activity, /* icon =*/ null,
1291                makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
1292    }
1293
1294    protected ShortcutInfo makeShortcutWithIntent(String id, Intent intent) {
1295        return makeShortcut(
1296                id, "Title-" + id, /* activity =*/ null, /* icon =*/ null,
1297                intent, /* rank =*/ 0);
1298    }
1299
1300    protected ShortcutInfo makeShortcutWithActivityAndTitle(String id, ComponentName activity,
1301            String title) {
1302        return makeShortcut(
1303                id, title, activity, /* icon =*/ null,
1304                makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
1305    }
1306
1307    protected ShortcutInfo makeShortcutWithActivityAndRank(String id, ComponentName activity,
1308            int rank) {
1309        return makeShortcut(
1310                id, "Title-" + id, activity, /* icon =*/ null,
1311                makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), rank);
1312    }
1313
1314    /**
1315     * Make a shortcut with details.
1316     */
1317    protected ShortcutInfo makeShortcut(String id, String title, ComponentName activity,
1318            Icon icon, Intent intent, int rank) {
1319        final ShortcutInfo.Builder  b = new ShortcutInfo.Builder(mClientContext, id)
1320                .setActivity(new ComponentName(mClientContext.getPackageName(), "dummy"))
1321                .setShortLabel(title)
1322                .setRank(rank)
1323                .setIntent(intent);
1324        if (icon != null) {
1325            b.setIcon(icon);
1326        }
1327        if (activity != null) {
1328            b.setActivity(activity);
1329        }
1330        final ShortcutInfo s = b.build();
1331
1332        s.setTimestamp(mInjectedCurrentTimeMillis); // HACK
1333
1334        return s;
1335    }
1336
1337    protected ShortcutInfo makeShortcutWithIntents(String id, Intent... intents) {
1338        return makeShortcut(
1339                id, "Title-" + id, /* activity =*/ null, /* icon =*/ null,
1340                intents, /* rank =*/ 0);
1341    }
1342
1343    /**
1344     * Make a shortcut with details.
1345     */
1346    protected ShortcutInfo makeShortcut(String id, String title, ComponentName activity,
1347            Icon icon, Intent[] intents, int rank) {
1348        final ShortcutInfo.Builder  b = new ShortcutInfo.Builder(mClientContext, id)
1349                .setActivity(new ComponentName(mClientContext.getPackageName(), "dummy"))
1350                .setShortLabel(title)
1351                .setRank(rank)
1352                .setIntents(intents);
1353        if (icon != null) {
1354            b.setIcon(icon);
1355        }
1356        if (activity != null) {
1357            b.setActivity(activity);
1358        }
1359        final ShortcutInfo s = b.build();
1360
1361        s.setTimestamp(mInjectedCurrentTimeMillis); // HACK
1362
1363        return s;
1364    }
1365
1366    /**
1367     * Make a shortcut with details.
1368     */
1369    protected ShortcutInfo makeShortcutWithExtras(String id, Intent intent,
1370            PersistableBundle extras) {
1371        final ShortcutInfo.Builder  b = new ShortcutInfo.Builder(mClientContext, id)
1372                .setActivity(new ComponentName(mClientContext.getPackageName(), "dummy"))
1373                .setShortLabel("title-" + id)
1374                .setExtras(extras)
1375                .setIntent(intent);
1376        final ShortcutInfo s = b.build();
1377
1378        s.setTimestamp(mInjectedCurrentTimeMillis); // HACK
1379
1380        return s;
1381    }
1382
1383    /**
1384     * Make an intent.
1385     */
1386    protected Intent makeIntent(String action, Class<?> clazz, Object... bundleKeysAndValues) {
1387        final Intent intent = new Intent(action);
1388        intent.setComponent(makeComponent(clazz));
1389        intent.replaceExtras(makeBundle(bundleKeysAndValues));
1390        return intent;
1391    }
1392
1393    /**
1394     * Make an component name, with the client context.
1395     */
1396    @NonNull
1397    protected ComponentName makeComponent(Class<?> clazz) {
1398        return new ComponentName(mClientContext, clazz);
1399    }
1400
1401    @NonNull
1402    protected ShortcutInfo findById(List<ShortcutInfo> list, String id) {
1403        for (ShortcutInfo s : list) {
1404            if (s.getId().equals(id)) {
1405                return s;
1406            }
1407        }
1408        fail("Shortcut with id " + id + " not found");
1409        return null;
1410    }
1411
1412    protected void assertSystem() {
1413        assertEquals("Caller must be system", Process.SYSTEM_UID, mInjectedCallingUid);
1414    }
1415
1416    protected void assertResetTimes(long expectedLastResetTime, long expectedNextResetTime) {
1417        assertEquals(expectedLastResetTime, mService.getLastResetTimeLocked());
1418        assertEquals(expectedNextResetTime, mService.getNextResetTimeLocked());
1419    }
1420
1421    public static List<ShortcutInfo> assertAllNotHaveIcon(
1422            List<ShortcutInfo> actualShortcuts) {
1423        for (ShortcutInfo s : actualShortcuts) {
1424            assertNull("ID " + s.getId(), s.getIcon());
1425        }
1426        return actualShortcuts;
1427    }
1428
1429    @NonNull
1430    protected List<ShortcutInfo> assertAllHaveFlags(@NonNull List<ShortcutInfo> actualShortcuts,
1431            int shortcutFlags) {
1432        for (ShortcutInfo s : actualShortcuts) {
1433            assertTrue("ID " + s.getId() + " doesn't have flags " + shortcutFlags,
1434                    s.hasFlags(shortcutFlags));
1435        }
1436        return actualShortcuts;
1437    }
1438
1439    protected ShortcutInfo getPackageShortcut(String packageName, String shortcutId, int userId) {
1440        return mService.getPackageShortcutForTest(packageName, shortcutId, userId);
1441    }
1442
1443    protected void assertShortcutExists(String packageName, String shortcutId, int userId) {
1444        assertTrue(getPackageShortcut(packageName, shortcutId, userId) != null);
1445    }
1446
1447    protected void assertShortcutNotExists(String packageName, String shortcutId, int userId) {
1448        assertTrue(getPackageShortcut(packageName, shortcutId, userId) == null);
1449    }
1450
1451    protected Intent[] launchShortcutAndGetIntentsInner(Runnable shortcutStarter,
1452            @NonNull String packageName, @NonNull String shortcutId, int userId) {
1453        reset(mMockActivityManagerInternal);
1454        shortcutStarter.run();
1455
1456        final ArgumentCaptor<Intent[]> intentsCaptor = ArgumentCaptor.forClass(Intent[].class);
1457        verify(mMockActivityManagerInternal).startActivitiesAsPackage(
1458                eq(packageName),
1459                eq(userId),
1460                intentsCaptor.capture(),
1461                any(Bundle.class));
1462        return intentsCaptor.getValue();
1463    }
1464
1465    protected Intent[] launchShortcutAndGetIntents(
1466            @NonNull String packageName, @NonNull String shortcutId, int userId) {
1467        return launchShortcutAndGetIntentsInner(
1468                () -> {
1469                    mLauncherApps.startShortcut(packageName, shortcutId, null, null,
1470                            UserHandle.of(userId));
1471                }, packageName, shortcutId, userId
1472        );
1473    }
1474
1475    protected Intent launchShortcutAndGetIntent(
1476            @NonNull String packageName, @NonNull String shortcutId, int userId) {
1477        final Intent[] intents = launchShortcutAndGetIntents(packageName, shortcutId, userId);
1478        assertEquals(1, intents.length);
1479        return intents[0];
1480    }
1481
1482    protected Intent[] launchShortcutAndGetIntents_withShortcutInfo(
1483            @NonNull String packageName, @NonNull String shortcutId, int userId) {
1484        return launchShortcutAndGetIntentsInner(
1485                () -> {
1486                    mLauncherApps.startShortcut(
1487                            getShortcutInfoAsLauncher(packageName, shortcutId, userId), null, null);
1488                }, packageName, shortcutId, userId
1489        );
1490    }
1491
1492    protected Intent launchShortcutAndGetIntent_withShortcutInfo(
1493            @NonNull String packageName, @NonNull String shortcutId, int userId) {
1494        final Intent[] intents = launchShortcutAndGetIntents_withShortcutInfo(
1495                packageName, shortcutId, userId);
1496        assertEquals(1, intents.length);
1497        return intents[0];
1498    }
1499
1500    protected void assertShortcutLaunchable(@NonNull String packageName, @NonNull String shortcutId,
1501            int userId) {
1502        assertNotNull(launchShortcutAndGetIntent(packageName, shortcutId, userId));
1503        assertNotNull(launchShortcutAndGetIntent_withShortcutInfo(packageName, shortcutId, userId));
1504    }
1505
1506    protected void assertShortcutNotLaunched(@NonNull String packageName,
1507            @NonNull String shortcutId, int userId) {
1508        reset(mMockActivityManagerInternal);
1509        try {
1510            mLauncherApps.startShortcut(packageName, shortcutId, null, null,
1511                    UserHandle.of(userId));
1512            fail("ActivityNotFoundException was not thrown");
1513        } catch (ActivityNotFoundException expected) {
1514        }
1515        // This shouldn't have been called.
1516        verify(mMockActivityManagerInternal, times(0)).startActivitiesAsPackage(
1517                anyString(),
1518                anyInt(),
1519                any(Intent[].class),
1520                any(Bundle.class));
1521    }
1522
1523    protected void assertStartShortcutThrowsException(@NonNull String packageName,
1524            @NonNull String shortcutId, int userId, Class<?> expectedException) {
1525        Exception thrown = null;
1526        try {
1527            mLauncherApps.startShortcut(packageName, shortcutId, null, null,
1528                    UserHandle.of(userId));
1529        } catch (Exception e) {
1530            thrown = e;
1531        }
1532        assertNotNull("Exception was not thrown", thrown);
1533        assertEquals("Exception type different", expectedException, thrown.getClass());
1534    }
1535
1536    protected void assertBitmapDirectories(int userId, String... expectedDirectories) {
1537        final Set<String> expected = hashSet(set(expectedDirectories));
1538
1539        final Set<String> actual = new HashSet<>();
1540
1541        final File[] files = mService.getUserBitmapFilePath(userId).listFiles();
1542        if (files != null) {
1543            for (File child : files) {
1544                if (child.isDirectory()) {
1545                    actual.add(child.getName());
1546                }
1547            }
1548        }
1549
1550        assertEquals(expected, actual);
1551    }
1552
1553    protected void assertBitmapFiles(int userId, String packageName, String... expectedFiles) {
1554        final Set<String> expected = hashSet(set(expectedFiles));
1555
1556        final Set<String> actual = new HashSet<>();
1557
1558        final File[] files = new File(mService.getUserBitmapFilePath(userId), packageName)
1559                .listFiles();
1560        if (files != null) {
1561            for (File child : files) {
1562                if (child.isFile()) {
1563                    actual.add(child.getName());
1564                }
1565            }
1566        }
1567
1568        assertEquals(expected, actual);
1569    }
1570
1571    protected String getBitmapFilename(int userId, String packageName, String shortcutId) {
1572        final ShortcutInfo si = mService.getPackageShortcutForTest(packageName, shortcutId, userId);
1573        if (si == null) {
1574            return null;
1575        }
1576        return new File(si.getBitmapPath()).getName();
1577    }
1578
1579    /**
1580     * @return all shortcuts stored internally for the caller.  This reflects the *internal* view
1581     * of shortcuts, which may be different from what {@link #getCallerVisibleShortcuts} would
1582     * return, because getCallerVisibleShortcuts() will get shortcuts from the proper "front door"
1583     * which performs some extra checks, like {@link ShortcutPackage#onRestored}.
1584     */
1585    protected List<ShortcutInfo> getCallerShortcuts() {
1586        final ShortcutPackage p = mService.getPackageShortcutForTest(
1587                getCallingPackage(), getCallingUserId());
1588        return p == null ? null : p.getAllShortcutsForTest();
1589    }
1590
1591    /**
1592     * @return all shortcuts owned by caller that are actually visible via ShortcutManager.
1593     * See also {@link #getCallerShortcuts}.
1594     */
1595    protected List<ShortcutInfo> getCallerVisibleShortcuts() {
1596        final ArrayList<ShortcutInfo> ret = new ArrayList<>();
1597        ret.addAll(mManager.getDynamicShortcuts());
1598        ret.addAll(mManager.getPinnedShortcuts());
1599        ret.addAll(mManager.getManifestShortcuts());
1600        return ret;
1601    }
1602
1603    protected ShortcutInfo getCallerShortcut(String shortcutId) {
1604        return getPackageShortcut(getCallingPackage(), shortcutId, getCallingUserId());
1605    }
1606
1607    protected List<ShortcutInfo> getLauncherShortcuts(String launcher, int userId, int queryFlags) {
1608        final List<ShortcutInfo>[] ret = new List[1];
1609        runWithCaller(launcher, userId, () -> {
1610            final ShortcutQuery q = new ShortcutQuery();
1611            q.setQueryFlags(queryFlags);
1612            ret[0] = mLauncherApps.getShortcuts(q, UserHandle.of(userId));
1613        });
1614        return ret[0];
1615    }
1616
1617    protected List<ShortcutInfo> getLauncherPinnedShortcuts(String launcher, int userId) {
1618        return getLauncherShortcuts(launcher, userId, ShortcutQuery.FLAG_GET_PINNED);
1619    }
1620
1621    protected ShortcutInfo getShortcutInfoAsLauncher(String packageName, String shortcutId,
1622            int userId) {
1623        final List<ShortcutInfo> infoList =
1624                mLauncherApps.getShortcutInfo(packageName, list(shortcutId),
1625                        UserHandle.of(userId));
1626        assertEquals("No shortcutInfo found (or too many of them)", 1, infoList.size());
1627        return infoList.get(0);
1628    }
1629
1630    protected Intent genPackageAddIntent(String packageName, int userId) {
1631        installPackage(userId, packageName);
1632
1633        Intent i = new Intent(Intent.ACTION_PACKAGE_ADDED);
1634        i.setData(Uri.parse("package:" + packageName));
1635        i.putExtra(Intent.EXTRA_USER_HANDLE, userId);
1636        return i;
1637    }
1638
1639    protected Intent genPackageDeleteIntent(String pakcageName, int userId) {
1640        uninstallPackage(userId, pakcageName);
1641
1642        Intent i = new Intent(Intent.ACTION_PACKAGE_REMOVED);
1643        i.setData(Uri.parse("package:" + pakcageName));
1644        i.putExtra(Intent.EXTRA_USER_HANDLE, userId);
1645        return i;
1646    }
1647
1648    protected Intent genPackageUpdateIntent(String pakcageName, int userId) {
1649        installPackage(userId, pakcageName);
1650
1651        Intent i = new Intent(Intent.ACTION_PACKAGE_ADDED);
1652        i.setData(Uri.parse("package:" + pakcageName));
1653        i.putExtra(Intent.EXTRA_USER_HANDLE, userId);
1654        i.putExtra(Intent.EXTRA_REPLACING, true);
1655        return i;
1656    }
1657
1658    protected Intent genPackageChangedIntent(String pakcageName, int userId) {
1659        Intent i = new Intent(Intent.ACTION_PACKAGE_CHANGED);
1660        i.setData(Uri.parse("package:" + pakcageName));
1661        i.putExtra(Intent.EXTRA_USER_HANDLE, userId);
1662        return i;
1663    }
1664
1665    protected Intent genPackageDataClear(String packageName, int userId) {
1666        Intent i = new Intent(Intent.ACTION_PACKAGE_DATA_CLEARED);
1667        i.setData(Uri.parse("package:" + packageName));
1668        i.putExtra(Intent.EXTRA_USER_HANDLE, userId);
1669        return i;
1670    }
1671
1672    protected void assertExistsAndShadow(ShortcutPackageItem spi) {
1673        assertNotNull(spi);
1674        assertTrue(spi.getPackageInfo().isShadow());
1675    }
1676
1677    protected File makeFile(File baseDirectory, String... paths) {
1678        File ret = baseDirectory;
1679
1680        for (String path : paths) {
1681            ret = new File(ret, path);
1682        }
1683
1684        return ret;
1685    }
1686
1687    protected boolean bitmapDirectoryExists(String packageName, int userId) {
1688        final File path = new File(mService.getUserBitmapFilePath(userId), packageName);
1689        return path.isDirectory();
1690    }
1691    protected static ShortcutQuery buildQuery(long changedSince,
1692            String packageName, ComponentName componentName,
1693            /* @ShortcutQuery.QueryFlags */ int flags) {
1694        return buildQuery(changedSince, packageName, null, componentName, flags);
1695    }
1696
1697    protected static ShortcutQuery buildQuery(long changedSince,
1698            String packageName, List<String> shortcutIds, ComponentName componentName,
1699            /* @ShortcutQuery.QueryFlags */ int flags) {
1700        final ShortcutQuery q = new ShortcutQuery();
1701        q.setChangedSince(changedSince);
1702        q.setPackage(packageName);
1703        q.setShortcutIds(shortcutIds);
1704        q.setActivity(componentName);
1705        q.setQueryFlags(flags);
1706        return q;
1707    }
1708
1709    protected static ShortcutQuery buildAllQuery(String packageName) {
1710        final ShortcutQuery q = new ShortcutQuery();
1711        q.setPackage(packageName);
1712        q.setQueryFlags(ShortcutQuery.FLAG_GET_ALL_KINDS);
1713        return q;
1714    }
1715
1716    protected static ShortcutQuery buildPinnedQuery(String packageName) {
1717        final ShortcutQuery q = new ShortcutQuery();
1718        q.setPackage(packageName);
1719        q.setQueryFlags(ShortcutQuery.FLAG_GET_PINNED);
1720        return q;
1721    }
1722
1723    protected static ShortcutQuery buildQueryWithFlags(int queryFlags) {
1724        final ShortcutQuery q = new ShortcutQuery();
1725        q.setQueryFlags(queryFlags);
1726        return q;
1727    }
1728
1729    protected void backupAndRestore() {
1730        int prevUid = mInjectedCallingUid;
1731
1732        mInjectedCallingUid = Process.SYSTEM_UID; // Only system can call it.
1733
1734        dumpsysOnLogcat("Before backup");
1735
1736        final byte[] payload =  mService.getBackupPayload(USER_0);
1737        if (ENABLE_DUMP) {
1738            final String xml = new String(payload);
1739            Log.v(TAG, "Backup payload:");
1740            for (String line : xml.split("\n")) {
1741                Log.v(TAG, line);
1742            }
1743        }
1744
1745        // Before doing anything else, uninstall all packages.
1746        for (int userId : list(USER_0, USER_P0)) {
1747            for (String pkg : list(CALLING_PACKAGE_1, CALLING_PACKAGE_2, CALLING_PACKAGE_3,
1748                    LAUNCHER_1, LAUNCHER_2, LAUNCHER_3)) {
1749                uninstallPackage(userId, pkg);
1750            }
1751        }
1752
1753        shutdownServices();
1754
1755        deleteAllSavedFiles();
1756
1757        initService();
1758        mService.applyRestore(payload, USER_0);
1759
1760        // handleUnlockUser will perform the gone package check, but it shouldn't remove
1761        // shadow information.
1762        mService.handleUnlockUser(USER_0);
1763
1764        dumpsysOnLogcat("After restore");
1765
1766        mInjectedCallingUid = prevUid;
1767    }
1768
1769    protected void prepareCrossProfileDataSet() {
1770        mRunningUsers.put(USER_10, true); // this test needs user 10.
1771
1772        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1773            assertTrue(mManager.setDynamicShortcuts(list(
1774                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"),
1775                    makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6"))));
1776        });
1777        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
1778            assertTrue(mManager.setDynamicShortcuts(list(
1779                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"),
1780                    makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6"))));
1781        });
1782        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
1783            assertTrue(mManager.setDynamicShortcuts(list(
1784                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"),
1785                    makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6"))));
1786        });
1787        runWithCaller(CALLING_PACKAGE_4, USER_0, () -> {
1788            assertTrue(mManager.setDynamicShortcuts(list()));
1789        });
1790        runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
1791            assertTrue(mManager.setDynamicShortcuts(list(
1792                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"),
1793                    makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6"))));
1794        });
1795        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
1796            assertTrue(mManager.setDynamicShortcuts(list(
1797                    makeShortcut("x1"), makeShortcut("x2"), makeShortcut("x3"),
1798                    makeShortcut("x4"), makeShortcut("x5"), makeShortcut("x6"))));
1799        });
1800
1801        runWithCaller(LAUNCHER_1, USER_0, () -> {
1802            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s1"), HANDLE_USER_0);
1803            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s1", "s2"), HANDLE_USER_0);
1804            mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("s1", "s2", "s3"), HANDLE_USER_0);
1805
1806            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s1", "s4"), HANDLE_USER_P0);
1807        });
1808        runWithCaller(LAUNCHER_2, USER_0, () -> {
1809            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s2"), HANDLE_USER_0);
1810            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s2", "s3"), HANDLE_USER_0);
1811            mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("s2", "s3", "s4"), HANDLE_USER_0);
1812
1813            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s2", "s5"), HANDLE_USER_P0);
1814        });
1815
1816        // Note LAUNCHER_3 has allowBackup=false.
1817        runWithCaller(LAUNCHER_3, USER_0, () -> {
1818            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s3"), HANDLE_USER_0);
1819            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s3", "s4"), HANDLE_USER_0);
1820            mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("s3", "s4", "s5"), HANDLE_USER_0);
1821
1822            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s3", "s6"), HANDLE_USER_P0);
1823        });
1824        runWithCaller(LAUNCHER_4, USER_0, () -> {
1825            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list(), HANDLE_USER_0);
1826            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list(), HANDLE_USER_0);
1827            mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list(), HANDLE_USER_0);
1828            mLauncherApps.pinShortcuts(CALLING_PACKAGE_4, list(), HANDLE_USER_0);
1829        });
1830
1831        // Launcher on a managed profile is referring ot user 0!
1832        runWithCaller(LAUNCHER_1, USER_P0, () -> {
1833            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s3", "s4"), HANDLE_USER_0);
1834            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s3", "s4", "s5"), HANDLE_USER_0);
1835            mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("s3", "s4", "s5", "s6"),
1836                    HANDLE_USER_0);
1837
1838            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s4", "s1"), HANDLE_USER_P0);
1839        });
1840        runWithCaller(LAUNCHER_1, USER_10, () -> {
1841            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("x4", "x5"), HANDLE_USER_10);
1842            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("x4", "x5", "x6"), HANDLE_USER_10);
1843            mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("x4", "x5", "x6", "x1"),
1844                    HANDLE_USER_10);
1845        });
1846
1847        // Then remove some dynamic shortcuts.
1848        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1849            assertTrue(mManager.setDynamicShortcuts(list(
1850                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
1851        });
1852        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
1853            assertTrue(mManager.setDynamicShortcuts(list(
1854                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
1855        });
1856        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
1857            assertTrue(mManager.setDynamicShortcuts(list(
1858                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
1859        });
1860        runWithCaller(CALLING_PACKAGE_4, USER_0, () -> {
1861            assertTrue(mManager.setDynamicShortcuts(list()));
1862        });
1863        runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
1864            assertTrue(mManager.setDynamicShortcuts(list(
1865                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
1866        });
1867        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
1868            assertTrue(mManager.setDynamicShortcuts(list(
1869                    makeShortcut("x1"), makeShortcut("x2"), makeShortcut("x3"))));
1870        });
1871    }
1872
1873    public static List<ShortcutInfo> assertAllHaveIconResId(
1874            List<ShortcutInfo> actualShortcuts) {
1875        for (ShortcutInfo s : actualShortcuts) {
1876            assertTrue("ID " + s.getId() + " not have icon res ID", s.hasIconResource());
1877            assertFalse("ID " + s.getId() + " shouldn't have icon FD", s.hasIconFile());
1878        }
1879        return actualShortcuts;
1880    }
1881
1882    public static List<ShortcutInfo> assertAllHaveIconFile(
1883            List<ShortcutInfo> actualShortcuts) {
1884        for (ShortcutInfo s : actualShortcuts) {
1885            assertFalse("ID " + s.getId() + " shouldn't have icon res ID", s.hasIconResource());
1886            assertTrue("ID " + s.getId() + " not have icon FD", s.hasIconFile());
1887        }
1888        return actualShortcuts;
1889    }
1890
1891    public static List<ShortcutInfo> assertAllHaveIcon(
1892            List<ShortcutInfo> actualShortcuts) {
1893        for (ShortcutInfo s : actualShortcuts) {
1894            assertTrue("ID " + s.getId() + " has no icon ", s.hasIconFile() || s.hasIconResource());
1895        }
1896        return actualShortcuts;
1897    }
1898
1899    public static List<ShortcutInfo> assertAllStringsResolved(
1900            List<ShortcutInfo> actualShortcuts) {
1901        for (ShortcutInfo s : actualShortcuts) {
1902            assertTrue("ID " + s.getId(), s.hasStringResourcesResolved());
1903        }
1904        return actualShortcuts;
1905    }
1906
1907    public String readTestAsset(String assetPath) throws IOException {
1908        final StringBuilder sb = new StringBuilder();
1909        try (BufferedReader br = new BufferedReader(
1910                new InputStreamReader(
1911                        getTestContext().getResources().getAssets().open(assetPath)))) {
1912            String line;
1913            while ((line = br.readLine()) != null) {
1914                sb.append(line);
1915                sb.append(System.lineSeparator());
1916            }
1917        }
1918        return sb.toString();
1919    }
1920
1921    protected void prepareGetHomeActivitiesAsUser(ComponentName preferred,
1922            List<ResolveInfo> candidates, int userId) {
1923        doAnswer(inv -> {
1924            ((List) inv.getArguments()[0]).addAll(candidates);
1925            return preferred;
1926        }).when(mMockPackageManagerInternal).getHomeActivitiesAsUser(any(List.class), eq(userId));
1927    }
1928
1929    protected static ComponentName cn(String packageName, String name) {
1930        return new ComponentName(packageName, name);
1931    }
1932
1933    protected static ResolveInfo ri(String packageName, String name, boolean isSystem, int priority) {
1934        final ResolveInfo ri = new ResolveInfo();
1935        ri.activityInfo = new ActivityInfo();
1936        ri.activityInfo.applicationInfo = new ApplicationInfo();
1937
1938        ri.activityInfo.packageName = packageName;
1939        ri.activityInfo.name = name;
1940        if (isSystem) {
1941            ri.activityInfo.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
1942        }
1943        ri.priority = priority;
1944        return ri;
1945    }
1946
1947    protected static ResolveInfo getSystemLauncher() {
1948        return ri(PACKAGE_SYSTEM_LAUNCHER, PACKAGE_SYSTEM_LAUNCHER_NAME, true,
1949                PACKAGE_SYSTEM_LAUNCHER_PRIORITY);
1950    }
1951
1952    protected static ResolveInfo getFallbackLauncher() {
1953        return ri(PACKAGE_FALLBACK_LAUNCHER, PACKAGE_FALLBACK_LAUNCHER_NAME, true,
1954                PACKAGE_FALLBACK_LAUNCHER_PRIORITY);
1955    }
1956}
1957