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