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