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