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