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