blob: 798420ee01370a566504ac35aa952764c225319b [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(
1243 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
1244 });
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
1253 setCaller(CALLING_PACKAGE_1);
1254 getCallerShortcut("s1").setCached();
1255 getCallerShortcut("s2").setCached();
1256
1257 // Get manifest shortcuts
1258 assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_MANIFEST),
1259 "ms1", "ms2", "ms3");
1260
1261 // Get dynamic shortcuts
1262 assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_DYNAMIC),
1263 "s1", "s2", "s3");
1264
1265 // Get pinned shortcuts
1266 assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_PINNED),
1267 "ms2", "ms3", "s2", "s3");
1268
1269 // Get cached shortcuts
1270 assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_CACHED),
1271 "s1", "s2");
1272
1273 // Get manifest and dynamic shortcuts
1274 assertShortcutIds(mManager.getShortcuts(
1275 ShortcutManager.FLAG_MATCH_MANIFEST | ShortcutManager.FLAG_MATCH_DYNAMIC),
1276 "ms1", "ms2", "ms3", "s1", "s2", "s3");
1277
1278 // Get manifest and pinned shortcuts
1279 assertShortcutIds(mManager.getShortcuts(
1280 ShortcutManager.FLAG_MATCH_MANIFEST | ShortcutManager.FLAG_MATCH_PINNED),
1281 "ms1", "ms2", "ms3", "s2", "s3");
1282
1283 // Get manifest and cached shortcuts
1284 assertShortcutIds(mManager.getShortcuts(
1285 ShortcutManager.FLAG_MATCH_MANIFEST | ShortcutManager.FLAG_MATCH_CACHED),
1286 "ms1", "ms2", "ms3", "s1", "s2");
1287
1288 // Get dynamic and pinned shortcuts
1289 assertShortcutIds(mManager.getShortcuts(
1290 ShortcutManager.FLAG_MATCH_DYNAMIC | ShortcutManager.FLAG_MATCH_PINNED),
1291 "ms2", "ms3", "s1", "s2", "s3");
1292
1293 // Get dynamic and cached shortcuts
1294 assertShortcutIds(mManager.getShortcuts(
1295 ShortcutManager.FLAG_MATCH_DYNAMIC | ShortcutManager.FLAG_MATCH_CACHED),
1296 "s1", "s2", "s3");
1297
1298 // Get pinned and cached shortcuts
1299 assertShortcutIds(mManager.getShortcuts(
1300 ShortcutManager.FLAG_MATCH_PINNED | ShortcutManager.FLAG_MATCH_CACHED),
1301 "ms2", "ms3", "s1", "s2", "s3");
1302
1303 // Remove a dynamic cached shortcut
1304 mManager.removeDynamicShortcuts(list("s1"));
1305 assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_DYNAMIC), "s2", "s3");
1306 assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_CACHED), "s1", "s2");
1307
1308 // Remove a dynamic cached and pinned shortcut
1309 mManager.removeDynamicShortcuts(list("s2"));
1310 assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_DYNAMIC), "s3");
1311 assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_PINNED),
1312 "ms2", "ms3", "s2", "s3");
1313 assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_CACHED), "s1", "s2");
1314 }
1315
1316 public void testCachedShortcuts() {
1317 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1318 assertTrue(mManager.setDynamicShortcuts(list(makeShortcut("s1"), makeShortcut("s2"),
1319 makeShortcut("s3"), makeShortcut("s4"))));
1320 });
1321
1322 // Pin s2
1323 runWithCaller(LAUNCHER_1, USER_0, () -> {
1324 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s2"),
1325 HANDLE_USER_0);
1326 });
1327
1328 // Cache 2, 3 and 4
1329 setCaller(CALLING_PACKAGE_1);
1330 getCallerShortcut("s2").setCached();
1331 getCallerShortcut("s3").setCached();
1332 getCallerShortcut("s4").setCached();
1333
1334 // Get dynamic shortcuts
1335 assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_DYNAMIC),
1336 "s1", "s2", "s3", "s4");
1337 // Get pinned shortcuts
1338 assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_PINNED),
1339 "s2");
1340 // Get cached shortcuts
1341 assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_CACHED),
1342 "s2", "s3", "s4");
1343
1344 // Remove a dynamic cached shortcut
1345 mManager.removeDynamicShortcuts(list("s3"));
1346 assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_DYNAMIC),
1347 "s1", "s2", "s4");
1348 assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_CACHED),
1349 "s2", "s3", "s4");
1350
1351 // Remove dynamic cached long lived shortcuts
1352 mManager.removeLongLivedShortcuts(list("s3", "s4"));
1353 assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_DYNAMIC),
1354 "s1", "s2");
1355 assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_CACHED),
1356 "s2");
1357
1358 // Remove a dynamic cached pinned long lived shortcut
1359 mManager.removeLongLivedShortcuts(list("s2"));
1360 assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_DYNAMIC),
1361 "s1");
1362 assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_CACHED));
1363 assertShortcutIds(mManager.getShortcuts(ShortcutManager.FLAG_MATCH_PINNED),
1364 "s2");
1365 }
1366
Makoto Onuki99302b52017-03-29 12:42:26 -07001367 // === Test for launcher side APIs ===
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001368
1369 public void testGetShortcuts() {
1370
1371 // Set up shortcuts.
1372
1373 setCaller(CALLING_PACKAGE_1);
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001374 final ShortcutInfo s1_1 = makeShortcut("s1");
1375 final ShortcutInfo s1_2 = makeShortcut("s2");
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001376
Makoto Onukid99c6f02016-03-28 11:02:54 -07001377 assertTrue(mManager.setDynamicShortcuts(list(s1_1, s1_2)));
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001378
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001379 // Because setDynamicShortcuts will update the timestamps when ranks are changing,
1380 // we explicitly set timestamps here.
1381 getCallerShortcut("s1").setTimestamp(5000);
1382 getCallerShortcut("s2").setTimestamp(1000);
1383
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001384 setCaller(CALLING_PACKAGE_2);
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001385 final ShortcutInfo s2_2 = makeShortcut("s2");
1386 final ShortcutInfo s2_3 = makeShortcutWithActivity("s3",
Makoto Onuki85694522016-05-04 12:53:37 -07001387 makeComponent(ShortcutActivity2.class));
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001388 final ShortcutInfo s2_4 = makeShortcutWithActivity("s4",
Makoto Onuki85694522016-05-04 12:53:37 -07001389 makeComponent(ShortcutActivity.class));
Makoto Onukid99c6f02016-03-28 11:02:54 -07001390 assertTrue(mManager.setDynamicShortcuts(list(s2_2, s2_3, s2_4)));
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001391
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001392 getCallerShortcut("s2").setTimestamp(1500);
1393 getCallerShortcut("s3").setTimestamp(3000);
1394 getCallerShortcut("s4").setTimestamp(500);
1395
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001396 setCaller(CALLING_PACKAGE_3);
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001397 final ShortcutInfo s3_2 = makeShortcut("s3");
Makoto Onukid99c6f02016-03-28 11:02:54 -07001398 assertTrue(mManager.setDynamicShortcuts(list(s3_2)));
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001399
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001400 getCallerShortcut("s3").setTimestamp(START_TIME + 5000);
1401
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001402 setCaller(LAUNCHER_1);
1403
1404 // Get dynamic
Makoto Onuki20c95f82016-05-11 16:51:01 -07001405 assertAllDynamic(assertAllHaveTitle(assertAllNotHaveIntents(assertAllStringsResolved(
1406 assertShortcutIds(
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001407 assertAllNotKeyFieldsOnly(
1408 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001409 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001410 "s1", "s2")))));
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001411
1412 // Get pinned
1413 assertShortcutIds(
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001414 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001415 /* activity =*/ null,
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001416 ShortcutQuery.FLAG_GET_PINNED), getCallingUser())
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001417 /* none */);
1418
1419 // Get both, with timestamp
1420 assertAllDynamic(assertAllHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001421 assertAllNotKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
Makoto Onuki55046222016-03-08 10:49:47 -08001422 /* time =*/ 1000, CALLING_PACKAGE_2,
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001423 /* activity =*/ null,
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001424 ShortcutQuery.FLAG_GET_PINNED | ShortcutQuery.FLAG_GET_DYNAMIC),
1425 getCallingUser())),
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001426 "s2", "s3"))));
1427
1428 // FLAG_GET_KEY_FIELDS_ONLY
1429 assertAllDynamic(assertAllNotHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001430 assertAllKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
Makoto Onuki55046222016-03-08 10:49:47 -08001431 /* time =*/ 1000, CALLING_PACKAGE_2,
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001432 /* activity =*/ null,
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001433 ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY),
1434 getCallingUser())),
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001435 "s2", "s3"))));
1436
Makoto Onuki85694522016-05-04 12:53:37 -07001437 // Filter by activity
1438 assertAllDynamic(assertAllHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
1439 assertAllNotKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
1440 /* time =*/ 0, CALLING_PACKAGE_2,
1441 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
1442 ShortcutQuery.FLAG_GET_PINNED | ShortcutQuery.FLAG_GET_DYNAMIC),
1443 getCallingUser())),
Makoto Onuki20c95f82016-05-11 16:51:01 -07001444 "s4"))));
Makoto Onuki85694522016-05-04 12:53:37 -07001445
Makoto Onukiabe84422016-04-07 09:41:19 -07001446 // With ID.
1447 assertAllDynamic(assertAllNotHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
1448 assertAllKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
1449 /* time =*/ 1000, CALLING_PACKAGE_2, list("s3"),
1450 /* activity =*/ null,
1451 ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY),
1452 getCallingUser())),
1453 "s3"))));
1454 assertAllDynamic(assertAllNotHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
1455 assertAllKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
1456 /* time =*/ 1000, CALLING_PACKAGE_2, list("s3", "s2", "ss"),
1457 /* activity =*/ null,
1458 ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY),
1459 getCallingUser())),
1460 "s2", "s3"))));
1461 assertAllDynamic(assertAllNotHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
1462 assertAllKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
1463 /* time =*/ 1000, CALLING_PACKAGE_2, list("s3x", "s2x"),
1464 /* activity =*/ null,
1465 ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY),
1466 getCallingUser()))
1467 /* empty */))));
1468 assertAllDynamic(assertAllNotHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
1469 assertAllKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
1470 /* time =*/ 1000, CALLING_PACKAGE_2, list(),
1471 /* activity =*/ null,
1472 ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY),
1473 getCallingUser()))
1474 /* empty */))));
1475
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001476 // Pin some shortcuts.
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001477 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
Makoto Onukid99c6f02016-03-28 11:02:54 -07001478 list("s3", "s4"), getCallingUser());
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001479
1480 // Pinned ones only
1481 assertAllPinned(assertAllHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001482 assertAllNotKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
Makoto Onuki55046222016-03-08 10:49:47 -08001483 /* time =*/ 1000, CALLING_PACKAGE_2,
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001484 /* activity =*/ null,
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001485 ShortcutQuery.FLAG_GET_PINNED),
1486 getCallingUser())),
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001487 "s3"))));
1488
1489 // All packages.
Makoto Onuki55046222016-03-08 10:49:47 -08001490 assertShortcutIds(assertAllNotKeyFieldsOnly(
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001491 mLauncherApps.getShortcuts(buildQuery(
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001492 /* time =*/ 5000, /* package= */ null,
1493 /* activity =*/ null,
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001494 ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_PINNED),
1495 getCallingUser())),
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001496 "s1", "s3");
1497
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07001498 assertExpectException(
Makoto Onukiabe84422016-04-07 09:41:19 -07001499 IllegalArgumentException.class, "package name must also be set", () -> {
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001500 mLauncherApps.getShortcuts(buildQuery(
Makoto Onukiabe84422016-04-07 09:41:19 -07001501 /* time =*/ 0, /* package= */ null, list("id"),
1502 /* activity =*/ null, /* flags */ 0), getCallingUser());
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001503 });
Makoto Onukiabe84422016-04-07 09:41:19 -07001504
Makoto Onuki9fd90192017-01-06 18:31:03 +00001505 // TODO More tests: pinned but dynamic.
Mehdi Alizadeh505fb762020-01-21 17:26:24 -08001506
1507 // Cache some shortcuts
1508 setCaller(CALLING_PACKAGE_1);
1509 getCallerShortcut("s1").setCached();
1510
1511 setCaller(CALLING_PACKAGE_2);
1512 getCallerShortcut("s4").setCached();
1513
1514 setCaller(CALLING_PACKAGE_3);
1515 getCallerShortcut("s3").setCached();
1516
1517 setCaller(LAUNCHER_1);
1518
1519 // Cached ones only
1520 assertShortcutIds(assertAllNotKeyFieldsOnly(
1521 mLauncherApps.getShortcuts(buildQuery(
1522 /* time =*/ 0, CALLING_PACKAGE_2,
1523 /* activity =*/ null,
1524 ShortcutQuery.FLAG_MATCH_CACHED),
1525 getCallingUser())),
1526 "s4");
1527
1528 // All packages.
1529 assertShortcutIds(assertAllNotKeyFieldsOnly(
1530 mLauncherApps.getShortcuts(buildQuery(
1531 /* time =*/ 0, /* package= */ null,
1532 /* activity =*/ null,
1533 ShortcutQuery.FLAG_MATCH_CACHED),
1534 getCallingUser())),
1535 "s1", "s4", "s3");
1536
1537 assertExpectException(
1538 IllegalArgumentException.class, "package name must also be set", () -> {
1539 mLauncherApps.getShortcuts(buildQuery(
1540 /* time =*/ 0, /* package= */ null, list("id"),
1541 /* activity =*/ null, /* flags */ 0), getCallingUser());
1542 });
1543
1544 // Change Launcher. Cached shortcuts are the same for all launchers.
1545 setCaller(LAUNCHER_2);
1546 // All packages.
1547 assertShortcutIds(assertAllNotKeyFieldsOnly(
1548 mLauncherApps.getShortcuts(buildQuery(
1549 /* time =*/ 0, /* package= */ null,
1550 /* activity =*/ null,
1551 ShortcutQuery.FLAG_MATCH_CACHED),
1552 getCallingUser())),
1553 "s1", "s4", "s3");
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001554 }
1555
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07001556 public void testGetShortcuts_shortcutKinds() throws Exception {
1557 // Create 3 manifest and 3 dynamic shortcuts
1558 addManifestShortcutResource(
1559 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
1560 R.xml.shortcut_3);
1561 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07001562 mService.mPackageMonitor.onReceive(getTestContext(),
1563 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07001564
1565 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1566 assertTrue(mManager.setDynamicShortcuts(list(
1567 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
1568 });
1569
1570 // Pin 2 and 3
1571 runWithCaller(LAUNCHER_1, USER_0, () -> {
1572 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms2", "ms3", "s2", "s3"),
1573 HANDLE_USER_0);
1574 });
1575
1576 // Remove ms3 and s3
1577 addManifestShortcutResource(
1578 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
1579 R.xml.shortcut_2);
1580 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07001581 mService.mPackageMonitor.onReceive(getTestContext(),
1582 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07001583
1584 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1585 assertTrue(mManager.setDynamicShortcuts(list(
1586 makeShortcut("s1"), makeShortcut("s2"))));
1587 });
1588
1589 // Check their status.
1590 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1591 assertWith(getCallerShortcuts())
1592 .haveIds("ms1", "ms2", "ms3", "s1", "s2", "s3")
1593
1594 .selectByIds("ms1", "ms2")
1595 .areAllManifest()
1596 .areAllImmutable()
1597 .areAllNotDynamic()
1598
1599 .revertToOriginalList()
1600 .selectByIds("ms3")
1601 .areAllNotManifest()
1602 .areAllImmutable()
1603 .areAllDisabled()
1604 .areAllNotDynamic()
1605
1606 .revertToOriginalList()
1607 .selectByIds("s1", "s2")
1608 .areAllNotManifest()
1609 .areAllMutable()
1610 .areAllDynamic()
1611
1612 .revertToOriginalList()
1613 .selectByIds("s3")
1614 .areAllNotManifest()
1615 .areAllMutable()
1616 .areAllEnabled()
1617 .areAllNotDynamic()
1618
1619 .revertToOriginalList()
1620 .selectByIds("s1", "ms1")
1621 .areAllNotPinned()
1622
1623 .revertToOriginalList()
1624 .selectByIds("s2", "s3", "ms2", "ms3")
1625 .areAllPinned()
1626 ;
1627 });
1628
1629 // Finally, actual tests.
1630 runWithCaller(LAUNCHER_1, USER_0, () -> {
1631 assertWith(mLauncherApps.getShortcuts(
1632 buildQueryWithFlags(ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0))
1633 .haveIds("s1", "s2");
1634 assertWith(mLauncherApps.getShortcuts(
1635 buildQueryWithFlags(ShortcutQuery.FLAG_GET_MANIFEST), HANDLE_USER_0))
1636 .haveIds("ms1", "ms2");
1637 assertWith(mLauncherApps.getShortcuts(
1638 buildQueryWithFlags(ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0))
1639 .haveIds("s2", "s3", "ms2", "ms3");
1640
1641 assertWith(mLauncherApps.getShortcuts(
1642 buildQueryWithFlags(
1643 ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_PINNED
1644 ), HANDLE_USER_0))
1645 .haveIds("s1", "s2", "s3", "ms2", "ms3");
1646
1647 assertWith(mLauncherApps.getShortcuts(
1648 buildQueryWithFlags(
1649 ShortcutQuery.FLAG_GET_MANIFEST | ShortcutQuery.FLAG_GET_PINNED
1650 ), HANDLE_USER_0))
1651 .haveIds("ms1", "s2", "s3", "ms2", "ms3");
1652
1653 assertWith(mLauncherApps.getShortcuts(
1654 buildQueryWithFlags(
1655 ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_MANIFEST
1656 ), HANDLE_USER_0))
1657 .haveIds("ms1", "ms2", "s1", "s2");
1658
1659 assertWith(mLauncherApps.getShortcuts(
1660 buildQueryWithFlags(
1661 ShortcutQuery.FLAG_GET_ALL_KINDS
1662 ), HANDLE_USER_0))
1663 .haveIds("ms1", "ms2", "ms3", "s1", "s2", "s3");
1664 });
1665 }
1666
Makoto Onuki20c95f82016-05-11 16:51:01 -07001667 public void testGetShortcuts_resolveStrings() throws Exception {
Makoto Onuki20c95f82016-05-11 16:51:01 -07001668 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1669 ShortcutInfo si = new ShortcutInfo.Builder(mClientContext)
1670 .setId("id")
Makoto Onuki22fcc682016-05-17 14:52:19 -07001671 .setActivity(new ComponentName(mClientContext, "dummy"))
Makoto Onuki20c95f82016-05-11 16:51:01 -07001672 .setTitleResId(10)
1673 .setTextResId(11)
1674 .setDisabledMessageResId(12)
1675 .setIntent(makeIntent("action", ShortcutActivity.class))
1676 .build();
1677 mManager.setDynamicShortcuts(list(si));
1678 });
1679 runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
1680 ShortcutInfo si = new ShortcutInfo.Builder(mClientContext)
1681 .setId("id")
Makoto Onuki22fcc682016-05-17 14:52:19 -07001682 .setActivity(new ComponentName(mClientContext, "dummy"))
Makoto Onuki20c95f82016-05-11 16:51:01 -07001683 .setTitleResId(10)
1684 .setTextResId(11)
1685 .setDisabledMessageResId(12)
1686 .setIntent(makeIntent("action", ShortcutActivity.class))
1687 .build();
1688 mManager.setDynamicShortcuts(list(si));
1689 });
1690
1691 runWithCaller(LAUNCHER_1, USER_0, () -> {
1692 final ShortcutQuery q = new ShortcutQuery();
1693 q.setQueryFlags(ShortcutQuery.FLAG_GET_DYNAMIC);
1694
1695 // USER 0
1696 List<ShortcutInfo> ret = assertShortcutIds(
1697 assertAllStringsResolved(mLauncherApps.getShortcuts(q, HANDLE_USER_0)),
1698 "id");
Makoto Onuki157b1622016-06-02 16:13:10 -07001699 assertEquals("string-com.android.test.1-user:0-res:10/en", ret.get(0).getTitle());
1700 assertEquals("string-com.android.test.1-user:0-res:11/en", ret.get(0).getText());
1701 assertEquals("string-com.android.test.1-user:0-res:12/en",
Makoto Onuki20c95f82016-05-11 16:51:01 -07001702 ret.get(0).getDisabledMessage());
1703
1704 // USER P0
1705 ret = assertShortcutIds(
1706 assertAllStringsResolved(mLauncherApps.getShortcuts(q, HANDLE_USER_P0)),
1707 "id");
Makoto Onuki157b1622016-06-02 16:13:10 -07001708 assertEquals("string-com.android.test.1-user:20-res:10/en", ret.get(0).getTitle());
1709 assertEquals("string-com.android.test.1-user:20-res:11/en", ret.get(0).getText());
1710 assertEquals("string-com.android.test.1-user:20-res:12/en",
Makoto Onuki20c95f82016-05-11 16:51:01 -07001711 ret.get(0).getDisabledMessage());
1712 });
1713 }
1714
1715 // TODO resource
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001716 public void testGetShortcutInfo() {
1717 // Create shortcuts.
1718 setCaller(CALLING_PACKAGE_1);
1719 final ShortcutInfo s1_1 = makeShortcut(
1720 "s1",
1721 "Title 1",
1722 makeComponent(ShortcutActivity.class),
1723 /* icon =*/ null,
1724 makeIntent(Intent.ACTION_ASSIST, ShortcutActivity2.class,
1725 "key1", "val1", "nest", makeBundle("key", 123)),
Makoto Onuki99302b52017-03-29 12:42:26 -07001726 /* weight */ 10);
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001727
1728 final ShortcutInfo s1_2 = makeShortcut(
Makoto Onuki99302b52017-03-29 12:42:26 -07001729 "s2",
1730 "Title 2",
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001731 /* activity */ null,
1732 /* icon =*/ null,
1733 makeIntent(Intent.ACTION_ASSIST, ShortcutActivity3.class),
Makoto Onuki99302b52017-03-29 12:42:26 -07001734 /* weight */ 12);
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001735
Makoto Onukid99c6f02016-03-28 11:02:54 -07001736 assertTrue(mManager.setDynamicShortcuts(list(s1_1, s1_2)));
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001737 dumpsysOnLogcat();
1738
1739 setCaller(CALLING_PACKAGE_2);
1740 final ShortcutInfo s2_1 = makeShortcut(
1741 "s1",
1742 "ABC",
1743 makeComponent(ShortcutActivity2.class),
1744 /* icon =*/ null,
1745 makeIntent(Intent.ACTION_ANSWER, ShortcutActivity2.class,
1746 "key1", "val1", "nest", makeBundle("key", 123)),
Makoto Onuki99302b52017-03-29 12:42:26 -07001747 /* weight */ 10);
Makoto Onukid99c6f02016-03-28 11:02:54 -07001748 assertTrue(mManager.setDynamicShortcuts(list(s2_1)));
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001749 dumpsysOnLogcat();
1750
1751 // Pin some.
1752 setCaller(LAUNCHER_1);
1753
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001754 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
Makoto Onukid99c6f02016-03-28 11:02:54 -07001755 list("s2"), getCallingUser());
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001756
1757 dumpsysOnLogcat();
1758
1759 // Delete some.
1760 setCaller(CALLING_PACKAGE_1);
1761 assertShortcutIds(mManager.getPinnedShortcuts(), "s2");
Makoto Onukib6d35232016-04-04 15:57:17 -07001762 mManager.removeDynamicShortcuts(list("s2"));
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001763 assertShortcutIds(mManager.getPinnedShortcuts(), "s2");
1764
1765 dumpsysOnLogcat();
1766
1767 setCaller(LAUNCHER_1);
1768 List<ShortcutInfo> list;
1769
1770 // Note we don't guarantee the orders.
1771 list = assertShortcutIds(assertAllHaveTitle(assertAllNotHaveIntents(
Makoto Onuki55046222016-03-08 10:49:47 -08001772 assertAllNotKeyFieldsOnly(
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001773 mLauncherApps.getShortcutInfo(CALLING_PACKAGE_1,
1774 list("s2", "s1", "s3", null), getCallingUser())))),
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001775 "s1", "s2");
1776 assertEquals("Title 1", findById(list, "s1").getTitle());
1777 assertEquals("Title 2", findById(list, "s2").getTitle());
1778
1779 assertShortcutIds(assertAllHaveTitle(assertAllNotHaveIntents(
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001780 mLauncherApps.getShortcutInfo(CALLING_PACKAGE_1,
Makoto Onukid99c6f02016-03-28 11:02:54 -07001781 list("s3"), getCallingUser())))
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001782 /* none */);
1783
1784 list = assertShortcutIds(assertAllHaveTitle(assertAllNotHaveIntents(
Makoto Onuki7a6a05f2016-03-10 17:01:08 -08001785 mLauncherApps.getShortcutInfo(CALLING_PACKAGE_2,
Makoto Onukid99c6f02016-03-28 11:02:54 -07001786 list("s1", "s2", "s3"), getCallingUser()))),
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001787 "s1");
1788 assertEquals("ABC", findById(list, "s1").getTitle());
1789 }
1790
1791 public void testPinShortcutAndGetPinnedShortcuts() {
Makoto Onukide667372016-03-15 14:29:20 -07001792 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1793 final ShortcutInfo s1_1 = makeShortcutWithTimestamp("s1", 1000);
1794 final ShortcutInfo s1_2 = makeShortcutWithTimestamp("s2", 2000);
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001795
Makoto Onukid99c6f02016-03-28 11:02:54 -07001796 assertTrue(mManager.setDynamicShortcuts(list(s1_1, s1_2)));
Makoto Onukide667372016-03-15 14:29:20 -07001797 });
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001798
Makoto Onukide667372016-03-15 14:29:20 -07001799 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
1800 final ShortcutInfo s2_2 = makeShortcutWithTimestamp("s2", 1500);
1801 final ShortcutInfo s2_3 = makeShortcutWithTimestamp("s3", 3000);
1802 final ShortcutInfo s2_4 = makeShortcutWithTimestamp("s4", 500);
Makoto Onukid99c6f02016-03-28 11:02:54 -07001803 assertTrue(mManager.setDynamicShortcuts(list(s2_2, s2_3, s2_4)));
Makoto Onukide667372016-03-15 14:29:20 -07001804 });
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001805
Makoto Onukide667372016-03-15 14:29:20 -07001806 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
1807 final ShortcutInfo s3_2 = makeShortcutWithTimestamp("s2", 1000);
Makoto Onukid99c6f02016-03-28 11:02:54 -07001808 assertTrue(mManager.setDynamicShortcuts(list(s3_2)));
Makoto Onukide667372016-03-15 14:29:20 -07001809 });
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001810
1811 // Pin some.
Makoto Onukide667372016-03-15 14:29:20 -07001812 runWithCaller(LAUNCHER_1, USER_0, () -> {
1813 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
Makoto Onukid99c6f02016-03-28 11:02:54 -07001814 list("s2", "s3"), getCallingUser());
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001815
Makoto Onukide667372016-03-15 14:29:20 -07001816 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
Makoto Onukid99c6f02016-03-28 11:02:54 -07001817 list("s3", "s4", "s5"), getCallingUser());
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001818
Makoto Onukide667372016-03-15 14:29:20 -07001819 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3,
Makoto Onukid99c6f02016-03-28 11:02:54 -07001820 list("s3"), getCallingUser()); // Note ID doesn't exist
Makoto Onukide667372016-03-15 14:29:20 -07001821 });
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001822
1823 // Delete some.
Makoto Onukide667372016-03-15 14:29:20 -07001824 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1825 assertShortcutIds(mManager.getPinnedShortcuts(), "s2");
Makoto Onukib6d35232016-04-04 15:57:17 -07001826 mManager.removeDynamicShortcuts(list("s2"));
Makoto Onukide667372016-03-15 14:29:20 -07001827 assertShortcutIds(mManager.getPinnedShortcuts(), "s2");
Makoto Onuki22fcc682016-05-17 14:52:19 -07001828
1829 assertShortcutIds(mManager.getDynamicShortcuts(), "s1");
Makoto Onukide667372016-03-15 14:29:20 -07001830 });
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001831
Makoto Onukide667372016-03-15 14:29:20 -07001832 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
1833 assertShortcutIds(mManager.getPinnedShortcuts(), "s3", "s4");
Makoto Onukib6d35232016-04-04 15:57:17 -07001834 mManager.removeDynamicShortcuts(list("s3"));
Makoto Onukide667372016-03-15 14:29:20 -07001835 assertShortcutIds(mManager.getPinnedShortcuts(), "s3", "s4");
Makoto Onuki22fcc682016-05-17 14:52:19 -07001836
1837 assertShortcutIds(mManager.getDynamicShortcuts(), "s2", "s4");
Makoto Onukide667372016-03-15 14:29:20 -07001838 });
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001839
Makoto Onukide667372016-03-15 14:29:20 -07001840 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
1841 assertShortcutIds(mManager.getPinnedShortcuts() /* none */);
Makoto Onukib6d35232016-04-04 15:57:17 -07001842 mManager.removeDynamicShortcuts(list("s2"));
Makoto Onukide667372016-03-15 14:29:20 -07001843 assertShortcutIds(mManager.getPinnedShortcuts() /* none */);
Makoto Onuki22fcc682016-05-17 14:52:19 -07001844
1845 assertEmpty(mManager.getDynamicShortcuts());
Makoto Onukide667372016-03-15 14:29:20 -07001846 });
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001847
1848 // Get pinned shortcuts from launcher
Makoto Onukide667372016-03-15 14:29:20 -07001849 runWithCaller(LAUNCHER_1, USER_0, () -> {
1850 // CALLING_PACKAGE_1 deleted s2, but it's pinned, so it still exists.
Makoto Onuki22fcc682016-05-17 14:52:19 -07001851 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(assertAllEnabled(
Makoto Onukide667372016-03-15 14:29:20 -07001852 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
Makoto Onuki22fcc682016-05-17 14:52:19 -07001853 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser())))),
Makoto Onukide667372016-03-15 14:29:20 -07001854 "s2");
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001855
Makoto Onuki22fcc682016-05-17 14:52:19 -07001856 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(assertAllEnabled(
1857 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
1858 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser())))),
1859 "s3", "s4");
1860
1861 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(assertAllEnabled(
1862 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_3,
1863 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))))
1864 /* none */);
1865 });
1866 }
1867
1868 /**
1869 * This is similar to the above test, except it used "disable" instead of "remove". It also
1870 * does "enable".
1871 */
1872 public void testDisableAndEnableShortcuts() {
1873 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1874 final ShortcutInfo s1_1 = makeShortcutWithTimestamp("s1", 1000);
1875 final ShortcutInfo s1_2 = makeShortcutWithTimestamp("s2", 2000);
1876
1877 assertTrue(mManager.setDynamicShortcuts(list(s1_1, s1_2)));
1878 });
1879
1880 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
1881 final ShortcutInfo s2_2 = makeShortcutWithTimestamp("s2", 1500);
1882 final ShortcutInfo s2_3 = makeShortcutWithTimestamp("s3", 3000);
1883 final ShortcutInfo s2_4 = makeShortcutWithTimestamp("s4", 500);
1884 assertTrue(mManager.setDynamicShortcuts(list(s2_2, s2_3, s2_4)));
1885 });
1886
1887 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
1888 final ShortcutInfo s3_2 = makeShortcutWithTimestamp("s2", 1000);
1889 assertTrue(mManager.setDynamicShortcuts(list(s3_2)));
1890 });
1891
1892 // Pin some.
1893 runWithCaller(LAUNCHER_1, USER_0, () -> {
1894 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
1895 list("s2", "s3"), getCallingUser());
1896
1897 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
1898 list("s3", "s4", "s5"), getCallingUser());
1899
1900 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3,
1901 list("s3"), getCallingUser()); // Note ID doesn't exist
1902 });
1903
1904 // Disable some.
1905 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1906 assertShortcutIds(mManager.getPinnedShortcuts(), "s2");
1907
Makoto Onukib1588c02017-10-12 15:11:45 -07001908 mManager.updateShortcuts(list(
1909 new ShortcutInfo.Builder(mClientContext, "s2").setDisabledMessage("xyz")
1910 .build()));
1911
Makoto Onuki22fcc682016-05-17 14:52:19 -07001912 mManager.disableShortcuts(list("s2"));
1913
1914 assertShortcutIds(mManager.getPinnedShortcuts(), "s2");
1915 assertShortcutIds(mManager.getDynamicShortcuts(), "s1");
1916 });
1917
1918 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
1919 assertShortcutIds(mManager.getPinnedShortcuts(), "s3", "s4");
1920
1921 // disable should work even if a shortcut is not dynamic, so try calling "remove" first
1922 // here.
1923 mManager.removeDynamicShortcuts(list("s3"));
1924 mManager.disableShortcuts(list("s3"));
1925
1926 assertShortcutIds(mManager.getPinnedShortcuts(), "s3", "s4");
1927 assertShortcutIds(mManager.getDynamicShortcuts(), "s2", "s4");
1928 });
1929
1930 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
1931 assertShortcutIds(mManager.getPinnedShortcuts() /* none */);
1932
1933 mManager.disableShortcuts(list("s2"));
1934
1935 assertShortcutIds(mManager.getPinnedShortcuts() /* none */);
1936
1937 assertEmpty(mManager.getDynamicShortcuts());
1938 assertEmpty(getCallerShortcuts());
1939 });
1940
1941 // Get pinned shortcuts from launcher
1942 runWithCaller(LAUNCHER_1, USER_0, () -> {
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07001943 // CALLING_PACKAGE_1 deleted s2, but it's pinned, so it still exists, and disabled.
1944 assertWith(mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1945 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))
1946 .haveIds("s2")
Makoto Onukib1588c02017-10-12 15:11:45 -07001947 .areAllWithDisabledReason(ShortcutInfo.DISABLED_REASON_BY_APP)
1948 .forAllShortcuts(si -> {
1949 assertEquals("xyz", si.getDisabledMessage());
1950 })
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07001951 .areAllPinned()
1952 .areAllNotWithKeyFieldsOnly()
1953 .areAllDisabled();
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001954 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_0,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07001955 ActivityNotFoundException.class);
Makoto Onuki22fcc682016-05-17 14:52:19 -07001956
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07001957 // Here, s4 is still enabled and launchable, but s3 is disabled.
1958 assertWith(mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
1959 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))
1960 .haveIds("s3", "s4")
1961 .areAllPinned()
1962 .areAllNotWithKeyFieldsOnly()
1963
1964 .selectByIds("s3")
1965 .areAllDisabled()
1966
1967 .revertToOriginalList()
1968 .selectByIds("s4")
1969 .areAllEnabled();
1970
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001971 assertStartShortcutThrowsException(CALLING_PACKAGE_2, "s3", USER_0,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07001972 ActivityNotFoundException.class);
Makoto Onukiff14f732016-06-30 17:07:25 -07001973 assertShortcutLaunchable(CALLING_PACKAGE_2, "s4", USER_0);
Makoto Onuki6f7362d92016-03-04 13:39:41 -08001974
Makoto Onuki22fcc682016-05-17 14:52:19 -07001975 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(assertAllEnabled(
Makoto Onukide667372016-03-15 14:29:20 -07001976 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_3,
Makoto Onuki22fcc682016-05-17 14:52:19 -07001977 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))))
Makoto Onukide667372016-03-15 14:29:20 -07001978 /* none */);
1979 });
Makoto Onuki22fcc682016-05-17 14:52:19 -07001980
1981 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1982 mManager.enableShortcuts(list("s2"));
1983
1984 assertShortcutIds(mManager.getPinnedShortcuts(), "s2");
1985 assertShortcutIds(mManager.getDynamicShortcuts(), "s1");
1986 });
1987 runWithCaller(LAUNCHER_1, USER_0, () -> {
1988 // CALLING_PACKAGE_1 deleted s2, but it's pinned, so it still exists.
1989 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(assertAllEnabled(
1990 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
1991 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser())))),
1992 "s2");
Makoto Onukiff14f732016-06-30 17:07:25 -07001993 assertShortcutLaunchable(CALLING_PACKAGE_1, "s2", USER_0);
1994 });
1995 }
1996
1997 public void testDisableShortcuts_thenRepublish() {
1998 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1999 assertTrue(mManager.setDynamicShortcuts(list(
2000 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2001
2002 runWithCaller(LAUNCHER_1, USER_0, () -> {
2003 mLauncherApps.pinShortcuts(
2004 CALLING_PACKAGE_1, list("s1", "s2", "s3"), HANDLE_USER_0);
2005 });
2006
2007 mManager.disableShortcuts(list("s1", "s2", "s3"));
2008
2009 assertWith(getCallerShortcuts())
2010 .haveIds("s1", "s2", "s3")
2011 .areAllNotDynamic()
2012 .areAllPinned()
2013 .areAllDisabled();
2014
2015 // Make sure updateShortcuts() will not re-enable them.
2016 assertTrue(mManager.updateShortcuts(list(
2017 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2018
2019 assertWith(getCallerShortcuts())
2020 .haveIds("s1", "s2", "s3")
2021 .areAllNotDynamic()
2022 .areAllPinned()
2023 .areAllDisabled();
2024
2025 // Re-publish s1 with setDynamicShortcuts.
2026 mInjectedCurrentTimeMillis += INTERVAL; // reset throttling
2027
2028 assertTrue(mManager.setDynamicShortcuts(list(
2029 makeShortcut("s1"))));
2030
2031 assertWith(getCallerShortcuts())
2032 .haveIds("s1", "s2", "s3")
2033
2034 .selectByIds("s1")
2035 .areAllDynamic()
2036 .areAllPinned()
2037 .areAllEnabled()
2038
2039 .revertToOriginalList()
2040 .selectByIds("s2", "s3")
2041 .areAllNotDynamic()
2042 .areAllPinned()
2043 .areAllDisabled();
2044
2045 // Re-publish s2 with addDynamicShortcuts.
2046 mInjectedCurrentTimeMillis += INTERVAL; // reset throttling
2047
2048 assertTrue(mManager.addDynamicShortcuts(list(
2049 makeShortcut("s2"))));
2050
2051 assertWith(getCallerShortcuts())
2052 .haveIds("s1", "s2", "s3")
2053
2054 .selectByIds("s1", "s2")
2055 .areAllDynamic()
2056 .areAllPinned()
2057 .areAllEnabled()
2058
2059 .revertToOriginalList()
2060 .selectByIds("s3")
2061 .areAllNotDynamic()
2062 .areAllPinned()
2063 .areAllDisabled();
Makoto Onuki22fcc682016-05-17 14:52:19 -07002064 });
Makoto Onukide667372016-03-15 14:29:20 -07002065 }
Makoto Onuki6f7362d92016-03-04 13:39:41 -08002066
Makoto Onukide667372016-03-15 14:29:20 -07002067 public void testPinShortcutAndGetPinnedShortcuts_multi() {
2068 // Create some shortcuts.
2069 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
Makoto Onukid99c6f02016-03-28 11:02:54 -07002070 assertTrue(mManager.setDynamicShortcuts(list(
Makoto Onukide667372016-03-15 14:29:20 -07002071 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2072 });
2073
2074 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
Makoto Onukid99c6f02016-03-28 11:02:54 -07002075 assertTrue(mManager.setDynamicShortcuts(list(
Makoto Onukide667372016-03-15 14:29:20 -07002076 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2077 });
2078
2079 dumpsysOnLogcat();
2080
2081 // Pin some.
2082 runWithCaller(LAUNCHER_1, USER_0, () -> {
2083 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
Makoto Onukid99c6f02016-03-28 11:02:54 -07002084 list("s3", "s4"), getCallingUser());
Makoto Onukide667372016-03-15 14:29:20 -07002085
2086 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
Makoto Onukid99c6f02016-03-28 11:02:54 -07002087 list("s1", "s2", "s4"), getCallingUser());
Makoto Onukide667372016-03-15 14:29:20 -07002088 });
2089
2090 dumpsysOnLogcat();
2091
2092 // Delete some.
2093 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2094 assertShortcutIds(mManager.getPinnedShortcuts(), "s3");
Makoto Onukib6d35232016-04-04 15:57:17 -07002095 mManager.removeDynamicShortcuts(list("s3"));
Makoto Onukide667372016-03-15 14:29:20 -07002096 assertShortcutIds(mManager.getPinnedShortcuts(), "s3");
2097 });
2098
2099 dumpsysOnLogcat();
2100
2101 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
2102 assertShortcutIds(mManager.getPinnedShortcuts(), "s1", "s2");
Makoto Onukib6d35232016-04-04 15:57:17 -07002103 mManager.removeDynamicShortcuts(list("s1"));
2104 mManager.removeDynamicShortcuts(list("s3"));
Makoto Onukide667372016-03-15 14:29:20 -07002105 assertShortcutIds(mManager.getPinnedShortcuts(), "s1", "s2");
2106 });
2107
2108 dumpsysOnLogcat();
2109
2110 // Get pinned shortcuts from launcher
2111 runWithCaller(LAUNCHER_1, USER_0, () -> {
2112 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
2113 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2114 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
2115 "s3");
2116
2117 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
2118 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2119 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
2120 "s1", "s2");
2121
2122 assertShortcutIds(assertAllDynamicOrPinned(
2123 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2124 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
2125 | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
2126 "s1", "s2", "s3");
2127
2128 assertShortcutIds(assertAllDynamicOrPinned(
2129 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2130 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
2131 | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
2132 "s1", "s2");
2133 });
2134
Makoto Onuki634cecb2017-10-13 17:10:48 -07002135 dumpsysOnLogcat("Before launcher 2");
Makoto Onukide667372016-03-15 14:29:20 -07002136
2137 runWithCaller(LAUNCHER_2, USER_0, () -> {
2138 // Launcher2 still has no pinned ones.
2139 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
2140 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2141 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser())))
2142 /* none */);
2143 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
2144 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2145 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser())))
2146 /* none */);
2147
Makoto Onuki634cecb2017-10-13 17:10:48 -07002148 // Make sure FLAG_MATCH_ALL_PINNED will be ignored.
2149 assertWith(mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2150 /* activity =*/ null, ShortcutQuery.FLAG_MATCH_PINNED
Makoto Onuki35559d62017-11-06 16:26:32 -08002151 | ShortcutQuery.FLAG_MATCH_PINNED_BY_ANY_LAUNCHER), getCallingUser()))
Makoto Onuki634cecb2017-10-13 17:10:48 -07002152 .isEmpty();
2153
2154 // Make sure the special permission works.
2155 mInjectCheckAccessShortcutsPermission = true;
2156
2157 dumpsysOnLogcat("All-pinned");
2158
2159 assertWith(mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2160 /* activity =*/ null, ShortcutQuery.FLAG_MATCH_PINNED
Makoto Onuki35559d62017-11-06 16:26:32 -08002161 | ShortcutQuery.FLAG_MATCH_PINNED_BY_ANY_LAUNCHER), getCallingUser()))
Makoto Onuki634cecb2017-10-13 17:10:48 -07002162 .haveIds("s1", "s2");
2163 assertWith(mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2164 /* activity =*/ null, ShortcutQuery.FLAG_MATCH_PINNED), getCallingUser()))
2165 .isEmpty();
2166
Makoto Onuki35559d62017-11-06 16:26:32 -08002167 assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", getCallingUser().getIdentifier());
2168
Makoto Onuki634cecb2017-10-13 17:10:48 -07002169 mInjectCheckAccessShortcutsPermission = false;
2170
Makoto Onuki35559d62017-11-06 16:26:32 -08002171 assertShortcutNotLaunched(CALLING_PACKAGE_2, "s1", getCallingUser().getIdentifier());
2172
Makoto Onukide667372016-03-15 14:29:20 -07002173 assertShortcutIds(assertAllDynamic(
2174 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2175 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
2176 | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
2177 "s1", "s2");
2178 assertShortcutIds(assertAllDynamic(
2179 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2180 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
2181 | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
2182 "s2");
2183
2184 // Now pin some.
2185 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
Makoto Onukid99c6f02016-03-28 11:02:54 -07002186 list("s1", "s2"), getCallingUser());
Makoto Onukide667372016-03-15 14:29:20 -07002187
2188 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
Makoto Onukid99c6f02016-03-28 11:02:54 -07002189 list("s1", "s2"), getCallingUser());
Makoto Onukide667372016-03-15 14:29:20 -07002190
2191 assertShortcutIds(assertAllDynamic(
2192 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2193 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
2194 | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
2195 "s1", "s2");
2196
2197 // S1 was not visible to it, so shouldn't be pinned.
2198 assertShortcutIds(assertAllDynamic(
2199 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2200 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
2201 | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
2202 "s2");
2203 });
2204
2205 // Re-initialize and load from the files.
Makoto Onukiaa8b94a2016-03-17 13:14:05 -07002206 mService.saveDirtyInfo();
Makoto Onukide667372016-03-15 14:29:20 -07002207 initService();
2208
Makoto Onuki0acbb142016-03-22 17:02:57 -07002209 // Load from file.
2210 mService.handleUnlockUser(USER_0);
2211
2212 // Make sure package info is restored too.
Makoto Onukide667372016-03-15 14:29:20 -07002213 runWithCaller(LAUNCHER_1, USER_0, () -> {
2214 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
2215 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2216 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
2217 "s3");
2218 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
2219 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2220 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
2221 "s1", "s2");
2222 });
2223 runWithCaller(LAUNCHER_2, USER_0, () -> {
2224 assertShortcutIds(assertAllDynamic(
2225 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2226 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
2227 | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
2228 "s1", "s2");
2229 assertShortcutIds(assertAllDynamic(
2230 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2231 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
2232 | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
2233 "s2");
2234 });
2235
2236 // Delete all dynamic.
2237 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
Makoto Onukib6d35232016-04-04 15:57:17 -07002238 mManager.removeAllDynamicShortcuts();
Makoto Onukide667372016-03-15 14:29:20 -07002239
2240 assertEquals(0, mManager.getDynamicShortcuts().size());
2241 assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()), "s1", "s2", "s3");
2242 });
2243 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
Makoto Onukib6d35232016-04-04 15:57:17 -07002244 mManager.removeAllDynamicShortcuts();
Makoto Onukide667372016-03-15 14:29:20 -07002245
2246 assertEquals(0, mManager.getDynamicShortcuts().size());
2247 assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()), "s2", "s1");
2248 });
2249
2250 runWithCaller(LAUNCHER_1, USER_0, () -> {
2251 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
2252 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2253 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
2254 "s3");
2255
2256 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
2257 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2258 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
2259 "s1", "s2");
2260
2261 // from all packages.
2262 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
2263 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, null,
2264 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
2265 "s1", "s2", "s3");
2266
2267 // Update pined. Note s2 and s3 are actually available, but not visible to this
2268 // launcher, so still can't be pinned.
Makoto Onukid99c6f02016-03-28 11:02:54 -07002269 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s1", "s2", "s3", "s4"),
Makoto Onukide667372016-03-15 14:29:20 -07002270 getCallingUser());
2271
2272 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
2273 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2274 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
2275 "s3");
2276 });
2277 // Re-publish s1.
2278 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
Makoto Onukib6d35232016-04-04 15:57:17 -07002279 assertTrue(mManager.addDynamicShortcuts(list(makeShortcut("s1"))));
Makoto Onukide667372016-03-15 14:29:20 -07002280
2281 assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()), "s1");
2282 assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()), "s1", "s2", "s3");
2283 });
2284 runWithCaller(LAUNCHER_1, USER_0, () -> {
2285 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
2286 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2287 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
2288 "s3");
2289
2290 // Now "s1" is visible, so can be pinned.
Makoto Onukid99c6f02016-03-28 11:02:54 -07002291 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s1", "s2", "s3", "s4"),
Makoto Onukide667372016-03-15 14:29:20 -07002292 getCallingUser());
2293
2294 assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
2295 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2296 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
2297 "s1", "s3");
2298 });
2299
2300 // Now clear pinned shortcuts. First, from launcher 1.
2301 runWithCaller(LAUNCHER_1, USER_0, () -> {
Makoto Onukid99c6f02016-03-28 11:02:54 -07002302 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list(), getCallingUser());
2303 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list(), getCallingUser());
Makoto Onukide667372016-03-15 14:29:20 -07002304
2305 assertEquals(0,
2306 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2307 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()).size());
2308 assertEquals(0,
2309 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2310 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()).size());
2311 });
2312 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2313 assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()), "s1");
2314 assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()), "s1", "s2");
2315 });
2316 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
2317 assertEquals(0, mManager.getDynamicShortcuts().size());
2318 assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()), "s2");
2319 });
2320
2321 // Clear all pins from launcher 2.
2322 runWithCaller(LAUNCHER_2, USER_0, () -> {
Makoto Onukid99c6f02016-03-28 11:02:54 -07002323 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list(), getCallingUser());
2324 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list(), getCallingUser());
Makoto Onukide667372016-03-15 14:29:20 -07002325
2326 assertEquals(0,
2327 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2328 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()).size());
2329 assertEquals(0,
2330 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2331 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()).size());
2332 });
2333 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2334 assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()), "s1");
2335 assertEquals(0, mManager.getPinnedShortcuts().size());
2336 });
2337 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
2338 assertEquals(0, mManager.getDynamicShortcuts().size());
2339 assertEquals(0, mManager.getPinnedShortcuts().size());
2340 });
Makoto Onuki6f7362d92016-03-04 13:39:41 -08002341 }
2342
Makoto Onuki35559d62017-11-06 16:26:32 -08002343 public void testPinShortcutAndGetPinnedShortcuts_assistant() {
2344 // Create some shortcuts.
2345 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2346 assertTrue(mManager.setDynamicShortcuts(list(
2347 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2348 });
2349
2350 // Pin some.
2351 runWithCaller(LAUNCHER_1, USER_0, () -> {
2352 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
2353 list("s3", "s4"), getCallingUser());
2354 });
2355
2356 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2357 assertTrue(mManager.setDynamicShortcuts(list(
2358 makeShortcut("s1"))));
2359 });
2360
2361 runWithCaller(LAUNCHER_2, USER_0, () -> {
2362 final ShortcutQuery allPinned = new ShortcutQuery().setQueryFlags(
2363 ShortcutQuery.FLAG_MATCH_PINNED_BY_ANY_LAUNCHER);
2364
2365 assertWith(mLauncherApps.getShortcuts(allPinned, HANDLE_USER_0))
2366 .isEmpty();
2367
2368 assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
2369 assertShortcutNotLaunched(CALLING_PACKAGE_1, "s3", USER_0);
2370 assertShortcutNotLaunched(CALLING_PACKAGE_1, "s4", USER_0);
2371
2372 // Make it the assistant app.
2373 mInternal.setShortcutHostPackage("assistant", LAUNCHER_2, USER_0);
2374 assertWith(mLauncherApps.getShortcuts(allPinned, HANDLE_USER_0))
2375 .haveIds("s3");
2376
2377 assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
2378 assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_0);
2379 assertShortcutNotLaunched(CALLING_PACKAGE_1, "s4", USER_0);
2380
2381 mInternal.setShortcutHostPackage("another-type", LAUNCHER_3, USER_0);
2382 assertWith(mLauncherApps.getShortcuts(allPinned, HANDLE_USER_0))
2383 .haveIds("s3");
2384
2385 mInternal.setShortcutHostPackage("assistant", null, USER_0);
2386 assertWith(mLauncherApps.getShortcuts(allPinned, HANDLE_USER_0))
2387 .isEmpty();
2388
2389 mInternal.setShortcutHostPackage("assistant", LAUNCHER_2, USER_0);
2390 assertWith(mLauncherApps.getShortcuts(allPinned, HANDLE_USER_0))
2391 .haveIds("s3");
2392
2393 mInternal.setShortcutHostPackage("assistant", LAUNCHER_1, USER_0);
2394 assertWith(mLauncherApps.getShortcuts(allPinned, HANDLE_USER_0))
2395 .isEmpty();
2396 });
2397 }
2398
Makoto Onukid99c6f02016-03-28 11:02:54 -07002399 public void testPinShortcutAndGetPinnedShortcuts_crossProfile_plusLaunch() {
2400 // Create some shortcuts.
2401 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2402 assertTrue(mManager.setDynamicShortcuts(list(
2403 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2404 });
2405 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
2406 assertTrue(mManager.setDynamicShortcuts(list(
2407 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2408 });
Makoto Onuki9c850012016-07-26 15:50:50 -07002409
2410 mRunningUsers.put(USER_10, true);
2411
Makoto Onukid99c6f02016-03-28 11:02:54 -07002412 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
2413 assertTrue(mManager.setDynamicShortcuts(list(
2414 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"),
2415 makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6"))));
2416 });
2417
2418 // Pin some shortcuts and see the result.
2419
2420 runWithCaller(LAUNCHER_1, USER_0, () -> {
2421 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
2422 list("s1"), HANDLE_USER_0);
2423
2424 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
2425 list("s1", "s2", "s3"), HANDLE_USER_0);
2426 });
2427
2428 runWithCaller(LAUNCHER_1, USER_P0, () -> {
2429 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
2430 list("s2"), HANDLE_USER_0);
2431
2432 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
2433 list("s2", "s3"), HANDLE_USER_0);
2434 });
2435
2436 runWithCaller(LAUNCHER_2, USER_P0, () -> {
2437 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
2438 list("s3"), HANDLE_USER_0);
2439
2440 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
2441 list("s3"), HANDLE_USER_0);
2442 });
2443
2444 runWithCaller(LAUNCHER_2, USER_10, () -> {
2445 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
2446 list("s1", "s2", "s3"), HANDLE_USER_10);
2447 });
2448
Makoto Onukide3c16c2017-01-26 11:39:31 -08002449 // First, make sure managed profile can't see other profiles.
2450 runWithCaller(LAUNCHER_1, USER_P1, () -> {
2451
2452 final ShortcutQuery q = new ShortcutQuery().setQueryFlags(
2453 ShortcutQuery.FLAG_MATCH_DYNAMIC | ShortcutQuery.FLAG_MATCH_PINNED
Makoto Onukia4f89b12017-10-05 10:37:55 -07002454 | ShortcutQuery.FLAG_MATCH_MANIFEST);
Makoto Onukide3c16c2017-01-26 11:39:31 -08002455
2456 // No shortcuts are visible.
2457 assertWith(mLauncherApps.getShortcuts(q, HANDLE_USER_0)).isEmpty();
2458
2459 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s1"), HANDLE_USER_0);
2460
2461 // Should have no effects.
2462 assertWith(mLauncherApps.getShortcuts(q, HANDLE_USER_0)).isEmpty();
2463
2464 assertShortcutNotLaunched(CALLING_PACKAGE_1, "s1", USER_0);
2465 });
2466
Makoto Onukid99c6f02016-03-28 11:02:54 -07002467 // Cross profile pinning.
2468 final int PIN_AND_DYNAMIC = ShortcutQuery.FLAG_GET_PINNED | ShortcutQuery.FLAG_GET_DYNAMIC;
2469
2470 runWithCaller(LAUNCHER_1, USER_0, () -> {
2471 assertShortcutIds(assertAllPinned(
2472 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2473 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2474 "s1");
2475 assertShortcutIds(assertAllDynamic(
2476 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2477 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2478 "s1", "s2", "s3");
2479 assertShortcutIds(assertAllDynamicOrPinned(
2480 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2481 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2482 "s1", "s2", "s3");
2483
2484 assertShortcutIds(assertAllPinned(
2485 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2486 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2487 "s1", "s2", "s3");
2488 assertShortcutIds(assertAllDynamic(
2489 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2490 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2491 "s1", "s2", "s3");
2492 assertShortcutIds(assertAllDynamicOrPinned(
2493 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2494 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2495 "s1", "s2", "s3");
2496
2497 assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
2498 assertShortcutLaunchable(CALLING_PACKAGE_1, "s2", USER_0);
2499 assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_0);
2500
2501 assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
2502 assertShortcutLaunchable(CALLING_PACKAGE_2, "s2", USER_0);
2503 assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2504
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002505 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002506 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002507 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002508 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002509 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002510 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002511 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002512 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002513 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002514 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002515 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002516 SecurityException.class);
Makoto Onukid99c6f02016-03-28 11:02:54 -07002517 });
2518 runWithCaller(LAUNCHER_1, USER_P0, () -> {
2519 assertShortcutIds(assertAllPinned(
2520 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2521 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2522 "s2");
2523 assertShortcutIds(assertAllDynamic(
2524 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2525 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2526 "s1", "s2", "s3");
2527 assertShortcutIds(assertAllDynamicOrPinned(
2528 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2529 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2530 "s1", "s2", "s3");
2531
2532 assertShortcutIds(assertAllPinned(
2533 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2534 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2535 "s2", "s3");
2536 assertShortcutIds(assertAllDynamic(
2537 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2538 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2539 "s1", "s2", "s3");
2540 assertShortcutIds(assertAllDynamicOrPinned(
2541 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2542 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2543 "s1", "s2", "s3");
2544
2545 assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
2546 assertShortcutLaunchable(CALLING_PACKAGE_1, "s2", USER_0);
2547 assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_0);
2548
2549 assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
2550 assertShortcutLaunchable(CALLING_PACKAGE_2, "s2", USER_0);
2551 assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2552
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002553 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002554 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002555 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002556 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002557 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002558 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002559 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002560 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002561 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002562 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002563 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002564 SecurityException.class);
Makoto Onukid99c6f02016-03-28 11:02:54 -07002565 });
2566 runWithCaller(LAUNCHER_2, USER_P0, () -> {
2567 assertShortcutIds(assertAllPinned(
2568 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2569 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2570 "s3");
2571 assertShortcutIds(assertAllDynamic(
2572 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2573 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2574 "s1", "s2", "s3");
2575 assertShortcutIds(assertAllDynamicOrPinned(
2576 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2577 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2578 "s1", "s2", "s3");
2579
2580 assertShortcutIds(assertAllPinned(
2581 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2582 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2583 "s3");
2584 assertShortcutIds(assertAllDynamic(
2585 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2586 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2587 "s1", "s2", "s3");
2588 assertShortcutIds(assertAllDynamicOrPinned(
2589 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2590 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2591 "s1", "s2", "s3");
2592
2593 assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
2594 assertShortcutLaunchable(CALLING_PACKAGE_1, "s2", USER_0);
2595 assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_0);
2596
2597 assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
2598 assertShortcutLaunchable(CALLING_PACKAGE_2, "s2", USER_0);
2599 assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2600
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002601 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002602 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002603 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002604 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002605 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002606 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002607 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002608 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002609 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002610 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002611 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002612 SecurityException.class);
Makoto Onukid99c6f02016-03-28 11:02:54 -07002613 });
2614 runWithCaller(LAUNCHER_2, USER_10, () -> {
2615 assertShortcutIds(assertAllPinned(
2616 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2617 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_10)),
2618 "s1", "s2", "s3");
2619 assertShortcutIds(assertAllDynamic(
2620 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2621 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_10)),
2622 "s1", "s2", "s3", "s4", "s5", "s6");
2623 assertShortcutIds(assertAllDynamicOrPinned(
2624 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2625 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_10)),
2626 "s1", "s2", "s3", "s4", "s5", "s6");
2627 });
2628
2629 // Remove some dynamic shortcuts.
2630
2631 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2632 assertTrue(mManager.setDynamicShortcuts(list(
2633 makeShortcut("s1"))));
2634 });
2635 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
2636 assertTrue(mManager.setDynamicShortcuts(list(
2637 makeShortcut("s1"))));
2638 });
2639 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
2640 assertTrue(mManager.setDynamicShortcuts(list(
2641 makeShortcut("s1"))));
2642 });
2643
2644 runWithCaller(LAUNCHER_1, USER_0, () -> {
2645 assertShortcutIds(assertAllPinned(
2646 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2647 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2648 "s1");
2649 assertShortcutIds(assertAllDynamic(
2650 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2651 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2652 "s1");
2653 assertShortcutIds(assertAllDynamicOrPinned(
2654 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2655 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2656 "s1");
2657
2658 assertShortcutIds(assertAllPinned(
2659 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2660 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2661 "s1", "s2", "s3");
2662 assertShortcutIds(assertAllDynamic(
2663 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2664 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2665 "s1");
2666 assertShortcutIds(assertAllDynamicOrPinned(
2667 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2668 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2669 "s1", "s2", "s3");
2670
2671 assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002672 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_0,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002673 ActivityNotFoundException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002674 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_0,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002675 ActivityNotFoundException.class);
Makoto Onukid99c6f02016-03-28 11:02:54 -07002676
2677 assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
2678 assertShortcutLaunchable(CALLING_PACKAGE_2, "s2", USER_0);
2679 assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2680
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002681 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002682 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002683 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002684 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002685 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002686 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002687 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002688 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002689 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002690 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002691 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002692 SecurityException.class);
Makoto Onukid99c6f02016-03-28 11:02:54 -07002693 });
2694 runWithCaller(LAUNCHER_1, USER_P0, () -> {
2695 assertShortcutIds(assertAllPinned(
2696 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2697 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2698 "s2");
2699 assertShortcutIds(assertAllDynamic(
2700 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2701 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2702 "s1");
2703 assertShortcutIds(assertAllDynamicOrPinned(
2704 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2705 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2706 "s1", "s2");
2707
2708 assertShortcutIds(assertAllPinned(
2709 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2710 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2711 "s2", "s3");
2712 assertShortcutIds(assertAllDynamic(
2713 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2714 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2715 "s1");
2716 assertShortcutIds(assertAllDynamicOrPinned(
2717 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2718 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2719 "s1", "s2", "s3");
2720
2721 assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
2722 assertShortcutLaunchable(CALLING_PACKAGE_1, "s2", USER_0);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002723 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_0,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002724 ActivityNotFoundException.class);
Makoto Onukid99c6f02016-03-28 11:02:54 -07002725
2726 assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
2727 assertShortcutLaunchable(CALLING_PACKAGE_2, "s2", USER_0);
2728 assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2729
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002730 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002731 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002732 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002733 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002734 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002735 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002736 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002737 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002738 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002739 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002740 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002741 SecurityException.class);
Makoto Onukid99c6f02016-03-28 11:02:54 -07002742 });
2743 runWithCaller(LAUNCHER_2, USER_P0, () -> {
2744 assertShortcutIds(assertAllPinned(
2745 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2746 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2747 "s3");
2748 assertShortcutIds(assertAllDynamic(
2749 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2750 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2751 "s1");
2752 assertShortcutIds(assertAllDynamicOrPinned(
2753 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2754 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2755 "s1", "s3");
2756
2757 assertShortcutIds(assertAllPinned(
2758 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2759 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2760 "s3");
2761 assertShortcutIds(assertAllDynamic(
2762 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2763 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2764 "s1");
2765 assertShortcutIds(assertAllDynamicOrPinned(
2766 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2767 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2768 "s1", "s3");
2769
2770 assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002771 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_0,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002772 ActivityNotFoundException.class);
Makoto Onukid99c6f02016-03-28 11:02:54 -07002773 assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_0);
2774
2775 assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002776 assertStartShortcutThrowsException(CALLING_PACKAGE_2, "s2", USER_0,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002777 ActivityNotFoundException.class);
Makoto Onukid99c6f02016-03-28 11:02:54 -07002778 assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2779
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002780 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002781 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002782 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002783 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002784 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002785 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002786 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002787 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002788 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002789 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002790 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002791 SecurityException.class);
Makoto Onukid99c6f02016-03-28 11:02:54 -07002792 });
2793 runWithCaller(LAUNCHER_2, USER_10, () -> {
2794 assertShortcutIds(assertAllPinned(
2795 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2796 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_10)),
2797 "s1", "s2", "s3");
2798 assertShortcutIds(assertAllDynamic(
2799 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2800 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_10)),
2801 "s1");
2802 assertShortcutIds(assertAllDynamicOrPinned(
2803 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2804 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_10)),
2805 "s1", "s2", "s3");
2806
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002807 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_0,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002808 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002809 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_0,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002810 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002811 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_0,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002812 SecurityException.class);
Makoto Onukid99c6f02016-03-28 11:02:54 -07002813
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002814 assertStartShortcutThrowsException(CALLING_PACKAGE_2, "s1", USER_0,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002815 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002816 assertStartShortcutThrowsException(CALLING_PACKAGE_2, "s2", USER_0,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002817 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002818 assertStartShortcutThrowsException(CALLING_PACKAGE_2, "s3", USER_0,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002819 SecurityException.class);
Makoto Onukid99c6f02016-03-28 11:02:54 -07002820
2821 assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_10);
2822 assertShortcutLaunchable(CALLING_PACKAGE_1, "s2", USER_10);
2823 assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_10);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002824 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002825 ActivityNotFoundException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002826 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002827 ActivityNotFoundException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002828 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002829 ActivityNotFoundException.class);
Makoto Onukid99c6f02016-03-28 11:02:54 -07002830 });
2831
2832 // Save & load and make sure we still have the same information.
2833 mService.saveDirtyInfo();
2834 initService();
2835 mService.handleUnlockUser(USER_0);
2836
2837 runWithCaller(LAUNCHER_1, USER_0, () -> {
2838 assertShortcutIds(assertAllPinned(
2839 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2840 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2841 "s1");
2842 assertShortcutIds(assertAllDynamic(
2843 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2844 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2845 "s1");
2846 assertShortcutIds(assertAllDynamicOrPinned(
2847 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2848 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2849 "s1");
2850
2851 assertShortcutIds(assertAllPinned(
2852 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2853 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2854 "s1", "s2", "s3");
2855 assertShortcutIds(assertAllDynamic(
2856 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2857 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2858 "s1");
2859 assertShortcutIds(assertAllDynamicOrPinned(
2860 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2861 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2862 "s1", "s2", "s3");
2863
2864 assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002865 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_0,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002866 ActivityNotFoundException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002867 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_0,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002868 ActivityNotFoundException.class);
Makoto Onukid99c6f02016-03-28 11:02:54 -07002869
2870 assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
2871 assertShortcutLaunchable(CALLING_PACKAGE_2, "s2", USER_0);
2872 assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2873
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002874 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002875 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002876 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002877 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002878 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002879 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002880 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002881 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002882 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002883 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002884 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002885 SecurityException.class);
Makoto Onukid99c6f02016-03-28 11:02:54 -07002886 });
2887 runWithCaller(LAUNCHER_1, USER_P0, () -> {
2888 assertShortcutIds(assertAllPinned(
2889 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2890 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2891 "s2");
2892 assertShortcutIds(assertAllDynamic(
2893 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2894 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2895 "s1");
2896 assertShortcutIds(assertAllDynamicOrPinned(
2897 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2898 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2899 "s1", "s2");
2900
2901 assertShortcutIds(assertAllPinned(
2902 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2903 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2904 "s2", "s3");
2905 assertShortcutIds(assertAllDynamic(
2906 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2907 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2908 "s1");
2909 assertShortcutIds(assertAllDynamicOrPinned(
2910 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2911 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2912 "s1", "s2", "s3");
2913
2914 assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
2915 assertShortcutLaunchable(CALLING_PACKAGE_1, "s2", USER_0);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002916 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_0,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002917 ActivityNotFoundException.class);
Makoto Onukid99c6f02016-03-28 11:02:54 -07002918
2919 assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
2920 assertShortcutLaunchable(CALLING_PACKAGE_2, "s2", USER_0);
2921 assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2922
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002923 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002924 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002925 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002926 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002927 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002928 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002929 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002930 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002931 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002932 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002933 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002934 SecurityException.class);
Makoto Onukid99c6f02016-03-28 11:02:54 -07002935 });
2936 runWithCaller(LAUNCHER_2, USER_P0, () -> {
2937 assertShortcutIds(assertAllPinned(
2938 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2939 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2940 "s3");
2941 assertShortcutIds(assertAllDynamic(
2942 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2943 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2944 "s1");
2945 assertShortcutIds(assertAllDynamicOrPinned(
2946 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
2947 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2948 "s1", "s3");
2949
2950 assertShortcutIds(assertAllPinned(
2951 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2952 /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
2953 "s3");
2954 assertShortcutIds(assertAllDynamic(
2955 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2956 /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
2957 "s1");
2958 assertShortcutIds(assertAllDynamicOrPinned(
2959 mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
2960 /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
2961 "s1", "s3");
2962
2963 assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002964 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_0,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002965 ActivityNotFoundException.class);
Makoto Onukid99c6f02016-03-28 11:02:54 -07002966 assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_0);
2967
2968 assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002969 assertStartShortcutThrowsException(CALLING_PACKAGE_2, "s2", USER_0,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002970 ActivityNotFoundException.class);
Makoto Onukid99c6f02016-03-28 11:02:54 -07002971 assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
2972
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002973 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002974 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002975 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002976 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002977 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002978 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002979 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002980 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002981 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002982 SecurityException.class);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07002983 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002984 SecurityException.class);
Makoto Onukid99c6f02016-03-28 11:02:54 -07002985 });
2986 }
2987
2988 public void testStartShortcut() {
Makoto Onuki6f7362d92016-03-04 13:39:41 -08002989 // Create some shortcuts.
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07002990 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2991 final ShortcutInfo s1_1 = makeShortcut(
2992 "s1",
2993 "Title 1",
2994 makeComponent(ShortcutActivity.class),
Makoto Onuki7e5c3e82016-07-22 11:57:05 -07002995 /* icon =*/ null,
Makoto Onukia4f89b12017-10-05 10:37:55 -07002996 new Intent[]{makeIntent(Intent.ACTION_ASSIST, ShortcutActivity2.class,
Makoto Onuki7e5c3e82016-07-22 11:57:05 -07002997 "key1", "val1", "nest", makeBundle("key", 123))
2998 .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK),
Makoto Onukia4f89b12017-10-05 10:37:55 -07002999 new Intent("act2").setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)},
Makoto Onuki7e5c3e82016-07-22 11:57:05 -07003000 /* rank */ 10);
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003001
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07003002 final ShortcutInfo s1_2 = makeShortcut(
3003 "s2",
3004 "Title 2",
Makoto Onuki99302b52017-03-29 12:42:26 -07003005 /* activity */ null,
3006 /* icon =*/ null,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07003007 makeIntent(Intent.ACTION_ASSIST, ShortcutActivity3.class),
Makoto Onuki99302b52017-03-29 12:42:26 -07003008 /* rank */ 12);
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003009
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07003010 final ShortcutInfo s1_3 = makeShortcut("s3");
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003011
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07003012 assertTrue(mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3)));
3013 });
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003014
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07003015 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
3016 final ShortcutInfo s2_1 = makeShortcut(
3017 "s1",
3018 "ABC",
3019 makeComponent(ShortcutActivity.class),
3020 /* icon =*/ null,
3021 makeIntent(Intent.ACTION_ANSWER, ShortcutActivity.class,
3022 "key1", "val1", "nest", makeBundle("key", 123)),
Makoto Onuki99302b52017-03-29 12:42:26 -07003023 /* weight */ 10);
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07003024 assertTrue(mManager.setDynamicShortcuts(list(s2_1)));
3025 });
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003026
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07003027 // Pin some.
3028 runWithCaller(LAUNCHER_1, USER_0, () -> {
3029 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
3030 list("s1", "s2"), getCallingUser());
3031
3032 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
3033 list("s1"), getCallingUser());
3034 });
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003035
3036 // Just to make it complicated, delete some.
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07003037 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3038 mManager.removeDynamicShortcuts(list("s2"));
3039 });
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003040
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07003041 runWithCaller(LAUNCHER_1, USER_0, () -> {
Makoto Onuki7e5c3e82016-07-22 11:57:05 -07003042 final Intent[] intents = launchShortcutAndGetIntents(CALLING_PACKAGE_1, "s1", USER_0);
3043 assertEquals(ShortcutActivity2.class.getName(),
3044 intents[0].getComponent().getClassName());
3045 assertEquals(Intent.ACTION_ASSIST,
3046 intents[0].getAction());
3047 assertEquals(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK,
3048 intents[0].getFlags());
3049
3050 assertEquals("act2",
3051 intents[1].getAction());
3052 assertEquals(Intent.FLAG_ACTIVITY_NO_ANIMATION,
3053 intents[1].getFlags());
3054
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07003055 assertEquals(
3056 ShortcutActivity3.class.getName(),
3057 launchShortcutAndGetIntent(CALLING_PACKAGE_1, "s2", USER_0)
3058 .getComponent().getClassName());
3059 assertEquals(
3060 ShortcutActivity.class.getName(),
3061 launchShortcutAndGetIntent(CALLING_PACKAGE_2, "s1", USER_0)
3062 .getComponent().getClassName());
Makoto Onukid99c6f02016-03-28 11:02:54 -07003063
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07003064 assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_0);
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003065
Makoto Onuki440a1ea2016-07-20 14:21:18 -07003066 assertShortcutNotLaunched("no-such-package", "s2", USER_0);
3067 assertShortcutNotLaunched(CALLING_PACKAGE_1, "xxxx", USER_0);
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07003068 });
Makoto Onukid99c6f02016-03-28 11:02:54 -07003069
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07003070 // LAUNCHER_1 is no longer the default launcher
3071 setDefaultLauncherChecker((pkg, userId) -> false);
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003072
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07003073 runWithCaller(LAUNCHER_1, USER_0, () -> {
3074 // Not the default launcher, but pinned shortcuts are still lauchable.
Makoto Onuki7e5c3e82016-07-22 11:57:05 -07003075 final Intent[] intents = launchShortcutAndGetIntents(CALLING_PACKAGE_1, "s1", USER_0);
3076 assertEquals(ShortcutActivity2.class.getName(),
3077 intents[0].getComponent().getClassName());
3078 assertEquals(Intent.ACTION_ASSIST,
3079 intents[0].getAction());
3080 assertEquals(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK,
3081 intents[0].getFlags());
3082
3083 assertEquals("act2",
3084 intents[1].getAction());
3085 assertEquals(Intent.FLAG_ACTIVITY_NO_ANIMATION,
3086 intents[1].getFlags());
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07003087 assertEquals(
3088 ShortcutActivity3.class.getName(),
3089 launchShortcutAndGetIntent(CALLING_PACKAGE_1, "s2", USER_0)
3090 .getComponent().getClassName());
3091 assertEquals(
3092 ShortcutActivity.class.getName(),
3093 launchShortcutAndGetIntent(CALLING_PACKAGE_2, "s1", USER_0)
3094 .getComponent().getClassName());
3095
3096 // Not pinned, so not lauchable.
3097 });
3098
3099 // Test inner errors.
3100 runWithCaller(LAUNCHER_1, USER_0, () -> {
3101 // Not launchable.
Makoto Onuki440a1ea2016-07-20 14:21:18 -07003102 doReturn(ActivityManager.START_CLASS_NOT_FOUND)
Wale Ogunwale6767eae2018-05-03 15:52:51 -07003103 .when(mMockActivityTaskManagerInternal).startActivitiesAsPackage(
Makoto Onukia4f89b12017-10-05 10:37:55 -07003104 anyStringOrNull(), anyInt(),
3105 anyOrNull(Intent[].class), anyOrNull(Bundle.class));
Makoto Onuki440a1ea2016-07-20 14:21:18 -07003106 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_0,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07003107 ActivityNotFoundException.class);
3108
3109 // Still not launchable.
Makoto Onuki440a1ea2016-07-20 14:21:18 -07003110 doReturn(ActivityManager.START_CLASS_NOT_FOUND)
Wale Ogunwale6767eae2018-05-03 15:52:51 -07003111 .when(mMockActivityTaskManagerInternal)
Makoto Onuki440a1ea2016-07-20 14:21:18 -07003112 .startActivitiesAsPackage(
Makoto Onukid0010c52017-03-30 14:17:35 -07003113 anyStringOrNull(), anyInt(),
3114 anyOrNull(Intent[].class), anyOrNull(Bundle.class));
Makoto Onuki440a1ea2016-07-20 14:21:18 -07003115 assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_0,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07003116 ActivityNotFoundException.class);
3117 });
3118
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003119
3120 // TODO Check extra, etc
3121 }
3122
Makoto Onuki4dbe0de2016-03-14 17:31:49 -07003123 public void testLauncherCallback() throws Throwable {
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07003124 // Disable throttling for this test.
3125 mService.updateConfigurationLocked(
3126 ConfigConstants.KEY_MAX_UPDATES_PER_INTERVAL + "=99999999,"
3127 + ConfigConstants.KEY_MAX_SHORTCUTS + "=99999999"
3128 );
3129
Makoto Onukib08790c2016-06-23 14:05:46 -07003130 setCaller(LAUNCHER_1, USER_0);
Makoto Onuki4dbe0de2016-03-14 17:31:49 -07003131
Makoto Onukib08790c2016-06-23 14:05:46 -07003132 assertForLauncherCallback(mLauncherApps, () -> {
3133 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3134 assertTrue(mManager.setDynamicShortcuts(list(
3135 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
3136 });
3137 }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07003138 .haveIds("s1", "s2", "s3")
3139 .areAllWithKeyFieldsOnly()
3140 .areAllDynamic();
Makoto Onuki4dbe0de2016-03-14 17:31:49 -07003141
3142 // From different package.
Makoto Onukib08790c2016-06-23 14:05:46 -07003143 assertForLauncherCallback(mLauncherApps, () -> {
3144 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
3145 assertTrue(mManager.setDynamicShortcuts(list(
3146 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
3147 });
3148 }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_2, HANDLE_USER_0)
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07003149 .haveIds("s1", "s2", "s3")
3150 .areAllWithKeyFieldsOnly()
3151 .areAllDynamic();
Makoto Onuki4dbe0de2016-03-14 17:31:49 -07003152
Makoto Onuki9c850012016-07-26 15:50:50 -07003153 mRunningUsers.put(USER_10, true);
3154
Makoto Onuki4dbe0de2016-03-14 17:31:49 -07003155 // Different user, callback shouldn't be called.
Makoto Onukib08790c2016-06-23 14:05:46 -07003156 assertForLauncherCallback(mLauncherApps, () -> {
3157 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
3158 assertTrue(mManager.setDynamicShortcuts(list(
3159 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
3160 });
3161 }).assertNoCallbackCalled();
3162
Makoto Onuki4dbe0de2016-03-14 17:31:49 -07003163
Makoto Onukib6d35232016-04-04 15:57:17 -07003164 // Test for addDynamicShortcuts.
Makoto Onukib08790c2016-06-23 14:05:46 -07003165 assertForLauncherCallback(mLauncherApps, () -> {
3166 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3167 assertTrue(mManager.addDynamicShortcuts(list(makeShortcut("s4"))));
3168 });
3169 }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07003170 .haveIds("s1", "s2", "s3", "s4")
3171 .areAllWithKeyFieldsOnly()
3172 .areAllDynamic();
Makoto Onuki4dbe0de2016-03-14 17:31:49 -07003173
3174 // Test for remove
Makoto Onukib08790c2016-06-23 14:05:46 -07003175 assertForLauncherCallback(mLauncherApps, () -> {
3176 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3177 mManager.removeDynamicShortcuts(list("s1"));
3178 });
3179 }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07003180 .haveIds("s2", "s3", "s4")
3181 .areAllWithKeyFieldsOnly()
3182 .areAllDynamic();
Makoto Onuki4dbe0de2016-03-14 17:31:49 -07003183
3184 // Test for update
Makoto Onukib08790c2016-06-23 14:05:46 -07003185 assertForLauncherCallback(mLauncherApps, () -> {
3186 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3187 assertTrue(mManager.updateShortcuts(list(
3188 makeShortcut("s1"), makeShortcut("s2"))));
3189 });
3190 }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
3191 // All remaining shortcuts will be passed regardless of what's been updated.
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07003192 .haveIds("s2", "s3", "s4")
3193 .areAllWithKeyFieldsOnly()
3194 .areAllDynamic();
Makoto Onuki4dbe0de2016-03-14 17:31:49 -07003195
3196 // Test for deleteAll
Makoto Onukib08790c2016-06-23 14:05:46 -07003197 assertForLauncherCallback(mLauncherApps, () -> {
3198 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3199 mManager.removeAllDynamicShortcuts();
3200 });
3201 }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07003202 .isEmpty();
3203
3204 // Update package1 with manifest shortcuts
Makoto Onukib08790c2016-06-23 14:05:46 -07003205 assertForLauncherCallback(mLauncherApps, () -> {
3206 addManifestShortcutResource(
3207 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
3208 R.xml.shortcut_2);
3209 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07003210 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onukib08790c2016-06-23 14:05:46 -07003211 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
3212 }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07003213 .areAllManifest()
3214 .areAllWithKeyFieldsOnly()
3215 .haveIds("ms1", "ms2");
3216
3217 // Make sure pinned shortcuts are passed too.
3218 // 1. Add dynamic shortcuts.
3219 runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
3220 assertTrue(mManager.setDynamicShortcuts(list(
3221 makeShortcut("s1"), makeShortcut("s2"))));
3222 });
3223
3224 // 2. Pin some.
3225 runWithCaller(LAUNCHER_1, UserHandle.USER_SYSTEM, () -> {
3226 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms2", "s2"), HANDLE_USER_0);
3227 });
3228 runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
3229 assertWith(getCallerShortcuts())
3230 .haveIds("ms1", "ms2", "s1", "s2")
3231 .areAllEnabled()
3232
3233 .selectByIds("ms1", "ms2")
3234 .areAllManifest()
3235
3236 .revertToOriginalList()
3237 .selectByIds("s1", "s2")
3238 .areAllDynamic()
Makoto Onukia4f89b12017-10-05 10:37:55 -07003239 ;
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07003240 });
3241
3242 // 3 Update the app with no manifest shortcuts. (Pinned one will survive.)
3243 addManifestShortcutResource(
3244 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
3245 R.xml.shortcut_0);
3246 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07003247 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07003248 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
3249
Makoto Onukib08790c2016-06-23 14:05:46 -07003250 assertForLauncherCallback(mLauncherApps, () -> {
3251 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3252 mManager.removeDynamicShortcuts(list("s2"));
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07003253
Makoto Onukib08790c2016-06-23 14:05:46 -07003254 assertWith(getCallerShortcuts())
3255 .haveIds("ms2", "s1", "s2")
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07003256
Makoto Onukib08790c2016-06-23 14:05:46 -07003257 .selectByIds("ms2")
3258 .areAllNotManifest()
3259 .areAllPinned()
3260 .areAllImmutable()
3261 .areAllDisabled()
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07003262
Makoto Onukib08790c2016-06-23 14:05:46 -07003263 .revertToOriginalList()
3264 .selectByIds("s1")
3265 .areAllDynamic()
3266 .areAllNotPinned()
3267 .areAllEnabled()
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07003268
Makoto Onukib08790c2016-06-23 14:05:46 -07003269 .revertToOriginalList()
3270 .selectByIds("s2")
3271 .areAllNotDynamic()
3272 .areAllPinned()
3273 .areAllEnabled()
3274 ;
3275 });
3276 }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07003277 .haveIds("ms2", "s1", "s2")
3278 .areAllWithKeyFieldsOnly();
Makoto Onukicdc78f72016-03-21 15:47:52 -07003279
3280 // Remove CALLING_PACKAGE_2
Makoto Onukib08790c2016-06-23 14:05:46 -07003281 assertForLauncherCallback(mLauncherApps, () -> {
3282 uninstallPackage(USER_0, CALLING_PACKAGE_2);
3283 mService.cleanUpPackageLocked(CALLING_PACKAGE_2, USER_0, USER_0,
3284 /* appStillExists = */ false);
3285 }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_2, HANDLE_USER_0)
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07003286 .isEmpty();
Makoto Onuki4dbe0de2016-03-14 17:31:49 -07003287 }
3288
Makoto Onukiac214972016-04-04 10:19:45 -07003289 public void testLauncherCallback_crossProfile() throws Throwable {
3290 prepareCrossProfileDataSet();
3291
3292 final Handler h = new Handler(Looper.getMainLooper());
3293
3294 final LauncherApps.Callback c0_1 = mock(LauncherApps.Callback.class);
3295 final LauncherApps.Callback c0_2 = mock(LauncherApps.Callback.class);
3296 final LauncherApps.Callback c0_3 = mock(LauncherApps.Callback.class);
3297 final LauncherApps.Callback c0_4 = mock(LauncherApps.Callback.class);
3298
3299 final LauncherApps.Callback cP0_1 = mock(LauncherApps.Callback.class);
Makoto Onukide3c16c2017-01-26 11:39:31 -08003300 final LauncherApps.Callback cP1_1 = mock(LauncherApps.Callback.class);
Makoto Onukiac214972016-04-04 10:19:45 -07003301 final LauncherApps.Callback c10_1 = mock(LauncherApps.Callback.class);
3302 final LauncherApps.Callback c10_2 = mock(LauncherApps.Callback.class);
3303 final LauncherApps.Callback c11_1 = mock(LauncherApps.Callback.class);
3304
3305 final List<LauncherApps.Callback> all =
3306 list(c0_1, c0_2, c0_3, c0_4, cP0_1, c10_1, c11_1);
3307
3308 setDefaultLauncherChecker((pkg, userId) -> {
3309 switch (userId) {
3310 case USER_0:
3311 return LAUNCHER_2.equals(pkg);
3312 case USER_P0:
3313 return LAUNCHER_1.equals(pkg);
Makoto Onukide3c16c2017-01-26 11:39:31 -08003314 case USER_P1:
3315 return LAUNCHER_1.equals(pkg);
Makoto Onukiac214972016-04-04 10:19:45 -07003316 case USER_10:
3317 return LAUNCHER_1.equals(pkg);
3318 case USER_11:
3319 return LAUNCHER_1.equals(pkg);
3320 default:
3321 return false;
3322 }
3323 });
3324
3325 runWithCaller(LAUNCHER_1, USER_0, () -> mLauncherApps.registerCallback(c0_1, h));
3326 runWithCaller(LAUNCHER_2, USER_0, () -> mLauncherApps.registerCallback(c0_2, h));
3327 runWithCaller(LAUNCHER_3, USER_0, () -> mLauncherApps.registerCallback(c0_3, h));
3328 runWithCaller(LAUNCHER_4, USER_0, () -> mLauncherApps.registerCallback(c0_4, h));
3329 runWithCaller(LAUNCHER_1, USER_P0, () -> mLauncherApps.registerCallback(cP0_1, h));
Makoto Onukide3c16c2017-01-26 11:39:31 -08003330 runWithCaller(LAUNCHER_1, USER_P1, () -> mLauncherApps.registerCallback(cP1_1, h));
Makoto Onukiac214972016-04-04 10:19:45 -07003331 runWithCaller(LAUNCHER_1, USER_10, () -> mLauncherApps.registerCallback(c10_1, h));
3332 runWithCaller(LAUNCHER_2, USER_10, () -> mLauncherApps.registerCallback(c10_2, h));
3333 runWithCaller(LAUNCHER_1, USER_11, () -> mLauncherApps.registerCallback(c11_1, h));
3334
3335 // User 0.
3336
3337 resetAll(all);
3338 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
Makoto Onukib6d35232016-04-04 15:57:17 -07003339 mManager.removeDynamicShortcuts(list());
Makoto Onukiac214972016-04-04 10:19:45 -07003340 });
3341 waitOnMainThread();
3342
3343 assertCallbackNotReceived(c0_1);
3344 assertCallbackNotReceived(c0_3);
3345 assertCallbackNotReceived(c0_4);
3346 assertCallbackNotReceived(c10_1);
3347 assertCallbackNotReceived(c10_2);
3348 assertCallbackNotReceived(c11_1);
3349 assertCallbackReceived(c0_2, HANDLE_USER_0, CALLING_PACKAGE_1, "s1", "s2", "s3");
3350 assertCallbackReceived(cP0_1, HANDLE_USER_0, CALLING_PACKAGE_1, "s1", "s2", "s3", "s4");
Makoto Onukide3c16c2017-01-26 11:39:31 -08003351 assertCallbackNotReceived(cP1_1);
Makoto Onukiac214972016-04-04 10:19:45 -07003352
3353 // User 0, different package.
3354
3355 resetAll(all);
3356 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
Makoto Onukib6d35232016-04-04 15:57:17 -07003357 mManager.removeDynamicShortcuts(list());
Makoto Onukiac214972016-04-04 10:19:45 -07003358 });
3359 waitOnMainThread();
3360
3361 assertCallbackNotReceived(c0_1);
3362 assertCallbackNotReceived(c0_3);
3363 assertCallbackNotReceived(c0_4);
3364 assertCallbackNotReceived(c10_1);
3365 assertCallbackNotReceived(c10_2);
3366 assertCallbackNotReceived(c11_1);
3367 assertCallbackReceived(c0_2, HANDLE_USER_0, CALLING_PACKAGE_3, "s1", "s2", "s3", "s4");
3368 assertCallbackReceived(cP0_1, HANDLE_USER_0, CALLING_PACKAGE_3,
3369 "s1", "s2", "s3", "s4", "s5", "s6");
Makoto Onukide3c16c2017-01-26 11:39:31 -08003370 assertCallbackNotReceived(cP1_1);
Makoto Onukiac214972016-04-04 10:19:45 -07003371
Makoto Onuki9c850012016-07-26 15:50:50 -07003372 // Work profile.
Makoto Onukiac214972016-04-04 10:19:45 -07003373 resetAll(all);
3374 runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
Makoto Onukib6d35232016-04-04 15:57:17 -07003375 mManager.removeDynamicShortcuts(list());
Makoto Onukiac214972016-04-04 10:19:45 -07003376 });
3377 waitOnMainThread();
3378
3379 assertCallbackNotReceived(c0_1);
3380 assertCallbackNotReceived(c0_3);
3381 assertCallbackNotReceived(c0_4);
3382 assertCallbackNotReceived(c10_1);
3383 assertCallbackNotReceived(c10_2);
3384 assertCallbackNotReceived(c11_1);
3385 assertCallbackReceived(c0_2, HANDLE_USER_P0, CALLING_PACKAGE_1, "s1", "s2", "s3", "s5");
3386 assertCallbackReceived(cP0_1, HANDLE_USER_P0, CALLING_PACKAGE_1, "s1", "s2", "s3", "s4");
Makoto Onukide3c16c2017-01-26 11:39:31 -08003387 assertCallbackNotReceived(cP1_1);
Makoto Onukiac214972016-04-04 10:19:45 -07003388
3389 // Normal secondary user.
Makoto Onukia2241832016-07-06 13:28:37 -07003390 mRunningUsers.put(USER_10, true);
Makoto Onukiac214972016-04-04 10:19:45 -07003391
3392 resetAll(all);
3393 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
Makoto Onukib6d35232016-04-04 15:57:17 -07003394 mManager.removeDynamicShortcuts(list());
Makoto Onukiac214972016-04-04 10:19:45 -07003395 });
3396 waitOnMainThread();
3397
3398 assertCallbackNotReceived(c0_1);
3399 assertCallbackNotReceived(c0_2);
3400 assertCallbackNotReceived(c0_3);
3401 assertCallbackNotReceived(c0_4);
3402 assertCallbackNotReceived(cP0_1);
3403 assertCallbackNotReceived(c10_2);
3404 assertCallbackNotReceived(c11_1);
3405 assertCallbackReceived(c10_1, HANDLE_USER_10, CALLING_PACKAGE_1,
3406 "x1", "x2", "x3", "x4", "x5");
Makoto Onukide3c16c2017-01-26 11:39:31 -08003407 assertCallbackNotReceived(cP1_1);
Makoto Onukiac214972016-04-04 10:19:45 -07003408 }
3409
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003410 // === Test for persisting ===
3411
3412 public void testSaveAndLoadUser_empty() {
Makoto Onukid99c6f02016-03-28 11:02:54 -07003413 assertTrue(mManager.setDynamicShortcuts(list()));
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003414
3415 Log.i(TAG, "Saved state");
3416 dumpsysOnLogcat();
3417 dumpUserFile(0);
3418
3419 // Restore.
Makoto Onukiaa8b94a2016-03-17 13:14:05 -07003420 mService.saveDirtyInfo();
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003421 initService();
3422
3423 assertEquals(0, mManager.getDynamicShortcuts().size());
3424 }
3425
3426 /**
3427 * Try save and load, also stop/start the user.
3428 */
3429 public void testSaveAndLoadUser() {
3430 // First, create some shortcuts and save.
Makoto Onuki41066a62016-03-09 16:18:44 -08003431 runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
Makoto Onuki4dbe0de2016-03-14 17:31:49 -07003432 final Icon icon1 = Icon.createWithResource(getTestContext(), R.drawable.black_64x16);
Makoto Onuki41066a62016-03-09 16:18:44 -08003433 final Icon icon2 = Icon.createWithBitmap(BitmapFactory.decodeResource(
Makoto Onuki4dbe0de2016-03-14 17:31:49 -07003434 getTestContext().getResources(), R.drawable.icon2));
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003435
Makoto Onuki41066a62016-03-09 16:18:44 -08003436 final ShortcutInfo si1 = makeShortcut(
3437 "s1",
3438 "title1-1",
3439 makeComponent(ShortcutActivity.class),
3440 icon1,
3441 makeIntent(Intent.ACTION_ASSIST, ShortcutActivity2.class,
3442 "key1", "val1", "nest", makeBundle("key", 123)),
Makoto Onuki99302b52017-03-29 12:42:26 -07003443 /* weight */ 10);
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003444
Makoto Onuki41066a62016-03-09 16:18:44 -08003445 final ShortcutInfo si2 = makeShortcut(
3446 "s2",
3447 "title1-2",
3448 /* activity */ null,
3449 icon2,
3450 makeIntent(Intent.ACTION_ASSIST, ShortcutActivity3.class),
Makoto Onuki99302b52017-03-29 12:42:26 -07003451 /* weight */ 12);
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003452
Makoto Onukid99c6f02016-03-28 11:02:54 -07003453 assertTrue(mManager.setDynamicShortcuts(list(si1, si2)));
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003454
Makoto Onuki41066a62016-03-09 16:18:44 -08003455 assertEquals(START_TIME + INTERVAL, mManager.getRateLimitResetTime());
3456 assertEquals(2, mManager.getRemainingCallCount());
3457 });
3458 runWithCaller(CALLING_PACKAGE_2, UserHandle.USER_SYSTEM, () -> {
Makoto Onuki4dbe0de2016-03-14 17:31:49 -07003459 final Icon icon1 = Icon.createWithResource(getTestContext(), R.drawable.black_16x64);
Makoto Onuki41066a62016-03-09 16:18:44 -08003460 final Icon icon2 = Icon.createWithBitmap(BitmapFactory.decodeResource(
Makoto Onuki4dbe0de2016-03-14 17:31:49 -07003461 getTestContext().getResources(), R.drawable.icon2));
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003462
Makoto Onuki41066a62016-03-09 16:18:44 -08003463 final ShortcutInfo si1 = makeShortcut(
3464 "s1",
3465 "title2-1",
3466 makeComponent(ShortcutActivity.class),
3467 icon1,
3468 makeIntent(Intent.ACTION_ASSIST, ShortcutActivity2.class,
Makoto Onuki99302b52017-03-29 12:42:26 -07003469 "key1", "val1", "nest", makeBundle("key", 123)),
3470 /* weight */ 10);
Makoto Onuki41066a62016-03-09 16:18:44 -08003471
3472 final ShortcutInfo si2 = makeShortcut(
3473 "s2",
3474 "title2-2",
3475 /* activity */ null,
3476 icon2,
3477 makeIntent(Intent.ACTION_ASSIST, ShortcutActivity3.class),
Makoto Onuki99302b52017-03-29 12:42:26 -07003478 /* weight */ 12);
Makoto Onuki41066a62016-03-09 16:18:44 -08003479
Makoto Onukid99c6f02016-03-28 11:02:54 -07003480 assertTrue(mManager.setDynamicShortcuts(list(si1, si2)));
Makoto Onuki41066a62016-03-09 16:18:44 -08003481
3482 assertEquals(START_TIME + INTERVAL, mManager.getRateLimitResetTime());
3483 assertEquals(2, mManager.getRemainingCallCount());
3484 });
Makoto Onuki9c850012016-07-26 15:50:50 -07003485
3486 mRunningUsers.put(USER_10, true);
3487
Makoto Onuki41066a62016-03-09 16:18:44 -08003488 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
Makoto Onuki4dbe0de2016-03-14 17:31:49 -07003489 final Icon icon1 = Icon.createWithResource(getTestContext(), R.drawable.black_64x64);
Makoto Onuki41066a62016-03-09 16:18:44 -08003490 final Icon icon2 = Icon.createWithBitmap(BitmapFactory.decodeResource(
Makoto Onuki4dbe0de2016-03-14 17:31:49 -07003491 getTestContext().getResources(), R.drawable.icon2));
Makoto Onuki41066a62016-03-09 16:18:44 -08003492
3493 final ShortcutInfo si1 = makeShortcut(
3494 "s1",
3495 "title10-1-1",
3496 makeComponent(ShortcutActivity.class),
3497 icon1,
3498 makeIntent(Intent.ACTION_ASSIST, ShortcutActivity2.class,
3499 "key1", "val1", "nest", makeBundle("key", 123)),
Makoto Onuki99302b52017-03-29 12:42:26 -07003500 /* weight */ 10);
Makoto Onuki41066a62016-03-09 16:18:44 -08003501
3502 final ShortcutInfo si2 = makeShortcut(
3503 "s2",
3504 "title10-1-2",
3505 /* activity */ null,
3506 icon2,
3507 makeIntent(Intent.ACTION_ASSIST, ShortcutActivity3.class),
Makoto Onuki99302b52017-03-29 12:42:26 -07003508 /* weight */ 12);
Makoto Onuki41066a62016-03-09 16:18:44 -08003509
Makoto Onukid99c6f02016-03-28 11:02:54 -07003510 assertTrue(mManager.setDynamicShortcuts(list(si1, si2)));
Makoto Onuki41066a62016-03-09 16:18:44 -08003511
3512 assertEquals(START_TIME + INTERVAL, mManager.getRateLimitResetTime());
3513 assertEquals(2, mManager.getRemainingCallCount());
3514 });
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003515
Makoto Onuki10305202016-07-14 18:14:08 -07003516 mService.getShortcutsForTest().get(UserHandle.USER_SYSTEM).setLauncher(
Makoto Onukic51b2872016-05-04 15:24:50 -07003517 new ComponentName("pkg1", "class"));
Makoto Onuki2d5b4652016-03-11 16:09:54 -08003518
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003519 // Restore.
Makoto Onukiaa8b94a2016-03-17 13:14:05 -07003520 mService.saveDirtyInfo();
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003521 initService();
3522
3523 // Before the load, the map should be empty.
3524 assertEquals(0, mService.getShortcutsForTest().size());
3525
3526 // this will pre-load the per-user info.
Makoto Onukicdc78f72016-03-21 15:47:52 -07003527 mService.handleUnlockUser(UserHandle.USER_SYSTEM);
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003528
3529 // Now it's loaded.
3530 assertEquals(1, mService.getShortcutsForTest().size());
3531
Makoto Onuki41066a62016-03-09 16:18:44 -08003532 runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
3533 assertShortcutIds(assertAllDynamic(assertAllHaveIntents(assertAllHaveIcon(
3534 mManager.getDynamicShortcuts()))), "s1", "s2");
3535 assertEquals(2, mManager.getRemainingCallCount());
3536
3537 assertEquals("title1-1", getCallerShortcut("s1").getTitle());
3538 assertEquals("title1-2", getCallerShortcut("s2").getTitle());
3539 });
3540 runWithCaller(CALLING_PACKAGE_2, UserHandle.USER_SYSTEM, () -> {
3541 assertShortcutIds(assertAllDynamic(assertAllHaveIntents(assertAllHaveIcon(
3542 mManager.getDynamicShortcuts()))), "s1", "s2");
3543 assertEquals(2, mManager.getRemainingCallCount());
3544
3545 assertEquals("title2-1", getCallerShortcut("s1").getTitle());
3546 assertEquals("title2-2", getCallerShortcut("s2").getTitle());
3547 });
3548
Makoto Onuki2d5b4652016-03-11 16:09:54 -08003549 assertEquals("pkg1", mService.getShortcutsForTest().get(UserHandle.USER_SYSTEM)
Makoto Onuki10305202016-07-14 18:14:08 -07003550 .getLastKnownLauncher().getPackageName());
Makoto Onuki2d5b4652016-03-11 16:09:54 -08003551
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003552 // Start another user
Makoto Onukicdc78f72016-03-21 15:47:52 -07003553 mService.handleUnlockUser(USER_10);
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003554
3555 // Now the size is 2.
3556 assertEquals(2, mService.getShortcutsForTest().size());
3557
Makoto Onuki41066a62016-03-09 16:18:44 -08003558 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
3559 assertShortcutIds(assertAllDynamic(assertAllHaveIntents(assertAllHaveIcon(
3560 mManager.getDynamicShortcuts()))), "s1", "s2");
3561 assertEquals(2, mManager.getRemainingCallCount());
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003562
Makoto Onuki41066a62016-03-09 16:18:44 -08003563 assertEquals("title10-1-1", getCallerShortcut("s1").getTitle());
3564 assertEquals("title10-1-2", getCallerShortcut("s2").getTitle());
3565 });
Makoto Onuki10305202016-07-14 18:14:08 -07003566 assertNull(mService.getShortcutsForTest().get(USER_10).getLastKnownLauncher());
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003567
3568 // Try stopping the user
Makoto Onuki01ce92b2017-04-28 12:24:16 -07003569 mService.handleStopUser(USER_10);
Makoto Onuki6f7362d92016-03-04 13:39:41 -08003570
3571 // Now it's unloaded.
3572 assertEquals(1, mService.getShortcutsForTest().size());
3573
3574 // TODO Check all other fields
3575 }
Makoto Onuki2d5b4652016-03-11 16:09:54 -08003576
Makoto Onukicdc78f72016-03-21 15:47:52 -07003577 public void testCleanupPackage() {
3578 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
Makoto Onukid99c6f02016-03-28 11:02:54 -07003579 assertTrue(mManager.setDynamicShortcuts(list(
Makoto Onukicdc78f72016-03-21 15:47:52 -07003580 makeShortcut("s0_1"))));
3581 });
3582 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
Makoto Onukid99c6f02016-03-28 11:02:54 -07003583 assertTrue(mManager.setDynamicShortcuts(list(
Makoto Onukicdc78f72016-03-21 15:47:52 -07003584 makeShortcut("s0_2"))));
3585 });
3586 runWithCaller(LAUNCHER_1, USER_0, () -> {
Makoto Onukid99c6f02016-03-28 11:02:54 -07003587 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s0_1"),
3588 HANDLE_USER_0);
3589 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s0_2"),
3590 HANDLE_USER_0);
Makoto Onukicdc78f72016-03-21 15:47:52 -07003591 });
3592 runWithCaller(LAUNCHER_2, USER_0, () -> {
Makoto Onukid99c6f02016-03-28 11:02:54 -07003593 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s0_1"),
3594 HANDLE_USER_0);
3595 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s0_2"),
3596 HANDLE_USER_0);
Makoto Onukicdc78f72016-03-21 15:47:52 -07003597 });
3598
Makoto Onuki9c850012016-07-26 15:50:50 -07003599 mRunningUsers.put(USER_10, true);
3600
Makoto Onukicdc78f72016-03-21 15:47:52 -07003601 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
Makoto Onukid99c6f02016-03-28 11:02:54 -07003602 assertTrue(mManager.setDynamicShortcuts(list(
Makoto Onukicdc78f72016-03-21 15:47:52 -07003603 makeShortcut("s10_1"))));
3604 });
3605 runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
Makoto Onukid99c6f02016-03-28 11:02:54 -07003606 assertTrue(mManager.setDynamicShortcuts(list(
Makoto Onukicdc78f72016-03-21 15:47:52 -07003607 makeShortcut("s10_2"))));
3608 });
3609 runWithCaller(LAUNCHER_1, USER_10, () -> {
Makoto Onukid99c6f02016-03-28 11:02:54 -07003610 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s10_1"),
3611 HANDLE_USER_10);
3612 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s10_2"),
3613 HANDLE_USER_10);
Makoto Onukicdc78f72016-03-21 15:47:52 -07003614 });
3615 runWithCaller(LAUNCHER_2, USER_10, () -> {
Makoto Onukid99c6f02016-03-28 11:02:54 -07003616 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s10_1"),
3617 HANDLE_USER_10);
3618 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s10_2"),
3619 HANDLE_USER_10);
Makoto Onukicdc78f72016-03-21 15:47:52 -07003620 });
3621
3622 // Remove all dynamic shortcuts; now all shortcuts are just pinned.
3623 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
Makoto Onukib6d35232016-04-04 15:57:17 -07003624 mManager.removeAllDynamicShortcuts();
Makoto Onukicdc78f72016-03-21 15:47:52 -07003625 });
3626 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
Makoto Onukib6d35232016-04-04 15:57:17 -07003627 mManager.removeAllDynamicShortcuts();
Makoto Onukicdc78f72016-03-21 15:47:52 -07003628 });
3629 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
Makoto Onukib6d35232016-04-04 15:57:17 -07003630 mManager.removeAllDynamicShortcuts();
Makoto Onukicdc78f72016-03-21 15:47:52 -07003631 });
3632 runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
Makoto Onukib6d35232016-04-04 15:57:17 -07003633 mManager.removeAllDynamicShortcuts();
Makoto Onukicdc78f72016-03-21 15:47:52 -07003634 });
3635
3636
Makoto Onukia4f89b12017-10-05 10:37:55 -07003637 final SparseArray<ShortcutUser> users = mService.getShortcutsForTest();
Makoto Onukicdc78f72016-03-21 15:47:52 -07003638 assertEquals(2, users.size());
3639 assertEquals(USER_0, users.keyAt(0));
3640 assertEquals(USER_10, users.keyAt(1));
3641
Makoto Onukia4f89b12017-10-05 10:37:55 -07003642 final ShortcutUser user0 = users.get(USER_0);
3643 final ShortcutUser user10 = users.get(USER_10);
Makoto Onukicdc78f72016-03-21 15:47:52 -07003644
3645
3646 // Check the registered packages.
Makoto Onukid99c6f02016-03-28 11:02:54 -07003647 dumpsysOnLogcat();
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003648 assertEquals(set(CALLING_PACKAGE_1, CALLING_PACKAGE_2),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003649 hashSet(user0.getAllPackagesForTest().keySet()));
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003650 assertEquals(set(CALLING_PACKAGE_1, CALLING_PACKAGE_2),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003651 hashSet(user10.getAllPackagesForTest().keySet()));
Makoto Onukid99c6f02016-03-28 11:02:54 -07003652 assertEquals(
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003653 set(PackageWithUser.of(USER_0, LAUNCHER_1),
Makoto Onukid99c6f02016-03-28 11:02:54 -07003654 PackageWithUser.of(USER_0, LAUNCHER_2)),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003655 hashSet(user0.getAllLaunchersForTest().keySet()));
Makoto Onukid99c6f02016-03-28 11:02:54 -07003656 assertEquals(
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003657 set(PackageWithUser.of(USER_10, LAUNCHER_1),
Makoto Onukid99c6f02016-03-28 11:02:54 -07003658 PackageWithUser.of(USER_10, LAUNCHER_2)),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003659 hashSet(user10.getAllLaunchersForTest().keySet()));
Makoto Onukicdc78f72016-03-21 15:47:52 -07003660 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_0),
3661 "s0_1", "s0_2");
3662 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_0),
3663 "s0_1", "s0_2");
3664 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_10),
3665 "s10_1", "s10_2");
3666 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_10),
3667 "s10_1", "s10_2");
3668 assertShortcutExists(CALLING_PACKAGE_1, "s0_1", USER_0);
3669 assertShortcutExists(CALLING_PACKAGE_2, "s0_2", USER_0);
3670 assertShortcutExists(CALLING_PACKAGE_1, "s10_1", USER_10);
3671 assertShortcutExists(CALLING_PACKAGE_2, "s10_2", USER_10);
3672
3673 mService.saveDirtyInfo();
3674
3675 // Nonexistent package.
Makoto Onuki2e210c42016-03-30 08:30:36 -07003676 uninstallPackage(USER_0, "abc");
Makoto Onukib08790c2016-06-23 14:05:46 -07003677 mService.cleanUpPackageLocked("abc", USER_0, USER_0, /* appStillExists = */ false);
Makoto Onukicdc78f72016-03-21 15:47:52 -07003678
3679 // No changes.
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003680 assertEquals(set(CALLING_PACKAGE_1, CALLING_PACKAGE_2),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003681 hashSet(user0.getAllPackagesForTest().keySet()));
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003682 assertEquals(set(CALLING_PACKAGE_1, CALLING_PACKAGE_2),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003683 hashSet(user10.getAllPackagesForTest().keySet()));
Makoto Onukid99c6f02016-03-28 11:02:54 -07003684 assertEquals(
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003685 set(PackageWithUser.of(USER_0, LAUNCHER_1),
Makoto Onukid99c6f02016-03-28 11:02:54 -07003686 PackageWithUser.of(USER_0, LAUNCHER_2)),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003687 hashSet(user0.getAllLaunchersForTest().keySet()));
Makoto Onukid99c6f02016-03-28 11:02:54 -07003688 assertEquals(
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003689 set(PackageWithUser.of(USER_10, LAUNCHER_1),
Makoto Onukid99c6f02016-03-28 11:02:54 -07003690 PackageWithUser.of(USER_10, LAUNCHER_2)),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003691 hashSet(user10.getAllLaunchersForTest().keySet()));
Makoto Onukicdc78f72016-03-21 15:47:52 -07003692 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_0),
3693 "s0_1", "s0_2");
3694 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_0),
3695 "s0_1", "s0_2");
3696 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_10),
3697 "s10_1", "s10_2");
3698 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_10),
3699 "s10_1", "s10_2");
3700 assertShortcutExists(CALLING_PACKAGE_1, "s0_1", USER_0);
3701 assertShortcutExists(CALLING_PACKAGE_2, "s0_2", USER_0);
3702 assertShortcutExists(CALLING_PACKAGE_1, "s10_1", USER_10);
3703 assertShortcutExists(CALLING_PACKAGE_2, "s10_2", USER_10);
3704
3705 mService.saveDirtyInfo();
3706
3707 // Remove a package.
Makoto Onuki2e210c42016-03-30 08:30:36 -07003708 uninstallPackage(USER_0, CALLING_PACKAGE_1);
Makoto Onukib08790c2016-06-23 14:05:46 -07003709 mService.cleanUpPackageLocked(CALLING_PACKAGE_1, USER_0, USER_0,
3710 /* appStillExists = */ false);
Makoto Onukicdc78f72016-03-21 15:47:52 -07003711
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003712 assertEquals(set(CALLING_PACKAGE_2),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003713 hashSet(user0.getAllPackagesForTest().keySet()));
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003714 assertEquals(set(CALLING_PACKAGE_1, CALLING_PACKAGE_2),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003715 hashSet(user10.getAllPackagesForTest().keySet()));
Makoto Onukid99c6f02016-03-28 11:02:54 -07003716 assertEquals(
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003717 set(PackageWithUser.of(USER_0, LAUNCHER_1),
Makoto Onukid99c6f02016-03-28 11:02:54 -07003718 PackageWithUser.of(USER_0, LAUNCHER_2)),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003719 hashSet(user0.getAllLaunchersForTest().keySet()));
Makoto Onukid99c6f02016-03-28 11:02:54 -07003720 assertEquals(
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003721 set(PackageWithUser.of(USER_10, LAUNCHER_1),
Makoto Onukid99c6f02016-03-28 11:02:54 -07003722 PackageWithUser.of(USER_10, LAUNCHER_2)),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003723 hashSet(user10.getAllLaunchersForTest().keySet()));
Makoto Onukicdc78f72016-03-21 15:47:52 -07003724 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_0),
3725 "s0_2");
3726 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_0),
3727 "s0_2");
3728 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_10),
3729 "s10_1", "s10_2");
3730 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_10),
3731 "s10_1", "s10_2");
3732 assertShortcutNotExists(CALLING_PACKAGE_1, "s0_1", USER_0);
3733 assertShortcutExists(CALLING_PACKAGE_2, "s0_2", USER_0);
3734 assertShortcutExists(CALLING_PACKAGE_1, "s10_1", USER_10);
3735 assertShortcutExists(CALLING_PACKAGE_2, "s10_2", USER_10);
3736
3737 mService.saveDirtyInfo();
3738
3739 // Remove a launcher.
Makoto Onuki2e210c42016-03-30 08:30:36 -07003740 uninstallPackage(USER_10, LAUNCHER_1);
Makoto Onukib08790c2016-06-23 14:05:46 -07003741 mService.cleanUpPackageLocked(LAUNCHER_1, USER_10, USER_10, /* appStillExists = */ false);
Makoto Onukicdc78f72016-03-21 15:47:52 -07003742
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003743 assertEquals(set(CALLING_PACKAGE_2),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003744 hashSet(user0.getAllPackagesForTest().keySet()));
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003745 assertEquals(set(CALLING_PACKAGE_1, CALLING_PACKAGE_2),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003746 hashSet(user10.getAllPackagesForTest().keySet()));
Makoto Onukid99c6f02016-03-28 11:02:54 -07003747 assertEquals(
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003748 set(PackageWithUser.of(USER_0, LAUNCHER_1),
Makoto Onukid99c6f02016-03-28 11:02:54 -07003749 PackageWithUser.of(USER_0, LAUNCHER_2)),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003750 hashSet(user0.getAllLaunchersForTest().keySet()));
Makoto Onukid99c6f02016-03-28 11:02:54 -07003751 assertEquals(
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003752 set(PackageWithUser.of(USER_10, LAUNCHER_2)),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003753 hashSet(user10.getAllLaunchersForTest().keySet()));
Makoto Onukicdc78f72016-03-21 15:47:52 -07003754 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_0),
3755 "s0_2");
3756 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_0),
3757 "s0_2");
3758 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_10),
3759 "s10_1", "s10_2");
3760 assertShortcutNotExists(CALLING_PACKAGE_1, "s0_1", USER_0);
3761 assertShortcutExists(CALLING_PACKAGE_2, "s0_2", USER_0);
3762 assertShortcutExists(CALLING_PACKAGE_1, "s10_1", USER_10);
3763 assertShortcutExists(CALLING_PACKAGE_2, "s10_2", USER_10);
3764
3765 mService.saveDirtyInfo();
3766
3767 // Remove a package.
Makoto Onuki2e210c42016-03-30 08:30:36 -07003768 uninstallPackage(USER_10, CALLING_PACKAGE_2);
Makoto Onukib08790c2016-06-23 14:05:46 -07003769 mService.cleanUpPackageLocked(CALLING_PACKAGE_2, USER_10, USER_10,
3770 /* appStillExists = */ false);
Makoto Onukicdc78f72016-03-21 15:47:52 -07003771
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003772 assertEquals(set(CALLING_PACKAGE_2),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003773 hashSet(user0.getAllPackagesForTest().keySet()));
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003774 assertEquals(set(CALLING_PACKAGE_1),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003775 hashSet(user10.getAllPackagesForTest().keySet()));
Makoto Onukid99c6f02016-03-28 11:02:54 -07003776 assertEquals(
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003777 set(PackageWithUser.of(USER_0, LAUNCHER_1),
Makoto Onukid99c6f02016-03-28 11:02:54 -07003778 PackageWithUser.of(USER_0, LAUNCHER_2)),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003779 hashSet(user0.getAllLaunchersForTest().keySet()));
Makoto Onukid99c6f02016-03-28 11:02:54 -07003780 assertEquals(
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003781 set(PackageWithUser.of(USER_10, LAUNCHER_2)),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003782 hashSet(user10.getAllLaunchersForTest().keySet()));
Makoto Onukicdc78f72016-03-21 15:47:52 -07003783 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_0),
3784 "s0_2");
3785 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_0),
3786 "s0_2");
3787 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_10),
3788 "s10_1");
3789 assertShortcutNotExists(CALLING_PACKAGE_1, "s0_1", USER_0);
3790 assertShortcutExists(CALLING_PACKAGE_2, "s0_2", USER_0);
3791 assertShortcutExists(CALLING_PACKAGE_1, "s10_1", USER_10);
3792 assertShortcutNotExists(CALLING_PACKAGE_2, "s10_2", USER_10);
3793
3794 mService.saveDirtyInfo();
3795
3796 // Remove the other launcher from user 10 too.
Makoto Onuki2e210c42016-03-30 08:30:36 -07003797 uninstallPackage(USER_10, LAUNCHER_2);
Makoto Onukib08790c2016-06-23 14:05:46 -07003798 mService.cleanUpPackageLocked(LAUNCHER_2, USER_10, USER_10,
3799 /* appStillExists = */ false);
Makoto Onukicdc78f72016-03-21 15:47:52 -07003800
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003801 assertEquals(set(CALLING_PACKAGE_2),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003802 hashSet(user0.getAllPackagesForTest().keySet()));
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003803 assertEquals(set(CALLING_PACKAGE_1),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003804 hashSet(user10.getAllPackagesForTest().keySet()));
Makoto Onukid99c6f02016-03-28 11:02:54 -07003805 assertEquals(
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003806 set(PackageWithUser.of(USER_0, LAUNCHER_1),
Makoto Onukid99c6f02016-03-28 11:02:54 -07003807 PackageWithUser.of(USER_0, LAUNCHER_2)),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003808 hashSet(user0.getAllLaunchersForTest().keySet()));
Makoto Onukid99c6f02016-03-28 11:02:54 -07003809 assertEquals(
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003810 set(),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003811 hashSet(user10.getAllLaunchersForTest().keySet()));
Makoto Onukicdc78f72016-03-21 15:47:52 -07003812 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_0),
3813 "s0_2");
3814 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_0),
3815 "s0_2");
3816
3817 // Note the pinned shortcuts on user-10 no longer referred, so they should both be removed.
3818 assertShortcutNotExists(CALLING_PACKAGE_1, "s0_1", USER_0);
3819 assertShortcutExists(CALLING_PACKAGE_2, "s0_2", USER_0);
3820 assertShortcutNotExists(CALLING_PACKAGE_1, "s10_1", USER_10);
3821 assertShortcutNotExists(CALLING_PACKAGE_2, "s10_2", USER_10);
3822
3823 mService.saveDirtyInfo();
3824
3825 // More remove.
Makoto Onuki2e210c42016-03-30 08:30:36 -07003826 uninstallPackage(USER_10, CALLING_PACKAGE_1);
Makoto Onukib08790c2016-06-23 14:05:46 -07003827 mService.cleanUpPackageLocked(CALLING_PACKAGE_1, USER_10, USER_10,
3828 /* appStillExists = */ false);
Makoto Onukicdc78f72016-03-21 15:47:52 -07003829
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003830 assertEquals(set(CALLING_PACKAGE_2),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003831 hashSet(user0.getAllPackagesForTest().keySet()));
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003832 assertEquals(set(),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003833 hashSet(user10.getAllPackagesForTest().keySet()));
Makoto Onukid99c6f02016-03-28 11:02:54 -07003834 assertEquals(
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003835 set(PackageWithUser.of(USER_0, LAUNCHER_1),
Makoto Onukid99c6f02016-03-28 11:02:54 -07003836 PackageWithUser.of(USER_0, LAUNCHER_2)),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003837 hashSet(user0.getAllLaunchersForTest().keySet()));
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07003838 assertEquals(set(),
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07003839 hashSet(user10.getAllLaunchersForTest().keySet()));
Makoto Onukicdc78f72016-03-21 15:47:52 -07003840 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_0),
3841 "s0_2");
3842 assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_0),
3843 "s0_2");
3844
3845 // Note the pinned shortcuts on user-10 no longer referred, so they should both be removed.
3846 assertShortcutNotExists(CALLING_PACKAGE_1, "s0_1", USER_0);
3847 assertShortcutExists(CALLING_PACKAGE_2, "s0_2", USER_0);
3848 assertShortcutNotExists(CALLING_PACKAGE_1, "s10_1", USER_10);
3849 assertShortcutNotExists(CALLING_PACKAGE_2, "s10_2", USER_10);
3850
3851 mService.saveDirtyInfo();
3852 }
3853
Makoto Onukib08790c2016-06-23 14:05:46 -07003854 public void testCleanupPackage_republishManifests() {
3855 addManifestShortcutResource(
3856 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
3857 R.xml.shortcut_2);
3858 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukia4f89b12017-10-05 10:37:55 -07003859 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onukib08790c2016-06-23 14:05:46 -07003860 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
3861
3862 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3863 assertTrue(mManager.setDynamicShortcuts(list(
3864 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
3865 });
3866 runWithCaller(LAUNCHER_1, USER_0, () -> {
3867 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
3868 list("s2", "s3", "ms1", "ms2"), HANDLE_USER_0);
3869 });
3870
3871 // Remove ms2 from manifest.
3872 addManifestShortcutResource(
3873 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
3874 R.xml.shortcut_1);
3875 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukia4f89b12017-10-05 10:37:55 -07003876 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onukib08790c2016-06-23 14:05:46 -07003877 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
3878
3879 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3880 assertTrue(mManager.setDynamicShortcuts(list(
3881 makeShortcut("s1"), makeShortcut("s2"))));
3882
3883 // Make sure the shortcuts are in the intended state.
3884 assertWith(getCallerShortcuts())
3885 .haveIds("ms1", "ms2", "s1", "s2", "s3")
3886
3887 .selectByIds("ms1")
3888 .areAllManifest()
3889 .areAllPinned()
3890
3891 .revertToOriginalList()
3892 .selectByIds("ms2")
3893 .areAllNotManifest()
3894 .areAllPinned()
3895
3896 .revertToOriginalList()
3897 .selectByIds("s1")
3898 .areAllDynamic()
3899 .areAllNotPinned()
3900
3901 .revertToOriginalList()
3902 .selectByIds("s2")
3903 .areAllDynamic()
3904 .areAllPinned()
3905
3906 .revertToOriginalList()
3907 .selectByIds("s3")
3908 .areAllNotDynamic()
3909 .areAllPinned();
3910 });
3911
3912 // Clean up + re-publish manifests.
3913 mService.cleanUpPackageLocked(CALLING_PACKAGE_1, USER_0, USER_0,
3914 /* appStillExists = */ true);
3915 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3916 assertWith(getCallerShortcuts())
3917 .haveIds("ms1")
3918 .areAllManifest();
3919 });
3920 }
3921
Makoto Onukid99c6f02016-03-28 11:02:54 -07003922 public void testHandleGonePackage_crossProfile() {
3923 // Create some shortcuts.
3924 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
3925 assertTrue(mManager.setDynamicShortcuts(list(
3926 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
3927 });
3928 runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
3929 assertTrue(mManager.setDynamicShortcuts(list(
3930 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
3931 });
3932 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
3933 assertTrue(mManager.setDynamicShortcuts(list(
3934 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
3935 });
Makoto Onuki9c850012016-07-26 15:50:50 -07003936
3937 mRunningUsers.put(USER_10, true);
3938
Makoto Onukid99c6f02016-03-28 11:02:54 -07003939 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
3940 assertTrue(mManager.setDynamicShortcuts(list(
3941 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
3942 });
3943
3944 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
3945 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
3946 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
3947
3948 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
3949 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
3950 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
3951
3952 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
3953 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
3954 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
3955
3956 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
3957 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
3958 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
3959
3960 // Pin some.
3961
3962 runWithCaller(LAUNCHER_1, USER_0, () -> {
3963 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
3964 list("s1"), HANDLE_USER_0);
3965
3966 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
3967 list("s2"), UserHandle.of(USER_P0));
3968
3969 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
3970 list("s3"), HANDLE_USER_0);
3971 });
3972
3973 runWithCaller(LAUNCHER_1, USER_P0, () -> {
3974 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
3975 list("s2"), HANDLE_USER_0);
3976
3977 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
3978 list("s3"), UserHandle.of(USER_P0));
3979
3980 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
3981 list("s1"), HANDLE_USER_0);
3982 });
3983
3984 runWithCaller(LAUNCHER_1, USER_10, () -> {
3985 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
3986 list("s3"), HANDLE_USER_10);
3987 });
3988
3989 // Check the state.
3990
3991 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
3992 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
3993 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
3994
3995 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
3996 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
3997 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
3998
3999 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
4000 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
4001 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
4002
4003 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
4004 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
4005 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
4006
Makoto Onukid99c6f02016-03-28 11:02:54 -07004007 // Make sure all the information is persisted.
4008 mService.saveDirtyInfo();
4009 initService();
4010 mService.handleUnlockUser(USER_0);
4011 mService.handleUnlockUser(USER_P0);
4012 mService.handleUnlockUser(USER_10);
4013
4014 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
4015 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
4016 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
4017
4018 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
4019 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
4020 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
4021
4022 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
4023 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
4024 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
4025
4026 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
4027 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
4028 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
4029
Makoto Onukid99c6f02016-03-28 11:02:54 -07004030 // Start uninstalling.
4031 uninstallPackage(USER_10, LAUNCHER_1);
Makoto Onuki39686e82016-04-13 18:03:00 -07004032 mService.checkPackageChanges(USER_10);
Makoto Onukid99c6f02016-03-28 11:02:54 -07004033
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 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
4049
Makoto Onukid99c6f02016-03-28 11:02:54 -07004050 // Uninstall.
4051 uninstallPackage(USER_10, CALLING_PACKAGE_1);
Makoto Onuki39686e82016-04-13 18:03:00 -07004052 mService.checkPackageChanges(USER_10);
Makoto Onukid99c6f02016-03-28 11:02:54 -07004053
4054 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
4055 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
4056 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
4057
4058 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
4059 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
4060 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
4061
4062 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
4063 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
4064 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
4065
4066 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
4067 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
4068 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
4069
Makoto Onukid99c6f02016-03-28 11:02:54 -07004070 uninstallPackage(USER_P0, LAUNCHER_1);
Makoto Onuki39686e82016-04-13 18:03:00 -07004071 mService.checkPackageChanges(USER_0);
Makoto Onukid99c6f02016-03-28 11:02:54 -07004072
4073 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
4074 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
4075 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
4076
4077 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
4078 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
4079 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
4080
4081 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
4082 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
4083 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
4084
4085 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
4086 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
4087 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
4088
Makoto Onuki39686e82016-04-13 18:03:00 -07004089 mService.checkPackageChanges(USER_P0);
Makoto Onuki7001a612016-05-27 13:24:28 -07004090
Makoto Onukid99c6f02016-03-28 11:02:54 -07004091 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
4092 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
4093 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
4094
4095 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
4096 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
4097 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
4098
4099 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
4100 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
4101 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
4102
4103 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
4104 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
4105 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
4106
Makoto Onukid99c6f02016-03-28 11:02:54 -07004107 uninstallPackage(USER_P0, CALLING_PACKAGE_1);
4108
4109 mService.saveDirtyInfo();
4110 initService();
4111 mService.handleUnlockUser(USER_0);
4112 mService.handleUnlockUser(USER_P0);
4113 mService.handleUnlockUser(USER_10);
4114
4115 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
4116 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
4117 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
4118
4119 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
4120 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
4121 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
4122
4123 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
4124 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
4125 assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
4126
4127 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
4128 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
4129 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
4130
Makoto Onukid99c6f02016-03-28 11:02:54 -07004131 // Uninstall
4132 uninstallPackage(USER_0, LAUNCHER_1);
4133
4134 mService.saveDirtyInfo();
4135 initService();
4136 mService.handleUnlockUser(USER_0);
4137 mService.handleUnlockUser(USER_P0);
4138 mService.handleUnlockUser(USER_10);
4139
4140 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
4141 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
4142 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
4143
4144 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
4145 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
4146 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
4147
4148 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
4149 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
4150 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
4151
4152 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
4153 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
4154 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
4155
Makoto Onukid99c6f02016-03-28 11:02:54 -07004156 uninstallPackage(USER_0, CALLING_PACKAGE_2);
4157
4158 mService.saveDirtyInfo();
4159 initService();
4160 mService.handleUnlockUser(USER_0);
4161 mService.handleUnlockUser(USER_P0);
4162 mService.handleUnlockUser(USER_10);
4163
4164 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
4165 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
4166 assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
4167
4168 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
4169 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
4170 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
4171
4172 assertNull(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
4173 assertNull(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
4174 assertNull(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
4175
4176 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
4177 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
4178 assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
Makoto Onukid99c6f02016-03-28 11:02:54 -07004179 }
4180
Makoto Onukia4f89b12017-10-05 10:37:55 -07004181 protected void checkCanRestoreTo(int expected, ShortcutPackageInfo spi,
4182 boolean anyVersionOk, int version, boolean nowBackupAllowed, String... signatures) {
4183 final PackageInfo pi = genPackage("dummy", /* uid */ 0, version, signatures);
4184 if (!nowBackupAllowed) {
4185 pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP;
4186 }
Michal Karpinski528c3e52018-02-07 17:47:10 +00004187
4188 doReturn(expected != DISABLED_REASON_SIGNATURE_MISMATCH).when(
4189 mMockPackageManagerInternal).isDataRestoreSafe(any(byte[].class), anyString());
4190
Makoto Onukia4f89b12017-10-05 10:37:55 -07004191 assertEquals(expected, spi.canRestoreTo(mService, pi, anyVersionOk));
Makoto Onuki0acbb142016-03-22 17:02:57 -07004192 }
4193
4194 public void testCanRestoreTo() {
4195 addPackage(CALLING_PACKAGE_1, CALLING_UID_1, 10, "sig1");
Makoto Onukia4f89b12017-10-05 10:37:55 -07004196 addPackage(CALLING_PACKAGE_2, CALLING_UID_2, 10, "sig1", "sig2");
4197 addPackage(CALLING_PACKAGE_3, CALLING_UID_3, 10, "sig1");
4198
4199 updatePackageInfo(CALLING_PACKAGE_3,
4200 pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP);
Makoto Onuki0acbb142016-03-22 17:02:57 -07004201
Makoto Onukic8c33292016-09-12 16:36:59 -07004202 final ShortcutPackageInfo spi1 = ShortcutPackageInfo.generateForInstalledPackageForTest(
Makoto Onuki0acbb142016-03-22 17:02:57 -07004203 mService, CALLING_PACKAGE_1, USER_0);
Makoto Onukic8c33292016-09-12 16:36:59 -07004204 final ShortcutPackageInfo spi2 = ShortcutPackageInfo.generateForInstalledPackageForTest(
Makoto Onuki0acbb142016-03-22 17:02:57 -07004205 mService, CALLING_PACKAGE_2, USER_0);
Makoto Onukia4f89b12017-10-05 10:37:55 -07004206 final ShortcutPackageInfo spi3 = ShortcutPackageInfo.generateForInstalledPackageForTest(
4207 mService, CALLING_PACKAGE_3, USER_0);
Makoto Onuki0acbb142016-03-22 17:02:57 -07004208
Makoto Onukia4f89b12017-10-05 10:37:55 -07004209 checkCanRestoreTo(DISABLED_REASON_NOT_DISABLED, spi1, false, 10, true, "sig1");
4210 checkCanRestoreTo(DISABLED_REASON_NOT_DISABLED, spi1, false, 10, true, "x", "sig1");
4211 checkCanRestoreTo(DISABLED_REASON_NOT_DISABLED, spi1, false, 10, true, "sig1", "y");
4212 checkCanRestoreTo(DISABLED_REASON_NOT_DISABLED, spi1, false, 10, true, "x", "sig1", "y");
4213 checkCanRestoreTo(DISABLED_REASON_NOT_DISABLED, spi1, false, 11, true, "sig1");
Makoto Onuki0acbb142016-03-22 17:02:57 -07004214
Makoto Onukia4f89b12017-10-05 10:37:55 -07004215 checkCanRestoreTo(DISABLED_REASON_SIGNATURE_MISMATCH, spi1, false, 10, true/* empty */);
4216 checkCanRestoreTo(DISABLED_REASON_SIGNATURE_MISMATCH, spi1, false, 10, true, "x");
4217 checkCanRestoreTo(DISABLED_REASON_SIGNATURE_MISMATCH, spi1, false, 10, true, "x", "y");
4218 checkCanRestoreTo(DISABLED_REASON_SIGNATURE_MISMATCH, spi1, false, 10, true, "x");
4219 checkCanRestoreTo(DISABLED_REASON_VERSION_LOWER, spi1, false, 9, true, "sig1");
Makoto Onuki0acbb142016-03-22 17:02:57 -07004220
Makoto Onukia4f89b12017-10-05 10:37:55 -07004221 // Any version okay.
4222 checkCanRestoreTo(DISABLED_REASON_NOT_DISABLED, spi1, true, 9, true, "sig1");
4223 checkCanRestoreTo(DISABLED_REASON_NOT_DISABLED, spi1, true, 9, true, "sig1");
Makoto Onuki0acbb142016-03-22 17:02:57 -07004224
Makoto Onukia4f89b12017-10-05 10:37:55 -07004225 checkCanRestoreTo(DISABLED_REASON_NOT_DISABLED, spi2, false, 10, true, "sig1", "sig2");
4226 checkCanRestoreTo(DISABLED_REASON_NOT_DISABLED, spi2, false, 10, true, "sig2", "sig1");
4227 checkCanRestoreTo(DISABLED_REASON_NOT_DISABLED, spi2, false, 10, true, "x", "sig1", "sig2");
4228 checkCanRestoreTo(DISABLED_REASON_NOT_DISABLED, spi2, false, 10, true, "x", "sig2", "sig1");
4229 checkCanRestoreTo(DISABLED_REASON_NOT_DISABLED, spi2, false, 10, true, "sig1", "sig2", "y");
4230 checkCanRestoreTo(DISABLED_REASON_NOT_DISABLED, spi2, false, 10, true, "sig2", "sig1", "y");
4231 checkCanRestoreTo(DISABLED_REASON_NOT_DISABLED, spi2, false, 10, true, "x", "sig1", "sig2", "y");
4232 checkCanRestoreTo(DISABLED_REASON_NOT_DISABLED, spi2, false, 10, true, "x", "sig2", "sig1", "y");
4233 checkCanRestoreTo(DISABLED_REASON_NOT_DISABLED, spi2, false, 11, true, "x", "sig2", "sig1", "y");
4234
4235 checkCanRestoreTo(DISABLED_REASON_SIGNATURE_MISMATCH,
4236 spi2, false, 10, true, "sig1", "sig2x");
4237 checkCanRestoreTo(DISABLED_REASON_SIGNATURE_MISMATCH,
4238 spi2, false, 10, true, "sig2", "sig1x");
4239 checkCanRestoreTo(DISABLED_REASON_SIGNATURE_MISMATCH,
4240 spi2, false, 10, true, "x", "sig1x", "sig2");
4241 checkCanRestoreTo(DISABLED_REASON_SIGNATURE_MISMATCH,
4242 spi2, false, 10, true, "x", "sig2x", "sig1");
4243 checkCanRestoreTo(DISABLED_REASON_SIGNATURE_MISMATCH,
4244 spi2, false, 10, true, "sig1", "sig2x", "y");
4245 checkCanRestoreTo(DISABLED_REASON_SIGNATURE_MISMATCH,
4246 spi2, false, 10, true, "sig2", "sig1x", "y");
4247 checkCanRestoreTo(DISABLED_REASON_SIGNATURE_MISMATCH,
4248 spi2, false, 10, true, "x", "sig1x", "sig2", "y");
4249 checkCanRestoreTo(DISABLED_REASON_SIGNATURE_MISMATCH,
4250 spi2, false, 10, true, "x", "sig2x", "sig1", "y");
4251 checkCanRestoreTo(DISABLED_REASON_SIGNATURE_MISMATCH,
4252 spi2, false, 11, true, "x", "sig2x", "sig1", "y");
4253
4254 checkCanRestoreTo(DISABLED_REASON_BACKUP_NOT_SUPPORTED, spi1, true, 10, false, "sig1");
4255 checkCanRestoreTo(DISABLED_REASON_BACKUP_NOT_SUPPORTED, spi3, true, 10, true, "sig1");
Makoto Onuki0acbb142016-03-22 17:02:57 -07004256 }
4257
Makoto Onuki0acbb142016-03-22 17:02:57 -07004258 public void testHandlePackageDelete() {
Makoto Onuki82fb2eb2017-03-31 16:58:26 -07004259 checkHandlePackageDeleteInner((userId, packageName) -> {
4260 uninstallPackage(userId, packageName);
4261 mService.mPackageMonitor.onReceive(getTestContext(),
4262 genPackageDeleteIntent(packageName, userId));
4263 });
4264 }
4265
4266 public void testHandlePackageDisable() {
4267 checkHandlePackageDeleteInner((userId, packageName) -> {
4268 disablePackage(userId, packageName);
4269 mService.mPackageMonitor.onReceive(getTestContext(),
4270 genPackageChangedIntent(packageName, userId));
4271 });
4272 }
4273
4274 private void checkHandlePackageDeleteInner(BiConsumer<Integer, String> remover) {
Makoto Onuki0033b2a2016-04-14 17:19:16 -07004275 final Icon bmp32x32 = Icon.createWithBitmap(BitmapFactory.decodeResource(
4276 getTestContext().getResources(), R.drawable.black_32x32));
Makoto Onuki0acbb142016-03-22 17:02:57 -07004277 setCaller(CALLING_PACKAGE_1, USER_0);
Makoto Onuki0033b2a2016-04-14 17:19:16 -07004278 assertTrue(mManager.addDynamicShortcuts(list(
4279 makeShortcutWithIcon("s1", bmp32x32), makeShortcutWithIcon("s2", bmp32x32)
4280 )));
Makoto Onukib08790c2016-06-23 14:05:46 -07004281 // Also add a manifest shortcut, which should be removed too.
4282 addManifestShortcutResource(
4283 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
4284 R.xml.shortcut_1);
4285 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukia4f89b12017-10-05 10:37:55 -07004286 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onukib08790c2016-06-23 14:05:46 -07004287 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
4288 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4289 assertWith(getCallerShortcuts())
4290 .haveIds("s1", "s2", "ms1")
4291
4292 .selectManifest()
4293 .haveIds("ms1");
4294 });
Makoto Onuki0acbb142016-03-22 17:02:57 -07004295
4296 setCaller(CALLING_PACKAGE_2, USER_0);
Makoto Onuki0033b2a2016-04-14 17:19:16 -07004297 assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
Makoto Onuki0acbb142016-03-22 17:02:57 -07004298
4299 setCaller(CALLING_PACKAGE_3, USER_0);
Makoto Onuki0033b2a2016-04-14 17:19:16 -07004300 assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
Makoto Onuki0acbb142016-03-22 17:02:57 -07004301
Makoto Onuki9c850012016-07-26 15:50:50 -07004302 mRunningUsers.put(USER_10, true);
4303
Makoto Onuki0acbb142016-03-22 17:02:57 -07004304 setCaller(CALLING_PACKAGE_1, USER_10);
Makoto Onuki0033b2a2016-04-14 17:19:16 -07004305 assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
Makoto Onuki0acbb142016-03-22 17:02:57 -07004306
4307 setCaller(CALLING_PACKAGE_2, USER_10);
Makoto Onuki0033b2a2016-04-14 17:19:16 -07004308 assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
Makoto Onuki0acbb142016-03-22 17:02:57 -07004309
4310 setCaller(CALLING_PACKAGE_3, USER_10);
Makoto Onuki0033b2a2016-04-14 17:19:16 -07004311 assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
Makoto Onuki0acbb142016-03-22 17:02:57 -07004312
Makoto Onuki9da23fc2016-03-29 11:14:42 -07004313 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
4314 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
4315 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));
4316 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
4317 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
4318 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
Makoto Onuki0acbb142016-03-22 17:02:57 -07004319
Makoto Onuki0033b2a2016-04-14 17:19:16 -07004320 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
4321 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
4322 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
4323 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
4324 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
4325 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
4326
Makoto Onuki82fb2eb2017-03-31 16:58:26 -07004327 remover.accept(USER_0, CALLING_PACKAGE_1);
Makoto Onuki0acbb142016-03-22 17:02:57 -07004328
Makoto Onuki9da23fc2016-03-29 11:14:42 -07004329 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
4330 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
4331 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));
4332 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
4333 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
4334 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
Makoto Onuki0acbb142016-03-22 17:02:57 -07004335
Makoto Onuki0033b2a2016-04-14 17:19:16 -07004336 assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
4337 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
4338 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
4339 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
4340 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
4341 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
4342
Makoto Onukia2241832016-07-06 13:28:37 -07004343 mRunningUsers.put(USER_10, true);
4344
Makoto Onuki82fb2eb2017-03-31 16:58:26 -07004345 remover.accept(USER_10, CALLING_PACKAGE_2);
Makoto Onuki0acbb142016-03-22 17:02:57 -07004346
Makoto Onuki9da23fc2016-03-29 11:14:42 -07004347 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
4348 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
4349 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));
4350 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
4351 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
4352 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
Makoto Onuki0acbb142016-03-22 17:02:57 -07004353
Makoto Onuki0033b2a2016-04-14 17:19:16 -07004354 assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
4355 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
4356 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
4357 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
4358 assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
4359 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
4360
Makoto Onuki0acbb142016-03-22 17:02:57 -07004361 mInjectedPackages.remove(CALLING_PACKAGE_1);
4362 mInjectedPackages.remove(CALLING_PACKAGE_3);
4363
Makoto Onuki085a05c2016-08-19 11:39:29 -07004364 mService.checkPackageChanges(USER_0);
Makoto Onuki9da23fc2016-03-29 11:14:42 -07004365
4366 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
4367 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
Makoto Onuki085a05c2016-08-19 11:39:29 -07004368 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0)); // ---------------
Makoto Onuki9da23fc2016-03-29 11:14:42 -07004369 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
4370 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
4371 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
Makoto Onuki0acbb142016-03-22 17:02:57 -07004372
Makoto Onuki0033b2a2016-04-14 17:19:16 -07004373 assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
4374 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
4375 assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
4376 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
4377 assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
4378 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
4379
Makoto Onuki085a05c2016-08-19 11:39:29 -07004380 mService.checkPackageChanges(USER_10);
Makoto Onuki9da23fc2016-03-29 11:14:42 -07004381
4382 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
4383 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
4384 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));
4385 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
4386 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
4387 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
Makoto Onuki0033b2a2016-04-14 17:19:16 -07004388
4389 assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
4390 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
4391 assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
4392 assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
4393 assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
4394 assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
Makoto Onuki0acbb142016-03-22 17:02:57 -07004395 }
4396
Makoto Onuki9ac59d02016-04-26 11:23:14 -07004397 /** Almost ame as testHandlePackageDelete, except it doesn't uninstall packages. */
4398 public void testHandlePackageClearData() {
4399 final Icon bmp32x32 = Icon.createWithBitmap(BitmapFactory.decodeResource(
4400 getTestContext().getResources(), R.drawable.black_32x32));
4401 setCaller(CALLING_PACKAGE_1, USER_0);
4402 assertTrue(mManager.addDynamicShortcuts(list(
4403 makeShortcutWithIcon("s1", bmp32x32), makeShortcutWithIcon("s2", bmp32x32)
4404 )));
4405
4406 setCaller(CALLING_PACKAGE_2, USER_0);
4407 assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
4408
4409 setCaller(CALLING_PACKAGE_3, USER_0);
4410 assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
4411
Makoto Onuki9c850012016-07-26 15:50:50 -07004412 mRunningUsers.put(USER_10, true);
4413
Makoto Onuki9ac59d02016-04-26 11:23:14 -07004414 setCaller(CALLING_PACKAGE_1, USER_10);
4415 assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
4416
4417 setCaller(CALLING_PACKAGE_2, USER_10);
4418 assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
4419
4420 setCaller(CALLING_PACKAGE_3, USER_10);
4421 assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
4422
4423 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
4424 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
4425 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));
4426 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
4427 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
4428 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
4429
4430 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
4431 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
4432 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
4433 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
4434 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
4435 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
4436
Makoto Onukia4f89b12017-10-05 10:37:55 -07004437 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki9ac59d02016-04-26 11:23:14 -07004438 genPackageDataClear(CALLING_PACKAGE_1, USER_0));
4439
4440 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
4441 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
4442 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));
4443 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
4444 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
4445 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
4446
4447 assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
4448 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
4449 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
4450 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
4451 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
4452 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
4453
Makoto Onukia2241832016-07-06 13:28:37 -07004454 mRunningUsers.put(USER_10, true);
4455
Makoto Onukia4f89b12017-10-05 10:37:55 -07004456 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki9ac59d02016-04-26 11:23:14 -07004457 genPackageDataClear(CALLING_PACKAGE_2, USER_10));
4458
4459 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
4460 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
4461 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));
4462 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
4463 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
4464 assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
4465
4466 assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
4467 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
4468 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
4469 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
4470 assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
4471 assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
4472 }
4473
Makoto Onukib08790c2016-06-23 14:05:46 -07004474 public void testHandlePackageClearData_manifestRepublished() {
Makoto Onuki39686e82016-04-13 18:03:00 -07004475
Makoto Onukia2241832016-07-06 13:28:37 -07004476 mRunningUsers.put(USER_10, true);
4477
Makoto Onukib08790c2016-06-23 14:05:46 -07004478 // Add two manifests and two dynamics.
4479 addManifestShortcutResource(
4480 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
4481 R.xml.shortcut_2);
4482 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukia4f89b12017-10-05 10:37:55 -07004483 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onukib08790c2016-06-23 14:05:46 -07004484 genPackageAddIntent(CALLING_PACKAGE_1, USER_10));
4485
4486 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4487 assertTrue(mManager.addDynamicShortcuts(list(
4488 makeShortcut("s1"), makeShortcut("s2"))));
4489 });
4490 runWithCaller(LAUNCHER_1, USER_10, () -> {
4491 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms2", "s2"), HANDLE_USER_10);
4492 });
4493
4494 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4495 assertWith(getCallerShortcuts())
4496 .haveIds("ms1", "ms2", "s1", "s2")
4497 .areAllEnabled()
4498
4499 .selectPinned()
4500 .haveIds("ms2", "s2");
4501 });
4502
4503 // Clear data
Makoto Onuki0b9d1db2016-07-18 14:16:41 -07004504 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onukib08790c2016-06-23 14:05:46 -07004505 genPackageDataClear(CALLING_PACKAGE_1, USER_10));
4506
4507 // Only manifest shortcuts will remain, and are no longer pinned.
4508 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4509 assertWith(getCallerShortcuts())
4510 .haveIds("ms1", "ms2")
4511 .areAllEnabled()
4512 .areAllNotPinned();
4513 });
4514 }
4515
4516 public void testHandlePackageUpdate() throws Throwable {
Makoto Onuki39686e82016-04-13 18:03:00 -07004517 // Set up shortcuts and launchers.
4518
4519 final Icon res32x32 = Icon.createWithResource(getTestContext(), R.drawable.black_32x32);
4520 final Icon bmp32x32 = Icon.createWithBitmap(BitmapFactory.decodeResource(
4521 getTestContext().getResources(), R.drawable.black_32x32));
4522
4523 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4524 assertTrue(mManager.setDynamicShortcuts(list(
4525 makeShortcut("s1"),
4526 makeShortcutWithIcon("s2", res32x32),
4527 makeShortcutWithIcon("s3", res32x32),
4528 makeShortcutWithIcon("s4", bmp32x32))));
4529 });
4530 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
4531 assertTrue(mManager.setDynamicShortcuts(list(
4532 makeShortcut("s1"),
4533 makeShortcutWithIcon("s2", bmp32x32))));
4534 });
4535 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
4536 assertTrue(mManager.setDynamicShortcuts(list(
4537 makeShortcutWithIcon("s1", res32x32))));
4538 });
4539
Makoto Onuki9c850012016-07-26 15:50:50 -07004540 mRunningUsers.put(USER_10, true);
4541
Makoto Onuki39686e82016-04-13 18:03:00 -07004542 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4543 assertTrue(mManager.setDynamicShortcuts(list(
4544 makeShortcutWithIcon("s1", res32x32),
4545 makeShortcutWithIcon("s2", res32x32))));
4546 });
4547 runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
4548 assertTrue(mManager.setDynamicShortcuts(list(
4549 makeShortcutWithIcon("s1", bmp32x32),
4550 makeShortcutWithIcon("s2", bmp32x32))));
4551 });
4552
4553 LauncherApps.Callback c0 = mock(LauncherApps.Callback.class);
4554 LauncherApps.Callback c10 = mock(LauncherApps.Callback.class);
4555
4556 runWithCaller(LAUNCHER_1, USER_0, () -> {
4557 mLauncherApps.registerCallback(c0, new Handler(Looper.getMainLooper()));
4558 });
4559 runWithCaller(LAUNCHER_1, USER_10, () -> {
4560 mLauncherApps.registerCallback(c10, new Handler(Looper.getMainLooper()));
4561 });
4562
Makoto Onuki51ab2b32016-06-02 11:03:51 -07004563 mInjectedCurrentTimeMillis = START_TIME + 100;
Makoto Onuki39686e82016-04-13 18:03:00 -07004564
4565 ArgumentCaptor<List> shortcuts;
4566
Makoto Onuki64183d52016-08-08 14:11:34 -07004567 // Update the version info for package 1.
Makoto Onuki39686e82016-04-13 18:03:00 -07004568 reset(c0);
4569 reset(c10);
4570 updatePackageVersion(CALLING_PACKAGE_1, 1);
4571
4572 // Then send the broadcast, to only user-0.
Makoto Onuki085a05c2016-08-19 11:39:29 -07004573 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki39686e82016-04-13 18:03:00 -07004574 genPackageUpdateIntent(CALLING_PACKAGE_1, USER_0));
4575
4576 waitOnMainThread();
4577
4578 // User-0 should get the notification.
4579 shortcuts = ArgumentCaptor.forClass(List.class);
4580 verify(c0).onShortcutsChanged(
4581 eq(CALLING_PACKAGE_1),
4582 shortcuts.capture(),
4583 eq(HANDLE_USER_0));
4584
4585 // User-10 shouldn't yet get the notification.
4586 verify(c10, times(0)).onShortcutsChanged(
4587 eq(CALLING_PACKAGE_1),
4588 any(List.class),
4589 any(UserHandle.class));
4590 assertShortcutIds(shortcuts.getValue(), "s1", "s2", "s3", "s4");
4591 assertEquals(START_TIME,
4592 findShortcut(shortcuts.getValue(), "s1").getLastChangedTimestamp());
4593 assertEquals(START_TIME + 100,
4594 findShortcut(shortcuts.getValue(), "s2").getLastChangedTimestamp());
4595 assertEquals(START_TIME + 100,
4596 findShortcut(shortcuts.getValue(), "s3").getLastChangedTimestamp());
4597 assertEquals(START_TIME,
4598 findShortcut(shortcuts.getValue(), "s4").getLastChangedTimestamp());
4599
Makoto Onuki248a0ef2016-11-03 15:59:01 -07004600 // 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 -07004601 // notification to the launcher.
Makoto Onuki51ab2b32016-06-02 11:03:51 -07004602 mInjectedCurrentTimeMillis = START_TIME + 200;
Makoto Onuki39686e82016-04-13 18:03:00 -07004603
Makoto Onukia2241832016-07-06 13:28:37 -07004604 mRunningUsers.put(USER_10, true);
Makoto Onuki085a05c2016-08-19 11:39:29 -07004605 mUnlockedUsers.put(USER_10, true);
Makoto Onuki39686e82016-04-13 18:03:00 -07004606
4607 reset(c0);
4608 reset(c10);
Makoto Onuki085a05c2016-08-19 11:39:29 -07004609 setPackageLastUpdateTime(CALLING_PACKAGE_1, mInjectedCurrentTimeMillis);
Makoto Onuki39686e82016-04-13 18:03:00 -07004610 mService.handleUnlockUser(USER_10);
Makoto Onuki085a05c2016-08-19 11:39:29 -07004611 mService.checkPackageChanges(USER_10);
Makoto Onuki39686e82016-04-13 18:03:00 -07004612
Makoto Onukia2241832016-07-06 13:28:37 -07004613 waitOnMainThread();
4614
Makoto Onuki39686e82016-04-13 18:03:00 -07004615 shortcuts = ArgumentCaptor.forClass(List.class);
4616 verify(c0, times(0)).onShortcutsChanged(
4617 eq(CALLING_PACKAGE_1),
4618 any(List.class),
4619 any(UserHandle.class));
4620
4621 verify(c10).onShortcutsChanged(
4622 eq(CALLING_PACKAGE_1),
4623 shortcuts.capture(),
4624 eq(HANDLE_USER_10));
4625
4626 assertShortcutIds(shortcuts.getValue(), "s1", "s2");
4627 assertEquals(START_TIME + 200,
4628 findShortcut(shortcuts.getValue(), "s1").getLastChangedTimestamp());
4629 assertEquals(START_TIME + 200,
4630 findShortcut(shortcuts.getValue(), "s2").getLastChangedTimestamp());
4631
4632
4633 // Do the same thing for package 2, which doesn't have resource icons.
Makoto Onuki51ab2b32016-06-02 11:03:51 -07004634 mInjectedCurrentTimeMillis = START_TIME + 300;
Makoto Onuki39686e82016-04-13 18:03:00 -07004635
4636 reset(c0);
4637 reset(c10);
4638 updatePackageVersion(CALLING_PACKAGE_2, 10);
4639
4640 // Then send the broadcast, to only user-0.
Makoto Onuki248a0ef2016-11-03 15:59:01 -07004641 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki39686e82016-04-13 18:03:00 -07004642 genPackageUpdateIntent(CALLING_PACKAGE_2, USER_0));
Makoto Onuki39686e82016-04-13 18:03:00 -07004643
4644 waitOnMainThread();
4645
4646 verify(c0, times(0)).onShortcutsChanged(
4647 eq(CALLING_PACKAGE_1),
4648 any(List.class),
4649 any(UserHandle.class));
4650
4651 verify(c10, times(0)).onShortcutsChanged(
4652 eq(CALLING_PACKAGE_1),
4653 any(List.class),
4654 any(UserHandle.class));
4655
4656 // Do the same thing for package 3
Makoto Onuki51ab2b32016-06-02 11:03:51 -07004657 mInjectedCurrentTimeMillis = START_TIME + 400;
Makoto Onuki39686e82016-04-13 18:03:00 -07004658
4659 reset(c0);
4660 reset(c10);
4661 updatePackageVersion(CALLING_PACKAGE_3, 100);
4662
4663 // Then send the broadcast, to only user-0.
Makoto Onuki085a05c2016-08-19 11:39:29 -07004664 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki39686e82016-04-13 18:03:00 -07004665 genPackageUpdateIntent(CALLING_PACKAGE_3, USER_0));
Makoto Onuki085a05c2016-08-19 11:39:29 -07004666 mService.checkPackageChanges(USER_10);
Makoto Onuki39686e82016-04-13 18:03:00 -07004667
4668 waitOnMainThread();
4669
4670 shortcuts = ArgumentCaptor.forClass(List.class);
4671 verify(c0).onShortcutsChanged(
4672 eq(CALLING_PACKAGE_3),
4673 shortcuts.capture(),
4674 eq(HANDLE_USER_0));
4675
4676 // User 10 doesn't have package 3, so no callback.
4677 verify(c10, times(0)).onShortcutsChanged(
4678 eq(CALLING_PACKAGE_3),
4679 any(List.class),
4680 any(UserHandle.class));
4681
4682 assertShortcutIds(shortcuts.getValue(), "s1");
4683 assertEquals(START_TIME + 400,
4684 findShortcut(shortcuts.getValue(), "s1").getLastChangedTimestamp());
4685 }
4686
Makoto Onuki157b1622016-06-02 16:13:10 -07004687 /**
4688 * Test the case where an updated app has resource IDs changed.
4689 */
4690 public void testHandlePackageUpdate_resIdChanged() throws Exception {
4691 final Icon icon1 = Icon.createWithResource(getTestContext(), /* res ID */ 1000);
4692 final Icon icon2 = Icon.createWithResource(getTestContext(), /* res ID */ 1001);
4693
4694 // Set up shortcuts.
4695 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4696 // Note resource strings are not officially supported (they're hidden), but
4697 // should work.
4698
4699 final ShortcutInfo s1 = new ShortcutInfo.Builder(mClientContext)
4700 .setId("s1")
4701 .setActivity(makeComponent(ShortcutActivity.class))
4702 .setIntent(new Intent(Intent.ACTION_VIEW))
4703 .setIcon(icon1)
4704 .setTitleResId(10000)
4705 .setTextResId(10001)
4706 .setDisabledMessageResId(10002)
4707 .build();
4708
4709 final ShortcutInfo s2 = new ShortcutInfo.Builder(mClientContext)
4710 .setId("s2")
4711 .setActivity(makeComponent(ShortcutActivity.class))
4712 .setIntent(new Intent(Intent.ACTION_VIEW))
4713 .setIcon(icon2)
4714 .setTitleResId(20000)
4715 .build();
4716
4717 assertTrue(mManager.setDynamicShortcuts(list(s1, s2)));
4718 });
4719
4720 // Verify.
4721 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4722 final ShortcutInfo s1 = getCallerShortcut("s1");
4723 final ShortcutInfo s2 = getCallerShortcut("s2");
4724
4725 assertEquals(1000, s1.getIconResourceId());
4726 assertEquals(10000, s1.getTitleResId());
4727 assertEquals(10001, s1.getTextResId());
4728 assertEquals(10002, s1.getDisabledMessageResourceId());
4729
4730 assertEquals(1001, s2.getIconResourceId());
4731 assertEquals(20000, s2.getTitleResId());
4732 assertEquals(0, s2.getTextResId());
4733 assertEquals(0, s2.getDisabledMessageResourceId());
4734 });
4735
4736 mService.saveDirtyInfo();
4737 initService();
4738
4739 // Set up the mock resources again, with an "adjustment".
4740 // When the package is updated, the service will fetch the updated res-IDs with res-names,
4741 // and the new IDs will have this offset.
4742 setUpAppResources(10);
4743
4744 // Update the package.
4745 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukia4f89b12017-10-05 10:37:55 -07004746 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki157b1622016-06-02 16:13:10 -07004747 genPackageUpdateIntent(CALLING_PACKAGE_1, USER_0));
4748
4749 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4750 final ShortcutInfo s1 = getCallerShortcut("s1");
4751 final ShortcutInfo s2 = getCallerShortcut("s2");
4752
4753 assertEquals(1010, s1.getIconResourceId());
4754 assertEquals(10010, s1.getTitleResId());
4755 assertEquals(10011, s1.getTextResId());
4756 assertEquals(10012, s1.getDisabledMessageResourceId());
4757
4758 assertEquals(1011, s2.getIconResourceId());
4759 assertEquals(20010, s2.getTitleResId());
4760 assertEquals(0, s2.getTextResId());
4761 assertEquals(0, s2.getDisabledMessageResourceId());
4762 });
4763 }
4764
Makoto Onuki33663282016-08-22 16:19:04 -07004765 public void testHandlePackageUpdate_systemAppUpdate() {
4766
4767 // Package1 is a system app. Package 2 is not a system app, so it's not scanned
4768 // in this test at all.
4769 mSystemPackages.add(CALLING_PACKAGE_1);
4770
4771 // Initial state: no shortcuts.
4772 mService.checkPackageChanges(USER_0);
4773
4774 assertEquals(mInjectedCurrentTimeMillis,
4775 mService.getUserShortcutsLocked(USER_0).getLastAppScanTime());
4776 assertEquals(mInjectedBuildFingerprint,
4777 mService.getUserShortcutsLocked(USER_0).getLastAppScanOsFingerprint());
4778
4779 // They have no shortcuts.
4780 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4781 assertWith(getCallerShortcuts())
4782 .isEmpty();
4783 });
4784
4785 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
4786 assertWith(getCallerShortcuts())
4787 .isEmpty();
4788 });
4789
4790 // Next.
4791 // Update the packages -- now they have 1 manifest shortcut.
4792 // But checkPackageChanges() don't notice it, since their version code / timestamp haven't
4793 // changed.
4794 addManifestShortcutResource(
4795 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
4796 R.xml.shortcut_1);
4797 addManifestShortcutResource(
4798 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
4799 R.xml.shortcut_1);
4800 mInjectedCurrentTimeMillis += 1000;
4801 mService.checkPackageChanges(USER_0);
4802
4803 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4804 assertWith(getCallerShortcuts())
4805 .isEmpty();
4806 });
4807 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
4808 assertWith(getCallerShortcuts())
4809 .isEmpty();
4810 });
4811
4812 // Next.
Makoto Onuki248a0ef2016-11-03 15:59:01 -07004813 // Update the build finger print. All apps will be scanned now.
Makoto Onuki33663282016-08-22 16:19:04 -07004814 mInjectedBuildFingerprint = "update1";
4815 mInjectedCurrentTimeMillis += 1000;
4816 mService.checkPackageChanges(USER_0);
4817
4818 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4819 assertWith(getCallerShortcuts())
4820 .haveIds("ms1");
4821 });
4822 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
4823 assertWith(getCallerShortcuts())
Makoto Onuki248a0ef2016-11-03 15:59:01 -07004824 .haveIds("ms1");
Makoto Onuki33663282016-08-22 16:19:04 -07004825 });
4826
4827 // Next.
4828 // Update manifest shortcuts.
Makoto Onuki33663282016-08-22 16:19:04 -07004829 addManifestShortcutResource(
4830 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
4831 R.xml.shortcut_2);
4832 addManifestShortcutResource(
4833 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
4834 R.xml.shortcut_2);
4835 mInjectedCurrentTimeMillis += 1000;
4836 mService.checkPackageChanges(USER_0);
4837
Makoto Onuki248a0ef2016-11-03 15:59:01 -07004838 // Fingerprint hasn't changed, so there packages weren't scanned.
Makoto Onuki33663282016-08-22 16:19:04 -07004839 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4840 assertWith(getCallerShortcuts())
4841 .haveIds("ms1");
4842 });
4843 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
4844 assertWith(getCallerShortcuts())
Makoto Onuki248a0ef2016-11-03 15:59:01 -07004845 .haveIds("ms1");
Makoto Onuki33663282016-08-22 16:19:04 -07004846 });
4847
Makoto Onuki248a0ef2016-11-03 15:59:01 -07004848 // Update the fingerprint. CALLING_PACKAGE_1's version code hasn't changed, but we scan
4849 // all apps anyway.
Makoto Onuki33663282016-08-22 16:19:04 -07004850 mInjectedBuildFingerprint = "update2";
4851 mInjectedCurrentTimeMillis += 1000;
4852 mService.checkPackageChanges(USER_0);
4853
4854 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
4855 assertWith(getCallerShortcuts())
Makoto Onuki33663282016-08-22 16:19:04 -07004856 .haveIds("ms1", "ms2");
4857 });
4858 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
4859 assertWith(getCallerShortcuts())
Makoto Onuki248a0ef2016-11-03 15:59:01 -07004860 .haveIds("ms1", "ms2");
Makoto Onuki33663282016-08-22 16:19:04 -07004861 });
4862
4863 // Make sure getLastAppScanTime / getLastAppScanOsFingerprint are persisted.
4864 initService();
4865 assertEquals(mInjectedCurrentTimeMillis,
4866 mService.getUserShortcutsLocked(USER_0).getLastAppScanTime());
4867 assertEquals(mInjectedBuildFingerprint,
4868 mService.getUserShortcutsLocked(USER_0).getLastAppScanOsFingerprint());
4869 }
4870
Makoto Onukib08790c2016-06-23 14:05:46 -07004871 public void testHandlePackageChanged() {
4872 final ComponentName ACTIVITY1 = new ComponentName(CALLING_PACKAGE_1, "act1");
4873 final ComponentName ACTIVITY2 = new ComponentName(CALLING_PACKAGE_1, "act2");
4874
4875 addManifestShortcutResource(ACTIVITY1, R.xml.shortcut_1);
4876 addManifestShortcutResource(ACTIVITY2, R.xml.shortcut_1_alt);
4877
Makoto Onukia2241832016-07-06 13:28:37 -07004878 mRunningUsers.put(USER_10, true);
4879
Makoto Onukib08790c2016-06-23 14:05:46 -07004880 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukia4f89b12017-10-05 10:37:55 -07004881 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onukib08790c2016-06-23 14:05:46 -07004882 genPackageAddIntent(CALLING_PACKAGE_1, USER_10));
4883
4884 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4885 assertTrue(mManager.addDynamicShortcuts(list(
4886 makeShortcutWithActivity("s1", ACTIVITY1),
4887 makeShortcutWithActivity("s2", ACTIVITY2)
4888 )));
4889 });
4890 runWithCaller(LAUNCHER_1, USER_10, () -> {
4891 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms1-alt", "s2"), HANDLE_USER_10);
4892 });
4893
4894 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4895 assertWith(getCallerShortcuts())
4896 .haveIds("ms1", "ms1-alt", "s1", "s2")
4897 .areAllEnabled()
4898
4899 .selectPinned()
4900 .haveIds("ms1-alt", "s2")
4901
4902 .revertToOriginalList()
4903 .selectByIds("ms1", "s1")
4904 .areAllWithActivity(ACTIVITY1)
4905
4906 .revertToOriginalList()
4907 .selectByIds("ms1-alt", "s2")
4908 .areAllWithActivity(ACTIVITY2)
Makoto Onukia4f89b12017-10-05 10:37:55 -07004909 ;
Makoto Onukib08790c2016-06-23 14:05:46 -07004910 });
4911
4912 // First, no changes.
Makoto Onukia4f89b12017-10-05 10:37:55 -07004913 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onukib08790c2016-06-23 14:05:46 -07004914 genPackageChangedIntent(CALLING_PACKAGE_1, USER_10));
4915
4916 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4917 assertWith(getCallerShortcuts())
4918 .haveIds("ms1", "ms1-alt", "s1", "s2")
4919 .areAllEnabled()
4920
4921 .selectPinned()
4922 .haveIds("ms1-alt", "s2")
4923
4924 .revertToOriginalList()
4925 .selectByIds("ms1", "s1")
4926 .areAllWithActivity(ACTIVITY1)
4927
4928 .revertToOriginalList()
4929 .selectByIds("ms1-alt", "s2")
4930 .areAllWithActivity(ACTIVITY2)
4931 ;
4932 });
4933
4934 // Disable activity 1
4935 mEnabledActivityChecker = (activity, userId) -> !ACTIVITY1.equals(activity);
Makoto Onukia4f89b12017-10-05 10:37:55 -07004936 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onukib08790c2016-06-23 14:05:46 -07004937 genPackageChangedIntent(CALLING_PACKAGE_1, USER_10));
4938
4939 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4940 assertWith(getCallerShortcuts())
4941 .haveIds("ms1-alt", "s2")
4942 .areAllEnabled()
4943
4944 .selectPinned()
4945 .haveIds("ms1-alt", "s2")
4946
4947 .revertToOriginalList()
4948 .selectByIds("ms1-alt", "s2")
4949 .areAllWithActivity(ACTIVITY2)
4950 ;
4951 });
4952
4953 // Re-enable activity 1.
4954 // Manifest shortcuts will be re-published, but dynamic ones are not.
4955 mEnabledActivityChecker = (activity, userId) -> true;
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", "s2")
4962 .areAllEnabled()
4963
4964 .selectPinned()
4965 .haveIds("ms1-alt", "s2")
4966
4967 .revertToOriginalList()
4968 .selectByIds("ms1")
4969 .areAllWithActivity(ACTIVITY1)
4970
4971 .revertToOriginalList()
4972 .selectByIds("ms1-alt", "s2")
4973 .areAllWithActivity(ACTIVITY2)
Makoto Onukia4f89b12017-10-05 10:37:55 -07004974 ;
Makoto Onukib08790c2016-06-23 14:05:46 -07004975 });
4976
4977 // Disable activity 2
4978 // Because "ms1-alt" and "s2" are both pinned, they will remain, but disabled.
4979 mEnabledActivityChecker = (activity, userId) -> !ACTIVITY2.equals(activity);
Makoto Onukia4f89b12017-10-05 10:37:55 -07004980 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onukib08790c2016-06-23 14:05:46 -07004981 genPackageChangedIntent(CALLING_PACKAGE_1, USER_10));
4982
4983 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
4984 assertWith(getCallerShortcuts())
4985 .haveIds("ms1", "ms1-alt", "s2")
4986
4987 .selectDynamic().isEmpty().revertToOriginalList() // no dynamics.
4988
4989 .selectPinned()
4990 .haveIds("ms1-alt", "s2")
4991 .areAllDisabled()
4992
4993 .revertToOriginalList()
4994 .selectByIds("ms1")
4995 .areAllWithActivity(ACTIVITY1)
4996 .areAllEnabled()
4997 ;
4998 });
4999 }
5000
5001 public void testHandlePackageUpdate_activityNoLongerMain() throws Throwable {
5002 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5003 assertTrue(mManager.setDynamicShortcuts(list(
5004 makeShortcutWithActivity("s1a",
5005 new ComponentName(getCallingPackage(), "act1")),
5006 makeShortcutWithActivity("s1b",
5007 new ComponentName(getCallingPackage(), "act1")),
5008 makeShortcutWithActivity("s2a",
5009 new ComponentName(getCallingPackage(), "act2")),
5010 makeShortcutWithActivity("s2b",
5011 new ComponentName(getCallingPackage(), "act2")),
5012 makeShortcutWithActivity("s3a",
5013 new ComponentName(getCallingPackage(), "act3")),
5014 makeShortcutWithActivity("s3b",
5015 new ComponentName(getCallingPackage(), "act3"))
5016 )));
5017 assertWith(getCallerShortcuts())
5018 .haveIds("s1a", "s1b", "s2a", "s2b", "s3a", "s3b")
5019 .areAllDynamic();
5020 });
5021 runWithCaller(LAUNCHER_1, USER_0, () -> {
5022 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
5023 list("s1b", "s2b", "s3b"),
5024 HANDLE_USER_0);
5025 });
5026 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5027 assertWith(getCallerShortcuts())
5028 .haveIds("s1a", "s1b", "s2a", "s2b", "s3a", "s3b")
5029 .areAllDynamic()
5030
5031 .selectByIds("s1b", "s2b", "s3b")
5032 .areAllPinned();
5033 });
5034
5035 // Update the app and act2 and act3 are no longer main.
5036 mMainActivityChecker = (activity, userId) -> {
5037 return activity.getClassName().equals("act1");
5038 };
5039
5040 setCaller(LAUNCHER_1, USER_0);
5041 assertForLauncherCallback(mLauncherApps, () -> {
5042 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukia4f89b12017-10-05 10:37:55 -07005043 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onukib08790c2016-06-23 14:05:46 -07005044 genPackageUpdateIntent(CALLING_PACKAGE_1, USER_0));
5045 }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
5046 // Make sure the launcher gets callbacks.
5047 .haveIds("s1a", "s1b", "s2b", "s3b")
5048 .areAllWithKeyFieldsOnly();
5049
5050 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5051 // s2a and s3a are gone, but s2b and s3b will remain because they're pinned, and
5052 // disabled.
5053 assertWith(getCallerShortcuts())
5054 .haveIds("s1a", "s1b", "s2b", "s3b")
5055
5056 .selectByIds("s1a", "s1b")
5057 .areAllDynamic()
5058 .areAllEnabled()
5059
5060 .revertToOriginalList()
5061 .selectByIds("s2b", "s3b")
5062 .areAllNotDynamic()
5063 .areAllDisabled()
5064 .areAllPinned()
Makoto Onukia4f89b12017-10-05 10:37:55 -07005065 ;
Makoto Onukib08790c2016-06-23 14:05:46 -07005066 });
5067 }
5068
Makoto Onuki51ab2b32016-06-02 11:03:51 -07005069 protected void prepareForBackupTest() {
Makoto Onuki2e210c42016-03-30 08:30:36 -07005070 prepareCrossProfileDataSet();
5071
5072 backupAndRestore();
5073 }
5074
Makoto Onuki2e210c42016-03-30 08:30:36 -07005075 /**
5076 * Make sure the backup data doesn't have the following information:
5077 * - Launchers on other users.
5078 * - Non-backup app information.
5079 *
5080 * But restores all other infomation.
5081 *
5082 * It also omits the following pieces of information, but that's tested in
Makoto Onuki51ab2b32016-06-02 11:03:51 -07005083 * {@link ShortcutManagerTest2#testShortcutInfoSaveAndLoad_forBackup}.
Makoto Onuki2e210c42016-03-30 08:30:36 -07005084 * - Unpinned dynamic shortcuts
5085 * - Bitmaps
5086 */
5087 public void testBackupAndRestore() {
Makoto Onuki50a320e2017-05-31 14:38:42 -07005088
5089 assertFileNotExists("user-0/shortcut_dump/restore-0-start.txt");
5090 assertFileNotExists("user-0/shortcut_dump/restore-1-payload.xml");
5091 assertFileNotExists("user-0/shortcut_dump/restore-2.txt");
5092 assertFileNotExists("user-0/shortcut_dump/restore-3.txt");
5093 assertFileNotExists("user-0/shortcut_dump/restore-4.txt");
5094 assertFileNotExists("user-0/shortcut_dump/restore-5-finish.txt");
5095
Makoto Onuki2e210c42016-03-30 08:30:36 -07005096 prepareForBackupTest();
5097
Makoto Onuki50a320e2017-05-31 14:38:42 -07005098 assertFileExistsWithContent("user-0/shortcut_dump/restore-0-start.txt");
5099 assertFileExistsWithContent("user-0/shortcut_dump/restore-1-payload.xml");
5100 assertFileExistsWithContent("user-0/shortcut_dump/restore-2.txt");
5101 assertFileExistsWithContent("user-0/shortcut_dump/restore-3.txt");
5102 assertFileExistsWithContent("user-0/shortcut_dump/restore-4.txt");
5103 assertFileExistsWithContent("user-0/shortcut_dump/restore-5-finish.txt");
5104
Makoto Onukia4f89b12017-10-05 10:37:55 -07005105 checkBackupAndRestore_success(/*firstRestore=*/ true);
Makoto Onuki2e210c42016-03-30 08:30:36 -07005106 }
5107
5108 public void testBackupAndRestore_backupRestoreTwice() {
5109 prepareForBackupTest();
5110
Makoto Onukia4f89b12017-10-05 10:37:55 -07005111 checkBackupAndRestore_success(/*firstRestore=*/ true);
5112
5113 // Run a backup&restore again. Note the shortcuts that weren't restored in the previous
5114 // restore are disabled, so they won't be restored again.
5115 dumpsysOnLogcat("Before second backup&restore");
Makoto Onuki2e210c42016-03-30 08:30:36 -07005116
5117 backupAndRestore();
5118
Makoto Onukia4f89b12017-10-05 10:37:55 -07005119 dumpsysOnLogcat("After second backup&restore");
Makoto Onuki2e210c42016-03-30 08:30:36 -07005120
Makoto Onukia4f89b12017-10-05 10:37:55 -07005121 checkBackupAndRestore_success(/*firstRestore=*/ false);
Makoto Onuki2e210c42016-03-30 08:30:36 -07005122 }
5123
5124 public void testBackupAndRestore_restoreToNewVersion() {
5125 prepareForBackupTest();
5126
Makoto Onuki2e210c42016-03-30 08:30:36 -07005127 addPackage(CALLING_PACKAGE_1, CALLING_UID_1, 2);
5128 addPackage(LAUNCHER_1, LAUNCHER_UID_1, 5);
5129
Makoto Onukia4f89b12017-10-05 10:37:55 -07005130 checkBackupAndRestore_success(/*firstRestore=*/ true);
Makoto Onuki2e210c42016-03-30 08:30:36 -07005131 }
5132
5133 public void testBackupAndRestore_restoreToSuperSetSignatures() {
5134 prepareForBackupTest();
5135
Makoto Onukiac214972016-04-04 10:19:45 -07005136 // Change package signatures.
Makoto Onuki2e210c42016-03-30 08:30:36 -07005137 addPackage(CALLING_PACKAGE_1, CALLING_UID_1, 1, "sigx", CALLING_PACKAGE_1);
5138 addPackage(LAUNCHER_1, LAUNCHER_UID_1, 4, LAUNCHER_1, "sigy");
5139
Makoto Onukia4f89b12017-10-05 10:37:55 -07005140 checkBackupAndRestore_success(/*firstRestore=*/ true);
Makoto Onuki2e210c42016-03-30 08:30:36 -07005141 }
5142
Makoto Onukia4f89b12017-10-05 10:37:55 -07005143 protected void checkBackupAndRestore_success(boolean firstRestore) {
Makoto Onuki2e210c42016-03-30 08:30:36 -07005144 // Make sure non-system user is not restored.
5145 final ShortcutUser userP0 = mService.getUserShortcutsLocked(USER_P0);
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07005146 assertEquals(0, userP0.getAllPackagesForTest().size());
5147 assertEquals(0, userP0.getAllLaunchersForTest().size());
Makoto Onuki2e210c42016-03-30 08:30:36 -07005148
5149 // Make sure only "allowBackup" apps are restored, and are shadow.
5150 final ShortcutUser user0 = mService.getUserShortcutsLocked(USER_0);
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07005151 assertExistsAndShadow(user0.getAllPackagesForTest().get(CALLING_PACKAGE_1));
5152 assertExistsAndShadow(user0.getAllPackagesForTest().get(CALLING_PACKAGE_2));
Makoto Onukia4f89b12017-10-05 10:37:55 -07005153
5154 assertExistsAndShadow(user0.getAllPackagesForTest().get(CALLING_PACKAGE_3));
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005155 assertExistsAndShadow(user0.getAllLaunchersForTest().get(
5156 PackageWithUser.of(USER_0, LAUNCHER_1)));
5157 assertExistsAndShadow(user0.getAllLaunchersForTest().get(
5158 PackageWithUser.of(USER_0, LAUNCHER_2)));
Makoto Onuki2e210c42016-03-30 08:30:36 -07005159
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07005160 assertNull(user0.getAllLaunchersForTest().get(PackageWithUser.of(USER_0, LAUNCHER_3)));
5161 assertNull(user0.getAllLaunchersForTest().get(PackageWithUser.of(USER_P0, LAUNCHER_1)));
Makoto Onuki2e210c42016-03-30 08:30:36 -07005162
Michal Karpinski528c3e52018-02-07 17:47:10 +00005163 doReturn(true).when(mMockPackageManagerInternal).isDataRestoreSafe(any(byte[].class),
5164 anyString());
5165
Makoto Onuki2e210c42016-03-30 08:30:36 -07005166 installPackage(USER_0, CALLING_PACKAGE_1);
5167 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005168 assertWith(getCallerVisibleShortcuts())
5169 .selectDynamic()
5170 .isEmpty()
5171
5172 .revertToOriginalList()
5173 .selectPinned()
Makoto Onukia4f89b12017-10-05 10:37:55 -07005174 .haveIds("s1", "s2")
5175 .areAllEnabled();
Makoto Onuki2e210c42016-03-30 08:30:36 -07005176 });
5177
5178 installPackage(USER_0, LAUNCHER_1);
5179 runWithCaller(LAUNCHER_1, USER_0, () -> {
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005180 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5181 .areAllPinned()
Makoto Onukia4f89b12017-10-05 10:37:55 -07005182 .haveIds("s1")
5183 .areAllEnabled();
Makoto Onuki2e210c42016-03-30 08:30:36 -07005184
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005185 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5186 .isEmpty();
5187
5188 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5189 .isEmpty();
5190
5191 assertWith(mLauncherApps.getShortcuts(QUERY_ALL, HANDLE_USER_P0))
5192 .isEmpty();
Makoto Onuki2e210c42016-03-30 08:30:36 -07005193 });
5194
5195 installPackage(USER_0, CALLING_PACKAGE_2);
5196 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005197 assertWith(getCallerVisibleShortcuts())
5198 .selectDynamic()
5199 .isEmpty()
5200
5201 .revertToOriginalList()
5202 .selectPinned()
Makoto Onukia4f89b12017-10-05 10:37:55 -07005203 .haveIds("s1", "s2", "s3")
5204 .areAllEnabled();
Makoto Onuki2e210c42016-03-30 08:30:36 -07005205 });
5206
5207 runWithCaller(LAUNCHER_1, USER_0, () -> {
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005208 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5209 .areAllPinned()
Makoto Onukia4f89b12017-10-05 10:37:55 -07005210 .haveIds("s1")
5211 .areAllEnabled();
Makoto Onuki2e210c42016-03-30 08:30:36 -07005212
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005213 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5214 .areAllPinned()
Makoto Onukia4f89b12017-10-05 10:37:55 -07005215 .haveIds("s1", "s2")
5216 .areAllEnabled();
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005217
5218 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5219 .isEmpty();
5220
5221 assertWith(mLauncherApps.getShortcuts(QUERY_ALL, HANDLE_USER_P0))
5222 .isEmpty();
Makoto Onuki2e210c42016-03-30 08:30:36 -07005223 });
5224
5225 // 3 shouldn't be backed up, so no pinned shortcuts.
5226 installPackage(USER_0, CALLING_PACKAGE_3);
5227 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005228 assertWith(getCallerVisibleShortcuts())
5229 .isEmpty();
Makoto Onuki2e210c42016-03-30 08:30:36 -07005230 });
5231
5232 // Launcher on a different profile shouldn't be restored.
5233 runWithCaller(LAUNCHER_1, USER_P0, () -> {
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005234 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5235 .isEmpty();
5236 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5237 .isEmpty();
5238 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5239 .isEmpty();
Makoto Onuki2e210c42016-03-30 08:30:36 -07005240 });
5241
5242 // Package on a different profile, no restore.
5243 installPackage(USER_P0, CALLING_PACKAGE_1);
5244 runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005245 assertWith(getCallerVisibleShortcuts())
5246 .isEmpty();
Makoto Onuki2e210c42016-03-30 08:30:36 -07005247 });
5248
5249 // Restore launcher 2 on user 0.
5250 installPackage(USER_0, LAUNCHER_2);
5251 runWithCaller(LAUNCHER_2, USER_0, () -> {
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005252 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5253 .areAllPinned()
Makoto Onukia4f89b12017-10-05 10:37:55 -07005254 .haveIds("s2")
5255 .areAllEnabled();
Makoto Onuki2e210c42016-03-30 08:30:36 -07005256
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005257 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5258 .areAllPinned()
Makoto Onukia4f89b12017-10-05 10:37:55 -07005259 .haveIds("s2", "s3")
5260 .areAllEnabled();
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005261
Makoto Onukia4f89b12017-10-05 10:37:55 -07005262 if (firstRestore) {
5263 assertWith(
5264 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5265 .haveIds("s2", "s3", "s4")
5266 .areAllDisabled()
5267 .areAllPinned()
5268 .areAllNotDynamic()
5269 .areAllWithDisabledReason(
5270 ShortcutInfo.DISABLED_REASON_BACKUP_NOT_SUPPORTED);
5271 } else {
5272 assertWith(
5273 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5274 .isEmpty();
5275 }
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005276
5277 assertWith(mLauncherApps.getShortcuts(QUERY_ALL, HANDLE_USER_P0))
5278 .isEmpty();
Makoto Onuki2e210c42016-03-30 08:30:36 -07005279 });
5280
5281
5282 // Restoration of launcher2 shouldn't affect other packages; so do the same checks and
5283 // make sure they still have the same result.
5284 installPackage(USER_0, CALLING_PACKAGE_1);
5285 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005286 assertWith(getCallerVisibleShortcuts())
5287 .areAllPinned()
5288 .haveIds("s1", "s2");
Makoto Onuki2e210c42016-03-30 08:30:36 -07005289 });
5290
5291 installPackage(USER_0, LAUNCHER_1);
5292 runWithCaller(LAUNCHER_1, USER_0, () -> {
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005293 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5294 .areAllPinned()
Makoto Onukia4f89b12017-10-05 10:37:55 -07005295 .haveIds("s1")
5296 .areAllEnabled();
Makoto Onuki2e210c42016-03-30 08:30:36 -07005297
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005298 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5299 .areAllPinned()
Makoto Onukia4f89b12017-10-05 10:37:55 -07005300 .haveIds("s1", "s2")
5301 .areAllEnabled();
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005302
Makoto Onukia4f89b12017-10-05 10:37:55 -07005303 if (firstRestore) {
5304 assertWith(
5305 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5306 .haveIds("s1", "s2", "s3")
5307 .areAllDisabled()
5308 .areAllPinned()
5309 .areAllNotDynamic()
5310 .areAllWithDisabledReason(ShortcutInfo.DISABLED_REASON_BACKUP_NOT_SUPPORTED);
5311 } else {
5312 assertWith(
5313 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5314 .isEmpty();
5315 }
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005316
5317 assertWith(mLauncherApps.getShortcuts(QUERY_ALL, HANDLE_USER_P0))
5318 .isEmpty();
Makoto Onuki2e210c42016-03-30 08:30:36 -07005319 });
5320
5321 installPackage(USER_0, CALLING_PACKAGE_2);
5322 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005323 assertWith(getCallerVisibleShortcuts())
5324 .areAllPinned()
Makoto Onukia4f89b12017-10-05 10:37:55 -07005325 .haveIds("s1", "s2", "s3")
5326 .areAllEnabled();
Makoto Onuki2e210c42016-03-30 08:30:36 -07005327 });
5328 }
5329
5330 public void testBackupAndRestore_publisherLowerVersion() {
5331 prepareForBackupTest();
5332
Makoto Onuki2e210c42016-03-30 08:30:36 -07005333 addPackage(CALLING_PACKAGE_1, CALLING_UID_1, 0); // Lower version
5334
Makoto Onukia4f89b12017-10-05 10:37:55 -07005335 checkBackupAndRestore_publisherNotRestored(ShortcutInfo.DISABLED_REASON_VERSION_LOWER);
Makoto Onuki2e210c42016-03-30 08:30:36 -07005336 }
5337
5338 public void testBackupAndRestore_publisherWrongSignature() {
5339 prepareForBackupTest();
5340
Makoto Onuki2e210c42016-03-30 08:30:36 -07005341 addPackage(CALLING_PACKAGE_1, CALLING_UID_1, 10, "sigx"); // different signature
5342
Makoto Onukia4f89b12017-10-05 10:37:55 -07005343 checkBackupAndRestore_publisherNotRestored(ShortcutInfo.DISABLED_REASON_SIGNATURE_MISMATCH);
Makoto Onuki2e210c42016-03-30 08:30:36 -07005344 }
5345
5346 public void testBackupAndRestore_publisherNoLongerBackupTarget() {
5347 prepareForBackupTest();
5348
Makoto Onuki2e210c42016-03-30 08:30:36 -07005349 updatePackageInfo(CALLING_PACKAGE_1,
5350 pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP);
5351
Makoto Onukia4f89b12017-10-05 10:37:55 -07005352 checkBackupAndRestore_publisherNotRestored(
5353 ShortcutInfo.DISABLED_REASON_BACKUP_NOT_SUPPORTED);
Makoto Onuki2e210c42016-03-30 08:30:36 -07005354 }
5355
Makoto Onukia4f89b12017-10-05 10:37:55 -07005356 protected void checkBackupAndRestore_publisherNotRestored(
5357 int package1DisabledReason) {
Michal Karpinski528c3e52018-02-07 17:47:10 +00005358 doReturn(package1DisabledReason != ShortcutInfo.DISABLED_REASON_SIGNATURE_MISMATCH).when(
5359 mMockPackageManagerInternal).isDataRestoreSafe(any(byte[].class),
5360 eq(CALLING_PACKAGE_1));
5361
Makoto Onuki2e210c42016-03-30 08:30:36 -07005362 installPackage(USER_0, CALLING_PACKAGE_1);
5363 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5364 assertEquals(0, mManager.getDynamicShortcuts().size());
5365 assertEquals(0, mManager.getPinnedShortcuts().size());
5366 });
Makoto Onukifac592f2016-11-21 13:41:32 -08005367 assertFalse(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, USER_0)
5368 .getPackageInfo().isShadow());
5369
Michal Karpinski528c3e52018-02-07 17:47:10 +00005370 doReturn(true).when(mMockPackageManagerInternal).isDataRestoreSafe(
5371 any(byte[].class), anyString());
Makoto Onuki2e210c42016-03-30 08:30:36 -07005372
5373 installPackage(USER_0, CALLING_PACKAGE_2);
5374 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5375 assertEquals(0, mManager.getDynamicShortcuts().size());
5376 assertShortcutIds(assertAllPinned(
5377 mManager.getPinnedShortcuts()),
5378 "s1", "s2", "s3");
5379 });
Makoto Onukifac592f2016-11-21 13:41:32 -08005380 assertFalse(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, USER_0)
5381 .getPackageInfo().isShadow());
Makoto Onuki2e210c42016-03-30 08:30:36 -07005382
5383 installPackage(USER_0, LAUNCHER_1);
5384 runWithCaller(LAUNCHER_1, USER_0, () -> {
Makoto Onukia4f89b12017-10-05 10:37:55 -07005385 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5386 .haveIds("s1")
5387 .areAllPinned()
5388 .areAllDisabled()
Makoto Onukib1588c02017-10-12 15:11:45 -07005389 .areAllWithDisabledReason(package1DisabledReason)
5390 .forAllShortcuts(si -> {
5391 switch (package1DisabledReason) {
5392 case ShortcutInfo.DISABLED_REASON_VERSION_LOWER:
Makoto Onuki7eb8b952018-02-01 11:31:03 -08005393 assertEquals("App version downgraded, or isn’t compatible"
5394 + " with this shortcut",
Makoto Onukib1588c02017-10-12 15:11:45 -07005395 si.getDisabledMessage());
5396 break;
5397 case ShortcutInfo.DISABLED_REASON_SIGNATURE_MISMATCH:
5398 assertEquals(
5399 "Couldn\u2019t restore shortcut because of app"
5400 + " signature mismatch",
5401 si.getDisabledMessage());
5402 break;
5403 case ShortcutInfo.DISABLED_REASON_BACKUP_NOT_SUPPORTED:
5404 assertEquals(
5405 "Couldn\u2019t restore shortcut because app"
5406 + " doesn\u2019t support backup and restore",
5407 si.getDisabledMessage());
5408 break;
5409 default:
5410 fail("Unhandled disabled reason: " + package1DisabledReason);
5411 }
5412 });
Makoto Onukia4f89b12017-10-05 10:37:55 -07005413 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5414 .haveIds("s1", "s2")
5415 .areAllPinned()
5416 .areAllEnabled();
5417 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5418 .isEmpty();
Makoto Onuki2e210c42016-03-30 08:30:36 -07005419 });
5420 installPackage(USER_0, LAUNCHER_2);
5421 runWithCaller(LAUNCHER_2, USER_0, () -> {
Makoto Onukia4f89b12017-10-05 10:37:55 -07005422 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5423 .haveIds("s2")
5424 .areAllPinned()
5425 .areAllDisabled()
5426 .areAllWithDisabledReason(package1DisabledReason);
5427 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5428 .haveIds("s2", "s3")
5429 .areAllPinned()
5430 .areAllEnabled();
5431 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5432 .isEmpty();
Makoto Onuki2e210c42016-03-30 08:30:36 -07005433 });
5434
5435 installPackage(USER_0, CALLING_PACKAGE_3);
5436 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
5437 assertEquals(0, mManager.getDynamicShortcuts().size());
5438 assertEquals(0, mManager.getPinnedShortcuts().size());
5439 });
5440
5441 runWithCaller(LAUNCHER_1, USER_0, () -> {
Makoto Onukia4f89b12017-10-05 10:37:55 -07005442 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5443 .haveIds("s1")
5444 .areAllPinned()
5445 .areAllDisabled()
5446 .areAllWithDisabledReason(package1DisabledReason);
5447 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5448 .haveIds("s1", "s2")
5449 .areAllPinned()
5450 .areAllEnabled();
5451 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5452 .haveIds("s1", "s2", "s3")
5453 .areAllPinned()
5454 .areAllDisabled()
5455 .areAllWithDisabledReason(ShortcutInfo.DISABLED_REASON_BACKUP_NOT_SUPPORTED);
Makoto Onuki2e210c42016-03-30 08:30:36 -07005456 });
5457 runWithCaller(LAUNCHER_2, USER_0, () -> {
Makoto Onukia4f89b12017-10-05 10:37:55 -07005458 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5459 .haveIds("s2")
5460 .areAllPinned()
5461 .areAllDisabled()
5462 .areAllWithDisabledReason(package1DisabledReason);
5463 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5464 .haveIds("s2", "s3")
5465 .areAllPinned()
5466 .areAllEnabled();
5467 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5468 .haveIds("s2", "s3", "s4")
5469 .areAllPinned()
5470 .areAllDisabled()
5471 .areAllWithDisabledReason(ShortcutInfo.DISABLED_REASON_BACKUP_NOT_SUPPORTED);
Makoto Onuki2e210c42016-03-30 08:30:36 -07005472 });
5473 }
5474
5475 public void testBackupAndRestore_launcherLowerVersion() {
5476 prepareForBackupTest();
5477
Makoto Onuki2e210c42016-03-30 08:30:36 -07005478 addPackage(LAUNCHER_1, LAUNCHER_UID_1, 0); // Lower version
5479
Makoto Onukia4f89b12017-10-05 10:37:55 -07005480 // Note, we restore pinned shortcuts even if the launcher is of a lower version.
5481 checkBackupAndRestore_success(/*firstRestore=*/ true);
Makoto Onuki2e210c42016-03-30 08:30:36 -07005482 }
5483
5484 public void testBackupAndRestore_launcherWrongSignature() {
5485 prepareForBackupTest();
5486
Makoto Onuki2e210c42016-03-30 08:30:36 -07005487 addPackage(LAUNCHER_1, LAUNCHER_UID_1, 10, "sigx"); // different signature
5488
Michal Karpinski528c3e52018-02-07 17:47:10 +00005489 checkBackupAndRestore_launcherNotRestored(true);
Makoto Onuki2e210c42016-03-30 08:30:36 -07005490 }
5491
5492 public void testBackupAndRestore_launcherNoLongerBackupTarget() {
5493 prepareForBackupTest();
5494
Makoto Onuki2e210c42016-03-30 08:30:36 -07005495 updatePackageInfo(LAUNCHER_1,
5496 pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP);
5497
Michal Karpinski528c3e52018-02-07 17:47:10 +00005498 checkBackupAndRestore_launcherNotRestored(false);
Makoto Onuki2e210c42016-03-30 08:30:36 -07005499 }
5500
Michal Karpinski528c3e52018-02-07 17:47:10 +00005501 protected void checkBackupAndRestore_launcherNotRestored(boolean differentSignatures) {
5502 doReturn(true).when(mMockPackageManagerInternal).isDataRestoreSafe(
5503 any(byte[].class), anyString());
5504
Makoto Onuki2e210c42016-03-30 08:30:36 -07005505 installPackage(USER_0, CALLING_PACKAGE_1);
5506 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5507 assertEquals(0, mManager.getDynamicShortcuts().size());
5508
5509 // s1 was pinned by launcher 1, which is not restored, yet, so we still see "s1" here.
5510 assertShortcutIds(assertAllPinned(
5511 mManager.getPinnedShortcuts()),
5512 "s1", "s2");
5513 });
5514
5515 installPackage(USER_0, CALLING_PACKAGE_2);
5516 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5517 assertEquals(0, mManager.getDynamicShortcuts().size());
5518 assertShortcutIds(assertAllPinned(
5519 mManager.getPinnedShortcuts()),
5520 "s1", "s2", "s3");
5521 });
5522
Michal Karpinski528c3e52018-02-07 17:47:10 +00005523 doReturn(!differentSignatures).when(mMockPackageManagerInternal).isDataRestoreSafe(
5524 any(byte[].class), eq(LAUNCHER_1));
5525
Makoto Onuki2e210c42016-03-30 08:30:36 -07005526 // Now we try to restore launcher 1. Then we realize it's not restorable, so L1 has no pinned
5527 // shortcuts.
5528 installPackage(USER_0, LAUNCHER_1);
5529 runWithCaller(LAUNCHER_1, USER_0, () -> {
5530 assertShortcutIds(assertAllPinned(
5531 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5532 /* empty */);
5533 assertShortcutIds(assertAllPinned(
5534 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5535 /* empty */);
5536 assertShortcutIds(assertAllPinned(
5537 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5538 /* empty */);
5539 });
Makoto Onukifac592f2016-11-21 13:41:32 -08005540 assertFalse(mService.getLauncherShortcutForTest(LAUNCHER_1, USER_0)
5541 .getPackageInfo().isShadow());
Makoto Onuki2e210c42016-03-30 08:30:36 -07005542
5543 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5544 assertEquals(0, mManager.getDynamicShortcuts().size());
5545
5546 // Now CALLING_PACKAGE_1 realizes "s1" is no longer pinned.
5547 assertShortcutIds(assertAllPinned(
5548 mManager.getPinnedShortcuts()),
5549 "s2");
5550 });
5551
Michal Karpinski528c3e52018-02-07 17:47:10 +00005552 doReturn(true).when(mMockPackageManagerInternal).isDataRestoreSafe(
5553 any(byte[].class), anyString());
5554
Makoto Onuki2e210c42016-03-30 08:30:36 -07005555 installPackage(USER_0, LAUNCHER_2);
5556 runWithCaller(LAUNCHER_2, USER_0, () -> {
5557 assertShortcutIds(assertAllPinned(
5558 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0)),
5559 "s2");
5560 assertShortcutIds(assertAllPinned(
5561 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0)),
5562 "s2", "s3");
5563 assertShortcutIds(assertAllPinned(
5564 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5565 /* empty */);
5566 });
Makoto Onukifac592f2016-11-21 13:41:32 -08005567 assertFalse(mService.getLauncherShortcutForTest(LAUNCHER_2, USER_0)
5568 .getPackageInfo().isShadow());
Makoto Onuki2e210c42016-03-30 08:30:36 -07005569
5570 installPackage(USER_0, CALLING_PACKAGE_3);
5571 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
5572 assertEquals(0, mManager.getDynamicShortcuts().size());
5573 assertEquals(0, mManager.getPinnedShortcuts().size());
5574 });
5575
5576 runWithCaller(LAUNCHER_1, USER_0, () -> {
5577 assertShortcutIds(assertAllPinned(
5578 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5579 /* empty */);
5580 assertShortcutIds(assertAllPinned(
5581 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5582 /* empty */);
5583 assertShortcutIds(assertAllPinned(
5584 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5585 /* empty */);
5586 });
5587 runWithCaller(LAUNCHER_2, USER_0, () -> {
5588 assertShortcutIds(assertAllPinned(
5589 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0)),
5590 "s2");
5591 assertShortcutIds(assertAllPinned(
5592 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0)),
5593 "s2", "s3");
Makoto Onuki2e210c42016-03-30 08:30:36 -07005594 });
5595 }
5596
5597 public void testBackupAndRestore_launcherAndPackageNoLongerBackupTarget() {
5598 prepareForBackupTest();
5599
Makoto Onuki2e210c42016-03-30 08:30:36 -07005600 updatePackageInfo(CALLING_PACKAGE_1,
5601 pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP);
5602
5603 updatePackageInfo(LAUNCHER_1,
5604 pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP);
5605
5606 checkBackupAndRestore_publisherAndLauncherNotRestored();
5607 }
5608
Makoto Onuki51ab2b32016-06-02 11:03:51 -07005609 protected void checkBackupAndRestore_publisherAndLauncherNotRestored() {
Michal Karpinski528c3e52018-02-07 17:47:10 +00005610 doReturn(true).when(mMockPackageManagerInternal).isDataRestoreSafe(any(byte[].class),
5611 anyString());
Makoto Onuki2e210c42016-03-30 08:30:36 -07005612 installPackage(USER_0, CALLING_PACKAGE_1);
5613 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5614 assertEquals(0, mManager.getDynamicShortcuts().size());
5615 assertEquals(0, mManager.getPinnedShortcuts().size());
5616 });
5617
5618 installPackage(USER_0, CALLING_PACKAGE_2);
5619 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5620 assertEquals(0, mManager.getDynamicShortcuts().size());
5621 assertShortcutIds(assertAllPinned(
5622 mManager.getPinnedShortcuts()),
5623 "s1", "s2", "s3");
5624 });
5625
5626 installPackage(USER_0, LAUNCHER_1);
5627 runWithCaller(LAUNCHER_1, USER_0, () -> {
5628 assertShortcutIds(assertAllPinned(
5629 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5630 /* empty */);
5631 assertShortcutIds(assertAllPinned(
5632 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5633 /* empty */);
5634 assertShortcutIds(assertAllPinned(
5635 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5636 /* empty */);
5637 });
5638 installPackage(USER_0, LAUNCHER_2);
5639 runWithCaller(LAUNCHER_2, USER_0, () -> {
Makoto Onukia4f89b12017-10-05 10:37:55 -07005640 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5641 .areAllPinned()
5642 .haveIds("s2")
5643 .areAllDisabled();
5644 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5645 .areAllPinned()
5646 .haveIds("s2", "s3");
5647 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5648 .isEmpty();
Makoto Onuki2e210c42016-03-30 08:30:36 -07005649 });
5650
5651 // Because launcher 1 wasn't restored, "s1" is no longer pinned.
5652 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
5653 assertEquals(0, mManager.getDynamicShortcuts().size());
5654 assertShortcutIds(assertAllPinned(
5655 mManager.getPinnedShortcuts()),
5656 "s2", "s3");
5657 });
5658
5659 installPackage(USER_0, CALLING_PACKAGE_3);
5660 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
5661 assertEquals(0, mManager.getDynamicShortcuts().size());
5662 assertEquals(0, mManager.getPinnedShortcuts().size());
5663 });
5664
5665 runWithCaller(LAUNCHER_1, USER_0, () -> {
5666 assertShortcutIds(assertAllPinned(
5667 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5668 /* empty */);
5669 assertShortcutIds(assertAllPinned(
5670 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5671 /* empty */);
5672 assertShortcutIds(assertAllPinned(
5673 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5674 /* empty */);
5675 });
5676 runWithCaller(LAUNCHER_2, USER_0, () -> {
Makoto Onukia4f89b12017-10-05 10:37:55 -07005677 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5678 .areAllPinned()
5679 .haveIds("s2")
5680 .areAllDisabled();
5681 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5682 .areAllPinned()
5683 .haveIds("s2", "s3");
5684 assertWith(
Makoto Onuki2e210c42016-03-30 08:30:36 -07005685 mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
Makoto Onukia4f89b12017-10-05 10:37:55 -07005686 .haveIds("s2", "s3", "s4")
5687 .areAllDisabled()
5688 .areAllPinned()
5689 .areAllNotDynamic()
5690 .areAllWithDisabledReason(
5691 ShortcutInfo.DISABLED_REASON_BACKUP_NOT_SUPPORTED);
Makoto Onuki2e210c42016-03-30 08:30:36 -07005692 });
5693 }
5694
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005695 public void testBackupAndRestore_disabled() {
5696 prepareCrossProfileDataSet();
5697
5698 // Before doing backup & restore, disable s1.
5699 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5700 mManager.disableShortcuts(list("s1"));
5701 });
5702
5703 backupAndRestore();
5704
5705 // Below is copied from checkBackupAndRestore_success.
5706
5707 // Make sure non-system user is not restored.
5708 final ShortcutUser userP0 = mService.getUserShortcutsLocked(USER_P0);
5709 assertEquals(0, userP0.getAllPackagesForTest().size());
5710 assertEquals(0, userP0.getAllLaunchersForTest().size());
5711
5712 // Make sure only "allowBackup" apps are restored, and are shadow.
5713 final ShortcutUser user0 = mService.getUserShortcutsLocked(USER_0);
5714 assertExistsAndShadow(user0.getAllPackagesForTest().get(CALLING_PACKAGE_1));
5715 assertExistsAndShadow(user0.getAllPackagesForTest().get(CALLING_PACKAGE_2));
Makoto Onukia4f89b12017-10-05 10:37:55 -07005716 assertExistsAndShadow(user0.getAllPackagesForTest().get(CALLING_PACKAGE_3));
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005717 assertExistsAndShadow(user0.getAllLaunchersForTest().get(
5718 PackageWithUser.of(USER_0, LAUNCHER_1)));
5719 assertExistsAndShadow(user0.getAllLaunchersForTest().get(
5720 PackageWithUser.of(USER_0, LAUNCHER_2)));
5721
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005722 assertNull(user0.getAllLaunchersForTest().get(PackageWithUser.of(USER_0, LAUNCHER_3)));
5723 assertNull(user0.getAllLaunchersForTest().get(PackageWithUser.of(USER_P0, LAUNCHER_1)));
5724
Michal Karpinski528c3e52018-02-07 17:47:10 +00005725 doReturn(true).when(mMockPackageManagerInternal).isDataRestoreSafe(any(byte[].class),
5726 anyString());
5727
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005728 installPackage(USER_0, CALLING_PACKAGE_1);
5729 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5730 assertWith(getCallerVisibleShortcuts())
5731 .areAllEnabled() // disabled shortcuts shouldn't be restored.
5732
5733 .selectDynamic()
5734 .isEmpty()
5735
5736 .revertToOriginalList()
5737 .selectPinned()
5738 // s1 is not restored.
5739 .haveIds("s2");
5740 });
5741
5742 installPackage(USER_0, LAUNCHER_1);
5743 runWithCaller(LAUNCHER_1, USER_0, () -> {
5744 // Note, s1 was pinned by launcher 1, but was disabled, so isn't restored.
5745 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
5746 .isEmpty();
5747
5748 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
5749 .isEmpty();
5750
5751 assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
5752 .isEmpty();
5753
5754 assertWith(mLauncherApps.getShortcuts(QUERY_ALL, HANDLE_USER_P0))
5755 .isEmpty();
5756 });
5757 }
5758
5759
Makoto Onuki377b7972016-08-09 14:43:55 -07005760 public void testBackupAndRestore_manifestRePublished() {
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005761 // Publish two manifest shortcuts.
5762 addManifestShortcutResource(
5763 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
5764 R.xml.shortcut_2);
5765 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07005766 mService.mPackageMonitor.onReceive(mServiceContext,
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005767 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
5768
Makoto Onuki377b7972016-08-09 14:43:55 -07005769 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5770 assertTrue(mManager.setDynamicShortcuts(list(
5771 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
5772 });
5773
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005774 // Pin from launcher 1.
5775 runWithCaller(LAUNCHER_1, USER_0, () -> {
Makoto Onuki377b7972016-08-09 14:43:55 -07005776 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
5777 list("ms1", "ms2", "s1", "s2"), HANDLE_USER_0);
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005778 });
5779
5780 // Update and now ms2 is gone -> disabled.
5781 addManifestShortcutResource(
5782 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
5783 R.xml.shortcut_1);
5784 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07005785 mService.mPackageMonitor.onReceive(mServiceContext,
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005786 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
5787
5788 // Make sure the manifest shortcuts have been published.
5789 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5790 assertWith(getCallerShortcuts())
Makoto Onuki377b7972016-08-09 14:43:55 -07005791 .selectManifest()
5792 .haveIds("ms1")
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005793
Makoto Onuki377b7972016-08-09 14:43:55 -07005794 .revertToOriginalList()
5795 .selectDynamic()
5796 .haveIds("s1", "s2", "s3")
5797
5798 .revertToOriginalList()
5799 .selectPinned()
5800 .haveIds("ms1", "ms2", "s1", "s2")
5801
5802 .revertToOriginalList()
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005803 .selectByIds("ms1")
5804 .areAllManifest()
5805 .areAllEnabled()
5806
5807 .revertToOriginalList()
5808 .selectByIds("ms2")
5809 .areAllNotManifest()
5810 .areAllDisabled();
5811 });
5812
Michal Karpinski528c3e52018-02-07 17:47:10 +00005813 doReturn(true).when(mMockPackageManagerInternal).isDataRestoreSafe(
5814 any(byte[].class), anyString());
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005815 backupAndRestore();
Makoto Onuki377b7972016-08-09 14:43:55 -07005816
5817 // When re-installing the app, the manifest shortcut should be re-published.
5818 mService.mPackageMonitor.onReceive(mServiceContext,
5819 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
5820 mService.mPackageMonitor.onReceive(mServiceContext,
5821 genPackageAddIntent(LAUNCHER_1, USER_0));
5822
5823 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5824 assertWith(getCallerVisibleShortcuts())
5825 .selectPinned()
5826 // ms2 was disabled, so not restored.
5827 .haveIds("ms1", "s1", "s2")
5828 .areAllEnabled()
5829
5830 .revertToOriginalList()
5831 .selectByIds("ms1")
5832 .areAllManifest()
5833
5834 .revertToOriginalList()
5835 .selectByIds("s1", "s2")
5836 .areAllNotDynamic()
Makoto Onukia4f89b12017-10-05 10:37:55 -07005837 ;
Makoto Onuki377b7972016-08-09 14:43:55 -07005838 });
5839 }
5840
5841 /**
5842 * It's the case with preintalled apps -- when applyRestore() is called, the system
5843 * apps are already installed, so manifest shortcuts need to be re-published.
Makoto Onukifc4cf2d2016-08-24 11:10:26 -07005844 *
5845 * Also, when a restore target app is already installed, and
5846 * - if it has allowBackup=true, we'll restore normally, so all existing shortcuts will be
5847 * replaced. (but manifest shortcuts will be re-published anyway.) We log a warning on
5848 * logcat.
5849 * - if it has allowBackup=false, we don't touch any of the existing shortcuts.
Makoto Onuki377b7972016-08-09 14:43:55 -07005850 */
5851 public void testBackupAndRestore_appAlreadyInstalledWhenRestored() {
5852 // Pre-backup. Same as testBackupAndRestore_manifestRePublished().
5853
5854 // Publish two manifest shortcuts.
5855 addManifestShortcutResource(
5856 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
5857 R.xml.shortcut_2);
5858 updatePackageVersion(CALLING_PACKAGE_1, 1);
5859 mService.mPackageMonitor.onReceive(mServiceContext,
5860 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
5861
5862 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5863 assertTrue(mManager.setDynamicShortcuts(list(
5864 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
5865 });
5866
5867 // Pin from launcher 1.
5868 runWithCaller(LAUNCHER_1, USER_0, () -> {
5869 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
5870 list("ms1", "ms2", "s1", "s2"), HANDLE_USER_0);
5871 });
5872
5873 // Update and now ms2 is gone -> disabled.
5874 addManifestShortcutResource(
5875 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
5876 R.xml.shortcut_1);
5877 updatePackageVersion(CALLING_PACKAGE_1, 1);
5878 mService.mPackageMonitor.onReceive(mServiceContext,
5879 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
5880
Makoto Onukifc4cf2d2016-08-24 11:10:26 -07005881 // Set up shortcuts for package 3, which won't be backed up / restored.
5882 addManifestShortcutResource(
5883 new ComponentName(CALLING_PACKAGE_3, ShortcutActivity.class.getName()),
5884 R.xml.shortcut_1);
5885 updatePackageVersion(CALLING_PACKAGE_3, 1);
5886 mService.mPackageMonitor.onReceive(mServiceContext,
5887 genPackageAddIntent(CALLING_PACKAGE_3, USER_0));
5888
5889 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
5890 assertTrue(getManager().setDynamicShortcuts(list(
5891 makeShortcut("s1"))));
5892 });
5893
Makoto Onuki377b7972016-08-09 14:43:55 -07005894 // Make sure the manifest shortcuts have been published.
5895 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5896 assertWith(getCallerShortcuts())
5897 .selectManifest()
5898 .haveIds("ms1")
5899
5900 .revertToOriginalList()
5901 .selectDynamic()
5902 .haveIds("s1", "s2", "s3")
5903
5904 .revertToOriginalList()
5905 .selectPinned()
5906 .haveIds("ms1", "ms2", "s1", "s2")
5907
5908 .revertToOriginalList()
5909 .selectByIds("ms1")
5910 .areAllManifest()
5911 .areAllEnabled()
5912
5913 .revertToOriginalList()
5914 .selectByIds("ms2")
5915 .areAllNotManifest()
5916 .areAllDisabled();
5917 });
5918
Makoto Onukifc4cf2d2016-08-24 11:10:26 -07005919 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
5920 assertWith(getCallerShortcuts())
5921 .haveIds("s1", "ms1");
5922 });
5923
Michal Karpinski528c3e52018-02-07 17:47:10 +00005924 doReturn(true).when(mMockPackageManagerInternal).isDataRestoreSafe(any(byte[].class),
5925 anyString());
Makoto Onuki377b7972016-08-09 14:43:55 -07005926 // Backup and *without restarting the service, just call applyRestore()*.
5927 {
5928 int prevUid = mInjectedCallingUid;
5929 mInjectedCallingUid = Process.SYSTEM_UID; // Only system can call it.
5930
5931 dumpsysOnLogcat("Before backup");
5932
5933 final byte[] payload = mService.getBackupPayload(USER_0);
5934 if (ENABLE_DUMP) {
5935 final String xml = new String(payload);
5936 Log.v(TAG, "Backup payload:");
5937 for (String line : xml.split("\n")) {
5938 Log.v(TAG, line);
5939 }
5940 }
5941 mService.applyRestore(payload, USER_0);
5942
5943 dumpsysOnLogcat("After restore");
5944
5945 mInjectedCallingUid = prevUid;
5946 }
5947
5948 // The check is also the same as testBackupAndRestore_manifestRePublished().
5949 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5950 assertWith(getCallerVisibleShortcuts())
5951 .selectPinned()
5952 // ms2 was disabled, so not restored.
5953 .haveIds("ms1", "s1", "s2")
5954 .areAllEnabled()
5955
5956 .revertToOriginalList()
5957 .selectByIds("ms1")
5958 .areAllManifest()
5959
5960 .revertToOriginalList()
5961 .selectByIds("s1", "s2")
5962 .areAllNotDynamic()
5963 ;
5964 });
Makoto Onukifc4cf2d2016-08-24 11:10:26 -07005965
5966 // Package 3 still has the same shortcuts.
5967 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
5968 assertWith(getCallerShortcuts())
5969 .haveIds("s1", "ms1");
5970 });
Makoto Onukif3ba2e02016-07-12 09:18:50 -07005971 }
5972
Makoto Onukia4f89b12017-10-05 10:37:55 -07005973
5974 /**
5975 * Restored to a lower version with no manifest shortcuts. All shortcuts are now invisible,
5976 * and all calls from the publisher should ignore them.
5977 */
5978 public void testBackupAndRestore_disabledShortcutsAreIgnored() {
5979 // Publish two manifest shortcuts.
5980 addManifestShortcutResource(
5981 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
5982 R.xml.shortcut_5_altalt);
5983 updatePackageVersion(CALLING_PACKAGE_1, 1);
5984 mService.mPackageMonitor.onReceive(mServiceContext,
5985 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
5986
5987 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
5988 assertTrue(mManager.setDynamicShortcuts(list(
5989 makeShortcutWithShortLabel("s1", "original-title"),
5990 makeShortcut("s2"), makeShortcut("s3"))));
5991 });
5992
5993 // Pin from launcher 1.
5994 runWithCaller(LAUNCHER_1, USER_0, () -> {
5995 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
5996 list("ms1", "ms2", "ms3", "ms4", "s1", "s2"), HANDLE_USER_0);
5997 });
5998
Michal Karpinski528c3e52018-02-07 17:47:10 +00005999 doReturn(true).when(mMockPackageManagerInternal).isDataRestoreSafe(
6000 any(byte[].class), anyString());
6001
Makoto Onukia4f89b12017-10-05 10:37:55 -07006002 backupAndRestore();
6003
6004 // Lower the version and remove the manifest shortcuts.
6005 addManifestShortcutResource(
6006 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6007 R.xml.shortcut_0);
6008 addPackage(CALLING_PACKAGE_1, CALLING_UID_1, 0); // Lower version
6009
6010 // When re-installing the app, the manifest shortcut should be re-published.
6011 mService.mPackageMonitor.onReceive(mServiceContext,
6012 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6013 mService.mPackageMonitor.onReceive(mServiceContext,
6014 genPackageAddIntent(LAUNCHER_1, USER_0));
6015
6016 // No shortcuts should be visible to the publisher.
6017 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6018 assertWith(getCallerVisibleShortcuts())
6019 .isEmpty();
6020 });
6021
6022 final Runnable checkAllDisabledForLauncher = () -> {
6023 runWithCaller(LAUNCHER_1, USER_0, () -> {
6024 assertWith(getShortcutAsLauncher(USER_0))
6025 .areAllPinned()
6026 .haveIds("ms1", "ms2", "ms3", "ms4", "s1", "s2")
6027 .areAllDisabled()
6028 .areAllWithDisabledReason(ShortcutInfo.DISABLED_REASON_VERSION_LOWER)
6029
6030 .forShortcutWithId("s1", si -> {
6031 assertEquals("original-title", si.getShortLabel());
6032 })
6033 .forShortcutWithId("ms1", si -> {
6034 assertEquals("string-com.android.test.1-user:0-res:"
6035 + R.string.shortcut_title1 + "/en"
6036 , si.getShortLabel());
6037 })
6038 .forShortcutWithId("ms2", si -> {
6039 assertEquals("string-com.android.test.1-user:0-res:"
6040 + R.string.shortcut_title2 + "/en"
6041 , si.getShortLabel());
6042 })
6043 .forShortcutWithId("ms3", si -> {
6044 assertEquals("string-com.android.test.1-user:0-res:"
6045 + R.string.shortcut_title1 + "/en"
6046 , si.getShortLabel());
6047 assertEquals("string-com.android.test.1-user:0-res:"
6048 + R.string.shortcut_title2 + "/en"
6049 , si.getLongLabel());
6050 })
6051 .forShortcutWithId("ms4", si -> {
6052 assertEquals("string-com.android.test.1-user:0-res:"
6053 + R.string.shortcut_title2 + "/en"
6054 , si.getShortLabel());
6055 assertEquals("string-com.android.test.1-user:0-res:"
6056 + R.string.shortcut_title2 + "/en"
6057 , si.getLongLabel());
6058 });
6059 });
6060 };
6061
6062 checkAllDisabledForLauncher.run();
6063
6064 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6065
6066 makeCallerForeground(); // CALLING_PACKAGE_1 is now in the foreground.
6067
6068 // All changing API calls should be ignored.
6069
6070 getManager().enableShortcuts(list("ms1", "ms2", "ms3", "ms4", "s1", "s2"));
6071 checkAllDisabledForLauncher.run();
6072
6073 getManager().enableShortcuts(list("ms1", "ms2", "ms3", "ms4", "s1", "s2"));
6074 checkAllDisabledForLauncher.run();
6075
6076 getManager().enableShortcuts(list("ms1", "ms2", "ms3", "ms4", "s1", "s2"));
6077 checkAllDisabledForLauncher.run();
6078
6079 getManager().removeAllDynamicShortcuts();
6080 getManager().removeDynamicShortcuts(list("ms1", "ms2", "ms3", "ms4", "s1", "s2"));
6081 checkAllDisabledForLauncher.run();
6082
6083 getManager().updateShortcuts(list(makeShortcutWithShortLabel("s1", "new-title")));
6084 checkAllDisabledForLauncher.run();
6085
6086
6087 // Add a shortcut -- even though ms1 was immutable, it will succeed.
6088 assertTrue(getManager().addDynamicShortcuts(list(
6089 makeShortcutWithShortLabel("ms1", "original-title"))));
6090
6091 runWithCaller(LAUNCHER_1, USER_0, () -> {
6092 assertWith(getShortcutAsLauncher(USER_0))
6093 .haveIds("ms1", "ms2", "ms3", "ms4", "s1", "s2")
6094
6095 .selectByIds("ms1")
6096 .areAllEnabled()
6097 .areAllDynamic()
6098 .areAllPinned()
6099 .forAllShortcuts(si -> {
6100 assertEquals("original-title", si.getShortLabel());
6101 })
6102
6103 // The rest still exist and disabled.
6104 .revertToOriginalList()
6105 .selectByIds("ms2", "ms3", "ms4", "s1", "s2")
6106 .areAllDisabled()
6107 .areAllPinned()
6108 ;
6109 });
6110
6111 assertTrue(getManager().setDynamicShortcuts(list(
6112 makeShortcutWithShortLabel("ms2", "new-title-2"))));
6113
6114 runWithCaller(LAUNCHER_1, USER_0, () -> {
6115 assertWith(getShortcutAsLauncher(USER_0))
6116 .haveIds("ms1", "ms2", "ms3", "ms4", "s1", "s2")
6117
6118 .selectByIds("ms1")
6119 .areAllEnabled()
6120 .areAllNotDynamic() // ms1 was not in the list, so no longer dynamic.
6121 .areAllPinned()
6122 .areAllMutable()
6123 .forAllShortcuts(si -> {
6124 assertEquals("original-title", si.getShortLabel());
6125 })
6126
6127 .revertToOriginalList()
6128 .selectByIds("ms2")
6129 .areAllEnabled()
6130 .areAllDynamic()
6131 .areAllPinned()
6132 .areAllMutable()
6133 .forAllShortcuts(si -> {
6134 assertEquals("new-title-2", si.getShortLabel());
6135 })
6136
6137 // The rest still exist and disabled.
6138 .revertToOriginalList()
6139 .selectByIds("ms3", "ms4", "s1", "s2")
6140 .areAllDisabled()
6141 .areAllPinned()
6142 ;
6143 });
6144
6145 // Prepare for requestPinShortcut().
6146 setDefaultLauncher(USER_0, mMainActivityFetcher.apply(LAUNCHER_1, USER_0));
6147 mPinConfirmActivityFetcher = (packageName, userId) ->
6148 new ComponentName(packageName, PIN_CONFIRM_ACTIVITY_CLASS);
6149
6150 mManager.requestPinShortcut(
6151 makeShortcutWithShortLabel("ms3", "new-title-3"),
6152 /*PendingIntent=*/ null);
6153
6154 // Note this was pinned, so it'll be accepted right away.
6155 runWithCaller(LAUNCHER_1, USER_0, () -> {
6156 assertWith(getShortcutAsLauncher(USER_0))
6157 .selectByIds("ms3")
6158 .areAllEnabled()
6159 .areAllNotDynamic()
6160 .areAllPinned()
6161 .areAllMutable()
6162 .forAllShortcuts(si -> {
6163 assertEquals("new-title-3", si.getShortLabel());
6164 // The new one replaces the old manifest shortcut, so the long label
6165 // should be gone now.
6166 assertNull(si.getLongLabel());
6167 });
6168 });
6169
6170 // Now, change the launcher to launcher2, and request pin again.
6171 setDefaultLauncher(USER_0, mMainActivityFetcher.apply(LAUNCHER_2, USER_0));
6172
6173 reset(mServiceContext);
6174
6175 assertTrue(mManager.isRequestPinShortcutSupported());
6176 mManager.requestPinShortcut(
6177 makeShortcutWithShortLabel("ms4", "new-title-4"),
6178 /*PendingIntent=*/ null);
6179
6180 // Initially there should be no pinned shortcuts for L2.
6181 runWithCaller(LAUNCHER_2, USER_0, () -> {
6182 assertWith(getShortcutAsLauncher(USER_0))
6183 .selectPinned()
6184 .isEmpty();
6185
6186 final ArgumentCaptor<Intent> intent = ArgumentCaptor.forClass(Intent.class);
6187
6188 verify(mServiceContext).startActivityAsUser(intent.capture(), eq(HANDLE_USER_0));
6189
6190 assertEquals(LauncherApps.ACTION_CONFIRM_PIN_SHORTCUT,
6191 intent.getValue().getAction());
6192 assertEquals(LAUNCHER_2, intent.getValue().getComponent().getPackageName());
6193
6194 // Check the request object.
6195 final PinItemRequest request = mLauncherApps.getPinItemRequest(intent.getValue());
6196
6197 assertNotNull(request);
6198 assertEquals(PinItemRequest.REQUEST_TYPE_SHORTCUT, request.getRequestType());
6199
6200 assertWith(request.getShortcutInfo())
6201 .haveIds("ms4")
6202 .areAllOrphan()
6203 .forAllShortcuts(si -> {
6204 assertEquals("new-title-4", si.getShortLabel());
6205 // The new one replaces the old manifest shortcut, so the long label
6206 // should be gone now.
6207 assertNull(si.getLongLabel());
6208 });
6209 assertTrue(request.accept());
6210
6211 assertWith(getShortcutAsLauncher(USER_0))
6212 .selectPinned()
6213 .haveIds("ms4")
6214 .areAllEnabled();
6215 });
6216 });
6217 }
6218
6219 /**
6220 * Test for restoring the pre-P backup format.
6221 */
6222 public void testBackupAndRestore_api27format() throws Exception {
6223 final byte[] payload = readTestAsset("shortcut/shortcut_api27_backup.xml").getBytes();
6224
6225 addPackage(CALLING_PACKAGE_1, CALLING_UID_1, 10, "22222");
6226 addPackage(LAUNCHER_1, LAUNCHER_UID_1, 10, "11111");
6227
Michal Karpinski528c3e52018-02-07 17:47:10 +00006228 doReturn(true).when(mMockPackageManagerInternal).isDataRestoreSafe(
6229 any(byte[].class), anyString());
6230
Makoto Onukia4f89b12017-10-05 10:37:55 -07006231 runWithSystemUid(() -> mService.applyRestore(payload, USER_0));
6232
6233 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6234 assertWith(getCallerShortcuts())
6235 .areAllPinned()
6236 .haveIds("s1")
6237 .areAllEnabled();
6238 });
6239
6240 runWithCaller(LAUNCHER_1, USER_0, () -> {
6241 assertWith(getShortcutAsLauncher(USER_0))
6242 .areAllPinned()
6243 .haveIds("s1")
6244 .areAllEnabled();
6245 });
6246 // Make sure getBackupSourceVersionCode and isBackupSourceBackupAllowed
6247 // are correct. We didn't have them in the old format.
6248 assertEquals(8, mService.getPackageShortcutForTest(CALLING_PACKAGE_1, USER_0)
6249 .getPackageInfo().getBackupSourceVersionCode());
6250 assertTrue(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, USER_0)
6251 .getPackageInfo().isBackupSourceBackupAllowed());
6252
6253 assertEquals(9, mService.getLauncherShortcutForTest(LAUNCHER_1, USER_0)
6254 .getPackageInfo().getBackupSourceVersionCode());
6255 assertTrue(mService.getLauncherShortcutForTest(LAUNCHER_1, USER_0)
6256 .getPackageInfo().isBackupSourceBackupAllowed());
6257
6258 }
6259
Makoto Onuki2e210c42016-03-30 08:30:36 -07006260 public void testSaveAndLoad_crossProfile() {
6261 prepareCrossProfileDataSet();
6262
6263 dumpsysOnLogcat("Before save & load");
6264
6265 mService.saveDirtyInfo();
6266 initService();
6267
6268 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6269 assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()),
Makoto Onukiac214972016-04-04 10:19:45 -07006270 "s1", "s2", "s3");
Makoto Onuki2e210c42016-03-30 08:30:36 -07006271 assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()),
6272 "s1", "s2", "s3", "s4");
6273 });
6274 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
6275 assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()),
Makoto Onukiac214972016-04-04 10:19:45 -07006276 "s1", "s2", "s3");
Makoto Onuki2e210c42016-03-30 08:30:36 -07006277 assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()),
6278 "s1", "s2", "s3", "s4", "s5");
6279 });
6280 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
6281 assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()),
Makoto Onukiac214972016-04-04 10:19:45 -07006282 "s1", "s2", "s3");
Makoto Onuki2e210c42016-03-30 08:30:36 -07006283 assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()),
6284 "s1", "s2", "s3", "s4", "s5", "s6");
6285 });
6286 runWithCaller(CALLING_PACKAGE_4, USER_0, () -> {
6287 assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts())
6288 /* empty */);
6289 assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts())
6290 /* empty */);
6291 });
6292 runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
6293 assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()),
Makoto Onukiac214972016-04-04 10:19:45 -07006294 "s1", "s2", "s3");
Makoto Onuki2e210c42016-03-30 08:30:36 -07006295 assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()),
6296 "s1", "s2", "s3", "s4", "s5", "s6");
6297 });
6298 runWithCaller(CALLING_PACKAGE_2, USER_P0, () -> {
6299 assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts())
6300 /* empty */);
6301 assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts())
6302 /* empty */);
6303 });
6304 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
6305 assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()),
Makoto Onukiac214972016-04-04 10:19:45 -07006306 "x1", "x2", "x3");
Makoto Onuki2e210c42016-03-30 08:30:36 -07006307 assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()),
6308 "x4", "x5");
6309 });
6310 runWithCaller(LAUNCHER_1, USER_0, () -> {
6311 assertShortcutIds(
6312 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_0),
6313 "s1");
6314 assertShortcutIds(
6315 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_0),
6316 "s1", "s2");
6317 assertShortcutIds(
6318 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_3), HANDLE_USER_0),
6319 "s1", "s2", "s3");
6320 assertShortcutIds(
6321 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_4), HANDLE_USER_0)
6322 /* empty */);
6323 assertShortcutIds(
6324 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_P0),
6325 "s1", "s4");
6326 assertShortcutIds(
6327 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_P0)
6328 /* empty */);
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07006329 assertExpectException(
Makoto Onuki2e210c42016-03-30 08:30:36 -07006330 SecurityException.class, "", () -> {
6331 mLauncherApps.getShortcuts(
6332 buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_10);
6333 });
6334 });
6335 runWithCaller(LAUNCHER_2, USER_0, () -> {
6336 assertShortcutIds(
6337 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_0),
6338 "s2");
6339 assertShortcutIds(
6340 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_0),
6341 "s2", "s3");
6342 assertShortcutIds(
6343 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_3), HANDLE_USER_0),
6344 "s2", "s3", "s4");
6345 assertShortcutIds(
6346 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_4), HANDLE_USER_0)
6347 /* empty */);
6348 assertShortcutIds(
6349 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_P0),
6350 "s2", "s5");
6351 assertShortcutIds(
6352 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_P0)
6353 /* empty */);
6354 });
6355 runWithCaller(LAUNCHER_3, USER_0, () -> {
6356 assertShortcutIds(
6357 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_0),
6358 "s3");
6359 assertShortcutIds(
6360 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_0),
6361 "s3", "s4");
6362 assertShortcutIds(
6363 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_3), HANDLE_USER_0),
6364 "s3", "s4", "s5");
6365 assertShortcutIds(
6366 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_4), HANDLE_USER_0)
6367 /* empty */);
6368 assertShortcutIds(
6369 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_P0),
6370 "s3", "s6");
6371 assertShortcutIds(
6372 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_P0)
6373 /* empty */);
6374 });
6375 runWithCaller(LAUNCHER_4, USER_0, () -> {
6376 assertShortcutIds(
6377 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_0)
6378 /* empty */);
6379 assertShortcutIds(
6380 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_0)
6381 /* empty */);
6382 assertShortcutIds(
6383 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_3), HANDLE_USER_0)
6384 /* empty */);
6385 assertShortcutIds(
6386 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_4), HANDLE_USER_0)
6387 /* empty */);
6388 assertShortcutIds(
6389 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_P0)
6390 /* empty */);
6391 assertShortcutIds(
6392 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_P0)
6393 /* empty */);
6394 });
6395 runWithCaller(LAUNCHER_1, USER_P0, () -> {
6396 assertShortcutIds(
6397 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_0),
6398 "s3", "s4");
6399 assertShortcutIds(
6400 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_0),
6401 "s3", "s4", "s5");
6402 assertShortcutIds(
6403 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_3), HANDLE_USER_0),
6404 "s3", "s4", "s5", "s6");
6405 assertShortcutIds(
6406 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_P0),
6407 "s1", "s4");
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07006408 assertExpectException(
Makoto Onukiac214972016-04-04 10:19:45 -07006409 SecurityException.class, "unrelated profile", () -> {
Makoto Onuki2e210c42016-03-30 08:30:36 -07006410 mLauncherApps.getShortcuts(
6411 buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_10);
6412 });
6413 });
6414 runWithCaller(LAUNCHER_1, USER_10, () -> {
6415 assertShortcutIds(
6416 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_10),
6417 "x4", "x5");
6418 assertShortcutIds(
6419 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_10)
6420 /* empty */);
6421 assertShortcutIds(
6422 mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_3), HANDLE_USER_10)
6423 /* empty */);
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07006424 assertExpectException(
Makoto Onukiac214972016-04-04 10:19:45 -07006425 SecurityException.class, "unrelated profile", () -> {
Makoto Onuki2e210c42016-03-30 08:30:36 -07006426 mLauncherApps.getShortcuts(
6427 buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0);
6428 });
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07006429 assertExpectException(
Makoto Onukiac214972016-04-04 10:19:45 -07006430 SecurityException.class, "unrelated profile", () -> {
Makoto Onuki2e210c42016-03-30 08:30:36 -07006431 mLauncherApps.getShortcuts(
6432 buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_P0);
6433 });
6434 });
Makoto Onukic8c33292016-09-12 16:36:59 -07006435 // Check the user-IDs.
6436 assertEquals(USER_0,
6437 mService.getUserShortcutsLocked(USER_0).getPackageShortcuts(CALLING_PACKAGE_1)
6438 .getOwnerUserId());
6439 assertEquals(USER_0,
6440 mService.getUserShortcutsLocked(USER_0).getPackageShortcuts(CALLING_PACKAGE_1)
6441 .getPackageUserId());
6442 assertEquals(USER_P0,
6443 mService.getUserShortcutsLocked(USER_P0).getPackageShortcuts(CALLING_PACKAGE_1)
6444 .getOwnerUserId());
6445 assertEquals(USER_P0,
6446 mService.getUserShortcutsLocked(USER_P0).getPackageShortcuts(CALLING_PACKAGE_1)
6447 .getPackageUserId());
6448
6449 assertEquals(USER_0,
6450 mService.getUserShortcutsLocked(USER_0).getLauncherShortcuts(LAUNCHER_1, USER_0)
6451 .getOwnerUserId());
6452 assertEquals(USER_0,
6453 mService.getUserShortcutsLocked(USER_0).getLauncherShortcuts(LAUNCHER_1, USER_0)
6454 .getPackageUserId());
6455 assertEquals(USER_P0,
6456 mService.getUserShortcutsLocked(USER_P0).getLauncherShortcuts(LAUNCHER_1, USER_0)
6457 .getOwnerUserId());
6458 assertEquals(USER_0,
6459 mService.getUserShortcutsLocked(USER_P0).getLauncherShortcuts(LAUNCHER_1, USER_0)
6460 .getPackageUserId());
Makoto Onuki2e210c42016-03-30 08:30:36 -07006461 }
6462
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07006463 public void testOnApplicationActive_permission() {
6464 assertExpectException(SecurityException.class, "Missing permission", () ->
Makoto Onukid6e1f3b2016-06-14 11:17:59 -07006465 mManager.onApplicationActive(CALLING_PACKAGE_1, USER_0));
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07006466
6467 // Has permission, now it should pass.
6468 mCallerPermissions.add(permission.RESET_SHORTCUT_MANAGER_THROTTLING);
Makoto Onukid6e1f3b2016-06-14 11:17:59 -07006469 mManager.onApplicationActive(CALLING_PACKAGE_1, USER_0);
Makoto Onuki4d36b3a2016-04-27 12:00:17 -07006470 }
6471
Mehdi Alizadehc86dd1f2019-01-25 16:59:54 -08006472 public void testGetShareTargets_permission() {
6473 IntentFilter filter = new IntentFilter();
6474
6475 assertExpectException(SecurityException.class, "Missing permission", () ->
6476 mManager.getShareTargets(filter));
6477
6478 // Has permission, now it should pass.
6479 mCallerPermissions.add(permission.MANAGE_APP_PREDICTIONS);
6480 mManager.getShareTargets(filter);
6481 }
6482
Mehdi Alizadeh627d4db2019-02-04 13:52:03 -08006483 public void testHasShareTargets_permission() {
6484 assertExpectException(SecurityException.class, "Missing permission", () ->
6485 mManager.hasShareTargets(CALLING_PACKAGE_1));
6486
6487 // Has permission, now it should pass.
6488 mCallerPermissions.add(permission.MANAGE_APP_PREDICTIONS);
6489 mManager.hasShareTargets(CALLING_PACKAGE_1);
6490 }
6491
Makoto Onuki2e210c42016-03-30 08:30:36 -07006492 public void testDumpsys_crossProfile() {
6493 prepareCrossProfileDataSet();
6494 dumpsysOnLogcat("test1", /* force= */ true);
6495 }
6496
Makoto Onuki5ba0d3e2016-04-11 14:03:46 -07006497 public void testDumpsys_withIcons() throws IOException {
Makoto Onuki2e210c42016-03-30 08:30:36 -07006498 testIcons();
6499 // Dump after having some icons.
6500 dumpsysOnLogcat("test1", /* force= */ true);
Makoto Onuki0acbb142016-03-22 17:02:57 -07006501 }
Makoto Onuki22fcc682016-05-17 14:52:19 -07006502
6503 public void testManifestShortcut_publishOnUnlockUser() {
6504 addManifestShortcutResource(
6505 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6506 R.xml.shortcut_1);
6507 addManifestShortcutResource(
6508 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
6509 R.xml.shortcut_2);
6510 addManifestShortcutResource(
6511 new ComponentName(CALLING_PACKAGE_3, ShortcutActivity.class.getName()),
6512 R.xml.shortcut_5);
6513
6514 // Unlock user-0.
Makoto Onuki248a0ef2016-11-03 15:59:01 -07006515 mInjectedCurrentTimeMillis += 100;
Makoto Onuki22fcc682016-05-17 14:52:19 -07006516 mService.handleUnlockUser(USER_0);
6517
6518 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6519 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6520 mManager.getManifestShortcuts()))),
6521 "ms1");
6522 assertEmpty(mManager.getPinnedShortcuts());
6523 });
6524
6525 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
6526 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6527 mManager.getManifestShortcuts()))),
6528 "ms1", "ms2");
6529 assertEmpty(mManager.getPinnedShortcuts());
6530 });
6531
6532 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
6533 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6534 mManager.getManifestShortcuts()))),
6535 "ms1", "ms2", "ms3", "ms4", "ms5");
6536 assertEmpty(mManager.getPinnedShortcuts());
6537 });
6538
6539 // Try on another user, with some packages uninstalled.
Makoto Onuki9c850012016-07-26 15:50:50 -07006540 mRunningUsers.put(USER_10, true);
6541
Makoto Onuki22fcc682016-05-17 14:52:19 -07006542 uninstallPackage(USER_10, CALLING_PACKAGE_1);
6543 uninstallPackage(USER_10, CALLING_PACKAGE_3);
6544
Makoto Onuki248a0ef2016-11-03 15:59:01 -07006545 mInjectedCurrentTimeMillis += 100;
Makoto Onuki22fcc682016-05-17 14:52:19 -07006546 mService.handleUnlockUser(USER_10);
6547
6548 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
6549 assertEmpty(mManager.getManifestShortcuts());
6550 assertEmpty(mManager.getPinnedShortcuts());
6551 });
6552
6553 runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
6554 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6555 mManager.getManifestShortcuts()))),
6556 "ms1", "ms2");
6557 assertEmpty(mManager.getPinnedShortcuts());
6558 });
6559
6560 runWithCaller(CALLING_PACKAGE_3, USER_10, () -> {
6561 assertEmpty(mManager.getManifestShortcuts());
6562 assertEmpty(mManager.getPinnedShortcuts());
6563 });
6564
6565 // Now change the resources for package 1, and unlock again.
6566 // But we still see *old* shortcuts, because the package version and install time
6567 // hasn't changed.
6568 shutdownServices();
6569
Makoto Onuki248a0ef2016-11-03 15:59:01 -07006570 mInjectedCurrentTimeMillis += 100;
6571
Makoto Onuki22fcc682016-05-17 14:52:19 -07006572 addManifestShortcutResource(
6573 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6574 R.xml.shortcut_5);
6575 addManifestShortcutResource(
6576 new ComponentName(CALLING_PACKAGE_3, ShortcutActivity.class.getName()),
6577 R.xml.shortcut_1);
6578
6579 initService();
6580 mService.handleUnlockUser(USER_0);
6581
6582 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
Makoto Onuki248a0ef2016-11-03 15:59:01 -07006583 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled( // FAIL
Makoto Onuki22fcc682016-05-17 14:52:19 -07006584 mManager.getManifestShortcuts()))),
6585 "ms1");
6586 assertEmpty(mManager.getPinnedShortcuts());
6587 });
6588
6589 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
6590 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6591 mManager.getManifestShortcuts()))),
6592 "ms1", "ms2");
6593 assertEmpty(mManager.getPinnedShortcuts());
6594 });
6595
6596 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
6597 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6598 mManager.getManifestShortcuts()))),
6599 "ms1", "ms2", "ms3", "ms4", "ms5");
6600 assertEmpty(mManager.getPinnedShortcuts());
6601 });
6602
6603 // Do it again, but this time we change the app version, so we do detect the changes.
6604 shutdownServices();
6605
Makoto Onuki248a0ef2016-11-03 15:59:01 -07006606 mInjectedCurrentTimeMillis += 100;
6607
Makoto Onuki22fcc682016-05-17 14:52:19 -07006608 updatePackageVersion(CALLING_PACKAGE_1, 1);
6609 updatePackageLastUpdateTime(CALLING_PACKAGE_3, 1);
6610
6611 initService();
6612 mService.handleUnlockUser(USER_0);
6613
6614 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6615 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6616 mManager.getManifestShortcuts()))),
6617 "ms1", "ms2", "ms3", "ms4", "ms5");
6618 assertEmpty(mManager.getPinnedShortcuts());
6619 });
6620
6621 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
6622 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6623 mManager.getManifestShortcuts()))),
6624 "ms1", "ms2");
6625 assertEmpty(mManager.getPinnedShortcuts());
6626 });
6627
6628 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
6629 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6630 mManager.getManifestShortcuts()))),
6631 "ms1");
6632 assertEmpty(mManager.getPinnedShortcuts());
6633 });
6634
6635 // Next, try removing all shortcuts, with some of them pinned.
6636 runWithCaller(LAUNCHER_1, USER_0, () -> {
6637 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms3"), HANDLE_USER_0);
6638 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("ms2"), HANDLE_USER_0);
6639 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("ms1"), HANDLE_USER_0);
6640 });
6641 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6642 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6643 mManager.getManifestShortcuts()))),
6644 "ms1", "ms2", "ms3", "ms4", "ms5");
6645 assertShortcutIds(assertAllImmutable(assertAllPinned(assertAllManifest(
6646 assertAllEnabled(mManager.getPinnedShortcuts())))),
6647 "ms3");
6648 });
6649
6650 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
6651 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6652 mManager.getManifestShortcuts()))),
6653 "ms1", "ms2");
6654 assertShortcutIds(assertAllImmutable(assertAllPinned(assertAllManifest(
6655 assertAllEnabled(mManager.getPinnedShortcuts())))),
6656 "ms2");
6657 });
6658
6659 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
6660 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6661 mManager.getManifestShortcuts()))),
6662 "ms1");
6663 assertShortcutIds(assertAllImmutable(assertAllPinned(assertAllManifest(
6664 assertAllEnabled(mManager.getPinnedShortcuts())))),
6665 "ms1");
6666 });
6667
6668 shutdownServices();
6669
Makoto Onuki248a0ef2016-11-03 15:59:01 -07006670 mInjectedCurrentTimeMillis += 100;
6671
Makoto Onuki22fcc682016-05-17 14:52:19 -07006672 addManifestShortcutResource(
6673 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6674 R.xml.shortcut_0);
6675 addManifestShortcutResource(
6676 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
6677 R.xml.shortcut_1);
6678 addManifestShortcutResource(
6679 new ComponentName(CALLING_PACKAGE_3, ShortcutActivity.class.getName()),
6680 R.xml.shortcut_0);
6681
6682 updatePackageVersion(CALLING_PACKAGE_1, 1);
6683 updatePackageVersion(CALLING_PACKAGE_2, 1);
6684 updatePackageVersion(CALLING_PACKAGE_3, 1);
6685
6686 initService();
6687 mService.handleUnlockUser(USER_0);
6688
6689 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6690 assertEmpty(mManager.getManifestShortcuts());
6691 assertShortcutIds(assertAllImmutable(assertAllPinned(assertAllNotManifest(
6692 assertAllDisabled(mManager.getPinnedShortcuts())))),
6693 "ms3");
6694 });
6695
6696 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
6697 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6698 mManager.getManifestShortcuts()))),
6699 "ms1");
6700 assertShortcutIds(assertAllImmutable(assertAllPinned(assertAllNotManifest(
6701 assertAllDisabled(mManager.getPinnedShortcuts())))),
6702 "ms2");
6703 });
6704
6705 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
6706 assertEmpty(mManager.getManifestShortcuts());
6707 assertShortcutIds(assertAllImmutable(assertAllPinned(assertAllNotManifest(
6708 assertAllDisabled(mManager.getPinnedShortcuts())))),
6709 "ms1");
6710 });
6711
6712 // Make sure we don't have ShortcutPackage for packages that don't have shortcuts.
6713 assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_4, USER_0));
6714 assertNull(mService.getPackageShortcutForTest(LAUNCHER_1, USER_0));
6715 }
6716
Makoto Onuki22fcc682016-05-17 14:52:19 -07006717 public void testManifestShortcut_publishOnBroadcast() {
6718 // First, no packages are installed.
6719 uninstallPackage(USER_0, CALLING_PACKAGE_1);
6720 uninstallPackage(USER_0, CALLING_PACKAGE_2);
6721 uninstallPackage(USER_0, CALLING_PACKAGE_3);
6722 uninstallPackage(USER_0, CALLING_PACKAGE_4);
6723 uninstallPackage(USER_10, CALLING_PACKAGE_1);
6724 uninstallPackage(USER_10, CALLING_PACKAGE_2);
6725 uninstallPackage(USER_10, CALLING_PACKAGE_3);
6726 uninstallPackage(USER_10, CALLING_PACKAGE_4);
6727
6728 mService.handleUnlockUser(USER_0);
Makoto Onuki9c850012016-07-26 15:50:50 -07006729
6730 mRunningUsers.put(USER_10, true);
Makoto Onuki22fcc682016-05-17 14:52:19 -07006731 mService.handleUnlockUser(USER_10);
6732
6733 // Originally no manifest shortcuts.
6734 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6735 assertEmpty(mManager.getManifestShortcuts());
6736 assertEmpty(mManager.getPinnedShortcuts());
6737 });
6738
6739 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
6740 assertEmpty(mManager.getManifestShortcuts());
6741 assertEmpty(mManager.getPinnedShortcuts());
6742 });
6743
6744 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
6745 assertEmpty(mManager.getManifestShortcuts());
6746 assertEmpty(mManager.getPinnedShortcuts());
6747 });
6748
6749 // Package 1 updated, with manifest shortcuts.
6750 addManifestShortcutResource(
6751 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6752 R.xml.shortcut_1);
6753 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07006754 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki22fcc682016-05-17 14:52:19 -07006755 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6756
6757 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6758 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6759 mManager.getManifestShortcuts()))),
6760 "ms1");
6761 assertEmpty(mManager.getPinnedShortcuts());
6762 });
6763
6764 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
6765 assertEmpty(mManager.getManifestShortcuts());
6766 assertEmpty(mManager.getPinnedShortcuts());
6767 });
6768
6769 // Package 2 updated, with manifest shortcuts.
6770
6771 addManifestShortcutResource(
6772 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
Makoto Onukib1588c02017-10-12 15:11:45 -07006773 R.xml.shortcut_5_altalt);
Makoto Onuki22fcc682016-05-17 14:52:19 -07006774 updatePackageVersion(CALLING_PACKAGE_2, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07006775 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki22fcc682016-05-17 14:52:19 -07006776 genPackageAddIntent(CALLING_PACKAGE_2, USER_0));
6777
6778 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6779 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6780 mManager.getManifestShortcuts()))),
6781 "ms1");
6782 assertEmpty(mManager.getPinnedShortcuts());
6783 });
6784
6785 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
6786 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6787 mManager.getManifestShortcuts()))),
6788 "ms1", "ms2", "ms3", "ms4", "ms5");
Makoto Onuki9e1f5592016-06-08 12:30:23 -07006789 assertWith(getCallerShortcuts()).selectManifest()
6790 .selectByActivity(
6791 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()))
6792 .haveRanksInOrder("ms1", "ms2", "ms3", "ms4", "ms5");
Makoto Onuki22fcc682016-05-17 14:52:19 -07006793 assertEmpty(mManager.getPinnedShortcuts());
6794 });
6795
6796 // Package 2 updated, with less manifest shortcuts.
6797 // This time we use updatePackageLastUpdateTime() instead of updatePackageVersion().
6798
6799 dumpsysOnLogcat("Before pinning");
6800
6801 // Also pin some.
6802 runWithCaller(LAUNCHER_1, USER_0, () -> {
6803 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("ms2", "ms3"), HANDLE_USER_0);
6804 });
6805
6806 dumpsysOnLogcat("After pinning");
6807
6808 addManifestShortcutResource(
6809 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
6810 R.xml.shortcut_2);
6811 updatePackageLastUpdateTime(CALLING_PACKAGE_2, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07006812 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki22fcc682016-05-17 14:52:19 -07006813 genPackageAddIntent(CALLING_PACKAGE_2, USER_0));
6814
Makoto Onukib1588c02017-10-12 15:11:45 -07006815 dumpsysOnLogcat("After updating package 2");
6816
Makoto Onuki22fcc682016-05-17 14:52:19 -07006817 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6818 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6819 mManager.getManifestShortcuts()))),
6820 "ms1");
6821 assertEmpty(mManager.getPinnedShortcuts());
6822 });
6823
6824 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
6825 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6826 mManager.getManifestShortcuts()))),
6827 "ms1", "ms2");
Makoto Onuki9e1f5592016-06-08 12:30:23 -07006828 assertWith(getCallerShortcuts()).selectManifest()
6829 .selectByActivity(
6830 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()))
6831 .haveRanksInOrder("ms1", "ms2");
Makoto Onuki22fcc682016-05-17 14:52:19 -07006832 assertShortcutIds(assertAllImmutable(assertAllPinned(
6833 mManager.getPinnedShortcuts())),
6834 "ms2", "ms3");
6835 // ms3 is no longer in manifest, so should be disabled.
6836 // but ms1 and ms2 should be enabled.
Makoto Onukib1588c02017-10-12 15:11:45 -07006837 assertWith(getCallerShortcuts())
6838 .selectByIds("ms1", "ms2")
6839 .areAllEnabled()
6840
6841 .revertToOriginalList()
6842 .selectByIds("ms3")
6843 .areAllDisabled()
6844 .areAllWithDisabledReason(ShortcutInfo.DISABLED_REASON_APP_CHANGED);
Makoto Onuki22fcc682016-05-17 14:52:19 -07006845 });
6846
Makoto Onukib1588c02017-10-12 15:11:45 -07006847 // Make sure the launcher see the correct disabled reason.
6848 runWithCaller(LAUNCHER_1, USER_0, () -> {
6849 assertWith(getShortcutAsLauncher(USER_0))
6850 .forShortcutWithId("ms3", si -> {
6851 assertEquals("string-com.android.test.2-user:0-res:"
6852 + R.string.shortcut_disabled_message3 + "/en",
6853 si.getDisabledMessage());
6854 });
6855 });
6856
6857
Makoto Onuki22fcc682016-05-17 14:52:19 -07006858 // Package 2 on user 10 has no shortcuts yet.
6859 runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
6860 assertEmpty(mManager.getManifestShortcuts());
6861 assertEmpty(mManager.getPinnedShortcuts());
6862 });
Makoto Onukia2241832016-07-06 13:28:37 -07006863 // Send add broadcast, but the user is not running, so should be ignored.
Makoto Onuki01ce92b2017-04-28 12:24:16 -07006864 mService.handleStopUser(USER_10);
Makoto Onukia2241832016-07-06 13:28:37 -07006865 mRunningUsers.put(USER_10, false);
6866 mUnlockedUsers.put(USER_10, false);
6867
Makoto Onuki4e6cef42016-07-13 16:14:01 -07006868 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onukia2241832016-07-06 13:28:37 -07006869 genPackageAddIntent(CALLING_PACKAGE_2, USER_10));
6870 runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
Makoto Onuki4e6cef42016-07-13 16:14:01 -07006871 // Don't use the mManager APIs to get shortcuts, because they'll trigger the package
6872 // update check.
6873 // So look the internal data directly using getCallerShortcuts().
6874 assertEmpty(getCallerShortcuts());
Makoto Onukia2241832016-07-06 13:28:37 -07006875 });
6876
6877 // Try again, but the user is locked, so still ignored.
6878 mRunningUsers.put(USER_10, true);
Makoto Onukif34c3082016-07-13 10:25:25 -07006879 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onukia2241832016-07-06 13:28:37 -07006880 genPackageAddIntent(CALLING_PACKAGE_2, USER_10));
6881 runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
Makoto Onuki4e6cef42016-07-13 16:14:01 -07006882 // Don't use the mManager APIs to get shortcuts, because they'll trigger the package
6883 // update check.
6884 // So look the internal data directly using getCallerShortcuts().
6885 assertEmpty(getCallerShortcuts());
Makoto Onukia2241832016-07-06 13:28:37 -07006886 });
6887
6888 // Unlock the user, now it should work.
6889 mUnlockedUsers.put(USER_10, true);
6890
Makoto Onuki22fcc682016-05-17 14:52:19 -07006891 // Send PACKAGE_ADD broadcast to have Package 2 on user-10 publish manifest shortcuts.
Makoto Onukif34c3082016-07-13 10:25:25 -07006892 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki22fcc682016-05-17 14:52:19 -07006893 genPackageAddIntent(CALLING_PACKAGE_2, USER_10));
6894
6895 runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
6896 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6897 mManager.getManifestShortcuts()))),
6898 "ms1", "ms2");
Makoto Onuki9e1f5592016-06-08 12:30:23 -07006899 assertWith(getCallerShortcuts()).selectManifest()
6900 .selectByActivity(
6901 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()))
6902 .haveRanksInOrder("ms1", "ms2");
Makoto Onuki22fcc682016-05-17 14:52:19 -07006903 assertEmpty(mManager.getPinnedShortcuts());
6904 });
6905
6906 // But it shouldn't affect user-0.
6907 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
6908 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6909 mManager.getManifestShortcuts()))),
6910 "ms1", "ms2");
Makoto Onuki9e1f5592016-06-08 12:30:23 -07006911 assertWith(getCallerShortcuts()).selectManifest()
6912 .selectByActivity(
6913 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()))
6914 .haveRanksInOrder("ms1", "ms2");
Makoto Onuki22fcc682016-05-17 14:52:19 -07006915 assertShortcutIds(assertAllImmutable(assertAllPinned(
6916 mManager.getPinnedShortcuts())),
6917 "ms2", "ms3");
6918 assertAllEnabled(list(getCallerShortcut("ms1")));
6919 assertAllEnabled(list(getCallerShortcut("ms2")));
6920 assertAllDisabled(list(getCallerShortcut("ms3")));
6921 });
6922
Makoto Onuki9e1f5592016-06-08 12:30:23 -07006923 // Multiple activities.
6924 // Add shortcuts on activity 2 for package 2.
6925 addManifestShortcutResource(
6926 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
6927 R.xml.shortcut_5_alt);
6928 addManifestShortcutResource(
6929 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity2.class.getName()),
6930 R.xml.shortcut_5_reverse);
6931
6932 updatePackageLastUpdateTime(CALLING_PACKAGE_2, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07006933 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki9e1f5592016-06-08 12:30:23 -07006934 genPackageAddIntent(CALLING_PACKAGE_2, USER_0));
6935
6936 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
6937 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
6938 mManager.getManifestShortcuts()))),
6939 "ms1", "ms2", "ms3", "ms4", "ms5",
6940 "ms1_alt", "ms2_alt", "ms3_alt", "ms4_alt", "ms5_alt");
6941
6942 // Make sure they have the correct ranks, regardless of their ID's alphabetical order.
6943 assertWith(getCallerShortcuts()).selectManifest()
6944 .selectByActivity(
6945 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()))
6946 .haveRanksInOrder("ms1_alt", "ms2_alt", "ms3_alt", "ms4_alt", "ms5_alt");
6947 assertWith(getCallerShortcuts()).selectManifest()
6948 .selectByActivity(
6949 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity2.class.getName()))
6950 .haveRanksInOrder("ms5", "ms4", "ms3", "ms2", "ms1");
6951 });
6952
Makoto Onuki22fcc682016-05-17 14:52:19 -07006953 // Package 2 now has no manifest shortcuts.
6954 addManifestShortcutResource(
6955 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
6956 R.xml.shortcut_0);
Makoto Onuki9e1f5592016-06-08 12:30:23 -07006957 addManifestShortcutResource(
6958 new ComponentName(CALLING_PACKAGE_2, ShortcutActivity2.class.getName()),
6959 R.xml.shortcut_0);
Makoto Onuki22fcc682016-05-17 14:52:19 -07006960 updatePackageLastUpdateTime(CALLING_PACKAGE_2, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07006961 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki22fcc682016-05-17 14:52:19 -07006962 genPackageAddIntent(CALLING_PACKAGE_2, USER_0));
6963
6964 // No manifest shortcuts, and pinned ones are disabled.
6965 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
6966 assertEmpty(mManager.getManifestShortcuts());
6967 assertShortcutIds(assertAllImmutable(assertAllPinned(assertAllDisabled(
6968 mManager.getPinnedShortcuts()))),
6969 "ms2", "ms3");
6970 });
6971 }
6972
6973 public void testManifestShortcuts_missingMandatoryFields() {
6974 // Start with no apps installed.
6975 uninstallPackage(USER_0, CALLING_PACKAGE_1);
6976 uninstallPackage(USER_0, CALLING_PACKAGE_2);
6977 uninstallPackage(USER_0, CALLING_PACKAGE_3);
6978 uninstallPackage(USER_0, CALLING_PACKAGE_4);
6979
6980 mService.handleUnlockUser(USER_0);
6981
6982 // Make sure no manifest shortcuts.
6983 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
6984 assertEmpty(mManager.getManifestShortcuts());
6985 });
6986
6987 // Package 1 updated, which has one valid manifest shortcut and one invalid.
6988 addManifestShortcutResource(
6989 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
6990 R.xml.shortcut_error_1);
6991 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07006992 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki22fcc682016-05-17 14:52:19 -07006993 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
6994
6995 // Only the valid one is published.
6996 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
Makoto Onuki6771d732016-07-14 12:58:40 -07006997 assertWith(getCallerShortcuts())
6998 .areAllManifest()
6999 .areAllImmutable()
7000 .areAllEnabled()
7001 .haveIds("x1");
Makoto Onuki22fcc682016-05-17 14:52:19 -07007002 });
7003
7004 // Package 1 updated, which has one valid manifest shortcut and one invalid.
7005 addManifestShortcutResource(
7006 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7007 R.xml.shortcut_error_2);
7008 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07007009 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki22fcc682016-05-17 14:52:19 -07007010 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7011
7012 // Only the valid one is published.
7013 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
Makoto Onuki6771d732016-07-14 12:58:40 -07007014 assertWith(getCallerShortcuts())
7015 .areAllManifest()
7016 .areAllImmutable()
7017 .areAllEnabled()
7018 .haveIds("x2");
Makoto Onuki22fcc682016-05-17 14:52:19 -07007019 });
7020
7021 // Package 1 updated, which has one valid manifest shortcut and one invalid.
7022 addManifestShortcutResource(
7023 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7024 R.xml.shortcut_error_3);
7025 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07007026 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki22fcc682016-05-17 14:52:19 -07007027 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7028
7029 // Only the valid one is published.
7030 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
Makoto Onuki6771d732016-07-14 12:58:40 -07007031 assertWith(getCallerShortcuts())
7032 .areAllManifest()
7033 .areAllImmutable()
7034 .areAllEnabled()
7035 .haveIds("x3")
7036 .forShortcutWithId("x3", si -> {
7037 assertEquals(set("cat2"), si.getCategories());
7038 });
Makoto Onuki22fcc682016-05-17 14:52:19 -07007039 });
7040 }
7041
Makoto Onukidf6da042016-06-16 09:51:40 -07007042 public void testManifestShortcuts_intentDefinitions() {
7043 addManifestShortcutResource(
7044 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7045 R.xml.shortcut_error_4);
7046 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07007047 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onukidf6da042016-06-16 09:51:40 -07007048 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7049
7050 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7051 // Make sure invalid ones are not published.
7052 // Note that at this point disabled ones don't show up because they weren't pinned.
7053 assertWith(getCallerShortcuts())
7054 .haveIds("ms1", "ms2")
7055 .areAllManifest()
7056 .areAllNotDynamic()
7057 .areAllNotPinned()
7058 .areAllImmutable()
7059 .areAllEnabled()
7060 .forShortcutWithId("ms1", si -> {
7061 assertTrue(si.isEnabled());
Makoto Onuki440a1ea2016-07-20 14:21:18 -07007062 assertEquals(1, si.getIntents().length);
7063
Makoto Onukidf6da042016-06-16 09:51:40 -07007064 assertEquals("action1", si.getIntent().getAction());
Makoto Onuki440a1ea2016-07-20 14:21:18 -07007065 assertEquals("value1", si.getIntent().getStringExtra("key1"));
7066 assertEquals(Intent.FLAG_ACTIVITY_NEW_TASK |
7067 Intent.FLAG_ACTIVITY_CLEAR_TASK |
7068 Intent.FLAG_ACTIVITY_TASK_ON_HOME, si.getIntent().getFlags());
7069
7070 assertEquals("action1", si.getIntents()[0].getAction());
7071 assertEquals("value1", si.getIntents()[0].getStringExtra("key1"));
7072 assertEquals(Intent.FLAG_ACTIVITY_NEW_TASK |
7073 Intent.FLAG_ACTIVITY_CLEAR_TASK |
7074 Intent.FLAG_ACTIVITY_TASK_ON_HOME, si.getIntents()[0].getFlags());
Makoto Onukidf6da042016-06-16 09:51:40 -07007075 })
7076 .forShortcutWithId("ms2", si -> {
7077 assertTrue(si.isEnabled());
Makoto Onuki440a1ea2016-07-20 14:21:18 -07007078 assertEquals(2, si.getIntents().length);
7079
7080 // getIntent will return the last one.
7081 assertEquals("action2_2", si.getIntent().getAction());
7082 assertEquals("value2", si.getIntent().getStringExtra("key2"));
7083 assertEquals(0, si.getIntent().getFlags());
7084
7085 final Intent i1 = si.getIntents()[0];
7086 final Intent i2 = si.getIntents()[1];
7087
7088 assertEquals("action2_1", i1.getAction());
7089 assertEquals("value1", i1.getStringExtra("key1"));
7090 assertEquals(Intent.FLAG_ACTIVITY_NEW_TASK |
7091 Intent.FLAG_ACTIVITY_CLEAR_TASK |
7092 Intent.FLAG_ACTIVITY_TASK_ON_HOME, i1.getFlags());
7093
7094 assertEquals("action2_2", i2.getAction());
7095 assertEquals("value2", i2.getStringExtra("key2"));
7096 assertEquals(0, i2.getFlags());
Makoto Onukidf6da042016-06-16 09:51:40 -07007097 });
7098 });
7099
7100 // Publish 5 enabled to pin some, so we can later test disabled manfiest shortcuts..
7101 addManifestShortcutResource(
7102 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7103 R.xml.shortcut_5);
7104 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07007105 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onukidf6da042016-06-16 09:51:40 -07007106 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7107
7108 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7109 // Make sure 5 manifest shortcuts are published.
7110 assertWith(getCallerShortcuts())
7111 .haveIds("ms1", "ms2", "ms3", "ms4", "ms5")
7112 .areAllManifest()
7113 .areAllNotDynamic()
7114 .areAllNotPinned()
7115 .areAllImmutable()
7116 .areAllEnabled();
7117 });
7118
7119 runWithCaller(LAUNCHER_1, USER_0, () -> {
7120 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
7121 list("ms3", "ms4", "ms5"), HANDLE_USER_0);
7122 });
7123
7124 // Make sure they're pinned.
7125 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7126 assertWith(getCallerShortcuts())
7127 .haveIds("ms1", "ms2", "ms3", "ms4", "ms5")
7128 .selectByIds("ms1", "ms2")
7129 .areAllNotPinned()
7130 .areAllEnabled()
7131
7132 .revertToOriginalList()
7133 .selectByIds("ms3", "ms4", "ms5")
7134 .areAllPinned()
7135 .areAllEnabled();
7136 });
7137
7138 // Update the app.
7139 addManifestShortcutResource(
7140 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7141 R.xml.shortcut_error_4);
7142 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07007143 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onukidf6da042016-06-16 09:51:40 -07007144 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7145
7146 // Make sure 3, 4 and 5 still exist but disabled.
7147 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7148 assertWith(getCallerShortcuts())
7149 .haveIds("ms1", "ms2", "ms3", "ms4", "ms5")
7150 .areAllNotDynamic()
7151 .areAllImmutable()
7152
7153 .selectByIds("ms1", "ms2")
7154 .areAllManifest()
7155 .areAllNotPinned()
7156 .areAllEnabled()
7157
7158 .revertToOriginalList()
7159 .selectByIds("ms3", "ms4", "ms5")
7160 .areAllNotManifest()
7161 .areAllPinned()
7162 .areAllDisabled()
7163
7164 .revertToOriginalList()
7165 .forShortcutWithId("ms1", si -> {
7166 assertEquals(si.getId(), "action1", si.getIntent().getAction());
7167 })
7168 .forShortcutWithId("ms2", si -> {
Makoto Onuki440a1ea2016-07-20 14:21:18 -07007169 // getIntent returns the last one.
7170 assertEquals(si.getId(), "action2_2", si.getIntent().getAction());
Makoto Onukidf6da042016-06-16 09:51:40 -07007171 })
7172 .forShortcutWithId("ms3", si -> {
7173 assertEquals(si.getId(), Intent.ACTION_VIEW, si.getIntent().getAction());
7174 })
7175 .forShortcutWithId("ms4", si -> {
7176 assertEquals(si.getId(), Intent.ACTION_VIEW, si.getIntent().getAction());
7177 })
7178 .forShortcutWithId("ms5", si -> {
7179 assertEquals(si.getId(), "action", si.getIntent().getAction());
7180 });
7181 });
7182 }
7183
Makoto Onuki22fcc682016-05-17 14:52:19 -07007184 public void testManifestShortcuts_checkAllFields() {
7185 mService.handleUnlockUser(USER_0);
7186
7187 // Package 1 updated, which has one valid manifest shortcut and one invalid.
7188 addManifestShortcutResource(
7189 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7190 R.xml.shortcut_5);
7191 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07007192 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki22fcc682016-05-17 14:52:19 -07007193 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7194
7195 // Only the valid one is published.
7196 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
Makoto Onukidf6da042016-06-16 09:51:40 -07007197 assertWith(getCallerShortcuts())
7198 .haveIds("ms1", "ms2", "ms3", "ms4", "ms5")
7199 .areAllManifest()
7200 .areAllImmutable()
7201 .areAllEnabled()
7202 .areAllNotPinned()
7203 .areAllNotDynamic()
Makoto Onuki22fcc682016-05-17 14:52:19 -07007204
Makoto Onukidf6da042016-06-16 09:51:40 -07007205 .forShortcutWithId("ms1", si -> {
7206 assertEquals(R.drawable.icon1, si.getIconResourceId());
7207 assertEquals(new ComponentName(CALLING_PACKAGE_1,
7208 ShortcutActivity.class.getName()),
7209 si.getActivity());
Makoto Onuki22fcc682016-05-17 14:52:19 -07007210
Makoto Onukidf6da042016-06-16 09:51:40 -07007211 assertEquals(R.string.shortcut_title1, si.getTitleResId());
7212 assertEquals("r" + R.string.shortcut_title1, si.getTitleResName());
7213 assertEquals(R.string.shortcut_text1, si.getTextResId());
7214 assertEquals("r" + R.string.shortcut_text1, si.getTextResName());
7215 assertEquals(R.string.shortcut_disabled_message1,
7216 si.getDisabledMessageResourceId());
7217 assertEquals("r" + R.string.shortcut_disabled_message1,
7218 si.getDisabledMessageResName());
Makoto Onuki157b1622016-06-02 16:13:10 -07007219
Makoto Onukidf6da042016-06-16 09:51:40 -07007220 assertEquals(set("android.shortcut.conversation", "android.shortcut.media"),
7221 si.getCategories());
7222 assertEquals("action1", si.getIntent().getAction());
7223 assertEquals(Uri.parse("http://a.b.c/1"), si.getIntent().getData());
7224 })
Makoto Onuki157b1622016-06-02 16:13:10 -07007225
Makoto Onukidf6da042016-06-16 09:51:40 -07007226 .forShortcutWithId("ms2", si -> {
7227 assertEquals("ms2", si.getId());
7228 assertEquals(R.drawable.icon2, si.getIconResourceId());
Makoto Onuki22fcc682016-05-17 14:52:19 -07007229
Makoto Onukidf6da042016-06-16 09:51:40 -07007230 assertEquals(R.string.shortcut_title2, si.getTitleResId());
7231 assertEquals("r" + R.string.shortcut_title2, si.getTitleResName());
7232 assertEquals(R.string.shortcut_text2, si.getTextResId());
7233 assertEquals("r" + R.string.shortcut_text2, si.getTextResName());
7234 assertEquals(R.string.shortcut_disabled_message2,
7235 si.getDisabledMessageResourceId());
7236 assertEquals("r" + R.string.shortcut_disabled_message2,
7237 si.getDisabledMessageResName());
Makoto Onuki22fcc682016-05-17 14:52:19 -07007238
Makoto Onukidf6da042016-06-16 09:51:40 -07007239 assertEquals(set("android.shortcut.conversation"), si.getCategories());
7240 assertEquals("action2", si.getIntent().getAction());
7241 assertEquals(null, si.getIntent().getData());
7242 })
Makoto Onuki157b1622016-06-02 16:13:10 -07007243
Makoto Onukidf6da042016-06-16 09:51:40 -07007244 .forShortcutWithId("ms3", si -> {
7245 assertEquals(0, si.getIconResourceId());
7246 assertEquals(R.string.shortcut_title1, si.getTitleResId());
7247 assertEquals("r" + R.string.shortcut_title1, si.getTitleResName());
Makoto Onuki157b1622016-06-02 16:13:10 -07007248
Makoto Onukidf6da042016-06-16 09:51:40 -07007249 assertEquals(0, si.getTextResId());
7250 assertEquals(null, si.getTextResName());
7251 assertEquals(0, si.getDisabledMessageResourceId());
7252 assertEquals(null, si.getDisabledMessageResName());
Makoto Onuki22fcc682016-05-17 14:52:19 -07007253
Makoto Onukidf6da042016-06-16 09:51:40 -07007254 assertEmpty(si.getCategories());
7255 assertEquals("android.intent.action.VIEW", si.getIntent().getAction());
7256 assertEquals(null, si.getIntent().getData());
7257 })
Makoto Onuki22fcc682016-05-17 14:52:19 -07007258
Makoto Onukidf6da042016-06-16 09:51:40 -07007259 .forShortcutWithId("ms4", si -> {
7260 assertEquals(0, si.getIconResourceId());
7261 assertEquals(R.string.shortcut_title2, si.getTitleResId());
7262 assertEquals("r" + R.string.shortcut_title2, si.getTitleResName());
Makoto Onuki157b1622016-06-02 16:13:10 -07007263
Makoto Onukidf6da042016-06-16 09:51:40 -07007264 assertEquals(0, si.getTextResId());
7265 assertEquals(null, si.getTextResName());
7266 assertEquals(0, si.getDisabledMessageResourceId());
7267 assertEquals(null, si.getDisabledMessageResName());
Makoto Onuki157b1622016-06-02 16:13:10 -07007268
Makoto Onukidf6da042016-06-16 09:51:40 -07007269 assertEquals(set("cat"), si.getCategories());
7270 assertEquals("android.intent.action.VIEW2", si.getIntent().getAction());
7271 assertEquals(null, si.getIntent().getData());
7272 })
7273
7274 .forShortcutWithId("ms5", si -> {
7275 si = getCallerShortcut("ms5");
7276 assertEquals("action", si.getIntent().getAction());
7277 assertEquals("http://www/", si.getIntent().getData().toString());
7278 assertEquals("foo/bar", si.getIntent().getType());
7279 assertEquals(
7280 new ComponentName("abc", ".xyz"), si.getIntent().getComponent());
7281
7282 assertEquals(set("cat1", "cat2"), si.getIntent().getCategories());
7283 assertEquals("value1", si.getIntent().getStringExtra("key1"));
7284 assertEquals("value2", si.getIntent().getStringExtra("key2"));
7285 });
Makoto Onuki157b1622016-06-02 16:13:10 -07007286 });
7287 }
7288
Makoto Onuki4e6cef42016-07-13 16:14:01 -07007289 public void testManifestShortcuts_localeChange() throws InterruptedException {
Makoto Onuki157b1622016-06-02 16:13:10 -07007290 mService.handleUnlockUser(USER_0);
7291
7292 // Package 1 updated, which has one valid manifest shortcut and one invalid.
7293 addManifestShortcutResource(
7294 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7295 R.xml.shortcut_2);
7296 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07007297 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki157b1622016-06-02 16:13:10 -07007298 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7299
7300 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7301 mManager.setDynamicShortcuts(list(makeShortcutWithTitle("s1", "title")));
7302
7303 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
7304 mManager.getManifestShortcuts()))),
7305 "ms1", "ms2");
7306
7307 // check first shortcut.
7308 ShortcutInfo si = getCallerShortcut("ms1");
7309
7310 assertEquals("ms1", si.getId());
7311 assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_title1 + "/en",
7312 si.getTitle());
7313 assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_text1 + "/en",
7314 si.getText());
7315 assertEquals("string-com.android.test.1-user:0-res:"
7316 + R.string.shortcut_disabled_message1 + "/en",
7317 si.getDisabledMessage());
7318 assertEquals(START_TIME, si.getLastChangedTimestamp());
7319
7320 // check another
7321 si = getCallerShortcut("ms2");
7322
7323 assertEquals("ms2", si.getId());
7324 assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_title2 + "/en",
7325 si.getTitle());
7326 assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_text2 + "/en",
7327 si.getText());
7328 assertEquals("string-com.android.test.1-user:0-res:"
7329 + R.string.shortcut_disabled_message2 + "/en",
7330 si.getDisabledMessage());
7331 assertEquals(START_TIME, si.getLastChangedTimestamp());
7332
7333 // Check the dynamic one.
7334 si = getCallerShortcut("s1");
7335
7336 assertEquals("s1", si.getId());
7337 assertEquals("title", si.getTitle());
7338 assertEquals(null, si.getText());
7339 assertEquals(null, si.getDisabledMessage());
7340 assertEquals(START_TIME, si.getLastChangedTimestamp());
7341 });
7342
7343 mInjectedCurrentTimeMillis++;
7344
Makoto Onuki4e6cef42016-07-13 16:14:01 -07007345 // Change the locale and send the broadcast, make sure the launcher gets a callback too.
Makoto Onuki157b1622016-06-02 16:13:10 -07007346 mInjectedLocale = Locale.JAPANESE;
Makoto Onuki4e6cef42016-07-13 16:14:01 -07007347
7348 setCaller(LAUNCHER_1, USER_0);
7349
7350 assertForLauncherCallback(mLauncherApps, () -> {
7351 mService.mReceiver.onReceive(mServiceContext, new Intent(Intent.ACTION_LOCALE_CHANGED));
7352 }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
7353 .haveIds("ms1", "ms2", "s1");
Makoto Onuki157b1622016-06-02 16:13:10 -07007354
7355 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7356 // check first shortcut.
7357 ShortcutInfo si = getCallerShortcut("ms1");
7358
7359 assertEquals("ms1", si.getId());
7360 assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_title1 + "/ja",
7361 si.getTitle());
7362 assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_text1 + "/ja",
7363 si.getText());
7364 assertEquals("string-com.android.test.1-user:0-res:"
7365 + R.string.shortcut_disabled_message1 + "/ja",
7366 si.getDisabledMessage());
7367 assertEquals(START_TIME + 1, si.getLastChangedTimestamp());
7368
7369 // check another
7370 si = getCallerShortcut("ms2");
7371
7372 assertEquals("ms2", si.getId());
7373 assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_title2 + "/ja",
7374 si.getTitle());
7375 assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_text2 + "/ja",
7376 si.getText());
7377 assertEquals("string-com.android.test.1-user:0-res:"
7378 + R.string.shortcut_disabled_message2 + "/ja",
7379 si.getDisabledMessage());
7380 assertEquals(START_TIME + 1, si.getLastChangedTimestamp());
7381
7382 // Check the dynamic one. (locale change shouldn't affect.)
7383 si = getCallerShortcut("s1");
7384
7385 assertEquals("s1", si.getId());
7386 assertEquals("title", si.getTitle());
7387 assertEquals(null, si.getText());
7388 assertEquals(null, si.getDisabledMessage());
7389 assertEquals(START_TIME, si.getLastChangedTimestamp()); // Not changed.
Makoto Onuki22fcc682016-05-17 14:52:19 -07007390 });
7391 }
7392
7393 public void testManifestShortcuts_updateAndDisabled_notPinned() {
7394 mService.handleUnlockUser(USER_0);
7395
7396 // First, just publish a manifest shortcut.
7397 addManifestShortcutResource(
7398 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7399 R.xml.shortcut_1);
7400 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07007401 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki22fcc682016-05-17 14:52:19 -07007402 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7403
7404 // Only the valid one is published.
7405 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7406 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
7407 mManager.getManifestShortcuts()))),
7408 "ms1");
7409 assertEmpty(mManager.getPinnedShortcuts());
7410
7411 // Make sure there's no other dangling shortcuts.
7412 assertShortcutIds(getCallerShortcuts(), "ms1");
7413 });
7414
7415 // Now version up, the manifest shortcut is disabled now.
7416 addManifestShortcutResource(
7417 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7418 R.xml.shortcut_1_disable);
7419 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07007420 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki22fcc682016-05-17 14:52:19 -07007421 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7422
7423 // Because shortcut 1 wasn't pinned, it'll just go away.
7424 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7425 assertEmpty(mManager.getManifestShortcuts());
7426 assertEmpty(mManager.getPinnedShortcuts());
7427
7428 // Make sure there's no other dangling shortcuts.
7429 assertEmpty(getCallerShortcuts());
7430 });
7431 }
7432
7433 public void testManifestShortcuts_updateAndDisabled_pinned() {
7434 mService.handleUnlockUser(USER_0);
7435
7436 // First, just publish a manifest shortcut.
7437 addManifestShortcutResource(
7438 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7439 R.xml.shortcut_1);
7440 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07007441 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki22fcc682016-05-17 14:52:19 -07007442 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7443
7444 // Only the valid one is published.
7445 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7446 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
7447 mManager.getManifestShortcuts()))),
7448 "ms1");
7449 assertEmpty(mManager.getPinnedShortcuts());
7450
7451 // Make sure there's no other dangling shortcuts.
7452 assertShortcutIds(getCallerShortcuts(), "ms1");
7453 });
7454
7455 runWithCaller(LAUNCHER_1, USER_0, () -> {
7456 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms1"), HANDLE_USER_0);
7457 });
7458
7459 // Now upgrade, the manifest shortcut is disabled now.
7460 addManifestShortcutResource(
7461 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7462 R.xml.shortcut_1_disable);
7463 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07007464 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki22fcc682016-05-17 14:52:19 -07007465 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7466
7467 // Because shortcut 1 was pinned, it'll still exist as pinned, but disabled.
7468 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7469 assertEmpty(mManager.getManifestShortcuts());
7470 assertShortcutIds(assertAllNotManifest(assertAllImmutable(assertAllDisabled(
7471 mManager.getPinnedShortcuts()))),
7472 "ms1");
7473
7474 // Make sure the fields are updated.
7475 ShortcutInfo si = getCallerShortcut("ms1");
7476
7477 assertEquals("ms1", si.getId());
7478 assertEquals(R.drawable.icon2, si.getIconResourceId());
7479 assertEquals(R.string.shortcut_title2, si.getTitleResId());
7480 assertEquals(R.string.shortcut_text2, si.getTextResId());
Makoto Onukieddbfec2016-05-31 17:04:34 -07007481 assertEquals(R.string.shortcut_disabled_message2, si.getDisabledMessageResourceId());
Makoto Onuki22fcc682016-05-17 14:52:19 -07007482 assertEquals(Intent.ACTION_VIEW, si.getIntent().getAction());
7483
7484 // Make sure there's no other dangling shortcuts.
7485 assertShortcutIds(getCallerShortcuts(), "ms1");
7486 });
7487 }
7488
7489 public void testManifestShortcuts_duplicateInSingleActivity() {
7490 mService.handleUnlockUser(USER_0);
7491
7492 // The XML has two shortcuts with the same ID.
7493 addManifestShortcutResource(
7494 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7495 R.xml.shortcut_2_duplicate);
7496 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07007497 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki22fcc682016-05-17 14:52:19 -07007498 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7499
7500 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7501 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
7502 mManager.getManifestShortcuts()))),
7503 "ms1");
7504
7505 // Make sure the first one has survived. (the second one has a different title.)
7506 ShortcutInfo si = getCallerShortcut("ms1");
7507 assertEquals(R.string.shortcut_title1, si.getTitleResId());
7508
7509 // Make sure there's no other dangling shortcuts.
7510 assertShortcutIds(getCallerShortcuts(), "ms1");
7511 });
7512 }
7513
7514 public void testManifestShortcuts_duplicateInTwoActivities() {
7515 mService.handleUnlockUser(USER_0);
7516
7517 // ShortcutActivity has shortcut ms1
7518 addManifestShortcutResource(
7519 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7520 R.xml.shortcut_1);
7521
7522 // ShortcutActivity2 has two shortcuts, ms1 and ms2.
7523 addManifestShortcutResource(
7524 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity2.class.getName()),
Makoto Onuki9e1f5592016-06-08 12:30:23 -07007525 R.xml.shortcut_5);
Makoto Onuki22fcc682016-05-17 14:52:19 -07007526 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07007527 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki22fcc682016-05-17 14:52:19 -07007528 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7529
7530 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7531 assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
7532 mManager.getManifestShortcuts()))),
Makoto Onuki9e1f5592016-06-08 12:30:23 -07007533 "ms1", "ms2", "ms3", "ms4", "ms5");
Makoto Onuki22fcc682016-05-17 14:52:19 -07007534
7535 // ms1 should belong to ShortcutActivity.
7536 ShortcutInfo si = getCallerShortcut("ms1");
7537 assertEquals(R.string.shortcut_title1, si.getTitleResId());
7538 assertEquals(new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7539 si.getActivity());
Makoto Onuki9e1f5592016-06-08 12:30:23 -07007540 assertEquals(0, si.getRank());
Makoto Onuki22fcc682016-05-17 14:52:19 -07007541
7542 // ms2 should belong to ShortcutActivity*2*.
7543 si = getCallerShortcut("ms2");
7544 assertEquals(R.string.shortcut_title2, si.getTitleResId());
7545 assertEquals(new ComponentName(CALLING_PACKAGE_1, ShortcutActivity2.class.getName()),
7546 si.getActivity());
7547
Makoto Onuki9e1f5592016-06-08 12:30:23 -07007548 // Also check the ranks
7549 assertWith(getCallerShortcuts()).selectManifest()
7550 .selectByActivity(
7551 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()))
7552 .haveRanksInOrder("ms1");
7553 assertWith(getCallerShortcuts()).selectManifest()
7554 .selectByActivity(
7555 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity2.class.getName()))
7556 .haveRanksInOrder("ms2", "ms3", "ms4", "ms5");
7557
Makoto Onuki22fcc682016-05-17 14:52:19 -07007558 // Make sure there's no other dangling shortcuts.
Makoto Onuki9e1f5592016-06-08 12:30:23 -07007559 assertShortcutIds(getCallerShortcuts(), "ms1", "ms2", "ms3", "ms4", "ms5");
Makoto Onuki22fcc682016-05-17 14:52:19 -07007560 });
7561 }
7562
7563 /**
7564 * Manifest shortcuts cannot override shortcuts that were published via the APIs.
7565 */
7566 public void testManifestShortcuts_cannotOverrideNonManifest() {
7567 mService.handleUnlockUser(USER_0);
7568
7569 // Create a non-pinned dynamic shortcut and a non-dynamic pinned shortcut.
7570
7571 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7572 mManager.setDynamicShortcuts(list(
7573 makeShortcut("ms1", "title1",
Makoto Onuki51ab2b32016-06-02 11:03:51 -07007574 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
Makoto Onuki99302b52017-03-29 12:42:26 -07007575 /* icon */ null, new Intent("action1"), /* rank */ 0),
Makoto Onuki22fcc682016-05-17 14:52:19 -07007576 makeShortcut("ms2", "title2",
Makoto Onuki51ab2b32016-06-02 11:03:51 -07007577 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
Makoto Onuki99302b52017-03-29 12:42:26 -07007578 /* icon */ null, new Intent("action1"), /* rank */ 0)));
Makoto Onuki22fcc682016-05-17 14:52:19 -07007579 });
7580
7581 runWithCaller(LAUNCHER_1, USER_0, () -> {
7582 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms2"), HANDLE_USER_0);
7583 });
7584
7585 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7586 mManager.removeDynamicShortcuts(list("ms2"));
7587
7588 assertShortcutIds(mManager.getDynamicShortcuts(), "ms1");
7589 assertShortcutIds(mManager.getPinnedShortcuts(), "ms2");
7590 assertEmpty(mManager.getManifestShortcuts());
7591 });
7592
7593 // Then update the app with 5 manifest shortcuts.
7594 // Make sure "ms1" and "ms2" won't be replaced.
7595 addManifestShortcutResource(
7596 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7597 R.xml.shortcut_5);
7598 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07007599 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki22fcc682016-05-17 14:52:19 -07007600 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7601
7602 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7603 assertShortcutIds(assertAllNotManifest(mManager.getDynamicShortcuts()), "ms1");
7604 assertShortcutIds(assertAllNotManifest(mManager.getPinnedShortcuts()), "ms2");
7605 assertShortcutIds(assertAllManifest(mManager.getManifestShortcuts()),
7606 "ms3", "ms4", "ms5");
7607
7608 // ms1 and ms2 shouold keep the original title.
7609 ShortcutInfo si = getCallerShortcut("ms1");
7610 assertEquals("title1", si.getTitle());
7611
7612 si = getCallerShortcut("ms2");
7613 assertEquals("title2", si.getTitle());
7614 });
7615 }
7616
Makoto Onuki51ab2b32016-06-02 11:03:51 -07007617 protected void checkManifestShortcuts_immutable_verify() {
Makoto Onuki22fcc682016-05-17 14:52:19 -07007618 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7619 assertShortcutIds(assertAllNotManifest(assertAllEnabled(
7620 mManager.getDynamicShortcuts())),
7621 "s1");
7622 assertShortcutIds(assertAllManifest(assertAllEnabled(
7623 mManager.getManifestShortcuts())),
7624 "ms1");
7625 assertShortcutIds(assertAllNotManifest(assertAllDisabled(
7626 mManager.getPinnedShortcuts())),
7627 "ms2");
7628
7629 assertEquals("t1", getCallerShortcut("s1").getTitle());
7630
7631 // Make sure there are no other shortcuts.
7632 assertShortcutIds(getCallerShortcuts(), "s1", "ms1", "ms2");
7633 });
7634 }
7635
7636 /**
7637 * Make sure the APIs won't work on manifest shortcuts.
7638 */
7639 public void testManifestShortcuts_immutable() {
7640 mService.handleUnlockUser(USER_0);
7641
7642 // Create a non-pinned manifest shortcut, a pinned shortcut that was originally
7643 // a manifest shortcut, as well as a dynamic shortcut.
7644
7645 addManifestShortcutResource(
7646 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7647 R.xml.shortcut_2);
7648 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07007649 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki22fcc682016-05-17 14:52:19 -07007650 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7651
7652 runWithCaller(LAUNCHER_1, USER_0, () -> {
7653 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms2"), HANDLE_USER_0);
7654 });
7655
7656 addManifestShortcutResource(
7657 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7658 R.xml.shortcut_1);
7659 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07007660 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki22fcc682016-05-17 14:52:19 -07007661 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7662
7663 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7664 mManager.addDynamicShortcuts(list(makeShortcutWithTitle("s1", "t1")));
7665 });
7666
7667 checkManifestShortcuts_immutable_verify();
7668
7669 // Note that even though the first argument is not immutable and only the second one
7670 // is immutable, the first argument should not be executed either.
7671
7672 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7673 assertCannotUpdateImmutable(() -> {
7674 mManager.setDynamicShortcuts(list(makeShortcut("xx"), makeShortcut("ms1")));
7675 });
7676 assertCannotUpdateImmutable(() -> {
7677 mManager.setDynamicShortcuts(list(makeShortcut("xx"), makeShortcut("ms2")));
7678 });
7679 });
7680 checkManifestShortcuts_immutable_verify();
7681
7682 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7683 assertCannotUpdateImmutable(() -> {
7684 mManager.addDynamicShortcuts(list(makeShortcut("xx"), makeShortcut("ms1")));
7685 });
7686 assertCannotUpdateImmutable(() -> {
7687 mManager.addDynamicShortcuts(list(makeShortcut("xx"), makeShortcut("ms2")));
7688 });
7689 });
7690 checkManifestShortcuts_immutable_verify();
7691
7692
7693 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7694 assertCannotUpdateImmutable(() -> {
7695 mManager.updateShortcuts(list(makeShortcut("s1"), makeShortcut("ms1")));
7696 });
7697 assertCannotUpdateImmutable(() -> {
7698 mManager.updateShortcuts(list(makeShortcut("s1"), makeShortcut("ms2")));
7699 });
7700 });
7701 checkManifestShortcuts_immutable_verify();
7702
7703 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7704 assertCannotUpdateImmutable(() -> {
7705 mManager.removeDynamicShortcuts(list("s1", "ms1"));
7706 });
7707 assertCannotUpdateImmutable(() -> {
7708 mManager.removeDynamicShortcuts(list("s2", "ms2"));
7709 });
7710 });
7711 checkManifestShortcuts_immutable_verify();
7712
7713 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7714 assertCannotUpdateImmutable(() -> {
7715 mManager.disableShortcuts(list("s1", "ms1"));
7716 });
7717 });
7718 checkManifestShortcuts_immutable_verify();
7719
7720 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7721 assertCannotUpdateImmutable(() -> {
7722 mManager.enableShortcuts(list("s1", "ms2"));
7723 });
7724 });
7725 checkManifestShortcuts_immutable_verify();
7726 }
Makoto Onuki7001a612016-05-27 13:24:28 -07007727
Makoto Onuki51ab2b32016-06-02 11:03:51 -07007728
Makoto Onuki7001a612016-05-27 13:24:28 -07007729 /**
7730 * Make sure the APIs won't work on manifest shortcuts.
7731 */
7732 public void testManifestShortcuts_tooMany() {
7733 // Change the max number of shortcuts.
7734 mService.updateConfigurationLocked(ConfigConstants.KEY_MAX_SHORTCUTS + "=3");
7735
7736 mService.handleUnlockUser(USER_0);
7737
7738 addManifestShortcutResource(
7739 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7740 R.xml.shortcut_5);
7741 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07007742 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki7001a612016-05-27 13:24:28 -07007743 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7744
7745 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7746 // Only the first 3 should be published.
7747 assertShortcutIds(mManager.getManifestShortcuts(), "ms1", "ms2", "ms3");
7748 });
7749 }
7750
7751 public void testMaxShortcutCount_set() {
7752 // Change the max number of shortcuts.
7753 mService.updateConfigurationLocked(ConfigConstants.KEY_MAX_SHORTCUTS + "=3");
7754
7755 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7756 final ComponentName a1 = new ComponentName(mClientContext, ShortcutActivity.class);
7757 final ComponentName a2 = new ComponentName(mClientContext, ShortcutActivity2.class);
7758 final ShortcutInfo s1_1 = makeShortcutWithActivity("s11", a1);
7759 final ShortcutInfo s1_2 = makeShortcutWithActivity("s12", a1);
7760 final ShortcutInfo s1_3 = makeShortcutWithActivity("s13", a1);
7761 final ShortcutInfo s1_4 = makeShortcutWithActivity("s14", a1);
7762 final ShortcutInfo s1_5 = makeShortcutWithActivity("s15", a1);
7763 final ShortcutInfo s1_6 = makeShortcutWithActivity("s16", a1);
7764 final ShortcutInfo s2_1 = makeShortcutWithActivity("s21", a2);
7765 final ShortcutInfo s2_2 = makeShortcutWithActivity("s22", a2);
7766 final ShortcutInfo s2_3 = makeShortcutWithActivity("s23", a2);
7767 final ShortcutInfo s2_4 = makeShortcutWithActivity("s24", a2);
7768
7769 // 3 shortcuts for 2 activities -> okay
7770 mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3, s2_1, s2_2, s2_3));
7771 assertShortcutIds(mManager.getDynamicShortcuts(),
7772 "s11", "s12", "s13", "s21", "s22", "s23");
7773
7774 mManager.removeAllDynamicShortcuts();
7775
7776 // 4 shortcut for activity 1 -> too many.
7777 assertDynamicShortcutCountExceeded(() -> {
7778 mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3, s1_4, s2_1, s2_2, s2_3));
7779 });
7780 assertEmpty(mManager.getDynamicShortcuts());
7781
7782 // 4 shortcut for activity 2 -> too many.
7783 assertDynamicShortcutCountExceeded(() -> {
7784 mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3, s2_1, s2_2, s2_3, s2_4));
7785 });
7786 assertEmpty(mManager.getDynamicShortcuts());
7787
7788 // First, set 3. Then set 4, which should be ignored.
7789 mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3));
7790 assertShortcutIds(mManager.getDynamicShortcuts(),
7791 "s11", "s12", "s13");
7792 assertDynamicShortcutCountExceeded(() -> {
7793 mManager.setDynamicShortcuts(list(s2_1, s2_2, s2_3, s2_4));
7794 });
7795 assertShortcutIds(mManager.getDynamicShortcuts(),
7796 "s11", "s12", "s13");
7797
7798 // Set will remove the old dynamic set, unlike add, so the following should pass.
7799 mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3));
7800 assertShortcutIds(mManager.getDynamicShortcuts(),
7801 "s11", "s12", "s13");
7802 mManager.setDynamicShortcuts(list(s1_4, s1_5, s1_6));
7803 assertShortcutIds(mManager.getDynamicShortcuts(),
7804 "s14", "s15", "s16");
7805
7806 // Now, test with 2 manifest shortcuts.
7807 mManager.removeAllDynamicShortcuts();
7808 addManifestShortcutResource(
7809 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7810 R.xml.shortcut_2);
7811 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07007812 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki7001a612016-05-27 13:24:28 -07007813 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7814 assertEquals(2, mManager.getManifestShortcuts().size());
7815
7816 // Setting 1 to activity 1 will work.
7817 mManager.setDynamicShortcuts(list(s1_1, s2_1, s2_2, s2_3));
7818 assertShortcutIds(mManager.getDynamicShortcuts(),
7819 "s11", "s21", "s22", "s23");
7820 assertEquals(2, mManager.getManifestShortcuts().size());
7821
7822 // But setting 2 will not.
7823 mManager.removeAllDynamicShortcuts();
7824 assertDynamicShortcutCountExceeded(() -> {
7825 mManager.setDynamicShortcuts(list(s1_1, s1_2, s2_1, s2_2, s2_3));
7826 });
7827 assertEmpty(mManager.getDynamicShortcuts());
7828 assertEquals(2, mManager.getManifestShortcuts().size());
7829 });
7830 }
7831
7832 public void testMaxShortcutCount_add() {
7833 // Change the max number of shortcuts.
7834 mService.updateConfigurationLocked(ConfigConstants.KEY_MAX_SHORTCUTS + "=3");
7835
7836 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7837 final ComponentName a1 = new ComponentName(mClientContext, ShortcutActivity.class);
7838 final ComponentName a2 = new ComponentName(mClientContext, ShortcutActivity2.class);
7839 final ShortcutInfo s1_1 = makeShortcutWithActivity("s11", a1);
7840 final ShortcutInfo s1_2 = makeShortcutWithActivity("s12", a1);
7841 final ShortcutInfo s1_3 = makeShortcutWithActivity("s13", a1);
7842 final ShortcutInfo s1_4 = makeShortcutWithActivity("s14", a1);
7843 final ShortcutInfo s2_1 = makeShortcutWithActivity("s21", a2);
7844 final ShortcutInfo s2_2 = makeShortcutWithActivity("s22", a2);
7845 final ShortcutInfo s2_3 = makeShortcutWithActivity("s23", a2);
7846 final ShortcutInfo s2_4 = makeShortcutWithActivity("s24", a2);
7847
7848 // 3 shortcuts for 2 activities -> okay
7849 mManager.addDynamicShortcuts(list(s1_1, s1_2, s1_3, s2_1, s2_2, s2_3));
7850 assertShortcutIds(mManager.getDynamicShortcuts(),
7851 "s11", "s12", "s13", "s21", "s22", "s23");
7852
7853 mManager.removeAllDynamicShortcuts();
Makoto Onuki7001a612016-05-27 13:24:28 -07007854
7855 // 4 shortcut for activity 1 -> too many.
7856 assertDynamicShortcutCountExceeded(() -> {
7857 mManager.addDynamicShortcuts(list(s1_1, s1_2, s1_3, s1_4, s2_1, s2_2, s2_3));
7858 });
7859 assertEmpty(mManager.getDynamicShortcuts());
7860
7861 // 4 shortcut for activity 2 -> too many.
7862 assertDynamicShortcutCountExceeded(() -> {
7863 mManager.addDynamicShortcuts(list(s1_1, s1_2, s1_3, s2_1, s2_2, s2_3, s2_4));
7864 });
7865 assertEmpty(mManager.getDynamicShortcuts());
7866
7867 // First, set 3. Then add 1 more, which should be ignored.
7868 mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3));
7869 assertShortcutIds(mManager.getDynamicShortcuts(),
7870 "s11", "s12", "s13");
7871 assertDynamicShortcutCountExceeded(() -> {
7872 mManager.addDynamicShortcuts(list(s1_4, s2_1));
7873 });
7874 assertShortcutIds(mManager.getDynamicShortcuts(),
7875 "s11", "s12", "s13");
7876
7877 // Update existing one, which should work.
7878 mManager.addDynamicShortcuts(list(makeShortcutWithActivityAndTitle(
7879 "s11", a1, "xxx"), s2_1));
7880 assertShortcutIds(mManager.getDynamicShortcuts(),
7881 "s11", "s12", "s13", "s21");
7882 assertEquals("xxx", getCallerShortcut("s11").getTitle());
7883
7884 // Make sure pinned shortcuts won't affect.
7885 // - Pin s11 - s13, and remove all dynamic.
7886 runWithCaller(LAUNCHER_1, USER_0, () -> {
7887 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s11", "s12", "s13"),
7888 HANDLE_USER_0);
7889 });
7890 mManager.removeAllDynamicShortcuts();
7891
7892 assertEmpty(mManager.getDynamicShortcuts());
7893 assertShortcutIds(mManager.getPinnedShortcuts(),
7894 "s11", "s12", "s13");
7895
7896 // Then add dynamic.
7897 mManager.addDynamicShortcuts(list(s1_4, s2_1, s2_2, s2_3));
7898
7899 assertShortcutIds(mManager.getDynamicShortcuts(),
7900 "s14", "s21", "s22", "s23");
7901 assertShortcutIds(mManager.getPinnedShortcuts(),
7902 "s11", "s12", "s13");
7903
7904 // Adding "s11" and "s12" back, should work
7905 mManager.addDynamicShortcuts(list(s1_1, s1_2));
7906
7907 assertShortcutIds(mManager.getDynamicShortcuts(),
7908 "s14", "s11", "s12", "s21", "s22", "s23");
7909 assertShortcutIds(mManager.getPinnedShortcuts(),
7910 "s11", "s12", "s13");
7911
7912 // Adding back s13 doesn't work.
7913 assertDynamicShortcutCountExceeded(() -> {
7914 mManager.addDynamicShortcuts(list(s1_3));
7915 });
7916
7917 assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a1),
7918 "s11", "s12", "s14");
7919 assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a2),
7920 "s21", "s22", "s23");
7921
7922 // Now swap the activities.
7923 mManager.updateShortcuts(list(
7924 makeShortcutWithActivity("s11", a2),
7925 makeShortcutWithActivity("s21", a1)));
7926
7927 assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a1),
7928 "s21", "s12", "s14");
7929 assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a2),
7930 "s11", "s22", "s23");
7931
7932 // Now, test with 2 manifest shortcuts.
7933 mManager.removeAllDynamicShortcuts();
7934 addManifestShortcutResource(
7935 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
7936 R.xml.shortcut_2);
7937 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07007938 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki7001a612016-05-27 13:24:28 -07007939 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
7940
7941 assertEquals(2, mManager.getManifestShortcuts().size());
7942
7943 // Adding one shortcut to activity 1 works fine.
7944 mManager.addDynamicShortcuts(list(s1_1, s2_1, s2_2, s2_3));
7945 assertShortcutIds(mManager.getDynamicShortcuts(),
7946 "s11", "s21", "s22", "s23");
7947 assertEquals(2, mManager.getManifestShortcuts().size());
7948
7949 // But adding one more doesn't.
7950 assertDynamicShortcutCountExceeded(() -> {
7951 mManager.addDynamicShortcuts(list(s1_4, s2_1));
7952 });
7953 assertShortcutIds(mManager.getDynamicShortcuts(),
7954 "s11", "s21", "s22", "s23");
7955 assertEquals(2, mManager.getManifestShortcuts().size());
7956 });
7957 }
7958
7959 public void testMaxShortcutCount_update() {
7960 // Change the max number of shortcuts.
7961 mService.updateConfigurationLocked(ConfigConstants.KEY_MAX_SHORTCUTS + "=3");
7962
7963 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
7964 final ComponentName a1 = new ComponentName(mClientContext, ShortcutActivity.class);
7965 final ComponentName a2 = new ComponentName(mClientContext, ShortcutActivity2.class);
7966 final ShortcutInfo s1_1 = makeShortcutWithActivity("s11", a1);
7967 final ShortcutInfo s1_2 = makeShortcutWithActivity("s12", a1);
7968 final ShortcutInfo s1_3 = makeShortcutWithActivity("s13", a1);
7969 final ShortcutInfo s1_4 = makeShortcutWithActivity("s14", a1);
7970 final ShortcutInfo s1_5 = makeShortcutWithActivity("s15", a1);
7971 final ShortcutInfo s2_1 = makeShortcutWithActivity("s21", a2);
7972 final ShortcutInfo s2_2 = makeShortcutWithActivity("s22", a2);
7973 final ShortcutInfo s2_3 = makeShortcutWithActivity("s23", a2);
7974 final ShortcutInfo s2_4 = makeShortcutWithActivity("s24", a2);
7975
7976 // 3 shortcuts for 2 activities -> okay
7977 mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3, s2_1, s2_2, s2_3));
7978 assertShortcutIds(mManager.getDynamicShortcuts(),
7979 "s11", "s12", "s13", "s21", "s22", "s23");
7980
7981 // Trying to move s11 from a1 to a2 should fail.
7982 assertDynamicShortcutCountExceeded(() -> {
7983 mManager.updateShortcuts(list(makeShortcutWithActivity("s11", a2)));
7984 });
7985 assertShortcutIds(mManager.getDynamicShortcuts(),
7986 "s11", "s12", "s13", "s21", "s22", "s23");
7987
7988 // Trying to move s21 from a2 to a1 should also fail.
7989 assertDynamicShortcutCountExceeded(() -> {
7990 mManager.updateShortcuts(list(makeShortcutWithActivity("s21", a1)));
7991 });
7992 assertShortcutIds(mManager.getDynamicShortcuts(),
7993 "s11", "s12", "s13", "s21", "s22", "s23");
7994
7995 // But, if we do these two at the same time, it should work.
7996 mManager.updateShortcuts(list(
7997 makeShortcutWithActivity("s11", a2),
7998 makeShortcutWithActivity("s21", a1)));
7999 assertShortcutIds(mManager.getDynamicShortcuts(),
8000 "s11", "s12", "s13", "s21", "s22", "s23");
8001 assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a1),
8002 "s21", "s12", "s13");
8003 assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a2),
8004 "s11", "s22", "s23");
8005
8006 // Then reset.
8007 mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3, s2_1, s2_2, s2_3));
8008 assertShortcutIds(mManager.getDynamicShortcuts(),
8009 "s11", "s12", "s13", "s21", "s22", "s23");
8010
8011 // Pin some to have more shortcuts for a1.
8012 runWithCaller(LAUNCHER_1, USER_0, () -> {
8013 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s11", "s12", "s13"),
8014 HANDLE_USER_0);
8015 });
8016 mManager.setDynamicShortcuts(list(s1_4, s1_5, s2_1, s2_2, s2_3));
8017 assertShortcutIds(mManager.getDynamicShortcuts(),
8018 "s14", "s15", "s21", "s22", "s23");
8019 assertShortcutIds(mManager.getPinnedShortcuts(),
8020 "s11", "s12", "s13");
8021
8022 // a1 already has 2 dynamic shortcuts (and 3 pinned shortcuts that used to belong on it)
8023 // But that doesn't matter for update -- the following should still work.
8024 mManager.updateShortcuts(list(
8025 makeShortcutWithActivityAndTitle("s11", a1, "xxx1"),
8026 makeShortcutWithActivityAndTitle("s12", a1, "xxx2"),
8027 makeShortcutWithActivityAndTitle("s13", a1, "xxx3"),
8028 makeShortcutWithActivityAndTitle("s14", a1, "xxx4"),
8029 makeShortcutWithActivityAndTitle("s15", a1, "xxx5")));
8030 // All the shortcuts should still exist they all belong on same activities,
8031 // with the updated titles.
8032 assertShortcutIds(mManager.getDynamicShortcuts(),
8033 "s14", "s15", "s21", "s22", "s23");
8034 assertShortcutIds(mManager.getPinnedShortcuts(),
8035 "s11", "s12", "s13");
8036
8037 assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a1),
8038 "s14", "s15");
8039 assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a2),
8040 "s21", "s22", "s23");
8041
8042 assertEquals("xxx1", getCallerShortcut("s11").getTitle());
8043 assertEquals("xxx2", getCallerShortcut("s12").getTitle());
8044 assertEquals("xxx3", getCallerShortcut("s13").getTitle());
8045 assertEquals("xxx4", getCallerShortcut("s14").getTitle());
8046 assertEquals("xxx5", getCallerShortcut("s15").getTitle());
8047 });
8048 }
8049
8050 public void testShortcutsPushedOutByManifest() {
8051 // Change the max number of shortcuts.
8052 mService.updateConfigurationLocked(ConfigConstants.KEY_MAX_SHORTCUTS + "=3");
8053
8054 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
8055 final ComponentName a1 = new ComponentName(mClientContext, ShortcutActivity.class);
8056 final ComponentName a2 = new ComponentName(mClientContext, ShortcutActivity2.class);
8057 final ShortcutInfo s1_1 = makeShortcutWithActivityAndRank("s11", a1, 4);
8058 final ShortcutInfo s1_2 = makeShortcutWithActivityAndRank("s12", a1, 3);
8059 final ShortcutInfo s1_3 = makeShortcutWithActivityAndRank("s13", a1, 2);
8060 final ShortcutInfo s1_4 = makeShortcutWithActivityAndRank("s14", a1, 1);
8061 final ShortcutInfo s1_5 = makeShortcutWithActivityAndRank("s15", a1, 0);
8062 final ShortcutInfo s2_1 = makeShortcutWithActivityAndRank("s21", a2, 0);
8063 final ShortcutInfo s2_2 = makeShortcutWithActivityAndRank("s22", a2, 1);
8064 final ShortcutInfo s2_3 = makeShortcutWithActivityAndRank("s23", a2, 2);
8065 final ShortcutInfo s2_4 = makeShortcutWithActivityAndRank("s24", a2, 3);
8066 final ShortcutInfo s2_5 = makeShortcutWithActivityAndRank("s25", a2, 4);
8067
8068 // Initial state.
8069 mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3, s2_1, s2_2, s2_3));
8070 runWithCaller(LAUNCHER_1, USER_0, () -> {
8071 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s11", "s12", "s21", "s22"),
8072 HANDLE_USER_0);
8073 });
8074 mManager.setDynamicShortcuts(list(s1_2, s1_3, s1_4, s2_2, s2_3, s2_4));
8075 assertShortcutIds(assertAllEnabled(mManager.getDynamicShortcuts()),
8076 "s12", "s13", "s14",
8077 "s22", "s23", "s24");
8078 assertShortcutIds(assertAllEnabled(mManager.getPinnedShortcuts()),
8079 "s11", "s12",
8080 "s21", "s22");
8081
8082 // Add 1 manifest shortcut to a1.
8083 addManifestShortcutResource(
8084 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
8085 R.xml.shortcut_1);
8086 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07008087 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki7001a612016-05-27 13:24:28 -07008088 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
8089 assertEquals(1, mManager.getManifestShortcuts().size());
8090
8091 // s12 removed.
8092 assertShortcutIds(assertAllEnabled(mManager.getDynamicShortcuts()),
8093 "s13", "s14",
8094 "s22", "s23", "s24");
8095 assertShortcutIds(assertAllEnabled(mManager.getPinnedShortcuts()),
8096 "s11", "s12",
8097 "s21", "s22");
8098
8099 // Add more manifest shortcuts.
8100 addManifestShortcutResource(
8101 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
8102 R.xml.shortcut_2);
8103 addManifestShortcutResource(
8104 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity2.class.getName()),
8105 R.xml.shortcut_1_alt);
8106 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07008107 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki7001a612016-05-27 13:24:28 -07008108 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
8109 assertEquals(3, mManager.getManifestShortcuts().size());
8110
8111 // Note the ones with the highest rank values (== least important) will be removed.
8112 assertShortcutIds(assertAllEnabled(mManager.getDynamicShortcuts()),
8113 "s14",
8114 "s22", "s23");
8115 assertShortcutIds(assertAllEnabled(mManager.getPinnedShortcuts()),
8116 "s11", "s12",
8117 "s21", "s22");
8118
8119 // Add more manifest shortcuts.
8120 addManifestShortcutResource(
8121 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
8122 R.xml.shortcut_2);
8123 addManifestShortcutResource(
8124 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity2.class.getName()),
8125 R.xml.shortcut_5_alt); // manifest has 5, but max is 3, so a2 will have 3.
8126 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07008127 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki7001a612016-05-27 13:24:28 -07008128 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
8129 assertEquals(5, mManager.getManifestShortcuts().size());
8130
8131 assertShortcutIds(assertAllEnabled(mManager.getDynamicShortcuts()),
8132 "s14" // a1 has 1 dynamic
8133 ); // a2 has no dynamic
8134 assertShortcutIds(assertAllEnabled(mManager.getPinnedShortcuts()),
8135 "s11", "s12",
8136 "s21", "s22");
8137
8138 // Update, no manifest shortucts. This doesn't affect anything.
8139 addManifestShortcutResource(
8140 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
8141 R.xml.shortcut_0);
8142 addManifestShortcutResource(
8143 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity2.class.getName()),
8144 R.xml.shortcut_0);
8145 updatePackageVersion(CALLING_PACKAGE_1, 1);
Makoto Onukif34c3082016-07-13 10:25:25 -07008146 mService.mPackageMonitor.onReceive(getTestContext(),
Makoto Onuki7001a612016-05-27 13:24:28 -07008147 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
8148 assertEquals(0, mManager.getManifestShortcuts().size());
8149
8150 assertShortcutIds(assertAllEnabled(mManager.getDynamicShortcuts()),
8151 "s14");
8152 assertShortcutIds(assertAllEnabled(mManager.getPinnedShortcuts()),
8153 "s11", "s12",
8154 "s21", "s22");
8155 });
8156 }
Makoto Onukibf563b62017-05-04 10:25:30 -07008157
8158 public void testReturnedByServer() {
8159 // Package 1 updated, with manifest shortcuts.
8160 addManifestShortcutResource(
8161 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
8162 R.xml.shortcut_1);
8163 updatePackageVersion(CALLING_PACKAGE_1, 1);
8164 mService.mPackageMonitor.onReceive(getTestContext(),
8165 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
8166
8167 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
8168 assertWith(mManager.getManifestShortcuts())
8169 .haveIds("ms1")
8170 .forAllShortcuts(si -> assertTrue(si.isReturnedByServer()));
8171
8172 assertTrue(mManager.setDynamicShortcuts(list(makeShortcut("s1"))));
8173
8174 assertWith(mManager.getDynamicShortcuts())
8175 .haveIds("s1")
8176 .forAllShortcuts(si -> assertTrue(si.isReturnedByServer()));
8177 });
8178
8179 // Pin them.
8180 runWithCaller(LAUNCHER_1, USER_0, () -> {
8181 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
8182 list("ms1", "s1"), getCallingUser());
8183 assertWith(getShortcutAsLauncher(USER_0))
8184 .haveIds("ms1", "s1")
8185 .forAllShortcuts(si -> assertTrue(si.isReturnedByServer()));
8186 });
8187
8188 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
8189 assertWith(mManager.getPinnedShortcuts())
8190 .haveIds("ms1", "s1")
8191 .forAllShortcuts(si -> assertTrue(si.isReturnedByServer()));
8192 });
8193
8194 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
8195 // This shows a warning log, but should still work.
8196 assertTrue(mManager.setDynamicShortcuts(mManager.getDynamicShortcuts()));
8197
8198 assertWith(mManager.getDynamicShortcuts())
8199 .haveIds("s1")
8200 .forAllShortcuts(si -> assertTrue(si.isReturnedByServer()));
8201 });
8202 }
Tony Maked6ef622017-12-07 16:36:16 +00008203
8204 public void testIsForegroundDefaultLauncher_true() {
8205 final ComponentName defaultLauncher = new ComponentName("default", "launcher");
8206 final int uid = 1024;
8207
8208 setDefaultLauncher(UserHandle.USER_SYSTEM, defaultLauncher);
8209 makeUidForeground(uid);
8210
8211 assertTrue(mInternal.isForegroundDefaultLauncher("default", uid));
8212 }
8213
8214
8215 public void testIsForegroundDefaultLauncher_defaultButNotForeground() {
8216 final ComponentName defaultLauncher = new ComponentName("default", "launcher");
8217 final int uid = 1024;
8218
8219 setDefaultLauncher(UserHandle.USER_SYSTEM, defaultLauncher);
8220 makeUidBackground(uid);
8221
8222 assertFalse(mInternal.isForegroundDefaultLauncher("default", uid));
8223 }
8224
8225 public void testIsForegroundDefaultLauncher_foregroundButNotDefault() {
8226 final ComponentName defaultLauncher = new ComponentName("default", "launcher");
8227 final int uid = 1024;
8228
8229 setDefaultLauncher(UserHandle.USER_SYSTEM, defaultLauncher);
8230 makeUidForeground(uid);
8231
8232 assertFalse(mInternal.isForegroundDefaultLauncher("another", uid));
8233 }
Mehdi Alizadeh32774622018-11-05 17:32:01 -08008234
8235 public void testParseShareTargetsFromManifest() {
8236 // These values must exactly match the content of shortcuts_share_targets.xml resource
8237 List<ShareTargetInfo> expectedValues = new ArrayList<>();
8238 expectedValues.add(new ShareTargetInfo(
8239 new ShareTargetInfo.TargetData[]{new ShareTargetInfo.TargetData(
8240 "http", "www.google.com", "1234", "somePath", "somePathPattern",
8241 "somePathPrefix", "text/plain")}, "com.test.directshare.TestActivity1",
8242 new String[]{"com.test.category.CATEGORY1", "com.test.category.CATEGORY2"}));
8243 expectedValues.add(new ShareTargetInfo(new ShareTargetInfo.TargetData[]{
8244 new ShareTargetInfo.TargetData(null, null, null, null, null, null, "video/mp4"),
8245 new ShareTargetInfo.TargetData("content", null, null, null, null, null, "video/*")},
8246 "com.test.directshare.TestActivity5",
8247 new String[]{"com.test.category.CATEGORY5", "com.test.category.CATEGORY6"}));
8248
8249 addManifestShortcutResource(
8250 new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
8251 R.xml.shortcut_share_targets);
8252 updatePackageVersion(CALLING_PACKAGE_1, 1);
8253 mService.mPackageMonitor.onReceive(getTestContext(),
8254 genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
8255
8256 List<ShareTargetInfo> shareTargets = getCallerShareTargets();
8257
8258 assertNotNull(shareTargets);
8259 assertEquals(expectedValues.size(), shareTargets.size());
8260
8261 for (int i = 0; i < expectedValues.size(); i++) {
8262 ShareTargetInfo expected = expectedValues.get(i);
8263 ShareTargetInfo actual = shareTargets.get(i);
8264
8265 assertEquals(expected.mTargetData.length, actual.mTargetData.length);
8266 for (int j = 0; j < expected.mTargetData.length; j++) {
8267 assertEquals(expected.mTargetData[j].mScheme, actual.mTargetData[j].mScheme);
8268 assertEquals(expected.mTargetData[j].mHost, actual.mTargetData[j].mHost);
8269 assertEquals(expected.mTargetData[j].mPort, actual.mTargetData[j].mPort);
8270 assertEquals(expected.mTargetData[j].mPath, actual.mTargetData[j].mPath);
8271 assertEquals(expected.mTargetData[j].mPathPrefix,
8272 actual.mTargetData[j].mPathPrefix);
8273 assertEquals(expected.mTargetData[j].mPathPattern,
8274 actual.mTargetData[j].mPathPattern);
8275 assertEquals(expected.mTargetData[j].mMimeType, actual.mTargetData[j].mMimeType);
8276 }
8277
8278 assertEquals(expected.mTargetClass, actual.mTargetClass);
8279
8280 assertEquals(expected.mCategories.length, actual.mCategories.length);
8281 for (int j = 0; j < expected.mCategories.length; j++) {
8282 assertEquals(expected.mCategories[j], actual.mCategories[j]);
8283 }
8284 }
8285 }
Mehdi Alizadehabec3192019-06-27 18:06:15 -07008286
8287 public void testShareTargetInfo_saveToXml() throws IOException, XmlPullParserException {
8288 List<ShareTargetInfo> expectedValues = new ArrayList<>();
8289 expectedValues.add(new ShareTargetInfo(
8290 new ShareTargetInfo.TargetData[]{new ShareTargetInfo.TargetData(
8291 "http", "www.google.com", "1234", "somePath", "somePathPattern",
8292 "somePathPrefix", "text/plain")}, "com.test.directshare.TestActivity1",
8293 new String[]{"com.test.category.CATEGORY1", "com.test.category.CATEGORY2"}));
8294 expectedValues.add(new ShareTargetInfo(new ShareTargetInfo.TargetData[]{
8295 new ShareTargetInfo.TargetData(null, null, null, null, null, null, "video/mp4"),
8296 new ShareTargetInfo.TargetData("content", null, null, null, null, null, "video/*")},
8297 "com.test.directshare.TestActivity5",
8298 new String[]{"com.test.category.CATEGORY5", "com.test.category.CATEGORY6"}));
8299
8300 // Write ShareTargets to Xml
8301 ByteArrayOutputStream outStream = new ByteArrayOutputStream();
8302 final XmlSerializer outXml = new FastXmlSerializer();
8303 outXml.setOutput(outStream, StandardCharsets.UTF_8.name());
8304 outXml.startDocument(null, true);
8305 for (int i = 0; i < expectedValues.size(); i++) {
8306 expectedValues.get(i).saveToXml(outXml);
8307 }
8308 outXml.endDocument();
8309 outXml.flush();
8310
8311 // Read ShareTargets from Xml
8312 ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray());
8313 XmlPullParser parser = Xml.newPullParser();
8314 parser.setInput(new InputStreamReader(inStream));
8315 List<ShareTargetInfo> shareTargets = new ArrayList<>();
8316 int type;
8317 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
8318 if (type == XmlPullParser.START_TAG && parser.getName().equals("share-target")) {
8319 shareTargets.add(ShareTargetInfo.loadFromXml(parser));
8320 }
8321 }
8322
8323 // Assert two lists are equal
8324 assertNotNull(shareTargets);
8325 assertEquals(expectedValues.size(), shareTargets.size());
8326
8327 for (int i = 0; i < expectedValues.size(); i++) {
8328 ShareTargetInfo expected = expectedValues.get(i);
8329 ShareTargetInfo actual = shareTargets.get(i);
8330
8331 assertEquals(expected.mTargetData.length, actual.mTargetData.length);
8332 for (int j = 0; j < expected.mTargetData.length; j++) {
8333 assertEquals(expected.mTargetData[j].mScheme, actual.mTargetData[j].mScheme);
8334 assertEquals(expected.mTargetData[j].mHost, actual.mTargetData[j].mHost);
8335 assertEquals(expected.mTargetData[j].mPort, actual.mTargetData[j].mPort);
8336 assertEquals(expected.mTargetData[j].mPath, actual.mTargetData[j].mPath);
8337 assertEquals(expected.mTargetData[j].mPathPrefix,
8338 actual.mTargetData[j].mPathPrefix);
8339 assertEquals(expected.mTargetData[j].mPathPattern,
8340 actual.mTargetData[j].mPathPattern);
8341 assertEquals(expected.mTargetData[j].mMimeType, actual.mTargetData[j].mMimeType);
8342 }
8343
8344 assertEquals(expected.mTargetClass, actual.mTargetClass);
8345
8346 assertEquals(expected.mCategories.length, actual.mCategories.length);
8347 for (int j = 0; j < expected.mCategories.length; j++) {
8348 assertEquals(expected.mCategories[j], actual.mCategories[j]);
8349 }
8350 }
8351 }
Makoto Onuki6f7362d92016-03-04 13:39:41 -08008352}