2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.android.server.pm;
18 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllDisabled;
19 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllDynamic;
20 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllDynamicOrPinned;
21 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllEnabled;
22 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllHaveIntents;
23 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllHaveTitle;
24 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllImmutable;
25 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllKeyFieldsOnly;
26 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllManifest;
27 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllNotHaveIntents;
28 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllNotHaveTitle;
29 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllNotKeyFieldsOnly;
30 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllNotManifest;
31 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllPinned;
32 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllUnique;
33 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertBitmapSize;
34 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertBundleEmpty;
35 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertCallbackNotReceived;
36 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertCallbackReceived;
37 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertCannotUpdateImmutable;
38 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertDynamicAndPinned;
39 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertDynamicOnly;
40 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertDynamicShortcutCountExceeded;
41 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertEmpty;
42 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertExpectException;
43 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertForLauncherCallback;
44 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertShortcutIds;
45 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertWith;
46 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.filterByActivity;
47 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.findShortcut;
48 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.hashSet;
49 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.list;
50 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.makeBundle;
51 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.pfdToBitmap;
52 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.resetAll;
53 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.set;
54 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.waitOnMainThread;
56 import static org.mockito.Matchers.any;
57 import static org.mockito.Matchers.anyInt;
58 import static org.mockito.Matchers.anyString;
59 import static org.mockito.Matchers.eq;
60 import static org.mockito.Mockito.doReturn;
61 import static org.mockito.Mockito.mock;
62 import static org.mockito.Mockito.reset;
63 import static org.mockito.Mockito.times;
64 import static org.mockito.Mockito.verify;
66 import android.Manifest.permission;
67 import android.app.ActivityManager;
68 import android.content.ActivityNotFoundException;
69 import android.content.ComponentName;
70 import android.content.Intent;
71 import android.content.pm.ApplicationInfo;
72 import android.content.pm.LauncherApps;
73 import android.content.pm.LauncherApps.ShortcutQuery;
74 import android.content.pm.ShortcutInfo;
75 import android.graphics.Bitmap;
76 import android.graphics.Bitmap.CompressFormat;
77 import android.graphics.BitmapFactory;
78 import android.graphics.drawable.Icon;
79 import android.net.Uri;
80 import android.os.Bundle;
81 import android.os.Handler;
82 import android.os.Looper;
83 import android.os.Process;
84 import android.os.UserHandle;
85 import android.test.suitebuilder.annotation.SmallTest;
86 import android.util.Log;
87 import android.util.SparseArray;
89 import com.android.frameworks.servicestests.R;
90 import com.android.server.pm.ShortcutService.ConfigConstants;
91 import com.android.server.pm.ShortcutService.FileOutputStreamWithPath;
92 import com.android.server.pm.ShortcutUser.PackageWithUser;
94 import org.mockito.ArgumentCaptor;
97 import java.io.IOException;
98 import java.util.List;
99 import java.util.Locale;
102 * Tests for ShortcutService and ShortcutManager.
104 m FrameworksServicesTests &&
106 -r -g ${ANDROID_PRODUCT_OUT}/data/app/FrameworksServicesTests/FrameworksServicesTests.apk &&
107 adb shell am instrument -e class com.android.server.pm.ShortcutManagerTest1 \
108 -w com.android.frameworks.servicestests/android.support.test.runner.AndroidJUnitRunner
111 public class ShortcutManagerTest1 extends BaseShortcutManagerTest {
114 * Test for the first launch path, no settings file available.
116 public void testFirstInitialize() {
117 assertResetTimes(START_TIME, START_TIME + INTERVAL);
121 * Test for {@link ShortcutService#getLastResetTimeLocked()} and
122 * {@link ShortcutService#getNextResetTimeLocked()}.
124 public void testUpdateAndGetNextResetTimeLocked() {
125 assertResetTimes(START_TIME, START_TIME + INTERVAL);
128 mInjectedCurrentTimeMillis += 100;
130 // Shouldn't have changed.
131 assertResetTimes(START_TIME, START_TIME + INTERVAL);
133 // Advance clock, almost the reset time.
134 mInjectedCurrentTimeMillis = START_TIME + INTERVAL - 1;
136 // Shouldn't have changed.
137 assertResetTimes(START_TIME, START_TIME + INTERVAL);
140 mInjectedCurrentTimeMillis += 1;
142 assertResetTimes(START_TIME + INTERVAL, START_TIME + 2 * INTERVAL);
144 // Advance further; 4 hours since start.
145 mInjectedCurrentTimeMillis = START_TIME + 4 * INTERVAL + 50;
147 assertResetTimes(START_TIME + 4 * INTERVAL, START_TIME + 5 * INTERVAL);
151 * Test for the restoration from saved file.
153 public void testInitializeFromSavedFile() {
155 mInjectedCurrentTimeMillis = START_TIME + 4 * INTERVAL + 50;
156 assertResetTimes(START_TIME + 4 * INTERVAL, START_TIME + 5 * INTERVAL);
158 mService.saveBaseStateLocked();
162 mService.saveDirtyInfo();
167 assertResetTimes(START_TIME + 4 * INTERVAL, START_TIME + 5 * INTERVAL);
171 * Test for the restoration from restored file.
173 public void testLoadFromBrokenFile() {
174 // TODO Add various broken cases.
177 public void testLoadConfig() {
178 mService.updateConfigurationLocked(
179 ConfigConstants.KEY_RESET_INTERVAL_SEC + "=123,"
180 + ConfigConstants.KEY_MAX_SHORTCUTS + "=4,"
181 + ConfigConstants.KEY_MAX_UPDATES_PER_INTERVAL + "=5,"
182 + ConfigConstants.KEY_MAX_ICON_DIMENSION_DP + "=100,"
183 + ConfigConstants.KEY_MAX_ICON_DIMENSION_DP_LOWRAM + "=50,"
184 + ConfigConstants.KEY_ICON_FORMAT + "=WEBP,"
185 + ConfigConstants.KEY_ICON_QUALITY + "=75");
186 assertEquals(123000, mService.getResetIntervalForTest());
187 assertEquals(4, mService.getMaxShortcutsForTest());
188 assertEquals(5, mService.getMaxUpdatesPerIntervalForTest());
189 assertEquals(100, mService.getMaxIconDimensionForTest());
190 assertEquals(CompressFormat.WEBP, mService.getIconPersistFormatForTest());
191 assertEquals(75, mService.getIconPersistQualityForTest());
193 mInjectedIsLowRamDevice = true;
194 mService.updateConfigurationLocked(
195 ConfigConstants.KEY_MAX_ICON_DIMENSION_DP + "=100,"
196 + ConfigConstants.KEY_MAX_ICON_DIMENSION_DP_LOWRAM + "=50,"
197 + ConfigConstants.KEY_ICON_FORMAT + "=JPEG");
198 assertEquals(ShortcutService.DEFAULT_RESET_INTERVAL_SEC * 1000,
199 mService.getResetIntervalForTest());
201 assertEquals(ShortcutService.DEFAULT_MAX_SHORTCUTS_PER_APP,
202 mService.getMaxShortcutsForTest());
204 assertEquals(ShortcutService.DEFAULT_MAX_UPDATES_PER_INTERVAL,
205 mService.getMaxUpdatesPerIntervalForTest());
207 assertEquals(50, mService.getMaxIconDimensionForTest());
209 assertEquals(CompressFormat.JPEG, mService.getIconPersistFormatForTest());
211 assertEquals(ShortcutService.DEFAULT_ICON_PERSIST_QUALITY,
212 mService.getIconPersistQualityForTest());
215 // === Test for app side APIs ===
217 /** Test for {@link android.content.pm.ShortcutManager#getMaxShortcutCountForActivity()} */
218 public void testGetMaxDynamicShortcutCount() {
219 assertEquals(MAX_SHORTCUTS, mManager.getMaxShortcutCountForActivity());
222 /** Test for {@link android.content.pm.ShortcutManager#getRemainingCallCount()} */
223 public void testGetRemainingCallCount() {
224 assertEquals(MAX_UPDATES_PER_INTERVAL, mManager.getRemainingCallCount());
227 public void testGetIconMaxDimensions() {
228 assertEquals(MAX_ICON_DIMENSION, mManager.getIconMaxWidth());
229 assertEquals(MAX_ICON_DIMENSION, mManager.getIconMaxHeight());
232 /** Test for {@link android.content.pm.ShortcutManager#getRateLimitResetTime()} */
233 public void testGetRateLimitResetTime() {
234 assertEquals(START_TIME + INTERVAL, mManager.getRateLimitResetTime());
236 mInjectedCurrentTimeMillis = START_TIME + 4 * INTERVAL + 50;
238 assertEquals(START_TIME + 5 * INTERVAL, mManager.getRateLimitResetTime());
241 public void testSetDynamicShortcuts() {
242 setCaller(CALLING_PACKAGE_1, USER_0);
244 final Icon icon1 = Icon.createWithResource(getTestContext(), R.drawable.icon1);
245 final Icon icon2 = Icon.createWithBitmap(BitmapFactory.decodeResource(
246 getTestContext().getResources(), R.drawable.icon2));
248 final ShortcutInfo si1 = makeShortcut(
251 makeComponent(ShortcutActivity.class),
253 makeIntent(Intent.ACTION_ASSIST, ShortcutActivity2.class,
254 "key1", "val1", "nest", makeBundle("key", 123)),
257 final ShortcutInfo si2 = makeShortcut(
262 makeIntent(Intent.ACTION_ASSIST, ShortcutActivity3.class),
264 final ShortcutInfo si3 = makeShortcut("shortcut3");
266 assertTrue(mManager.setDynamicShortcuts(list(si1, si2)));
267 assertShortcutIds(assertAllNotKeyFieldsOnly(
268 mManager.getDynamicShortcuts()),
269 "shortcut1", "shortcut2");
270 assertEquals(2, mManager.getRemainingCallCount());
272 // TODO: Check fields
274 assertTrue(mManager.setDynamicShortcuts(list(si1)));
275 assertShortcutIds(assertAllNotKeyFieldsOnly(
276 mManager.getDynamicShortcuts()),
278 assertEquals(1, mManager.getRemainingCallCount());
280 assertTrue(mManager.setDynamicShortcuts(list()));
281 assertEquals(0, mManager.getDynamicShortcuts().size());
282 assertEquals(0, mManager.getRemainingCallCount());
286 mInjectedCurrentTimeMillis++; // Need to advance the clock for reset to work.
287 mService.resetThrottlingInner(UserHandle.USER_SYSTEM);
291 assertTrue(mManager.setDynamicShortcuts(list(si2, si3)));
292 assertEquals(2, mManager.getDynamicShortcuts().size());
294 // TODO Check max number
296 mRunningUsers.put(USER_10, true);
298 runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
299 assertTrue(mManager.setDynamicShortcuts(list(makeShortcut("s1"))));
303 public void testAddDynamicShortcuts() {
304 setCaller(CALLING_PACKAGE_1, USER_0);
306 final ShortcutInfo si1 = makeShortcut("shortcut1");
307 final ShortcutInfo si2 = makeShortcut("shortcut2");
308 final ShortcutInfo si3 = makeShortcut("shortcut3");
310 assertEquals(3, mManager.getRemainingCallCount());
312 assertTrue(mManager.setDynamicShortcuts(list(si1)));
313 assertEquals(2, mManager.getRemainingCallCount());
314 assertShortcutIds(assertAllNotKeyFieldsOnly(
315 mManager.getDynamicShortcuts()),
318 assertTrue(mManager.addDynamicShortcuts(list(si2, si3)));
319 assertEquals(1, mManager.getRemainingCallCount());
320 assertShortcutIds(assertAllNotKeyFieldsOnly(
321 mManager.getDynamicShortcuts()),
322 "shortcut1", "shortcut2", "shortcut3");
324 // This should not crash. It'll still consume the quota.
325 assertTrue(mManager.addDynamicShortcuts(list()));
326 assertEquals(0, mManager.getRemainingCallCount());
327 assertShortcutIds(assertAllNotKeyFieldsOnly(
328 mManager.getDynamicShortcuts()),
329 "shortcut1", "shortcut2", "shortcut3");
331 mInjectedCurrentTimeMillis += INTERVAL; // reset
333 // Add with the same ID
334 assertTrue(mManager.addDynamicShortcuts(list(makeShortcut("shortcut1"))));
335 assertEquals(2, mManager.getRemainingCallCount());
336 assertShortcutIds(assertAllNotKeyFieldsOnly(
337 mManager.getDynamicShortcuts()),
338 "shortcut1", "shortcut2", "shortcut3");
340 // TODO Check max number
342 // TODO Check fields.
344 mRunningUsers.put(USER_10, true);
346 runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
347 assertTrue(mManager.addDynamicShortcuts(list(makeShortcut("s1"))));
351 public void testPublishWithNoActivity() {
352 // If activity is not explicitly set, use the default one.
354 mRunningUsers.put(USER_10, true);
356 runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
357 // s1 and s3 has no activities.
358 final ShortcutInfo si1 = new ShortcutInfo.Builder(mClientContext, "si1")
359 .setShortLabel("label1")
360 .setIntent(new Intent("action1"))
362 final ShortcutInfo si2 = new ShortcutInfo.Builder(mClientContext, "si2")
363 .setShortLabel("label2")
364 .setActivity(new ComponentName(getCallingPackage(), "abc"))
365 .setIntent(new Intent("action2"))
367 final ShortcutInfo si3 = new ShortcutInfo.Builder(mClientContext, "si3")
368 .setShortLabel("label3")
369 .setIntent(new Intent("action3"))
373 assertTrue(mManager.setDynamicShortcuts(list(si1)));
375 assertWith(getCallerShortcuts())
377 .forShortcutWithId("si1", si -> {
378 assertEquals(new ComponentName(getCallingPackage(),
379 MAIN_ACTIVITY_CLASS), si.getActivity());
383 assertTrue(mManager.setDynamicShortcuts(list(si2, si1)));
385 assertWith(getCallerShortcuts())
386 .haveIds("si1", "si2")
387 .forShortcutWithId("si1", si -> {
388 assertEquals(new ComponentName(getCallingPackage(),
389 MAIN_ACTIVITY_CLASS), si.getActivity());
391 .forShortcutWithId("si2", si -> {
392 assertEquals(new ComponentName(getCallingPackage(),
393 "abc"), si.getActivity());
398 assertTrue(mManager.setDynamicShortcuts(list(si3, si1)));
400 assertWith(getCallerShortcuts())
401 .haveIds("si1", "si3")
402 .forShortcutWithId("si1", si -> {
403 assertEquals(new ComponentName(getCallingPackage(),
404 MAIN_ACTIVITY_CLASS), si.getActivity());
406 .forShortcutWithId("si3", si -> {
407 assertEquals(new ComponentName(getCallingPackage(),
408 MAIN_ACTIVITY_CLASS), si.getActivity());
411 mInjectedCurrentTimeMillis += INTERVAL; // reset throttling
414 mManager.removeAllDynamicShortcuts();
415 assertTrue(mManager.addDynamicShortcuts(list(si1)));
417 assertWith(getCallerShortcuts())
419 .forShortcutWithId("si1", si -> {
420 assertEquals(new ComponentName(getCallingPackage(),
421 MAIN_ACTIVITY_CLASS), si.getActivity());
425 mManager.removeAllDynamicShortcuts();
426 assertTrue(mManager.addDynamicShortcuts(list(si2, si1)));
428 assertWith(getCallerShortcuts())
429 .haveIds("si1", "si2")
430 .forShortcutWithId("si1", si -> {
431 assertEquals(new ComponentName(getCallingPackage(),
432 MAIN_ACTIVITY_CLASS), si.getActivity());
434 .forShortcutWithId("si2", si -> {
435 assertEquals(new ComponentName(getCallingPackage(),
436 "abc"), si.getActivity());
441 mManager.removeAllDynamicShortcuts();
442 assertTrue(mManager.addDynamicShortcuts(list(si3, si1)));
444 assertWith(getCallerShortcuts())
445 .haveIds("si1", "si3")
446 .forShortcutWithId("si1", si -> {
447 assertEquals(new ComponentName(getCallingPackage(),
448 MAIN_ACTIVITY_CLASS), si.getActivity());
450 .forShortcutWithId("si3", si -> {
451 assertEquals(new ComponentName(getCallingPackage(),
452 MAIN_ACTIVITY_CLASS), si.getActivity());
457 public void testPublishWithNoActivity_noMainActivityInPackage() {
458 mRunningUsers.put(USER_10, true);
460 runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
461 final ShortcutInfo si1 = new ShortcutInfo.Builder(mClientContext, "si1")
462 .setShortLabel("label1")
463 .setIntent(new Intent("action1"))
466 // Returning null means there's no main activity in this package.
467 mMainActivityFetcher = (packageName, userId) -> null;
469 assertExpectException(
470 RuntimeException.class, "Launcher activity not found for", () -> {
471 assertTrue(mManager.setDynamicShortcuts(list(si1)));
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");
482 assertTrue(mManager.setDynamicShortcuts(list(si1, si2, si3, si4)));
483 assertShortcutIds(assertAllNotKeyFieldsOnly(
484 mManager.getDynamicShortcuts()),
485 "shortcut1", "shortcut2", "shortcut3", "shortcut4");
487 assertEquals(2, mManager.getRemainingCallCount());
489 mManager.removeDynamicShortcuts(list("shortcut1"));
490 assertShortcutIds(assertAllNotKeyFieldsOnly(
491 mManager.getDynamicShortcuts()),
492 "shortcut2", "shortcut3", "shortcut4");
494 mManager.removeDynamicShortcuts(list("shortcut1"));
495 assertShortcutIds(assertAllNotKeyFieldsOnly(
496 mManager.getDynamicShortcuts()),
497 "shortcut2", "shortcut3", "shortcut4");
499 mManager.removeDynamicShortcuts(list("shortcutXXX"));
500 assertShortcutIds(assertAllNotKeyFieldsOnly(
501 mManager.getDynamicShortcuts()),
502 "shortcut2", "shortcut3", "shortcut4");
504 mManager.removeDynamicShortcuts(list("shortcut2", "shortcut4"));
505 assertShortcutIds(assertAllNotKeyFieldsOnly(
506 mManager.getDynamicShortcuts()),
509 mManager.removeDynamicShortcuts(list("shortcut3"));
510 assertShortcutIds(assertAllNotKeyFieldsOnly(
511 mManager.getDynamicShortcuts()));
513 // Still 2 calls left.
514 assertEquals(2, mManager.getRemainingCallCount());
517 public void testDeleteAllDynamicShortcuts() {
518 final ShortcutInfo si1 = makeShortcut("shortcut1");
519 final ShortcutInfo si2 = makeShortcut("shortcut2");
520 final ShortcutInfo si3 = makeShortcut("shortcut3");
522 assertTrue(mManager.setDynamicShortcuts(list(si1, si2, si3)));
523 assertShortcutIds(assertAllNotKeyFieldsOnly(
524 mManager.getDynamicShortcuts()),
525 "shortcut1", "shortcut2", "shortcut3");
527 assertEquals(2, mManager.getRemainingCallCount());
529 mManager.removeAllDynamicShortcuts();
530 assertEquals(0, mManager.getDynamicShortcuts().size());
531 assertEquals(2, mManager.getRemainingCallCount());
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());
538 // This should still work.
539 assertTrue(mManager.setDynamicShortcuts(list(si1, si2, si3)));
540 assertEquals(3, mManager.getDynamicShortcuts().size());
543 assertEquals(1, mManager.getRemainingCallCount());
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);
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));
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),
569 // getDynamicShortcuts() shouldn't return icons, thus assertAllNotHaveIcon().
570 assertShortcutIds(assertAllNotHaveIcon(mManager.getDynamicShortcuts()),
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)
585 assertShortcutIds(assertAllNotHaveIcon(mManager.getDynamicShortcuts()),
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)
596 assertShortcutIds(assertAllNotHaveIcon(mManager.getDynamicShortcuts()),
600 // Re-initialize and load from the files.
601 mService.saveDirtyInfo();
604 // Load from launcher.
607 setCaller(LAUNCHER_1);
608 // Check hasIconResource()/hasIconFile().
609 assertShortcutIds(assertAllHaveIconResId(
610 list(getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "res32x32", USER_0))),
613 assertShortcutIds(assertAllHaveIconResId(
614 list(getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "res64x64", USER_0))),
617 assertShortcutIds(assertAllHaveIconFile(
618 list(getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp32x32", USER_0))),
621 assertShortcutIds(assertAllHaveIconFile(
622 list(getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp64x64", USER_0))),
625 assertShortcutIds(assertAllHaveIconFile(
626 list(getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp512x512", USER_0))),
629 assertShortcutIds(assertAllHaveIconResId(
630 list(getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "res32x32", USER_P0))),
632 assertShortcutIds(assertAllHaveIconFile(
633 list(getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp32x32", USER_P0))),
638 R.drawable.black_32x32,
639 mLauncherApps.getShortcutIconResId(
640 getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "res32x32", USER_0)));
643 R.drawable.black_64x64,
644 mLauncherApps.getShortcutIconResId(
645 getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "res64x64", USER_0)));
648 0, // because it's not a resource
649 mLauncherApps.getShortcutIconResId(
650 getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp32x32", USER_0)));
652 0, // because it's not a resource
653 mLauncherApps.getShortcutIconResId(
654 getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp64x64", USER_0)));
656 0, // because it's not a resource
657 mLauncherApps.getShortcutIconResId(
658 getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp512x512", USER_0)));
660 bmp = pfdToBitmap(mLauncherApps.getShortcutIconFd(
661 getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp32x32", USER_0)));
662 assertBitmapSize(32, 32, bmp);
664 bmp = pfdToBitmap(mLauncherApps.getShortcutIconFd(
665 getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp64x64", USER_0)));
666 assertBitmapSize(64, 64, bmp);
668 bmp = pfdToBitmap(mLauncherApps.getShortcutIconFd(
669 getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp512x512", USER_0)));
670 assertBitmapSize(128, 128, bmp);
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);
681 // Also check the overload APIs too.
683 R.drawable.black_32x32,
684 mLauncherApps.getShortcutIconResId(CALLING_PACKAGE_1, "res32x32", HANDLE_USER_0));
686 R.drawable.black_64x64,
687 mLauncherApps.getShortcutIconResId(CALLING_PACKAGE_1, "res64x64", HANDLE_USER_0));
689 R.drawable.black_512x512,
690 mLauncherApps.getShortcutIconResId(CALLING_PACKAGE_1, "res32x32", HANDLE_USER_P0));
692 mLauncherApps.getShortcutIconFd(CALLING_PACKAGE_1, "bmp32x32", HANDLE_USER_P0));
693 assertBitmapSize(128, 128, bmp);
696 public void testCleanupDanglingBitmaps() throws Exception {
697 assertBitmapDirectories(USER_0, EMPTY_STRINGS);
698 assertBitmapDirectories(USER_10, EMPTY_STRINGS);
700 // Make some shortcuts with bitmap icons.
701 final Icon bmp32x32 = Icon.createWithBitmap(BitmapFactory.decodeResource(
702 getTestContext().getResources(), R.drawable.black_32x32));
704 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
705 mManager.setDynamicShortcuts(list(
706 makeShortcutWithIcon("s1", bmp32x32),
707 makeShortcutWithIcon("s2", bmp32x32),
708 makeShortcutWithIcon("s3", bmp32x32)
712 // Increment the time (which actually we don't have to), which is used for filenames.
713 mInjectedCurrentTimeMillis++;
715 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
716 mManager.setDynamicShortcuts(list(
717 makeShortcutWithIcon("s4", bmp32x32),
718 makeShortcutWithIcon("s5", bmp32x32),
719 makeShortcutWithIcon("s6", bmp32x32)
723 // Increment the time, which is used for filenames.
724 mInjectedCurrentTimeMillis++;
726 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
727 mManager.setDynamicShortcuts(list(
731 // For USER-10, let's try without updating the times.
732 mRunningUsers.put(USER_10, true);
734 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
735 mManager.setDynamicShortcuts(list(
736 makeShortcutWithIcon("10s1", bmp32x32),
737 makeShortcutWithIcon("10s2", bmp32x32),
738 makeShortcutWithIcon("10s3", bmp32x32)
741 runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
742 mManager.setDynamicShortcuts(list(
743 makeShortcutWithIcon("10s4", bmp32x32),
744 makeShortcutWithIcon("10s5", bmp32x32),
745 makeShortcutWithIcon("10s6", bmp32x32)
748 runWithCaller(CALLING_PACKAGE_3, USER_10, () -> {
749 mManager.setDynamicShortcuts(list(
755 // Check files and directories.
756 // Package 3 has no bitmaps, so we don't create a directory.
757 assertBitmapDirectories(USER_0, CALLING_PACKAGE_1, CALLING_PACKAGE_2);
758 assertBitmapDirectories(USER_10, CALLING_PACKAGE_1, CALLING_PACKAGE_2);
760 assertBitmapFiles(USER_0, CALLING_PACKAGE_1,
761 getBitmapFilename(USER_0, CALLING_PACKAGE_1, "s1"),
762 getBitmapFilename(USER_0, CALLING_PACKAGE_1, "s2"),
763 getBitmapFilename(USER_0, CALLING_PACKAGE_1, "s3")
765 assertBitmapFiles(USER_0, CALLING_PACKAGE_2,
766 getBitmapFilename(USER_0, CALLING_PACKAGE_2, "s4"),
767 getBitmapFilename(USER_0, CALLING_PACKAGE_2, "s5"),
768 getBitmapFilename(USER_0, CALLING_PACKAGE_2, "s6")
770 assertBitmapFiles(USER_0, CALLING_PACKAGE_3,
773 assertBitmapFiles(USER_10, CALLING_PACKAGE_1,
774 getBitmapFilename(USER_10, CALLING_PACKAGE_1, "10s1"),
775 getBitmapFilename(USER_10, CALLING_PACKAGE_1, "10s2"),
776 getBitmapFilename(USER_10, CALLING_PACKAGE_1, "10s3")
778 assertBitmapFiles(USER_10, CALLING_PACKAGE_2,
779 getBitmapFilename(USER_10, CALLING_PACKAGE_2, "10s4"),
780 getBitmapFilename(USER_10, CALLING_PACKAGE_2, "10s5"),
781 getBitmapFilename(USER_10, CALLING_PACKAGE_2, "10s6")
783 assertBitmapFiles(USER_10, CALLING_PACKAGE_3,
787 // Then create random directories and files.
788 makeFile(mService.getUserBitmapFilePath(USER_0), "a.b.c").mkdir();
789 makeFile(mService.getUserBitmapFilePath(USER_0), "d.e.f").mkdir();
790 makeFile(mService.getUserBitmapFilePath(USER_0), "d.e.f", "123").createNewFile();
791 makeFile(mService.getUserBitmapFilePath(USER_0), "d.e.f", "456").createNewFile();
793 makeFile(mService.getUserBitmapFilePath(USER_0), CALLING_PACKAGE_3).mkdir();
795 makeFile(mService.getUserBitmapFilePath(USER_0), CALLING_PACKAGE_1, "1").createNewFile();
796 makeFile(mService.getUserBitmapFilePath(USER_0), CALLING_PACKAGE_1, "2").createNewFile();
797 makeFile(mService.getUserBitmapFilePath(USER_0), CALLING_PACKAGE_1, "3").createNewFile();
798 makeFile(mService.getUserBitmapFilePath(USER_0), CALLING_PACKAGE_1, "4").createNewFile();
800 makeFile(mService.getUserBitmapFilePath(USER_10), "10a.b.c").mkdir();
801 makeFile(mService.getUserBitmapFilePath(USER_10), "10d.e.f").mkdir();
802 makeFile(mService.getUserBitmapFilePath(USER_10), "10d.e.f", "123").createNewFile();
803 makeFile(mService.getUserBitmapFilePath(USER_10), "10d.e.f", "456").createNewFile();
805 makeFile(mService.getUserBitmapFilePath(USER_10), CALLING_PACKAGE_2, "1").createNewFile();
806 makeFile(mService.getUserBitmapFilePath(USER_10), CALLING_PACKAGE_2, "2").createNewFile();
807 makeFile(mService.getUserBitmapFilePath(USER_10), CALLING_PACKAGE_2, "3").createNewFile();
808 makeFile(mService.getUserBitmapFilePath(USER_10), CALLING_PACKAGE_2, "4").createNewFile();
810 assertBitmapDirectories(USER_0, CALLING_PACKAGE_1, CALLING_PACKAGE_2, CALLING_PACKAGE_3,
813 // Save and load. When a user is loaded, we do the cleanup.
814 mService.saveDirtyInfo();
817 mService.handleUnlockUser(USER_0);
818 mService.handleUnlockUser(USER_10);
819 mService.handleUnlockUser(20); // Make sure the logic will still work for nonexistent user.
821 // The below check is the same as above, except this time USER_0 use the CALLING_PACKAGE_3
824 assertBitmapDirectories(USER_0, CALLING_PACKAGE_1, CALLING_PACKAGE_2, CALLING_PACKAGE_3);
825 assertBitmapDirectories(USER_10, CALLING_PACKAGE_1, CALLING_PACKAGE_2);
827 assertBitmapFiles(USER_0, CALLING_PACKAGE_1,
828 getBitmapFilename(USER_0, CALLING_PACKAGE_1, "s1"),
829 getBitmapFilename(USER_0, CALLING_PACKAGE_1, "s2"),
830 getBitmapFilename(USER_0, CALLING_PACKAGE_1, "s3")
832 assertBitmapFiles(USER_0, CALLING_PACKAGE_2,
833 getBitmapFilename(USER_0, CALLING_PACKAGE_2, "s4"),
834 getBitmapFilename(USER_0, CALLING_PACKAGE_2, "s5"),
835 getBitmapFilename(USER_0, CALLING_PACKAGE_2, "s6")
837 assertBitmapFiles(USER_0, CALLING_PACKAGE_3,
840 assertBitmapFiles(USER_10, CALLING_PACKAGE_1,
841 getBitmapFilename(USER_10, CALLING_PACKAGE_1, "10s1"),
842 getBitmapFilename(USER_10, CALLING_PACKAGE_1, "10s2"),
843 getBitmapFilename(USER_10, CALLING_PACKAGE_1, "10s3")
845 assertBitmapFiles(USER_10, CALLING_PACKAGE_2,
846 getBitmapFilename(USER_10, CALLING_PACKAGE_2, "10s4"),
847 getBitmapFilename(USER_10, CALLING_PACKAGE_2, "10s5"),
848 getBitmapFilename(USER_10, CALLING_PACKAGE_2, "10s6")
850 assertBitmapFiles(USER_10, CALLING_PACKAGE_3,
855 protected void checkShrinkBitmap(int expectedWidth, int expectedHeight, int resId, int maxSize) {
856 assertBitmapSize(expectedWidth, expectedHeight,
857 ShortcutService.shrinkBitmap(BitmapFactory.decodeResource(
858 getTestContext().getResources(), resId),
862 public void testShrinkBitmap() {
863 checkShrinkBitmap(32, 32, R.drawable.black_512x512, 32);
864 checkShrinkBitmap(511, 511, R.drawable.black_512x512, 511);
865 checkShrinkBitmap(512, 512, R.drawable.black_512x512, 512);
867 checkShrinkBitmap(1024, 4096, R.drawable.black_1024x4096, 4096);
868 checkShrinkBitmap(1024, 4096, R.drawable.black_1024x4096, 4100);
869 checkShrinkBitmap(512, 2048, R.drawable.black_1024x4096, 2048);
871 checkShrinkBitmap(4096, 1024, R.drawable.black_4096x1024, 4096);
872 checkShrinkBitmap(4096, 1024, R.drawable.black_4096x1024, 4100);
873 checkShrinkBitmap(2048, 512, R.drawable.black_4096x1024, 2048);
876 protected File openIconFileForWriteAndGetPath(int userId, String packageName)
878 // Shortcut IDs aren't used in the path, so just pass the same ID.
879 final FileOutputStreamWithPath out =
880 mService.openIconFileForWrite(userId, makePackageShortcut(packageName, "id"));
882 return out.getFile();
885 public void testOpenIconFileForWrite() throws IOException {
886 mInjectedCurrentTimeMillis = 1000;
888 final File p10_1_1 = openIconFileForWriteAndGetPath(10, CALLING_PACKAGE_1);
889 final File p10_1_2 = openIconFileForWriteAndGetPath(10, CALLING_PACKAGE_1);
891 final File p10_2_1 = openIconFileForWriteAndGetPath(10, CALLING_PACKAGE_2);
892 final File p10_2_2 = openIconFileForWriteAndGetPath(10, CALLING_PACKAGE_2);
894 final File p11_1_1 = openIconFileForWriteAndGetPath(11, CALLING_PACKAGE_1);
895 final File p11_1_2 = openIconFileForWriteAndGetPath(11, CALLING_PACKAGE_1);
897 mInjectedCurrentTimeMillis++;
899 final File p10_1_3 = openIconFileForWriteAndGetPath(10, CALLING_PACKAGE_1);
900 final File p10_1_4 = openIconFileForWriteAndGetPath(10, CALLING_PACKAGE_1);
901 final File p10_1_5 = openIconFileForWriteAndGetPath(10, CALLING_PACKAGE_1);
903 final File p10_2_3 = openIconFileForWriteAndGetPath(10, CALLING_PACKAGE_2);
904 final File p11_1_3 = openIconFileForWriteAndGetPath(11, CALLING_PACKAGE_1);
906 // Make sure their paths are all unique
907 assertAllUnique(list(
923 // Check each set has the same parent.
924 assertEquals(p10_1_1.getParent(), p10_1_2.getParent());
925 assertEquals(p10_1_1.getParent(), p10_1_3.getParent());
926 assertEquals(p10_1_1.getParent(), p10_1_4.getParent());
927 assertEquals(p10_1_1.getParent(), p10_1_5.getParent());
929 assertEquals(p10_2_1.getParent(), p10_2_2.getParent());
930 assertEquals(p10_2_1.getParent(), p10_2_3.getParent());
932 assertEquals(p11_1_1.getParent(), p11_1_2.getParent());
933 assertEquals(p11_1_1.getParent(), p11_1_3.getParent());
935 // Check the parents are still unique.
936 assertAllUnique(list(
942 // All files created at the same time for the same package/user, expcet for the first ones,
943 // will have "_" in the path.
944 assertFalse(p10_1_1.getName().contains("_"));
945 assertTrue(p10_1_2.getName().contains("_"));
946 assertFalse(p10_1_3.getName().contains("_"));
947 assertTrue(p10_1_4.getName().contains("_"));
948 assertTrue(p10_1_5.getName().contains("_"));
950 assertFalse(p10_2_1.getName().contains("_"));
951 assertTrue(p10_2_2.getName().contains("_"));
952 assertFalse(p10_2_3.getName().contains("_"));
954 assertFalse(p11_1_1.getName().contains("_"));
955 assertTrue(p11_1_2.getName().contains("_"));
956 assertFalse(p11_1_3.getName().contains("_"));
959 public void testUpdateShortcuts() {
960 runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
961 assertTrue(mManager.setDynamicShortcuts(list(
969 runWithCaller(CALLING_PACKAGE_2, UserHandle.USER_SYSTEM, () -> {
970 assertTrue(mManager.setDynamicShortcuts(list(
978 runWithCaller(LAUNCHER_1, UserHandle.USER_SYSTEM, () -> {
979 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s2", "s3"),
981 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s4", "s5"),
984 runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
985 mManager.removeDynamicShortcuts(list("s1"));
986 mManager.removeDynamicShortcuts(list("s2"));
988 runWithCaller(CALLING_PACKAGE_2, UserHandle.USER_SYSTEM, () -> {
989 mManager.removeDynamicShortcuts(list("s1"));
990 mManager.removeDynamicShortcuts(list("s3"));
991 mManager.removeDynamicShortcuts(list("s5"));
993 runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
994 assertShortcutIds(assertAllDynamic(
995 mManager.getDynamicShortcuts()),
997 assertShortcutIds(assertAllPinned(
998 mManager.getPinnedShortcuts()),
1001 runWithCaller(CALLING_PACKAGE_2, UserHandle.USER_SYSTEM, () -> {
1002 assertShortcutIds(assertAllDynamic(
1003 mManager.getDynamicShortcuts()),
1005 assertShortcutIds(assertAllPinned(
1006 mManager.getPinnedShortcuts()),
1010 runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
1011 ShortcutInfo s2 = makeShortcutBuilder()
1013 .setIcon(Icon.createWithResource(getTestContext(), R.drawable.black_32x32))
1016 ShortcutInfo s4 = makeShortcutBuilder()
1018 .setTitle("new title")
1021 mManager.updateShortcuts(list(s2, s4));
1023 runWithCaller(CALLING_PACKAGE_2, UserHandle.USER_SYSTEM, () -> {
1024 ShortcutInfo s2 = makeShortcutBuilder()
1026 .setIntent(makeIntent(Intent.ACTION_ANSWER, ShortcutActivity.class,
1030 ShortcutInfo s4 = makeShortcutBuilder()
1032 .setIntent(new Intent(Intent.ACTION_ALL_APPS))
1035 mManager.updateShortcuts(list(s2, s4));
1038 runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
1039 assertShortcutIds(assertAllDynamic(
1040 mManager.getDynamicShortcuts()),
1042 assertShortcutIds(assertAllPinned(
1043 mManager.getPinnedShortcuts()),
1046 ShortcutInfo s = getCallerShortcut("s2");
1047 assertTrue(s.hasIconResource());
1048 assertEquals(R.drawable.black_32x32, s.getIconResourceId());
1049 assertEquals("string/r" + R.drawable.black_32x32, s.getIconResName());
1050 assertEquals("Title-s2", s.getTitle());
1052 s = getCallerShortcut("s4");
1053 assertFalse(s.hasIconResource());
1054 assertEquals(0, s.getIconResourceId());
1055 assertEquals("new title", s.getTitle());
1057 runWithCaller(CALLING_PACKAGE_2, UserHandle.USER_SYSTEM, () -> {
1058 assertShortcutIds(assertAllDynamic(
1059 mManager.getDynamicShortcuts()),
1061 assertShortcutIds(assertAllPinned(
1062 mManager.getPinnedShortcuts()),
1065 ShortcutInfo s = getCallerShortcut("s2");
1066 assertFalse(s.hasIconResource());
1067 assertEquals(0, s.getIconResourceId());
1068 assertEquals("Title-s2", s.getTitle());
1069 assertEquals(Intent.ACTION_ANSWER, s.getIntent().getAction());
1070 assertEquals(1, s.getIntent().getExtras().size());
1072 s = getCallerShortcut("s4");
1073 assertFalse(s.hasIconResource());
1074 assertEquals(0, s.getIconResourceId());
1075 assertEquals("Title-s4", s.getTitle());
1076 assertEquals(Intent.ACTION_ALL_APPS, s.getIntent().getAction());
1077 assertBundleEmpty(s.getIntent().getExtras());
1079 // TODO Check with other fields too.
1081 // TODO Check bitmap removal too.
1083 mRunningUsers.put(USER_11, true);
1085 runWithCaller(CALLING_PACKAGE_2, USER_11, () -> {
1086 mManager.updateShortcuts(list());
1090 public void testUpdateShortcuts_icons() {
1091 runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
1092 assertTrue(mManager.setDynamicShortcuts(list(
1096 // Set resource icon
1097 assertTrue(mManager.updateShortcuts(list(
1098 new ShortcutInfo.Builder(mClientContext, "s1")
1099 .setIcon(Icon.createWithResource(getTestContext(), R.drawable.black_32x32))
1103 assertWith(getCallerShortcuts())
1104 .forShortcutWithId("s1", si -> {
1105 assertTrue(si.hasIconResource());
1106 assertEquals(R.drawable.black_32x32, si.getIconResourceId());
1110 assertTrue(mManager.updateShortcuts(list(
1111 new ShortcutInfo.Builder(mClientContext, "s1")
1112 .setIcon(Icon.createWithBitmap(BitmapFactory.decodeResource(
1113 getTestContext().getResources(), R.drawable.black_64x64)))
1117 assertWith(getCallerShortcuts())
1118 .forShortcutWithId("s1", si -> {
1119 assertTrue(si.hasIconFile());
1122 mInjectedCurrentTimeMillis += INTERVAL; // reset throttling
1124 // Do it again, with the reverse order (bitmap -> icon)
1125 assertTrue(mManager.setDynamicShortcuts(list(
1130 assertTrue(mManager.updateShortcuts(list(
1131 new ShortcutInfo.Builder(mClientContext, "s1")
1132 .setIcon(Icon.createWithBitmap(BitmapFactory.decodeResource(
1133 getTestContext().getResources(), R.drawable.black_64x64)))
1137 assertWith(getCallerShortcuts())
1138 .forShortcutWithId("s1", si -> {
1139 assertTrue(si.hasIconFile());
1142 // Set resource icon
1143 assertTrue(mManager.updateShortcuts(list(
1144 new ShortcutInfo.Builder(mClientContext, "s1")
1145 .setIcon(Icon.createWithResource(getTestContext(), R.drawable.black_32x32))
1149 assertWith(getCallerShortcuts())
1150 .forShortcutWithId("s1", si -> {
1151 assertTrue(si.hasIconResource());
1152 assertEquals(R.drawable.black_32x32, si.getIconResourceId());
1157 // === Test for launcher side APIs ===
1159 public void testGetShortcuts() {
1161 // Set up shortcuts.
1163 setCaller(CALLING_PACKAGE_1);
1164 final ShortcutInfo s1_1 = makeShortcut("s1");
1165 final ShortcutInfo s1_2 = makeShortcut("s2");
1167 assertTrue(mManager.setDynamicShortcuts(list(s1_1, s1_2)));
1169 // Because setDynamicShortcuts will update the timestamps when ranks are changing,
1170 // we explicitly set timestamps here.
1171 getCallerShortcut("s1").setTimestamp(5000);
1172 getCallerShortcut("s2").setTimestamp(1000);
1174 setCaller(CALLING_PACKAGE_2);
1175 final ShortcutInfo s2_2 = makeShortcut("s2");
1176 final ShortcutInfo s2_3 = makeShortcutWithActivity("s3",
1177 makeComponent(ShortcutActivity2.class));
1178 final ShortcutInfo s2_4 = makeShortcutWithActivity("s4",
1179 makeComponent(ShortcutActivity.class));
1180 assertTrue(mManager.setDynamicShortcuts(list(s2_2, s2_3, s2_4)));
1182 getCallerShortcut("s2").setTimestamp(1500);
1183 getCallerShortcut("s3").setTimestamp(3000);
1184 getCallerShortcut("s4").setTimestamp(500);
1186 setCaller(CALLING_PACKAGE_3);
1187 final ShortcutInfo s3_2 = makeShortcut("s3");
1188 assertTrue(mManager.setDynamicShortcuts(list(s3_2)));
1190 getCallerShortcut("s3").setTimestamp(START_TIME + 5000);
1192 setCaller(LAUNCHER_1);
1195 assertAllDynamic(assertAllHaveTitle(assertAllNotHaveIntents(assertAllStringsResolved(
1197 assertAllNotKeyFieldsOnly(
1198 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1199 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
1204 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1205 /* activity =*/ null,
1206 ShortcutQuery.FLAG_GET_PINNED), getCallingUser())
1209 // Get both, with timestamp
1210 assertAllDynamic(assertAllHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
1211 assertAllNotKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
1212 /* time =*/ 1000, CALLING_PACKAGE_2,
1213 /* activity =*/ null,
1214 ShortcutQuery.FLAG_GET_PINNED | ShortcutQuery.FLAG_GET_DYNAMIC),
1218 // FLAG_GET_KEY_FIELDS_ONLY
1219 assertAllDynamic(assertAllNotHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
1220 assertAllKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
1221 /* time =*/ 1000, CALLING_PACKAGE_2,
1222 /* activity =*/ null,
1223 ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY),
1227 // Filter by activity
1228 assertAllDynamic(assertAllHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
1229 assertAllNotKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
1230 /* time =*/ 0, CALLING_PACKAGE_2,
1231 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
1232 ShortcutQuery.FLAG_GET_PINNED | ShortcutQuery.FLAG_GET_DYNAMIC),
1237 assertAllDynamic(assertAllNotHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
1238 assertAllKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
1239 /* time =*/ 1000, CALLING_PACKAGE_2, list("s3"),
1240 /* activity =*/ null,
1241 ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY),
1244 assertAllDynamic(assertAllNotHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
1245 assertAllKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
1246 /* time =*/ 1000, CALLING_PACKAGE_2, list("s3", "s2", "ss"),
1247 /* activity =*/ null,
1248 ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY),
1251 assertAllDynamic(assertAllNotHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
1252 assertAllKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
1253 /* time =*/ 1000, CALLING_PACKAGE_2, list("s3x", "s2x"),
1254 /* activity =*/ null,
1255 ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY),
1258 assertAllDynamic(assertAllNotHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
1259 assertAllKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
1260 /* time =*/ 1000, CALLING_PACKAGE_2, list(),
1261 /* activity =*/ null,
1262 ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY),
1266 // Pin some shortcuts.
1267 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
1268 list("s3", "s4"), getCallingUser());
1271 assertAllPinned(assertAllHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
1272 assertAllNotKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
1273 /* time =*/ 1000, CALLING_PACKAGE_2,
1274 /* activity =*/ null,
1275 ShortcutQuery.FLAG_GET_PINNED),
1280 assertShortcutIds(assertAllNotKeyFieldsOnly(
1281 mLauncherApps.getShortcuts(buildQuery(
1282 /* time =*/ 5000, /* package= */ null,
1283 /* activity =*/ null,
1284 ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_PINNED),
1288 assertExpectException(
1289 IllegalArgumentException.class, "package name must also be set", () -> {
1290 mLauncherApps.getShortcuts(buildQuery(
1291 /* time =*/ 0, /* package= */ null, list("id"),
1292 /* activity =*/ null, /* flags */ 0), getCallingUser());
1295 // TODO More tests: pinned but dynamic.
1298 public void testGetShortcuts_shortcutKinds() throws Exception {
1299 // Create 3 manifest and 3 dynamic shortcuts
1300 addManifestShortcutResource(
1301 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
1303 updatePackageVersion(CALLING_PACKAGE_1, 1);
1304 mService.mPackageMonitor.onReceive(getTestContext(),
1305 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
1307 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1308 assertTrue(mManager.setDynamicShortcuts(list(
1309 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
1313 runWithCaller(LAUNCHER_1, USER_0, () -> {
1314 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms2", "ms3", "s2", "s3"),
1318 // Remove ms3 and s3
1319 addManifestShortcutResource(
1320 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
1322 updatePackageVersion(CALLING_PACKAGE_1, 1);
1323 mService.mPackageMonitor.onReceive(getTestContext(),
1324 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
1326 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1327 assertTrue(mManager.setDynamicShortcuts(list(
1328 makeShortcut("s1"), makeShortcut("s2"))));
1331 // Check their status.
1332 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1333 assertWith(getCallerShortcuts())
1334 .haveIds("ms1", "ms2", "ms3", "s1", "s2", "s3")
1336 .selectByIds("ms1", "ms2")
1341 .revertToOriginalList()
1343 .areAllNotManifest()
1348 .revertToOriginalList()
1349 .selectByIds("s1", "s2")
1350 .areAllNotManifest()
1354 .revertToOriginalList()
1356 .areAllNotManifest()
1361 .revertToOriginalList()
1362 .selectByIds("s1", "ms1")
1365 .revertToOriginalList()
1366 .selectByIds("s2", "s3", "ms2", "ms3")
1371 // Finally, actual tests.
1372 runWithCaller(LAUNCHER_1, USER_0, () -> {
1373 assertWith(mLauncherApps.getShortcuts(
1374 buildQueryWithFlags(ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0))
1375 .haveIds("s1", "s2");
1376 assertWith(mLauncherApps.getShortcuts(
1377 buildQueryWithFlags(ShortcutQuery.FLAG_GET_MANIFEST), HANDLE_USER_0))
1378 .haveIds("ms1", "ms2");
1379 assertWith(mLauncherApps.getShortcuts(
1380 buildQueryWithFlags(ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0))
1381 .haveIds("s2", "s3", "ms2", "ms3");
1383 assertWith(mLauncherApps.getShortcuts(
1384 buildQueryWithFlags(
1385 ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_PINNED
1387 .haveIds("s1", "s2", "s3", "ms2", "ms3");
1389 assertWith(mLauncherApps.getShortcuts(
1390 buildQueryWithFlags(
1391 ShortcutQuery.FLAG_GET_MANIFEST | ShortcutQuery.FLAG_GET_PINNED
1393 .haveIds("ms1", "s2", "s3", "ms2", "ms3");
1395 assertWith(mLauncherApps.getShortcuts(
1396 buildQueryWithFlags(
1397 ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_MANIFEST
1399 .haveIds("ms1", "ms2", "s1", "s2");
1401 assertWith(mLauncherApps.getShortcuts(
1402 buildQueryWithFlags(
1403 ShortcutQuery.FLAG_GET_ALL_KINDS
1405 .haveIds("ms1", "ms2", "ms3", "s1", "s2", "s3");
1409 public void testGetShortcuts_resolveStrings() throws Exception {
1410 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1411 ShortcutInfo si = new ShortcutInfo.Builder(mClientContext)
1413 .setActivity(new ComponentName(mClientContext, "dummy"))
1416 .setDisabledMessageResId(12)
1417 .setIntent(makeIntent("action", ShortcutActivity.class))
1419 mManager.setDynamicShortcuts(list(si));
1421 runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
1422 ShortcutInfo si = new ShortcutInfo.Builder(mClientContext)
1424 .setActivity(new ComponentName(mClientContext, "dummy"))
1427 .setDisabledMessageResId(12)
1428 .setIntent(makeIntent("action", ShortcutActivity.class))
1430 mManager.setDynamicShortcuts(list(si));
1433 runWithCaller(LAUNCHER_1, USER_0, () -> {
1434 final ShortcutQuery q = new ShortcutQuery();
1435 q.setQueryFlags(ShortcutQuery.FLAG_GET_DYNAMIC);
1438 List<ShortcutInfo> ret = assertShortcutIds(
1439 assertAllStringsResolved(mLauncherApps.getShortcuts(q, HANDLE_USER_0)),
1441 assertEquals("string-com.android.test.1-user:0-res:10/en", ret.get(0).getTitle());
1442 assertEquals("string-com.android.test.1-user:0-res:11/en", ret.get(0).getText());
1443 assertEquals("string-com.android.test.1-user:0-res:12/en",
1444 ret.get(0).getDisabledMessage());
1447 ret = assertShortcutIds(
1448 assertAllStringsResolved(mLauncherApps.getShortcuts(q, HANDLE_USER_P0)),
1450 assertEquals("string-com.android.test.1-user:20-res:10/en", ret.get(0).getTitle());
1451 assertEquals("string-com.android.test.1-user:20-res:11/en", ret.get(0).getText());
1452 assertEquals("string-com.android.test.1-user:20-res:12/en",
1453 ret.get(0).getDisabledMessage());
1458 public void testGetShortcutInfo() {
1459 // Create shortcuts.
1460 setCaller(CALLING_PACKAGE_1);
1461 final ShortcutInfo s1_1 = makeShortcut(
1464 makeComponent(ShortcutActivity.class),
1466 makeIntent(Intent.ACTION_ASSIST, ShortcutActivity2.class,
1467 "key1", "val1", "nest", makeBundle("key", 123)),
1470 final ShortcutInfo s1_2 = makeShortcut(
1473 /* activity */ null,
1475 makeIntent(Intent.ACTION_ASSIST, ShortcutActivity3.class),
1478 assertTrue(mManager.setDynamicShortcuts(list(s1_1, s1_2)));
1481 setCaller(CALLING_PACKAGE_2);
1482 final ShortcutInfo s2_1 = makeShortcut(
1485 makeComponent(ShortcutActivity2.class),
1487 makeIntent(Intent.ACTION_ANSWER, ShortcutActivity2.class,
1488 "key1", "val1", "nest", makeBundle("key", 123)),
1490 assertTrue(mManager.setDynamicShortcuts(list(s2_1)));
1494 setCaller(LAUNCHER_1);
1496 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
1497 list("s2"), getCallingUser());
1502 setCaller(CALLING_PACKAGE_1);
1503 assertShortcutIds(mManager.getPinnedShortcuts(), "s2");
1504 mManager.removeDynamicShortcuts(list("s2"));
1505 assertShortcutIds(mManager.getPinnedShortcuts(), "s2");
1509 setCaller(LAUNCHER_1);
1510 List<ShortcutInfo> list;
1512 // Note we don't guarantee the orders.
1513 list = assertShortcutIds(assertAllHaveTitle(assertAllNotHaveIntents(
1514 assertAllNotKeyFieldsOnly(
1515 mLauncherApps.getShortcutInfo(CALLING_PACKAGE_1,
1516 list("s2", "s1", "s3", null), getCallingUser())))),
1518 assertEquals("Title 1", findById(list, "s1").getTitle());
1519 assertEquals("Title 2", findById(list, "s2").getTitle());
1521 assertShortcutIds(assertAllHaveTitle(assertAllNotHaveIntents(
1522 mLauncherApps.getShortcutInfo(CALLING_PACKAGE_1,
1523 list("s3"), getCallingUser())))
1526 list = assertShortcutIds(assertAllHaveTitle(assertAllNotHaveIntents(
1527 mLauncherApps.getShortcutInfo(CALLING_PACKAGE_2,
1528 list("s1", "s2", "s3"), getCallingUser()))),
1530 assertEquals("ABC", findById(list, "s1").getTitle());
1533 public void testPinShortcutAndGetPinnedShortcuts() {
1534 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1535 final ShortcutInfo s1_1 = makeShortcutWithTimestamp("s1", 1000);
1536 final ShortcutInfo s1_2 = makeShortcutWithTimestamp("s2", 2000);
1538 assertTrue(mManager.setDynamicShortcuts(list(s1_1, s1_2)));
1541 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
1542 final ShortcutInfo s2_2 = makeShortcutWithTimestamp("s2", 1500);
1543 final ShortcutInfo s2_3 = makeShortcutWithTimestamp("s3", 3000);
1544 final ShortcutInfo s2_4 = makeShortcutWithTimestamp("s4", 500);
1545 assertTrue(mManager.setDynamicShortcuts(list(s2_2, s2_3, s2_4)));
1548 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
1549 final ShortcutInfo s3_2 = makeShortcutWithTimestamp("s2", 1000);
1550 assertTrue(mManager.setDynamicShortcuts(list(s3_2)));
1554 runWithCaller(LAUNCHER_1, USER_0, () -> {
1555 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
1556 list("s2", "s3"), getCallingUser());
1558 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
1559 list("s3", "s4", "s5"), getCallingUser());
1561 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3,
1562 list("s3"), getCallingUser()); // Note ID doesn't exist
1566 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1567 assertShortcutIds(mManager.getPinnedShortcuts(), "s2");
1568 mManager.removeDynamicShortcuts(list("s2"));
1569 assertShortcutIds(mManager.getPinnedShortcuts(), "s2");
1571 assertShortcutIds(mManager.getDynamicShortcuts(), "s1");
1574 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
1575 assertShortcutIds(mManager.getPinnedShortcuts(), "s3", "s4");
1576 mManager.removeDynamicShortcuts(list("s3"));
1577 assertShortcutIds(mManager.getPinnedShortcuts(), "s3", "s4");
1579 assertShortcutIds(mManager.getDynamicShortcuts(), "s2", "s4");
1582 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
1583 assertShortcutIds(mManager.getPinnedShortcuts() /* none */);
1584 mManager.removeDynamicShortcuts(list("s2"));
1585 assertShortcutIds(mManager.getPinnedShortcuts() /* none */);
1587 assertEmpty(mManager.getDynamicShortcuts());
1590 // Get pinned shortcuts from launcher
1591 runWithCaller(LAUNCHER_1, USER_0, () -> {
1592 // CALLING_PACKAGE_1 deleted s2, but it's pinned, so it still exists.
1593 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(assertAllEnabled(
1594 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1595 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser())))),
1598 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(assertAllEnabled(
1599 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
1600 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser())))),
1603 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(assertAllEnabled(
1604 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_3,
1605 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))))
1611 * This is similar to the above test, except it used "disable" instead of "remove". It also
1614 public void testDisableAndEnableShortcuts() {
1615 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1616 final ShortcutInfo s1_1 = makeShortcutWithTimestamp("s1", 1000);
1617 final ShortcutInfo s1_2 = makeShortcutWithTimestamp("s2", 2000);
1619 assertTrue(mManager.setDynamicShortcuts(list(s1_1, s1_2)));
1622 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
1623 final ShortcutInfo s2_2 = makeShortcutWithTimestamp("s2", 1500);
1624 final ShortcutInfo s2_3 = makeShortcutWithTimestamp("s3", 3000);
1625 final ShortcutInfo s2_4 = makeShortcutWithTimestamp("s4", 500);
1626 assertTrue(mManager.setDynamicShortcuts(list(s2_2, s2_3, s2_4)));
1629 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
1630 final ShortcutInfo s3_2 = makeShortcutWithTimestamp("s2", 1000);
1631 assertTrue(mManager.setDynamicShortcuts(list(s3_2)));
1635 runWithCaller(LAUNCHER_1, USER_0, () -> {
1636 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
1637 list("s2", "s3"), getCallingUser());
1639 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
1640 list("s3", "s4", "s5"), getCallingUser());
1642 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3,
1643 list("s3"), getCallingUser()); // Note ID doesn't exist
1647 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1648 assertShortcutIds(mManager.getPinnedShortcuts(), "s2");
1650 mManager.disableShortcuts(list("s2"));
1652 assertShortcutIds(mManager.getPinnedShortcuts(), "s2");
1653 assertShortcutIds(mManager.getDynamicShortcuts(), "s1");
1656 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
1657 assertShortcutIds(mManager.getPinnedShortcuts(), "s3", "s4");
1659 // disable should work even if a shortcut is not dynamic, so try calling "remove" first
1661 mManager.removeDynamicShortcuts(list("s3"));
1662 mManager.disableShortcuts(list("s3"));
1664 assertShortcutIds(mManager.getPinnedShortcuts(), "s3", "s4");
1665 assertShortcutIds(mManager.getDynamicShortcuts(), "s2", "s4");
1668 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
1669 assertShortcutIds(mManager.getPinnedShortcuts() /* none */);
1671 mManager.disableShortcuts(list("s2"));
1673 assertShortcutIds(mManager.getPinnedShortcuts() /* none */);
1675 assertEmpty(mManager.getDynamicShortcuts());
1676 assertEmpty(getCallerShortcuts());
1679 // Get pinned shortcuts from launcher
1680 runWithCaller(LAUNCHER_1, USER_0, () -> {
1681 // CALLING_PACKAGE_1 deleted s2, but it's pinned, so it still exists, and disabled.
1682 assertWith(mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1683 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))
1686 .areAllNotWithKeyFieldsOnly()
1688 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_0,
1689 ActivityNotFoundException.class);
1691 // Here, s4 is still enabled and launchable, but s3 is disabled.
1692 assertWith(mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
1693 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))
1694 .haveIds("s3", "s4")
1696 .areAllNotWithKeyFieldsOnly()
1701 .revertToOriginalList()
1705 assertStartShortcutThrowsException(CALLING_PACKAGE_2, "s3", USER_0,
1706 ActivityNotFoundException.class);
1707 assertShortcutLaunchable(CALLING_PACKAGE_2, "s4", USER_0);
1709 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(assertAllEnabled(
1710 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_3,
1711 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))))
1715 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1716 mManager.enableShortcuts(list("s2"));
1718 assertShortcutIds(mManager.getPinnedShortcuts(), "s2");
1719 assertShortcutIds(mManager.getDynamicShortcuts(), "s1");
1721 runWithCaller(LAUNCHER_1, USER_0, () -> {
1722 // CALLING_PACKAGE_1 deleted s2, but it's pinned, so it still exists.
1723 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(assertAllEnabled(
1724 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1725 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser())))),
1727 assertShortcutLaunchable(CALLING_PACKAGE_1, "s2", USER_0);
1731 public void testDisableShortcuts_thenRepublish() {
1732 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1733 assertTrue(mManager.setDynamicShortcuts(list(
1734 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
1736 runWithCaller(LAUNCHER_1, USER_0, () -> {
1737 mLauncherApps.pinShortcuts(
1738 CALLING_PACKAGE_1, list("s1", "s2", "s3"), HANDLE_USER_0);
1741 mManager.disableShortcuts(list("s1", "s2", "s3"));
1743 assertWith(getCallerShortcuts())
1744 .haveIds("s1", "s2", "s3")
1749 // Make sure updateShortcuts() will not re-enable them.
1750 assertTrue(mManager.updateShortcuts(list(
1751 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
1753 assertWith(getCallerShortcuts())
1754 .haveIds("s1", "s2", "s3")
1759 // Re-publish s1 with setDynamicShortcuts.
1760 mInjectedCurrentTimeMillis += INTERVAL; // reset throttling
1762 assertTrue(mManager.setDynamicShortcuts(list(
1763 makeShortcut("s1"))));
1765 assertWith(getCallerShortcuts())
1766 .haveIds("s1", "s2", "s3")
1773 .revertToOriginalList()
1774 .selectByIds("s2", "s3")
1779 // Re-publish s2 with addDynamicShortcuts.
1780 mInjectedCurrentTimeMillis += INTERVAL; // reset throttling
1782 assertTrue(mManager.addDynamicShortcuts(list(
1783 makeShortcut("s2"))));
1785 assertWith(getCallerShortcuts())
1786 .haveIds("s1", "s2", "s3")
1788 .selectByIds("s1", "s2")
1793 .revertToOriginalList()
1801 public void testPinShortcutAndGetPinnedShortcuts_multi() {
1802 // Create some shortcuts.
1803 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1804 assertTrue(mManager.setDynamicShortcuts(list(
1805 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
1808 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
1809 assertTrue(mManager.setDynamicShortcuts(list(
1810 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
1816 runWithCaller(LAUNCHER_1, USER_0, () -> {
1817 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
1818 list("s3", "s4"), getCallingUser());
1820 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
1821 list("s1", "s2", "s4"), getCallingUser());
1827 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1828 assertShortcutIds(mManager.getPinnedShortcuts(), "s3");
1829 mManager.removeDynamicShortcuts(list("s3"));
1830 assertShortcutIds(mManager.getPinnedShortcuts(), "s3");
1835 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
1836 assertShortcutIds(mManager.getPinnedShortcuts(), "s1", "s2");
1837 mManager.removeDynamicShortcuts(list("s1"));
1838 mManager.removeDynamicShortcuts(list("s3"));
1839 assertShortcutIds(mManager.getPinnedShortcuts(), "s1", "s2");
1844 // Get pinned shortcuts from launcher
1845 runWithCaller(LAUNCHER_1, USER_0, () -> {
1846 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
1847 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1848 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
1851 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
1852 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
1853 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
1856 assertShortcutIds(assertAllDynamicOrPinned(
1857 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1858 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
1859 | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
1862 assertShortcutIds(assertAllDynamicOrPinned(
1863 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
1864 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
1865 | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
1871 runWithCaller(LAUNCHER_2, USER_0, () -> {
1872 // Launcher2 still has no pinned ones.
1873 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
1874 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1875 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser())))
1877 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
1878 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
1879 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser())))
1882 assertShortcutIds(assertAllDynamic(
1883 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1884 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
1885 | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
1887 assertShortcutIds(assertAllDynamic(
1888 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
1889 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
1890 | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
1894 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
1895 list("s1", "s2"), getCallingUser());
1897 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
1898 list("s1", "s2"), getCallingUser());
1900 assertShortcutIds(assertAllDynamic(
1901 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1902 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
1903 | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
1906 // S1 was not visible to it, so shouldn't be pinned.
1907 assertShortcutIds(assertAllDynamic(
1908 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
1909 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
1910 | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
1914 // Re-initialize and load from the files.
1915 mService.saveDirtyInfo();
1919 mService.handleUnlockUser(USER_0);
1921 // Make sure package info is restored too.
1922 runWithCaller(LAUNCHER_1, USER_0, () -> {
1923 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
1924 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1925 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
1927 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
1928 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
1929 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
1932 runWithCaller(LAUNCHER_2, USER_0, () -> {
1933 assertShortcutIds(assertAllDynamic(
1934 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1935 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
1936 | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
1938 assertShortcutIds(assertAllDynamic(
1939 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
1940 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
1941 | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
1945 // Delete all dynamic.
1946 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1947 mManager.removeAllDynamicShortcuts();
1949 assertEquals(0, mManager.getDynamicShortcuts().size());
1950 assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()), "s1", "s2", "s3");
1952 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
1953 mManager.removeAllDynamicShortcuts();
1955 assertEquals(0, mManager.getDynamicShortcuts().size());
1956 assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()), "s2", "s1");
1959 runWithCaller(LAUNCHER_1, USER_0, () -> {
1960 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
1961 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1962 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
1965 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
1966 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
1967 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
1970 // from all packages.
1971 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
1972 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, null,
1973 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
1976 // Update pined. Note s2 and s3 are actually available, but not visible to this
1977 // launcher, so still can't be pinned.
1978 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s1", "s2", "s3", "s4"),
1981 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
1982 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1983 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
1987 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1988 assertTrue(mManager.addDynamicShortcuts(list(makeShortcut("s1"))));
1990 assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()), "s1");
1991 assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()), "s1", "s2", "s3");
1993 runWithCaller(LAUNCHER_1, USER_0, () -> {
1994 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
1995 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1996 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
1999 // Now "s1" is visible, so can be pinned.
2000 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s1", "s2", "s3", "s4"),
2003 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
2004 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2005 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
2009 // Now clear pinned shortcuts. First, from launcher 1.
2010 runWithCaller(LAUNCHER_1, USER_0, () -> {
2011 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list(), getCallingUser());
2012 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list(), getCallingUser());
2015 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2016 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()).size());
2018 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2019 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()).size());
2021 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2022 assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()), "s1");
2023 assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()), "s1", "s2");
2025 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
2026 assertEquals(0, mManager.getDynamicShortcuts().size());
2027 assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()), "s2");
2030 // Clear all pins from launcher 2.
2031 runWithCaller(LAUNCHER_2, USER_0, () -> {
2032 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list(), getCallingUser());
2033 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list(), getCallingUser());
2036 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2037 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()).size());
2039 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2040 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()).size());
2042 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2043 assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()), "s1");
2044 assertEquals(0, mManager.getPinnedShortcuts().size());
2046 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
2047 assertEquals(0, mManager.getDynamicShortcuts().size());
2048 assertEquals(0, mManager.getPinnedShortcuts().size());
2052 public void testPinShortcutAndGetPinnedShortcuts_crossProfile_plusLaunch() {
2053 // Create some shortcuts.
2054 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2055 assertTrue(mManager.setDynamicShortcuts(list(
2056 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2058 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
2059 assertTrue(mManager.setDynamicShortcuts(list(
2060 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2063 mRunningUsers.put(USER_10, true);
2065 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
2066 assertTrue(mManager.setDynamicShortcuts(list(
2067 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"),
2068 makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6"))));
2071 // Pin some shortcuts and see the result.
2073 runWithCaller(LAUNCHER_1, USER_0, () -> {
2074 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
2075 list("s1"), HANDLE_USER_0);
2077 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
2078 list("s1", "s2", "s3"), HANDLE_USER_0);
2081 runWithCaller(LAUNCHER_1, USER_P0, () -> {
2082 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
2083 list("s2"), HANDLE_USER_0);
2085 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
2086 list("s2", "s3"), HANDLE_USER_0);
2089 runWithCaller(LAUNCHER_2, USER_P0, () -> {
2090 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
2091 list("s3"), HANDLE_USER_0);
2093 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
2094 list("s3"), HANDLE_USER_0);
2097 runWithCaller(LAUNCHER_2, USER_10, () -> {
2098 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
2099 list("s1", "s2", "s3"), HANDLE_USER_10);
2102 // Cross profile pinning.
2103 final int PIN_AND_DYNAMIC = ShortcutQuery.FLAG_GET_PINNED | ShortcutQuery.FLAG_GET_DYNAMIC;
2105 runWithCaller(LAUNCHER_1, USER_0, () -> {
2106 assertShortcutIds(assertAllPinned(
2107 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2108 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2110 assertShortcutIds(assertAllDynamic(
2111 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2112 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2114 assertShortcutIds(assertAllDynamicOrPinned(
2115 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2116 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2119 assertShortcutIds(assertAllPinned(
2120 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2121 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2123 assertShortcutIds(assertAllDynamic(
2124 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2125 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2127 assertShortcutIds(assertAllDynamicOrPinned(
2128 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2129 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2132 assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
2133 assertShortcutLaunchable(CALLING_PACKAGE_1, "s2", USER_0);
2134 assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_0);
2136 assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
2137 assertShortcutLaunchable(CALLING_PACKAGE_2, "s2", USER_0);
2138 assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2140 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
2141 SecurityException.class);
2142 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
2143 SecurityException.class);
2144 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
2145 SecurityException.class);
2146 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
2147 SecurityException.class);
2148 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
2149 SecurityException.class);
2150 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
2151 SecurityException.class);
2153 runWithCaller(LAUNCHER_1, USER_P0, () -> {
2154 assertShortcutIds(assertAllPinned(
2155 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2156 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2158 assertShortcutIds(assertAllDynamic(
2159 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2160 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2162 assertShortcutIds(assertAllDynamicOrPinned(
2163 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2164 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2167 assertShortcutIds(assertAllPinned(
2168 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2169 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2171 assertShortcutIds(assertAllDynamic(
2172 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2173 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2175 assertShortcutIds(assertAllDynamicOrPinned(
2176 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2177 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2180 assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
2181 assertShortcutLaunchable(CALLING_PACKAGE_1, "s2", USER_0);
2182 assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_0);
2184 assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
2185 assertShortcutLaunchable(CALLING_PACKAGE_2, "s2", USER_0);
2186 assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2188 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
2189 SecurityException.class);
2190 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
2191 SecurityException.class);
2192 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
2193 SecurityException.class);
2194 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
2195 SecurityException.class);
2196 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
2197 SecurityException.class);
2198 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
2199 SecurityException.class);
2201 runWithCaller(LAUNCHER_2, USER_P0, () -> {
2202 assertShortcutIds(assertAllPinned(
2203 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2204 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2206 assertShortcutIds(assertAllDynamic(
2207 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2208 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2210 assertShortcutIds(assertAllDynamicOrPinned(
2211 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2212 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2215 assertShortcutIds(assertAllPinned(
2216 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2217 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2219 assertShortcutIds(assertAllDynamic(
2220 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2221 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2223 assertShortcutIds(assertAllDynamicOrPinned(
2224 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2225 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2228 assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
2229 assertShortcutLaunchable(CALLING_PACKAGE_1, "s2", USER_0);
2230 assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_0);
2232 assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
2233 assertShortcutLaunchable(CALLING_PACKAGE_2, "s2", USER_0);
2234 assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2236 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
2237 SecurityException.class);
2238 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
2239 SecurityException.class);
2240 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
2241 SecurityException.class);
2242 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
2243 SecurityException.class);
2244 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
2245 SecurityException.class);
2246 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
2247 SecurityException.class);
2249 runWithCaller(LAUNCHER_2, USER_10, () -> {
2250 assertShortcutIds(assertAllPinned(
2251 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2252 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_10)),
2254 assertShortcutIds(assertAllDynamic(
2255 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2256 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_10)),
2257 "s1", "s2", "s3", "s4", "s5", "s6");
2258 assertShortcutIds(assertAllDynamicOrPinned(
2259 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2260 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_10)),
2261 "s1", "s2", "s3", "s4", "s5", "s6");
2264 // Remove some dynamic shortcuts.
2266 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2267 assertTrue(mManager.setDynamicShortcuts(list(
2268 makeShortcut("s1"))));
2270 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
2271 assertTrue(mManager.setDynamicShortcuts(list(
2272 makeShortcut("s1"))));
2274 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
2275 assertTrue(mManager.setDynamicShortcuts(list(
2276 makeShortcut("s1"))));
2279 runWithCaller(LAUNCHER_1, USER_0, () -> {
2280 assertShortcutIds(assertAllPinned(
2281 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2282 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2284 assertShortcutIds(assertAllDynamic(
2285 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2286 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2288 assertShortcutIds(assertAllDynamicOrPinned(
2289 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2290 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2293 assertShortcutIds(assertAllPinned(
2294 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2295 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2297 assertShortcutIds(assertAllDynamic(
2298 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2299 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2301 assertShortcutIds(assertAllDynamicOrPinned(
2302 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2303 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2306 assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
2307 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_0,
2308 ActivityNotFoundException.class);
2309 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_0,
2310 ActivityNotFoundException.class);
2312 assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
2313 assertShortcutLaunchable(CALLING_PACKAGE_2, "s2", USER_0);
2314 assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2316 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
2317 SecurityException.class);
2318 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
2319 SecurityException.class);
2320 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
2321 SecurityException.class);
2322 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
2323 SecurityException.class);
2324 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
2325 SecurityException.class);
2326 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
2327 SecurityException.class);
2329 runWithCaller(LAUNCHER_1, USER_P0, () -> {
2330 assertShortcutIds(assertAllPinned(
2331 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2332 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2334 assertShortcutIds(assertAllDynamic(
2335 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2336 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2338 assertShortcutIds(assertAllDynamicOrPinned(
2339 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2340 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2343 assertShortcutIds(assertAllPinned(
2344 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2345 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2347 assertShortcutIds(assertAllDynamic(
2348 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2349 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2351 assertShortcutIds(assertAllDynamicOrPinned(
2352 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2353 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2356 assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
2357 assertShortcutLaunchable(CALLING_PACKAGE_1, "s2", USER_0);
2358 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_0,
2359 ActivityNotFoundException.class);
2361 assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
2362 assertShortcutLaunchable(CALLING_PACKAGE_2, "s2", USER_0);
2363 assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2365 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
2366 SecurityException.class);
2367 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
2368 SecurityException.class);
2369 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
2370 SecurityException.class);
2371 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
2372 SecurityException.class);
2373 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
2374 SecurityException.class);
2375 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
2376 SecurityException.class);
2378 runWithCaller(LAUNCHER_2, USER_P0, () -> {
2379 assertShortcutIds(assertAllPinned(
2380 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2381 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2383 assertShortcutIds(assertAllDynamic(
2384 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2385 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2387 assertShortcutIds(assertAllDynamicOrPinned(
2388 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2389 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2392 assertShortcutIds(assertAllPinned(
2393 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2394 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2396 assertShortcutIds(assertAllDynamic(
2397 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2398 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2400 assertShortcutIds(assertAllDynamicOrPinned(
2401 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2402 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2405 assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
2406 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_0,
2407 ActivityNotFoundException.class);
2408 assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_0);
2410 assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
2411 assertStartShortcutThrowsException(CALLING_PACKAGE_2, "s2", USER_0,
2412 ActivityNotFoundException.class);
2413 assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2415 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
2416 SecurityException.class);
2417 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
2418 SecurityException.class);
2419 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
2420 SecurityException.class);
2421 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
2422 SecurityException.class);
2423 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
2424 SecurityException.class);
2425 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
2426 SecurityException.class);
2428 runWithCaller(LAUNCHER_2, USER_10, () -> {
2429 assertShortcutIds(assertAllPinned(
2430 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2431 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_10)),
2433 assertShortcutIds(assertAllDynamic(
2434 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2435 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_10)),
2437 assertShortcutIds(assertAllDynamicOrPinned(
2438 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2439 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_10)),
2442 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_0,
2443 SecurityException.class);
2444 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_0,
2445 SecurityException.class);
2446 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_0,
2447 SecurityException.class);
2449 assertStartShortcutThrowsException(CALLING_PACKAGE_2, "s1", USER_0,
2450 SecurityException.class);
2451 assertStartShortcutThrowsException(CALLING_PACKAGE_2, "s2", USER_0,
2452 SecurityException.class);
2453 assertStartShortcutThrowsException(CALLING_PACKAGE_2, "s3", USER_0,
2454 SecurityException.class);
2456 assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_10);
2457 assertShortcutLaunchable(CALLING_PACKAGE_1, "s2", USER_10);
2458 assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_10);
2459 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
2460 ActivityNotFoundException.class);
2461 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
2462 ActivityNotFoundException.class);
2463 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
2464 ActivityNotFoundException.class);
2467 // Save & load and make sure we still have the same information.
2468 mService.saveDirtyInfo();
2470 mService.handleUnlockUser(USER_0);
2472 runWithCaller(LAUNCHER_1, USER_0, () -> {
2473 assertShortcutIds(assertAllPinned(
2474 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2475 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2477 assertShortcutIds(assertAllDynamic(
2478 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2479 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2481 assertShortcutIds(assertAllDynamicOrPinned(
2482 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2483 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2486 assertShortcutIds(assertAllPinned(
2487 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2488 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2490 assertShortcutIds(assertAllDynamic(
2491 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2492 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2494 assertShortcutIds(assertAllDynamicOrPinned(
2495 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2496 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2499 assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
2500 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_0,
2501 ActivityNotFoundException.class);
2502 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_0,
2503 ActivityNotFoundException.class);
2505 assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
2506 assertShortcutLaunchable(CALLING_PACKAGE_2, "s2", USER_0);
2507 assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2509 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
2510 SecurityException.class);
2511 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
2512 SecurityException.class);
2513 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
2514 SecurityException.class);
2515 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
2516 SecurityException.class);
2517 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
2518 SecurityException.class);
2519 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
2520 SecurityException.class);
2522 runWithCaller(LAUNCHER_1, USER_P0, () -> {
2523 assertShortcutIds(assertAllPinned(
2524 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2525 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2527 assertShortcutIds(assertAllDynamic(
2528 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2529 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2531 assertShortcutIds(assertAllDynamicOrPinned(
2532 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2533 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2536 assertShortcutIds(assertAllPinned(
2537 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2538 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2540 assertShortcutIds(assertAllDynamic(
2541 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2542 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2544 assertShortcutIds(assertAllDynamicOrPinned(
2545 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2546 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2549 assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
2550 assertShortcutLaunchable(CALLING_PACKAGE_1, "s2", USER_0);
2551 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_0,
2552 ActivityNotFoundException.class);
2554 assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
2555 assertShortcutLaunchable(CALLING_PACKAGE_2, "s2", USER_0);
2556 assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2558 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
2559 SecurityException.class);
2560 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
2561 SecurityException.class);
2562 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
2563 SecurityException.class);
2564 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
2565 SecurityException.class);
2566 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
2567 SecurityException.class);
2568 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
2569 SecurityException.class);
2571 runWithCaller(LAUNCHER_2, USER_P0, () -> {
2572 assertShortcutIds(assertAllPinned(
2573 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2574 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2576 assertShortcutIds(assertAllDynamic(
2577 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2578 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2580 assertShortcutIds(assertAllDynamicOrPinned(
2581 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2582 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2585 assertShortcutIds(assertAllPinned(
2586 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2587 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2589 assertShortcutIds(assertAllDynamic(
2590 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2591 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2593 assertShortcutIds(assertAllDynamicOrPinned(
2594 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2595 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2598 assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
2599 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_0,
2600 ActivityNotFoundException.class);
2601 assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_0);
2603 assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
2604 assertStartShortcutThrowsException(CALLING_PACKAGE_2, "s2", USER_0,
2605 ActivityNotFoundException.class);
2606 assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2608 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
2609 SecurityException.class);
2610 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
2611 SecurityException.class);
2612 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
2613 SecurityException.class);
2614 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
2615 SecurityException.class);
2616 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
2617 SecurityException.class);
2618 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
2619 SecurityException.class);
2623 public void testStartShortcut() {
2624 // Create some shortcuts.
2625 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2626 final ShortcutInfo s1_1 = makeShortcut(
2629 makeComponent(ShortcutActivity.class),
2631 new Intent[] {makeIntent(Intent.ACTION_ASSIST, ShortcutActivity2.class,
2632 "key1", "val1", "nest", makeBundle("key", 123))
2633 .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK),
2634 new Intent("act2").setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)},
2637 final ShortcutInfo s1_2 = makeShortcut(
2640 /* activity */ null,
2642 makeIntent(Intent.ACTION_ASSIST, ShortcutActivity3.class),
2645 final ShortcutInfo s1_3 = makeShortcut("s3");
2647 assertTrue(mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3)));
2650 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
2651 final ShortcutInfo s2_1 = makeShortcut(
2654 makeComponent(ShortcutActivity.class),
2656 makeIntent(Intent.ACTION_ANSWER, ShortcutActivity.class,
2657 "key1", "val1", "nest", makeBundle("key", 123)),
2659 assertTrue(mManager.setDynamicShortcuts(list(s2_1)));
2663 runWithCaller(LAUNCHER_1, USER_0, () -> {
2664 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
2665 list("s1", "s2"), getCallingUser());
2667 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
2668 list("s1"), getCallingUser());
2671 // Just to make it complicated, delete some.
2672 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2673 mManager.removeDynamicShortcuts(list("s2"));
2676 runWithCaller(LAUNCHER_1, USER_0, () -> {
2677 final Intent[] intents = launchShortcutAndGetIntents(CALLING_PACKAGE_1, "s1", USER_0);
2678 assertEquals(ShortcutActivity2.class.getName(),
2679 intents[0].getComponent().getClassName());
2680 assertEquals(Intent.ACTION_ASSIST,
2681 intents[0].getAction());
2682 assertEquals(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK,
2683 intents[0].getFlags());
2685 assertEquals("act2",
2686 intents[1].getAction());
2687 assertEquals(Intent.FLAG_ACTIVITY_NO_ANIMATION,
2688 intents[1].getFlags());
2691 ShortcutActivity3.class.getName(),
2692 launchShortcutAndGetIntent(CALLING_PACKAGE_1, "s2", USER_0)
2693 .getComponent().getClassName());
2695 ShortcutActivity.class.getName(),
2696 launchShortcutAndGetIntent(CALLING_PACKAGE_2, "s1", USER_0)
2697 .getComponent().getClassName());
2699 assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_0);
2701 assertShortcutNotLaunched("no-such-package", "s2", USER_0);
2702 assertShortcutNotLaunched(CALLING_PACKAGE_1, "xxxx", USER_0);
2705 // LAUNCHER_1 is no longer the default launcher
2706 setDefaultLauncherChecker((pkg, userId) -> false);
2708 runWithCaller(LAUNCHER_1, USER_0, () -> {
2709 // Not the default launcher, but pinned shortcuts are still lauchable.
2710 final Intent[] intents = launchShortcutAndGetIntents(CALLING_PACKAGE_1, "s1", USER_0);
2711 assertEquals(ShortcutActivity2.class.getName(),
2712 intents[0].getComponent().getClassName());
2713 assertEquals(Intent.ACTION_ASSIST,
2714 intents[0].getAction());
2715 assertEquals(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK,
2716 intents[0].getFlags());
2718 assertEquals("act2",
2719 intents[1].getAction());
2720 assertEquals(Intent.FLAG_ACTIVITY_NO_ANIMATION,
2721 intents[1].getFlags());
2723 ShortcutActivity3.class.getName(),
2724 launchShortcutAndGetIntent(CALLING_PACKAGE_1, "s2", USER_0)
2725 .getComponent().getClassName());
2727 ShortcutActivity.class.getName(),
2728 launchShortcutAndGetIntent(CALLING_PACKAGE_2, "s1", USER_0)
2729 .getComponent().getClassName());
2731 // Not pinned, so not lauchable.
2734 // Test inner errors.
2735 runWithCaller(LAUNCHER_1, USER_0, () -> {
2737 doReturn(ActivityManager.START_CLASS_NOT_FOUND)
2738 .when(mMockActivityManagerInternal).startActivitiesAsPackage(
2739 anyString(), anyInt(), any(Intent[].class), any(Bundle.class));
2740 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_0,
2741 ActivityNotFoundException.class);
2743 // Still not launchable.
2744 doReturn(ActivityManager.START_CLASS_NOT_FOUND)
2745 .when(mMockActivityManagerInternal)
2746 .startActivitiesAsPackage(
2747 anyString(), anyInt(), any(Intent[].class), any(Bundle.class));
2748 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_0,
2749 ActivityNotFoundException.class);
2753 // TODO Check extra, etc
2756 public void testLauncherCallback() throws Throwable {
2757 // Disable throttling for this test.
2758 mService.updateConfigurationLocked(
2759 ConfigConstants.KEY_MAX_UPDATES_PER_INTERVAL + "=99999999,"
2760 + ConfigConstants.KEY_MAX_SHORTCUTS + "=99999999"
2763 setCaller(LAUNCHER_1, USER_0);
2765 assertForLauncherCallback(mLauncherApps, () -> {
2766 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2767 assertTrue(mManager.setDynamicShortcuts(list(
2768 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2770 }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
2771 .haveIds("s1", "s2", "s3")
2772 .areAllWithKeyFieldsOnly()
2775 // From different package.
2776 assertForLauncherCallback(mLauncherApps, () -> {
2777 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
2778 assertTrue(mManager.setDynamicShortcuts(list(
2779 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2781 }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_2, HANDLE_USER_0)
2782 .haveIds("s1", "s2", "s3")
2783 .areAllWithKeyFieldsOnly()
2786 mRunningUsers.put(USER_10, true);
2788 // Different user, callback shouldn't be called.
2789 assertForLauncherCallback(mLauncherApps, () -> {
2790 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
2791 assertTrue(mManager.setDynamicShortcuts(list(
2792 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2794 }).assertNoCallbackCalled();
2797 // Test for addDynamicShortcuts.
2798 assertForLauncherCallback(mLauncherApps, () -> {
2799 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2800 assertTrue(mManager.addDynamicShortcuts(list(makeShortcut("s4"))));
2802 }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
2803 .haveIds("s1", "s2", "s3", "s4")
2804 .areAllWithKeyFieldsOnly()
2808 assertForLauncherCallback(mLauncherApps, () -> {
2809 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2810 mManager.removeDynamicShortcuts(list("s1"));
2812 }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
2813 .haveIds("s2", "s3", "s4")
2814 .areAllWithKeyFieldsOnly()
2818 assertForLauncherCallback(mLauncherApps, () -> {
2819 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2820 assertTrue(mManager.updateShortcuts(list(
2821 makeShortcut("s1"), makeShortcut("s2"))));
2823 }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
2824 // All remaining shortcuts will be passed regardless of what's been updated.
2825 .haveIds("s2", "s3", "s4")
2826 .areAllWithKeyFieldsOnly()
2829 // Test for deleteAll
2830 assertForLauncherCallback(mLauncherApps, () -> {
2831 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2832 mManager.removeAllDynamicShortcuts();
2834 }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
2837 // Update package1 with manifest shortcuts
2838 assertForLauncherCallback(mLauncherApps, () -> {
2839 addManifestShortcutResource(
2840 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
2842 updatePackageVersion(CALLING_PACKAGE_1, 1);
2843 mService.mPackageMonitor.onReceive(getTestContext(),
2844 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
2845 }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
2847 .areAllWithKeyFieldsOnly()
2848 .haveIds("ms1", "ms2");
2850 // Make sure pinned shortcuts are passed too.
2851 // 1. Add dynamic shortcuts.
2852 runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
2853 assertTrue(mManager.setDynamicShortcuts(list(
2854 makeShortcut("s1"), makeShortcut("s2"))));
2858 runWithCaller(LAUNCHER_1, UserHandle.USER_SYSTEM, () -> {
2859 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms2", "s2"), HANDLE_USER_0);
2861 runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
2862 assertWith(getCallerShortcuts())
2863 .haveIds("ms1", "ms2", "s1", "s2")
2866 .selectByIds("ms1", "ms2")
2869 .revertToOriginalList()
2870 .selectByIds("s1", "s2")
2875 // 3 Update the app with no manifest shortcuts. (Pinned one will survive.)
2876 addManifestShortcutResource(
2877 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
2879 updatePackageVersion(CALLING_PACKAGE_1, 1);
2880 mService.mPackageMonitor.onReceive(getTestContext(),
2881 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
2883 assertForLauncherCallback(mLauncherApps, () -> {
2884 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2885 mManager.removeDynamicShortcuts(list("s2"));
2887 assertWith(getCallerShortcuts())
2888 .haveIds("ms2", "s1", "s2")
2891 .areAllNotManifest()
2896 .revertToOriginalList()
2902 .revertToOriginalList()
2909 }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
2910 .haveIds("ms2", "s1", "s2")
2911 .areAllWithKeyFieldsOnly();
2913 // Remove CALLING_PACKAGE_2
2914 assertForLauncherCallback(mLauncherApps, () -> {
2915 uninstallPackage(USER_0, CALLING_PACKAGE_2);
2916 mService.cleanUpPackageLocked(CALLING_PACKAGE_2, USER_0, USER_0,
2917 /* appStillExists = */ false);
2918 }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_2, HANDLE_USER_0)
2922 public void testLauncherCallback_crossProfile() throws Throwable {
2923 prepareCrossProfileDataSet();
2925 final Handler h = new Handler(Looper.getMainLooper());
2927 final LauncherApps.Callback c0_1 = mock(LauncherApps.Callback.class);
2928 final LauncherApps.Callback c0_2 = mock(LauncherApps.Callback.class);
2929 final LauncherApps.Callback c0_3 = mock(LauncherApps.Callback.class);
2930 final LauncherApps.Callback c0_4 = mock(LauncherApps.Callback.class);
2932 final LauncherApps.Callback cP0_1 = mock(LauncherApps.Callback.class);
2933 final LauncherApps.Callback c10_1 = mock(LauncherApps.Callback.class);
2934 final LauncherApps.Callback c10_2 = mock(LauncherApps.Callback.class);
2935 final LauncherApps.Callback c11_1 = mock(LauncherApps.Callback.class);
2937 final List<LauncherApps.Callback> all =
2938 list(c0_1, c0_2, c0_3, c0_4, cP0_1, c10_1, c11_1);
2940 setDefaultLauncherChecker((pkg, userId) -> {
2943 return LAUNCHER_2.equals(pkg);
2945 return LAUNCHER_1.equals(pkg);
2947 return LAUNCHER_1.equals(pkg);
2949 return LAUNCHER_1.equals(pkg);
2955 runWithCaller(LAUNCHER_1, USER_0, () -> mLauncherApps.registerCallback(c0_1, h));
2956 runWithCaller(LAUNCHER_2, USER_0, () -> mLauncherApps.registerCallback(c0_2, h));
2957 runWithCaller(LAUNCHER_3, USER_0, () -> mLauncherApps.registerCallback(c0_3, h));
2958 runWithCaller(LAUNCHER_4, USER_0, () -> mLauncherApps.registerCallback(c0_4, h));
2959 runWithCaller(LAUNCHER_1, USER_P0, () -> mLauncherApps.registerCallback(cP0_1, h));
2960 runWithCaller(LAUNCHER_1, USER_10, () -> mLauncherApps.registerCallback(c10_1, h));
2961 runWithCaller(LAUNCHER_2, USER_10, () -> mLauncherApps.registerCallback(c10_2, h));
2962 runWithCaller(LAUNCHER_1, USER_11, () -> mLauncherApps.registerCallback(c11_1, h));
2967 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2968 mManager.removeDynamicShortcuts(list());
2972 assertCallbackNotReceived(c0_1);
2973 assertCallbackNotReceived(c0_3);
2974 assertCallbackNotReceived(c0_4);
2975 assertCallbackNotReceived(c10_1);
2976 assertCallbackNotReceived(c10_2);
2977 assertCallbackNotReceived(c11_1);
2978 assertCallbackReceived(c0_2, HANDLE_USER_0, CALLING_PACKAGE_1, "s1", "s2", "s3");
2979 assertCallbackReceived(cP0_1, HANDLE_USER_0, CALLING_PACKAGE_1, "s1", "s2", "s3", "s4");
2981 // User 0, different package.
2984 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
2985 mManager.removeDynamicShortcuts(list());
2989 assertCallbackNotReceived(c0_1);
2990 assertCallbackNotReceived(c0_3);
2991 assertCallbackNotReceived(c0_4);
2992 assertCallbackNotReceived(c10_1);
2993 assertCallbackNotReceived(c10_2);
2994 assertCallbackNotReceived(c11_1);
2995 assertCallbackReceived(c0_2, HANDLE_USER_0, CALLING_PACKAGE_3, "s1", "s2", "s3", "s4");
2996 assertCallbackReceived(cP0_1, HANDLE_USER_0, CALLING_PACKAGE_3,
2997 "s1", "s2", "s3", "s4", "s5", "s6");
3001 runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
3002 mManager.removeDynamicShortcuts(list());
3006 assertCallbackNotReceived(c0_1);
3007 assertCallbackNotReceived(c0_3);
3008 assertCallbackNotReceived(c0_4);
3009 assertCallbackNotReceived(c10_1);
3010 assertCallbackNotReceived(c10_2);
3011 assertCallbackNotReceived(c11_1);
3012 assertCallbackReceived(c0_2, HANDLE_USER_P0, CALLING_PACKAGE_1, "s1", "s2", "s3", "s5");
3013 assertCallbackReceived(cP0_1, HANDLE_USER_P0, CALLING_PACKAGE_1, "s1", "s2", "s3", "s4");
3015 // Normal secondary user.
3016 mRunningUsers.put(USER_10, true);
3019 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
3020 mManager.removeDynamicShortcuts(list());
3024 assertCallbackNotReceived(c0_1);
3025 assertCallbackNotReceived(c0_2);
3026 assertCallbackNotReceived(c0_3);
3027 assertCallbackNotReceived(c0_4);
3028 assertCallbackNotReceived(cP0_1);
3029 assertCallbackNotReceived(c10_2);
3030 assertCallbackNotReceived(c11_1);
3031 assertCallbackReceived(c10_1, HANDLE_USER_10, CALLING_PACKAGE_1,
3032 "x1", "x2", "x3", "x4", "x5");
3035 // === Test for persisting ===
3037 public void testSaveAndLoadUser_empty() {
3038 assertTrue(mManager.setDynamicShortcuts(list()));
3040 Log.i(TAG, "Saved state");
3045 mService.saveDirtyInfo();
3048 assertEquals(0, mManager.getDynamicShortcuts().size());
3052 * Try save and load, also stop/start the user.
3054 public void testSaveAndLoadUser() {
3055 // First, create some shortcuts and save.
3056 runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
3057 final Icon icon1 = Icon.createWithResource(getTestContext(), R.drawable.black_64x16);
3058 final Icon icon2 = Icon.createWithBitmap(BitmapFactory.decodeResource(
3059 getTestContext().getResources(), R.drawable.icon2));
3061 final ShortcutInfo si1 = makeShortcut(
3064 makeComponent(ShortcutActivity.class),
3066 makeIntent(Intent.ACTION_ASSIST, ShortcutActivity2.class,
3067 "key1", "val1", "nest", makeBundle("key", 123)),
3070 final ShortcutInfo si2 = makeShortcut(
3073 /* activity */ null,
3075 makeIntent(Intent.ACTION_ASSIST, ShortcutActivity3.class),
3078 assertTrue(mManager.setDynamicShortcuts(list(si1, si2)));
3080 assertEquals(START_TIME + INTERVAL, mManager.getRateLimitResetTime());
3081 assertEquals(2, mManager.getRemainingCallCount());
3083 runWithCaller(CALLING_PACKAGE_2, UserHandle.USER_SYSTEM, () -> {
3084 final Icon icon1 = Icon.createWithResource(getTestContext(), R.drawable.black_16x64);
3085 final Icon icon2 = Icon.createWithBitmap(BitmapFactory.decodeResource(
3086 getTestContext().getResources(), R.drawable.icon2));
3088 final ShortcutInfo si1 = makeShortcut(
3091 makeComponent(ShortcutActivity.class),
3093 makeIntent(Intent.ACTION_ASSIST, ShortcutActivity2.class,
3094 "key1", "val1", "nest", makeBundle("key", 123)),
3097 final ShortcutInfo si2 = makeShortcut(
3100 /* activity */ null,
3102 makeIntent(Intent.ACTION_ASSIST, ShortcutActivity3.class),
3105 assertTrue(mManager.setDynamicShortcuts(list(si1, si2)));
3107 assertEquals(START_TIME + INTERVAL, mManager.getRateLimitResetTime());
3108 assertEquals(2, mManager.getRemainingCallCount());
3111 mRunningUsers.put(USER_10, true);
3113 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
3114 final Icon icon1 = Icon.createWithResource(getTestContext(), R.drawable.black_64x64);
3115 final Icon icon2 = Icon.createWithBitmap(BitmapFactory.decodeResource(
3116 getTestContext().getResources(), R.drawable.icon2));
3118 final ShortcutInfo si1 = makeShortcut(
3121 makeComponent(ShortcutActivity.class),
3123 makeIntent(Intent.ACTION_ASSIST, ShortcutActivity2.class,
3124 "key1", "val1", "nest", makeBundle("key", 123)),
3127 final ShortcutInfo si2 = makeShortcut(
3130 /* activity */ null,
3132 makeIntent(Intent.ACTION_ASSIST, ShortcutActivity3.class),
3135 assertTrue(mManager.setDynamicShortcuts(list(si1, si2)));
3137 assertEquals(START_TIME + INTERVAL, mManager.getRateLimitResetTime());
3138 assertEquals(2, mManager.getRemainingCallCount());
3141 mService.getShortcutsForTest().get(UserHandle.USER_SYSTEM).setLauncher(
3142 new ComponentName("pkg1", "class"));
3145 mService.saveDirtyInfo();
3148 // Before the load, the map should be empty.
3149 assertEquals(0, mService.getShortcutsForTest().size());
3151 // this will pre-load the per-user info.
3152 mService.handleUnlockUser(UserHandle.USER_SYSTEM);
3155 assertEquals(1, mService.getShortcutsForTest().size());
3157 runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
3158 assertShortcutIds(assertAllDynamic(assertAllHaveIntents(assertAllHaveIcon(
3159 mManager.getDynamicShortcuts()))), "s1", "s2");
3160 assertEquals(2, mManager.getRemainingCallCount());
3162 assertEquals("title1-1", getCallerShortcut("s1").getTitle());
3163 assertEquals("title1-2", getCallerShortcut("s2").getTitle());
3165 runWithCaller(CALLING_PACKAGE_2, UserHandle.USER_SYSTEM, () -> {
3166 assertShortcutIds(assertAllDynamic(assertAllHaveIntents(assertAllHaveIcon(
3167 mManager.getDynamicShortcuts()))), "s1", "s2");
3168 assertEquals(2, mManager.getRemainingCallCount());
3170 assertEquals("title2-1", getCallerShortcut("s1").getTitle());
3171 assertEquals("title2-2", getCallerShortcut("s2").getTitle());
3174 assertEquals("pkg1", mService.getShortcutsForTest().get(UserHandle.USER_SYSTEM)
3175 .getLastKnownLauncher().getPackageName());
3177 // Start another user
3178 mService.handleUnlockUser(USER_10);
3180 // Now the size is 2.
3181 assertEquals(2, mService.getShortcutsForTest().size());
3183 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
3184 assertShortcutIds(assertAllDynamic(assertAllHaveIntents(assertAllHaveIcon(
3185 mManager.getDynamicShortcuts()))), "s1", "s2");
3186 assertEquals(2, mManager.getRemainingCallCount());
3188 assertEquals("title10-1-1", getCallerShortcut("s1").getTitle());
3189 assertEquals("title10-1-2", getCallerShortcut("s2").getTitle());
3191 assertNull(mService.getShortcutsForTest().get(USER_10).getLastKnownLauncher());
3193 // Try stopping the user
3194 mService.handleCleanupUser(USER_10);
3196 // Now it's unloaded.
3197 assertEquals(1, mService.getShortcutsForTest().size());
3199 // TODO Check all other fields
3202 public void testCleanupPackage() {
3203 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3204 assertTrue(mManager.setDynamicShortcuts(list(
3205 makeShortcut("s0_1"))));
3207 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
3208 assertTrue(mManager.setDynamicShortcuts(list(
3209 makeShortcut("s0_2"))));
3211 runWithCaller(LAUNCHER_1, USER_0, () -> {
3212 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s0_1"),
3214 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s0_2"),
3217 runWithCaller(LAUNCHER_2, USER_0, () -> {
3218 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s0_1"),
3220 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s0_2"),
3224 mRunningUsers.put(USER_10, true);
3226 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
3227 assertTrue(mManager.setDynamicShortcuts(list(
3228 makeShortcut("s10_1"))));
3230 runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
3231 assertTrue(mManager.setDynamicShortcuts(list(
3232 makeShortcut("s10_2"))));
3234 runWithCaller(LAUNCHER_1, USER_10, () -> {
3235 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s10_1"),
3237 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s10_2"),
3240 runWithCaller(LAUNCHER_2, USER_10, () -> {
3241 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s10_1"),
3243 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s10_2"),
3247 // Remove all dynamic shortcuts; now all shortcuts are just pinned.
3248 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3249 mManager.removeAllDynamicShortcuts();
3251 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
3252 mManager.removeAllDynamicShortcuts();
3254 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
3255 mManager.removeAllDynamicShortcuts();
3257 runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
3258 mManager.removeAllDynamicShortcuts();
3262 final SparseArray<ShortcutUser> users = mService.getShortcutsForTest();
3263 assertEquals(2, users.size());
3264 assertEquals(USER_0, users.keyAt(0));
3265 assertEquals(USER_10, users.keyAt(1));
3267 final ShortcutUser user0 = users.get(USER_0);
3268 final ShortcutUser user10 = users.get(USER_10);
3271 // Check the registered packages.
3273 assertEquals(set(CALLING_PACKAGE_1, CALLING_PACKAGE_2),
3274 hashSet(user0.getAllPackagesForTest().keySet()));
3275 assertEquals(set(CALLING_PACKAGE_1, CALLING_PACKAGE_2),
3276 hashSet(user10.getAllPackagesForTest().keySet()));
3278 set(PackageWithUser.of(USER_0, LAUNCHER_1),
3279 PackageWithUser.of(USER_0, LAUNCHER_2)),
3280 hashSet(user0.getAllLaunchersForTest().keySet()));
3282 set(PackageWithUser.of(USER_10, LAUNCHER_1),
3283 PackageWithUser.of(USER_10, LAUNCHER_2)),
3284 hashSet(user10.getAllLaunchersForTest().keySet()));
3285 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_0),
3287 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_0),
3289 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_10),
3291 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_10),
3293 assertShortcutExists(CALLING_PACKAGE_1, "s0_1", USER_0);
3294 assertShortcutExists(CALLING_PACKAGE_2, "s0_2", USER_0);
3295 assertShortcutExists(CALLING_PACKAGE_1, "s10_1", USER_10);
3296 assertShortcutExists(CALLING_PACKAGE_2, "s10_2", USER_10);
3298 mService.saveDirtyInfo();
3300 // Nonexistent package.
3301 uninstallPackage(USER_0, "abc");
3302 mService.cleanUpPackageLocked("abc", USER_0, USER_0, /* appStillExists = */ false);
3305 assertEquals(set(CALLING_PACKAGE_1, CALLING_PACKAGE_2),
3306 hashSet(user0.getAllPackagesForTest().keySet()));
3307 assertEquals(set(CALLING_PACKAGE_1, CALLING_PACKAGE_2),
3308 hashSet(user10.getAllPackagesForTest().keySet()));
3310 set(PackageWithUser.of(USER_0, LAUNCHER_1),
3311 PackageWithUser.of(USER_0, LAUNCHER_2)),
3312 hashSet(user0.getAllLaunchersForTest().keySet()));
3314 set(PackageWithUser.of(USER_10, LAUNCHER_1),
3315 PackageWithUser.of(USER_10, LAUNCHER_2)),
3316 hashSet(user10.getAllLaunchersForTest().keySet()));
3317 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_0),
3319 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_0),
3321 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_10),
3323 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_10),
3325 assertShortcutExists(CALLING_PACKAGE_1, "s0_1", USER_0);
3326 assertShortcutExists(CALLING_PACKAGE_2, "s0_2", USER_0);
3327 assertShortcutExists(CALLING_PACKAGE_1, "s10_1", USER_10);
3328 assertShortcutExists(CALLING_PACKAGE_2, "s10_2", USER_10);
3330 mService.saveDirtyInfo();
3332 // Remove a package.
3333 uninstallPackage(USER_0, CALLING_PACKAGE_1);
3334 mService.cleanUpPackageLocked(CALLING_PACKAGE_1, USER_0, USER_0,
3335 /* appStillExists = */ false);
3337 assertEquals(set(CALLING_PACKAGE_2),
3338 hashSet(user0.getAllPackagesForTest().keySet()));
3339 assertEquals(set(CALLING_PACKAGE_1, CALLING_PACKAGE_2),
3340 hashSet(user10.getAllPackagesForTest().keySet()));
3342 set(PackageWithUser.of(USER_0, LAUNCHER_1),
3343 PackageWithUser.of(USER_0, LAUNCHER_2)),
3344 hashSet(user0.getAllLaunchersForTest().keySet()));
3346 set(PackageWithUser.of(USER_10, LAUNCHER_1),
3347 PackageWithUser.of(USER_10, LAUNCHER_2)),
3348 hashSet(user10.getAllLaunchersForTest().keySet()));
3349 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_0),
3351 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_0),
3353 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_10),
3355 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_10),
3357 assertShortcutNotExists(CALLING_PACKAGE_1, "s0_1", USER_0);
3358 assertShortcutExists(CALLING_PACKAGE_2, "s0_2", USER_0);
3359 assertShortcutExists(CALLING_PACKAGE_1, "s10_1", USER_10);
3360 assertShortcutExists(CALLING_PACKAGE_2, "s10_2", USER_10);
3362 mService.saveDirtyInfo();
3364 // Remove a launcher.
3365 uninstallPackage(USER_10, LAUNCHER_1);
3366 mService.cleanUpPackageLocked(LAUNCHER_1, USER_10, USER_10, /* appStillExists = */ false);
3368 assertEquals(set(CALLING_PACKAGE_2),
3369 hashSet(user0.getAllPackagesForTest().keySet()));
3370 assertEquals(set(CALLING_PACKAGE_1, CALLING_PACKAGE_2),
3371 hashSet(user10.getAllPackagesForTest().keySet()));
3373 set(PackageWithUser.of(USER_0, LAUNCHER_1),
3374 PackageWithUser.of(USER_0, LAUNCHER_2)),
3375 hashSet(user0.getAllLaunchersForTest().keySet()));
3377 set(PackageWithUser.of(USER_10, LAUNCHER_2)),
3378 hashSet(user10.getAllLaunchersForTest().keySet()));
3379 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_0),
3381 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_0),
3383 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_10),
3385 assertShortcutNotExists(CALLING_PACKAGE_1, "s0_1", USER_0);
3386 assertShortcutExists(CALLING_PACKAGE_2, "s0_2", USER_0);
3387 assertShortcutExists(CALLING_PACKAGE_1, "s10_1", USER_10);
3388 assertShortcutExists(CALLING_PACKAGE_2, "s10_2", USER_10);
3390 mService.saveDirtyInfo();
3392 // Remove a package.
3393 uninstallPackage(USER_10, CALLING_PACKAGE_2);
3394 mService.cleanUpPackageLocked(CALLING_PACKAGE_2, USER_10, USER_10,
3395 /* appStillExists = */ false);
3397 assertEquals(set(CALLING_PACKAGE_2),
3398 hashSet(user0.getAllPackagesForTest().keySet()));
3399 assertEquals(set(CALLING_PACKAGE_1),
3400 hashSet(user10.getAllPackagesForTest().keySet()));
3402 set(PackageWithUser.of(USER_0, LAUNCHER_1),
3403 PackageWithUser.of(USER_0, LAUNCHER_2)),
3404 hashSet(user0.getAllLaunchersForTest().keySet()));
3406 set(PackageWithUser.of(USER_10, LAUNCHER_2)),
3407 hashSet(user10.getAllLaunchersForTest().keySet()));
3408 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_0),
3410 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_0),
3412 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_10),
3414 assertShortcutNotExists(CALLING_PACKAGE_1, "s0_1", USER_0);
3415 assertShortcutExists(CALLING_PACKAGE_2, "s0_2", USER_0);
3416 assertShortcutExists(CALLING_PACKAGE_1, "s10_1", USER_10);
3417 assertShortcutNotExists(CALLING_PACKAGE_2, "s10_2", USER_10);
3419 mService.saveDirtyInfo();
3421 // Remove the other launcher from user 10 too.
3422 uninstallPackage(USER_10, LAUNCHER_2);
3423 mService.cleanUpPackageLocked(LAUNCHER_2, USER_10, USER_10,
3424 /* appStillExists = */ false);
3426 assertEquals(set(CALLING_PACKAGE_2),
3427 hashSet(user0.getAllPackagesForTest().keySet()));
3428 assertEquals(set(CALLING_PACKAGE_1),
3429 hashSet(user10.getAllPackagesForTest().keySet()));
3431 set(PackageWithUser.of(USER_0, LAUNCHER_1),
3432 PackageWithUser.of(USER_0, LAUNCHER_2)),
3433 hashSet(user0.getAllLaunchersForTest().keySet()));
3436 hashSet(user10.getAllLaunchersForTest().keySet()));
3437 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_0),
3439 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_0),
3442 // Note the pinned shortcuts on user-10 no longer referred, so they should both be removed.
3443 assertShortcutNotExists(CALLING_PACKAGE_1, "s0_1", USER_0);
3444 assertShortcutExists(CALLING_PACKAGE_2, "s0_2", USER_0);
3445 assertShortcutNotExists(CALLING_PACKAGE_1, "s10_1", USER_10);
3446 assertShortcutNotExists(CALLING_PACKAGE_2, "s10_2", USER_10);
3448 mService.saveDirtyInfo();
3451 uninstallPackage(USER_10, CALLING_PACKAGE_1);
3452 mService.cleanUpPackageLocked(CALLING_PACKAGE_1, USER_10, USER_10,
3453 /* appStillExists = */ false);
3455 assertEquals(set(CALLING_PACKAGE_2),
3456 hashSet(user0.getAllPackagesForTest().keySet()));
3458 hashSet(user10.getAllPackagesForTest().keySet()));
3460 set(PackageWithUser.of(USER_0, LAUNCHER_1),
3461 PackageWithUser.of(USER_0, LAUNCHER_2)),
3462 hashSet(user0.getAllLaunchersForTest().keySet()));
3464 hashSet(user10.getAllLaunchersForTest().keySet()));
3465 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_0),
3467 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_0),
3470 // Note the pinned shortcuts on user-10 no longer referred, so they should both be removed.
3471 assertShortcutNotExists(CALLING_PACKAGE_1, "s0_1", USER_0);
3472 assertShortcutExists(CALLING_PACKAGE_2, "s0_2", USER_0);
3473 assertShortcutNotExists(CALLING_PACKAGE_1, "s10_1", USER_10);
3474 assertShortcutNotExists(CALLING_PACKAGE_2, "s10_2", USER_10);
3476 mService.saveDirtyInfo();
3479 public void testCleanupPackage_republishManifests() {
3480 addManifestShortcutResource(
3481 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
3483 updatePackageVersion(CALLING_PACKAGE_1, 1);
3484 mService.mPackageMonitor.onReceive(getTestContext(),
3485 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
3487 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3488 assertTrue(mManager.setDynamicShortcuts(list(
3489 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
3491 runWithCaller(LAUNCHER_1, USER_0, () -> {
3492 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
3493 list("s2", "s3", "ms1", "ms2"), HANDLE_USER_0);
3496 // Remove ms2 from manifest.
3497 addManifestShortcutResource(
3498 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
3500 updatePackageVersion(CALLING_PACKAGE_1, 1);
3501 mService.mPackageMonitor.onReceive(getTestContext(),
3502 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
3504 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3505 assertTrue(mManager.setDynamicShortcuts(list(
3506 makeShortcut("s1"), makeShortcut("s2"))));
3508 // Make sure the shortcuts are in the intended state.
3509 assertWith(getCallerShortcuts())
3510 .haveIds("ms1", "ms2", "s1", "s2", "s3")
3516 .revertToOriginalList()
3518 .areAllNotManifest()
3521 .revertToOriginalList()
3526 .revertToOriginalList()
3531 .revertToOriginalList()
3537 // Clean up + re-publish manifests.
3538 mService.cleanUpPackageLocked(CALLING_PACKAGE_1, USER_0, USER_0,
3539 /* appStillExists = */ true);
3540 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3541 assertWith(getCallerShortcuts())
3547 public void testHandleGonePackage_crossProfile() {
3548 // Create some shortcuts.
3549 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3550 assertTrue(mManager.setDynamicShortcuts(list(
3551 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
3553 runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
3554 assertTrue(mManager.setDynamicShortcuts(list(
3555 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
3557 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
3558 assertTrue(mManager.setDynamicShortcuts(list(
3559 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
3562 mRunningUsers.put(USER_10, true);
3564 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
3565 assertTrue(mManager.setDynamicShortcuts(list(
3566 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
3569 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
3570 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
3571 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
3573 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
3574 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
3575 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
3577 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
3578 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
3579 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
3581 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
3582 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
3583 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
3587 runWithCaller(LAUNCHER_1, USER_0, () -> {
3588 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
3589 list("s1"), HANDLE_USER_0);
3591 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
3592 list("s2"), UserHandle.of(USER_P0));
3594 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
3595 list("s3"), HANDLE_USER_0);
3598 runWithCaller(LAUNCHER_1, USER_P0, () -> {
3599 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
3600 list("s2"), HANDLE_USER_0);
3602 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
3603 list("s3"), UserHandle.of(USER_P0));
3605 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
3606 list("s1"), HANDLE_USER_0);
3609 runWithCaller(LAUNCHER_1, USER_10, () -> {
3610 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
3611 list("s3"), HANDLE_USER_10);
3616 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
3617 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
3618 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
3620 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
3621 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
3622 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
3624 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
3625 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
3626 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
3628 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
3629 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
3630 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
3632 // Make sure all the information is persisted.
3633 mService.saveDirtyInfo();
3635 mService.handleUnlockUser(USER_0);
3636 mService.handleUnlockUser(USER_P0);
3637 mService.handleUnlockUser(USER_10);
3639 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
3640 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
3641 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
3643 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
3644 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
3645 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
3647 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
3648 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
3649 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
3651 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
3652 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
3653 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
3655 // Start uninstalling.
3656 uninstallPackage(USER_10, LAUNCHER_1);
3657 mService.checkPackageChanges(USER_10);
3659 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
3660 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
3661 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
3663 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
3664 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
3665 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
3667 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
3668 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
3669 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
3671 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
3672 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
3673 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
3676 uninstallPackage(USER_10, CALLING_PACKAGE_1);
3677 mService.checkPackageChanges(USER_10);
3679 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
3680 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
3681 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
3683 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
3684 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
3685 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
3687 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
3688 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
3689 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
3691 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
3692 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
3693 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
3695 uninstallPackage(USER_P0, LAUNCHER_1);
3696 mService.checkPackageChanges(USER_0);
3698 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
3699 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
3700 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
3702 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
3703 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
3704 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
3706 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
3707 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
3708 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
3710 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
3711 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
3712 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
3714 mService.checkPackageChanges(USER_P0);
3716 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
3717 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
3718 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
3720 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
3721 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
3722 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
3724 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
3725 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
3726 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
3728 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
3729 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
3730 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
3732 uninstallPackage(USER_P0, CALLING_PACKAGE_1);
3734 mService.saveDirtyInfo();
3736 mService.handleUnlockUser(USER_0);
3737 mService.handleUnlockUser(USER_P0);
3738 mService.handleUnlockUser(USER_10);
3740 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
3741 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
3742 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
3744 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
3745 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
3746 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
3748 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
3749 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
3750 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
3752 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
3753 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
3754 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
3757 uninstallPackage(USER_0, LAUNCHER_1);
3759 mService.saveDirtyInfo();
3761 mService.handleUnlockUser(USER_0);
3762 mService.handleUnlockUser(USER_P0);
3763 mService.handleUnlockUser(USER_10);
3765 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
3766 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
3767 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
3769 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
3770 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
3771 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
3773 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
3774 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
3775 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
3777 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
3778 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
3779 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
3781 uninstallPackage(USER_0, CALLING_PACKAGE_2);
3783 mService.saveDirtyInfo();
3785 mService.handleUnlockUser(USER_0);
3786 mService.handleUnlockUser(USER_P0);
3787 mService.handleUnlockUser(USER_10);
3789 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
3790 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
3791 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
3793 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
3794 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
3795 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
3797 assertNull(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
3798 assertNull(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
3799 assertNull(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
3801 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
3802 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
3803 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
3806 protected void checkCanRestoreTo(boolean expected, ShortcutPackageInfo spi,
3807 int version, String... signatures) {
3808 assertEquals(expected, spi.canRestoreTo(mService, genPackage(
3809 "dummy", /* uid */ 0, version, signatures)));
3812 public void testCanRestoreTo() {
3813 addPackage(CALLING_PACKAGE_1, CALLING_UID_1, 10, "sig1");
3814 addPackage(CALLING_PACKAGE_2, CALLING_UID_1, 10, "sig1", "sig2");
3816 final ShortcutPackageInfo spi1 = ShortcutPackageInfo.generateForInstalledPackage(
3817 mService, CALLING_PACKAGE_1, USER_0);
3818 final ShortcutPackageInfo spi2 = ShortcutPackageInfo.generateForInstalledPackage(
3819 mService, CALLING_PACKAGE_2, USER_0);
3821 checkCanRestoreTo(true, spi1, 10, "sig1");
3822 checkCanRestoreTo(true, spi1, 10, "x", "sig1");
3823 checkCanRestoreTo(true, spi1, 10, "sig1", "y");
3824 checkCanRestoreTo(true, spi1, 10, "x", "sig1", "y");
3825 checkCanRestoreTo(true, spi1, 11, "sig1");
3827 checkCanRestoreTo(false, spi1, 10 /* empty */);
3828 checkCanRestoreTo(false, spi1, 10, "x");
3829 checkCanRestoreTo(false, spi1, 10, "x", "y");
3830 checkCanRestoreTo(false, spi1, 10, "x");
3831 checkCanRestoreTo(false, spi1, 9, "sig1");
3833 checkCanRestoreTo(true, spi2, 10, "sig1", "sig2");
3834 checkCanRestoreTo(true, spi2, 10, "sig2", "sig1");
3835 checkCanRestoreTo(true, spi2, 10, "x", "sig1", "sig2");
3836 checkCanRestoreTo(true, spi2, 10, "x", "sig2", "sig1");
3837 checkCanRestoreTo(true, spi2, 10, "sig1", "sig2", "y");
3838 checkCanRestoreTo(true, spi2, 10, "sig2", "sig1", "y");
3839 checkCanRestoreTo(true, spi2, 10, "x", "sig1", "sig2", "y");
3840 checkCanRestoreTo(true, spi2, 10, "x", "sig2", "sig1", "y");
3841 checkCanRestoreTo(true, spi2, 11, "x", "sig2", "sig1", "y");
3843 checkCanRestoreTo(false, spi2, 10, "sig1", "sig2x");
3844 checkCanRestoreTo(false, spi2, 10, "sig2", "sig1x");
3845 checkCanRestoreTo(false, spi2, 10, "x", "sig1x", "sig2");
3846 checkCanRestoreTo(false, spi2, 10, "x", "sig2x", "sig1");
3847 checkCanRestoreTo(false, spi2, 10, "sig1", "sig2x", "y");
3848 checkCanRestoreTo(false, spi2, 10, "sig2", "sig1x", "y");
3849 checkCanRestoreTo(false, spi2, 10, "x", "sig1x", "sig2", "y");
3850 checkCanRestoreTo(false, spi2, 10, "x", "sig2x", "sig1", "y");
3851 checkCanRestoreTo(false, spi2, 11, "x", "sig2x", "sig1", "y");
3854 public void testHandlePackageDelete() {
3855 final Icon bmp32x32 = Icon.createWithBitmap(BitmapFactory.decodeResource(
3856 getTestContext().getResources(), R.drawable.black_32x32));
3857 setCaller(CALLING_PACKAGE_1, USER_0);
3858 assertTrue(mManager.addDynamicShortcuts(list(
3859 makeShortcutWithIcon("s1", bmp32x32), makeShortcutWithIcon("s2", bmp32x32)
3861 // Also add a manifest shortcut, which should be removed too.
3862 addManifestShortcutResource(
3863 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
3865 updatePackageVersion(CALLING_PACKAGE_1, 1);
3866 mService.mPackageMonitor.onReceive(getTestContext(),
3867 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
3868 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3869 assertWith(getCallerShortcuts())
3870 .haveIds("s1", "s2", "ms1")
3876 setCaller(CALLING_PACKAGE_2, USER_0);
3877 assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
3879 setCaller(CALLING_PACKAGE_3, USER_0);
3880 assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
3882 mRunningUsers.put(USER_10, true);
3884 setCaller(CALLING_PACKAGE_1, USER_10);
3885 assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
3887 setCaller(CALLING_PACKAGE_2, USER_10);
3888 assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
3890 setCaller(CALLING_PACKAGE_3, USER_10);
3891 assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
3893 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
3894 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
3895 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));
3896 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
3897 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
3898 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
3900 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
3901 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
3902 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
3903 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
3904 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
3905 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
3907 uninstallPackage(USER_0, CALLING_PACKAGE_1);
3908 mService.mPackageMonitor.onReceive(getTestContext(),
3909 genPackageDeleteIntent(CALLING_PACKAGE_1, USER_0));
3911 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
3912 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
3913 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));
3914 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
3915 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
3916 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
3918 assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
3919 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
3920 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
3921 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
3922 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
3923 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
3925 mRunningUsers.put(USER_10, true);
3927 uninstallPackage(USER_10, CALLING_PACKAGE_2);
3928 mService.mPackageMonitor.onReceive(getTestContext(),
3929 genPackageDeleteIntent(CALLING_PACKAGE_2, USER_10));
3931 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
3932 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
3933 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));
3934 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
3935 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
3936 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
3938 assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
3939 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
3940 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
3941 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
3942 assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
3943 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
3945 mInjectedPackages.remove(CALLING_PACKAGE_1);
3946 mInjectedPackages.remove(CALLING_PACKAGE_3);
3948 mService.checkPackageChanges(USER_0);
3950 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
3951 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
3952 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0)); // ---------------
3953 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
3954 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
3955 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
3957 assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
3958 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
3959 assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
3960 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
3961 assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
3962 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
3964 mService.checkPackageChanges(USER_10);
3966 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
3967 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
3968 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));
3969 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
3970 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
3971 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
3973 assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
3974 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
3975 assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
3976 assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
3977 assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
3978 assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
3981 /** Almost ame as testHandlePackageDelete, except it doesn't uninstall packages. */
3982 public void testHandlePackageClearData() {
3983 final Icon bmp32x32 = Icon.createWithBitmap(BitmapFactory.decodeResource(
3984 getTestContext().getResources(), R.drawable.black_32x32));
3985 setCaller(CALLING_PACKAGE_1, USER_0);
3986 assertTrue(mManager.addDynamicShortcuts(list(
3987 makeShortcutWithIcon("s1", bmp32x32), makeShortcutWithIcon("s2", bmp32x32)
3990 setCaller(CALLING_PACKAGE_2, USER_0);
3991 assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
3993 setCaller(CALLING_PACKAGE_3, USER_0);
3994 assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
3996 mRunningUsers.put(USER_10, true);
3998 setCaller(CALLING_PACKAGE_1, USER_10);
3999 assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
4001 setCaller(CALLING_PACKAGE_2, USER_10);
4002 assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
4004 setCaller(CALLING_PACKAGE_3, USER_10);
4005 assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
4007 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
4008 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
4009 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));
4010 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
4011 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
4012 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
4014 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
4015 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
4016 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
4017 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
4018 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
4019 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
4021 mService.mPackageMonitor.onReceive(getTestContext(),
4022 genPackageDataClear(CALLING_PACKAGE_1, USER_0));
4024 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
4025 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
4026 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));
4027 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
4028 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
4029 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
4031 assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
4032 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
4033 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
4034 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
4035 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
4036 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
4038 mRunningUsers.put(USER_10, true);
4040 mService.mPackageMonitor.onReceive(getTestContext(),
4041 genPackageDataClear(CALLING_PACKAGE_2, USER_10));
4043 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
4044 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
4045 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));
4046 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
4047 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
4048 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
4050 assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
4051 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
4052 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
4053 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
4054 assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
4055 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
4058 public void testHandlePackageClearData_manifestRepublished() {
4060 mRunningUsers.put(USER_10, true);
4062 // Add two manifests and two dynamics.
4063 addManifestShortcutResource(
4064 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
4066 updatePackageVersion(CALLING_PACKAGE_1, 1);
4067 mService.mPackageMonitor.onReceive(getTestContext(),
4068 genPackageAddIntent(CALLING_PACKAGE_1, USER_10));
4070 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4071 assertTrue(mManager.addDynamicShortcuts(list(
4072 makeShortcut("s1"), makeShortcut("s2"))));
4074 runWithCaller(LAUNCHER_1, USER_10, () -> {
4075 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms2", "s2"), HANDLE_USER_10);
4078 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4079 assertWith(getCallerShortcuts())
4080 .haveIds("ms1", "ms2", "s1", "s2")
4084 .haveIds("ms2", "s2");
4088 mService.mPackageMonitor.onReceive(getTestContext(),
4089 genPackageDataClear(CALLING_PACKAGE_1, USER_10));
4091 // Only manifest shortcuts will remain, and are no longer pinned.
4092 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4093 assertWith(getCallerShortcuts())
4094 .haveIds("ms1", "ms2")
4100 public void testHandlePackageUpdate() throws Throwable {
4101 // Set up shortcuts and launchers.
4103 final Icon res32x32 = Icon.createWithResource(getTestContext(), R.drawable.black_32x32);
4104 final Icon bmp32x32 = Icon.createWithBitmap(BitmapFactory.decodeResource(
4105 getTestContext().getResources(), R.drawable.black_32x32));
4107 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4108 assertTrue(mManager.setDynamicShortcuts(list(
4110 makeShortcutWithIcon("s2", res32x32),
4111 makeShortcutWithIcon("s3", res32x32),
4112 makeShortcutWithIcon("s4", bmp32x32))));
4114 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
4115 assertTrue(mManager.setDynamicShortcuts(list(
4117 makeShortcutWithIcon("s2", bmp32x32))));
4119 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
4120 assertTrue(mManager.setDynamicShortcuts(list(
4121 makeShortcutWithIcon("s1", res32x32))));
4124 mRunningUsers.put(USER_10, true);
4126 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4127 assertTrue(mManager.setDynamicShortcuts(list(
4128 makeShortcutWithIcon("s1", res32x32),
4129 makeShortcutWithIcon("s2", res32x32))));
4131 runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
4132 assertTrue(mManager.setDynamicShortcuts(list(
4133 makeShortcutWithIcon("s1", bmp32x32),
4134 makeShortcutWithIcon("s2", bmp32x32))));
4137 LauncherApps.Callback c0 = mock(LauncherApps.Callback.class);
4138 LauncherApps.Callback c10 = mock(LauncherApps.Callback.class);
4140 runWithCaller(LAUNCHER_1, USER_0, () -> {
4141 mLauncherApps.registerCallback(c0, new Handler(Looper.getMainLooper()));
4143 runWithCaller(LAUNCHER_1, USER_10, () -> {
4144 mLauncherApps.registerCallback(c10, new Handler(Looper.getMainLooper()));
4147 mInjectedCurrentTimeMillis = START_TIME + 100;
4149 ArgumentCaptor<List> shortcuts;
4151 // Update the version info for package 1.
4154 updatePackageVersion(CALLING_PACKAGE_1, 1);
4156 // Then send the broadcast, to only user-0.
4157 mService.mPackageMonitor.onReceive(getTestContext(),
4158 genPackageUpdateIntent(CALLING_PACKAGE_1, USER_0));
4162 // User-0 should get the notification.
4163 shortcuts = ArgumentCaptor.forClass(List.class);
4164 verify(c0).onShortcutsChanged(
4165 eq(CALLING_PACKAGE_1),
4166 shortcuts.capture(),
4169 // User-10 shouldn't yet get the notification.
4170 verify(c10, times(0)).onShortcutsChanged(
4171 eq(CALLING_PACKAGE_1),
4173 any(UserHandle.class));
4174 assertShortcutIds(shortcuts.getValue(), "s1", "s2", "s3", "s4");
4175 assertEquals(START_TIME,
4176 findShortcut(shortcuts.getValue(), "s1").getLastChangedTimestamp());
4177 assertEquals(START_TIME + 100,
4178 findShortcut(shortcuts.getValue(), "s2").getLastChangedTimestamp());
4179 assertEquals(START_TIME + 100,
4180 findShortcut(shortcuts.getValue(), "s3").getLastChangedTimestamp());
4181 assertEquals(START_TIME,
4182 findShortcut(shortcuts.getValue(), "s4").getLastChangedTimestamp());
4184 // Next, send unlock even on user-10. Now we scan packages on this user and send a
4185 // notification to the launcher.
4186 mInjectedCurrentTimeMillis = START_TIME + 200;
4188 mRunningUsers.put(USER_10, true);
4189 mUnlockedUsers.put(USER_10, true);
4193 setPackageLastUpdateTime(CALLING_PACKAGE_1, mInjectedCurrentTimeMillis);
4194 mService.handleUnlockUser(USER_10);
4195 mService.checkPackageChanges(USER_10);
4199 shortcuts = ArgumentCaptor.forClass(List.class);
4200 verify(c0, times(0)).onShortcutsChanged(
4201 eq(CALLING_PACKAGE_1),
4203 any(UserHandle.class));
4205 verify(c10).onShortcutsChanged(
4206 eq(CALLING_PACKAGE_1),
4207 shortcuts.capture(),
4208 eq(HANDLE_USER_10));
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());
4217 // Do the same thing for package 2, which doesn't have resource icons.
4218 mInjectedCurrentTimeMillis = START_TIME + 300;
4222 updatePackageVersion(CALLING_PACKAGE_2, 10);
4224 // Then send the broadcast, to only user-0.
4225 mService.mPackageMonitor.onReceive(getTestContext(),
4226 genPackageUpdateIntent(CALLING_PACKAGE_2, USER_0));
4227 mService.checkPackageChanges(USER_10);
4231 verify(c0, times(0)).onShortcutsChanged(
4232 eq(CALLING_PACKAGE_1),
4234 any(UserHandle.class));
4236 verify(c10, times(0)).onShortcutsChanged(
4237 eq(CALLING_PACKAGE_1),
4239 any(UserHandle.class));
4241 // Do the same thing for package 3
4242 mInjectedCurrentTimeMillis = START_TIME + 400;
4246 updatePackageVersion(CALLING_PACKAGE_3, 100);
4248 // Then send the broadcast, to only user-0.
4249 mService.mPackageMonitor.onReceive(getTestContext(),
4250 genPackageUpdateIntent(CALLING_PACKAGE_3, USER_0));
4251 mService.checkPackageChanges(USER_10);
4255 shortcuts = ArgumentCaptor.forClass(List.class);
4256 verify(c0).onShortcutsChanged(
4257 eq(CALLING_PACKAGE_3),
4258 shortcuts.capture(),
4261 // User 10 doesn't have package 3, so no callback.
4262 verify(c10, times(0)).onShortcutsChanged(
4263 eq(CALLING_PACKAGE_3),
4265 any(UserHandle.class));
4267 assertShortcutIds(shortcuts.getValue(), "s1");
4268 assertEquals(START_TIME + 400,
4269 findShortcut(shortcuts.getValue(), "s1").getLastChangedTimestamp());
4273 * Test the case where an updated app has resource IDs changed.
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);
4279 // Set up shortcuts.
4280 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4281 // Note resource strings are not officially supported (they're hidden), but
4284 final ShortcutInfo s1 = new ShortcutInfo.Builder(mClientContext)
4286 .setActivity(makeComponent(ShortcutActivity.class))
4287 .setIntent(new Intent(Intent.ACTION_VIEW))
4289 .setTitleResId(10000)
4290 .setTextResId(10001)
4291 .setDisabledMessageResId(10002)
4294 final ShortcutInfo s2 = new ShortcutInfo.Builder(mClientContext)
4296 .setActivity(makeComponent(ShortcutActivity.class))
4297 .setIntent(new Intent(Intent.ACTION_VIEW))
4299 .setTitleResId(20000)
4302 assertTrue(mManager.setDynamicShortcuts(list(s1, s2)));
4306 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4307 final ShortcutInfo s1 = getCallerShortcut("s1");
4308 final ShortcutInfo s2 = getCallerShortcut("s2");
4310 assertEquals(1000, s1.getIconResourceId());
4311 assertEquals(10000, s1.getTitleResId());
4312 assertEquals(10001, s1.getTextResId());
4313 assertEquals(10002, s1.getDisabledMessageResourceId());
4315 assertEquals(1001, s2.getIconResourceId());
4316 assertEquals(20000, s2.getTitleResId());
4317 assertEquals(0, s2.getTextResId());
4318 assertEquals(0, s2.getDisabledMessageResourceId());
4321 mService.saveDirtyInfo();
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);
4329 // Update the package.
4330 updatePackageVersion(CALLING_PACKAGE_1, 1);
4331 mService.mPackageMonitor.onReceive(getTestContext(),
4332 genPackageUpdateIntent(CALLING_PACKAGE_1, USER_0));
4334 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4335 final ShortcutInfo s1 = getCallerShortcut("s1");
4336 final ShortcutInfo s2 = getCallerShortcut("s2");
4338 assertEquals(1010, s1.getIconResourceId());
4339 assertEquals(10010, s1.getTitleResId());
4340 assertEquals(10011, s1.getTextResId());
4341 assertEquals(10012, s1.getDisabledMessageResourceId());
4343 assertEquals(1011, s2.getIconResourceId());
4344 assertEquals(20010, s2.getTitleResId());
4345 assertEquals(0, s2.getTextResId());
4346 assertEquals(0, s2.getDisabledMessageResourceId());
4350 public void testHandlePackageUpdate_systemAppUpdate() {
4352 // Package1 is a system app. Package 2 is not a system app, so it's not scanned
4353 // in this test at all.
4354 mSystemPackages.add(CALLING_PACKAGE_1);
4356 // Initial state: no shortcuts.
4357 mService.checkPackageChanges(USER_0);
4359 assertEquals(mInjectedCurrentTimeMillis,
4360 mService.getUserShortcutsLocked(USER_0).getLastAppScanTime());
4361 assertEquals(mInjectedBuildFingerprint,
4362 mService.getUserShortcutsLocked(USER_0).getLastAppScanOsFingerprint());
4364 // They have no shortcuts.
4365 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4366 assertWith(getCallerShortcuts())
4370 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
4371 assertWith(getCallerShortcuts())
4376 // Update the packages -- now they have 1 manifest shortcut.
4377 // But checkPackageChanges() don't notice it, since their version code / timestamp haven't
4379 addManifestShortcutResource(
4380 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
4382 addManifestShortcutResource(
4383 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
4385 mInjectedCurrentTimeMillis += 1000;
4386 mService.checkPackageChanges(USER_0);
4388 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4389 assertWith(getCallerShortcuts())
4392 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
4393 assertWith(getCallerShortcuts())
4398 // Update the build finger print. All system apps will be scanned now.
4399 mInjectedBuildFingerprint = "update1";
4400 mInjectedCurrentTimeMillis += 1000;
4401 mService.checkPackageChanges(USER_0);
4403 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4404 assertWith(getCallerShortcuts())
4407 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
4408 assertWith(getCallerShortcuts())
4413 // Update manifest shortcuts.
4414 mInjectedBuildFingerprint = "update2";
4415 addManifestShortcutResource(
4416 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
4418 addManifestShortcutResource(
4419 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
4421 mInjectedCurrentTimeMillis += 1000;
4422 mService.checkPackageChanges(USER_0);
4424 // Fingerprint hasn't changed, so CALLING_PACKAGE_1 wasn't scanned.
4425 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4426 assertWith(getCallerShortcuts())
4429 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
4430 assertWith(getCallerShortcuts())
4434 // Update the fingerprint, but CALLING_PACKAGE_1's version code hasn't changed, so
4435 // still not scanned.
4436 mInjectedBuildFingerprint = "update2";
4437 mInjectedCurrentTimeMillis += 1000;
4438 mService.checkPackageChanges(USER_0);
4440 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4441 assertWith(getCallerShortcuts())
4444 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
4445 assertWith(getCallerShortcuts())
4449 // Now update the version code, so CALLING_PACKAGE_1 is scanned again.
4450 mInjectedBuildFingerprint = "update3";
4451 mInjectedCurrentTimeMillis += 1000;
4452 updatePackageVersion(CALLING_PACKAGE_1, 1);
4453 mService.checkPackageChanges(USER_0);
4455 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4456 assertWith(getCallerShortcuts())
4457 .haveIds("ms1", "ms2");
4459 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
4460 assertWith(getCallerShortcuts())
4464 // Make sure getLastAppScanTime / getLastAppScanOsFingerprint are persisted.
4466 assertEquals(mInjectedCurrentTimeMillis,
4467 mService.getUserShortcutsLocked(USER_0).getLastAppScanTime());
4468 assertEquals(mInjectedBuildFingerprint,
4469 mService.getUserShortcutsLocked(USER_0).getLastAppScanOsFingerprint());
4472 public void testHandlePackageChanged() {
4473 final ComponentName ACTIVITY1 = new ComponentName(CALLING_PACKAGE_1, "act1");
4474 final ComponentName ACTIVITY2 = new ComponentName(CALLING_PACKAGE_1, "act2");
4476 addManifestShortcutResource(ACTIVITY1, R.xml.shortcut_1);
4477 addManifestShortcutResource(ACTIVITY2, R.xml.shortcut_1_alt);
4479 mRunningUsers.put(USER_10, true);
4481 updatePackageVersion(CALLING_PACKAGE_1, 1);
4482 mService.mPackageMonitor.onReceive(getTestContext(),
4483 genPackageAddIntent(CALLING_PACKAGE_1, USER_10));
4485 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4486 assertTrue(mManager.addDynamicShortcuts(list(
4487 makeShortcutWithActivity("s1", ACTIVITY1),
4488 makeShortcutWithActivity("s2", ACTIVITY2)
4491 runWithCaller(LAUNCHER_1, USER_10, () -> {
4492 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms1-alt", "s2"), HANDLE_USER_10);
4495 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4496 assertWith(getCallerShortcuts())
4497 .haveIds("ms1", "ms1-alt", "s1", "s2")
4501 .haveIds("ms1-alt", "s2")
4503 .revertToOriginalList()
4504 .selectByIds("ms1", "s1")
4505 .areAllWithActivity(ACTIVITY1)
4507 .revertToOriginalList()
4508 .selectByIds("ms1-alt", "s2")
4509 .areAllWithActivity(ACTIVITY2)
4513 // First, no changes.
4514 mService.mPackageMonitor.onReceive(getTestContext(),
4515 genPackageChangedIntent(CALLING_PACKAGE_1, USER_10));
4517 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4518 assertWith(getCallerShortcuts())
4519 .haveIds("ms1", "ms1-alt", "s1", "s2")
4523 .haveIds("ms1-alt", "s2")
4525 .revertToOriginalList()
4526 .selectByIds("ms1", "s1")
4527 .areAllWithActivity(ACTIVITY1)
4529 .revertToOriginalList()
4530 .selectByIds("ms1-alt", "s2")
4531 .areAllWithActivity(ACTIVITY2)
4535 // Disable activity 1
4536 mEnabledActivityChecker = (activity, userId) -> !ACTIVITY1.equals(activity);
4537 mService.mPackageMonitor.onReceive(getTestContext(),
4538 genPackageChangedIntent(CALLING_PACKAGE_1, USER_10));
4540 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4541 assertWith(getCallerShortcuts())
4542 .haveIds("ms1-alt", "s2")
4546 .haveIds("ms1-alt", "s2")
4548 .revertToOriginalList()
4549 .selectByIds("ms1-alt", "s2")
4550 .areAllWithActivity(ACTIVITY2)
4554 // Re-enable activity 1.
4555 // Manifest shortcuts will be re-published, but dynamic ones are not.
4556 mEnabledActivityChecker = (activity, userId) -> true;
4557 mService.mPackageMonitor.onReceive(getTestContext(),
4558 genPackageChangedIntent(CALLING_PACKAGE_1, USER_10));
4560 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4561 assertWith(getCallerShortcuts())
4562 .haveIds("ms1", "ms1-alt", "s2")
4566 .haveIds("ms1-alt", "s2")
4568 .revertToOriginalList()
4570 .areAllWithActivity(ACTIVITY1)
4572 .revertToOriginalList()
4573 .selectByIds("ms1-alt", "s2")
4574 .areAllWithActivity(ACTIVITY2)
4578 // Disable activity 2
4579 // Because "ms1-alt" and "s2" are both pinned, they will remain, but disabled.
4580 mEnabledActivityChecker = (activity, userId) -> !ACTIVITY2.equals(activity);
4581 mService.mPackageMonitor.onReceive(getTestContext(),
4582 genPackageChangedIntent(CALLING_PACKAGE_1, USER_10));
4584 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4585 assertWith(getCallerShortcuts())
4586 .haveIds("ms1", "ms1-alt", "s2")
4588 .selectDynamic().isEmpty().revertToOriginalList() // no dynamics.
4591 .haveIds("ms1-alt", "s2")
4594 .revertToOriginalList()
4596 .areAllWithActivity(ACTIVITY1)
4602 public void testHandlePackageUpdate_activityNoLongerMain() throws Throwable {
4603 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4604 assertTrue(mManager.setDynamicShortcuts(list(
4605 makeShortcutWithActivity("s1a",
4606 new ComponentName(getCallingPackage(), "act1")),
4607 makeShortcutWithActivity("s1b",
4608 new ComponentName(getCallingPackage(), "act1")),
4609 makeShortcutWithActivity("s2a",
4610 new ComponentName(getCallingPackage(), "act2")),
4611 makeShortcutWithActivity("s2b",
4612 new ComponentName(getCallingPackage(), "act2")),
4613 makeShortcutWithActivity("s3a",
4614 new ComponentName(getCallingPackage(), "act3")),
4615 makeShortcutWithActivity("s3b",
4616 new ComponentName(getCallingPackage(), "act3"))
4618 assertWith(getCallerShortcuts())
4619 .haveIds("s1a", "s1b", "s2a", "s2b", "s3a", "s3b")
4622 runWithCaller(LAUNCHER_1, USER_0, () -> {
4623 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
4624 list("s1b", "s2b", "s3b"),
4627 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4628 assertWith(getCallerShortcuts())
4629 .haveIds("s1a", "s1b", "s2a", "s2b", "s3a", "s3b")
4632 .selectByIds("s1b", "s2b", "s3b")
4636 // Update the app and act2 and act3 are no longer main.
4637 mMainActivityChecker = (activity, userId) -> {
4638 return activity.getClassName().equals("act1");
4641 setCaller(LAUNCHER_1, USER_0);
4642 assertForLauncherCallback(mLauncherApps, () -> {
4643 updatePackageVersion(CALLING_PACKAGE_1, 1);
4644 mService.mPackageMonitor.onReceive(getTestContext(),
4645 genPackageUpdateIntent(CALLING_PACKAGE_1, USER_0));
4646 }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
4647 // Make sure the launcher gets callbacks.
4648 .haveIds("s1a", "s1b", "s2b", "s3b")
4649 .areAllWithKeyFieldsOnly();
4651 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4652 // s2a and s3a are gone, but s2b and s3b will remain because they're pinned, and
4654 assertWith(getCallerShortcuts())
4655 .haveIds("s1a", "s1b", "s2b", "s3b")
4657 .selectByIds("s1a", "s1b")
4661 .revertToOriginalList()
4662 .selectByIds("s2b", "s3b")
4670 protected void prepareForBackupTest() {
4672 prepareCrossProfileDataSet();
4678 * Make sure the backup data doesn't have the following information:
4679 * - Launchers on other users.
4680 * - Non-backup app information.
4682 * But restores all other infomation.
4684 * It also omits the following pieces of information, but that's tested in
4685 * {@link ShortcutManagerTest2#testShortcutInfoSaveAndLoad_forBackup}.
4686 * - Unpinned dynamic shortcuts
4689 public void testBackupAndRestore() {
4690 prepareForBackupTest();
4692 checkBackupAndRestore_success();
4695 public void testBackupAndRestore_backupRestoreTwice() {
4696 prepareForBackupTest();
4698 // Note doing a backup & restore again here shouldn't affect the result.
4699 dumpsysOnLogcat("Before second backup");
4703 dumpsysOnLogcat("After second backup");
4705 checkBackupAndRestore_success();
4708 public void testBackupAndRestore_backupRestoreMultiple() {
4709 prepareForBackupTest();
4711 // Note doing a backup & restore again here shouldn't affect the result.
4714 // This also shouldn't affect the result.
4715 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4716 assertTrue(mManager.setDynamicShortcuts(list(
4717 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"),
4718 makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6"))));
4723 checkBackupAndRestore_success();
4726 public void testBackupAndRestore_restoreToNewVersion() {
4727 prepareForBackupTest();
4729 // Note doing a backup & restore again here shouldn't affect the result.
4732 addPackage(CALLING_PACKAGE_1, CALLING_UID_1, 2);
4733 addPackage(LAUNCHER_1, LAUNCHER_UID_1, 5);
4735 checkBackupAndRestore_success();
4738 public void testBackupAndRestore_restoreToSuperSetSignatures() {
4739 prepareForBackupTest();
4741 // Note doing a backup & restore again here shouldn't affect the result.
4744 // Change package signatures.
4745 addPackage(CALLING_PACKAGE_1, CALLING_UID_1, 1, "sigx", CALLING_PACKAGE_1);
4746 addPackage(LAUNCHER_1, LAUNCHER_UID_1, 4, LAUNCHER_1, "sigy");
4748 checkBackupAndRestore_success();
4751 protected void checkBackupAndRestore_success() {
4752 // Make sure non-system user is not restored.
4753 final ShortcutUser userP0 = mService.getUserShortcutsLocked(USER_P0);
4754 assertEquals(0, userP0.getAllPackagesForTest().size());
4755 assertEquals(0, userP0.getAllLaunchersForTest().size());
4757 // Make sure only "allowBackup" apps are restored, and are shadow.
4758 final ShortcutUser user0 = mService.getUserShortcutsLocked(USER_0);
4759 assertExistsAndShadow(user0.getAllPackagesForTest().get(CALLING_PACKAGE_1));
4760 assertExistsAndShadow(user0.getAllPackagesForTest().get(CALLING_PACKAGE_2));
4761 assertExistsAndShadow(user0.getAllLaunchersForTest().get(
4762 PackageWithUser.of(USER_0, LAUNCHER_1)));
4763 assertExistsAndShadow(user0.getAllLaunchersForTest().get(
4764 PackageWithUser.of(USER_0, LAUNCHER_2)));
4766 assertNull(user0.getAllPackagesForTest().get(CALLING_PACKAGE_3));
4767 assertNull(user0.getAllLaunchersForTest().get(PackageWithUser.of(USER_0, LAUNCHER_3)));
4768 assertNull(user0.getAllLaunchersForTest().get(PackageWithUser.of(USER_P0, LAUNCHER_1)));
4770 installPackage(USER_0, CALLING_PACKAGE_1);
4771 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4772 assertWith(getCallerVisibleShortcuts())
4776 .revertToOriginalList()
4778 .haveIds("s1", "s2");
4781 installPackage(USER_0, LAUNCHER_1);
4782 runWithCaller(LAUNCHER_1, USER_0, () -> {
4783 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
4787 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
4790 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
4793 assertWith(mLauncherApps.getShortcuts(QUERY_ALL, HANDLE_USER_P0))
4797 installPackage(USER_0, CALLING_PACKAGE_2);
4798 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
4799 assertWith(getCallerVisibleShortcuts())
4803 .revertToOriginalList()
4805 .haveIds("s1", "s2", "s3");
4808 runWithCaller(LAUNCHER_1, USER_0, () -> {
4809 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
4813 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
4815 .haveIds("s1", "s2");
4817 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
4820 assertWith(mLauncherApps.getShortcuts(QUERY_ALL, HANDLE_USER_P0))
4824 // 3 shouldn't be backed up, so no pinned shortcuts.
4825 installPackage(USER_0, CALLING_PACKAGE_3);
4826 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
4827 assertWith(getCallerVisibleShortcuts())
4831 // Launcher on a different profile shouldn't be restored.
4832 runWithCaller(LAUNCHER_1, USER_P0, () -> {
4833 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
4835 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
4837 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
4841 // Package on a different profile, no restore.
4842 installPackage(USER_P0, CALLING_PACKAGE_1);
4843 runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
4844 assertWith(getCallerVisibleShortcuts())
4848 // Restore launcher 2 on user 0.
4849 installPackage(USER_0, LAUNCHER_2);
4850 runWithCaller(LAUNCHER_2, USER_0, () -> {
4851 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
4855 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
4857 .haveIds("s2", "s3");
4859 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
4862 assertWith(mLauncherApps.getShortcuts(QUERY_ALL, HANDLE_USER_P0))
4867 // Restoration of launcher2 shouldn't affect other packages; so do the same checks and
4868 // make sure they still have the same result.
4869 installPackage(USER_0, CALLING_PACKAGE_1);
4870 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4871 assertWith(getCallerVisibleShortcuts())
4873 .haveIds("s1", "s2");
4876 installPackage(USER_0, LAUNCHER_1);
4877 runWithCaller(LAUNCHER_1, USER_0, () -> {
4878 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
4882 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
4884 .haveIds("s1", "s2");
4886 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
4889 assertWith(mLauncherApps.getShortcuts(QUERY_ALL, HANDLE_USER_P0))
4893 installPackage(USER_0, CALLING_PACKAGE_2);
4894 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
4895 assertWith(getCallerVisibleShortcuts())
4897 .haveIds("s1", "s2", "s3");
4901 public void testBackupAndRestore_publisherLowerVersion() {
4902 prepareForBackupTest();
4904 // Note doing a backup & restore again here shouldn't affect the result.
4907 addPackage(CALLING_PACKAGE_1, CALLING_UID_1, 0); // Lower version
4909 checkBackupAndRestore_publisherNotRestored();
4912 public void testBackupAndRestore_publisherWrongSignature() {
4913 prepareForBackupTest();
4915 // Note doing a backup & restore again here shouldn't affect the result.
4918 addPackage(CALLING_PACKAGE_1, CALLING_UID_1, 10, "sigx"); // different signature
4920 checkBackupAndRestore_publisherNotRestored();
4923 public void testBackupAndRestore_publisherNoLongerBackupTarget() {
4924 prepareForBackupTest();
4926 // Note doing a backup & restore again here shouldn't affect the result.
4929 updatePackageInfo(CALLING_PACKAGE_1,
4930 pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP);
4932 checkBackupAndRestore_publisherNotRestored();
4935 protected void checkBackupAndRestore_publisherNotRestored() {
4936 installPackage(USER_0, CALLING_PACKAGE_1);
4937 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4938 assertEquals(0, mManager.getDynamicShortcuts().size());
4939 assertEquals(0, mManager.getPinnedShortcuts().size());
4942 installPackage(USER_0, CALLING_PACKAGE_2);
4943 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
4944 assertEquals(0, mManager.getDynamicShortcuts().size());
4945 assertShortcutIds(assertAllPinned(
4946 mManager.getPinnedShortcuts()),
4950 installPackage(USER_0, LAUNCHER_1);
4951 runWithCaller(LAUNCHER_1, USER_0, () -> {
4952 assertShortcutIds(assertAllPinned(
4953 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
4955 assertShortcutIds(assertAllPinned(
4956 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0)),
4958 assertShortcutIds(assertAllPinned(
4959 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
4962 installPackage(USER_0, LAUNCHER_2);
4963 runWithCaller(LAUNCHER_2, USER_0, () -> {
4964 assertShortcutIds(assertAllPinned(
4965 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
4967 assertShortcutIds(assertAllPinned(
4968 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0)),
4970 assertShortcutIds(assertAllPinned(
4971 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
4975 installPackage(USER_0, CALLING_PACKAGE_3);
4976 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
4977 assertEquals(0, mManager.getDynamicShortcuts().size());
4978 assertEquals(0, mManager.getPinnedShortcuts().size());
4981 runWithCaller(LAUNCHER_1, USER_0, () -> {
4982 assertShortcutIds(assertAllPinned(
4983 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
4985 assertShortcutIds(assertAllPinned(
4986 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0)),
4988 assertShortcutIds(assertAllPinned(
4989 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
4992 runWithCaller(LAUNCHER_2, USER_0, () -> {
4993 assertShortcutIds(assertAllPinned(
4994 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
4996 assertShortcutIds(assertAllPinned(
4997 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0)),
4999 assertShortcutIds(assertAllPinned(
5000 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5005 public void testBackupAndRestore_launcherLowerVersion() {
5006 prepareForBackupTest();
5008 // Note doing a backup & restore again here shouldn't affect the result.
5011 addPackage(LAUNCHER_1, LAUNCHER_UID_1, 0); // Lower version
5013 checkBackupAndRestore_launcherNotRestored();
5016 public void testBackupAndRestore_launcherWrongSignature() {
5017 prepareForBackupTest();
5019 // Note doing a backup & restore again here shouldn't affect the result.
5022 addPackage(LAUNCHER_1, LAUNCHER_UID_1, 10, "sigx"); // different signature
5024 checkBackupAndRestore_launcherNotRestored();
5027 public void testBackupAndRestore_launcherNoLongerBackupTarget() {
5028 prepareForBackupTest();
5030 // Note doing a backup & restore again here shouldn't affect the result.
5033 updatePackageInfo(LAUNCHER_1,
5034 pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP);
5036 checkBackupAndRestore_launcherNotRestored();
5039 protected void checkBackupAndRestore_launcherNotRestored() {
5040 installPackage(USER_0, CALLING_PACKAGE_1);
5041 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5042 assertEquals(0, mManager.getDynamicShortcuts().size());
5044 // s1 was pinned by launcher 1, which is not restored, yet, so we still see "s1" here.
5045 assertShortcutIds(assertAllPinned(
5046 mManager.getPinnedShortcuts()),
5050 installPackage(USER_0, CALLING_PACKAGE_2);
5051 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5052 assertEquals(0, mManager.getDynamicShortcuts().size());
5053 assertShortcutIds(assertAllPinned(
5054 mManager.getPinnedShortcuts()),
5058 // Now we try to restore launcher 1. Then we realize it's not restorable, so L1 has no pinned
5060 installPackage(USER_0, LAUNCHER_1);
5061 runWithCaller(LAUNCHER_1, USER_0, () -> {
5062 assertShortcutIds(assertAllPinned(
5063 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5065 assertShortcutIds(assertAllPinned(
5066 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5068 assertShortcutIds(assertAllPinned(
5069 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5073 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5074 assertEquals(0, mManager.getDynamicShortcuts().size());
5076 // Now CALLING_PACKAGE_1 realizes "s1" is no longer pinned.
5077 assertShortcutIds(assertAllPinned(
5078 mManager.getPinnedShortcuts()),
5082 installPackage(USER_0, LAUNCHER_2);
5083 runWithCaller(LAUNCHER_2, USER_0, () -> {
5084 assertShortcutIds(assertAllPinned(
5085 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0)),
5087 assertShortcutIds(assertAllPinned(
5088 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0)),
5090 assertShortcutIds(assertAllPinned(
5091 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5095 installPackage(USER_0, CALLING_PACKAGE_3);
5096 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
5097 assertEquals(0, mManager.getDynamicShortcuts().size());
5098 assertEquals(0, mManager.getPinnedShortcuts().size());
5101 runWithCaller(LAUNCHER_1, USER_0, () -> {
5102 assertShortcutIds(assertAllPinned(
5103 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5105 assertShortcutIds(assertAllPinned(
5106 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5108 assertShortcutIds(assertAllPinned(
5109 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5112 runWithCaller(LAUNCHER_2, USER_0, () -> {
5113 assertShortcutIds(assertAllPinned(
5114 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0)),
5116 assertShortcutIds(assertAllPinned(
5117 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0)),
5119 assertShortcutIds(assertAllPinned(
5120 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5125 public void testBackupAndRestore_launcherAndPackageNoLongerBackupTarget() {
5126 prepareForBackupTest();
5128 // Note doing a backup & restore again here shouldn't affect the result.
5131 updatePackageInfo(CALLING_PACKAGE_1,
5132 pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP);
5134 updatePackageInfo(LAUNCHER_1,
5135 pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP);
5137 checkBackupAndRestore_publisherAndLauncherNotRestored();
5140 protected void checkBackupAndRestore_publisherAndLauncherNotRestored() {
5141 installPackage(USER_0, CALLING_PACKAGE_1);
5142 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5143 assertEquals(0, mManager.getDynamicShortcuts().size());
5144 assertEquals(0, mManager.getPinnedShortcuts().size());
5147 installPackage(USER_0, CALLING_PACKAGE_2);
5148 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5149 assertEquals(0, mManager.getDynamicShortcuts().size());
5150 assertShortcutIds(assertAllPinned(
5151 mManager.getPinnedShortcuts()),
5155 installPackage(USER_0, LAUNCHER_1);
5156 runWithCaller(LAUNCHER_1, USER_0, () -> {
5157 assertShortcutIds(assertAllPinned(
5158 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5160 assertShortcutIds(assertAllPinned(
5161 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5163 assertShortcutIds(assertAllPinned(
5164 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5167 installPackage(USER_0, LAUNCHER_2);
5168 runWithCaller(LAUNCHER_2, USER_0, () -> {
5169 assertShortcutIds(assertAllPinned(
5170 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5172 assertShortcutIds(assertAllPinned(
5173 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0)),
5175 assertShortcutIds(assertAllPinned(
5176 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5180 // Because launcher 1 wasn't restored, "s1" is no longer pinned.
5181 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5182 assertEquals(0, mManager.getDynamicShortcuts().size());
5183 assertShortcutIds(assertAllPinned(
5184 mManager.getPinnedShortcuts()),
5188 installPackage(USER_0, CALLING_PACKAGE_3);
5189 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
5190 assertEquals(0, mManager.getDynamicShortcuts().size());
5191 assertEquals(0, mManager.getPinnedShortcuts().size());
5194 runWithCaller(LAUNCHER_1, USER_0, () -> {
5195 assertShortcutIds(assertAllPinned(
5196 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5198 assertShortcutIds(assertAllPinned(
5199 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5201 assertShortcutIds(assertAllPinned(
5202 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5205 runWithCaller(LAUNCHER_2, USER_0, () -> {
5206 assertShortcutIds(assertAllPinned(
5207 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5209 assertShortcutIds(assertAllPinned(
5210 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0)),
5212 assertShortcutIds(assertAllPinned(
5213 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5218 public void testBackupAndRestore_disabled() {
5219 prepareCrossProfileDataSet();
5221 // Before doing backup & restore, disable s1.
5222 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5223 mManager.disableShortcuts(list("s1"));
5228 // Below is copied from checkBackupAndRestore_success.
5230 // Make sure non-system user is not restored.
5231 final ShortcutUser userP0 = mService.getUserShortcutsLocked(USER_P0);
5232 assertEquals(0, userP0.getAllPackagesForTest().size());
5233 assertEquals(0, userP0.getAllLaunchersForTest().size());
5235 // Make sure only "allowBackup" apps are restored, and are shadow.
5236 final ShortcutUser user0 = mService.getUserShortcutsLocked(USER_0);
5237 assertExistsAndShadow(user0.getAllPackagesForTest().get(CALLING_PACKAGE_1));
5238 assertExistsAndShadow(user0.getAllPackagesForTest().get(CALLING_PACKAGE_2));
5239 assertExistsAndShadow(user0.getAllLaunchersForTest().get(
5240 PackageWithUser.of(USER_0, LAUNCHER_1)));
5241 assertExistsAndShadow(user0.getAllLaunchersForTest().get(
5242 PackageWithUser.of(USER_0, LAUNCHER_2)));
5244 assertNull(user0.getAllPackagesForTest().get(CALLING_PACKAGE_3));
5245 assertNull(user0.getAllLaunchersForTest().get(PackageWithUser.of(USER_0, LAUNCHER_3)));
5246 assertNull(user0.getAllLaunchersForTest().get(PackageWithUser.of(USER_P0, LAUNCHER_1)));
5248 installPackage(USER_0, CALLING_PACKAGE_1);
5249 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5250 assertWith(getCallerVisibleShortcuts())
5251 .areAllEnabled() // disabled shortcuts shouldn't be restored.
5256 .revertToOriginalList()
5258 // s1 is not restored.
5262 installPackage(USER_0, LAUNCHER_1);
5263 runWithCaller(LAUNCHER_1, USER_0, () -> {
5264 // Note, s1 was pinned by launcher 1, but was disabled, so isn't restored.
5265 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5268 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5271 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5274 assertWith(mLauncherApps.getShortcuts(QUERY_ALL, HANDLE_USER_P0))
5280 public void testBackupAndRestore_manifestRePublished() {
5281 // Publish two manifest shortcuts.
5282 addManifestShortcutResource(
5283 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
5285 updatePackageVersion(CALLING_PACKAGE_1, 1);
5286 mService.mPackageMonitor.onReceive(mServiceContext,
5287 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
5289 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5290 assertTrue(mManager.setDynamicShortcuts(list(
5291 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
5294 // Pin from launcher 1.
5295 runWithCaller(LAUNCHER_1, USER_0, () -> {
5296 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
5297 list("ms1", "ms2", "s1", "s2"), HANDLE_USER_0);
5300 // Update and now ms2 is gone -> disabled.
5301 addManifestShortcutResource(
5302 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
5304 updatePackageVersion(CALLING_PACKAGE_1, 1);
5305 mService.mPackageMonitor.onReceive(mServiceContext,
5306 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
5308 // Make sure the manifest shortcuts have been published.
5309 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5310 assertWith(getCallerShortcuts())
5314 .revertToOriginalList()
5316 .haveIds("s1", "s2", "s3")
5318 .revertToOriginalList()
5320 .haveIds("ms1", "ms2", "s1", "s2")
5322 .revertToOriginalList()
5327 .revertToOriginalList()
5329 .areAllNotManifest()
5335 // When re-installing the app, the manifest shortcut should be re-published.
5336 mService.mPackageMonitor.onReceive(mServiceContext,
5337 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
5338 mService.mPackageMonitor.onReceive(mServiceContext,
5339 genPackageAddIntent(LAUNCHER_1, USER_0));
5341 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5342 assertWith(getCallerVisibleShortcuts())
5344 // ms2 was disabled, so not restored.
5345 .haveIds("ms1", "s1", "s2")
5348 .revertToOriginalList()
5352 .revertToOriginalList()
5353 .selectByIds("s1", "s2")
5360 * It's the case with preintalled apps -- when applyRestore() is called, the system
5361 * apps are already installed, so manifest shortcuts need to be re-published.
5363 public void testBackupAndRestore_appAlreadyInstalledWhenRestored() {
5364 // Pre-backup. Same as testBackupAndRestore_manifestRePublished().
5366 // Publish two manifest shortcuts.
5367 addManifestShortcutResource(
5368 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
5370 updatePackageVersion(CALLING_PACKAGE_1, 1);
5371 mService.mPackageMonitor.onReceive(mServiceContext,
5372 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
5374 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5375 assertTrue(mManager.setDynamicShortcuts(list(
5376 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
5379 // Pin from launcher 1.
5380 runWithCaller(LAUNCHER_1, USER_0, () -> {
5381 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
5382 list("ms1", "ms2", "s1", "s2"), HANDLE_USER_0);
5385 // Update and now ms2 is gone -> disabled.
5386 addManifestShortcutResource(
5387 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
5389 updatePackageVersion(CALLING_PACKAGE_1, 1);
5390 mService.mPackageMonitor.onReceive(mServiceContext,
5391 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
5393 // Make sure the manifest shortcuts have been published.
5394 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5395 assertWith(getCallerShortcuts())
5399 .revertToOriginalList()
5401 .haveIds("s1", "s2", "s3")
5403 .revertToOriginalList()
5405 .haveIds("ms1", "ms2", "s1", "s2")
5407 .revertToOriginalList()
5412 .revertToOriginalList()
5414 .areAllNotManifest()
5418 // Backup and *without restarting the service, just call applyRestore()*.
5420 int prevUid = mInjectedCallingUid;
5421 mInjectedCallingUid = Process.SYSTEM_UID; // Only system can call it.
5423 dumpsysOnLogcat("Before backup");
5425 final byte[] payload = mService.getBackupPayload(USER_0);
5427 final String xml = new String(payload);
5428 Log.v(TAG, "Backup payload:");
5429 for (String line : xml.split("\n")) {
5433 mService.applyRestore(payload, USER_0);
5435 dumpsysOnLogcat("After restore");
5437 mInjectedCallingUid = prevUid;
5440 // The check is also the same as testBackupAndRestore_manifestRePublished().
5441 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5442 assertWith(getCallerVisibleShortcuts())
5444 // ms2 was disabled, so not restored.
5445 .haveIds("ms1", "s1", "s2")
5448 .revertToOriginalList()
5452 .revertToOriginalList()
5453 .selectByIds("s1", "s2")
5459 public void testSaveAndLoad_crossProfile() {
5460 prepareCrossProfileDataSet();
5462 dumpsysOnLogcat("Before save & load");
5464 mService.saveDirtyInfo();
5467 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5468 assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()),
5470 assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()),
5471 "s1", "s2", "s3", "s4");
5473 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5474 assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()),
5476 assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()),
5477 "s1", "s2", "s3", "s4", "s5");
5479 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
5480 assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()),
5482 assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()),
5483 "s1", "s2", "s3", "s4", "s5", "s6");
5485 runWithCaller(CALLING_PACKAGE_4, USER_0, () -> {
5486 assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts())
5488 assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts())
5491 runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
5492 assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()),
5494 assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()),
5495 "s1", "s2", "s3", "s4", "s5", "s6");
5497 runWithCaller(CALLING_PACKAGE_2, USER_P0, () -> {
5498 assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts())
5500 assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts())
5503 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
5504 assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()),
5506 assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()),
5509 runWithCaller(LAUNCHER_1, USER_0, () -> {
5511 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_0),
5514 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_0),
5517 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_3), HANDLE_USER_0),
5520 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_4), HANDLE_USER_0)
5523 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_P0),
5526 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_P0)
5528 assertExpectException(
5529 SecurityException.class, "", () -> {
5530 mLauncherApps.getShortcuts(
5531 buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_10);
5534 runWithCaller(LAUNCHER_2, USER_0, () -> {
5536 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_0),
5539 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_0),
5542 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_3), HANDLE_USER_0),
5545 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_4), HANDLE_USER_0)
5548 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_P0),
5551 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_P0)
5554 runWithCaller(LAUNCHER_3, USER_0, () -> {
5556 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_0),
5559 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_0),
5562 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_3), HANDLE_USER_0),
5565 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_4), HANDLE_USER_0)
5568 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_P0),
5571 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_P0)
5574 runWithCaller(LAUNCHER_4, USER_0, () -> {
5576 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_0)
5579 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_0)
5582 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_3), HANDLE_USER_0)
5585 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_4), HANDLE_USER_0)
5588 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_P0)
5591 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_P0)
5594 runWithCaller(LAUNCHER_1, USER_P0, () -> {
5596 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_0),
5599 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_0),
5602 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_3), HANDLE_USER_0),
5603 "s3", "s4", "s5", "s6");
5605 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_P0),
5607 assertExpectException(
5608 SecurityException.class, "unrelated profile", () -> {
5609 mLauncherApps.getShortcuts(
5610 buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_10);
5613 runWithCaller(LAUNCHER_1, USER_10, () -> {
5615 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_10),
5618 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_10)
5621 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_3), HANDLE_USER_10)
5623 assertExpectException(
5624 SecurityException.class, "unrelated profile", () -> {
5625 mLauncherApps.getShortcuts(
5626 buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0);
5628 assertExpectException(
5629 SecurityException.class, "unrelated profile", () -> {
5630 mLauncherApps.getShortcuts(
5631 buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_P0);
5636 public void testOnApplicationActive_permission() {
5637 assertExpectException(SecurityException.class, "Missing permission", () ->
5638 mManager.onApplicationActive(CALLING_PACKAGE_1, USER_0));
5640 // Has permission, now it should pass.
5641 mCallerPermissions.add(permission.RESET_SHORTCUT_MANAGER_THROTTLING);
5642 mManager.onApplicationActive(CALLING_PACKAGE_1, USER_0);
5645 public void testDumpsys_crossProfile() {
5646 prepareCrossProfileDataSet();
5647 dumpsysOnLogcat("test1", /* force= */ true);
5650 public void testDumpsys_withIcons() throws IOException {
5652 // Dump after having some icons.
5653 dumpsysOnLogcat("test1", /* force= */ true);
5656 public void testManifestShortcut_publishOnUnlockUser() {
5657 addManifestShortcutResource(
5658 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
5660 addManifestShortcutResource(
5661 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
5663 addManifestShortcutResource(
5664 new ComponentName(CALLING_PACKAGE_3, ShortcutActivity.class.getName()),
5668 mService.handleUnlockUser(USER_0);
5670 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5671 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5672 mManager.getManifestShortcuts()))),
5674 assertEmpty(mManager.getPinnedShortcuts());
5677 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5678 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5679 mManager.getManifestShortcuts()))),
5681 assertEmpty(mManager.getPinnedShortcuts());
5684 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
5685 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5686 mManager.getManifestShortcuts()))),
5687 "ms1", "ms2", "ms3", "ms4", "ms5");
5688 assertEmpty(mManager.getPinnedShortcuts());
5691 // Try on another user, with some packages uninstalled.
5692 mRunningUsers.put(USER_10, true);
5694 uninstallPackage(USER_10, CALLING_PACKAGE_1);
5695 uninstallPackage(USER_10, CALLING_PACKAGE_3);
5697 mService.handleUnlockUser(USER_10);
5699 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
5700 assertEmpty(mManager.getManifestShortcuts());
5701 assertEmpty(mManager.getPinnedShortcuts());
5704 runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
5705 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5706 mManager.getManifestShortcuts()))),
5708 assertEmpty(mManager.getPinnedShortcuts());
5711 runWithCaller(CALLING_PACKAGE_3, USER_10, () -> {
5712 assertEmpty(mManager.getManifestShortcuts());
5713 assertEmpty(mManager.getPinnedShortcuts());
5716 // Now change the resources for package 1, and unlock again.
5717 // But we still see *old* shortcuts, because the package version and install time
5721 addManifestShortcutResource(
5722 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
5724 addManifestShortcutResource(
5725 new ComponentName(CALLING_PACKAGE_3, ShortcutActivity.class.getName()),
5729 mService.handleUnlockUser(USER_0);
5731 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5732 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5733 mManager.getManifestShortcuts()))),
5735 assertEmpty(mManager.getPinnedShortcuts());
5738 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5739 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5740 mManager.getManifestShortcuts()))),
5742 assertEmpty(mManager.getPinnedShortcuts());
5745 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
5746 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5747 mManager.getManifestShortcuts()))),
5748 "ms1", "ms2", "ms3", "ms4", "ms5");
5749 assertEmpty(mManager.getPinnedShortcuts());
5752 // Do it again, but this time we change the app version, so we do detect the changes.
5755 updatePackageVersion(CALLING_PACKAGE_1, 1);
5756 updatePackageLastUpdateTime(CALLING_PACKAGE_3, 1);
5759 mService.handleUnlockUser(USER_0);
5761 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5762 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5763 mManager.getManifestShortcuts()))),
5764 "ms1", "ms2", "ms3", "ms4", "ms5");
5765 assertEmpty(mManager.getPinnedShortcuts());
5768 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5769 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5770 mManager.getManifestShortcuts()))),
5772 assertEmpty(mManager.getPinnedShortcuts());
5775 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
5776 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5777 mManager.getManifestShortcuts()))),
5779 assertEmpty(mManager.getPinnedShortcuts());
5782 // Next, try removing all shortcuts, with some of them pinned.
5783 runWithCaller(LAUNCHER_1, USER_0, () -> {
5784 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms3"), HANDLE_USER_0);
5785 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("ms2"), HANDLE_USER_0);
5786 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("ms1"), HANDLE_USER_0);
5788 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5789 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5790 mManager.getManifestShortcuts()))),
5791 "ms1", "ms2", "ms3", "ms4", "ms5");
5792 assertShortcutIds(assertAllImmutable(assertAllPinned(assertAllManifest(
5793 assertAllEnabled(mManager.getPinnedShortcuts())))),
5797 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5798 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5799 mManager.getManifestShortcuts()))),
5801 assertShortcutIds(assertAllImmutable(assertAllPinned(assertAllManifest(
5802 assertAllEnabled(mManager.getPinnedShortcuts())))),
5806 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
5807 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5808 mManager.getManifestShortcuts()))),
5810 assertShortcutIds(assertAllImmutable(assertAllPinned(assertAllManifest(
5811 assertAllEnabled(mManager.getPinnedShortcuts())))),
5817 addManifestShortcutResource(
5818 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
5820 addManifestShortcutResource(
5821 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
5823 addManifestShortcutResource(
5824 new ComponentName(CALLING_PACKAGE_3, ShortcutActivity.class.getName()),
5827 updatePackageVersion(CALLING_PACKAGE_1, 1);
5828 updatePackageVersion(CALLING_PACKAGE_2, 1);
5829 updatePackageVersion(CALLING_PACKAGE_3, 1);
5832 mService.handleUnlockUser(USER_0);
5834 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5835 assertEmpty(mManager.getManifestShortcuts());
5836 assertShortcutIds(assertAllImmutable(assertAllPinned(assertAllNotManifest(
5837 assertAllDisabled(mManager.getPinnedShortcuts())))),
5841 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5842 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5843 mManager.getManifestShortcuts()))),
5845 assertShortcutIds(assertAllImmutable(assertAllPinned(assertAllNotManifest(
5846 assertAllDisabled(mManager.getPinnedShortcuts())))),
5850 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
5851 assertEmpty(mManager.getManifestShortcuts());
5852 assertShortcutIds(assertAllImmutable(assertAllPinned(assertAllNotManifest(
5853 assertAllDisabled(mManager.getPinnedShortcuts())))),
5857 // Make sure we don't have ShortcutPackage for packages that don't have shortcuts.
5858 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_4, USER_0));
5859 assertNull(mService.getPackageShortcutForTest(LAUNCHER_1, USER_0));
5862 public void testManifestShortcut_publishOnBroadcast() {
5863 // First, no packages are installed.
5864 uninstallPackage(USER_0, CALLING_PACKAGE_1);
5865 uninstallPackage(USER_0, CALLING_PACKAGE_2);
5866 uninstallPackage(USER_0, CALLING_PACKAGE_3);
5867 uninstallPackage(USER_0, CALLING_PACKAGE_4);
5868 uninstallPackage(USER_10, CALLING_PACKAGE_1);
5869 uninstallPackage(USER_10, CALLING_PACKAGE_2);
5870 uninstallPackage(USER_10, CALLING_PACKAGE_3);
5871 uninstallPackage(USER_10, CALLING_PACKAGE_4);
5873 mService.handleUnlockUser(USER_0);
5875 mRunningUsers.put(USER_10, true);
5876 mService.handleUnlockUser(USER_10);
5878 // Originally no manifest shortcuts.
5879 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5880 assertEmpty(mManager.getManifestShortcuts());
5881 assertEmpty(mManager.getPinnedShortcuts());
5884 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5885 assertEmpty(mManager.getManifestShortcuts());
5886 assertEmpty(mManager.getPinnedShortcuts());
5889 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
5890 assertEmpty(mManager.getManifestShortcuts());
5891 assertEmpty(mManager.getPinnedShortcuts());
5894 // Package 1 updated, with manifest shortcuts.
5895 addManifestShortcutResource(
5896 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
5898 updatePackageVersion(CALLING_PACKAGE_1, 1);
5899 mService.mPackageMonitor.onReceive(getTestContext(),
5900 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
5902 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5903 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5904 mManager.getManifestShortcuts()))),
5906 assertEmpty(mManager.getPinnedShortcuts());
5909 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5910 assertEmpty(mManager.getManifestShortcuts());
5911 assertEmpty(mManager.getPinnedShortcuts());
5914 // Package 2 updated, with manifest shortcuts.
5916 addManifestShortcutResource(
5917 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
5919 updatePackageVersion(CALLING_PACKAGE_2, 1);
5920 mService.mPackageMonitor.onReceive(getTestContext(),
5921 genPackageAddIntent(CALLING_PACKAGE_2, USER_0));
5923 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5924 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5925 mManager.getManifestShortcuts()))),
5927 assertEmpty(mManager.getPinnedShortcuts());
5930 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5931 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5932 mManager.getManifestShortcuts()))),
5933 "ms1", "ms2", "ms3", "ms4", "ms5");
5934 assertWith(getCallerShortcuts()).selectManifest()
5936 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()))
5937 .haveRanksInOrder("ms1", "ms2", "ms3", "ms4", "ms5");
5938 assertEmpty(mManager.getPinnedShortcuts());
5941 // Package 2 updated, with less manifest shortcuts.
5942 // This time we use updatePackageLastUpdateTime() instead of updatePackageVersion().
5944 dumpsysOnLogcat("Before pinning");
5947 runWithCaller(LAUNCHER_1, USER_0, () -> {
5948 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("ms2", "ms3"), HANDLE_USER_0);
5951 dumpsysOnLogcat("After pinning");
5953 addManifestShortcutResource(
5954 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
5956 updatePackageLastUpdateTime(CALLING_PACKAGE_2, 1);
5957 mService.mPackageMonitor.onReceive(getTestContext(),
5958 genPackageAddIntent(CALLING_PACKAGE_2, USER_0));
5960 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5961 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5962 mManager.getManifestShortcuts()))),
5964 assertEmpty(mManager.getPinnedShortcuts());
5967 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5968 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
5969 mManager.getManifestShortcuts()))),
5971 assertWith(getCallerShortcuts()).selectManifest()
5973 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()))
5974 .haveRanksInOrder("ms1", "ms2");
5975 assertShortcutIds(assertAllImmutable(assertAllPinned(
5976 mManager.getPinnedShortcuts())),
5978 // ms3 is no longer in manifest, so should be disabled.
5979 // but ms1 and ms2 should be enabled.
5980 assertAllEnabled(list(getCallerShortcut("ms1")));
5981 assertAllEnabled(list(getCallerShortcut("ms2")));
5982 assertAllDisabled(list(getCallerShortcut("ms3")));
5985 // Package 2 on user 10 has no shortcuts yet.
5986 runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
5987 assertEmpty(mManager.getManifestShortcuts());
5988 assertEmpty(mManager.getPinnedShortcuts());
5990 // Send add broadcast, but the user is not running, so should be ignored.
5991 mService.handleCleanupUser(USER_10);
5992 mRunningUsers.put(USER_10, false);
5993 mUnlockedUsers.put(USER_10, false);
5995 mService.mPackageMonitor.onReceive(getTestContext(),
5996 genPackageAddIntent(CALLING_PACKAGE_2, USER_10));
5997 runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
5998 // Don't use the mManager APIs to get shortcuts, because they'll trigger the package
6000 // So look the internal data directly using getCallerShortcuts().
6001 assertEmpty(getCallerShortcuts());
6004 // Try again, but the user is locked, so still ignored.
6005 mRunningUsers.put(USER_10, true);
6006 mService.mPackageMonitor.onReceive(getTestContext(),
6007 genPackageAddIntent(CALLING_PACKAGE_2, USER_10));
6008 runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
6009 // Don't use the mManager APIs to get shortcuts, because they'll trigger the package
6011 // So look the internal data directly using getCallerShortcuts().
6012 assertEmpty(getCallerShortcuts());
6015 // Unlock the user, now it should work.
6016 mUnlockedUsers.put(USER_10, true);
6018 // Send PACKAGE_ADD broadcast to have Package 2 on user-10 publish manifest shortcuts.
6019 mService.mPackageMonitor.onReceive(getTestContext(),
6020 genPackageAddIntent(CALLING_PACKAGE_2, USER_10));
6022 runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
6023 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6024 mManager.getManifestShortcuts()))),
6026 assertWith(getCallerShortcuts()).selectManifest()
6028 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()))
6029 .haveRanksInOrder("ms1", "ms2");
6030 assertEmpty(mManager.getPinnedShortcuts());
6033 // But it shouldn't affect user-0.
6034 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
6035 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6036 mManager.getManifestShortcuts()))),
6038 assertWith(getCallerShortcuts()).selectManifest()
6040 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()))
6041 .haveRanksInOrder("ms1", "ms2");
6042 assertShortcutIds(assertAllImmutable(assertAllPinned(
6043 mManager.getPinnedShortcuts())),
6045 assertAllEnabled(list(getCallerShortcut("ms1")));
6046 assertAllEnabled(list(getCallerShortcut("ms2")));
6047 assertAllDisabled(list(getCallerShortcut("ms3")));
6050 // Multiple activities.
6051 // Add shortcuts on activity 2 for package 2.
6052 addManifestShortcutResource(
6053 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
6054 R.xml.shortcut_5_alt);
6055 addManifestShortcutResource(
6056 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity2.class.getName()),
6057 R.xml.shortcut_5_reverse);
6059 updatePackageLastUpdateTime(CALLING_PACKAGE_2, 1);
6060 mService.mPackageMonitor.onReceive(getTestContext(),
6061 genPackageAddIntent(CALLING_PACKAGE_2, USER_0));
6063 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
6064 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6065 mManager.getManifestShortcuts()))),
6066 "ms1", "ms2", "ms3", "ms4", "ms5",
6067 "ms1_alt", "ms2_alt", "ms3_alt", "ms4_alt", "ms5_alt");
6069 // Make sure they have the correct ranks, regardless of their ID's alphabetical order.
6070 assertWith(getCallerShortcuts()).selectManifest()
6072 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()))
6073 .haveRanksInOrder("ms1_alt", "ms2_alt", "ms3_alt", "ms4_alt", "ms5_alt");
6074 assertWith(getCallerShortcuts()).selectManifest()
6076 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity2.class.getName()))
6077 .haveRanksInOrder("ms5", "ms4", "ms3", "ms2", "ms1");
6080 // Package 2 now has no manifest shortcuts.
6081 addManifestShortcutResource(
6082 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
6084 addManifestShortcutResource(
6085 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity2.class.getName()),
6087 updatePackageLastUpdateTime(CALLING_PACKAGE_2, 1);
6088 mService.mPackageMonitor.onReceive(getTestContext(),
6089 genPackageAddIntent(CALLING_PACKAGE_2, USER_0));
6091 // No manifest shortcuts, and pinned ones are disabled.
6092 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
6093 assertEmpty(mManager.getManifestShortcuts());
6094 assertShortcutIds(assertAllImmutable(assertAllPinned(assertAllDisabled(
6095 mManager.getPinnedShortcuts()))),
6100 public void testManifestShortcuts_missingMandatoryFields() {
6101 // Start with no apps installed.
6102 uninstallPackage(USER_0, CALLING_PACKAGE_1);
6103 uninstallPackage(USER_0, CALLING_PACKAGE_2);
6104 uninstallPackage(USER_0, CALLING_PACKAGE_3);
6105 uninstallPackage(USER_0, CALLING_PACKAGE_4);
6107 mService.handleUnlockUser(USER_0);
6109 // Make sure no manifest shortcuts.
6110 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6111 assertEmpty(mManager.getManifestShortcuts());
6114 // Package 1 updated, which has one valid manifest shortcut and one invalid.
6115 addManifestShortcutResource(
6116 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6117 R.xml.shortcut_error_1);
6118 updatePackageVersion(CALLING_PACKAGE_1, 1);
6119 mService.mPackageMonitor.onReceive(getTestContext(),
6120 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6122 // Only the valid one is published.
6123 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6124 assertWith(getCallerShortcuts())
6131 // Package 1 updated, which has one valid manifest shortcut and one invalid.
6132 addManifestShortcutResource(
6133 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6134 R.xml.shortcut_error_2);
6135 updatePackageVersion(CALLING_PACKAGE_1, 1);
6136 mService.mPackageMonitor.onReceive(getTestContext(),
6137 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6139 // Only the valid one is published.
6140 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6141 assertWith(getCallerShortcuts())
6148 // Package 1 updated, which has one valid manifest shortcut and one invalid.
6149 addManifestShortcutResource(
6150 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6151 R.xml.shortcut_error_3);
6152 updatePackageVersion(CALLING_PACKAGE_1, 1);
6153 mService.mPackageMonitor.onReceive(getTestContext(),
6154 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6156 // Only the valid one is published.
6157 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6158 assertWith(getCallerShortcuts())
6163 .forShortcutWithId("x3", si -> {
6164 assertEquals(set("cat2"), si.getCategories());
6169 public void testManifestShortcuts_intentDefinitions() {
6170 addManifestShortcutResource(
6171 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6172 R.xml.shortcut_error_4);
6173 updatePackageVersion(CALLING_PACKAGE_1, 1);
6174 mService.mPackageMonitor.onReceive(getTestContext(),
6175 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6177 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6178 // Make sure invalid ones are not published.
6179 // Note that at this point disabled ones don't show up because they weren't pinned.
6180 assertWith(getCallerShortcuts())
6181 .haveIds("ms1", "ms2")
6187 .forShortcutWithId("ms1", si -> {
6188 assertTrue(si.isEnabled());
6189 assertEquals(1, si.getIntents().length);
6191 assertEquals("action1", si.getIntent().getAction());
6192 assertEquals("value1", si.getIntent().getStringExtra("key1"));
6193 assertEquals(Intent.FLAG_ACTIVITY_NEW_TASK |
6194 Intent.FLAG_ACTIVITY_CLEAR_TASK |
6195 Intent.FLAG_ACTIVITY_TASK_ON_HOME, si.getIntent().getFlags());
6197 assertEquals("action1", si.getIntents()[0].getAction());
6198 assertEquals("value1", si.getIntents()[0].getStringExtra("key1"));
6199 assertEquals(Intent.FLAG_ACTIVITY_NEW_TASK |
6200 Intent.FLAG_ACTIVITY_CLEAR_TASK |
6201 Intent.FLAG_ACTIVITY_TASK_ON_HOME, si.getIntents()[0].getFlags());
6203 .forShortcutWithId("ms2", si -> {
6204 assertTrue(si.isEnabled());
6205 assertEquals(2, si.getIntents().length);
6207 // getIntent will return the last one.
6208 assertEquals("action2_2", si.getIntent().getAction());
6209 assertEquals("value2", si.getIntent().getStringExtra("key2"));
6210 assertEquals(0, si.getIntent().getFlags());
6212 final Intent i1 = si.getIntents()[0];
6213 final Intent i2 = si.getIntents()[1];
6215 assertEquals("action2_1", i1.getAction());
6216 assertEquals("value1", i1.getStringExtra("key1"));
6217 assertEquals(Intent.FLAG_ACTIVITY_NEW_TASK |
6218 Intent.FLAG_ACTIVITY_CLEAR_TASK |
6219 Intent.FLAG_ACTIVITY_TASK_ON_HOME, i1.getFlags());
6221 assertEquals("action2_2", i2.getAction());
6222 assertEquals("value2", i2.getStringExtra("key2"));
6223 assertEquals(0, i2.getFlags());
6227 // Publish 5 enabled to pin some, so we can later test disabled manfiest shortcuts..
6228 addManifestShortcutResource(
6229 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6231 updatePackageVersion(CALLING_PACKAGE_1, 1);
6232 mService.mPackageMonitor.onReceive(getTestContext(),
6233 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6235 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6236 // Make sure 5 manifest shortcuts are published.
6237 assertWith(getCallerShortcuts())
6238 .haveIds("ms1", "ms2", "ms3", "ms4", "ms5")
6246 runWithCaller(LAUNCHER_1, USER_0, () -> {
6247 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
6248 list("ms3", "ms4", "ms5"), HANDLE_USER_0);
6251 // Make sure they're pinned.
6252 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6253 assertWith(getCallerShortcuts())
6254 .haveIds("ms1", "ms2", "ms3", "ms4", "ms5")
6255 .selectByIds("ms1", "ms2")
6259 .revertToOriginalList()
6260 .selectByIds("ms3", "ms4", "ms5")
6266 addManifestShortcutResource(
6267 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6268 R.xml.shortcut_error_4);
6269 updatePackageVersion(CALLING_PACKAGE_1, 1);
6270 mService.mPackageMonitor.onReceive(getTestContext(),
6271 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6273 // Make sure 3, 4 and 5 still exist but disabled.
6274 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6275 assertWith(getCallerShortcuts())
6276 .haveIds("ms1", "ms2", "ms3", "ms4", "ms5")
6280 .selectByIds("ms1", "ms2")
6285 .revertToOriginalList()
6286 .selectByIds("ms3", "ms4", "ms5")
6287 .areAllNotManifest()
6291 .revertToOriginalList()
6292 .forShortcutWithId("ms1", si -> {
6293 assertEquals(si.getId(), "action1", si.getIntent().getAction());
6295 .forShortcutWithId("ms2", si -> {
6296 // getIntent returns the last one.
6297 assertEquals(si.getId(), "action2_2", si.getIntent().getAction());
6299 .forShortcutWithId("ms3", si -> {
6300 assertEquals(si.getId(), Intent.ACTION_VIEW, si.getIntent().getAction());
6302 .forShortcutWithId("ms4", si -> {
6303 assertEquals(si.getId(), Intent.ACTION_VIEW, si.getIntent().getAction());
6305 .forShortcutWithId("ms5", si -> {
6306 assertEquals(si.getId(), "action", si.getIntent().getAction());
6311 public void testManifestShortcuts_checkAllFields() {
6312 mService.handleUnlockUser(USER_0);
6314 // Package 1 updated, which has one valid manifest shortcut and one invalid.
6315 addManifestShortcutResource(
6316 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6318 updatePackageVersion(CALLING_PACKAGE_1, 1);
6319 mService.mPackageMonitor.onReceive(getTestContext(),
6320 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6322 // Only the valid one is published.
6323 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6324 assertWith(getCallerShortcuts())
6325 .haveIds("ms1", "ms2", "ms3", "ms4", "ms5")
6332 .forShortcutWithId("ms1", si -> {
6333 assertEquals(R.drawable.icon1, si.getIconResourceId());
6334 assertEquals(new ComponentName(CALLING_PACKAGE_1,
6335 ShortcutActivity.class.getName()),
6338 assertEquals(R.string.shortcut_title1, si.getTitleResId());
6339 assertEquals("r" + R.string.shortcut_title1, si.getTitleResName());
6340 assertEquals(R.string.shortcut_text1, si.getTextResId());
6341 assertEquals("r" + R.string.shortcut_text1, si.getTextResName());
6342 assertEquals(R.string.shortcut_disabled_message1,
6343 si.getDisabledMessageResourceId());
6344 assertEquals("r" + R.string.shortcut_disabled_message1,
6345 si.getDisabledMessageResName());
6347 assertEquals(set("android.shortcut.conversation", "android.shortcut.media"),
6348 si.getCategories());
6349 assertEquals("action1", si.getIntent().getAction());
6350 assertEquals(Uri.parse("http://a.b.c/1"), si.getIntent().getData());
6353 .forShortcutWithId("ms2", si -> {
6354 assertEquals("ms2", si.getId());
6355 assertEquals(R.drawable.icon2, si.getIconResourceId());
6357 assertEquals(R.string.shortcut_title2, si.getTitleResId());
6358 assertEquals("r" + R.string.shortcut_title2, si.getTitleResName());
6359 assertEquals(R.string.shortcut_text2, si.getTextResId());
6360 assertEquals("r" + R.string.shortcut_text2, si.getTextResName());
6361 assertEquals(R.string.shortcut_disabled_message2,
6362 si.getDisabledMessageResourceId());
6363 assertEquals("r" + R.string.shortcut_disabled_message2,
6364 si.getDisabledMessageResName());
6366 assertEquals(set("android.shortcut.conversation"), si.getCategories());
6367 assertEquals("action2", si.getIntent().getAction());
6368 assertEquals(null, si.getIntent().getData());
6371 .forShortcutWithId("ms3", si -> {
6372 assertEquals(0, si.getIconResourceId());
6373 assertEquals(R.string.shortcut_title1, si.getTitleResId());
6374 assertEquals("r" + R.string.shortcut_title1, si.getTitleResName());
6376 assertEquals(0, si.getTextResId());
6377 assertEquals(null, si.getTextResName());
6378 assertEquals(0, si.getDisabledMessageResourceId());
6379 assertEquals(null, si.getDisabledMessageResName());
6381 assertEmpty(si.getCategories());
6382 assertEquals("android.intent.action.VIEW", si.getIntent().getAction());
6383 assertEquals(null, si.getIntent().getData());
6386 .forShortcutWithId("ms4", si -> {
6387 assertEquals(0, si.getIconResourceId());
6388 assertEquals(R.string.shortcut_title2, si.getTitleResId());
6389 assertEquals("r" + R.string.shortcut_title2, si.getTitleResName());
6391 assertEquals(0, si.getTextResId());
6392 assertEquals(null, si.getTextResName());
6393 assertEquals(0, si.getDisabledMessageResourceId());
6394 assertEquals(null, si.getDisabledMessageResName());
6396 assertEquals(set("cat"), si.getCategories());
6397 assertEquals("android.intent.action.VIEW2", si.getIntent().getAction());
6398 assertEquals(null, si.getIntent().getData());
6401 .forShortcutWithId("ms5", si -> {
6402 si = getCallerShortcut("ms5");
6403 assertEquals("action", si.getIntent().getAction());
6404 assertEquals("http://www/", si.getIntent().getData().toString());
6405 assertEquals("foo/bar", si.getIntent().getType());
6407 new ComponentName("abc", ".xyz"), si.getIntent().getComponent());
6409 assertEquals(set("cat1", "cat2"), si.getIntent().getCategories());
6410 assertEquals("value1", si.getIntent().getStringExtra("key1"));
6411 assertEquals("value2", si.getIntent().getStringExtra("key2"));
6416 public void testManifestShortcuts_localeChange() throws InterruptedException {
6417 mService.handleUnlockUser(USER_0);
6419 // Package 1 updated, which has one valid manifest shortcut and one invalid.
6420 addManifestShortcutResource(
6421 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6423 updatePackageVersion(CALLING_PACKAGE_1, 1);
6424 mService.mPackageMonitor.onReceive(getTestContext(),
6425 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6427 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6428 mManager.setDynamicShortcuts(list(makeShortcutWithTitle("s1", "title")));
6430 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6431 mManager.getManifestShortcuts()))),
6434 // check first shortcut.
6435 ShortcutInfo si = getCallerShortcut("ms1");
6437 assertEquals("ms1", si.getId());
6438 assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_title1 + "/en",
6440 assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_text1 + "/en",
6442 assertEquals("string-com.android.test.1-user:0-res:"
6443 + R.string.shortcut_disabled_message1 + "/en",
6444 si.getDisabledMessage());
6445 assertEquals(START_TIME, si.getLastChangedTimestamp());
6448 si = getCallerShortcut("ms2");
6450 assertEquals("ms2", si.getId());
6451 assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_title2 + "/en",
6453 assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_text2 + "/en",
6455 assertEquals("string-com.android.test.1-user:0-res:"
6456 + R.string.shortcut_disabled_message2 + "/en",
6457 si.getDisabledMessage());
6458 assertEquals(START_TIME, si.getLastChangedTimestamp());
6460 // Check the dynamic one.
6461 si = getCallerShortcut("s1");
6463 assertEquals("s1", si.getId());
6464 assertEquals("title", si.getTitle());
6465 assertEquals(null, si.getText());
6466 assertEquals(null, si.getDisabledMessage());
6467 assertEquals(START_TIME, si.getLastChangedTimestamp());
6470 mInjectedCurrentTimeMillis++;
6472 // Change the locale and send the broadcast, make sure the launcher gets a callback too.
6473 mInjectedLocale = Locale.JAPANESE;
6475 setCaller(LAUNCHER_1, USER_0);
6477 assertForLauncherCallback(mLauncherApps, () -> {
6478 mService.mReceiver.onReceive(mServiceContext, new Intent(Intent.ACTION_LOCALE_CHANGED));
6479 }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
6480 .haveIds("ms1", "ms2", "s1");
6482 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6483 // check first shortcut.
6484 ShortcutInfo si = getCallerShortcut("ms1");
6486 assertEquals("ms1", si.getId());
6487 assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_title1 + "/ja",
6489 assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_text1 + "/ja",
6491 assertEquals("string-com.android.test.1-user:0-res:"
6492 + R.string.shortcut_disabled_message1 + "/ja",
6493 si.getDisabledMessage());
6494 assertEquals(START_TIME + 1, si.getLastChangedTimestamp());
6497 si = getCallerShortcut("ms2");
6499 assertEquals("ms2", si.getId());
6500 assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_title2 + "/ja",
6502 assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_text2 + "/ja",
6504 assertEquals("string-com.android.test.1-user:0-res:"
6505 + R.string.shortcut_disabled_message2 + "/ja",
6506 si.getDisabledMessage());
6507 assertEquals(START_TIME + 1, si.getLastChangedTimestamp());
6509 // Check the dynamic one. (locale change shouldn't affect.)
6510 si = getCallerShortcut("s1");
6512 assertEquals("s1", si.getId());
6513 assertEquals("title", si.getTitle());
6514 assertEquals(null, si.getText());
6515 assertEquals(null, si.getDisabledMessage());
6516 assertEquals(START_TIME, si.getLastChangedTimestamp()); // Not changed.
6520 public void testManifestShortcuts_updateAndDisabled_notPinned() {
6521 mService.handleUnlockUser(USER_0);
6523 // First, just publish a manifest shortcut.
6524 addManifestShortcutResource(
6525 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6527 updatePackageVersion(CALLING_PACKAGE_1, 1);
6528 mService.mPackageMonitor.onReceive(getTestContext(),
6529 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6531 // Only the valid one is published.
6532 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6533 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6534 mManager.getManifestShortcuts()))),
6536 assertEmpty(mManager.getPinnedShortcuts());
6538 // Make sure there's no other dangling shortcuts.
6539 assertShortcutIds(getCallerShortcuts(), "ms1");
6542 // Now version up, the manifest shortcut is disabled now.
6543 addManifestShortcutResource(
6544 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6545 R.xml.shortcut_1_disable);
6546 updatePackageVersion(CALLING_PACKAGE_1, 1);
6547 mService.mPackageMonitor.onReceive(getTestContext(),
6548 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6550 // Because shortcut 1 wasn't pinned, it'll just go away.
6551 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6552 assertEmpty(mManager.getManifestShortcuts());
6553 assertEmpty(mManager.getPinnedShortcuts());
6555 // Make sure there's no other dangling shortcuts.
6556 assertEmpty(getCallerShortcuts());
6560 public void testManifestShortcuts_updateAndDisabled_pinned() {
6561 mService.handleUnlockUser(USER_0);
6563 // First, just publish a manifest shortcut.
6564 addManifestShortcutResource(
6565 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6567 updatePackageVersion(CALLING_PACKAGE_1, 1);
6568 mService.mPackageMonitor.onReceive(getTestContext(),
6569 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6571 // Only the valid one is published.
6572 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6573 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6574 mManager.getManifestShortcuts()))),
6576 assertEmpty(mManager.getPinnedShortcuts());
6578 // Make sure there's no other dangling shortcuts.
6579 assertShortcutIds(getCallerShortcuts(), "ms1");
6582 runWithCaller(LAUNCHER_1, USER_0, () -> {
6583 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms1"), HANDLE_USER_0);
6586 // Now upgrade, the manifest shortcut is disabled now.
6587 addManifestShortcutResource(
6588 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6589 R.xml.shortcut_1_disable);
6590 updatePackageVersion(CALLING_PACKAGE_1, 1);
6591 mService.mPackageMonitor.onReceive(getTestContext(),
6592 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6594 // Because shortcut 1 was pinned, it'll still exist as pinned, but disabled.
6595 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6596 assertEmpty(mManager.getManifestShortcuts());
6597 assertShortcutIds(assertAllNotManifest(assertAllImmutable(assertAllDisabled(
6598 mManager.getPinnedShortcuts()))),
6601 // Make sure the fields are updated.
6602 ShortcutInfo si = getCallerShortcut("ms1");
6604 assertEquals("ms1", si.getId());
6605 assertEquals(R.drawable.icon2, si.getIconResourceId());
6606 assertEquals(R.string.shortcut_title2, si.getTitleResId());
6607 assertEquals(R.string.shortcut_text2, si.getTextResId());
6608 assertEquals(R.string.shortcut_disabled_message2, si.getDisabledMessageResourceId());
6609 assertEquals(Intent.ACTION_VIEW, si.getIntent().getAction());
6611 // Make sure there's no other dangling shortcuts.
6612 assertShortcutIds(getCallerShortcuts(), "ms1");
6616 public void testManifestShortcuts_duplicateInSingleActivity() {
6617 mService.handleUnlockUser(USER_0);
6619 // The XML has two shortcuts with the same ID.
6620 addManifestShortcutResource(
6621 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6622 R.xml.shortcut_2_duplicate);
6623 updatePackageVersion(CALLING_PACKAGE_1, 1);
6624 mService.mPackageMonitor.onReceive(getTestContext(),
6625 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6627 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6628 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6629 mManager.getManifestShortcuts()))),
6632 // Make sure the first one has survived. (the second one has a different title.)
6633 ShortcutInfo si = getCallerShortcut("ms1");
6634 assertEquals(R.string.shortcut_title1, si.getTitleResId());
6636 // Make sure there's no other dangling shortcuts.
6637 assertShortcutIds(getCallerShortcuts(), "ms1");
6641 public void testManifestShortcuts_duplicateInTwoActivities() {
6642 mService.handleUnlockUser(USER_0);
6644 // ShortcutActivity has shortcut ms1
6645 addManifestShortcutResource(
6646 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6649 // ShortcutActivity2 has two shortcuts, ms1 and ms2.
6650 addManifestShortcutResource(
6651 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity2.class.getName()),
6653 updatePackageVersion(CALLING_PACKAGE_1, 1);
6654 mService.mPackageMonitor.onReceive(getTestContext(),
6655 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6657 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6658 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6659 mManager.getManifestShortcuts()))),
6660 "ms1", "ms2", "ms3", "ms4", "ms5");
6662 // ms1 should belong to ShortcutActivity.
6663 ShortcutInfo si = getCallerShortcut("ms1");
6664 assertEquals(R.string.shortcut_title1, si.getTitleResId());
6665 assertEquals(new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6667 assertEquals(0, si.getRank());
6669 // ms2 should belong to ShortcutActivity*2*.
6670 si = getCallerShortcut("ms2");
6671 assertEquals(R.string.shortcut_title2, si.getTitleResId());
6672 assertEquals(new ComponentName(CALLING_PACKAGE_1, ShortcutActivity2.class.getName()),
6675 // Also check the ranks
6676 assertWith(getCallerShortcuts()).selectManifest()
6678 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()))
6679 .haveRanksInOrder("ms1");
6680 assertWith(getCallerShortcuts()).selectManifest()
6682 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity2.class.getName()))
6683 .haveRanksInOrder("ms2", "ms3", "ms4", "ms5");
6685 // Make sure there's no other dangling shortcuts.
6686 assertShortcutIds(getCallerShortcuts(), "ms1", "ms2", "ms3", "ms4", "ms5");
6691 * Manifest shortcuts cannot override shortcuts that were published via the APIs.
6693 public void testManifestShortcuts_cannotOverrideNonManifest() {
6694 mService.handleUnlockUser(USER_0);
6696 // Create a non-pinned dynamic shortcut and a non-dynamic pinned shortcut.
6698 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6699 mManager.setDynamicShortcuts(list(
6700 makeShortcut("ms1", "title1",
6701 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6702 /* icon */ null, new Intent("action1"), /* rank */ 0),
6703 makeShortcut("ms2", "title2",
6704 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6705 /* icon */ null, new Intent("action1"), /* rank */ 0)));
6708 runWithCaller(LAUNCHER_1, USER_0, () -> {
6709 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms2"), HANDLE_USER_0);
6712 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6713 mManager.removeDynamicShortcuts(list("ms2"));
6715 assertShortcutIds(mManager.getDynamicShortcuts(), "ms1");
6716 assertShortcutIds(mManager.getPinnedShortcuts(), "ms2");
6717 assertEmpty(mManager.getManifestShortcuts());
6720 // Then update the app with 5 manifest shortcuts.
6721 // Make sure "ms1" and "ms2" won't be replaced.
6722 addManifestShortcutResource(
6723 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6725 updatePackageVersion(CALLING_PACKAGE_1, 1);
6726 mService.mPackageMonitor.onReceive(getTestContext(),
6727 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6729 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6730 assertShortcutIds(assertAllNotManifest(mManager.getDynamicShortcuts()), "ms1");
6731 assertShortcutIds(assertAllNotManifest(mManager.getPinnedShortcuts()), "ms2");
6732 assertShortcutIds(assertAllManifest(mManager.getManifestShortcuts()),
6733 "ms3", "ms4", "ms5");
6735 // ms1 and ms2 shouold keep the original title.
6736 ShortcutInfo si = getCallerShortcut("ms1");
6737 assertEquals("title1", si.getTitle());
6739 si = getCallerShortcut("ms2");
6740 assertEquals("title2", si.getTitle());
6744 protected void checkManifestShortcuts_immutable_verify() {
6745 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6746 assertShortcutIds(assertAllNotManifest(assertAllEnabled(
6747 mManager.getDynamicShortcuts())),
6749 assertShortcutIds(assertAllManifest(assertAllEnabled(
6750 mManager.getManifestShortcuts())),
6752 assertShortcutIds(assertAllNotManifest(assertAllDisabled(
6753 mManager.getPinnedShortcuts())),
6756 assertEquals("t1", getCallerShortcut("s1").getTitle());
6758 // Make sure there are no other shortcuts.
6759 assertShortcutIds(getCallerShortcuts(), "s1", "ms1", "ms2");
6764 * Make sure the APIs won't work on manifest shortcuts.
6766 public void testManifestShortcuts_immutable() {
6767 mService.handleUnlockUser(USER_0);
6769 // Create a non-pinned manifest shortcut, a pinned shortcut that was originally
6770 // a manifest shortcut, as well as a dynamic shortcut.
6772 addManifestShortcutResource(
6773 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6775 updatePackageVersion(CALLING_PACKAGE_1, 1);
6776 mService.mPackageMonitor.onReceive(getTestContext(),
6777 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6779 runWithCaller(LAUNCHER_1, USER_0, () -> {
6780 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms2"), HANDLE_USER_0);
6783 addManifestShortcutResource(
6784 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6786 updatePackageVersion(CALLING_PACKAGE_1, 1);
6787 mService.mPackageMonitor.onReceive(getTestContext(),
6788 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6790 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6791 mManager.addDynamicShortcuts(list(makeShortcutWithTitle("s1", "t1")));
6794 checkManifestShortcuts_immutable_verify();
6796 // Note that even though the first argument is not immutable and only the second one
6797 // is immutable, the first argument should not be executed either.
6799 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6800 assertCannotUpdateImmutable(() -> {
6801 mManager.setDynamicShortcuts(list(makeShortcut("xx"), makeShortcut("ms1")));
6803 assertCannotUpdateImmutable(() -> {
6804 mManager.setDynamicShortcuts(list(makeShortcut("xx"), makeShortcut("ms2")));
6807 checkManifestShortcuts_immutable_verify();
6809 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6810 assertCannotUpdateImmutable(() -> {
6811 mManager.addDynamicShortcuts(list(makeShortcut("xx"), makeShortcut("ms1")));
6813 assertCannotUpdateImmutable(() -> {
6814 mManager.addDynamicShortcuts(list(makeShortcut("xx"), makeShortcut("ms2")));
6817 checkManifestShortcuts_immutable_verify();
6820 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6821 assertCannotUpdateImmutable(() -> {
6822 mManager.updateShortcuts(list(makeShortcut("s1"), makeShortcut("ms1")));
6824 assertCannotUpdateImmutable(() -> {
6825 mManager.updateShortcuts(list(makeShortcut("s1"), makeShortcut("ms2")));
6828 checkManifestShortcuts_immutable_verify();
6830 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6831 assertCannotUpdateImmutable(() -> {
6832 mManager.removeDynamicShortcuts(list("s1", "ms1"));
6834 assertCannotUpdateImmutable(() -> {
6835 mManager.removeDynamicShortcuts(list("s2", "ms2"));
6838 checkManifestShortcuts_immutable_verify();
6840 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6841 assertCannotUpdateImmutable(() -> {
6842 mManager.disableShortcuts(list("s1", "ms1"));
6845 checkManifestShortcuts_immutable_verify();
6847 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6848 assertCannotUpdateImmutable(() -> {
6849 mManager.enableShortcuts(list("s1", "ms2"));
6852 checkManifestShortcuts_immutable_verify();
6857 * Make sure the APIs won't work on manifest shortcuts.
6859 public void testManifestShortcuts_tooMany() {
6860 // Change the max number of shortcuts.
6861 mService.updateConfigurationLocked(ConfigConstants.KEY_MAX_SHORTCUTS + "=3");
6863 mService.handleUnlockUser(USER_0);
6865 addManifestShortcutResource(
6866 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6868 updatePackageVersion(CALLING_PACKAGE_1, 1);
6869 mService.mPackageMonitor.onReceive(getTestContext(),
6870 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6872 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6873 // Only the first 3 should be published.
6874 assertShortcutIds(mManager.getManifestShortcuts(), "ms1", "ms2", "ms3");
6878 public void testMaxShortcutCount_set() {
6879 // Change the max number of shortcuts.
6880 mService.updateConfigurationLocked(ConfigConstants.KEY_MAX_SHORTCUTS + "=3");
6882 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6883 final ComponentName a1 = new ComponentName(mClientContext, ShortcutActivity.class);
6884 final ComponentName a2 = new ComponentName(mClientContext, ShortcutActivity2.class);
6885 final ShortcutInfo s1_1 = makeShortcutWithActivity("s11", a1);
6886 final ShortcutInfo s1_2 = makeShortcutWithActivity("s12", a1);
6887 final ShortcutInfo s1_3 = makeShortcutWithActivity("s13", a1);
6888 final ShortcutInfo s1_4 = makeShortcutWithActivity("s14", a1);
6889 final ShortcutInfo s1_5 = makeShortcutWithActivity("s15", a1);
6890 final ShortcutInfo s1_6 = makeShortcutWithActivity("s16", a1);
6891 final ShortcutInfo s2_1 = makeShortcutWithActivity("s21", a2);
6892 final ShortcutInfo s2_2 = makeShortcutWithActivity("s22", a2);
6893 final ShortcutInfo s2_3 = makeShortcutWithActivity("s23", a2);
6894 final ShortcutInfo s2_4 = makeShortcutWithActivity("s24", a2);
6896 // 3 shortcuts for 2 activities -> okay
6897 mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3, s2_1, s2_2, s2_3));
6898 assertShortcutIds(mManager.getDynamicShortcuts(),
6899 "s11", "s12", "s13", "s21", "s22", "s23");
6901 mManager.removeAllDynamicShortcuts();
6903 // 4 shortcut for activity 1 -> too many.
6904 assertDynamicShortcutCountExceeded(() -> {
6905 mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3, s1_4, s2_1, s2_2, s2_3));
6907 assertEmpty(mManager.getDynamicShortcuts());
6909 // 4 shortcut for activity 2 -> too many.
6910 assertDynamicShortcutCountExceeded(() -> {
6911 mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3, s2_1, s2_2, s2_3, s2_4));
6913 assertEmpty(mManager.getDynamicShortcuts());
6915 // First, set 3. Then set 4, which should be ignored.
6916 mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3));
6917 assertShortcutIds(mManager.getDynamicShortcuts(),
6918 "s11", "s12", "s13");
6919 assertDynamicShortcutCountExceeded(() -> {
6920 mManager.setDynamicShortcuts(list(s2_1, s2_2, s2_3, s2_4));
6922 assertShortcutIds(mManager.getDynamicShortcuts(),
6923 "s11", "s12", "s13");
6925 // Set will remove the old dynamic set, unlike add, so the following should pass.
6926 mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3));
6927 assertShortcutIds(mManager.getDynamicShortcuts(),
6928 "s11", "s12", "s13");
6929 mManager.setDynamicShortcuts(list(s1_4, s1_5, s1_6));
6930 assertShortcutIds(mManager.getDynamicShortcuts(),
6931 "s14", "s15", "s16");
6933 // Now, test with 2 manifest shortcuts.
6934 mManager.removeAllDynamicShortcuts();
6935 addManifestShortcutResource(
6936 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6938 updatePackageVersion(CALLING_PACKAGE_1, 1);
6939 mService.mPackageMonitor.onReceive(getTestContext(),
6940 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6941 assertEquals(2, mManager.getManifestShortcuts().size());
6943 // Setting 1 to activity 1 will work.
6944 mManager.setDynamicShortcuts(list(s1_1, s2_1, s2_2, s2_3));
6945 assertShortcutIds(mManager.getDynamicShortcuts(),
6946 "s11", "s21", "s22", "s23");
6947 assertEquals(2, mManager.getManifestShortcuts().size());
6949 // But setting 2 will not.
6950 mManager.removeAllDynamicShortcuts();
6951 assertDynamicShortcutCountExceeded(() -> {
6952 mManager.setDynamicShortcuts(list(s1_1, s1_2, s2_1, s2_2, s2_3));
6954 assertEmpty(mManager.getDynamicShortcuts());
6955 assertEquals(2, mManager.getManifestShortcuts().size());
6959 public void testMaxShortcutCount_add() {
6960 // Change the max number of shortcuts.
6961 mService.updateConfigurationLocked(ConfigConstants.KEY_MAX_SHORTCUTS + "=3");
6963 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6964 final ComponentName a1 = new ComponentName(mClientContext, ShortcutActivity.class);
6965 final ComponentName a2 = new ComponentName(mClientContext, ShortcutActivity2.class);
6966 final ShortcutInfo s1_1 = makeShortcutWithActivity("s11", a1);
6967 final ShortcutInfo s1_2 = makeShortcutWithActivity("s12", a1);
6968 final ShortcutInfo s1_3 = makeShortcutWithActivity("s13", a1);
6969 final ShortcutInfo s1_4 = makeShortcutWithActivity("s14", a1);
6970 final ShortcutInfo s2_1 = makeShortcutWithActivity("s21", a2);
6971 final ShortcutInfo s2_2 = makeShortcutWithActivity("s22", a2);
6972 final ShortcutInfo s2_3 = makeShortcutWithActivity("s23", a2);
6973 final ShortcutInfo s2_4 = makeShortcutWithActivity("s24", a2);
6975 // 3 shortcuts for 2 activities -> okay
6976 mManager.addDynamicShortcuts(list(s1_1, s1_2, s1_3, s2_1, s2_2, s2_3));
6977 assertShortcutIds(mManager.getDynamicShortcuts(),
6978 "s11", "s12", "s13", "s21", "s22", "s23");
6980 mManager.removeAllDynamicShortcuts();
6982 // 4 shortcut for activity 1 -> too many.
6983 assertDynamicShortcutCountExceeded(() -> {
6984 mManager.addDynamicShortcuts(list(s1_1, s1_2, s1_3, s1_4, s2_1, s2_2, s2_3));
6986 assertEmpty(mManager.getDynamicShortcuts());
6988 // 4 shortcut for activity 2 -> too many.
6989 assertDynamicShortcutCountExceeded(() -> {
6990 mManager.addDynamicShortcuts(list(s1_1, s1_2, s1_3, s2_1, s2_2, s2_3, s2_4));
6992 assertEmpty(mManager.getDynamicShortcuts());
6994 // First, set 3. Then add 1 more, which should be ignored.
6995 mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3));
6996 assertShortcutIds(mManager.getDynamicShortcuts(),
6997 "s11", "s12", "s13");
6998 assertDynamicShortcutCountExceeded(() -> {
6999 mManager.addDynamicShortcuts(list(s1_4, s2_1));
7001 assertShortcutIds(mManager.getDynamicShortcuts(),
7002 "s11", "s12", "s13");
7004 // Update existing one, which should work.
7005 mManager.addDynamicShortcuts(list(makeShortcutWithActivityAndTitle(
7006 "s11", a1, "xxx"), s2_1));
7007 assertShortcutIds(mManager.getDynamicShortcuts(),
7008 "s11", "s12", "s13", "s21");
7009 assertEquals("xxx", getCallerShortcut("s11").getTitle());
7011 // Make sure pinned shortcuts won't affect.
7012 // - Pin s11 - s13, and remove all dynamic.
7013 runWithCaller(LAUNCHER_1, USER_0, () -> {
7014 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s11", "s12", "s13"),
7017 mManager.removeAllDynamicShortcuts();
7019 assertEmpty(mManager.getDynamicShortcuts());
7020 assertShortcutIds(mManager.getPinnedShortcuts(),
7021 "s11", "s12", "s13");
7023 // Then add dynamic.
7024 mManager.addDynamicShortcuts(list(s1_4, s2_1, s2_2, s2_3));
7026 assertShortcutIds(mManager.getDynamicShortcuts(),
7027 "s14", "s21", "s22", "s23");
7028 assertShortcutIds(mManager.getPinnedShortcuts(),
7029 "s11", "s12", "s13");
7031 // Adding "s11" and "s12" back, should work
7032 mManager.addDynamicShortcuts(list(s1_1, s1_2));
7034 assertShortcutIds(mManager.getDynamicShortcuts(),
7035 "s14", "s11", "s12", "s21", "s22", "s23");
7036 assertShortcutIds(mManager.getPinnedShortcuts(),
7037 "s11", "s12", "s13");
7039 // Adding back s13 doesn't work.
7040 assertDynamicShortcutCountExceeded(() -> {
7041 mManager.addDynamicShortcuts(list(s1_3));
7044 assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a1),
7045 "s11", "s12", "s14");
7046 assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a2),
7047 "s21", "s22", "s23");
7049 // Now swap the activities.
7050 mManager.updateShortcuts(list(
7051 makeShortcutWithActivity("s11", a2),
7052 makeShortcutWithActivity("s21", a1)));
7054 assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a1),
7055 "s21", "s12", "s14");
7056 assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a2),
7057 "s11", "s22", "s23");
7059 // Now, test with 2 manifest shortcuts.
7060 mManager.removeAllDynamicShortcuts();
7061 addManifestShortcutResource(
7062 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7064 updatePackageVersion(CALLING_PACKAGE_1, 1);
7065 mService.mPackageMonitor.onReceive(getTestContext(),
7066 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7068 assertEquals(2, mManager.getManifestShortcuts().size());
7070 // Adding one shortcut to activity 1 works fine.
7071 mManager.addDynamicShortcuts(list(s1_1, s2_1, s2_2, s2_3));
7072 assertShortcutIds(mManager.getDynamicShortcuts(),
7073 "s11", "s21", "s22", "s23");
7074 assertEquals(2, mManager.getManifestShortcuts().size());
7076 // But adding one more doesn't.
7077 assertDynamicShortcutCountExceeded(() -> {
7078 mManager.addDynamicShortcuts(list(s1_4, s2_1));
7080 assertShortcutIds(mManager.getDynamicShortcuts(),
7081 "s11", "s21", "s22", "s23");
7082 assertEquals(2, mManager.getManifestShortcuts().size());
7086 public void testMaxShortcutCount_update() {
7087 // Change the max number of shortcuts.
7088 mService.updateConfigurationLocked(ConfigConstants.KEY_MAX_SHORTCUTS + "=3");
7090 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7091 final ComponentName a1 = new ComponentName(mClientContext, ShortcutActivity.class);
7092 final ComponentName a2 = new ComponentName(mClientContext, ShortcutActivity2.class);
7093 final ShortcutInfo s1_1 = makeShortcutWithActivity("s11", a1);
7094 final ShortcutInfo s1_2 = makeShortcutWithActivity("s12", a1);
7095 final ShortcutInfo s1_3 = makeShortcutWithActivity("s13", a1);
7096 final ShortcutInfo s1_4 = makeShortcutWithActivity("s14", a1);
7097 final ShortcutInfo s1_5 = makeShortcutWithActivity("s15", a1);
7098 final ShortcutInfo s2_1 = makeShortcutWithActivity("s21", a2);
7099 final ShortcutInfo s2_2 = makeShortcutWithActivity("s22", a2);
7100 final ShortcutInfo s2_3 = makeShortcutWithActivity("s23", a2);
7101 final ShortcutInfo s2_4 = makeShortcutWithActivity("s24", a2);
7103 // 3 shortcuts for 2 activities -> okay
7104 mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3, s2_1, s2_2, s2_3));
7105 assertShortcutIds(mManager.getDynamicShortcuts(),
7106 "s11", "s12", "s13", "s21", "s22", "s23");
7108 // Trying to move s11 from a1 to a2 should fail.
7109 assertDynamicShortcutCountExceeded(() -> {
7110 mManager.updateShortcuts(list(makeShortcutWithActivity("s11", a2)));
7112 assertShortcutIds(mManager.getDynamicShortcuts(),
7113 "s11", "s12", "s13", "s21", "s22", "s23");
7115 // Trying to move s21 from a2 to a1 should also fail.
7116 assertDynamicShortcutCountExceeded(() -> {
7117 mManager.updateShortcuts(list(makeShortcutWithActivity("s21", a1)));
7119 assertShortcutIds(mManager.getDynamicShortcuts(),
7120 "s11", "s12", "s13", "s21", "s22", "s23");
7122 // But, if we do these two at the same time, it should work.
7123 mManager.updateShortcuts(list(
7124 makeShortcutWithActivity("s11", a2),
7125 makeShortcutWithActivity("s21", a1)));
7126 assertShortcutIds(mManager.getDynamicShortcuts(),
7127 "s11", "s12", "s13", "s21", "s22", "s23");
7128 assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a1),
7129 "s21", "s12", "s13");
7130 assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a2),
7131 "s11", "s22", "s23");
7134 mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3, s2_1, s2_2, s2_3));
7135 assertShortcutIds(mManager.getDynamicShortcuts(),
7136 "s11", "s12", "s13", "s21", "s22", "s23");
7138 // Pin some to have more shortcuts for a1.
7139 runWithCaller(LAUNCHER_1, USER_0, () -> {
7140 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s11", "s12", "s13"),
7143 mManager.setDynamicShortcuts(list(s1_4, s1_5, s2_1, s2_2, s2_3));
7144 assertShortcutIds(mManager.getDynamicShortcuts(),
7145 "s14", "s15", "s21", "s22", "s23");
7146 assertShortcutIds(mManager.getPinnedShortcuts(),
7147 "s11", "s12", "s13");
7149 // a1 already has 2 dynamic shortcuts (and 3 pinned shortcuts that used to belong on it)
7150 // But that doesn't matter for update -- the following should still work.
7151 mManager.updateShortcuts(list(
7152 makeShortcutWithActivityAndTitle("s11", a1, "xxx1"),
7153 makeShortcutWithActivityAndTitle("s12", a1, "xxx2"),
7154 makeShortcutWithActivityAndTitle("s13", a1, "xxx3"),
7155 makeShortcutWithActivityAndTitle("s14", a1, "xxx4"),
7156 makeShortcutWithActivityAndTitle("s15", a1, "xxx5")));
7157 // All the shortcuts should still exist they all belong on same activities,
7158 // with the updated titles.
7159 assertShortcutIds(mManager.getDynamicShortcuts(),
7160 "s14", "s15", "s21", "s22", "s23");
7161 assertShortcutIds(mManager.getPinnedShortcuts(),
7162 "s11", "s12", "s13");
7164 assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a1),
7166 assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a2),
7167 "s21", "s22", "s23");
7169 assertEquals("xxx1", getCallerShortcut("s11").getTitle());
7170 assertEquals("xxx2", getCallerShortcut("s12").getTitle());
7171 assertEquals("xxx3", getCallerShortcut("s13").getTitle());
7172 assertEquals("xxx4", getCallerShortcut("s14").getTitle());
7173 assertEquals("xxx5", getCallerShortcut("s15").getTitle());
7177 public void testShortcutsPushedOutByManifest() {
7178 // Change the max number of shortcuts.
7179 mService.updateConfigurationLocked(ConfigConstants.KEY_MAX_SHORTCUTS + "=3");
7181 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7182 final ComponentName a1 = new ComponentName(mClientContext, ShortcutActivity.class);
7183 final ComponentName a2 = new ComponentName(mClientContext, ShortcutActivity2.class);
7184 final ShortcutInfo s1_1 = makeShortcutWithActivityAndRank("s11", a1, 4);
7185 final ShortcutInfo s1_2 = makeShortcutWithActivityAndRank("s12", a1, 3);
7186 final ShortcutInfo s1_3 = makeShortcutWithActivityAndRank("s13", a1, 2);
7187 final ShortcutInfo s1_4 = makeShortcutWithActivityAndRank("s14", a1, 1);
7188 final ShortcutInfo s1_5 = makeShortcutWithActivityAndRank("s15", a1, 0);
7189 final ShortcutInfo s2_1 = makeShortcutWithActivityAndRank("s21", a2, 0);
7190 final ShortcutInfo s2_2 = makeShortcutWithActivityAndRank("s22", a2, 1);
7191 final ShortcutInfo s2_3 = makeShortcutWithActivityAndRank("s23", a2, 2);
7192 final ShortcutInfo s2_4 = makeShortcutWithActivityAndRank("s24", a2, 3);
7193 final ShortcutInfo s2_5 = makeShortcutWithActivityAndRank("s25", a2, 4);
7196 mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3, s2_1, s2_2, s2_3));
7197 runWithCaller(LAUNCHER_1, USER_0, () -> {
7198 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s11", "s12", "s21", "s22"),
7201 mManager.setDynamicShortcuts(list(s1_2, s1_3, s1_4, s2_2, s2_3, s2_4));
7202 assertShortcutIds(assertAllEnabled(mManager.getDynamicShortcuts()),
7203 "s12", "s13", "s14",
7204 "s22", "s23", "s24");
7205 assertShortcutIds(assertAllEnabled(mManager.getPinnedShortcuts()),
7209 // Add 1 manifest shortcut to a1.
7210 addManifestShortcutResource(
7211 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7213 updatePackageVersion(CALLING_PACKAGE_1, 1);
7214 mService.mPackageMonitor.onReceive(getTestContext(),
7215 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7216 assertEquals(1, mManager.getManifestShortcuts().size());
7219 assertShortcutIds(assertAllEnabled(mManager.getDynamicShortcuts()),
7221 "s22", "s23", "s24");
7222 assertShortcutIds(assertAllEnabled(mManager.getPinnedShortcuts()),
7226 // Add more manifest shortcuts.
7227 addManifestShortcutResource(
7228 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7230 addManifestShortcutResource(
7231 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity2.class.getName()),
7232 R.xml.shortcut_1_alt);
7233 updatePackageVersion(CALLING_PACKAGE_1, 1);
7234 mService.mPackageMonitor.onReceive(getTestContext(),
7235 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7236 assertEquals(3, mManager.getManifestShortcuts().size());
7238 // Note the ones with the highest rank values (== least important) will be removed.
7239 assertShortcutIds(assertAllEnabled(mManager.getDynamicShortcuts()),
7242 assertShortcutIds(assertAllEnabled(mManager.getPinnedShortcuts()),
7246 // Add more manifest shortcuts.
7247 addManifestShortcutResource(
7248 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7250 addManifestShortcutResource(
7251 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity2.class.getName()),
7252 R.xml.shortcut_5_alt); // manifest has 5, but max is 3, so a2 will have 3.
7253 updatePackageVersion(CALLING_PACKAGE_1, 1);
7254 mService.mPackageMonitor.onReceive(getTestContext(),
7255 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7256 assertEquals(5, mManager.getManifestShortcuts().size());
7258 assertShortcutIds(assertAllEnabled(mManager.getDynamicShortcuts()),
7259 "s14" // a1 has 1 dynamic
7260 ); // a2 has no dynamic
7261 assertShortcutIds(assertAllEnabled(mManager.getPinnedShortcuts()),
7265 // Update, no manifest shortucts. This doesn't affect anything.
7266 addManifestShortcutResource(
7267 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7269 addManifestShortcutResource(
7270 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity2.class.getName()),
7272 updatePackageVersion(CALLING_PACKAGE_1, 1);
7273 mService.mPackageMonitor.onReceive(getTestContext(),
7274 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7275 assertEquals(0, mManager.getManifestShortcuts().size());
7277 assertShortcutIds(assertAllEnabled(mManager.getDynamicShortcuts()),
7279 assertShortcutIds(assertAllEnabled(mManager.getPinnedShortcuts()),