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