blob: a6e0a662ffbcfe4974f1c692be7e4bb9f1eb7eb2 [file] [log] [blame]
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001/*
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 Onukid0010c52017-03-30 14:17:35 -070018import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.anyOrNull;
19import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.anyStringOrNull;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070020import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.cloneShortcutList;
21import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.hashSet;
22import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.list;
23import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.makeBundle;
24import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.set;
25
Sunny Goyal145c8f82018-02-15 14:27:09 -080026import static org.mockito.ArgumentMatchers.anyBoolean;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070027import static org.mockito.Matchers.any;
Makoto Onuki157b1622016-06-02 16:13:10 -070028import static org.mockito.Matchers.anyInt;
29import static org.mockito.Matchers.anyString;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070030import static org.mockito.Matchers.eq;
31import static org.mockito.Mockito.doAnswer;
32import static org.mockito.Mockito.mock;
33import static org.mockito.Mockito.reset;
34import static org.mockito.Mockito.spy;
Makoto Onukiff14f732016-06-30 17:07:25 -070035import static org.mockito.Mockito.times;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070036import static org.mockito.Mockito.verify;
37import static org.mockito.Mockito.when;
38
39import android.annotation.NonNull;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070040import android.annotation.UserIdInt;
41import android.app.Activity;
Makoto Onuki33525d22016-08-03 15:45:24 -070042import android.app.ActivityManager;
Makoto Onukiea11db12016-06-24 15:17:44 -070043import android.app.ActivityManagerInternal;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070044import android.app.IUidObserver;
Makoto Onukiac042502016-05-20 16:39:42 -070045import android.app.usage.UsageStatsManagerInternal;
Makoto Onuki440a1ea2016-07-20 14:21:18 -070046import android.content.ActivityNotFoundException;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070047import android.content.BroadcastReceiver;
48import android.content.ComponentName;
49import android.content.Context;
50import android.content.Intent;
51import android.content.IntentFilter;
Makoto Onuki2d895c32016-12-02 15:48:40 -080052import android.content.IntentSender;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070053import android.content.pm.ActivityInfo;
54import android.content.pm.ApplicationInfo;
55import android.content.pm.ILauncherApps;
56import android.content.pm.LauncherApps;
57import android.content.pm.LauncherApps.ShortcutQuery;
58import android.content.pm.PackageInfo;
59import android.content.pm.PackageManager;
60import android.content.pm.PackageManagerInternal;
Makoto Onukib08790c2016-06-23 14:05:46 -070061import android.content.pm.ResolveInfo;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070062import android.content.pm.ShortcutInfo;
63import android.content.pm.ShortcutManager;
64import android.content.pm.ShortcutServiceInternal;
65import android.content.pm.Signature;
66import android.content.pm.UserInfo;
67import android.content.res.Resources;
68import android.content.res.XmlResourceParser;
69import android.graphics.drawable.Icon;
70import android.net.Uri;
71import android.os.Bundle;
72import android.os.FileUtils;
73import android.os.Handler;
74import android.os.Looper;
Makoto Onukib5a012f2016-06-21 11:13:53 -070075import android.os.PersistableBundle;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070076import android.os.Process;
Makoto Onukia01f4f02016-12-15 15:58:41 -080077import android.os.RemoteException;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070078import android.os.UserHandle;
79import android.os.UserManager;
Sunny Goyal145c8f82018-02-15 14:27:09 -080080import android.os.UserManagerInternal;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070081import android.test.InstrumentationTestCase;
82import android.test.mock.MockContext;
Makoto Onuki2d895c32016-12-02 15:48:40 -080083import android.util.ArrayMap;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070084import android.util.Log;
85import android.util.Pair;
86
87import com.android.internal.util.Preconditions;
88import com.android.server.LocalServices;
89import com.android.server.SystemService;
90import com.android.server.pm.LauncherAppsService.LauncherAppsImpl;
91import com.android.server.pm.ShortcutUser.PackageWithUser;
92
93import org.junit.Assert;
94import org.mockito.ArgumentCaptor;
Makoto Onukie70b29e2018-04-03 16:44:39 -070095import org.mockito.invocation.InvocationOnMock;
96import org.mockito.stubbing.Answer;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070097
98import java.io.BufferedReader;
99import java.io.ByteArrayOutputStream;
100import java.io.File;
101import java.io.FileReader;
Makoto Onuki76269922016-07-15 14:58:54 -0700102import java.io.IOException;
103import java.io.InputStreamReader;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700104import java.io.PrintWriter;
105import java.util.ArrayList;
106import java.util.HashMap;
107import java.util.HashSet;
108import java.util.LinkedHashMap;
109import java.util.List;
Makoto Onuki157b1622016-06-02 16:13:10 -0700110import java.util.Locale;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700111import java.util.Map;
112import java.util.Set;
Makoto Onukib08790c2016-06-23 14:05:46 -0700113import java.util.function.BiFunction;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700114import java.util.function.BiPredicate;
115import java.util.function.Consumer;
Makoto Onukie70b29e2018-04-03 16:44:39 -0700116import java.util.function.Function;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700117
118public abstract class BaseShortcutManagerTest extends InstrumentationTestCase {
119 protected static final String TAG = "ShortcutManagerTest";
120
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700121 protected static final boolean DUMP_IN_TEARDOWN = false; // DO NOT SUBMIT WITH true
122
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700123 /**
124 * Whether to enable dump or not. Should be only true when debugging to avoid bugs where
125 * dump affecting the behavior.
126 */
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700127 protected static final boolean ENABLE_DUMP = false // DO NOT SUBMIT WITH true
128 || DUMP_IN_TEARDOWN || ShortcutService.DEBUG;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700129
130 protected static final String[] EMPTY_STRINGS = new String[0]; // Just for readability.
131
Makoto Onukib08790c2016-06-23 14:05:46 -0700132 protected static final String MAIN_ACTIVITY_CLASS = "MainActivity";
Makoto Onuki2d895c32016-12-02 15:48:40 -0800133 protected static final String PIN_CONFIRM_ACTIVITY_CLASS = "PinConfirmActivity";
Makoto Onukib08790c2016-06-23 14:05:46 -0700134
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700135 // public for mockito
136 public class BaseContext extends MockContext {
137 @Override
138 public Object getSystemService(String name) {
139 switch (name) {
140 case Context.USER_SERVICE:
141 return mMockUserManager;
142 }
143 throw new UnsupportedOperationException();
144 }
145
146 @Override
147 public String getSystemServiceName(Class<?> serviceClass) {
148 return getTestContext().getSystemServiceName(serviceClass);
149 }
150
151 @Override
152 public PackageManager getPackageManager() {
153 return mMockPackageManager;
154 }
155
156 @Override
157 public Resources getResources() {
158 return getTestContext().getResources();
159 }
160
161 @Override
162 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
163 IntentFilter filter, String broadcastPermission, Handler scheduler) {
164 // ignore.
165 return null;
166 }
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700167
168 @Override
169 public void unregisterReceiver(BroadcastReceiver receiver) {
170 // ignore.
171 }
Makoto Onuki2d895c32016-12-02 15:48:40 -0800172
173 @Override
174 public void startActivityAsUser(Intent intent, UserHandle user) {
175 // ignore, use spy to intercept it.
176 }
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700177 }
178
179 /** Context used in the client side */
180 public class ClientContext extends BaseContext {
181 @Override
182 public String getPackageName() {
183 return mInjectedClientPackage;
184 }
185
186 @Override
187 public int getUserId() {
188 return getCallingUserId();
189 }
190 }
191
192 /** Context used in the service side */
193 public class ServiceContext extends BaseContext {
194 long injectClearCallingIdentity() {
195 final int prevCallingUid = mInjectedCallingUid;
196 mInjectedCallingUid = Process.SYSTEM_UID;
197 return prevCallingUid;
198 }
199
200 void injectRestoreCallingIdentity(long token) {
201 mInjectedCallingUid = (int) token;
202 }
203
204 @Override
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700205 public int getUserId() {
206 return UserHandle.USER_SYSTEM;
207 }
208
209 public PackageInfo injectGetActivitiesWithMetadata(
210 String packageName, @UserIdInt int userId) {
211 return BaseShortcutManagerTest.this.injectGetActivitiesWithMetadata(packageName, userId);
212 }
213
214 public XmlResourceParser injectXmlMetaData(ActivityInfo activityInfo, String key) {
215 return BaseShortcutManagerTest.this.injectXmlMetaData(activityInfo, key);
216 }
Makoto Onuki2d895c32016-12-02 15:48:40 -0800217
218 public void sendIntentSender(IntentSender intent) {
219 // Placeholder for spying.
220 }
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700221 }
222
223 /** ShortcutService with injection override methods. */
224 protected final class ShortcutServiceTestable extends ShortcutService {
225 final ServiceContext mContext;
226 IUidObserver mUidObserver;
227
228 public ShortcutServiceTestable(ServiceContext context, Looper looper) {
Makoto Onukiee6b6e42016-06-29 17:34:02 -0700229 super(context, looper, /* onyForPackageManagerApis */ false);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700230 mContext = context;
231 }
232
233 @Override
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700234 public String injectGetLocaleTagsForUser(@UserIdInt int userId) {
235 return mInjectedLocale.toLanguageTag();
236 }
237
238 @Override
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700239 boolean injectShouldPerformVerification() {
240 return true; // Always verify during unit tests.
241 }
242
243 @Override
244 String injectShortcutManagerConstants() {
245 return ConfigConstants.KEY_RESET_INTERVAL_SEC + "=" + (INTERVAL / 1000) + ","
246 + ConfigConstants.KEY_MAX_SHORTCUTS + "=" + MAX_SHORTCUTS + ","
247 + ConfigConstants.KEY_MAX_UPDATES_PER_INTERVAL + "="
248 + MAX_UPDATES_PER_INTERVAL + ","
249 + ConfigConstants.KEY_MAX_ICON_DIMENSION_DP + "=" + MAX_ICON_DIMENSION + ","
250 + ConfigConstants.KEY_MAX_ICON_DIMENSION_DP_LOWRAM + "="
251 + MAX_ICON_DIMENSION_LOWRAM + ","
252 + ConfigConstants.KEY_ICON_FORMAT + "=PNG,"
253 + ConfigConstants.KEY_ICON_QUALITY + "=100";
254 }
255
256 @Override
257 long injectClearCallingIdentity() {
258 return mContext.injectClearCallingIdentity();
259 }
260
261 @Override
262 void injectRestoreCallingIdentity(long token) {
263 mContext.injectRestoreCallingIdentity(token);
264 }
265
266 @Override
267 int injectDipToPixel(int dip) {
268 return dip;
269 }
270
271 @Override
272 long injectCurrentTimeMillis() {
273 return mInjectedCurrentTimeMillis;
274 }
275
276 @Override
277 long injectElapsedRealtime() {
278 // TODO This should be kept separately from mInjectedCurrentTimeMillis, since
279 // this should increase even if we rewind mInjectedCurrentTimeMillis in some tests.
280 return mInjectedCurrentTimeMillis - START_TIME;
281 }
282
283 @Override
Makoto Onuki475c3652017-05-08 14:29:03 -0700284 long injectUptimeMillis() {
285 return mInjectedCurrentTimeMillis - START_TIME - mDeepSleepTime;
286 }
287
288 @Override
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700289 int injectBinderCallingUid() {
290 return mInjectedCallingUid;
291 }
292
293 @Override
Makoto Onuki7d0fa812018-02-21 11:24:43 -0800294 int injectBinderCallingPid() {
295 // Note it's not used in tests, so just return a "random" value.
296 return mInjectedCallingUid * 123;
297 }
298
299 @Override
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700300 int injectGetPackageUid(String packageName, int userId) {
301 return getInjectedPackageInfo(packageName, userId, false).applicationInfo.uid;
302 }
303
304 @Override
305 File injectSystemDataPath() {
306 return new File(mInjectedFilePathRoot, "system");
307 }
308
309 @Override
310 File injectUserDataPath(@UserIdInt int userId) {
311 return new File(mInjectedFilePathRoot, "user-" + userId);
312 }
313
314 @Override
315 void injectValidateIconResPackage(ShortcutInfo shortcut, Icon icon) {
316 // Can't check
317 }
318
319 @Override
320 boolean injectIsLowRamDevice() {
321 return mInjectedIsLowRamDevice;
322 }
323
324 @Override
325 void injectRegisterUidObserver(IUidObserver observer, int which) {
326 mUidObserver = observer;
327 }
328
329 @Override
Makoto Onuki634cecb2017-10-13 17:10:48 -0700330 boolean hasShortcutHostPermission(@NonNull String callingPackage, int userId,
331 int callingPid, int callingUid) {
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700332 return mDefaultLauncherChecker.test(callingPackage, userId);
333 }
334
335 @Override
Makoto Onuki7d0fa812018-02-21 11:24:43 -0800336 boolean injectHasUnlimitedShortcutsApiCallsPermission(int callingPid, int callingUid) {
337 return mInjectHasUnlimitedShortcutsApiCallsPermission;
338 }
339
340 @Override
Makoto Onuki2d895c32016-12-02 15:48:40 -0800341 ComponentName getDefaultLauncher(@UserIdInt int userId) {
342 final ComponentName activity = mDefaultLauncher.get(userId);
343 if (activity != null) {
344 return activity;
345 }
346 return super.getDefaultLauncher(userId);
347 }
348
349 @Override
Makoto Onukiee6b6e42016-06-29 17:34:02 -0700350 PackageInfo injectPackageInfoWithUninstalled(String packageName, @UserIdInt int userId,
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700351 boolean getSignatures) {
352 return getInjectedPackageInfo(packageName, userId, getSignatures);
353 }
354
355 @Override
Makoto Onukiee6b6e42016-06-29 17:34:02 -0700356 ApplicationInfo injectApplicationInfoWithUninstalled(
357 String packageName, @UserIdInt int userId) {
358 PackageInfo pi = injectPackageInfoWithUninstalled(
359 packageName, userId, /* getSignatures= */ false);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700360 return pi != null ? pi.applicationInfo : null;
361 }
362
363 @Override
Makoto Onukiee6b6e42016-06-29 17:34:02 -0700364 List<PackageInfo> injectGetPackagesWithUninstalled(@UserIdInt int userId) {
365 return BaseShortcutManagerTest.this.getInstalledPackagesWithUninstalled(userId);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700366 }
367
368 @Override
Makoto Onukiee6b6e42016-06-29 17:34:02 -0700369 ActivityInfo injectGetActivityInfoWithMetadataWithUninstalled(ComponentName activity,
Makoto Onukib08790c2016-06-23 14:05:46 -0700370 @UserIdInt int userId) {
371 final PackageInfo pi = mContext.injectGetActivitiesWithMetadata(
372 activity.getPackageName(), userId);
373 if (pi == null || pi.activities == null) {
374 return null;
375 }
376 for (ActivityInfo ai : pi.activities) {
377 if (!mEnabledActivityChecker.test(ai.getComponentName(), userId)) {
378 continue;
379 }
380 if (activity.equals(ai.getComponentName())) {
381 return ai;
382 }
383 }
384 return null;
385 }
386
387 @Override
388 boolean injectIsMainActivity(@NonNull ComponentName activity, int userId) {
389 if (!mEnabledActivityChecker.test(activity, userId)) {
390 return false;
391 }
392 return mMainActivityChecker.test(activity, userId);
393 }
394
395 @Override
396 List<ResolveInfo> injectGetMainActivities(@NonNull String packageName, int userId) {
397 final PackageInfo pi = mContext.injectGetActivitiesWithMetadata(
398 packageName, userId);
399 if (pi == null || pi.activities == null) {
400 return null;
401 }
402 final ArrayList<ResolveInfo> ret = new ArrayList<>(pi.activities.length);
403 for (int i = 0; i < pi.activities.length; i++) {
404 if (!mEnabledActivityChecker.test(pi.activities[i].getComponentName(), userId)) {
405 continue;
406 }
407 final ResolveInfo ri = new ResolveInfo();
408 ri.activityInfo = pi.activities[i];
409 ret.add(ri);
410 }
411
412 return ret;
413 }
414
415 @Override
416 ComponentName injectGetDefaultMainActivity(@NonNull String packageName, int userId) {
417 return mMainActivityFetcher.apply(packageName, userId);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700418 }
419
420 @Override
Makoto Onuki2d895c32016-12-02 15:48:40 -0800421 ComponentName injectGetPinConfirmationActivity(@NonNull String launcherPackageName,
Sunny Goyal7f7372a2017-01-24 11:53:54 -0800422 int launcherUserId, int requestType) {
Makoto Onuki2d895c32016-12-02 15:48:40 -0800423 return mPinConfirmActivityFetcher.apply(launcherPackageName, launcherUserId);
424 }
425
426 @Override
Makoto Onukiee6b6e42016-06-29 17:34:02 -0700427 boolean injectIsActivityEnabledAndExported(ComponentName activity, @UserIdInt int userId) {
428 return mEnabledActivityChecker.test(activity, userId);
429 }
430
431 @Override
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700432 XmlResourceParser injectXmlMetaData(ActivityInfo activityInfo, String key) {
433 return mContext.injectXmlMetaData(activityInfo, key);
434 }
435
436 @Override
Makoto Onuki157b1622016-06-02 16:13:10 -0700437 void injectPostToHandler(Runnable r) {
Makoto Onukia2241832016-07-06 13:28:37 -0700438 runOnHandler(r);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700439 }
440
441 @Override
Makoto Onuki085a05c2016-08-19 11:39:29 -0700442 void injectRunOnNewThread(Runnable r) {
443 runOnHandler(r);
444 }
445
446 @Override
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700447 void injectEnforceCallingPermission(String permission, String message) {
448 if (!mCallerPermissions.contains(permission)) {
449 throw new SecurityException("Missing permission: " + permission);
450 }
451 }
452
453 @Override
Makoto Onukib08790c2016-06-23 14:05:46 -0700454 boolean injectIsSafeModeEnabled() {
455 return mSafeMode;
456 }
457
458 @Override
Makoto Onuki33663282016-08-22 16:19:04 -0700459 String injectBuildFingerprint() {
460 return mInjectedBuildFingerprint;
461 }
462
463 @Override
Sunny Goyal87a563e2017-01-01 19:42:45 -0800464 void injectSendIntentSender(IntentSender intent, Intent extras) {
Makoto Onuki2d895c32016-12-02 15:48:40 -0800465 mContext.sendIntentSender(intent);
466 }
467
468 @Override
Makoto Onuki35559d62017-11-06 16:26:32 -0800469 boolean injectHasAccessShortcutsPermission(int callingPid, int callingUid) {
Makoto Onuki634cecb2017-10-13 17:10:48 -0700470 return mInjectCheckAccessShortcutsPermission;
471 }
472
473 @Override
Makoto Onukia2241832016-07-06 13:28:37 -0700474 void wtf(String message, Throwable th) {
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700475 // During tests, WTF is fatal.
Makoto Onukia2241832016-07-06 13:28:37 -0700476 fail(message + " exception: " + th + "\n" + Log.getStackTraceString(th));
477 }
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700478 }
479
480 /** ShortcutManager with injection override methods. */
481 protected class ShortcutManagerTestable extends ShortcutManager {
482 public ShortcutManagerTestable(Context context, ShortcutServiceTestable service) {
483 super(context, service);
484 }
485
486 @Override
487 protected int injectMyUserId() {
488 return UserHandle.getUserId(mInjectedCallingUid);
489 }
490
491 @Override
492 public boolean setDynamicShortcuts(@NonNull List<ShortcutInfo> shortcutInfoList) {
493 // Note to simulate the binder RPC, we need to clone the incoming arguments.
494 // Otherwise bad things will happen because they're mutable.
495 return super.setDynamicShortcuts(cloneShortcutList(shortcutInfoList));
496 }
497
498 @Override
499 public boolean addDynamicShortcuts(@NonNull List<ShortcutInfo> shortcutInfoList) {
500 // Note to simulate the binder RPC, we need to clone the incoming arguments.
501 return super.addDynamicShortcuts(cloneShortcutList(shortcutInfoList));
502 }
503
504 @Override
505 public boolean updateShortcuts(List<ShortcutInfo> shortcutInfoList) {
506 // Note to simulate the binder RPC, we need to clone the incoming arguments.
507 return super.updateShortcuts(cloneShortcutList(shortcutInfoList));
508 }
509 }
510
511 protected class LauncherAppImplTestable extends LauncherAppsImpl {
512 final ServiceContext mContext;
513
514 public LauncherAppImplTestable(ServiceContext context) {
515 super(context);
516 mContext = context;
517 }
518
519 @Override
520 public void verifyCallingPackage(String callingPackage) {
521 // SKIP
522 }
523
524 @Override
525 void postToPackageMonitorHandler(Runnable r) {
Makoto Onukia2241832016-07-06 13:28:37 -0700526 runOnHandler(r);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700527 }
528
529 @Override
530 int injectBinderCallingUid() {
531 return mInjectedCallingUid;
532 }
533
534 @Override
Makoto Onuki7d0fa812018-02-21 11:24:43 -0800535 int injectBinderCallingPid() {
536 // Note it's not used in tests, so just return a "random" value.
537 return mInjectedCallingUid * 123;
538 }
539
540 @Override
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700541 long injectClearCallingIdentity() {
542 final int prevCallingUid = mInjectedCallingUid;
543 mInjectedCallingUid = Process.SYSTEM_UID;
544 return prevCallingUid;
545 }
546
547 @Override
548 void injectRestoreCallingIdentity(long token) {
549 mInjectedCallingUid = (int) token;
550 }
551 }
552
553 protected class LauncherAppsTestable extends LauncherApps {
554 public LauncherAppsTestable(Context context, ILauncherApps service) {
555 super(context, service);
556 }
557 }
558
559 public static class ShortcutActivity extends Activity {
560 }
561
562 public static class ShortcutActivity2 extends Activity {
563 }
564
565 public static class ShortcutActivity3 extends Activity {
566 }
567
Makoto Onukia2241832016-07-06 13:28:37 -0700568 protected Looper mLooper;
569 protected Handler mHandler;
570
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700571 protected ServiceContext mServiceContext;
572 protected ClientContext mClientContext;
573
574 protected ShortcutServiceTestable mService;
575 protected ShortcutManagerTestable mManager;
576 protected ShortcutServiceInternal mInternal;
577
578 protected LauncherAppImplTestable mLauncherAppImpl;
579
580 // LauncherApps has per-instace state, so we need a differnt instance for each launcher.
581 protected final Map<Pair<Integer, String>, LauncherAppsTestable>
582 mLauncherAppsMap = new HashMap<>();
583 protected LauncherAppsTestable mLauncherApps; // Current one
584
585 protected File mInjectedFilePathRoot;
586
Makoto Onukib08790c2016-06-23 14:05:46 -0700587 protected boolean mSafeMode;
588
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700589 protected long mInjectedCurrentTimeMillis;
Makoto Onuki475c3652017-05-08 14:29:03 -0700590 protected long mDeepSleepTime; // Used to calculate "uptimeMillis".
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700591
592 protected boolean mInjectedIsLowRamDevice;
593
Makoto Onuki157b1622016-06-02 16:13:10 -0700594 protected Locale mInjectedLocale = Locale.ENGLISH;
595
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700596 protected int mInjectedCallingUid;
597 protected String mInjectedClientPackage;
598
599 protected Map<String, PackageInfo> mInjectedPackages;
600
601 protected Set<PackageWithUser> mUninstalledPackages;
Makoto Onuki82fb2eb2017-03-31 16:58:26 -0700602 protected Set<PackageWithUser> mDisabledPackages;
Makoto Onuki66e4a2b2017-01-23 11:37:45 -0800603 protected Set<PackageWithUser> mEphemeralPackages;
Makoto Onuki33663282016-08-22 16:19:04 -0700604 protected Set<String> mSystemPackages;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700605
606 protected PackageManager mMockPackageManager;
607 protected PackageManagerInternal mMockPackageManagerInternal;
608 protected UserManager mMockUserManager;
Sunny Goyal145c8f82018-02-15 14:27:09 -0800609 protected UserManagerInternal mMockUserManagerInternal;
Makoto Onukiac042502016-05-20 16:39:42 -0700610 protected UsageStatsManagerInternal mMockUsageStatsManagerInternal;
Makoto Onukiea11db12016-06-24 15:17:44 -0700611 protected ActivityManagerInternal mMockActivityManagerInternal;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700612
613 protected static final String CALLING_PACKAGE_1 = "com.android.test.1";
614 protected static final int CALLING_UID_1 = 10001;
615
616 protected static final String CALLING_PACKAGE_2 = "com.android.test.2";
617 protected static final int CALLING_UID_2 = 10002;
618
619 protected static final String CALLING_PACKAGE_3 = "com.android.test.3";
620 protected static final int CALLING_UID_3 = 10003;
621
622 protected static final String CALLING_PACKAGE_4 = "com.android.test.4";
623 protected static final int CALLING_UID_4 = 10004;
624
625 protected static final String LAUNCHER_1 = "com.android.launcher.1";
626 protected static final int LAUNCHER_UID_1 = 10011;
627
628 protected static final String LAUNCHER_2 = "com.android.launcher.2";
629 protected static final int LAUNCHER_UID_2 = 10012;
630
631 protected static final String LAUNCHER_3 = "com.android.launcher.3";
632 protected static final int LAUNCHER_UID_3 = 10013;
633
634 protected static final String LAUNCHER_4 = "com.android.launcher.4";
635 protected static final int LAUNCHER_UID_4 = 10014;
636
637 protected static final int USER_0 = UserHandle.USER_SYSTEM;
638 protected static final int USER_10 = 10;
639 protected static final int USER_11 = 11;
Makoto Onukide3c16c2017-01-26 11:39:31 -0800640 protected static final int USER_P0 = 20; // profile of user 0 (MANAGED_PROFILE *not* set)
641 protected static final int USER_P1 = 21; // another profile of user 0 (MANAGED_PROFILE set)
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700642
643 protected static final UserHandle HANDLE_USER_0 = UserHandle.of(USER_0);
644 protected static final UserHandle HANDLE_USER_10 = UserHandle.of(USER_10);
645 protected static final UserHandle HANDLE_USER_11 = UserHandle.of(USER_11);
646 protected static final UserHandle HANDLE_USER_P0 = UserHandle.of(USER_P0);
Makoto Onukide3c16c2017-01-26 11:39:31 -0800647 protected static final UserHandle HANDLE_USER_P1 = UserHandle.of(USER_P1);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700648
649 protected static final UserInfo USER_INFO_0 = withProfileGroupId(
650 new UserInfo(USER_0, "user0",
Makoto Onuki2d895c32016-12-02 15:48:40 -0800651 UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY | UserInfo.FLAG_INITIALIZED), 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700652
653 protected static final UserInfo USER_INFO_10 =
654 new UserInfo(USER_10, "user10", UserInfo.FLAG_INITIALIZED);
655
656 protected static final UserInfo USER_INFO_11 =
657 new UserInfo(USER_11, "user11", UserInfo.FLAG_INITIALIZED);
658
Makoto Onukiaecbd032017-01-19 12:11:11 -0800659 /*
660 * Cheat: USER_P0 is a sub profile of USER_0, but it doesn't have the MANAGED_PROFILE flag set.
661 * Due to a change made to LauncherApps (b/34340531), work profile apps a no longer able
662 * to see the main profile, which would break tons of unit tests. We avoid it by not setting
663 * MANAGED_PROFILE for P0.
664 * We cover this negative case in CTS. (i.e. CTS has tests to make sure maanged profile
665 * can't access main profile's shortcuts.)
666 */
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700667 protected static final UserInfo USER_INFO_P0 = withProfileGroupId(
Makoto Onukiaecbd032017-01-19 12:11:11 -0800668 new UserInfo(USER_P0, "userP0", UserInfo.FLAG_INITIALIZED), 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700669
Makoto Onukide3c16c2017-01-26 11:39:31 -0800670 protected static final UserInfo USER_INFO_P1 = withProfileGroupId(
671 new UserInfo(USER_P1, "userP1",
672 UserInfo.FLAG_INITIALIZED | UserInfo.FLAG_MANAGED_PROFILE), 0);
673
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700674 protected BiPredicate<String, Integer> mDefaultLauncherChecker =
675 (callingPackage, userId) ->
676 LAUNCHER_1.equals(callingPackage) || LAUNCHER_2.equals(callingPackage)
677 || LAUNCHER_3.equals(callingPackage) || LAUNCHER_4.equals(callingPackage);
678
Makoto Onuki2d895c32016-12-02 15:48:40 -0800679 private final Map<Integer, ComponentName> mDefaultLauncher = new ArrayMap<>();
680
Makoto Onukib08790c2016-06-23 14:05:46 -0700681 protected BiPredicate<ComponentName, Integer> mMainActivityChecker =
682 (activity, userId) -> true;
683
684 protected BiFunction<String, Integer, ComponentName> mMainActivityFetcher =
685 (packageName, userId) -> new ComponentName(packageName, MAIN_ACTIVITY_CLASS);
686
Makoto Onuki2d895c32016-12-02 15:48:40 -0800687 protected BiFunction<String, Integer, ComponentName> mPinConfirmActivityFetcher =
688 (packageName, userId) -> new ComponentName(packageName, PIN_CONFIRM_ACTIVITY_CLASS);
689
Makoto Onukib08790c2016-06-23 14:05:46 -0700690 protected BiPredicate<ComponentName, Integer> mEnabledActivityChecker
691 = (activity, userId) -> true; // all activities are enabled.
692
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700693 protected static final long START_TIME = 1440000000101L;
694
695 protected static final long INTERVAL = 10000;
696
697 protected static final int MAX_SHORTCUTS = 10;
698
699 protected static final int MAX_UPDATES_PER_INTERVAL = 3;
700
701 protected static final int MAX_ICON_DIMENSION = 128;
702
703 protected static final int MAX_ICON_DIMENSION_LOWRAM = 32;
704
705 protected static final ShortcutQuery QUERY_ALL = new ShortcutQuery();
706
707 protected final ArrayList<String> mCallerPermissions = new ArrayList<>();
708
709 protected final HashMap<String, LinkedHashMap<ComponentName, Integer>> mActivityMetadataResId
710 = new HashMap<>();
711
Makoto Onukia2241832016-07-06 13:28:37 -0700712 protected final Map<Integer, UserInfo> mUserInfos = new HashMap<>();
713 protected final Map<Integer, Boolean> mRunningUsers = new HashMap<>();
714 protected final Map<Integer, Boolean> mUnlockedUsers = new HashMap<>();
715
Makoto Onuki0b9d1db2016-07-18 14:16:41 -0700716 protected static final String PACKAGE_SYSTEM_LAUNCHER = "com.android.systemlauncher";
717 protected static final String PACKAGE_SYSTEM_LAUNCHER_NAME = "systemlauncher_name";
718 protected static final int PACKAGE_SYSTEM_LAUNCHER_PRIORITY = 0;
719
720 protected static final String PACKAGE_FALLBACK_LAUNCHER = "com.android.settings";
721 protected static final String PACKAGE_FALLBACK_LAUNCHER_NAME = "fallback";
722 protected static final int PACKAGE_FALLBACK_LAUNCHER_PRIORITY = -999;
723
Makoto Onuki33663282016-08-22 16:19:04 -0700724 protected String mInjectedBuildFingerprint = "build1";
725
Makoto Onuki634cecb2017-10-13 17:10:48 -0700726 protected boolean mInjectCheckAccessShortcutsPermission = false;
727
Makoto Onuki7d0fa812018-02-21 11:24:43 -0800728 protected boolean mInjectHasUnlimitedShortcutsApiCallsPermission = false;
729
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700730 static {
731 QUERY_ALL.setQueryFlags(
Makoto Onuki4d6b87f2016-06-17 13:47:40 -0700732 ShortcutQuery.FLAG_GET_ALL_KINDS);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700733 }
734
735 @Override
736 protected void setUp() throws Exception {
737 super.setUp();
738
Makoto Onukia2241832016-07-06 13:28:37 -0700739 mLooper = Looper.getMainLooper();
740 mHandler = new Handler(mLooper);
741
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700742 mServiceContext = spy(new ServiceContext());
743 mClientContext = new ClientContext();
744
745 mMockPackageManager = mock(PackageManager.class);
746 mMockPackageManagerInternal = mock(PackageManagerInternal.class);
747 mMockUserManager = mock(UserManager.class);
Sunny Goyal145c8f82018-02-15 14:27:09 -0800748 mMockUserManagerInternal = mock(UserManagerInternal.class);
Makoto Onukiac042502016-05-20 16:39:42 -0700749 mMockUsageStatsManagerInternal = mock(UsageStatsManagerInternal.class);
Makoto Onukiea11db12016-06-24 15:17:44 -0700750 mMockActivityManagerInternal = mock(ActivityManagerInternal.class);
Makoto Onukiac042502016-05-20 16:39:42 -0700751
752 LocalServices.removeServiceForTest(PackageManagerInternal.class);
753 LocalServices.addService(PackageManagerInternal.class, mMockPackageManagerInternal);
754 LocalServices.removeServiceForTest(UsageStatsManagerInternal.class);
755 LocalServices.addService(UsageStatsManagerInternal.class, mMockUsageStatsManagerInternal);
Makoto Onukiea11db12016-06-24 15:17:44 -0700756 LocalServices.removeServiceForTest(ActivityManagerInternal.class);
757 LocalServices.addService(ActivityManagerInternal.class, mMockActivityManagerInternal);
Sunny Goyal145c8f82018-02-15 14:27:09 -0800758 LocalServices.removeServiceForTest(UserManagerInternal.class);
759 LocalServices.addService(UserManagerInternal.class, mMockUserManagerInternal);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700760
761 // Prepare injection values.
762
763 mInjectedCurrentTimeMillis = START_TIME;
764
765 mInjectedPackages = new HashMap<>();
766 addPackage(CALLING_PACKAGE_1, CALLING_UID_1, 1);
767 addPackage(CALLING_PACKAGE_2, CALLING_UID_2, 2);
768 addPackage(CALLING_PACKAGE_3, CALLING_UID_3, 3);
769 addPackage(CALLING_PACKAGE_4, CALLING_UID_4, 10);
770 addPackage(LAUNCHER_1, LAUNCHER_UID_1, 4);
771 addPackage(LAUNCHER_2, LAUNCHER_UID_2, 5);
772 addPackage(LAUNCHER_3, LAUNCHER_UID_3, 6);
773 addPackage(LAUNCHER_4, LAUNCHER_UID_4, 10);
774
775 // CALLING_PACKAGE_3 / LAUNCHER_3 are not backup target.
776 updatePackageInfo(CALLING_PACKAGE_3,
777 pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP);
778 updatePackageInfo(LAUNCHER_3,
779 pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP);
780
781 mUninstalledPackages = new HashSet<>();
Makoto Onuki82fb2eb2017-03-31 16:58:26 -0700782 mDisabledPackages = new HashSet<>();
Makoto Onuki33663282016-08-22 16:19:04 -0700783 mSystemPackages = new HashSet<>();
Makoto Onuki66e4a2b2017-01-23 11:37:45 -0800784 mEphemeralPackages = new HashSet<>();
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700785
786 mInjectedFilePathRoot = new File(getTestContext().getCacheDir(), "test-files");
787
788 deleteAllSavedFiles();
789
790 // Set up users.
Makoto Onukia2241832016-07-06 13:28:37 -0700791 mUserInfos.put(USER_0, USER_INFO_0);
792 mUserInfos.put(USER_10, USER_INFO_10);
793 mUserInfos.put(USER_11, USER_INFO_11);
794 mUserInfos.put(USER_P0, USER_INFO_P0);
Makoto Onukide3c16c2017-01-26 11:39:31 -0800795 mUserInfos.put(USER_P1, USER_INFO_P1);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700796
Sunny Goyal145c8f82018-02-15 14:27:09 -0800797 when(mMockUserManagerInternal.isUserUnlockingOrUnlocked(anyInt()))
798 .thenAnswer(inv -> {
Makoto Onuki9c850012016-07-26 15:50:50 -0700799 final int userId = (Integer) inv.getArguments()[0];
800 return b(mRunningUsers.get(userId)) && b(mUnlockedUsers.get(userId));
Sunny Goyal145c8f82018-02-15 14:27:09 -0800801 });
802 when(mMockUserManagerInternal.getProfileParentId(anyInt()))
803 .thenAnswer(inv -> {
Makoto Onuki2d895c32016-12-02 15:48:40 -0800804 final int userId = (Integer) inv.getArguments()[0];
805 final UserInfo ui = mUserInfos.get(userId);
806 assertNotNull(ui);
807 if (ui.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID) {
Sunny Goyal145c8f82018-02-15 14:27:09 -0800808 return userId;
Makoto Onuki2d895c32016-12-02 15:48:40 -0800809 }
810 final UserInfo parent = mUserInfos.get(ui.profileGroupId);
811 assertNotNull(parent);
Sunny Goyal145c8f82018-02-15 14:27:09 -0800812 return parent.id;
813 });
814
815 when(mMockUserManagerInternal.isProfileAccessible(anyInt(), anyInt(), anyString(),
816 anyBoolean())).thenAnswer(inv -> {
817 final int callingUserId = (Integer) inv.getArguments()[0];
818 final int targetUserId = (Integer) inv.getArguments()[1];
819 if (targetUserId == callingUserId) {
820 return true;
821 }
822 final UserInfo callingUserInfo = mUserInfos.get(callingUserId);
823 final UserInfo targetUserInfo = mUserInfos.get(targetUserId);
824 if (callingUserInfo == null || callingUserInfo.isManagedProfile()
825 || targetUserInfo == null || !targetUserInfo.isEnabled()) {
826 return false;
827 }
828 if (targetUserInfo.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID
829 && targetUserInfo.profileGroupId == callingUserInfo.profileGroupId) {
830 return true;
831 }
832 final boolean isExternal = (Boolean) inv.getArguments()[3];
833 if (!isExternal) {
834 return false;
835 }
836 throw new SecurityException(inv.getArguments()[2] + " for unrelated profile "
837 + targetUserId);
838 });
Makoto Onuki2d895c32016-12-02 15:48:40 -0800839
Makoto Onukie70b29e2018-04-03 16:44:39 -0700840 when(mMockUserManager.getUserInfo(anyInt())).thenAnswer(new AnswerWithSystemCheck<>(
841 inv -> mUserInfos.get((Integer) inv.getArguments()[0])));
Makoto Onuki33525d22016-08-03 15:45:24 -0700842 when(mMockActivityManagerInternal.getUidProcessState(anyInt())).thenReturn(
843 ActivityManager.PROCESS_STATE_CACHED_EMPTY);
844
Makoto Onuki9c850012016-07-26 15:50:50 -0700845 // User 0 and P0 are always running
Makoto Onukia2241832016-07-06 13:28:37 -0700846 mRunningUsers.put(USER_0, true);
847 mRunningUsers.put(USER_10, false);
848 mRunningUsers.put(USER_11, false);
Makoto Onuki9c850012016-07-26 15:50:50 -0700849 mRunningUsers.put(USER_P0, true);
Makoto Onukide3c16c2017-01-26 11:39:31 -0800850 mRunningUsers.put(USER_P1, true);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700851
Makoto Onukia2241832016-07-06 13:28:37 -0700852 // Unlock all users by default.
853 mUnlockedUsers.put(USER_0, true);
854 mUnlockedUsers.put(USER_10, true);
855 mUnlockedUsers.put(USER_11, true);
856 mUnlockedUsers.put(USER_P0, true);
Makoto Onukide3c16c2017-01-26 11:39:31 -0800857 mUnlockedUsers.put(USER_P1, true);
Makoto Onukia2241832016-07-06 13:28:37 -0700858
859 // Set up resources
Makoto Onuki157b1622016-06-02 16:13:10 -0700860 setUpAppResources();
861
862 // Start the service.
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700863 initService();
864 setCaller(CALLING_PACKAGE_1);
Makoto Onuki248a0ef2016-11-03 15:59:01 -0700865
866 if (ENABLE_DUMP) {
867 Log.d(TAG, "setUp done");
868 }
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700869 }
870
Makoto Onukie70b29e2018-04-03 16:44:39 -0700871 /**
872 * Returns a boolean but also checks if the current UID is SYSTEM_UID.
873 */
874 protected class AnswerWithSystemCheck<T> implements Answer<T> {
875 private final Function<InvocationOnMock, T> mChecker;
876
877 public AnswerWithSystemCheck(Function<InvocationOnMock, T> checker) {
878 mChecker = checker;
879 }
880
881 @Override
882 public T answer(InvocationOnMock invocation) throws Throwable {
883 assertEquals("Must be called on SYSTEM UID.",
884 Process.SYSTEM_UID, mInjectedCallingUid);
885 return mChecker.apply(invocation);
886 }
887 }
888
Makoto Onuki9c850012016-07-26 15:50:50 -0700889 private static boolean b(Boolean value) {
890 return (value != null && value);
891 }
892
Makoto Onuki157b1622016-06-02 16:13:10 -0700893 protected void setUpAppResources() throws Exception {
894 setUpAppResources(/* offset = */ 0);
895 }
896
897 protected void setUpAppResources(int ressIdOffset) throws Exception {
898 // ressIdOffset is used to adjust resource IDs to emulate the case where an updated app
899 // has resource IDs changed.
900
901 doAnswer(pmInvocation -> {
902 assertEquals(Process.SYSTEM_UID, mInjectedCallingUid);
903
904 final String packageName = (String) pmInvocation.getArguments()[0];
905 final int userId = (Integer) pmInvocation.getArguments()[1];
906
907 final Resources res = mock(Resources.class);
908
909 doAnswer(resInvocation -> {
910 final int argResId = (Integer) resInvocation.getArguments()[0];
911
912 return "string-" + packageName + "-user:" + userId + "-res:" + argResId
913 + "/" + mInjectedLocale;
914 }).when(res).getString(anyInt());
915
916 doAnswer(resInvocation -> {
917 final int resId = (Integer) resInvocation.getArguments()[0];
918
919 // Always use the "string" resource type. The type doesn't matter during the test.
920 return packageName + ":string/r" + resId;
921 }).when(res).getResourceName(anyInt());
922
923 doAnswer(resInvocation -> {
924 final String argResName = (String) resInvocation.getArguments()[0];
925 final String argType = (String) resInvocation.getArguments()[1];
926 final String argPackageName = (String) resInvocation.getArguments()[2];
927
928 // See the above code. getResourceName() will just use "r" + res ID as the entry
929 // name.
930 String entryName = argResName;
931 if (entryName.contains("/")) {
932 entryName = ShortcutInfo.getResourceEntryName(entryName);
933 }
934 return Integer.parseInt(entryName.substring(1)) + ressIdOffset;
Makoto Onukid0010c52017-03-30 14:17:35 -0700935 }).when(res).getIdentifier(anyStringOrNull(), anyStringOrNull(), anyStringOrNull());
Makoto Onuki157b1622016-06-02 16:13:10 -0700936 return res;
937 }).when(mMockPackageManager).getResourcesForApplicationAsUser(anyString(), anyInt());
938 }
939
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700940 protected static UserInfo withProfileGroupId(UserInfo in, int groupId) {
941 in.profileGroupId = groupId;
942 return in;
943 }
944
945 @Override
946 protected void tearDown() throws Exception {
947 if (DUMP_IN_TEARDOWN) dumpsysOnLogcat("Teardown");
948
949 shutdownServices();
950
951 super.tearDown();
952 }
953
954 protected Context getTestContext() {
955 return getInstrumentation().getContext();
956 }
957
Makoto Onukia1d38b32016-06-10 15:32:26 -0700958 protected ShortcutManager getManager() {
959 return mManager;
960 }
961
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700962 protected void deleteAllSavedFiles() {
963 // Empty the data directory.
964 if (mInjectedFilePathRoot.exists()) {
965 Assert.assertTrue("failed to delete dir",
966 FileUtils.deleteContents(mInjectedFilePathRoot));
967 }
968 mInjectedFilePathRoot.mkdirs();
969 }
970
971 /** (Re-) init the manager and the service. */
972 protected void initService() {
973 shutdownServices();
974
975 LocalServices.removeServiceForTest(ShortcutServiceInternal.class);
976
977 // Instantiate targets.
Makoto Onukia2241832016-07-06 13:28:37 -0700978 mService = new ShortcutServiceTestable(mServiceContext, mLooper);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700979 mManager = new ShortcutManagerTestable(mClientContext, mService);
980
981 mInternal = LocalServices.getService(ShortcutServiceInternal.class);
982
983 mLauncherAppImpl = new LauncherAppImplTestable(mServiceContext);
984 mLauncherApps = null;
985 mLauncherAppsMap.clear();
986
Makoto Onuki157b1622016-06-02 16:13:10 -0700987 // Send boot sequence events.
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700988 mService.onBootPhase(SystemService.PHASE_LOCK_SETTINGS_READY);
Makoto Onuki157b1622016-06-02 16:13:10 -0700989
Makoto Onuki157b1622016-06-02 16:13:10 -0700990 mService.onBootPhase(SystemService.PHASE_BOOT_COMPLETED);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700991 }
992
993 protected void shutdownServices() {
994 if (mService != null) {
995 // Flush all the unsaved data from the previous instance.
996 mService.saveDirtyInfo();
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700997
998 // Make sure everything is consistent.
999 mService.verifyStates();
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001000 }
1001 LocalServices.removeServiceForTest(ShortcutServiceInternal.class);
1002
1003 mService = null;
1004 mManager = null;
1005 mInternal = null;
1006 mLauncherAppImpl = null;
1007 mLauncherApps = null;
1008 mLauncherAppsMap.clear();
1009 }
1010
Makoto Onukia2241832016-07-06 13:28:37 -07001011 protected void runOnHandler(Runnable r) {
1012 final long token = mServiceContext.injectClearCallingIdentity();
1013 try {
1014 r.run();
1015 } finally {
1016 mServiceContext.injectRestoreCallingIdentity(token);
1017 }
1018 }
1019
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001020 protected void addPackage(String packageName, int uid, int version) {
1021 addPackage(packageName, uid, version, packageName);
1022 }
1023
1024 protected Signature[] genSignatures(String... signatures) {
1025 final Signature[] sigs = new Signature[signatures.length];
1026 for (int i = 0; i < signatures.length; i++){
1027 sigs[i] = new Signature(signatures[i].getBytes());
1028 }
1029 return sigs;
1030 }
1031
1032 protected PackageInfo genPackage(String packageName, int uid, int version, String... signatures) {
1033 final PackageInfo pi = new PackageInfo();
1034 pi.packageName = packageName;
1035 pi.applicationInfo = new ApplicationInfo();
1036 pi.applicationInfo.uid = uid;
1037 pi.applicationInfo.flags = ApplicationInfo.FLAG_INSTALLED
1038 | ApplicationInfo.FLAG_ALLOW_BACKUP;
1039 pi.versionCode = version;
1040 pi.applicationInfo.versionCode = version;
Michal Karpinski528c3e52018-02-07 17:47:10 +00001041 pi.signatures = null;
1042 pi.signingCertificateHistory = new Signature[][] {genSignatures(signatures)};
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001043
1044 return pi;
1045 }
1046
1047 protected void addPackage(String packageName, int uid, int version, String... signatures) {
1048 mInjectedPackages.put(packageName, genPackage(packageName, uid, version, signatures));
1049 }
1050
1051 protected void updatePackageInfo(String packageName, Consumer<PackageInfo> c) {
1052 c.accept(mInjectedPackages.get(packageName));
1053 }
1054
1055 protected void updatePackageVersion(String packageName, int increment) {
1056 updatePackageInfo(packageName, pi -> {
1057 pi.versionCode += increment;
1058 pi.applicationInfo.versionCode += increment;
1059 });
1060 }
1061
1062 protected void updatePackageLastUpdateTime(String packageName, long increment) {
1063 updatePackageInfo(packageName, pi -> {
1064 pi.lastUpdateTime += increment;
1065 });
1066 }
1067
Makoto Onuki085a05c2016-08-19 11:39:29 -07001068 protected void setPackageLastUpdateTime(String packageName, long value) {
1069 updatePackageInfo(packageName, pi -> {
1070 pi.lastUpdateTime = value;
1071 });
1072 }
1073
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001074 protected void uninstallPackage(int userId, String packageName) {
1075 if (ENABLE_DUMP) {
Makoto Onuki82fb2eb2017-03-31 16:58:26 -07001076 Log.v(TAG, "Uninstall package " + packageName + " / " + userId);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001077 }
1078 mUninstalledPackages.add(PackageWithUser.of(userId, packageName));
1079 }
1080
1081 protected void installPackage(int userId, String packageName) {
1082 if (ENABLE_DUMP) {
1083 Log.v(TAG, "Install package " + packageName + " / " + userId);
1084 }
1085 mUninstalledPackages.remove(PackageWithUser.of(userId, packageName));
1086 }
1087
Makoto Onuki82fb2eb2017-03-31 16:58:26 -07001088 protected void disablePackage(int userId, String packageName) {
1089 if (ENABLE_DUMP) {
1090 Log.v(TAG, "Disable package " + packageName + " / " + userId);
1091 }
1092 mDisabledPackages.add(PackageWithUser.of(userId, packageName));
1093 }
1094
1095 protected void enablePackage(int userId, String packageName) {
1096 if (ENABLE_DUMP) {
1097 Log.v(TAG, "Enable package " + packageName + " / " + userId);
1098 }
1099 mDisabledPackages.remove(PackageWithUser.of(userId, packageName));
1100 }
1101
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001102 PackageInfo getInjectedPackageInfo(String packageName, @UserIdInt int userId,
1103 boolean getSignatures) {
1104 final PackageInfo pi = mInjectedPackages.get(packageName);
1105 if (pi == null) return null;
1106
1107 final PackageInfo ret = new PackageInfo();
1108 ret.packageName = pi.packageName;
1109 ret.versionCode = pi.versionCode;
Dianne Hackborn3accca02013-09-20 09:32:11 -07001110 ret.versionCodeMajor = pi.versionCodeMajor;
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001111 ret.lastUpdateTime = pi.lastUpdateTime;
1112
1113 ret.applicationInfo = new ApplicationInfo(pi.applicationInfo);
1114 ret.applicationInfo.uid = UserHandle.getUid(userId, pi.applicationInfo.uid);
1115 ret.applicationInfo.packageName = pi.packageName;
1116
1117 if (mUninstalledPackages.contains(PackageWithUser.of(userId, packageName))) {
1118 ret.applicationInfo.flags &= ~ApplicationInfo.FLAG_INSTALLED;
1119 }
Makoto Onuki66e4a2b2017-01-23 11:37:45 -08001120 if (mEphemeralPackages.contains(PackageWithUser.of(userId, packageName))) {
Todd Kennedybe0b8892017-02-15 14:13:52 -08001121 ret.applicationInfo.privateFlags |= ApplicationInfo.PRIVATE_FLAG_INSTANT;
Makoto Onuki66e4a2b2017-01-23 11:37:45 -08001122 }
Makoto Onuki33663282016-08-22 16:19:04 -07001123 if (mSystemPackages.contains(packageName)) {
1124 ret.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
1125 }
Makoto Onuki82fb2eb2017-03-31 16:58:26 -07001126 ret.applicationInfo.enabled =
1127 !mDisabledPackages.contains(PackageWithUser.of(userId, packageName));
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001128
1129 if (getSignatures) {
Michal Karpinski528c3e52018-02-07 17:47:10 +00001130 ret.signatures = null;
1131 ret.signingCertificateHistory = pi.signingCertificateHistory;
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001132 }
1133
1134 return ret;
1135 }
1136
1137 protected void addApplicationInfo(PackageInfo pi, List<ApplicationInfo> list) {
1138 if (pi != null && pi.applicationInfo != null) {
1139 list.add(pi.applicationInfo);
1140 }
1141 }
1142
1143 protected List<ApplicationInfo> getInstalledApplications(int userId) {
1144 final ArrayList<ApplicationInfo> ret = new ArrayList<>();
1145
1146 addApplicationInfo(getInjectedPackageInfo(CALLING_PACKAGE_1, userId, false), ret);
1147 addApplicationInfo(getInjectedPackageInfo(CALLING_PACKAGE_2, userId, false), ret);
1148 addApplicationInfo(getInjectedPackageInfo(CALLING_PACKAGE_3, userId, false), ret);
1149 addApplicationInfo(getInjectedPackageInfo(CALLING_PACKAGE_4, userId, false), ret);
1150 addApplicationInfo(getInjectedPackageInfo(LAUNCHER_1, userId, false), ret);
1151 addApplicationInfo(getInjectedPackageInfo(LAUNCHER_2, userId, false), ret);
1152 addApplicationInfo(getInjectedPackageInfo(LAUNCHER_3, userId, false), ret);
1153 addApplicationInfo(getInjectedPackageInfo(LAUNCHER_4, userId, false), ret);
1154
1155 return ret;
1156 }
1157
Makoto Onuki6dd9fb72016-06-01 13:55:54 -07001158 private void addPackageInfo(PackageInfo pi, List<PackageInfo> list) {
1159 if (pi != null) {
1160 list.add(pi);
1161 }
1162 }
1163
Makoto Onukiee6b6e42016-06-29 17:34:02 -07001164 private List<PackageInfo> getInstalledPackagesWithUninstalled(int userId) {
Makoto Onuki6dd9fb72016-06-01 13:55:54 -07001165 final ArrayList<PackageInfo> ret = new ArrayList<>();
1166
1167 addPackageInfo(getInjectedPackageInfo(CALLING_PACKAGE_1, userId, false), ret);
1168 addPackageInfo(getInjectedPackageInfo(CALLING_PACKAGE_2, userId, false), ret);
1169 addPackageInfo(getInjectedPackageInfo(CALLING_PACKAGE_3, userId, false), ret);
1170 addPackageInfo(getInjectedPackageInfo(CALLING_PACKAGE_4, userId, false), ret);
1171 addPackageInfo(getInjectedPackageInfo(LAUNCHER_1, userId, false), ret);
1172 addPackageInfo(getInjectedPackageInfo(LAUNCHER_2, userId, false), ret);
1173 addPackageInfo(getInjectedPackageInfo(LAUNCHER_3, userId, false), ret);
1174 addPackageInfo(getInjectedPackageInfo(LAUNCHER_4, userId, false), ret);
1175
1176 return ret;
1177 }
1178
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001179 protected void addManifestShortcutResource(ComponentName activity, int resId) {
1180 final String packageName = activity.getPackageName();
1181 LinkedHashMap<ComponentName, Integer> map = mActivityMetadataResId.get(packageName);
1182 if (map == null) {
1183 map = new LinkedHashMap<>();
1184 mActivityMetadataResId.put(packageName, map);
1185 }
1186 map.put(activity, resId);
1187 }
1188
1189 protected PackageInfo injectGetActivitiesWithMetadata(String packageName, @UserIdInt int userId) {
1190 final PackageInfo ret = getInjectedPackageInfo(packageName, userId,
1191 /* getSignatures=*/ false);
1192
1193 final HashMap<ComponentName, Integer> activities = mActivityMetadataResId.get(packageName);
1194 if (activities != null) {
1195 final ArrayList<ActivityInfo> list = new ArrayList<>();
1196
1197 for (ComponentName cn : activities.keySet()) {
1198 ActivityInfo ai = new ActivityInfo();
1199 ai.packageName = cn.getPackageName();
1200 ai.name = cn.getClassName();
1201 ai.metaData = new Bundle();
1202 ai.metaData.putInt(ShortcutParser.METADATA_KEY, activities.get(cn));
Makoto Onukiee6b6e42016-06-29 17:34:02 -07001203 ai.applicationInfo = ret.applicationInfo;
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001204 list.add(ai);
1205 }
1206 ret.activities = list.toArray(new ActivityInfo[list.size()]);
1207 }
1208 return ret;
1209 }
1210
1211 protected XmlResourceParser injectXmlMetaData(ActivityInfo activityInfo, String key) {
1212 if (!ShortcutParser.METADATA_KEY.equals(key) || activityInfo.metaData == null) {
1213 return null;
1214 }
1215 final int resId = activityInfo.metaData.getInt(key);
1216 return getTestContext().getResources().getXml(resId);
1217 }
1218
1219 /** Replace the current calling package */
1220 protected void setCaller(String packageName, int userId) {
1221 mInjectedClientPackage = packageName;
1222 mInjectedCallingUid =
1223 Preconditions.checkNotNull(getInjectedPackageInfo(packageName, userId, false),
1224 "Unknown package").applicationInfo.uid;
1225
1226 // Set up LauncherApps for this caller.
1227 final Pair<Integer, String> key = Pair.create(userId, packageName);
1228 if (!mLauncherAppsMap.containsKey(key)) {
1229 mLauncherAppsMap.put(key, new LauncherAppsTestable(mClientContext, mLauncherAppImpl));
1230 }
1231 mLauncherApps = mLauncherAppsMap.get(key);
1232 }
1233
1234 protected void setCaller(String packageName) {
1235 setCaller(packageName, UserHandle.USER_SYSTEM);
1236 }
1237
1238 protected String getCallingPackage() {
1239 return mInjectedClientPackage;
1240 }
1241
Makoto Onuki2d895c32016-12-02 15:48:40 -08001242 /**
Makoto Onuki7d0fa812018-02-21 11:24:43 -08001243 * This controls {@link ShortcutService#hasShortcutHostPermission}, but
Makoto Onuki2d895c32016-12-02 15:48:40 -08001244 * not {@link ShortcutService#getDefaultLauncher(int)}. To control the later, use
1245 * {@link #setDefaultLauncher(int, ComponentName)}.
1246 */
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001247 protected void setDefaultLauncherChecker(BiPredicate<String, Integer> p) {
1248 mDefaultLauncherChecker = p;
1249 }
1250
Makoto Onuki2d895c32016-12-02 15:48:40 -08001251 /**
1252 * Set the default launcher. This will update {@link #mDefaultLauncherChecker} set by
1253 * {@link #setDefaultLauncherChecker} too.
1254 */
1255 protected void setDefaultLauncher(int userId, ComponentName launcherActivity) {
1256 mDefaultLauncher.put(userId, launcherActivity);
1257
1258 final BiPredicate<String, Integer> oldChecker = mDefaultLauncherChecker;
1259 mDefaultLauncherChecker = (checkPackageName, checkUserId) -> {
1260 if ((checkUserId == userId) && (launcherActivity != null)) {
1261 return launcherActivity.getPackageName().equals(checkPackageName);
1262 }
1263 return oldChecker.test(checkPackageName, checkUserId);
1264 };
1265 }
1266
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001267 protected void runWithCaller(String packageName, int userId, Runnable r) {
1268 final String previousPackage = mInjectedClientPackage;
1269 final int previousUserId = UserHandle.getUserId(mInjectedCallingUid);
1270
1271 setCaller(packageName, userId);
1272
1273 r.run();
1274
1275 setCaller(previousPackage, previousUserId);
1276 }
1277
Makoto Onuki157b1622016-06-02 16:13:10 -07001278 protected void runWithSystemUid(Runnable r) {
1279 final int origUid = mInjectedCallingUid;
1280 mInjectedCallingUid = Process.SYSTEM_UID;
1281 r.run();
1282 mInjectedCallingUid = origUid;
1283 }
1284
1285 protected void lookupAndFillInResourceNames(ShortcutInfo si) {
1286 runWithSystemUid(() -> si.lookupAndFillInResourceNames(
1287 mService.injectGetResourcesForApplicationAsUser(si.getPackage(), si.getUserId())));
1288 }
1289
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001290 protected int getCallingUserId() {
1291 return UserHandle.getUserId(mInjectedCallingUid);
1292 }
1293
1294 protected UserHandle getCallingUser() {
1295 return UserHandle.of(getCallingUserId());
1296 }
1297
1298 /** For debugging */
1299 protected void dumpsysOnLogcat() {
1300 dumpsysOnLogcat("");
1301 }
1302
1303 protected void dumpsysOnLogcat(String message) {
1304 dumpsysOnLogcat(message, false);
1305 }
1306
1307 protected void dumpsysOnLogcat(String message, boolean force) {
1308 if (force || !ENABLE_DUMP) return;
1309
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001310 Log.v(TAG, "Dumping ShortcutService: " + message);
Makoto Onuki50a320e2017-05-31 14:38:42 -07001311 for (String line : dumpsys("-u").split("\n")) {
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001312 Log.v(TAG, line);
1313 }
1314 }
1315
Makoto Onuki76269922016-07-15 14:58:54 -07001316 protected String dumpCheckin() {
Makoto Onuki50a320e2017-05-31 14:38:42 -07001317 return dumpsys("--checkin");
Makoto Onuki76269922016-07-15 14:58:54 -07001318 }
1319
Makoto Onuki50a320e2017-05-31 14:38:42 -07001320 protected String dumpsys(String... args) {
Makoto Onuki76269922016-07-15 14:58:54 -07001321 final ArrayList<String> origPermissions = new ArrayList<>(mCallerPermissions);
1322 mCallerPermissions.add(android.Manifest.permission.DUMP);
1323 try {
1324 final ByteArrayOutputStream out = new ByteArrayOutputStream();
1325 final PrintWriter pw = new PrintWriter(out);
Makoto Onukic4361e32017-04-03 11:24:25 -07001326 mService.dumpNoCheck(/* fd */ null, pw, args);
Makoto Onuki76269922016-07-15 14:58:54 -07001327 pw.close();
1328
1329 return out.toString();
1330 } finally {
1331 mCallerPermissions.clear();
1332 mCallerPermissions.addAll(origPermissions);
1333 }
1334 }
1335
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001336 /**
1337 * For debugging, dump arbitrary file on logcat.
1338 */
1339 protected void dumpFileOnLogcat(String path) {
1340 dumpFileOnLogcat(path, "");
1341 }
1342
1343 protected void dumpFileOnLogcat(String path, String message) {
1344 if (!ENABLE_DUMP) return;
1345
1346 Log.v(TAG, "Dumping file: " + path + " " + message);
1347 final StringBuilder sb = new StringBuilder();
1348 try (BufferedReader br = new BufferedReader(new FileReader(path))) {
1349 String line;
1350 while ((line = br.readLine()) != null) {
1351 Log.v(TAG, line);
1352 }
1353 } catch (Exception e) {
1354 Log.e(TAG, "Couldn't read file", e);
1355 fail("Exception " + e);
1356 }
1357 }
1358
1359 /**
1360 * For debugging, dump the main state file on logcat.
1361 */
1362 protected void dumpBaseStateFile() {
1363 mService.saveDirtyInfo();
1364 dumpFileOnLogcat(mInjectedFilePathRoot.getAbsolutePath()
1365 + "/system/" + ShortcutService.FILENAME_BASE_STATE);
1366 }
1367
1368 /**
1369 * For debugging, dump per-user state file on logcat.
1370 */
1371 protected void dumpUserFile(int userId) {
1372 dumpUserFile(userId, "");
1373 }
1374
1375 protected void dumpUserFile(int userId, String message) {
1376 mService.saveDirtyInfo();
1377 dumpFileOnLogcat(mInjectedFilePathRoot.getAbsolutePath()
1378 + "/user-" + userId
1379 + "/" + ShortcutService.FILENAME_USER_PACKAGES, message);
1380 }
1381
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001382 /**
Makoto Onukia01f4f02016-12-15 15:58:41 -08001383 * Make a shortcut with an ID only.
1384 */
1385 protected ShortcutInfo makeShortcutIdOnly(String id) {
1386 return new ShortcutInfo.Builder(mClientContext, id).build();
1387 }
1388
1389 /**
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001390 * Make a shortcut with an ID.
1391 */
1392 protected ShortcutInfo makeShortcut(String id) {
1393 return makeShortcut(
1394 id, "Title-" + id, /* activity =*/ null, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001395 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001396 }
1397
Makoto Onukia01f4f02016-12-15 15:58:41 -08001398 @Deprecated // Title was renamed to short label.
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001399 protected ShortcutInfo makeShortcutWithTitle(String id, String title) {
1400 return makeShortcut(
1401 id, title, /* activity =*/ null, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001402 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001403 }
1404
Makoto Onukia01f4f02016-12-15 15:58:41 -08001405 protected ShortcutInfo makeShortcutWithShortLabel(String id, String shortLabel) {
1406 return makeShortcut(
1407 id, shortLabel, /* activity =*/ null, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001408 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onukia01f4f02016-12-15 15:58:41 -08001409 }
1410
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001411 /**
1412 * Make a shortcut with an ID and timestamp.
1413 */
1414 protected ShortcutInfo makeShortcutWithTimestamp(String id, long timestamp) {
1415 final ShortcutInfo s = makeShortcut(
1416 id, "Title-" + id, /* activity =*/ null, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001417 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001418 s.setTimestamp(timestamp);
1419 return s;
1420 }
1421
1422 /**
1423 * Make a shortcut with an ID, a timestamp and an activity component
1424 */
1425 protected ShortcutInfo makeShortcutWithTimestampWithActivity(String id, long timestamp,
1426 ComponentName activity) {
1427 final ShortcutInfo s = makeShortcut(
1428 id, "Title-" + id, activity, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001429 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001430 s.setTimestamp(timestamp);
1431 return s;
1432 }
1433
1434 /**
1435 * Make a shortcut with an ID and icon.
1436 */
1437 protected ShortcutInfo makeShortcutWithIcon(String id, Icon icon) {
1438 return makeShortcut(
1439 id, "Title-" + id, /* activity =*/ null, icon,
Makoto Onuki99302b52017-03-29 12:42:26 -07001440 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001441 }
1442
1443 protected ShortcutInfo makePackageShortcut(String packageName, String id) {
1444 String origCaller = getCallingPackage();
1445
1446 setCaller(packageName);
1447 ShortcutInfo s = makeShortcut(
1448 id, "Title-" + id, /* activity =*/ null, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001449 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001450 setCaller(origCaller); // restore the caller
1451
1452 return s;
1453 }
1454
1455 /**
1456 * Make multiple shortcuts with IDs.
1457 */
1458 protected List<ShortcutInfo> makeShortcuts(String... ids) {
1459 final ArrayList<ShortcutInfo> ret = new ArrayList();
1460 for (String id : ids) {
1461 ret.add(makeShortcut(id));
1462 }
1463 return ret;
1464 }
1465
1466 protected ShortcutInfo.Builder makeShortcutBuilder() {
1467 return new ShortcutInfo.Builder(mClientContext);
1468 }
1469
1470 protected ShortcutInfo makeShortcutWithActivity(String id, ComponentName activity) {
1471 return makeShortcut(
1472 id, "Title-" + id, activity, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001473 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001474 }
1475
Makoto Onukib5a012f2016-06-21 11:13:53 -07001476 protected ShortcutInfo makeShortcutWithIntent(String id, Intent intent) {
1477 return makeShortcut(
1478 id, "Title-" + id, /* activity =*/ null, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001479 intent, /* rank =*/ 0);
Makoto Onukib5a012f2016-06-21 11:13:53 -07001480 }
1481
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001482 protected ShortcutInfo makeShortcutWithActivityAndTitle(String id, ComponentName activity,
1483 String title) {
1484 return makeShortcut(
1485 id, title, activity, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001486 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001487 }
1488
1489 protected ShortcutInfo makeShortcutWithActivityAndRank(String id, ComponentName activity,
1490 int rank) {
1491 return makeShortcut(
1492 id, "Title-" + id, activity, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001493 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), rank);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001494 }
1495
1496 /**
1497 * Make a shortcut with details.
1498 */
1499 protected ShortcutInfo makeShortcut(String id, String title, ComponentName activity,
Makoto Onuki99302b52017-03-29 12:42:26 -07001500 Icon icon, Intent intent, int rank) {
Makoto Onukib5a012f2016-06-21 11:13:53 -07001501 final ShortcutInfo.Builder b = new ShortcutInfo.Builder(mClientContext, id)
Makoto Onuki255461f2017-01-10 11:47:25 -08001502 .setActivity(new ComponentName(mClientContext.getPackageName(), "main"))
Makoto Onukib5a012f2016-06-21 11:13:53 -07001503 .setShortLabel(title)
Makoto Onuki99302b52017-03-29 12:42:26 -07001504 .setRank(rank)
1505 .setIntent(intent);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001506 if (icon != null) {
1507 b.setIcon(icon);
1508 }
1509 if (activity != null) {
1510 b.setActivity(activity);
1511 }
1512 final ShortcutInfo s = b.build();
1513
1514 s.setTimestamp(mInjectedCurrentTimeMillis); // HACK
1515
1516 return s;
1517 }
1518
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001519 protected ShortcutInfo makeShortcutWithIntents(String id, Intent... intents) {
1520 return makeShortcut(
1521 id, "Title-" + id, /* activity =*/ null, /* icon =*/ null,
1522 intents, /* rank =*/ 0);
1523 }
1524
1525 /**
1526 * Make a shortcut with details.
1527 */
1528 protected ShortcutInfo makeShortcut(String id, String title, ComponentName activity,
1529 Icon icon, Intent[] intents, int rank) {
1530 final ShortcutInfo.Builder b = new ShortcutInfo.Builder(mClientContext, id)
Makoto Onuki255461f2017-01-10 11:47:25 -08001531 .setActivity(new ComponentName(mClientContext.getPackageName(), "main"))
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001532 .setShortLabel(title)
1533 .setRank(rank)
1534 .setIntents(intents);
1535 if (icon != null) {
1536 b.setIcon(icon);
1537 }
1538 if (activity != null) {
1539 b.setActivity(activity);
1540 }
1541 final ShortcutInfo s = b.build();
1542
1543 s.setTimestamp(mInjectedCurrentTimeMillis); // HACK
1544
1545 return s;
1546 }
1547
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001548 /**
Makoto Onukib5a012f2016-06-21 11:13:53 -07001549 * Make a shortcut with details.
1550 */
1551 protected ShortcutInfo makeShortcutWithExtras(String id, Intent intent,
1552 PersistableBundle extras) {
1553 final ShortcutInfo.Builder b = new ShortcutInfo.Builder(mClientContext, id)
Makoto Onuki255461f2017-01-10 11:47:25 -08001554 .setActivity(new ComponentName(mClientContext.getPackageName(), "main"))
Makoto Onukib5a012f2016-06-21 11:13:53 -07001555 .setShortLabel("title-" + id)
1556 .setExtras(extras)
1557 .setIntent(intent);
1558 final ShortcutInfo s = b.build();
1559
1560 s.setTimestamp(mInjectedCurrentTimeMillis); // HACK
1561
1562 return s;
1563 }
1564
1565 /**
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001566 * Make an intent.
1567 */
1568 protected Intent makeIntent(String action, Class<?> clazz, Object... bundleKeysAndValues) {
1569 final Intent intent = new Intent(action);
1570 intent.setComponent(makeComponent(clazz));
1571 intent.replaceExtras(makeBundle(bundleKeysAndValues));
1572 return intent;
1573 }
1574
1575 /**
1576 * Make an component name, with the client context.
1577 */
1578 @NonNull
1579 protected ComponentName makeComponent(Class<?> clazz) {
1580 return new ComponentName(mClientContext, clazz);
1581 }
1582
1583 @NonNull
1584 protected ShortcutInfo findById(List<ShortcutInfo> list, String id) {
1585 for (ShortcutInfo s : list) {
1586 if (s.getId().equals(id)) {
1587 return s;
1588 }
1589 }
1590 fail("Shortcut with id " + id + " not found");
1591 return null;
1592 }
1593
1594 protected void assertSystem() {
1595 assertEquals("Caller must be system", Process.SYSTEM_UID, mInjectedCallingUid);
1596 }
1597
1598 protected void assertResetTimes(long expectedLastResetTime, long expectedNextResetTime) {
1599 assertEquals(expectedLastResetTime, mService.getLastResetTimeLocked());
1600 assertEquals(expectedNextResetTime, mService.getNextResetTimeLocked());
1601 }
1602
1603 public static List<ShortcutInfo> assertAllNotHaveIcon(
1604 List<ShortcutInfo> actualShortcuts) {
1605 for (ShortcutInfo s : actualShortcuts) {
1606 assertNull("ID " + s.getId(), s.getIcon());
1607 }
1608 return actualShortcuts;
1609 }
1610
1611 @NonNull
1612 protected List<ShortcutInfo> assertAllHaveFlags(@NonNull List<ShortcutInfo> actualShortcuts,
1613 int shortcutFlags) {
1614 for (ShortcutInfo s : actualShortcuts) {
1615 assertTrue("ID " + s.getId() + " doesn't have flags " + shortcutFlags,
1616 s.hasFlags(shortcutFlags));
1617 }
1618 return actualShortcuts;
1619 }
1620
1621 protected ShortcutInfo getPackageShortcut(String packageName, String shortcutId, int userId) {
1622 return mService.getPackageShortcutForTest(packageName, shortcutId, userId);
1623 }
1624
1625 protected void assertShortcutExists(String packageName, String shortcutId, int userId) {
1626 assertTrue(getPackageShortcut(packageName, shortcutId, userId) != null);
1627 }
1628
1629 protected void assertShortcutNotExists(String packageName, String shortcutId, int userId) {
1630 assertTrue(getPackageShortcut(packageName, shortcutId, userId) == null);
1631 }
1632
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001633 protected Intent[] launchShortcutAndGetIntentsInner(Runnable shortcutStarter,
1634 @NonNull String packageName, @NonNull String shortcutId, int userId) {
1635 reset(mMockActivityManagerInternal);
1636 shortcutStarter.run();
1637
1638 final ArgumentCaptor<Intent[]> intentsCaptor = ArgumentCaptor.forClass(Intent[].class);
1639 verify(mMockActivityManagerInternal).startActivitiesAsPackage(
1640 eq(packageName),
1641 eq(userId),
1642 intentsCaptor.capture(),
Makoto Onukid0010c52017-03-30 14:17:35 -07001643 anyOrNull(Bundle.class));
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001644 return intentsCaptor.getValue();
1645 }
1646
1647 protected Intent[] launchShortcutAndGetIntents(
1648 @NonNull String packageName, @NonNull String shortcutId, int userId) {
1649 return launchShortcutAndGetIntentsInner(
1650 () -> {
1651 mLauncherApps.startShortcut(packageName, shortcutId, null, null,
1652 UserHandle.of(userId));
1653 }, packageName, shortcutId, userId
1654 );
1655 }
1656
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001657 protected Intent launchShortcutAndGetIntent(
1658 @NonNull String packageName, @NonNull String shortcutId, int userId) {
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001659 final Intent[] intents = launchShortcutAndGetIntents(packageName, shortcutId, userId);
1660 assertEquals(1, intents.length);
1661 return intents[0];
1662 }
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001663
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001664 protected Intent[] launchShortcutAndGetIntents_withShortcutInfo(
1665 @NonNull String packageName, @NonNull String shortcutId, int userId) {
1666 return launchShortcutAndGetIntentsInner(
1667 () -> {
1668 mLauncherApps.startShortcut(
1669 getShortcutInfoAsLauncher(packageName, shortcutId, userId), null, null);
1670 }, packageName, shortcutId, userId
1671 );
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001672 }
1673
1674 protected Intent launchShortcutAndGetIntent_withShortcutInfo(
1675 @NonNull String packageName, @NonNull String shortcutId, int userId) {
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001676 final Intent[] intents = launchShortcutAndGetIntents_withShortcutInfo(
1677 packageName, shortcutId, userId);
1678 assertEquals(1, intents.length);
1679 return intents[0];
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001680 }
1681
1682 protected void assertShortcutLaunchable(@NonNull String packageName, @NonNull String shortcutId,
1683 int userId) {
1684 assertNotNull(launchShortcutAndGetIntent(packageName, shortcutId, userId));
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001685 }
1686
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001687 protected void assertShortcutNotLaunched(@NonNull String packageName,
1688 @NonNull String shortcutId, int userId) {
1689 reset(mMockActivityManagerInternal);
1690 try {
1691 mLauncherApps.startShortcut(packageName, shortcutId, null, null,
1692 UserHandle.of(userId));
1693 fail("ActivityNotFoundException was not thrown");
1694 } catch (ActivityNotFoundException expected) {
1695 }
1696 // This shouldn't have been called.
1697 verify(mMockActivityManagerInternal, times(0)).startActivitiesAsPackage(
1698 anyString(),
1699 anyInt(),
1700 any(Intent[].class),
Makoto Onukid0010c52017-03-30 14:17:35 -07001701 anyOrNull(Bundle.class));
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001702 }
1703
1704 protected void assertStartShortcutThrowsException(@NonNull String packageName,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07001705 @NonNull String shortcutId, int userId, Class<?> expectedException) {
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07001706 Exception thrown = null;
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001707 try {
Makoto Onukid6880792016-06-29 13:37:43 -07001708 mLauncherApps.startShortcut(packageName, shortcutId, null, null,
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001709 UserHandle.of(userId));
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07001710 } catch (Exception e) {
1711 thrown = e;
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001712 }
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07001713 assertNotNull("Exception was not thrown", thrown);
1714 assertEquals("Exception type different", expectedException, thrown.getClass());
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001715 }
1716
1717 protected void assertBitmapDirectories(int userId, String... expectedDirectories) {
1718 final Set<String> expected = hashSet(set(expectedDirectories));
1719
1720 final Set<String> actual = new HashSet<>();
1721
1722 final File[] files = mService.getUserBitmapFilePath(userId).listFiles();
1723 if (files != null) {
1724 for (File child : files) {
1725 if (child.isDirectory()) {
1726 actual.add(child.getName());
1727 }
1728 }
1729 }
1730
1731 assertEquals(expected, actual);
1732 }
1733
1734 protected void assertBitmapFiles(int userId, String packageName, String... expectedFiles) {
1735 final Set<String> expected = hashSet(set(expectedFiles));
1736
1737 final Set<String> actual = new HashSet<>();
1738
1739 final File[] files = new File(mService.getUserBitmapFilePath(userId), packageName)
1740 .listFiles();
1741 if (files != null) {
1742 for (File child : files) {
1743 if (child.isFile()) {
1744 actual.add(child.getName());
1745 }
1746 }
1747 }
1748
1749 assertEquals(expected, actual);
1750 }
1751
1752 protected String getBitmapFilename(int userId, String packageName, String shortcutId) {
1753 final ShortcutInfo si = mService.getPackageShortcutForTest(packageName, shortcutId, userId);
1754 if (si == null) {
1755 return null;
1756 }
Makoto Onuki475c3652017-05-08 14:29:03 -07001757 mService.waitForBitmapSavesForTest();
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001758 return new File(si.getBitmapPath()).getName();
1759 }
1760
Makoto Onuki475c3652017-05-08 14:29:03 -07001761 protected String getBitmapAbsPath(int userId, String packageName, String shortcutId) {
1762 final ShortcutInfo si = mService.getPackageShortcutForTest(packageName, shortcutId, userId);
1763 if (si == null) {
1764 return null;
1765 }
1766 mService.waitForBitmapSavesForTest();
1767 return new File(si.getBitmapPath()).getAbsolutePath();
1768 }
1769
Makoto Onukif3ba2e02016-07-12 09:18:50 -07001770 /**
1771 * @return all shortcuts stored internally for the caller. This reflects the *internal* view
1772 * of shortcuts, which may be different from what {@link #getCallerVisibleShortcuts} would
1773 * return, because getCallerVisibleShortcuts() will get shortcuts from the proper "front door"
1774 * which performs some extra checks, like {@link ShortcutPackage#onRestored}.
1775 */
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001776 protected List<ShortcutInfo> getCallerShortcuts() {
1777 final ShortcutPackage p = mService.getPackageShortcutForTest(
1778 getCallingPackage(), getCallingUserId());
1779 return p == null ? null : p.getAllShortcutsForTest();
1780 }
1781
Makoto Onukif3ba2e02016-07-12 09:18:50 -07001782 /**
1783 * @return all shortcuts owned by caller that are actually visible via ShortcutManager.
1784 * See also {@link #getCallerShortcuts}.
1785 */
1786 protected List<ShortcutInfo> getCallerVisibleShortcuts() {
1787 final ArrayList<ShortcutInfo> ret = new ArrayList<>();
1788 ret.addAll(mManager.getDynamicShortcuts());
1789 ret.addAll(mManager.getPinnedShortcuts());
1790 ret.addAll(mManager.getManifestShortcuts());
1791 return ret;
1792 }
1793
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001794 protected ShortcutInfo getCallerShortcut(String shortcutId) {
1795 return getPackageShortcut(getCallingPackage(), shortcutId, getCallingUserId());
1796 }
1797
1798 protected List<ShortcutInfo> getLauncherShortcuts(String launcher, int userId, int queryFlags) {
1799 final List<ShortcutInfo>[] ret = new List[1];
1800 runWithCaller(launcher, userId, () -> {
1801 final ShortcutQuery q = new ShortcutQuery();
1802 q.setQueryFlags(queryFlags);
1803 ret[0] = mLauncherApps.getShortcuts(q, UserHandle.of(userId));
1804 });
1805 return ret[0];
1806 }
1807
1808 protected List<ShortcutInfo> getLauncherPinnedShortcuts(String launcher, int userId) {
1809 return getLauncherShortcuts(launcher, userId, ShortcutQuery.FLAG_GET_PINNED);
1810 }
1811
Makoto Onukia01f4f02016-12-15 15:58:41 -08001812 protected List<ShortcutInfo> getShortcutAsLauncher(int targetUserId) {
1813 final ShortcutQuery q = new ShortcutQuery();
1814 q.setQueryFlags(ShortcutQuery.FLAG_MATCH_DYNAMIC | ShortcutQuery.FLAG_MATCH_DYNAMIC
1815 | ShortcutQuery.FLAG_MATCH_PINNED);
1816 return mLauncherApps.getShortcuts(q, UserHandle.of(targetUserId));
1817 }
1818
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001819 protected ShortcutInfo getShortcutInfoAsLauncher(String packageName, String shortcutId,
1820 int userId) {
1821 final List<ShortcutInfo> infoList =
1822 mLauncherApps.getShortcutInfo(packageName, list(shortcutId),
1823 UserHandle.of(userId));
1824 assertEquals("No shortcutInfo found (or too many of them)", 1, infoList.size());
1825 return infoList.get(0);
1826 }
1827
Makoto Onukiee6b6e42016-06-29 17:34:02 -07001828 protected Intent genPackageAddIntent(String packageName, int userId) {
1829 installPackage(userId, packageName);
1830
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001831 Intent i = new Intent(Intent.ACTION_PACKAGE_ADDED);
Makoto Onukiee6b6e42016-06-29 17:34:02 -07001832 i.setData(Uri.parse("package:" + packageName));
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001833 i.putExtra(Intent.EXTRA_USER_HANDLE, userId);
1834 return i;
1835 }
1836
1837 protected Intent genPackageDeleteIntent(String pakcageName, int userId) {
Makoto Onukiee6b6e42016-06-29 17:34:02 -07001838 uninstallPackage(userId, pakcageName);
1839
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001840 Intent i = new Intent(Intent.ACTION_PACKAGE_REMOVED);
1841 i.setData(Uri.parse("package:" + pakcageName));
1842 i.putExtra(Intent.EXTRA_USER_HANDLE, userId);
1843 return i;
1844 }
1845
1846 protected Intent genPackageUpdateIntent(String pakcageName, int userId) {
Makoto Onukiee6b6e42016-06-29 17:34:02 -07001847 installPackage(userId, pakcageName);
1848
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001849 Intent i = new Intent(Intent.ACTION_PACKAGE_ADDED);
1850 i.setData(Uri.parse("package:" + pakcageName));
1851 i.putExtra(Intent.EXTRA_USER_HANDLE, userId);
1852 i.putExtra(Intent.EXTRA_REPLACING, true);
1853 return i;
1854 }
1855
Makoto Onukib08790c2016-06-23 14:05:46 -07001856 protected Intent genPackageChangedIntent(String pakcageName, int userId) {
1857 Intent i = new Intent(Intent.ACTION_PACKAGE_CHANGED);
1858 i.setData(Uri.parse("package:" + pakcageName));
1859 i.putExtra(Intent.EXTRA_USER_HANDLE, userId);
1860 return i;
1861 }
1862
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001863 protected Intent genPackageDataClear(String packageName, int userId) {
1864 Intent i = new Intent(Intent.ACTION_PACKAGE_DATA_CLEARED);
1865 i.setData(Uri.parse("package:" + packageName));
1866 i.putExtra(Intent.EXTRA_USER_HANDLE, userId);
1867 return i;
1868 }
1869
1870 protected void assertExistsAndShadow(ShortcutPackageItem spi) {
1871 assertNotNull(spi);
1872 assertTrue(spi.getPackageInfo().isShadow());
1873 }
1874
1875 protected File makeFile(File baseDirectory, String... paths) {
1876 File ret = baseDirectory;
1877
1878 for (String path : paths) {
1879 ret = new File(ret, path);
1880 }
1881
1882 return ret;
1883 }
1884
1885 protected boolean bitmapDirectoryExists(String packageName, int userId) {
Makoto Onuki475c3652017-05-08 14:29:03 -07001886 mService.waitForBitmapSavesForTest();
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001887 final File path = new File(mService.getUserBitmapFilePath(userId), packageName);
1888 return path.isDirectory();
1889 }
1890 protected static ShortcutQuery buildQuery(long changedSince,
1891 String packageName, ComponentName componentName,
1892 /* @ShortcutQuery.QueryFlags */ int flags) {
1893 return buildQuery(changedSince, packageName, null, componentName, flags);
1894 }
1895
1896 protected static ShortcutQuery buildQuery(long changedSince,
1897 String packageName, List<String> shortcutIds, ComponentName componentName,
1898 /* @ShortcutQuery.QueryFlags */ int flags) {
1899 final ShortcutQuery q = new ShortcutQuery();
1900 q.setChangedSince(changedSince);
1901 q.setPackage(packageName);
1902 q.setShortcutIds(shortcutIds);
1903 q.setActivity(componentName);
1904 q.setQueryFlags(flags);
1905 return q;
1906 }
1907
1908 protected static ShortcutQuery buildAllQuery(String packageName) {
1909 final ShortcutQuery q = new ShortcutQuery();
1910 q.setPackage(packageName);
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07001911 q.setQueryFlags(ShortcutQuery.FLAG_GET_ALL_KINDS);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001912 return q;
1913 }
1914
1915 protected static ShortcutQuery buildPinnedQuery(String packageName) {
1916 final ShortcutQuery q = new ShortcutQuery();
1917 q.setPackage(packageName);
1918 q.setQueryFlags(ShortcutQuery.FLAG_GET_PINNED);
1919 return q;
1920 }
1921
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07001922 protected static ShortcutQuery buildQueryWithFlags(int queryFlags) {
1923 final ShortcutQuery q = new ShortcutQuery();
1924 q.setQueryFlags(queryFlags);
1925 return q;
1926 }
1927
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001928 protected void backupAndRestore() {
1929 int prevUid = mInjectedCallingUid;
1930
1931 mInjectedCallingUid = Process.SYSTEM_UID; // Only system can call it.
1932
1933 dumpsysOnLogcat("Before backup");
1934
1935 final byte[] payload = mService.getBackupPayload(USER_0);
1936 if (ENABLE_DUMP) {
1937 final String xml = new String(payload);
1938 Log.v(TAG, "Backup payload:");
1939 for (String line : xml.split("\n")) {
1940 Log.v(TAG, line);
1941 }
1942 }
1943
1944 // Before doing anything else, uninstall all packages.
1945 for (int userId : list(USER_0, USER_P0)) {
1946 for (String pkg : list(CALLING_PACKAGE_1, CALLING_PACKAGE_2, CALLING_PACKAGE_3,
1947 LAUNCHER_1, LAUNCHER_2, LAUNCHER_3)) {
1948 uninstallPackage(userId, pkg);
1949 }
1950 }
1951
1952 shutdownServices();
1953
1954 deleteAllSavedFiles();
1955
1956 initService();
1957 mService.applyRestore(payload, USER_0);
1958
1959 // handleUnlockUser will perform the gone package check, but it shouldn't remove
1960 // shadow information.
1961 mService.handleUnlockUser(USER_0);
1962
1963 dumpsysOnLogcat("After restore");
1964
1965 mInjectedCallingUid = prevUid;
1966 }
1967
1968 protected void prepareCrossProfileDataSet() {
Makoto Onuki9c850012016-07-26 15:50:50 -07001969 mRunningUsers.put(USER_10, true); // this test needs user 10.
1970
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001971 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1972 assertTrue(mManager.setDynamicShortcuts(list(
1973 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"),
1974 makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6"))));
1975 });
1976 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
1977 assertTrue(mManager.setDynamicShortcuts(list(
1978 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"),
1979 makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6"))));
1980 });
1981 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
1982 assertTrue(mManager.setDynamicShortcuts(list(
1983 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"),
1984 makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6"))));
1985 });
1986 runWithCaller(CALLING_PACKAGE_4, USER_0, () -> {
1987 assertTrue(mManager.setDynamicShortcuts(list()));
1988 });
1989 runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
1990 assertTrue(mManager.setDynamicShortcuts(list(
1991 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"),
1992 makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6"))));
1993 });
1994 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
1995 assertTrue(mManager.setDynamicShortcuts(list(
1996 makeShortcut("x1"), makeShortcut("x2"), makeShortcut("x3"),
1997 makeShortcut("x4"), makeShortcut("x5"), makeShortcut("x6"))));
1998 });
1999
2000 runWithCaller(LAUNCHER_1, USER_0, () -> {
2001 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s1"), HANDLE_USER_0);
2002 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s1", "s2"), HANDLE_USER_0);
2003 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("s1", "s2", "s3"), HANDLE_USER_0);
2004
2005 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s1", "s4"), HANDLE_USER_P0);
2006 });
2007 runWithCaller(LAUNCHER_2, USER_0, () -> {
2008 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s2"), HANDLE_USER_0);
2009 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s2", "s3"), HANDLE_USER_0);
2010 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("s2", "s3", "s4"), HANDLE_USER_0);
2011
2012 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s2", "s5"), HANDLE_USER_P0);
2013 });
Makoto Onukif3ba2e02016-07-12 09:18:50 -07002014
2015 // Note LAUNCHER_3 has allowBackup=false.
Makoto Onuki51ab2b32016-06-02 11:03:51 -07002016 runWithCaller(LAUNCHER_3, USER_0, () -> {
2017 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s3"), HANDLE_USER_0);
2018 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s3", "s4"), HANDLE_USER_0);
2019 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("s3", "s4", "s5"), HANDLE_USER_0);
2020
2021 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s3", "s6"), HANDLE_USER_P0);
2022 });
2023 runWithCaller(LAUNCHER_4, USER_0, () -> {
2024 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list(), HANDLE_USER_0);
2025 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list(), HANDLE_USER_0);
2026 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list(), HANDLE_USER_0);
2027 mLauncherApps.pinShortcuts(CALLING_PACKAGE_4, list(), HANDLE_USER_0);
2028 });
2029
2030 // Launcher on a managed profile is referring ot user 0!
2031 runWithCaller(LAUNCHER_1, USER_P0, () -> {
2032 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s3", "s4"), HANDLE_USER_0);
2033 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s3", "s4", "s5"), HANDLE_USER_0);
2034 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("s3", "s4", "s5", "s6"),
2035 HANDLE_USER_0);
2036
2037 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s4", "s1"), HANDLE_USER_P0);
2038 });
2039 runWithCaller(LAUNCHER_1, USER_10, () -> {
2040 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("x4", "x5"), HANDLE_USER_10);
2041 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("x4", "x5", "x6"), HANDLE_USER_10);
2042 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("x4", "x5", "x6", "x1"),
2043 HANDLE_USER_10);
2044 });
2045
2046 // Then remove some dynamic shortcuts.
2047 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2048 assertTrue(mManager.setDynamicShortcuts(list(
2049 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2050 });
2051 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
2052 assertTrue(mManager.setDynamicShortcuts(list(
2053 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2054 });
2055 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
2056 assertTrue(mManager.setDynamicShortcuts(list(
2057 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2058 });
2059 runWithCaller(CALLING_PACKAGE_4, USER_0, () -> {
2060 assertTrue(mManager.setDynamicShortcuts(list()));
2061 });
2062 runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
2063 assertTrue(mManager.setDynamicShortcuts(list(
2064 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2065 });
2066 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
2067 assertTrue(mManager.setDynamicShortcuts(list(
2068 makeShortcut("x1"), makeShortcut("x2"), makeShortcut("x3"))));
2069 });
2070 }
Makoto Onukib5a012f2016-06-21 11:13:53 -07002071
2072 public static List<ShortcutInfo> assertAllHaveIconResId(
2073 List<ShortcutInfo> actualShortcuts) {
2074 for (ShortcutInfo s : actualShortcuts) {
2075 assertTrue("ID " + s.getId() + " not have icon res ID", s.hasIconResource());
2076 assertFalse("ID " + s.getId() + " shouldn't have icon FD", s.hasIconFile());
2077 }
2078 return actualShortcuts;
2079 }
2080
2081 public static List<ShortcutInfo> assertAllHaveIconFile(
2082 List<ShortcutInfo> actualShortcuts) {
2083 for (ShortcutInfo s : actualShortcuts) {
2084 assertFalse("ID " + s.getId() + " shouldn't have icon res ID", s.hasIconResource());
2085 assertTrue("ID " + s.getId() + " not have icon FD", s.hasIconFile());
2086 }
2087 return actualShortcuts;
2088 }
2089
2090 public static List<ShortcutInfo> assertAllHaveIcon(
2091 List<ShortcutInfo> actualShortcuts) {
2092 for (ShortcutInfo s : actualShortcuts) {
Makoto Onukia01f4f02016-12-15 15:58:41 -08002093 assertTrue("ID " + s.getId() + " has no icon ",
2094 s.hasIconFile() || s.hasIconResource() || s.getIcon() != null);
Makoto Onukib5a012f2016-06-21 11:13:53 -07002095 }
2096 return actualShortcuts;
2097 }
2098
2099 public static List<ShortcutInfo> assertAllStringsResolved(
2100 List<ShortcutInfo> actualShortcuts) {
2101 for (ShortcutInfo s : actualShortcuts) {
2102 assertTrue("ID " + s.getId(), s.hasStringResourcesResolved());
2103 }
2104 return actualShortcuts;
2105 }
Makoto Onuki76269922016-07-15 14:58:54 -07002106
2107 public String readTestAsset(String assetPath) throws IOException {
2108 final StringBuilder sb = new StringBuilder();
2109 try (BufferedReader br = new BufferedReader(
2110 new InputStreamReader(
2111 getTestContext().getResources().getAssets().open(assetPath)))) {
2112 String line;
2113 while ((line = br.readLine()) != null) {
2114 sb.append(line);
2115 sb.append(System.lineSeparator());
2116 }
2117 }
2118 return sb.toString();
2119 }
Makoto Onuki0b9d1db2016-07-18 14:16:41 -07002120
2121 protected void prepareGetHomeActivitiesAsUser(ComponentName preferred,
2122 List<ResolveInfo> candidates, int userId) {
2123 doAnswer(inv -> {
2124 ((List) inv.getArguments()[0]).addAll(candidates);
2125 return preferred;
2126 }).when(mMockPackageManagerInternal).getHomeActivitiesAsUser(any(List.class), eq(userId));
2127 }
2128
2129 protected static ComponentName cn(String packageName, String name) {
2130 return new ComponentName(packageName, name);
2131 }
2132
2133 protected static ResolveInfo ri(String packageName, String name, boolean isSystem, int priority) {
2134 final ResolveInfo ri = new ResolveInfo();
2135 ri.activityInfo = new ActivityInfo();
2136 ri.activityInfo.applicationInfo = new ApplicationInfo();
2137
2138 ri.activityInfo.packageName = packageName;
2139 ri.activityInfo.name = name;
2140 if (isSystem) {
2141 ri.activityInfo.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
2142 }
2143 ri.priority = priority;
2144 return ri;
2145 }
2146
2147 protected static ResolveInfo getSystemLauncher() {
2148 return ri(PACKAGE_SYSTEM_LAUNCHER, PACKAGE_SYSTEM_LAUNCHER_NAME, true,
2149 PACKAGE_SYSTEM_LAUNCHER_PRIORITY);
2150 }
2151
2152 protected static ResolveInfo getFallbackLauncher() {
2153 return ri(PACKAGE_FALLBACK_LAUNCHER, PACKAGE_FALLBACK_LAUNCHER_NAME, true,
2154 PACKAGE_FALLBACK_LAUNCHER_PRIORITY);
2155 }
Makoto Onukia01f4f02016-12-15 15:58:41 -08002156
Tony Maked6ef622017-12-07 16:36:16 +00002157 protected void makeUidForeground(int uid) {
Makoto Onukia01f4f02016-12-15 15:58:41 -08002158 try {
2159 mService.mUidObserver.onUidStateChanged(
Tony Maked6ef622017-12-07 16:36:16 +00002160 uid, ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE, 0);
2161 } catch (RemoteException e) {
2162 e.rethrowAsRuntimeException();
2163 }
2164 }
2165
2166 protected void makeCallerForeground() {
2167 makeUidForeground(mInjectedCallingUid);
2168 }
2169
2170 protected void makeUidBackground(int uid) {
2171 try {
2172 mService.mUidObserver.onUidStateChanged(
2173 uid, ActivityManager.PROCESS_STATE_TOP_SLEEPING, 0);
Makoto Onukia01f4f02016-12-15 15:58:41 -08002174 } catch (RemoteException e) {
2175 e.rethrowAsRuntimeException();
2176 }
2177 }
2178
2179 protected void makeCallerBackground() {
Tony Maked6ef622017-12-07 16:36:16 +00002180 makeUidBackground(mInjectedCallingUid);
Makoto Onukia01f4f02016-12-15 15:58:41 -08002181 }
2182
2183 protected void publishManifestShortcutsAsCaller(int resId) {
2184 addManifestShortcutResource(
2185 new ComponentName(getCallingPackage(), ShortcutActivity.class.getName()),
2186 resId);
2187 updatePackageVersion(getCallingPackage(), 1);
2188 mService.mPackageMonitor.onReceive(getTestContext(),
2189 genPackageAddIntent(getCallingPackage(), getCallingUserId()));
2190 }
Makoto Onuki50a320e2017-05-31 14:38:42 -07002191
2192 protected void assertFileNotExists(String path) {
2193 final File f = new File(mInjectedFilePathRoot, path);
2194 assertFalse("File shouldn't exist: " + f.getAbsolutePath(), f.exists());
2195 }
2196
2197 protected void assertFileExistsWithContent(String path) {
2198 final File f = new File(mInjectedFilePathRoot, path);
2199 assertTrue("File should exist: " + f.getAbsolutePath(), f.exists());
2200 assertTrue("File should be larger than 0b: " + f.getAbsolutePath(), f.length() > 0);
2201 }
Makoto Onuki51ab2b32016-06-02 11:03:51 -07002202}