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