ShortcutManagerTest1.java revision 3fa345b5c2cf5d7fd8c87e44d1887f011787c548
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.assertAllDisabled;
19import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllDynamic;
20import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllDynamicOrPinned;
21import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllEnabled;
22import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllHaveIntents;
23import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllHaveTitle;
24import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllImmutable;
25import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllKeyFieldsOnly;
26import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllManifest;
27import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllNotHaveIntents;
28import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllNotHaveTitle;
29import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllNotKeyFieldsOnly;
30import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllNotManifest;
31import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllPinned;
32import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllUnique;
33import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertBitmapSize;
34import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertBundleEmpty;
35import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertCallbackNotReceived;
36import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertCallbackReceived;
37import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertCannotUpdateImmutable;
38import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertDynamicAndPinned;
39import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertDynamicOnly;
40import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertDynamicShortcutCountExceeded;
41import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertEmpty;
42import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertExpectException;
43import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertForLauncherCallback;
44import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertShortcutIds;
45import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertWith;
46import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.filterByActivity;
47import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.findShortcut;
48import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.hashSet;
49import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.list;
50import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.makeBundle;
51import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.pfdToBitmap;
52import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.resetAll;
53import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.set;
54import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.waitOnMainThread;
55
56import static org.mockito.Matchers.any;
57import static org.mockito.Matchers.anyInt;
58import static org.mockito.Matchers.anyString;
59import static org.mockito.Matchers.eq;
60import static org.mockito.Mockito.doAnswer;
61import static org.mockito.Mockito.doReturn;
62import static org.mockito.Mockito.mock;
63import static org.mockito.Mockito.reset;
64import static org.mockito.Mockito.times;
65import static org.mockito.Mockito.verify;
66import static org.mockito.Mockito.when;
67
68import android.Manifest.permission;
69import android.app.ActivityManager;
70import android.content.ActivityNotFoundException;
71import android.content.ComponentName;
72import android.content.Intent;
73import android.content.pm.ApplicationInfo;
74import android.content.pm.LauncherApps;
75import android.content.pm.LauncherApps.ShortcutQuery;
76import android.content.pm.ShortcutInfo;
77import android.graphics.Bitmap;
78import android.graphics.Bitmap.CompressFormat;
79import android.graphics.BitmapFactory;
80import android.graphics.drawable.Icon;
81import android.net.Uri;
82import android.os.Bundle;
83import android.os.Handler;
84import android.os.Looper;
85import android.os.UserHandle;
86import android.test.suitebuilder.annotation.SmallTest;
87import android.util.Log;
88import android.util.SparseArray;
89
90import com.android.frameworks.servicestests.R;
91import com.android.server.pm.ShortcutService.ConfigConstants;
92import com.android.server.pm.ShortcutService.FileOutputStreamWithPath;
93import com.android.server.pm.ShortcutUser.PackageWithUser;
94
95import org.mockito.ArgumentCaptor;
96
97import java.io.File;
98import java.io.IOException;
99import java.util.List;
100import java.util.Locale;
101
102/**
103 * Tests for ShortcutService and ShortcutManager.
104 *
105 m FrameworksServicesTests &&
106 adb install \
107 -r -g ${ANDROID_PRODUCT_OUT}/data/app/FrameworksServicesTests/FrameworksServicesTests.apk &&
108 adb shell am instrument -e class com.android.server.pm.ShortcutManagerTest1 \
109 -w com.android.frameworks.servicestests/android.support.test.runner.AndroidJUnitRunner
110 */
111@SmallTest
112public class ShortcutManagerTest1 extends BaseShortcutManagerTest {
113
114    /**
115     * Test for the first launch path, no settings file available.
116     */
117    public void testFirstInitialize() {
118        assertResetTimes(START_TIME, START_TIME + INTERVAL);
119    }
120
121    /**
122     * Test for {@link ShortcutService#getLastResetTimeLocked()} and
123     * {@link ShortcutService#getNextResetTimeLocked()}.
124     */
125    public void testUpdateAndGetNextResetTimeLocked() {
126        assertResetTimes(START_TIME, START_TIME + INTERVAL);
127
128        // Advance clock.
129        mInjectedCurrentTimeMillis += 100;
130
131        // Shouldn't have changed.
132        assertResetTimes(START_TIME, START_TIME + INTERVAL);
133
134        // Advance clock, almost the reset time.
135        mInjectedCurrentTimeMillis = START_TIME + INTERVAL - 1;
136
137        // Shouldn't have changed.
138        assertResetTimes(START_TIME, START_TIME + INTERVAL);
139
140        // Advance clock.
141        mInjectedCurrentTimeMillis += 1;
142
143        assertResetTimes(START_TIME + INTERVAL, START_TIME + 2 * INTERVAL);
144
145        // Advance further; 4 hours since start.
146        mInjectedCurrentTimeMillis = START_TIME + 4 * INTERVAL + 50;
147
148        assertResetTimes(START_TIME + 4 * INTERVAL, START_TIME + 5 * INTERVAL);
149    }
150
151    /**
152     * Test for the restoration from saved file.
153     */
154    public void testInitializeFromSavedFile() {
155
156        mInjectedCurrentTimeMillis = START_TIME + 4 * INTERVAL + 50;
157        assertResetTimes(START_TIME + 4 * INTERVAL, START_TIME + 5 * INTERVAL);
158
159        mService.saveBaseStateLocked();
160
161        dumpBaseStateFile();
162
163        mService.saveDirtyInfo();
164
165        // Restore.
166        initService();
167
168        assertResetTimes(START_TIME + 4 * INTERVAL, START_TIME + 5 * INTERVAL);
169    }
170
171    /**
172     * Test for the restoration from restored file.
173     */
174    public void testLoadFromBrokenFile() {
175        // TODO Add various broken cases.
176    }
177
178    public void testLoadConfig() {
179        mService.updateConfigurationLocked(
180                ConfigConstants.KEY_RESET_INTERVAL_SEC + "=123,"
181                        + ConfigConstants.KEY_MAX_SHORTCUTS + "=4,"
182                        + ConfigConstants.KEY_MAX_UPDATES_PER_INTERVAL + "=5,"
183                        + ConfigConstants.KEY_MAX_ICON_DIMENSION_DP + "=100,"
184                        + ConfigConstants.KEY_MAX_ICON_DIMENSION_DP_LOWRAM + "=50,"
185                        + ConfigConstants.KEY_ICON_FORMAT + "=WEBP,"
186                        + ConfigConstants.KEY_ICON_QUALITY + "=75");
187        assertEquals(123000, mService.getResetIntervalForTest());
188        assertEquals(4, mService.getMaxShortcutsForTest());
189        assertEquals(5, mService.getMaxUpdatesPerIntervalForTest());
190        assertEquals(100, mService.getMaxIconDimensionForTest());
191        assertEquals(CompressFormat.WEBP, mService.getIconPersistFormatForTest());
192        assertEquals(75, mService.getIconPersistQualityForTest());
193
194        mInjectedIsLowRamDevice = true;
195        mService.updateConfigurationLocked(
196                ConfigConstants.KEY_MAX_ICON_DIMENSION_DP + "=100,"
197                        + ConfigConstants.KEY_MAX_ICON_DIMENSION_DP_LOWRAM + "=50,"
198                        + ConfigConstants.KEY_ICON_FORMAT + "=JPEG");
199        assertEquals(ShortcutService.DEFAULT_RESET_INTERVAL_SEC * 1000,
200                mService.getResetIntervalForTest());
201
202        assertEquals(ShortcutService.DEFAULT_MAX_SHORTCUTS_PER_APP,
203                mService.getMaxShortcutsForTest());
204
205        assertEquals(ShortcutService.DEFAULT_MAX_UPDATES_PER_INTERVAL,
206                mService.getMaxUpdatesPerIntervalForTest());
207
208        assertEquals(50, mService.getMaxIconDimensionForTest());
209
210        assertEquals(CompressFormat.JPEG, mService.getIconPersistFormatForTest());
211
212        assertEquals(ShortcutService.DEFAULT_ICON_PERSIST_QUALITY,
213                mService.getIconPersistQualityForTest());
214    }
215
216    // === Test for app side APIs ===
217
218    /** Test for {@link android.content.pm.ShortcutManager#getMaxShortcutCountForActivity()} */
219    public void testGetMaxDynamicShortcutCount() {
220        assertEquals(MAX_SHORTCUTS, mManager.getMaxShortcutCountForActivity());
221    }
222
223    /** Test for {@link android.content.pm.ShortcutManager#getRemainingCallCount()} */
224    public void testGetRemainingCallCount() {
225        assertEquals(MAX_UPDATES_PER_INTERVAL, mManager.getRemainingCallCount());
226    }
227
228    public void testGetIconMaxDimensions() {
229        assertEquals(MAX_ICON_DIMENSION, mManager.getIconMaxWidth());
230        assertEquals(MAX_ICON_DIMENSION, mManager.getIconMaxHeight());
231    }
232
233    /** Test for {@link android.content.pm.ShortcutManager#getRateLimitResetTime()} */
234    public void testGetRateLimitResetTime() {
235        assertEquals(START_TIME + INTERVAL, mManager.getRateLimitResetTime());
236
237        mInjectedCurrentTimeMillis = START_TIME + 4 * INTERVAL + 50;
238
239        assertEquals(START_TIME + 5 * INTERVAL, mManager.getRateLimitResetTime());
240    }
241
242    public void testSetDynamicShortcuts() {
243        setCaller(CALLING_PACKAGE_1, USER_0);
244
245        final Icon icon1 = Icon.createWithResource(getTestContext(), R.drawable.icon1);
246        final Icon icon2 = Icon.createWithBitmap(BitmapFactory.decodeResource(
247                getTestContext().getResources(), R.drawable.icon2));
248
249        final ShortcutInfo si1 = makeShortcut(
250                "shortcut1",
251                "Title 1",
252                makeComponent(ShortcutActivity.class),
253                icon1,
254                makeIntent(Intent.ACTION_ASSIST, ShortcutActivity2.class,
255                        "key1", "val1", "nest", makeBundle("key", 123)),
256                /* weight */ 10);
257
258        final ShortcutInfo si2 = makeShortcut(
259                "shortcut2",
260                "Title 2",
261                /* activity */ null,
262                icon2,
263                makeIntent(Intent.ACTION_ASSIST, ShortcutActivity3.class),
264                /* weight */ 12);
265        final ShortcutInfo si3 = makeShortcut("shortcut3");
266
267        assertTrue(mManager.setDynamicShortcuts(list(si1, si2)));
268        assertShortcutIds(assertAllNotKeyFieldsOnly(
269                mManager.getDynamicShortcuts()),
270                "shortcut1", "shortcut2");
271        assertEquals(2, mManager.getRemainingCallCount());
272
273        // TODO: Check fields
274
275        assertTrue(mManager.setDynamicShortcuts(list(si1)));
276        assertShortcutIds(assertAllNotKeyFieldsOnly(
277                mManager.getDynamicShortcuts()),
278                "shortcut1");
279        assertEquals(1, mManager.getRemainingCallCount());
280
281        assertTrue(mManager.setDynamicShortcuts(list()));
282        assertEquals(0, mManager.getDynamicShortcuts().size());
283        assertEquals(0, mManager.getRemainingCallCount());
284
285        dumpsysOnLogcat();
286
287        mInjectedCurrentTimeMillis++; // Need to advance the clock for reset to work.
288        mService.resetThrottlingInner(UserHandle.USER_SYSTEM);
289
290        dumpsysOnLogcat();
291
292        assertTrue(mManager.setDynamicShortcuts(list(si2, si3)));
293        assertEquals(2, mManager.getDynamicShortcuts().size());
294
295        // TODO Check max number
296
297        runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
298            assertTrue(mManager.setDynamicShortcuts(list(makeShortcut("s1"))));
299        });
300    }
301
302    public void testAddDynamicShortcuts() {
303        setCaller(CALLING_PACKAGE_1, USER_0);
304
305        final ShortcutInfo si1 = makeShortcut("shortcut1");
306        final ShortcutInfo si2 = makeShortcut("shortcut2");
307        final ShortcutInfo si3 = makeShortcut("shortcut3");
308
309        assertEquals(3, mManager.getRemainingCallCount());
310
311        assertTrue(mManager.setDynamicShortcuts(list(si1)));
312        assertEquals(2, mManager.getRemainingCallCount());
313        assertShortcutIds(assertAllNotKeyFieldsOnly(
314                mManager.getDynamicShortcuts()),
315                "shortcut1");
316
317        assertTrue(mManager.addDynamicShortcuts(list(si2, si3)));
318        assertEquals(1, mManager.getRemainingCallCount());
319        assertShortcutIds(assertAllNotKeyFieldsOnly(
320                mManager.getDynamicShortcuts()),
321                "shortcut1", "shortcut2", "shortcut3");
322
323        // This should not crash.  It'll still consume the quota.
324        assertTrue(mManager.addDynamicShortcuts(list()));
325        assertEquals(0, mManager.getRemainingCallCount());
326        assertShortcutIds(assertAllNotKeyFieldsOnly(
327                mManager.getDynamicShortcuts()),
328                "shortcut1", "shortcut2", "shortcut3");
329
330        mInjectedCurrentTimeMillis += INTERVAL; // reset
331
332        // Add with the same ID
333        assertTrue(mManager.addDynamicShortcuts(list(makeShortcut("shortcut1"))));
334        assertEquals(2, mManager.getRemainingCallCount());
335        assertShortcutIds(assertAllNotKeyFieldsOnly(
336                mManager.getDynamicShortcuts()),
337                "shortcut1", "shortcut2", "shortcut3");
338
339        // TODO Check max number
340
341        // TODO Check fields.
342
343        runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
344            assertTrue(mManager.addDynamicShortcuts(list(makeShortcut("s1"))));
345        });
346    }
347
348    public void testPublishWithNoActivity() {
349        // If activity is not explicitly set, use the default one.
350
351        runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
352            // s1 and s3 has no activities.
353            final ShortcutInfo si1 = new ShortcutInfo.Builder(mClientContext, "si1")
354                    .setShortLabel("label1")
355                    .setIntent(new Intent("action1"))
356                    .build();
357            final ShortcutInfo si2 = new ShortcutInfo.Builder(mClientContext, "si2")
358                    .setShortLabel("label2")
359                    .setActivity(new ComponentName(getCallingPackage(), "abc"))
360                    .setIntent(new Intent("action2"))
361                    .build();
362            final ShortcutInfo si3 = new ShortcutInfo.Builder(mClientContext, "si3")
363                    .setShortLabel("label3")
364                    .setIntent(new Intent("action3"))
365                    .build();
366
367            // Set test 1
368            assertTrue(mManager.setDynamicShortcuts(list(si1)));
369
370            assertWith(getCallerShortcuts())
371                    .haveIds("si1")
372                    .forShortcutWithId("si1", si -> {
373                        assertEquals(new ComponentName(getCallingPackage(),
374                                MAIN_ACTIVITY_CLASS), si.getActivity());
375                    });
376
377            // Set test 2
378            assertTrue(mManager.setDynamicShortcuts(list(si2, si1)));
379
380            assertWith(getCallerShortcuts())
381                    .haveIds("si1", "si2")
382                    .forShortcutWithId("si1", si -> {
383                        assertEquals(new ComponentName(getCallingPackage(),
384                                MAIN_ACTIVITY_CLASS), si.getActivity());
385                    })
386                    .forShortcutWithId("si2", si -> {
387                        assertEquals(new ComponentName(getCallingPackage(),
388                                "abc"), si.getActivity());
389                    });
390
391
392            // Set test 3
393            assertTrue(mManager.setDynamicShortcuts(list(si3, si1)));
394
395            assertWith(getCallerShortcuts())
396                    .haveIds("si1", "si3")
397                    .forShortcutWithId("si1", si -> {
398                        assertEquals(new ComponentName(getCallingPackage(),
399                                MAIN_ACTIVITY_CLASS), si.getActivity());
400                    })
401                    .forShortcutWithId("si3", si -> {
402                        assertEquals(new ComponentName(getCallingPackage(),
403                                MAIN_ACTIVITY_CLASS), si.getActivity());
404                    });
405
406            mInjectedCurrentTimeMillis += INTERVAL; // reset throttling
407
408            // Add test 1
409            mManager.removeAllDynamicShortcuts();
410            assertTrue(mManager.addDynamicShortcuts(list(si1)));
411
412            assertWith(getCallerShortcuts())
413                    .haveIds("si1")
414                    .forShortcutWithId("si1", si -> {
415                        assertEquals(new ComponentName(getCallingPackage(),
416                                MAIN_ACTIVITY_CLASS), si.getActivity());
417                    });
418
419            // Add test 2
420            mManager.removeAllDynamicShortcuts();
421            assertTrue(mManager.addDynamicShortcuts(list(si2, si1)));
422
423            assertWith(getCallerShortcuts())
424                    .haveIds("si1", "si2")
425                    .forShortcutWithId("si1", si -> {
426                        assertEquals(new ComponentName(getCallingPackage(),
427                                MAIN_ACTIVITY_CLASS), si.getActivity());
428                    })
429                    .forShortcutWithId("si2", si -> {
430                        assertEquals(new ComponentName(getCallingPackage(),
431                                "abc"), si.getActivity());
432                    });
433
434
435            // Add test 3
436            mManager.removeAllDynamicShortcuts();
437            assertTrue(mManager.addDynamicShortcuts(list(si3, si1)));
438
439            assertWith(getCallerShortcuts())
440                    .haveIds("si1", "si3")
441                    .forShortcutWithId("si1", si -> {
442                        assertEquals(new ComponentName(getCallingPackage(),
443                                MAIN_ACTIVITY_CLASS), si.getActivity());
444                    })
445                    .forShortcutWithId("si3", si -> {
446                        assertEquals(new ComponentName(getCallingPackage(),
447                                MAIN_ACTIVITY_CLASS), si.getActivity());
448                    });
449        });
450    }
451
452    public void testPublishWithNoActivity_noMainActivityInPackage() {
453        runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
454            final ShortcutInfo si1 = new ShortcutInfo.Builder(mClientContext, "si1")
455                    .setShortLabel("label1")
456                    .setIntent(new Intent("action1"))
457                    .build();
458
459            // Returning null means there's no main activity in this package.
460            mMainActivityFetcher = (packageName, userId) -> null;
461
462            assertExpectException(
463                    RuntimeException.class, "Launcher activity not found for", () -> {
464                        assertTrue(mManager.setDynamicShortcuts(list(si1)));
465                    });
466        });
467    }
468
469    public void testDeleteDynamicShortcuts() {
470        final ShortcutInfo si1 = makeShortcut("shortcut1");
471        final ShortcutInfo si2 = makeShortcut("shortcut2");
472        final ShortcutInfo si3 = makeShortcut("shortcut3");
473        final ShortcutInfo si4 = makeShortcut("shortcut4");
474
475        assertTrue(mManager.setDynamicShortcuts(list(si1, si2, si3, si4)));
476        assertShortcutIds(assertAllNotKeyFieldsOnly(
477                mManager.getDynamicShortcuts()),
478                "shortcut1", "shortcut2", "shortcut3", "shortcut4");
479
480        assertEquals(2, mManager.getRemainingCallCount());
481
482        mManager.removeDynamicShortcuts(list("shortcut1"));
483        assertShortcutIds(assertAllNotKeyFieldsOnly(
484                mManager.getDynamicShortcuts()),
485                "shortcut2", "shortcut3", "shortcut4");
486
487        mManager.removeDynamicShortcuts(list("shortcut1"));
488        assertShortcutIds(assertAllNotKeyFieldsOnly(
489                mManager.getDynamicShortcuts()),
490                "shortcut2", "shortcut3", "shortcut4");
491
492        mManager.removeDynamicShortcuts(list("shortcutXXX"));
493        assertShortcutIds(assertAllNotKeyFieldsOnly(
494                mManager.getDynamicShortcuts()),
495                "shortcut2", "shortcut3", "shortcut4");
496
497        mManager.removeDynamicShortcuts(list("shortcut2", "shortcut4"));
498        assertShortcutIds(assertAllNotKeyFieldsOnly(
499                mManager.getDynamicShortcuts()),
500                "shortcut3");
501
502        mManager.removeDynamicShortcuts(list("shortcut3"));
503        assertShortcutIds(assertAllNotKeyFieldsOnly(
504                mManager.getDynamicShortcuts()));
505
506        // Still 2 calls left.
507        assertEquals(2, mManager.getRemainingCallCount());
508    }
509
510    public void testDeleteAllDynamicShortcuts() {
511        final ShortcutInfo si1 = makeShortcut("shortcut1");
512        final ShortcutInfo si2 = makeShortcut("shortcut2");
513        final ShortcutInfo si3 = makeShortcut("shortcut3");
514
515        assertTrue(mManager.setDynamicShortcuts(list(si1, si2, si3)));
516        assertShortcutIds(assertAllNotKeyFieldsOnly(
517                mManager.getDynamicShortcuts()),
518                "shortcut1", "shortcut2", "shortcut3");
519
520        assertEquals(2, mManager.getRemainingCallCount());
521
522        mManager.removeAllDynamicShortcuts();
523        assertEquals(0, mManager.getDynamicShortcuts().size());
524        assertEquals(2, mManager.getRemainingCallCount());
525
526        // Note delete shouldn't affect throttling, so...
527        assertEquals(0, mManager.getDynamicShortcuts().size());
528        assertEquals(0, mManager.getDynamicShortcuts().size());
529        assertEquals(0, mManager.getDynamicShortcuts().size());
530
531        // This should still work.
532        assertTrue(mManager.setDynamicShortcuts(list(si1, si2, si3)));
533        assertEquals(3, mManager.getDynamicShortcuts().size());
534
535        // Still 1 call left
536        assertEquals(1, mManager.getRemainingCallCount());
537    }
538
539    public void testIcons() throws IOException {
540        final Icon res32x32 = Icon.createWithResource(getTestContext(), R.drawable.black_32x32);
541        final Icon res64x64 = Icon.createWithResource(getTestContext(), R.drawable.black_64x64);
542        final Icon res512x512 = Icon.createWithResource(getTestContext(), R.drawable.black_512x512);
543
544        final Icon bmp32x32 = Icon.createWithBitmap(BitmapFactory.decodeResource(
545                getTestContext().getResources(), R.drawable.black_32x32));
546        final Icon bmp64x64 = Icon.createWithBitmap(BitmapFactory.decodeResource(
547                getTestContext().getResources(), R.drawable.black_64x64));
548        final Icon bmp512x512 = Icon.createWithBitmap(BitmapFactory.decodeResource(
549                getTestContext().getResources(), R.drawable.black_512x512));
550
551        // Set from package 1
552        setCaller(CALLING_PACKAGE_1);
553        assertTrue(mManager.setDynamicShortcuts(list(
554                makeShortcutWithIcon("res32x32", res32x32),
555                makeShortcutWithIcon("res64x64", res64x64),
556                makeShortcutWithIcon("bmp32x32", bmp32x32),
557                makeShortcutWithIcon("bmp64x64", bmp64x64),
558                makeShortcutWithIcon("bmp512x512", bmp512x512),
559                makeShortcut("none")
560        )));
561
562        // getDynamicShortcuts() shouldn't return icons, thus assertAllNotHaveIcon().
563        assertShortcutIds(assertAllNotHaveIcon(mManager.getDynamicShortcuts()),
564                "res32x32",
565                "res64x64",
566                "bmp32x32",
567                "bmp64x64",
568                "bmp512x512",
569                "none");
570
571        // Call from another caller with the same ID, just to make sure storage is per-package.
572        setCaller(CALLING_PACKAGE_2);
573        assertTrue(mManager.setDynamicShortcuts(list(
574                makeShortcutWithIcon("res32x32", res512x512),
575                makeShortcutWithIcon("res64x64", res512x512),
576                makeShortcutWithIcon("none", res512x512)
577        )));
578        assertShortcutIds(assertAllNotHaveIcon(mManager.getDynamicShortcuts()),
579                "res32x32",
580                "res64x64",
581                "none");
582
583        // Different profile.  Note the names and the contents don't match.
584        setCaller(CALLING_PACKAGE_1, USER_P0);
585        assertTrue(mManager.setDynamicShortcuts(list(
586                makeShortcutWithIcon("res32x32", res512x512),
587                makeShortcutWithIcon("bmp32x32", bmp512x512)
588        )));
589        assertShortcutIds(assertAllNotHaveIcon(mManager.getDynamicShortcuts()),
590                "res32x32",
591                "bmp32x32");
592
593        // Re-initialize and load from the files.
594        mService.saveDirtyInfo();
595        initService();
596
597        // Load from launcher.
598        Bitmap bmp;
599
600        setCaller(LAUNCHER_1);
601        // Check hasIconResource()/hasIconFile().
602        assertShortcutIds(assertAllHaveIconResId(
603                list(getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "res32x32", USER_0))),
604                "res32x32");
605
606        assertShortcutIds(assertAllHaveIconResId(
607                list(getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "res64x64", USER_0))),
608                "res64x64");
609
610        assertShortcutIds(assertAllHaveIconFile(
611                list(getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp32x32", USER_0))),
612                "bmp32x32");
613
614        assertShortcutIds(assertAllHaveIconFile(
615                list(getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp64x64", USER_0))),
616                "bmp64x64");
617
618        assertShortcutIds(assertAllHaveIconFile(
619                list(getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp512x512", USER_0))),
620                "bmp512x512");
621
622        assertShortcutIds(assertAllHaveIconResId(
623                list(getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "res32x32", USER_P0))),
624                "res32x32");
625        assertShortcutIds(assertAllHaveIconFile(
626                list(getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp32x32", USER_P0))),
627                "bmp32x32");
628
629        // Check
630        assertEquals(
631                R.drawable.black_32x32,
632                mLauncherApps.getShortcutIconResId(
633                        getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "res32x32", USER_0)));
634
635        assertEquals(
636                R.drawable.black_64x64,
637                mLauncherApps.getShortcutIconResId(
638                        getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "res64x64", USER_0)));
639
640        assertEquals(
641                0, // because it's not a resource
642                mLauncherApps.getShortcutIconResId(
643                        getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp32x32", USER_0)));
644        assertEquals(
645                0, // because it's not a resource
646                mLauncherApps.getShortcutIconResId(
647                        getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp64x64", USER_0)));
648        assertEquals(
649                0, // because it's not a resource
650                mLauncherApps.getShortcutIconResId(
651                        getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp512x512", USER_0)));
652
653        bmp = pfdToBitmap(mLauncherApps.getShortcutIconFd(
654                getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp32x32", USER_0)));
655        assertBitmapSize(32, 32, bmp);
656
657        bmp = pfdToBitmap(mLauncherApps.getShortcutIconFd(
658                getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp64x64", USER_0)));
659        assertBitmapSize(64, 64, bmp);
660
661        bmp = pfdToBitmap(mLauncherApps.getShortcutIconFd(
662                getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp512x512", USER_0)));
663        assertBitmapSize(128, 128, bmp);
664
665        assertEquals(
666                R.drawable.black_512x512,
667                mLauncherApps.getShortcutIconResId(
668                        getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "res32x32", USER_P0)));
669        // Should be 512x512, so shrunk.
670        bmp = pfdToBitmap(mLauncherApps.getShortcutIconFd(
671                getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp32x32", USER_P0)));
672        assertBitmapSize(128, 128, bmp);
673
674        // Also check the overload APIs too.
675        assertEquals(
676                R.drawable.black_32x32,
677                mLauncherApps.getShortcutIconResId(CALLING_PACKAGE_1, "res32x32", HANDLE_USER_0));
678        assertEquals(
679                R.drawable.black_64x64,
680                mLauncherApps.getShortcutIconResId(CALLING_PACKAGE_1, "res64x64", HANDLE_USER_0));
681        assertEquals(
682                R.drawable.black_512x512,
683                mLauncherApps.getShortcutIconResId(CALLING_PACKAGE_1, "res32x32", HANDLE_USER_P0));
684        bmp = pfdToBitmap(
685                mLauncherApps.getShortcutIconFd(CALLING_PACKAGE_1, "bmp32x32", HANDLE_USER_P0));
686        assertBitmapSize(128, 128, bmp);
687    }
688
689    public void testCleanupDanglingBitmaps() throws Exception {
690        assertBitmapDirectories(USER_0, EMPTY_STRINGS);
691        assertBitmapDirectories(USER_10, EMPTY_STRINGS);
692
693        // Make some shortcuts with bitmap icons.
694        final Icon bmp32x32 = Icon.createWithBitmap(BitmapFactory.decodeResource(
695                getTestContext().getResources(), R.drawable.black_32x32));
696
697        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
698            mManager.setDynamicShortcuts(list(
699                    makeShortcutWithIcon("s1", bmp32x32),
700                    makeShortcutWithIcon("s2", bmp32x32),
701                    makeShortcutWithIcon("s3", bmp32x32)
702            ));
703        });
704
705        // Increment the time (which actually we don't have to), which is used for filenames.
706        mInjectedCurrentTimeMillis++;
707
708        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
709            mManager.setDynamicShortcuts(list(
710                    makeShortcutWithIcon("s4", bmp32x32),
711                    makeShortcutWithIcon("s5", bmp32x32),
712                    makeShortcutWithIcon("s6", bmp32x32)
713            ));
714        });
715
716        // Increment the time, which is used for filenames.
717        mInjectedCurrentTimeMillis++;
718
719        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
720            mManager.setDynamicShortcuts(list(
721            ));
722        });
723
724        // For USER-10, let's try without updating the times.
725        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
726            mManager.setDynamicShortcuts(list(
727                    makeShortcutWithIcon("10s1", bmp32x32),
728                    makeShortcutWithIcon("10s2", bmp32x32),
729                    makeShortcutWithIcon("10s3", bmp32x32)
730            ));
731        });
732        runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
733            mManager.setDynamicShortcuts(list(
734                    makeShortcutWithIcon("10s4", bmp32x32),
735                    makeShortcutWithIcon("10s5", bmp32x32),
736                    makeShortcutWithIcon("10s6", bmp32x32)
737            ));
738        });
739        runWithCaller(CALLING_PACKAGE_3, USER_10, () -> {
740            mManager.setDynamicShortcuts(list(
741            ));
742        });
743
744        dumpsysOnLogcat();
745
746        // Check files and directories.
747        // Package 3 has no bitmaps, so we don't create a directory.
748        assertBitmapDirectories(USER_0, CALLING_PACKAGE_1, CALLING_PACKAGE_2);
749        assertBitmapDirectories(USER_10, CALLING_PACKAGE_1, CALLING_PACKAGE_2);
750
751        assertBitmapFiles(USER_0, CALLING_PACKAGE_1,
752                getBitmapFilename(USER_0, CALLING_PACKAGE_1, "s1"),
753                getBitmapFilename(USER_0, CALLING_PACKAGE_1, "s2"),
754                getBitmapFilename(USER_0, CALLING_PACKAGE_1, "s3")
755        );
756        assertBitmapFiles(USER_0, CALLING_PACKAGE_2,
757                getBitmapFilename(USER_0, CALLING_PACKAGE_2, "s4"),
758                getBitmapFilename(USER_0, CALLING_PACKAGE_2, "s5"),
759                getBitmapFilename(USER_0, CALLING_PACKAGE_2, "s6")
760        );
761        assertBitmapFiles(USER_0, CALLING_PACKAGE_3,
762                EMPTY_STRINGS
763        );
764        assertBitmapFiles(USER_10, CALLING_PACKAGE_1,
765                getBitmapFilename(USER_10, CALLING_PACKAGE_1, "10s1"),
766                getBitmapFilename(USER_10, CALLING_PACKAGE_1, "10s2"),
767                getBitmapFilename(USER_10, CALLING_PACKAGE_1, "10s3")
768        );
769        assertBitmapFiles(USER_10, CALLING_PACKAGE_2,
770                getBitmapFilename(USER_10, CALLING_PACKAGE_2, "10s4"),
771                getBitmapFilename(USER_10, CALLING_PACKAGE_2, "10s5"),
772                getBitmapFilename(USER_10, CALLING_PACKAGE_2, "10s6")
773        );
774        assertBitmapFiles(USER_10, CALLING_PACKAGE_3,
775                EMPTY_STRINGS
776        );
777
778        // Then create random directories and files.
779        makeFile(mService.getUserBitmapFilePath(USER_0), "a.b.c").mkdir();
780        makeFile(mService.getUserBitmapFilePath(USER_0), "d.e.f").mkdir();
781        makeFile(mService.getUserBitmapFilePath(USER_0), "d.e.f", "123").createNewFile();
782        makeFile(mService.getUserBitmapFilePath(USER_0), "d.e.f", "456").createNewFile();
783
784        makeFile(mService.getUserBitmapFilePath(USER_0), CALLING_PACKAGE_3).mkdir();
785
786        makeFile(mService.getUserBitmapFilePath(USER_0), CALLING_PACKAGE_1, "1").createNewFile();
787        makeFile(mService.getUserBitmapFilePath(USER_0), CALLING_PACKAGE_1, "2").createNewFile();
788        makeFile(mService.getUserBitmapFilePath(USER_0), CALLING_PACKAGE_1, "3").createNewFile();
789        makeFile(mService.getUserBitmapFilePath(USER_0), CALLING_PACKAGE_1, "4").createNewFile();
790
791        makeFile(mService.getUserBitmapFilePath(USER_10), "10a.b.c").mkdir();
792        makeFile(mService.getUserBitmapFilePath(USER_10), "10d.e.f").mkdir();
793        makeFile(mService.getUserBitmapFilePath(USER_10), "10d.e.f", "123").createNewFile();
794        makeFile(mService.getUserBitmapFilePath(USER_10), "10d.e.f", "456").createNewFile();
795
796        makeFile(mService.getUserBitmapFilePath(USER_10), CALLING_PACKAGE_2, "1").createNewFile();
797        makeFile(mService.getUserBitmapFilePath(USER_10), CALLING_PACKAGE_2, "2").createNewFile();
798        makeFile(mService.getUserBitmapFilePath(USER_10), CALLING_PACKAGE_2, "3").createNewFile();
799        makeFile(mService.getUserBitmapFilePath(USER_10), CALLING_PACKAGE_2, "4").createNewFile();
800
801        assertBitmapDirectories(USER_0, CALLING_PACKAGE_1, CALLING_PACKAGE_2, CALLING_PACKAGE_3,
802                "a.b.c", "d.e.f");
803
804        // Save and load.  When a user is loaded, we do the cleanup.
805        mService.saveDirtyInfo();
806        initService();
807
808        mService.handleUnlockUser(USER_0);
809        mService.handleUnlockUser(USER_10);
810        mService.handleUnlockUser(20); // Make sure the logic will still work for nonexistent user.
811
812        // The below check is the same as above, except this time USER_0 use the CALLING_PACKAGE_3
813        // directory.
814
815        assertBitmapDirectories(USER_0, CALLING_PACKAGE_1, CALLING_PACKAGE_2, CALLING_PACKAGE_3);
816        assertBitmapDirectories(USER_10, CALLING_PACKAGE_1, CALLING_PACKAGE_2);
817
818        assertBitmapFiles(USER_0, CALLING_PACKAGE_1,
819                getBitmapFilename(USER_0, CALLING_PACKAGE_1, "s1"),
820                getBitmapFilename(USER_0, CALLING_PACKAGE_1, "s2"),
821                getBitmapFilename(USER_0, CALLING_PACKAGE_1, "s3")
822        );
823        assertBitmapFiles(USER_0, CALLING_PACKAGE_2,
824                getBitmapFilename(USER_0, CALLING_PACKAGE_2, "s4"),
825                getBitmapFilename(USER_0, CALLING_PACKAGE_2, "s5"),
826                getBitmapFilename(USER_0, CALLING_PACKAGE_2, "s6")
827        );
828        assertBitmapFiles(USER_0, CALLING_PACKAGE_3,
829                EMPTY_STRINGS
830        );
831        assertBitmapFiles(USER_10, CALLING_PACKAGE_1,
832                getBitmapFilename(USER_10, CALLING_PACKAGE_1, "10s1"),
833                getBitmapFilename(USER_10, CALLING_PACKAGE_1, "10s2"),
834                getBitmapFilename(USER_10, CALLING_PACKAGE_1, "10s3")
835        );
836        assertBitmapFiles(USER_10, CALLING_PACKAGE_2,
837                getBitmapFilename(USER_10, CALLING_PACKAGE_2, "10s4"),
838                getBitmapFilename(USER_10, CALLING_PACKAGE_2, "10s5"),
839                getBitmapFilename(USER_10, CALLING_PACKAGE_2, "10s6")
840        );
841        assertBitmapFiles(USER_10, CALLING_PACKAGE_3,
842                EMPTY_STRINGS
843        );
844    }
845
846    protected void checkShrinkBitmap(int expectedWidth, int expectedHeight, int resId, int maxSize) {
847        assertBitmapSize(expectedWidth, expectedHeight,
848                ShortcutService.shrinkBitmap(BitmapFactory.decodeResource(
849                        getTestContext().getResources(), resId),
850                        maxSize));
851    }
852
853    public void testShrinkBitmap() {
854        checkShrinkBitmap(32, 32, R.drawable.black_512x512, 32);
855        checkShrinkBitmap(511, 511, R.drawable.black_512x512, 511);
856        checkShrinkBitmap(512, 512, R.drawable.black_512x512, 512);
857
858        checkShrinkBitmap(1024, 4096, R.drawable.black_1024x4096, 4096);
859        checkShrinkBitmap(1024, 4096, R.drawable.black_1024x4096, 4100);
860        checkShrinkBitmap(512, 2048, R.drawable.black_1024x4096, 2048);
861
862        checkShrinkBitmap(4096, 1024, R.drawable.black_4096x1024, 4096);
863        checkShrinkBitmap(4096, 1024, R.drawable.black_4096x1024, 4100);
864        checkShrinkBitmap(2048, 512, R.drawable.black_4096x1024, 2048);
865    }
866
867    protected File openIconFileForWriteAndGetPath(int userId, String packageName)
868            throws IOException {
869        // Shortcut IDs aren't used in the path, so just pass the same ID.
870        final FileOutputStreamWithPath out =
871                mService.openIconFileForWrite(userId, makePackageShortcut(packageName, "id"));
872        out.close();
873        return out.getFile();
874    }
875
876    public void testOpenIconFileForWrite() throws IOException {
877        mInjectedCurrentTimeMillis = 1000;
878
879        final File p10_1_1 = openIconFileForWriteAndGetPath(10, CALLING_PACKAGE_1);
880        final File p10_1_2 = openIconFileForWriteAndGetPath(10, CALLING_PACKAGE_1);
881
882        final File p10_2_1 = openIconFileForWriteAndGetPath(10, CALLING_PACKAGE_2);
883        final File p10_2_2 = openIconFileForWriteAndGetPath(10, CALLING_PACKAGE_2);
884
885        final File p11_1_1 = openIconFileForWriteAndGetPath(11, CALLING_PACKAGE_1);
886        final File p11_1_2 = openIconFileForWriteAndGetPath(11, CALLING_PACKAGE_1);
887
888        mInjectedCurrentTimeMillis++;
889
890        final File p10_1_3 = openIconFileForWriteAndGetPath(10, CALLING_PACKAGE_1);
891        final File p10_1_4 = openIconFileForWriteAndGetPath(10, CALLING_PACKAGE_1);
892        final File p10_1_5 = openIconFileForWriteAndGetPath(10, CALLING_PACKAGE_1);
893
894        final File p10_2_3 = openIconFileForWriteAndGetPath(10, CALLING_PACKAGE_2);
895        final File p11_1_3 = openIconFileForWriteAndGetPath(11, CALLING_PACKAGE_1);
896
897        // Make sure their paths are all unique
898        assertAllUnique(list(
899                p10_1_1,
900                p10_1_2,
901                p10_1_3,
902                p10_1_4,
903                p10_1_5,
904
905                p10_2_1,
906                p10_2_2,
907                p10_2_3,
908
909                p11_1_1,
910                p11_1_2,
911                p11_1_3
912        ));
913
914        // Check each set has the same parent.
915        assertEquals(p10_1_1.getParent(), p10_1_2.getParent());
916        assertEquals(p10_1_1.getParent(), p10_1_3.getParent());
917        assertEquals(p10_1_1.getParent(), p10_1_4.getParent());
918        assertEquals(p10_1_1.getParent(), p10_1_5.getParent());
919
920        assertEquals(p10_2_1.getParent(), p10_2_2.getParent());
921        assertEquals(p10_2_1.getParent(), p10_2_3.getParent());
922
923        assertEquals(p11_1_1.getParent(), p11_1_2.getParent());
924        assertEquals(p11_1_1.getParent(), p11_1_3.getParent());
925
926        // Check the parents are still unique.
927        assertAllUnique(list(
928                p10_1_1.getParent(),
929                p10_2_1.getParent(),
930                p11_1_1.getParent()
931        ));
932
933        // All files created at the same time for the same package/user, expcet for the first ones,
934        // will have "_" in the path.
935        assertFalse(p10_1_1.getName().contains("_"));
936        assertTrue(p10_1_2.getName().contains("_"));
937        assertFalse(p10_1_3.getName().contains("_"));
938        assertTrue(p10_1_4.getName().contains("_"));
939        assertTrue(p10_1_5.getName().contains("_"));
940
941        assertFalse(p10_2_1.getName().contains("_"));
942        assertTrue(p10_2_2.getName().contains("_"));
943        assertFalse(p10_2_3.getName().contains("_"));
944
945        assertFalse(p11_1_1.getName().contains("_"));
946        assertTrue(p11_1_2.getName().contains("_"));
947        assertFalse(p11_1_3.getName().contains("_"));
948    }
949
950    public void testUpdateShortcuts() {
951        runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
952            assertTrue(mManager.setDynamicShortcuts(list(
953                    makeShortcut("s1"),
954                    makeShortcut("s2"),
955                    makeShortcut("s3"),
956                    makeShortcut("s4"),
957                    makeShortcut("s5")
958            )));
959        });
960        runWithCaller(CALLING_PACKAGE_2, UserHandle.USER_SYSTEM, () -> {
961            assertTrue(mManager.setDynamicShortcuts(list(
962                    makeShortcut("s1"),
963                    makeShortcut("s2"),
964                    makeShortcut("s3"),
965                    makeShortcut("s4"),
966                    makeShortcut("s5")
967            )));
968        });
969        runWithCaller(LAUNCHER_1, UserHandle.USER_SYSTEM, () -> {
970            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s2", "s3"),
971                    getCallingUser());
972            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s4", "s5"),
973                    getCallingUser());
974        });
975        runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
976            mManager.removeDynamicShortcuts(list("s1"));
977            mManager.removeDynamicShortcuts(list("s2"));
978        });
979        runWithCaller(CALLING_PACKAGE_2, UserHandle.USER_SYSTEM, () -> {
980            mManager.removeDynamicShortcuts(list("s1"));
981            mManager.removeDynamicShortcuts(list("s3"));
982            mManager.removeDynamicShortcuts(list("s5"));
983        });
984        runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
985            assertShortcutIds(assertAllDynamic(
986                    mManager.getDynamicShortcuts()),
987                    "s3", "s4", "s5");
988            assertShortcutIds(assertAllPinned(
989                    mManager.getPinnedShortcuts()),
990                    "s2", "s3");
991        });
992        runWithCaller(CALLING_PACKAGE_2, UserHandle.USER_SYSTEM, () -> {
993            assertShortcutIds(assertAllDynamic(
994                    mManager.getDynamicShortcuts()),
995                    "s2", "s4");
996            assertShortcutIds(assertAllPinned(
997                    mManager.getPinnedShortcuts()),
998                    "s4", "s5");
999        });
1000
1001        runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
1002            ShortcutInfo s2 = makeShortcutBuilder()
1003                    .setId("s2")
1004                    .setIcon(Icon.createWithResource(getTestContext(), R.drawable.black_32x32))
1005                    .build();
1006
1007            ShortcutInfo s4 = makeShortcutBuilder()
1008                    .setId("s4")
1009                    .setTitle("new title")
1010                    .build();
1011
1012            mManager.updateShortcuts(list(s2, s4));
1013        });
1014        runWithCaller(CALLING_PACKAGE_2, UserHandle.USER_SYSTEM, () -> {
1015            ShortcutInfo s2 = makeShortcutBuilder()
1016                    .setId("s2")
1017                    .setIntent(makeIntent(Intent.ACTION_ANSWER, ShortcutActivity.class,
1018                            "key1", "val1"))
1019                    .build();
1020
1021            ShortcutInfo s4 = makeShortcutBuilder()
1022                    .setId("s4")
1023                    .setIntent(new Intent(Intent.ACTION_ALL_APPS))
1024                    .build();
1025
1026            mManager.updateShortcuts(list(s2, s4));
1027        });
1028
1029        runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
1030            assertShortcutIds(assertAllDynamic(
1031                    mManager.getDynamicShortcuts()),
1032                    "s3", "s4", "s5");
1033            assertShortcutIds(assertAllPinned(
1034                    mManager.getPinnedShortcuts()),
1035                    "s2", "s3");
1036
1037            ShortcutInfo s = getCallerShortcut("s2");
1038            assertTrue(s.hasIconResource());
1039            assertEquals(R.drawable.black_32x32, s.getIconResourceId());
1040            assertEquals("string/r" + R.drawable.black_32x32, s.getIconResName());
1041            assertEquals("Title-s2", s.getTitle());
1042
1043            s = getCallerShortcut("s4");
1044            assertFalse(s.hasIconResource());
1045            assertEquals(0, s.getIconResourceId());
1046            assertEquals("new title", s.getTitle());
1047        });
1048        runWithCaller(CALLING_PACKAGE_2, UserHandle.USER_SYSTEM, () -> {
1049            assertShortcutIds(assertAllDynamic(
1050                    mManager.getDynamicShortcuts()),
1051                    "s2", "s4");
1052            assertShortcutIds(assertAllPinned(
1053                    mManager.getPinnedShortcuts()),
1054                    "s4", "s5");
1055
1056            ShortcutInfo s = getCallerShortcut("s2");
1057            assertFalse(s.hasIconResource());
1058            assertEquals(0, s.getIconResourceId());
1059            assertEquals("Title-s2", s.getTitle());
1060            assertEquals(Intent.ACTION_ANSWER, s.getIntent().getAction());
1061            assertEquals(1, s.getIntent().getExtras().size());
1062
1063            s = getCallerShortcut("s4");
1064            assertFalse(s.hasIconResource());
1065            assertEquals(0, s.getIconResourceId());
1066            assertEquals("Title-s4", s.getTitle());
1067            assertEquals(Intent.ACTION_ALL_APPS, s.getIntent().getAction());
1068            assertBundleEmpty(s.getIntent().getExtras());
1069        });
1070        // TODO Check with other fields too.
1071
1072        // TODO Check bitmap removal too.
1073
1074        runWithCaller(CALLING_PACKAGE_2, USER_11, () -> {
1075            mManager.updateShortcuts(list());
1076        });
1077    }
1078
1079    public void testUpdateShortcuts_icons() {
1080        runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
1081            assertTrue(mManager.setDynamicShortcuts(list(
1082                    makeShortcut("s1")
1083            )));
1084
1085            // Set resource icon
1086            assertTrue(mManager.updateShortcuts(list(
1087                    new ShortcutInfo.Builder(mClientContext, "s1")
1088                    .setIcon(Icon.createWithResource(getTestContext(), R.drawable.black_32x32))
1089                    .build()
1090            )));
1091
1092            assertWith(getCallerShortcuts())
1093                    .forShortcutWithId("s1", si -> {
1094                        assertTrue(si.hasIconResource());
1095                        assertEquals(R.drawable.black_32x32, si.getIconResourceId());
1096                    });
1097
1098            // Set bitmap icon
1099            assertTrue(mManager.updateShortcuts(list(
1100                    new ShortcutInfo.Builder(mClientContext, "s1")
1101                    .setIcon(Icon.createWithBitmap(BitmapFactory.decodeResource(
1102                            getTestContext().getResources(), R.drawable.black_64x64)))
1103                    .build()
1104            )));
1105
1106            assertWith(getCallerShortcuts())
1107                    .forShortcutWithId("s1", si -> {
1108                        assertTrue(si.hasIconFile());
1109                    });
1110
1111            mInjectedCurrentTimeMillis += INTERVAL; // reset throttling
1112
1113            // Do it again, with the reverse order (bitmap -> icon)
1114            assertTrue(mManager.setDynamicShortcuts(list(
1115                    makeShortcut("s1")
1116            )));
1117
1118            // Set bitmap icon
1119            assertTrue(mManager.updateShortcuts(list(
1120                    new ShortcutInfo.Builder(mClientContext, "s1")
1121                            .setIcon(Icon.createWithBitmap(BitmapFactory.decodeResource(
1122                                    getTestContext().getResources(), R.drawable.black_64x64)))
1123                            .build()
1124            )));
1125
1126            assertWith(getCallerShortcuts())
1127                    .forShortcutWithId("s1", si -> {
1128                        assertTrue(si.hasIconFile());
1129                    });
1130
1131            // Set resource icon
1132            assertTrue(mManager.updateShortcuts(list(
1133                    new ShortcutInfo.Builder(mClientContext, "s1")
1134                            .setIcon(Icon.createWithResource(getTestContext(), R.drawable.black_32x32))
1135                            .build()
1136            )));
1137
1138            assertWith(getCallerShortcuts())
1139                    .forShortcutWithId("s1", si -> {
1140                        assertTrue(si.hasIconResource());
1141                        assertEquals(R.drawable.black_32x32, si.getIconResourceId());
1142                    });
1143        });
1144    }
1145
1146    // === Test for launcher side APIs ===
1147
1148    public void testGetShortcuts() {
1149
1150        // Set up shortcuts.
1151
1152        setCaller(CALLING_PACKAGE_1);
1153        final ShortcutInfo s1_1 = makeShortcut("s1");
1154        final ShortcutInfo s1_2 = makeShortcut("s2");
1155
1156        assertTrue(mManager.setDynamicShortcuts(list(s1_1, s1_2)));
1157
1158        // Because setDynamicShortcuts will update the timestamps when ranks are changing,
1159        // we explicitly set timestamps here.
1160        getCallerShortcut("s1").setTimestamp(5000);
1161        getCallerShortcut("s2").setTimestamp(1000);
1162
1163        setCaller(CALLING_PACKAGE_2);
1164        final ShortcutInfo s2_2 = makeShortcut("s2");
1165        final ShortcutInfo s2_3 = makeShortcutWithActivity("s3",
1166                makeComponent(ShortcutActivity2.class));
1167        final ShortcutInfo s2_4 = makeShortcutWithActivity("s4",
1168                makeComponent(ShortcutActivity.class));
1169        assertTrue(mManager.setDynamicShortcuts(list(s2_2, s2_3, s2_4)));
1170
1171        getCallerShortcut("s2").setTimestamp(1500);
1172        getCallerShortcut("s3").setTimestamp(3000);
1173        getCallerShortcut("s4").setTimestamp(500);
1174
1175        setCaller(CALLING_PACKAGE_3);
1176        final ShortcutInfo s3_2 = makeShortcut("s3");
1177        assertTrue(mManager.setDynamicShortcuts(list(s3_2)));
1178
1179        getCallerShortcut("s3").setTimestamp(START_TIME + 5000);
1180
1181        setCaller(LAUNCHER_1);
1182
1183        // Get dynamic
1184        assertAllDynamic(assertAllHaveTitle(assertAllNotHaveIntents(assertAllStringsResolved(
1185                assertShortcutIds(
1186                        assertAllNotKeyFieldsOnly(
1187                                mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1188                        /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
1189                        "s1", "s2")))));
1190
1191        // Get pinned
1192        assertShortcutIds(
1193                mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1194                        /* activity =*/ null,
1195                        ShortcutQuery.FLAG_GET_PINNED), getCallingUser())
1196                /* none */);
1197
1198        // Get both, with timestamp
1199        assertAllDynamic(assertAllHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
1200                assertAllNotKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
1201                        /* time =*/ 1000, CALLING_PACKAGE_2,
1202                        /* activity =*/ null,
1203                        ShortcutQuery.FLAG_GET_PINNED | ShortcutQuery.FLAG_GET_DYNAMIC),
1204                        getCallingUser())),
1205                "s2", "s3"))));
1206
1207        // FLAG_GET_KEY_FIELDS_ONLY
1208        assertAllDynamic(assertAllNotHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
1209                assertAllKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
1210                        /* time =*/ 1000, CALLING_PACKAGE_2,
1211                        /* activity =*/ null,
1212                        ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY),
1213                        getCallingUser())),
1214                "s2", "s3"))));
1215
1216        // Filter by activity
1217        assertAllDynamic(assertAllHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
1218                assertAllNotKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
1219                        /* time =*/ 0, CALLING_PACKAGE_2,
1220                        new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
1221                        ShortcutQuery.FLAG_GET_PINNED | ShortcutQuery.FLAG_GET_DYNAMIC),
1222                        getCallingUser())),
1223                "s4"))));
1224
1225        // With ID.
1226        assertAllDynamic(assertAllNotHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
1227                assertAllKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
1228                        /* time =*/ 1000, CALLING_PACKAGE_2, list("s3"),
1229                        /* activity =*/ null,
1230                        ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY),
1231                        getCallingUser())),
1232                "s3"))));
1233        assertAllDynamic(assertAllNotHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
1234                assertAllKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
1235                        /* time =*/ 1000, CALLING_PACKAGE_2, list("s3", "s2", "ss"),
1236                        /* activity =*/ null,
1237                        ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY),
1238                        getCallingUser())),
1239                "s2", "s3"))));
1240        assertAllDynamic(assertAllNotHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
1241                assertAllKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
1242                        /* time =*/ 1000, CALLING_PACKAGE_2, list("s3x", "s2x"),
1243                        /* activity =*/ null,
1244                        ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY),
1245                        getCallingUser()))
1246                /* empty */))));
1247        assertAllDynamic(assertAllNotHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
1248                assertAllKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
1249                        /* time =*/ 1000, CALLING_PACKAGE_2, list(),
1250                        /* activity =*/ null,
1251                        ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY),
1252                        getCallingUser()))
1253                /* empty */))));
1254
1255        // Pin some shortcuts.
1256        mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
1257                list("s3", "s4"), getCallingUser());
1258
1259        // Pinned ones only
1260        assertAllPinned(assertAllHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
1261                assertAllNotKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
1262                        /* time =*/ 1000, CALLING_PACKAGE_2,
1263                        /* activity =*/ null,
1264                        ShortcutQuery.FLAG_GET_PINNED),
1265                        getCallingUser())),
1266                "s3"))));
1267
1268        // All packages.
1269        assertShortcutIds(assertAllNotKeyFieldsOnly(
1270                mLauncherApps.getShortcuts(buildQuery(
1271                        /* time =*/ 5000, /* package= */ null,
1272                        /* activity =*/ null,
1273                        ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_PINNED),
1274                        getCallingUser())),
1275                "s1", "s3");
1276
1277        assertExpectException(
1278                IllegalArgumentException.class, "package name must also be set", () -> {
1279                    mLauncherApps.getShortcuts(buildQuery(
1280                    /* time =*/ 0, /* package= */ null, list("id"),
1281                    /* activity =*/ null, /* flags */ 0), getCallingUser());
1282                });
1283
1284        // TODO More tests: pinned but dynamic.
1285    }
1286
1287    public void testGetShortcuts_shortcutKinds() throws Exception {
1288        // Create 3 manifest and 3 dynamic shortcuts
1289        addManifestShortcutResource(
1290                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
1291                R.xml.shortcut_3);
1292        updatePackageVersion(CALLING_PACKAGE_1, 1);
1293        mService.mPackageMonitor.onReceive(getTestContext(),
1294                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
1295
1296        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1297            assertTrue(mManager.setDynamicShortcuts(list(
1298                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
1299        });
1300
1301        // Pin 2 and 3
1302        runWithCaller(LAUNCHER_1, USER_0, () -> {
1303            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms2", "ms3", "s2", "s3"),
1304                    HANDLE_USER_0);
1305        });
1306
1307        // Remove ms3 and s3
1308        addManifestShortcutResource(
1309                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
1310                R.xml.shortcut_2);
1311        updatePackageVersion(CALLING_PACKAGE_1, 1);
1312        mService.mPackageMonitor.onReceive(getTestContext(),
1313                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
1314
1315        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1316            assertTrue(mManager.setDynamicShortcuts(list(
1317                    makeShortcut("s1"), makeShortcut("s2"))));
1318        });
1319
1320        // Check their status.
1321        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1322            assertWith(getCallerShortcuts())
1323                    .haveIds("ms1", "ms2", "ms3", "s1", "s2", "s3")
1324
1325                    .selectByIds("ms1", "ms2")
1326                    .areAllManifest()
1327                    .areAllImmutable()
1328                    .areAllNotDynamic()
1329
1330                    .revertToOriginalList()
1331                    .selectByIds("ms3")
1332                    .areAllNotManifest()
1333                    .areAllImmutable()
1334                    .areAllDisabled()
1335                    .areAllNotDynamic()
1336
1337                    .revertToOriginalList()
1338                    .selectByIds("s1", "s2")
1339                    .areAllNotManifest()
1340                    .areAllMutable()
1341                    .areAllDynamic()
1342
1343                    .revertToOriginalList()
1344                    .selectByIds("s3")
1345                    .areAllNotManifest()
1346                    .areAllMutable()
1347                    .areAllEnabled()
1348                    .areAllNotDynamic()
1349
1350                    .revertToOriginalList()
1351                    .selectByIds("s1", "ms1")
1352                    .areAllNotPinned()
1353
1354                    .revertToOriginalList()
1355                    .selectByIds("s2", "s3", "ms2", "ms3")
1356                    .areAllPinned()
1357            ;
1358        });
1359
1360        // Finally, actual tests.
1361        runWithCaller(LAUNCHER_1, USER_0, () -> {
1362            assertWith(mLauncherApps.getShortcuts(
1363                    buildQueryWithFlags(ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0))
1364                    .haveIds("s1", "s2");
1365            assertWith(mLauncherApps.getShortcuts(
1366                    buildQueryWithFlags(ShortcutQuery.FLAG_GET_MANIFEST), HANDLE_USER_0))
1367                    .haveIds("ms1", "ms2");
1368            assertWith(mLauncherApps.getShortcuts(
1369                    buildQueryWithFlags(ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0))
1370                    .haveIds("s2", "s3", "ms2", "ms3");
1371
1372            assertWith(mLauncherApps.getShortcuts(
1373                    buildQueryWithFlags(
1374                            ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_PINNED
1375                    ), HANDLE_USER_0))
1376                    .haveIds("s1", "s2", "s3", "ms2", "ms3");
1377
1378            assertWith(mLauncherApps.getShortcuts(
1379                    buildQueryWithFlags(
1380                            ShortcutQuery.FLAG_GET_MANIFEST | ShortcutQuery.FLAG_GET_PINNED
1381                    ), HANDLE_USER_0))
1382                    .haveIds("ms1", "s2", "s3", "ms2", "ms3");
1383
1384            assertWith(mLauncherApps.getShortcuts(
1385                    buildQueryWithFlags(
1386                            ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_MANIFEST
1387                    ), HANDLE_USER_0))
1388                    .haveIds("ms1", "ms2", "s1", "s2");
1389
1390            assertWith(mLauncherApps.getShortcuts(
1391                    buildQueryWithFlags(
1392                            ShortcutQuery.FLAG_GET_ALL_KINDS
1393                    ), HANDLE_USER_0))
1394                    .haveIds("ms1", "ms2", "ms3", "s1", "s2", "s3");
1395        });
1396    }
1397
1398    public void testGetShortcuts_resolveStrings() throws Exception {
1399        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1400            ShortcutInfo si = new ShortcutInfo.Builder(mClientContext)
1401                    .setId("id")
1402                    .setActivity(new ComponentName(mClientContext, "dummy"))
1403                    .setTitleResId(10)
1404                    .setTextResId(11)
1405                    .setDisabledMessageResId(12)
1406                    .setIntent(makeIntent("action", ShortcutActivity.class))
1407                    .build();
1408            mManager.setDynamicShortcuts(list(si));
1409        });
1410        runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
1411            ShortcutInfo si = new ShortcutInfo.Builder(mClientContext)
1412                    .setId("id")
1413                    .setActivity(new ComponentName(mClientContext, "dummy"))
1414                    .setTitleResId(10)
1415                    .setTextResId(11)
1416                    .setDisabledMessageResId(12)
1417                    .setIntent(makeIntent("action", ShortcutActivity.class))
1418                    .build();
1419            mManager.setDynamicShortcuts(list(si));
1420        });
1421
1422        runWithCaller(LAUNCHER_1, USER_0, () -> {
1423            final ShortcutQuery q = new ShortcutQuery();
1424            q.setQueryFlags(ShortcutQuery.FLAG_GET_DYNAMIC);
1425
1426            // USER 0
1427            List<ShortcutInfo> ret = assertShortcutIds(
1428                    assertAllStringsResolved(mLauncherApps.getShortcuts(q, HANDLE_USER_0)),
1429                    "id");
1430            assertEquals("string-com.android.test.1-user:0-res:10/en", ret.get(0).getTitle());
1431            assertEquals("string-com.android.test.1-user:0-res:11/en", ret.get(0).getText());
1432            assertEquals("string-com.android.test.1-user:0-res:12/en",
1433                    ret.get(0).getDisabledMessage());
1434
1435            // USER P0
1436            ret = assertShortcutIds(
1437                    assertAllStringsResolved(mLauncherApps.getShortcuts(q, HANDLE_USER_P0)),
1438                    "id");
1439            assertEquals("string-com.android.test.1-user:20-res:10/en", ret.get(0).getTitle());
1440            assertEquals("string-com.android.test.1-user:20-res:11/en", ret.get(0).getText());
1441            assertEquals("string-com.android.test.1-user:20-res:12/en",
1442                    ret.get(0).getDisabledMessage());
1443        });
1444    }
1445
1446    // TODO resource
1447    public void testGetShortcutInfo() {
1448        // Create shortcuts.
1449        setCaller(CALLING_PACKAGE_1);
1450        final ShortcutInfo s1_1 = makeShortcut(
1451                "s1",
1452                "Title 1",
1453                makeComponent(ShortcutActivity.class),
1454                /* icon =*/ null,
1455                makeIntent(Intent.ACTION_ASSIST, ShortcutActivity2.class,
1456                        "key1", "val1", "nest", makeBundle("key", 123)),
1457                /* weight */ 10);
1458
1459        final ShortcutInfo s1_2 = makeShortcut(
1460                "s2",
1461                "Title 2",
1462                /* activity */ null,
1463                /* icon =*/ null,
1464                makeIntent(Intent.ACTION_ASSIST, ShortcutActivity3.class),
1465                /* weight */ 12);
1466
1467        assertTrue(mManager.setDynamicShortcuts(list(s1_1, s1_2)));
1468        dumpsysOnLogcat();
1469
1470        setCaller(CALLING_PACKAGE_2);
1471        final ShortcutInfo s2_1 = makeShortcut(
1472                "s1",
1473                "ABC",
1474                makeComponent(ShortcutActivity2.class),
1475                /* icon =*/ null,
1476                makeIntent(Intent.ACTION_ANSWER, ShortcutActivity2.class,
1477                        "key1", "val1", "nest", makeBundle("key", 123)),
1478                /* weight */ 10);
1479        assertTrue(mManager.setDynamicShortcuts(list(s2_1)));
1480        dumpsysOnLogcat();
1481
1482        // Pin some.
1483        setCaller(LAUNCHER_1);
1484
1485        mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
1486                list("s2"), getCallingUser());
1487
1488        dumpsysOnLogcat();
1489
1490        // Delete some.
1491        setCaller(CALLING_PACKAGE_1);
1492        assertShortcutIds(mManager.getPinnedShortcuts(), "s2");
1493        mManager.removeDynamicShortcuts(list("s2"));
1494        assertShortcutIds(mManager.getPinnedShortcuts(), "s2");
1495
1496        dumpsysOnLogcat();
1497
1498        setCaller(LAUNCHER_1);
1499        List<ShortcutInfo> list;
1500
1501        // Note we don't guarantee the orders.
1502        list = assertShortcutIds(assertAllHaveTitle(assertAllNotHaveIntents(
1503                assertAllNotKeyFieldsOnly(
1504                        mLauncherApps.getShortcutInfo(CALLING_PACKAGE_1,
1505                                list("s2", "s1", "s3", null), getCallingUser())))),
1506                "s1", "s2");
1507        assertEquals("Title 1", findById(list, "s1").getTitle());
1508        assertEquals("Title 2", findById(list, "s2").getTitle());
1509
1510        assertShortcutIds(assertAllHaveTitle(assertAllNotHaveIntents(
1511                mLauncherApps.getShortcutInfo(CALLING_PACKAGE_1,
1512                        list("s3"), getCallingUser())))
1513                /* none */);
1514
1515        list = assertShortcutIds(assertAllHaveTitle(assertAllNotHaveIntents(
1516                mLauncherApps.getShortcutInfo(CALLING_PACKAGE_2,
1517                        list("s1", "s2", "s3"), getCallingUser()))),
1518                "s1");
1519        assertEquals("ABC", findById(list, "s1").getTitle());
1520    }
1521
1522    public void testPinShortcutAndGetPinnedShortcuts() {
1523        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1524            final ShortcutInfo s1_1 = makeShortcutWithTimestamp("s1", 1000);
1525            final ShortcutInfo s1_2 = makeShortcutWithTimestamp("s2", 2000);
1526
1527            assertTrue(mManager.setDynamicShortcuts(list(s1_1, s1_2)));
1528        });
1529
1530        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
1531            final ShortcutInfo s2_2 = makeShortcutWithTimestamp("s2", 1500);
1532            final ShortcutInfo s2_3 = makeShortcutWithTimestamp("s3", 3000);
1533            final ShortcutInfo s2_4 = makeShortcutWithTimestamp("s4", 500);
1534            assertTrue(mManager.setDynamicShortcuts(list(s2_2, s2_3, s2_4)));
1535        });
1536
1537        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
1538            final ShortcutInfo s3_2 = makeShortcutWithTimestamp("s2", 1000);
1539            assertTrue(mManager.setDynamicShortcuts(list(s3_2)));
1540        });
1541
1542        // Pin some.
1543        runWithCaller(LAUNCHER_1, USER_0, () -> {
1544            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
1545                    list("s2", "s3"), getCallingUser());
1546
1547            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
1548                    list("s3", "s4", "s5"), getCallingUser());
1549
1550            mLauncherApps.pinShortcuts(CALLING_PACKAGE_3,
1551                    list("s3"), getCallingUser());  // Note ID doesn't exist
1552        });
1553
1554        // Delete some.
1555        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1556            assertShortcutIds(mManager.getPinnedShortcuts(), "s2");
1557            mManager.removeDynamicShortcuts(list("s2"));
1558            assertShortcutIds(mManager.getPinnedShortcuts(), "s2");
1559
1560            assertShortcutIds(mManager.getDynamicShortcuts(), "s1");
1561        });
1562
1563        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
1564            assertShortcutIds(mManager.getPinnedShortcuts(), "s3", "s4");
1565            mManager.removeDynamicShortcuts(list("s3"));
1566            assertShortcutIds(mManager.getPinnedShortcuts(), "s3", "s4");
1567
1568            assertShortcutIds(mManager.getDynamicShortcuts(), "s2", "s4");
1569        });
1570
1571        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
1572            assertShortcutIds(mManager.getPinnedShortcuts() /* none */);
1573            mManager.removeDynamicShortcuts(list("s2"));
1574            assertShortcutIds(mManager.getPinnedShortcuts() /* none */);
1575
1576            assertEmpty(mManager.getDynamicShortcuts());
1577        });
1578
1579        // Get pinned shortcuts from launcher
1580        runWithCaller(LAUNCHER_1, USER_0, () -> {
1581            // CALLING_PACKAGE_1 deleted s2, but it's pinned, so it still exists.
1582            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(assertAllEnabled(
1583                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1584                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser())))),
1585                    "s2");
1586
1587            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(assertAllEnabled(
1588                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
1589                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser())))),
1590                    "s3", "s4");
1591
1592            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(assertAllEnabled(
1593                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_3,
1594                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))))
1595                    /* none */);
1596        });
1597    }
1598
1599    /**
1600     * This is similar to the above test, except it used "disable" instead of "remove".  It also
1601     * does "enable".
1602     */
1603    public void testDisableAndEnableShortcuts() {
1604        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1605            final ShortcutInfo s1_1 = makeShortcutWithTimestamp("s1", 1000);
1606            final ShortcutInfo s1_2 = makeShortcutWithTimestamp("s2", 2000);
1607
1608            assertTrue(mManager.setDynamicShortcuts(list(s1_1, s1_2)));
1609        });
1610
1611        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
1612            final ShortcutInfo s2_2 = makeShortcutWithTimestamp("s2", 1500);
1613            final ShortcutInfo s2_3 = makeShortcutWithTimestamp("s3", 3000);
1614            final ShortcutInfo s2_4 = makeShortcutWithTimestamp("s4", 500);
1615            assertTrue(mManager.setDynamicShortcuts(list(s2_2, s2_3, s2_4)));
1616        });
1617
1618        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
1619            final ShortcutInfo s3_2 = makeShortcutWithTimestamp("s2", 1000);
1620            assertTrue(mManager.setDynamicShortcuts(list(s3_2)));
1621        });
1622
1623        // Pin some.
1624        runWithCaller(LAUNCHER_1, USER_0, () -> {
1625            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
1626                    list("s2", "s3"), getCallingUser());
1627
1628            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
1629                    list("s3", "s4", "s5"), getCallingUser());
1630
1631            mLauncherApps.pinShortcuts(CALLING_PACKAGE_3,
1632                    list("s3"), getCallingUser());  // Note ID doesn't exist
1633        });
1634
1635        // Disable some.
1636        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1637            assertShortcutIds(mManager.getPinnedShortcuts(), "s2");
1638
1639            mManager.disableShortcuts(list("s2"));
1640
1641            assertShortcutIds(mManager.getPinnedShortcuts(), "s2");
1642            assertShortcutIds(mManager.getDynamicShortcuts(), "s1");
1643        });
1644
1645        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
1646            assertShortcutIds(mManager.getPinnedShortcuts(), "s3", "s4");
1647
1648            // disable should work even if a shortcut is not dynamic, so try calling "remove" first
1649            // here.
1650            mManager.removeDynamicShortcuts(list("s3"));
1651            mManager.disableShortcuts(list("s3"));
1652
1653            assertShortcutIds(mManager.getPinnedShortcuts(), "s3", "s4");
1654            assertShortcutIds(mManager.getDynamicShortcuts(), "s2", "s4");
1655        });
1656
1657        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
1658            assertShortcutIds(mManager.getPinnedShortcuts() /* none */);
1659
1660            mManager.disableShortcuts(list("s2"));
1661
1662            assertShortcutIds(mManager.getPinnedShortcuts() /* none */);
1663
1664            assertEmpty(mManager.getDynamicShortcuts());
1665            assertEmpty(getCallerShortcuts());
1666        });
1667
1668        // Get pinned shortcuts from launcher
1669        runWithCaller(LAUNCHER_1, USER_0, () -> {
1670            // CALLING_PACKAGE_1 deleted s2, but it's pinned, so it still exists, and disabled.
1671            assertWith(mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1672                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))
1673                    .haveIds("s2")
1674                    .areAllPinned()
1675                    .areAllNotWithKeyFieldsOnly()
1676                    .areAllDisabled();
1677            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_0,
1678                    ActivityNotFoundException.class);
1679
1680            // Here, s4 is still enabled and launchable, but s3 is disabled.
1681            assertWith(mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
1682                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))
1683                    .haveIds("s3", "s4")
1684                    .areAllPinned()
1685                    .areAllNotWithKeyFieldsOnly()
1686
1687                    .selectByIds("s3")
1688                    .areAllDisabled()
1689
1690                    .revertToOriginalList()
1691                    .selectByIds("s4")
1692                    .areAllEnabled();
1693
1694            assertStartShortcutThrowsException(CALLING_PACKAGE_2, "s3", USER_0,
1695                    ActivityNotFoundException.class);
1696            assertShortcutLaunchable(CALLING_PACKAGE_2, "s4", USER_0);
1697
1698            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(assertAllEnabled(
1699                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_3,
1700                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))))
1701                    /* none */);
1702        });
1703
1704        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1705            mManager.enableShortcuts(list("s2"));
1706
1707            assertShortcutIds(mManager.getPinnedShortcuts(), "s2");
1708            assertShortcutIds(mManager.getDynamicShortcuts(), "s1");
1709        });
1710        runWithCaller(LAUNCHER_1, USER_0, () -> {
1711            // CALLING_PACKAGE_1 deleted s2, but it's pinned, so it still exists.
1712            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(assertAllEnabled(
1713                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1714                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser())))),
1715                    "s2");
1716            assertShortcutLaunchable(CALLING_PACKAGE_1, "s2", USER_0);
1717        });
1718    }
1719
1720    public void testDisableShortcuts_thenRepublish() {
1721        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1722            assertTrue(mManager.setDynamicShortcuts(list(
1723                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
1724
1725            runWithCaller(LAUNCHER_1, USER_0, () -> {
1726                mLauncherApps.pinShortcuts(
1727                        CALLING_PACKAGE_1, list("s1", "s2", "s3"), HANDLE_USER_0);
1728            });
1729
1730            mManager.disableShortcuts(list("s1", "s2", "s3"));
1731
1732            assertWith(getCallerShortcuts())
1733                    .haveIds("s1", "s2", "s3")
1734                    .areAllNotDynamic()
1735                    .areAllPinned()
1736                    .areAllDisabled();
1737
1738            // Make sure updateShortcuts() will not re-enable them.
1739            assertTrue(mManager.updateShortcuts(list(
1740                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
1741
1742            assertWith(getCallerShortcuts())
1743                    .haveIds("s1", "s2", "s3")
1744                    .areAllNotDynamic()
1745                    .areAllPinned()
1746                    .areAllDisabled();
1747
1748            // Re-publish s1 with setDynamicShortcuts.
1749            mInjectedCurrentTimeMillis += INTERVAL; // reset throttling
1750
1751            assertTrue(mManager.setDynamicShortcuts(list(
1752                    makeShortcut("s1"))));
1753
1754            assertWith(getCallerShortcuts())
1755                    .haveIds("s1", "s2", "s3")
1756
1757                    .selectByIds("s1")
1758                    .areAllDynamic()
1759                    .areAllPinned()
1760                    .areAllEnabled()
1761
1762                    .revertToOriginalList()
1763                    .selectByIds("s2", "s3")
1764                    .areAllNotDynamic()
1765                    .areAllPinned()
1766                    .areAllDisabled();
1767
1768            // Re-publish s2 with addDynamicShortcuts.
1769            mInjectedCurrentTimeMillis += INTERVAL; // reset throttling
1770
1771            assertTrue(mManager.addDynamicShortcuts(list(
1772                    makeShortcut("s2"))));
1773
1774            assertWith(getCallerShortcuts())
1775                    .haveIds("s1", "s2", "s3")
1776
1777                    .selectByIds("s1", "s2")
1778                    .areAllDynamic()
1779                    .areAllPinned()
1780                    .areAllEnabled()
1781
1782                    .revertToOriginalList()
1783                    .selectByIds("s3")
1784                    .areAllNotDynamic()
1785                    .areAllPinned()
1786                    .areAllDisabled();
1787        });
1788    }
1789
1790    public void testPinShortcutAndGetPinnedShortcuts_multi() {
1791        // Create some shortcuts.
1792        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1793            assertTrue(mManager.setDynamicShortcuts(list(
1794                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
1795        });
1796
1797        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
1798            assertTrue(mManager.setDynamicShortcuts(list(
1799                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
1800        });
1801
1802        dumpsysOnLogcat();
1803
1804        // Pin some.
1805        runWithCaller(LAUNCHER_1, USER_0, () -> {
1806            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
1807                    list("s3", "s4"), getCallingUser());
1808
1809            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
1810                    list("s1", "s2", "s4"), getCallingUser());
1811        });
1812
1813        dumpsysOnLogcat();
1814
1815        // Delete some.
1816        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1817            assertShortcutIds(mManager.getPinnedShortcuts(), "s3");
1818            mManager.removeDynamicShortcuts(list("s3"));
1819            assertShortcutIds(mManager.getPinnedShortcuts(), "s3");
1820        });
1821
1822        dumpsysOnLogcat();
1823
1824        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
1825            assertShortcutIds(mManager.getPinnedShortcuts(), "s1", "s2");
1826            mManager.removeDynamicShortcuts(list("s1"));
1827            mManager.removeDynamicShortcuts(list("s3"));
1828            assertShortcutIds(mManager.getPinnedShortcuts(), "s1", "s2");
1829        });
1830
1831        dumpsysOnLogcat();
1832
1833        // Get pinned shortcuts from launcher
1834        runWithCaller(LAUNCHER_1, USER_0, () -> {
1835            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
1836                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1837                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
1838                    "s3");
1839
1840            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
1841                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
1842                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
1843                    "s1", "s2");
1844
1845            assertShortcutIds(assertAllDynamicOrPinned(
1846                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1847                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
1848                                    | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
1849                    "s1", "s2", "s3");
1850
1851            assertShortcutIds(assertAllDynamicOrPinned(
1852                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
1853                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
1854                                    | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
1855                    "s1", "s2");
1856        });
1857
1858        dumpsysOnLogcat();
1859
1860        runWithCaller(LAUNCHER_2, USER_0, () -> {
1861            // Launcher2 still has no pinned ones.
1862            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
1863                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1864                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser())))
1865                    /* none */);
1866            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
1867                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
1868                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser())))
1869                    /* none */);
1870
1871            assertShortcutIds(assertAllDynamic(
1872                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1873                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
1874                                    | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
1875                    "s1", "s2");
1876            assertShortcutIds(assertAllDynamic(
1877                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
1878                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
1879                                    | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
1880                    "s2");
1881
1882            // Now pin some.
1883            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
1884                    list("s1", "s2"), getCallingUser());
1885
1886            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
1887                    list("s1", "s2"), getCallingUser());
1888
1889            assertShortcutIds(assertAllDynamic(
1890                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1891                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
1892                                    | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
1893                    "s1", "s2");
1894
1895            // S1 was not visible to it, so shouldn't be pinned.
1896            assertShortcutIds(assertAllDynamic(
1897                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
1898                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
1899                                    | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
1900                    "s2");
1901        });
1902
1903        // Re-initialize and load from the files.
1904        mService.saveDirtyInfo();
1905        initService();
1906
1907        // Load from file.
1908        mService.handleUnlockUser(USER_0);
1909
1910        // Make sure package info is restored too.
1911        runWithCaller(LAUNCHER_1, USER_0, () -> {
1912            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
1913                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1914                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
1915                    "s3");
1916            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
1917                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
1918                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
1919                    "s1", "s2");
1920        });
1921        runWithCaller(LAUNCHER_2, USER_0, () -> {
1922            assertShortcutIds(assertAllDynamic(
1923                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1924                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
1925                                    | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
1926                    "s1", "s2");
1927            assertShortcutIds(assertAllDynamic(
1928                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
1929                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
1930                                    | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
1931                    "s2");
1932        });
1933
1934        // Delete all dynamic.
1935        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1936            mManager.removeAllDynamicShortcuts();
1937
1938            assertEquals(0, mManager.getDynamicShortcuts().size());
1939            assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()), "s1", "s2", "s3");
1940        });
1941        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
1942            mManager.removeAllDynamicShortcuts();
1943
1944            assertEquals(0, mManager.getDynamicShortcuts().size());
1945            assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()), "s2", "s1");
1946        });
1947
1948        runWithCaller(LAUNCHER_1, USER_0, () -> {
1949            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
1950                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1951                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
1952                    "s3");
1953
1954            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
1955                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
1956                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
1957                    "s1", "s2");
1958
1959            // from all packages.
1960            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
1961                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, null,
1962                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
1963                    "s1", "s2", "s3");
1964
1965            // Update pined.  Note s2 and s3 are actually available, but not visible to this
1966            // launcher, so still can't be pinned.
1967            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s1", "s2", "s3", "s4"),
1968                    getCallingUser());
1969
1970            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
1971                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1972                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
1973                    "s3");
1974        });
1975        // Re-publish s1.
1976        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1977            assertTrue(mManager.addDynamicShortcuts(list(makeShortcut("s1"))));
1978
1979            assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()), "s1");
1980            assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()), "s1", "s2", "s3");
1981        });
1982        runWithCaller(LAUNCHER_1, USER_0, () -> {
1983            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
1984                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1985                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
1986                    "s3");
1987
1988            // Now "s1" is visible, so can be pinned.
1989            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s1", "s2", "s3", "s4"),
1990                    getCallingUser());
1991
1992            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
1993                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1994                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
1995                    "s1", "s3");
1996        });
1997
1998        // Now clear pinned shortcuts.  First, from launcher 1.
1999        runWithCaller(LAUNCHER_1, USER_0, () -> {
2000            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list(), getCallingUser());
2001            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list(), getCallingUser());
2002
2003            assertEquals(0,
2004                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2005                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()).size());
2006            assertEquals(0,
2007                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2008                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()).size());
2009        });
2010        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2011            assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()), "s1");
2012            assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()), "s1", "s2");
2013        });
2014        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
2015            assertEquals(0, mManager.getDynamicShortcuts().size());
2016            assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()), "s2");
2017        });
2018
2019        // Clear all pins from launcher 2.
2020        runWithCaller(LAUNCHER_2, USER_0, () -> {
2021            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list(), getCallingUser());
2022            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list(), getCallingUser());
2023
2024            assertEquals(0,
2025                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2026                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()).size());
2027            assertEquals(0,
2028                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2029                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()).size());
2030        });
2031        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2032            assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()), "s1");
2033            assertEquals(0, mManager.getPinnedShortcuts().size());
2034        });
2035        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
2036            assertEquals(0, mManager.getDynamicShortcuts().size());
2037            assertEquals(0, mManager.getPinnedShortcuts().size());
2038        });
2039    }
2040
2041    public void testPinShortcutAndGetPinnedShortcuts_crossProfile_plusLaunch() {
2042        // Create some shortcuts.
2043        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2044            assertTrue(mManager.setDynamicShortcuts(list(
2045                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2046        });
2047        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
2048            assertTrue(mManager.setDynamicShortcuts(list(
2049                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2050        });
2051        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
2052            assertTrue(mManager.setDynamicShortcuts(list(
2053                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"),
2054                    makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6"))));
2055        });
2056
2057        // Pin some shortcuts and see the result.
2058
2059        runWithCaller(LAUNCHER_1, USER_0, () -> {
2060            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
2061                    list("s1"), HANDLE_USER_0);
2062
2063            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
2064                    list("s1", "s2", "s3"), HANDLE_USER_0);
2065        });
2066
2067        runWithCaller(LAUNCHER_1, USER_P0, () -> {
2068            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
2069                    list("s2"), HANDLE_USER_0);
2070
2071            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
2072                    list("s2", "s3"), HANDLE_USER_0);
2073        });
2074
2075        runWithCaller(LAUNCHER_2, USER_P0, () -> {
2076            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
2077                    list("s3"), HANDLE_USER_0);
2078
2079            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
2080                    list("s3"), HANDLE_USER_0);
2081        });
2082
2083        runWithCaller(LAUNCHER_2, USER_10, () -> {
2084            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
2085                    list("s1", "s2", "s3"), HANDLE_USER_10);
2086        });
2087
2088        // Cross profile pinning.
2089        final int PIN_AND_DYNAMIC = ShortcutQuery.FLAG_GET_PINNED | ShortcutQuery.FLAG_GET_DYNAMIC;
2090
2091        runWithCaller(LAUNCHER_1, USER_0, () -> {
2092            assertShortcutIds(assertAllPinned(
2093                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2094                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2095                    "s1");
2096            assertShortcutIds(assertAllDynamic(
2097                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2098                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2099                    "s1", "s2", "s3");
2100            assertShortcutIds(assertAllDynamicOrPinned(
2101                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2102                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2103                    "s1", "s2", "s3");
2104
2105            assertShortcutIds(assertAllPinned(
2106                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2107                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2108                    "s1", "s2", "s3");
2109            assertShortcutIds(assertAllDynamic(
2110                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2111                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2112                    "s1", "s2", "s3");
2113            assertShortcutIds(assertAllDynamicOrPinned(
2114                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2115                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2116                    "s1", "s2", "s3");
2117
2118            assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
2119            assertShortcutLaunchable(CALLING_PACKAGE_1, "s2", USER_0);
2120            assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_0);
2121
2122            assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
2123            assertShortcutLaunchable(CALLING_PACKAGE_2, "s2", USER_0);
2124            assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2125
2126            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
2127                    SecurityException.class);
2128            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
2129                    SecurityException.class);
2130            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
2131                    SecurityException.class);
2132            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
2133                    SecurityException.class);
2134            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
2135                    SecurityException.class);
2136            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
2137                    SecurityException.class);
2138        });
2139        runWithCaller(LAUNCHER_1, USER_P0, () -> {
2140            assertShortcutIds(assertAllPinned(
2141                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2142                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2143                    "s2");
2144            assertShortcutIds(assertAllDynamic(
2145                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2146                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2147                    "s1", "s2", "s3");
2148            assertShortcutIds(assertAllDynamicOrPinned(
2149                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2150                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2151                    "s1", "s2", "s3");
2152
2153            assertShortcutIds(assertAllPinned(
2154                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2155                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2156                    "s2", "s3");
2157            assertShortcutIds(assertAllDynamic(
2158                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2159                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2160                    "s1", "s2", "s3");
2161            assertShortcutIds(assertAllDynamicOrPinned(
2162                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2163                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2164                    "s1", "s2", "s3");
2165
2166            assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
2167            assertShortcutLaunchable(CALLING_PACKAGE_1, "s2", USER_0);
2168            assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_0);
2169
2170            assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
2171            assertShortcutLaunchable(CALLING_PACKAGE_2, "s2", USER_0);
2172            assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2173
2174            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
2175                    SecurityException.class);
2176            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
2177                    SecurityException.class);
2178            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
2179                    SecurityException.class);
2180            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
2181                    SecurityException.class);
2182            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
2183                    SecurityException.class);
2184            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
2185                    SecurityException.class);
2186        });
2187        runWithCaller(LAUNCHER_2, USER_P0, () -> {
2188            assertShortcutIds(assertAllPinned(
2189                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2190                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2191                    "s3");
2192            assertShortcutIds(assertAllDynamic(
2193                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2194                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2195                    "s1", "s2", "s3");
2196            assertShortcutIds(assertAllDynamicOrPinned(
2197                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2198                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2199                    "s1", "s2", "s3");
2200
2201            assertShortcutIds(assertAllPinned(
2202                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2203                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2204                    "s3");
2205            assertShortcutIds(assertAllDynamic(
2206                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2207                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2208                    "s1", "s2", "s3");
2209            assertShortcutIds(assertAllDynamicOrPinned(
2210                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2211                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2212                    "s1", "s2", "s3");
2213
2214            assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
2215            assertShortcutLaunchable(CALLING_PACKAGE_1, "s2", USER_0);
2216            assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_0);
2217
2218            assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
2219            assertShortcutLaunchable(CALLING_PACKAGE_2, "s2", USER_0);
2220            assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2221
2222            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
2223                    SecurityException.class);
2224            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
2225                    SecurityException.class);
2226            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
2227                    SecurityException.class);
2228            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
2229                    SecurityException.class);
2230            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
2231                    SecurityException.class);
2232            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
2233                    SecurityException.class);
2234        });
2235        runWithCaller(LAUNCHER_2, USER_10, () -> {
2236            assertShortcutIds(assertAllPinned(
2237                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2238                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_10)),
2239                    "s1", "s2", "s3");
2240            assertShortcutIds(assertAllDynamic(
2241                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2242                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_10)),
2243                    "s1", "s2", "s3", "s4", "s5", "s6");
2244            assertShortcutIds(assertAllDynamicOrPinned(
2245                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2246                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_10)),
2247                    "s1", "s2", "s3", "s4", "s5", "s6");
2248        });
2249
2250        // Remove some dynamic shortcuts.
2251
2252        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2253            assertTrue(mManager.setDynamicShortcuts(list(
2254                    makeShortcut("s1"))));
2255        });
2256        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
2257            assertTrue(mManager.setDynamicShortcuts(list(
2258                    makeShortcut("s1"))));
2259        });
2260        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
2261            assertTrue(mManager.setDynamicShortcuts(list(
2262                    makeShortcut("s1"))));
2263        });
2264
2265        runWithCaller(LAUNCHER_1, USER_0, () -> {
2266            assertShortcutIds(assertAllPinned(
2267                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2268                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2269                    "s1");
2270            assertShortcutIds(assertAllDynamic(
2271                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2272                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2273                    "s1");
2274            assertShortcutIds(assertAllDynamicOrPinned(
2275                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2276                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2277                    "s1");
2278
2279            assertShortcutIds(assertAllPinned(
2280                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2281                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2282                    "s1", "s2", "s3");
2283            assertShortcutIds(assertAllDynamic(
2284                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2285                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2286                    "s1");
2287            assertShortcutIds(assertAllDynamicOrPinned(
2288                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2289                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2290                    "s1", "s2", "s3");
2291
2292            assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
2293            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_0,
2294                    ActivityNotFoundException.class);
2295            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_0,
2296                    ActivityNotFoundException.class);
2297
2298            assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
2299            assertShortcutLaunchable(CALLING_PACKAGE_2, "s2", USER_0);
2300            assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2301
2302            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
2303                    SecurityException.class);
2304            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
2305                    SecurityException.class);
2306            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
2307                    SecurityException.class);
2308            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
2309                    SecurityException.class);
2310            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
2311                    SecurityException.class);
2312            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
2313                    SecurityException.class);
2314        });
2315        runWithCaller(LAUNCHER_1, USER_P0, () -> {
2316            assertShortcutIds(assertAllPinned(
2317                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2318                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2319                    "s2");
2320            assertShortcutIds(assertAllDynamic(
2321                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2322                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2323                    "s1");
2324            assertShortcutIds(assertAllDynamicOrPinned(
2325                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2326                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2327                    "s1", "s2");
2328
2329            assertShortcutIds(assertAllPinned(
2330                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2331                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2332                    "s2", "s3");
2333            assertShortcutIds(assertAllDynamic(
2334                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2335                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2336                    "s1");
2337            assertShortcutIds(assertAllDynamicOrPinned(
2338                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2339                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2340                    "s1", "s2", "s3");
2341
2342            assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
2343            assertShortcutLaunchable(CALLING_PACKAGE_1, "s2", USER_0);
2344            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_0,
2345                    ActivityNotFoundException.class);
2346
2347            assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
2348            assertShortcutLaunchable(CALLING_PACKAGE_2, "s2", USER_0);
2349            assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2350
2351            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
2352                    SecurityException.class);
2353            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
2354                    SecurityException.class);
2355            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
2356                    SecurityException.class);
2357            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
2358                    SecurityException.class);
2359            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
2360                    SecurityException.class);
2361            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
2362                    SecurityException.class);
2363        });
2364        runWithCaller(LAUNCHER_2, USER_P0, () -> {
2365            assertShortcutIds(assertAllPinned(
2366                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2367                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2368                    "s3");
2369            assertShortcutIds(assertAllDynamic(
2370                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2371                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2372                    "s1");
2373            assertShortcutIds(assertAllDynamicOrPinned(
2374                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2375                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2376                    "s1", "s3");
2377
2378            assertShortcutIds(assertAllPinned(
2379                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2380                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2381                    "s3");
2382            assertShortcutIds(assertAllDynamic(
2383                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2384                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2385                    "s1");
2386            assertShortcutIds(assertAllDynamicOrPinned(
2387                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2388                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2389                    "s1", "s3");
2390
2391            assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
2392            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_0,
2393                    ActivityNotFoundException.class);
2394            assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_0);
2395
2396            assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
2397            assertStartShortcutThrowsException(CALLING_PACKAGE_2, "s2", USER_0,
2398                    ActivityNotFoundException.class);
2399            assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2400
2401            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
2402                    SecurityException.class);
2403            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
2404                    SecurityException.class);
2405            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
2406                    SecurityException.class);
2407            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
2408                    SecurityException.class);
2409            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
2410                    SecurityException.class);
2411            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
2412                    SecurityException.class);
2413        });
2414        runWithCaller(LAUNCHER_2, USER_10, () -> {
2415            assertShortcutIds(assertAllPinned(
2416                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2417                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_10)),
2418                    "s1", "s2", "s3");
2419            assertShortcutIds(assertAllDynamic(
2420                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2421                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_10)),
2422                    "s1");
2423            assertShortcutIds(assertAllDynamicOrPinned(
2424                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2425                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_10)),
2426                    "s1", "s2", "s3");
2427
2428            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_0,
2429                    SecurityException.class);
2430            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_0,
2431                    SecurityException.class);
2432            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_0,
2433                    SecurityException.class);
2434
2435            assertStartShortcutThrowsException(CALLING_PACKAGE_2, "s1", USER_0,
2436                    SecurityException.class);
2437            assertStartShortcutThrowsException(CALLING_PACKAGE_2, "s2", USER_0,
2438                    SecurityException.class);
2439            assertStartShortcutThrowsException(CALLING_PACKAGE_2, "s3", USER_0,
2440                    SecurityException.class);
2441
2442            assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_10);
2443            assertShortcutLaunchable(CALLING_PACKAGE_1, "s2", USER_10);
2444            assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_10);
2445            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
2446                    ActivityNotFoundException.class);
2447            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
2448                    ActivityNotFoundException.class);
2449            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
2450                    ActivityNotFoundException.class);
2451        });
2452
2453        // Save & load and make sure we still have the same information.
2454        mService.saveDirtyInfo();
2455        initService();
2456        mService.handleUnlockUser(USER_0);
2457
2458        runWithCaller(LAUNCHER_1, USER_0, () -> {
2459            assertShortcutIds(assertAllPinned(
2460                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2461                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2462                    "s1");
2463            assertShortcutIds(assertAllDynamic(
2464                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2465                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2466                    "s1");
2467            assertShortcutIds(assertAllDynamicOrPinned(
2468                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2469                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2470                    "s1");
2471
2472            assertShortcutIds(assertAllPinned(
2473                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2474                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2475                    "s1", "s2", "s3");
2476            assertShortcutIds(assertAllDynamic(
2477                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2478                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2479                    "s1");
2480            assertShortcutIds(assertAllDynamicOrPinned(
2481                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2482                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2483                    "s1", "s2", "s3");
2484
2485            assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
2486            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_0,
2487                    ActivityNotFoundException.class);
2488            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_0,
2489                    ActivityNotFoundException.class);
2490
2491            assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
2492            assertShortcutLaunchable(CALLING_PACKAGE_2, "s2", USER_0);
2493            assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2494
2495            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
2496                    SecurityException.class);
2497            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
2498                    SecurityException.class);
2499            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
2500                    SecurityException.class);
2501            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
2502                    SecurityException.class);
2503            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
2504                    SecurityException.class);
2505            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
2506                    SecurityException.class);
2507        });
2508        runWithCaller(LAUNCHER_1, USER_P0, () -> {
2509            assertShortcutIds(assertAllPinned(
2510                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2511                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2512                    "s2");
2513            assertShortcutIds(assertAllDynamic(
2514                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2515                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2516                    "s1");
2517            assertShortcutIds(assertAllDynamicOrPinned(
2518                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2519                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2520                    "s1", "s2");
2521
2522            assertShortcutIds(assertAllPinned(
2523                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2524                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2525                    "s2", "s3");
2526            assertShortcutIds(assertAllDynamic(
2527                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2528                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2529                    "s1");
2530            assertShortcutIds(assertAllDynamicOrPinned(
2531                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2532                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2533                    "s1", "s2", "s3");
2534
2535            assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
2536            assertShortcutLaunchable(CALLING_PACKAGE_1, "s2", USER_0);
2537            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_0,
2538                    ActivityNotFoundException.class);
2539
2540            assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
2541            assertShortcutLaunchable(CALLING_PACKAGE_2, "s2", USER_0);
2542            assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2543
2544            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
2545                    SecurityException.class);
2546            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
2547                    SecurityException.class);
2548            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
2549                    SecurityException.class);
2550            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
2551                    SecurityException.class);
2552            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
2553                    SecurityException.class);
2554            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
2555                    SecurityException.class);
2556        });
2557        runWithCaller(LAUNCHER_2, USER_P0, () -> {
2558            assertShortcutIds(assertAllPinned(
2559                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2560                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2561                    "s3");
2562            assertShortcutIds(assertAllDynamic(
2563                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2564                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2565                    "s1");
2566            assertShortcutIds(assertAllDynamicOrPinned(
2567                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2568                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2569                    "s1", "s3");
2570
2571            assertShortcutIds(assertAllPinned(
2572                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2573                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2574                    "s3");
2575            assertShortcutIds(assertAllDynamic(
2576                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2577                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2578                    "s1");
2579            assertShortcutIds(assertAllDynamicOrPinned(
2580                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2581                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2582                    "s1", "s3");
2583
2584            assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
2585            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_0,
2586                    ActivityNotFoundException.class);
2587            assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_0);
2588
2589            assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
2590            assertStartShortcutThrowsException(CALLING_PACKAGE_2, "s2", USER_0,
2591                    ActivityNotFoundException.class);
2592            assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2593
2594            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
2595                    SecurityException.class);
2596            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
2597                    SecurityException.class);
2598            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
2599                    SecurityException.class);
2600            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
2601                    SecurityException.class);
2602            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
2603                    SecurityException.class);
2604            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
2605                    SecurityException.class);
2606        });
2607    }
2608
2609    public void testStartShortcut() {
2610        // Create some shortcuts.
2611        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2612            final ShortcutInfo s1_1 = makeShortcut(
2613                    "s1",
2614                    "Title 1",
2615                    makeComponent(ShortcutActivity.class),
2616            /* icon =*/ null,
2617                    makeIntent(Intent.ACTION_ASSIST, ShortcutActivity2.class,
2618                            "key1", "val1", "nest", makeBundle("key", 123)),
2619            /* rank */ 10);
2620
2621            final ShortcutInfo s1_2 = makeShortcut(
2622                    "s2",
2623                    "Title 2",
2624            /* activity */ null,
2625            /* icon =*/ null,
2626                    makeIntent(Intent.ACTION_ASSIST, ShortcutActivity3.class),
2627            /* rank */ 12);
2628
2629            final ShortcutInfo s1_3 = makeShortcut("s3");
2630
2631            assertTrue(mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3)));
2632        });
2633
2634        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
2635            final ShortcutInfo s2_1 = makeShortcut(
2636                    "s1",
2637                    "ABC",
2638                    makeComponent(ShortcutActivity.class),
2639                    /* icon =*/ null,
2640                    makeIntent(Intent.ACTION_ANSWER, ShortcutActivity.class,
2641                            "key1", "val1", "nest", makeBundle("key", 123)),
2642                    /* weight */ 10);
2643            assertTrue(mManager.setDynamicShortcuts(list(s2_1)));
2644        });
2645
2646        // Pin some.
2647        runWithCaller(LAUNCHER_1, USER_0, () -> {
2648            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
2649                    list("s1", "s2"), getCallingUser());
2650
2651            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
2652                    list("s1"), getCallingUser());
2653        });
2654
2655        // Just to make it complicated, delete some.
2656        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2657            mManager.removeDynamicShortcuts(list("s2"));
2658        });
2659
2660        runWithCaller(LAUNCHER_1, USER_0, () -> {
2661            assertEquals(
2662                    ShortcutActivity2.class.getName(),
2663                    launchShortcutAndGetIntent(CALLING_PACKAGE_1, "s1", USER_0)
2664                            .getComponent().getClassName());
2665            assertEquals(
2666                    ShortcutActivity3.class.getName(),
2667                    launchShortcutAndGetIntent(CALLING_PACKAGE_1, "s2", USER_0)
2668                            .getComponent().getClassName());
2669            assertEquals(
2670                    ShortcutActivity.class.getName(),
2671                    launchShortcutAndGetIntent(CALLING_PACKAGE_2, "s1", USER_0)
2672                            .getComponent().getClassName());
2673
2674            assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_0);
2675
2676            assertShortcutNotLaunched("no-such-package", "s2", USER_0);
2677            assertShortcutNotLaunched(CALLING_PACKAGE_1, "xxxx", USER_0);
2678        });
2679
2680        // LAUNCHER_1 is no longer the default launcher
2681        setDefaultLauncherChecker((pkg, userId) -> false);
2682
2683        runWithCaller(LAUNCHER_1, USER_0, () -> {
2684            // Not the default launcher, but pinned shortcuts are still lauchable.
2685            assertEquals(
2686                    ShortcutActivity2.class.getName(),
2687                    launchShortcutAndGetIntent(CALLING_PACKAGE_1, "s1", USER_0)
2688                            .getComponent().getClassName());
2689            assertEquals(
2690                    ShortcutActivity3.class.getName(),
2691                    launchShortcutAndGetIntent(CALLING_PACKAGE_1, "s2", USER_0)
2692                            .getComponent().getClassName());
2693            assertEquals(
2694                    ShortcutActivity.class.getName(),
2695                    launchShortcutAndGetIntent(CALLING_PACKAGE_2, "s1", USER_0)
2696                            .getComponent().getClassName());
2697
2698            // Not pinned, so not lauchable.
2699        });
2700
2701        // Test inner errors.
2702        runWithCaller(LAUNCHER_1, USER_0, () -> {
2703            // Not launchable.
2704            doReturn(ActivityManager.START_CLASS_NOT_FOUND)
2705                    .when(mMockActivityManagerInternal).startActivitiesAsPackage(
2706                            anyString(), anyInt(), any(Intent[].class), any(Bundle.class));
2707            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_0,
2708                    ActivityNotFoundException.class);
2709
2710            // Still not launchable.
2711            doReturn(ActivityManager.START_CLASS_NOT_FOUND)
2712                    .when(mMockActivityManagerInternal)
2713                    .startActivitiesAsPackage(
2714                            anyString(), anyInt(), any(Intent[].class), any(Bundle.class));
2715            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_0,
2716                    ActivityNotFoundException.class);
2717        });
2718
2719
2720        // TODO Check extra, etc
2721    }
2722
2723    public void testLauncherCallback() throws Throwable {
2724        // Disable throttling for this test.
2725        mService.updateConfigurationLocked(
2726                ConfigConstants.KEY_MAX_UPDATES_PER_INTERVAL + "=99999999,"
2727                        + ConfigConstants.KEY_MAX_SHORTCUTS + "=99999999"
2728        );
2729
2730        setCaller(LAUNCHER_1, USER_0);
2731
2732        assertForLauncherCallback(mLauncherApps, () -> {
2733            runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2734                assertTrue(mManager.setDynamicShortcuts(list(
2735                        makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2736            });
2737        }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
2738                .haveIds("s1", "s2", "s3")
2739                .areAllWithKeyFieldsOnly()
2740                .areAllDynamic();
2741
2742        // From different package.
2743        assertForLauncherCallback(mLauncherApps, () -> {
2744            runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
2745                assertTrue(mManager.setDynamicShortcuts(list(
2746                        makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2747            });
2748        }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_2, HANDLE_USER_0)
2749                .haveIds("s1", "s2", "s3")
2750                .areAllWithKeyFieldsOnly()
2751                .areAllDynamic();
2752
2753        // Different user, callback shouldn't be called.
2754        assertForLauncherCallback(mLauncherApps, () -> {
2755            runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
2756                assertTrue(mManager.setDynamicShortcuts(list(
2757                        makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2758            });
2759        }).assertNoCallbackCalled();
2760
2761
2762        // Test for addDynamicShortcuts.
2763        assertForLauncherCallback(mLauncherApps, () -> {
2764            runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2765                assertTrue(mManager.addDynamicShortcuts(list(makeShortcut("s4"))));
2766            });
2767        }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
2768                .haveIds("s1", "s2", "s3", "s4")
2769                .areAllWithKeyFieldsOnly()
2770                .areAllDynamic();
2771
2772        // Test for remove
2773        assertForLauncherCallback(mLauncherApps, () -> {
2774            runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2775                mManager.removeDynamicShortcuts(list("s1"));
2776            });
2777        }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
2778                .haveIds("s2", "s3", "s4")
2779                .areAllWithKeyFieldsOnly()
2780                .areAllDynamic();
2781
2782        // Test for update
2783        assertForLauncherCallback(mLauncherApps, () -> {
2784            runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2785                assertTrue(mManager.updateShortcuts(list(
2786                        makeShortcut("s1"), makeShortcut("s2"))));
2787            });
2788        }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
2789                // All remaining shortcuts will be passed regardless of what's been updated.
2790                .haveIds("s2", "s3", "s4")
2791                .areAllWithKeyFieldsOnly()
2792                .areAllDynamic();
2793
2794        // Test for deleteAll
2795        assertForLauncherCallback(mLauncherApps, () -> {
2796            runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2797                mManager.removeAllDynamicShortcuts();
2798            });
2799        }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
2800                .isEmpty();
2801
2802        // Update package1 with manifest shortcuts
2803        assertForLauncherCallback(mLauncherApps, () -> {
2804            addManifestShortcutResource(
2805                    new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
2806                    R.xml.shortcut_2);
2807            updatePackageVersion(CALLING_PACKAGE_1, 1);
2808            mService.mPackageMonitor.onReceive(getTestContext(),
2809                    genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
2810        }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
2811                .areAllManifest()
2812                .areAllWithKeyFieldsOnly()
2813                .haveIds("ms1", "ms2");
2814
2815        // Make sure pinned shortcuts are passed too.
2816        // 1. Add dynamic shortcuts.
2817        runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
2818            assertTrue(mManager.setDynamicShortcuts(list(
2819                    makeShortcut("s1"), makeShortcut("s2"))));
2820        });
2821
2822        // 2. Pin some.
2823        runWithCaller(LAUNCHER_1, UserHandle.USER_SYSTEM, () -> {
2824            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms2", "s2"), HANDLE_USER_0);
2825        });
2826        runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
2827            assertWith(getCallerShortcuts())
2828                    .haveIds("ms1", "ms2", "s1", "s2")
2829                    .areAllEnabled()
2830
2831                    .selectByIds("ms1", "ms2")
2832                    .areAllManifest()
2833
2834                    .revertToOriginalList()
2835                    .selectByIds("s1", "s2")
2836                    .areAllDynamic()
2837                    ;
2838        });
2839
2840        // 3 Update the app with no manifest shortcuts.  (Pinned one will survive.)
2841        addManifestShortcutResource(
2842                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
2843                R.xml.shortcut_0);
2844        updatePackageVersion(CALLING_PACKAGE_1, 1);
2845        mService.mPackageMonitor.onReceive(getTestContext(),
2846                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
2847
2848        assertForLauncherCallback(mLauncherApps, () -> {
2849            runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2850                mManager.removeDynamicShortcuts(list("s2"));
2851
2852                assertWith(getCallerShortcuts())
2853                        .haveIds("ms2", "s1", "s2")
2854
2855                        .selectByIds("ms2")
2856                        .areAllNotManifest()
2857                        .areAllPinned()
2858                        .areAllImmutable()
2859                        .areAllDisabled()
2860
2861                        .revertToOriginalList()
2862                        .selectByIds("s1")
2863                        .areAllDynamic()
2864                        .areAllNotPinned()
2865                        .areAllEnabled()
2866
2867                        .revertToOriginalList()
2868                        .selectByIds("s2")
2869                        .areAllNotDynamic()
2870                        .areAllPinned()
2871                        .areAllEnabled()
2872                ;
2873            });
2874        }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
2875                .haveIds("ms2", "s1", "s2")
2876                .areAllWithKeyFieldsOnly();
2877
2878        // Remove CALLING_PACKAGE_2
2879        assertForLauncherCallback(mLauncherApps, () -> {
2880            uninstallPackage(USER_0, CALLING_PACKAGE_2);
2881            mService.cleanUpPackageLocked(CALLING_PACKAGE_2, USER_0, USER_0,
2882                    /* appStillExists = */ false);
2883        }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_2, HANDLE_USER_0)
2884                .isEmpty();
2885    }
2886
2887    public void testLauncherCallback_crossProfile() throws Throwable {
2888        prepareCrossProfileDataSet();
2889
2890        final Handler h = new Handler(Looper.getMainLooper());
2891
2892        final LauncherApps.Callback c0_1 = mock(LauncherApps.Callback.class);
2893        final LauncherApps.Callback c0_2 = mock(LauncherApps.Callback.class);
2894        final LauncherApps.Callback c0_3 = mock(LauncherApps.Callback.class);
2895        final LauncherApps.Callback c0_4 = mock(LauncherApps.Callback.class);
2896
2897        final LauncherApps.Callback cP0_1 = mock(LauncherApps.Callback.class);
2898        final LauncherApps.Callback c10_1 = mock(LauncherApps.Callback.class);
2899        final LauncherApps.Callback c10_2 = mock(LauncherApps.Callback.class);
2900        final LauncherApps.Callback c11_1 = mock(LauncherApps.Callback.class);
2901
2902        final List<LauncherApps.Callback> all =
2903                list(c0_1, c0_2, c0_3, c0_4, cP0_1, c10_1, c11_1);
2904
2905        setDefaultLauncherChecker((pkg, userId) -> {
2906            switch (userId) {
2907                case USER_0:
2908                    return LAUNCHER_2.equals(pkg);
2909                case USER_P0:
2910                    return LAUNCHER_1.equals(pkg);
2911                case USER_10:
2912                    return LAUNCHER_1.equals(pkg);
2913                case USER_11:
2914                    return LAUNCHER_1.equals(pkg);
2915                default:
2916                    return false;
2917            }
2918        });
2919
2920        runWithCaller(LAUNCHER_1, USER_0, () -> mLauncherApps.registerCallback(c0_1, h));
2921        runWithCaller(LAUNCHER_2, USER_0, () -> mLauncherApps.registerCallback(c0_2, h));
2922        runWithCaller(LAUNCHER_3, USER_0, () -> mLauncherApps.registerCallback(c0_3, h));
2923        runWithCaller(LAUNCHER_4, USER_0, () -> mLauncherApps.registerCallback(c0_4, h));
2924        runWithCaller(LAUNCHER_1, USER_P0, () -> mLauncherApps.registerCallback(cP0_1, h));
2925        runWithCaller(LAUNCHER_1, USER_10, () -> mLauncherApps.registerCallback(c10_1, h));
2926        runWithCaller(LAUNCHER_2, USER_10, () -> mLauncherApps.registerCallback(c10_2, h));
2927        runWithCaller(LAUNCHER_1, USER_11, () -> mLauncherApps.registerCallback(c11_1, h));
2928
2929        // User 0.
2930
2931        resetAll(all);
2932        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2933            mManager.removeDynamicShortcuts(list());
2934        });
2935        waitOnMainThread();
2936
2937        assertCallbackNotReceived(c0_1);
2938        assertCallbackNotReceived(c0_3);
2939        assertCallbackNotReceived(c0_4);
2940        assertCallbackNotReceived(c10_1);
2941        assertCallbackNotReceived(c10_2);
2942        assertCallbackNotReceived(c11_1);
2943        assertCallbackReceived(c0_2, HANDLE_USER_0, CALLING_PACKAGE_1, "s1", "s2", "s3");
2944        assertCallbackReceived(cP0_1, HANDLE_USER_0, CALLING_PACKAGE_1, "s1", "s2", "s3", "s4");
2945
2946        // User 0, different package.
2947
2948        resetAll(all);
2949        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
2950            mManager.removeDynamicShortcuts(list());
2951        });
2952        waitOnMainThread();
2953
2954        assertCallbackNotReceived(c0_1);
2955        assertCallbackNotReceived(c0_3);
2956        assertCallbackNotReceived(c0_4);
2957        assertCallbackNotReceived(c10_1);
2958        assertCallbackNotReceived(c10_2);
2959        assertCallbackNotReceived(c11_1);
2960        assertCallbackReceived(c0_2, HANDLE_USER_0, CALLING_PACKAGE_3, "s1", "s2", "s3", "s4");
2961        assertCallbackReceived(cP0_1, HANDLE_USER_0, CALLING_PACKAGE_3,
2962                "s1", "s2", "s3", "s4", "s5", "s6");
2963
2964        // Work profile, but not running, so don't send notifications.
2965
2966        resetAll(all);
2967        runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
2968            mManager.removeDynamicShortcuts(list());
2969        });
2970        waitOnMainThread();
2971
2972        assertCallbackNotReceived(c0_1);
2973        assertCallbackNotReceived(c0_2);
2974        assertCallbackNotReceived(c0_3);
2975        assertCallbackNotReceived(c0_4);
2976        assertCallbackNotReceived(cP0_1);
2977        assertCallbackNotReceived(c10_1);
2978        assertCallbackNotReceived(c10_2);
2979        assertCallbackNotReceived(c11_1);
2980
2981        // Work profile, now running.
2982        mRunningUsers.clear();
2983        mRunningUsers.put(USER_P0, true);
2984
2985        resetAll(all);
2986        runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
2987            mManager.removeDynamicShortcuts(list());
2988        });
2989        waitOnMainThread();
2990
2991        assertCallbackNotReceived(c0_1);
2992        assertCallbackNotReceived(c0_3);
2993        assertCallbackNotReceived(c0_4);
2994        assertCallbackNotReceived(c10_1);
2995        assertCallbackNotReceived(c10_2);
2996        assertCallbackNotReceived(c11_1);
2997        assertCallbackReceived(c0_2, HANDLE_USER_P0, CALLING_PACKAGE_1, "s1", "s2", "s3", "s5");
2998        assertCallbackReceived(cP0_1, HANDLE_USER_P0, CALLING_PACKAGE_1, "s1", "s2", "s3", "s4");
2999
3000        // Normal secondary user.
3001        mRunningUsers.clear();
3002        mRunningUsers.put(USER_10, true);
3003
3004        resetAll(all);
3005        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
3006            mManager.removeDynamicShortcuts(list());
3007        });
3008        waitOnMainThread();
3009
3010        assertCallbackNotReceived(c0_1);
3011        assertCallbackNotReceived(c0_2);
3012        assertCallbackNotReceived(c0_3);
3013        assertCallbackNotReceived(c0_4);
3014        assertCallbackNotReceived(cP0_1);
3015        assertCallbackNotReceived(c10_2);
3016        assertCallbackNotReceived(c11_1);
3017        assertCallbackReceived(c10_1, HANDLE_USER_10, CALLING_PACKAGE_1,
3018                "x1", "x2", "x3", "x4", "x5");
3019    }
3020
3021    // === Test for persisting ===
3022
3023    public void testSaveAndLoadUser_empty() {
3024        assertTrue(mManager.setDynamicShortcuts(list()));
3025
3026        Log.i(TAG, "Saved state");
3027        dumpsysOnLogcat();
3028        dumpUserFile(0);
3029
3030        // Restore.
3031        mService.saveDirtyInfo();
3032        initService();
3033
3034        assertEquals(0, mManager.getDynamicShortcuts().size());
3035    }
3036
3037    /**
3038     * Try save and load, also stop/start the user.
3039     */
3040    public void testSaveAndLoadUser() {
3041        // First, create some shortcuts and save.
3042        runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
3043            final Icon icon1 = Icon.createWithResource(getTestContext(), R.drawable.black_64x16);
3044            final Icon icon2 = Icon.createWithBitmap(BitmapFactory.decodeResource(
3045                    getTestContext().getResources(), R.drawable.icon2));
3046
3047            final ShortcutInfo si1 = makeShortcut(
3048                    "s1",
3049                    "title1-1",
3050                    makeComponent(ShortcutActivity.class),
3051                    icon1,
3052                    makeIntent(Intent.ACTION_ASSIST, ShortcutActivity2.class,
3053                            "key1", "val1", "nest", makeBundle("key", 123)),
3054                        /* weight */ 10);
3055
3056            final ShortcutInfo si2 = makeShortcut(
3057                    "s2",
3058                    "title1-2",
3059                        /* activity */ null,
3060                    icon2,
3061                    makeIntent(Intent.ACTION_ASSIST, ShortcutActivity3.class),
3062                        /* weight */ 12);
3063
3064            assertTrue(mManager.setDynamicShortcuts(list(si1, si2)));
3065
3066            assertEquals(START_TIME + INTERVAL, mManager.getRateLimitResetTime());
3067            assertEquals(2, mManager.getRemainingCallCount());
3068        });
3069        runWithCaller(CALLING_PACKAGE_2, UserHandle.USER_SYSTEM, () -> {
3070            final Icon icon1 = Icon.createWithResource(getTestContext(), R.drawable.black_16x64);
3071            final Icon icon2 = Icon.createWithBitmap(BitmapFactory.decodeResource(
3072                    getTestContext().getResources(), R.drawable.icon2));
3073
3074            final ShortcutInfo si1 = makeShortcut(
3075                    "s1",
3076                    "title2-1",
3077                    makeComponent(ShortcutActivity.class),
3078                    icon1,
3079                    makeIntent(Intent.ACTION_ASSIST, ShortcutActivity2.class,
3080                            "key1", "val1", "nest", makeBundle("key", 123)),
3081                        /* weight */ 10);
3082
3083            final ShortcutInfo si2 = makeShortcut(
3084                    "s2",
3085                    "title2-2",
3086                        /* activity */ null,
3087                    icon2,
3088                    makeIntent(Intent.ACTION_ASSIST, ShortcutActivity3.class),
3089                        /* weight */ 12);
3090
3091            assertTrue(mManager.setDynamicShortcuts(list(si1, si2)));
3092
3093            assertEquals(START_TIME + INTERVAL, mManager.getRateLimitResetTime());
3094            assertEquals(2, mManager.getRemainingCallCount());
3095        });
3096        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
3097            final Icon icon1 = Icon.createWithResource(getTestContext(), R.drawable.black_64x64);
3098            final Icon icon2 = Icon.createWithBitmap(BitmapFactory.decodeResource(
3099                    getTestContext().getResources(), R.drawable.icon2));
3100
3101            final ShortcutInfo si1 = makeShortcut(
3102                    "s1",
3103                    "title10-1-1",
3104                    makeComponent(ShortcutActivity.class),
3105                    icon1,
3106                    makeIntent(Intent.ACTION_ASSIST, ShortcutActivity2.class,
3107                            "key1", "val1", "nest", makeBundle("key", 123)),
3108                        /* weight */ 10);
3109
3110            final ShortcutInfo si2 = makeShortcut(
3111                    "s2",
3112                    "title10-1-2",
3113                        /* activity */ null,
3114                    icon2,
3115                    makeIntent(Intent.ACTION_ASSIST, ShortcutActivity3.class),
3116                        /* weight */ 12);
3117
3118            assertTrue(mManager.setDynamicShortcuts(list(si1, si2)));
3119
3120            assertEquals(START_TIME + INTERVAL, mManager.getRateLimitResetTime());
3121            assertEquals(2, mManager.getRemainingCallCount());
3122        });
3123
3124        mService.getShortcutsForTest().get(UserHandle.USER_SYSTEM).setLauncher(
3125                new ComponentName("pkg1", "class"));
3126
3127        // Restore.
3128        mService.saveDirtyInfo();
3129        initService();
3130
3131        // Before the load, the map should be empty.
3132        assertEquals(0, mService.getShortcutsForTest().size());
3133
3134        // this will pre-load the per-user info.
3135        mService.handleUnlockUser(UserHandle.USER_SYSTEM);
3136
3137        // Now it's loaded.
3138        assertEquals(1, mService.getShortcutsForTest().size());
3139
3140        runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
3141            assertShortcutIds(assertAllDynamic(assertAllHaveIntents(assertAllHaveIcon(
3142                    mManager.getDynamicShortcuts()))), "s1", "s2");
3143            assertEquals(2, mManager.getRemainingCallCount());
3144
3145            assertEquals("title1-1", getCallerShortcut("s1").getTitle());
3146            assertEquals("title1-2", getCallerShortcut("s2").getTitle());
3147        });
3148        runWithCaller(CALLING_PACKAGE_2, UserHandle.USER_SYSTEM, () -> {
3149            assertShortcutIds(assertAllDynamic(assertAllHaveIntents(assertAllHaveIcon(
3150                    mManager.getDynamicShortcuts()))), "s1", "s2");
3151            assertEquals(2, mManager.getRemainingCallCount());
3152
3153            assertEquals("title2-1", getCallerShortcut("s1").getTitle());
3154            assertEquals("title2-2", getCallerShortcut("s2").getTitle());
3155        });
3156
3157        assertEquals("pkg1", mService.getShortcutsForTest().get(UserHandle.USER_SYSTEM)
3158                .getLastKnownLauncher().getPackageName());
3159
3160        // Start another user
3161        mService.handleUnlockUser(USER_10);
3162
3163        // Now the size is 2.
3164        assertEquals(2, mService.getShortcutsForTest().size());
3165
3166        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
3167            assertShortcutIds(assertAllDynamic(assertAllHaveIntents(assertAllHaveIcon(
3168                    mManager.getDynamicShortcuts()))), "s1", "s2");
3169            assertEquals(2, mManager.getRemainingCallCount());
3170
3171            assertEquals("title10-1-1", getCallerShortcut("s1").getTitle());
3172            assertEquals("title10-1-2", getCallerShortcut("s2").getTitle());
3173        });
3174        assertNull(mService.getShortcutsForTest().get(USER_10).getLastKnownLauncher());
3175
3176        // Try stopping the user
3177        mService.handleCleanupUser(USER_10);
3178
3179        // Now it's unloaded.
3180        assertEquals(1, mService.getShortcutsForTest().size());
3181
3182        // TODO Check all other fields
3183    }
3184
3185    public void testCleanupPackage() {
3186        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3187            assertTrue(mManager.setDynamicShortcuts(list(
3188                    makeShortcut("s0_1"))));
3189        });
3190        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
3191            assertTrue(mManager.setDynamicShortcuts(list(
3192                    makeShortcut("s0_2"))));
3193        });
3194        runWithCaller(LAUNCHER_1, USER_0, () -> {
3195            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s0_1"),
3196                    HANDLE_USER_0);
3197            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s0_2"),
3198                    HANDLE_USER_0);
3199        });
3200        runWithCaller(LAUNCHER_2, USER_0, () -> {
3201            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s0_1"),
3202                    HANDLE_USER_0);
3203            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s0_2"),
3204                    HANDLE_USER_0);
3205        });
3206
3207        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
3208            assertTrue(mManager.setDynamicShortcuts(list(
3209                    makeShortcut("s10_1"))));
3210        });
3211        runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
3212            assertTrue(mManager.setDynamicShortcuts(list(
3213                    makeShortcut("s10_2"))));
3214        });
3215        runWithCaller(LAUNCHER_1, USER_10, () -> {
3216            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s10_1"),
3217                    HANDLE_USER_10);
3218            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s10_2"),
3219                    HANDLE_USER_10);
3220        });
3221        runWithCaller(LAUNCHER_2, USER_10, () -> {
3222            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s10_1"),
3223                    HANDLE_USER_10);
3224            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s10_2"),
3225                    HANDLE_USER_10);
3226        });
3227
3228        // Remove all dynamic shortcuts; now all shortcuts are just pinned.
3229        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3230            mManager.removeAllDynamicShortcuts();
3231        });
3232        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
3233            mManager.removeAllDynamicShortcuts();
3234        });
3235        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
3236            mManager.removeAllDynamicShortcuts();
3237        });
3238        runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
3239            mManager.removeAllDynamicShortcuts();
3240        });
3241
3242
3243        final SparseArray<ShortcutUser> users =  mService.getShortcutsForTest();
3244        assertEquals(2, users.size());
3245        assertEquals(USER_0, users.keyAt(0));
3246        assertEquals(USER_10, users.keyAt(1));
3247
3248        final ShortcutUser user0 =  users.get(USER_0);
3249        final ShortcutUser user10 =  users.get(USER_10);
3250
3251
3252        // Check the registered packages.
3253        dumpsysOnLogcat();
3254        assertEquals(set(CALLING_PACKAGE_1, CALLING_PACKAGE_2),
3255                hashSet(user0.getAllPackagesForTest().keySet()));
3256        assertEquals(set(CALLING_PACKAGE_1, CALLING_PACKAGE_2),
3257                hashSet(user10.getAllPackagesForTest().keySet()));
3258        assertEquals(
3259                set(PackageWithUser.of(USER_0, LAUNCHER_1),
3260                        PackageWithUser.of(USER_0, LAUNCHER_2)),
3261                hashSet(user0.getAllLaunchersForTest().keySet()));
3262        assertEquals(
3263                set(PackageWithUser.of(USER_10, LAUNCHER_1),
3264                        PackageWithUser.of(USER_10, LAUNCHER_2)),
3265                hashSet(user10.getAllLaunchersForTest().keySet()));
3266        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_0),
3267                "s0_1", "s0_2");
3268        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_0),
3269                "s0_1", "s0_2");
3270        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_10),
3271                "s10_1", "s10_2");
3272        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_10),
3273                "s10_1", "s10_2");
3274        assertShortcutExists(CALLING_PACKAGE_1, "s0_1", USER_0);
3275        assertShortcutExists(CALLING_PACKAGE_2, "s0_2", USER_0);
3276        assertShortcutExists(CALLING_PACKAGE_1, "s10_1", USER_10);
3277        assertShortcutExists(CALLING_PACKAGE_2, "s10_2", USER_10);
3278
3279        mService.saveDirtyInfo();
3280
3281        // Nonexistent package.
3282        uninstallPackage(USER_0, "abc");
3283        mService.cleanUpPackageLocked("abc", USER_0, USER_0, /* appStillExists = */ false);
3284
3285        // No changes.
3286        assertEquals(set(CALLING_PACKAGE_1, CALLING_PACKAGE_2),
3287                hashSet(user0.getAllPackagesForTest().keySet()));
3288        assertEquals(set(CALLING_PACKAGE_1, CALLING_PACKAGE_2),
3289                hashSet(user10.getAllPackagesForTest().keySet()));
3290        assertEquals(
3291                set(PackageWithUser.of(USER_0, LAUNCHER_1),
3292                        PackageWithUser.of(USER_0, LAUNCHER_2)),
3293                hashSet(user0.getAllLaunchersForTest().keySet()));
3294        assertEquals(
3295                set(PackageWithUser.of(USER_10, LAUNCHER_1),
3296                        PackageWithUser.of(USER_10, LAUNCHER_2)),
3297                hashSet(user10.getAllLaunchersForTest().keySet()));
3298        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_0),
3299                "s0_1", "s0_2");
3300        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_0),
3301                "s0_1", "s0_2");
3302        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_10),
3303                "s10_1", "s10_2");
3304        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_10),
3305                "s10_1", "s10_2");
3306        assertShortcutExists(CALLING_PACKAGE_1, "s0_1", USER_0);
3307        assertShortcutExists(CALLING_PACKAGE_2, "s0_2", USER_0);
3308        assertShortcutExists(CALLING_PACKAGE_1, "s10_1", USER_10);
3309        assertShortcutExists(CALLING_PACKAGE_2, "s10_2", USER_10);
3310
3311        mService.saveDirtyInfo();
3312
3313        // Remove a package.
3314        uninstallPackage(USER_0, CALLING_PACKAGE_1);
3315        mService.cleanUpPackageLocked(CALLING_PACKAGE_1, USER_0, USER_0,
3316                /* appStillExists = */ false);
3317
3318        assertEquals(set(CALLING_PACKAGE_2),
3319                hashSet(user0.getAllPackagesForTest().keySet()));
3320        assertEquals(set(CALLING_PACKAGE_1, CALLING_PACKAGE_2),
3321                hashSet(user10.getAllPackagesForTest().keySet()));
3322        assertEquals(
3323                set(PackageWithUser.of(USER_0, LAUNCHER_1),
3324                        PackageWithUser.of(USER_0, LAUNCHER_2)),
3325                hashSet(user0.getAllLaunchersForTest().keySet()));
3326        assertEquals(
3327                set(PackageWithUser.of(USER_10, LAUNCHER_1),
3328                        PackageWithUser.of(USER_10, LAUNCHER_2)),
3329                hashSet(user10.getAllLaunchersForTest().keySet()));
3330        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_0),
3331                "s0_2");
3332        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_0),
3333                "s0_2");
3334        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_10),
3335                "s10_1", "s10_2");
3336        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_10),
3337                "s10_1", "s10_2");
3338        assertShortcutNotExists(CALLING_PACKAGE_1, "s0_1", USER_0);
3339        assertShortcutExists(CALLING_PACKAGE_2, "s0_2", USER_0);
3340        assertShortcutExists(CALLING_PACKAGE_1, "s10_1", USER_10);
3341        assertShortcutExists(CALLING_PACKAGE_2, "s10_2", USER_10);
3342
3343        mService.saveDirtyInfo();
3344
3345        // Remove a launcher.
3346        uninstallPackage(USER_10, LAUNCHER_1);
3347        mService.cleanUpPackageLocked(LAUNCHER_1, USER_10, USER_10, /* appStillExists = */ false);
3348
3349        assertEquals(set(CALLING_PACKAGE_2),
3350                hashSet(user0.getAllPackagesForTest().keySet()));
3351        assertEquals(set(CALLING_PACKAGE_1, CALLING_PACKAGE_2),
3352                hashSet(user10.getAllPackagesForTest().keySet()));
3353        assertEquals(
3354                set(PackageWithUser.of(USER_0, LAUNCHER_1),
3355                        PackageWithUser.of(USER_0, LAUNCHER_2)),
3356                hashSet(user0.getAllLaunchersForTest().keySet()));
3357        assertEquals(
3358                set(PackageWithUser.of(USER_10, LAUNCHER_2)),
3359                hashSet(user10.getAllLaunchersForTest().keySet()));
3360        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_0),
3361                "s0_2");
3362        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_0),
3363                "s0_2");
3364        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_10),
3365                "s10_1", "s10_2");
3366        assertShortcutNotExists(CALLING_PACKAGE_1, "s0_1", USER_0);
3367        assertShortcutExists(CALLING_PACKAGE_2, "s0_2", USER_0);
3368        assertShortcutExists(CALLING_PACKAGE_1, "s10_1", USER_10);
3369        assertShortcutExists(CALLING_PACKAGE_2, "s10_2", USER_10);
3370
3371        mService.saveDirtyInfo();
3372
3373        // Remove a package.
3374        uninstallPackage(USER_10, CALLING_PACKAGE_2);
3375        mService.cleanUpPackageLocked(CALLING_PACKAGE_2, USER_10, USER_10,
3376                /* appStillExists = */ false);
3377
3378        assertEquals(set(CALLING_PACKAGE_2),
3379                hashSet(user0.getAllPackagesForTest().keySet()));
3380        assertEquals(set(CALLING_PACKAGE_1),
3381                hashSet(user10.getAllPackagesForTest().keySet()));
3382        assertEquals(
3383                set(PackageWithUser.of(USER_0, LAUNCHER_1),
3384                        PackageWithUser.of(USER_0, LAUNCHER_2)),
3385                hashSet(user0.getAllLaunchersForTest().keySet()));
3386        assertEquals(
3387                set(PackageWithUser.of(USER_10, LAUNCHER_2)),
3388                hashSet(user10.getAllLaunchersForTest().keySet()));
3389        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_0),
3390                "s0_2");
3391        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_0),
3392                "s0_2");
3393        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_10),
3394                "s10_1");
3395        assertShortcutNotExists(CALLING_PACKAGE_1, "s0_1", USER_0);
3396        assertShortcutExists(CALLING_PACKAGE_2, "s0_2", USER_0);
3397        assertShortcutExists(CALLING_PACKAGE_1, "s10_1", USER_10);
3398        assertShortcutNotExists(CALLING_PACKAGE_2, "s10_2", USER_10);
3399
3400        mService.saveDirtyInfo();
3401
3402        // Remove the other launcher from user 10 too.
3403        uninstallPackage(USER_10, LAUNCHER_2);
3404        mService.cleanUpPackageLocked(LAUNCHER_2, USER_10, USER_10,
3405                /* appStillExists = */ false);
3406
3407        assertEquals(set(CALLING_PACKAGE_2),
3408                hashSet(user0.getAllPackagesForTest().keySet()));
3409        assertEquals(set(CALLING_PACKAGE_1),
3410                hashSet(user10.getAllPackagesForTest().keySet()));
3411        assertEquals(
3412                set(PackageWithUser.of(USER_0, LAUNCHER_1),
3413                        PackageWithUser.of(USER_0, LAUNCHER_2)),
3414                hashSet(user0.getAllLaunchersForTest().keySet()));
3415        assertEquals(
3416                set(),
3417                hashSet(user10.getAllLaunchersForTest().keySet()));
3418        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_0),
3419                "s0_2");
3420        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_0),
3421                "s0_2");
3422
3423        // Note the pinned shortcuts on user-10 no longer referred, so they should both be removed.
3424        assertShortcutNotExists(CALLING_PACKAGE_1, "s0_1", USER_0);
3425        assertShortcutExists(CALLING_PACKAGE_2, "s0_2", USER_0);
3426        assertShortcutNotExists(CALLING_PACKAGE_1, "s10_1", USER_10);
3427        assertShortcutNotExists(CALLING_PACKAGE_2, "s10_2", USER_10);
3428
3429        mService.saveDirtyInfo();
3430
3431        // More remove.
3432        uninstallPackage(USER_10, CALLING_PACKAGE_1);
3433        mService.cleanUpPackageLocked(CALLING_PACKAGE_1, USER_10, USER_10,
3434                /* appStillExists = */ false);
3435
3436        assertEquals(set(CALLING_PACKAGE_2),
3437                hashSet(user0.getAllPackagesForTest().keySet()));
3438        assertEquals(set(),
3439                hashSet(user10.getAllPackagesForTest().keySet()));
3440        assertEquals(
3441                set(PackageWithUser.of(USER_0, LAUNCHER_1),
3442                        PackageWithUser.of(USER_0, LAUNCHER_2)),
3443                hashSet(user0.getAllLaunchersForTest().keySet()));
3444        assertEquals(set(),
3445                hashSet(user10.getAllLaunchersForTest().keySet()));
3446        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_0),
3447                "s0_2");
3448        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_0),
3449                "s0_2");
3450
3451        // Note the pinned shortcuts on user-10 no longer referred, so they should both be removed.
3452        assertShortcutNotExists(CALLING_PACKAGE_1, "s0_1", USER_0);
3453        assertShortcutExists(CALLING_PACKAGE_2, "s0_2", USER_0);
3454        assertShortcutNotExists(CALLING_PACKAGE_1, "s10_1", USER_10);
3455        assertShortcutNotExists(CALLING_PACKAGE_2, "s10_2", USER_10);
3456
3457        mService.saveDirtyInfo();
3458    }
3459
3460    public void testCleanupPackage_republishManifests() {
3461        addManifestShortcutResource(
3462                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
3463                R.xml.shortcut_2);
3464        updatePackageVersion(CALLING_PACKAGE_1, 1);
3465                mService.mPackageMonitor.onReceive(getTestContext(),
3466                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
3467
3468        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3469            assertTrue(mManager.setDynamicShortcuts(list(
3470                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
3471        });
3472        runWithCaller(LAUNCHER_1, USER_0, () -> {
3473            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
3474                    list("s2", "s3", "ms1", "ms2"), HANDLE_USER_0);
3475        });
3476
3477        // Remove ms2 from manifest.
3478        addManifestShortcutResource(
3479                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
3480                R.xml.shortcut_1);
3481        updatePackageVersion(CALLING_PACKAGE_1, 1);
3482                mService.mPackageMonitor.onReceive(getTestContext(),
3483                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
3484
3485        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3486            assertTrue(mManager.setDynamicShortcuts(list(
3487                    makeShortcut("s1"), makeShortcut("s2"))));
3488
3489            // Make sure the shortcuts are in the intended state.
3490            assertWith(getCallerShortcuts())
3491                    .haveIds("ms1", "ms2", "s1", "s2", "s3")
3492
3493                    .selectByIds("ms1")
3494                    .areAllManifest()
3495                    .areAllPinned()
3496
3497                    .revertToOriginalList()
3498                    .selectByIds("ms2")
3499                    .areAllNotManifest()
3500                    .areAllPinned()
3501
3502                    .revertToOriginalList()
3503                    .selectByIds("s1")
3504                    .areAllDynamic()
3505                    .areAllNotPinned()
3506
3507                    .revertToOriginalList()
3508                    .selectByIds("s2")
3509                    .areAllDynamic()
3510                    .areAllPinned()
3511
3512                    .revertToOriginalList()
3513                    .selectByIds("s3")
3514                    .areAllNotDynamic()
3515                    .areAllPinned();
3516        });
3517
3518        // Clean up + re-publish manifests.
3519        mService.cleanUpPackageLocked(CALLING_PACKAGE_1, USER_0, USER_0,
3520                /* appStillExists = */ true);
3521        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3522            assertWith(getCallerShortcuts())
3523                    .haveIds("ms1")
3524                    .areAllManifest();
3525        });
3526    }
3527
3528    public void testHandleGonePackage_crossProfile() {
3529        // Create some shortcuts.
3530        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3531            assertTrue(mManager.setDynamicShortcuts(list(
3532                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
3533        });
3534        runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
3535            assertTrue(mManager.setDynamicShortcuts(list(
3536                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
3537        });
3538        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
3539            assertTrue(mManager.setDynamicShortcuts(list(
3540                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
3541        });
3542        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
3543            assertTrue(mManager.setDynamicShortcuts(list(
3544                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
3545        });
3546
3547        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
3548        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
3549        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
3550
3551        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
3552        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
3553        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
3554
3555        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
3556        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
3557        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
3558
3559        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
3560        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
3561        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
3562
3563        // Pin some.
3564
3565        runWithCaller(LAUNCHER_1, USER_0, () -> {
3566            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
3567                    list("s1"), HANDLE_USER_0);
3568
3569            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
3570                    list("s2"), UserHandle.of(USER_P0));
3571
3572            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
3573                    list("s3"), HANDLE_USER_0);
3574        });
3575
3576        runWithCaller(LAUNCHER_1, USER_P0, () -> {
3577            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
3578                    list("s2"), HANDLE_USER_0);
3579
3580            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
3581                    list("s3"), UserHandle.of(USER_P0));
3582
3583            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
3584                    list("s1"), HANDLE_USER_0);
3585        });
3586
3587        runWithCaller(LAUNCHER_1, USER_10, () -> {
3588            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
3589                    list("s3"), HANDLE_USER_10);
3590        });
3591
3592        // Check the state.
3593
3594        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
3595        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
3596        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
3597
3598        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
3599        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
3600        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
3601
3602        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
3603        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
3604        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
3605
3606        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
3607        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
3608        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
3609
3610        // Make sure all the information is persisted.
3611        mService.saveDirtyInfo();
3612        initService();
3613        mService.handleUnlockUser(USER_0);
3614        mService.handleUnlockUser(USER_P0);
3615        mService.handleUnlockUser(USER_10);
3616
3617        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
3618        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
3619        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
3620
3621        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
3622        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
3623        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
3624
3625        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
3626        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
3627        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
3628
3629        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
3630        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
3631        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
3632
3633        // Start uninstalling.
3634        uninstallPackage(USER_10, LAUNCHER_1);
3635        mService.checkPackageChanges(USER_10);
3636
3637        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
3638        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
3639        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
3640
3641        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
3642        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
3643        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
3644
3645        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
3646        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
3647        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
3648
3649        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
3650        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
3651        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
3652
3653        // Uninstall.
3654        uninstallPackage(USER_10, CALLING_PACKAGE_1);
3655        mService.checkPackageChanges(USER_10);
3656
3657        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
3658        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
3659        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
3660
3661        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
3662        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
3663        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
3664
3665        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
3666        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
3667        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
3668
3669        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
3670        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
3671        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
3672
3673        uninstallPackage(USER_P0, LAUNCHER_1);
3674        mService.checkPackageChanges(USER_0);
3675
3676        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
3677        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
3678        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
3679
3680        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
3681        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
3682        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
3683
3684        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
3685        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
3686        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
3687
3688        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
3689        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
3690        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
3691
3692        mService.checkPackageChanges(USER_P0);
3693
3694        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
3695        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
3696        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
3697
3698        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
3699        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
3700        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
3701
3702        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
3703        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
3704        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
3705
3706        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
3707        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
3708        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
3709
3710        uninstallPackage(USER_P0, CALLING_PACKAGE_1);
3711
3712        mService.saveDirtyInfo();
3713        initService();
3714        mService.handleUnlockUser(USER_0);
3715        mService.handleUnlockUser(USER_P0);
3716        mService.handleUnlockUser(USER_10);
3717
3718        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
3719        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
3720        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
3721
3722        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
3723        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
3724        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
3725
3726        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
3727        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
3728        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
3729
3730        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
3731        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
3732        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
3733
3734        // Uninstall
3735        uninstallPackage(USER_0, LAUNCHER_1);
3736
3737        mService.saveDirtyInfo();
3738        initService();
3739        mService.handleUnlockUser(USER_0);
3740        mService.handleUnlockUser(USER_P0);
3741        mService.handleUnlockUser(USER_10);
3742
3743        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
3744        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
3745        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
3746
3747        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
3748        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
3749        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
3750
3751        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
3752        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
3753        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
3754
3755        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
3756        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
3757        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
3758
3759        uninstallPackage(USER_0, CALLING_PACKAGE_2);
3760
3761        mService.saveDirtyInfo();
3762        initService();
3763        mService.handleUnlockUser(USER_0);
3764        mService.handleUnlockUser(USER_P0);
3765        mService.handleUnlockUser(USER_10);
3766
3767        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
3768        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
3769        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
3770
3771        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
3772        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
3773        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
3774
3775        assertNull(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
3776        assertNull(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
3777        assertNull(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
3778
3779        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
3780        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
3781        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
3782    }
3783
3784    protected void checkCanRestoreTo(boolean expected, ShortcutPackageInfo spi,
3785            int version, String... signatures) {
3786        assertEquals(expected, spi.canRestoreTo(mService, genPackage(
3787                "dummy", /* uid */ 0, version, signatures)));
3788    }
3789
3790    public void testCanRestoreTo() {
3791        addPackage(CALLING_PACKAGE_1, CALLING_UID_1, 10, "sig1");
3792        addPackage(CALLING_PACKAGE_2, CALLING_UID_1, 10, "sig1", "sig2");
3793
3794        final ShortcutPackageInfo spi1 = ShortcutPackageInfo.generateForInstalledPackage(
3795                mService, CALLING_PACKAGE_1, USER_0);
3796        final ShortcutPackageInfo spi2 = ShortcutPackageInfo.generateForInstalledPackage(
3797                mService, CALLING_PACKAGE_2, USER_0);
3798
3799        checkCanRestoreTo(true, spi1, 10, "sig1");
3800        checkCanRestoreTo(true, spi1, 10, "x", "sig1");
3801        checkCanRestoreTo(true, spi1, 10, "sig1", "y");
3802        checkCanRestoreTo(true, spi1, 10, "x", "sig1", "y");
3803        checkCanRestoreTo(true, spi1, 11, "sig1");
3804
3805        checkCanRestoreTo(false, spi1, 10 /* empty */);
3806        checkCanRestoreTo(false, spi1, 10, "x");
3807        checkCanRestoreTo(false, spi1, 10, "x", "y");
3808        checkCanRestoreTo(false, spi1, 10, "x");
3809        checkCanRestoreTo(false, spi1, 9, "sig1");
3810
3811        checkCanRestoreTo(true, spi2, 10, "sig1", "sig2");
3812        checkCanRestoreTo(true, spi2, 10, "sig2", "sig1");
3813        checkCanRestoreTo(true, spi2, 10, "x", "sig1", "sig2");
3814        checkCanRestoreTo(true, spi2, 10, "x", "sig2", "sig1");
3815        checkCanRestoreTo(true, spi2, 10, "sig1", "sig2", "y");
3816        checkCanRestoreTo(true, spi2, 10, "sig2", "sig1", "y");
3817        checkCanRestoreTo(true, spi2, 10, "x", "sig1", "sig2", "y");
3818        checkCanRestoreTo(true, spi2, 10, "x", "sig2", "sig1", "y");
3819        checkCanRestoreTo(true, spi2, 11, "x", "sig2", "sig1", "y");
3820
3821        checkCanRestoreTo(false, spi2, 10, "sig1", "sig2x");
3822        checkCanRestoreTo(false, spi2, 10, "sig2", "sig1x");
3823        checkCanRestoreTo(false, spi2, 10, "x", "sig1x", "sig2");
3824        checkCanRestoreTo(false, spi2, 10, "x", "sig2x", "sig1");
3825        checkCanRestoreTo(false, spi2, 10, "sig1", "sig2x", "y");
3826        checkCanRestoreTo(false, spi2, 10, "sig2", "sig1x", "y");
3827        checkCanRestoreTo(false, spi2, 10, "x", "sig1x", "sig2", "y");
3828        checkCanRestoreTo(false, spi2, 10, "x", "sig2x", "sig1", "y");
3829        checkCanRestoreTo(false, spi2, 11, "x", "sig2x", "sig1", "y");
3830    }
3831
3832    public void testHandlePackageDelete() {
3833        final Icon bmp32x32 = Icon.createWithBitmap(BitmapFactory.decodeResource(
3834                getTestContext().getResources(), R.drawable.black_32x32));
3835        setCaller(CALLING_PACKAGE_1, USER_0);
3836        assertTrue(mManager.addDynamicShortcuts(list(
3837                makeShortcutWithIcon("s1", bmp32x32), makeShortcutWithIcon("s2", bmp32x32)
3838        )));
3839        // Also add a manifest shortcut, which should be removed too.
3840        addManifestShortcutResource(
3841                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
3842                R.xml.shortcut_1);
3843        updatePackageVersion(CALLING_PACKAGE_1, 1);
3844                mService.mPackageMonitor.onReceive(getTestContext(),
3845                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
3846        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3847            assertWith(getCallerShortcuts())
3848                    .haveIds("s1", "s2", "ms1")
3849
3850                    .selectManifest()
3851                    .haveIds("ms1");
3852        });
3853
3854        setCaller(CALLING_PACKAGE_2, USER_0);
3855        assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
3856
3857        setCaller(CALLING_PACKAGE_3, USER_0);
3858        assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
3859
3860        setCaller(CALLING_PACKAGE_1, USER_10);
3861        assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
3862
3863        setCaller(CALLING_PACKAGE_2, USER_10);
3864        assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
3865
3866        setCaller(CALLING_PACKAGE_3, USER_10);
3867        assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
3868
3869        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
3870        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
3871        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));
3872        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
3873        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
3874        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
3875
3876        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
3877        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
3878        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
3879        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
3880        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
3881        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
3882
3883        uninstallPackage(USER_0, CALLING_PACKAGE_1);
3884                mService.mPackageMonitor.onReceive(getTestContext(),
3885                genPackageDeleteIntent(CALLING_PACKAGE_1, USER_0));
3886
3887        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
3888        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
3889        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));
3890        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
3891        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
3892        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
3893
3894        assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
3895        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
3896        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
3897        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
3898        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
3899        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
3900
3901        mRunningUsers.put(USER_10, true);
3902
3903        uninstallPackage(USER_10, CALLING_PACKAGE_2);
3904                mService.mPackageMonitor.onReceive(getTestContext(),
3905                genPackageDeleteIntent(CALLING_PACKAGE_2, USER_10));
3906
3907        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
3908        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
3909        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));
3910        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
3911        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
3912        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
3913
3914        assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
3915        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
3916        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
3917        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
3918        assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
3919        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
3920
3921        mInjectedPackages.remove(CALLING_PACKAGE_1);
3922        mInjectedPackages.remove(CALLING_PACKAGE_3);
3923
3924        mService.handleUnlockUser(USER_0);
3925
3926        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
3927        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
3928        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));
3929        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
3930        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
3931        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
3932
3933        assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
3934        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
3935        assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
3936        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
3937        assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
3938        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
3939
3940        mService.handleUnlockUser(USER_10);
3941
3942        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
3943        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
3944        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));
3945        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
3946        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
3947        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
3948
3949        assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
3950        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
3951        assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
3952        assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
3953        assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
3954        assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
3955    }
3956
3957    /** Almost ame as testHandlePackageDelete, except it doesn't uninstall packages. */
3958    public void testHandlePackageClearData() {
3959        final Icon bmp32x32 = Icon.createWithBitmap(BitmapFactory.decodeResource(
3960                getTestContext().getResources(), R.drawable.black_32x32));
3961        setCaller(CALLING_PACKAGE_1, USER_0);
3962        assertTrue(mManager.addDynamicShortcuts(list(
3963                makeShortcutWithIcon("s1", bmp32x32), makeShortcutWithIcon("s2", bmp32x32)
3964        )));
3965
3966        setCaller(CALLING_PACKAGE_2, USER_0);
3967        assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
3968
3969        setCaller(CALLING_PACKAGE_3, USER_0);
3970        assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
3971
3972        setCaller(CALLING_PACKAGE_1, USER_10);
3973        assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
3974
3975        setCaller(CALLING_PACKAGE_2, USER_10);
3976        assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
3977
3978        setCaller(CALLING_PACKAGE_3, USER_10);
3979        assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
3980
3981        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
3982        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
3983        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));
3984        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
3985        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
3986        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
3987
3988        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
3989        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
3990        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
3991        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
3992        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
3993        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
3994
3995                mService.mPackageMonitor.onReceive(getTestContext(),
3996                genPackageDataClear(CALLING_PACKAGE_1, USER_0));
3997
3998        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
3999        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
4000        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));
4001        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
4002        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
4003        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
4004
4005        assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
4006        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
4007        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
4008        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
4009        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
4010        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
4011
4012        mRunningUsers.put(USER_10, true);
4013
4014                mService.mPackageMonitor.onReceive(getTestContext(),
4015                genPackageDataClear(CALLING_PACKAGE_2, USER_10));
4016
4017        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
4018        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
4019        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));
4020        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
4021        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
4022        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
4023
4024        assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
4025        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
4026        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
4027        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
4028        assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
4029        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
4030    }
4031
4032    public void testHandlePackageClearData_manifestRepublished() {
4033
4034        mRunningUsers.put(USER_10, true);
4035
4036        // Add two manifests and two dynamics.
4037        addManifestShortcutResource(
4038                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
4039                R.xml.shortcut_2);
4040        updatePackageVersion(CALLING_PACKAGE_1, 1);
4041                mService.mPackageMonitor.onReceive(getTestContext(),
4042                genPackageAddIntent(CALLING_PACKAGE_1, USER_10));
4043
4044        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4045            assertTrue(mManager.addDynamicShortcuts(list(
4046                    makeShortcut("s1"), makeShortcut("s2"))));
4047        });
4048        runWithCaller(LAUNCHER_1, USER_10, () -> {
4049            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms2", "s2"), HANDLE_USER_10);
4050        });
4051
4052        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4053            assertWith(getCallerShortcuts())
4054                    .haveIds("ms1", "ms2", "s1", "s2")
4055                    .areAllEnabled()
4056
4057                    .selectPinned()
4058                    .haveIds("ms2", "s2");
4059        });
4060
4061        // Clear data
4062        mService.mPackageMonitor.onReceive(getTestContext(),
4063                genPackageDataClear(CALLING_PACKAGE_1, USER_10));
4064
4065        // Only manifest shortcuts will remain, and are no longer pinned.
4066        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4067            assertWith(getCallerShortcuts())
4068                    .haveIds("ms1", "ms2")
4069                    .areAllEnabled()
4070                    .areAllNotPinned();
4071        });
4072    }
4073
4074    public void testHandlePackageUpdate() throws Throwable {
4075        // Set up shortcuts and launchers.
4076
4077        final Icon res32x32 = Icon.createWithResource(getTestContext(), R.drawable.black_32x32);
4078        final Icon bmp32x32 = Icon.createWithBitmap(BitmapFactory.decodeResource(
4079                getTestContext().getResources(), R.drawable.black_32x32));
4080
4081        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4082            assertTrue(mManager.setDynamicShortcuts(list(
4083                    makeShortcut("s1"),
4084                    makeShortcutWithIcon("s2", res32x32),
4085                    makeShortcutWithIcon("s3", res32x32),
4086                    makeShortcutWithIcon("s4", bmp32x32))));
4087        });
4088        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
4089            assertTrue(mManager.setDynamicShortcuts(list(
4090                    makeShortcut("s1"),
4091                    makeShortcutWithIcon("s2", bmp32x32))));
4092        });
4093        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
4094            assertTrue(mManager.setDynamicShortcuts(list(
4095                    makeShortcutWithIcon("s1", res32x32))));
4096        });
4097
4098        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4099            assertTrue(mManager.setDynamicShortcuts(list(
4100                    makeShortcutWithIcon("s1", res32x32),
4101                    makeShortcutWithIcon("s2", res32x32))));
4102        });
4103        runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
4104            assertTrue(mManager.setDynamicShortcuts(list(
4105                    makeShortcutWithIcon("s1", bmp32x32),
4106                    makeShortcutWithIcon("s2", bmp32x32))));
4107        });
4108
4109        LauncherApps.Callback c0 = mock(LauncherApps.Callback.class);
4110        LauncherApps.Callback c10 = mock(LauncherApps.Callback.class);
4111
4112        runWithCaller(LAUNCHER_1, USER_0, () -> {
4113            mLauncherApps.registerCallback(c0, new Handler(Looper.getMainLooper()));
4114        });
4115        runWithCaller(LAUNCHER_1, USER_10, () -> {
4116            mLauncherApps.registerCallback(c10, new Handler(Looper.getMainLooper()));
4117        });
4118
4119        mInjectedCurrentTimeMillis = START_TIME + 100;
4120
4121        ArgumentCaptor<List> shortcuts;
4122
4123        // First, call the event without updating the versions.
4124        reset(c0);
4125        reset(c10);
4126
4127                mService.mPackageMonitor.onReceive(getTestContext(),
4128                genPackageUpdateIntent(CALLING_PACKAGE_1, USER_0));
4129                mService.mPackageMonitor.onReceive(getTestContext(),
4130                genPackageUpdateIntent(CALLING_PACKAGE_1, USER_10));
4131
4132        waitOnMainThread();
4133
4134        // Version not changed, so no callback.
4135        verify(c0, times(0)).onShortcutsChanged(
4136                eq(CALLING_PACKAGE_1),
4137                any(List.class),
4138                any(UserHandle.class));
4139        verify(c10, times(0)).onShortcutsChanged(
4140                eq(CALLING_PACKAGE_1),
4141                any(List.class),
4142                any(UserHandle.class));
4143
4144        // Next, update the version info for package 1.
4145        reset(c0);
4146        reset(c10);
4147        updatePackageVersion(CALLING_PACKAGE_1, 1);
4148
4149        // Then send the broadcast, to only user-0.
4150                mService.mPackageMonitor.onReceive(getTestContext(),
4151                genPackageUpdateIntent(CALLING_PACKAGE_1, USER_0));
4152
4153        waitOnMainThread();
4154
4155        // User-0 should get the notification.
4156        shortcuts = ArgumentCaptor.forClass(List.class);
4157        verify(c0).onShortcutsChanged(
4158                eq(CALLING_PACKAGE_1),
4159                shortcuts.capture(),
4160                eq(HANDLE_USER_0));
4161
4162        // User-10 shouldn't yet get the notification.
4163        verify(c10, times(0)).onShortcutsChanged(
4164                eq(CALLING_PACKAGE_1),
4165                any(List.class),
4166                any(UserHandle.class));
4167        assertShortcutIds(shortcuts.getValue(), "s1", "s2", "s3", "s4");
4168        assertEquals(START_TIME,
4169                findShortcut(shortcuts.getValue(), "s1").getLastChangedTimestamp());
4170        assertEquals(START_TIME + 100,
4171                findShortcut(shortcuts.getValue(), "s2").getLastChangedTimestamp());
4172        assertEquals(START_TIME + 100,
4173                findShortcut(shortcuts.getValue(), "s3").getLastChangedTimestamp());
4174        assertEquals(START_TIME,
4175                findShortcut(shortcuts.getValue(), "s4").getLastChangedTimestamp());
4176
4177        // Next, send unlock even on user-10.  Now we scan packages on this user and send a
4178        // notification to the launcher.
4179        mInjectedCurrentTimeMillis = START_TIME + 200;
4180
4181        mRunningUsers.put(USER_10, true);
4182
4183        reset(c0);
4184        reset(c10);
4185        mService.handleUnlockUser(USER_10);
4186
4187        waitOnMainThread();
4188
4189        shortcuts = ArgumentCaptor.forClass(List.class);
4190        verify(c0, times(0)).onShortcutsChanged(
4191                eq(CALLING_PACKAGE_1),
4192                any(List.class),
4193                any(UserHandle.class));
4194
4195        verify(c10).onShortcutsChanged(
4196                eq(CALLING_PACKAGE_1),
4197                shortcuts.capture(),
4198                eq(HANDLE_USER_10));
4199
4200        assertShortcutIds(shortcuts.getValue(), "s1", "s2");
4201        assertEquals(START_TIME + 200,
4202                findShortcut(shortcuts.getValue(), "s1").getLastChangedTimestamp());
4203        assertEquals(START_TIME + 200,
4204                findShortcut(shortcuts.getValue(), "s2").getLastChangedTimestamp());
4205
4206
4207        // Do the same thing for package 2, which doesn't have resource icons.
4208        mInjectedCurrentTimeMillis = START_TIME + 300;
4209
4210        reset(c0);
4211        reset(c10);
4212        updatePackageVersion(CALLING_PACKAGE_2, 10);
4213
4214        // Then send the broadcast, to only user-0.
4215                mService.mPackageMonitor.onReceive(getTestContext(),
4216                genPackageUpdateIntent(CALLING_PACKAGE_2, USER_0));
4217        mService.handleUnlockUser(USER_10);
4218
4219        waitOnMainThread();
4220
4221        verify(c0, times(0)).onShortcutsChanged(
4222                eq(CALLING_PACKAGE_1),
4223                any(List.class),
4224                any(UserHandle.class));
4225
4226        verify(c10, times(0)).onShortcutsChanged(
4227                eq(CALLING_PACKAGE_1),
4228                any(List.class),
4229                any(UserHandle.class));
4230
4231        // Do the same thing for package 3
4232        mInjectedCurrentTimeMillis = START_TIME + 400;
4233
4234        reset(c0);
4235        reset(c10);
4236        updatePackageVersion(CALLING_PACKAGE_3, 100);
4237
4238        // Then send the broadcast, to only user-0.
4239                mService.mPackageMonitor.onReceive(getTestContext(),
4240                genPackageUpdateIntent(CALLING_PACKAGE_3, USER_0));
4241        mService.handleUnlockUser(USER_10);
4242
4243        waitOnMainThread();
4244
4245        shortcuts = ArgumentCaptor.forClass(List.class);
4246        verify(c0).onShortcutsChanged(
4247                eq(CALLING_PACKAGE_3),
4248                shortcuts.capture(),
4249                eq(HANDLE_USER_0));
4250
4251        // User 10 doesn't have package 3, so no callback.
4252        verify(c10, times(0)).onShortcutsChanged(
4253                eq(CALLING_PACKAGE_3),
4254                any(List.class),
4255                any(UserHandle.class));
4256
4257        assertShortcutIds(shortcuts.getValue(), "s1");
4258        assertEquals(START_TIME + 400,
4259                findShortcut(shortcuts.getValue(), "s1").getLastChangedTimestamp());
4260    }
4261
4262    /**
4263     * Test the case where an updated app has resource IDs changed.
4264     */
4265    public void testHandlePackageUpdate_resIdChanged() throws Exception {
4266        final Icon icon1 = Icon.createWithResource(getTestContext(), /* res ID */ 1000);
4267        final Icon icon2 = Icon.createWithResource(getTestContext(), /* res ID */ 1001);
4268
4269        // Set up shortcuts.
4270        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4271            // Note resource strings are not officially supported (they're hidden), but
4272            // should work.
4273
4274            final ShortcutInfo s1 = new ShortcutInfo.Builder(mClientContext)
4275                    .setId("s1")
4276                    .setActivity(makeComponent(ShortcutActivity.class))
4277                    .setIntent(new Intent(Intent.ACTION_VIEW))
4278                    .setIcon(icon1)
4279                    .setTitleResId(10000)
4280                    .setTextResId(10001)
4281                    .setDisabledMessageResId(10002)
4282                    .build();
4283
4284            final ShortcutInfo s2 = new ShortcutInfo.Builder(mClientContext)
4285                    .setId("s2")
4286                    .setActivity(makeComponent(ShortcutActivity.class))
4287                    .setIntent(new Intent(Intent.ACTION_VIEW))
4288                    .setIcon(icon2)
4289                    .setTitleResId(20000)
4290                    .build();
4291
4292            assertTrue(mManager.setDynamicShortcuts(list(s1, s2)));
4293        });
4294
4295        // Verify.
4296        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4297            final ShortcutInfo s1 = getCallerShortcut("s1");
4298            final ShortcutInfo s2 = getCallerShortcut("s2");
4299
4300            assertEquals(1000, s1.getIconResourceId());
4301            assertEquals(10000, s1.getTitleResId());
4302            assertEquals(10001, s1.getTextResId());
4303            assertEquals(10002, s1.getDisabledMessageResourceId());
4304
4305            assertEquals(1001, s2.getIconResourceId());
4306            assertEquals(20000, s2.getTitleResId());
4307            assertEquals(0, s2.getTextResId());
4308            assertEquals(0, s2.getDisabledMessageResourceId());
4309        });
4310
4311        mService.saveDirtyInfo();
4312        initService();
4313
4314        // Set up the mock resources again, with an "adjustment".
4315        // When the package is updated, the service will fetch the updated res-IDs with res-names,
4316        // and the new IDs will have this offset.
4317        setUpAppResources(10);
4318
4319        // Update the package.
4320        updatePackageVersion(CALLING_PACKAGE_1, 1);
4321                mService.mPackageMonitor.onReceive(getTestContext(),
4322                genPackageUpdateIntent(CALLING_PACKAGE_1, USER_0));
4323
4324        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4325            final ShortcutInfo s1 = getCallerShortcut("s1");
4326            final ShortcutInfo s2 = getCallerShortcut("s2");
4327
4328            assertEquals(1010, s1.getIconResourceId());
4329            assertEquals(10010, s1.getTitleResId());
4330            assertEquals(10011, s1.getTextResId());
4331            assertEquals(10012, s1.getDisabledMessageResourceId());
4332
4333            assertEquals(1011, s2.getIconResourceId());
4334            assertEquals(20010, s2.getTitleResId());
4335            assertEquals(0, s2.getTextResId());
4336            assertEquals(0, s2.getDisabledMessageResourceId());
4337        });
4338    }
4339
4340    public void testHandlePackageChanged() {
4341        final ComponentName ACTIVITY1 = new ComponentName(CALLING_PACKAGE_1, "act1");
4342        final ComponentName ACTIVITY2 = new ComponentName(CALLING_PACKAGE_1, "act2");
4343
4344        addManifestShortcutResource(ACTIVITY1, R.xml.shortcut_1);
4345        addManifestShortcutResource(ACTIVITY2, R.xml.shortcut_1_alt);
4346
4347        mRunningUsers.put(USER_10, true);
4348
4349        updatePackageVersion(CALLING_PACKAGE_1, 1);
4350                mService.mPackageMonitor.onReceive(getTestContext(),
4351                genPackageAddIntent(CALLING_PACKAGE_1, USER_10));
4352
4353        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4354            assertTrue(mManager.addDynamicShortcuts(list(
4355                    makeShortcutWithActivity("s1", ACTIVITY1),
4356                    makeShortcutWithActivity("s2", ACTIVITY2)
4357            )));
4358        });
4359        runWithCaller(LAUNCHER_1, USER_10, () -> {
4360            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms1-alt", "s2"), HANDLE_USER_10);
4361        });
4362
4363        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4364            assertWith(getCallerShortcuts())
4365                    .haveIds("ms1", "ms1-alt", "s1", "s2")
4366                    .areAllEnabled()
4367
4368                    .selectPinned()
4369                    .haveIds("ms1-alt", "s2")
4370
4371                    .revertToOriginalList()
4372                    .selectByIds("ms1", "s1")
4373                    .areAllWithActivity(ACTIVITY1)
4374
4375                    .revertToOriginalList()
4376                    .selectByIds("ms1-alt", "s2")
4377                    .areAllWithActivity(ACTIVITY2)
4378                    ;
4379        });
4380
4381        // First, no changes.
4382                mService.mPackageMonitor.onReceive(getTestContext(),
4383                genPackageChangedIntent(CALLING_PACKAGE_1, USER_10));
4384
4385        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4386            assertWith(getCallerShortcuts())
4387                    .haveIds("ms1", "ms1-alt", "s1", "s2")
4388                    .areAllEnabled()
4389
4390                    .selectPinned()
4391                    .haveIds("ms1-alt", "s2")
4392
4393                    .revertToOriginalList()
4394                    .selectByIds("ms1", "s1")
4395                    .areAllWithActivity(ACTIVITY1)
4396
4397                    .revertToOriginalList()
4398                    .selectByIds("ms1-alt", "s2")
4399                    .areAllWithActivity(ACTIVITY2)
4400            ;
4401        });
4402
4403        // Disable activity 1
4404        mEnabledActivityChecker = (activity, userId) -> !ACTIVITY1.equals(activity);
4405                mService.mPackageMonitor.onReceive(getTestContext(),
4406                genPackageChangedIntent(CALLING_PACKAGE_1, USER_10));
4407
4408        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4409            assertWith(getCallerShortcuts())
4410                    .haveIds("ms1-alt", "s2")
4411                    .areAllEnabled()
4412
4413                    .selectPinned()
4414                    .haveIds("ms1-alt", "s2")
4415
4416                    .revertToOriginalList()
4417                    .selectByIds("ms1-alt", "s2")
4418                    .areAllWithActivity(ACTIVITY2)
4419            ;
4420        });
4421
4422        // Re-enable activity 1.
4423        // Manifest shortcuts will be re-published, but dynamic ones are not.
4424        mEnabledActivityChecker = (activity, userId) -> true;
4425                mService.mPackageMonitor.onReceive(getTestContext(),
4426                genPackageChangedIntent(CALLING_PACKAGE_1, USER_10));
4427
4428        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4429            assertWith(getCallerShortcuts())
4430                    .haveIds("ms1", "ms1-alt", "s2")
4431                    .areAllEnabled()
4432
4433                    .selectPinned()
4434                    .haveIds("ms1-alt", "s2")
4435
4436                    .revertToOriginalList()
4437                    .selectByIds("ms1")
4438                    .areAllWithActivity(ACTIVITY1)
4439
4440                    .revertToOriginalList()
4441                    .selectByIds("ms1-alt", "s2")
4442                    .areAllWithActivity(ACTIVITY2)
4443                    ;
4444        });
4445
4446        // Disable activity 2
4447        // Because "ms1-alt" and "s2" are both pinned, they will remain, but disabled.
4448        mEnabledActivityChecker = (activity, userId) -> !ACTIVITY2.equals(activity);
4449                mService.mPackageMonitor.onReceive(getTestContext(),
4450                genPackageChangedIntent(CALLING_PACKAGE_1, USER_10));
4451
4452        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4453            assertWith(getCallerShortcuts())
4454                    .haveIds("ms1", "ms1-alt", "s2")
4455
4456                    .selectDynamic().isEmpty().revertToOriginalList() // no dynamics.
4457
4458                    .selectPinned()
4459                    .haveIds("ms1-alt", "s2")
4460                    .areAllDisabled()
4461
4462                    .revertToOriginalList()
4463                    .selectByIds("ms1")
4464                    .areAllWithActivity(ACTIVITY1)
4465                    .areAllEnabled()
4466            ;
4467        });
4468    }
4469
4470    public void testHandlePackageUpdate_activityNoLongerMain() throws Throwable {
4471        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4472            assertTrue(mManager.setDynamicShortcuts(list(
4473                    makeShortcutWithActivity("s1a",
4474                            new ComponentName(getCallingPackage(), "act1")),
4475                    makeShortcutWithActivity("s1b",
4476                            new ComponentName(getCallingPackage(), "act1")),
4477                    makeShortcutWithActivity("s2a",
4478                            new ComponentName(getCallingPackage(), "act2")),
4479                    makeShortcutWithActivity("s2b",
4480                            new ComponentName(getCallingPackage(), "act2")),
4481                    makeShortcutWithActivity("s3a",
4482                            new ComponentName(getCallingPackage(), "act3")),
4483                    makeShortcutWithActivity("s3b",
4484                            new ComponentName(getCallingPackage(), "act3"))
4485            )));
4486            assertWith(getCallerShortcuts())
4487                    .haveIds("s1a", "s1b", "s2a", "s2b", "s3a", "s3b")
4488                    .areAllDynamic();
4489        });
4490        runWithCaller(LAUNCHER_1, USER_0, () -> {
4491            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
4492                    list("s1b", "s2b", "s3b"),
4493                    HANDLE_USER_0);
4494        });
4495        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4496            assertWith(getCallerShortcuts())
4497                    .haveIds("s1a", "s1b", "s2a", "s2b", "s3a", "s3b")
4498                    .areAllDynamic()
4499
4500                    .selectByIds("s1b", "s2b", "s3b")
4501                    .areAllPinned();
4502        });
4503
4504        // Update the app and act2 and act3 are no longer main.
4505        mMainActivityChecker = (activity, userId) -> {
4506            return activity.getClassName().equals("act1");
4507        };
4508
4509        setCaller(LAUNCHER_1, USER_0);
4510        assertForLauncherCallback(mLauncherApps, () -> {
4511            updatePackageVersion(CALLING_PACKAGE_1, 1);
4512                    mService.mPackageMonitor.onReceive(getTestContext(),
4513                    genPackageUpdateIntent(CALLING_PACKAGE_1, USER_0));
4514        }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
4515                // Make sure the launcher gets callbacks.
4516                .haveIds("s1a", "s1b", "s2b", "s3b")
4517                .areAllWithKeyFieldsOnly();
4518
4519        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4520            // s2a and s3a are gone, but s2b and s3b will remain because they're pinned, and
4521            // disabled.
4522            assertWith(getCallerShortcuts())
4523                    .haveIds("s1a", "s1b", "s2b", "s3b")
4524
4525                    .selectByIds("s1a", "s1b")
4526                    .areAllDynamic()
4527                    .areAllEnabled()
4528
4529                    .revertToOriginalList()
4530                    .selectByIds("s2b", "s3b")
4531                    .areAllNotDynamic()
4532                    .areAllDisabled()
4533                    .areAllPinned()
4534                    ;
4535        });
4536    }
4537
4538    protected void prepareForBackupTest() {
4539
4540        prepareCrossProfileDataSet();
4541
4542        backupAndRestore();
4543    }
4544
4545    /**
4546     * Make sure the backup data doesn't have the following information:
4547     * - Launchers on other users.
4548     * - Non-backup app information.
4549     *
4550     * But restores all other infomation.
4551     *
4552     * It also omits the following pieces of information, but that's tested in
4553     * {@link ShortcutManagerTest2#testShortcutInfoSaveAndLoad_forBackup}.
4554     * - Unpinned dynamic shortcuts
4555     * - Bitmaps
4556     */
4557    public void testBackupAndRestore() {
4558        prepareForBackupTest();
4559
4560        checkBackupAndRestore_success();
4561    }
4562
4563    public void testBackupAndRestore_backupRestoreTwice() {
4564        prepareForBackupTest();
4565
4566        // Note doing a backup & restore again here shouldn't affect the result.
4567        dumpsysOnLogcat("Before second backup");
4568
4569        backupAndRestore();
4570
4571        dumpsysOnLogcat("After second backup");
4572
4573        checkBackupAndRestore_success();
4574    }
4575
4576    public void testBackupAndRestore_backupRestoreMultiple() {
4577        prepareForBackupTest();
4578
4579        // Note doing a backup & restore again here shouldn't affect the result.
4580        backupAndRestore();
4581
4582        // This also shouldn't affect the result.
4583        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4584            assertTrue(mManager.setDynamicShortcuts(list(
4585                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"),
4586                    makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6"))));
4587        });
4588
4589        backupAndRestore();
4590
4591        checkBackupAndRestore_success();
4592    }
4593
4594    public void testBackupAndRestore_restoreToNewVersion() {
4595        prepareForBackupTest();
4596
4597        // Note doing a backup & restore again here shouldn't affect the result.
4598        backupAndRestore();
4599
4600        addPackage(CALLING_PACKAGE_1, CALLING_UID_1, 2);
4601        addPackage(LAUNCHER_1, LAUNCHER_UID_1, 5);
4602
4603        checkBackupAndRestore_success();
4604    }
4605
4606    public void testBackupAndRestore_restoreToSuperSetSignatures() {
4607        prepareForBackupTest();
4608
4609        // Note doing a backup & restore again here shouldn't affect the result.
4610        backupAndRestore();
4611
4612        // Change package signatures.
4613        addPackage(CALLING_PACKAGE_1, CALLING_UID_1, 1, "sigx", CALLING_PACKAGE_1);
4614        addPackage(LAUNCHER_1, LAUNCHER_UID_1, 4, LAUNCHER_1, "sigy");
4615
4616        checkBackupAndRestore_success();
4617    }
4618
4619    protected void checkBackupAndRestore_success() {
4620        // Make sure non-system user is not restored.
4621        final ShortcutUser userP0 = mService.getUserShortcutsLocked(USER_P0);
4622        assertEquals(0, userP0.getAllPackagesForTest().size());
4623        assertEquals(0, userP0.getAllLaunchersForTest().size());
4624
4625        // Make sure only "allowBackup" apps are restored, and are shadow.
4626        final ShortcutUser user0 = mService.getUserShortcutsLocked(USER_0);
4627        assertExistsAndShadow(user0.getAllPackagesForTest().get(CALLING_PACKAGE_1));
4628        assertExistsAndShadow(user0.getAllPackagesForTest().get(CALLING_PACKAGE_2));
4629        assertExistsAndShadow(user0.getAllLaunchersForTest().get(
4630                PackageWithUser.of(USER_0, LAUNCHER_1)));
4631        assertExistsAndShadow(user0.getAllLaunchersForTest().get(
4632                PackageWithUser.of(USER_0, LAUNCHER_2)));
4633
4634        assertNull(user0.getAllPackagesForTest().get(CALLING_PACKAGE_3));
4635        assertNull(user0.getAllLaunchersForTest().get(PackageWithUser.of(USER_0, LAUNCHER_3)));
4636        assertNull(user0.getAllLaunchersForTest().get(PackageWithUser.of(USER_P0, LAUNCHER_1)));
4637
4638        installPackage(USER_0, CALLING_PACKAGE_1);
4639        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4640            assertWith(getCallerVisibleShortcuts())
4641                    .selectDynamic()
4642                    .isEmpty()
4643
4644                    .revertToOriginalList()
4645                    .selectPinned()
4646                    .haveIds("s1", "s2");
4647        });
4648
4649        installPackage(USER_0, LAUNCHER_1);
4650        runWithCaller(LAUNCHER_1, USER_0, () -> {
4651            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
4652                    .areAllPinned()
4653                    .haveIds("s1");
4654
4655            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
4656                    .isEmpty();
4657
4658            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
4659                    .isEmpty();
4660
4661            assertWith(mLauncherApps.getShortcuts(QUERY_ALL, HANDLE_USER_P0))
4662                    .isEmpty();
4663        });
4664
4665        installPackage(USER_0, CALLING_PACKAGE_2);
4666        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
4667            assertWith(getCallerVisibleShortcuts())
4668                    .selectDynamic()
4669                    .isEmpty()
4670
4671                    .revertToOriginalList()
4672                    .selectPinned()
4673                    .haveIds("s1", "s2", "s3");
4674        });
4675
4676        runWithCaller(LAUNCHER_1, USER_0, () -> {
4677            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
4678                    .areAllPinned()
4679                    .haveIds("s1");
4680
4681            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
4682                    .areAllPinned()
4683                    .haveIds("s1", "s2");
4684
4685            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
4686                    .isEmpty();
4687
4688            assertWith(mLauncherApps.getShortcuts(QUERY_ALL, HANDLE_USER_P0))
4689                    .isEmpty();
4690        });
4691
4692        // 3 shouldn't be backed up, so no pinned shortcuts.
4693        installPackage(USER_0, CALLING_PACKAGE_3);
4694        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
4695            assertWith(getCallerVisibleShortcuts())
4696                    .isEmpty();
4697        });
4698
4699        // Launcher on a different profile shouldn't be restored.
4700        runWithCaller(LAUNCHER_1, USER_P0, () -> {
4701            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
4702                    .isEmpty();
4703            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
4704                    .isEmpty();
4705            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
4706                    .isEmpty();
4707        });
4708
4709        // Package on a different profile, no restore.
4710        installPackage(USER_P0, CALLING_PACKAGE_1);
4711        runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
4712            assertWith(getCallerVisibleShortcuts())
4713                    .isEmpty();
4714        });
4715
4716        // Restore launcher 2 on user 0.
4717        installPackage(USER_0, LAUNCHER_2);
4718        runWithCaller(LAUNCHER_2, USER_0, () -> {
4719            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
4720                    .areAllPinned()
4721                    .haveIds("s2");
4722
4723            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
4724                    .areAllPinned()
4725                    .haveIds("s2", "s3");
4726
4727            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
4728                    .isEmpty();
4729
4730            assertWith(mLauncherApps.getShortcuts(QUERY_ALL, HANDLE_USER_P0))
4731                    .isEmpty();
4732        });
4733
4734
4735        // Restoration of launcher2 shouldn't affect other packages; so do the same checks and
4736        // make sure they still have the same result.
4737        installPackage(USER_0, CALLING_PACKAGE_1);
4738        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4739            assertWith(getCallerVisibleShortcuts())
4740                    .areAllPinned()
4741                    .haveIds("s1", "s2");
4742        });
4743
4744        installPackage(USER_0, LAUNCHER_1);
4745        runWithCaller(LAUNCHER_1, USER_0, () -> {
4746            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
4747                    .areAllPinned()
4748                    .haveIds("s1");
4749
4750            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
4751                    .areAllPinned()
4752                    .haveIds("s1", "s2");
4753
4754            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
4755                    .isEmpty();
4756
4757            assertWith(mLauncherApps.getShortcuts(QUERY_ALL, HANDLE_USER_P0))
4758                    .isEmpty();
4759        });
4760
4761        installPackage(USER_0, CALLING_PACKAGE_2);
4762        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
4763            assertWith(getCallerVisibleShortcuts())
4764                    .areAllPinned()
4765                    .haveIds("s1", "s2", "s3");
4766        });
4767    }
4768
4769    public void testBackupAndRestore_publisherLowerVersion() {
4770        prepareForBackupTest();
4771
4772        // Note doing a backup & restore again here shouldn't affect the result.
4773        backupAndRestore();
4774
4775        addPackage(CALLING_PACKAGE_1, CALLING_UID_1, 0); // Lower version
4776
4777        checkBackupAndRestore_publisherNotRestored();
4778    }
4779
4780    public void testBackupAndRestore_publisherWrongSignature() {
4781        prepareForBackupTest();
4782
4783        // Note doing a backup & restore again here shouldn't affect the result.
4784        backupAndRestore();
4785
4786        addPackage(CALLING_PACKAGE_1, CALLING_UID_1, 10, "sigx"); // different signature
4787
4788        checkBackupAndRestore_publisherNotRestored();
4789    }
4790
4791    public void testBackupAndRestore_publisherNoLongerBackupTarget() {
4792        prepareForBackupTest();
4793
4794        // Note doing a backup & restore again here shouldn't affect the result.
4795        backupAndRestore();
4796
4797        updatePackageInfo(CALLING_PACKAGE_1,
4798                pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP);
4799
4800        checkBackupAndRestore_publisherNotRestored();
4801    }
4802
4803    protected void checkBackupAndRestore_publisherNotRestored() {
4804        installPackage(USER_0, CALLING_PACKAGE_1);
4805        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4806            assertEquals(0, mManager.getDynamicShortcuts().size());
4807            assertEquals(0, mManager.getPinnedShortcuts().size());
4808        });
4809
4810        installPackage(USER_0, CALLING_PACKAGE_2);
4811        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
4812            assertEquals(0, mManager.getDynamicShortcuts().size());
4813            assertShortcutIds(assertAllPinned(
4814                    mManager.getPinnedShortcuts()),
4815                    "s1", "s2", "s3");
4816        });
4817
4818        installPackage(USER_0, LAUNCHER_1);
4819        runWithCaller(LAUNCHER_1, USER_0, () -> {
4820            assertShortcutIds(assertAllPinned(
4821                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
4822                    /* empty */);
4823            assertShortcutIds(assertAllPinned(
4824                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0)),
4825                    "s1", "s2");
4826            assertShortcutIds(assertAllPinned(
4827                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
4828                    /* empty */);
4829        });
4830        installPackage(USER_0, LAUNCHER_2);
4831        runWithCaller(LAUNCHER_2, USER_0, () -> {
4832            assertShortcutIds(assertAllPinned(
4833                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
4834                    /* empty */);
4835            assertShortcutIds(assertAllPinned(
4836                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0)),
4837                    "s2", "s3");
4838            assertShortcutIds(assertAllPinned(
4839                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
4840                    /* empty */);
4841        });
4842
4843        installPackage(USER_0, CALLING_PACKAGE_3);
4844        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
4845            assertEquals(0, mManager.getDynamicShortcuts().size());
4846            assertEquals(0, mManager.getPinnedShortcuts().size());
4847        });
4848
4849        runWithCaller(LAUNCHER_1, USER_0, () -> {
4850            assertShortcutIds(assertAllPinned(
4851                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
4852                    /* empty */);
4853            assertShortcutIds(assertAllPinned(
4854                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0)),
4855                    "s1", "s2");
4856            assertShortcutIds(assertAllPinned(
4857                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
4858                    /* empty */);
4859        });
4860        runWithCaller(LAUNCHER_2, USER_0, () -> {
4861            assertShortcutIds(assertAllPinned(
4862                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
4863                    /* empty */);
4864            assertShortcutIds(assertAllPinned(
4865                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0)),
4866                    "s2", "s3");
4867            assertShortcutIds(assertAllPinned(
4868                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
4869                    /* empty */);
4870        });
4871    }
4872
4873    public void testBackupAndRestore_launcherLowerVersion() {
4874        prepareForBackupTest();
4875
4876        // Note doing a backup & restore again here shouldn't affect the result.
4877        backupAndRestore();
4878
4879        addPackage(LAUNCHER_1, LAUNCHER_UID_1, 0); // Lower version
4880
4881        checkBackupAndRestore_launcherNotRestored();
4882    }
4883
4884    public void testBackupAndRestore_launcherWrongSignature() {
4885        prepareForBackupTest();
4886
4887        // Note doing a backup & restore again here shouldn't affect the result.
4888        backupAndRestore();
4889
4890        addPackage(LAUNCHER_1, LAUNCHER_UID_1, 10, "sigx"); // different signature
4891
4892        checkBackupAndRestore_launcherNotRestored();
4893    }
4894
4895    public void testBackupAndRestore_launcherNoLongerBackupTarget() {
4896        prepareForBackupTest();
4897
4898        // Note doing a backup & restore again here shouldn't affect the result.
4899        backupAndRestore();
4900
4901        updatePackageInfo(LAUNCHER_1,
4902                pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP);
4903
4904        checkBackupAndRestore_launcherNotRestored();
4905    }
4906
4907    protected void checkBackupAndRestore_launcherNotRestored() {
4908        installPackage(USER_0, CALLING_PACKAGE_1);
4909        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4910            assertEquals(0, mManager.getDynamicShortcuts().size());
4911
4912            // s1 was pinned by launcher 1, which is not restored, yet, so we still see "s1" here.
4913            assertShortcutIds(assertAllPinned(
4914                    mManager.getPinnedShortcuts()),
4915                    "s1", "s2");
4916        });
4917
4918        installPackage(USER_0, CALLING_PACKAGE_2);
4919        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
4920            assertEquals(0, mManager.getDynamicShortcuts().size());
4921            assertShortcutIds(assertAllPinned(
4922                    mManager.getPinnedShortcuts()),
4923                    "s1", "s2", "s3");
4924        });
4925
4926        // Now we try to restore launcher 1.  Then we realize it's not restorable, so L1 has no pinned
4927        // shortcuts.
4928        installPackage(USER_0, LAUNCHER_1);
4929        runWithCaller(LAUNCHER_1, USER_0, () -> {
4930            assertShortcutIds(assertAllPinned(
4931                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
4932                    /* empty */);
4933            assertShortcutIds(assertAllPinned(
4934                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
4935                    /* empty */);
4936            assertShortcutIds(assertAllPinned(
4937                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
4938                    /* empty */);
4939        });
4940
4941        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4942            assertEquals(0, mManager.getDynamicShortcuts().size());
4943
4944            // Now CALLING_PACKAGE_1 realizes "s1" is no longer pinned.
4945            assertShortcutIds(assertAllPinned(
4946                    mManager.getPinnedShortcuts()),
4947                    "s2");
4948        });
4949
4950        installPackage(USER_0, LAUNCHER_2);
4951        runWithCaller(LAUNCHER_2, USER_0, () -> {
4952            assertShortcutIds(assertAllPinned(
4953                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0)),
4954                    "s2");
4955            assertShortcutIds(assertAllPinned(
4956                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0)),
4957                    "s2", "s3");
4958            assertShortcutIds(assertAllPinned(
4959                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
4960                    /* empty */);
4961        });
4962
4963        installPackage(USER_0, CALLING_PACKAGE_3);
4964        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
4965            assertEquals(0, mManager.getDynamicShortcuts().size());
4966            assertEquals(0, mManager.getPinnedShortcuts().size());
4967        });
4968
4969        runWithCaller(LAUNCHER_1, USER_0, () -> {
4970            assertShortcutIds(assertAllPinned(
4971                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
4972                    /* empty */);
4973            assertShortcutIds(assertAllPinned(
4974                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
4975                    /* empty */);
4976            assertShortcutIds(assertAllPinned(
4977                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
4978                    /* empty */);
4979        });
4980        runWithCaller(LAUNCHER_2, USER_0, () -> {
4981            assertShortcutIds(assertAllPinned(
4982                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0)),
4983                    "s2");
4984            assertShortcutIds(assertAllPinned(
4985                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0)),
4986                    "s2", "s3");
4987            assertShortcutIds(assertAllPinned(
4988                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
4989                    /* empty */);
4990        });
4991    }
4992
4993    public void testBackupAndRestore_launcherAndPackageNoLongerBackupTarget() {
4994        prepareForBackupTest();
4995
4996        // Note doing a backup & restore again here shouldn't affect the result.
4997        backupAndRestore();
4998
4999        updatePackageInfo(CALLING_PACKAGE_1,
5000                pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP);
5001
5002        updatePackageInfo(LAUNCHER_1,
5003                pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP);
5004
5005        checkBackupAndRestore_publisherAndLauncherNotRestored();
5006    }
5007
5008    protected void checkBackupAndRestore_publisherAndLauncherNotRestored() {
5009        installPackage(USER_0, CALLING_PACKAGE_1);
5010        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5011            assertEquals(0, mManager.getDynamicShortcuts().size());
5012            assertEquals(0, mManager.getPinnedShortcuts().size());
5013        });
5014
5015        installPackage(USER_0, CALLING_PACKAGE_2);
5016        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5017            assertEquals(0, mManager.getDynamicShortcuts().size());
5018            assertShortcutIds(assertAllPinned(
5019                    mManager.getPinnedShortcuts()),
5020                    "s1", "s2", "s3");
5021        });
5022
5023        installPackage(USER_0, LAUNCHER_1);
5024        runWithCaller(LAUNCHER_1, USER_0, () -> {
5025            assertShortcutIds(assertAllPinned(
5026                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5027                    /* empty */);
5028            assertShortcutIds(assertAllPinned(
5029                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5030                    /* empty */);
5031            assertShortcutIds(assertAllPinned(
5032                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5033                    /* empty */);
5034        });
5035        installPackage(USER_0, LAUNCHER_2);
5036        runWithCaller(LAUNCHER_2, USER_0, () -> {
5037            assertShortcutIds(assertAllPinned(
5038                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5039                    /* empty */);
5040            assertShortcutIds(assertAllPinned(
5041                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0)),
5042                    "s2", "s3");
5043            assertShortcutIds(assertAllPinned(
5044                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5045                    /* empty */);
5046        });
5047
5048        // Because launcher 1 wasn't restored, "s1" is no longer pinned.
5049        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5050            assertEquals(0, mManager.getDynamicShortcuts().size());
5051            assertShortcutIds(assertAllPinned(
5052                    mManager.getPinnedShortcuts()),
5053                    "s2", "s3");
5054        });
5055
5056        installPackage(USER_0, CALLING_PACKAGE_3);
5057        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
5058            assertEquals(0, mManager.getDynamicShortcuts().size());
5059            assertEquals(0, mManager.getPinnedShortcuts().size());
5060        });
5061
5062        runWithCaller(LAUNCHER_1, USER_0, () -> {
5063            assertShortcutIds(assertAllPinned(
5064                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5065                    /* empty */);
5066            assertShortcutIds(assertAllPinned(
5067                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5068                    /* empty */);
5069            assertShortcutIds(assertAllPinned(
5070                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5071                    /* empty */);
5072        });
5073        runWithCaller(LAUNCHER_2, USER_0, () -> {
5074            assertShortcutIds(assertAllPinned(
5075                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5076                    /* empty */);
5077            assertShortcutIds(assertAllPinned(
5078                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0)),
5079                    "s2", "s3");
5080            assertShortcutIds(assertAllPinned(
5081                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5082                    /* empty */);
5083        });
5084    }
5085
5086    public void testBackupAndRestore_disabled() {
5087        prepareCrossProfileDataSet();
5088
5089        // Before doing backup & restore, disable s1.
5090        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5091            mManager.disableShortcuts(list("s1"));
5092        });
5093
5094        backupAndRestore();
5095
5096        // Below is copied from checkBackupAndRestore_success.
5097
5098        // Make sure non-system user is not restored.
5099        final ShortcutUser userP0 = mService.getUserShortcutsLocked(USER_P0);
5100        assertEquals(0, userP0.getAllPackagesForTest().size());
5101        assertEquals(0, userP0.getAllLaunchersForTest().size());
5102
5103        // Make sure only "allowBackup" apps are restored, and are shadow.
5104        final ShortcutUser user0 = mService.getUserShortcutsLocked(USER_0);
5105        assertExistsAndShadow(user0.getAllPackagesForTest().get(CALLING_PACKAGE_1));
5106        assertExistsAndShadow(user0.getAllPackagesForTest().get(CALLING_PACKAGE_2));
5107        assertExistsAndShadow(user0.getAllLaunchersForTest().get(
5108                PackageWithUser.of(USER_0, LAUNCHER_1)));
5109        assertExistsAndShadow(user0.getAllLaunchersForTest().get(
5110                PackageWithUser.of(USER_0, LAUNCHER_2)));
5111
5112        assertNull(user0.getAllPackagesForTest().get(CALLING_PACKAGE_3));
5113        assertNull(user0.getAllLaunchersForTest().get(PackageWithUser.of(USER_0, LAUNCHER_3)));
5114        assertNull(user0.getAllLaunchersForTest().get(PackageWithUser.of(USER_P0, LAUNCHER_1)));
5115
5116        installPackage(USER_0, CALLING_PACKAGE_1);
5117        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5118            assertWith(getCallerVisibleShortcuts())
5119                    .areAllEnabled() // disabled shortcuts shouldn't be restored.
5120
5121                    .selectDynamic()
5122                    .isEmpty()
5123
5124                    .revertToOriginalList()
5125                    .selectPinned()
5126                    // s1 is not restored.
5127                    .haveIds("s2");
5128        });
5129
5130        installPackage(USER_0, LAUNCHER_1);
5131        runWithCaller(LAUNCHER_1, USER_0, () -> {
5132            // Note, s1 was pinned by launcher 1, but was disabled, so isn't restored.
5133            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5134                    .isEmpty();
5135
5136            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5137                    .isEmpty();
5138
5139            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5140                    .isEmpty();
5141
5142            assertWith(mLauncherApps.getShortcuts(QUERY_ALL, HANDLE_USER_P0))
5143                    .isEmpty();
5144        });
5145    }
5146
5147
5148    public void testBackupAndRestore_manifestNotRestored() {
5149        // Publish two manifest shortcuts.
5150        addManifestShortcutResource(
5151                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
5152                R.xml.shortcut_2);
5153        updatePackageVersion(CALLING_PACKAGE_1, 1);
5154        mService.mPackageMonitor.onReceive(mServiceContext,
5155                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
5156
5157        // Pin from launcher 1.
5158        runWithCaller(LAUNCHER_1, USER_0, () -> {
5159            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms1", "ms2"), HANDLE_USER_0);
5160        });
5161
5162        // Update and now ms2 is gone -> disabled.
5163        addManifestShortcutResource(
5164                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
5165                R.xml.shortcut_1);
5166        updatePackageVersion(CALLING_PACKAGE_1, 1);
5167        mService.mPackageMonitor.onReceive(mServiceContext,
5168                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
5169
5170        // Make sure the manifest shortcuts have been published.
5171        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5172            assertWith(getCallerShortcuts())
5173                    .areAllPinned()
5174                    .haveIds("ms1", "ms2")
5175
5176                    .selectByIds("ms1")
5177                    .areAllManifest()
5178                    .areAllEnabled()
5179
5180                    .revertToOriginalList()
5181                    .selectByIds("ms2")
5182                    .areAllNotManifest()
5183                    .areAllDisabled();
5184        });
5185
5186        // Now do the regular backup & restore test.
5187        // The existence of the manifest shortcuts shouldn't affect the result.
5188        prepareCrossProfileDataSet();
5189        backupAndRestore();
5190    }
5191
5192    public void testSaveAndLoad_crossProfile() {
5193        prepareCrossProfileDataSet();
5194
5195        dumpsysOnLogcat("Before save & load");
5196
5197        mService.saveDirtyInfo();
5198        initService();
5199
5200        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5201            assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()),
5202                    "s1", "s2", "s3");
5203            assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()),
5204                    "s1", "s2", "s3", "s4");
5205        });
5206        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5207            assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()),
5208                    "s1", "s2", "s3");
5209            assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()),
5210                    "s1", "s2", "s3", "s4", "s5");
5211        });
5212        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
5213            assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()),
5214                    "s1", "s2", "s3");
5215            assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()),
5216                    "s1", "s2", "s3", "s4", "s5", "s6");
5217        });
5218        runWithCaller(CALLING_PACKAGE_4, USER_0, () -> {
5219            assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts())
5220                    /* empty */);
5221            assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts())
5222                    /* empty */);
5223        });
5224        runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
5225            assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()),
5226                    "s1", "s2", "s3");
5227            assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()),
5228                    "s1", "s2", "s3", "s4", "s5", "s6");
5229        });
5230        runWithCaller(CALLING_PACKAGE_2, USER_P0, () -> {
5231            assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts())
5232                    /* empty */);
5233            assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts())
5234                    /* empty */);
5235        });
5236        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
5237            assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()),
5238                    "x1", "x2", "x3");
5239            assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()),
5240                    "x4", "x5");
5241        });
5242        runWithCaller(LAUNCHER_1, USER_0, () -> {
5243            assertShortcutIds(
5244                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_0),
5245                    "s1");
5246            assertShortcutIds(
5247                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_0),
5248                    "s1", "s2");
5249            assertShortcutIds(
5250                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_3), HANDLE_USER_0),
5251                    "s1", "s2", "s3");
5252            assertShortcutIds(
5253                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_4), HANDLE_USER_0)
5254                    /* empty */);
5255            assertShortcutIds(
5256                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_P0),
5257                    "s1", "s4");
5258            assertShortcutIds(
5259                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_P0)
5260                    /* empty */);
5261            assertExpectException(
5262                    SecurityException.class, "", () -> {
5263                        mLauncherApps.getShortcuts(
5264                                buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_10);
5265                    });
5266        });
5267        runWithCaller(LAUNCHER_2, USER_0, () -> {
5268            assertShortcutIds(
5269                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_0),
5270                    "s2");
5271            assertShortcutIds(
5272                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_0),
5273                    "s2", "s3");
5274            assertShortcutIds(
5275                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_3), HANDLE_USER_0),
5276                    "s2", "s3", "s4");
5277            assertShortcutIds(
5278                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_4), HANDLE_USER_0)
5279                    /* empty */);
5280            assertShortcutIds(
5281                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_P0),
5282                    "s2", "s5");
5283            assertShortcutIds(
5284                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_P0)
5285                    /* empty */);
5286        });
5287        runWithCaller(LAUNCHER_3, USER_0, () -> {
5288            assertShortcutIds(
5289                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_0),
5290                    "s3");
5291            assertShortcutIds(
5292                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_0),
5293                    "s3", "s4");
5294            assertShortcutIds(
5295                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_3), HANDLE_USER_0),
5296                    "s3", "s4", "s5");
5297            assertShortcutIds(
5298                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_4), HANDLE_USER_0)
5299                    /* empty */);
5300            assertShortcutIds(
5301                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_P0),
5302                    "s3", "s6");
5303            assertShortcutIds(
5304                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_P0)
5305                    /* empty */);
5306        });
5307        runWithCaller(LAUNCHER_4, USER_0, () -> {
5308            assertShortcutIds(
5309                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_0)
5310                    /* empty */);
5311            assertShortcutIds(
5312                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_0)
5313                    /* empty */);
5314            assertShortcutIds(
5315                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_3), HANDLE_USER_0)
5316                    /* empty */);
5317            assertShortcutIds(
5318                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_4), HANDLE_USER_0)
5319                    /* empty */);
5320            assertShortcutIds(
5321                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_P0)
5322                    /* empty */);
5323            assertShortcutIds(
5324                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_P0)
5325                    /* empty */);
5326        });
5327        runWithCaller(LAUNCHER_1, USER_P0, () -> {
5328            assertShortcutIds(
5329                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_0),
5330                    "s3", "s4");
5331            assertShortcutIds(
5332                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_0),
5333                    "s3", "s4", "s5");
5334            assertShortcutIds(
5335                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_3), HANDLE_USER_0),
5336                    "s3", "s4", "s5", "s6");
5337            assertShortcutIds(
5338                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_P0),
5339                    "s1", "s4");
5340            assertExpectException(
5341                    SecurityException.class, "unrelated profile", () -> {
5342                        mLauncherApps.getShortcuts(
5343                                buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_10);
5344                    });
5345        });
5346        runWithCaller(LAUNCHER_1, USER_10, () -> {
5347            assertShortcutIds(
5348                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_10),
5349                    "x4", "x5");
5350            assertShortcutIds(
5351                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_10)
5352                    /* empty */);
5353            assertShortcutIds(
5354                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_3), HANDLE_USER_10)
5355                    /* empty */);
5356            assertExpectException(
5357                    SecurityException.class, "unrelated profile", () -> {
5358                        mLauncherApps.getShortcuts(
5359                                buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0);
5360                    });
5361            assertExpectException(
5362                    SecurityException.class, "unrelated profile", () -> {
5363                        mLauncherApps.getShortcuts(
5364                                buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_P0);
5365                    });
5366        });
5367    }
5368
5369    public void testOnApplicationActive_permission() {
5370        assertExpectException(SecurityException.class, "Missing permission", () ->
5371                mManager.onApplicationActive(CALLING_PACKAGE_1, USER_0));
5372
5373        // Has permission, now it should pass.
5374        mCallerPermissions.add(permission.RESET_SHORTCUT_MANAGER_THROTTLING);
5375        mManager.onApplicationActive(CALLING_PACKAGE_1, USER_0);
5376    }
5377
5378    public void testDumpsys_crossProfile() {
5379        prepareCrossProfileDataSet();
5380        dumpsysOnLogcat("test1", /* force= */ true);
5381    }
5382
5383    public void testDumpsys_withIcons() throws IOException {
5384        testIcons();
5385        // Dump after having some icons.
5386        dumpsysOnLogcat("test1", /* force= */ true);
5387    }
5388
5389    public void testManifestShortcut_publishOnUnlockUser() {
5390        addManifestShortcutResource(
5391                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
5392                R.xml.shortcut_1);
5393        addManifestShortcutResource(
5394                new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
5395                R.xml.shortcut_2);
5396        addManifestShortcutResource(
5397                new ComponentName(CALLING_PACKAGE_3, ShortcutActivity.class.getName()),
5398                R.xml.shortcut_5);
5399
5400        // Unlock user-0.
5401        mService.handleUnlockUser(USER_0);
5402
5403        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5404            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5405                    mManager.getManifestShortcuts()))),
5406                    "ms1");
5407            assertEmpty(mManager.getPinnedShortcuts());
5408        });
5409
5410        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5411            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5412                    mManager.getManifestShortcuts()))),
5413                    "ms1", "ms2");
5414            assertEmpty(mManager.getPinnedShortcuts());
5415        });
5416
5417        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
5418            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5419                    mManager.getManifestShortcuts()))),
5420                    "ms1", "ms2", "ms3", "ms4", "ms5");
5421            assertEmpty(mManager.getPinnedShortcuts());
5422        });
5423
5424        // Try on another user, with some packages uninstalled.
5425        uninstallPackage(USER_10, CALLING_PACKAGE_1);
5426        uninstallPackage(USER_10, CALLING_PACKAGE_3);
5427
5428        mService.handleUnlockUser(USER_10);
5429
5430        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
5431            assertEmpty(mManager.getManifestShortcuts());
5432            assertEmpty(mManager.getPinnedShortcuts());
5433        });
5434
5435        runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
5436            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5437                    mManager.getManifestShortcuts()))),
5438                    "ms1", "ms2");
5439            assertEmpty(mManager.getPinnedShortcuts());
5440        });
5441
5442        runWithCaller(CALLING_PACKAGE_3, USER_10, () -> {
5443            assertEmpty(mManager.getManifestShortcuts());
5444            assertEmpty(mManager.getPinnedShortcuts());
5445        });
5446
5447        // Now change the resources for package 1, and unlock again.
5448        // But we still see *old* shortcuts, because the package version and install time
5449        // hasn't changed.
5450        shutdownServices();
5451
5452        addManifestShortcutResource(
5453                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
5454                R.xml.shortcut_5);
5455        addManifestShortcutResource(
5456                new ComponentName(CALLING_PACKAGE_3, ShortcutActivity.class.getName()),
5457                R.xml.shortcut_1);
5458
5459        initService();
5460        mService.handleUnlockUser(USER_0);
5461
5462        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5463            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5464                    mManager.getManifestShortcuts()))),
5465                    "ms1");
5466            assertEmpty(mManager.getPinnedShortcuts());
5467        });
5468
5469        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5470            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5471                    mManager.getManifestShortcuts()))),
5472                    "ms1", "ms2");
5473            assertEmpty(mManager.getPinnedShortcuts());
5474        });
5475
5476        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
5477            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5478                    mManager.getManifestShortcuts()))),
5479                    "ms1", "ms2", "ms3", "ms4", "ms5");
5480            assertEmpty(mManager.getPinnedShortcuts());
5481        });
5482
5483        // Do it again, but this time we change the app version, so we do detect the changes.
5484        shutdownServices();
5485
5486        updatePackageVersion(CALLING_PACKAGE_1, 1);
5487        updatePackageLastUpdateTime(CALLING_PACKAGE_3, 1);
5488
5489        initService();
5490        mService.handleUnlockUser(USER_0);
5491
5492        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5493            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5494                    mManager.getManifestShortcuts()))),
5495                    "ms1", "ms2", "ms3", "ms4", "ms5");
5496            assertEmpty(mManager.getPinnedShortcuts());
5497        });
5498
5499        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5500            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5501                    mManager.getManifestShortcuts()))),
5502                    "ms1", "ms2");
5503            assertEmpty(mManager.getPinnedShortcuts());
5504        });
5505
5506        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
5507            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5508                    mManager.getManifestShortcuts()))),
5509                    "ms1");
5510            assertEmpty(mManager.getPinnedShortcuts());
5511        });
5512
5513        // Next, try removing all shortcuts, with some of them pinned.
5514        runWithCaller(LAUNCHER_1, USER_0, () -> {
5515            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms3"), HANDLE_USER_0);
5516            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("ms2"), HANDLE_USER_0);
5517            mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("ms1"), HANDLE_USER_0);
5518        });
5519        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5520            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5521                    mManager.getManifestShortcuts()))),
5522                    "ms1", "ms2", "ms3", "ms4", "ms5");
5523            assertShortcutIds(assertAllImmutable(assertAllPinned(assertAllManifest(
5524                    assertAllEnabled(mManager.getPinnedShortcuts())))),
5525                    "ms3");
5526        });
5527
5528        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5529            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5530                    mManager.getManifestShortcuts()))),
5531                    "ms1", "ms2");
5532            assertShortcutIds(assertAllImmutable(assertAllPinned(assertAllManifest(
5533                    assertAllEnabled(mManager.getPinnedShortcuts())))),
5534                    "ms2");
5535        });
5536
5537        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
5538            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5539                    mManager.getManifestShortcuts()))),
5540                    "ms1");
5541            assertShortcutIds(assertAllImmutable(assertAllPinned(assertAllManifest(
5542                    assertAllEnabled(mManager.getPinnedShortcuts())))),
5543                    "ms1");
5544        });
5545
5546        shutdownServices();
5547
5548        addManifestShortcutResource(
5549                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
5550                R.xml.shortcut_0);
5551        addManifestShortcutResource(
5552                new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
5553                R.xml.shortcut_1);
5554        addManifestShortcutResource(
5555                new ComponentName(CALLING_PACKAGE_3, ShortcutActivity.class.getName()),
5556                R.xml.shortcut_0);
5557
5558        updatePackageVersion(CALLING_PACKAGE_1, 1);
5559        updatePackageVersion(CALLING_PACKAGE_2, 1);
5560        updatePackageVersion(CALLING_PACKAGE_3, 1);
5561
5562        initService();
5563        mService.handleUnlockUser(USER_0);
5564
5565        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5566            assertEmpty(mManager.getManifestShortcuts());
5567            assertShortcutIds(assertAllImmutable(assertAllPinned(assertAllNotManifest(
5568                    assertAllDisabled(mManager.getPinnedShortcuts())))),
5569                    "ms3");
5570        });
5571
5572        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5573            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5574                    mManager.getManifestShortcuts()))),
5575                    "ms1");
5576            assertShortcutIds(assertAllImmutable(assertAllPinned(assertAllNotManifest(
5577                    assertAllDisabled(mManager.getPinnedShortcuts())))),
5578                    "ms2");
5579        });
5580
5581        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
5582            assertEmpty(mManager.getManifestShortcuts());
5583            assertShortcutIds(assertAllImmutable(assertAllPinned(assertAllNotManifest(
5584                    assertAllDisabled(mManager.getPinnedShortcuts())))),
5585                    "ms1");
5586        });
5587
5588        // Make sure we don't have ShortcutPackage for packages that don't have shortcuts.
5589        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_4, USER_0));
5590        assertNull(mService.getPackageShortcutForTest(LAUNCHER_1, USER_0));
5591    }
5592
5593    public void testManifestShortcut_publishOnBroadcast() {
5594        // First, no packages are installed.
5595        uninstallPackage(USER_0, CALLING_PACKAGE_1);
5596        uninstallPackage(USER_0, CALLING_PACKAGE_2);
5597        uninstallPackage(USER_0, CALLING_PACKAGE_3);
5598        uninstallPackage(USER_0, CALLING_PACKAGE_4);
5599        uninstallPackage(USER_10, CALLING_PACKAGE_1);
5600        uninstallPackage(USER_10, CALLING_PACKAGE_2);
5601        uninstallPackage(USER_10, CALLING_PACKAGE_3);
5602        uninstallPackage(USER_10, CALLING_PACKAGE_4);
5603
5604        mService.handleUnlockUser(USER_0);
5605        mService.handleUnlockUser(USER_10);
5606
5607        // Originally no manifest shortcuts.
5608        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5609            assertEmpty(mManager.getManifestShortcuts());
5610            assertEmpty(mManager.getPinnedShortcuts());
5611        });
5612
5613        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5614            assertEmpty(mManager.getManifestShortcuts());
5615            assertEmpty(mManager.getPinnedShortcuts());
5616        });
5617
5618        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
5619            assertEmpty(mManager.getManifestShortcuts());
5620            assertEmpty(mManager.getPinnedShortcuts());
5621        });
5622
5623        // Package 1 updated, with manifest shortcuts.
5624        addManifestShortcutResource(
5625                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
5626                R.xml.shortcut_1);
5627        updatePackageVersion(CALLING_PACKAGE_1, 1);
5628                mService.mPackageMonitor.onReceive(getTestContext(),
5629                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
5630
5631        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5632            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5633                    mManager.getManifestShortcuts()))),
5634                    "ms1");
5635            assertEmpty(mManager.getPinnedShortcuts());
5636        });
5637
5638        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5639            assertEmpty(mManager.getManifestShortcuts());
5640            assertEmpty(mManager.getPinnedShortcuts());
5641        });
5642
5643        // Package 2 updated, with manifest shortcuts.
5644
5645        addManifestShortcutResource(
5646                new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
5647                R.xml.shortcut_5);
5648        updatePackageVersion(CALLING_PACKAGE_2, 1);
5649                mService.mPackageMonitor.onReceive(getTestContext(),
5650                genPackageAddIntent(CALLING_PACKAGE_2, USER_0));
5651
5652        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5653            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5654                    mManager.getManifestShortcuts()))),
5655                    "ms1");
5656            assertEmpty(mManager.getPinnedShortcuts());
5657        });
5658
5659        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5660            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5661                    mManager.getManifestShortcuts()))),
5662                    "ms1", "ms2", "ms3", "ms4", "ms5");
5663            assertWith(getCallerShortcuts()).selectManifest()
5664                    .selectByActivity(
5665                            new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()))
5666                    .haveRanksInOrder("ms1", "ms2", "ms3", "ms4", "ms5");
5667            assertEmpty(mManager.getPinnedShortcuts());
5668        });
5669
5670        // Package 2 updated, with less manifest shortcuts.
5671        // This time we use updatePackageLastUpdateTime() instead of updatePackageVersion().
5672
5673        dumpsysOnLogcat("Before pinning");
5674
5675        // Also pin some.
5676        runWithCaller(LAUNCHER_1, USER_0, () -> {
5677            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("ms2", "ms3"), HANDLE_USER_0);
5678        });
5679
5680        dumpsysOnLogcat("After pinning");
5681
5682        addManifestShortcutResource(
5683                new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
5684                R.xml.shortcut_2);
5685        updatePackageLastUpdateTime(CALLING_PACKAGE_2, 1);
5686                mService.mPackageMonitor.onReceive(getTestContext(),
5687                genPackageAddIntent(CALLING_PACKAGE_2, USER_0));
5688
5689        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5690            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5691                    mManager.getManifestShortcuts()))),
5692                    "ms1");
5693            assertEmpty(mManager.getPinnedShortcuts());
5694        });
5695
5696        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5697            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5698                    mManager.getManifestShortcuts()))),
5699                    "ms1", "ms2");
5700            assertWith(getCallerShortcuts()).selectManifest()
5701                    .selectByActivity(
5702                            new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()))
5703                    .haveRanksInOrder("ms1", "ms2");
5704            assertShortcutIds(assertAllImmutable(assertAllPinned(
5705                    mManager.getPinnedShortcuts())),
5706                    "ms2", "ms3");
5707            // ms3 is no longer in manifest, so should be disabled.
5708            // but ms1 and ms2 should be enabled.
5709            assertAllEnabled(list(getCallerShortcut("ms1")));
5710            assertAllEnabled(list(getCallerShortcut("ms2")));
5711            assertAllDisabled(list(getCallerShortcut("ms3")));
5712        });
5713
5714        // Package 2 on user 10 has no shortcuts yet.
5715        runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
5716            assertEmpty(mManager.getManifestShortcuts());
5717            assertEmpty(mManager.getPinnedShortcuts());
5718        });
5719        // Send add broadcast, but the user is not running, so should be ignored.
5720        mRunningUsers.put(USER_10, false);
5721        mUnlockedUsers.put(USER_10, false);
5722
5723        mService.mPackageMonitor.onReceive(getTestContext(),
5724                genPackageAddIntent(CALLING_PACKAGE_2, USER_10));
5725        runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
5726            // Don't use the mManager APIs to get shortcuts, because they'll trigger the package
5727            // update check.
5728            // So look the internal data directly using getCallerShortcuts().
5729            assertEmpty(getCallerShortcuts());
5730        });
5731
5732        // Try again, but the user is locked, so still ignored.
5733        mRunningUsers.put(USER_10, true);
5734                mService.mPackageMonitor.onReceive(getTestContext(),
5735                genPackageAddIntent(CALLING_PACKAGE_2, USER_10));
5736        runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
5737            // Don't use the mManager APIs to get shortcuts, because they'll trigger the package
5738            // update check.
5739            // So look the internal data directly using getCallerShortcuts().
5740            assertEmpty(getCallerShortcuts());
5741        });
5742
5743        // Unlock the user, now it should work.
5744        mUnlockedUsers.put(USER_10, true);
5745
5746        // Send PACKAGE_ADD broadcast to have Package 2 on user-10 publish manifest shortcuts.
5747                mService.mPackageMonitor.onReceive(getTestContext(),
5748                genPackageAddIntent(CALLING_PACKAGE_2, USER_10));
5749
5750        runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
5751            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5752                    mManager.getManifestShortcuts()))),
5753                    "ms1", "ms2");
5754            assertWith(getCallerShortcuts()).selectManifest()
5755                    .selectByActivity(
5756                            new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()))
5757                    .haveRanksInOrder("ms1", "ms2");
5758            assertEmpty(mManager.getPinnedShortcuts());
5759        });
5760
5761        // But it shouldn't affect user-0.
5762        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5763            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5764                    mManager.getManifestShortcuts()))),
5765                    "ms1", "ms2");
5766            assertWith(getCallerShortcuts()).selectManifest()
5767                    .selectByActivity(
5768                            new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()))
5769                    .haveRanksInOrder("ms1", "ms2");
5770            assertShortcutIds(assertAllImmutable(assertAllPinned(
5771                    mManager.getPinnedShortcuts())),
5772                    "ms2", "ms3");
5773            assertAllEnabled(list(getCallerShortcut("ms1")));
5774            assertAllEnabled(list(getCallerShortcut("ms2")));
5775            assertAllDisabled(list(getCallerShortcut("ms3")));
5776        });
5777
5778        // Multiple activities.
5779        // Add shortcuts on activity 2 for package 2.
5780        addManifestShortcutResource(
5781                new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
5782                R.xml.shortcut_5_alt);
5783        addManifestShortcutResource(
5784                new ComponentName(CALLING_PACKAGE_2, ShortcutActivity2.class.getName()),
5785                R.xml.shortcut_5_reverse);
5786
5787        updatePackageLastUpdateTime(CALLING_PACKAGE_2, 1);
5788                mService.mPackageMonitor.onReceive(getTestContext(),
5789                genPackageAddIntent(CALLING_PACKAGE_2, USER_0));
5790
5791        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5792            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5793                    mManager.getManifestShortcuts()))),
5794                    "ms1", "ms2", "ms3", "ms4", "ms5",
5795                    "ms1_alt", "ms2_alt", "ms3_alt", "ms4_alt", "ms5_alt");
5796
5797            // Make sure they have the correct ranks, regardless of their ID's alphabetical order.
5798            assertWith(getCallerShortcuts()).selectManifest()
5799                    .selectByActivity(
5800                            new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()))
5801                    .haveRanksInOrder("ms1_alt", "ms2_alt", "ms3_alt", "ms4_alt", "ms5_alt");
5802            assertWith(getCallerShortcuts()).selectManifest()
5803                    .selectByActivity(
5804                            new ComponentName(CALLING_PACKAGE_2, ShortcutActivity2.class.getName()))
5805                    .haveRanksInOrder("ms5", "ms4", "ms3", "ms2", "ms1");
5806        });
5807
5808        // Package 2 now has no manifest shortcuts.
5809        addManifestShortcutResource(
5810                new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
5811                R.xml.shortcut_0);
5812        addManifestShortcutResource(
5813                new ComponentName(CALLING_PACKAGE_2, ShortcutActivity2.class.getName()),
5814                R.xml.shortcut_0);
5815        updatePackageLastUpdateTime(CALLING_PACKAGE_2, 1);
5816                mService.mPackageMonitor.onReceive(getTestContext(),
5817                genPackageAddIntent(CALLING_PACKAGE_2, USER_0));
5818
5819        // No manifest shortcuts, and pinned ones are disabled.
5820        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5821            assertEmpty(mManager.getManifestShortcuts());
5822            assertShortcutIds(assertAllImmutable(assertAllPinned(assertAllDisabled(
5823                    mManager.getPinnedShortcuts()))),
5824                    "ms2", "ms3");
5825        });
5826    }
5827
5828    public void testManifestShortcuts_missingMandatoryFields() {
5829        // Start with no apps installed.
5830        uninstallPackage(USER_0, CALLING_PACKAGE_1);
5831        uninstallPackage(USER_0, CALLING_PACKAGE_2);
5832        uninstallPackage(USER_0, CALLING_PACKAGE_3);
5833        uninstallPackage(USER_0, CALLING_PACKAGE_4);
5834
5835        mService.handleUnlockUser(USER_0);
5836
5837        // Make sure no manifest shortcuts.
5838        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5839            assertEmpty(mManager.getManifestShortcuts());
5840        });
5841
5842        // Package 1 updated, which has one valid manifest shortcut and one invalid.
5843        addManifestShortcutResource(
5844                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
5845                R.xml.shortcut_error_1);
5846        updatePackageVersion(CALLING_PACKAGE_1, 1);
5847                mService.mPackageMonitor.onReceive(getTestContext(),
5848                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
5849
5850        // Only the valid one is published.
5851        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5852            assertWith(getCallerShortcuts())
5853                    .areAllManifest()
5854                    .areAllImmutable()
5855                    .areAllEnabled()
5856                    .haveIds("x1");
5857        });
5858
5859        // Package 1 updated, which has one valid manifest shortcut and one invalid.
5860        addManifestShortcutResource(
5861                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
5862                R.xml.shortcut_error_2);
5863        updatePackageVersion(CALLING_PACKAGE_1, 1);
5864                mService.mPackageMonitor.onReceive(getTestContext(),
5865                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
5866
5867        // Only the valid one is published.
5868        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5869            assertWith(getCallerShortcuts())
5870                    .areAllManifest()
5871                    .areAllImmutable()
5872                    .areAllEnabled()
5873                    .haveIds("x2");
5874        });
5875
5876        // Package 1 updated, which has one valid manifest shortcut and one invalid.
5877        addManifestShortcutResource(
5878                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
5879                R.xml.shortcut_error_3);
5880        updatePackageVersion(CALLING_PACKAGE_1, 1);
5881                mService.mPackageMonitor.onReceive(getTestContext(),
5882                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
5883
5884        // Only the valid one is published.
5885        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5886            assertWith(getCallerShortcuts())
5887                    .areAllManifest()
5888                    .areAllImmutable()
5889                    .areAllEnabled()
5890                    .haveIds("x3")
5891                    .forShortcutWithId("x3", si -> {
5892                        assertEquals(set("cat2"), si.getCategories());
5893                     });
5894        });
5895    }
5896
5897    public void testManifestShortcuts_intentDefinitions() {
5898        addManifestShortcutResource(
5899                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
5900                R.xml.shortcut_error_4);
5901        updatePackageVersion(CALLING_PACKAGE_1, 1);
5902                mService.mPackageMonitor.onReceive(getTestContext(),
5903                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
5904
5905        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5906            // Make sure invalid ones are not published.
5907            // Note that at this point disabled ones don't show up because they weren't pinned.
5908            assertWith(getCallerShortcuts())
5909                    .haveIds("ms1", "ms2")
5910                    .areAllManifest()
5911                    .areAllNotDynamic()
5912                    .areAllNotPinned()
5913                    .areAllImmutable()
5914                    .areAllEnabled()
5915                    .forShortcutWithId("ms1", si -> {
5916                        assertTrue(si.isEnabled());
5917                        assertEquals(1, si.getIntents().length);
5918
5919                        assertEquals("action1", si.getIntent().getAction());
5920                        assertEquals("value1", si.getIntent().getStringExtra("key1"));
5921                        assertEquals(Intent.FLAG_ACTIVITY_NEW_TASK |
5922                                Intent.FLAG_ACTIVITY_CLEAR_TASK |
5923                                Intent.FLAG_ACTIVITY_TASK_ON_HOME, si.getIntent().getFlags());
5924
5925                        assertEquals("action1", si.getIntents()[0].getAction());
5926                        assertEquals("value1", si.getIntents()[0].getStringExtra("key1"));
5927                        assertEquals(Intent.FLAG_ACTIVITY_NEW_TASK |
5928                                Intent.FLAG_ACTIVITY_CLEAR_TASK |
5929                                Intent.FLAG_ACTIVITY_TASK_ON_HOME, si.getIntents()[0].getFlags());
5930                    })
5931                    .forShortcutWithId("ms2", si -> {
5932                        assertTrue(si.isEnabled());
5933                        assertEquals(2, si.getIntents().length);
5934
5935                        // getIntent will return the last one.
5936                        assertEquals("action2_2", si.getIntent().getAction());
5937                        assertEquals("value2", si.getIntent().getStringExtra("key2"));
5938                        assertEquals(0, si.getIntent().getFlags());
5939
5940                        final Intent i1 = si.getIntents()[0];
5941                        final Intent i2 = si.getIntents()[1];
5942
5943                        assertEquals("action2_1", i1.getAction());
5944                        assertEquals("value1", i1.getStringExtra("key1"));
5945                        assertEquals(Intent.FLAG_ACTIVITY_NEW_TASK |
5946                                        Intent.FLAG_ACTIVITY_CLEAR_TASK |
5947                                        Intent.FLAG_ACTIVITY_TASK_ON_HOME, i1.getFlags());
5948
5949                        assertEquals("action2_2", i2.getAction());
5950                        assertEquals("value2", i2.getStringExtra("key2"));
5951                        assertEquals(0, i2.getFlags());
5952                    });
5953        });
5954
5955        // Publish 5 enabled to pin some, so we can later test disabled manfiest shortcuts..
5956        addManifestShortcutResource(
5957                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
5958                R.xml.shortcut_5);
5959        updatePackageVersion(CALLING_PACKAGE_1, 1);
5960                mService.mPackageMonitor.onReceive(getTestContext(),
5961                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
5962
5963        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5964            // Make sure 5 manifest shortcuts are published.
5965            assertWith(getCallerShortcuts())
5966                    .haveIds("ms1", "ms2", "ms3", "ms4", "ms5")
5967                    .areAllManifest()
5968                    .areAllNotDynamic()
5969                    .areAllNotPinned()
5970                    .areAllImmutable()
5971                    .areAllEnabled();
5972        });
5973
5974        runWithCaller(LAUNCHER_1, USER_0, () -> {
5975            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
5976                    list("ms3", "ms4", "ms5"), HANDLE_USER_0);
5977        });
5978
5979        // Make sure they're pinned.
5980        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5981            assertWith(getCallerShortcuts())
5982                    .haveIds("ms1", "ms2", "ms3", "ms4", "ms5")
5983                    .selectByIds("ms1", "ms2")
5984                    .areAllNotPinned()
5985                    .areAllEnabled()
5986
5987                    .revertToOriginalList()
5988                    .selectByIds("ms3", "ms4", "ms5")
5989                    .areAllPinned()
5990                    .areAllEnabled();
5991        });
5992
5993        // Update the app.
5994        addManifestShortcutResource(
5995                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
5996                R.xml.shortcut_error_4);
5997        updatePackageVersion(CALLING_PACKAGE_1, 1);
5998                mService.mPackageMonitor.onReceive(getTestContext(),
5999                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6000
6001        // Make sure 3, 4 and 5 still exist but disabled.
6002        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6003            assertWith(getCallerShortcuts())
6004                    .haveIds("ms1", "ms2", "ms3", "ms4", "ms5")
6005                    .areAllNotDynamic()
6006                    .areAllImmutable()
6007
6008                    .selectByIds("ms1", "ms2")
6009                    .areAllManifest()
6010                    .areAllNotPinned()
6011                    .areAllEnabled()
6012
6013                    .revertToOriginalList()
6014                    .selectByIds("ms3", "ms4", "ms5")
6015                    .areAllNotManifest()
6016                    .areAllPinned()
6017                    .areAllDisabled()
6018
6019                    .revertToOriginalList()
6020                    .forShortcutWithId("ms1", si -> {
6021                        assertEquals(si.getId(), "action1", si.getIntent().getAction());
6022                    })
6023                    .forShortcutWithId("ms2", si -> {
6024                        // getIntent returns the last one.
6025                        assertEquals(si.getId(), "action2_2", si.getIntent().getAction());
6026                    })
6027                    .forShortcutWithId("ms3", si -> {
6028                        assertEquals(si.getId(), Intent.ACTION_VIEW, si.getIntent().getAction());
6029                    })
6030                    .forShortcutWithId("ms4", si -> {
6031                        assertEquals(si.getId(), Intent.ACTION_VIEW, si.getIntent().getAction());
6032                    })
6033                    .forShortcutWithId("ms5", si -> {
6034                        assertEquals(si.getId(), "action", si.getIntent().getAction());
6035                    });
6036        });
6037    }
6038
6039    public void testManifestShortcuts_checkAllFields() {
6040        mService.handleUnlockUser(USER_0);
6041
6042        // Package 1 updated, which has one valid manifest shortcut and one invalid.
6043        addManifestShortcutResource(
6044                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6045                R.xml.shortcut_5);
6046        updatePackageVersion(CALLING_PACKAGE_1, 1);
6047                mService.mPackageMonitor.onReceive(getTestContext(),
6048                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6049
6050        // Only the valid one is published.
6051        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6052            assertWith(getCallerShortcuts())
6053                    .haveIds("ms1", "ms2", "ms3", "ms4", "ms5")
6054                    .areAllManifest()
6055                    .areAllImmutable()
6056                    .areAllEnabled()
6057                    .areAllNotPinned()
6058                    .areAllNotDynamic()
6059
6060                    .forShortcutWithId("ms1", si -> {
6061                        assertEquals(R.drawable.icon1, si.getIconResourceId());
6062                        assertEquals(new ComponentName(CALLING_PACKAGE_1,
6063                                ShortcutActivity.class.getName()),
6064                                si.getActivity());
6065
6066                        assertEquals(R.string.shortcut_title1, si.getTitleResId());
6067                        assertEquals("r" + R.string.shortcut_title1, si.getTitleResName());
6068                        assertEquals(R.string.shortcut_text1, si.getTextResId());
6069                        assertEquals("r" + R.string.shortcut_text1, si.getTextResName());
6070                        assertEquals(R.string.shortcut_disabled_message1,
6071                                si.getDisabledMessageResourceId());
6072                        assertEquals("r" + R.string.shortcut_disabled_message1,
6073                                si.getDisabledMessageResName());
6074
6075                        assertEquals(set("android.shortcut.conversation", "android.shortcut.media"),
6076                                si.getCategories());
6077                        assertEquals("action1", si.getIntent().getAction());
6078                        assertEquals(Uri.parse("http://a.b.c/1"), si.getIntent().getData());
6079                    })
6080
6081                    .forShortcutWithId("ms2", si -> {
6082                        assertEquals("ms2", si.getId());
6083                        assertEquals(R.drawable.icon2, si.getIconResourceId());
6084
6085                        assertEquals(R.string.shortcut_title2, si.getTitleResId());
6086                        assertEquals("r" + R.string.shortcut_title2, si.getTitleResName());
6087                        assertEquals(R.string.shortcut_text2, si.getTextResId());
6088                        assertEquals("r" + R.string.shortcut_text2, si.getTextResName());
6089                        assertEquals(R.string.shortcut_disabled_message2,
6090                                si.getDisabledMessageResourceId());
6091                        assertEquals("r" + R.string.shortcut_disabled_message2,
6092                                si.getDisabledMessageResName());
6093
6094                        assertEquals(set("android.shortcut.conversation"), si.getCategories());
6095                        assertEquals("action2", si.getIntent().getAction());
6096                        assertEquals(null, si.getIntent().getData());
6097                    })
6098
6099                    .forShortcutWithId("ms3", si -> {
6100                        assertEquals(0, si.getIconResourceId());
6101                        assertEquals(R.string.shortcut_title1, si.getTitleResId());
6102                        assertEquals("r" + R.string.shortcut_title1, si.getTitleResName());
6103
6104                        assertEquals(0, si.getTextResId());
6105                        assertEquals(null, si.getTextResName());
6106                        assertEquals(0, si.getDisabledMessageResourceId());
6107                        assertEquals(null, si.getDisabledMessageResName());
6108
6109                        assertEmpty(si.getCategories());
6110                        assertEquals("android.intent.action.VIEW", si.getIntent().getAction());
6111                        assertEquals(null, si.getIntent().getData());
6112                    })
6113
6114                    .forShortcutWithId("ms4", si -> {
6115                        assertEquals(0, si.getIconResourceId());
6116                        assertEquals(R.string.shortcut_title2, si.getTitleResId());
6117                        assertEquals("r" + R.string.shortcut_title2, si.getTitleResName());
6118
6119                        assertEquals(0, si.getTextResId());
6120                        assertEquals(null, si.getTextResName());
6121                        assertEquals(0, si.getDisabledMessageResourceId());
6122                        assertEquals(null, si.getDisabledMessageResName());
6123
6124                        assertEquals(set("cat"), si.getCategories());
6125                        assertEquals("android.intent.action.VIEW2", si.getIntent().getAction());
6126                        assertEquals(null, si.getIntent().getData());
6127                    })
6128
6129                    .forShortcutWithId("ms5", si -> {
6130                        si = getCallerShortcut("ms5");
6131                        assertEquals("action", si.getIntent().getAction());
6132                        assertEquals("http://www/", si.getIntent().getData().toString());
6133                        assertEquals("foo/bar", si.getIntent().getType());
6134                        assertEquals(
6135                                new ComponentName("abc", ".xyz"), si.getIntent().getComponent());
6136
6137                        assertEquals(set("cat1", "cat2"), si.getIntent().getCategories());
6138                        assertEquals("value1", si.getIntent().getStringExtra("key1"));
6139                        assertEquals("value2", si.getIntent().getStringExtra("key2"));
6140                    });
6141        });
6142    }
6143
6144    public void testManifestShortcuts_localeChange() throws InterruptedException {
6145        mService.handleUnlockUser(USER_0);
6146
6147        // Package 1 updated, which has one valid manifest shortcut and one invalid.
6148        addManifestShortcutResource(
6149                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6150                R.xml.shortcut_2);
6151        updatePackageVersion(CALLING_PACKAGE_1, 1);
6152                mService.mPackageMonitor.onReceive(getTestContext(),
6153                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6154
6155        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6156            mManager.setDynamicShortcuts(list(makeShortcutWithTitle("s1", "title")));
6157
6158            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6159                    mManager.getManifestShortcuts()))),
6160                    "ms1", "ms2");
6161
6162            // check first shortcut.
6163            ShortcutInfo si = getCallerShortcut("ms1");
6164
6165            assertEquals("ms1", si.getId());
6166            assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_title1 + "/en",
6167                    si.getTitle());
6168            assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_text1 + "/en",
6169                    si.getText());
6170            assertEquals("string-com.android.test.1-user:0-res:"
6171                            + R.string.shortcut_disabled_message1 + "/en",
6172                    si.getDisabledMessage());
6173            assertEquals(START_TIME, si.getLastChangedTimestamp());
6174
6175            // check another
6176            si = getCallerShortcut("ms2");
6177
6178            assertEquals("ms2", si.getId());
6179            assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_title2 + "/en",
6180                    si.getTitle());
6181            assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_text2 + "/en",
6182                    si.getText());
6183            assertEquals("string-com.android.test.1-user:0-res:"
6184                            + R.string.shortcut_disabled_message2 + "/en",
6185                    si.getDisabledMessage());
6186            assertEquals(START_TIME, si.getLastChangedTimestamp());
6187
6188            // Check the dynamic one.
6189            si = getCallerShortcut("s1");
6190
6191            assertEquals("s1", si.getId());
6192            assertEquals("title", si.getTitle());
6193            assertEquals(null, si.getText());
6194            assertEquals(null, si.getDisabledMessage());
6195            assertEquals(START_TIME, si.getLastChangedTimestamp());
6196        });
6197
6198        mInjectedCurrentTimeMillis++;
6199
6200        // Change the locale and send the broadcast, make sure the launcher gets a callback too.
6201        mInjectedLocale = Locale.JAPANESE;
6202
6203        setCaller(LAUNCHER_1, USER_0);
6204
6205        assertForLauncherCallback(mLauncherApps, () -> {
6206            mService.mReceiver.onReceive(mServiceContext, new Intent(Intent.ACTION_LOCALE_CHANGED));
6207        }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
6208                .haveIds("ms1", "ms2", "s1");
6209
6210        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6211            // check first shortcut.
6212            ShortcutInfo si = getCallerShortcut("ms1");
6213
6214            assertEquals("ms1", si.getId());
6215            assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_title1 + "/ja",
6216                    si.getTitle());
6217            assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_text1 + "/ja",
6218                    si.getText());
6219            assertEquals("string-com.android.test.1-user:0-res:"
6220                            + R.string.shortcut_disabled_message1 + "/ja",
6221                    si.getDisabledMessage());
6222            assertEquals(START_TIME + 1, si.getLastChangedTimestamp());
6223
6224            // check another
6225            si = getCallerShortcut("ms2");
6226
6227            assertEquals("ms2", si.getId());
6228            assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_title2 + "/ja",
6229                    si.getTitle());
6230            assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_text2 + "/ja",
6231                    si.getText());
6232            assertEquals("string-com.android.test.1-user:0-res:"
6233                            + R.string.shortcut_disabled_message2 + "/ja",
6234                    si.getDisabledMessage());
6235            assertEquals(START_TIME + 1, si.getLastChangedTimestamp());
6236
6237            // Check the dynamic one.  (locale change shouldn't affect.)
6238            si = getCallerShortcut("s1");
6239
6240            assertEquals("s1", si.getId());
6241            assertEquals("title", si.getTitle());
6242            assertEquals(null, si.getText());
6243            assertEquals(null, si.getDisabledMessage());
6244            assertEquals(START_TIME, si.getLastChangedTimestamp()); // Not changed.
6245        });
6246    }
6247
6248    public void testManifestShortcuts_updateAndDisabled_notPinned() {
6249        mService.handleUnlockUser(USER_0);
6250
6251        // First, just publish a manifest shortcut.
6252        addManifestShortcutResource(
6253                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6254                R.xml.shortcut_1);
6255        updatePackageVersion(CALLING_PACKAGE_1, 1);
6256                mService.mPackageMonitor.onReceive(getTestContext(),
6257                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6258
6259        // Only the valid one is published.
6260        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6261            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6262                    mManager.getManifestShortcuts()))),
6263                    "ms1");
6264            assertEmpty(mManager.getPinnedShortcuts());
6265
6266            // Make sure there's no other dangling shortcuts.
6267            assertShortcutIds(getCallerShortcuts(), "ms1");
6268        });
6269
6270        // Now version up, the manifest shortcut is disabled now.
6271        addManifestShortcutResource(
6272                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6273                R.xml.shortcut_1_disable);
6274        updatePackageVersion(CALLING_PACKAGE_1, 1);
6275                mService.mPackageMonitor.onReceive(getTestContext(),
6276                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6277
6278        // Because shortcut 1 wasn't pinned, it'll just go away.
6279        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6280            assertEmpty(mManager.getManifestShortcuts());
6281            assertEmpty(mManager.getPinnedShortcuts());
6282
6283            // Make sure there's no other dangling shortcuts.
6284            assertEmpty(getCallerShortcuts());
6285        });
6286    }
6287
6288    public void testManifestShortcuts_updateAndDisabled_pinned() {
6289        mService.handleUnlockUser(USER_0);
6290
6291        // First, just publish a manifest shortcut.
6292        addManifestShortcutResource(
6293                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6294                R.xml.shortcut_1);
6295        updatePackageVersion(CALLING_PACKAGE_1, 1);
6296                mService.mPackageMonitor.onReceive(getTestContext(),
6297                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6298
6299        // Only the valid one is published.
6300        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6301            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6302                    mManager.getManifestShortcuts()))),
6303                    "ms1");
6304            assertEmpty(mManager.getPinnedShortcuts());
6305
6306            // Make sure there's no other dangling shortcuts.
6307            assertShortcutIds(getCallerShortcuts(), "ms1");
6308        });
6309
6310        runWithCaller(LAUNCHER_1, USER_0, () -> {
6311            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms1"), HANDLE_USER_0);
6312        });
6313
6314        // Now upgrade, the manifest shortcut is disabled now.
6315        addManifestShortcutResource(
6316                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6317                R.xml.shortcut_1_disable);
6318        updatePackageVersion(CALLING_PACKAGE_1, 1);
6319                mService.mPackageMonitor.onReceive(getTestContext(),
6320                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6321
6322        // Because shortcut 1 was pinned, it'll still exist as pinned, but disabled.
6323        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6324            assertEmpty(mManager.getManifestShortcuts());
6325            assertShortcutIds(assertAllNotManifest(assertAllImmutable(assertAllDisabled(
6326                    mManager.getPinnedShortcuts()))),
6327                    "ms1");
6328
6329            // Make sure the fields are updated.
6330            ShortcutInfo si = getCallerShortcut("ms1");
6331
6332            assertEquals("ms1", si.getId());
6333            assertEquals(R.drawable.icon2, si.getIconResourceId());
6334            assertEquals(R.string.shortcut_title2, si.getTitleResId());
6335            assertEquals(R.string.shortcut_text2, si.getTextResId());
6336            assertEquals(R.string.shortcut_disabled_message2, si.getDisabledMessageResourceId());
6337            assertEquals(Intent.ACTION_VIEW, si.getIntent().getAction());
6338
6339            // Make sure there's no other dangling shortcuts.
6340            assertShortcutIds(getCallerShortcuts(), "ms1");
6341        });
6342    }
6343
6344    public void testManifestShortcuts_duplicateInSingleActivity() {
6345        mService.handleUnlockUser(USER_0);
6346
6347        // The XML has two shortcuts with the same ID.
6348        addManifestShortcutResource(
6349                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6350                R.xml.shortcut_2_duplicate);
6351        updatePackageVersion(CALLING_PACKAGE_1, 1);
6352                mService.mPackageMonitor.onReceive(getTestContext(),
6353                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6354
6355        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6356            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6357                    mManager.getManifestShortcuts()))),
6358                    "ms1");
6359
6360            // Make sure the first one has survived.  (the second one has a different title.)
6361            ShortcutInfo si = getCallerShortcut("ms1");
6362            assertEquals(R.string.shortcut_title1, si.getTitleResId());
6363
6364            // Make sure there's no other dangling shortcuts.
6365            assertShortcutIds(getCallerShortcuts(), "ms1");
6366        });
6367    }
6368
6369    public void testManifestShortcuts_duplicateInTwoActivities() {
6370        mService.handleUnlockUser(USER_0);
6371
6372        // ShortcutActivity has shortcut ms1
6373        addManifestShortcutResource(
6374                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6375                R.xml.shortcut_1);
6376
6377        // ShortcutActivity2 has two shortcuts, ms1 and ms2.
6378        addManifestShortcutResource(
6379                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity2.class.getName()),
6380                R.xml.shortcut_5);
6381        updatePackageVersion(CALLING_PACKAGE_1, 1);
6382                mService.mPackageMonitor.onReceive(getTestContext(),
6383                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6384
6385        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6386            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6387                    mManager.getManifestShortcuts()))),
6388                    "ms1", "ms2", "ms3", "ms4", "ms5");
6389
6390            // ms1 should belong to ShortcutActivity.
6391            ShortcutInfo si = getCallerShortcut("ms1");
6392            assertEquals(R.string.shortcut_title1, si.getTitleResId());
6393            assertEquals(new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6394                    si.getActivity());
6395            assertEquals(0, si.getRank());
6396
6397            // ms2 should belong to ShortcutActivity*2*.
6398            si = getCallerShortcut("ms2");
6399            assertEquals(R.string.shortcut_title2, si.getTitleResId());
6400            assertEquals(new ComponentName(CALLING_PACKAGE_1, ShortcutActivity2.class.getName()),
6401                    si.getActivity());
6402
6403            // Also check the ranks
6404            assertWith(getCallerShortcuts()).selectManifest()
6405                    .selectByActivity(
6406                            new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()))
6407                    .haveRanksInOrder("ms1");
6408            assertWith(getCallerShortcuts()).selectManifest()
6409                    .selectByActivity(
6410                            new ComponentName(CALLING_PACKAGE_1, ShortcutActivity2.class.getName()))
6411                    .haveRanksInOrder("ms2", "ms3", "ms4", "ms5");
6412
6413            // Make sure there's no other dangling shortcuts.
6414            assertShortcutIds(getCallerShortcuts(), "ms1", "ms2", "ms3", "ms4", "ms5");
6415        });
6416    }
6417
6418    /**
6419     * Manifest shortcuts cannot override shortcuts that were published via the APIs.
6420     */
6421    public void testManifestShortcuts_cannotOverrideNonManifest() {
6422        mService.handleUnlockUser(USER_0);
6423
6424        // Create a non-pinned dynamic shortcut and a non-dynamic pinned shortcut.
6425
6426        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6427            mManager.setDynamicShortcuts(list(
6428                    makeShortcut("ms1", "title1",
6429                            new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6430                    /* icon */ null, new Intent("action1"), /* rank */ 0),
6431                    makeShortcut("ms2", "title2",
6432                            new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6433                    /* icon */ null, new Intent("action1"), /* rank */ 0)));
6434        });
6435
6436        runWithCaller(LAUNCHER_1, USER_0, () -> {
6437            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms2"), HANDLE_USER_0);
6438        });
6439
6440        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6441            mManager.removeDynamicShortcuts(list("ms2"));
6442
6443            assertShortcutIds(mManager.getDynamicShortcuts(), "ms1");
6444            assertShortcutIds(mManager.getPinnedShortcuts(), "ms2");
6445            assertEmpty(mManager.getManifestShortcuts());
6446        });
6447
6448        // Then update the app with 5 manifest shortcuts.
6449        // Make sure "ms1" and "ms2" won't be replaced.
6450        addManifestShortcutResource(
6451                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6452                R.xml.shortcut_5);
6453        updatePackageVersion(CALLING_PACKAGE_1, 1);
6454                mService.mPackageMonitor.onReceive(getTestContext(),
6455                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6456
6457        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6458            assertShortcutIds(assertAllNotManifest(mManager.getDynamicShortcuts()), "ms1");
6459            assertShortcutIds(assertAllNotManifest(mManager.getPinnedShortcuts()), "ms2");
6460            assertShortcutIds(assertAllManifest(mManager.getManifestShortcuts()),
6461                    "ms3", "ms4", "ms5");
6462
6463            // ms1 and ms2 shouold keep the original title.
6464            ShortcutInfo si = getCallerShortcut("ms1");
6465            assertEquals("title1", si.getTitle());
6466
6467            si = getCallerShortcut("ms2");
6468            assertEquals("title2", si.getTitle());
6469        });
6470    }
6471
6472    protected void checkManifestShortcuts_immutable_verify() {
6473        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6474            assertShortcutIds(assertAllNotManifest(assertAllEnabled(
6475                    mManager.getDynamicShortcuts())),
6476                    "s1");
6477            assertShortcutIds(assertAllManifest(assertAllEnabled(
6478                    mManager.getManifestShortcuts())),
6479                    "ms1");
6480            assertShortcutIds(assertAllNotManifest(assertAllDisabled(
6481                    mManager.getPinnedShortcuts())),
6482                    "ms2");
6483
6484            assertEquals("t1", getCallerShortcut("s1").getTitle());
6485
6486            // Make sure there are no other shortcuts.
6487            assertShortcutIds(getCallerShortcuts(), "s1", "ms1", "ms2");
6488        });
6489    }
6490
6491    /**
6492     * Make sure the APIs won't work on manifest shortcuts.
6493     */
6494    public void testManifestShortcuts_immutable() {
6495        mService.handleUnlockUser(USER_0);
6496
6497        // Create a non-pinned manifest shortcut, a pinned shortcut that was originally
6498        // a manifest shortcut, as well as a dynamic shortcut.
6499
6500        addManifestShortcutResource(
6501                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6502                R.xml.shortcut_2);
6503        updatePackageVersion(CALLING_PACKAGE_1, 1);
6504                mService.mPackageMonitor.onReceive(getTestContext(),
6505                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6506
6507        runWithCaller(LAUNCHER_1, USER_0, () -> {
6508            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms2"), HANDLE_USER_0);
6509        });
6510
6511        addManifestShortcutResource(
6512                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6513                R.xml.shortcut_1);
6514        updatePackageVersion(CALLING_PACKAGE_1, 1);
6515                mService.mPackageMonitor.onReceive(getTestContext(),
6516                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6517
6518        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6519            mManager.addDynamicShortcuts(list(makeShortcutWithTitle("s1", "t1")));
6520        });
6521
6522        checkManifestShortcuts_immutable_verify();
6523
6524        // Note that even though the first argument is not immutable and only the second one
6525        // is immutable, the first argument should not be executed either.
6526
6527        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6528            assertCannotUpdateImmutable(() -> {
6529                mManager.setDynamicShortcuts(list(makeShortcut("xx"), makeShortcut("ms1")));
6530            });
6531            assertCannotUpdateImmutable(() -> {
6532                mManager.setDynamicShortcuts(list(makeShortcut("xx"), makeShortcut("ms2")));
6533            });
6534        });
6535        checkManifestShortcuts_immutable_verify();
6536
6537        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6538            assertCannotUpdateImmutable(() -> {
6539                mManager.addDynamicShortcuts(list(makeShortcut("xx"), makeShortcut("ms1")));
6540            });
6541            assertCannotUpdateImmutable(() -> {
6542                mManager.addDynamicShortcuts(list(makeShortcut("xx"), makeShortcut("ms2")));
6543            });
6544        });
6545        checkManifestShortcuts_immutable_verify();
6546
6547
6548        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6549            assertCannotUpdateImmutable(() -> {
6550                mManager.updateShortcuts(list(makeShortcut("s1"), makeShortcut("ms1")));
6551            });
6552            assertCannotUpdateImmutable(() -> {
6553                mManager.updateShortcuts(list(makeShortcut("s1"), makeShortcut("ms2")));
6554            });
6555        });
6556        checkManifestShortcuts_immutable_verify();
6557
6558        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6559            assertCannotUpdateImmutable(() -> {
6560                mManager.removeDynamicShortcuts(list("s1", "ms1"));
6561            });
6562            assertCannotUpdateImmutable(() -> {
6563                mManager.removeDynamicShortcuts(list("s2", "ms2"));
6564            });
6565        });
6566        checkManifestShortcuts_immutable_verify();
6567
6568        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6569            assertCannotUpdateImmutable(() -> {
6570                mManager.disableShortcuts(list("s1", "ms1"));
6571            });
6572        });
6573        checkManifestShortcuts_immutable_verify();
6574
6575        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6576            assertCannotUpdateImmutable(() -> {
6577                mManager.enableShortcuts(list("s1", "ms2"));
6578            });
6579        });
6580        checkManifestShortcuts_immutable_verify();
6581    }
6582
6583
6584    /**
6585     * Make sure the APIs won't work on manifest shortcuts.
6586     */
6587    public void testManifestShortcuts_tooMany() {
6588        // Change the max number of shortcuts.
6589        mService.updateConfigurationLocked(ConfigConstants.KEY_MAX_SHORTCUTS + "=3");
6590
6591        mService.handleUnlockUser(USER_0);
6592
6593        addManifestShortcutResource(
6594                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6595                R.xml.shortcut_5);
6596        updatePackageVersion(CALLING_PACKAGE_1, 1);
6597                mService.mPackageMonitor.onReceive(getTestContext(),
6598                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6599
6600        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6601            // Only the first 3 should be published.
6602            assertShortcutIds(mManager.getManifestShortcuts(), "ms1", "ms2", "ms3");
6603        });
6604    }
6605
6606    public void testMaxShortcutCount_set() {
6607        // Change the max number of shortcuts.
6608        mService.updateConfigurationLocked(ConfigConstants.KEY_MAX_SHORTCUTS + "=3");
6609
6610        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6611            final ComponentName a1 = new ComponentName(mClientContext, ShortcutActivity.class);
6612            final ComponentName a2 = new ComponentName(mClientContext, ShortcutActivity2.class);
6613            final ShortcutInfo s1_1 = makeShortcutWithActivity("s11", a1);
6614            final ShortcutInfo s1_2 = makeShortcutWithActivity("s12", a1);
6615            final ShortcutInfo s1_3 = makeShortcutWithActivity("s13", a1);
6616            final ShortcutInfo s1_4 = makeShortcutWithActivity("s14", a1);
6617            final ShortcutInfo s1_5 = makeShortcutWithActivity("s15", a1);
6618            final ShortcutInfo s1_6 = makeShortcutWithActivity("s16", a1);
6619            final ShortcutInfo s2_1 = makeShortcutWithActivity("s21", a2);
6620            final ShortcutInfo s2_2 = makeShortcutWithActivity("s22", a2);
6621            final ShortcutInfo s2_3 = makeShortcutWithActivity("s23", a2);
6622            final ShortcutInfo s2_4 = makeShortcutWithActivity("s24", a2);
6623
6624            // 3 shortcuts for 2 activities -> okay
6625            mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3, s2_1, s2_2, s2_3));
6626            assertShortcutIds(mManager.getDynamicShortcuts(),
6627                    "s11", "s12", "s13", "s21", "s22", "s23");
6628
6629            mManager.removeAllDynamicShortcuts();
6630
6631            // 4 shortcut for activity 1 -> too many.
6632            assertDynamicShortcutCountExceeded(() -> {
6633                mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3, s1_4, s2_1, s2_2, s2_3));
6634            });
6635            assertEmpty(mManager.getDynamicShortcuts());
6636
6637            // 4 shortcut for activity 2 -> too many.
6638            assertDynamicShortcutCountExceeded(() -> {
6639                mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3, s2_1, s2_2, s2_3, s2_4));
6640            });
6641            assertEmpty(mManager.getDynamicShortcuts());
6642
6643            // First, set 3.  Then set 4, which should be ignored.
6644            mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3));
6645            assertShortcutIds(mManager.getDynamicShortcuts(),
6646                    "s11", "s12", "s13");
6647            assertDynamicShortcutCountExceeded(() -> {
6648                mManager.setDynamicShortcuts(list(s2_1, s2_2, s2_3, s2_4));
6649            });
6650            assertShortcutIds(mManager.getDynamicShortcuts(),
6651                    "s11", "s12", "s13");
6652
6653            // Set will remove the old dynamic set, unlike add, so the following should pass.
6654            mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3));
6655            assertShortcutIds(mManager.getDynamicShortcuts(),
6656                    "s11", "s12", "s13");
6657            mManager.setDynamicShortcuts(list(s1_4, s1_5, s1_6));
6658            assertShortcutIds(mManager.getDynamicShortcuts(),
6659                    "s14", "s15", "s16");
6660
6661            // Now, test with 2 manifest shortcuts.
6662            mManager.removeAllDynamicShortcuts();
6663            addManifestShortcutResource(
6664                    new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6665                    R.xml.shortcut_2);
6666            updatePackageVersion(CALLING_PACKAGE_1, 1);
6667                    mService.mPackageMonitor.onReceive(getTestContext(),
6668                    genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6669            assertEquals(2, mManager.getManifestShortcuts().size());
6670
6671            // Setting 1 to activity 1 will work.
6672            mManager.setDynamicShortcuts(list(s1_1, s2_1, s2_2, s2_3));
6673            assertShortcutIds(mManager.getDynamicShortcuts(),
6674                    "s11", "s21", "s22", "s23");
6675            assertEquals(2, mManager.getManifestShortcuts().size());
6676
6677            // But setting 2 will not.
6678            mManager.removeAllDynamicShortcuts();
6679            assertDynamicShortcutCountExceeded(() -> {
6680                mManager.setDynamicShortcuts(list(s1_1, s1_2, s2_1, s2_2, s2_3));
6681            });
6682            assertEmpty(mManager.getDynamicShortcuts());
6683            assertEquals(2, mManager.getManifestShortcuts().size());
6684        });
6685    }
6686
6687    public void testMaxShortcutCount_add() {
6688        // Change the max number of shortcuts.
6689        mService.updateConfigurationLocked(ConfigConstants.KEY_MAX_SHORTCUTS + "=3");
6690
6691        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6692            final ComponentName a1 = new ComponentName(mClientContext, ShortcutActivity.class);
6693            final ComponentName a2 = new ComponentName(mClientContext, ShortcutActivity2.class);
6694            final ShortcutInfo s1_1 = makeShortcutWithActivity("s11", a1);
6695            final ShortcutInfo s1_2 = makeShortcutWithActivity("s12", a1);
6696            final ShortcutInfo s1_3 = makeShortcutWithActivity("s13", a1);
6697            final ShortcutInfo s1_4 = makeShortcutWithActivity("s14", a1);
6698            final ShortcutInfo s2_1 = makeShortcutWithActivity("s21", a2);
6699            final ShortcutInfo s2_2 = makeShortcutWithActivity("s22", a2);
6700            final ShortcutInfo s2_3 = makeShortcutWithActivity("s23", a2);
6701            final ShortcutInfo s2_4 = makeShortcutWithActivity("s24", a2);
6702
6703            // 3 shortcuts for 2 activities -> okay
6704            mManager.addDynamicShortcuts(list(s1_1, s1_2, s1_3, s2_1, s2_2, s2_3));
6705            assertShortcutIds(mManager.getDynamicShortcuts(),
6706                    "s11", "s12", "s13", "s21", "s22", "s23");
6707
6708            mManager.removeAllDynamicShortcuts();
6709
6710            // 4 shortcut for activity 1 -> too many.
6711            assertDynamicShortcutCountExceeded(() -> {
6712                mManager.addDynamicShortcuts(list(s1_1, s1_2, s1_3, s1_4, s2_1, s2_2, s2_3));
6713            });
6714            assertEmpty(mManager.getDynamicShortcuts());
6715
6716            // 4 shortcut for activity 2 -> too many.
6717            assertDynamicShortcutCountExceeded(() -> {
6718                mManager.addDynamicShortcuts(list(s1_1, s1_2, s1_3, s2_1, s2_2, s2_3, s2_4));
6719            });
6720            assertEmpty(mManager.getDynamicShortcuts());
6721
6722            // First, set 3.  Then add 1 more, which should be ignored.
6723            mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3));
6724            assertShortcutIds(mManager.getDynamicShortcuts(),
6725                    "s11", "s12", "s13");
6726            assertDynamicShortcutCountExceeded(() -> {
6727                mManager.addDynamicShortcuts(list(s1_4, s2_1));
6728            });
6729            assertShortcutIds(mManager.getDynamicShortcuts(),
6730                    "s11", "s12", "s13");
6731
6732            // Update existing one, which should work.
6733            mManager.addDynamicShortcuts(list(makeShortcutWithActivityAndTitle(
6734                    "s11", a1, "xxx"), s2_1));
6735            assertShortcutIds(mManager.getDynamicShortcuts(),
6736                    "s11", "s12", "s13", "s21");
6737            assertEquals("xxx", getCallerShortcut("s11").getTitle());
6738
6739            // Make sure pinned shortcuts won't affect.
6740            // - Pin s11 - s13, and remove all dynamic.
6741            runWithCaller(LAUNCHER_1, USER_0, () -> {
6742                mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s11", "s12", "s13"),
6743                        HANDLE_USER_0);
6744            });
6745            mManager.removeAllDynamicShortcuts();
6746
6747            assertEmpty(mManager.getDynamicShortcuts());
6748            assertShortcutIds(mManager.getPinnedShortcuts(),
6749                    "s11", "s12", "s13");
6750
6751            // Then add dynamic.
6752            mManager.addDynamicShortcuts(list(s1_4, s2_1, s2_2, s2_3));
6753
6754            assertShortcutIds(mManager.getDynamicShortcuts(),
6755                    "s14", "s21", "s22", "s23");
6756            assertShortcutIds(mManager.getPinnedShortcuts(),
6757                    "s11", "s12", "s13");
6758
6759            // Adding "s11" and "s12" back, should work
6760            mManager.addDynamicShortcuts(list(s1_1, s1_2));
6761
6762            assertShortcutIds(mManager.getDynamicShortcuts(),
6763                    "s14", "s11", "s12", "s21", "s22", "s23");
6764            assertShortcutIds(mManager.getPinnedShortcuts(),
6765                    "s11", "s12", "s13");
6766
6767            // Adding back s13 doesn't work.
6768            assertDynamicShortcutCountExceeded(() -> {
6769                mManager.addDynamicShortcuts(list(s1_3));
6770            });
6771
6772            assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a1),
6773                    "s11", "s12", "s14");
6774            assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a2),
6775                    "s21", "s22", "s23");
6776
6777            // Now swap the activities.
6778            mManager.updateShortcuts(list(
6779                    makeShortcutWithActivity("s11", a2),
6780                    makeShortcutWithActivity("s21", a1)));
6781
6782            assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a1),
6783                    "s21", "s12", "s14");
6784            assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a2),
6785                    "s11", "s22", "s23");
6786
6787            // Now, test with 2 manifest shortcuts.
6788            mManager.removeAllDynamicShortcuts();
6789            addManifestShortcutResource(
6790                    new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6791                    R.xml.shortcut_2);
6792            updatePackageVersion(CALLING_PACKAGE_1, 1);
6793                    mService.mPackageMonitor.onReceive(getTestContext(),
6794                    genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6795
6796            assertEquals(2, mManager.getManifestShortcuts().size());
6797
6798            // Adding one shortcut to activity 1 works fine.
6799            mManager.addDynamicShortcuts(list(s1_1, s2_1, s2_2, s2_3));
6800            assertShortcutIds(mManager.getDynamicShortcuts(),
6801                    "s11", "s21", "s22", "s23");
6802            assertEquals(2, mManager.getManifestShortcuts().size());
6803
6804            // But adding one more doesn't.
6805            assertDynamicShortcutCountExceeded(() -> {
6806                mManager.addDynamicShortcuts(list(s1_4, s2_1));
6807            });
6808            assertShortcutIds(mManager.getDynamicShortcuts(),
6809                    "s11", "s21", "s22", "s23");
6810            assertEquals(2, mManager.getManifestShortcuts().size());
6811        });
6812    }
6813
6814    public void testMaxShortcutCount_update() {
6815        // Change the max number of shortcuts.
6816        mService.updateConfigurationLocked(ConfigConstants.KEY_MAX_SHORTCUTS + "=3");
6817
6818        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6819            final ComponentName a1 = new ComponentName(mClientContext, ShortcutActivity.class);
6820            final ComponentName a2 = new ComponentName(mClientContext, ShortcutActivity2.class);
6821            final ShortcutInfo s1_1 = makeShortcutWithActivity("s11", a1);
6822            final ShortcutInfo s1_2 = makeShortcutWithActivity("s12", a1);
6823            final ShortcutInfo s1_3 = makeShortcutWithActivity("s13", a1);
6824            final ShortcutInfo s1_4 = makeShortcutWithActivity("s14", a1);
6825            final ShortcutInfo s1_5 = makeShortcutWithActivity("s15", a1);
6826            final ShortcutInfo s2_1 = makeShortcutWithActivity("s21", a2);
6827            final ShortcutInfo s2_2 = makeShortcutWithActivity("s22", a2);
6828            final ShortcutInfo s2_3 = makeShortcutWithActivity("s23", a2);
6829            final ShortcutInfo s2_4 = makeShortcutWithActivity("s24", a2);
6830
6831            // 3 shortcuts for 2 activities -> okay
6832            mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3, s2_1, s2_2, s2_3));
6833            assertShortcutIds(mManager.getDynamicShortcuts(),
6834                    "s11", "s12", "s13", "s21", "s22", "s23");
6835
6836            // Trying to move s11 from a1 to a2 should fail.
6837            assertDynamicShortcutCountExceeded(() -> {
6838                mManager.updateShortcuts(list(makeShortcutWithActivity("s11", a2)));
6839            });
6840            assertShortcutIds(mManager.getDynamicShortcuts(),
6841                    "s11", "s12", "s13", "s21", "s22", "s23");
6842
6843            // Trying to move s21 from a2 to a1 should also fail.
6844            assertDynamicShortcutCountExceeded(() -> {
6845                mManager.updateShortcuts(list(makeShortcutWithActivity("s21", a1)));
6846            });
6847            assertShortcutIds(mManager.getDynamicShortcuts(),
6848                    "s11", "s12", "s13", "s21", "s22", "s23");
6849
6850            // But, if we do these two at the same time, it should work.
6851            mManager.updateShortcuts(list(
6852                    makeShortcutWithActivity("s11", a2),
6853                    makeShortcutWithActivity("s21", a1)));
6854            assertShortcutIds(mManager.getDynamicShortcuts(),
6855                    "s11", "s12", "s13", "s21", "s22", "s23");
6856            assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a1),
6857                    "s21", "s12", "s13");
6858            assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a2),
6859                    "s11", "s22", "s23");
6860
6861            // Then reset.
6862            mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3, s2_1, s2_2, s2_3));
6863            assertShortcutIds(mManager.getDynamicShortcuts(),
6864                    "s11", "s12", "s13", "s21", "s22", "s23");
6865
6866            // Pin some to have more shortcuts for a1.
6867            runWithCaller(LAUNCHER_1, USER_0, () -> {
6868                mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s11", "s12", "s13"),
6869                        HANDLE_USER_0);
6870            });
6871            mManager.setDynamicShortcuts(list(s1_4, s1_5, s2_1, s2_2, s2_3));
6872            assertShortcutIds(mManager.getDynamicShortcuts(),
6873                    "s14", "s15", "s21", "s22", "s23");
6874            assertShortcutIds(mManager.getPinnedShortcuts(),
6875                    "s11", "s12", "s13");
6876
6877            // a1 already has 2 dynamic shortcuts (and 3 pinned shortcuts that used to belong on it)
6878            // But that doesn't matter for update -- the following should still work.
6879            mManager.updateShortcuts(list(
6880                    makeShortcutWithActivityAndTitle("s11", a1, "xxx1"),
6881                    makeShortcutWithActivityAndTitle("s12", a1, "xxx2"),
6882                    makeShortcutWithActivityAndTitle("s13", a1, "xxx3"),
6883                    makeShortcutWithActivityAndTitle("s14", a1, "xxx4"),
6884                    makeShortcutWithActivityAndTitle("s15", a1, "xxx5")));
6885            // All the shortcuts should still exist they all belong on same activities,
6886            // with the updated titles.
6887            assertShortcutIds(mManager.getDynamicShortcuts(),
6888                    "s14", "s15", "s21", "s22", "s23");
6889            assertShortcutIds(mManager.getPinnedShortcuts(),
6890                    "s11", "s12", "s13");
6891
6892            assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a1),
6893                    "s14", "s15");
6894            assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a2),
6895                    "s21", "s22", "s23");
6896
6897            assertEquals("xxx1", getCallerShortcut("s11").getTitle());
6898            assertEquals("xxx2", getCallerShortcut("s12").getTitle());
6899            assertEquals("xxx3", getCallerShortcut("s13").getTitle());
6900            assertEquals("xxx4", getCallerShortcut("s14").getTitle());
6901            assertEquals("xxx5", getCallerShortcut("s15").getTitle());
6902        });
6903    }
6904
6905    public void testShortcutsPushedOutByManifest() {
6906        // Change the max number of shortcuts.
6907        mService.updateConfigurationLocked(ConfigConstants.KEY_MAX_SHORTCUTS + "=3");
6908
6909        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6910            final ComponentName a1 = new ComponentName(mClientContext, ShortcutActivity.class);
6911            final ComponentName a2 = new ComponentName(mClientContext, ShortcutActivity2.class);
6912            final ShortcutInfo s1_1 = makeShortcutWithActivityAndRank("s11", a1, 4);
6913            final ShortcutInfo s1_2 = makeShortcutWithActivityAndRank("s12", a1, 3);
6914            final ShortcutInfo s1_3 = makeShortcutWithActivityAndRank("s13", a1, 2);
6915            final ShortcutInfo s1_4 = makeShortcutWithActivityAndRank("s14", a1, 1);
6916            final ShortcutInfo s1_5 = makeShortcutWithActivityAndRank("s15", a1, 0);
6917            final ShortcutInfo s2_1 = makeShortcutWithActivityAndRank("s21", a2, 0);
6918            final ShortcutInfo s2_2 = makeShortcutWithActivityAndRank("s22", a2, 1);
6919            final ShortcutInfo s2_3 = makeShortcutWithActivityAndRank("s23", a2, 2);
6920            final ShortcutInfo s2_4 = makeShortcutWithActivityAndRank("s24", a2, 3);
6921            final ShortcutInfo s2_5 = makeShortcutWithActivityAndRank("s25", a2, 4);
6922
6923            // Initial state.
6924            mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3, s2_1, s2_2, s2_3));
6925            runWithCaller(LAUNCHER_1, USER_0, () -> {
6926                mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s11", "s12", "s21", "s22"),
6927                        HANDLE_USER_0);
6928            });
6929            mManager.setDynamicShortcuts(list(s1_2, s1_3, s1_4, s2_2, s2_3, s2_4));
6930            assertShortcutIds(assertAllEnabled(mManager.getDynamicShortcuts()),
6931                    "s12", "s13", "s14",
6932                    "s22", "s23", "s24");
6933            assertShortcutIds(assertAllEnabled(mManager.getPinnedShortcuts()),
6934                    "s11", "s12",
6935                    "s21", "s22");
6936
6937            // Add 1 manifest shortcut to a1.
6938            addManifestShortcutResource(
6939                    new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6940                    R.xml.shortcut_1);
6941            updatePackageVersion(CALLING_PACKAGE_1, 1);
6942                    mService.mPackageMonitor.onReceive(getTestContext(),
6943                    genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6944            assertEquals(1, mManager.getManifestShortcuts().size());
6945
6946            // s12 removed.
6947            assertShortcutIds(assertAllEnabled(mManager.getDynamicShortcuts()),
6948                    "s13", "s14",
6949                    "s22", "s23", "s24");
6950            assertShortcutIds(assertAllEnabled(mManager.getPinnedShortcuts()),
6951                    "s11", "s12",
6952                    "s21", "s22");
6953
6954            // Add more manifest shortcuts.
6955            addManifestShortcutResource(
6956                    new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6957                    R.xml.shortcut_2);
6958            addManifestShortcutResource(
6959                    new ComponentName(CALLING_PACKAGE_1, ShortcutActivity2.class.getName()),
6960                    R.xml.shortcut_1_alt);
6961            updatePackageVersion(CALLING_PACKAGE_1, 1);
6962                    mService.mPackageMonitor.onReceive(getTestContext(),
6963                    genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6964            assertEquals(3, mManager.getManifestShortcuts().size());
6965
6966            // Note the ones with the highest rank values (== least important) will be removed.
6967            assertShortcutIds(assertAllEnabled(mManager.getDynamicShortcuts()),
6968                    "s14",
6969                    "s22", "s23");
6970            assertShortcutIds(assertAllEnabled(mManager.getPinnedShortcuts()),
6971                    "s11", "s12",
6972                    "s21", "s22");
6973
6974            // Add more manifest shortcuts.
6975            addManifestShortcutResource(
6976                    new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6977                    R.xml.shortcut_2);
6978            addManifestShortcutResource(
6979                    new ComponentName(CALLING_PACKAGE_1, ShortcutActivity2.class.getName()),
6980                    R.xml.shortcut_5_alt); // manifest has 5, but max is 3, so a2 will have 3.
6981            updatePackageVersion(CALLING_PACKAGE_1, 1);
6982                    mService.mPackageMonitor.onReceive(getTestContext(),
6983                    genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6984            assertEquals(5, mManager.getManifestShortcuts().size());
6985
6986            assertShortcutIds(assertAllEnabled(mManager.getDynamicShortcuts()),
6987                    "s14" // a1 has 1 dynamic
6988            ); // a2 has no dynamic
6989            assertShortcutIds(assertAllEnabled(mManager.getPinnedShortcuts()),
6990                    "s11", "s12",
6991                    "s21", "s22");
6992
6993            // Update, no manifest shortucts.  This doesn't affect anything.
6994            addManifestShortcutResource(
6995                    new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6996                    R.xml.shortcut_0);
6997            addManifestShortcutResource(
6998                    new ComponentName(CALLING_PACKAGE_1, ShortcutActivity2.class.getName()),
6999                    R.xml.shortcut_0);
7000            updatePackageVersion(CALLING_PACKAGE_1, 1);
7001                    mService.mPackageMonitor.onReceive(getTestContext(),
7002                    genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7003            assertEquals(0, mManager.getManifestShortcuts().size());
7004
7005            assertShortcutIds(assertAllEnabled(mManager.getDynamicShortcuts()),
7006                    "s14");
7007            assertShortcutIds(assertAllEnabled(mManager.getPinnedShortcuts()),
7008                    "s11", "s12",
7009                    "s21", "s22");
7010        });
7011    }
7012}
7013