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