blob: 2936bdd8501ba434ecfa49ebf3e976f0b75652d0 [file] [log] [blame]
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.server.pm;
17
Makoto Onukia4f89b12017-10-05 10:37:55 -070018import static android.content.pm.ShortcutInfo.DISABLED_REASON_BACKUP_NOT_SUPPORTED;
19import static android.content.pm.ShortcutInfo.DISABLED_REASON_NOT_DISABLED;
20import static android.content.pm.ShortcutInfo.DISABLED_REASON_SIGNATURE_MISMATCH;
21import static android.content.pm.ShortcutInfo.DISABLED_REASON_VERSION_LOWER;
22
Makoto Onukid0010c52017-03-30 14:17:35 -070023import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.anyOrNull;
24import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.anyStringOrNull;
Makoto Onuki22fcc682016-05-17 14:52:19 -070025import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllDisabled;
Makoto Onuki0033b2a2016-04-14 17:19:16 -070026import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllDynamic;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070027import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllDynamicOrPinned;
Makoto Onuki22fcc682016-05-17 14:52:19 -070028import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllEnabled;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070029import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllHaveIntents;
Makoto Onuki0033b2a2016-04-14 17:19:16 -070030import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllHaveTitle;
Makoto Onuki22fcc682016-05-17 14:52:19 -070031import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllImmutable;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070032import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllKeyFieldsOnly;
Makoto Onuki22fcc682016-05-17 14:52:19 -070033import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllManifest;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070034import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllNotHaveIntents;
35import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllNotHaveTitle;
36import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllNotKeyFieldsOnly;
37import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllNotManifest;
Makoto Onuki0033b2a2016-04-14 17:19:16 -070038import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllPinned;
39import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllUnique;
40import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertBitmapSize;
41import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertBundleEmpty;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070042import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertCallbackNotReceived;
43import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertCallbackReceived;
44import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertCannotUpdateImmutable;
45import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertDynamicAndPinned;
Makoto Onuki0033b2a2016-04-14 17:19:16 -070046import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertDynamicOnly;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070047import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertDynamicShortcutCountExceeded;
Makoto Onuki22fcc682016-05-17 14:52:19 -070048import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertEmpty;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070049import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertExpectException;
Makoto Onukib08790c2016-06-23 14:05:46 -070050import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertForLauncherCallback;
Makoto Onuki0033b2a2016-04-14 17:19:16 -070051import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertShortcutIds;
Makoto Onuki9e1f5592016-06-08 12:30:23 -070052import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertWith;
Makoto Onuki7001a612016-05-27 13:24:28 -070053import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.filterByActivity;
Makoto Onuki39686e82016-04-13 18:03:00 -070054import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.findShortcut;
Makoto Onuki0033b2a2016-04-14 17:19:16 -070055import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.hashSet;
56import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.list;
57import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.makeBundle;
58import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.pfdToBitmap;
59import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.resetAll;
60import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.set;
Makoto Onukib08790c2016-06-23 14:05:46 -070061import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.waitOnMainThread;
Makoto Onuki0033b2a2016-04-14 17:19:16 -070062
Makoto Onuki4dbe0de2016-03-14 17:31:49 -070063import static org.mockito.Matchers.any;
Makoto Onuki83f6d2d2016-07-11 14:30:19 -070064import static org.mockito.Matchers.anyInt;
Michal Karpinski528c3e52018-02-07 17:47:10 +000065import static org.mockito.Matchers.anyString;
Makoto Onuki4dbe0de2016-03-14 17:31:49 -070066import static org.mockito.Matchers.eq;
Makoto Onuki440a1ea2016-07-20 14:21:18 -070067import static org.mockito.Mockito.doReturn;
Makoto Onuki7a6a05f2016-03-10 17:01:08 -080068import static org.mockito.Mockito.mock;
Makoto Onuki4dbe0de2016-03-14 17:31:49 -070069import static org.mockito.Mockito.reset;
70import static org.mockito.Mockito.times;
71import static org.mockito.Mockito.verify;
Makoto Onuki7a6a05f2016-03-10 17:01:08 -080072
Makoto Onuki4d36b3a2016-04-27 12:00:17 -070073import android.Manifest.permission;
Makoto Onuki83f6d2d2016-07-11 14:30:19 -070074import android.app.ActivityManager;
75import android.content.ActivityNotFoundException;
Makoto Onuki6f7362d92016-03-04 13:39:41 -080076import android.content.ComponentName;
Makoto Onuki6f7362d92016-03-04 13:39:41 -080077import android.content.Intent;
Mehdi Alizadehc86dd1f2019-01-25 16:59:54 -080078import android.content.IntentFilter;
Makoto Onuki0acbb142016-03-22 17:02:57 -070079import android.content.pm.ApplicationInfo;
Makoto Onuki7a6a05f2016-03-10 17:01:08 -080080import android.content.pm.LauncherApps;
Makoto Onukia4f89b12017-10-05 10:37:55 -070081import android.content.pm.LauncherApps.PinItemRequest;
Makoto Onuki6f7362d92016-03-04 13:39:41 -080082import android.content.pm.LauncherApps.ShortcutQuery;
Makoto Onukia4f89b12017-10-05 10:37:55 -070083import android.content.pm.PackageInfo;
Makoto Onuki6f7362d92016-03-04 13:39:41 -080084import android.content.pm.ShortcutInfo;
Mehdi Alizadeh505fb762020-01-21 17:26:24 -080085import android.content.pm.ShortcutManager;
Makoto Onuki55046222016-03-08 10:49:47 -080086import android.graphics.Bitmap;
Makoto Onuki4362a662016-03-08 18:59:09 -080087import android.graphics.Bitmap.CompressFormat;
Makoto Onuki6f7362d92016-03-04 13:39:41 -080088import android.graphics.BitmapFactory;
Hyunyoung Songbe8835e2017-02-17 11:25:08 -080089import android.graphics.drawable.AdaptiveIconDrawable;
Hyunyoung Songf281e7a2017-02-13 10:57:42 -080090import android.graphics.drawable.Drawable;
Makoto Onuki6f7362d92016-03-04 13:39:41 -080091import android.graphics.drawable.Icon;
Makoto Onuki0acbb142016-03-22 17:02:57 -070092import android.net.Uri;
Makoto Onuki83f6d2d2016-07-11 14:30:19 -070093import android.os.Bundle;
Makoto Onuki4dbe0de2016-03-14 17:31:49 -070094import android.os.Handler;
95import android.os.Looper;
Makoto Onuki377b7972016-08-09 14:43:55 -070096import android.os.Process;
Makoto Onuki6f7362d92016-03-04 13:39:41 -080097import android.os.UserHandle;
Makoto Onuki55046222016-03-08 10:49:47 -080098import android.test.suitebuilder.annotation.SmallTest;
Makoto Onuki6f7362d92016-03-04 13:39:41 -080099import android.util.Log;
Makoto Onukide667372016-03-15 14:29:20 -0700100import android.util.SparseArray;
Mehdi Alizadehabec3192019-06-27 18:06:15 -0700101import android.util.Xml;
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800102
103import com.android.frameworks.servicestests.R;
Mehdi Alizadehabec3192019-06-27 18:06:15 -0700104import com.android.internal.util.FastXmlSerializer;
Makoto Onuki4362a662016-03-08 18:59:09 -0800105import com.android.server.pm.ShortcutService.ConfigConstants;
Makoto Onuki55046222016-03-08 10:49:47 -0800106import com.android.server.pm.ShortcutService.FileOutputStreamWithPath;
Makoto Onukid99c6f02016-03-28 11:02:54 -0700107import com.android.server.pm.ShortcutUser.PackageWithUser;
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800108
Makoto Onuki4dbe0de2016-03-14 17:31:49 -0700109import org.mockito.ArgumentCaptor;
Mehdi Alizadehabec3192019-06-27 18:06:15 -0700110import org.xmlpull.v1.XmlPullParser;
111import org.xmlpull.v1.XmlPullParserException;
112import org.xmlpull.v1.XmlSerializer;
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800113
Mehdi Alizadehabec3192019-06-27 18:06:15 -0700114import java.io.ByteArrayInputStream;
115import java.io.ByteArrayOutputStream;
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800116import java.io.File;
Makoto Onuki55046222016-03-08 10:49:47 -0800117import java.io.IOException;
Mehdi Alizadehabec3192019-06-27 18:06:15 -0700118import java.io.InputStreamReader;
119import java.nio.charset.StandardCharsets;
Mehdi Alizadeh32774622018-11-05 17:32:01 -0800120import java.util.ArrayList;
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800121import java.util.List;
Makoto Onuki157b1622016-06-02 16:13:10 -0700122import java.util.Locale;
Makoto Onuki82fb2eb2017-03-31 16:58:26 -0700123import java.util.function.BiConsumer;
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800124
125/**
126 * Tests for ShortcutService and ShortcutManager.
127 *
128 m FrameworksServicesTests &&
129 adb install \
130 -r -g ${ANDROID_PRODUCT_OUT}/data/app/FrameworksServicesTests/FrameworksServicesTests.apk &&
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700131 adb shell am instrument -e class com.android.server.pm.ShortcutManagerTest1 \
Brett Chabota26eda92018-07-23 13:08:30 -0700132 -w com.android.frameworks.servicestests/androidx.test.runner.AndroidJUnitRunner
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800133 */
Makoto Onuki55046222016-03-08 10:49:47 -0800134@SmallTest
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700135public class ShortcutManagerTest1 extends BaseShortcutManagerTest {
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800136 /**
137 * Test for the first launch path, no settings file available.
138 */
139 public void testFirstInitialize() {
140 assertResetTimes(START_TIME, START_TIME + INTERVAL);
141 }
142
143 /**
Makoto Onukicdc78f72016-03-21 15:47:52 -0700144 * Test for {@link ShortcutService#getLastResetTimeLocked()} and
145 * {@link ShortcutService#getNextResetTimeLocked()}.
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800146 */
147 public void testUpdateAndGetNextResetTimeLocked() {
148 assertResetTimes(START_TIME, START_TIME + INTERVAL);
149
150 // Advance clock.
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700151 mInjectedCurrentTimeMillis += 100;
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800152
153 // Shouldn't have changed.
154 assertResetTimes(START_TIME, START_TIME + INTERVAL);
155
156 // Advance clock, almost the reset time.
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700157 mInjectedCurrentTimeMillis = START_TIME + INTERVAL - 1;
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800158
159 // Shouldn't have changed.
160 assertResetTimes(START_TIME, START_TIME + INTERVAL);
161
162 // Advance clock.
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700163 mInjectedCurrentTimeMillis += 1;
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800164
165 assertResetTimes(START_TIME + INTERVAL, START_TIME + 2 * INTERVAL);
166
Makoto Onukib6d35232016-04-04 15:57:17 -0700167 // Advance further; 4 hours since start.
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700168 mInjectedCurrentTimeMillis = START_TIME + 4 * INTERVAL + 50;
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800169
170 assertResetTimes(START_TIME + 4 * INTERVAL, START_TIME + 5 * INTERVAL);
171 }
172
173 /**
174 * Test for the restoration from saved file.
175 */
176 public void testInitializeFromSavedFile() {
177
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700178 mInjectedCurrentTimeMillis = START_TIME + 4 * INTERVAL + 50;
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800179 assertResetTimes(START_TIME + 4 * INTERVAL, START_TIME + 5 * INTERVAL);
180
181 mService.saveBaseStateLocked();
182
183 dumpBaseStateFile();
184
Makoto Onukiaa8b94a2016-03-17 13:14:05 -0700185 mService.saveDirtyInfo();
186
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800187 // Restore.
188 initService();
189
190 assertResetTimes(START_TIME + 4 * INTERVAL, START_TIME + 5 * INTERVAL);
191 }
192
193 /**
194 * Test for the restoration from restored file.
195 */
196 public void testLoadFromBrokenFile() {
197 // TODO Add various broken cases.
198 }
199
Makoto Onuki4362a662016-03-08 18:59:09 -0800200 public void testLoadConfig() {
201 mService.updateConfigurationLocked(
202 ConfigConstants.KEY_RESET_INTERVAL_SEC + "=123,"
203 + ConfigConstants.KEY_MAX_SHORTCUTS + "=4,"
Makoto Onukib6d35232016-04-04 15:57:17 -0700204 + ConfigConstants.KEY_MAX_UPDATES_PER_INTERVAL + "=5,"
Makoto Onuki4362a662016-03-08 18:59:09 -0800205 + ConfigConstants.KEY_MAX_ICON_DIMENSION_DP + "=100,"
206 + ConfigConstants.KEY_MAX_ICON_DIMENSION_DP_LOWRAM + "=50,"
207 + ConfigConstants.KEY_ICON_FORMAT + "=WEBP,"
208 + ConfigConstants.KEY_ICON_QUALITY + "=75");
209 assertEquals(123000, mService.getResetIntervalForTest());
Makoto Onukib5a012f2016-06-21 11:13:53 -0700210 assertEquals(4, mService.getMaxShortcutsForTest());
Makoto Onukib6d35232016-04-04 15:57:17 -0700211 assertEquals(5, mService.getMaxUpdatesPerIntervalForTest());
Makoto Onuki4362a662016-03-08 18:59:09 -0800212 assertEquals(100, mService.getMaxIconDimensionForTest());
213 assertEquals(CompressFormat.WEBP, mService.getIconPersistFormatForTest());
214 assertEquals(75, mService.getIconPersistQualityForTest());
215
Makoto Onukicdc78f72016-03-21 15:47:52 -0700216 mInjectedIsLowRamDevice = true;
Makoto Onuki4362a662016-03-08 18:59:09 -0800217 mService.updateConfigurationLocked(
218 ConfigConstants.KEY_MAX_ICON_DIMENSION_DP + "=100,"
219 + ConfigConstants.KEY_MAX_ICON_DIMENSION_DP_LOWRAM + "=50,"
220 + ConfigConstants.KEY_ICON_FORMAT + "=JPEG");
221 assertEquals(ShortcutService.DEFAULT_RESET_INTERVAL_SEC * 1000,
222 mService.getResetIntervalForTest());
223
224 assertEquals(ShortcutService.DEFAULT_MAX_SHORTCUTS_PER_APP,
Makoto Onukib5a012f2016-06-21 11:13:53 -0700225 mService.getMaxShortcutsForTest());
Makoto Onuki4362a662016-03-08 18:59:09 -0800226
Makoto Onukib6d35232016-04-04 15:57:17 -0700227 assertEquals(ShortcutService.DEFAULT_MAX_UPDATES_PER_INTERVAL,
228 mService.getMaxUpdatesPerIntervalForTest());
Makoto Onuki4362a662016-03-08 18:59:09 -0800229
230 assertEquals(50, mService.getMaxIconDimensionForTest());
231
232 assertEquals(CompressFormat.JPEG, mService.getIconPersistFormatForTest());
233
234 assertEquals(ShortcutService.DEFAULT_ICON_PERSIST_QUALITY,
235 mService.getIconPersistQualityForTest());
236 }
237
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800238 // === Test for app side APIs ===
239
Makoto Onuki22fcc682016-05-17 14:52:19 -0700240 /** Test for {@link android.content.pm.ShortcutManager#getMaxShortcutCountForActivity()} */
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800241 public void testGetMaxDynamicShortcutCount() {
Makoto Onuki22fcc682016-05-17 14:52:19 -0700242 assertEquals(MAX_SHORTCUTS, mManager.getMaxShortcutCountForActivity());
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800243 }
244
245 /** Test for {@link android.content.pm.ShortcutManager#getRemainingCallCount()} */
246 public void testGetRemainingCallCount() {
Makoto Onukib6d35232016-04-04 15:57:17 -0700247 assertEquals(MAX_UPDATES_PER_INTERVAL, mManager.getRemainingCallCount());
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800248 }
249
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700250 public void testGetIconMaxDimensions() {
Makoto Onuki22fcc682016-05-17 14:52:19 -0700251 assertEquals(MAX_ICON_DIMENSION, mManager.getIconMaxWidth());
252 assertEquals(MAX_ICON_DIMENSION, mManager.getIconMaxHeight());
253 }
254
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800255 /** Test for {@link android.content.pm.ShortcutManager#getRateLimitResetTime()} */
256 public void testGetRateLimitResetTime() {
257 assertEquals(START_TIME + INTERVAL, mManager.getRateLimitResetTime());
258
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700259 mInjectedCurrentTimeMillis = START_TIME + 4 * INTERVAL + 50;
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800260
261 assertEquals(START_TIME + 5 * INTERVAL, mManager.getRateLimitResetTime());
262 }
263
264 public void testSetDynamicShortcuts() {
Makoto Onuki0acbb142016-03-22 17:02:57 -0700265 setCaller(CALLING_PACKAGE_1, USER_0);
266
Makoto Onuki4dbe0de2016-03-14 17:31:49 -0700267 final Icon icon1 = Icon.createWithResource(getTestContext(), R.drawable.icon1);
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800268 final Icon icon2 = Icon.createWithBitmap(BitmapFactory.decodeResource(
Makoto Onuki4dbe0de2016-03-14 17:31:49 -0700269 getTestContext().getResources(), R.drawable.icon2));
Hyunyoung Songe4179e22017-03-01 12:51:26 -0800270 final Icon icon3 = Icon.createWithAdaptiveBitmap(BitmapFactory.decodeResource(
Makoto Onukia4f89b12017-10-05 10:37:55 -0700271 getTestContext().getResources(), R.drawable.icon2));
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800272
273 final ShortcutInfo si1 = makeShortcut(
274 "shortcut1",
275 "Title 1",
276 makeComponent(ShortcutActivity.class),
277 icon1,
278 makeIntent(Intent.ACTION_ASSIST, ShortcutActivity2.class,
279 "key1", "val1", "nest", makeBundle("key", 123)),
Makoto Onuki99302b52017-03-29 12:42:26 -0700280 /* weight */ 10);
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800281
282 final ShortcutInfo si2 = makeShortcut(
283 "shortcut2",
284 "Title 2",
285 /* activity */ null,
286 icon2,
287 makeIntent(Intent.ACTION_ASSIST, ShortcutActivity3.class),
Makoto Onuki99302b52017-03-29 12:42:26 -0700288 /* weight */ 12);
Hyunyoung Songf281e7a2017-02-13 10:57:42 -0800289 final ShortcutInfo si3 = makeShortcut(
290 "shortcut3",
291 "Title 3",
292 /* activity */ null,
293 icon3,
294 makeIntent(Intent.ACTION_ASSIST, ShortcutActivity3.class),
Makoto Onuki99302b52017-03-29 12:42:26 -0700295 /* weight */ 13);
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800296
Hyunyoung Songf281e7a2017-02-13 10:57:42 -0800297 assertTrue(mManager.setDynamicShortcuts(list(si1, si2, si3)));
Makoto Onuki55046222016-03-08 10:49:47 -0800298 assertShortcutIds(assertAllNotKeyFieldsOnly(
299 mManager.getDynamicShortcuts()),
Hyunyoung Songf281e7a2017-02-13 10:57:42 -0800300 "shortcut1", "shortcut2", "shortcut3");
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800301 assertEquals(2, mManager.getRemainingCallCount());
302
303 // TODO: Check fields
304
Makoto Onukid99c6f02016-03-28 11:02:54 -0700305 assertTrue(mManager.setDynamicShortcuts(list(si1)));
Makoto Onuki55046222016-03-08 10:49:47 -0800306 assertShortcutIds(assertAllNotKeyFieldsOnly(
307 mManager.getDynamicShortcuts()),
308 "shortcut1");
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800309 assertEquals(1, mManager.getRemainingCallCount());
310
Makoto Onukid99c6f02016-03-28 11:02:54 -0700311 assertTrue(mManager.setDynamicShortcuts(list()));
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800312 assertEquals(0, mManager.getDynamicShortcuts().size());
313 assertEquals(0, mManager.getRemainingCallCount());
314
315 dumpsysOnLogcat();
316
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700317 mInjectedCurrentTimeMillis++; // Need to advance the clock for reset to work.
Makoto Onuki4554d0e2016-03-14 15:51:41 -0700318 mService.resetThrottlingInner(UserHandle.USER_SYSTEM);
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800319
320 dumpsysOnLogcat();
321
Makoto Onukid99c6f02016-03-28 11:02:54 -0700322 assertTrue(mManager.setDynamicShortcuts(list(si2, si3)));
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800323 assertEquals(2, mManager.getDynamicShortcuts().size());
324
325 // TODO Check max number
Makoto Onuki0acbb142016-03-22 17:02:57 -0700326
Makoto Onuki9c850012016-07-26 15:50:50 -0700327 mRunningUsers.put(USER_10, true);
328
Makoto Onuki0acbb142016-03-22 17:02:57 -0700329 runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
Makoto Onukid99c6f02016-03-28 11:02:54 -0700330 assertTrue(mManager.setDynamicShortcuts(list(makeShortcut("s1"))));
Makoto Onuki0acbb142016-03-22 17:02:57 -0700331 });
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800332 }
333
334 public void testAddDynamicShortcuts() {
Makoto Onuki0acbb142016-03-22 17:02:57 -0700335 setCaller(CALLING_PACKAGE_1, USER_0);
336
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800337 final ShortcutInfo si1 = makeShortcut("shortcut1");
338 final ShortcutInfo si2 = makeShortcut("shortcut2");
339 final ShortcutInfo si3 = makeShortcut("shortcut3");
340
341 assertEquals(3, mManager.getRemainingCallCount());
342
Makoto Onukid99c6f02016-03-28 11:02:54 -0700343 assertTrue(mManager.setDynamicShortcuts(list(si1)));
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800344 assertEquals(2, mManager.getRemainingCallCount());
Makoto Onuki55046222016-03-08 10:49:47 -0800345 assertShortcutIds(assertAllNotKeyFieldsOnly(
346 mManager.getDynamicShortcuts()),
347 "shortcut1");
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800348
Makoto Onukib6d35232016-04-04 15:57:17 -0700349 assertTrue(mManager.addDynamicShortcuts(list(si2, si3)));
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800350 assertEquals(1, mManager.getRemainingCallCount());
Makoto Onuki55046222016-03-08 10:49:47 -0800351 assertShortcutIds(assertAllNotKeyFieldsOnly(
352 mManager.getDynamicShortcuts()),
Makoto Onukib6d35232016-04-04 15:57:17 -0700353 "shortcut1", "shortcut2", "shortcut3");
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800354
Makoto Onukib6d35232016-04-04 15:57:17 -0700355 // This should not crash. It'll still consume the quota.
356 assertTrue(mManager.addDynamicShortcuts(list()));
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800357 assertEquals(0, mManager.getRemainingCallCount());
Makoto Onuki55046222016-03-08 10:49:47 -0800358 assertShortcutIds(assertAllNotKeyFieldsOnly(
359 mManager.getDynamicShortcuts()),
Makoto Onukib6d35232016-04-04 15:57:17 -0700360 "shortcut1", "shortcut2", "shortcut3");
361
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700362 mInjectedCurrentTimeMillis += INTERVAL; // reset
Makoto Onukib6d35232016-04-04 15:57:17 -0700363
364 // Add with the same ID
365 assertTrue(mManager.addDynamicShortcuts(list(makeShortcut("shortcut1"))));
366 assertEquals(2, mManager.getRemainingCallCount());
367 assertShortcutIds(assertAllNotKeyFieldsOnly(
368 mManager.getDynamicShortcuts()),
369 "shortcut1", "shortcut2", "shortcut3");
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800370
371 // TODO Check max number
372
373 // TODO Check fields.
Makoto Onuki0acbb142016-03-22 17:02:57 -0700374
Makoto Onuki9c850012016-07-26 15:50:50 -0700375 mRunningUsers.put(USER_10, true);
376
Makoto Onuki0acbb142016-03-22 17:02:57 -0700377 runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
Makoto Onukib6d35232016-04-04 15:57:17 -0700378 assertTrue(mManager.addDynamicShortcuts(list(makeShortcut("s1"))));
Makoto Onuki0acbb142016-03-22 17:02:57 -0700379 });
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800380 }
381
Makoto Onuki7d0fa812018-02-21 11:24:43 -0800382 public void testUnlimitedCalls() {
383 setCaller(CALLING_PACKAGE_1, USER_0);
384
385 final ShortcutInfo si1 = makeShortcut("shortcut1");
386
387 assertEquals(3, mManager.getRemainingCallCount());
388
389 assertTrue(mManager.setDynamicShortcuts(list(si1)));
390 assertEquals(2, mManager.getRemainingCallCount());
391
392 assertTrue(mManager.addDynamicShortcuts(list(si1)));
393 assertEquals(1, mManager.getRemainingCallCount());
394
395 assertTrue(mManager.updateShortcuts(list(si1)));
396 assertEquals(0, mManager.getRemainingCallCount());
397
398 // Unlimited now.
399 mInjectHasUnlimitedShortcutsApiCallsPermission = true;
400
401 assertEquals(3, mManager.getRemainingCallCount());
402
403 assertTrue(mManager.setDynamicShortcuts(list(si1)));
404 assertEquals(3, mManager.getRemainingCallCount());
405
406 assertTrue(mManager.addDynamicShortcuts(list(si1)));
407 assertEquals(3, mManager.getRemainingCallCount());
408
409 assertTrue(mManager.updateShortcuts(list(si1)));
410 assertEquals(3, mManager.getRemainingCallCount());
411 }
412
Mehdi Alizadeh32774622018-11-05 17:32:01 -0800413 public void testPublishWithNoActivity() {
Makoto Onukib08790c2016-06-23 14:05:46 -0700414 // If activity is not explicitly set, use the default one.
415
Makoto Onuki9c850012016-07-26 15:50:50 -0700416 mRunningUsers.put(USER_10, true);
417
Makoto Onukib08790c2016-06-23 14:05:46 -0700418 runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
419 // s1 and s3 has no activities.
420 final ShortcutInfo si1 = new ShortcutInfo.Builder(mClientContext, "si1")
421 .setShortLabel("label1")
422 .setIntent(new Intent("action1"))
423 .build();
424 final ShortcutInfo si2 = new ShortcutInfo.Builder(mClientContext, "si2")
425 .setShortLabel("label2")
426 .setActivity(new ComponentName(getCallingPackage(), "abc"))
427 .setIntent(new Intent("action2"))
428 .build();
429 final ShortcutInfo si3 = new ShortcutInfo.Builder(mClientContext, "si3")
430 .setShortLabel("label3")
431 .setIntent(new Intent("action3"))
432 .build();
433
434 // Set test 1
435 assertTrue(mManager.setDynamicShortcuts(list(si1)));
436
437 assertWith(getCallerShortcuts())
438 .haveIds("si1")
439 .forShortcutWithId("si1", si -> {
440 assertEquals(new ComponentName(getCallingPackage(),
441 MAIN_ACTIVITY_CLASS), si.getActivity());
442 });
443
444 // Set test 2
445 assertTrue(mManager.setDynamicShortcuts(list(si2, si1)));
446
447 assertWith(getCallerShortcuts())
448 .haveIds("si1", "si2")
449 .forShortcutWithId("si1", si -> {
450 assertEquals(new ComponentName(getCallingPackage(),
451 MAIN_ACTIVITY_CLASS), si.getActivity());
452 })
453 .forShortcutWithId("si2", si -> {
454 assertEquals(new ComponentName(getCallingPackage(),
455 "abc"), si.getActivity());
456 });
457
458
459 // Set test 3
460 assertTrue(mManager.setDynamicShortcuts(list(si3, si1)));
461
462 assertWith(getCallerShortcuts())
463 .haveIds("si1", "si3")
464 .forShortcutWithId("si1", si -> {
465 assertEquals(new ComponentName(getCallingPackage(),
466 MAIN_ACTIVITY_CLASS), si.getActivity());
467 })
468 .forShortcutWithId("si3", si -> {
469 assertEquals(new ComponentName(getCallingPackage(),
470 MAIN_ACTIVITY_CLASS), si.getActivity());
471 });
472
473 mInjectedCurrentTimeMillis += INTERVAL; // reset throttling
474
475 // Add test 1
476 mManager.removeAllDynamicShortcuts();
477 assertTrue(mManager.addDynamicShortcuts(list(si1)));
478
479 assertWith(getCallerShortcuts())
480 .haveIds("si1")
481 .forShortcutWithId("si1", si -> {
482 assertEquals(new ComponentName(getCallingPackage(),
483 MAIN_ACTIVITY_CLASS), si.getActivity());
484 });
485
486 // Add test 2
487 mManager.removeAllDynamicShortcuts();
488 assertTrue(mManager.addDynamicShortcuts(list(si2, si1)));
489
490 assertWith(getCallerShortcuts())
491 .haveIds("si1", "si2")
492 .forShortcutWithId("si1", si -> {
493 assertEquals(new ComponentName(getCallingPackage(),
494 MAIN_ACTIVITY_CLASS), si.getActivity());
495 })
496 .forShortcutWithId("si2", si -> {
497 assertEquals(new ComponentName(getCallingPackage(),
498 "abc"), si.getActivity());
499 });
500
501
502 // Add test 3
503 mManager.removeAllDynamicShortcuts();
504 assertTrue(mManager.addDynamicShortcuts(list(si3, si1)));
505
506 assertWith(getCallerShortcuts())
507 .haveIds("si1", "si3")
508 .forShortcutWithId("si1", si -> {
509 assertEquals(new ComponentName(getCallingPackage(),
510 MAIN_ACTIVITY_CLASS), si.getActivity());
511 })
512 .forShortcutWithId("si3", si -> {
513 assertEquals(new ComponentName(getCallingPackage(),
514 MAIN_ACTIVITY_CLASS), si.getActivity());
515 });
516 });
517 }
518
519 public void testPublishWithNoActivity_noMainActivityInPackage() {
Makoto Onuki9c850012016-07-26 15:50:50 -0700520 mRunningUsers.put(USER_10, true);
521
Makoto Onukib08790c2016-06-23 14:05:46 -0700522 runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
523 final ShortcutInfo si1 = new ShortcutInfo.Builder(mClientContext, "si1")
524 .setShortLabel("label1")
525 .setIntent(new Intent("action1"))
526 .build();
527
528 // Returning null means there's no main activity in this package.
529 mMainActivityFetcher = (packageName, userId) -> null;
530
531 assertExpectException(
532 RuntimeException.class, "Launcher activity not found for", () -> {
533 assertTrue(mManager.setDynamicShortcuts(list(si1)));
534 });
535 });
536 }
537
Makoto Onukib6d35232016-04-04 15:57:17 -0700538 public void testDeleteDynamicShortcuts() {
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800539 final ShortcutInfo si1 = makeShortcut("shortcut1");
540 final ShortcutInfo si2 = makeShortcut("shortcut2");
541 final ShortcutInfo si3 = makeShortcut("shortcut3");
Makoto Onukib6d35232016-04-04 15:57:17 -0700542 final ShortcutInfo si4 = makeShortcut("shortcut4");
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800543
Makoto Onukib6d35232016-04-04 15:57:17 -0700544 assertTrue(mManager.setDynamicShortcuts(list(si1, si2, si3, si4)));
Makoto Onuki55046222016-03-08 10:49:47 -0800545 assertShortcutIds(assertAllNotKeyFieldsOnly(
546 mManager.getDynamicShortcuts()),
Makoto Onukib6d35232016-04-04 15:57:17 -0700547 "shortcut1", "shortcut2", "shortcut3", "shortcut4");
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800548
549 assertEquals(2, mManager.getRemainingCallCount());
550
Makoto Onukib6d35232016-04-04 15:57:17 -0700551 mManager.removeDynamicShortcuts(list("shortcut1"));
Makoto Onuki55046222016-03-08 10:49:47 -0800552 assertShortcutIds(assertAllNotKeyFieldsOnly(
553 mManager.getDynamicShortcuts()),
Makoto Onukib6d35232016-04-04 15:57:17 -0700554 "shortcut2", "shortcut3", "shortcut4");
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800555
Makoto Onukib6d35232016-04-04 15:57:17 -0700556 mManager.removeDynamicShortcuts(list("shortcut1"));
Makoto Onuki55046222016-03-08 10:49:47 -0800557 assertShortcutIds(assertAllNotKeyFieldsOnly(
558 mManager.getDynamicShortcuts()),
Makoto Onukib6d35232016-04-04 15:57:17 -0700559 "shortcut2", "shortcut3", "shortcut4");
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800560
Makoto Onukib6d35232016-04-04 15:57:17 -0700561 mManager.removeDynamicShortcuts(list("shortcutXXX"));
Makoto Onuki55046222016-03-08 10:49:47 -0800562 assertShortcutIds(assertAllNotKeyFieldsOnly(
563 mManager.getDynamicShortcuts()),
Makoto Onukib6d35232016-04-04 15:57:17 -0700564 "shortcut2", "shortcut3", "shortcut4");
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800565
Makoto Onukib6d35232016-04-04 15:57:17 -0700566 mManager.removeDynamicShortcuts(list("shortcut2", "shortcut4"));
Makoto Onuki55046222016-03-08 10:49:47 -0800567 assertShortcutIds(assertAllNotKeyFieldsOnly(
568 mManager.getDynamicShortcuts()),
569 "shortcut3");
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800570
Makoto Onukib6d35232016-04-04 15:57:17 -0700571 mManager.removeDynamicShortcuts(list("shortcut3"));
Makoto Onuki55046222016-03-08 10:49:47 -0800572 assertShortcutIds(assertAllNotKeyFieldsOnly(
573 mManager.getDynamicShortcuts()));
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800574
575 // Still 2 calls left.
576 assertEquals(2, mManager.getRemainingCallCount());
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800577 }
578
579 public void testDeleteAllDynamicShortcuts() {
580 final ShortcutInfo si1 = makeShortcut("shortcut1");
581 final ShortcutInfo si2 = makeShortcut("shortcut2");
582 final ShortcutInfo si3 = makeShortcut("shortcut3");
583
Makoto Onukid99c6f02016-03-28 11:02:54 -0700584 assertTrue(mManager.setDynamicShortcuts(list(si1, si2, si3)));
Makoto Onuki55046222016-03-08 10:49:47 -0800585 assertShortcutIds(assertAllNotKeyFieldsOnly(
586 mManager.getDynamicShortcuts()),
587 "shortcut1", "shortcut2", "shortcut3");
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800588
589 assertEquals(2, mManager.getRemainingCallCount());
590
Makoto Onukib6d35232016-04-04 15:57:17 -0700591 mManager.removeAllDynamicShortcuts();
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800592 assertEquals(0, mManager.getDynamicShortcuts().size());
593 assertEquals(2, mManager.getRemainingCallCount());
594
595 // Note delete shouldn't affect throttling, so...
596 assertEquals(0, mManager.getDynamicShortcuts().size());
597 assertEquals(0, mManager.getDynamicShortcuts().size());
598 assertEquals(0, mManager.getDynamicShortcuts().size());
599
600 // This should still work.
Makoto Onukid99c6f02016-03-28 11:02:54 -0700601 assertTrue(mManager.setDynamicShortcuts(list(si1, si2, si3)));
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800602 assertEquals(3, mManager.getDynamicShortcuts().size());
603
604 // Still 1 call left
605 assertEquals(1, mManager.getRemainingCallCount());
Makoto Onuki6f7362d92016-03-04 13:39:41 -0800606 }
607
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -0700608 public void testIcons() throws IOException {
Makoto Onuki4dbe0de2016-03-14 17:31:49 -0700609 final Icon res32x32 = Icon.createWithResource(getTestContext(), R.drawable.black_32x32);
610 final Icon res64x64 = Icon.createWithResource(getTestContext(), R.drawable.black_64x64);
611 final Icon res512x512 = Icon.createWithResource(getTestContext(), R.drawable.black_512x512);
Makoto Onuki55046222016-03-08 10:49:47 -0800612
613 final Icon bmp32x32 = Icon.createWithBitmap(BitmapFactory.decodeResource(
Makoto Onuki4dbe0de2016-03-14 17:31:49 -0700614 getTestContext().getResources(), R.drawable.black_32x32));
Hyunyoung Songe4179e22017-03-01 12:51:26 -0800615 final Icon bmp64x64_maskable = Icon.createWithAdaptiveBitmap(BitmapFactory.decodeResource(
Makoto Onuki4dbe0de2016-03-14 17:31:49 -0700616 getTestContext().getResources(), R.drawable.black_64x64));
Makoto Onuki55046222016-03-08 10:49:47 -0800617 final Icon bmp512x512 = Icon.createWithBitmap(BitmapFactory.decodeResource(
Makoto Onuki4dbe0de2016-03-14 17:31:49 -0700618 getTestContext().getResources(), R.drawable.black_512x512));
Makoto Onuki55046222016-03-08 10:49:47 -0800619
620 // Set from package 1
621 setCaller(CALLING_PACKAGE_1);
Makoto Onukid99c6f02016-03-28 11:02:54 -0700622 assertTrue(mManager.setDynamicShortcuts(list(
Makoto Onuki55046222016-03-08 10:49:47 -0800623 makeShortcutWithIcon("res32x32", res32x32),
624 makeShortcutWithIcon("res64x64", res64x64),
625 makeShortcutWithIcon("bmp32x32", bmp32x32),
Hyunyoung Songf281e7a2017-02-13 10:57:42 -0800626 makeShortcutWithIcon("bmp64x64", bmp64x64_maskable),
Makoto Onuki55046222016-03-08 10:49:47 -0800627 makeShortcutWithIcon("bmp512x512", bmp512x512),
628 makeShortcut("none")
629 )));
630
631 // getDynamicShortcuts() shouldn't return icons, thus assertAllNotHaveIcon().
632 assertShortcutIds(assertAllNotHaveIcon(mManager.getDynamicShortcuts()),
633 "res32x32",
634 "res64x64",
635 "bmp32x32",
636 "bmp64x64",
637 "bmp512x512",
638 "none");
639
640 // Call from another caller with the same ID, just to make sure storage is per-package.
641 setCaller(CALLING_PACKAGE_2);
Makoto Onukid99c6f02016-03-28 11:02:54 -0700642 assertTrue(mManager.setDynamicShortcuts(list(
Makoto Onuki55046222016-03-08 10:49:47 -0800643 makeShortcutWithIcon("res32x32", res512x512),
644 makeShortcutWithIcon("res64x64", res512x512),
645 makeShortcutWithIcon("none", res512x512)
646 )));
647 assertShortcutIds(assertAllNotHaveIcon(mManager.getDynamicShortcuts()),
648 "res32x32",
649 "res64x64",
650 "none");
651
Makoto Onukiabe84422016-04-07 09:41:19 -0700652 // Different profile. Note the names and the contents don't match.
653 setCaller(CALLING_PACKAGE_1, USER_P0);
654 assertTrue(mManager.setDynamicShortcuts(list(
655 makeShortcutWithIcon("res32x32", res512x512),
656 makeShortcutWithIcon("bmp32x32", bmp512x512)
657 )));
658 assertShortcutIds(assertAllNotHaveIcon(mManager.getDynamicShortcuts()),
659 "res32x32",
660 "bmp32x32");
661
Makoto Onuki41066a62016-03-09 16:18:44 -0800662 // Re-initialize and load from the files.
Makoto Onukiaa8b94a2016-03-17 13:14:05 -0700663 mService.saveDirtyInfo();
Makoto Onuki41066a62016-03-09 16:18:44 -0800664 initService();
Makoto Onuki55046222016-03-08 10:49:47 -0800665
666 // Load from launcher.
667 Bitmap bmp;
668
669 setCaller(LAUNCHER_1);
Makoto Onuki55046222016-03-08 10:49:47 -0800670 // Check hasIconResource()/hasIconFile().
Makoto Onukiabe84422016-04-07 09:41:19 -0700671 assertShortcutIds(assertAllHaveIconResId(
672 list(getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "res32x32", USER_0))),
673 "res32x32");
Makoto Onuki55046222016-03-08 10:49:47 -0800674
Makoto Onukiabe84422016-04-07 09:41:19 -0700675 assertShortcutIds(assertAllHaveIconResId(
676 list(getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "res64x64", USER_0))),
Makoto Onuki7a6a05f2016-03-10 17:01:08 -0800677 "res64x64");
Makoto Onuki55046222016-03-08 10:49:47 -0800678
Makoto Onukiabe84422016-04-07 09:41:19 -0700679 assertShortcutIds(assertAllHaveIconFile(
680 list(getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp32x32", USER_0))),
Makoto Onuki7a6a05f2016-03-10 17:01:08 -0800681 "bmp32x32");
682
Makoto Onukiabe84422016-04-07 09:41:19 -0700683 assertShortcutIds(assertAllHaveIconFile(
684 list(getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp64x64", USER_0))),
Makoto Onuki7a6a05f2016-03-10 17:01:08 -0800685 "bmp64x64");
686
Makoto Onukiabe84422016-04-07 09:41:19 -0700687 assertShortcutIds(assertAllHaveIconFile(
688 list(getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp512x512", USER_0))),
Makoto Onuki7a6a05f2016-03-10 17:01:08 -0800689 "bmp512x512");
Makoto Onuki55046222016-03-08 10:49:47 -0800690
Makoto Onukiabe84422016-04-07 09:41:19 -0700691 assertShortcutIds(assertAllHaveIconResId(
692 list(getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "res32x32", USER_P0))),
693 "res32x32");
694 assertShortcutIds(assertAllHaveIconFile(
695 list(getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp32x32", USER_P0))),
696 "bmp32x32");
697
Makoto Onuki55046222016-03-08 10:49:47 -0800698 // Check
699 assertEquals(
700 R.drawable.black_32x32,
Makoto Onuki7a6a05f2016-03-10 17:01:08 -0800701 mLauncherApps.getShortcutIconResId(
Makoto Onukiabe84422016-04-07 09:41:19 -0700702 getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "res32x32", USER_0)));
Makoto Onuki55046222016-03-08 10:49:47 -0800703
704 assertEquals(
705 R.drawable.black_64x64,
Makoto Onuki7a6a05f2016-03-10 17:01:08 -0800706 mLauncherApps.getShortcutIconResId(
Makoto Onukiabe84422016-04-07 09:41:19 -0700707 getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "res64x64", USER_0)));
Makoto Onuki55046222016-03-08 10:49:47 -0800708
709 assertEquals(
710 0, // because it's not a resource
Makoto Onuki7a6a05f2016-03-10 17:01:08 -0800711 mLauncherApps.getShortcutIconResId(
Makoto Onukiabe84422016-04-07 09:41:19 -0700712 getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp32x32", USER_0)));
Makoto Onuki55046222016-03-08 10:49:47 -0800713 assertEquals(
714 0, // because it's not a resource
Makoto Onuki7a6a05f2016-03-10 17:01:08 -0800715 mLauncherApps.getShortcutIconResId(
Makoto Onukiabe84422016-04-07 09:41:19 -0700716 getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp64x64", USER_0)));
Makoto Onuki55046222016-03-08 10:49:47 -0800717 assertEquals(
718 0, // because it's not a resource
Makoto Onuki7a6a05f2016-03-10 17:01:08 -0800719 mLauncherApps.getShortcutIconResId(
Makoto Onukiabe84422016-04-07 09:41:19 -0700720 getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp512x512", USER_0)));
Makoto Onuki55046222016-03-08 10:49:47 -0800721
Makoto Onuki7a6a05f2016-03-10 17:01:08 -0800722 bmp = pfdToBitmap(mLauncherApps.getShortcutIconFd(
Makoto Onukiabe84422016-04-07 09:41:19 -0700723 getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp32x32", USER_0)));
Makoto Onuki55046222016-03-08 10:49:47 -0800724 assertBitmapSize(32, 32, bmp);
725
Makoto Onuki7a6a05f2016-03-10 17:01:08 -0800726 bmp = pfdToBitmap(mLauncherApps.getShortcutIconFd(
Makoto Onukiabe84422016-04-07 09:41:19 -0700727 getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp64x64", USER_0)));
Makoto Onuki55046222016-03-08 10:49:47 -0800728 assertBitmapSize(64, 64, bmp);
729
Makoto Onuki7a6a05f2016-03-10 17:01:08 -0800730 bmp = pfdToBitmap(mLauncherApps.getShortcutIconFd(
Makoto Onukiabe84422016-04-07 09:41:19 -0700731 getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp512x512", USER_0)));
732 assertBitmapSize(128, 128, bmp);
733
734 assertEquals(
735 R.drawable.black_512x512,
736 mLauncherApps.getShortcutIconResId(
737 getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "res32x32", USER_P0)));
738 // Should be 512x512, so shrunk.
739 bmp = pfdToBitmap(mLauncherApps.getShortcutIconFd(
740 getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp32x32", USER_P0)));
741 assertBitmapSize(128, 128, bmp);
742
743 // Also check the overload APIs too.
744 assertEquals(
745 R.drawable.black_32x32,
746 mLauncherApps.getShortcutIconResId(CALLING_PACKAGE_1, "res32x32", HANDLE_USER_0));
747 assertEquals(
748 R.drawable.black_64x64,
749 mLauncherApps.getShortcutIconResId(CALLING_PACKAGE_1, "res64x64", HANDLE_USER_0));
750 assertEquals(
751 R.drawable.black_512x512,
752 mLauncherApps.getShortcutIconResId(CALLING_PACKAGE_1, "res32x32", HANDLE_USER_P0));
753 bmp = pfdToBitmap(
754 mLauncherApps.getShortcutIconFd(CALLING_PACKAGE_1, "bmp32x32", HANDLE_USER_P0));
Makoto Onuki55046222016-03-08 10:49:47 -0800755 assertBitmapSize(128, 128, bmp);
Hyunyoung Songf281e7a2017-02-13 10:57:42 -0800756
757 Drawable dr = mLauncherApps.getShortcutIconDrawable(
Makoto Onukia4f89b12017-10-05 10:37:55 -0700758 makeShortcutWithIcon("bmp64x64", bmp64x64_maskable), 0);
Hyunyoung Songbe8835e2017-02-17 11:25:08 -0800759 assertTrue(dr instanceof AdaptiveIconDrawable);
Hyunyoung Song92e3da22017-04-06 23:04:19 -0700760 float viewportPercentage = 1 / (1 + 2 * AdaptiveIconDrawable.getExtraInsetFraction());
Hyunyoung Songf281e7a2017-02-13 10:57:42 -0800761 assertEquals((int) (bmp64x64_maskable.getBitmap().getWidth() * viewportPercentage),
Makoto Onukia4f89b12017-10-05 10:37:55 -0700762 dr.getIntrinsicWidth());
Hyunyoung Songf281e7a2017-02-13 10:57:42 -0800763 assertEquals((int) (bmp64x64_maskable.getBitmap().getHeight() * viewportPercentage),
Makoto Onukia4f89b12017-10-05 10:37:55 -0700764 dr.getIntrinsicHeight());
Makoto Onuki6c1dbd52016-05-02 15:19:32 -0700765 }
Makoto Onuki55046222016-03-08 10:49:47 -0800766
Makoto Onuki6c1dbd52016-05-02 15:19:32 -0700767 public void testCleanupDanglingBitmaps() throws Exception {
768 assertBitmapDirectories(USER_0, EMPTY_STRINGS);
769 assertBitmapDirectories(USER_10, EMPTY_STRINGS);
770
771 // Make some shortcuts with bitmap icons.
772 final Icon bmp32x32 = Icon.createWithBitmap(BitmapFactory.decodeResource(
773 getTestContext().getResources(), R.drawable.black_32x32));
774
775 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
776 mManager.setDynamicShortcuts(list(
777 makeShortcutWithIcon("s1", bmp32x32),
778 makeShortcutWithIcon("s2", bmp32x32),
779 makeShortcutWithIcon("s3", bmp32x32)
780 ));
781 });
782
783 // Increment the time (which actually we don't have to), which is used for filenames.
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700784 mInjectedCurrentTimeMillis++;
Makoto Onuki6c1dbd52016-05-02 15:19:32 -0700785
786 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
787 mManager.setDynamicShortcuts(list(
788 makeShortcutWithIcon("s4", bmp32x32),
789 makeShortcutWithIcon("s5", bmp32x32),
790 makeShortcutWithIcon("s6", bmp32x32)
791 ));
792 });
793
794 // Increment the time, which is used for filenames.
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700795 mInjectedCurrentTimeMillis++;
Makoto Onuki6c1dbd52016-05-02 15:19:32 -0700796
797 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
798 mManager.setDynamicShortcuts(list(
799 ));
800 });
801
802 // For USER-10, let's try without updating the times.
Makoto Onuki9c850012016-07-26 15:50:50 -0700803 mRunningUsers.put(USER_10, true);
804
Makoto Onuki6c1dbd52016-05-02 15:19:32 -0700805 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
806 mManager.setDynamicShortcuts(list(
807 makeShortcutWithIcon("10s1", bmp32x32),
808 makeShortcutWithIcon("10s2", bmp32x32),
809 makeShortcutWithIcon("10s3", bmp32x32)
810 ));
811 });
812 runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
813 mManager.setDynamicShortcuts(list(
814 makeShortcutWithIcon("10s4", bmp32x32),
815 makeShortcutWithIcon("10s5", bmp32x32),
816 makeShortcutWithIcon("10s6", bmp32x32)
817 ));
818 });
819 runWithCaller(CALLING_PACKAGE_3, USER_10, () -> {
820 mManager.setDynamicShortcuts(list(
821 ));
822 });
823
824 dumpsysOnLogcat();
825
Makoto Onukiffa519c2017-12-13 13:14:38 -0800826 mService.waitForBitmapSavesForTest();
Makoto Onuki6c1dbd52016-05-02 15:19:32 -0700827 // Check files and directories.
828 // Package 3 has no bitmaps, so we don't create a directory.
829 assertBitmapDirectories(USER_0, CALLING_PACKAGE_1, CALLING_PACKAGE_2);
830 assertBitmapDirectories(USER_10, CALLING_PACKAGE_1, CALLING_PACKAGE_2);
831
832 assertBitmapFiles(USER_0, CALLING_PACKAGE_1,
833 getBitmapFilename(USER_0, CALLING_PACKAGE_1, "s1"),
834 getBitmapFilename(USER_0, CALLING_PACKAGE_1, "s2"),
835 getBitmapFilename(USER_0, CALLING_PACKAGE_1, "s3")
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700836 );
Makoto Onuki6c1dbd52016-05-02 15:19:32 -0700837 assertBitmapFiles(USER_0, CALLING_PACKAGE_2,
838 getBitmapFilename(USER_0, CALLING_PACKAGE_2, "s4"),
839 getBitmapFilename(USER_0, CALLING_PACKAGE_2, "s5"),
840 getBitmapFilename(USER_0, CALLING_PACKAGE_2, "s6")
841 );
842 assertBitmapFiles(USER_0, CALLING_PACKAGE_3,
843 EMPTY_STRINGS
844 );
845 assertBitmapFiles(USER_10, CALLING_PACKAGE_1,
846 getBitmapFilename(USER_10, CALLING_PACKAGE_1, "10s1"),
847 getBitmapFilename(USER_10, CALLING_PACKAGE_1, "10s2"),
848 getBitmapFilename(USER_10, CALLING_PACKAGE_1, "10s3")
849 );
850 assertBitmapFiles(USER_10, CALLING_PACKAGE_2,
851 getBitmapFilename(USER_10, CALLING_PACKAGE_2, "10s4"),
852 getBitmapFilename(USER_10, CALLING_PACKAGE_2, "10s5"),
853 getBitmapFilename(USER_10, CALLING_PACKAGE_2, "10s6")
854 );
855 assertBitmapFiles(USER_10, CALLING_PACKAGE_3,
856 EMPTY_STRINGS
857 );
858
859 // Then create random directories and files.
860 makeFile(mService.getUserBitmapFilePath(USER_0), "a.b.c").mkdir();
861 makeFile(mService.getUserBitmapFilePath(USER_0), "d.e.f").mkdir();
862 makeFile(mService.getUserBitmapFilePath(USER_0), "d.e.f", "123").createNewFile();
863 makeFile(mService.getUserBitmapFilePath(USER_0), "d.e.f", "456").createNewFile();
864
865 makeFile(mService.getUserBitmapFilePath(USER_0), CALLING_PACKAGE_3).mkdir();
866
867 makeFile(mService.getUserBitmapFilePath(USER_0), CALLING_PACKAGE_1, "1").createNewFile();
868 makeFile(mService.getUserBitmapFilePath(USER_0), CALLING_PACKAGE_1, "2").createNewFile();
869 makeFile(mService.getUserBitmapFilePath(USER_0), CALLING_PACKAGE_1, "3").createNewFile();
870 makeFile(mService.getUserBitmapFilePath(USER_0), CALLING_PACKAGE_1, "4").createNewFile();
871
872 makeFile(mService.getUserBitmapFilePath(USER_10), "10a.b.c").mkdir();
873 makeFile(mService.getUserBitmapFilePath(USER_10), "10d.e.f").mkdir();
874 makeFile(mService.getUserBitmapFilePath(USER_10), "10d.e.f", "123").createNewFile();
875 makeFile(mService.getUserBitmapFilePath(USER_10), "10d.e.f", "456").createNewFile();
876
877 makeFile(mService.getUserBitmapFilePath(USER_10), CALLING_PACKAGE_2, "1").createNewFile();
878 makeFile(mService.getUserBitmapFilePath(USER_10), CALLING_PACKAGE_2, "2").createNewFile();
879 makeFile(mService.getUserBitmapFilePath(USER_10), CALLING_PACKAGE_2, "3").createNewFile();
880 makeFile(mService.getUserBitmapFilePath(USER_10), CALLING_PACKAGE_2, "4").createNewFile();
881
Makoto Onukiffa519c2017-12-13 13:14:38 -0800882 mService.waitForBitmapSavesForTest();
Makoto Onuki6c1dbd52016-05-02 15:19:32 -0700883 assertBitmapDirectories(USER_0, CALLING_PACKAGE_1, CALLING_PACKAGE_2, CALLING_PACKAGE_3,
884 "a.b.c", "d.e.f");
885
886 // Save and load. When a user is loaded, we do the cleanup.
887 mService.saveDirtyInfo();
888 initService();
889
890 mService.handleUnlockUser(USER_0);
891 mService.handleUnlockUser(USER_10);
892 mService.handleUnlockUser(20); // Make sure the logic will still work for nonexistent user.
893
894 // The below check is the same as above, except this time USER_0 use the CALLING_PACKAGE_3
895 // directory.
896
Makoto Onukiffa519c2017-12-13 13:14:38 -0800897 mService.waitForBitmapSavesForTest();
Makoto Onuki6c1dbd52016-05-02 15:19:32 -0700898 assertBitmapDirectories(USER_0, CALLING_PACKAGE_1, CALLING_PACKAGE_2, CALLING_PACKAGE_3);
899 assertBitmapDirectories(USER_10, CALLING_PACKAGE_1, CALLING_PACKAGE_2);
900
901 assertBitmapFiles(USER_0, CALLING_PACKAGE_1,
902 getBitmapFilename(USER_0, CALLING_PACKAGE_1, "s1"),
903 getBitmapFilename(USER_0, CALLING_PACKAGE_1, "s2"),
904 getBitmapFilename(USER_0, CALLING_PACKAGE_1, "s3")
905 );
906 assertBitmapFiles(USER_0, CALLING_PACKAGE_2,
907 getBitmapFilename(USER_0, CALLING_PACKAGE_2, "s4"),
908 getBitmapFilename(USER_0, CALLING_PACKAGE_2, "s5"),
909 getBitmapFilename(USER_0, CALLING_PACKAGE_2, "s6")
910 );
911 assertBitmapFiles(USER_0, CALLING_PACKAGE_3,
912 EMPTY_STRINGS
913 );
914 assertBitmapFiles(USER_10, CALLING_PACKAGE_1,
915 getBitmapFilename(USER_10, CALLING_PACKAGE_1, "10s1"),
916 getBitmapFilename(USER_10, CALLING_PACKAGE_1, "10s2"),
917 getBitmapFilename(USER_10, CALLING_PACKAGE_1, "10s3")
918 );
919 assertBitmapFiles(USER_10, CALLING_PACKAGE_2,
920 getBitmapFilename(USER_10, CALLING_PACKAGE_2, "10s4"),
921 getBitmapFilename(USER_10, CALLING_PACKAGE_2, "10s5"),
922 getBitmapFilename(USER_10, CALLING_PACKAGE_2, "10s6")
923 );
924 assertBitmapFiles(USER_10, CALLING_PACKAGE_3,
925 EMPTY_STRINGS
926 );
Makoto Onuki55046222016-03-08 10:49:47 -0800927 }
928
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700929 protected void checkShrinkBitmap(int expectedWidth, int expectedHeight, int resId, int maxSize) {
Makoto Onuki55046222016-03-08 10:49:47 -0800930 assertBitmapSize(expectedWidth, expectedHeight,
931 ShortcutService.shrinkBitmap(BitmapFactory.decodeResource(
Makoto Onuki4dbe0de2016-03-14 17:31:49 -0700932 getTestContext().getResources(), resId),
Makoto Onuki55046222016-03-08 10:49:47 -0800933 maxSize));
934 }
935
936 public void testShrinkBitmap() {
937 checkShrinkBitmap(32, 32, R.drawable.black_512x512, 32);
938 checkShrinkBitmap(511, 511, R.drawable.black_512x512, 511);
939 checkShrinkBitmap(512, 512, R.drawable.black_512x512, 512);
940
941 checkShrinkBitmap(1024, 4096, R.drawable.black_1024x4096, 4096);
942 checkShrinkBitmap(1024, 4096, R.drawable.black_1024x4096, 4100);
943 checkShrinkBitmap(512, 2048, R.drawable.black_1024x4096, 2048);
944
945 checkShrinkBitmap(4096, 1024, R.drawable.black_4096x1024, 4096);
946 checkShrinkBitmap(4096, 1024, R.drawable.black_4096x1024, 4100);
947 checkShrinkBitmap(2048, 512, R.drawable.black_4096x1024, 2048);
948 }
949
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700950 protected File openIconFileForWriteAndGetPath(int userId, String packageName)
Makoto Onuki55046222016-03-08 10:49:47 -0800951 throws IOException {
952 // Shortcut IDs aren't used in the path, so just pass the same ID.
953 final FileOutputStreamWithPath out =
954 mService.openIconFileForWrite(userId, makePackageShortcut(packageName, "id"));
955 out.close();
956 return out.getFile();
957 }
958
959 public void testOpenIconFileForWrite() throws IOException {
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700960 mInjectedCurrentTimeMillis = 1000;
Makoto Onuki55046222016-03-08 10:49:47 -0800961
962 final File p10_1_1 = openIconFileForWriteAndGetPath(10, CALLING_PACKAGE_1);
963 final File p10_1_2 = openIconFileForWriteAndGetPath(10, CALLING_PACKAGE_1);
964
965 final File p10_2_1 = openIconFileForWriteAndGetPath(10, CALLING_PACKAGE_2);
966 final File p10_2_2 = openIconFileForWriteAndGetPath(10, CALLING_PACKAGE_2);
967
968 final File p11_1_1 = openIconFileForWriteAndGetPath(11, CALLING_PACKAGE_1);
969 final File p11_1_2 = openIconFileForWriteAndGetPath(11, CALLING_PACKAGE_1);
970
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700971 mInjectedCurrentTimeMillis++;
Makoto Onuki55046222016-03-08 10:49:47 -0800972
973 final File p10_1_3 = openIconFileForWriteAndGetPath(10, CALLING_PACKAGE_1);
974 final File p10_1_4 = openIconFileForWriteAndGetPath(10, CALLING_PACKAGE_1);
975 final File p10_1_5 = openIconFileForWriteAndGetPath(10, CALLING_PACKAGE_1);
976
977 final File p10_2_3 = openIconFileForWriteAndGetPath(10, CALLING_PACKAGE_2);
978 final File p11_1_3 = openIconFileForWriteAndGetPath(11, CALLING_PACKAGE_1);
979
980 // Make sure their paths are all unique
Makoto Onukid99c6f02016-03-28 11:02:54 -0700981 assertAllUnique(list(
Makoto Onuki55046222016-03-08 10:49:47 -0800982 p10_1_1,
983 p10_1_2,
984 p10_1_3,
985 p10_1_4,
986 p10_1_5,
987
988 p10_2_1,
989 p10_2_2,
990 p10_2_3,
991
992 p11_1_1,
993 p11_1_2,
994 p11_1_3
995 ));
996
997 // Check each set has the same parent.
998 assertEquals(p10_1_1.getParent(), p10_1_2.getParent());
999 assertEquals(p10_1_1.getParent(), p10_1_3.getParent());
1000 assertEquals(p10_1_1.getParent(), p10_1_4.getParent());
1001 assertEquals(p10_1_1.getParent(), p10_1_5.getParent());
1002
1003 assertEquals(p10_2_1.getParent(), p10_2_2.getParent());
1004 assertEquals(p10_2_1.getParent(), p10_2_3.getParent());
1005
1006 assertEquals(p11_1_1.getParent(), p11_1_2.getParent());
1007 assertEquals(p11_1_1.getParent(), p11_1_3.getParent());
1008
1009 // Check the parents are still unique.
Makoto Onukid99c6f02016-03-28 11:02:54 -07001010 assertAllUnique(list(
Makoto Onuki55046222016-03-08 10:49:47 -08001011 p10_1_1.getParent(),
1012 p10_2_1.getParent(),
1013 p11_1_1.getParent()
1014 ));
1015
1016 // All files created at the same time for the same package/user, expcet for the first ones,
1017 // will have "_" in the path.
1018 assertFalse(p10_1_1.getName().contains("_"));
1019 assertTrue(p10_1_2.getName().contains("_"));
1020 assertFalse(p10_1_3.getName().contains("_"));
1021 assertTrue(p10_1_4.getName().contains("_"));
1022 assertTrue(p10_1_5.getName().contains("_"));
1023
1024 assertFalse(p10_2_1.getName().contains("_"));
1025 assertTrue(p10_2_2.getName().contains("_"));
1026 assertFalse(p10_2_3.getName().contains("_"));
1027
1028 assertFalse(p11_1_1.getName().contains("_"));
1029 assertTrue(p11_1_2.getName().contains("_"));
1030 assertFalse(p11_1_3.getName().contains("_"));
1031 }
1032
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001033 public void testUpdateShortcuts() {
1034 runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
Makoto Onukid99c6f02016-03-28 11:02:54 -07001035 assertTrue(mManager.setDynamicShortcuts(list(
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001036 makeShortcut("s1"),
1037 makeShortcut("s2"),
1038 makeShortcut("s3"),
1039 makeShortcut("s4"),
Makoto Onuki99302b52017-03-29 12:42:26 -07001040 makeShortcut("s5")
1041 )));
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001042 });
1043 runWithCaller(CALLING_PACKAGE_2, UserHandle.USER_SYSTEM, () -> {
Makoto Onukid99c6f02016-03-28 11:02:54 -07001044 assertTrue(mManager.setDynamicShortcuts(list(
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001045 makeShortcut("s1"),
1046 makeShortcut("s2"),
1047 makeShortcut("s3"),
1048 makeShortcut("s4"),
Makoto Onuki99302b52017-03-29 12:42:26 -07001049 makeShortcut("s5")
1050 )));
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001051 });
1052 runWithCaller(LAUNCHER_1, UserHandle.USER_SYSTEM, () -> {
Makoto Onuki99302b52017-03-29 12:42:26 -07001053 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s2", "s3"),
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001054 getCallingUser());
Makoto Onukid99c6f02016-03-28 11:02:54 -07001055 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s4", "s5"),
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001056 getCallingUser());
1057 });
1058 runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
Makoto Onukib6d35232016-04-04 15:57:17 -07001059 mManager.removeDynamicShortcuts(list("s1"));
1060 mManager.removeDynamicShortcuts(list("s2"));
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001061 });
1062 runWithCaller(CALLING_PACKAGE_2, UserHandle.USER_SYSTEM, () -> {
Makoto Onukib6d35232016-04-04 15:57:17 -07001063 mManager.removeDynamicShortcuts(list("s1"));
1064 mManager.removeDynamicShortcuts(list("s3"));
1065 mManager.removeDynamicShortcuts(list("s5"));
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001066 });
1067 runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
1068 assertShortcutIds(assertAllDynamic(
1069 mManager.getDynamicShortcuts()),
Makoto Onuki99302b52017-03-29 12:42:26 -07001070 "s3", "s4", "s5");
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001071 assertShortcutIds(assertAllPinned(
1072 mManager.getPinnedShortcuts()),
Makoto Onuki99302b52017-03-29 12:42:26 -07001073 "s2", "s3");
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001074 });
1075 runWithCaller(CALLING_PACKAGE_2, UserHandle.USER_SYSTEM, () -> {
1076 assertShortcutIds(assertAllDynamic(
1077 mManager.getDynamicShortcuts()),
Makoto Onuki99302b52017-03-29 12:42:26 -07001078 "s2", "s4");
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001079 assertShortcutIds(assertAllPinned(
1080 mManager.getPinnedShortcuts()),
1081 "s4", "s5");
1082 });
1083
1084 runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
1085 ShortcutInfo s2 = makeShortcutBuilder()
1086 .setId("s2")
Makoto Onuki4dbe0de2016-03-14 17:31:49 -07001087 .setIcon(Icon.createWithResource(getTestContext(), R.drawable.black_32x32))
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001088 .build();
1089
1090 ShortcutInfo s4 = makeShortcutBuilder()
1091 .setId("s4")
1092 .setTitle("new title")
1093 .build();
1094
Makoto Onukid99c6f02016-03-28 11:02:54 -07001095 mManager.updateShortcuts(list(s2, s4));
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001096 });
1097 runWithCaller(CALLING_PACKAGE_2, UserHandle.USER_SYSTEM, () -> {
1098 ShortcutInfo s2 = makeShortcutBuilder()
1099 .setId("s2")
1100 .setIntent(makeIntent(Intent.ACTION_ANSWER, ShortcutActivity.class,
1101 "key1", "val1"))
1102 .build();
1103
1104 ShortcutInfo s4 = makeShortcutBuilder()
1105 .setId("s4")
1106 .setIntent(new Intent(Intent.ACTION_ALL_APPS))
1107 .build();
1108
Makoto Onukid99c6f02016-03-28 11:02:54 -07001109 mManager.updateShortcuts(list(s2, s4));
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001110 });
1111
1112 runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
1113 assertShortcutIds(assertAllDynamic(
1114 mManager.getDynamicShortcuts()),
Makoto Onuki99302b52017-03-29 12:42:26 -07001115 "s3", "s4", "s5");
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001116 assertShortcutIds(assertAllPinned(
1117 mManager.getPinnedShortcuts()),
Makoto Onuki99302b52017-03-29 12:42:26 -07001118 "s2", "s3");
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001119
1120 ShortcutInfo s = getCallerShortcut("s2");
1121 assertTrue(s.hasIconResource());
1122 assertEquals(R.drawable.black_32x32, s.getIconResourceId());
Makoto Onuki157b1622016-06-02 16:13:10 -07001123 assertEquals("string/r" + R.drawable.black_32x32, s.getIconResName());
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001124 assertEquals("Title-s2", s.getTitle());
1125
1126 s = getCallerShortcut("s4");
1127 assertFalse(s.hasIconResource());
1128 assertEquals(0, s.getIconResourceId());
1129 assertEquals("new title", s.getTitle());
1130 });
1131 runWithCaller(CALLING_PACKAGE_2, UserHandle.USER_SYSTEM, () -> {
1132 assertShortcutIds(assertAllDynamic(
1133 mManager.getDynamicShortcuts()),
Makoto Onuki99302b52017-03-29 12:42:26 -07001134 "s2", "s4");
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001135 assertShortcutIds(assertAllPinned(
1136 mManager.getPinnedShortcuts()),
1137 "s4", "s5");
1138
1139 ShortcutInfo s = getCallerShortcut("s2");
1140 assertFalse(s.hasIconResource());
1141 assertEquals(0, s.getIconResourceId());
1142 assertEquals("Title-s2", s.getTitle());
1143 assertEquals(Intent.ACTION_ANSWER, s.getIntent().getAction());
1144 assertEquals(1, s.getIntent().getExtras().size());
1145
1146 s = getCallerShortcut("s4");
1147 assertFalse(s.hasIconResource());
1148 assertEquals(0, s.getIconResourceId());
1149 assertEquals("Title-s4", s.getTitle());
1150 assertEquals(Intent.ACTION_ALL_APPS, s.getIntent().getAction());
1151 assertBundleEmpty(s.getIntent().getExtras());
1152 });
1153 // TODO Check with other fields too.
1154
1155 // TODO Check bitmap removal too.
Makoto Onuki0acbb142016-03-22 17:02:57 -07001156
Makoto Onuki9c850012016-07-26 15:50:50 -07001157 mRunningUsers.put(USER_11, true);
1158
Makoto Onuki0acbb142016-03-22 17:02:57 -07001159 runWithCaller(CALLING_PACKAGE_2, USER_11, () -> {
Makoto Onukid99c6f02016-03-28 11:02:54 -07001160 mManager.updateShortcuts(list());
Makoto Onuki0acbb142016-03-22 17:02:57 -07001161 });
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001162 }
1163
Makoto Onukidd097812016-06-29 13:10:09 -07001164 public void testUpdateShortcuts_icons() {
1165 runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
1166 assertTrue(mManager.setDynamicShortcuts(list(
1167 makeShortcut("s1")
1168 )));
1169
1170 // Set resource icon
1171 assertTrue(mManager.updateShortcuts(list(
1172 new ShortcutInfo.Builder(mClientContext, "s1")
Makoto Onukia4f89b12017-10-05 10:37:55 -07001173 .setIcon(Icon.createWithResource(getTestContext(), R.drawable.black_32x32))
1174 .build()
Makoto Onukidd097812016-06-29 13:10:09 -07001175 )));
Makoto Onukiffa519c2017-12-13 13:14:38 -08001176 mService.waitForBitmapSavesForTest();
Makoto Onukidd097812016-06-29 13:10:09 -07001177 assertWith(getCallerShortcuts())
1178 .forShortcutWithId("s1", si -> {
1179 assertTrue(si.hasIconResource());
1180 assertEquals(R.drawable.black_32x32, si.getIconResourceId());
1181 });
Makoto Onukiffa519c2017-12-13 13:14:38 -08001182 mService.waitForBitmapSavesForTest();
Makoto Onukidd097812016-06-29 13:10:09 -07001183 // Set bitmap icon
1184 assertTrue(mManager.updateShortcuts(list(
1185 new ShortcutInfo.Builder(mClientContext, "s1")
Makoto Onukia4f89b12017-10-05 10:37:55 -07001186 .setIcon(Icon.createWithBitmap(BitmapFactory.decodeResource(
1187 getTestContext().getResources(), R.drawable.black_64x64)))
1188 .build()
Makoto Onukidd097812016-06-29 13:10:09 -07001189 )));
Makoto Onukiffa519c2017-12-13 13:14:38 -08001190 mService.waitForBitmapSavesForTest();
Makoto Onukidd097812016-06-29 13:10:09 -07001191 assertWith(getCallerShortcuts())
1192 .forShortcutWithId("s1", si -> {
1193 assertTrue(si.hasIconFile());
1194 });
1195
1196 mInjectedCurrentTimeMillis += INTERVAL; // reset throttling
1197
1198 // Do it again, with the reverse order (bitmap -> icon)
1199 assertTrue(mManager.setDynamicShortcuts(list(
1200 makeShortcut("s1")
1201 )));
1202
1203 // Set bitmap icon
1204 assertTrue(mManager.updateShortcuts(list(
1205 new ShortcutInfo.Builder(mClientContext, "s1")
1206 .setIcon(Icon.createWithBitmap(BitmapFactory.decodeResource(
1207 getTestContext().getResources(), R.drawable.black_64x64)))
1208 .build()
1209 )));
Makoto Onukiffa519c2017-12-13 13:14:38 -08001210 mService.waitForBitmapSavesForTest();
Makoto Onukidd097812016-06-29 13:10:09 -07001211 assertWith(getCallerShortcuts())
1212 .forShortcutWithId("s1", si -> {
1213 assertTrue(si.hasIconFile());
1214 });
1215
1216 // Set resource icon
1217 assertTrue(mManager.updateShortcuts(list(
1218 new ShortcutInfo.Builder(mClientContext, "s1")
1219 .setIcon(Icon.createWithResource(getTestContext(), R.drawable.black_32x32))
1220 .build()
1221 )));
Makoto Onukiffa519c2017-12-13 13:14:38 -08001222 mService.waitForBitmapSavesForTest();
Makoto Onukidd097812016-06-29 13:10:09 -07001223 assertWith(getCallerShortcuts())
1224 .forShortcutWithId("s1", si -> {
1225 assertTrue(si.hasIconResource());
1226 assertEquals(R.drawable.black_32x32, si.getIconResourceId());
1227 });
1228 });
1229 }
1230
Mehdi Alizadeh505fb762020-01-21 17:26:24 -08001231 public void testShortcutManagerGetShortcuts_shortcutTypes() {
1232
1233 // Create 3 manifest and 3 dynamic shortcuts
1234 addManifestShortcutResource(
1235 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
1236 R.xml.shortcut_3);
1237 updatePackageVersion(CALLING_PACKAGE_1, 1);
1238 mService.mPackageMonitor.onReceive(getTestContext(),
1239 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
1240
1241 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1242 assertTrue(mManager.setDynamicShortcuts(list(
Mehdi Alizadeh0de8c292020-01-21 17:27:26 -08001243 makeLongLivedShortcut("s1"), makeLongLivedShortcut("s2"), makeShortcut("s3"))));
Mehdi Alizadeh505fb762020-01-21 17:26:24 -08001244 });
1245
1246 // Pin 2 and 3
1247 runWithCaller(LAUNCHER_1, USER_0, () -> {
1248 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms2", "ms3", "s2", "s3"),
1249 HANDLE_USER_0);
1250 });
1251
1252 // Cache 1 and 2
Mehdi Alizadeh0de8c292020-01-21 17:27:26 -08001253 runWithCaller(LAUNCHER_1, USER_0, () -> {
1254 mLauncherApps.cacheShortcuts(CALLING_PACKAGE_1, list("s1", "s2"),
1255 HANDLE_USER_0);
1256 });
1257
Mehdi Alizadeh505fb762020-01-21 17:26:24 -08001258 setCaller(CALLING_PACKAGE_1);
Mehdi Alizadeh505fb762020-01-21 17:26:24 -08001259
1260 // Get manifest shortcuts
1261 assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_MANIFEST),
1262 "ms1", "ms2", "ms3");
1263
1264 // Get dynamic shortcuts
1265 assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_DYNAMIC),
1266 "s1", "s2", "s3");
1267
1268 // Get pinned shortcuts
1269 assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_PINNED),
1270 "ms2", "ms3", "s2", "s3");
1271
1272 // Get cached shortcuts
1273 assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_CACHED),
1274 "s1", "s2");
1275
1276 // Get manifest and dynamic shortcuts
1277 assertShortcutIds(mManager.getShortcuts(
1278 ShortcutManager.FLAG_MATCH_MANIFEST | ShortcutManager.FLAG_MATCH_DYNAMIC),
1279 "ms1", "ms2", "ms3", "s1", "s2", "s3");
1280
1281 // Get manifest and pinned shortcuts
1282 assertShortcutIds(mManager.getShortcuts(
1283 ShortcutManager.FLAG_MATCH_MANIFEST | ShortcutManager.FLAG_MATCH_PINNED),
1284 "ms1", "ms2", "ms3", "s2", "s3");
1285
1286 // Get manifest and cached shortcuts
1287 assertShortcutIds(mManager.getShortcuts(
1288 ShortcutManager.FLAG_MATCH_MANIFEST | ShortcutManager.FLAG_MATCH_CACHED),
1289 "ms1", "ms2", "ms3", "s1", "s2");
1290
1291 // Get dynamic and pinned shortcuts
1292 assertShortcutIds(mManager.getShortcuts(
1293 ShortcutManager.FLAG_MATCH_DYNAMIC | ShortcutManager.FLAG_MATCH_PINNED),
1294 "ms2", "ms3", "s1", "s2", "s3");
1295
1296 // Get dynamic and cached shortcuts
1297 assertShortcutIds(mManager.getShortcuts(
1298 ShortcutManager.FLAG_MATCH_DYNAMIC | ShortcutManager.FLAG_MATCH_CACHED),
1299 "s1", "s2", "s3");
1300
1301 // Get pinned and cached shortcuts
1302 assertShortcutIds(mManager.getShortcuts(
1303 ShortcutManager.FLAG_MATCH_PINNED | ShortcutManager.FLAG_MATCH_CACHED),
1304 "ms2", "ms3", "s1", "s2", "s3");
1305
1306 // Remove a dynamic cached shortcut
1307 mManager.removeDynamicShortcuts(list("s1"));
1308 assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_DYNAMIC), "s2", "s3");
1309 assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_CACHED), "s1", "s2");
1310
1311 // Remove a dynamic cached and pinned shortcut
1312 mManager.removeDynamicShortcuts(list("s2"));
1313 assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_DYNAMIC), "s3");
1314 assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_PINNED),
1315 "ms2", "ms3", "s2", "s3");
1316 assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_CACHED), "s1", "s2");
1317 }
1318
1319 public void testCachedShortcuts() {
1320 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
Mehdi Alizadeh0de8c292020-01-21 17:27:26 -08001321 assertTrue(mManager.setDynamicShortcuts(list(makeShortcut("s1"),
1322 makeLongLivedShortcut("s2"), makeLongLivedShortcut("s3"),
1323 makeLongLivedShortcut("s4"))));
Mehdi Alizadeh505fb762020-01-21 17:26:24 -08001324 });
1325
1326 // Pin s2
1327 runWithCaller(LAUNCHER_1, USER_0, () -> {
1328 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s2"),
1329 HANDLE_USER_0);
1330 });
1331
Mehdi Alizadeh0de8c292020-01-21 17:27:26 -08001332 // Cache some, but non long lived shortcuts will be ignored.
1333 runWithCaller(LAUNCHER_1, USER_0, () -> {
1334 mLauncherApps.cacheShortcuts(CALLING_PACKAGE_1, list("s1", "s2", "s4"),
1335 HANDLE_USER_0);
1336 });
1337
Mehdi Alizadeh505fb762020-01-21 17:26:24 -08001338 setCaller(CALLING_PACKAGE_1);
Mehdi Alizadeh505fb762020-01-21 17:26:24 -08001339
1340 // Get dynamic shortcuts
1341 assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_DYNAMIC),
1342 "s1", "s2", "s3", "s4");
1343 // Get pinned shortcuts
1344 assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_PINNED),
1345 "s2");
1346 // Get cached shortcuts
1347 assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_CACHED),
Mehdi Alizadeh0de8c292020-01-21 17:27:26 -08001348 "s2", "s4");
Mehdi Alizadeh505fb762020-01-21 17:26:24 -08001349
1350 // Remove a dynamic cached shortcut
Mehdi Alizadeh0de8c292020-01-21 17:27:26 -08001351 mManager.removeDynamicShortcuts(list("s4"));
Mehdi Alizadeh505fb762020-01-21 17:26:24 -08001352 assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_DYNAMIC),
Mehdi Alizadeh0de8c292020-01-21 17:27:26 -08001353 "s1", "s2", "s3");
Mehdi Alizadeh505fb762020-01-21 17:26:24 -08001354 assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_CACHED),
Mehdi Alizadeh0de8c292020-01-21 17:27:26 -08001355 "s2", "s4");
Mehdi Alizadeh505fb762020-01-21 17:26:24 -08001356
Mehdi Alizadeh0de8c292020-01-21 17:27:26 -08001357 // uncache a non-dynamic shortcut. Should be removed.
1358 runWithCaller(LAUNCHER_1, USER_0, () -> {
1359 mLauncherApps.uncacheShortcuts(CALLING_PACKAGE_1, list("s4"),
1360 HANDLE_USER_0);
1361 });
Mehdi Alizadeh505fb762020-01-21 17:26:24 -08001362 assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_CACHED),
1363 "s2");
1364
Mehdi Alizadeh0de8c292020-01-21 17:27:26 -08001365 // Cache another shortcut
1366 runWithCaller(LAUNCHER_1, USER_0, () -> {
1367 mLauncherApps.cacheShortcuts(CALLING_PACKAGE_1, list("s3"),
1368 HANDLE_USER_0);
1369 });
1370 assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_CACHED),
1371 "s2", "s3");
1372
Mehdi Alizadeh505fb762020-01-21 17:26:24 -08001373 // Remove a dynamic cached pinned long lived shortcut
1374 mManager.removeLongLivedShortcuts(list("s2"));
1375 assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_DYNAMIC),
Mehdi Alizadeh0de8c292020-01-21 17:27:26 -08001376 "s1", "s3");
1377 assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_CACHED),
1378 "s3");
Mehdi Alizadeh505fb762020-01-21 17:26:24 -08001379 assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_PINNED),
1380 "s2");
1381 }
1382
Makoto Onuki99302b52017-03-29 12:42:26 -07001383 // === Test for launcher side APIs ===
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001384
1385 public void testGetShortcuts() {
1386
1387 // Set up shortcuts.
1388
1389 setCaller(CALLING_PACKAGE_1);
Mehdi Alizadeh0de8c292020-01-21 17:27:26 -08001390 final ShortcutInfo s1_1 = makeLongLivedShortcut("s1");
Mehdi Alizadehae808ff2020-01-21 13:39:53 -08001391 final ShortcutInfo s1_2 = makeShortcutWithLocusId("s2", makeLocusId("l1"));
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001392
Makoto Onukid99c6f02016-03-28 11:02:54 -07001393 assertTrue(mManager.setDynamicShortcuts(list(s1_1, s1_2)));
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001394
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001395 // Because setDynamicShortcuts will update the timestamps when ranks are changing,
1396 // we explicitly set timestamps here.
1397 getCallerShortcut("s1").setTimestamp(5000);
1398 getCallerShortcut("s2").setTimestamp(1000);
1399
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001400 setCaller(CALLING_PACKAGE_2);
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001401 final ShortcutInfo s2_2 = makeShortcut("s2");
1402 final ShortcutInfo s2_3 = makeShortcutWithActivity("s3",
Makoto Onuki85694522016-05-04 12:53:37 -07001403 makeComponent(ShortcutActivity2.class));
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001404 final ShortcutInfo s2_4 = makeShortcutWithActivity("s4",
Makoto Onuki85694522016-05-04 12:53:37 -07001405 makeComponent(ShortcutActivity.class));
Makoto Onukid99c6f02016-03-28 11:02:54 -07001406 assertTrue(mManager.setDynamicShortcuts(list(s2_2, s2_3, s2_4)));
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001407
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001408 getCallerShortcut("s2").setTimestamp(1500);
1409 getCallerShortcut("s3").setTimestamp(3000);
1410 getCallerShortcut("s4").setTimestamp(500);
1411
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001412 setCaller(CALLING_PACKAGE_3);
Mehdi Alizadehae808ff2020-01-21 13:39:53 -08001413 final ShortcutInfo s3_2 = makeShortcutWithLocusId("s3", makeLocusId("l2"));
Mehdi Alizadeh0de8c292020-01-21 17:27:26 -08001414 s3_2.setLongLived();
1415
Makoto Onukid99c6f02016-03-28 11:02:54 -07001416 assertTrue(mManager.setDynamicShortcuts(list(s3_2)));
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001417
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001418 getCallerShortcut("s3").setTimestamp(START_TIME + 5000);
1419
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001420 setCaller(LAUNCHER_1);
1421
1422 // Get dynamic
Makoto Onuki20c95f82016-05-11 16:51:01 -07001423 assertAllDynamic(assertAllHaveTitle(assertAllNotHaveIntents(assertAllStringsResolved(
1424 assertShortcutIds(
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001425 assertAllNotKeyFieldsOnly(
1426 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001427 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001428 "s1", "s2")))));
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001429
1430 // Get pinned
1431 assertShortcutIds(
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001432 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001433 /* activity =*/ null,
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001434 ShortcutQuery.FLAG_GET_PINNED), getCallingUser())
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001435 /* none */);
1436
1437 // Get both, with timestamp
1438 assertAllDynamic(assertAllHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001439 assertAllNotKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
Makoto Onuki55046222016-03-08 10:49:47 -08001440 /* time =*/ 1000, CALLING_PACKAGE_2,
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001441 /* activity =*/ null,
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001442 ShortcutQuery.FLAG_GET_PINNED | ShortcutQuery.FLAG_GET_DYNAMIC),
1443 getCallingUser())),
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001444 "s2", "s3"))));
1445
1446 // FLAG_GET_KEY_FIELDS_ONLY
1447 assertAllDynamic(assertAllNotHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001448 assertAllKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
Makoto Onuki55046222016-03-08 10:49:47 -08001449 /* time =*/ 1000, CALLING_PACKAGE_2,
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001450 /* activity =*/ null,
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001451 ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY),
1452 getCallingUser())),
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001453 "s2", "s3"))));
1454
Makoto Onuki85694522016-05-04 12:53:37 -07001455 // Filter by activity
1456 assertAllDynamic(assertAllHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
1457 assertAllNotKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
1458 /* time =*/ 0, CALLING_PACKAGE_2,
1459 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
1460 ShortcutQuery.FLAG_GET_PINNED | ShortcutQuery.FLAG_GET_DYNAMIC),
1461 getCallingUser())),
Makoto Onuki20c95f82016-05-11 16:51:01 -07001462 "s4"))));
Makoto Onuki85694522016-05-04 12:53:37 -07001463
Makoto Onukiabe84422016-04-07 09:41:19 -07001464 // With ID.
1465 assertAllDynamic(assertAllNotHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
1466 assertAllKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
Mehdi Alizadehae808ff2020-01-21 13:39:53 -08001467 /* time =*/ 1000, CALLING_PACKAGE_2, list("s3"), /* locusIds =*/ null,
Makoto Onukiabe84422016-04-07 09:41:19 -07001468 /* activity =*/ null,
1469 ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY),
1470 getCallingUser())),
1471 "s3"))));
1472 assertAllDynamic(assertAllNotHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
1473 assertAllKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
1474 /* time =*/ 1000, CALLING_PACKAGE_2, list("s3", "s2", "ss"),
Mehdi Alizadehae808ff2020-01-21 13:39:53 -08001475 /* locusIds =*/ null, /* activity =*/ null,
Makoto Onukiabe84422016-04-07 09:41:19 -07001476 ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY),
1477 getCallingUser())),
1478 "s2", "s3"))));
1479 assertAllDynamic(assertAllNotHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
1480 assertAllKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
1481 /* time =*/ 1000, CALLING_PACKAGE_2, list("s3x", "s2x"),
Mehdi Alizadehae808ff2020-01-21 13:39:53 -08001482 /* locusIds =*/ null, /* activity =*/ null,
Makoto Onukiabe84422016-04-07 09:41:19 -07001483 ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY),
1484 getCallingUser()))
1485 /* empty */))));
1486 assertAllDynamic(assertAllNotHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
1487 assertAllKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
Mehdi Alizadehae808ff2020-01-21 13:39:53 -08001488 /* time =*/ 1000, CALLING_PACKAGE_2, list(), /* locusIds =*/ null,
1489 /* activity =*/ null,
1490 ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY),
1491 getCallingUser()))
1492 /* empty */))));
1493
1494 // With locus ID.
1495 assertAllDynamic(assertAllNotHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
1496 assertAllKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
1497 /* time =*/ 1000, CALLING_PACKAGE_3, /* shortcutIds =*/ null,
1498 list(makeLocusId("l2")), /* activity =*/ null,
1499 ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY),
1500 getCallingUser())),
1501 "s3"))));
1502 assertAllDynamic(assertAllNotHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
1503 assertAllKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
1504 /* time =*/ 1000, CALLING_PACKAGE_1, /* shortcutIds =*/ null,
1505 list(makeLocusId("l1"), makeLocusId("l2"), makeLocusId("l3")),
1506 /* activity =*/ null,
1507 ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY),
1508 getCallingUser())),
1509 "s2"))));
1510 assertAllDynamic(assertAllNotHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
1511 assertAllKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
1512 /* time =*/ 1000, CALLING_PACKAGE_1, /* shortcutIds =*/ null,
1513 list(makeLocusId("lx1"), makeLocusId("lx2")), /* activity =*/ null,
1514 ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY),
1515 getCallingUser()))
1516 /* empty */))));
1517 assertAllDynamic(assertAllNotHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
1518 assertAllKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
1519 /* time =*/ 1000, CALLING_PACKAGE_3, /* shortcutIds =*/ null, list(),
Makoto Onukiabe84422016-04-07 09:41:19 -07001520 /* activity =*/ null,
1521 ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY),
1522 getCallingUser()))
1523 /* empty */))));
1524
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001525 // Pin some shortcuts.
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001526 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
Makoto Onukid99c6f02016-03-28 11:02:54 -07001527 list("s3", "s4"), getCallingUser());
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001528
1529 // Pinned ones only
1530 assertAllPinned(assertAllHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001531 assertAllNotKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
Makoto Onuki55046222016-03-08 10:49:47 -08001532 /* time =*/ 1000, CALLING_PACKAGE_2,
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001533 /* activity =*/ null,
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001534 ShortcutQuery.FLAG_GET_PINNED),
1535 getCallingUser())),
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001536 "s3"))));
1537
1538 // All packages.
Makoto Onuki55046222016-03-08 10:49:47 -08001539 assertShortcutIds(assertAllNotKeyFieldsOnly(
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001540 mLauncherApps.getShortcuts(buildQuery(
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001541 /* time =*/ 5000, /* package= */ null,
1542 /* activity =*/ null,
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001543 ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_PINNED),
1544 getCallingUser())),
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001545 "s1", "s3");
1546
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07001547 assertExpectException(
Makoto Onukiabe84422016-04-07 09:41:19 -07001548 IllegalArgumentException.class, "package name must also be set", () -> {
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001549 mLauncherApps.getShortcuts(buildQuery(
Mehdi Alizadehae808ff2020-01-21 13:39:53 -08001550 /* time =*/ 0, /* package= */ null, list("id"), /* locusIds =*/ null,
Makoto Onukiabe84422016-04-07 09:41:19 -07001551 /* activity =*/ null, /* flags */ 0), getCallingUser());
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001552 });
Makoto Onukiabe84422016-04-07 09:41:19 -07001553
Makoto Onuki9fd90192017-01-06 18:31:03 +00001554 // TODO More tests: pinned but dynamic.
Mehdi Alizadeh505fb762020-01-21 17:26:24 -08001555
Mehdi Alizadeh505fb762020-01-21 17:26:24 -08001556 setCaller(LAUNCHER_1);
1557
Mehdi Alizadeh0de8c292020-01-21 17:27:26 -08001558 // Cache some shortcuts. Only long lived shortcuts can get cached.
1559 mLauncherApps.cacheShortcuts(CALLING_PACKAGE_1, list("s1"), getCallingUser());
1560 mLauncherApps.cacheShortcuts(CALLING_PACKAGE_3, list("s3"), getCallingUser());
1561
Mehdi Alizadeh505fb762020-01-21 17:26:24 -08001562 // Cached ones only
1563 assertShortcutIds(assertAllNotKeyFieldsOnly(
1564 mLauncherApps.getShortcuts(buildQuery(
Mehdi Alizadeh0de8c292020-01-21 17:27:26 -08001565 /* time =*/ 0, CALLING_PACKAGE_3,
Mehdi Alizadeh505fb762020-01-21 17:26:24 -08001566 /* activity =*/ null,
1567 ShortcutQuery.FLAG_MATCH_CACHED),
1568 getCallingUser())),
Mehdi Alizadeh0de8c292020-01-21 17:27:26 -08001569 "s3");
Mehdi Alizadeh505fb762020-01-21 17:26:24 -08001570
1571 // All packages.
1572 assertShortcutIds(assertAllNotKeyFieldsOnly(
1573 mLauncherApps.getShortcuts(buildQuery(
1574 /* time =*/ 0, /* package= */ null,
1575 /* activity =*/ null,
1576 ShortcutQuery.FLAG_MATCH_CACHED),
1577 getCallingUser())),
Mehdi Alizadeh0de8c292020-01-21 17:27:26 -08001578 "s1", "s3");
Mehdi Alizadeh505fb762020-01-21 17:26:24 -08001579
1580 assertExpectException(
1581 IllegalArgumentException.class, "package name must also be set", () -> {
1582 mLauncherApps.getShortcuts(buildQuery(
Mehdi Alizadehae808ff2020-01-21 13:39:53 -08001583 /* time =*/ 0, /* package= */ null, list("id"), /* locusIds= */ null,
Mehdi Alizadeh505fb762020-01-21 17:26:24 -08001584 /* activity =*/ null, /* flags */ 0), getCallingUser());
1585 });
1586
1587 // Change Launcher. Cached shortcuts are the same for all launchers.
1588 setCaller(LAUNCHER_2);
1589 // All packages.
1590 assertShortcutIds(assertAllNotKeyFieldsOnly(
1591 mLauncherApps.getShortcuts(buildQuery(
1592 /* time =*/ 0, /* package= */ null,
1593 /* activity =*/ null,
1594 ShortcutQuery.FLAG_MATCH_CACHED),
1595 getCallingUser())),
Mehdi Alizadeh0de8c292020-01-21 17:27:26 -08001596 "s1", "s3");
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001597 }
1598
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07001599 public void testGetShortcuts_shortcutKinds() throws Exception {
1600 // Create 3 manifest and 3 dynamic shortcuts
1601 addManifestShortcutResource(
1602 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
1603 R.xml.shortcut_3);
1604 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07001605 mService.mPackageMonitor.onReceive(getTestContext(),
1606 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07001607
1608 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1609 assertTrue(mManager.setDynamicShortcuts(list(
1610 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
1611 });
1612
1613 // Pin 2 and 3
1614 runWithCaller(LAUNCHER_1, USER_0, () -> {
1615 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms2", "ms3", "s2", "s3"),
1616 HANDLE_USER_0);
1617 });
1618
1619 // Remove ms3 and s3
1620 addManifestShortcutResource(
1621 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
1622 R.xml.shortcut_2);
1623 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07001624 mService.mPackageMonitor.onReceive(getTestContext(),
1625 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07001626
1627 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1628 assertTrue(mManager.setDynamicShortcuts(list(
1629 makeShortcut("s1"), makeShortcut("s2"))));
1630 });
1631
1632 // Check their status.
1633 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1634 assertWith(getCallerShortcuts())
1635 .haveIds("ms1", "ms2", "ms3", "s1", "s2", "s3")
1636
1637 .selectByIds("ms1", "ms2")
1638 .areAllManifest()
1639 .areAllImmutable()
1640 .areAllNotDynamic()
1641
1642 .revertToOriginalList()
1643 .selectByIds("ms3")
1644 .areAllNotManifest()
1645 .areAllImmutable()
1646 .areAllDisabled()
1647 .areAllNotDynamic()
1648
1649 .revertToOriginalList()
1650 .selectByIds("s1", "s2")
1651 .areAllNotManifest()
1652 .areAllMutable()
1653 .areAllDynamic()
1654
1655 .revertToOriginalList()
1656 .selectByIds("s3")
1657 .areAllNotManifest()
1658 .areAllMutable()
1659 .areAllEnabled()
1660 .areAllNotDynamic()
1661
1662 .revertToOriginalList()
1663 .selectByIds("s1", "ms1")
1664 .areAllNotPinned()
1665
1666 .revertToOriginalList()
1667 .selectByIds("s2", "s3", "ms2", "ms3")
1668 .areAllPinned()
1669 ;
1670 });
1671
1672 // Finally, actual tests.
1673 runWithCaller(LAUNCHER_1, USER_0, () -> {
1674 assertWith(mLauncherApps.getShortcuts(
1675 buildQueryWithFlags(ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0))
1676 .haveIds("s1", "s2");
1677 assertWith(mLauncherApps.getShortcuts(
1678 buildQueryWithFlags(ShortcutQuery.FLAG_GET_MANIFEST), HANDLE_USER_0))
1679 .haveIds("ms1", "ms2");
1680 assertWith(mLauncherApps.getShortcuts(
1681 buildQueryWithFlags(ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0))
1682 .haveIds("s2", "s3", "ms2", "ms3");
1683
1684 assertWith(mLauncherApps.getShortcuts(
1685 buildQueryWithFlags(
1686 ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_PINNED
1687 ), HANDLE_USER_0))
1688 .haveIds("s1", "s2", "s3", "ms2", "ms3");
1689
1690 assertWith(mLauncherApps.getShortcuts(
1691 buildQueryWithFlags(
1692 ShortcutQuery.FLAG_GET_MANIFEST | ShortcutQuery.FLAG_GET_PINNED
1693 ), HANDLE_USER_0))
1694 .haveIds("ms1", "s2", "s3", "ms2", "ms3");
1695
1696 assertWith(mLauncherApps.getShortcuts(
1697 buildQueryWithFlags(
1698 ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_MANIFEST
1699 ), HANDLE_USER_0))
1700 .haveIds("ms1", "ms2", "s1", "s2");
1701
1702 assertWith(mLauncherApps.getShortcuts(
1703 buildQueryWithFlags(
1704 ShortcutQuery.FLAG_GET_ALL_KINDS
1705 ), HANDLE_USER_0))
1706 .haveIds("ms1", "ms2", "ms3", "s1", "s2", "s3");
1707 });
1708 }
1709
Makoto Onuki20c95f82016-05-11 16:51:01 -07001710 public void testGetShortcuts_resolveStrings() throws Exception {
Makoto Onuki20c95f82016-05-11 16:51:01 -07001711 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1712 ShortcutInfo si = new ShortcutInfo.Builder(mClientContext)
1713 .setId("id")
Makoto Onuki22fcc682016-05-17 14:52:19 -07001714 .setActivity(new ComponentName(mClientContext, "dummy"))
Makoto Onuki20c95f82016-05-11 16:51:01 -07001715 .setTitleResId(10)
1716 .setTextResId(11)
1717 .setDisabledMessageResId(12)
1718 .setIntent(makeIntent("action", ShortcutActivity.class))
1719 .build();
1720 mManager.setDynamicShortcuts(list(si));
1721 });
1722 runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
1723 ShortcutInfo si = new ShortcutInfo.Builder(mClientContext)
1724 .setId("id")
Makoto Onuki22fcc682016-05-17 14:52:19 -07001725 .setActivity(new ComponentName(mClientContext, "dummy"))
Makoto Onuki20c95f82016-05-11 16:51:01 -07001726 .setTitleResId(10)
1727 .setTextResId(11)
1728 .setDisabledMessageResId(12)
1729 .setIntent(makeIntent("action", ShortcutActivity.class))
1730 .build();
1731 mManager.setDynamicShortcuts(list(si));
1732 });
1733
1734 runWithCaller(LAUNCHER_1, USER_0, () -> {
1735 final ShortcutQuery q = new ShortcutQuery();
1736 q.setQueryFlags(ShortcutQuery.FLAG_GET_DYNAMIC);
1737
1738 // USER 0
1739 List<ShortcutInfo> ret = assertShortcutIds(
1740 assertAllStringsResolved(mLauncherApps.getShortcuts(q, HANDLE_USER_0)),
1741 "id");
Makoto Onuki157b1622016-06-02 16:13:10 -07001742 assertEquals("string-com.android.test.1-user:0-res:10/en", ret.get(0).getTitle());
1743 assertEquals("string-com.android.test.1-user:0-res:11/en", ret.get(0).getText());
1744 assertEquals("string-com.android.test.1-user:0-res:12/en",
Makoto Onuki20c95f82016-05-11 16:51:01 -07001745 ret.get(0).getDisabledMessage());
1746
1747 // USER P0
1748 ret = assertShortcutIds(
1749 assertAllStringsResolved(mLauncherApps.getShortcuts(q, HANDLE_USER_P0)),
1750 "id");
Makoto Onuki157b1622016-06-02 16:13:10 -07001751 assertEquals("string-com.android.test.1-user:20-res:10/en", ret.get(0).getTitle());
1752 assertEquals("string-com.android.test.1-user:20-res:11/en", ret.get(0).getText());
1753 assertEquals("string-com.android.test.1-user:20-res:12/en",
Makoto Onuki20c95f82016-05-11 16:51:01 -07001754 ret.get(0).getDisabledMessage());
1755 });
1756 }
1757
1758 // TODO resource
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001759 public void testGetShortcutInfo() {
1760 // Create shortcuts.
1761 setCaller(CALLING_PACKAGE_1);
1762 final ShortcutInfo s1_1 = makeShortcut(
1763 "s1",
1764 "Title 1",
1765 makeComponent(ShortcutActivity.class),
1766 /* icon =*/ null,
1767 makeIntent(Intent.ACTION_ASSIST, ShortcutActivity2.class,
1768 "key1", "val1", "nest", makeBundle("key", 123)),
Makoto Onuki99302b52017-03-29 12:42:26 -07001769 /* weight */ 10);
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001770
1771 final ShortcutInfo s1_2 = makeShortcut(
Makoto Onuki99302b52017-03-29 12:42:26 -07001772 "s2",
1773 "Title 2",
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001774 /* activity */ null,
1775 /* icon =*/ null,
1776 makeIntent(Intent.ACTION_ASSIST, ShortcutActivity3.class),
Makoto Onuki99302b52017-03-29 12:42:26 -07001777 /* weight */ 12);
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001778
Makoto Onukid99c6f02016-03-28 11:02:54 -07001779 assertTrue(mManager.setDynamicShortcuts(list(s1_1, s1_2)));
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001780 dumpsysOnLogcat();
1781
1782 setCaller(CALLING_PACKAGE_2);
1783 final ShortcutInfo s2_1 = makeShortcut(
1784 "s1",
1785 "ABC",
1786 makeComponent(ShortcutActivity2.class),
1787 /* icon =*/ null,
1788 makeIntent(Intent.ACTION_ANSWER, ShortcutActivity2.class,
1789 "key1", "val1", "nest", makeBundle("key", 123)),
Makoto Onuki99302b52017-03-29 12:42:26 -07001790 /* weight */ 10);
Makoto Onukid99c6f02016-03-28 11:02:54 -07001791 assertTrue(mManager.setDynamicShortcuts(list(s2_1)));
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001792 dumpsysOnLogcat();
1793
1794 // Pin some.
1795 setCaller(LAUNCHER_1);
1796
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001797 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
Makoto Onukid99c6f02016-03-28 11:02:54 -07001798 list("s2"), getCallingUser());
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001799
1800 dumpsysOnLogcat();
1801
1802 // Delete some.
1803 setCaller(CALLING_PACKAGE_1);
1804 assertShortcutIds(mManager.getPinnedShortcuts(), "s2");
Makoto Onukib6d35232016-04-04 15:57:17 -07001805 mManager.removeDynamicShortcuts(list("s2"));
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001806 assertShortcutIds(mManager.getPinnedShortcuts(), "s2");
1807
1808 dumpsysOnLogcat();
1809
1810 setCaller(LAUNCHER_1);
1811 List<ShortcutInfo> list;
1812
1813 // Note we don't guarantee the orders.
1814 list = assertShortcutIds(assertAllHaveTitle(assertAllNotHaveIntents(
Makoto Onuki55046222016-03-08 10:49:47 -08001815 assertAllNotKeyFieldsOnly(
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001816 mLauncherApps.getShortcutInfo(CALLING_PACKAGE_1,
1817 list("s2", "s1", "s3", null), getCallingUser())))),
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001818 "s1", "s2");
1819 assertEquals("Title 1", findById(list, "s1").getTitle());
1820 assertEquals("Title 2", findById(list, "s2").getTitle());
1821
1822 assertShortcutIds(assertAllHaveTitle(assertAllNotHaveIntents(
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001823 mLauncherApps.getShortcutInfo(CALLING_PACKAGE_1,
Makoto Onukid99c6f02016-03-28 11:02:54 -07001824 list("s3"), getCallingUser())))
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001825 /* none */);
1826
1827 list = assertShortcutIds(assertAllHaveTitle(assertAllNotHaveIntents(
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001828 mLauncherApps.getShortcutInfo(CALLING_PACKAGE_2,
Makoto Onukid99c6f02016-03-28 11:02:54 -07001829 list("s1", "s2", "s3"), getCallingUser()))),
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001830 "s1");
1831 assertEquals("ABC", findById(list, "s1").getTitle());
1832 }
1833
1834 public void testPinShortcutAndGetPinnedShortcuts() {
Makoto Onukide667372016-03-15 14:29:20 -07001835 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1836 final ShortcutInfo s1_1 = makeShortcutWithTimestamp("s1", 1000);
1837 final ShortcutInfo s1_2 = makeShortcutWithTimestamp("s2", 2000);
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001838
Makoto Onukid99c6f02016-03-28 11:02:54 -07001839 assertTrue(mManager.setDynamicShortcuts(list(s1_1, s1_2)));
Makoto Onukide667372016-03-15 14:29:20 -07001840 });
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001841
Makoto Onukide667372016-03-15 14:29:20 -07001842 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
1843 final ShortcutInfo s2_2 = makeShortcutWithTimestamp("s2", 1500);
1844 final ShortcutInfo s2_3 = makeShortcutWithTimestamp("s3", 3000);
1845 final ShortcutInfo s2_4 = makeShortcutWithTimestamp("s4", 500);
Makoto Onukid99c6f02016-03-28 11:02:54 -07001846 assertTrue(mManager.setDynamicShortcuts(list(s2_2, s2_3, s2_4)));
Makoto Onukide667372016-03-15 14:29:20 -07001847 });
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001848
Makoto Onukide667372016-03-15 14:29:20 -07001849 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
1850 final ShortcutInfo s3_2 = makeShortcutWithTimestamp("s2", 1000);
Makoto Onukid99c6f02016-03-28 11:02:54 -07001851 assertTrue(mManager.setDynamicShortcuts(list(s3_2)));
Makoto Onukide667372016-03-15 14:29:20 -07001852 });
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001853
1854 // Pin some.
Makoto Onukide667372016-03-15 14:29:20 -07001855 runWithCaller(LAUNCHER_1, USER_0, () -> {
1856 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
Makoto Onukid99c6f02016-03-28 11:02:54 -07001857 list("s2", "s3"), getCallingUser());
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001858
Makoto Onukide667372016-03-15 14:29:20 -07001859 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
Makoto Onukid99c6f02016-03-28 11:02:54 -07001860 list("s3", "s4", "s5"), getCallingUser());
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001861
Makoto Onukide667372016-03-15 14:29:20 -07001862 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3,
Makoto Onukid99c6f02016-03-28 11:02:54 -07001863 list("s3"), getCallingUser()); // Note ID doesn't exist
Makoto Onukide667372016-03-15 14:29:20 -07001864 });
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001865
1866 // Delete some.
Makoto Onukide667372016-03-15 14:29:20 -07001867 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1868 assertShortcutIds(mManager.getPinnedShortcuts(), "s2");
Makoto Onukib6d35232016-04-04 15:57:17 -07001869 mManager.removeDynamicShortcuts(list("s2"));
Makoto Onukide667372016-03-15 14:29:20 -07001870 assertShortcutIds(mManager.getPinnedShortcuts(), "s2");
Makoto Onuki22fcc682016-05-17 14:52:19 -07001871
1872 assertShortcutIds(mManager.getDynamicShortcuts(), "s1");
Makoto Onukide667372016-03-15 14:29:20 -07001873 });
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001874
Makoto Onukide667372016-03-15 14:29:20 -07001875 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
1876 assertShortcutIds(mManager.getPinnedShortcuts(), "s3", "s4");
Makoto Onukib6d35232016-04-04 15:57:17 -07001877 mManager.removeDynamicShortcuts(list("s3"));
Makoto Onukide667372016-03-15 14:29:20 -07001878 assertShortcutIds(mManager.getPinnedShortcuts(), "s3", "s4");
Makoto Onuki22fcc682016-05-17 14:52:19 -07001879
1880 assertShortcutIds(mManager.getDynamicShortcuts(), "s2", "s4");
Makoto Onukide667372016-03-15 14:29:20 -07001881 });
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001882
Makoto Onukide667372016-03-15 14:29:20 -07001883 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
1884 assertShortcutIds(mManager.getPinnedShortcuts() /* none */);
Makoto Onukib6d35232016-04-04 15:57:17 -07001885 mManager.removeDynamicShortcuts(list("s2"));
Makoto Onukide667372016-03-15 14:29:20 -07001886 assertShortcutIds(mManager.getPinnedShortcuts() /* none */);
Makoto Onuki22fcc682016-05-17 14:52:19 -07001887
1888 assertEmpty(mManager.getDynamicShortcuts());
Makoto Onukide667372016-03-15 14:29:20 -07001889 });
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001890
1891 // Get pinned shortcuts from launcher
Makoto Onukide667372016-03-15 14:29:20 -07001892 runWithCaller(LAUNCHER_1, USER_0, () -> {
1893 // CALLING_PACKAGE_1 deleted s2, but it's pinned, so it still exists.
Makoto Onuki22fcc682016-05-17 14:52:19 -07001894 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(assertAllEnabled(
Makoto Onukide667372016-03-15 14:29:20 -07001895 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
Makoto Onuki22fcc682016-05-17 14:52:19 -07001896 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser())))),
Makoto Onukide667372016-03-15 14:29:20 -07001897 "s2");
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001898
Makoto Onuki22fcc682016-05-17 14:52:19 -07001899 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(assertAllEnabled(
1900 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
1901 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser())))),
1902 "s3", "s4");
1903
1904 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(assertAllEnabled(
1905 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_3,
1906 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))))
1907 /* none */);
1908 });
1909 }
1910
1911 /**
1912 * This is similar to the above test, except it used "disable" instead of "remove". It also
1913 * does "enable".
1914 */
1915 public void testDisableAndEnableShortcuts() {
1916 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1917 final ShortcutInfo s1_1 = makeShortcutWithTimestamp("s1", 1000);
1918 final ShortcutInfo s1_2 = makeShortcutWithTimestamp("s2", 2000);
1919
1920 assertTrue(mManager.setDynamicShortcuts(list(s1_1, s1_2)));
1921 });
1922
1923 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
1924 final ShortcutInfo s2_2 = makeShortcutWithTimestamp("s2", 1500);
1925 final ShortcutInfo s2_3 = makeShortcutWithTimestamp("s3", 3000);
1926 final ShortcutInfo s2_4 = makeShortcutWithTimestamp("s4", 500);
1927 assertTrue(mManager.setDynamicShortcuts(list(s2_2, s2_3, s2_4)));
1928 });
1929
1930 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
1931 final ShortcutInfo s3_2 = makeShortcutWithTimestamp("s2", 1000);
1932 assertTrue(mManager.setDynamicShortcuts(list(s3_2)));
1933 });
1934
1935 // Pin some.
1936 runWithCaller(LAUNCHER_1, USER_0, () -> {
1937 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
1938 list("s2", "s3"), getCallingUser());
1939
1940 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
1941 list("s3", "s4", "s5"), getCallingUser());
1942
1943 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3,
1944 list("s3"), getCallingUser()); // Note ID doesn't exist
1945 });
1946
1947 // Disable some.
1948 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1949 assertShortcutIds(mManager.getPinnedShortcuts(), "s2");
1950
Makoto Onukib1588c02017-10-12 15:11:45 -07001951 mManager.updateShortcuts(list(
1952 new ShortcutInfo.Builder(mClientContext, "s2").setDisabledMessage("xyz")
1953 .build()));
1954
Makoto Onuki22fcc682016-05-17 14:52:19 -07001955 mManager.disableShortcuts(list("s2"));
1956
1957 assertShortcutIds(mManager.getPinnedShortcuts(), "s2");
1958 assertShortcutIds(mManager.getDynamicShortcuts(), "s1");
1959 });
1960
1961 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
1962 assertShortcutIds(mManager.getPinnedShortcuts(), "s3", "s4");
1963
1964 // disable should work even if a shortcut is not dynamic, so try calling "remove" first
1965 // here.
1966 mManager.removeDynamicShortcuts(list("s3"));
1967 mManager.disableShortcuts(list("s3"));
1968
1969 assertShortcutIds(mManager.getPinnedShortcuts(), "s3", "s4");
1970 assertShortcutIds(mManager.getDynamicShortcuts(), "s2", "s4");
1971 });
1972
1973 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
1974 assertShortcutIds(mManager.getPinnedShortcuts() /* none */);
1975
1976 mManager.disableShortcuts(list("s2"));
1977
1978 assertShortcutIds(mManager.getPinnedShortcuts() /* none */);
1979
1980 assertEmpty(mManager.getDynamicShortcuts());
1981 assertEmpty(getCallerShortcuts());
1982 });
1983
1984 // Get pinned shortcuts from launcher
1985 runWithCaller(LAUNCHER_1, USER_0, () -> {
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07001986 // CALLING_PACKAGE_1 deleted s2, but it's pinned, so it still exists, and disabled.
1987 assertWith(mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1988 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))
1989 .haveIds("s2")
Makoto Onukib1588c02017-10-12 15:11:45 -07001990 .areAllWithDisabledReason(ShortcutInfo.DISABLED_REASON_BY_APP)
1991 .forAllShortcuts(si -> {
1992 assertEquals("xyz", si.getDisabledMessage());
1993 })
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07001994 .areAllPinned()
1995 .areAllNotWithKeyFieldsOnly()
1996 .areAllDisabled();
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001997 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_0,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07001998 ActivityNotFoundException.class);
Makoto Onuki22fcc682016-05-17 14:52:19 -07001999
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002000 // Here, s4 is still enabled and launchable, but s3 is disabled.
2001 assertWith(mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2002 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))
2003 .haveIds("s3", "s4")
2004 .areAllPinned()
2005 .areAllNotWithKeyFieldsOnly()
2006
2007 .selectByIds("s3")
2008 .areAllDisabled()
2009
2010 .revertToOriginalList()
2011 .selectByIds("s4")
2012 .areAllEnabled();
2013
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002014 assertStartShortcutThrowsException(CALLING_PACKAGE_2, "s3", USER_0,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002015 ActivityNotFoundException.class);
Makoto Onukiff14f732016-06-30 17:07:25 -07002016 assertShortcutLaunchable(CALLING_PACKAGE_2, "s4", USER_0);
Makoto Onuki6f7362d92016-03-04 13:39:41 -08002017
Makoto Onuki22fcc682016-05-17 14:52:19 -07002018 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(assertAllEnabled(
Makoto Onukide667372016-03-15 14:29:20 -07002019 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_3,
Makoto Onuki22fcc682016-05-17 14:52:19 -07002020 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))))
Makoto Onukide667372016-03-15 14:29:20 -07002021 /* none */);
2022 });
Makoto Onuki22fcc682016-05-17 14:52:19 -07002023
2024 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2025 mManager.enableShortcuts(list("s2"));
2026
2027 assertShortcutIds(mManager.getPinnedShortcuts(), "s2");
2028 assertShortcutIds(mManager.getDynamicShortcuts(), "s1");
2029 });
2030 runWithCaller(LAUNCHER_1, USER_0, () -> {
2031 // CALLING_PACKAGE_1 deleted s2, but it's pinned, so it still exists.
2032 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(assertAllEnabled(
2033 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2034 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser())))),
2035 "s2");
Makoto Onukiff14f732016-06-30 17:07:25 -07002036 assertShortcutLaunchable(CALLING_PACKAGE_1, "s2", USER_0);
2037 });
2038 }
2039
2040 public void testDisableShortcuts_thenRepublish() {
2041 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2042 assertTrue(mManager.setDynamicShortcuts(list(
2043 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2044
2045 runWithCaller(LAUNCHER_1, USER_0, () -> {
2046 mLauncherApps.pinShortcuts(
2047 CALLING_PACKAGE_1, list("s1", "s2", "s3"), HANDLE_USER_0);
2048 });
2049
2050 mManager.disableShortcuts(list("s1", "s2", "s3"));
2051
2052 assertWith(getCallerShortcuts())
2053 .haveIds("s1", "s2", "s3")
2054 .areAllNotDynamic()
2055 .areAllPinned()
2056 .areAllDisabled();
2057
2058 // Make sure updateShortcuts() will not re-enable them.
2059 assertTrue(mManager.updateShortcuts(list(
2060 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2061
2062 assertWith(getCallerShortcuts())
2063 .haveIds("s1", "s2", "s3")
2064 .areAllNotDynamic()
2065 .areAllPinned()
2066 .areAllDisabled();
2067
2068 // Re-publish s1 with setDynamicShortcuts.
2069 mInjectedCurrentTimeMillis += INTERVAL; // reset throttling
2070
2071 assertTrue(mManager.setDynamicShortcuts(list(
2072 makeShortcut("s1"))));
2073
2074 assertWith(getCallerShortcuts())
2075 .haveIds("s1", "s2", "s3")
2076
2077 .selectByIds("s1")
2078 .areAllDynamic()
2079 .areAllPinned()
2080 .areAllEnabled()
2081
2082 .revertToOriginalList()
2083 .selectByIds("s2", "s3")
2084 .areAllNotDynamic()
2085 .areAllPinned()
2086 .areAllDisabled();
2087
2088 // Re-publish s2 with addDynamicShortcuts.
2089 mInjectedCurrentTimeMillis += INTERVAL; // reset throttling
2090
2091 assertTrue(mManager.addDynamicShortcuts(list(
2092 makeShortcut("s2"))));
2093
2094 assertWith(getCallerShortcuts())
2095 .haveIds("s1", "s2", "s3")
2096
2097 .selectByIds("s1", "s2")
2098 .areAllDynamic()
2099 .areAllPinned()
2100 .areAllEnabled()
2101
2102 .revertToOriginalList()
2103 .selectByIds("s3")
2104 .areAllNotDynamic()
2105 .areAllPinned()
2106 .areAllDisabled();
Makoto Onuki22fcc682016-05-17 14:52:19 -07002107 });
Makoto Onukide667372016-03-15 14:29:20 -07002108 }
Makoto Onuki6f7362d92016-03-04 13:39:41 -08002109
Makoto Onukide667372016-03-15 14:29:20 -07002110 public void testPinShortcutAndGetPinnedShortcuts_multi() {
2111 // Create some shortcuts.
2112 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
Makoto Onukid99c6f02016-03-28 11:02:54 -07002113 assertTrue(mManager.setDynamicShortcuts(list(
Makoto Onukide667372016-03-15 14:29:20 -07002114 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2115 });
2116
2117 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
Makoto Onukid99c6f02016-03-28 11:02:54 -07002118 assertTrue(mManager.setDynamicShortcuts(list(
Makoto Onukide667372016-03-15 14:29:20 -07002119 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2120 });
2121
2122 dumpsysOnLogcat();
2123
2124 // Pin some.
2125 runWithCaller(LAUNCHER_1, USER_0, () -> {
2126 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
Makoto Onukid99c6f02016-03-28 11:02:54 -07002127 list("s3", "s4"), getCallingUser());
Makoto Onukide667372016-03-15 14:29:20 -07002128
2129 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
Makoto Onukid99c6f02016-03-28 11:02:54 -07002130 list("s1", "s2", "s4"), getCallingUser());
Makoto Onukide667372016-03-15 14:29:20 -07002131 });
2132
2133 dumpsysOnLogcat();
2134
2135 // Delete some.
2136 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2137 assertShortcutIds(mManager.getPinnedShortcuts(), "s3");
Makoto Onukib6d35232016-04-04 15:57:17 -07002138 mManager.removeDynamicShortcuts(list("s3"));
Makoto Onukide667372016-03-15 14:29:20 -07002139 assertShortcutIds(mManager.getPinnedShortcuts(), "s3");
2140 });
2141
2142 dumpsysOnLogcat();
2143
2144 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
2145 assertShortcutIds(mManager.getPinnedShortcuts(), "s1", "s2");
Makoto Onukib6d35232016-04-04 15:57:17 -07002146 mManager.removeDynamicShortcuts(list("s1"));
2147 mManager.removeDynamicShortcuts(list("s3"));
Makoto Onukide667372016-03-15 14:29:20 -07002148 assertShortcutIds(mManager.getPinnedShortcuts(), "s1", "s2");
2149 });
2150
2151 dumpsysOnLogcat();
2152
2153 // Get pinned shortcuts from launcher
2154 runWithCaller(LAUNCHER_1, USER_0, () -> {
2155 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
2156 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2157 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
2158 "s3");
2159
2160 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
2161 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2162 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
2163 "s1", "s2");
2164
2165 assertShortcutIds(assertAllDynamicOrPinned(
2166 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2167 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
2168 | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
2169 "s1", "s2", "s3");
2170
2171 assertShortcutIds(assertAllDynamicOrPinned(
2172 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2173 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
2174 | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
2175 "s1", "s2");
2176 });
2177
Makoto Onuki634cecb2017-10-13 17:10:48 -07002178 dumpsysOnLogcat("Before launcher 2");
Makoto Onukide667372016-03-15 14:29:20 -07002179
2180 runWithCaller(LAUNCHER_2, USER_0, () -> {
2181 // Launcher2 still has no pinned ones.
2182 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
2183 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2184 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser())))
2185 /* none */);
2186 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
2187 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2188 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser())))
2189 /* none */);
2190
Makoto Onuki634cecb2017-10-13 17:10:48 -07002191 // Make sure FLAG_MATCH_ALL_PINNED will be ignored.
2192 assertWith(mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2193 /* activity =*/ null, ShortcutQuery.FLAG_MATCH_PINNED
Makoto Onuki35559d62017-11-06 16:26:32 -08002194 | ShortcutQuery.FLAG_MATCH_PINNED_BY_ANY_LAUNCHER), getCallingUser()))
Makoto Onuki634cecb2017-10-13 17:10:48 -07002195 .isEmpty();
2196
2197 // Make sure the special permission works.
2198 mInjectCheckAccessShortcutsPermission = true;
2199
2200 dumpsysOnLogcat("All-pinned");
2201
2202 assertWith(mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2203 /* activity =*/ null, ShortcutQuery.FLAG_MATCH_PINNED
Makoto Onuki35559d62017-11-06 16:26:32 -08002204 | ShortcutQuery.FLAG_MATCH_PINNED_BY_ANY_LAUNCHER), getCallingUser()))
Makoto Onuki634cecb2017-10-13 17:10:48 -07002205 .haveIds("s1", "s2");
2206 assertWith(mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2207 /* activity =*/ null, ShortcutQuery.FLAG_MATCH_PINNED), getCallingUser()))
2208 .isEmpty();
2209
Makoto Onuki35559d62017-11-06 16:26:32 -08002210 assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", getCallingUser().getIdentifier());
2211
Makoto Onuki634cecb2017-10-13 17:10:48 -07002212 mInjectCheckAccessShortcutsPermission = false;
2213
Makoto Onuki35559d62017-11-06 16:26:32 -08002214 assertShortcutNotLaunched(CALLING_PACKAGE_2, "s1", getCallingUser().getIdentifier());
2215
Makoto Onukide667372016-03-15 14:29:20 -07002216 assertShortcutIds(assertAllDynamic(
2217 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2218 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
2219 | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
2220 "s1", "s2");
2221 assertShortcutIds(assertAllDynamic(
2222 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2223 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
2224 | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
2225 "s2");
2226
2227 // Now pin some.
2228 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
Makoto Onukid99c6f02016-03-28 11:02:54 -07002229 list("s1", "s2"), getCallingUser());
Makoto Onukide667372016-03-15 14:29:20 -07002230
2231 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
Makoto Onukid99c6f02016-03-28 11:02:54 -07002232 list("s1", "s2"), getCallingUser());
Makoto Onukide667372016-03-15 14:29:20 -07002233
2234 assertShortcutIds(assertAllDynamic(
2235 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2236 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
2237 | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
2238 "s1", "s2");
2239
2240 // S1 was not visible to it, so shouldn't be pinned.
2241 assertShortcutIds(assertAllDynamic(
2242 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2243 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
2244 | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
2245 "s2");
2246 });
2247
2248 // Re-initialize and load from the files.
Makoto Onukiaa8b94a2016-03-17 13:14:05 -07002249 mService.saveDirtyInfo();
Makoto Onukide667372016-03-15 14:29:20 -07002250 initService();
2251
Makoto Onuki0acbb142016-03-22 17:02:57 -07002252 // Load from file.
2253 mService.handleUnlockUser(USER_0);
2254
2255 // Make sure package info is restored too.
Makoto Onukide667372016-03-15 14:29:20 -07002256 runWithCaller(LAUNCHER_1, USER_0, () -> {
2257 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
2258 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2259 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
2260 "s3");
2261 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
2262 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2263 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
2264 "s1", "s2");
2265 });
2266 runWithCaller(LAUNCHER_2, USER_0, () -> {
2267 assertShortcutIds(assertAllDynamic(
2268 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2269 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
2270 | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
2271 "s1", "s2");
2272 assertShortcutIds(assertAllDynamic(
2273 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2274 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
2275 | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
2276 "s2");
2277 });
2278
2279 // Delete all dynamic.
2280 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
Makoto Onukib6d35232016-04-04 15:57:17 -07002281 mManager.removeAllDynamicShortcuts();
Makoto Onukide667372016-03-15 14:29:20 -07002282
2283 assertEquals(0, mManager.getDynamicShortcuts().size());
2284 assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()), "s1", "s2", "s3");
2285 });
2286 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
Makoto Onukib6d35232016-04-04 15:57:17 -07002287 mManager.removeAllDynamicShortcuts();
Makoto Onukide667372016-03-15 14:29:20 -07002288
2289 assertEquals(0, mManager.getDynamicShortcuts().size());
2290 assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()), "s2", "s1");
2291 });
2292
2293 runWithCaller(LAUNCHER_1, USER_0, () -> {
2294 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
2295 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2296 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
2297 "s3");
2298
2299 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
2300 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2301 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
2302 "s1", "s2");
2303
2304 // from all packages.
2305 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
2306 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, null,
2307 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
2308 "s1", "s2", "s3");
2309
2310 // Update pined. Note s2 and s3 are actually available, but not visible to this
2311 // launcher, so still can't be pinned.
Makoto Onukid99c6f02016-03-28 11:02:54 -07002312 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s1", "s2", "s3", "s4"),
Makoto Onukide667372016-03-15 14:29:20 -07002313 getCallingUser());
2314
2315 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
2316 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2317 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
2318 "s3");
2319 });
2320 // Re-publish s1.
2321 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
Makoto Onukib6d35232016-04-04 15:57:17 -07002322 assertTrue(mManager.addDynamicShortcuts(list(makeShortcut("s1"))));
Makoto Onukide667372016-03-15 14:29:20 -07002323
2324 assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()), "s1");
2325 assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()), "s1", "s2", "s3");
2326 });
2327 runWithCaller(LAUNCHER_1, USER_0, () -> {
2328 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
2329 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2330 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
2331 "s3");
2332
2333 // Now "s1" is visible, so can be pinned.
Makoto Onukid99c6f02016-03-28 11:02:54 -07002334 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s1", "s2", "s3", "s4"),
Makoto Onukide667372016-03-15 14:29:20 -07002335 getCallingUser());
2336
2337 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
2338 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2339 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
2340 "s1", "s3");
2341 });
2342
2343 // Now clear pinned shortcuts. First, from launcher 1.
2344 runWithCaller(LAUNCHER_1, USER_0, () -> {
Makoto Onukid99c6f02016-03-28 11:02:54 -07002345 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list(), getCallingUser());
2346 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list(), getCallingUser());
Makoto Onukide667372016-03-15 14:29:20 -07002347
2348 assertEquals(0,
2349 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2350 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()).size());
2351 assertEquals(0,
2352 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2353 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()).size());
2354 });
2355 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2356 assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()), "s1");
2357 assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()), "s1", "s2");
2358 });
2359 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
2360 assertEquals(0, mManager.getDynamicShortcuts().size());
2361 assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()), "s2");
2362 });
2363
2364 // Clear all pins from launcher 2.
2365 runWithCaller(LAUNCHER_2, USER_0, () -> {
Makoto Onukid99c6f02016-03-28 11:02:54 -07002366 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list(), getCallingUser());
2367 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list(), getCallingUser());
Makoto Onukide667372016-03-15 14:29:20 -07002368
2369 assertEquals(0,
2370 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2371 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()).size());
2372 assertEquals(0,
2373 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2374 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()).size());
2375 });
2376 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2377 assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()), "s1");
2378 assertEquals(0, mManager.getPinnedShortcuts().size());
2379 });
2380 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
2381 assertEquals(0, mManager.getDynamicShortcuts().size());
2382 assertEquals(0, mManager.getPinnedShortcuts().size());
2383 });
Makoto Onuki6f7362d92016-03-04 13:39:41 -08002384 }
2385
Makoto Onuki35559d62017-11-06 16:26:32 -08002386 public void testPinShortcutAndGetPinnedShortcuts_assistant() {
2387 // Create some shortcuts.
2388 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2389 assertTrue(mManager.setDynamicShortcuts(list(
2390 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2391 });
2392
2393 // Pin some.
2394 runWithCaller(LAUNCHER_1, USER_0, () -> {
2395 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
2396 list("s3", "s4"), getCallingUser());
2397 });
2398
2399 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2400 assertTrue(mManager.setDynamicShortcuts(list(
2401 makeShortcut("s1"))));
2402 });
2403
2404 runWithCaller(LAUNCHER_2, USER_0, () -> {
2405 final ShortcutQuery allPinned = new ShortcutQuery().setQueryFlags(
2406 ShortcutQuery.FLAG_MATCH_PINNED_BY_ANY_LAUNCHER);
2407
2408 assertWith(mLauncherApps.getShortcuts(allPinned, HANDLE_USER_0))
2409 .isEmpty();
2410
2411 assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
2412 assertShortcutNotLaunched(CALLING_PACKAGE_1, "s3", USER_0);
2413 assertShortcutNotLaunched(CALLING_PACKAGE_1, "s4", USER_0);
2414
2415 // Make it the assistant app.
2416 mInternal.setShortcutHostPackage("assistant", LAUNCHER_2, USER_0);
2417 assertWith(mLauncherApps.getShortcuts(allPinned, HANDLE_USER_0))
2418 .haveIds("s3");
2419
2420 assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
2421 assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_0);
2422 assertShortcutNotLaunched(CALLING_PACKAGE_1, "s4", USER_0);
2423
2424 mInternal.setShortcutHostPackage("another-type", LAUNCHER_3, USER_0);
2425 assertWith(mLauncherApps.getShortcuts(allPinned, HANDLE_USER_0))
2426 .haveIds("s3");
2427
2428 mInternal.setShortcutHostPackage("assistant", null, USER_0);
2429 assertWith(mLauncherApps.getShortcuts(allPinned, HANDLE_USER_0))
2430 .isEmpty();
2431
2432 mInternal.setShortcutHostPackage("assistant", LAUNCHER_2, USER_0);
2433 assertWith(mLauncherApps.getShortcuts(allPinned, HANDLE_USER_0))
2434 .haveIds("s3");
2435
2436 mInternal.setShortcutHostPackage("assistant", LAUNCHER_1, USER_0);
2437 assertWith(mLauncherApps.getShortcuts(allPinned, HANDLE_USER_0))
2438 .isEmpty();
2439 });
2440 }
2441
Makoto Onukid99c6f02016-03-28 11:02:54 -07002442 public void testPinShortcutAndGetPinnedShortcuts_crossProfile_plusLaunch() {
2443 // Create some shortcuts.
2444 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2445 assertTrue(mManager.setDynamicShortcuts(list(
2446 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2447 });
2448 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
2449 assertTrue(mManager.setDynamicShortcuts(list(
2450 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2451 });
Makoto Onuki9c850012016-07-26 15:50:50 -07002452
2453 mRunningUsers.put(USER_10, true);
2454
Makoto Onukid99c6f02016-03-28 11:02:54 -07002455 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
2456 assertTrue(mManager.setDynamicShortcuts(list(
2457 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"),
2458 makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6"))));
2459 });
2460
2461 // Pin some shortcuts and see the result.
2462
2463 runWithCaller(LAUNCHER_1, USER_0, () -> {
2464 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
2465 list("s1"), HANDLE_USER_0);
2466
2467 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
2468 list("s1", "s2", "s3"), HANDLE_USER_0);
2469 });
2470
2471 runWithCaller(LAUNCHER_1, USER_P0, () -> {
2472 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
2473 list("s2"), HANDLE_USER_0);
2474
2475 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
2476 list("s2", "s3"), HANDLE_USER_0);
2477 });
2478
2479 runWithCaller(LAUNCHER_2, USER_P0, () -> {
2480 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
2481 list("s3"), HANDLE_USER_0);
2482
2483 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
2484 list("s3"), HANDLE_USER_0);
2485 });
2486
2487 runWithCaller(LAUNCHER_2, USER_10, () -> {
2488 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
2489 list("s1", "s2", "s3"), HANDLE_USER_10);
2490 });
2491
Makoto Onukide3c16c2017-01-26 11:39:31 -08002492 // First, make sure managed profile can't see other profiles.
2493 runWithCaller(LAUNCHER_1, USER_P1, () -> {
2494
2495 final ShortcutQuery q = new ShortcutQuery().setQueryFlags(
2496 ShortcutQuery.FLAG_MATCH_DYNAMIC | ShortcutQuery.FLAG_MATCH_PINNED
Makoto Onukia4f89b12017-10-05 10:37:55 -07002497 | ShortcutQuery.FLAG_MATCH_MANIFEST);
Makoto Onukide3c16c2017-01-26 11:39:31 -08002498
2499 // No shortcuts are visible.
2500 assertWith(mLauncherApps.getShortcuts(q, HANDLE_USER_0)).isEmpty();
2501
2502 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s1"), HANDLE_USER_0);
2503
2504 // Should have no effects.
2505 assertWith(mLauncherApps.getShortcuts(q, HANDLE_USER_0)).isEmpty();
2506
2507 assertShortcutNotLaunched(CALLING_PACKAGE_1, "s1", USER_0);
2508 });
2509
Makoto Onukid99c6f02016-03-28 11:02:54 -07002510 // Cross profile pinning.
2511 final int PIN_AND_DYNAMIC = ShortcutQuery.FLAG_GET_PINNED | ShortcutQuery.FLAG_GET_DYNAMIC;
2512
2513 runWithCaller(LAUNCHER_1, USER_0, () -> {
2514 assertShortcutIds(assertAllPinned(
2515 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2516 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2517 "s1");
2518 assertShortcutIds(assertAllDynamic(
2519 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2520 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2521 "s1", "s2", "s3");
2522 assertShortcutIds(assertAllDynamicOrPinned(
2523 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2524 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2525 "s1", "s2", "s3");
2526
2527 assertShortcutIds(assertAllPinned(
2528 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2529 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2530 "s1", "s2", "s3");
2531 assertShortcutIds(assertAllDynamic(
2532 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2533 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2534 "s1", "s2", "s3");
2535 assertShortcutIds(assertAllDynamicOrPinned(
2536 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2537 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2538 "s1", "s2", "s3");
2539
2540 assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
2541 assertShortcutLaunchable(CALLING_PACKAGE_1, "s2", USER_0);
2542 assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_0);
2543
2544 assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
2545 assertShortcutLaunchable(CALLING_PACKAGE_2, "s2", USER_0);
2546 assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2547
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002548 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002549 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002550 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002551 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002552 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002553 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002554 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002555 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002556 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002557 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002558 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002559 SecurityException.class);
Makoto Onukid99c6f02016-03-28 11:02:54 -07002560 });
2561 runWithCaller(LAUNCHER_1, USER_P0, () -> {
2562 assertShortcutIds(assertAllPinned(
2563 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2564 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2565 "s2");
2566 assertShortcutIds(assertAllDynamic(
2567 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2568 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2569 "s1", "s2", "s3");
2570 assertShortcutIds(assertAllDynamicOrPinned(
2571 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2572 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2573 "s1", "s2", "s3");
2574
2575 assertShortcutIds(assertAllPinned(
2576 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2577 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2578 "s2", "s3");
2579 assertShortcutIds(assertAllDynamic(
2580 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2581 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2582 "s1", "s2", "s3");
2583 assertShortcutIds(assertAllDynamicOrPinned(
2584 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2585 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2586 "s1", "s2", "s3");
2587
2588 assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
2589 assertShortcutLaunchable(CALLING_PACKAGE_1, "s2", USER_0);
2590 assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_0);
2591
2592 assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
2593 assertShortcutLaunchable(CALLING_PACKAGE_2, "s2", USER_0);
2594 assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2595
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002596 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002597 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002598 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002599 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002600 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002601 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002602 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002603 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002604 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002605 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002606 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002607 SecurityException.class);
Makoto Onukid99c6f02016-03-28 11:02:54 -07002608 });
2609 runWithCaller(LAUNCHER_2, USER_P0, () -> {
2610 assertShortcutIds(assertAllPinned(
2611 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2612 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2613 "s3");
2614 assertShortcutIds(assertAllDynamic(
2615 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2616 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2617 "s1", "s2", "s3");
2618 assertShortcutIds(assertAllDynamicOrPinned(
2619 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2620 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2621 "s1", "s2", "s3");
2622
2623 assertShortcutIds(assertAllPinned(
2624 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2625 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2626 "s3");
2627 assertShortcutIds(assertAllDynamic(
2628 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2629 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2630 "s1", "s2", "s3");
2631 assertShortcutIds(assertAllDynamicOrPinned(
2632 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2633 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2634 "s1", "s2", "s3");
2635
2636 assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
2637 assertShortcutLaunchable(CALLING_PACKAGE_1, "s2", USER_0);
2638 assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_0);
2639
2640 assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
2641 assertShortcutLaunchable(CALLING_PACKAGE_2, "s2", USER_0);
2642 assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2643
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002644 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002645 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002646 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002647 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002648 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002649 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002650 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002651 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002652 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002653 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002654 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002655 SecurityException.class);
Makoto Onukid99c6f02016-03-28 11:02:54 -07002656 });
2657 runWithCaller(LAUNCHER_2, USER_10, () -> {
2658 assertShortcutIds(assertAllPinned(
2659 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2660 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_10)),
2661 "s1", "s2", "s3");
2662 assertShortcutIds(assertAllDynamic(
2663 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2664 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_10)),
2665 "s1", "s2", "s3", "s4", "s5", "s6");
2666 assertShortcutIds(assertAllDynamicOrPinned(
2667 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2668 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_10)),
2669 "s1", "s2", "s3", "s4", "s5", "s6");
2670 });
2671
2672 // Remove some dynamic shortcuts.
2673
2674 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2675 assertTrue(mManager.setDynamicShortcuts(list(
2676 makeShortcut("s1"))));
2677 });
2678 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
2679 assertTrue(mManager.setDynamicShortcuts(list(
2680 makeShortcut("s1"))));
2681 });
2682 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
2683 assertTrue(mManager.setDynamicShortcuts(list(
2684 makeShortcut("s1"))));
2685 });
2686
2687 runWithCaller(LAUNCHER_1, USER_0, () -> {
2688 assertShortcutIds(assertAllPinned(
2689 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2690 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2691 "s1");
2692 assertShortcutIds(assertAllDynamic(
2693 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2694 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2695 "s1");
2696 assertShortcutIds(assertAllDynamicOrPinned(
2697 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2698 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2699 "s1");
2700
2701 assertShortcutIds(assertAllPinned(
2702 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2703 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2704 "s1", "s2", "s3");
2705 assertShortcutIds(assertAllDynamic(
2706 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2707 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2708 "s1");
2709 assertShortcutIds(assertAllDynamicOrPinned(
2710 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2711 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2712 "s1", "s2", "s3");
2713
2714 assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002715 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_0,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002716 ActivityNotFoundException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002717 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_0,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002718 ActivityNotFoundException.class);
Makoto Onukid99c6f02016-03-28 11:02:54 -07002719
2720 assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
2721 assertShortcutLaunchable(CALLING_PACKAGE_2, "s2", USER_0);
2722 assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2723
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002724 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002725 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002726 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002727 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002728 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002729 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002730 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002731 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002732 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002733 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002734 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002735 SecurityException.class);
Makoto Onukid99c6f02016-03-28 11:02:54 -07002736 });
2737 runWithCaller(LAUNCHER_1, USER_P0, () -> {
2738 assertShortcutIds(assertAllPinned(
2739 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2740 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2741 "s2");
2742 assertShortcutIds(assertAllDynamic(
2743 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2744 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2745 "s1");
2746 assertShortcutIds(assertAllDynamicOrPinned(
2747 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2748 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2749 "s1", "s2");
2750
2751 assertShortcutIds(assertAllPinned(
2752 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2753 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2754 "s2", "s3");
2755 assertShortcutIds(assertAllDynamic(
2756 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2757 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2758 "s1");
2759 assertShortcutIds(assertAllDynamicOrPinned(
2760 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2761 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2762 "s1", "s2", "s3");
2763
2764 assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
2765 assertShortcutLaunchable(CALLING_PACKAGE_1, "s2", USER_0);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002766 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_0,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002767 ActivityNotFoundException.class);
Makoto Onukid99c6f02016-03-28 11:02:54 -07002768
2769 assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
2770 assertShortcutLaunchable(CALLING_PACKAGE_2, "s2", USER_0);
2771 assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2772
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002773 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002774 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002775 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002776 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002777 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002778 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002779 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002780 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002781 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002782 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002783 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002784 SecurityException.class);
Makoto Onukid99c6f02016-03-28 11:02:54 -07002785 });
2786 runWithCaller(LAUNCHER_2, USER_P0, () -> {
2787 assertShortcutIds(assertAllPinned(
2788 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2789 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2790 "s3");
2791 assertShortcutIds(assertAllDynamic(
2792 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2793 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2794 "s1");
2795 assertShortcutIds(assertAllDynamicOrPinned(
2796 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2797 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2798 "s1", "s3");
2799
2800 assertShortcutIds(assertAllPinned(
2801 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2802 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2803 "s3");
2804 assertShortcutIds(assertAllDynamic(
2805 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2806 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2807 "s1");
2808 assertShortcutIds(assertAllDynamicOrPinned(
2809 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2810 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2811 "s1", "s3");
2812
2813 assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002814 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_0,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002815 ActivityNotFoundException.class);
Makoto Onukid99c6f02016-03-28 11:02:54 -07002816 assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_0);
2817
2818 assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002819 assertStartShortcutThrowsException(CALLING_PACKAGE_2, "s2", USER_0,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002820 ActivityNotFoundException.class);
Makoto Onukid99c6f02016-03-28 11:02:54 -07002821 assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2822
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002823 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002824 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002825 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002826 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002827 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002828 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002829 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002830 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002831 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002832 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002833 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002834 SecurityException.class);
Makoto Onukid99c6f02016-03-28 11:02:54 -07002835 });
2836 runWithCaller(LAUNCHER_2, USER_10, () -> {
2837 assertShortcutIds(assertAllPinned(
2838 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2839 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_10)),
2840 "s1", "s2", "s3");
2841 assertShortcutIds(assertAllDynamic(
2842 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2843 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_10)),
2844 "s1");
2845 assertShortcutIds(assertAllDynamicOrPinned(
2846 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2847 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_10)),
2848 "s1", "s2", "s3");
2849
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002850 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_0,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002851 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002852 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_0,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002853 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002854 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_0,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002855 SecurityException.class);
Makoto Onukid99c6f02016-03-28 11:02:54 -07002856
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002857 assertStartShortcutThrowsException(CALLING_PACKAGE_2, "s1", USER_0,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002858 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002859 assertStartShortcutThrowsException(CALLING_PACKAGE_2, "s2", USER_0,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002860 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002861 assertStartShortcutThrowsException(CALLING_PACKAGE_2, "s3", USER_0,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002862 SecurityException.class);
Makoto Onukid99c6f02016-03-28 11:02:54 -07002863
2864 assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_10);
2865 assertShortcutLaunchable(CALLING_PACKAGE_1, "s2", USER_10);
2866 assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_10);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002867 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002868 ActivityNotFoundException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002869 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002870 ActivityNotFoundException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002871 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002872 ActivityNotFoundException.class);
Makoto Onukid99c6f02016-03-28 11:02:54 -07002873 });
2874
2875 // Save & load and make sure we still have the same information.
2876 mService.saveDirtyInfo();
2877 initService();
2878 mService.handleUnlockUser(USER_0);
2879
2880 runWithCaller(LAUNCHER_1, USER_0, () -> {
2881 assertShortcutIds(assertAllPinned(
2882 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2883 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2884 "s1");
2885 assertShortcutIds(assertAllDynamic(
2886 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2887 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2888 "s1");
2889 assertShortcutIds(assertAllDynamicOrPinned(
2890 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2891 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2892 "s1");
2893
2894 assertShortcutIds(assertAllPinned(
2895 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2896 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2897 "s1", "s2", "s3");
2898 assertShortcutIds(assertAllDynamic(
2899 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2900 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2901 "s1");
2902 assertShortcutIds(assertAllDynamicOrPinned(
2903 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2904 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2905 "s1", "s2", "s3");
2906
2907 assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002908 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_0,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002909 ActivityNotFoundException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002910 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_0,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002911 ActivityNotFoundException.class);
Makoto Onukid99c6f02016-03-28 11:02:54 -07002912
2913 assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
2914 assertShortcutLaunchable(CALLING_PACKAGE_2, "s2", USER_0);
2915 assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2916
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002917 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002918 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002919 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002920 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002921 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002922 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002923 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002924 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002925 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002926 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002927 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002928 SecurityException.class);
Makoto Onukid99c6f02016-03-28 11:02:54 -07002929 });
2930 runWithCaller(LAUNCHER_1, USER_P0, () -> {
2931 assertShortcutIds(assertAllPinned(
2932 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2933 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2934 "s2");
2935 assertShortcutIds(assertAllDynamic(
2936 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2937 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2938 "s1");
2939 assertShortcutIds(assertAllDynamicOrPinned(
2940 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2941 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2942 "s1", "s2");
2943
2944 assertShortcutIds(assertAllPinned(
2945 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2946 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2947 "s2", "s3");
2948 assertShortcutIds(assertAllDynamic(
2949 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2950 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2951 "s1");
2952 assertShortcutIds(assertAllDynamicOrPinned(
2953 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2954 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2955 "s1", "s2", "s3");
2956
2957 assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
2958 assertShortcutLaunchable(CALLING_PACKAGE_1, "s2", USER_0);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002959 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_0,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002960 ActivityNotFoundException.class);
Makoto Onukid99c6f02016-03-28 11:02:54 -07002961
2962 assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
2963 assertShortcutLaunchable(CALLING_PACKAGE_2, "s2", USER_0);
2964 assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2965
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002966 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002967 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002968 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002969 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002970 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002971 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002972 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002973 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002974 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002975 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002976 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002977 SecurityException.class);
Makoto Onukid99c6f02016-03-28 11:02:54 -07002978 });
2979 runWithCaller(LAUNCHER_2, USER_P0, () -> {
2980 assertShortcutIds(assertAllPinned(
2981 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2982 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2983 "s3");
2984 assertShortcutIds(assertAllDynamic(
2985 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2986 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2987 "s1");
2988 assertShortcutIds(assertAllDynamicOrPinned(
2989 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2990 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2991 "s1", "s3");
2992
2993 assertShortcutIds(assertAllPinned(
2994 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2995 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2996 "s3");
2997 assertShortcutIds(assertAllDynamic(
2998 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2999 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
3000 "s1");
3001 assertShortcutIds(assertAllDynamicOrPinned(
3002 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
3003 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
3004 "s1", "s3");
3005
3006 assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07003007 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_0,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07003008 ActivityNotFoundException.class);
Makoto Onukid99c6f02016-03-28 11:02:54 -07003009 assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_0);
3010
3011 assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07003012 assertStartShortcutThrowsException(CALLING_PACKAGE_2, "s2", USER_0,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07003013 ActivityNotFoundException.class);
Makoto Onukid99c6f02016-03-28 11:02:54 -07003014 assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
3015
Makoto Onuki440a1ea2016-07-20 14:21:18 -07003016 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07003017 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07003018 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07003019 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07003020 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07003021 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07003022 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07003023 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07003024 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07003025 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07003026 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07003027 SecurityException.class);
Makoto Onukid99c6f02016-03-28 11:02:54 -07003028 });
3029 }
3030
3031 public void testStartShortcut() {
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003032 // Create some shortcuts.
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07003033 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3034 final ShortcutInfo s1_1 = makeShortcut(
3035 "s1",
3036 "Title 1",
3037 makeComponent(ShortcutActivity.class),
Makoto Onuki7e5c3e82016-07-22 11:57:05 -07003038 /* icon =*/ null,
Makoto Onukia4f89b12017-10-05 10:37:55 -07003039 new Intent[]{makeIntent(Intent.ACTION_ASSIST, ShortcutActivity2.class,
Makoto Onuki7e5c3e82016-07-22 11:57:05 -07003040 "key1", "val1", "nest", makeBundle("key", 123))
3041 .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK),
Makoto Onukia4f89b12017-10-05 10:37:55 -07003042 new Intent("act2").setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)},
Makoto Onuki7e5c3e82016-07-22 11:57:05 -07003043 /* rank */ 10);
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003044
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07003045 final ShortcutInfo s1_2 = makeShortcut(
3046 "s2",
3047 "Title 2",
Makoto Onuki99302b52017-03-29 12:42:26 -07003048 /* activity */ null,
3049 /* icon =*/ null,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07003050 makeIntent(Intent.ACTION_ASSIST, ShortcutActivity3.class),
Makoto Onuki99302b52017-03-29 12:42:26 -07003051 /* rank */ 12);
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003052
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07003053 final ShortcutInfo s1_3 = makeShortcut("s3");
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003054
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07003055 assertTrue(mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3)));
3056 });
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003057
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07003058 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
3059 final ShortcutInfo s2_1 = makeShortcut(
3060 "s1",
3061 "ABC",
3062 makeComponent(ShortcutActivity.class),
3063 /* icon =*/ null,
3064 makeIntent(Intent.ACTION_ANSWER, ShortcutActivity.class,
3065 "key1", "val1", "nest", makeBundle("key", 123)),
Makoto Onuki99302b52017-03-29 12:42:26 -07003066 /* weight */ 10);
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07003067 assertTrue(mManager.setDynamicShortcuts(list(s2_1)));
3068 });
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003069
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07003070 // Pin some.
3071 runWithCaller(LAUNCHER_1, USER_0, () -> {
3072 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
3073 list("s1", "s2"), getCallingUser());
3074
3075 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
3076 list("s1"), getCallingUser());
3077 });
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003078
3079 // Just to make it complicated, delete some.
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07003080 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3081 mManager.removeDynamicShortcuts(list("s2"));
3082 });
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003083
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07003084 runWithCaller(LAUNCHER_1, USER_0, () -> {
Makoto Onuki7e5c3e82016-07-22 11:57:05 -07003085 final Intent[] intents = launchShortcutAndGetIntents(CALLING_PACKAGE_1, "s1", USER_0);
3086 assertEquals(ShortcutActivity2.class.getName(),
3087 intents[0].getComponent().getClassName());
3088 assertEquals(Intent.ACTION_ASSIST,
3089 intents[0].getAction());
3090 assertEquals(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK,
3091 intents[0].getFlags());
3092
3093 assertEquals("act2",
3094 intents[1].getAction());
3095 assertEquals(Intent.FLAG_ACTIVITY_NO_ANIMATION,
3096 intents[1].getFlags());
3097
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07003098 assertEquals(
3099 ShortcutActivity3.class.getName(),
3100 launchShortcutAndGetIntent(CALLING_PACKAGE_1, "s2", USER_0)
3101 .getComponent().getClassName());
3102 assertEquals(
3103 ShortcutActivity.class.getName(),
3104 launchShortcutAndGetIntent(CALLING_PACKAGE_2, "s1", USER_0)
3105 .getComponent().getClassName());
Makoto Onukid99c6f02016-03-28 11:02:54 -07003106
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07003107 assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_0);
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003108
Makoto Onuki440a1ea2016-07-20 14:21:18 -07003109 assertShortcutNotLaunched("no-such-package", "s2", USER_0);
3110 assertShortcutNotLaunched(CALLING_PACKAGE_1, "xxxx", USER_0);
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07003111 });
Makoto Onukid99c6f02016-03-28 11:02:54 -07003112
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07003113 // LAUNCHER_1 is no longer the default launcher
3114 setDefaultLauncherChecker((pkg, userId) -> false);
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003115
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07003116 runWithCaller(LAUNCHER_1, USER_0, () -> {
3117 // Not the default launcher, but pinned shortcuts are still lauchable.
Makoto Onuki7e5c3e82016-07-22 11:57:05 -07003118 final Intent[] intents = launchShortcutAndGetIntents(CALLING_PACKAGE_1, "s1", USER_0);
3119 assertEquals(ShortcutActivity2.class.getName(),
3120 intents[0].getComponent().getClassName());
3121 assertEquals(Intent.ACTION_ASSIST,
3122 intents[0].getAction());
3123 assertEquals(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK,
3124 intents[0].getFlags());
3125
3126 assertEquals("act2",
3127 intents[1].getAction());
3128 assertEquals(Intent.FLAG_ACTIVITY_NO_ANIMATION,
3129 intents[1].getFlags());
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07003130 assertEquals(
3131 ShortcutActivity3.class.getName(),
3132 launchShortcutAndGetIntent(CALLING_PACKAGE_1, "s2", USER_0)
3133 .getComponent().getClassName());
3134 assertEquals(
3135 ShortcutActivity.class.getName(),
3136 launchShortcutAndGetIntent(CALLING_PACKAGE_2, "s1", USER_0)
3137 .getComponent().getClassName());
3138
3139 // Not pinned, so not lauchable.
3140 });
3141
3142 // Test inner errors.
3143 runWithCaller(LAUNCHER_1, USER_0, () -> {
3144 // Not launchable.
Makoto Onuki440a1ea2016-07-20 14:21:18 -07003145 doReturn(ActivityManager.START_CLASS_NOT_FOUND)
Wale Ogunwale6767eae2018-05-03 15:52:51 -07003146 .when(mMockActivityTaskManagerInternal).startActivitiesAsPackage(
Philip P. Moltmannee295092020-02-10 08:46:26 -08003147 anyStringOrNull(), anyStringOrNull(), anyInt(),
Makoto Onukia4f89b12017-10-05 10:37:55 -07003148 anyOrNull(Intent[].class), anyOrNull(Bundle.class));
Makoto Onuki440a1ea2016-07-20 14:21:18 -07003149 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_0,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07003150 ActivityNotFoundException.class);
3151
3152 // Still not launchable.
Makoto Onuki440a1ea2016-07-20 14:21:18 -07003153 doReturn(ActivityManager.START_CLASS_NOT_FOUND)
Wale Ogunwale6767eae2018-05-03 15:52:51 -07003154 .when(mMockActivityTaskManagerInternal)
Makoto Onuki440a1ea2016-07-20 14:21:18 -07003155 .startActivitiesAsPackage(
Philip P. Moltmannee295092020-02-10 08:46:26 -08003156 anyStringOrNull(), anyStringOrNull(), anyInt(),
Makoto Onukid0010c52017-03-30 14:17:35 -07003157 anyOrNull(Intent[].class), anyOrNull(Bundle.class));
Makoto Onuki440a1ea2016-07-20 14:21:18 -07003158 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_0,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07003159 ActivityNotFoundException.class);
3160 });
3161
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003162
3163 // TODO Check extra, etc
3164 }
3165
Makoto Onuki4dbe0de2016-03-14 17:31:49 -07003166 public void testLauncherCallback() throws Throwable {
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07003167 // Disable throttling for this test.
3168 mService.updateConfigurationLocked(
3169 ConfigConstants.KEY_MAX_UPDATES_PER_INTERVAL + "=99999999,"
3170 + ConfigConstants.KEY_MAX_SHORTCUTS + "=99999999"
3171 );
3172
Makoto Onukib08790c2016-06-23 14:05:46 -07003173 setCaller(LAUNCHER_1, USER_0);
Makoto Onuki4dbe0de2016-03-14 17:31:49 -07003174
Makoto Onukib08790c2016-06-23 14:05:46 -07003175 assertForLauncherCallback(mLauncherApps, () -> {
3176 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3177 assertTrue(mManager.setDynamicShortcuts(list(
3178 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
3179 });
3180 }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07003181 .haveIds("s1", "s2", "s3")
3182 .areAllWithKeyFieldsOnly()
3183 .areAllDynamic();
Makoto Onuki4dbe0de2016-03-14 17:31:49 -07003184
3185 // From different package.
Makoto Onukib08790c2016-06-23 14:05:46 -07003186 assertForLauncherCallback(mLauncherApps, () -> {
3187 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
3188 assertTrue(mManager.setDynamicShortcuts(list(
3189 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
3190 });
3191 }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_2, HANDLE_USER_0)
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07003192 .haveIds("s1", "s2", "s3")
3193 .areAllWithKeyFieldsOnly()
3194 .areAllDynamic();
Makoto Onuki4dbe0de2016-03-14 17:31:49 -07003195
Makoto Onuki9c850012016-07-26 15:50:50 -07003196 mRunningUsers.put(USER_10, true);
3197
Makoto Onuki4dbe0de2016-03-14 17:31:49 -07003198 // Different user, callback shouldn't be called.
Makoto Onukib08790c2016-06-23 14:05:46 -07003199 assertForLauncherCallback(mLauncherApps, () -> {
3200 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
3201 assertTrue(mManager.setDynamicShortcuts(list(
3202 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
3203 });
3204 }).assertNoCallbackCalled();
3205
Makoto Onuki4dbe0de2016-03-14 17:31:49 -07003206
Makoto Onukib6d35232016-04-04 15:57:17 -07003207 // Test for addDynamicShortcuts.
Makoto Onukib08790c2016-06-23 14:05:46 -07003208 assertForLauncherCallback(mLauncherApps, () -> {
3209 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3210 assertTrue(mManager.addDynamicShortcuts(list(makeShortcut("s4"))));
3211 });
3212 }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07003213 .haveIds("s1", "s2", "s3", "s4")
3214 .areAllWithKeyFieldsOnly()
3215 .areAllDynamic();
Makoto Onuki4dbe0de2016-03-14 17:31:49 -07003216
3217 // Test for remove
Makoto Onukib08790c2016-06-23 14:05:46 -07003218 assertForLauncherCallback(mLauncherApps, () -> {
3219 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3220 mManager.removeDynamicShortcuts(list("s1"));
3221 });
3222 }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07003223 .haveIds("s2", "s3", "s4")
3224 .areAllWithKeyFieldsOnly()
3225 .areAllDynamic();
Makoto Onuki4dbe0de2016-03-14 17:31:49 -07003226
3227 // Test for update
Makoto Onukib08790c2016-06-23 14:05:46 -07003228 assertForLauncherCallback(mLauncherApps, () -> {
3229 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3230 assertTrue(mManager.updateShortcuts(list(
3231 makeShortcut("s1"), makeShortcut("s2"))));
3232 });
3233 }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
3234 // All remaining shortcuts will be passed regardless of what's been updated.
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07003235 .haveIds("s2", "s3", "s4")
3236 .areAllWithKeyFieldsOnly()
3237 .areAllDynamic();
Makoto Onuki4dbe0de2016-03-14 17:31:49 -07003238
3239 // Test for deleteAll
Makoto Onukib08790c2016-06-23 14:05:46 -07003240 assertForLauncherCallback(mLauncherApps, () -> {
3241 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3242 mManager.removeAllDynamicShortcuts();
3243 });
3244 }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07003245 .isEmpty();
3246
3247 // Update package1 with manifest shortcuts
Makoto Onukib08790c2016-06-23 14:05:46 -07003248 assertForLauncherCallback(mLauncherApps, () -> {
3249 addManifestShortcutResource(
3250 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
3251 R.xml.shortcut_2);
3252 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07003253 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onukib08790c2016-06-23 14:05:46 -07003254 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
3255 }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07003256 .areAllManifest()
3257 .areAllWithKeyFieldsOnly()
3258 .haveIds("ms1", "ms2");
3259
3260 // Make sure pinned shortcuts are passed too.
3261 // 1. Add dynamic shortcuts.
3262 runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
3263 assertTrue(mManager.setDynamicShortcuts(list(
3264 makeShortcut("s1"), makeShortcut("s2"))));
3265 });
3266
3267 // 2. Pin some.
3268 runWithCaller(LAUNCHER_1, UserHandle.USER_SYSTEM, () -> {
3269 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms2", "s2"), HANDLE_USER_0);
3270 });
3271 runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
3272 assertWith(getCallerShortcuts())
3273 .haveIds("ms1", "ms2", "s1", "s2")
3274 .areAllEnabled()
3275
3276 .selectByIds("ms1", "ms2")
3277 .areAllManifest()
3278
3279 .revertToOriginalList()
3280 .selectByIds("s1", "s2")
3281 .areAllDynamic()
Makoto Onukia4f89b12017-10-05 10:37:55 -07003282 ;
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07003283 });
3284
3285 // 3 Update the app with no manifest shortcuts. (Pinned one will survive.)
3286 addManifestShortcutResource(
3287 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
3288 R.xml.shortcut_0);
3289 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07003290 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07003291 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
3292
Makoto Onukib08790c2016-06-23 14:05:46 -07003293 assertForLauncherCallback(mLauncherApps, () -> {
3294 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3295 mManager.removeDynamicShortcuts(list("s2"));
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07003296
Makoto Onukib08790c2016-06-23 14:05:46 -07003297 assertWith(getCallerShortcuts())
3298 .haveIds("ms2", "s1", "s2")
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07003299
Makoto Onukib08790c2016-06-23 14:05:46 -07003300 .selectByIds("ms2")
3301 .areAllNotManifest()
3302 .areAllPinned()
3303 .areAllImmutable()
3304 .areAllDisabled()
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07003305
Makoto Onukib08790c2016-06-23 14:05:46 -07003306 .revertToOriginalList()
3307 .selectByIds("s1")
3308 .areAllDynamic()
3309 .areAllNotPinned()
3310 .areAllEnabled()
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07003311
Makoto Onukib08790c2016-06-23 14:05:46 -07003312 .revertToOriginalList()
3313 .selectByIds("s2")
3314 .areAllNotDynamic()
3315 .areAllPinned()
3316 .areAllEnabled()
3317 ;
3318 });
3319 }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07003320 .haveIds("ms2", "s1", "s2")
3321 .areAllWithKeyFieldsOnly();
Makoto Onukicdc78f72016-03-21 15:47:52 -07003322
3323 // Remove CALLING_PACKAGE_2
Makoto Onukib08790c2016-06-23 14:05:46 -07003324 assertForLauncherCallback(mLauncherApps, () -> {
3325 uninstallPackage(USER_0, CALLING_PACKAGE_2);
3326 mService.cleanUpPackageLocked(CALLING_PACKAGE_2, USER_0, USER_0,
3327 /* appStillExists = */ false);
3328 }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_2, HANDLE_USER_0)
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07003329 .isEmpty();
Makoto Onuki4dbe0de2016-03-14 17:31:49 -07003330 }
3331
Makoto Onukiac214972016-04-04 10:19:45 -07003332 public void testLauncherCallback_crossProfile() throws Throwable {
3333 prepareCrossProfileDataSet();
3334
3335 final Handler h = new Handler(Looper.getMainLooper());
3336
3337 final LauncherApps.Callback c0_1 = mock(LauncherApps.Callback.class);
3338 final LauncherApps.Callback c0_2 = mock(LauncherApps.Callback.class);
3339 final LauncherApps.Callback c0_3 = mock(LauncherApps.Callback.class);
3340 final LauncherApps.Callback c0_4 = mock(LauncherApps.Callback.class);
3341
3342 final LauncherApps.Callback cP0_1 = mock(LauncherApps.Callback.class);
Makoto Onukide3c16c2017-01-26 11:39:31 -08003343 final LauncherApps.Callback cP1_1 = mock(LauncherApps.Callback.class);
Makoto Onukiac214972016-04-04 10:19:45 -07003344 final LauncherApps.Callback c10_1 = mock(LauncherApps.Callback.class);
3345 final LauncherApps.Callback c10_2 = mock(LauncherApps.Callback.class);
3346 final LauncherApps.Callback c11_1 = mock(LauncherApps.Callback.class);
3347
3348 final List<LauncherApps.Callback> all =
3349 list(c0_1, c0_2, c0_3, c0_4, cP0_1, c10_1, c11_1);
3350
3351 setDefaultLauncherChecker((pkg, userId) -> {
3352 switch (userId) {
3353 case USER_0:
3354 return LAUNCHER_2.equals(pkg);
3355 case USER_P0:
3356 return LAUNCHER_1.equals(pkg);
Makoto Onukide3c16c2017-01-26 11:39:31 -08003357 case USER_P1:
3358 return LAUNCHER_1.equals(pkg);
Makoto Onukiac214972016-04-04 10:19:45 -07003359 case USER_10:
3360 return LAUNCHER_1.equals(pkg);
3361 case USER_11:
3362 return LAUNCHER_1.equals(pkg);
3363 default:
3364 return false;
3365 }
3366 });
3367
3368 runWithCaller(LAUNCHER_1, USER_0, () -> mLauncherApps.registerCallback(c0_1, h));
3369 runWithCaller(LAUNCHER_2, USER_0, () -> mLauncherApps.registerCallback(c0_2, h));
3370 runWithCaller(LAUNCHER_3, USER_0, () -> mLauncherApps.registerCallback(c0_3, h));
3371 runWithCaller(LAUNCHER_4, USER_0, () -> mLauncherApps.registerCallback(c0_4, h));
3372 runWithCaller(LAUNCHER_1, USER_P0, () -> mLauncherApps.registerCallback(cP0_1, h));
Makoto Onukide3c16c2017-01-26 11:39:31 -08003373 runWithCaller(LAUNCHER_1, USER_P1, () -> mLauncherApps.registerCallback(cP1_1, h));
Makoto Onukiac214972016-04-04 10:19:45 -07003374 runWithCaller(LAUNCHER_1, USER_10, () -> mLauncherApps.registerCallback(c10_1, h));
3375 runWithCaller(LAUNCHER_2, USER_10, () -> mLauncherApps.registerCallback(c10_2, h));
3376 runWithCaller(LAUNCHER_1, USER_11, () -> mLauncherApps.registerCallback(c11_1, h));
3377
3378 // User 0.
3379
3380 resetAll(all);
3381 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
Makoto Onukib6d35232016-04-04 15:57:17 -07003382 mManager.removeDynamicShortcuts(list());
Makoto Onukiac214972016-04-04 10:19:45 -07003383 });
3384 waitOnMainThread();
3385
3386 assertCallbackNotReceived(c0_1);
3387 assertCallbackNotReceived(c0_3);
3388 assertCallbackNotReceived(c0_4);
3389 assertCallbackNotReceived(c10_1);
3390 assertCallbackNotReceived(c10_2);
3391 assertCallbackNotReceived(c11_1);
3392 assertCallbackReceived(c0_2, HANDLE_USER_0, CALLING_PACKAGE_1, "s1", "s2", "s3");
3393 assertCallbackReceived(cP0_1, HANDLE_USER_0, CALLING_PACKAGE_1, "s1", "s2", "s3", "s4");
Makoto Onukide3c16c2017-01-26 11:39:31 -08003394 assertCallbackNotReceived(cP1_1);
Makoto Onukiac214972016-04-04 10:19:45 -07003395
3396 // User 0, different package.
3397
3398 resetAll(all);
3399 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
Makoto Onukib6d35232016-04-04 15:57:17 -07003400 mManager.removeDynamicShortcuts(list());
Makoto Onukiac214972016-04-04 10:19:45 -07003401 });
3402 waitOnMainThread();
3403
3404 assertCallbackNotReceived(c0_1);
3405 assertCallbackNotReceived(c0_3);
3406 assertCallbackNotReceived(c0_4);
3407 assertCallbackNotReceived(c10_1);
3408 assertCallbackNotReceived(c10_2);
3409 assertCallbackNotReceived(c11_1);
3410 assertCallbackReceived(c0_2, HANDLE_USER_0, CALLING_PACKAGE_3, "s1", "s2", "s3", "s4");
3411 assertCallbackReceived(cP0_1, HANDLE_USER_0, CALLING_PACKAGE_3,
3412 "s1", "s2", "s3", "s4", "s5", "s6");
Makoto Onukide3c16c2017-01-26 11:39:31 -08003413 assertCallbackNotReceived(cP1_1);
Makoto Onukiac214972016-04-04 10:19:45 -07003414
Makoto Onuki9c850012016-07-26 15:50:50 -07003415 // Work profile.
Makoto Onukiac214972016-04-04 10:19:45 -07003416 resetAll(all);
3417 runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
Makoto Onukib6d35232016-04-04 15:57:17 -07003418 mManager.removeDynamicShortcuts(list());
Makoto Onukiac214972016-04-04 10:19:45 -07003419 });
3420 waitOnMainThread();
3421
3422 assertCallbackNotReceived(c0_1);
3423 assertCallbackNotReceived(c0_3);
3424 assertCallbackNotReceived(c0_4);
3425 assertCallbackNotReceived(c10_1);
3426 assertCallbackNotReceived(c10_2);
3427 assertCallbackNotReceived(c11_1);
3428 assertCallbackReceived(c0_2, HANDLE_USER_P0, CALLING_PACKAGE_1, "s1", "s2", "s3", "s5");
3429 assertCallbackReceived(cP0_1, HANDLE_USER_P0, CALLING_PACKAGE_1, "s1", "s2", "s3", "s4");
Makoto Onukide3c16c2017-01-26 11:39:31 -08003430 assertCallbackNotReceived(cP1_1);
Makoto Onukiac214972016-04-04 10:19:45 -07003431
3432 // Normal secondary user.
Makoto Onukia2241832016-07-06 13:28:37 -07003433 mRunningUsers.put(USER_10, true);
Makoto Onukiac214972016-04-04 10:19:45 -07003434
3435 resetAll(all);
3436 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
Makoto Onukib6d35232016-04-04 15:57:17 -07003437 mManager.removeDynamicShortcuts(list());
Makoto Onukiac214972016-04-04 10:19:45 -07003438 });
3439 waitOnMainThread();
3440
3441 assertCallbackNotReceived(c0_1);
3442 assertCallbackNotReceived(c0_2);
3443 assertCallbackNotReceived(c0_3);
3444 assertCallbackNotReceived(c0_4);
3445 assertCallbackNotReceived(cP0_1);
3446 assertCallbackNotReceived(c10_2);
3447 assertCallbackNotReceived(c11_1);
3448 assertCallbackReceived(c10_1, HANDLE_USER_10, CALLING_PACKAGE_1,
3449 "x1", "x2", "x3", "x4", "x5");
Makoto Onukide3c16c2017-01-26 11:39:31 -08003450 assertCallbackNotReceived(cP1_1);
Makoto Onukiac214972016-04-04 10:19:45 -07003451 }
3452
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003453 // === Test for persisting ===
3454
3455 public void testSaveAndLoadUser_empty() {
Makoto Onukid99c6f02016-03-28 11:02:54 -07003456 assertTrue(mManager.setDynamicShortcuts(list()));
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003457
3458 Log.i(TAG, "Saved state");
3459 dumpsysOnLogcat();
3460 dumpUserFile(0);
3461
3462 // Restore.
Makoto Onukiaa8b94a2016-03-17 13:14:05 -07003463 mService.saveDirtyInfo();
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003464 initService();
3465
3466 assertEquals(0, mManager.getDynamicShortcuts().size());
3467 }
3468
3469 /**
3470 * Try save and load, also stop/start the user.
3471 */
3472 public void testSaveAndLoadUser() {
3473 // First, create some shortcuts and save.
Makoto Onuki41066a62016-03-09 16:18:44 -08003474 runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
Makoto Onuki4dbe0de2016-03-14 17:31:49 -07003475 final Icon icon1 = Icon.createWithResource(getTestContext(), R.drawable.black_64x16);
Makoto Onuki41066a62016-03-09 16:18:44 -08003476 final Icon icon2 = Icon.createWithBitmap(BitmapFactory.decodeResource(
Makoto Onuki4dbe0de2016-03-14 17:31:49 -07003477 getTestContext().getResources(), R.drawable.icon2));
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003478
Makoto Onuki41066a62016-03-09 16:18:44 -08003479 final ShortcutInfo si1 = makeShortcut(
3480 "s1",
3481 "title1-1",
3482 makeComponent(ShortcutActivity.class),
3483 icon1,
3484 makeIntent(Intent.ACTION_ASSIST, ShortcutActivity2.class,
3485 "key1", "val1", "nest", makeBundle("key", 123)),
Makoto Onuki99302b52017-03-29 12:42:26 -07003486 /* weight */ 10);
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003487
Makoto Onuki41066a62016-03-09 16:18:44 -08003488 final ShortcutInfo si2 = makeShortcut(
3489 "s2",
3490 "title1-2",
3491 /* activity */ null,
3492 icon2,
3493 makeIntent(Intent.ACTION_ASSIST, ShortcutActivity3.class),
Makoto Onuki99302b52017-03-29 12:42:26 -07003494 /* weight */ 12);
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003495
Makoto Onukid99c6f02016-03-28 11:02:54 -07003496 assertTrue(mManager.setDynamicShortcuts(list(si1, si2)));
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003497
Makoto Onuki41066a62016-03-09 16:18:44 -08003498 assertEquals(START_TIME + INTERVAL, mManager.getRateLimitResetTime());
3499 assertEquals(2, mManager.getRemainingCallCount());
3500 });
3501 runWithCaller(CALLING_PACKAGE_2, UserHandle.USER_SYSTEM, () -> {
Makoto Onuki4dbe0de2016-03-14 17:31:49 -07003502 final Icon icon1 = Icon.createWithResource(getTestContext(), R.drawable.black_16x64);
Makoto Onuki41066a62016-03-09 16:18:44 -08003503 final Icon icon2 = Icon.createWithBitmap(BitmapFactory.decodeResource(
Makoto Onuki4dbe0de2016-03-14 17:31:49 -07003504 getTestContext().getResources(), R.drawable.icon2));
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003505
Makoto Onuki41066a62016-03-09 16:18:44 -08003506 final ShortcutInfo si1 = makeShortcut(
3507 "s1",
3508 "title2-1",
3509 makeComponent(ShortcutActivity.class),
3510 icon1,
3511 makeIntent(Intent.ACTION_ASSIST, ShortcutActivity2.class,
Makoto Onuki99302b52017-03-29 12:42:26 -07003512 "key1", "val1", "nest", makeBundle("key", 123)),
3513 /* weight */ 10);
Makoto Onuki41066a62016-03-09 16:18:44 -08003514
3515 final ShortcutInfo si2 = makeShortcut(
3516 "s2",
3517 "title2-2",
3518 /* activity */ null,
3519 icon2,
3520 makeIntent(Intent.ACTION_ASSIST, ShortcutActivity3.class),
Makoto Onuki99302b52017-03-29 12:42:26 -07003521 /* weight */ 12);
Makoto Onuki41066a62016-03-09 16:18:44 -08003522
Makoto Onukid99c6f02016-03-28 11:02:54 -07003523 assertTrue(mManager.setDynamicShortcuts(list(si1, si2)));
Makoto Onuki41066a62016-03-09 16:18:44 -08003524
3525 assertEquals(START_TIME + INTERVAL, mManager.getRateLimitResetTime());
3526 assertEquals(2, mManager.getRemainingCallCount());
3527 });
Makoto Onuki9c850012016-07-26 15:50:50 -07003528
3529 mRunningUsers.put(USER_10, true);
3530
Makoto Onuki41066a62016-03-09 16:18:44 -08003531 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
Makoto Onuki4dbe0de2016-03-14 17:31:49 -07003532 final Icon icon1 = Icon.createWithResource(getTestContext(), R.drawable.black_64x64);
Makoto Onuki41066a62016-03-09 16:18:44 -08003533 final Icon icon2 = Icon.createWithBitmap(BitmapFactory.decodeResource(
Makoto Onuki4dbe0de2016-03-14 17:31:49 -07003534 getTestContext().getResources(), R.drawable.icon2));
Makoto Onuki41066a62016-03-09 16:18:44 -08003535
3536 final ShortcutInfo si1 = makeShortcut(
3537 "s1",
3538 "title10-1-1",
3539 makeComponent(ShortcutActivity.class),
3540 icon1,
3541 makeIntent(Intent.ACTION_ASSIST, ShortcutActivity2.class,
3542 "key1", "val1", "nest", makeBundle("key", 123)),
Makoto Onuki99302b52017-03-29 12:42:26 -07003543 /* weight */ 10);
Makoto Onuki41066a62016-03-09 16:18:44 -08003544
3545 final ShortcutInfo si2 = makeShortcut(
3546 "s2",
3547 "title10-1-2",
3548 /* activity */ null,
3549 icon2,
3550 makeIntent(Intent.ACTION_ASSIST, ShortcutActivity3.class),
Makoto Onuki99302b52017-03-29 12:42:26 -07003551 /* weight */ 12);
Makoto Onuki41066a62016-03-09 16:18:44 -08003552
Makoto Onukid99c6f02016-03-28 11:02:54 -07003553 assertTrue(mManager.setDynamicShortcuts(list(si1, si2)));
Makoto Onuki41066a62016-03-09 16:18:44 -08003554
3555 assertEquals(START_TIME + INTERVAL, mManager.getRateLimitResetTime());
3556 assertEquals(2, mManager.getRemainingCallCount());
3557 });
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003558
Makoto Onuki10305202016-07-14 18:14:08 -07003559 mService.getShortcutsForTest().get(UserHandle.USER_SYSTEM).setLauncher(
Makoto Onukic51b2872016-05-04 15:24:50 -07003560 new ComponentName("pkg1", "class"));
Makoto Onuki2d5b4652016-03-11 16:09:54 -08003561
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003562 // Restore.
Makoto Onukiaa8b94a2016-03-17 13:14:05 -07003563 mService.saveDirtyInfo();
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003564 initService();
3565
3566 // Before the load, the map should be empty.
3567 assertEquals(0, mService.getShortcutsForTest().size());
3568
3569 // this will pre-load the per-user info.
Makoto Onukicdc78f72016-03-21 15:47:52 -07003570 mService.handleUnlockUser(UserHandle.USER_SYSTEM);
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003571
3572 // Now it's loaded.
3573 assertEquals(1, mService.getShortcutsForTest().size());
3574
Makoto Onuki41066a62016-03-09 16:18:44 -08003575 runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
3576 assertShortcutIds(assertAllDynamic(assertAllHaveIntents(assertAllHaveIcon(
3577 mManager.getDynamicShortcuts()))), "s1", "s2");
3578 assertEquals(2, mManager.getRemainingCallCount());
3579
3580 assertEquals("title1-1", getCallerShortcut("s1").getTitle());
3581 assertEquals("title1-2", getCallerShortcut("s2").getTitle());
3582 });
3583 runWithCaller(CALLING_PACKAGE_2, UserHandle.USER_SYSTEM, () -> {
3584 assertShortcutIds(assertAllDynamic(assertAllHaveIntents(assertAllHaveIcon(
3585 mManager.getDynamicShortcuts()))), "s1", "s2");
3586 assertEquals(2, mManager.getRemainingCallCount());
3587
3588 assertEquals("title2-1", getCallerShortcut("s1").getTitle());
3589 assertEquals("title2-2", getCallerShortcut("s2").getTitle());
3590 });
3591
Makoto Onuki2d5b4652016-03-11 16:09:54 -08003592 assertEquals("pkg1", mService.getShortcutsForTest().get(UserHandle.USER_SYSTEM)
Makoto Onuki10305202016-07-14 18:14:08 -07003593 .getLastKnownLauncher().getPackageName());
Makoto Onuki2d5b4652016-03-11 16:09:54 -08003594
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003595 // Start another user
Makoto Onukicdc78f72016-03-21 15:47:52 -07003596 mService.handleUnlockUser(USER_10);
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003597
3598 // Now the size is 2.
3599 assertEquals(2, mService.getShortcutsForTest().size());
3600
Makoto Onuki41066a62016-03-09 16:18:44 -08003601 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
3602 assertShortcutIds(assertAllDynamic(assertAllHaveIntents(assertAllHaveIcon(
3603 mManager.getDynamicShortcuts()))), "s1", "s2");
3604 assertEquals(2, mManager.getRemainingCallCount());
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003605
Makoto Onuki41066a62016-03-09 16:18:44 -08003606 assertEquals("title10-1-1", getCallerShortcut("s1").getTitle());
3607 assertEquals("title10-1-2", getCallerShortcut("s2").getTitle());
3608 });
Makoto Onuki10305202016-07-14 18:14:08 -07003609 assertNull(mService.getShortcutsForTest().get(USER_10).getLastKnownLauncher());
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003610
3611 // Try stopping the user
Makoto Onuki01ce92b2017-04-28 12:24:16 -07003612 mService.handleStopUser(USER_10);
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003613
3614 // Now it's unloaded.
3615 assertEquals(1, mService.getShortcutsForTest().size());
3616
3617 // TODO Check all other fields
3618 }
Makoto Onuki2d5b4652016-03-11 16:09:54 -08003619
Makoto Onukicdc78f72016-03-21 15:47:52 -07003620 public void testCleanupPackage() {
3621 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
Makoto Onukid99c6f02016-03-28 11:02:54 -07003622 assertTrue(mManager.setDynamicShortcuts(list(
Makoto Onukicdc78f72016-03-21 15:47:52 -07003623 makeShortcut("s0_1"))));
3624 });
3625 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
Makoto Onukid99c6f02016-03-28 11:02:54 -07003626 assertTrue(mManager.setDynamicShortcuts(list(
Makoto Onukicdc78f72016-03-21 15:47:52 -07003627 makeShortcut("s0_2"))));
3628 });
3629 runWithCaller(LAUNCHER_1, USER_0, () -> {
Makoto Onukid99c6f02016-03-28 11:02:54 -07003630 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s0_1"),
3631 HANDLE_USER_0);
3632 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s0_2"),
3633 HANDLE_USER_0);
Makoto Onukicdc78f72016-03-21 15:47:52 -07003634 });
3635 runWithCaller(LAUNCHER_2, USER_0, () -> {
Makoto Onukid99c6f02016-03-28 11:02:54 -07003636 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s0_1"),
3637 HANDLE_USER_0);
3638 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s0_2"),
3639 HANDLE_USER_0);
Makoto Onukicdc78f72016-03-21 15:47:52 -07003640 });
3641
Makoto Onuki9c850012016-07-26 15:50:50 -07003642 mRunningUsers.put(USER_10, true);
3643
Makoto Onukicdc78f72016-03-21 15:47:52 -07003644 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
Makoto Onukid99c6f02016-03-28 11:02:54 -07003645 assertTrue(mManager.setDynamicShortcuts(list(
Makoto Onukicdc78f72016-03-21 15:47:52 -07003646 makeShortcut("s10_1"))));
3647 });
3648 runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
Makoto Onukid99c6f02016-03-28 11:02:54 -07003649 assertTrue(mManager.setDynamicShortcuts(list(
Makoto Onukicdc78f72016-03-21 15:47:52 -07003650 makeShortcut("s10_2"))));
3651 });
3652 runWithCaller(LAUNCHER_1, USER_10, () -> {
Makoto Onukid99c6f02016-03-28 11:02:54 -07003653 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s10_1"),
3654 HANDLE_USER_10);
3655 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s10_2"),
3656 HANDLE_USER_10);
Makoto Onukicdc78f72016-03-21 15:47:52 -07003657 });
3658 runWithCaller(LAUNCHER_2, USER_10, () -> {
Makoto Onukid99c6f02016-03-28 11:02:54 -07003659 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s10_1"),
3660 HANDLE_USER_10);
3661 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s10_2"),
3662 HANDLE_USER_10);
Makoto Onukicdc78f72016-03-21 15:47:52 -07003663 });
3664
3665 // Remove all dynamic shortcuts; now all shortcuts are just pinned.
3666 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
Makoto Onukib6d35232016-04-04 15:57:17 -07003667 mManager.removeAllDynamicShortcuts();
Makoto Onukicdc78f72016-03-21 15:47:52 -07003668 });
3669 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
Makoto Onukib6d35232016-04-04 15:57:17 -07003670 mManager.removeAllDynamicShortcuts();
Makoto Onukicdc78f72016-03-21 15:47:52 -07003671 });
3672 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
Makoto Onukib6d35232016-04-04 15:57:17 -07003673 mManager.removeAllDynamicShortcuts();
Makoto Onukicdc78f72016-03-21 15:47:52 -07003674 });
3675 runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
Makoto Onukib6d35232016-04-04 15:57:17 -07003676 mManager.removeAllDynamicShortcuts();
Makoto Onukicdc78f72016-03-21 15:47:52 -07003677 });
3678
3679
Makoto Onukia4f89b12017-10-05 10:37:55 -07003680 final SparseArray<ShortcutUser> users = mService.getShortcutsForTest();
Makoto Onukicdc78f72016-03-21 15:47:52 -07003681 assertEquals(2, users.size());
3682 assertEquals(USER_0, users.keyAt(0));
3683 assertEquals(USER_10, users.keyAt(1));
3684
Makoto Onukia4f89b12017-10-05 10:37:55 -07003685 final ShortcutUser user0 = users.get(USER_0);
3686 final ShortcutUser user10 = users.get(USER_10);
Makoto Onukicdc78f72016-03-21 15:47:52 -07003687
3688
3689 // Check the registered packages.
Makoto Onukid99c6f02016-03-28 11:02:54 -07003690 dumpsysOnLogcat();
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003691 assertEquals(set(CALLING_PACKAGE_1, CALLING_PACKAGE_2),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003692 hashSet(user0.getAllPackagesForTest().keySet()));
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003693 assertEquals(set(CALLING_PACKAGE_1, CALLING_PACKAGE_2),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003694 hashSet(user10.getAllPackagesForTest().keySet()));
Makoto Onukid99c6f02016-03-28 11:02:54 -07003695 assertEquals(
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003696 set(PackageWithUser.of(USER_0, LAUNCHER_1),
Makoto Onukid99c6f02016-03-28 11:02:54 -07003697 PackageWithUser.of(USER_0, LAUNCHER_2)),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003698 hashSet(user0.getAllLaunchersForTest().keySet()));
Makoto Onukid99c6f02016-03-28 11:02:54 -07003699 assertEquals(
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003700 set(PackageWithUser.of(USER_10, LAUNCHER_1),
Makoto Onukid99c6f02016-03-28 11:02:54 -07003701 PackageWithUser.of(USER_10, LAUNCHER_2)),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003702 hashSet(user10.getAllLaunchersForTest().keySet()));
Makoto Onukicdc78f72016-03-21 15:47:52 -07003703 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_0),
3704 "s0_1", "s0_2");
3705 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_0),
3706 "s0_1", "s0_2");
3707 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_10),
3708 "s10_1", "s10_2");
3709 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_10),
3710 "s10_1", "s10_2");
3711 assertShortcutExists(CALLING_PACKAGE_1, "s0_1", USER_0);
3712 assertShortcutExists(CALLING_PACKAGE_2, "s0_2", USER_0);
3713 assertShortcutExists(CALLING_PACKAGE_1, "s10_1", USER_10);
3714 assertShortcutExists(CALLING_PACKAGE_2, "s10_2", USER_10);
3715
3716 mService.saveDirtyInfo();
3717
3718 // Nonexistent package.
Makoto Onuki2e210c42016-03-30 08:30:36 -07003719 uninstallPackage(USER_0, "abc");
Makoto Onukib08790c2016-06-23 14:05:46 -07003720 mService.cleanUpPackageLocked("abc", USER_0, USER_0, /* appStillExists = */ false);
Makoto Onukicdc78f72016-03-21 15:47:52 -07003721
3722 // No changes.
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003723 assertEquals(set(CALLING_PACKAGE_1, CALLING_PACKAGE_2),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003724 hashSet(user0.getAllPackagesForTest().keySet()));
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003725 assertEquals(set(CALLING_PACKAGE_1, CALLING_PACKAGE_2),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003726 hashSet(user10.getAllPackagesForTest().keySet()));
Makoto Onukid99c6f02016-03-28 11:02:54 -07003727 assertEquals(
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003728 set(PackageWithUser.of(USER_0, LAUNCHER_1),
Makoto Onukid99c6f02016-03-28 11:02:54 -07003729 PackageWithUser.of(USER_0, LAUNCHER_2)),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003730 hashSet(user0.getAllLaunchersForTest().keySet()));
Makoto Onukid99c6f02016-03-28 11:02:54 -07003731 assertEquals(
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003732 set(PackageWithUser.of(USER_10, LAUNCHER_1),
Makoto Onukid99c6f02016-03-28 11:02:54 -07003733 PackageWithUser.of(USER_10, LAUNCHER_2)),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003734 hashSet(user10.getAllLaunchersForTest().keySet()));
Makoto Onukicdc78f72016-03-21 15:47:52 -07003735 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_0),
3736 "s0_1", "s0_2");
3737 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_0),
3738 "s0_1", "s0_2");
3739 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_10),
3740 "s10_1", "s10_2");
3741 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_10),
3742 "s10_1", "s10_2");
3743 assertShortcutExists(CALLING_PACKAGE_1, "s0_1", USER_0);
3744 assertShortcutExists(CALLING_PACKAGE_2, "s0_2", USER_0);
3745 assertShortcutExists(CALLING_PACKAGE_1, "s10_1", USER_10);
3746 assertShortcutExists(CALLING_PACKAGE_2, "s10_2", USER_10);
3747
3748 mService.saveDirtyInfo();
3749
3750 // Remove a package.
Makoto Onuki2e210c42016-03-30 08:30:36 -07003751 uninstallPackage(USER_0, CALLING_PACKAGE_1);
Makoto Onukib08790c2016-06-23 14:05:46 -07003752 mService.cleanUpPackageLocked(CALLING_PACKAGE_1, USER_0, USER_0,
3753 /* appStillExists = */ false);
Makoto Onukicdc78f72016-03-21 15:47:52 -07003754
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003755 assertEquals(set(CALLING_PACKAGE_2),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003756 hashSet(user0.getAllPackagesForTest().keySet()));
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003757 assertEquals(set(CALLING_PACKAGE_1, CALLING_PACKAGE_2),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003758 hashSet(user10.getAllPackagesForTest().keySet()));
Makoto Onukid99c6f02016-03-28 11:02:54 -07003759 assertEquals(
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003760 set(PackageWithUser.of(USER_0, LAUNCHER_1),
Makoto Onukid99c6f02016-03-28 11:02:54 -07003761 PackageWithUser.of(USER_0, LAUNCHER_2)),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003762 hashSet(user0.getAllLaunchersForTest().keySet()));
Makoto Onukid99c6f02016-03-28 11:02:54 -07003763 assertEquals(
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003764 set(PackageWithUser.of(USER_10, LAUNCHER_1),
Makoto Onukid99c6f02016-03-28 11:02:54 -07003765 PackageWithUser.of(USER_10, LAUNCHER_2)),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003766 hashSet(user10.getAllLaunchersForTest().keySet()));
Makoto Onukicdc78f72016-03-21 15:47:52 -07003767 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_0),
3768 "s0_2");
3769 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_0),
3770 "s0_2");
3771 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_10),
3772 "s10_1", "s10_2");
3773 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_10),
3774 "s10_1", "s10_2");
3775 assertShortcutNotExists(CALLING_PACKAGE_1, "s0_1", USER_0);
3776 assertShortcutExists(CALLING_PACKAGE_2, "s0_2", USER_0);
3777 assertShortcutExists(CALLING_PACKAGE_1, "s10_1", USER_10);
3778 assertShortcutExists(CALLING_PACKAGE_2, "s10_2", USER_10);
3779
3780 mService.saveDirtyInfo();
3781
3782 // Remove a launcher.
Makoto Onuki2e210c42016-03-30 08:30:36 -07003783 uninstallPackage(USER_10, LAUNCHER_1);
Makoto Onukib08790c2016-06-23 14:05:46 -07003784 mService.cleanUpPackageLocked(LAUNCHER_1, USER_10, USER_10, /* appStillExists = */ false);
Makoto Onukicdc78f72016-03-21 15:47:52 -07003785
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003786 assertEquals(set(CALLING_PACKAGE_2),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003787 hashSet(user0.getAllPackagesForTest().keySet()));
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003788 assertEquals(set(CALLING_PACKAGE_1, CALLING_PACKAGE_2),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003789 hashSet(user10.getAllPackagesForTest().keySet()));
Makoto Onukid99c6f02016-03-28 11:02:54 -07003790 assertEquals(
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003791 set(PackageWithUser.of(USER_0, LAUNCHER_1),
Makoto Onukid99c6f02016-03-28 11:02:54 -07003792 PackageWithUser.of(USER_0, LAUNCHER_2)),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003793 hashSet(user0.getAllLaunchersForTest().keySet()));
Makoto Onukid99c6f02016-03-28 11:02:54 -07003794 assertEquals(
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003795 set(PackageWithUser.of(USER_10, LAUNCHER_2)),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003796 hashSet(user10.getAllLaunchersForTest().keySet()));
Makoto Onukicdc78f72016-03-21 15:47:52 -07003797 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_0),
3798 "s0_2");
3799 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_0),
3800 "s0_2");
3801 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_10),
3802 "s10_1", "s10_2");
3803 assertShortcutNotExists(CALLING_PACKAGE_1, "s0_1", USER_0);
3804 assertShortcutExists(CALLING_PACKAGE_2, "s0_2", USER_0);
3805 assertShortcutExists(CALLING_PACKAGE_1, "s10_1", USER_10);
3806 assertShortcutExists(CALLING_PACKAGE_2, "s10_2", USER_10);
3807
3808 mService.saveDirtyInfo();
3809
3810 // Remove a package.
Makoto Onuki2e210c42016-03-30 08:30:36 -07003811 uninstallPackage(USER_10, CALLING_PACKAGE_2);
Makoto Onukib08790c2016-06-23 14:05:46 -07003812 mService.cleanUpPackageLocked(CALLING_PACKAGE_2, USER_10, USER_10,
3813 /* appStillExists = */ false);
Makoto Onukicdc78f72016-03-21 15:47:52 -07003814
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003815 assertEquals(set(CALLING_PACKAGE_2),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003816 hashSet(user0.getAllPackagesForTest().keySet()));
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003817 assertEquals(set(CALLING_PACKAGE_1),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003818 hashSet(user10.getAllPackagesForTest().keySet()));
Makoto Onukid99c6f02016-03-28 11:02:54 -07003819 assertEquals(
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003820 set(PackageWithUser.of(USER_0, LAUNCHER_1),
Makoto Onukid99c6f02016-03-28 11:02:54 -07003821 PackageWithUser.of(USER_0, LAUNCHER_2)),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003822 hashSet(user0.getAllLaunchersForTest().keySet()));
Makoto Onukid99c6f02016-03-28 11:02:54 -07003823 assertEquals(
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003824 set(PackageWithUser.of(USER_10, LAUNCHER_2)),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003825 hashSet(user10.getAllLaunchersForTest().keySet()));
Makoto Onukicdc78f72016-03-21 15:47:52 -07003826 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_0),
3827 "s0_2");
3828 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_0),
3829 "s0_2");
3830 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_10),
3831 "s10_1");
3832 assertShortcutNotExists(CALLING_PACKAGE_1, "s0_1", USER_0);
3833 assertShortcutExists(CALLING_PACKAGE_2, "s0_2", USER_0);
3834 assertShortcutExists(CALLING_PACKAGE_1, "s10_1", USER_10);
3835 assertShortcutNotExists(CALLING_PACKAGE_2, "s10_2", USER_10);
3836
3837 mService.saveDirtyInfo();
3838
3839 // Remove the other launcher from user 10 too.
Makoto Onuki2e210c42016-03-30 08:30:36 -07003840 uninstallPackage(USER_10, LAUNCHER_2);
Makoto Onukib08790c2016-06-23 14:05:46 -07003841 mService.cleanUpPackageLocked(LAUNCHER_2, USER_10, USER_10,
3842 /* appStillExists = */ false);
Makoto Onukicdc78f72016-03-21 15:47:52 -07003843
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003844 assertEquals(set(CALLING_PACKAGE_2),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003845 hashSet(user0.getAllPackagesForTest().keySet()));
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003846 assertEquals(set(CALLING_PACKAGE_1),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003847 hashSet(user10.getAllPackagesForTest().keySet()));
Makoto Onukid99c6f02016-03-28 11:02:54 -07003848 assertEquals(
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003849 set(PackageWithUser.of(USER_0, LAUNCHER_1),
Makoto Onukid99c6f02016-03-28 11:02:54 -07003850 PackageWithUser.of(USER_0, LAUNCHER_2)),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003851 hashSet(user0.getAllLaunchersForTest().keySet()));
Makoto Onukid99c6f02016-03-28 11:02:54 -07003852 assertEquals(
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003853 set(),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003854 hashSet(user10.getAllLaunchersForTest().keySet()));
Makoto Onukicdc78f72016-03-21 15:47:52 -07003855 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_0),
3856 "s0_2");
3857 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_0),
3858 "s0_2");
3859
3860 // Note the pinned shortcuts on user-10 no longer referred, so they should both be removed.
3861 assertShortcutNotExists(CALLING_PACKAGE_1, "s0_1", USER_0);
3862 assertShortcutExists(CALLING_PACKAGE_2, "s0_2", USER_0);
3863 assertShortcutNotExists(CALLING_PACKAGE_1, "s10_1", USER_10);
3864 assertShortcutNotExists(CALLING_PACKAGE_2, "s10_2", USER_10);
3865
3866 mService.saveDirtyInfo();
3867
3868 // More remove.
Makoto Onuki2e210c42016-03-30 08:30:36 -07003869 uninstallPackage(USER_10, CALLING_PACKAGE_1);
Makoto Onukib08790c2016-06-23 14:05:46 -07003870 mService.cleanUpPackageLocked(CALLING_PACKAGE_1, USER_10, USER_10,
3871 /* appStillExists = */ false);
Makoto Onukicdc78f72016-03-21 15:47:52 -07003872
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003873 assertEquals(set(CALLING_PACKAGE_2),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003874 hashSet(user0.getAllPackagesForTest().keySet()));
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003875 assertEquals(set(),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003876 hashSet(user10.getAllPackagesForTest().keySet()));
Makoto Onukid99c6f02016-03-28 11:02:54 -07003877 assertEquals(
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003878 set(PackageWithUser.of(USER_0, LAUNCHER_1),
Makoto Onukid99c6f02016-03-28 11:02:54 -07003879 PackageWithUser.of(USER_0, LAUNCHER_2)),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003880 hashSet(user0.getAllLaunchersForTest().keySet()));
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003881 assertEquals(set(),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003882 hashSet(user10.getAllLaunchersForTest().keySet()));
Makoto Onukicdc78f72016-03-21 15:47:52 -07003883 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_0),
3884 "s0_2");
3885 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_0),
3886 "s0_2");
3887
3888 // Note the pinned shortcuts on user-10 no longer referred, so they should both be removed.
3889 assertShortcutNotExists(CALLING_PACKAGE_1, "s0_1", USER_0);
3890 assertShortcutExists(CALLING_PACKAGE_2, "s0_2", USER_0);
3891 assertShortcutNotExists(CALLING_PACKAGE_1, "s10_1", USER_10);
3892 assertShortcutNotExists(CALLING_PACKAGE_2, "s10_2", USER_10);
3893
3894 mService.saveDirtyInfo();
3895 }
3896
Makoto Onukib08790c2016-06-23 14:05:46 -07003897 public void testCleanupPackage_republishManifests() {
3898 addManifestShortcutResource(
3899 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
3900 R.xml.shortcut_2);
3901 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukia4f89b12017-10-05 10:37:55 -07003902 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onukib08790c2016-06-23 14:05:46 -07003903 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
3904
3905 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3906 assertTrue(mManager.setDynamicShortcuts(list(
3907 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
3908 });
3909 runWithCaller(LAUNCHER_1, USER_0, () -> {
3910 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
3911 list("s2", "s3", "ms1", "ms2"), HANDLE_USER_0);
3912 });
3913
3914 // Remove ms2 from manifest.
3915 addManifestShortcutResource(
3916 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
3917 R.xml.shortcut_1);
3918 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukia4f89b12017-10-05 10:37:55 -07003919 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onukib08790c2016-06-23 14:05:46 -07003920 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
3921
3922 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3923 assertTrue(mManager.setDynamicShortcuts(list(
3924 makeShortcut("s1"), makeShortcut("s2"))));
3925
3926 // Make sure the shortcuts are in the intended state.
3927 assertWith(getCallerShortcuts())
3928 .haveIds("ms1", "ms2", "s1", "s2", "s3")
3929
3930 .selectByIds("ms1")
3931 .areAllManifest()
3932 .areAllPinned()
3933
3934 .revertToOriginalList()
3935 .selectByIds("ms2")
3936 .areAllNotManifest()
3937 .areAllPinned()
3938
3939 .revertToOriginalList()
3940 .selectByIds("s1")
3941 .areAllDynamic()
3942 .areAllNotPinned()
3943
3944 .revertToOriginalList()
3945 .selectByIds("s2")
3946 .areAllDynamic()
3947 .areAllPinned()
3948
3949 .revertToOriginalList()
3950 .selectByIds("s3")
3951 .areAllNotDynamic()
3952 .areAllPinned();
3953 });
3954
3955 // Clean up + re-publish manifests.
3956 mService.cleanUpPackageLocked(CALLING_PACKAGE_1, USER_0, USER_0,
3957 /* appStillExists = */ true);
3958 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3959 assertWith(getCallerShortcuts())
3960 .haveIds("ms1")
3961 .areAllManifest();
3962 });
3963 }
3964
Makoto Onukid99c6f02016-03-28 11:02:54 -07003965 public void testHandleGonePackage_crossProfile() {
3966 // Create some shortcuts.
3967 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3968 assertTrue(mManager.setDynamicShortcuts(list(
3969 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
3970 });
3971 runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
3972 assertTrue(mManager.setDynamicShortcuts(list(
3973 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
3974 });
3975 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
3976 assertTrue(mManager.setDynamicShortcuts(list(
3977 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
3978 });
Makoto Onuki9c850012016-07-26 15:50:50 -07003979
3980 mRunningUsers.put(USER_10, true);
3981
Makoto Onukid99c6f02016-03-28 11:02:54 -07003982 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
3983 assertTrue(mManager.setDynamicShortcuts(list(
3984 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
3985 });
3986
3987 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
3988 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
3989 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
3990
3991 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
3992 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
3993 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
3994
3995 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
3996 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
3997 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
3998
3999 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
4000 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
4001 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
4002
4003 // Pin some.
4004
4005 runWithCaller(LAUNCHER_1, USER_0, () -> {
4006 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
4007 list("s1"), HANDLE_USER_0);
4008
4009 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
4010 list("s2"), UserHandle.of(USER_P0));
4011
4012 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
4013 list("s3"), HANDLE_USER_0);
4014 });
4015
4016 runWithCaller(LAUNCHER_1, USER_P0, () -> {
4017 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
4018 list("s2"), HANDLE_USER_0);
4019
4020 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
4021 list("s3"), UserHandle.of(USER_P0));
4022
4023 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
4024 list("s1"), HANDLE_USER_0);
4025 });
4026
4027 runWithCaller(LAUNCHER_1, USER_10, () -> {
4028 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
4029 list("s3"), HANDLE_USER_10);
4030 });
4031
4032 // Check the state.
4033
4034 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
4035 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
4036 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
4037
4038 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
4039 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
4040 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
4041
4042 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
4043 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
4044 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
4045
4046 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
4047 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
4048 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
4049
Makoto Onukid99c6f02016-03-28 11:02:54 -07004050 // Make sure all the information is persisted.
4051 mService.saveDirtyInfo();
4052 initService();
4053 mService.handleUnlockUser(USER_0);
4054 mService.handleUnlockUser(USER_P0);
4055 mService.handleUnlockUser(USER_10);
4056
4057 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
4058 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
4059 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
4060
4061 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
4062 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
4063 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
4064
4065 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
4066 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
4067 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
4068
4069 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
4070 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
4071 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
4072
Makoto Onukid99c6f02016-03-28 11:02:54 -07004073 // Start uninstalling.
4074 uninstallPackage(USER_10, LAUNCHER_1);
Makoto Onuki39686e82016-04-13 18:03:00 -07004075 mService.checkPackageChanges(USER_10);
Makoto Onukid99c6f02016-03-28 11:02:54 -07004076
4077 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
4078 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
4079 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
4080
4081 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
4082 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
4083 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
4084
4085 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
4086 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
4087 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
4088
4089 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
4090 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
4091 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
4092
Makoto Onukid99c6f02016-03-28 11:02:54 -07004093 // Uninstall.
4094 uninstallPackage(USER_10, CALLING_PACKAGE_1);
Makoto Onuki39686e82016-04-13 18:03:00 -07004095 mService.checkPackageChanges(USER_10);
Makoto Onukid99c6f02016-03-28 11:02:54 -07004096
4097 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
4098 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
4099 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
4100
4101 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
4102 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
4103 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
4104
4105 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
4106 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
4107 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
4108
4109 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
4110 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
4111 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
4112
Makoto Onukid99c6f02016-03-28 11:02:54 -07004113 uninstallPackage(USER_P0, LAUNCHER_1);
Makoto Onuki39686e82016-04-13 18:03:00 -07004114 mService.checkPackageChanges(USER_0);
Makoto Onukid99c6f02016-03-28 11:02:54 -07004115
4116 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
4117 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
4118 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
4119
4120 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
4121 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
4122 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
4123
4124 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
4125 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
4126 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
4127
4128 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
4129 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
4130 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
4131
Makoto Onuki39686e82016-04-13 18:03:00 -07004132 mService.checkPackageChanges(USER_P0);
Makoto Onuki7001a612016-05-27 13:24:28 -07004133
Makoto Onukid99c6f02016-03-28 11:02:54 -07004134 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
4135 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
4136 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
4137
4138 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
4139 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
4140 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
4141
4142 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
4143 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
4144 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
4145
4146 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
4147 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
4148 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
4149
Makoto Onukid99c6f02016-03-28 11:02:54 -07004150 uninstallPackage(USER_P0, CALLING_PACKAGE_1);
4151
4152 mService.saveDirtyInfo();
4153 initService();
4154 mService.handleUnlockUser(USER_0);
4155 mService.handleUnlockUser(USER_P0);
4156 mService.handleUnlockUser(USER_10);
4157
4158 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
4159 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
4160 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
4161
4162 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
4163 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
4164 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
4165
4166 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
4167 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
4168 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
4169
4170 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
4171 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
4172 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
4173
Makoto Onukid99c6f02016-03-28 11:02:54 -07004174 // Uninstall
4175 uninstallPackage(USER_0, LAUNCHER_1);
4176
4177 mService.saveDirtyInfo();
4178 initService();
4179 mService.handleUnlockUser(USER_0);
4180 mService.handleUnlockUser(USER_P0);
4181 mService.handleUnlockUser(USER_10);
4182
4183 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
4184 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
4185 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
4186
4187 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
4188 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
4189 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
4190
4191 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
4192 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
4193 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
4194
4195 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
4196 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
4197 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
4198
Makoto Onukid99c6f02016-03-28 11:02:54 -07004199 uninstallPackage(USER_0, CALLING_PACKAGE_2);
4200
4201 mService.saveDirtyInfo();
4202 initService();
4203 mService.handleUnlockUser(USER_0);
4204 mService.handleUnlockUser(USER_P0);
4205 mService.handleUnlockUser(USER_10);
4206
4207 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
4208 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
4209 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
4210
4211 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
4212 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
4213 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
4214
4215 assertNull(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
4216 assertNull(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
4217 assertNull(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
4218
4219 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
4220 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
4221 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
Makoto Onukid99c6f02016-03-28 11:02:54 -07004222 }
4223
Makoto Onukia4f89b12017-10-05 10:37:55 -07004224 protected void checkCanRestoreTo(int expected, ShortcutPackageInfo spi,
4225 boolean anyVersionOk, int version, boolean nowBackupAllowed, String... signatures) {
4226 final PackageInfo pi = genPackage("dummy", /* uid */ 0, version, signatures);
4227 if (!nowBackupAllowed) {
4228 pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP;
4229 }
Michal Karpinski528c3e52018-02-07 17:47:10 +00004230
4231 doReturn(expected != DISABLED_REASON_SIGNATURE_MISMATCH).when(
4232 mMockPackageManagerInternal).isDataRestoreSafe(any(byte[].class), anyString());
4233
Makoto Onukia4f89b12017-10-05 10:37:55 -07004234 assertEquals(expected, spi.canRestoreTo(mService, pi, anyVersionOk));
Makoto Onuki0acbb142016-03-22 17:02:57 -07004235 }
4236
4237 public void testCanRestoreTo() {
4238 addPackage(CALLING_PACKAGE_1, CALLING_UID_1, 10, "sig1");
Makoto Onukia4f89b12017-10-05 10:37:55 -07004239 addPackage(CALLING_PACKAGE_2, CALLING_UID_2, 10, "sig1", "sig2");
4240 addPackage(CALLING_PACKAGE_3, CALLING_UID_3, 10, "sig1");
4241
4242 updatePackageInfo(CALLING_PACKAGE_3,
4243 pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP);
Makoto Onuki0acbb142016-03-22 17:02:57 -07004244
Makoto Onukic8c33292016-09-12 16:36:59 -07004245 final ShortcutPackageInfo spi1 = ShortcutPackageInfo.generateForInstalledPackageForTest(
Makoto Onuki0acbb142016-03-22 17:02:57 -07004246 mService, CALLING_PACKAGE_1, USER_0);
Makoto Onukic8c33292016-09-12 16:36:59 -07004247 final ShortcutPackageInfo spi2 = ShortcutPackageInfo.generateForInstalledPackageForTest(
Makoto Onuki0acbb142016-03-22 17:02:57 -07004248 mService, CALLING_PACKAGE_2, USER_0);
Makoto Onukia4f89b12017-10-05 10:37:55 -07004249 final ShortcutPackageInfo spi3 = ShortcutPackageInfo.generateForInstalledPackageForTest(
4250 mService, CALLING_PACKAGE_3, USER_0);
Makoto Onuki0acbb142016-03-22 17:02:57 -07004251
Makoto Onukia4f89b12017-10-05 10:37:55 -07004252 checkCanRestoreTo(DISABLED_REASON_NOT_DISABLED, spi1, false, 10, true, "sig1");
4253 checkCanRestoreTo(DISABLED_REASON_NOT_DISABLED, spi1, false, 10, true, "x", "sig1");
4254 checkCanRestoreTo(DISABLED_REASON_NOT_DISABLED, spi1, false, 10, true, "sig1", "y");
4255 checkCanRestoreTo(DISABLED_REASON_NOT_DISABLED, spi1, false, 10, true, "x", "sig1", "y");
4256 checkCanRestoreTo(DISABLED_REASON_NOT_DISABLED, spi1, false, 11, true, "sig1");
Makoto Onuki0acbb142016-03-22 17:02:57 -07004257
Makoto Onukia4f89b12017-10-05 10:37:55 -07004258 checkCanRestoreTo(DISABLED_REASON_SIGNATURE_MISMATCH, spi1, false, 10, true/* empty */);
4259 checkCanRestoreTo(DISABLED_REASON_SIGNATURE_MISMATCH, spi1, false, 10, true, "x");
4260 checkCanRestoreTo(DISABLED_REASON_SIGNATURE_MISMATCH, spi1, false, 10, true, "x", "y");
4261 checkCanRestoreTo(DISABLED_REASON_SIGNATURE_MISMATCH, spi1, false, 10, true, "x");
4262 checkCanRestoreTo(DISABLED_REASON_VERSION_LOWER, spi1, false, 9, true, "sig1");
Makoto Onuki0acbb142016-03-22 17:02:57 -07004263
Makoto Onukia4f89b12017-10-05 10:37:55 -07004264 // Any version okay.
4265 checkCanRestoreTo(DISABLED_REASON_NOT_DISABLED, spi1, true, 9, true, "sig1");
4266 checkCanRestoreTo(DISABLED_REASON_NOT_DISABLED, spi1, true, 9, true, "sig1");
Makoto Onuki0acbb142016-03-22 17:02:57 -07004267
Makoto Onukia4f89b12017-10-05 10:37:55 -07004268 checkCanRestoreTo(DISABLED_REASON_NOT_DISABLED, spi2, false, 10, true, "sig1", "sig2");
4269 checkCanRestoreTo(DISABLED_REASON_NOT_DISABLED, spi2, false, 10, true, "sig2", "sig1");
4270 checkCanRestoreTo(DISABLED_REASON_NOT_DISABLED, spi2, false, 10, true, "x", "sig1", "sig2");
4271 checkCanRestoreTo(DISABLED_REASON_NOT_DISABLED, spi2, false, 10, true, "x", "sig2", "sig1");
4272 checkCanRestoreTo(DISABLED_REASON_NOT_DISABLED, spi2, false, 10, true, "sig1", "sig2", "y");
4273 checkCanRestoreTo(DISABLED_REASON_NOT_DISABLED, spi2, false, 10, true, "sig2", "sig1", "y");
4274 checkCanRestoreTo(DISABLED_REASON_NOT_DISABLED, spi2, false, 10, true, "x", "sig1", "sig2", "y");
4275 checkCanRestoreTo(DISABLED_REASON_NOT_DISABLED, spi2, false, 10, true, "x", "sig2", "sig1", "y");
4276 checkCanRestoreTo(DISABLED_REASON_NOT_DISABLED, spi2, false, 11, true, "x", "sig2", "sig1", "y");
4277
4278 checkCanRestoreTo(DISABLED_REASON_SIGNATURE_MISMATCH,
4279 spi2, false, 10, true, "sig1", "sig2x");
4280 checkCanRestoreTo(DISABLED_REASON_SIGNATURE_MISMATCH,
4281 spi2, false, 10, true, "sig2", "sig1x");
4282 checkCanRestoreTo(DISABLED_REASON_SIGNATURE_MISMATCH,
4283 spi2, false, 10, true, "x", "sig1x", "sig2");
4284 checkCanRestoreTo(DISABLED_REASON_SIGNATURE_MISMATCH,
4285 spi2, false, 10, true, "x", "sig2x", "sig1");
4286 checkCanRestoreTo(DISABLED_REASON_SIGNATURE_MISMATCH,
4287 spi2, false, 10, true, "sig1", "sig2x", "y");
4288 checkCanRestoreTo(DISABLED_REASON_SIGNATURE_MISMATCH,
4289 spi2, false, 10, true, "sig2", "sig1x", "y");
4290 checkCanRestoreTo(DISABLED_REASON_SIGNATURE_MISMATCH,
4291 spi2, false, 10, true, "x", "sig1x", "sig2", "y");
4292 checkCanRestoreTo(DISABLED_REASON_SIGNATURE_MISMATCH,
4293 spi2, false, 10, true, "x", "sig2x", "sig1", "y");
4294 checkCanRestoreTo(DISABLED_REASON_SIGNATURE_MISMATCH,
4295 spi2, false, 11, true, "x", "sig2x", "sig1", "y");
4296
4297 checkCanRestoreTo(DISABLED_REASON_BACKUP_NOT_SUPPORTED, spi1, true, 10, false, "sig1");
4298 checkCanRestoreTo(DISABLED_REASON_BACKUP_NOT_SUPPORTED, spi3, true, 10, true, "sig1");
Makoto Onuki0acbb142016-03-22 17:02:57 -07004299 }
4300
Makoto Onuki0acbb142016-03-22 17:02:57 -07004301 public void testHandlePackageDelete() {
Makoto Onuki82fb2eb2017-03-31 16:58:26 -07004302 checkHandlePackageDeleteInner((userId, packageName) -> {
4303 uninstallPackage(userId, packageName);
4304 mService.mPackageMonitor.onReceive(getTestContext(),
4305 genPackageDeleteIntent(packageName, userId));
4306 });
4307 }
4308
4309 public void testHandlePackageDisable() {
4310 checkHandlePackageDeleteInner((userId, packageName) -> {
4311 disablePackage(userId, packageName);
4312 mService.mPackageMonitor.onReceive(getTestContext(),
4313 genPackageChangedIntent(packageName, userId));
4314 });
4315 }
4316
4317 private void checkHandlePackageDeleteInner(BiConsumer<Integer, String> remover) {
Makoto Onuki0033b2a2016-04-14 17:19:16 -07004318 final Icon bmp32x32 = Icon.createWithBitmap(BitmapFactory.decodeResource(
4319 getTestContext().getResources(), R.drawable.black_32x32));
Makoto Onuki0acbb142016-03-22 17:02:57 -07004320 setCaller(CALLING_PACKAGE_1, USER_0);
Makoto Onuki0033b2a2016-04-14 17:19:16 -07004321 assertTrue(mManager.addDynamicShortcuts(list(
4322 makeShortcutWithIcon("s1", bmp32x32), makeShortcutWithIcon("s2", bmp32x32)
4323 )));
Makoto Onukib08790c2016-06-23 14:05:46 -07004324 // Also add a manifest shortcut, which should be removed too.
4325 addManifestShortcutResource(
4326 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
4327 R.xml.shortcut_1);
4328 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukia4f89b12017-10-05 10:37:55 -07004329 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onukib08790c2016-06-23 14:05:46 -07004330 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
4331 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4332 assertWith(getCallerShortcuts())
4333 .haveIds("s1", "s2", "ms1")
4334
4335 .selectManifest()
4336 .haveIds("ms1");
4337 });
Makoto Onuki0acbb142016-03-22 17:02:57 -07004338
4339 setCaller(CALLING_PACKAGE_2, USER_0);
Makoto Onuki0033b2a2016-04-14 17:19:16 -07004340 assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
Makoto Onuki0acbb142016-03-22 17:02:57 -07004341
4342 setCaller(CALLING_PACKAGE_3, USER_0);
Makoto Onuki0033b2a2016-04-14 17:19:16 -07004343 assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
Makoto Onuki0acbb142016-03-22 17:02:57 -07004344
Makoto Onuki9c850012016-07-26 15:50:50 -07004345 mRunningUsers.put(USER_10, true);
4346
Makoto Onuki0acbb142016-03-22 17:02:57 -07004347 setCaller(CALLING_PACKAGE_1, USER_10);
Makoto Onuki0033b2a2016-04-14 17:19:16 -07004348 assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
Makoto Onuki0acbb142016-03-22 17:02:57 -07004349
4350 setCaller(CALLING_PACKAGE_2, USER_10);
Makoto Onuki0033b2a2016-04-14 17:19:16 -07004351 assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
Makoto Onuki0acbb142016-03-22 17:02:57 -07004352
4353 setCaller(CALLING_PACKAGE_3, USER_10);
Makoto Onuki0033b2a2016-04-14 17:19:16 -07004354 assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
Makoto Onuki0acbb142016-03-22 17:02:57 -07004355
Makoto Onuki9da23fc2016-03-29 11:14:42 -07004356 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
4357 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
4358 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));
4359 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
4360 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
4361 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
Makoto Onuki0acbb142016-03-22 17:02:57 -07004362
Makoto Onuki0033b2a2016-04-14 17:19:16 -07004363 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
4364 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
4365 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
4366 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
4367 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
4368 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
4369
Makoto Onuki82fb2eb2017-03-31 16:58:26 -07004370 remover.accept(USER_0, CALLING_PACKAGE_1);
Makoto Onuki0acbb142016-03-22 17:02:57 -07004371
Makoto Onuki9da23fc2016-03-29 11:14:42 -07004372 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
4373 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
4374 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));
4375 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
4376 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
4377 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
Makoto Onuki0acbb142016-03-22 17:02:57 -07004378
Makoto Onuki0033b2a2016-04-14 17:19:16 -07004379 assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
4380 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
4381 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
4382 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
4383 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
4384 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
4385
Makoto Onukia2241832016-07-06 13:28:37 -07004386 mRunningUsers.put(USER_10, true);
4387
Makoto Onuki82fb2eb2017-03-31 16:58:26 -07004388 remover.accept(USER_10, CALLING_PACKAGE_2);
Makoto Onuki0acbb142016-03-22 17:02:57 -07004389
Makoto Onuki9da23fc2016-03-29 11:14:42 -07004390 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
4391 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
4392 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));
4393 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
4394 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
4395 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
Makoto Onuki0acbb142016-03-22 17:02:57 -07004396
Makoto Onuki0033b2a2016-04-14 17:19:16 -07004397 assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
4398 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
4399 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
4400 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
4401 assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
4402 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
4403
Makoto Onuki0acbb142016-03-22 17:02:57 -07004404 mInjectedPackages.remove(CALLING_PACKAGE_1);
4405 mInjectedPackages.remove(CALLING_PACKAGE_3);
4406
Makoto Onuki085a05c2016-08-19 11:39:29 -07004407 mService.checkPackageChanges(USER_0);
Makoto Onuki9da23fc2016-03-29 11:14:42 -07004408
4409 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
4410 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
Makoto Onuki085a05c2016-08-19 11:39:29 -07004411 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0)); // ---------------
Makoto Onuki9da23fc2016-03-29 11:14:42 -07004412 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
4413 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
4414 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
Makoto Onuki0acbb142016-03-22 17:02:57 -07004415
Makoto Onuki0033b2a2016-04-14 17:19:16 -07004416 assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
4417 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
4418 assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
4419 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
4420 assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
4421 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
4422
Makoto Onuki085a05c2016-08-19 11:39:29 -07004423 mService.checkPackageChanges(USER_10);
Makoto Onuki9da23fc2016-03-29 11:14:42 -07004424
4425 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
4426 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
4427 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));
4428 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
4429 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
4430 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
Makoto Onuki0033b2a2016-04-14 17:19:16 -07004431
4432 assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
4433 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
4434 assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
4435 assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
4436 assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
4437 assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
Makoto Onuki0acbb142016-03-22 17:02:57 -07004438 }
4439
Makoto Onuki9ac59d02016-04-26 11:23:14 -07004440 /** Almost ame as testHandlePackageDelete, except it doesn't uninstall packages. */
4441 public void testHandlePackageClearData() {
4442 final Icon bmp32x32 = Icon.createWithBitmap(BitmapFactory.decodeResource(
4443 getTestContext().getResources(), R.drawable.black_32x32));
4444 setCaller(CALLING_PACKAGE_1, USER_0);
4445 assertTrue(mManager.addDynamicShortcuts(list(
4446 makeShortcutWithIcon("s1", bmp32x32), makeShortcutWithIcon("s2", bmp32x32)
4447 )));
4448
4449 setCaller(CALLING_PACKAGE_2, USER_0);
4450 assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
4451
4452 setCaller(CALLING_PACKAGE_3, USER_0);
4453 assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
4454
Makoto Onuki9c850012016-07-26 15:50:50 -07004455 mRunningUsers.put(USER_10, true);
4456
Makoto Onuki9ac59d02016-04-26 11:23:14 -07004457 setCaller(CALLING_PACKAGE_1, USER_10);
4458 assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
4459
4460 setCaller(CALLING_PACKAGE_2, USER_10);
4461 assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
4462
4463 setCaller(CALLING_PACKAGE_3, USER_10);
4464 assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
4465
4466 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
4467 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
4468 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));
4469 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
4470 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
4471 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
4472
4473 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
4474 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
4475 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
4476 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
4477 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
4478 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
4479
Makoto Onukia4f89b12017-10-05 10:37:55 -07004480 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki9ac59d02016-04-26 11:23:14 -07004481 genPackageDataClear(CALLING_PACKAGE_1, USER_0));
4482
4483 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
4484 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
4485 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));
4486 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
4487 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
4488 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
4489
4490 assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
4491 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
4492 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
4493 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
4494 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
4495 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
4496
Makoto Onukia2241832016-07-06 13:28:37 -07004497 mRunningUsers.put(USER_10, true);
4498
Makoto Onukia4f89b12017-10-05 10:37:55 -07004499 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki9ac59d02016-04-26 11:23:14 -07004500 genPackageDataClear(CALLING_PACKAGE_2, USER_10));
4501
4502 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
4503 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
4504 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));
4505 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
4506 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
4507 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
4508
4509 assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
4510 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
4511 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
4512 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
4513 assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
4514 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
4515 }
4516
Makoto Onukib08790c2016-06-23 14:05:46 -07004517 public void testHandlePackageClearData_manifestRepublished() {
Makoto Onuki39686e82016-04-13 18:03:00 -07004518
Makoto Onukia2241832016-07-06 13:28:37 -07004519 mRunningUsers.put(USER_10, true);
4520
Makoto Onukib08790c2016-06-23 14:05:46 -07004521 // Add two manifests and two dynamics.
4522 addManifestShortcutResource(
4523 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
4524 R.xml.shortcut_2);
4525 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukia4f89b12017-10-05 10:37:55 -07004526 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onukib08790c2016-06-23 14:05:46 -07004527 genPackageAddIntent(CALLING_PACKAGE_1, USER_10));
4528
4529 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4530 assertTrue(mManager.addDynamicShortcuts(list(
4531 makeShortcut("s1"), makeShortcut("s2"))));
4532 });
4533 runWithCaller(LAUNCHER_1, USER_10, () -> {
4534 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms2", "s2"), HANDLE_USER_10);
4535 });
4536
4537 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4538 assertWith(getCallerShortcuts())
4539 .haveIds("ms1", "ms2", "s1", "s2")
4540 .areAllEnabled()
4541
4542 .selectPinned()
4543 .haveIds("ms2", "s2");
4544 });
4545
4546 // Clear data
Makoto Onuki0b9d1db2016-07-18 14:16:41 -07004547 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onukib08790c2016-06-23 14:05:46 -07004548 genPackageDataClear(CALLING_PACKAGE_1, USER_10));
4549
4550 // Only manifest shortcuts will remain, and are no longer pinned.
4551 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4552 assertWith(getCallerShortcuts())
4553 .haveIds("ms1", "ms2")
4554 .areAllEnabled()
4555 .areAllNotPinned();
4556 });
4557 }
4558
4559 public void testHandlePackageUpdate() throws Throwable {
Makoto Onuki39686e82016-04-13 18:03:00 -07004560 // Set up shortcuts and launchers.
4561
4562 final Icon res32x32 = Icon.createWithResource(getTestContext(), R.drawable.black_32x32);
4563 final Icon bmp32x32 = Icon.createWithBitmap(BitmapFactory.decodeResource(
4564 getTestContext().getResources(), R.drawable.black_32x32));
4565
4566 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4567 assertTrue(mManager.setDynamicShortcuts(list(
4568 makeShortcut("s1"),
4569 makeShortcutWithIcon("s2", res32x32),
4570 makeShortcutWithIcon("s3", res32x32),
4571 makeShortcutWithIcon("s4", bmp32x32))));
4572 });
4573 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
4574 assertTrue(mManager.setDynamicShortcuts(list(
4575 makeShortcut("s1"),
4576 makeShortcutWithIcon("s2", bmp32x32))));
4577 });
4578 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
4579 assertTrue(mManager.setDynamicShortcuts(list(
4580 makeShortcutWithIcon("s1", res32x32))));
4581 });
4582
Makoto Onuki9c850012016-07-26 15:50:50 -07004583 mRunningUsers.put(USER_10, true);
4584
Makoto Onuki39686e82016-04-13 18:03:00 -07004585 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4586 assertTrue(mManager.setDynamicShortcuts(list(
4587 makeShortcutWithIcon("s1", res32x32),
4588 makeShortcutWithIcon("s2", res32x32))));
4589 });
4590 runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
4591 assertTrue(mManager.setDynamicShortcuts(list(
4592 makeShortcutWithIcon("s1", bmp32x32),
4593 makeShortcutWithIcon("s2", bmp32x32))));
4594 });
4595
4596 LauncherApps.Callback c0 = mock(LauncherApps.Callback.class);
4597 LauncherApps.Callback c10 = mock(LauncherApps.Callback.class);
4598
4599 runWithCaller(LAUNCHER_1, USER_0, () -> {
4600 mLauncherApps.registerCallback(c0, new Handler(Looper.getMainLooper()));
4601 });
4602 runWithCaller(LAUNCHER_1, USER_10, () -> {
4603 mLauncherApps.registerCallback(c10, new Handler(Looper.getMainLooper()));
4604 });
4605
Makoto Onuki51ab2b32016-06-02 11:03:51 -07004606 mInjectedCurrentTimeMillis = START_TIME + 100;
Makoto Onuki39686e82016-04-13 18:03:00 -07004607
4608 ArgumentCaptor<List> shortcuts;
4609
Makoto Onuki64183d52016-08-08 14:11:34 -07004610 // Update the version info for package 1.
Makoto Onuki39686e82016-04-13 18:03:00 -07004611 reset(c0);
4612 reset(c10);
4613 updatePackageVersion(CALLING_PACKAGE_1, 1);
4614
4615 // Then send the broadcast, to only user-0.
Makoto Onuki085a05c2016-08-19 11:39:29 -07004616 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki39686e82016-04-13 18:03:00 -07004617 genPackageUpdateIntent(CALLING_PACKAGE_1, USER_0));
4618
4619 waitOnMainThread();
4620
4621 // User-0 should get the notification.
4622 shortcuts = ArgumentCaptor.forClass(List.class);
4623 verify(c0).onShortcutsChanged(
4624 eq(CALLING_PACKAGE_1),
4625 shortcuts.capture(),
4626 eq(HANDLE_USER_0));
4627
4628 // User-10 shouldn't yet get the notification.
4629 verify(c10, times(0)).onShortcutsChanged(
4630 eq(CALLING_PACKAGE_1),
4631 any(List.class),
4632 any(UserHandle.class));
4633 assertShortcutIds(shortcuts.getValue(), "s1", "s2", "s3", "s4");
4634 assertEquals(START_TIME,
4635 findShortcut(shortcuts.getValue(), "s1").getLastChangedTimestamp());
4636 assertEquals(START_TIME + 100,
4637 findShortcut(shortcuts.getValue(), "s2").getLastChangedTimestamp());
4638 assertEquals(START_TIME + 100,
4639 findShortcut(shortcuts.getValue(), "s3").getLastChangedTimestamp());
4640 assertEquals(START_TIME,
4641 findShortcut(shortcuts.getValue(), "s4").getLastChangedTimestamp());
4642
Makoto Onuki248a0ef2016-11-03 15:59:01 -07004643 // Next, send an unlock event on user-10. Now we scan packages on this user and send a
Makoto Onuki39686e82016-04-13 18:03:00 -07004644 // notification to the launcher.
Makoto Onuki51ab2b32016-06-02 11:03:51 -07004645 mInjectedCurrentTimeMillis = START_TIME + 200;
Makoto Onuki39686e82016-04-13 18:03:00 -07004646
Makoto Onukia2241832016-07-06 13:28:37 -07004647 mRunningUsers.put(USER_10, true);
Makoto Onuki085a05c2016-08-19 11:39:29 -07004648 mUnlockedUsers.put(USER_10, true);
Makoto Onuki39686e82016-04-13 18:03:00 -07004649
4650 reset(c0);
4651 reset(c10);
Makoto Onuki085a05c2016-08-19 11:39:29 -07004652 setPackageLastUpdateTime(CALLING_PACKAGE_1, mInjectedCurrentTimeMillis);
Makoto Onuki39686e82016-04-13 18:03:00 -07004653 mService.handleUnlockUser(USER_10);
Makoto Onuki085a05c2016-08-19 11:39:29 -07004654 mService.checkPackageChanges(USER_10);
Makoto Onuki39686e82016-04-13 18:03:00 -07004655
Makoto Onukia2241832016-07-06 13:28:37 -07004656 waitOnMainThread();
4657
Makoto Onuki39686e82016-04-13 18:03:00 -07004658 shortcuts = ArgumentCaptor.forClass(List.class);
4659 verify(c0, times(0)).onShortcutsChanged(
4660 eq(CALLING_PACKAGE_1),
4661 any(List.class),
4662 any(UserHandle.class));
4663
4664 verify(c10).onShortcutsChanged(
4665 eq(CALLING_PACKAGE_1),
4666 shortcuts.capture(),
4667 eq(HANDLE_USER_10));
4668
4669 assertShortcutIds(shortcuts.getValue(), "s1", "s2");
4670 assertEquals(START_TIME + 200,
4671 findShortcut(shortcuts.getValue(), "s1").getLastChangedTimestamp());
4672 assertEquals(START_TIME + 200,
4673 findShortcut(shortcuts.getValue(), "s2").getLastChangedTimestamp());
4674
4675
4676 // Do the same thing for package 2, which doesn't have resource icons.
Makoto Onuki51ab2b32016-06-02 11:03:51 -07004677 mInjectedCurrentTimeMillis = START_TIME + 300;
Makoto Onuki39686e82016-04-13 18:03:00 -07004678
4679 reset(c0);
4680 reset(c10);
4681 updatePackageVersion(CALLING_PACKAGE_2, 10);
4682
4683 // Then send the broadcast, to only user-0.
Makoto Onuki248a0ef2016-11-03 15:59:01 -07004684 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki39686e82016-04-13 18:03:00 -07004685 genPackageUpdateIntent(CALLING_PACKAGE_2, USER_0));
Makoto Onuki39686e82016-04-13 18:03:00 -07004686
4687 waitOnMainThread();
4688
4689 verify(c0, times(0)).onShortcutsChanged(
4690 eq(CALLING_PACKAGE_1),
4691 any(List.class),
4692 any(UserHandle.class));
4693
4694 verify(c10, times(0)).onShortcutsChanged(
4695 eq(CALLING_PACKAGE_1),
4696 any(List.class),
4697 any(UserHandle.class));
4698
4699 // Do the same thing for package 3
Makoto Onuki51ab2b32016-06-02 11:03:51 -07004700 mInjectedCurrentTimeMillis = START_TIME + 400;
Makoto Onuki39686e82016-04-13 18:03:00 -07004701
4702 reset(c0);
4703 reset(c10);
4704 updatePackageVersion(CALLING_PACKAGE_3, 100);
4705
4706 // Then send the broadcast, to only user-0.
Makoto Onuki085a05c2016-08-19 11:39:29 -07004707 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki39686e82016-04-13 18:03:00 -07004708 genPackageUpdateIntent(CALLING_PACKAGE_3, USER_0));
Makoto Onuki085a05c2016-08-19 11:39:29 -07004709 mService.checkPackageChanges(USER_10);
Makoto Onuki39686e82016-04-13 18:03:00 -07004710
4711 waitOnMainThread();
4712
4713 shortcuts = ArgumentCaptor.forClass(List.class);
4714 verify(c0).onShortcutsChanged(
4715 eq(CALLING_PACKAGE_3),
4716 shortcuts.capture(),
4717 eq(HANDLE_USER_0));
4718
4719 // User 10 doesn't have package 3, so no callback.
4720 verify(c10, times(0)).onShortcutsChanged(
4721 eq(CALLING_PACKAGE_3),
4722 any(List.class),
4723 any(UserHandle.class));
4724
4725 assertShortcutIds(shortcuts.getValue(), "s1");
4726 assertEquals(START_TIME + 400,
4727 findShortcut(shortcuts.getValue(), "s1").getLastChangedTimestamp());
4728 }
4729
Makoto Onuki157b1622016-06-02 16:13:10 -07004730 /**
4731 * Test the case where an updated app has resource IDs changed.
4732 */
4733 public void testHandlePackageUpdate_resIdChanged() throws Exception {
4734 final Icon icon1 = Icon.createWithResource(getTestContext(), /* res ID */ 1000);
4735 final Icon icon2 = Icon.createWithResource(getTestContext(), /* res ID */ 1001);
4736
4737 // Set up shortcuts.
4738 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4739 // Note resource strings are not officially supported (they're hidden), but
4740 // should work.
4741
4742 final ShortcutInfo s1 = new ShortcutInfo.Builder(mClientContext)
4743 .setId("s1")
4744 .setActivity(makeComponent(ShortcutActivity.class))
4745 .setIntent(new Intent(Intent.ACTION_VIEW))
4746 .setIcon(icon1)
4747 .setTitleResId(10000)
4748 .setTextResId(10001)
4749 .setDisabledMessageResId(10002)
4750 .build();
4751
4752 final ShortcutInfo s2 = new ShortcutInfo.Builder(mClientContext)
4753 .setId("s2")
4754 .setActivity(makeComponent(ShortcutActivity.class))
4755 .setIntent(new Intent(Intent.ACTION_VIEW))
4756 .setIcon(icon2)
4757 .setTitleResId(20000)
4758 .build();
4759
4760 assertTrue(mManager.setDynamicShortcuts(list(s1, s2)));
4761 });
4762
4763 // Verify.
4764 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4765 final ShortcutInfo s1 = getCallerShortcut("s1");
4766 final ShortcutInfo s2 = getCallerShortcut("s2");
4767
4768 assertEquals(1000, s1.getIconResourceId());
4769 assertEquals(10000, s1.getTitleResId());
4770 assertEquals(10001, s1.getTextResId());
4771 assertEquals(10002, s1.getDisabledMessageResourceId());
4772
4773 assertEquals(1001, s2.getIconResourceId());
4774 assertEquals(20000, s2.getTitleResId());
4775 assertEquals(0, s2.getTextResId());
4776 assertEquals(0, s2.getDisabledMessageResourceId());
4777 });
4778
4779 mService.saveDirtyInfo();
4780 initService();
4781
4782 // Set up the mock resources again, with an "adjustment".
4783 // When the package is updated, the service will fetch the updated res-IDs with res-names,
4784 // and the new IDs will have this offset.
4785 setUpAppResources(10);
4786
4787 // Update the package.
4788 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukia4f89b12017-10-05 10:37:55 -07004789 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki157b1622016-06-02 16:13:10 -07004790 genPackageUpdateIntent(CALLING_PACKAGE_1, USER_0));
4791
4792 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4793 final ShortcutInfo s1 = getCallerShortcut("s1");
4794 final ShortcutInfo s2 = getCallerShortcut("s2");
4795
4796 assertEquals(1010, s1.getIconResourceId());
4797 assertEquals(10010, s1.getTitleResId());
4798 assertEquals(10011, s1.getTextResId());
4799 assertEquals(10012, s1.getDisabledMessageResourceId());
4800
4801 assertEquals(1011, s2.getIconResourceId());
4802 assertEquals(20010, s2.getTitleResId());
4803 assertEquals(0, s2.getTextResId());
4804 assertEquals(0, s2.getDisabledMessageResourceId());
4805 });
4806 }
4807
Makoto Onuki33663282016-08-22 16:19:04 -07004808 public void testHandlePackageUpdate_systemAppUpdate() {
4809
4810 // Package1 is a system app. Package 2 is not a system app, so it's not scanned
4811 // in this test at all.
4812 mSystemPackages.add(CALLING_PACKAGE_1);
4813
4814 // Initial state: no shortcuts.
4815 mService.checkPackageChanges(USER_0);
4816
4817 assertEquals(mInjectedCurrentTimeMillis,
4818 mService.getUserShortcutsLocked(USER_0).getLastAppScanTime());
4819 assertEquals(mInjectedBuildFingerprint,
4820 mService.getUserShortcutsLocked(USER_0).getLastAppScanOsFingerprint());
4821
4822 // They have no shortcuts.
4823 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4824 assertWith(getCallerShortcuts())
4825 .isEmpty();
4826 });
4827
4828 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
4829 assertWith(getCallerShortcuts())
4830 .isEmpty();
4831 });
4832
4833 // Next.
4834 // Update the packages -- now they have 1 manifest shortcut.
4835 // But checkPackageChanges() don't notice it, since their version code / timestamp haven't
4836 // changed.
4837 addManifestShortcutResource(
4838 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
4839 R.xml.shortcut_1);
4840 addManifestShortcutResource(
4841 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
4842 R.xml.shortcut_1);
4843 mInjectedCurrentTimeMillis += 1000;
4844 mService.checkPackageChanges(USER_0);
4845
4846 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4847 assertWith(getCallerShortcuts())
4848 .isEmpty();
4849 });
4850 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
4851 assertWith(getCallerShortcuts())
4852 .isEmpty();
4853 });
4854
4855 // Next.
Makoto Onuki248a0ef2016-11-03 15:59:01 -07004856 // Update the build finger print. All apps will be scanned now.
Makoto Onuki33663282016-08-22 16:19:04 -07004857 mInjectedBuildFingerprint = "update1";
4858 mInjectedCurrentTimeMillis += 1000;
4859 mService.checkPackageChanges(USER_0);
4860
4861 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4862 assertWith(getCallerShortcuts())
4863 .haveIds("ms1");
4864 });
4865 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
4866 assertWith(getCallerShortcuts())
Makoto Onuki248a0ef2016-11-03 15:59:01 -07004867 .haveIds("ms1");
Makoto Onuki33663282016-08-22 16:19:04 -07004868 });
4869
4870 // Next.
4871 // Update manifest shortcuts.
Makoto Onuki33663282016-08-22 16:19:04 -07004872 addManifestShortcutResource(
4873 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
4874 R.xml.shortcut_2);
4875 addManifestShortcutResource(
4876 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
4877 R.xml.shortcut_2);
4878 mInjectedCurrentTimeMillis += 1000;
4879 mService.checkPackageChanges(USER_0);
4880
Makoto Onuki248a0ef2016-11-03 15:59:01 -07004881 // Fingerprint hasn't changed, so there packages weren't scanned.
Makoto Onuki33663282016-08-22 16:19:04 -07004882 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4883 assertWith(getCallerShortcuts())
4884 .haveIds("ms1");
4885 });
4886 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
4887 assertWith(getCallerShortcuts())
Makoto Onuki248a0ef2016-11-03 15:59:01 -07004888 .haveIds("ms1");
Makoto Onuki33663282016-08-22 16:19:04 -07004889 });
4890
Makoto Onuki248a0ef2016-11-03 15:59:01 -07004891 // Update the fingerprint. CALLING_PACKAGE_1's version code hasn't changed, but we scan
4892 // all apps anyway.
Makoto Onuki33663282016-08-22 16:19:04 -07004893 mInjectedBuildFingerprint = "update2";
4894 mInjectedCurrentTimeMillis += 1000;
4895 mService.checkPackageChanges(USER_0);
4896
4897 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4898 assertWith(getCallerShortcuts())
Makoto Onuki33663282016-08-22 16:19:04 -07004899 .haveIds("ms1", "ms2");
4900 });
4901 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
4902 assertWith(getCallerShortcuts())
Makoto Onuki248a0ef2016-11-03 15:59:01 -07004903 .haveIds("ms1", "ms2");
Makoto Onuki33663282016-08-22 16:19:04 -07004904 });
4905
4906 // Make sure getLastAppScanTime / getLastAppScanOsFingerprint are persisted.
4907 initService();
4908 assertEquals(mInjectedCurrentTimeMillis,
4909 mService.getUserShortcutsLocked(USER_0).getLastAppScanTime());
4910 assertEquals(mInjectedBuildFingerprint,
4911 mService.getUserShortcutsLocked(USER_0).getLastAppScanOsFingerprint());
4912 }
4913
Makoto Onukib08790c2016-06-23 14:05:46 -07004914 public void testHandlePackageChanged() {
4915 final ComponentName ACTIVITY1 = new ComponentName(CALLING_PACKAGE_1, "act1");
4916 final ComponentName ACTIVITY2 = new ComponentName(CALLING_PACKAGE_1, "act2");
4917
4918 addManifestShortcutResource(ACTIVITY1, R.xml.shortcut_1);
4919 addManifestShortcutResource(ACTIVITY2, R.xml.shortcut_1_alt);
4920
Makoto Onukia2241832016-07-06 13:28:37 -07004921 mRunningUsers.put(USER_10, true);
4922
Makoto Onukib08790c2016-06-23 14:05:46 -07004923 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukia4f89b12017-10-05 10:37:55 -07004924 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onukib08790c2016-06-23 14:05:46 -07004925 genPackageAddIntent(CALLING_PACKAGE_1, USER_10));
4926
4927 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4928 assertTrue(mManager.addDynamicShortcuts(list(
4929 makeShortcutWithActivity("s1", ACTIVITY1),
4930 makeShortcutWithActivity("s2", ACTIVITY2)
4931 )));
4932 });
4933 runWithCaller(LAUNCHER_1, USER_10, () -> {
4934 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms1-alt", "s2"), HANDLE_USER_10);
4935 });
4936
4937 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4938 assertWith(getCallerShortcuts())
4939 .haveIds("ms1", "ms1-alt", "s1", "s2")
4940 .areAllEnabled()
4941
4942 .selectPinned()
4943 .haveIds("ms1-alt", "s2")
4944
4945 .revertToOriginalList()
4946 .selectByIds("ms1", "s1")
4947 .areAllWithActivity(ACTIVITY1)
4948
4949 .revertToOriginalList()
4950 .selectByIds("ms1-alt", "s2")
4951 .areAllWithActivity(ACTIVITY2)
Makoto Onukia4f89b12017-10-05 10:37:55 -07004952 ;
Makoto Onukib08790c2016-06-23 14:05:46 -07004953 });
4954
4955 // First, no changes.
Makoto Onukia4f89b12017-10-05 10:37:55 -07004956 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onukib08790c2016-06-23 14:05:46 -07004957 genPackageChangedIntent(CALLING_PACKAGE_1, USER_10));
4958
4959 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4960 assertWith(getCallerShortcuts())
4961 .haveIds("ms1", "ms1-alt", "s1", "s2")
4962 .areAllEnabled()
4963
4964 .selectPinned()
4965 .haveIds("ms1-alt", "s2")
4966
4967 .revertToOriginalList()
4968 .selectByIds("ms1", "s1")
4969 .areAllWithActivity(ACTIVITY1)
4970
4971 .revertToOriginalList()
4972 .selectByIds("ms1-alt", "s2")
4973 .areAllWithActivity(ACTIVITY2)
4974 ;
4975 });
4976
4977 // Disable activity 1
4978 mEnabledActivityChecker = (activity, userId) -> !ACTIVITY1.equals(activity);
Makoto Onukia4f89b12017-10-05 10:37:55 -07004979 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onukib08790c2016-06-23 14:05:46 -07004980 genPackageChangedIntent(CALLING_PACKAGE_1, USER_10));
4981
4982 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4983 assertWith(getCallerShortcuts())
4984 .haveIds("ms1-alt", "s2")
4985 .areAllEnabled()
4986
4987 .selectPinned()
4988 .haveIds("ms1-alt", "s2")
4989
4990 .revertToOriginalList()
4991 .selectByIds("ms1-alt", "s2")
4992 .areAllWithActivity(ACTIVITY2)
4993 ;
4994 });
4995
4996 // Re-enable activity 1.
4997 // Manifest shortcuts will be re-published, but dynamic ones are not.
4998 mEnabledActivityChecker = (activity, userId) -> true;
Makoto Onukia4f89b12017-10-05 10:37:55 -07004999 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onukib08790c2016-06-23 14:05:46 -07005000 genPackageChangedIntent(CALLING_PACKAGE_1, USER_10));
5001
5002 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
5003 assertWith(getCallerShortcuts())
5004 .haveIds("ms1", "ms1-alt", "s2")
5005 .areAllEnabled()
5006
5007 .selectPinned()
5008 .haveIds("ms1-alt", "s2")
5009
5010 .revertToOriginalList()
5011 .selectByIds("ms1")
5012 .areAllWithActivity(ACTIVITY1)
5013
5014 .revertToOriginalList()
5015 .selectByIds("ms1-alt", "s2")
5016 .areAllWithActivity(ACTIVITY2)
Makoto Onukia4f89b12017-10-05 10:37:55 -07005017 ;
Makoto Onukib08790c2016-06-23 14:05:46 -07005018 });
5019
5020 // Disable activity 2
5021 // Because "ms1-alt" and "s2" are both pinned, they will remain, but disabled.
5022 mEnabledActivityChecker = (activity, userId) -> !ACTIVITY2.equals(activity);
Makoto Onukia4f89b12017-10-05 10:37:55 -07005023 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onukib08790c2016-06-23 14:05:46 -07005024 genPackageChangedIntent(CALLING_PACKAGE_1, USER_10));
5025
5026 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
5027 assertWith(getCallerShortcuts())
5028 .haveIds("ms1", "ms1-alt", "s2")
5029
5030 .selectDynamic().isEmpty().revertToOriginalList() // no dynamics.
5031
5032 .selectPinned()
5033 .haveIds("ms1-alt", "s2")
5034 .areAllDisabled()
5035
5036 .revertToOriginalList()
5037 .selectByIds("ms1")
5038 .areAllWithActivity(ACTIVITY1)
5039 .areAllEnabled()
5040 ;
5041 });
5042 }
5043
5044 public void testHandlePackageUpdate_activityNoLongerMain() throws Throwable {
5045 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5046 assertTrue(mManager.setDynamicShortcuts(list(
5047 makeShortcutWithActivity("s1a",
5048 new ComponentName(getCallingPackage(), "act1")),
5049 makeShortcutWithActivity("s1b",
5050 new ComponentName(getCallingPackage(), "act1")),
5051 makeShortcutWithActivity("s2a",
5052 new ComponentName(getCallingPackage(), "act2")),
5053 makeShortcutWithActivity("s2b",
5054 new ComponentName(getCallingPackage(), "act2")),
5055 makeShortcutWithActivity("s3a",
5056 new ComponentName(getCallingPackage(), "act3")),
5057 makeShortcutWithActivity("s3b",
5058 new ComponentName(getCallingPackage(), "act3"))
5059 )));
5060 assertWith(getCallerShortcuts())
5061 .haveIds("s1a", "s1b", "s2a", "s2b", "s3a", "s3b")
5062 .areAllDynamic();
5063 });
5064 runWithCaller(LAUNCHER_1, USER_0, () -> {
5065 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
5066 list("s1b", "s2b", "s3b"),
5067 HANDLE_USER_0);
5068 });
5069 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5070 assertWith(getCallerShortcuts())
5071 .haveIds("s1a", "s1b", "s2a", "s2b", "s3a", "s3b")
5072 .areAllDynamic()
5073
5074 .selectByIds("s1b", "s2b", "s3b")
5075 .areAllPinned();
5076 });
5077
5078 // Update the app and act2 and act3 are no longer main.
5079 mMainActivityChecker = (activity, userId) -> {
5080 return activity.getClassName().equals("act1");
5081 };
5082
5083 setCaller(LAUNCHER_1, USER_0);
5084 assertForLauncherCallback(mLauncherApps, () -> {
5085 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukia4f89b12017-10-05 10:37:55 -07005086 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onukib08790c2016-06-23 14:05:46 -07005087 genPackageUpdateIntent(CALLING_PACKAGE_1, USER_0));
5088 }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
5089 // Make sure the launcher gets callbacks.
5090 .haveIds("s1a", "s1b", "s2b", "s3b")
5091 .areAllWithKeyFieldsOnly();
5092
5093 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5094 // s2a and s3a are gone, but s2b and s3b will remain because they're pinned, and
5095 // disabled.
5096 assertWith(getCallerShortcuts())
5097 .haveIds("s1a", "s1b", "s2b", "s3b")
5098
5099 .selectByIds("s1a", "s1b")
5100 .areAllDynamic()
5101 .areAllEnabled()
5102
5103 .revertToOriginalList()
5104 .selectByIds("s2b", "s3b")
5105 .areAllNotDynamic()
5106 .areAllDisabled()
5107 .areAllPinned()
Makoto Onukia4f89b12017-10-05 10:37:55 -07005108 ;
Makoto Onukib08790c2016-06-23 14:05:46 -07005109 });
5110 }
5111
Makoto Onuki51ab2b32016-06-02 11:03:51 -07005112 protected void prepareForBackupTest() {
Makoto Onuki2e210c42016-03-30 08:30:36 -07005113 prepareCrossProfileDataSet();
5114
5115 backupAndRestore();
5116 }
5117
Makoto Onuki2e210c42016-03-30 08:30:36 -07005118 /**
5119 * Make sure the backup data doesn't have the following information:
5120 * - Launchers on other users.
5121 * - Non-backup app information.
5122 *
5123 * But restores all other infomation.
5124 *
5125 * It also omits the following pieces of information, but that's tested in
Makoto Onuki51ab2b32016-06-02 11:03:51 -07005126 * {@link ShortcutManagerTest2#testShortcutInfoSaveAndLoad_forBackup}.
Makoto Onuki2e210c42016-03-30 08:30:36 -07005127 * - Unpinned dynamic shortcuts
5128 * - Bitmaps
5129 */
5130 public void testBackupAndRestore() {
Makoto Onuki50a320e2017-05-31 14:38:42 -07005131
5132 assertFileNotExists("user-0/shortcut_dump/restore-0-start.txt");
5133 assertFileNotExists("user-0/shortcut_dump/restore-1-payload.xml");
5134 assertFileNotExists("user-0/shortcut_dump/restore-2.txt");
5135 assertFileNotExists("user-0/shortcut_dump/restore-3.txt");
5136 assertFileNotExists("user-0/shortcut_dump/restore-4.txt");
5137 assertFileNotExists("user-0/shortcut_dump/restore-5-finish.txt");
5138
Makoto Onuki2e210c42016-03-30 08:30:36 -07005139 prepareForBackupTest();
5140
Makoto Onuki50a320e2017-05-31 14:38:42 -07005141 assertFileExistsWithContent("user-0/shortcut_dump/restore-0-start.txt");
5142 assertFileExistsWithContent("user-0/shortcut_dump/restore-1-payload.xml");
5143 assertFileExistsWithContent("user-0/shortcut_dump/restore-2.txt");
5144 assertFileExistsWithContent("user-0/shortcut_dump/restore-3.txt");
5145 assertFileExistsWithContent("user-0/shortcut_dump/restore-4.txt");
5146 assertFileExistsWithContent("user-0/shortcut_dump/restore-5-finish.txt");
5147
Makoto Onukia4f89b12017-10-05 10:37:55 -07005148 checkBackupAndRestore_success(/*firstRestore=*/ true);
Makoto Onuki2e210c42016-03-30 08:30:36 -07005149 }
5150
5151 public void testBackupAndRestore_backupRestoreTwice() {
5152 prepareForBackupTest();
5153
Makoto Onukia4f89b12017-10-05 10:37:55 -07005154 checkBackupAndRestore_success(/*firstRestore=*/ true);
5155
5156 // Run a backup&restore again. Note the shortcuts that weren't restored in the previous
5157 // restore are disabled, so they won't be restored again.
5158 dumpsysOnLogcat("Before second backup&restore");
Makoto Onuki2e210c42016-03-30 08:30:36 -07005159
5160 backupAndRestore();
5161
Makoto Onukia4f89b12017-10-05 10:37:55 -07005162 dumpsysOnLogcat("After second backup&restore");
Makoto Onuki2e210c42016-03-30 08:30:36 -07005163
Makoto Onukia4f89b12017-10-05 10:37:55 -07005164 checkBackupAndRestore_success(/*firstRestore=*/ false);
Makoto Onuki2e210c42016-03-30 08:30:36 -07005165 }
5166
5167 public void testBackupAndRestore_restoreToNewVersion() {
5168 prepareForBackupTest();
5169
Makoto Onuki2e210c42016-03-30 08:30:36 -07005170 addPackage(CALLING_PACKAGE_1, CALLING_UID_1, 2);
5171 addPackage(LAUNCHER_1, LAUNCHER_UID_1, 5);
5172
Makoto Onukia4f89b12017-10-05 10:37:55 -07005173 checkBackupAndRestore_success(/*firstRestore=*/ true);
Makoto Onuki2e210c42016-03-30 08:30:36 -07005174 }
5175
5176 public void testBackupAndRestore_restoreToSuperSetSignatures() {
5177 prepareForBackupTest();
5178
Makoto Onukiac214972016-04-04 10:19:45 -07005179 // Change package signatures.
Makoto Onuki2e210c42016-03-30 08:30:36 -07005180 addPackage(CALLING_PACKAGE_1, CALLING_UID_1, 1, "sigx", CALLING_PACKAGE_1);
5181 addPackage(LAUNCHER_1, LAUNCHER_UID_1, 4, LAUNCHER_1, "sigy");
5182
Makoto Onukia4f89b12017-10-05 10:37:55 -07005183 checkBackupAndRestore_success(/*firstRestore=*/ true);
Makoto Onuki2e210c42016-03-30 08:30:36 -07005184 }
5185
Makoto Onukia4f89b12017-10-05 10:37:55 -07005186 protected void checkBackupAndRestore_success(boolean firstRestore) {
Makoto Onuki2e210c42016-03-30 08:30:36 -07005187 // Make sure non-system user is not restored.
5188 final ShortcutUser userP0 = mService.getUserShortcutsLocked(USER_P0);
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07005189 assertEquals(0, userP0.getAllPackagesForTest().size());
5190 assertEquals(0, userP0.getAllLaunchersForTest().size());
Makoto Onuki2e210c42016-03-30 08:30:36 -07005191
5192 // Make sure only "allowBackup" apps are restored, and are shadow.
5193 final ShortcutUser user0 = mService.getUserShortcutsLocked(USER_0);
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07005194 assertExistsAndShadow(user0.getAllPackagesForTest().get(CALLING_PACKAGE_1));
5195 assertExistsAndShadow(user0.getAllPackagesForTest().get(CALLING_PACKAGE_2));
Makoto Onukia4f89b12017-10-05 10:37:55 -07005196
5197 assertExistsAndShadow(user0.getAllPackagesForTest().get(CALLING_PACKAGE_3));
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005198 assertExistsAndShadow(user0.getAllLaunchersForTest().get(
5199 PackageWithUser.of(USER_0, LAUNCHER_1)));
5200 assertExistsAndShadow(user0.getAllLaunchersForTest().get(
5201 PackageWithUser.of(USER_0, LAUNCHER_2)));
Makoto Onuki2e210c42016-03-30 08:30:36 -07005202
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07005203 assertNull(user0.getAllLaunchersForTest().get(PackageWithUser.of(USER_0, LAUNCHER_3)));
5204 assertNull(user0.getAllLaunchersForTest().get(PackageWithUser.of(USER_P0, LAUNCHER_1)));
Makoto Onuki2e210c42016-03-30 08:30:36 -07005205
Michal Karpinski528c3e52018-02-07 17:47:10 +00005206 doReturn(true).when(mMockPackageManagerInternal).isDataRestoreSafe(any(byte[].class),
5207 anyString());
5208
Makoto Onuki2e210c42016-03-30 08:30:36 -07005209 installPackage(USER_0, CALLING_PACKAGE_1);
5210 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005211 assertWith(getCallerVisibleShortcuts())
5212 .selectDynamic()
5213 .isEmpty()
5214
5215 .revertToOriginalList()
5216 .selectPinned()
Makoto Onukia4f89b12017-10-05 10:37:55 -07005217 .haveIds("s1", "s2")
5218 .areAllEnabled();
Makoto Onuki2e210c42016-03-30 08:30:36 -07005219 });
5220
5221 installPackage(USER_0, LAUNCHER_1);
5222 runWithCaller(LAUNCHER_1, USER_0, () -> {
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005223 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5224 .areAllPinned()
Makoto Onukia4f89b12017-10-05 10:37:55 -07005225 .haveIds("s1")
5226 .areAllEnabled();
Makoto Onuki2e210c42016-03-30 08:30:36 -07005227
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005228 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5229 .isEmpty();
5230
5231 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5232 .isEmpty();
5233
5234 assertWith(mLauncherApps.getShortcuts(QUERY_ALL, HANDLE_USER_P0))
5235 .isEmpty();
Makoto Onuki2e210c42016-03-30 08:30:36 -07005236 });
5237
5238 installPackage(USER_0, CALLING_PACKAGE_2);
5239 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005240 assertWith(getCallerVisibleShortcuts())
5241 .selectDynamic()
5242 .isEmpty()
5243
5244 .revertToOriginalList()
5245 .selectPinned()
Makoto Onukia4f89b12017-10-05 10:37:55 -07005246 .haveIds("s1", "s2", "s3")
5247 .areAllEnabled();
Makoto Onuki2e210c42016-03-30 08:30:36 -07005248 });
5249
5250 runWithCaller(LAUNCHER_1, USER_0, () -> {
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005251 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5252 .areAllPinned()
Makoto Onukia4f89b12017-10-05 10:37:55 -07005253 .haveIds("s1")
5254 .areAllEnabled();
Makoto Onuki2e210c42016-03-30 08:30:36 -07005255
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005256 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5257 .areAllPinned()
Makoto Onukia4f89b12017-10-05 10:37:55 -07005258 .haveIds("s1", "s2")
5259 .areAllEnabled();
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005260
5261 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5262 .isEmpty();
5263
5264 assertWith(mLauncherApps.getShortcuts(QUERY_ALL, HANDLE_USER_P0))
5265 .isEmpty();
Makoto Onuki2e210c42016-03-30 08:30:36 -07005266 });
5267
5268 // 3 shouldn't be backed up, so no pinned shortcuts.
5269 installPackage(USER_0, CALLING_PACKAGE_3);
5270 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005271 assertWith(getCallerVisibleShortcuts())
5272 .isEmpty();
Makoto Onuki2e210c42016-03-30 08:30:36 -07005273 });
5274
5275 // Launcher on a different profile shouldn't be restored.
5276 runWithCaller(LAUNCHER_1, USER_P0, () -> {
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005277 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5278 .isEmpty();
5279 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5280 .isEmpty();
5281 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5282 .isEmpty();
Makoto Onuki2e210c42016-03-30 08:30:36 -07005283 });
5284
5285 // Package on a different profile, no restore.
5286 installPackage(USER_P0, CALLING_PACKAGE_1);
5287 runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005288 assertWith(getCallerVisibleShortcuts())
5289 .isEmpty();
Makoto Onuki2e210c42016-03-30 08:30:36 -07005290 });
5291
5292 // Restore launcher 2 on user 0.
5293 installPackage(USER_0, LAUNCHER_2);
5294 runWithCaller(LAUNCHER_2, USER_0, () -> {
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005295 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5296 .areAllPinned()
Makoto Onukia4f89b12017-10-05 10:37:55 -07005297 .haveIds("s2")
5298 .areAllEnabled();
Makoto Onuki2e210c42016-03-30 08:30:36 -07005299
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005300 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5301 .areAllPinned()
Makoto Onukia4f89b12017-10-05 10:37:55 -07005302 .haveIds("s2", "s3")
5303 .areAllEnabled();
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005304
Makoto Onukia4f89b12017-10-05 10:37:55 -07005305 if (firstRestore) {
5306 assertWith(
5307 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5308 .haveIds("s2", "s3", "s4")
5309 .areAllDisabled()
5310 .areAllPinned()
5311 .areAllNotDynamic()
5312 .areAllWithDisabledReason(
5313 ShortcutInfo.DISABLED_REASON_BACKUP_NOT_SUPPORTED);
5314 } else {
5315 assertWith(
5316 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5317 .isEmpty();
5318 }
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005319
5320 assertWith(mLauncherApps.getShortcuts(QUERY_ALL, HANDLE_USER_P0))
5321 .isEmpty();
Makoto Onuki2e210c42016-03-30 08:30:36 -07005322 });
5323
5324
5325 // Restoration of launcher2 shouldn't affect other packages; so do the same checks and
5326 // make sure they still have the same result.
5327 installPackage(USER_0, CALLING_PACKAGE_1);
5328 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005329 assertWith(getCallerVisibleShortcuts())
5330 .areAllPinned()
5331 .haveIds("s1", "s2");
Makoto Onuki2e210c42016-03-30 08:30:36 -07005332 });
5333
5334 installPackage(USER_0, LAUNCHER_1);
5335 runWithCaller(LAUNCHER_1, USER_0, () -> {
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005336 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5337 .areAllPinned()
Makoto Onukia4f89b12017-10-05 10:37:55 -07005338 .haveIds("s1")
5339 .areAllEnabled();
Makoto Onuki2e210c42016-03-30 08:30:36 -07005340
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005341 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5342 .areAllPinned()
Makoto Onukia4f89b12017-10-05 10:37:55 -07005343 .haveIds("s1", "s2")
5344 .areAllEnabled();
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005345
Makoto Onukia4f89b12017-10-05 10:37:55 -07005346 if (firstRestore) {
5347 assertWith(
5348 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5349 .haveIds("s1", "s2", "s3")
5350 .areAllDisabled()
5351 .areAllPinned()
5352 .areAllNotDynamic()
5353 .areAllWithDisabledReason(ShortcutInfo.DISABLED_REASON_BACKUP_NOT_SUPPORTED);
5354 } else {
5355 assertWith(
5356 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5357 .isEmpty();
5358 }
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005359
5360 assertWith(mLauncherApps.getShortcuts(QUERY_ALL, HANDLE_USER_P0))
5361 .isEmpty();
Makoto Onuki2e210c42016-03-30 08:30:36 -07005362 });
5363
5364 installPackage(USER_0, CALLING_PACKAGE_2);
5365 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005366 assertWith(getCallerVisibleShortcuts())
5367 .areAllPinned()
Makoto Onukia4f89b12017-10-05 10:37:55 -07005368 .haveIds("s1", "s2", "s3")
5369 .areAllEnabled();
Makoto Onuki2e210c42016-03-30 08:30:36 -07005370 });
5371 }
5372
5373 public void testBackupAndRestore_publisherLowerVersion() {
5374 prepareForBackupTest();
5375
Makoto Onuki2e210c42016-03-30 08:30:36 -07005376 addPackage(CALLING_PACKAGE_1, CALLING_UID_1, 0); // Lower version
5377
Makoto Onukia4f89b12017-10-05 10:37:55 -07005378 checkBackupAndRestore_publisherNotRestored(ShortcutInfo.DISABLED_REASON_VERSION_LOWER);
Makoto Onuki2e210c42016-03-30 08:30:36 -07005379 }
5380
5381 public void testBackupAndRestore_publisherWrongSignature() {
5382 prepareForBackupTest();
5383
Makoto Onuki2e210c42016-03-30 08:30:36 -07005384 addPackage(CALLING_PACKAGE_1, CALLING_UID_1, 10, "sigx"); // different signature
5385
Makoto Onukia4f89b12017-10-05 10:37:55 -07005386 checkBackupAndRestore_publisherNotRestored(ShortcutInfo.DISABLED_REASON_SIGNATURE_MISMATCH);
Makoto Onuki2e210c42016-03-30 08:30:36 -07005387 }
5388
5389 public void testBackupAndRestore_publisherNoLongerBackupTarget() {
5390 prepareForBackupTest();
5391
Makoto Onuki2e210c42016-03-30 08:30:36 -07005392 updatePackageInfo(CALLING_PACKAGE_1,
5393 pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP);
5394
Makoto Onukia4f89b12017-10-05 10:37:55 -07005395 checkBackupAndRestore_publisherNotRestored(
5396 ShortcutInfo.DISABLED_REASON_BACKUP_NOT_SUPPORTED);
Makoto Onuki2e210c42016-03-30 08:30:36 -07005397 }
5398
Makoto Onukia4f89b12017-10-05 10:37:55 -07005399 protected void checkBackupAndRestore_publisherNotRestored(
5400 int package1DisabledReason) {
Michal Karpinski528c3e52018-02-07 17:47:10 +00005401 doReturn(package1DisabledReason != ShortcutInfo.DISABLED_REASON_SIGNATURE_MISMATCH).when(
5402 mMockPackageManagerInternal).isDataRestoreSafe(any(byte[].class),
5403 eq(CALLING_PACKAGE_1));
5404
Makoto Onuki2e210c42016-03-30 08:30:36 -07005405 installPackage(USER_0, CALLING_PACKAGE_1);
5406 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5407 assertEquals(0, mManager.getDynamicShortcuts().size());
5408 assertEquals(0, mManager.getPinnedShortcuts().size());
5409 });
Makoto Onukifac592f2016-11-21 13:41:32 -08005410 assertFalse(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, USER_0)
5411 .getPackageInfo().isShadow());
5412
Michal Karpinski528c3e52018-02-07 17:47:10 +00005413 doReturn(true).when(mMockPackageManagerInternal).isDataRestoreSafe(
5414 any(byte[].class), anyString());
Makoto Onuki2e210c42016-03-30 08:30:36 -07005415
5416 installPackage(USER_0, CALLING_PACKAGE_2);
5417 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5418 assertEquals(0, mManager.getDynamicShortcuts().size());
5419 assertShortcutIds(assertAllPinned(
5420 mManager.getPinnedShortcuts()),
5421 "s1", "s2", "s3");
5422 });
Makoto Onukifac592f2016-11-21 13:41:32 -08005423 assertFalse(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, USER_0)
5424 .getPackageInfo().isShadow());
Makoto Onuki2e210c42016-03-30 08:30:36 -07005425
5426 installPackage(USER_0, LAUNCHER_1);
5427 runWithCaller(LAUNCHER_1, USER_0, () -> {
Makoto Onukia4f89b12017-10-05 10:37:55 -07005428 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5429 .haveIds("s1")
5430 .areAllPinned()
5431 .areAllDisabled()
Makoto Onukib1588c02017-10-12 15:11:45 -07005432 .areAllWithDisabledReason(package1DisabledReason)
5433 .forAllShortcuts(si -> {
5434 switch (package1DisabledReason) {
5435 case ShortcutInfo.DISABLED_REASON_VERSION_LOWER:
Makoto Onuki7eb8b952018-02-01 11:31:03 -08005436 assertEquals("App version downgraded, or isn’t compatible"
5437 + " with this shortcut",
Makoto Onukib1588c02017-10-12 15:11:45 -07005438 si.getDisabledMessage());
5439 break;
5440 case ShortcutInfo.DISABLED_REASON_SIGNATURE_MISMATCH:
5441 assertEquals(
5442 "Couldn\u2019t restore shortcut because of app"
5443 + " signature mismatch",
5444 si.getDisabledMessage());
5445 break;
5446 case ShortcutInfo.DISABLED_REASON_BACKUP_NOT_SUPPORTED:
5447 assertEquals(
5448 "Couldn\u2019t restore shortcut because app"
5449 + " doesn\u2019t support backup and restore",
5450 si.getDisabledMessage());
5451 break;
5452 default:
5453 fail("Unhandled disabled reason: " + package1DisabledReason);
5454 }
5455 });
Makoto Onukia4f89b12017-10-05 10:37:55 -07005456 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5457 .haveIds("s1", "s2")
5458 .areAllPinned()
5459 .areAllEnabled();
5460 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5461 .isEmpty();
Makoto Onuki2e210c42016-03-30 08:30:36 -07005462 });
5463 installPackage(USER_0, LAUNCHER_2);
5464 runWithCaller(LAUNCHER_2, USER_0, () -> {
Makoto Onukia4f89b12017-10-05 10:37:55 -07005465 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5466 .haveIds("s2")
5467 .areAllPinned()
5468 .areAllDisabled()
5469 .areAllWithDisabledReason(package1DisabledReason);
5470 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5471 .haveIds("s2", "s3")
5472 .areAllPinned()
5473 .areAllEnabled();
5474 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5475 .isEmpty();
Makoto Onuki2e210c42016-03-30 08:30:36 -07005476 });
5477
5478 installPackage(USER_0, CALLING_PACKAGE_3);
5479 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
5480 assertEquals(0, mManager.getDynamicShortcuts().size());
5481 assertEquals(0, mManager.getPinnedShortcuts().size());
5482 });
5483
5484 runWithCaller(LAUNCHER_1, USER_0, () -> {
Makoto Onukia4f89b12017-10-05 10:37:55 -07005485 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5486 .haveIds("s1")
5487 .areAllPinned()
5488 .areAllDisabled()
5489 .areAllWithDisabledReason(package1DisabledReason);
5490 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5491 .haveIds("s1", "s2")
5492 .areAllPinned()
5493 .areAllEnabled();
5494 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5495 .haveIds("s1", "s2", "s3")
5496 .areAllPinned()
5497 .areAllDisabled()
5498 .areAllWithDisabledReason(ShortcutInfo.DISABLED_REASON_BACKUP_NOT_SUPPORTED);
Makoto Onuki2e210c42016-03-30 08:30:36 -07005499 });
5500 runWithCaller(LAUNCHER_2, USER_0, () -> {
Makoto Onukia4f89b12017-10-05 10:37:55 -07005501 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5502 .haveIds("s2")
5503 .areAllPinned()
5504 .areAllDisabled()
5505 .areAllWithDisabledReason(package1DisabledReason);
5506 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5507 .haveIds("s2", "s3")
5508 .areAllPinned()
5509 .areAllEnabled();
5510 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5511 .haveIds("s2", "s3", "s4")
5512 .areAllPinned()
5513 .areAllDisabled()
5514 .areAllWithDisabledReason(ShortcutInfo.DISABLED_REASON_BACKUP_NOT_SUPPORTED);
Makoto Onuki2e210c42016-03-30 08:30:36 -07005515 });
5516 }
5517
5518 public void testBackupAndRestore_launcherLowerVersion() {
5519 prepareForBackupTest();
5520
Makoto Onuki2e210c42016-03-30 08:30:36 -07005521 addPackage(LAUNCHER_1, LAUNCHER_UID_1, 0); // Lower version
5522
Makoto Onukia4f89b12017-10-05 10:37:55 -07005523 // Note, we restore pinned shortcuts even if the launcher is of a lower version.
5524 checkBackupAndRestore_success(/*firstRestore=*/ true);
Makoto Onuki2e210c42016-03-30 08:30:36 -07005525 }
5526
5527 public void testBackupAndRestore_launcherWrongSignature() {
5528 prepareForBackupTest();
5529
Makoto Onuki2e210c42016-03-30 08:30:36 -07005530 addPackage(LAUNCHER_1, LAUNCHER_UID_1, 10, "sigx"); // different signature
5531
Michal Karpinski528c3e52018-02-07 17:47:10 +00005532 checkBackupAndRestore_launcherNotRestored(true);
Makoto Onuki2e210c42016-03-30 08:30:36 -07005533 }
5534
5535 public void testBackupAndRestore_launcherNoLongerBackupTarget() {
5536 prepareForBackupTest();
5537
Makoto Onuki2e210c42016-03-30 08:30:36 -07005538 updatePackageInfo(LAUNCHER_1,
5539 pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP);
5540
Michal Karpinski528c3e52018-02-07 17:47:10 +00005541 checkBackupAndRestore_launcherNotRestored(false);
Makoto Onuki2e210c42016-03-30 08:30:36 -07005542 }
5543
Michal Karpinski528c3e52018-02-07 17:47:10 +00005544 protected void checkBackupAndRestore_launcherNotRestored(boolean differentSignatures) {
5545 doReturn(true).when(mMockPackageManagerInternal).isDataRestoreSafe(
5546 any(byte[].class), anyString());
5547
Makoto Onuki2e210c42016-03-30 08:30:36 -07005548 installPackage(USER_0, CALLING_PACKAGE_1);
5549 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5550 assertEquals(0, mManager.getDynamicShortcuts().size());
5551
5552 // s1 was pinned by launcher 1, which is not restored, yet, so we still see "s1" here.
5553 assertShortcutIds(assertAllPinned(
5554 mManager.getPinnedShortcuts()),
5555 "s1", "s2");
5556 });
5557
5558 installPackage(USER_0, CALLING_PACKAGE_2);
5559 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5560 assertEquals(0, mManager.getDynamicShortcuts().size());
5561 assertShortcutIds(assertAllPinned(
5562 mManager.getPinnedShortcuts()),
5563 "s1", "s2", "s3");
5564 });
5565
Michal Karpinski528c3e52018-02-07 17:47:10 +00005566 doReturn(!differentSignatures).when(mMockPackageManagerInternal).isDataRestoreSafe(
5567 any(byte[].class), eq(LAUNCHER_1));
5568
Makoto Onuki2e210c42016-03-30 08:30:36 -07005569 // Now we try to restore launcher 1. Then we realize it's not restorable, so L1 has no pinned
5570 // shortcuts.
5571 installPackage(USER_0, LAUNCHER_1);
5572 runWithCaller(LAUNCHER_1, USER_0, () -> {
5573 assertShortcutIds(assertAllPinned(
5574 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5575 /* empty */);
5576 assertShortcutIds(assertAllPinned(
5577 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5578 /* empty */);
5579 assertShortcutIds(assertAllPinned(
5580 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5581 /* empty */);
5582 });
Makoto Onukifac592f2016-11-21 13:41:32 -08005583 assertFalse(mService.getLauncherShortcutForTest(LAUNCHER_1, USER_0)
5584 .getPackageInfo().isShadow());
Makoto Onuki2e210c42016-03-30 08:30:36 -07005585
5586 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5587 assertEquals(0, mManager.getDynamicShortcuts().size());
5588
5589 // Now CALLING_PACKAGE_1 realizes "s1" is no longer pinned.
5590 assertShortcutIds(assertAllPinned(
5591 mManager.getPinnedShortcuts()),
5592 "s2");
5593 });
5594
Michal Karpinski528c3e52018-02-07 17:47:10 +00005595 doReturn(true).when(mMockPackageManagerInternal).isDataRestoreSafe(
5596 any(byte[].class), anyString());
5597
Makoto Onuki2e210c42016-03-30 08:30:36 -07005598 installPackage(USER_0, LAUNCHER_2);
5599 runWithCaller(LAUNCHER_2, USER_0, () -> {
5600 assertShortcutIds(assertAllPinned(
5601 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0)),
5602 "s2");
5603 assertShortcutIds(assertAllPinned(
5604 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0)),
5605 "s2", "s3");
5606 assertShortcutIds(assertAllPinned(
5607 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5608 /* empty */);
5609 });
Makoto Onukifac592f2016-11-21 13:41:32 -08005610 assertFalse(mService.getLauncherShortcutForTest(LAUNCHER_2, USER_0)
5611 .getPackageInfo().isShadow());
Makoto Onuki2e210c42016-03-30 08:30:36 -07005612
5613 installPackage(USER_0, CALLING_PACKAGE_3);
5614 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
5615 assertEquals(0, mManager.getDynamicShortcuts().size());
5616 assertEquals(0, mManager.getPinnedShortcuts().size());
5617 });
5618
5619 runWithCaller(LAUNCHER_1, USER_0, () -> {
5620 assertShortcutIds(assertAllPinned(
5621 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5622 /* empty */);
5623 assertShortcutIds(assertAllPinned(
5624 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5625 /* empty */);
5626 assertShortcutIds(assertAllPinned(
5627 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5628 /* empty */);
5629 });
5630 runWithCaller(LAUNCHER_2, USER_0, () -> {
5631 assertShortcutIds(assertAllPinned(
5632 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0)),
5633 "s2");
5634 assertShortcutIds(assertAllPinned(
5635 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0)),
5636 "s2", "s3");
Makoto Onuki2e210c42016-03-30 08:30:36 -07005637 });
5638 }
5639
5640 public void testBackupAndRestore_launcherAndPackageNoLongerBackupTarget() {
5641 prepareForBackupTest();
5642
Makoto Onuki2e210c42016-03-30 08:30:36 -07005643 updatePackageInfo(CALLING_PACKAGE_1,
5644 pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP);
5645
5646 updatePackageInfo(LAUNCHER_1,
5647 pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP);
5648
5649 checkBackupAndRestore_publisherAndLauncherNotRestored();
5650 }
5651
Makoto Onuki51ab2b32016-06-02 11:03:51 -07005652 protected void checkBackupAndRestore_publisherAndLauncherNotRestored() {
Michal Karpinski528c3e52018-02-07 17:47:10 +00005653 doReturn(true).when(mMockPackageManagerInternal).isDataRestoreSafe(any(byte[].class),
5654 anyString());
Makoto Onuki2e210c42016-03-30 08:30:36 -07005655 installPackage(USER_0, CALLING_PACKAGE_1);
5656 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5657 assertEquals(0, mManager.getDynamicShortcuts().size());
5658 assertEquals(0, mManager.getPinnedShortcuts().size());
5659 });
5660
5661 installPackage(USER_0, CALLING_PACKAGE_2);
5662 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5663 assertEquals(0, mManager.getDynamicShortcuts().size());
5664 assertShortcutIds(assertAllPinned(
5665 mManager.getPinnedShortcuts()),
5666 "s1", "s2", "s3");
5667 });
5668
5669 installPackage(USER_0, LAUNCHER_1);
5670 runWithCaller(LAUNCHER_1, USER_0, () -> {
5671 assertShortcutIds(assertAllPinned(
5672 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5673 /* empty */);
5674 assertShortcutIds(assertAllPinned(
5675 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5676 /* empty */);
5677 assertShortcutIds(assertAllPinned(
5678 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5679 /* empty */);
5680 });
5681 installPackage(USER_0, LAUNCHER_2);
5682 runWithCaller(LAUNCHER_2, USER_0, () -> {
Makoto Onukia4f89b12017-10-05 10:37:55 -07005683 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5684 .areAllPinned()
5685 .haveIds("s2")
5686 .areAllDisabled();
5687 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5688 .areAllPinned()
5689 .haveIds("s2", "s3");
5690 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5691 .isEmpty();
Makoto Onuki2e210c42016-03-30 08:30:36 -07005692 });
5693
5694 // Because launcher 1 wasn't restored, "s1" is no longer pinned.
5695 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5696 assertEquals(0, mManager.getDynamicShortcuts().size());
5697 assertShortcutIds(assertAllPinned(
5698 mManager.getPinnedShortcuts()),
5699 "s2", "s3");
5700 });
5701
5702 installPackage(USER_0, CALLING_PACKAGE_3);
5703 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
5704 assertEquals(0, mManager.getDynamicShortcuts().size());
5705 assertEquals(0, mManager.getPinnedShortcuts().size());
5706 });
5707
5708 runWithCaller(LAUNCHER_1, USER_0, () -> {
5709 assertShortcutIds(assertAllPinned(
5710 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5711 /* empty */);
5712 assertShortcutIds(assertAllPinned(
5713 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5714 /* empty */);
5715 assertShortcutIds(assertAllPinned(
5716 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5717 /* empty */);
5718 });
5719 runWithCaller(LAUNCHER_2, USER_0, () -> {
Makoto Onukia4f89b12017-10-05 10:37:55 -07005720 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5721 .areAllPinned()
5722 .haveIds("s2")
5723 .areAllDisabled();
5724 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5725 .areAllPinned()
5726 .haveIds("s2", "s3");
5727 assertWith(
Makoto Onuki2e210c42016-03-30 08:30:36 -07005728 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
Makoto Onukia4f89b12017-10-05 10:37:55 -07005729 .haveIds("s2", "s3", "s4")
5730 .areAllDisabled()
5731 .areAllPinned()
5732 .areAllNotDynamic()
5733 .areAllWithDisabledReason(
5734 ShortcutInfo.DISABLED_REASON_BACKUP_NOT_SUPPORTED);
Makoto Onuki2e210c42016-03-30 08:30:36 -07005735 });
5736 }
5737
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005738 public void testBackupAndRestore_disabled() {
5739 prepareCrossProfileDataSet();
5740
5741 // Before doing backup & restore, disable s1.
5742 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5743 mManager.disableShortcuts(list("s1"));
5744 });
5745
5746 backupAndRestore();
5747
5748 // Below is copied from checkBackupAndRestore_success.
5749
5750 // Make sure non-system user is not restored.
5751 final ShortcutUser userP0 = mService.getUserShortcutsLocked(USER_P0);
5752 assertEquals(0, userP0.getAllPackagesForTest().size());
5753 assertEquals(0, userP0.getAllLaunchersForTest().size());
5754
5755 // Make sure only "allowBackup" apps are restored, and are shadow.
5756 final ShortcutUser user0 = mService.getUserShortcutsLocked(USER_0);
5757 assertExistsAndShadow(user0.getAllPackagesForTest().get(CALLING_PACKAGE_1));
5758 assertExistsAndShadow(user0.getAllPackagesForTest().get(CALLING_PACKAGE_2));
Makoto Onukia4f89b12017-10-05 10:37:55 -07005759 assertExistsAndShadow(user0.getAllPackagesForTest().get(CALLING_PACKAGE_3));
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005760 assertExistsAndShadow(user0.getAllLaunchersForTest().get(
5761 PackageWithUser.of(USER_0, LAUNCHER_1)));
5762 assertExistsAndShadow(user0.getAllLaunchersForTest().get(
5763 PackageWithUser.of(USER_0, LAUNCHER_2)));
5764
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005765 assertNull(user0.getAllLaunchersForTest().get(PackageWithUser.of(USER_0, LAUNCHER_3)));
5766 assertNull(user0.getAllLaunchersForTest().get(PackageWithUser.of(USER_P0, LAUNCHER_1)));
5767
Michal Karpinski528c3e52018-02-07 17:47:10 +00005768 doReturn(true).when(mMockPackageManagerInternal).isDataRestoreSafe(any(byte[].class),
5769 anyString());
5770
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005771 installPackage(USER_0, CALLING_PACKAGE_1);
5772 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5773 assertWith(getCallerVisibleShortcuts())
5774 .areAllEnabled() // disabled shortcuts shouldn't be restored.
5775
5776 .selectDynamic()
5777 .isEmpty()
5778
5779 .revertToOriginalList()
5780 .selectPinned()
5781 // s1 is not restored.
5782 .haveIds("s2");
5783 });
5784
5785 installPackage(USER_0, LAUNCHER_1);
5786 runWithCaller(LAUNCHER_1, USER_0, () -> {
5787 // Note, s1 was pinned by launcher 1, but was disabled, so isn't restored.
5788 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5789 .isEmpty();
5790
5791 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5792 .isEmpty();
5793
5794 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5795 .isEmpty();
5796
5797 assertWith(mLauncherApps.getShortcuts(QUERY_ALL, HANDLE_USER_P0))
5798 .isEmpty();
5799 });
5800 }
5801
5802
Makoto Onuki377b7972016-08-09 14:43:55 -07005803 public void testBackupAndRestore_manifestRePublished() {
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005804 // Publish two manifest shortcuts.
5805 addManifestShortcutResource(
5806 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
5807 R.xml.shortcut_2);
5808 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07005809 mService.mPackageMonitor.onReceive(mServiceContext,
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005810 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
5811
Makoto Onuki377b7972016-08-09 14:43:55 -07005812 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5813 assertTrue(mManager.setDynamicShortcuts(list(
5814 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
5815 });
5816
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005817 // Pin from launcher 1.
5818 runWithCaller(LAUNCHER_1, USER_0, () -> {
Makoto Onuki377b7972016-08-09 14:43:55 -07005819 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
5820 list("ms1", "ms2", "s1", "s2"), HANDLE_USER_0);
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005821 });
5822
5823 // Update and now ms2 is gone -> disabled.
5824 addManifestShortcutResource(
5825 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
5826 R.xml.shortcut_1);
5827 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07005828 mService.mPackageMonitor.onReceive(mServiceContext,
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005829 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
5830
5831 // Make sure the manifest shortcuts have been published.
5832 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5833 assertWith(getCallerShortcuts())
Makoto Onuki377b7972016-08-09 14:43:55 -07005834 .selectManifest()
5835 .haveIds("ms1")
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005836
Makoto Onuki377b7972016-08-09 14:43:55 -07005837 .revertToOriginalList()
5838 .selectDynamic()
5839 .haveIds("s1", "s2", "s3")
5840
5841 .revertToOriginalList()
5842 .selectPinned()
5843 .haveIds("ms1", "ms2", "s1", "s2")
5844
5845 .revertToOriginalList()
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005846 .selectByIds("ms1")
5847 .areAllManifest()
5848 .areAllEnabled()
5849
5850 .revertToOriginalList()
5851 .selectByIds("ms2")
5852 .areAllNotManifest()
5853 .areAllDisabled();
5854 });
5855
Michal Karpinski528c3e52018-02-07 17:47:10 +00005856 doReturn(true).when(mMockPackageManagerInternal).isDataRestoreSafe(
5857 any(byte[].class), anyString());
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005858 backupAndRestore();
Makoto Onuki377b7972016-08-09 14:43:55 -07005859
5860 // When re-installing the app, the manifest shortcut should be re-published.
5861 mService.mPackageMonitor.onReceive(mServiceContext,
5862 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
5863 mService.mPackageMonitor.onReceive(mServiceContext,
5864 genPackageAddIntent(LAUNCHER_1, USER_0));
5865
5866 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5867 assertWith(getCallerVisibleShortcuts())
5868 .selectPinned()
5869 // ms2 was disabled, so not restored.
5870 .haveIds("ms1", "s1", "s2")
5871 .areAllEnabled()
5872
5873 .revertToOriginalList()
5874 .selectByIds("ms1")
5875 .areAllManifest()
5876
5877 .revertToOriginalList()
5878 .selectByIds("s1", "s2")
5879 .areAllNotDynamic()
Makoto Onukia4f89b12017-10-05 10:37:55 -07005880 ;
Makoto Onuki377b7972016-08-09 14:43:55 -07005881 });
5882 }
5883
5884 /**
5885 * It's the case with preintalled apps -- when applyRestore() is called, the system
5886 * apps are already installed, so manifest shortcuts need to be re-published.
Makoto Onukifc4cf2d2016-08-24 11:10:26 -07005887 *
5888 * Also, when a restore target app is already installed, and
5889 * - if it has allowBackup=true, we'll restore normally, so all existing shortcuts will be
5890 * replaced. (but manifest shortcuts will be re-published anyway.) We log a warning on
5891 * logcat.
5892 * - if it has allowBackup=false, we don't touch any of the existing shortcuts.
Makoto Onuki377b7972016-08-09 14:43:55 -07005893 */
5894 public void testBackupAndRestore_appAlreadyInstalledWhenRestored() {
5895 // Pre-backup. Same as testBackupAndRestore_manifestRePublished().
5896
5897 // Publish two manifest shortcuts.
5898 addManifestShortcutResource(
5899 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
5900 R.xml.shortcut_2);
5901 updatePackageVersion(CALLING_PACKAGE_1, 1);
5902 mService.mPackageMonitor.onReceive(mServiceContext,
5903 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
5904
5905 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5906 assertTrue(mManager.setDynamicShortcuts(list(
5907 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
5908 });
5909
5910 // Pin from launcher 1.
5911 runWithCaller(LAUNCHER_1, USER_0, () -> {
5912 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
5913 list("ms1", "ms2", "s1", "s2"), HANDLE_USER_0);
5914 });
5915
5916 // Update and now ms2 is gone -> disabled.
5917 addManifestShortcutResource(
5918 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
5919 R.xml.shortcut_1);
5920 updatePackageVersion(CALLING_PACKAGE_1, 1);
5921 mService.mPackageMonitor.onReceive(mServiceContext,
5922 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
5923
Makoto Onukifc4cf2d2016-08-24 11:10:26 -07005924 // Set up shortcuts for package 3, which won't be backed up / restored.
5925 addManifestShortcutResource(
5926 new ComponentName(CALLING_PACKAGE_3, ShortcutActivity.class.getName()),
5927 R.xml.shortcut_1);
5928 updatePackageVersion(CALLING_PACKAGE_3, 1);
5929 mService.mPackageMonitor.onReceive(mServiceContext,
5930 genPackageAddIntent(CALLING_PACKAGE_3, USER_0));
5931
5932 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
5933 assertTrue(getManager().setDynamicShortcuts(list(
5934 makeShortcut("s1"))));
5935 });
5936
Makoto Onuki377b7972016-08-09 14:43:55 -07005937 // Make sure the manifest shortcuts have been published.
5938 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5939 assertWith(getCallerShortcuts())
5940 .selectManifest()
5941 .haveIds("ms1")
5942
5943 .revertToOriginalList()
5944 .selectDynamic()
5945 .haveIds("s1", "s2", "s3")
5946
5947 .revertToOriginalList()
5948 .selectPinned()
5949 .haveIds("ms1", "ms2", "s1", "s2")
5950
5951 .revertToOriginalList()
5952 .selectByIds("ms1")
5953 .areAllManifest()
5954 .areAllEnabled()
5955
5956 .revertToOriginalList()
5957 .selectByIds("ms2")
5958 .areAllNotManifest()
5959 .areAllDisabled();
5960 });
5961
Makoto Onukifc4cf2d2016-08-24 11:10:26 -07005962 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
5963 assertWith(getCallerShortcuts())
5964 .haveIds("s1", "ms1");
5965 });
5966
Michal Karpinski528c3e52018-02-07 17:47:10 +00005967 doReturn(true).when(mMockPackageManagerInternal).isDataRestoreSafe(any(byte[].class),
5968 anyString());
Makoto Onuki377b7972016-08-09 14:43:55 -07005969 // Backup and *without restarting the service, just call applyRestore()*.
5970 {
5971 int prevUid = mInjectedCallingUid;
5972 mInjectedCallingUid = Process.SYSTEM_UID; // Only system can call it.
5973
5974 dumpsysOnLogcat("Before backup");
5975
5976 final byte[] payload = mService.getBackupPayload(USER_0);
5977 if (ENABLE_DUMP) {
5978 final String xml = new String(payload);
5979 Log.v(TAG, "Backup payload:");
5980 for (String line : xml.split("\n")) {
5981 Log.v(TAG, line);
5982 }
5983 }
5984 mService.applyRestore(payload, USER_0);
5985
5986 dumpsysOnLogcat("After restore");
5987
5988 mInjectedCallingUid = prevUid;
5989 }
5990
5991 // The check is also the same as testBackupAndRestore_manifestRePublished().
5992 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5993 assertWith(getCallerVisibleShortcuts())
5994 .selectPinned()
5995 // ms2 was disabled, so not restored.
5996 .haveIds("ms1", "s1", "s2")
5997 .areAllEnabled()
5998
5999 .revertToOriginalList()
6000 .selectByIds("ms1")
6001 .areAllManifest()
6002
6003 .revertToOriginalList()
6004 .selectByIds("s1", "s2")
6005 .areAllNotDynamic()
6006 ;
6007 });
Makoto Onukifc4cf2d2016-08-24 11:10:26 -07006008
6009 // Package 3 still has the same shortcuts.
6010 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
6011 assertWith(getCallerShortcuts())
6012 .haveIds("s1", "ms1");
6013 });
Makoto Onukif3ba2e02016-07-12 09:18:50 -07006014 }
6015
Makoto Onukia4f89b12017-10-05 10:37:55 -07006016
6017 /**
6018 * Restored to a lower version with no manifest shortcuts. All shortcuts are now invisible,
6019 * and all calls from the publisher should ignore them.
6020 */
6021 public void testBackupAndRestore_disabledShortcutsAreIgnored() {
6022 // Publish two manifest shortcuts.
6023 addManifestShortcutResource(
6024 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6025 R.xml.shortcut_5_altalt);
6026 updatePackageVersion(CALLING_PACKAGE_1, 1);
6027 mService.mPackageMonitor.onReceive(mServiceContext,
6028 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6029
6030 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6031 assertTrue(mManager.setDynamicShortcuts(list(
6032 makeShortcutWithShortLabel("s1", "original-title"),
6033 makeShortcut("s2"), makeShortcut("s3"))));
6034 });
6035
6036 // Pin from launcher 1.
6037 runWithCaller(LAUNCHER_1, USER_0, () -> {
6038 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
6039 list("ms1", "ms2", "ms3", "ms4", "s1", "s2"), HANDLE_USER_0);
6040 });
6041
Michal Karpinski528c3e52018-02-07 17:47:10 +00006042 doReturn(true).when(mMockPackageManagerInternal).isDataRestoreSafe(
6043 any(byte[].class), anyString());
6044
Makoto Onukia4f89b12017-10-05 10:37:55 -07006045 backupAndRestore();
6046
6047 // Lower the version and remove the manifest shortcuts.
6048 addManifestShortcutResource(
6049 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6050 R.xml.shortcut_0);
6051 addPackage(CALLING_PACKAGE_1, CALLING_UID_1, 0); // Lower version
6052
6053 // When re-installing the app, the manifest shortcut should be re-published.
6054 mService.mPackageMonitor.onReceive(mServiceContext,
6055 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6056 mService.mPackageMonitor.onReceive(mServiceContext,
6057 genPackageAddIntent(LAUNCHER_1, USER_0));
6058
6059 // No shortcuts should be visible to the publisher.
6060 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6061 assertWith(getCallerVisibleShortcuts())
6062 .isEmpty();
6063 });
6064
6065 final Runnable checkAllDisabledForLauncher = () -> {
6066 runWithCaller(LAUNCHER_1, USER_0, () -> {
6067 assertWith(getShortcutAsLauncher(USER_0))
6068 .areAllPinned()
6069 .haveIds("ms1", "ms2", "ms3", "ms4", "s1", "s2")
6070 .areAllDisabled()
6071 .areAllWithDisabledReason(ShortcutInfo.DISABLED_REASON_VERSION_LOWER)
6072
6073 .forShortcutWithId("s1", si -> {
6074 assertEquals("original-title", si.getShortLabel());
6075 })
6076 .forShortcutWithId("ms1", si -> {
6077 assertEquals("string-com.android.test.1-user:0-res:"
6078 + R.string.shortcut_title1 + "/en"
6079 , si.getShortLabel());
6080 })
6081 .forShortcutWithId("ms2", si -> {
6082 assertEquals("string-com.android.test.1-user:0-res:"
6083 + R.string.shortcut_title2 + "/en"
6084 , si.getShortLabel());
6085 })
6086 .forShortcutWithId("ms3", si -> {
6087 assertEquals("string-com.android.test.1-user:0-res:"
6088 + R.string.shortcut_title1 + "/en"
6089 , si.getShortLabel());
6090 assertEquals("string-com.android.test.1-user:0-res:"
6091 + R.string.shortcut_title2 + "/en"
6092 , si.getLongLabel());
6093 })
6094 .forShortcutWithId("ms4", si -> {
6095 assertEquals("string-com.android.test.1-user:0-res:"
6096 + R.string.shortcut_title2 + "/en"
6097 , si.getShortLabel());
6098 assertEquals("string-com.android.test.1-user:0-res:"
6099 + R.string.shortcut_title2 + "/en"
6100 , si.getLongLabel());
6101 });
6102 });
6103 };
6104
6105 checkAllDisabledForLauncher.run();
6106
6107 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6108
6109 makeCallerForeground(); // CALLING_PACKAGE_1 is now in the foreground.
6110
6111 // All changing API calls should be ignored.
6112
6113 getManager().enableShortcuts(list("ms1", "ms2", "ms3", "ms4", "s1", "s2"));
6114 checkAllDisabledForLauncher.run();
6115
6116 getManager().enableShortcuts(list("ms1", "ms2", "ms3", "ms4", "s1", "s2"));
6117 checkAllDisabledForLauncher.run();
6118
6119 getManager().enableShortcuts(list("ms1", "ms2", "ms3", "ms4", "s1", "s2"));
6120 checkAllDisabledForLauncher.run();
6121
6122 getManager().removeAllDynamicShortcuts();
6123 getManager().removeDynamicShortcuts(list("ms1", "ms2", "ms3", "ms4", "s1", "s2"));
6124 checkAllDisabledForLauncher.run();
6125
6126 getManager().updateShortcuts(list(makeShortcutWithShortLabel("s1", "new-title")));
6127 checkAllDisabledForLauncher.run();
6128
6129
6130 // Add a shortcut -- even though ms1 was immutable, it will succeed.
6131 assertTrue(getManager().addDynamicShortcuts(list(
6132 makeShortcutWithShortLabel("ms1", "original-title"))));
6133
6134 runWithCaller(LAUNCHER_1, USER_0, () -> {
6135 assertWith(getShortcutAsLauncher(USER_0))
6136 .haveIds("ms1", "ms2", "ms3", "ms4", "s1", "s2")
6137
6138 .selectByIds("ms1")
6139 .areAllEnabled()
6140 .areAllDynamic()
6141 .areAllPinned()
6142 .forAllShortcuts(si -> {
6143 assertEquals("original-title", si.getShortLabel());
6144 })
6145
6146 // The rest still exist and disabled.
6147 .revertToOriginalList()
6148 .selectByIds("ms2", "ms3", "ms4", "s1", "s2")
6149 .areAllDisabled()
6150 .areAllPinned()
6151 ;
6152 });
6153
6154 assertTrue(getManager().setDynamicShortcuts(list(
6155 makeShortcutWithShortLabel("ms2", "new-title-2"))));
6156
6157 runWithCaller(LAUNCHER_1, USER_0, () -> {
6158 assertWith(getShortcutAsLauncher(USER_0))
6159 .haveIds("ms1", "ms2", "ms3", "ms4", "s1", "s2")
6160
6161 .selectByIds("ms1")
6162 .areAllEnabled()
6163 .areAllNotDynamic() // ms1 was not in the list, so no longer dynamic.
6164 .areAllPinned()
6165 .areAllMutable()
6166 .forAllShortcuts(si -> {
6167 assertEquals("original-title", si.getShortLabel());
6168 })
6169
6170 .revertToOriginalList()
6171 .selectByIds("ms2")
6172 .areAllEnabled()
6173 .areAllDynamic()
6174 .areAllPinned()
6175 .areAllMutable()
6176 .forAllShortcuts(si -> {
6177 assertEquals("new-title-2", si.getShortLabel());
6178 })
6179
6180 // The rest still exist and disabled.
6181 .revertToOriginalList()
6182 .selectByIds("ms3", "ms4", "s1", "s2")
6183 .areAllDisabled()
6184 .areAllPinned()
6185 ;
6186 });
6187
6188 // Prepare for requestPinShortcut().
6189 setDefaultLauncher(USER_0, mMainActivityFetcher.apply(LAUNCHER_1, USER_0));
6190 mPinConfirmActivityFetcher = (packageName, userId) ->
6191 new ComponentName(packageName, PIN_CONFIRM_ACTIVITY_CLASS);
6192
6193 mManager.requestPinShortcut(
6194 makeShortcutWithShortLabel("ms3", "new-title-3"),
6195 /*PendingIntent=*/ null);
6196
6197 // Note this was pinned, so it'll be accepted right away.
6198 runWithCaller(LAUNCHER_1, USER_0, () -> {
6199 assertWith(getShortcutAsLauncher(USER_0))
6200 .selectByIds("ms3")
6201 .areAllEnabled()
6202 .areAllNotDynamic()
6203 .areAllPinned()
6204 .areAllMutable()
6205 .forAllShortcuts(si -> {
6206 assertEquals("new-title-3", si.getShortLabel());
6207 // The new one replaces the old manifest shortcut, so the long label
6208 // should be gone now.
6209 assertNull(si.getLongLabel());
6210 });
6211 });
6212
6213 // Now, change the launcher to launcher2, and request pin again.
6214 setDefaultLauncher(USER_0, mMainActivityFetcher.apply(LAUNCHER_2, USER_0));
6215
6216 reset(mServiceContext);
6217
6218 assertTrue(mManager.isRequestPinShortcutSupported());
6219 mManager.requestPinShortcut(
6220 makeShortcutWithShortLabel("ms4", "new-title-4"),
6221 /*PendingIntent=*/ null);
6222
6223 // Initially there should be no pinned shortcuts for L2.
6224 runWithCaller(LAUNCHER_2, USER_0, () -> {
6225 assertWith(getShortcutAsLauncher(USER_0))
6226 .selectPinned()
6227 .isEmpty();
6228
6229 final ArgumentCaptor<Intent> intent = ArgumentCaptor.forClass(Intent.class);
6230
6231 verify(mServiceContext).startActivityAsUser(intent.capture(), eq(HANDLE_USER_0));
6232
6233 assertEquals(LauncherApps.ACTION_CONFIRM_PIN_SHORTCUT,
6234 intent.getValue().getAction());
6235 assertEquals(LAUNCHER_2, intent.getValue().getComponent().getPackageName());
6236
6237 // Check the request object.
6238 final PinItemRequest request = mLauncherApps.getPinItemRequest(intent.getValue());
6239
6240 assertNotNull(request);
6241 assertEquals(PinItemRequest.REQUEST_TYPE_SHORTCUT, request.getRequestType());
6242
6243 assertWith(request.getShortcutInfo())
6244 .haveIds("ms4")
6245 .areAllOrphan()
6246 .forAllShortcuts(si -> {
6247 assertEquals("new-title-4", si.getShortLabel());
6248 // The new one replaces the old manifest shortcut, so the long label
6249 // should be gone now.
6250 assertNull(si.getLongLabel());
6251 });
6252 assertTrue(request.accept());
6253
6254 assertWith(getShortcutAsLauncher(USER_0))
6255 .selectPinned()
6256 .haveIds("ms4")
6257 .areAllEnabled();
6258 });
6259 });
6260 }
6261
6262 /**
6263 * Test for restoring the pre-P backup format.
6264 */
6265 public void testBackupAndRestore_api27format() throws Exception {
6266 final byte[] payload = readTestAsset("shortcut/shortcut_api27_backup.xml").getBytes();
6267
6268 addPackage(CALLING_PACKAGE_1, CALLING_UID_1, 10, "22222");
6269 addPackage(LAUNCHER_1, LAUNCHER_UID_1, 10, "11111");
6270
Michal Karpinski528c3e52018-02-07 17:47:10 +00006271 doReturn(true).when(mMockPackageManagerInternal).isDataRestoreSafe(
6272 any(byte[].class), anyString());
6273
Makoto Onukia4f89b12017-10-05 10:37:55 -07006274 runWithSystemUid(() -> mService.applyRestore(payload, USER_0));
6275
6276 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6277 assertWith(getCallerShortcuts())
6278 .areAllPinned()
6279 .haveIds("s1")
6280 .areAllEnabled();
6281 });
6282
6283 runWithCaller(LAUNCHER_1, USER_0, () -> {
6284 assertWith(getShortcutAsLauncher(USER_0))
6285 .areAllPinned()
6286 .haveIds("s1")
6287 .areAllEnabled();
6288 });
6289 // Make sure getBackupSourceVersionCode and isBackupSourceBackupAllowed
6290 // are correct. We didn't have them in the old format.
6291 assertEquals(8, mService.getPackageShortcutForTest(CALLING_PACKAGE_1, USER_0)
6292 .getPackageInfo().getBackupSourceVersionCode());
6293 assertTrue(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, USER_0)
6294 .getPackageInfo().isBackupSourceBackupAllowed());
6295
6296 assertEquals(9, mService.getLauncherShortcutForTest(LAUNCHER_1, USER_0)
6297 .getPackageInfo().getBackupSourceVersionCode());
6298 assertTrue(mService.getLauncherShortcutForTest(LAUNCHER_1, USER_0)
6299 .getPackageInfo().isBackupSourceBackupAllowed());
6300
6301 }
6302
Makoto Onuki2e210c42016-03-30 08:30:36 -07006303 public void testSaveAndLoad_crossProfile() {
6304 prepareCrossProfileDataSet();
6305
6306 dumpsysOnLogcat("Before save & load");
6307
6308 mService.saveDirtyInfo();
6309 initService();
6310
6311 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6312 assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()),
Makoto Onukiac214972016-04-04 10:19:45 -07006313 "s1", "s2", "s3");
Makoto Onuki2e210c42016-03-30 08:30:36 -07006314 assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()),
6315 "s1", "s2", "s3", "s4");
6316 });
6317 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
6318 assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()),
Makoto Onukiac214972016-04-04 10:19:45 -07006319 "s1", "s2", "s3");
Makoto Onuki2e210c42016-03-30 08:30:36 -07006320 assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()),
6321 "s1", "s2", "s3", "s4", "s5");
6322 });
6323 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
6324 assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()),
Makoto Onukiac214972016-04-04 10:19:45 -07006325 "s1", "s2", "s3");
Makoto Onuki2e210c42016-03-30 08:30:36 -07006326 assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()),
6327 "s1", "s2", "s3", "s4", "s5", "s6");
6328 });
6329 runWithCaller(CALLING_PACKAGE_4, USER_0, () -> {
6330 assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts())
6331 /* empty */);
6332 assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts())
6333 /* empty */);
6334 });
6335 runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
6336 assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()),
Makoto Onukiac214972016-04-04 10:19:45 -07006337 "s1", "s2", "s3");
Makoto Onuki2e210c42016-03-30 08:30:36 -07006338 assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()),
6339 "s1", "s2", "s3", "s4", "s5", "s6");
6340 });
6341 runWithCaller(CALLING_PACKAGE_2, USER_P0, () -> {
6342 assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts())
6343 /* empty */);
6344 assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts())
6345 /* empty */);
6346 });
6347 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
6348 assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()),
Makoto Onukiac214972016-04-04 10:19:45 -07006349 "x1", "x2", "x3");
Makoto Onuki2e210c42016-03-30 08:30:36 -07006350 assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()),
6351 "x4", "x5");
6352 });
6353 runWithCaller(LAUNCHER_1, USER_0, () -> {
6354 assertShortcutIds(
6355 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_0),
6356 "s1");
6357 assertShortcutIds(
6358 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_0),
6359 "s1", "s2");
6360 assertShortcutIds(
6361 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_3), HANDLE_USER_0),
6362 "s1", "s2", "s3");
6363 assertShortcutIds(
6364 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_4), HANDLE_USER_0)
6365 /* empty */);
6366 assertShortcutIds(
6367 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_P0),
6368 "s1", "s4");
6369 assertShortcutIds(
6370 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_P0)
6371 /* empty */);
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07006372 assertExpectException(
Makoto Onuki2e210c42016-03-30 08:30:36 -07006373 SecurityException.class, "", () -> {
6374 mLauncherApps.getShortcuts(
6375 buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_10);
6376 });
6377 });
6378 runWithCaller(LAUNCHER_2, USER_0, () -> {
6379 assertShortcutIds(
6380 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_0),
6381 "s2");
6382 assertShortcutIds(
6383 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_0),
6384 "s2", "s3");
6385 assertShortcutIds(
6386 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_3), HANDLE_USER_0),
6387 "s2", "s3", "s4");
6388 assertShortcutIds(
6389 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_4), HANDLE_USER_0)
6390 /* empty */);
6391 assertShortcutIds(
6392 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_P0),
6393 "s2", "s5");
6394 assertShortcutIds(
6395 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_P0)
6396 /* empty */);
6397 });
6398 runWithCaller(LAUNCHER_3, USER_0, () -> {
6399 assertShortcutIds(
6400 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_0),
6401 "s3");
6402 assertShortcutIds(
6403 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_0),
6404 "s3", "s4");
6405 assertShortcutIds(
6406 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_3), HANDLE_USER_0),
6407 "s3", "s4", "s5");
6408 assertShortcutIds(
6409 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_4), HANDLE_USER_0)
6410 /* empty */);
6411 assertShortcutIds(
6412 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_P0),
6413 "s3", "s6");
6414 assertShortcutIds(
6415 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_P0)
6416 /* empty */);
6417 });
6418 runWithCaller(LAUNCHER_4, USER_0, () -> {
6419 assertShortcutIds(
6420 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_0)
6421 /* empty */);
6422 assertShortcutIds(
6423 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_0)
6424 /* empty */);
6425 assertShortcutIds(
6426 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_3), HANDLE_USER_0)
6427 /* empty */);
6428 assertShortcutIds(
6429 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_4), HANDLE_USER_0)
6430 /* empty */);
6431 assertShortcutIds(
6432 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_P0)
6433 /* empty */);
6434 assertShortcutIds(
6435 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_P0)
6436 /* empty */);
6437 });
6438 runWithCaller(LAUNCHER_1, USER_P0, () -> {
6439 assertShortcutIds(
6440 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_0),
6441 "s3", "s4");
6442 assertShortcutIds(
6443 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_0),
6444 "s3", "s4", "s5");
6445 assertShortcutIds(
6446 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_3), HANDLE_USER_0),
6447 "s3", "s4", "s5", "s6");
6448 assertShortcutIds(
6449 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_P0),
6450 "s1", "s4");
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07006451 assertExpectException(
Makoto Onukiac214972016-04-04 10:19:45 -07006452 SecurityException.class, "unrelated profile", () -> {
Makoto Onuki2e210c42016-03-30 08:30:36 -07006453 mLauncherApps.getShortcuts(
6454 buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_10);
6455 });
6456 });
6457 runWithCaller(LAUNCHER_1, USER_10, () -> {
6458 assertShortcutIds(
6459 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_10),
6460 "x4", "x5");
6461 assertShortcutIds(
6462 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_10)
6463 /* empty */);
6464 assertShortcutIds(
6465 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_3), HANDLE_USER_10)
6466 /* empty */);
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07006467 assertExpectException(
Makoto Onukiac214972016-04-04 10:19:45 -07006468 SecurityException.class, "unrelated profile", () -> {
Makoto Onuki2e210c42016-03-30 08:30:36 -07006469 mLauncherApps.getShortcuts(
6470 buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0);
6471 });
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07006472 assertExpectException(
Makoto Onukiac214972016-04-04 10:19:45 -07006473 SecurityException.class, "unrelated profile", () -> {
Makoto Onuki2e210c42016-03-30 08:30:36 -07006474 mLauncherApps.getShortcuts(
6475 buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_P0);
6476 });
6477 });
Makoto Onukic8c33292016-09-12 16:36:59 -07006478 // Check the user-IDs.
6479 assertEquals(USER_0,
6480 mService.getUserShortcutsLocked(USER_0).getPackageShortcuts(CALLING_PACKAGE_1)
6481 .getOwnerUserId());
6482 assertEquals(USER_0,
6483 mService.getUserShortcutsLocked(USER_0).getPackageShortcuts(CALLING_PACKAGE_1)
6484 .getPackageUserId());
6485 assertEquals(USER_P0,
6486 mService.getUserShortcutsLocked(USER_P0).getPackageShortcuts(CALLING_PACKAGE_1)
6487 .getOwnerUserId());
6488 assertEquals(USER_P0,
6489 mService.getUserShortcutsLocked(USER_P0).getPackageShortcuts(CALLING_PACKAGE_1)
6490 .getPackageUserId());
6491
6492 assertEquals(USER_0,
6493 mService.getUserShortcutsLocked(USER_0).getLauncherShortcuts(LAUNCHER_1, USER_0)
6494 .getOwnerUserId());
6495 assertEquals(USER_0,
6496 mService.getUserShortcutsLocked(USER_0).getLauncherShortcuts(LAUNCHER_1, USER_0)
6497 .getPackageUserId());
6498 assertEquals(USER_P0,
6499 mService.getUserShortcutsLocked(USER_P0).getLauncherShortcuts(LAUNCHER_1, USER_0)
6500 .getOwnerUserId());
6501 assertEquals(USER_0,
6502 mService.getUserShortcutsLocked(USER_P0).getLauncherShortcuts(LAUNCHER_1, USER_0)
6503 .getPackageUserId());
Makoto Onuki2e210c42016-03-30 08:30:36 -07006504 }
6505
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07006506 public void testOnApplicationActive_permission() {
6507 assertExpectException(SecurityException.class, "Missing permission", () ->
Makoto Onukid6e1f3b2016-06-14 11:17:59 -07006508 mManager.onApplicationActive(CALLING_PACKAGE_1, USER_0));
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07006509
6510 // Has permission, now it should pass.
6511 mCallerPermissions.add(permission.RESET_SHORTCUT_MANAGER_THROTTLING);
Makoto Onukid6e1f3b2016-06-14 11:17:59 -07006512 mManager.onApplicationActive(CALLING_PACKAGE_1, USER_0);
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07006513 }
6514
Mehdi Alizadehc86dd1f2019-01-25 16:59:54 -08006515 public void testGetShareTargets_permission() {
6516 IntentFilter filter = new IntentFilter();
6517
6518 assertExpectException(SecurityException.class, "Missing permission", () ->
6519 mManager.getShareTargets(filter));
6520
6521 // Has permission, now it should pass.
6522 mCallerPermissions.add(permission.MANAGE_APP_PREDICTIONS);
6523 mManager.getShareTargets(filter);
6524 }
6525
Mehdi Alizadeh627d4db2019-02-04 13:52:03 -08006526 public void testHasShareTargets_permission() {
6527 assertExpectException(SecurityException.class, "Missing permission", () ->
6528 mManager.hasShareTargets(CALLING_PACKAGE_1));
6529
6530 // Has permission, now it should pass.
6531 mCallerPermissions.add(permission.MANAGE_APP_PREDICTIONS);
6532 mManager.hasShareTargets(CALLING_PACKAGE_1);
6533 }
6534
Makoto Onuki2e210c42016-03-30 08:30:36 -07006535 public void testDumpsys_crossProfile() {
6536 prepareCrossProfileDataSet();
6537 dumpsysOnLogcat("test1", /* force= */ true);
6538 }
6539
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07006540 public void testDumpsys_withIcons() throws IOException {
Makoto Onuki2e210c42016-03-30 08:30:36 -07006541 testIcons();
6542 // Dump after having some icons.
6543 dumpsysOnLogcat("test1", /* force= */ true);
Makoto Onuki0acbb142016-03-22 17:02:57 -07006544 }
Makoto Onuki22fcc682016-05-17 14:52:19 -07006545
6546 public void testManifestShortcut_publishOnUnlockUser() {
6547 addManifestShortcutResource(
6548 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6549 R.xml.shortcut_1);
6550 addManifestShortcutResource(
6551 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
6552 R.xml.shortcut_2);
6553 addManifestShortcutResource(
6554 new ComponentName(CALLING_PACKAGE_3, ShortcutActivity.class.getName()),
6555 R.xml.shortcut_5);
6556
6557 // Unlock user-0.
Makoto Onuki248a0ef2016-11-03 15:59:01 -07006558 mInjectedCurrentTimeMillis += 100;
Makoto Onuki22fcc682016-05-17 14:52:19 -07006559 mService.handleUnlockUser(USER_0);
6560
6561 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6562 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6563 mManager.getManifestShortcuts()))),
6564 "ms1");
6565 assertEmpty(mManager.getPinnedShortcuts());
6566 });
6567
6568 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
6569 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6570 mManager.getManifestShortcuts()))),
6571 "ms1", "ms2");
6572 assertEmpty(mManager.getPinnedShortcuts());
6573 });
6574
6575 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
6576 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6577 mManager.getManifestShortcuts()))),
6578 "ms1", "ms2", "ms3", "ms4", "ms5");
6579 assertEmpty(mManager.getPinnedShortcuts());
6580 });
6581
6582 // Try on another user, with some packages uninstalled.
Makoto Onuki9c850012016-07-26 15:50:50 -07006583 mRunningUsers.put(USER_10, true);
6584
Makoto Onuki22fcc682016-05-17 14:52:19 -07006585 uninstallPackage(USER_10, CALLING_PACKAGE_1);
6586 uninstallPackage(USER_10, CALLING_PACKAGE_3);
6587
Makoto Onuki248a0ef2016-11-03 15:59:01 -07006588 mInjectedCurrentTimeMillis += 100;
Makoto Onuki22fcc682016-05-17 14:52:19 -07006589 mService.handleUnlockUser(USER_10);
6590
6591 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
6592 assertEmpty(mManager.getManifestShortcuts());
6593 assertEmpty(mManager.getPinnedShortcuts());
6594 });
6595
6596 runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
6597 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6598 mManager.getManifestShortcuts()))),
6599 "ms1", "ms2");
6600 assertEmpty(mManager.getPinnedShortcuts());
6601 });
6602
6603 runWithCaller(CALLING_PACKAGE_3, USER_10, () -> {
6604 assertEmpty(mManager.getManifestShortcuts());
6605 assertEmpty(mManager.getPinnedShortcuts());
6606 });
6607
6608 // Now change the resources for package 1, and unlock again.
6609 // But we still see *old* shortcuts, because the package version and install time
6610 // hasn't changed.
6611 shutdownServices();
6612
Makoto Onuki248a0ef2016-11-03 15:59:01 -07006613 mInjectedCurrentTimeMillis += 100;
6614
Makoto Onuki22fcc682016-05-17 14:52:19 -07006615 addManifestShortcutResource(
6616 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6617 R.xml.shortcut_5);
6618 addManifestShortcutResource(
6619 new ComponentName(CALLING_PACKAGE_3, ShortcutActivity.class.getName()),
6620 R.xml.shortcut_1);
6621
6622 initService();
6623 mService.handleUnlockUser(USER_0);
6624
6625 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
Makoto Onuki248a0ef2016-11-03 15:59:01 -07006626 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled( // FAIL
Makoto Onuki22fcc682016-05-17 14:52:19 -07006627 mManager.getManifestShortcuts()))),
6628 "ms1");
6629 assertEmpty(mManager.getPinnedShortcuts());
6630 });
6631
6632 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
6633 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6634 mManager.getManifestShortcuts()))),
6635 "ms1", "ms2");
6636 assertEmpty(mManager.getPinnedShortcuts());
6637 });
6638
6639 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
6640 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6641 mManager.getManifestShortcuts()))),
6642 "ms1", "ms2", "ms3", "ms4", "ms5");
6643 assertEmpty(mManager.getPinnedShortcuts());
6644 });
6645
6646 // Do it again, but this time we change the app version, so we do detect the changes.
6647 shutdownServices();
6648
Makoto Onuki248a0ef2016-11-03 15:59:01 -07006649 mInjectedCurrentTimeMillis += 100;
6650
Makoto Onuki22fcc682016-05-17 14:52:19 -07006651 updatePackageVersion(CALLING_PACKAGE_1, 1);
6652 updatePackageLastUpdateTime(CALLING_PACKAGE_3, 1);
6653
6654 initService();
6655 mService.handleUnlockUser(USER_0);
6656
6657 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6658 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6659 mManager.getManifestShortcuts()))),
6660 "ms1", "ms2", "ms3", "ms4", "ms5");
6661 assertEmpty(mManager.getPinnedShortcuts());
6662 });
6663
6664 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
6665 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6666 mManager.getManifestShortcuts()))),
6667 "ms1", "ms2");
6668 assertEmpty(mManager.getPinnedShortcuts());
6669 });
6670
6671 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
6672 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6673 mManager.getManifestShortcuts()))),
6674 "ms1");
6675 assertEmpty(mManager.getPinnedShortcuts());
6676 });
6677
6678 // Next, try removing all shortcuts, with some of them pinned.
6679 runWithCaller(LAUNCHER_1, USER_0, () -> {
6680 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms3"), HANDLE_USER_0);
6681 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("ms2"), HANDLE_USER_0);
6682 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("ms1"), HANDLE_USER_0);
6683 });
6684 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6685 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6686 mManager.getManifestShortcuts()))),
6687 "ms1", "ms2", "ms3", "ms4", "ms5");
6688 assertShortcutIds(assertAllImmutable(assertAllPinned(assertAllManifest(
6689 assertAllEnabled(mManager.getPinnedShortcuts())))),
6690 "ms3");
6691 });
6692
6693 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
6694 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6695 mManager.getManifestShortcuts()))),
6696 "ms1", "ms2");
6697 assertShortcutIds(assertAllImmutable(assertAllPinned(assertAllManifest(
6698 assertAllEnabled(mManager.getPinnedShortcuts())))),
6699 "ms2");
6700 });
6701
6702 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
6703 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6704 mManager.getManifestShortcuts()))),
6705 "ms1");
6706 assertShortcutIds(assertAllImmutable(assertAllPinned(assertAllManifest(
6707 assertAllEnabled(mManager.getPinnedShortcuts())))),
6708 "ms1");
6709 });
6710
6711 shutdownServices();
6712
Makoto Onuki248a0ef2016-11-03 15:59:01 -07006713 mInjectedCurrentTimeMillis += 100;
6714
Makoto Onuki22fcc682016-05-17 14:52:19 -07006715 addManifestShortcutResource(
6716 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6717 R.xml.shortcut_0);
6718 addManifestShortcutResource(
6719 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
6720 R.xml.shortcut_1);
6721 addManifestShortcutResource(
6722 new ComponentName(CALLING_PACKAGE_3, ShortcutActivity.class.getName()),
6723 R.xml.shortcut_0);
6724
6725 updatePackageVersion(CALLING_PACKAGE_1, 1);
6726 updatePackageVersion(CALLING_PACKAGE_2, 1);
6727 updatePackageVersion(CALLING_PACKAGE_3, 1);
6728
6729 initService();
6730 mService.handleUnlockUser(USER_0);
6731
6732 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6733 assertEmpty(mManager.getManifestShortcuts());
6734 assertShortcutIds(assertAllImmutable(assertAllPinned(assertAllNotManifest(
6735 assertAllDisabled(mManager.getPinnedShortcuts())))),
6736 "ms3");
6737 });
6738
6739 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
6740 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6741 mManager.getManifestShortcuts()))),
6742 "ms1");
6743 assertShortcutIds(assertAllImmutable(assertAllPinned(assertAllNotManifest(
6744 assertAllDisabled(mManager.getPinnedShortcuts())))),
6745 "ms2");
6746 });
6747
6748 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
6749 assertEmpty(mManager.getManifestShortcuts());
6750 assertShortcutIds(assertAllImmutable(assertAllPinned(assertAllNotManifest(
6751 assertAllDisabled(mManager.getPinnedShortcuts())))),
6752 "ms1");
6753 });
6754
6755 // Make sure we don't have ShortcutPackage for packages that don't have shortcuts.
6756 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_4, USER_0));
6757 assertNull(mService.getPackageShortcutForTest(LAUNCHER_1, USER_0));
6758 }
6759
Makoto Onuki22fcc682016-05-17 14:52:19 -07006760 public void testManifestShortcut_publishOnBroadcast() {
6761 // First, no packages are installed.
6762 uninstallPackage(USER_0, CALLING_PACKAGE_1);
6763 uninstallPackage(USER_0, CALLING_PACKAGE_2);
6764 uninstallPackage(USER_0, CALLING_PACKAGE_3);
6765 uninstallPackage(USER_0, CALLING_PACKAGE_4);
6766 uninstallPackage(USER_10, CALLING_PACKAGE_1);
6767 uninstallPackage(USER_10, CALLING_PACKAGE_2);
6768 uninstallPackage(USER_10, CALLING_PACKAGE_3);
6769 uninstallPackage(USER_10, CALLING_PACKAGE_4);
6770
6771 mService.handleUnlockUser(USER_0);
Makoto Onuki9c850012016-07-26 15:50:50 -07006772
6773 mRunningUsers.put(USER_10, true);
Makoto Onuki22fcc682016-05-17 14:52:19 -07006774 mService.handleUnlockUser(USER_10);
6775
6776 // Originally no manifest shortcuts.
6777 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6778 assertEmpty(mManager.getManifestShortcuts());
6779 assertEmpty(mManager.getPinnedShortcuts());
6780 });
6781
6782 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
6783 assertEmpty(mManager.getManifestShortcuts());
6784 assertEmpty(mManager.getPinnedShortcuts());
6785 });
6786
6787 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
6788 assertEmpty(mManager.getManifestShortcuts());
6789 assertEmpty(mManager.getPinnedShortcuts());
6790 });
6791
6792 // Package 1 updated, with manifest shortcuts.
6793 addManifestShortcutResource(
6794 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6795 R.xml.shortcut_1);
6796 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07006797 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki22fcc682016-05-17 14:52:19 -07006798 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6799
6800 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6801 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6802 mManager.getManifestShortcuts()))),
6803 "ms1");
6804 assertEmpty(mManager.getPinnedShortcuts());
6805 });
6806
6807 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
6808 assertEmpty(mManager.getManifestShortcuts());
6809 assertEmpty(mManager.getPinnedShortcuts());
6810 });
6811
6812 // Package 2 updated, with manifest shortcuts.
6813
6814 addManifestShortcutResource(
6815 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
Makoto Onukib1588c02017-10-12 15:11:45 -07006816 R.xml.shortcut_5_altalt);
Makoto Onuki22fcc682016-05-17 14:52:19 -07006817 updatePackageVersion(CALLING_PACKAGE_2, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07006818 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki22fcc682016-05-17 14:52:19 -07006819 genPackageAddIntent(CALLING_PACKAGE_2, USER_0));
6820
6821 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6822 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6823 mManager.getManifestShortcuts()))),
6824 "ms1");
6825 assertEmpty(mManager.getPinnedShortcuts());
6826 });
6827
6828 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
6829 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6830 mManager.getManifestShortcuts()))),
6831 "ms1", "ms2", "ms3", "ms4", "ms5");
Makoto Onuki9e1f5592016-06-08 12:30:23 -07006832 assertWith(getCallerShortcuts()).selectManifest()
6833 .selectByActivity(
6834 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()))
6835 .haveRanksInOrder("ms1", "ms2", "ms3", "ms4", "ms5");
Makoto Onuki22fcc682016-05-17 14:52:19 -07006836 assertEmpty(mManager.getPinnedShortcuts());
6837 });
6838
6839 // Package 2 updated, with less manifest shortcuts.
6840 // This time we use updatePackageLastUpdateTime() instead of updatePackageVersion().
6841
6842 dumpsysOnLogcat("Before pinning");
6843
6844 // Also pin some.
6845 runWithCaller(LAUNCHER_1, USER_0, () -> {
6846 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("ms2", "ms3"), HANDLE_USER_0);
6847 });
6848
6849 dumpsysOnLogcat("After pinning");
6850
6851 addManifestShortcutResource(
6852 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
6853 R.xml.shortcut_2);
6854 updatePackageLastUpdateTime(CALLING_PACKAGE_2, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07006855 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki22fcc682016-05-17 14:52:19 -07006856 genPackageAddIntent(CALLING_PACKAGE_2, USER_0));
6857
Makoto Onukib1588c02017-10-12 15:11:45 -07006858 dumpsysOnLogcat("After updating package 2");
6859
Makoto Onuki22fcc682016-05-17 14:52:19 -07006860 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6861 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6862 mManager.getManifestShortcuts()))),
6863 "ms1");
6864 assertEmpty(mManager.getPinnedShortcuts());
6865 });
6866
6867 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
6868 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6869 mManager.getManifestShortcuts()))),
6870 "ms1", "ms2");
Makoto Onuki9e1f5592016-06-08 12:30:23 -07006871 assertWith(getCallerShortcuts()).selectManifest()
6872 .selectByActivity(
6873 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()))
6874 .haveRanksInOrder("ms1", "ms2");
Makoto Onuki22fcc682016-05-17 14:52:19 -07006875 assertShortcutIds(assertAllImmutable(assertAllPinned(
6876 mManager.getPinnedShortcuts())),
6877 "ms2", "ms3");
6878 // ms3 is no longer in manifest, so should be disabled.
6879 // but ms1 and ms2 should be enabled.
Makoto Onukib1588c02017-10-12 15:11:45 -07006880 assertWith(getCallerShortcuts())
6881 .selectByIds("ms1", "ms2")
6882 .areAllEnabled()
6883
6884 .revertToOriginalList()
6885 .selectByIds("ms3")
6886 .areAllDisabled()
6887 .areAllWithDisabledReason(ShortcutInfo.DISABLED_REASON_APP_CHANGED);
Makoto Onuki22fcc682016-05-17 14:52:19 -07006888 });
6889
Makoto Onukib1588c02017-10-12 15:11:45 -07006890 // Make sure the launcher see the correct disabled reason.
6891 runWithCaller(LAUNCHER_1, USER_0, () -> {
6892 assertWith(getShortcutAsLauncher(USER_0))
6893 .forShortcutWithId("ms3", si -> {
6894 assertEquals("string-com.android.test.2-user:0-res:"
6895 + R.string.shortcut_disabled_message3 + "/en",
6896 si.getDisabledMessage());
6897 });
6898 });
6899
6900
Makoto Onuki22fcc682016-05-17 14:52:19 -07006901 // Package 2 on user 10 has no shortcuts yet.
6902 runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
6903 assertEmpty(mManager.getManifestShortcuts());
6904 assertEmpty(mManager.getPinnedShortcuts());
6905 });
Makoto Onukia2241832016-07-06 13:28:37 -07006906 // Send add broadcast, but the user is not running, so should be ignored.
Makoto Onuki01ce92b2017-04-28 12:24:16 -07006907 mService.handleStopUser(USER_10);
Makoto Onukia2241832016-07-06 13:28:37 -07006908 mRunningUsers.put(USER_10, false);
6909 mUnlockedUsers.put(USER_10, false);
6910
Makoto Onuki4e6cef42016-07-13 16:14:01 -07006911 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onukia2241832016-07-06 13:28:37 -07006912 genPackageAddIntent(CALLING_PACKAGE_2, USER_10));
6913 runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
Makoto Onuki4e6cef42016-07-13 16:14:01 -07006914 // Don't use the mManager APIs to get shortcuts, because they'll trigger the package
6915 // update check.
6916 // So look the internal data directly using getCallerShortcuts().
6917 assertEmpty(getCallerShortcuts());
Makoto Onukia2241832016-07-06 13:28:37 -07006918 });
6919
6920 // Try again, but the user is locked, so still ignored.
6921 mRunningUsers.put(USER_10, true);
Makoto Onukif34c3082016-07-13 10:25:25 -07006922 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onukia2241832016-07-06 13:28:37 -07006923 genPackageAddIntent(CALLING_PACKAGE_2, USER_10));
6924 runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
Makoto Onuki4e6cef42016-07-13 16:14:01 -07006925 // Don't use the mManager APIs to get shortcuts, because they'll trigger the package
6926 // update check.
6927 // So look the internal data directly using getCallerShortcuts().
6928 assertEmpty(getCallerShortcuts());
Makoto Onukia2241832016-07-06 13:28:37 -07006929 });
6930
6931 // Unlock the user, now it should work.
6932 mUnlockedUsers.put(USER_10, true);
6933
Makoto Onuki22fcc682016-05-17 14:52:19 -07006934 // Send PACKAGE_ADD broadcast to have Package 2 on user-10 publish manifest shortcuts.
Makoto Onukif34c3082016-07-13 10:25:25 -07006935 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki22fcc682016-05-17 14:52:19 -07006936 genPackageAddIntent(CALLING_PACKAGE_2, USER_10));
6937
6938 runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
6939 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6940 mManager.getManifestShortcuts()))),
6941 "ms1", "ms2");
Makoto Onuki9e1f5592016-06-08 12:30:23 -07006942 assertWith(getCallerShortcuts()).selectManifest()
6943 .selectByActivity(
6944 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()))
6945 .haveRanksInOrder("ms1", "ms2");
Makoto Onuki22fcc682016-05-17 14:52:19 -07006946 assertEmpty(mManager.getPinnedShortcuts());
6947 });
6948
6949 // But it shouldn't affect user-0.
6950 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
6951 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6952 mManager.getManifestShortcuts()))),
6953 "ms1", "ms2");
Makoto Onuki9e1f5592016-06-08 12:30:23 -07006954 assertWith(getCallerShortcuts()).selectManifest()
6955 .selectByActivity(
6956 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()))
6957 .haveRanksInOrder("ms1", "ms2");
Makoto Onuki22fcc682016-05-17 14:52:19 -07006958 assertShortcutIds(assertAllImmutable(assertAllPinned(
6959 mManager.getPinnedShortcuts())),
6960 "ms2", "ms3");
6961 assertAllEnabled(list(getCallerShortcut("ms1")));
6962 assertAllEnabled(list(getCallerShortcut("ms2")));
6963 assertAllDisabled(list(getCallerShortcut("ms3")));
6964 });
6965
Makoto Onuki9e1f5592016-06-08 12:30:23 -07006966 // Multiple activities.
6967 // Add shortcuts on activity 2 for package 2.
6968 addManifestShortcutResource(
6969 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
6970 R.xml.shortcut_5_alt);
6971 addManifestShortcutResource(
6972 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity2.class.getName()),
6973 R.xml.shortcut_5_reverse);
6974
6975 updatePackageLastUpdateTime(CALLING_PACKAGE_2, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07006976 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki9e1f5592016-06-08 12:30:23 -07006977 genPackageAddIntent(CALLING_PACKAGE_2, USER_0));
6978
6979 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
6980 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6981 mManager.getManifestShortcuts()))),
6982 "ms1", "ms2", "ms3", "ms4", "ms5",
6983 "ms1_alt", "ms2_alt", "ms3_alt", "ms4_alt", "ms5_alt");
6984
6985 // Make sure they have the correct ranks, regardless of their ID's alphabetical order.
6986 assertWith(getCallerShortcuts()).selectManifest()
6987 .selectByActivity(
6988 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()))
6989 .haveRanksInOrder("ms1_alt", "ms2_alt", "ms3_alt", "ms4_alt", "ms5_alt");
6990 assertWith(getCallerShortcuts()).selectManifest()
6991 .selectByActivity(
6992 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity2.class.getName()))
6993 .haveRanksInOrder("ms5", "ms4", "ms3", "ms2", "ms1");
6994 });
6995
Makoto Onuki22fcc682016-05-17 14:52:19 -07006996 // Package 2 now has no manifest shortcuts.
6997 addManifestShortcutResource(
6998 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
6999 R.xml.shortcut_0);
Makoto Onuki9e1f5592016-06-08 12:30:23 -07007000 addManifestShortcutResource(
7001 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity2.class.getName()),
7002 R.xml.shortcut_0);
Makoto Onuki22fcc682016-05-17 14:52:19 -07007003 updatePackageLastUpdateTime(CALLING_PACKAGE_2, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07007004 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki22fcc682016-05-17 14:52:19 -07007005 genPackageAddIntent(CALLING_PACKAGE_2, USER_0));
7006
7007 // No manifest shortcuts, and pinned ones are disabled.
7008 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
7009 assertEmpty(mManager.getManifestShortcuts());
7010 assertShortcutIds(assertAllImmutable(assertAllPinned(assertAllDisabled(
7011 mManager.getPinnedShortcuts()))),
7012 "ms2", "ms3");
7013 });
7014 }
7015
7016 public void testManifestShortcuts_missingMandatoryFields() {
7017 // Start with no apps installed.
7018 uninstallPackage(USER_0, CALLING_PACKAGE_1);
7019 uninstallPackage(USER_0, CALLING_PACKAGE_2);
7020 uninstallPackage(USER_0, CALLING_PACKAGE_3);
7021 uninstallPackage(USER_0, CALLING_PACKAGE_4);
7022
7023 mService.handleUnlockUser(USER_0);
7024
7025 // Make sure no manifest shortcuts.
7026 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7027 assertEmpty(mManager.getManifestShortcuts());
7028 });
7029
7030 // Package 1 updated, which has one valid manifest shortcut and one invalid.
7031 addManifestShortcutResource(
7032 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7033 R.xml.shortcut_error_1);
7034 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07007035 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki22fcc682016-05-17 14:52:19 -07007036 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7037
7038 // Only the valid one is published.
7039 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
Makoto Onuki6771d732016-07-14 12:58:40 -07007040 assertWith(getCallerShortcuts())
7041 .areAllManifest()
7042 .areAllImmutable()
7043 .areAllEnabled()
7044 .haveIds("x1");
Makoto Onuki22fcc682016-05-17 14:52:19 -07007045 });
7046
7047 // Package 1 updated, which has one valid manifest shortcut and one invalid.
7048 addManifestShortcutResource(
7049 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7050 R.xml.shortcut_error_2);
7051 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07007052 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki22fcc682016-05-17 14:52:19 -07007053 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7054
7055 // Only the valid one is published.
7056 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
Makoto Onuki6771d732016-07-14 12:58:40 -07007057 assertWith(getCallerShortcuts())
7058 .areAllManifest()
7059 .areAllImmutable()
7060 .areAllEnabled()
7061 .haveIds("x2");
Makoto Onuki22fcc682016-05-17 14:52:19 -07007062 });
7063
7064 // Package 1 updated, which has one valid manifest shortcut and one invalid.
7065 addManifestShortcutResource(
7066 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7067 R.xml.shortcut_error_3);
7068 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07007069 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki22fcc682016-05-17 14:52:19 -07007070 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7071
7072 // Only the valid one is published.
7073 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
Makoto Onuki6771d732016-07-14 12:58:40 -07007074 assertWith(getCallerShortcuts())
7075 .areAllManifest()
7076 .areAllImmutable()
7077 .areAllEnabled()
7078 .haveIds("x3")
7079 .forShortcutWithId("x3", si -> {
7080 assertEquals(set("cat2"), si.getCategories());
7081 });
Makoto Onuki22fcc682016-05-17 14:52:19 -07007082 });
7083 }
7084
Makoto Onukidf6da042016-06-16 09:51:40 -07007085 public void testManifestShortcuts_intentDefinitions() {
7086 addManifestShortcutResource(
7087 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7088 R.xml.shortcut_error_4);
7089 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07007090 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onukidf6da042016-06-16 09:51:40 -07007091 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7092
7093 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7094 // Make sure invalid ones are not published.
7095 // Note that at this point disabled ones don't show up because they weren't pinned.
7096 assertWith(getCallerShortcuts())
7097 .haveIds("ms1", "ms2")
7098 .areAllManifest()
7099 .areAllNotDynamic()
7100 .areAllNotPinned()
7101 .areAllImmutable()
7102 .areAllEnabled()
7103 .forShortcutWithId("ms1", si -> {
7104 assertTrue(si.isEnabled());
Makoto Onuki440a1ea2016-07-20 14:21:18 -07007105 assertEquals(1, si.getIntents().length);
7106
Makoto Onukidf6da042016-06-16 09:51:40 -07007107 assertEquals("action1", si.getIntent().getAction());
Makoto Onuki440a1ea2016-07-20 14:21:18 -07007108 assertEquals("value1", si.getIntent().getStringExtra("key1"));
7109 assertEquals(Intent.FLAG_ACTIVITY_NEW_TASK |
7110 Intent.FLAG_ACTIVITY_CLEAR_TASK |
7111 Intent.FLAG_ACTIVITY_TASK_ON_HOME, si.getIntent().getFlags());
7112
7113 assertEquals("action1", si.getIntents()[0].getAction());
7114 assertEquals("value1", si.getIntents()[0].getStringExtra("key1"));
7115 assertEquals(Intent.FLAG_ACTIVITY_NEW_TASK |
7116 Intent.FLAG_ACTIVITY_CLEAR_TASK |
7117 Intent.FLAG_ACTIVITY_TASK_ON_HOME, si.getIntents()[0].getFlags());
Makoto Onukidf6da042016-06-16 09:51:40 -07007118 })
7119 .forShortcutWithId("ms2", si -> {
7120 assertTrue(si.isEnabled());
Makoto Onuki440a1ea2016-07-20 14:21:18 -07007121 assertEquals(2, si.getIntents().length);
7122
7123 // getIntent will return the last one.
7124 assertEquals("action2_2", si.getIntent().getAction());
7125 assertEquals("value2", si.getIntent().getStringExtra("key2"));
7126 assertEquals(0, si.getIntent().getFlags());
7127
7128 final Intent i1 = si.getIntents()[0];
7129 final Intent i2 = si.getIntents()[1];
7130
7131 assertEquals("action2_1", i1.getAction());
7132 assertEquals("value1", i1.getStringExtra("key1"));
7133 assertEquals(Intent.FLAG_ACTIVITY_NEW_TASK |
7134 Intent.FLAG_ACTIVITY_CLEAR_TASK |
7135 Intent.FLAG_ACTIVITY_TASK_ON_HOME, i1.getFlags());
7136
7137 assertEquals("action2_2", i2.getAction());
7138 assertEquals("value2", i2.getStringExtra("key2"));
7139 assertEquals(0, i2.getFlags());
Makoto Onukidf6da042016-06-16 09:51:40 -07007140 });
7141 });
7142
7143 // Publish 5 enabled to pin some, so we can later test disabled manfiest shortcuts..
7144 addManifestShortcutResource(
7145 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7146 R.xml.shortcut_5);
7147 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07007148 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onukidf6da042016-06-16 09:51:40 -07007149 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7150
7151 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7152 // Make sure 5 manifest shortcuts are published.
7153 assertWith(getCallerShortcuts())
7154 .haveIds("ms1", "ms2", "ms3", "ms4", "ms5")
7155 .areAllManifest()
7156 .areAllNotDynamic()
7157 .areAllNotPinned()
7158 .areAllImmutable()
7159 .areAllEnabled();
7160 });
7161
7162 runWithCaller(LAUNCHER_1, USER_0, () -> {
7163 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
7164 list("ms3", "ms4", "ms5"), HANDLE_USER_0);
7165 });
7166
7167 // Make sure they're pinned.
7168 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7169 assertWith(getCallerShortcuts())
7170 .haveIds("ms1", "ms2", "ms3", "ms4", "ms5")
7171 .selectByIds("ms1", "ms2")
7172 .areAllNotPinned()
7173 .areAllEnabled()
7174
7175 .revertToOriginalList()
7176 .selectByIds("ms3", "ms4", "ms5")
7177 .areAllPinned()
7178 .areAllEnabled();
7179 });
7180
7181 // Update the app.
7182 addManifestShortcutResource(
7183 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7184 R.xml.shortcut_error_4);
7185 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07007186 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onukidf6da042016-06-16 09:51:40 -07007187 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7188
7189 // Make sure 3, 4 and 5 still exist but disabled.
7190 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7191 assertWith(getCallerShortcuts())
7192 .haveIds("ms1", "ms2", "ms3", "ms4", "ms5")
7193 .areAllNotDynamic()
7194 .areAllImmutable()
7195
7196 .selectByIds("ms1", "ms2")
7197 .areAllManifest()
7198 .areAllNotPinned()
7199 .areAllEnabled()
7200
7201 .revertToOriginalList()
7202 .selectByIds("ms3", "ms4", "ms5")
7203 .areAllNotManifest()
7204 .areAllPinned()
7205 .areAllDisabled()
7206
7207 .revertToOriginalList()
7208 .forShortcutWithId("ms1", si -> {
7209 assertEquals(si.getId(), "action1", si.getIntent().getAction());
7210 })
7211 .forShortcutWithId("ms2", si -> {
Makoto Onuki440a1ea2016-07-20 14:21:18 -07007212 // getIntent returns the last one.
7213 assertEquals(si.getId(), "action2_2", si.getIntent().getAction());
Makoto Onukidf6da042016-06-16 09:51:40 -07007214 })
7215 .forShortcutWithId("ms3", si -> {
7216 assertEquals(si.getId(), Intent.ACTION_VIEW, si.getIntent().getAction());
7217 })
7218 .forShortcutWithId("ms4", si -> {
7219 assertEquals(si.getId(), Intent.ACTION_VIEW, si.getIntent().getAction());
7220 })
7221 .forShortcutWithId("ms5", si -> {
7222 assertEquals(si.getId(), "action", si.getIntent().getAction());
7223 });
7224 });
7225 }
7226
Makoto Onuki22fcc682016-05-17 14:52:19 -07007227 public void testManifestShortcuts_checkAllFields() {
7228 mService.handleUnlockUser(USER_0);
7229
7230 // Package 1 updated, which has one valid manifest shortcut and one invalid.
7231 addManifestShortcutResource(
7232 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7233 R.xml.shortcut_5);
7234 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07007235 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki22fcc682016-05-17 14:52:19 -07007236 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7237
7238 // Only the valid one is published.
7239 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
Makoto Onukidf6da042016-06-16 09:51:40 -07007240 assertWith(getCallerShortcuts())
7241 .haveIds("ms1", "ms2", "ms3", "ms4", "ms5")
7242 .areAllManifest()
7243 .areAllImmutable()
7244 .areAllEnabled()
7245 .areAllNotPinned()
7246 .areAllNotDynamic()
Makoto Onuki22fcc682016-05-17 14:52:19 -07007247
Makoto Onukidf6da042016-06-16 09:51:40 -07007248 .forShortcutWithId("ms1", si -> {
7249 assertEquals(R.drawable.icon1, si.getIconResourceId());
7250 assertEquals(new ComponentName(CALLING_PACKAGE_1,
7251 ShortcutActivity.class.getName()),
7252 si.getActivity());
Makoto Onuki22fcc682016-05-17 14:52:19 -07007253
Makoto Onukidf6da042016-06-16 09:51:40 -07007254 assertEquals(R.string.shortcut_title1, si.getTitleResId());
7255 assertEquals("r" + R.string.shortcut_title1, si.getTitleResName());
7256 assertEquals(R.string.shortcut_text1, si.getTextResId());
7257 assertEquals("r" + R.string.shortcut_text1, si.getTextResName());
7258 assertEquals(R.string.shortcut_disabled_message1,
7259 si.getDisabledMessageResourceId());
7260 assertEquals("r" + R.string.shortcut_disabled_message1,
7261 si.getDisabledMessageResName());
Makoto Onuki157b1622016-06-02 16:13:10 -07007262
Makoto Onukidf6da042016-06-16 09:51:40 -07007263 assertEquals(set("android.shortcut.conversation", "android.shortcut.media"),
7264 si.getCategories());
7265 assertEquals("action1", si.getIntent().getAction());
7266 assertEquals(Uri.parse("http://a.b.c/1"), si.getIntent().getData());
7267 })
Makoto Onuki157b1622016-06-02 16:13:10 -07007268
Makoto Onukidf6da042016-06-16 09:51:40 -07007269 .forShortcutWithId("ms2", si -> {
7270 assertEquals("ms2", si.getId());
7271 assertEquals(R.drawable.icon2, si.getIconResourceId());
Makoto Onuki22fcc682016-05-17 14:52:19 -07007272
Makoto Onukidf6da042016-06-16 09:51:40 -07007273 assertEquals(R.string.shortcut_title2, si.getTitleResId());
7274 assertEquals("r" + R.string.shortcut_title2, si.getTitleResName());
7275 assertEquals(R.string.shortcut_text2, si.getTextResId());
7276 assertEquals("r" + R.string.shortcut_text2, si.getTextResName());
7277 assertEquals(R.string.shortcut_disabled_message2,
7278 si.getDisabledMessageResourceId());
7279 assertEquals("r" + R.string.shortcut_disabled_message2,
7280 si.getDisabledMessageResName());
Makoto Onuki22fcc682016-05-17 14:52:19 -07007281
Makoto Onukidf6da042016-06-16 09:51:40 -07007282 assertEquals(set("android.shortcut.conversation"), si.getCategories());
7283 assertEquals("action2", si.getIntent().getAction());
7284 assertEquals(null, si.getIntent().getData());
7285 })
Makoto Onuki157b1622016-06-02 16:13:10 -07007286
Makoto Onukidf6da042016-06-16 09:51:40 -07007287 .forShortcutWithId("ms3", si -> {
7288 assertEquals(0, si.getIconResourceId());
7289 assertEquals(R.string.shortcut_title1, si.getTitleResId());
7290 assertEquals("r" + R.string.shortcut_title1, si.getTitleResName());
Makoto Onuki157b1622016-06-02 16:13:10 -07007291
Makoto Onukidf6da042016-06-16 09:51:40 -07007292 assertEquals(0, si.getTextResId());
7293 assertEquals(null, si.getTextResName());
7294 assertEquals(0, si.getDisabledMessageResourceId());
7295 assertEquals(null, si.getDisabledMessageResName());
Makoto Onuki22fcc682016-05-17 14:52:19 -07007296
Makoto Onukidf6da042016-06-16 09:51:40 -07007297 assertEmpty(si.getCategories());
7298 assertEquals("android.intent.action.VIEW", si.getIntent().getAction());
7299 assertEquals(null, si.getIntent().getData());
7300 })
Makoto Onuki22fcc682016-05-17 14:52:19 -07007301
Makoto Onukidf6da042016-06-16 09:51:40 -07007302 .forShortcutWithId("ms4", si -> {
7303 assertEquals(0, si.getIconResourceId());
7304 assertEquals(R.string.shortcut_title2, si.getTitleResId());
7305 assertEquals("r" + R.string.shortcut_title2, si.getTitleResName());
Makoto Onuki157b1622016-06-02 16:13:10 -07007306
Makoto Onukidf6da042016-06-16 09:51:40 -07007307 assertEquals(0, si.getTextResId());
7308 assertEquals(null, si.getTextResName());
7309 assertEquals(0, si.getDisabledMessageResourceId());
7310 assertEquals(null, si.getDisabledMessageResName());
Makoto Onuki157b1622016-06-02 16:13:10 -07007311
Makoto Onukidf6da042016-06-16 09:51:40 -07007312 assertEquals(set("cat"), si.getCategories());
7313 assertEquals("android.intent.action.VIEW2", si.getIntent().getAction());
7314 assertEquals(null, si.getIntent().getData());
7315 })
7316
7317 .forShortcutWithId("ms5", si -> {
7318 si = getCallerShortcut("ms5");
7319 assertEquals("action", si.getIntent().getAction());
7320 assertEquals("http://www/", si.getIntent().getData().toString());
7321 assertEquals("foo/bar", si.getIntent().getType());
7322 assertEquals(
7323 new ComponentName("abc", ".xyz"), si.getIntent().getComponent());
7324
7325 assertEquals(set("cat1", "cat2"), si.getIntent().getCategories());
7326 assertEquals("value1", si.getIntent().getStringExtra("key1"));
7327 assertEquals("value2", si.getIntent().getStringExtra("key2"));
7328 });
Makoto Onuki157b1622016-06-02 16:13:10 -07007329 });
7330 }
7331
Makoto Onuki4e6cef42016-07-13 16:14:01 -07007332 public void testManifestShortcuts_localeChange() throws InterruptedException {
Makoto Onuki157b1622016-06-02 16:13:10 -07007333 mService.handleUnlockUser(USER_0);
7334
7335 // Package 1 updated, which has one valid manifest shortcut and one invalid.
7336 addManifestShortcutResource(
7337 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7338 R.xml.shortcut_2);
7339 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07007340 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki157b1622016-06-02 16:13:10 -07007341 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7342
7343 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7344 mManager.setDynamicShortcuts(list(makeShortcutWithTitle("s1", "title")));
7345
7346 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
7347 mManager.getManifestShortcuts()))),
7348 "ms1", "ms2");
7349
7350 // check first shortcut.
7351 ShortcutInfo si = getCallerShortcut("ms1");
7352
7353 assertEquals("ms1", si.getId());
7354 assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_title1 + "/en",
7355 si.getTitle());
7356 assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_text1 + "/en",
7357 si.getText());
7358 assertEquals("string-com.android.test.1-user:0-res:"
7359 + R.string.shortcut_disabled_message1 + "/en",
7360 si.getDisabledMessage());
7361 assertEquals(START_TIME, si.getLastChangedTimestamp());
7362
7363 // check another
7364 si = getCallerShortcut("ms2");
7365
7366 assertEquals("ms2", si.getId());
7367 assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_title2 + "/en",
7368 si.getTitle());
7369 assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_text2 + "/en",
7370 si.getText());
7371 assertEquals("string-com.android.test.1-user:0-res:"
7372 + R.string.shortcut_disabled_message2 + "/en",
7373 si.getDisabledMessage());
7374 assertEquals(START_TIME, si.getLastChangedTimestamp());
7375
7376 // Check the dynamic one.
7377 si = getCallerShortcut("s1");
7378
7379 assertEquals("s1", si.getId());
7380 assertEquals("title", si.getTitle());
7381 assertEquals(null, si.getText());
7382 assertEquals(null, si.getDisabledMessage());
7383 assertEquals(START_TIME, si.getLastChangedTimestamp());
7384 });
7385
7386 mInjectedCurrentTimeMillis++;
7387
Makoto Onuki4e6cef42016-07-13 16:14:01 -07007388 // Change the locale and send the broadcast, make sure the launcher gets a callback too.
Makoto Onuki157b1622016-06-02 16:13:10 -07007389 mInjectedLocale = Locale.JAPANESE;
Makoto Onuki4e6cef42016-07-13 16:14:01 -07007390
7391 setCaller(LAUNCHER_1, USER_0);
7392
7393 assertForLauncherCallback(mLauncherApps, () -> {
7394 mService.mReceiver.onReceive(mServiceContext, new Intent(Intent.ACTION_LOCALE_CHANGED));
7395 }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
7396 .haveIds("ms1", "ms2", "s1");
Makoto Onuki157b1622016-06-02 16:13:10 -07007397
7398 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7399 // check first shortcut.
7400 ShortcutInfo si = getCallerShortcut("ms1");
7401
7402 assertEquals("ms1", si.getId());
7403 assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_title1 + "/ja",
7404 si.getTitle());
7405 assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_text1 + "/ja",
7406 si.getText());
7407 assertEquals("string-com.android.test.1-user:0-res:"
7408 + R.string.shortcut_disabled_message1 + "/ja",
7409 si.getDisabledMessage());
7410 assertEquals(START_TIME + 1, si.getLastChangedTimestamp());
7411
7412 // check another
7413 si = getCallerShortcut("ms2");
7414
7415 assertEquals("ms2", si.getId());
7416 assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_title2 + "/ja",
7417 si.getTitle());
7418 assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_text2 + "/ja",
7419 si.getText());
7420 assertEquals("string-com.android.test.1-user:0-res:"
7421 + R.string.shortcut_disabled_message2 + "/ja",
7422 si.getDisabledMessage());
7423 assertEquals(START_TIME + 1, si.getLastChangedTimestamp());
7424
7425 // Check the dynamic one. (locale change shouldn't affect.)
7426 si = getCallerShortcut("s1");
7427
7428 assertEquals("s1", si.getId());
7429 assertEquals("title", si.getTitle());
7430 assertEquals(null, si.getText());
7431 assertEquals(null, si.getDisabledMessage());
7432 assertEquals(START_TIME, si.getLastChangedTimestamp()); // Not changed.
Makoto Onuki22fcc682016-05-17 14:52:19 -07007433 });
7434 }
7435
7436 public void testManifestShortcuts_updateAndDisabled_notPinned() {
7437 mService.handleUnlockUser(USER_0);
7438
7439 // First, just publish a manifest shortcut.
7440 addManifestShortcutResource(
7441 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7442 R.xml.shortcut_1);
7443 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07007444 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki22fcc682016-05-17 14:52:19 -07007445 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7446
7447 // Only the valid one is published.
7448 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7449 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
7450 mManager.getManifestShortcuts()))),
7451 "ms1");
7452 assertEmpty(mManager.getPinnedShortcuts());
7453
7454 // Make sure there's no other dangling shortcuts.
7455 assertShortcutIds(getCallerShortcuts(), "ms1");
7456 });
7457
7458 // Now version up, the manifest shortcut is disabled now.
7459 addManifestShortcutResource(
7460 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7461 R.xml.shortcut_1_disable);
7462 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07007463 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki22fcc682016-05-17 14:52:19 -07007464 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7465
7466 // Because shortcut 1 wasn't pinned, it'll just go away.
7467 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7468 assertEmpty(mManager.getManifestShortcuts());
7469 assertEmpty(mManager.getPinnedShortcuts());
7470
7471 // Make sure there's no other dangling shortcuts.
7472 assertEmpty(getCallerShortcuts());
7473 });
7474 }
7475
7476 public void testManifestShortcuts_updateAndDisabled_pinned() {
7477 mService.handleUnlockUser(USER_0);
7478
7479 // First, just publish a manifest shortcut.
7480 addManifestShortcutResource(
7481 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7482 R.xml.shortcut_1);
7483 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07007484 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki22fcc682016-05-17 14:52:19 -07007485 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7486
7487 // Only the valid one is published.
7488 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7489 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
7490 mManager.getManifestShortcuts()))),
7491 "ms1");
7492 assertEmpty(mManager.getPinnedShortcuts());
7493
7494 // Make sure there's no other dangling shortcuts.
7495 assertShortcutIds(getCallerShortcuts(), "ms1");
7496 });
7497
7498 runWithCaller(LAUNCHER_1, USER_0, () -> {
7499 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms1"), HANDLE_USER_0);
7500 });
7501
7502 // Now upgrade, the manifest shortcut is disabled now.
7503 addManifestShortcutResource(
7504 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7505 R.xml.shortcut_1_disable);
7506 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07007507 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki22fcc682016-05-17 14:52:19 -07007508 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7509
7510 // Because shortcut 1 was pinned, it'll still exist as pinned, but disabled.
7511 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7512 assertEmpty(mManager.getManifestShortcuts());
7513 assertShortcutIds(assertAllNotManifest(assertAllImmutable(assertAllDisabled(
7514 mManager.getPinnedShortcuts()))),
7515 "ms1");
7516
7517 // Make sure the fields are updated.
7518 ShortcutInfo si = getCallerShortcut("ms1");
7519
7520 assertEquals("ms1", si.getId());
7521 assertEquals(R.drawable.icon2, si.getIconResourceId());
7522 assertEquals(R.string.shortcut_title2, si.getTitleResId());
7523 assertEquals(R.string.shortcut_text2, si.getTextResId());
Makoto Onukieddbfec2016-05-31 17:04:34 -07007524 assertEquals(R.string.shortcut_disabled_message2, si.getDisabledMessageResourceId());
Makoto Onuki22fcc682016-05-17 14:52:19 -07007525 assertEquals(Intent.ACTION_VIEW, si.getIntent().getAction());
7526
7527 // Make sure there's no other dangling shortcuts.
7528 assertShortcutIds(getCallerShortcuts(), "ms1");
7529 });
7530 }
7531
7532 public void testManifestShortcuts_duplicateInSingleActivity() {
7533 mService.handleUnlockUser(USER_0);
7534
7535 // The XML has two shortcuts with the same ID.
7536 addManifestShortcutResource(
7537 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7538 R.xml.shortcut_2_duplicate);
7539 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07007540 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki22fcc682016-05-17 14:52:19 -07007541 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7542
7543 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7544 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
7545 mManager.getManifestShortcuts()))),
7546 "ms1");
7547
7548 // Make sure the first one has survived. (the second one has a different title.)
7549 ShortcutInfo si = getCallerShortcut("ms1");
7550 assertEquals(R.string.shortcut_title1, si.getTitleResId());
7551
7552 // Make sure there's no other dangling shortcuts.
7553 assertShortcutIds(getCallerShortcuts(), "ms1");
7554 });
7555 }
7556
7557 public void testManifestShortcuts_duplicateInTwoActivities() {
7558 mService.handleUnlockUser(USER_0);
7559
7560 // ShortcutActivity has shortcut ms1
7561 addManifestShortcutResource(
7562 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7563 R.xml.shortcut_1);
7564
7565 // ShortcutActivity2 has two shortcuts, ms1 and ms2.
7566 addManifestShortcutResource(
7567 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity2.class.getName()),
Makoto Onuki9e1f5592016-06-08 12:30:23 -07007568 R.xml.shortcut_5);
Makoto Onuki22fcc682016-05-17 14:52:19 -07007569 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07007570 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki22fcc682016-05-17 14:52:19 -07007571 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7572
7573 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7574 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
7575 mManager.getManifestShortcuts()))),
Makoto Onuki9e1f5592016-06-08 12:30:23 -07007576 "ms1", "ms2", "ms3", "ms4", "ms5");
Makoto Onuki22fcc682016-05-17 14:52:19 -07007577
7578 // ms1 should belong to ShortcutActivity.
7579 ShortcutInfo si = getCallerShortcut("ms1");
7580 assertEquals(R.string.shortcut_title1, si.getTitleResId());
7581 assertEquals(new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7582 si.getActivity());
Makoto Onuki9e1f5592016-06-08 12:30:23 -07007583 assertEquals(0, si.getRank());
Makoto Onuki22fcc682016-05-17 14:52:19 -07007584
7585 // ms2 should belong to ShortcutActivity*2*.
7586 si = getCallerShortcut("ms2");
7587 assertEquals(R.string.shortcut_title2, si.getTitleResId());
7588 assertEquals(new ComponentName(CALLING_PACKAGE_1, ShortcutActivity2.class.getName()),
7589 si.getActivity());
7590
Makoto Onuki9e1f5592016-06-08 12:30:23 -07007591 // Also check the ranks
7592 assertWith(getCallerShortcuts()).selectManifest()
7593 .selectByActivity(
7594 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()))
7595 .haveRanksInOrder("ms1");
7596 assertWith(getCallerShortcuts()).selectManifest()
7597 .selectByActivity(
7598 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity2.class.getName()))
7599 .haveRanksInOrder("ms2", "ms3", "ms4", "ms5");
7600
Makoto Onuki22fcc682016-05-17 14:52:19 -07007601 // Make sure there's no other dangling shortcuts.
Makoto Onuki9e1f5592016-06-08 12:30:23 -07007602 assertShortcutIds(getCallerShortcuts(), "ms1", "ms2", "ms3", "ms4", "ms5");
Makoto Onuki22fcc682016-05-17 14:52:19 -07007603 });
7604 }
7605
7606 /**
7607 * Manifest shortcuts cannot override shortcuts that were published via the APIs.
7608 */
7609 public void testManifestShortcuts_cannotOverrideNonManifest() {
7610 mService.handleUnlockUser(USER_0);
7611
7612 // Create a non-pinned dynamic shortcut and a non-dynamic pinned shortcut.
7613
7614 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7615 mManager.setDynamicShortcuts(list(
7616 makeShortcut("ms1", "title1",
Makoto Onuki51ab2b32016-06-02 11:03:51 -07007617 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
Makoto Onuki99302b52017-03-29 12:42:26 -07007618 /* icon */ null, new Intent("action1"), /* rank */ 0),
Makoto Onuki22fcc682016-05-17 14:52:19 -07007619 makeShortcut("ms2", "title2",
Makoto Onuki51ab2b32016-06-02 11:03:51 -07007620 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
Makoto Onuki99302b52017-03-29 12:42:26 -07007621 /* icon */ null, new Intent("action1"), /* rank */ 0)));
Makoto Onuki22fcc682016-05-17 14:52:19 -07007622 });
7623
7624 runWithCaller(LAUNCHER_1, USER_0, () -> {
7625 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms2"), HANDLE_USER_0);
7626 });
7627
7628 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7629 mManager.removeDynamicShortcuts(list("ms2"));
7630
7631 assertShortcutIds(mManager.getDynamicShortcuts(), "ms1");
7632 assertShortcutIds(mManager.getPinnedShortcuts(), "ms2");
7633 assertEmpty(mManager.getManifestShortcuts());
7634 });
7635
7636 // Then update the app with 5 manifest shortcuts.
7637 // Make sure "ms1" and "ms2" won't be replaced.
7638 addManifestShortcutResource(
7639 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7640 R.xml.shortcut_5);
7641 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07007642 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki22fcc682016-05-17 14:52:19 -07007643 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7644
7645 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7646 assertShortcutIds(assertAllNotManifest(mManager.getDynamicShortcuts()), "ms1");
7647 assertShortcutIds(assertAllNotManifest(mManager.getPinnedShortcuts()), "ms2");
7648 assertShortcutIds(assertAllManifest(mManager.getManifestShortcuts()),
7649 "ms3", "ms4", "ms5");
7650
7651 // ms1 and ms2 shouold keep the original title.
7652 ShortcutInfo si = getCallerShortcut("ms1");
7653 assertEquals("title1", si.getTitle());
7654
7655 si = getCallerShortcut("ms2");
7656 assertEquals("title2", si.getTitle());
7657 });
7658 }
7659
Makoto Onuki51ab2b32016-06-02 11:03:51 -07007660 protected void checkManifestShortcuts_immutable_verify() {
Makoto Onuki22fcc682016-05-17 14:52:19 -07007661 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7662 assertShortcutIds(assertAllNotManifest(assertAllEnabled(
7663 mManager.getDynamicShortcuts())),
7664 "s1");
7665 assertShortcutIds(assertAllManifest(assertAllEnabled(
7666 mManager.getManifestShortcuts())),
7667 "ms1");
7668 assertShortcutIds(assertAllNotManifest(assertAllDisabled(
7669 mManager.getPinnedShortcuts())),
7670 "ms2");
7671
7672 assertEquals("t1", getCallerShortcut("s1").getTitle());
7673
7674 // Make sure there are no other shortcuts.
7675 assertShortcutIds(getCallerShortcuts(), "s1", "ms1", "ms2");
7676 });
7677 }
7678
7679 /**
7680 * Make sure the APIs won't work on manifest shortcuts.
7681 */
7682 public void testManifestShortcuts_immutable() {
7683 mService.handleUnlockUser(USER_0);
7684
7685 // Create a non-pinned manifest shortcut, a pinned shortcut that was originally
7686 // a manifest shortcut, as well as a dynamic shortcut.
7687
7688 addManifestShortcutResource(
7689 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7690 R.xml.shortcut_2);
7691 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07007692 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki22fcc682016-05-17 14:52:19 -07007693 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7694
7695 runWithCaller(LAUNCHER_1, USER_0, () -> {
7696 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms2"), HANDLE_USER_0);
7697 });
7698
7699 addManifestShortcutResource(
7700 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7701 R.xml.shortcut_1);
7702 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07007703 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki22fcc682016-05-17 14:52:19 -07007704 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7705
7706 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7707 mManager.addDynamicShortcuts(list(makeShortcutWithTitle("s1", "t1")));
7708 });
7709
7710 checkManifestShortcuts_immutable_verify();
7711
7712 // Note that even though the first argument is not immutable and only the second one
7713 // is immutable, the first argument should not be executed either.
7714
7715 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7716 assertCannotUpdateImmutable(() -> {
7717 mManager.setDynamicShortcuts(list(makeShortcut("xx"), makeShortcut("ms1")));
7718 });
7719 assertCannotUpdateImmutable(() -> {
7720 mManager.setDynamicShortcuts(list(makeShortcut("xx"), makeShortcut("ms2")));
7721 });
7722 });
7723 checkManifestShortcuts_immutable_verify();
7724
7725 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7726 assertCannotUpdateImmutable(() -> {
7727 mManager.addDynamicShortcuts(list(makeShortcut("xx"), makeShortcut("ms1")));
7728 });
7729 assertCannotUpdateImmutable(() -> {
7730 mManager.addDynamicShortcuts(list(makeShortcut("xx"), makeShortcut("ms2")));
7731 });
7732 });
7733 checkManifestShortcuts_immutable_verify();
7734
7735
7736 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7737 assertCannotUpdateImmutable(() -> {
7738 mManager.updateShortcuts(list(makeShortcut("s1"), makeShortcut("ms1")));
7739 });
7740 assertCannotUpdateImmutable(() -> {
7741 mManager.updateShortcuts(list(makeShortcut("s1"), makeShortcut("ms2")));
7742 });
7743 });
7744 checkManifestShortcuts_immutable_verify();
7745
7746 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7747 assertCannotUpdateImmutable(() -> {
7748 mManager.removeDynamicShortcuts(list("s1", "ms1"));
7749 });
7750 assertCannotUpdateImmutable(() -> {
7751 mManager.removeDynamicShortcuts(list("s2", "ms2"));
7752 });
7753 });
7754 checkManifestShortcuts_immutable_verify();
7755
7756 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7757 assertCannotUpdateImmutable(() -> {
7758 mManager.disableShortcuts(list("s1", "ms1"));
7759 });
7760 });
7761 checkManifestShortcuts_immutable_verify();
7762
7763 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7764 assertCannotUpdateImmutable(() -> {
7765 mManager.enableShortcuts(list("s1", "ms2"));
7766 });
7767 });
7768 checkManifestShortcuts_immutable_verify();
7769 }
Makoto Onuki7001a612016-05-27 13:24:28 -07007770
Makoto Onuki51ab2b32016-06-02 11:03:51 -07007771
Makoto Onuki7001a612016-05-27 13:24:28 -07007772 /**
7773 * Make sure the APIs won't work on manifest shortcuts.
7774 */
7775 public void testManifestShortcuts_tooMany() {
7776 // Change the max number of shortcuts.
7777 mService.updateConfigurationLocked(ConfigConstants.KEY_MAX_SHORTCUTS + "=3");
7778
7779 mService.handleUnlockUser(USER_0);
7780
7781 addManifestShortcutResource(
7782 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7783 R.xml.shortcut_5);
7784 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07007785 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki7001a612016-05-27 13:24:28 -07007786 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7787
7788 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7789 // Only the first 3 should be published.
7790 assertShortcutIds(mManager.getManifestShortcuts(), "ms1", "ms2", "ms3");
7791 });
7792 }
7793
7794 public void testMaxShortcutCount_set() {
7795 // Change the max number of shortcuts.
7796 mService.updateConfigurationLocked(ConfigConstants.KEY_MAX_SHORTCUTS + "=3");
7797
7798 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7799 final ComponentName a1 = new ComponentName(mClientContext, ShortcutActivity.class);
7800 final ComponentName a2 = new ComponentName(mClientContext, ShortcutActivity2.class);
7801 final ShortcutInfo s1_1 = makeShortcutWithActivity("s11", a1);
7802 final ShortcutInfo s1_2 = makeShortcutWithActivity("s12", a1);
7803 final ShortcutInfo s1_3 = makeShortcutWithActivity("s13", a1);
7804 final ShortcutInfo s1_4 = makeShortcutWithActivity("s14", a1);
7805 final ShortcutInfo s1_5 = makeShortcutWithActivity("s15", a1);
7806 final ShortcutInfo s1_6 = makeShortcutWithActivity("s16", a1);
7807 final ShortcutInfo s2_1 = makeShortcutWithActivity("s21", a2);
7808 final ShortcutInfo s2_2 = makeShortcutWithActivity("s22", a2);
7809 final ShortcutInfo s2_3 = makeShortcutWithActivity("s23", a2);
7810 final ShortcutInfo s2_4 = makeShortcutWithActivity("s24", a2);
7811
7812 // 3 shortcuts for 2 activities -> okay
7813 mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3, s2_1, s2_2, s2_3));
7814 assertShortcutIds(mManager.getDynamicShortcuts(),
7815 "s11", "s12", "s13", "s21", "s22", "s23");
7816
7817 mManager.removeAllDynamicShortcuts();
7818
7819 // 4 shortcut for activity 1 -> too many.
7820 assertDynamicShortcutCountExceeded(() -> {
7821 mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3, s1_4, s2_1, s2_2, s2_3));
7822 });
7823 assertEmpty(mManager.getDynamicShortcuts());
7824
7825 // 4 shortcut for activity 2 -> too many.
7826 assertDynamicShortcutCountExceeded(() -> {
7827 mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3, s2_1, s2_2, s2_3, s2_4));
7828 });
7829 assertEmpty(mManager.getDynamicShortcuts());
7830
7831 // First, set 3. Then set 4, which should be ignored.
7832 mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3));
7833 assertShortcutIds(mManager.getDynamicShortcuts(),
7834 "s11", "s12", "s13");
7835 assertDynamicShortcutCountExceeded(() -> {
7836 mManager.setDynamicShortcuts(list(s2_1, s2_2, s2_3, s2_4));
7837 });
7838 assertShortcutIds(mManager.getDynamicShortcuts(),
7839 "s11", "s12", "s13");
7840
7841 // Set will remove the old dynamic set, unlike add, so the following should pass.
7842 mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3));
7843 assertShortcutIds(mManager.getDynamicShortcuts(),
7844 "s11", "s12", "s13");
7845 mManager.setDynamicShortcuts(list(s1_4, s1_5, s1_6));
7846 assertShortcutIds(mManager.getDynamicShortcuts(),
7847 "s14", "s15", "s16");
7848
7849 // Now, test with 2 manifest shortcuts.
7850 mManager.removeAllDynamicShortcuts();
7851 addManifestShortcutResource(
7852 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7853 R.xml.shortcut_2);
7854 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07007855 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki7001a612016-05-27 13:24:28 -07007856 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7857 assertEquals(2, mManager.getManifestShortcuts().size());
7858
7859 // Setting 1 to activity 1 will work.
7860 mManager.setDynamicShortcuts(list(s1_1, s2_1, s2_2, s2_3));
7861 assertShortcutIds(mManager.getDynamicShortcuts(),
7862 "s11", "s21", "s22", "s23");
7863 assertEquals(2, mManager.getManifestShortcuts().size());
7864
7865 // But setting 2 will not.
7866 mManager.removeAllDynamicShortcuts();
7867 assertDynamicShortcutCountExceeded(() -> {
7868 mManager.setDynamicShortcuts(list(s1_1, s1_2, s2_1, s2_2, s2_3));
7869 });
7870 assertEmpty(mManager.getDynamicShortcuts());
7871 assertEquals(2, mManager.getManifestShortcuts().size());
7872 });
7873 }
7874
7875 public void testMaxShortcutCount_add() {
7876 // Change the max number of shortcuts.
7877 mService.updateConfigurationLocked(ConfigConstants.KEY_MAX_SHORTCUTS + "=3");
7878
7879 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7880 final ComponentName a1 = new ComponentName(mClientContext, ShortcutActivity.class);
7881 final ComponentName a2 = new ComponentName(mClientContext, ShortcutActivity2.class);
7882 final ShortcutInfo s1_1 = makeShortcutWithActivity("s11", a1);
7883 final ShortcutInfo s1_2 = makeShortcutWithActivity("s12", a1);
7884 final ShortcutInfo s1_3 = makeShortcutWithActivity("s13", a1);
7885 final ShortcutInfo s1_4 = makeShortcutWithActivity("s14", a1);
7886 final ShortcutInfo s2_1 = makeShortcutWithActivity("s21", a2);
7887 final ShortcutInfo s2_2 = makeShortcutWithActivity("s22", a2);
7888 final ShortcutInfo s2_3 = makeShortcutWithActivity("s23", a2);
7889 final ShortcutInfo s2_4 = makeShortcutWithActivity("s24", a2);
7890
7891 // 3 shortcuts for 2 activities -> okay
7892 mManager.addDynamicShortcuts(list(s1_1, s1_2, s1_3, s2_1, s2_2, s2_3));
7893 assertShortcutIds(mManager.getDynamicShortcuts(),
7894 "s11", "s12", "s13", "s21", "s22", "s23");
7895
7896 mManager.removeAllDynamicShortcuts();
Makoto Onuki7001a612016-05-27 13:24:28 -07007897
7898 // 4 shortcut for activity 1 -> too many.
7899 assertDynamicShortcutCountExceeded(() -> {
7900 mManager.addDynamicShortcuts(list(s1_1, s1_2, s1_3, s1_4, s2_1, s2_2, s2_3));
7901 });
7902 assertEmpty(mManager.getDynamicShortcuts());
7903
7904 // 4 shortcut for activity 2 -> too many.
7905 assertDynamicShortcutCountExceeded(() -> {
7906 mManager.addDynamicShortcuts(list(s1_1, s1_2, s1_3, s2_1, s2_2, s2_3, s2_4));
7907 });
7908 assertEmpty(mManager.getDynamicShortcuts());
7909
7910 // First, set 3. Then add 1 more, which should be ignored.
7911 mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3));
7912 assertShortcutIds(mManager.getDynamicShortcuts(),
7913 "s11", "s12", "s13");
7914 assertDynamicShortcutCountExceeded(() -> {
7915 mManager.addDynamicShortcuts(list(s1_4, s2_1));
7916 });
7917 assertShortcutIds(mManager.getDynamicShortcuts(),
7918 "s11", "s12", "s13");
7919
7920 // Update existing one, which should work.
7921 mManager.addDynamicShortcuts(list(makeShortcutWithActivityAndTitle(
7922 "s11", a1, "xxx"), s2_1));
7923 assertShortcutIds(mManager.getDynamicShortcuts(),
7924 "s11", "s12", "s13", "s21");
7925 assertEquals("xxx", getCallerShortcut("s11").getTitle());
7926
7927 // Make sure pinned shortcuts won't affect.
7928 // - Pin s11 - s13, and remove all dynamic.
7929 runWithCaller(LAUNCHER_1, USER_0, () -> {
7930 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s11", "s12", "s13"),
7931 HANDLE_USER_0);
7932 });
7933 mManager.removeAllDynamicShortcuts();
7934
7935 assertEmpty(mManager.getDynamicShortcuts());
7936 assertShortcutIds(mManager.getPinnedShortcuts(),
7937 "s11", "s12", "s13");
7938
7939 // Then add dynamic.
7940 mManager.addDynamicShortcuts(list(s1_4, s2_1, s2_2, s2_3));
7941
7942 assertShortcutIds(mManager.getDynamicShortcuts(),
7943 "s14", "s21", "s22", "s23");
7944 assertShortcutIds(mManager.getPinnedShortcuts(),
7945 "s11", "s12", "s13");
7946
7947 // Adding "s11" and "s12" back, should work
7948 mManager.addDynamicShortcuts(list(s1_1, s1_2));
7949
7950 assertShortcutIds(mManager.getDynamicShortcuts(),
7951 "s14", "s11", "s12", "s21", "s22", "s23");
7952 assertShortcutIds(mManager.getPinnedShortcuts(),
7953 "s11", "s12", "s13");
7954
7955 // Adding back s13 doesn't work.
7956 assertDynamicShortcutCountExceeded(() -> {
7957 mManager.addDynamicShortcuts(list(s1_3));
7958 });
7959
7960 assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a1),
7961 "s11", "s12", "s14");
7962 assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a2),
7963 "s21", "s22", "s23");
7964
7965 // Now swap the activities.
7966 mManager.updateShortcuts(list(
7967 makeShortcutWithActivity("s11", a2),
7968 makeShortcutWithActivity("s21", a1)));
7969
7970 assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a1),
7971 "s21", "s12", "s14");
7972 assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a2),
7973 "s11", "s22", "s23");
7974
7975 // Now, test with 2 manifest shortcuts.
7976 mManager.removeAllDynamicShortcuts();
7977 addManifestShortcutResource(
7978 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7979 R.xml.shortcut_2);
7980 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07007981 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki7001a612016-05-27 13:24:28 -07007982 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7983
7984 assertEquals(2, mManager.getManifestShortcuts().size());
7985
7986 // Adding one shortcut to activity 1 works fine.
7987 mManager.addDynamicShortcuts(list(s1_1, s2_1, s2_2, s2_3));
7988 assertShortcutIds(mManager.getDynamicShortcuts(),
7989 "s11", "s21", "s22", "s23");
7990 assertEquals(2, mManager.getManifestShortcuts().size());
7991
7992 // But adding one more doesn't.
7993 assertDynamicShortcutCountExceeded(() -> {
7994 mManager.addDynamicShortcuts(list(s1_4, s2_1));
7995 });
7996 assertShortcutIds(mManager.getDynamicShortcuts(),
7997 "s11", "s21", "s22", "s23");
7998 assertEquals(2, mManager.getManifestShortcuts().size());
7999 });
8000 }
8001
8002 public void testMaxShortcutCount_update() {
8003 // Change the max number of shortcuts.
8004 mService.updateConfigurationLocked(ConfigConstants.KEY_MAX_SHORTCUTS + "=3");
8005
8006 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
8007 final ComponentName a1 = new ComponentName(mClientContext, ShortcutActivity.class);
8008 final ComponentName a2 = new ComponentName(mClientContext, ShortcutActivity2.class);
8009 final ShortcutInfo s1_1 = makeShortcutWithActivity("s11", a1);
8010 final ShortcutInfo s1_2 = makeShortcutWithActivity("s12", a1);
8011 final ShortcutInfo s1_3 = makeShortcutWithActivity("s13", a1);
8012 final ShortcutInfo s1_4 = makeShortcutWithActivity("s14", a1);
8013 final ShortcutInfo s1_5 = makeShortcutWithActivity("s15", a1);
8014 final ShortcutInfo s2_1 = makeShortcutWithActivity("s21", a2);
8015 final ShortcutInfo s2_2 = makeShortcutWithActivity("s22", a2);
8016 final ShortcutInfo s2_3 = makeShortcutWithActivity("s23", a2);
8017 final ShortcutInfo s2_4 = makeShortcutWithActivity("s24", a2);
8018
8019 // 3 shortcuts for 2 activities -> okay
8020 mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3, s2_1, s2_2, s2_3));
8021 assertShortcutIds(mManager.getDynamicShortcuts(),
8022 "s11", "s12", "s13", "s21", "s22", "s23");
8023
8024 // Trying to move s11 from a1 to a2 should fail.
8025 assertDynamicShortcutCountExceeded(() -> {
8026 mManager.updateShortcuts(list(makeShortcutWithActivity("s11", a2)));
8027 });
8028 assertShortcutIds(mManager.getDynamicShortcuts(),
8029 "s11", "s12", "s13", "s21", "s22", "s23");
8030
8031 // Trying to move s21 from a2 to a1 should also fail.
8032 assertDynamicShortcutCountExceeded(() -> {
8033 mManager.updateShortcuts(list(makeShortcutWithActivity("s21", a1)));
8034 });
8035 assertShortcutIds(mManager.getDynamicShortcuts(),
8036 "s11", "s12", "s13", "s21", "s22", "s23");
8037
8038 // But, if we do these two at the same time, it should work.
8039 mManager.updateShortcuts(list(
8040 makeShortcutWithActivity("s11", a2),
8041 makeShortcutWithActivity("s21", a1)));
8042 assertShortcutIds(mManager.getDynamicShortcuts(),
8043 "s11", "s12", "s13", "s21", "s22", "s23");
8044 assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a1),
8045 "s21", "s12", "s13");
8046 assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a2),
8047 "s11", "s22", "s23");
8048
8049 // Then reset.
8050 mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3, s2_1, s2_2, s2_3));
8051 assertShortcutIds(mManager.getDynamicShortcuts(),
8052 "s11", "s12", "s13", "s21", "s22", "s23");
8053
8054 // Pin some to have more shortcuts for a1.
8055 runWithCaller(LAUNCHER_1, USER_0, () -> {
8056 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s11", "s12", "s13"),
8057 HANDLE_USER_0);
8058 });
8059 mManager.setDynamicShortcuts(list(s1_4, s1_5, s2_1, s2_2, s2_3));
8060 assertShortcutIds(mManager.getDynamicShortcuts(),
8061 "s14", "s15", "s21", "s22", "s23");
8062 assertShortcutIds(mManager.getPinnedShortcuts(),
8063 "s11", "s12", "s13");
8064
8065 // a1 already has 2 dynamic shortcuts (and 3 pinned shortcuts that used to belong on it)
8066 // But that doesn't matter for update -- the following should still work.
8067 mManager.updateShortcuts(list(
8068 makeShortcutWithActivityAndTitle("s11", a1, "xxx1"),
8069 makeShortcutWithActivityAndTitle("s12", a1, "xxx2"),
8070 makeShortcutWithActivityAndTitle("s13", a1, "xxx3"),
8071 makeShortcutWithActivityAndTitle("s14", a1, "xxx4"),
8072 makeShortcutWithActivityAndTitle("s15", a1, "xxx5")));
8073 // All the shortcuts should still exist they all belong on same activities,
8074 // with the updated titles.
8075 assertShortcutIds(mManager.getDynamicShortcuts(),
8076 "s14", "s15", "s21", "s22", "s23");
8077 assertShortcutIds(mManager.getPinnedShortcuts(),
8078 "s11", "s12", "s13");
8079
8080 assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a1),
8081 "s14", "s15");
8082 assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a2),
8083 "s21", "s22", "s23");
8084
8085 assertEquals("xxx1", getCallerShortcut("s11").getTitle());
8086 assertEquals("xxx2", getCallerShortcut("s12").getTitle());
8087 assertEquals("xxx3", getCallerShortcut("s13").getTitle());
8088 assertEquals("xxx4", getCallerShortcut("s14").getTitle());
8089 assertEquals("xxx5", getCallerShortcut("s15").getTitle());
8090 });
8091 }
8092
8093 public void testShortcutsPushedOutByManifest() {
8094 // Change the max number of shortcuts.
8095 mService.updateConfigurationLocked(ConfigConstants.KEY_MAX_SHORTCUTS + "=3");
8096
8097 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
8098 final ComponentName a1 = new ComponentName(mClientContext, ShortcutActivity.class);
8099 final ComponentName a2 = new ComponentName(mClientContext, ShortcutActivity2.class);
8100 final ShortcutInfo s1_1 = makeShortcutWithActivityAndRank("s11", a1, 4);
8101 final ShortcutInfo s1_2 = makeShortcutWithActivityAndRank("s12", a1, 3);
8102 final ShortcutInfo s1_3 = makeShortcutWithActivityAndRank("s13", a1, 2);
8103 final ShortcutInfo s1_4 = makeShortcutWithActivityAndRank("s14", a1, 1);
8104 final ShortcutInfo s1_5 = makeShortcutWithActivityAndRank("s15", a1, 0);
8105 final ShortcutInfo s2_1 = makeShortcutWithActivityAndRank("s21", a2, 0);
8106 final ShortcutInfo s2_2 = makeShortcutWithActivityAndRank("s22", a2, 1);
8107 final ShortcutInfo s2_3 = makeShortcutWithActivityAndRank("s23", a2, 2);
8108 final ShortcutInfo s2_4 = makeShortcutWithActivityAndRank("s24", a2, 3);
8109 final ShortcutInfo s2_5 = makeShortcutWithActivityAndRank("s25", a2, 4);
8110
8111 // Initial state.
8112 mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3, s2_1, s2_2, s2_3));
8113 runWithCaller(LAUNCHER_1, USER_0, () -> {
8114 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s11", "s12", "s21", "s22"),
8115 HANDLE_USER_0);
8116 });
8117 mManager.setDynamicShortcuts(list(s1_2, s1_3, s1_4, s2_2, s2_3, s2_4));
8118 assertShortcutIds(assertAllEnabled(mManager.getDynamicShortcuts()),
8119 "s12", "s13", "s14",
8120 "s22", "s23", "s24");
8121 assertShortcutIds(assertAllEnabled(mManager.getPinnedShortcuts()),
8122 "s11", "s12",
8123 "s21", "s22");
8124
8125 // Add 1 manifest shortcut to a1.
8126 addManifestShortcutResource(
8127 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
8128 R.xml.shortcut_1);
8129 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07008130 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki7001a612016-05-27 13:24:28 -07008131 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
8132 assertEquals(1, mManager.getManifestShortcuts().size());
8133
8134 // s12 removed.
8135 assertShortcutIds(assertAllEnabled(mManager.getDynamicShortcuts()),
8136 "s13", "s14",
8137 "s22", "s23", "s24");
8138 assertShortcutIds(assertAllEnabled(mManager.getPinnedShortcuts()),
8139 "s11", "s12",
8140 "s21", "s22");
8141
8142 // Add more manifest shortcuts.
8143 addManifestShortcutResource(
8144 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
8145 R.xml.shortcut_2);
8146 addManifestShortcutResource(
8147 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity2.class.getName()),
8148 R.xml.shortcut_1_alt);
8149 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07008150 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki7001a612016-05-27 13:24:28 -07008151 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
8152 assertEquals(3, mManager.getManifestShortcuts().size());
8153
8154 // Note the ones with the highest rank values (== least important) will be removed.
8155 assertShortcutIds(assertAllEnabled(mManager.getDynamicShortcuts()),
8156 "s14",
8157 "s22", "s23");
8158 assertShortcutIds(assertAllEnabled(mManager.getPinnedShortcuts()),
8159 "s11", "s12",
8160 "s21", "s22");
8161
8162 // Add more manifest shortcuts.
8163 addManifestShortcutResource(
8164 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
8165 R.xml.shortcut_2);
8166 addManifestShortcutResource(
8167 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity2.class.getName()),
8168 R.xml.shortcut_5_alt); // manifest has 5, but max is 3, so a2 will have 3.
8169 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07008170 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki7001a612016-05-27 13:24:28 -07008171 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
8172 assertEquals(5, mManager.getManifestShortcuts().size());
8173
8174 assertShortcutIds(assertAllEnabled(mManager.getDynamicShortcuts()),
8175 "s14" // a1 has 1 dynamic
8176 ); // a2 has no dynamic
8177 assertShortcutIds(assertAllEnabled(mManager.getPinnedShortcuts()),
8178 "s11", "s12",
8179 "s21", "s22");
8180
8181 // Update, no manifest shortucts. This doesn't affect anything.
8182 addManifestShortcutResource(
8183 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
8184 R.xml.shortcut_0);
8185 addManifestShortcutResource(
8186 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity2.class.getName()),
8187 R.xml.shortcut_0);
8188 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07008189 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki7001a612016-05-27 13:24:28 -07008190 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
8191 assertEquals(0, mManager.getManifestShortcuts().size());
8192
8193 assertShortcutIds(assertAllEnabled(mManager.getDynamicShortcuts()),
8194 "s14");
8195 assertShortcutIds(assertAllEnabled(mManager.getPinnedShortcuts()),
8196 "s11", "s12",
8197 "s21", "s22");
8198 });
8199 }
Makoto Onukibf563b62017-05-04 10:25:30 -07008200
8201 public void testReturnedByServer() {
8202 // Package 1 updated, with manifest shortcuts.
8203 addManifestShortcutResource(
8204 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
8205 R.xml.shortcut_1);
8206 updatePackageVersion(CALLING_PACKAGE_1, 1);
8207 mService.mPackageMonitor.onReceive(getTestContext(),
8208 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
8209
8210 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
8211 assertWith(mManager.getManifestShortcuts())
8212 .haveIds("ms1")
8213 .forAllShortcuts(si -> assertTrue(si.isReturnedByServer()));
8214
8215 assertTrue(mManager.setDynamicShortcuts(list(makeShortcut("s1"))));
8216
8217 assertWith(mManager.getDynamicShortcuts())
8218 .haveIds("s1")
8219 .forAllShortcuts(si -> assertTrue(si.isReturnedByServer()));
8220 });
8221
8222 // Pin them.
8223 runWithCaller(LAUNCHER_1, USER_0, () -> {
8224 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
8225 list("ms1", "s1"), getCallingUser());
8226 assertWith(getShortcutAsLauncher(USER_0))
8227 .haveIds("ms1", "s1")
8228 .forAllShortcuts(si -> assertTrue(si.isReturnedByServer()));
8229 });
8230
8231 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
8232 assertWith(mManager.getPinnedShortcuts())
8233 .haveIds("ms1", "s1")
8234 .forAllShortcuts(si -> assertTrue(si.isReturnedByServer()));
8235 });
8236
8237 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
8238 // This shows a warning log, but should still work.
8239 assertTrue(mManager.setDynamicShortcuts(mManager.getDynamicShortcuts()));
8240
8241 assertWith(mManager.getDynamicShortcuts())
8242 .haveIds("s1")
8243 .forAllShortcuts(si -> assertTrue(si.isReturnedByServer()));
8244 });
8245 }
Tony Maked6ef622017-12-07 16:36:16 +00008246
8247 public void testIsForegroundDefaultLauncher_true() {
8248 final ComponentName defaultLauncher = new ComponentName("default", "launcher");
8249 final int uid = 1024;
8250
8251 setDefaultLauncher(UserHandle.USER_SYSTEM, defaultLauncher);
8252 makeUidForeground(uid);
8253
8254 assertTrue(mInternal.isForegroundDefaultLauncher("default", uid));
8255 }
8256
8257
8258 public void testIsForegroundDefaultLauncher_defaultButNotForeground() {
8259 final ComponentName defaultLauncher = new ComponentName("default", "launcher");
8260 final int uid = 1024;
8261
8262 setDefaultLauncher(UserHandle.USER_SYSTEM, defaultLauncher);
8263 makeUidBackground(uid);
8264
8265 assertFalse(mInternal.isForegroundDefaultLauncher("default", uid));
8266 }
8267
8268 public void testIsForegroundDefaultLauncher_foregroundButNotDefault() {
8269 final ComponentName defaultLauncher = new ComponentName("default", "launcher");
8270 final int uid = 1024;
8271
8272 setDefaultLauncher(UserHandle.USER_SYSTEM, defaultLauncher);
8273 makeUidForeground(uid);
8274
8275 assertFalse(mInternal.isForegroundDefaultLauncher("another", uid));
8276 }
Mehdi Alizadeh32774622018-11-05 17:32:01 -08008277
8278 public void testParseShareTargetsFromManifest() {
8279 // These values must exactly match the content of shortcuts_share_targets.xml resource
8280 List<ShareTargetInfo> expectedValues = new ArrayList<>();
8281 expectedValues.add(new ShareTargetInfo(
8282 new ShareTargetInfo.TargetData[]{new ShareTargetInfo.TargetData(
8283 "http", "www.google.com", "1234", "somePath", "somePathPattern",
8284 "somePathPrefix", "text/plain")}, "com.test.directshare.TestActivity1",
8285 new String[]{"com.test.category.CATEGORY1", "com.test.category.CATEGORY2"}));
8286 expectedValues.add(new ShareTargetInfo(new ShareTargetInfo.TargetData[]{
8287 new ShareTargetInfo.TargetData(null, null, null, null, null, null, "video/mp4"),
8288 new ShareTargetInfo.TargetData("content", null, null, null, null, null, "video/*")},
8289 "com.test.directshare.TestActivity5",
8290 new String[]{"com.test.category.CATEGORY5", "com.test.category.CATEGORY6"}));
8291
8292 addManifestShortcutResource(
8293 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
8294 R.xml.shortcut_share_targets);
8295 updatePackageVersion(CALLING_PACKAGE_1, 1);
8296 mService.mPackageMonitor.onReceive(getTestContext(),
8297 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
8298
8299 List<ShareTargetInfo> shareTargets = getCallerShareTargets();
8300
8301 assertNotNull(shareTargets);
8302 assertEquals(expectedValues.size(), shareTargets.size());
8303
8304 for (int i = 0; i < expectedValues.size(); i++) {
8305 ShareTargetInfo expected = expectedValues.get(i);
8306 ShareTargetInfo actual = shareTargets.get(i);
8307
8308 assertEquals(expected.mTargetData.length, actual.mTargetData.length);
8309 for (int j = 0; j < expected.mTargetData.length; j++) {
8310 assertEquals(expected.mTargetData[j].mScheme, actual.mTargetData[j].mScheme);
8311 assertEquals(expected.mTargetData[j].mHost, actual.mTargetData[j].mHost);
8312 assertEquals(expected.mTargetData[j].mPort, actual.mTargetData[j].mPort);
8313 assertEquals(expected.mTargetData[j].mPath, actual.mTargetData[j].mPath);
8314 assertEquals(expected.mTargetData[j].mPathPrefix,
8315 actual.mTargetData[j].mPathPrefix);
8316 assertEquals(expected.mTargetData[j].mPathPattern,
8317 actual.mTargetData[j].mPathPattern);
8318 assertEquals(expected.mTargetData[j].mMimeType, actual.mTargetData[j].mMimeType);
8319 }
8320
8321 assertEquals(expected.mTargetClass, actual.mTargetClass);
8322
8323 assertEquals(expected.mCategories.length, actual.mCategories.length);
8324 for (int j = 0; j < expected.mCategories.length; j++) {
8325 assertEquals(expected.mCategories[j], actual.mCategories[j]);
8326 }
8327 }
8328 }
Mehdi Alizadehabec3192019-06-27 18:06:15 -07008329
8330 public void testShareTargetInfo_saveToXml() throws IOException, XmlPullParserException {
8331 List<ShareTargetInfo> expectedValues = new ArrayList<>();
8332 expectedValues.add(new ShareTargetInfo(
8333 new ShareTargetInfo.TargetData[]{new ShareTargetInfo.TargetData(
8334 "http", "www.google.com", "1234", "somePath", "somePathPattern",
8335 "somePathPrefix", "text/plain")}, "com.test.directshare.TestActivity1",
8336 new String[]{"com.test.category.CATEGORY1", "com.test.category.CATEGORY2"}));
8337 expectedValues.add(new ShareTargetInfo(new ShareTargetInfo.TargetData[]{
8338 new ShareTargetInfo.TargetData(null, null, null, null, null, null, "video/mp4"),
8339 new ShareTargetInfo.TargetData("content", null, null, null, null, null, "video/*")},
8340 "com.test.directshare.TestActivity5",
8341 new String[]{"com.test.category.CATEGORY5", "com.test.category.CATEGORY6"}));
8342
8343 // Write ShareTargets to Xml
8344 ByteArrayOutputStream outStream = new ByteArrayOutputStream();
8345 final XmlSerializer outXml = new FastXmlSerializer();
8346 outXml.setOutput(outStream, StandardCharsets.UTF_8.name());
8347 outXml.startDocument(null, true);
8348 for (int i = 0; i < expectedValues.size(); i++) {
8349 expectedValues.get(i).saveToXml(outXml);
8350 }
8351 outXml.endDocument();
8352 outXml.flush();
8353
8354 // Read ShareTargets from Xml
8355 ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray());
8356 XmlPullParser parser = Xml.newPullParser();
8357 parser.setInput(new InputStreamReader(inStream));
8358 List<ShareTargetInfo> shareTargets = new ArrayList<>();
8359 int type;
8360 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
8361 if (type == XmlPullParser.START_TAG && parser.getName().equals("share-target")) {
8362 shareTargets.add(ShareTargetInfo.loadFromXml(parser));
8363 }
8364 }
8365
8366 // Assert two lists are equal
8367 assertNotNull(shareTargets);
8368 assertEquals(expectedValues.size(), shareTargets.size());
8369
8370 for (int i = 0; i < expectedValues.size(); i++) {
8371 ShareTargetInfo expected = expectedValues.get(i);
8372 ShareTargetInfo actual = shareTargets.get(i);
8373
8374 assertEquals(expected.mTargetData.length, actual.mTargetData.length);
8375 for (int j = 0; j < expected.mTargetData.length; j++) {
8376 assertEquals(expected.mTargetData[j].mScheme, actual.mTargetData[j].mScheme);
8377 assertEquals(expected.mTargetData[j].mHost, actual.mTargetData[j].mHost);
8378 assertEquals(expected.mTargetData[j].mPort, actual.mTargetData[j].mPort);
8379 assertEquals(expected.mTargetData[j].mPath, actual.mTargetData[j].mPath);
8380 assertEquals(expected.mTargetData[j].mPathPrefix,
8381 actual.mTargetData[j].mPathPrefix);
8382 assertEquals(expected.mTargetData[j].mPathPattern,
8383 actual.mTargetData[j].mPathPattern);
8384 assertEquals(expected.mTargetData[j].mMimeType, actual.mTargetData[j].mMimeType);
8385 }
8386
8387 assertEquals(expected.mTargetClass, actual.mTargetClass);
8388
8389 assertEquals(expected.mCategories.length, actual.mCategories.length);
8390 for (int j = 0; j < expected.mCategories.length; j++) {
8391 assertEquals(expected.mCategories[j], actual.mCategories[j]);
8392 }
8393 }
8394 }
Makoto Onuki6f7362d92016-03-04 13:39:41 -08008395}