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