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