blob: 2599fdd8ed8305ba7c9708bd22f4db6feb557ab4 [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
26import static org.mockito.Matchers.any;
Makoto Onuki157b1622016-06-02 16:13:10 -070027import static org.mockito.Matchers.anyInt;
28import static org.mockito.Matchers.anyString;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070029import static org.mockito.Matchers.eq;
30import static org.mockito.Mockito.doAnswer;
31import static org.mockito.Mockito.mock;
32import static org.mockito.Mockito.reset;
33import static org.mockito.Mockito.spy;
Makoto Onukiff14f732016-06-30 17:07:25 -070034import static org.mockito.Mockito.times;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070035import static org.mockito.Mockito.verify;
36import static org.mockito.Mockito.when;
37
38import android.annotation.NonNull;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070039import android.annotation.UserIdInt;
40import android.app.Activity;
Makoto Onuki33525d22016-08-03 15:45:24 -070041import android.app.ActivityManager;
Makoto Onukiea11db12016-06-24 15:17:44 -070042import android.app.ActivityManagerInternal;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070043import android.app.IUidObserver;
Makoto Onukiac042502016-05-20 16:39:42 -070044import android.app.usage.UsageStatsManagerInternal;
Makoto Onuki440a1ea2016-07-20 14:21:18 -070045import android.content.ActivityNotFoundException;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070046import android.content.BroadcastReceiver;
47import android.content.ComponentName;
48import android.content.Context;
49import android.content.Intent;
50import android.content.IntentFilter;
Makoto Onuki2d895c32016-12-02 15:48:40 -080051import android.content.IntentSender;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070052import android.content.pm.ActivityInfo;
53import android.content.pm.ApplicationInfo;
54import android.content.pm.ILauncherApps;
55import android.content.pm.LauncherApps;
56import android.content.pm.LauncherApps.ShortcutQuery;
57import android.content.pm.PackageInfo;
58import android.content.pm.PackageManager;
59import android.content.pm.PackageManagerInternal;
Makoto Onukib08790c2016-06-23 14:05:46 -070060import android.content.pm.ResolveInfo;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070061import android.content.pm.ShortcutInfo;
62import android.content.pm.ShortcutManager;
63import android.content.pm.ShortcutServiceInternal;
64import android.content.pm.Signature;
65import android.content.pm.UserInfo;
66import android.content.res.Resources;
67import android.content.res.XmlResourceParser;
68import android.graphics.drawable.Icon;
69import android.net.Uri;
70import android.os.Bundle;
71import android.os.FileUtils;
72import android.os.Handler;
73import android.os.Looper;
Makoto Onukib5a012f2016-06-21 11:13:53 -070074import android.os.PersistableBundle;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070075import android.os.Process;
Makoto Onukia01f4f02016-12-15 15:58:41 -080076import android.os.RemoteException;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070077import android.os.UserHandle;
78import android.os.UserManager;
79import android.test.InstrumentationTestCase;
80import android.test.mock.MockContext;
Makoto Onuki2d895c32016-12-02 15:48:40 -080081import android.util.ArrayMap;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070082import android.util.Log;
83import android.util.Pair;
84
85import com.android.internal.util.Preconditions;
86import com.android.server.LocalServices;
87import com.android.server.SystemService;
88import com.android.server.pm.LauncherAppsService.LauncherAppsImpl;
89import com.android.server.pm.ShortcutUser.PackageWithUser;
90
91import org.junit.Assert;
92import org.mockito.ArgumentCaptor;
93import org.mockito.invocation.InvocationOnMock;
94import org.mockito.stubbing.Answer;
95
96import java.io.BufferedReader;
97import java.io.ByteArrayOutputStream;
98import java.io.File;
99import java.io.FileReader;
Makoto Onuki76269922016-07-15 14:58:54 -0700100import java.io.IOException;
101import java.io.InputStreamReader;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700102import java.io.PrintWriter;
103import java.util.ArrayList;
104import java.util.HashMap;
105import java.util.HashSet;
106import java.util.LinkedHashMap;
107import java.util.List;
Makoto Onuki157b1622016-06-02 16:13:10 -0700108import java.util.Locale;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700109import java.util.Map;
110import java.util.Set;
Makoto Onukib08790c2016-06-23 14:05:46 -0700111import java.util.function.BiFunction;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700112import java.util.function.BiPredicate;
113import java.util.function.Consumer;
Makoto Onukia2241832016-07-06 13:28:37 -0700114import java.util.function.Function;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700115
116public abstract class BaseShortcutManagerTest extends InstrumentationTestCase {
117 protected static final String TAG = "ShortcutManagerTest";
118
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700119 protected static final boolean DUMP_IN_TEARDOWN = false; // DO NOT SUBMIT WITH true
120
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700121 /**
122 * Whether to enable dump or not. Should be only true when debugging to avoid bugs where
123 * dump affecting the behavior.
124 */
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700125 protected static final boolean ENABLE_DUMP = false // DO NOT SUBMIT WITH true
126 || DUMP_IN_TEARDOWN || ShortcutService.DEBUG;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700127
128 protected static final String[] EMPTY_STRINGS = new String[0]; // Just for readability.
129
Makoto Onukib08790c2016-06-23 14:05:46 -0700130 protected static final String MAIN_ACTIVITY_CLASS = "MainActivity";
Makoto Onuki2d895c32016-12-02 15:48:40 -0800131 protected static final String PIN_CONFIRM_ACTIVITY_CLASS = "PinConfirmActivity";
Makoto Onukib08790c2016-06-23 14:05:46 -0700132
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700133 // public for mockito
134 public class BaseContext extends MockContext {
135 @Override
136 public Object getSystemService(String name) {
137 switch (name) {
138 case Context.USER_SERVICE:
139 return mMockUserManager;
140 }
141 throw new UnsupportedOperationException();
142 }
143
144 @Override
145 public String getSystemServiceName(Class<?> serviceClass) {
146 return getTestContext().getSystemServiceName(serviceClass);
147 }
148
149 @Override
150 public PackageManager getPackageManager() {
151 return mMockPackageManager;
152 }
153
154 @Override
155 public Resources getResources() {
156 return getTestContext().getResources();
157 }
158
159 @Override
160 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
161 IntentFilter filter, String broadcastPermission, Handler scheduler) {
162 // ignore.
163 return null;
164 }
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700165
166 @Override
167 public void unregisterReceiver(BroadcastReceiver receiver) {
168 // ignore.
169 }
Makoto Onuki2d895c32016-12-02 15:48:40 -0800170
171 @Override
172 public void startActivityAsUser(Intent intent, UserHandle user) {
173 // ignore, use spy to intercept it.
174 }
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700175 }
176
177 /** Context used in the client side */
178 public class ClientContext extends BaseContext {
179 @Override
180 public String getPackageName() {
181 return mInjectedClientPackage;
182 }
183
184 @Override
185 public int getUserId() {
186 return getCallingUserId();
187 }
188 }
189
190 /** Context used in the service side */
191 public class ServiceContext extends BaseContext {
192 long injectClearCallingIdentity() {
193 final int prevCallingUid = mInjectedCallingUid;
194 mInjectedCallingUid = Process.SYSTEM_UID;
195 return prevCallingUid;
196 }
197
198 void injectRestoreCallingIdentity(long token) {
199 mInjectedCallingUid = (int) token;
200 }
201
202 @Override
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700203 public int getUserId() {
204 return UserHandle.USER_SYSTEM;
205 }
206
207 public PackageInfo injectGetActivitiesWithMetadata(
208 String packageName, @UserIdInt int userId) {
209 return BaseShortcutManagerTest.this.injectGetActivitiesWithMetadata(packageName, userId);
210 }
211
212 public XmlResourceParser injectXmlMetaData(ActivityInfo activityInfo, String key) {
213 return BaseShortcutManagerTest.this.injectXmlMetaData(activityInfo, key);
214 }
Makoto Onuki2d895c32016-12-02 15:48:40 -0800215
216 public void sendIntentSender(IntentSender intent) {
217 // Placeholder for spying.
218 }
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700219 }
220
221 /** ShortcutService with injection override methods. */
222 protected final class ShortcutServiceTestable extends ShortcutService {
223 final ServiceContext mContext;
224 IUidObserver mUidObserver;
225
226 public ShortcutServiceTestable(ServiceContext context, Looper looper) {
Makoto Onukiee6b6e42016-06-29 17:34:02 -0700227 super(context, looper, /* onyForPackageManagerApis */ false);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700228 mContext = context;
229 }
230
231 @Override
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700232 public String injectGetLocaleTagsForUser(@UserIdInt int userId) {
233 return mInjectedLocale.toLanguageTag();
234 }
235
236 @Override
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700237 boolean injectShouldPerformVerification() {
238 return true; // Always verify during unit tests.
239 }
240
241 @Override
242 String injectShortcutManagerConstants() {
243 return ConfigConstants.KEY_RESET_INTERVAL_SEC + "=" + (INTERVAL / 1000) + ","
244 + ConfigConstants.KEY_MAX_SHORTCUTS + "=" + MAX_SHORTCUTS + ","
245 + ConfigConstants.KEY_MAX_UPDATES_PER_INTERVAL + "="
246 + MAX_UPDATES_PER_INTERVAL + ","
247 + ConfigConstants.KEY_MAX_ICON_DIMENSION_DP + "=" + MAX_ICON_DIMENSION + ","
248 + ConfigConstants.KEY_MAX_ICON_DIMENSION_DP_LOWRAM + "="
249 + MAX_ICON_DIMENSION_LOWRAM + ","
250 + ConfigConstants.KEY_ICON_FORMAT + "=PNG,"
251 + ConfigConstants.KEY_ICON_QUALITY + "=100";
252 }
253
254 @Override
255 long injectClearCallingIdentity() {
256 return mContext.injectClearCallingIdentity();
257 }
258
259 @Override
260 void injectRestoreCallingIdentity(long token) {
261 mContext.injectRestoreCallingIdentity(token);
262 }
263
264 @Override
265 int injectDipToPixel(int dip) {
266 return dip;
267 }
268
269 @Override
270 long injectCurrentTimeMillis() {
271 return mInjectedCurrentTimeMillis;
272 }
273
274 @Override
275 long injectElapsedRealtime() {
276 // TODO This should be kept separately from mInjectedCurrentTimeMillis, since
277 // this should increase even if we rewind mInjectedCurrentTimeMillis in some tests.
278 return mInjectedCurrentTimeMillis - START_TIME;
279 }
280
281 @Override
282 int injectBinderCallingUid() {
283 return mInjectedCallingUid;
284 }
285
286 @Override
287 int injectGetPackageUid(String packageName, int userId) {
288 return getInjectedPackageInfo(packageName, userId, false).applicationInfo.uid;
289 }
290
291 @Override
292 File injectSystemDataPath() {
293 return new File(mInjectedFilePathRoot, "system");
294 }
295
296 @Override
297 File injectUserDataPath(@UserIdInt int userId) {
298 return new File(mInjectedFilePathRoot, "user-" + userId);
299 }
300
301 @Override
302 void injectValidateIconResPackage(ShortcutInfo shortcut, Icon icon) {
303 // Can't check
304 }
305
306 @Override
307 boolean injectIsLowRamDevice() {
308 return mInjectedIsLowRamDevice;
309 }
310
311 @Override
312 void injectRegisterUidObserver(IUidObserver observer, int which) {
313 mUidObserver = observer;
314 }
315
316 @Override
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700317 boolean hasShortcutHostPermission(@NonNull String callingPackage, int userId) {
318 return mDefaultLauncherChecker.test(callingPackage, userId);
319 }
320
321 @Override
Makoto Onuki2d895c32016-12-02 15:48:40 -0800322 ComponentName getDefaultLauncher(@UserIdInt int userId) {
323 final ComponentName activity = mDefaultLauncher.get(userId);
324 if (activity != null) {
325 return activity;
326 }
327 return super.getDefaultLauncher(userId);
328 }
329
330 @Override
Makoto Onukiee6b6e42016-06-29 17:34:02 -0700331 PackageInfo injectPackageInfoWithUninstalled(String packageName, @UserIdInt int userId,
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700332 boolean getSignatures) {
333 return getInjectedPackageInfo(packageName, userId, getSignatures);
334 }
335
336 @Override
Makoto Onukiee6b6e42016-06-29 17:34:02 -0700337 ApplicationInfo injectApplicationInfoWithUninstalled(
338 String packageName, @UserIdInt int userId) {
339 PackageInfo pi = injectPackageInfoWithUninstalled(
340 packageName, userId, /* getSignatures= */ false);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700341 return pi != null ? pi.applicationInfo : null;
342 }
343
344 @Override
Makoto Onukiee6b6e42016-06-29 17:34:02 -0700345 List<PackageInfo> injectGetPackagesWithUninstalled(@UserIdInt int userId) {
346 return BaseShortcutManagerTest.this.getInstalledPackagesWithUninstalled(userId);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700347 }
348
349 @Override
Makoto Onukiee6b6e42016-06-29 17:34:02 -0700350 ActivityInfo injectGetActivityInfoWithMetadataWithUninstalled(ComponentName activity,
Makoto Onukib08790c2016-06-23 14:05:46 -0700351 @UserIdInt int userId) {
352 final PackageInfo pi = mContext.injectGetActivitiesWithMetadata(
353 activity.getPackageName(), userId);
354 if (pi == null || pi.activities == null) {
355 return null;
356 }
357 for (ActivityInfo ai : pi.activities) {
358 if (!mEnabledActivityChecker.test(ai.getComponentName(), userId)) {
359 continue;
360 }
361 if (activity.equals(ai.getComponentName())) {
362 return ai;
363 }
364 }
365 return null;
366 }
367
368 @Override
369 boolean injectIsMainActivity(@NonNull ComponentName activity, int userId) {
370 if (!mEnabledActivityChecker.test(activity, userId)) {
371 return false;
372 }
373 return mMainActivityChecker.test(activity, userId);
374 }
375
376 @Override
377 List<ResolveInfo> injectGetMainActivities(@NonNull String packageName, int userId) {
378 final PackageInfo pi = mContext.injectGetActivitiesWithMetadata(
379 packageName, userId);
380 if (pi == null || pi.activities == null) {
381 return null;
382 }
383 final ArrayList<ResolveInfo> ret = new ArrayList<>(pi.activities.length);
384 for (int i = 0; i < pi.activities.length; i++) {
385 if (!mEnabledActivityChecker.test(pi.activities[i].getComponentName(), userId)) {
386 continue;
387 }
388 final ResolveInfo ri = new ResolveInfo();
389 ri.activityInfo = pi.activities[i];
390 ret.add(ri);
391 }
392
393 return ret;
394 }
395
396 @Override
397 ComponentName injectGetDefaultMainActivity(@NonNull String packageName, int userId) {
398 return mMainActivityFetcher.apply(packageName, userId);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700399 }
400
401 @Override
Makoto Onuki2d895c32016-12-02 15:48:40 -0800402 ComponentName injectGetPinConfirmationActivity(@NonNull String launcherPackageName,
Sunny Goyal7f7372a2017-01-24 11:53:54 -0800403 int launcherUserId, int requestType) {
Makoto Onuki2d895c32016-12-02 15:48:40 -0800404 return mPinConfirmActivityFetcher.apply(launcherPackageName, launcherUserId);
405 }
406
407 @Override
Makoto Onukiee6b6e42016-06-29 17:34:02 -0700408 boolean injectIsActivityEnabledAndExported(ComponentName activity, @UserIdInt int userId) {
409 return mEnabledActivityChecker.test(activity, userId);
410 }
411
412 @Override
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700413 XmlResourceParser injectXmlMetaData(ActivityInfo activityInfo, String key) {
414 return mContext.injectXmlMetaData(activityInfo, key);
415 }
416
417 @Override
Makoto Onuki157b1622016-06-02 16:13:10 -0700418 void injectPostToHandler(Runnable r) {
Makoto Onukia2241832016-07-06 13:28:37 -0700419 runOnHandler(r);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700420 }
421
422 @Override
Makoto Onuki085a05c2016-08-19 11:39:29 -0700423 void injectRunOnNewThread(Runnable r) {
424 runOnHandler(r);
425 }
426
427 @Override
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700428 void injectEnforceCallingPermission(String permission, String message) {
429 if (!mCallerPermissions.contains(permission)) {
430 throw new SecurityException("Missing permission: " + permission);
431 }
432 }
433
434 @Override
Makoto Onukib08790c2016-06-23 14:05:46 -0700435 boolean injectIsSafeModeEnabled() {
436 return mSafeMode;
437 }
438
439 @Override
Makoto Onuki33663282016-08-22 16:19:04 -0700440 String injectBuildFingerprint() {
441 return mInjectedBuildFingerprint;
442 }
443
444 @Override
Sunny Goyal87a563e2017-01-01 19:42:45 -0800445 void injectSendIntentSender(IntentSender intent, Intent extras) {
Makoto Onuki2d895c32016-12-02 15:48:40 -0800446 mContext.sendIntentSender(intent);
447 }
448
449 @Override
Makoto Onukia2241832016-07-06 13:28:37 -0700450 void wtf(String message, Throwable th) {
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700451 // During tests, WTF is fatal.
Makoto Onukia2241832016-07-06 13:28:37 -0700452 fail(message + " exception: " + th + "\n" + Log.getStackTraceString(th));
453 }
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700454 }
455
456 /** ShortcutManager with injection override methods. */
457 protected class ShortcutManagerTestable extends ShortcutManager {
458 public ShortcutManagerTestable(Context context, ShortcutServiceTestable service) {
459 super(context, service);
460 }
461
462 @Override
463 protected int injectMyUserId() {
464 return UserHandle.getUserId(mInjectedCallingUid);
465 }
466
467 @Override
468 public boolean setDynamicShortcuts(@NonNull List<ShortcutInfo> shortcutInfoList) {
469 // Note to simulate the binder RPC, we need to clone the incoming arguments.
470 // Otherwise bad things will happen because they're mutable.
471 return super.setDynamicShortcuts(cloneShortcutList(shortcutInfoList));
472 }
473
474 @Override
475 public boolean addDynamicShortcuts(@NonNull List<ShortcutInfo> shortcutInfoList) {
476 // Note to simulate the binder RPC, we need to clone the incoming arguments.
477 return super.addDynamicShortcuts(cloneShortcutList(shortcutInfoList));
478 }
479
480 @Override
481 public boolean updateShortcuts(List<ShortcutInfo> shortcutInfoList) {
482 // Note to simulate the binder RPC, we need to clone the incoming arguments.
483 return super.updateShortcuts(cloneShortcutList(shortcutInfoList));
484 }
485 }
486
487 protected class LauncherAppImplTestable extends LauncherAppsImpl {
488 final ServiceContext mContext;
489
490 public LauncherAppImplTestable(ServiceContext context) {
491 super(context);
492 mContext = context;
493 }
494
495 @Override
496 public void verifyCallingPackage(String callingPackage) {
497 // SKIP
498 }
499
500 @Override
501 void postToPackageMonitorHandler(Runnable r) {
Makoto Onukia2241832016-07-06 13:28:37 -0700502 runOnHandler(r);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700503 }
504
505 @Override
506 int injectBinderCallingUid() {
507 return mInjectedCallingUid;
508 }
509
510 @Override
511 long injectClearCallingIdentity() {
512 final int prevCallingUid = mInjectedCallingUid;
513 mInjectedCallingUid = Process.SYSTEM_UID;
514 return prevCallingUid;
515 }
516
517 @Override
518 void injectRestoreCallingIdentity(long token) {
519 mInjectedCallingUid = (int) token;
520 }
521 }
522
523 protected class LauncherAppsTestable extends LauncherApps {
524 public LauncherAppsTestable(Context context, ILauncherApps service) {
525 super(context, service);
526 }
527 }
528
529 public static class ShortcutActivity extends Activity {
530 }
531
532 public static class ShortcutActivity2 extends Activity {
533 }
534
535 public static class ShortcutActivity3 extends Activity {
536 }
537
Makoto Onukia2241832016-07-06 13:28:37 -0700538 protected Looper mLooper;
539 protected Handler mHandler;
540
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700541 protected ServiceContext mServiceContext;
542 protected ClientContext mClientContext;
543
544 protected ShortcutServiceTestable mService;
545 protected ShortcutManagerTestable mManager;
546 protected ShortcutServiceInternal mInternal;
547
548 protected LauncherAppImplTestable mLauncherAppImpl;
549
550 // LauncherApps has per-instace state, so we need a differnt instance for each launcher.
551 protected final Map<Pair<Integer, String>, LauncherAppsTestable>
552 mLauncherAppsMap = new HashMap<>();
553 protected LauncherAppsTestable mLauncherApps; // Current one
554
555 protected File mInjectedFilePathRoot;
556
Makoto Onukib08790c2016-06-23 14:05:46 -0700557 protected boolean mSafeMode;
558
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700559 protected long mInjectedCurrentTimeMillis;
560
561 protected boolean mInjectedIsLowRamDevice;
562
Makoto Onuki157b1622016-06-02 16:13:10 -0700563 protected Locale mInjectedLocale = Locale.ENGLISH;
564
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700565 protected int mInjectedCallingUid;
566 protected String mInjectedClientPackage;
567
568 protected Map<String, PackageInfo> mInjectedPackages;
569
570 protected Set<PackageWithUser> mUninstalledPackages;
Makoto Onuki66e4a2b2017-01-23 11:37:45 -0800571 protected Set<PackageWithUser> mEphemeralPackages;
Makoto Onuki33663282016-08-22 16:19:04 -0700572 protected Set<String> mSystemPackages;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700573
574 protected PackageManager mMockPackageManager;
575 protected PackageManagerInternal mMockPackageManagerInternal;
576 protected UserManager mMockUserManager;
Makoto Onukiac042502016-05-20 16:39:42 -0700577 protected UsageStatsManagerInternal mMockUsageStatsManagerInternal;
Makoto Onukiea11db12016-06-24 15:17:44 -0700578 protected ActivityManagerInternal mMockActivityManagerInternal;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700579
580 protected static final String CALLING_PACKAGE_1 = "com.android.test.1";
581 protected static final int CALLING_UID_1 = 10001;
582
583 protected static final String CALLING_PACKAGE_2 = "com.android.test.2";
584 protected static final int CALLING_UID_2 = 10002;
585
586 protected static final String CALLING_PACKAGE_3 = "com.android.test.3";
587 protected static final int CALLING_UID_3 = 10003;
588
589 protected static final String CALLING_PACKAGE_4 = "com.android.test.4";
590 protected static final int CALLING_UID_4 = 10004;
591
592 protected static final String LAUNCHER_1 = "com.android.launcher.1";
593 protected static final int LAUNCHER_UID_1 = 10011;
594
595 protected static final String LAUNCHER_2 = "com.android.launcher.2";
596 protected static final int LAUNCHER_UID_2 = 10012;
597
598 protected static final String LAUNCHER_3 = "com.android.launcher.3";
599 protected static final int LAUNCHER_UID_3 = 10013;
600
601 protected static final String LAUNCHER_4 = "com.android.launcher.4";
602 protected static final int LAUNCHER_UID_4 = 10014;
603
604 protected static final int USER_0 = UserHandle.USER_SYSTEM;
605 protected static final int USER_10 = 10;
606 protected static final int USER_11 = 11;
Makoto Onukide3c16c2017-01-26 11:39:31 -0800607 protected static final int USER_P0 = 20; // profile of user 0 (MANAGED_PROFILE *not* set)
608 protected static final int USER_P1 = 21; // another profile of user 0 (MANAGED_PROFILE set)
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700609
610 protected static final UserHandle HANDLE_USER_0 = UserHandle.of(USER_0);
611 protected static final UserHandle HANDLE_USER_10 = UserHandle.of(USER_10);
612 protected static final UserHandle HANDLE_USER_11 = UserHandle.of(USER_11);
613 protected static final UserHandle HANDLE_USER_P0 = UserHandle.of(USER_P0);
Makoto Onukide3c16c2017-01-26 11:39:31 -0800614 protected static final UserHandle HANDLE_USER_P1 = UserHandle.of(USER_P1);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700615
616 protected static final UserInfo USER_INFO_0 = withProfileGroupId(
617 new UserInfo(USER_0, "user0",
Makoto Onuki2d895c32016-12-02 15:48:40 -0800618 UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY | UserInfo.FLAG_INITIALIZED), 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700619
620 protected static final UserInfo USER_INFO_10 =
621 new UserInfo(USER_10, "user10", UserInfo.FLAG_INITIALIZED);
622
623 protected static final UserInfo USER_INFO_11 =
624 new UserInfo(USER_11, "user11", UserInfo.FLAG_INITIALIZED);
625
Makoto Onukiaecbd032017-01-19 12:11:11 -0800626 /*
627 * Cheat: USER_P0 is a sub profile of USER_0, but it doesn't have the MANAGED_PROFILE flag set.
628 * Due to a change made to LauncherApps (b/34340531), work profile apps a no longer able
629 * to see the main profile, which would break tons of unit tests. We avoid it by not setting
630 * MANAGED_PROFILE for P0.
631 * We cover this negative case in CTS. (i.e. CTS has tests to make sure maanged profile
632 * can't access main profile's shortcuts.)
633 */
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700634 protected static final UserInfo USER_INFO_P0 = withProfileGroupId(
Makoto Onukiaecbd032017-01-19 12:11:11 -0800635 new UserInfo(USER_P0, "userP0", UserInfo.FLAG_INITIALIZED), 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700636
Makoto Onukide3c16c2017-01-26 11:39:31 -0800637 protected static final UserInfo USER_INFO_P1 = withProfileGroupId(
638 new UserInfo(USER_P1, "userP1",
639 UserInfo.FLAG_INITIALIZED | UserInfo.FLAG_MANAGED_PROFILE), 0);
640
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700641 protected BiPredicate<String, Integer> mDefaultLauncherChecker =
642 (callingPackage, userId) ->
643 LAUNCHER_1.equals(callingPackage) || LAUNCHER_2.equals(callingPackage)
644 || LAUNCHER_3.equals(callingPackage) || LAUNCHER_4.equals(callingPackage);
645
Makoto Onuki2d895c32016-12-02 15:48:40 -0800646 private final Map<Integer, ComponentName> mDefaultLauncher = new ArrayMap<>();
647
Makoto Onukib08790c2016-06-23 14:05:46 -0700648 protected BiPredicate<ComponentName, Integer> mMainActivityChecker =
649 (activity, userId) -> true;
650
651 protected BiFunction<String, Integer, ComponentName> mMainActivityFetcher =
652 (packageName, userId) -> new ComponentName(packageName, MAIN_ACTIVITY_CLASS);
653
Makoto Onuki2d895c32016-12-02 15:48:40 -0800654 protected BiFunction<String, Integer, ComponentName> mPinConfirmActivityFetcher =
655 (packageName, userId) -> new ComponentName(packageName, PIN_CONFIRM_ACTIVITY_CLASS);
656
Makoto Onukib08790c2016-06-23 14:05:46 -0700657 protected BiPredicate<ComponentName, Integer> mEnabledActivityChecker
658 = (activity, userId) -> true; // all activities are enabled.
659
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700660 protected static final long START_TIME = 1440000000101L;
661
662 protected static final long INTERVAL = 10000;
663
664 protected static final int MAX_SHORTCUTS = 10;
665
666 protected static final int MAX_UPDATES_PER_INTERVAL = 3;
667
668 protected static final int MAX_ICON_DIMENSION = 128;
669
670 protected static final int MAX_ICON_DIMENSION_LOWRAM = 32;
671
672 protected static final ShortcutQuery QUERY_ALL = new ShortcutQuery();
673
674 protected final ArrayList<String> mCallerPermissions = new ArrayList<>();
675
676 protected final HashMap<String, LinkedHashMap<ComponentName, Integer>> mActivityMetadataResId
677 = new HashMap<>();
678
Makoto Onukia2241832016-07-06 13:28:37 -0700679 protected final Map<Integer, UserInfo> mUserInfos = new HashMap<>();
680 protected final Map<Integer, Boolean> mRunningUsers = new HashMap<>();
681 protected final Map<Integer, Boolean> mUnlockedUsers = new HashMap<>();
682
Makoto Onuki0b9d1db2016-07-18 14:16:41 -0700683 protected static final String PACKAGE_SYSTEM_LAUNCHER = "com.android.systemlauncher";
684 protected static final String PACKAGE_SYSTEM_LAUNCHER_NAME = "systemlauncher_name";
685 protected static final int PACKAGE_SYSTEM_LAUNCHER_PRIORITY = 0;
686
687 protected static final String PACKAGE_FALLBACK_LAUNCHER = "com.android.settings";
688 protected static final String PACKAGE_FALLBACK_LAUNCHER_NAME = "fallback";
689 protected static final int PACKAGE_FALLBACK_LAUNCHER_PRIORITY = -999;
690
Makoto Onuki33663282016-08-22 16:19:04 -0700691 protected String mInjectedBuildFingerprint = "build1";
692
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700693 static {
694 QUERY_ALL.setQueryFlags(
Makoto Onuki4d6b87f2016-06-17 13:47:40 -0700695 ShortcutQuery.FLAG_GET_ALL_KINDS);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700696 }
697
698 @Override
699 protected void setUp() throws Exception {
700 super.setUp();
701
Makoto Onukia2241832016-07-06 13:28:37 -0700702 mLooper = Looper.getMainLooper();
703 mHandler = new Handler(mLooper);
704
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700705 mServiceContext = spy(new ServiceContext());
706 mClientContext = new ClientContext();
707
708 mMockPackageManager = mock(PackageManager.class);
709 mMockPackageManagerInternal = mock(PackageManagerInternal.class);
710 mMockUserManager = mock(UserManager.class);
Makoto Onukiac042502016-05-20 16:39:42 -0700711 mMockUsageStatsManagerInternal = mock(UsageStatsManagerInternal.class);
Makoto Onukiea11db12016-06-24 15:17:44 -0700712 mMockActivityManagerInternal = mock(ActivityManagerInternal.class);
Makoto Onukiac042502016-05-20 16:39:42 -0700713
714 LocalServices.removeServiceForTest(PackageManagerInternal.class);
715 LocalServices.addService(PackageManagerInternal.class, mMockPackageManagerInternal);
716 LocalServices.removeServiceForTest(UsageStatsManagerInternal.class);
717 LocalServices.addService(UsageStatsManagerInternal.class, mMockUsageStatsManagerInternal);
Makoto Onukiea11db12016-06-24 15:17:44 -0700718 LocalServices.removeServiceForTest(ActivityManagerInternal.class);
719 LocalServices.addService(ActivityManagerInternal.class, mMockActivityManagerInternal);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700720
721 // Prepare injection values.
722
723 mInjectedCurrentTimeMillis = START_TIME;
724
725 mInjectedPackages = new HashMap<>();
726 addPackage(CALLING_PACKAGE_1, CALLING_UID_1, 1);
727 addPackage(CALLING_PACKAGE_2, CALLING_UID_2, 2);
728 addPackage(CALLING_PACKAGE_3, CALLING_UID_3, 3);
729 addPackage(CALLING_PACKAGE_4, CALLING_UID_4, 10);
730 addPackage(LAUNCHER_1, LAUNCHER_UID_1, 4);
731 addPackage(LAUNCHER_2, LAUNCHER_UID_2, 5);
732 addPackage(LAUNCHER_3, LAUNCHER_UID_3, 6);
733 addPackage(LAUNCHER_4, LAUNCHER_UID_4, 10);
734
735 // CALLING_PACKAGE_3 / LAUNCHER_3 are not backup target.
736 updatePackageInfo(CALLING_PACKAGE_3,
737 pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP);
738 updatePackageInfo(LAUNCHER_3,
739 pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP);
740
741 mUninstalledPackages = new HashSet<>();
Makoto Onuki33663282016-08-22 16:19:04 -0700742 mSystemPackages = new HashSet<>();
Makoto Onuki66e4a2b2017-01-23 11:37:45 -0800743 mEphemeralPackages = new HashSet<>();
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700744
745 mInjectedFilePathRoot = new File(getTestContext().getCacheDir(), "test-files");
746
747 deleteAllSavedFiles();
748
749 // Set up users.
Makoto Onukia2241832016-07-06 13:28:37 -0700750 when(mMockUserManager.getUserInfo(anyInt())).thenAnswer(new AnswerWithSystemCheck<>(
751 inv -> mUserInfos.get((Integer) inv.getArguments()[0])));
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700752
Makoto Onukia2241832016-07-06 13:28:37 -0700753 mUserInfos.put(USER_0, USER_INFO_0);
754 mUserInfos.put(USER_10, USER_INFO_10);
755 mUserInfos.put(USER_11, USER_INFO_11);
756 mUserInfos.put(USER_P0, USER_INFO_P0);
Makoto Onukide3c16c2017-01-26 11:39:31 -0800757 mUserInfos.put(USER_P1, USER_INFO_P1);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700758
Makoto Onukia2241832016-07-06 13:28:37 -0700759 // Set up isUserRunning and isUserUnlocked.
760 when(mMockUserManager.isUserRunning(anyInt())).thenAnswer(new AnswerWithSystemCheck<>(
Makoto Onuki9c850012016-07-26 15:50:50 -0700761 inv -> b(mRunningUsers.get((Integer) inv.getArguments()[0]))));
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700762
Makoto Onuki9c850012016-07-26 15:50:50 -0700763 when(mMockUserManager.isUserUnlocked(anyInt()))
764 .thenAnswer(new AnswerWithSystemCheck<>(inv -> {
765 final int userId = (Integer) inv.getArguments()[0];
766 return b(mRunningUsers.get(userId)) && b(mUnlockedUsers.get(userId));
767 }));
768 // isUserUnlockingOrUnlocked() return the same value as isUserUnlocked().
769 when(mMockUserManager.isUserUnlockingOrUnlocked(anyInt()))
770 .thenAnswer(new AnswerWithSystemCheck<>(inv -> {
771 final int userId = (Integer) inv.getArguments()[0];
772 return b(mRunningUsers.get(userId)) && b(mUnlockedUsers.get(userId));
773 }));
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700774
Makoto Onuki2d895c32016-12-02 15:48:40 -0800775 when(mMockUserManager.getProfileParent(anyInt()))
776 .thenAnswer(new AnswerWithSystemCheck<>(inv -> {
777 final int userId = (Integer) inv.getArguments()[0];
778 final UserInfo ui = mUserInfos.get(userId);
779 assertNotNull(ui);
780 if (ui.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID) {
781 return null;
782 }
783 final UserInfo parent = mUserInfos.get(ui.profileGroupId);
784 assertNotNull(parent);
785 return parent;
786 }));
Makoto Onukide3c16c2017-01-26 11:39:31 -0800787 when(mMockUserManager.isManagedProfile(anyInt()))
788 .thenAnswer(new AnswerWithSystemCheck<>(inv -> {
789 final int userId = (Integer) inv.getArguments()[0];
790 final UserInfo ui = mUserInfos.get(userId);
791 assertNotNull(ui);
792 return ui.isManagedProfile();
793 }));
Makoto Onuki2d895c32016-12-02 15:48:40 -0800794
Makoto Onuki33525d22016-08-03 15:45:24 -0700795 when(mMockActivityManagerInternal.getUidProcessState(anyInt())).thenReturn(
796 ActivityManager.PROCESS_STATE_CACHED_EMPTY);
797
Makoto Onuki9c850012016-07-26 15:50:50 -0700798 // User 0 and P0 are always running
Makoto Onukia2241832016-07-06 13:28:37 -0700799 mRunningUsers.put(USER_0, true);
800 mRunningUsers.put(USER_10, false);
801 mRunningUsers.put(USER_11, false);
Makoto Onuki9c850012016-07-26 15:50:50 -0700802 mRunningUsers.put(USER_P0, true);
Makoto Onukide3c16c2017-01-26 11:39:31 -0800803 mRunningUsers.put(USER_P1, true);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700804
Makoto Onukia2241832016-07-06 13:28:37 -0700805 // Unlock all users by default.
806 mUnlockedUsers.put(USER_0, true);
807 mUnlockedUsers.put(USER_10, true);
808 mUnlockedUsers.put(USER_11, true);
809 mUnlockedUsers.put(USER_P0, true);
Makoto Onukide3c16c2017-01-26 11:39:31 -0800810 mUnlockedUsers.put(USER_P1, true);
Makoto Onukia2241832016-07-06 13:28:37 -0700811
812 // Set up resources
Makoto Onuki157b1622016-06-02 16:13:10 -0700813 setUpAppResources();
814
815 // Start the service.
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700816 initService();
817 setCaller(CALLING_PACKAGE_1);
Makoto Onuki248a0ef2016-11-03 15:59:01 -0700818
819 if (ENABLE_DUMP) {
820 Log.d(TAG, "setUp done");
821 }
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700822 }
823
Makoto Onuki9c850012016-07-26 15:50:50 -0700824 private static boolean b(Boolean value) {
825 return (value != null && value);
826 }
827
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700828 /**
829 * Returns a boolean but also checks if the current UID is SYSTEM_UID.
830 */
Makoto Onukia2241832016-07-06 13:28:37 -0700831 protected class AnswerWithSystemCheck<T> implements Answer<T> {
832 private final Function<InvocationOnMock, T> mChecker;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700833
Makoto Onukia2241832016-07-06 13:28:37 -0700834 public AnswerWithSystemCheck(Function<InvocationOnMock, T> checker) {
835 mChecker = checker;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700836 }
837
838 @Override
Makoto Onukia2241832016-07-06 13:28:37 -0700839 public T answer(InvocationOnMock invocation) throws Throwable {
840 assertEquals("Must be called on SYSTEM UID.",
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700841 Process.SYSTEM_UID, mInjectedCallingUid);
Makoto Onukia2241832016-07-06 13:28:37 -0700842 return mChecker.apply(invocation);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700843 }
844 }
845
Makoto Onuki157b1622016-06-02 16:13:10 -0700846 protected void setUpAppResources() throws Exception {
847 setUpAppResources(/* offset = */ 0);
848 }
849
850 protected void setUpAppResources(int ressIdOffset) throws Exception {
851 // ressIdOffset is used to adjust resource IDs to emulate the case where an updated app
852 // has resource IDs changed.
853
854 doAnswer(pmInvocation -> {
855 assertEquals(Process.SYSTEM_UID, mInjectedCallingUid);
856
857 final String packageName = (String) pmInvocation.getArguments()[0];
858 final int userId = (Integer) pmInvocation.getArguments()[1];
859
860 final Resources res = mock(Resources.class);
861
862 doAnswer(resInvocation -> {
863 final int argResId = (Integer) resInvocation.getArguments()[0];
864
865 return "string-" + packageName + "-user:" + userId + "-res:" + argResId
866 + "/" + mInjectedLocale;
867 }).when(res).getString(anyInt());
868
869 doAnswer(resInvocation -> {
870 final int resId = (Integer) resInvocation.getArguments()[0];
871
872 // Always use the "string" resource type. The type doesn't matter during the test.
873 return packageName + ":string/r" + resId;
874 }).when(res).getResourceName(anyInt());
875
876 doAnswer(resInvocation -> {
877 final String argResName = (String) resInvocation.getArguments()[0];
878 final String argType = (String) resInvocation.getArguments()[1];
879 final String argPackageName = (String) resInvocation.getArguments()[2];
880
881 // See the above code. getResourceName() will just use "r" + res ID as the entry
882 // name.
883 String entryName = argResName;
884 if (entryName.contains("/")) {
885 entryName = ShortcutInfo.getResourceEntryName(entryName);
886 }
887 return Integer.parseInt(entryName.substring(1)) + ressIdOffset;
Makoto Onukid0010c52017-03-30 14:17:35 -0700888 }).when(res).getIdentifier(anyStringOrNull(), anyStringOrNull(), anyStringOrNull());
Makoto Onuki157b1622016-06-02 16:13:10 -0700889 return res;
890 }).when(mMockPackageManager).getResourcesForApplicationAsUser(anyString(), anyInt());
891 }
892
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700893 protected static UserInfo withProfileGroupId(UserInfo in, int groupId) {
894 in.profileGroupId = groupId;
895 return in;
896 }
897
898 @Override
899 protected void tearDown() throws Exception {
900 if (DUMP_IN_TEARDOWN) dumpsysOnLogcat("Teardown");
901
902 shutdownServices();
903
904 super.tearDown();
905 }
906
907 protected Context getTestContext() {
908 return getInstrumentation().getContext();
909 }
910
Makoto Onukia1d38b32016-06-10 15:32:26 -0700911 protected ShortcutManager getManager() {
912 return mManager;
913 }
914
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700915 protected void deleteAllSavedFiles() {
916 // Empty the data directory.
917 if (mInjectedFilePathRoot.exists()) {
918 Assert.assertTrue("failed to delete dir",
919 FileUtils.deleteContents(mInjectedFilePathRoot));
920 }
921 mInjectedFilePathRoot.mkdirs();
922 }
923
924 /** (Re-) init the manager and the service. */
925 protected void initService() {
926 shutdownServices();
927
928 LocalServices.removeServiceForTest(ShortcutServiceInternal.class);
929
930 // Instantiate targets.
Makoto Onukia2241832016-07-06 13:28:37 -0700931 mService = new ShortcutServiceTestable(mServiceContext, mLooper);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700932 mManager = new ShortcutManagerTestable(mClientContext, mService);
933
934 mInternal = LocalServices.getService(ShortcutServiceInternal.class);
935
936 mLauncherAppImpl = new LauncherAppImplTestable(mServiceContext);
937 mLauncherApps = null;
938 mLauncherAppsMap.clear();
939
Makoto Onuki157b1622016-06-02 16:13:10 -0700940 // Send boot sequence events.
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700941 mService.onBootPhase(SystemService.PHASE_LOCK_SETTINGS_READY);
Makoto Onuki157b1622016-06-02 16:13:10 -0700942
Makoto Onuki157b1622016-06-02 16:13:10 -0700943 mService.onBootPhase(SystemService.PHASE_BOOT_COMPLETED);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700944 }
945
946 protected void shutdownServices() {
947 if (mService != null) {
948 // Flush all the unsaved data from the previous instance.
949 mService.saveDirtyInfo();
Makoto Onuki9e1f5592016-06-08 12:30:23 -0700950
951 // Make sure everything is consistent.
952 mService.verifyStates();
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700953 }
954 LocalServices.removeServiceForTest(ShortcutServiceInternal.class);
955
956 mService = null;
957 mManager = null;
958 mInternal = null;
959 mLauncherAppImpl = null;
960 mLauncherApps = null;
961 mLauncherAppsMap.clear();
962 }
963
Makoto Onukia2241832016-07-06 13:28:37 -0700964 protected void runOnHandler(Runnable r) {
965 final long token = mServiceContext.injectClearCallingIdentity();
966 try {
967 r.run();
968 } finally {
969 mServiceContext.injectRestoreCallingIdentity(token);
970 }
971 }
972
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700973 protected void addPackage(String packageName, int uid, int version) {
974 addPackage(packageName, uid, version, packageName);
975 }
976
977 protected Signature[] genSignatures(String... signatures) {
978 final Signature[] sigs = new Signature[signatures.length];
979 for (int i = 0; i < signatures.length; i++){
980 sigs[i] = new Signature(signatures[i].getBytes());
981 }
982 return sigs;
983 }
984
985 protected PackageInfo genPackage(String packageName, int uid, int version, String... signatures) {
986 final PackageInfo pi = new PackageInfo();
987 pi.packageName = packageName;
988 pi.applicationInfo = new ApplicationInfo();
989 pi.applicationInfo.uid = uid;
990 pi.applicationInfo.flags = ApplicationInfo.FLAG_INSTALLED
991 | ApplicationInfo.FLAG_ALLOW_BACKUP;
992 pi.versionCode = version;
993 pi.applicationInfo.versionCode = version;
994 pi.signatures = genSignatures(signatures);
995
996 return pi;
997 }
998
999 protected void addPackage(String packageName, int uid, int version, String... signatures) {
1000 mInjectedPackages.put(packageName, genPackage(packageName, uid, version, signatures));
1001 }
1002
1003 protected void updatePackageInfo(String packageName, Consumer<PackageInfo> c) {
1004 c.accept(mInjectedPackages.get(packageName));
1005 }
1006
1007 protected void updatePackageVersion(String packageName, int increment) {
1008 updatePackageInfo(packageName, pi -> {
1009 pi.versionCode += increment;
1010 pi.applicationInfo.versionCode += increment;
1011 });
1012 }
1013
1014 protected void updatePackageLastUpdateTime(String packageName, long increment) {
1015 updatePackageInfo(packageName, pi -> {
1016 pi.lastUpdateTime += increment;
1017 });
1018 }
1019
Makoto Onuki085a05c2016-08-19 11:39:29 -07001020 protected void setPackageLastUpdateTime(String packageName, long value) {
1021 updatePackageInfo(packageName, pi -> {
1022 pi.lastUpdateTime = value;
1023 });
1024 }
1025
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001026 protected void uninstallPackage(int userId, String packageName) {
1027 if (ENABLE_DUMP) {
1028 Log.v(TAG, "Unnstall package " + packageName + " / " + userId);
1029 }
1030 mUninstalledPackages.add(PackageWithUser.of(userId, packageName));
1031 }
1032
1033 protected void installPackage(int userId, String packageName) {
1034 if (ENABLE_DUMP) {
1035 Log.v(TAG, "Install package " + packageName + " / " + userId);
1036 }
1037 mUninstalledPackages.remove(PackageWithUser.of(userId, packageName));
1038 }
1039
1040 PackageInfo getInjectedPackageInfo(String packageName, @UserIdInt int userId,
1041 boolean getSignatures) {
1042 final PackageInfo pi = mInjectedPackages.get(packageName);
1043 if (pi == null) return null;
1044
1045 final PackageInfo ret = new PackageInfo();
1046 ret.packageName = pi.packageName;
1047 ret.versionCode = pi.versionCode;
1048 ret.lastUpdateTime = pi.lastUpdateTime;
1049
1050 ret.applicationInfo = new ApplicationInfo(pi.applicationInfo);
1051 ret.applicationInfo.uid = UserHandle.getUid(userId, pi.applicationInfo.uid);
1052 ret.applicationInfo.packageName = pi.packageName;
1053
1054 if (mUninstalledPackages.contains(PackageWithUser.of(userId, packageName))) {
1055 ret.applicationInfo.flags &= ~ApplicationInfo.FLAG_INSTALLED;
1056 }
Makoto Onuki66e4a2b2017-01-23 11:37:45 -08001057 if (mEphemeralPackages.contains(PackageWithUser.of(userId, packageName))) {
Todd Kennedybe0b8892017-02-15 14:13:52 -08001058 ret.applicationInfo.privateFlags |= ApplicationInfo.PRIVATE_FLAG_INSTANT;
Makoto Onuki66e4a2b2017-01-23 11:37:45 -08001059 }
Makoto Onuki33663282016-08-22 16:19:04 -07001060 if (mSystemPackages.contains(packageName)) {
1061 ret.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
1062 }
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001063
1064 if (getSignatures) {
1065 ret.signatures = pi.signatures;
1066 }
1067
1068 return ret;
1069 }
1070
1071 protected void addApplicationInfo(PackageInfo pi, List<ApplicationInfo> list) {
1072 if (pi != null && pi.applicationInfo != null) {
1073 list.add(pi.applicationInfo);
1074 }
1075 }
1076
1077 protected List<ApplicationInfo> getInstalledApplications(int userId) {
1078 final ArrayList<ApplicationInfo> ret = new ArrayList<>();
1079
1080 addApplicationInfo(getInjectedPackageInfo(CALLING_PACKAGE_1, userId, false), ret);
1081 addApplicationInfo(getInjectedPackageInfo(CALLING_PACKAGE_2, userId, false), ret);
1082 addApplicationInfo(getInjectedPackageInfo(CALLING_PACKAGE_3, userId, false), ret);
1083 addApplicationInfo(getInjectedPackageInfo(CALLING_PACKAGE_4, userId, false), ret);
1084 addApplicationInfo(getInjectedPackageInfo(LAUNCHER_1, userId, false), ret);
1085 addApplicationInfo(getInjectedPackageInfo(LAUNCHER_2, userId, false), ret);
1086 addApplicationInfo(getInjectedPackageInfo(LAUNCHER_3, userId, false), ret);
1087 addApplicationInfo(getInjectedPackageInfo(LAUNCHER_4, userId, false), ret);
1088
1089 return ret;
1090 }
1091
Makoto Onuki6dd9fb72016-06-01 13:55:54 -07001092 private void addPackageInfo(PackageInfo pi, List<PackageInfo> list) {
1093 if (pi != null) {
1094 list.add(pi);
1095 }
1096 }
1097
Makoto Onukiee6b6e42016-06-29 17:34:02 -07001098 private List<PackageInfo> getInstalledPackagesWithUninstalled(int userId) {
Makoto Onuki6dd9fb72016-06-01 13:55:54 -07001099 final ArrayList<PackageInfo> ret = new ArrayList<>();
1100
1101 addPackageInfo(getInjectedPackageInfo(CALLING_PACKAGE_1, userId, false), ret);
1102 addPackageInfo(getInjectedPackageInfo(CALLING_PACKAGE_2, userId, false), ret);
1103 addPackageInfo(getInjectedPackageInfo(CALLING_PACKAGE_3, userId, false), ret);
1104 addPackageInfo(getInjectedPackageInfo(CALLING_PACKAGE_4, userId, false), ret);
1105 addPackageInfo(getInjectedPackageInfo(LAUNCHER_1, userId, false), ret);
1106 addPackageInfo(getInjectedPackageInfo(LAUNCHER_2, userId, false), ret);
1107 addPackageInfo(getInjectedPackageInfo(LAUNCHER_3, userId, false), ret);
1108 addPackageInfo(getInjectedPackageInfo(LAUNCHER_4, userId, false), ret);
1109
1110 return ret;
1111 }
1112
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001113 protected void addManifestShortcutResource(ComponentName activity, int resId) {
1114 final String packageName = activity.getPackageName();
1115 LinkedHashMap<ComponentName, Integer> map = mActivityMetadataResId.get(packageName);
1116 if (map == null) {
1117 map = new LinkedHashMap<>();
1118 mActivityMetadataResId.put(packageName, map);
1119 }
1120 map.put(activity, resId);
1121 }
1122
1123 protected PackageInfo injectGetActivitiesWithMetadata(String packageName, @UserIdInt int userId) {
1124 final PackageInfo ret = getInjectedPackageInfo(packageName, userId,
1125 /* getSignatures=*/ false);
1126
1127 final HashMap<ComponentName, Integer> activities = mActivityMetadataResId.get(packageName);
1128 if (activities != null) {
1129 final ArrayList<ActivityInfo> list = new ArrayList<>();
1130
1131 for (ComponentName cn : activities.keySet()) {
1132 ActivityInfo ai = new ActivityInfo();
1133 ai.packageName = cn.getPackageName();
1134 ai.name = cn.getClassName();
1135 ai.metaData = new Bundle();
1136 ai.metaData.putInt(ShortcutParser.METADATA_KEY, activities.get(cn));
Makoto Onukiee6b6e42016-06-29 17:34:02 -07001137 ai.applicationInfo = ret.applicationInfo;
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001138 list.add(ai);
1139 }
1140 ret.activities = list.toArray(new ActivityInfo[list.size()]);
1141 }
1142 return ret;
1143 }
1144
1145 protected XmlResourceParser injectXmlMetaData(ActivityInfo activityInfo, String key) {
1146 if (!ShortcutParser.METADATA_KEY.equals(key) || activityInfo.metaData == null) {
1147 return null;
1148 }
1149 final int resId = activityInfo.metaData.getInt(key);
1150 return getTestContext().getResources().getXml(resId);
1151 }
1152
1153 /** Replace the current calling package */
1154 protected void setCaller(String packageName, int userId) {
1155 mInjectedClientPackage = packageName;
1156 mInjectedCallingUid =
1157 Preconditions.checkNotNull(getInjectedPackageInfo(packageName, userId, false),
1158 "Unknown package").applicationInfo.uid;
1159
1160 // Set up LauncherApps for this caller.
1161 final Pair<Integer, String> key = Pair.create(userId, packageName);
1162 if (!mLauncherAppsMap.containsKey(key)) {
1163 mLauncherAppsMap.put(key, new LauncherAppsTestable(mClientContext, mLauncherAppImpl));
1164 }
1165 mLauncherApps = mLauncherAppsMap.get(key);
1166 }
1167
1168 protected void setCaller(String packageName) {
1169 setCaller(packageName, UserHandle.USER_SYSTEM);
1170 }
1171
1172 protected String getCallingPackage() {
1173 return mInjectedClientPackage;
1174 }
1175
Makoto Onuki2d895c32016-12-02 15:48:40 -08001176 /**
1177 * This controls {@link ShortcutService#hasShortcutHostPermission(String, int)}, but
1178 * not {@link ShortcutService#getDefaultLauncher(int)}. To control the later, use
1179 * {@link #setDefaultLauncher(int, ComponentName)}.
1180 */
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001181 protected void setDefaultLauncherChecker(BiPredicate<String, Integer> p) {
1182 mDefaultLauncherChecker = p;
1183 }
1184
Makoto Onuki2d895c32016-12-02 15:48:40 -08001185 /**
1186 * Set the default launcher. This will update {@link #mDefaultLauncherChecker} set by
1187 * {@link #setDefaultLauncherChecker} too.
1188 */
1189 protected void setDefaultLauncher(int userId, ComponentName launcherActivity) {
1190 mDefaultLauncher.put(userId, launcherActivity);
1191
1192 final BiPredicate<String, Integer> oldChecker = mDefaultLauncherChecker;
1193 mDefaultLauncherChecker = (checkPackageName, checkUserId) -> {
1194 if ((checkUserId == userId) && (launcherActivity != null)) {
1195 return launcherActivity.getPackageName().equals(checkPackageName);
1196 }
1197 return oldChecker.test(checkPackageName, checkUserId);
1198 };
1199 }
1200
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001201 protected void runWithCaller(String packageName, int userId, Runnable r) {
1202 final String previousPackage = mInjectedClientPackage;
1203 final int previousUserId = UserHandle.getUserId(mInjectedCallingUid);
1204
1205 setCaller(packageName, userId);
1206
1207 r.run();
1208
1209 setCaller(previousPackage, previousUserId);
1210 }
1211
Makoto Onuki157b1622016-06-02 16:13:10 -07001212 protected void runWithSystemUid(Runnable r) {
1213 final int origUid = mInjectedCallingUid;
1214 mInjectedCallingUid = Process.SYSTEM_UID;
1215 r.run();
1216 mInjectedCallingUid = origUid;
1217 }
1218
1219 protected void lookupAndFillInResourceNames(ShortcutInfo si) {
1220 runWithSystemUid(() -> si.lookupAndFillInResourceNames(
1221 mService.injectGetResourcesForApplicationAsUser(si.getPackage(), si.getUserId())));
1222 }
1223
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001224 protected int getCallingUserId() {
1225 return UserHandle.getUserId(mInjectedCallingUid);
1226 }
1227
1228 protected UserHandle getCallingUser() {
1229 return UserHandle.of(getCallingUserId());
1230 }
1231
1232 /** For debugging */
1233 protected void dumpsysOnLogcat() {
1234 dumpsysOnLogcat("");
1235 }
1236
1237 protected void dumpsysOnLogcat(String message) {
1238 dumpsysOnLogcat(message, false);
1239 }
1240
1241 protected void dumpsysOnLogcat(String message, boolean force) {
1242 if (force || !ENABLE_DUMP) return;
1243
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001244 Log.v(TAG, "Dumping ShortcutService: " + message);
Makoto Onuki76269922016-07-15 14:58:54 -07001245 for (String line : dumpsys(null).split("\n")) {
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001246 Log.v(TAG, line);
1247 }
1248 }
1249
Makoto Onuki76269922016-07-15 14:58:54 -07001250 protected String dumpCheckin() {
1251 return dumpsys(new String[]{"--checkin"});
1252 }
1253
1254 private String dumpsys(String[] args) {
1255 final ArrayList<String> origPermissions = new ArrayList<>(mCallerPermissions);
1256 mCallerPermissions.add(android.Manifest.permission.DUMP);
1257 try {
1258 final ByteArrayOutputStream out = new ByteArrayOutputStream();
1259 final PrintWriter pw = new PrintWriter(out);
1260 mService.dump(/* fd */ null, pw, args);
1261 pw.close();
1262
1263 return out.toString();
1264 } finally {
1265 mCallerPermissions.clear();
1266 mCallerPermissions.addAll(origPermissions);
1267 }
1268 }
1269
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001270 /**
1271 * For debugging, dump arbitrary file on logcat.
1272 */
1273 protected void dumpFileOnLogcat(String path) {
1274 dumpFileOnLogcat(path, "");
1275 }
1276
1277 protected void dumpFileOnLogcat(String path, String message) {
1278 if (!ENABLE_DUMP) return;
1279
1280 Log.v(TAG, "Dumping file: " + path + " " + message);
1281 final StringBuilder sb = new StringBuilder();
1282 try (BufferedReader br = new BufferedReader(new FileReader(path))) {
1283 String line;
1284 while ((line = br.readLine()) != null) {
1285 Log.v(TAG, line);
1286 }
1287 } catch (Exception e) {
1288 Log.e(TAG, "Couldn't read file", e);
1289 fail("Exception " + e);
1290 }
1291 }
1292
1293 /**
1294 * For debugging, dump the main state file on logcat.
1295 */
1296 protected void dumpBaseStateFile() {
1297 mService.saveDirtyInfo();
1298 dumpFileOnLogcat(mInjectedFilePathRoot.getAbsolutePath()
1299 + "/system/" + ShortcutService.FILENAME_BASE_STATE);
1300 }
1301
1302 /**
1303 * For debugging, dump per-user state file on logcat.
1304 */
1305 protected void dumpUserFile(int userId) {
1306 dumpUserFile(userId, "");
1307 }
1308
1309 protected void dumpUserFile(int userId, String message) {
1310 mService.saveDirtyInfo();
1311 dumpFileOnLogcat(mInjectedFilePathRoot.getAbsolutePath()
1312 + "/user-" + userId
1313 + "/" + ShortcutService.FILENAME_USER_PACKAGES, message);
1314 }
1315
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001316 /**
Makoto Onukia01f4f02016-12-15 15:58:41 -08001317 * Make a shortcut with an ID only.
1318 */
1319 protected ShortcutInfo makeShortcutIdOnly(String id) {
1320 return new ShortcutInfo.Builder(mClientContext, id).build();
1321 }
1322
1323 /**
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001324 * Make a shortcut with an ID.
1325 */
1326 protected ShortcutInfo makeShortcut(String id) {
1327 return makeShortcut(
1328 id, "Title-" + id, /* activity =*/ null, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001329 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001330 }
1331
Makoto Onukia01f4f02016-12-15 15:58:41 -08001332 @Deprecated // Title was renamed to short label.
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001333 protected ShortcutInfo makeShortcutWithTitle(String id, String title) {
1334 return makeShortcut(
1335 id, title, /* activity =*/ null, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001336 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001337 }
1338
Makoto Onukia01f4f02016-12-15 15:58:41 -08001339 protected ShortcutInfo makeShortcutWithShortLabel(String id, String shortLabel) {
1340 return makeShortcut(
1341 id, shortLabel, /* activity =*/ null, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001342 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onukia01f4f02016-12-15 15:58:41 -08001343 }
1344
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001345 /**
1346 * Make a shortcut with an ID and timestamp.
1347 */
1348 protected ShortcutInfo makeShortcutWithTimestamp(String id, long timestamp) {
1349 final ShortcutInfo s = makeShortcut(
1350 id, "Title-" + id, /* activity =*/ null, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001351 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001352 s.setTimestamp(timestamp);
1353 return s;
1354 }
1355
1356 /**
1357 * Make a shortcut with an ID, a timestamp and an activity component
1358 */
1359 protected ShortcutInfo makeShortcutWithTimestampWithActivity(String id, long timestamp,
1360 ComponentName activity) {
1361 final ShortcutInfo s = makeShortcut(
1362 id, "Title-" + id, activity, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001363 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001364 s.setTimestamp(timestamp);
1365 return s;
1366 }
1367
1368 /**
1369 * Make a shortcut with an ID and icon.
1370 */
1371 protected ShortcutInfo makeShortcutWithIcon(String id, Icon icon) {
1372 return makeShortcut(
1373 id, "Title-" + id, /* activity =*/ null, icon,
Makoto Onuki99302b52017-03-29 12:42:26 -07001374 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001375 }
1376
1377 protected ShortcutInfo makePackageShortcut(String packageName, String id) {
1378 String origCaller = getCallingPackage();
1379
1380 setCaller(packageName);
1381 ShortcutInfo s = makeShortcut(
1382 id, "Title-" + id, /* activity =*/ null, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001383 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001384 setCaller(origCaller); // restore the caller
1385
1386 return s;
1387 }
1388
1389 /**
1390 * Make multiple shortcuts with IDs.
1391 */
1392 protected List<ShortcutInfo> makeShortcuts(String... ids) {
1393 final ArrayList<ShortcutInfo> ret = new ArrayList();
1394 for (String id : ids) {
1395 ret.add(makeShortcut(id));
1396 }
1397 return ret;
1398 }
1399
1400 protected ShortcutInfo.Builder makeShortcutBuilder() {
1401 return new ShortcutInfo.Builder(mClientContext);
1402 }
1403
1404 protected ShortcutInfo makeShortcutWithActivity(String id, ComponentName activity) {
1405 return makeShortcut(
1406 id, "Title-" + id, activity, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001407 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001408 }
1409
Makoto Onukib5a012f2016-06-21 11:13:53 -07001410 protected ShortcutInfo makeShortcutWithIntent(String id, Intent intent) {
1411 return makeShortcut(
1412 id, "Title-" + id, /* activity =*/ null, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001413 intent, /* rank =*/ 0);
Makoto Onukib5a012f2016-06-21 11:13:53 -07001414 }
1415
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001416 protected ShortcutInfo makeShortcutWithActivityAndTitle(String id, ComponentName activity,
1417 String title) {
1418 return makeShortcut(
1419 id, title, activity, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001420 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001421 }
1422
1423 protected ShortcutInfo makeShortcutWithActivityAndRank(String id, ComponentName activity,
1424 int rank) {
1425 return makeShortcut(
1426 id, "Title-" + id, activity, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001427 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), rank);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001428 }
1429
1430 /**
1431 * Make a shortcut with details.
1432 */
1433 protected ShortcutInfo makeShortcut(String id, String title, ComponentName activity,
Makoto Onuki99302b52017-03-29 12:42:26 -07001434 Icon icon, Intent intent, int rank) {
Makoto Onukib5a012f2016-06-21 11:13:53 -07001435 final ShortcutInfo.Builder b = new ShortcutInfo.Builder(mClientContext, id)
Makoto Onuki255461f2017-01-10 11:47:25 -08001436 .setActivity(new ComponentName(mClientContext.getPackageName(), "main"))
Makoto Onukib5a012f2016-06-21 11:13:53 -07001437 .setShortLabel(title)
Makoto Onuki99302b52017-03-29 12:42:26 -07001438 .setRank(rank)
1439 .setIntent(intent);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001440 if (icon != null) {
1441 b.setIcon(icon);
1442 }
1443 if (activity != null) {
1444 b.setActivity(activity);
1445 }
1446 final ShortcutInfo s = b.build();
1447
1448 s.setTimestamp(mInjectedCurrentTimeMillis); // HACK
1449
1450 return s;
1451 }
1452
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001453 protected ShortcutInfo makeShortcutWithIntents(String id, Intent... intents) {
1454 return makeShortcut(
1455 id, "Title-" + id, /* activity =*/ null, /* icon =*/ null,
1456 intents, /* rank =*/ 0);
1457 }
1458
1459 /**
1460 * Make a shortcut with details.
1461 */
1462 protected ShortcutInfo makeShortcut(String id, String title, ComponentName activity,
1463 Icon icon, Intent[] intents, int rank) {
1464 final ShortcutInfo.Builder b = new ShortcutInfo.Builder(mClientContext, id)
Makoto Onuki255461f2017-01-10 11:47:25 -08001465 .setActivity(new ComponentName(mClientContext.getPackageName(), "main"))
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001466 .setShortLabel(title)
1467 .setRank(rank)
1468 .setIntents(intents);
1469 if (icon != null) {
1470 b.setIcon(icon);
1471 }
1472 if (activity != null) {
1473 b.setActivity(activity);
1474 }
1475 final ShortcutInfo s = b.build();
1476
1477 s.setTimestamp(mInjectedCurrentTimeMillis); // HACK
1478
1479 return s;
1480 }
1481
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001482 /**
Makoto Onukib5a012f2016-06-21 11:13:53 -07001483 * Make a shortcut with details.
1484 */
1485 protected ShortcutInfo makeShortcutWithExtras(String id, Intent intent,
1486 PersistableBundle extras) {
1487 final ShortcutInfo.Builder b = new ShortcutInfo.Builder(mClientContext, id)
Makoto Onuki255461f2017-01-10 11:47:25 -08001488 .setActivity(new ComponentName(mClientContext.getPackageName(), "main"))
Makoto Onukib5a012f2016-06-21 11:13:53 -07001489 .setShortLabel("title-" + id)
1490 .setExtras(extras)
1491 .setIntent(intent);
1492 final ShortcutInfo s = b.build();
1493
1494 s.setTimestamp(mInjectedCurrentTimeMillis); // HACK
1495
1496 return s;
1497 }
1498
1499 /**
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001500 * Make an intent.
1501 */
1502 protected Intent makeIntent(String action, Class<?> clazz, Object... bundleKeysAndValues) {
1503 final Intent intent = new Intent(action);
1504 intent.setComponent(makeComponent(clazz));
1505 intent.replaceExtras(makeBundle(bundleKeysAndValues));
1506 return intent;
1507 }
1508
1509 /**
1510 * Make an component name, with the client context.
1511 */
1512 @NonNull
1513 protected ComponentName makeComponent(Class<?> clazz) {
1514 return new ComponentName(mClientContext, clazz);
1515 }
1516
1517 @NonNull
1518 protected ShortcutInfo findById(List<ShortcutInfo> list, String id) {
1519 for (ShortcutInfo s : list) {
1520 if (s.getId().equals(id)) {
1521 return s;
1522 }
1523 }
1524 fail("Shortcut with id " + id + " not found");
1525 return null;
1526 }
1527
1528 protected void assertSystem() {
1529 assertEquals("Caller must be system", Process.SYSTEM_UID, mInjectedCallingUid);
1530 }
1531
1532 protected void assertResetTimes(long expectedLastResetTime, long expectedNextResetTime) {
1533 assertEquals(expectedLastResetTime, mService.getLastResetTimeLocked());
1534 assertEquals(expectedNextResetTime, mService.getNextResetTimeLocked());
1535 }
1536
1537 public static List<ShortcutInfo> assertAllNotHaveIcon(
1538 List<ShortcutInfo> actualShortcuts) {
1539 for (ShortcutInfo s : actualShortcuts) {
1540 assertNull("ID " + s.getId(), s.getIcon());
1541 }
1542 return actualShortcuts;
1543 }
1544
1545 @NonNull
1546 protected List<ShortcutInfo> assertAllHaveFlags(@NonNull List<ShortcutInfo> actualShortcuts,
1547 int shortcutFlags) {
1548 for (ShortcutInfo s : actualShortcuts) {
1549 assertTrue("ID " + s.getId() + " doesn't have flags " + shortcutFlags,
1550 s.hasFlags(shortcutFlags));
1551 }
1552 return actualShortcuts;
1553 }
1554
1555 protected ShortcutInfo getPackageShortcut(String packageName, String shortcutId, int userId) {
1556 return mService.getPackageShortcutForTest(packageName, shortcutId, userId);
1557 }
1558
1559 protected void assertShortcutExists(String packageName, String shortcutId, int userId) {
1560 assertTrue(getPackageShortcut(packageName, shortcutId, userId) != null);
1561 }
1562
1563 protected void assertShortcutNotExists(String packageName, String shortcutId, int userId) {
1564 assertTrue(getPackageShortcut(packageName, shortcutId, userId) == null);
1565 }
1566
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001567 protected Intent[] launchShortcutAndGetIntentsInner(Runnable shortcutStarter,
1568 @NonNull String packageName, @NonNull String shortcutId, int userId) {
1569 reset(mMockActivityManagerInternal);
1570 shortcutStarter.run();
1571
1572 final ArgumentCaptor<Intent[]> intentsCaptor = ArgumentCaptor.forClass(Intent[].class);
1573 verify(mMockActivityManagerInternal).startActivitiesAsPackage(
1574 eq(packageName),
1575 eq(userId),
1576 intentsCaptor.capture(),
Makoto Onukid0010c52017-03-30 14:17:35 -07001577 anyOrNull(Bundle.class));
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001578 return intentsCaptor.getValue();
1579 }
1580
1581 protected Intent[] launchShortcutAndGetIntents(
1582 @NonNull String packageName, @NonNull String shortcutId, int userId) {
1583 return launchShortcutAndGetIntentsInner(
1584 () -> {
1585 mLauncherApps.startShortcut(packageName, shortcutId, null, null,
1586 UserHandle.of(userId));
1587 }, packageName, shortcutId, userId
1588 );
1589 }
1590
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001591 protected Intent launchShortcutAndGetIntent(
1592 @NonNull String packageName, @NonNull String shortcutId, int userId) {
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001593 final Intent[] intents = launchShortcutAndGetIntents(packageName, shortcutId, userId);
1594 assertEquals(1, intents.length);
1595 return intents[0];
1596 }
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001597
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001598 protected Intent[] launchShortcutAndGetIntents_withShortcutInfo(
1599 @NonNull String packageName, @NonNull String shortcutId, int userId) {
1600 return launchShortcutAndGetIntentsInner(
1601 () -> {
1602 mLauncherApps.startShortcut(
1603 getShortcutInfoAsLauncher(packageName, shortcutId, userId), null, null);
1604 }, packageName, shortcutId, userId
1605 );
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001606 }
1607
1608 protected Intent launchShortcutAndGetIntent_withShortcutInfo(
1609 @NonNull String packageName, @NonNull String shortcutId, int userId) {
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001610 final Intent[] intents = launchShortcutAndGetIntents_withShortcutInfo(
1611 packageName, shortcutId, userId);
1612 assertEquals(1, intents.length);
1613 return intents[0];
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001614 }
1615
1616 protected void assertShortcutLaunchable(@NonNull String packageName, @NonNull String shortcutId,
1617 int userId) {
1618 assertNotNull(launchShortcutAndGetIntent(packageName, shortcutId, userId));
1619 assertNotNull(launchShortcutAndGetIntent_withShortcutInfo(packageName, shortcutId, userId));
1620 }
1621
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001622 protected void assertShortcutNotLaunched(@NonNull String packageName,
1623 @NonNull String shortcutId, int userId) {
1624 reset(mMockActivityManagerInternal);
1625 try {
1626 mLauncherApps.startShortcut(packageName, shortcutId, null, null,
1627 UserHandle.of(userId));
1628 fail("ActivityNotFoundException was not thrown");
1629 } catch (ActivityNotFoundException expected) {
1630 }
1631 // This shouldn't have been called.
1632 verify(mMockActivityManagerInternal, times(0)).startActivitiesAsPackage(
1633 anyString(),
1634 anyInt(),
1635 any(Intent[].class),
Makoto Onukid0010c52017-03-30 14:17:35 -07001636 anyOrNull(Bundle.class));
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001637 }
1638
1639 protected void assertStartShortcutThrowsException(@NonNull String packageName,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07001640 @NonNull String shortcutId, int userId, Class<?> expectedException) {
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07001641 Exception thrown = null;
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001642 try {
Makoto Onukid6880792016-06-29 13:37:43 -07001643 mLauncherApps.startShortcut(packageName, shortcutId, null, null,
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001644 UserHandle.of(userId));
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07001645 } catch (Exception e) {
1646 thrown = e;
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001647 }
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07001648 assertNotNull("Exception was not thrown", thrown);
1649 assertEquals("Exception type different", expectedException, thrown.getClass());
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001650 }
1651
1652 protected void assertBitmapDirectories(int userId, String... expectedDirectories) {
1653 final Set<String> expected = hashSet(set(expectedDirectories));
1654
1655 final Set<String> actual = new HashSet<>();
1656
1657 final File[] files = mService.getUserBitmapFilePath(userId).listFiles();
1658 if (files != null) {
1659 for (File child : files) {
1660 if (child.isDirectory()) {
1661 actual.add(child.getName());
1662 }
1663 }
1664 }
1665
1666 assertEquals(expected, actual);
1667 }
1668
1669 protected void assertBitmapFiles(int userId, String packageName, String... expectedFiles) {
1670 final Set<String> expected = hashSet(set(expectedFiles));
1671
1672 final Set<String> actual = new HashSet<>();
1673
1674 final File[] files = new File(mService.getUserBitmapFilePath(userId), packageName)
1675 .listFiles();
1676 if (files != null) {
1677 for (File child : files) {
1678 if (child.isFile()) {
1679 actual.add(child.getName());
1680 }
1681 }
1682 }
1683
1684 assertEquals(expected, actual);
1685 }
1686
1687 protected String getBitmapFilename(int userId, String packageName, String shortcutId) {
1688 final ShortcutInfo si = mService.getPackageShortcutForTest(packageName, shortcutId, userId);
1689 if (si == null) {
1690 return null;
1691 }
1692 return new File(si.getBitmapPath()).getName();
1693 }
1694
Makoto Onukif3ba2e02016-07-12 09:18:50 -07001695 /**
1696 * @return all shortcuts stored internally for the caller. This reflects the *internal* view
1697 * of shortcuts, which may be different from what {@link #getCallerVisibleShortcuts} would
1698 * return, because getCallerVisibleShortcuts() will get shortcuts from the proper "front door"
1699 * which performs some extra checks, like {@link ShortcutPackage#onRestored}.
1700 */
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001701 protected List<ShortcutInfo> getCallerShortcuts() {
1702 final ShortcutPackage p = mService.getPackageShortcutForTest(
1703 getCallingPackage(), getCallingUserId());
1704 return p == null ? null : p.getAllShortcutsForTest();
1705 }
1706
Makoto Onukif3ba2e02016-07-12 09:18:50 -07001707 /**
1708 * @return all shortcuts owned by caller that are actually visible via ShortcutManager.
1709 * See also {@link #getCallerShortcuts}.
1710 */
1711 protected List<ShortcutInfo> getCallerVisibleShortcuts() {
1712 final ArrayList<ShortcutInfo> ret = new ArrayList<>();
1713 ret.addAll(mManager.getDynamicShortcuts());
1714 ret.addAll(mManager.getPinnedShortcuts());
1715 ret.addAll(mManager.getManifestShortcuts());
1716 return ret;
1717 }
1718
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001719 protected ShortcutInfo getCallerShortcut(String shortcutId) {
1720 return getPackageShortcut(getCallingPackage(), shortcutId, getCallingUserId());
1721 }
1722
1723 protected List<ShortcutInfo> getLauncherShortcuts(String launcher, int userId, int queryFlags) {
1724 final List<ShortcutInfo>[] ret = new List[1];
1725 runWithCaller(launcher, userId, () -> {
1726 final ShortcutQuery q = new ShortcutQuery();
1727 q.setQueryFlags(queryFlags);
1728 ret[0] = mLauncherApps.getShortcuts(q, UserHandle.of(userId));
1729 });
1730 return ret[0];
1731 }
1732
1733 protected List<ShortcutInfo> getLauncherPinnedShortcuts(String launcher, int userId) {
1734 return getLauncherShortcuts(launcher, userId, ShortcutQuery.FLAG_GET_PINNED);
1735 }
1736
Makoto Onukia01f4f02016-12-15 15:58:41 -08001737 protected List<ShortcutInfo> getShortcutAsLauncher(int targetUserId) {
1738 final ShortcutQuery q = new ShortcutQuery();
1739 q.setQueryFlags(ShortcutQuery.FLAG_MATCH_DYNAMIC | ShortcutQuery.FLAG_MATCH_DYNAMIC
1740 | ShortcutQuery.FLAG_MATCH_PINNED);
1741 return mLauncherApps.getShortcuts(q, UserHandle.of(targetUserId));
1742 }
1743
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001744 protected ShortcutInfo getShortcutInfoAsLauncher(String packageName, String shortcutId,
1745 int userId) {
1746 final List<ShortcutInfo> infoList =
1747 mLauncherApps.getShortcutInfo(packageName, list(shortcutId),
1748 UserHandle.of(userId));
1749 assertEquals("No shortcutInfo found (or too many of them)", 1, infoList.size());
1750 return infoList.get(0);
1751 }
1752
Makoto Onukiee6b6e42016-06-29 17:34:02 -07001753 protected Intent genPackageAddIntent(String packageName, int userId) {
1754 installPackage(userId, packageName);
1755
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001756 Intent i = new Intent(Intent.ACTION_PACKAGE_ADDED);
Makoto Onukiee6b6e42016-06-29 17:34:02 -07001757 i.setData(Uri.parse("package:" + packageName));
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001758 i.putExtra(Intent.EXTRA_USER_HANDLE, userId);
1759 return i;
1760 }
1761
1762 protected Intent genPackageDeleteIntent(String pakcageName, int userId) {
Makoto Onukiee6b6e42016-06-29 17:34:02 -07001763 uninstallPackage(userId, pakcageName);
1764
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001765 Intent i = new Intent(Intent.ACTION_PACKAGE_REMOVED);
1766 i.setData(Uri.parse("package:" + pakcageName));
1767 i.putExtra(Intent.EXTRA_USER_HANDLE, userId);
1768 return i;
1769 }
1770
1771 protected Intent genPackageUpdateIntent(String pakcageName, int userId) {
Makoto Onukiee6b6e42016-06-29 17:34:02 -07001772 installPackage(userId, pakcageName);
1773
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001774 Intent i = new Intent(Intent.ACTION_PACKAGE_ADDED);
1775 i.setData(Uri.parse("package:" + pakcageName));
1776 i.putExtra(Intent.EXTRA_USER_HANDLE, userId);
1777 i.putExtra(Intent.EXTRA_REPLACING, true);
1778 return i;
1779 }
1780
Makoto Onukib08790c2016-06-23 14:05:46 -07001781 protected Intent genPackageChangedIntent(String pakcageName, int userId) {
1782 Intent i = new Intent(Intent.ACTION_PACKAGE_CHANGED);
1783 i.setData(Uri.parse("package:" + pakcageName));
1784 i.putExtra(Intent.EXTRA_USER_HANDLE, userId);
1785 return i;
1786 }
1787
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001788 protected Intent genPackageDataClear(String packageName, int userId) {
1789 Intent i = new Intent(Intent.ACTION_PACKAGE_DATA_CLEARED);
1790 i.setData(Uri.parse("package:" + packageName));
1791 i.putExtra(Intent.EXTRA_USER_HANDLE, userId);
1792 return i;
1793 }
1794
1795 protected void assertExistsAndShadow(ShortcutPackageItem spi) {
1796 assertNotNull(spi);
1797 assertTrue(spi.getPackageInfo().isShadow());
1798 }
1799
1800 protected File makeFile(File baseDirectory, String... paths) {
1801 File ret = baseDirectory;
1802
1803 for (String path : paths) {
1804 ret = new File(ret, path);
1805 }
1806
1807 return ret;
1808 }
1809
1810 protected boolean bitmapDirectoryExists(String packageName, int userId) {
1811 final File path = new File(mService.getUserBitmapFilePath(userId), packageName);
1812 return path.isDirectory();
1813 }
1814 protected static ShortcutQuery buildQuery(long changedSince,
1815 String packageName, ComponentName componentName,
1816 /* @ShortcutQuery.QueryFlags */ int flags) {
1817 return buildQuery(changedSince, packageName, null, componentName, flags);
1818 }
1819
1820 protected static ShortcutQuery buildQuery(long changedSince,
1821 String packageName, List<String> shortcutIds, ComponentName componentName,
1822 /* @ShortcutQuery.QueryFlags */ int flags) {
1823 final ShortcutQuery q = new ShortcutQuery();
1824 q.setChangedSince(changedSince);
1825 q.setPackage(packageName);
1826 q.setShortcutIds(shortcutIds);
1827 q.setActivity(componentName);
1828 q.setQueryFlags(flags);
1829 return q;
1830 }
1831
1832 protected static ShortcutQuery buildAllQuery(String packageName) {
1833 final ShortcutQuery q = new ShortcutQuery();
1834 q.setPackage(packageName);
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07001835 q.setQueryFlags(ShortcutQuery.FLAG_GET_ALL_KINDS);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001836 return q;
1837 }
1838
1839 protected static ShortcutQuery buildPinnedQuery(String packageName) {
1840 final ShortcutQuery q = new ShortcutQuery();
1841 q.setPackage(packageName);
1842 q.setQueryFlags(ShortcutQuery.FLAG_GET_PINNED);
1843 return q;
1844 }
1845
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07001846 protected static ShortcutQuery buildQueryWithFlags(int queryFlags) {
1847 final ShortcutQuery q = new ShortcutQuery();
1848 q.setQueryFlags(queryFlags);
1849 return q;
1850 }
1851
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001852 protected void backupAndRestore() {
1853 int prevUid = mInjectedCallingUid;
1854
1855 mInjectedCallingUid = Process.SYSTEM_UID; // Only system can call it.
1856
1857 dumpsysOnLogcat("Before backup");
1858
1859 final byte[] payload = mService.getBackupPayload(USER_0);
1860 if (ENABLE_DUMP) {
1861 final String xml = new String(payload);
1862 Log.v(TAG, "Backup payload:");
1863 for (String line : xml.split("\n")) {
1864 Log.v(TAG, line);
1865 }
1866 }
1867
1868 // Before doing anything else, uninstall all packages.
1869 for (int userId : list(USER_0, USER_P0)) {
1870 for (String pkg : list(CALLING_PACKAGE_1, CALLING_PACKAGE_2, CALLING_PACKAGE_3,
1871 LAUNCHER_1, LAUNCHER_2, LAUNCHER_3)) {
1872 uninstallPackage(userId, pkg);
1873 }
1874 }
1875
1876 shutdownServices();
1877
1878 deleteAllSavedFiles();
1879
1880 initService();
1881 mService.applyRestore(payload, USER_0);
1882
1883 // handleUnlockUser will perform the gone package check, but it shouldn't remove
1884 // shadow information.
1885 mService.handleUnlockUser(USER_0);
1886
1887 dumpsysOnLogcat("After restore");
1888
1889 mInjectedCallingUid = prevUid;
1890 }
1891
1892 protected void prepareCrossProfileDataSet() {
Makoto Onuki9c850012016-07-26 15:50:50 -07001893 mRunningUsers.put(USER_10, true); // this test needs user 10.
1894
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001895 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1896 assertTrue(mManager.setDynamicShortcuts(list(
1897 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"),
1898 makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6"))));
1899 });
1900 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
1901 assertTrue(mManager.setDynamicShortcuts(list(
1902 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"),
1903 makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6"))));
1904 });
1905 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
1906 assertTrue(mManager.setDynamicShortcuts(list(
1907 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"),
1908 makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6"))));
1909 });
1910 runWithCaller(CALLING_PACKAGE_4, USER_0, () -> {
1911 assertTrue(mManager.setDynamicShortcuts(list()));
1912 });
1913 runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
1914 assertTrue(mManager.setDynamicShortcuts(list(
1915 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"),
1916 makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6"))));
1917 });
1918 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
1919 assertTrue(mManager.setDynamicShortcuts(list(
1920 makeShortcut("x1"), makeShortcut("x2"), makeShortcut("x3"),
1921 makeShortcut("x4"), makeShortcut("x5"), makeShortcut("x6"))));
1922 });
1923
1924 runWithCaller(LAUNCHER_1, USER_0, () -> {
1925 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s1"), HANDLE_USER_0);
1926 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s1", "s2"), HANDLE_USER_0);
1927 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("s1", "s2", "s3"), HANDLE_USER_0);
1928
1929 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s1", "s4"), HANDLE_USER_P0);
1930 });
1931 runWithCaller(LAUNCHER_2, USER_0, () -> {
1932 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s2"), HANDLE_USER_0);
1933 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s2", "s3"), HANDLE_USER_0);
1934 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("s2", "s3", "s4"), HANDLE_USER_0);
1935
1936 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s2", "s5"), HANDLE_USER_P0);
1937 });
Makoto Onukif3ba2e02016-07-12 09:18:50 -07001938
1939 // Note LAUNCHER_3 has allowBackup=false.
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001940 runWithCaller(LAUNCHER_3, USER_0, () -> {
1941 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s3"), HANDLE_USER_0);
1942 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s3", "s4"), HANDLE_USER_0);
1943 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("s3", "s4", "s5"), HANDLE_USER_0);
1944
1945 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s3", "s6"), HANDLE_USER_P0);
1946 });
1947 runWithCaller(LAUNCHER_4, USER_0, () -> {
1948 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list(), HANDLE_USER_0);
1949 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list(), HANDLE_USER_0);
1950 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list(), HANDLE_USER_0);
1951 mLauncherApps.pinShortcuts(CALLING_PACKAGE_4, list(), HANDLE_USER_0);
1952 });
1953
1954 // Launcher on a managed profile is referring ot user 0!
1955 runWithCaller(LAUNCHER_1, USER_P0, () -> {
1956 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s3", "s4"), HANDLE_USER_0);
1957 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s3", "s4", "s5"), HANDLE_USER_0);
1958 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("s3", "s4", "s5", "s6"),
1959 HANDLE_USER_0);
1960
1961 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s4", "s1"), HANDLE_USER_P0);
1962 });
1963 runWithCaller(LAUNCHER_1, USER_10, () -> {
1964 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("x4", "x5"), HANDLE_USER_10);
1965 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("x4", "x5", "x6"), HANDLE_USER_10);
1966 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("x4", "x5", "x6", "x1"),
1967 HANDLE_USER_10);
1968 });
1969
1970 // Then remove some dynamic shortcuts.
1971 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1972 assertTrue(mManager.setDynamicShortcuts(list(
1973 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
1974 });
1975 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
1976 assertTrue(mManager.setDynamicShortcuts(list(
1977 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
1978 });
1979 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
1980 assertTrue(mManager.setDynamicShortcuts(list(
1981 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
1982 });
1983 runWithCaller(CALLING_PACKAGE_4, USER_0, () -> {
1984 assertTrue(mManager.setDynamicShortcuts(list()));
1985 });
1986 runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
1987 assertTrue(mManager.setDynamicShortcuts(list(
1988 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
1989 });
1990 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
1991 assertTrue(mManager.setDynamicShortcuts(list(
1992 makeShortcut("x1"), makeShortcut("x2"), makeShortcut("x3"))));
1993 });
1994 }
Makoto Onukib5a012f2016-06-21 11:13:53 -07001995
1996 public static List<ShortcutInfo> assertAllHaveIconResId(
1997 List<ShortcutInfo> actualShortcuts) {
1998 for (ShortcutInfo s : actualShortcuts) {
1999 assertTrue("ID " + s.getId() + " not have icon res ID", s.hasIconResource());
2000 assertFalse("ID " + s.getId() + " shouldn't have icon FD", s.hasIconFile());
2001 }
2002 return actualShortcuts;
2003 }
2004
2005 public static List<ShortcutInfo> assertAllHaveIconFile(
2006 List<ShortcutInfo> actualShortcuts) {
2007 for (ShortcutInfo s : actualShortcuts) {
2008 assertFalse("ID " + s.getId() + " shouldn't have icon res ID", s.hasIconResource());
2009 assertTrue("ID " + s.getId() + " not have icon FD", s.hasIconFile());
2010 }
2011 return actualShortcuts;
2012 }
2013
2014 public static List<ShortcutInfo> assertAllHaveIcon(
2015 List<ShortcutInfo> actualShortcuts) {
2016 for (ShortcutInfo s : actualShortcuts) {
Makoto Onukia01f4f02016-12-15 15:58:41 -08002017 assertTrue("ID " + s.getId() + " has no icon ",
2018 s.hasIconFile() || s.hasIconResource() || s.getIcon() != null);
Makoto Onukib5a012f2016-06-21 11:13:53 -07002019 }
2020 return actualShortcuts;
2021 }
2022
2023 public static List<ShortcutInfo> assertAllStringsResolved(
2024 List<ShortcutInfo> actualShortcuts) {
2025 for (ShortcutInfo s : actualShortcuts) {
2026 assertTrue("ID " + s.getId(), s.hasStringResourcesResolved());
2027 }
2028 return actualShortcuts;
2029 }
Makoto Onuki76269922016-07-15 14:58:54 -07002030
2031 public String readTestAsset(String assetPath) throws IOException {
2032 final StringBuilder sb = new StringBuilder();
2033 try (BufferedReader br = new BufferedReader(
2034 new InputStreamReader(
2035 getTestContext().getResources().getAssets().open(assetPath)))) {
2036 String line;
2037 while ((line = br.readLine()) != null) {
2038 sb.append(line);
2039 sb.append(System.lineSeparator());
2040 }
2041 }
2042 return sb.toString();
2043 }
Makoto Onuki0b9d1db2016-07-18 14:16:41 -07002044
2045 protected void prepareGetHomeActivitiesAsUser(ComponentName preferred,
2046 List<ResolveInfo> candidates, int userId) {
2047 doAnswer(inv -> {
2048 ((List) inv.getArguments()[0]).addAll(candidates);
2049 return preferred;
2050 }).when(mMockPackageManagerInternal).getHomeActivitiesAsUser(any(List.class), eq(userId));
2051 }
2052
2053 protected static ComponentName cn(String packageName, String name) {
2054 return new ComponentName(packageName, name);
2055 }
2056
2057 protected static ResolveInfo ri(String packageName, String name, boolean isSystem, int priority) {
2058 final ResolveInfo ri = new ResolveInfo();
2059 ri.activityInfo = new ActivityInfo();
2060 ri.activityInfo.applicationInfo = new ApplicationInfo();
2061
2062 ri.activityInfo.packageName = packageName;
2063 ri.activityInfo.name = name;
2064 if (isSystem) {
2065 ri.activityInfo.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
2066 }
2067 ri.priority = priority;
2068 return ri;
2069 }
2070
2071 protected static ResolveInfo getSystemLauncher() {
2072 return ri(PACKAGE_SYSTEM_LAUNCHER, PACKAGE_SYSTEM_LAUNCHER_NAME, true,
2073 PACKAGE_SYSTEM_LAUNCHER_PRIORITY);
2074 }
2075
2076 protected static ResolveInfo getFallbackLauncher() {
2077 return ri(PACKAGE_FALLBACK_LAUNCHER, PACKAGE_FALLBACK_LAUNCHER_NAME, true,
2078 PACKAGE_FALLBACK_LAUNCHER_PRIORITY);
2079 }
Makoto Onukia01f4f02016-12-15 15:58:41 -08002080
2081 protected void makeCallerForeground() {
2082 try {
2083 mService.mUidObserver.onUidStateChanged(
Sudheer Shanka80255802017-03-04 14:48:53 -08002084 mInjectedCallingUid, ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE, 0);
Makoto Onukia01f4f02016-12-15 15:58:41 -08002085 } catch (RemoteException e) {
2086 e.rethrowAsRuntimeException();
2087 }
2088 }
2089
2090 protected void makeCallerBackground() {
2091 try {
2092 mService.mUidObserver.onUidStateChanged(
Sudheer Shanka80255802017-03-04 14:48:53 -08002093 mInjectedCallingUid, ActivityManager.PROCESS_STATE_TOP_SLEEPING, 0);
Makoto Onukia01f4f02016-12-15 15:58:41 -08002094 } catch (RemoteException e) {
2095 e.rethrowAsRuntimeException();
2096 }
2097 }
2098
2099 protected void publishManifestShortcutsAsCaller(int resId) {
2100 addManifestShortcutResource(
2101 new ComponentName(getCallingPackage(), ShortcutActivity.class.getName()),
2102 resId);
2103 updatePackageVersion(getCallingPackage(), 1);
2104 mService.mPackageMonitor.onReceive(getTestContext(),
2105 genPackageAddIntent(getCallingPackage(), getCallingUserId()));
2106 }
Makoto Onuki51ab2b32016-06-02 11:03:51 -07002107}