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