blob: 3d64f2a1e95a147bb82330dcf9f3bff5b39fd464 [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;
Wale Ogunwale6767eae2018-05-03 15:52:51 -070044import android.app.ActivityTaskManagerInternal;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070045import android.app.IUidObserver;
Makoto Onukiac042502016-05-20 16:39:42 -070046import android.app.usage.UsageStatsManagerInternal;
Makoto Onuki440a1ea2016-07-20 14:21:18 -070047import android.content.ActivityNotFoundException;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070048import android.content.BroadcastReceiver;
49import android.content.ComponentName;
50import android.content.Context;
51import android.content.Intent;
52import android.content.IntentFilter;
Makoto Onuki2d895c32016-12-02 15:48:40 -080053import android.content.IntentSender;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070054import android.content.pm.ActivityInfo;
55import android.content.pm.ApplicationInfo;
56import android.content.pm.ILauncherApps;
57import android.content.pm.LauncherApps;
58import android.content.pm.LauncherApps.ShortcutQuery;
59import android.content.pm.PackageInfo;
60import android.content.pm.PackageManager;
61import android.content.pm.PackageManagerInternal;
Dan Cashman5c9f527e2018-04-03 16:42:23 -070062import android.content.pm.PackageParser;
Makoto Onukib08790c2016-06-23 14:05:46 -070063import android.content.pm.ResolveInfo;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070064import android.content.pm.ShortcutInfo;
65import android.content.pm.ShortcutManager;
66import android.content.pm.ShortcutServiceInternal;
67import android.content.pm.Signature;
Dan Cashman5c9f527e2018-04-03 16:42:23 -070068import android.content.pm.SigningInfo;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070069import android.content.pm.UserInfo;
70import android.content.res.Resources;
71import android.content.res.XmlResourceParser;
72import android.graphics.drawable.Icon;
73import android.net.Uri;
74import android.os.Bundle;
75import android.os.FileUtils;
76import android.os.Handler;
77import android.os.Looper;
Makoto Onukib5a012f2016-06-21 11:13:53 -070078import android.os.PersistableBundle;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070079import android.os.Process;
Makoto Onukia01f4f02016-12-15 15:58:41 -080080import android.os.RemoteException;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070081import android.os.UserHandle;
82import android.os.UserManager;
Sunny Goyal145c8f82018-02-15 14:27:09 -080083import android.os.UserManagerInternal;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070084import android.test.InstrumentationTestCase;
85import android.test.mock.MockContext;
Makoto Onuki2d895c32016-12-02 15:48:40 -080086import android.util.ArrayMap;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070087import android.util.Log;
88import android.util.Pair;
89
90import com.android.internal.util.Preconditions;
91import com.android.server.LocalServices;
92import com.android.server.SystemService;
93import com.android.server.pm.LauncherAppsService.LauncherAppsImpl;
94import com.android.server.pm.ShortcutUser.PackageWithUser;
95
96import org.junit.Assert;
97import org.mockito.ArgumentCaptor;
Makoto Onukie70b29e2018-04-03 16:44:39 -070098import org.mockito.invocation.InvocationOnMock;
99import org.mockito.stubbing.Answer;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700100
101import java.io.BufferedReader;
102import java.io.ByteArrayOutputStream;
103import java.io.File;
104import java.io.FileReader;
Makoto Onuki76269922016-07-15 14:58:54 -0700105import java.io.IOException;
106import java.io.InputStreamReader;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700107import java.io.PrintWriter;
Dan Cashman5c9f527e2018-04-03 16:42:23 -0700108import java.security.cert.CertificateEncodingException;
109import java.security.cert.CertificateException;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700110import java.util.ArrayList;
111import java.util.HashMap;
112import java.util.HashSet;
113import java.util.LinkedHashMap;
114import java.util.List;
Makoto Onuki157b1622016-06-02 16:13:10 -0700115import java.util.Locale;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700116import java.util.Map;
117import java.util.Set;
Makoto Onukib08790c2016-06-23 14:05:46 -0700118import java.util.function.BiFunction;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700119import java.util.function.BiPredicate;
120import java.util.function.Consumer;
Makoto Onukie70b29e2018-04-03 16:44:39 -0700121import java.util.function.Function;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700122
123public abstract class BaseShortcutManagerTest extends InstrumentationTestCase {
124 protected static final String TAG = "ShortcutManagerTest";
125
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700126 protected static final boolean DUMP_IN_TEARDOWN = false; // DO NOT SUBMIT WITH true
127
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700128 /**
129 * Whether to enable dump or not. Should be only true when debugging to avoid bugs where
130 * dump affecting the behavior.
131 */
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700132 protected static final boolean ENABLE_DUMP = false // DO NOT SUBMIT WITH true
133 || DUMP_IN_TEARDOWN || ShortcutService.DEBUG;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700134
135 protected static final String[] EMPTY_STRINGS = new String[0]; // Just for readability.
136
Makoto Onukib08790c2016-06-23 14:05:46 -0700137 protected static final String MAIN_ACTIVITY_CLASS = "MainActivity";
Makoto Onuki2d895c32016-12-02 15:48:40 -0800138 protected static final String PIN_CONFIRM_ACTIVITY_CLASS = "PinConfirmActivity";
Makoto Onukib08790c2016-06-23 14:05:46 -0700139
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700140 // public for mockito
141 public class BaseContext extends MockContext {
142 @Override
143 public Object getSystemService(String name) {
144 switch (name) {
145 case Context.USER_SERVICE:
146 return mMockUserManager;
147 }
148 throw new UnsupportedOperationException();
149 }
150
151 @Override
152 public String getSystemServiceName(Class<?> serviceClass) {
153 return getTestContext().getSystemServiceName(serviceClass);
154 }
155
156 @Override
157 public PackageManager getPackageManager() {
158 return mMockPackageManager;
159 }
160
161 @Override
162 public Resources getResources() {
163 return getTestContext().getResources();
164 }
165
166 @Override
167 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
168 IntentFilter filter, String broadcastPermission, Handler scheduler) {
169 // ignore.
170 return null;
171 }
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700172
173 @Override
174 public void unregisterReceiver(BroadcastReceiver receiver) {
175 // ignore.
176 }
Makoto Onuki2d895c32016-12-02 15:48:40 -0800177
178 @Override
179 public void startActivityAsUser(Intent intent, UserHandle user) {
180 // ignore, use spy to intercept it.
181 }
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700182 }
183
184 /** Context used in the client side */
185 public class ClientContext extends BaseContext {
186 @Override
187 public String getPackageName() {
188 return mInjectedClientPackage;
189 }
190
191 @Override
192 public int getUserId() {
193 return getCallingUserId();
194 }
195 }
196
197 /** Context used in the service side */
198 public class ServiceContext extends BaseContext {
199 long injectClearCallingIdentity() {
200 final int prevCallingUid = mInjectedCallingUid;
201 mInjectedCallingUid = Process.SYSTEM_UID;
202 return prevCallingUid;
203 }
204
205 void injectRestoreCallingIdentity(long token) {
206 mInjectedCallingUid = (int) token;
207 }
208
209 @Override
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700210 public int getUserId() {
211 return UserHandle.USER_SYSTEM;
212 }
213
214 public PackageInfo injectGetActivitiesWithMetadata(
215 String packageName, @UserIdInt int userId) {
216 return BaseShortcutManagerTest.this.injectGetActivitiesWithMetadata(packageName, userId);
217 }
218
219 public XmlResourceParser injectXmlMetaData(ActivityInfo activityInfo, String key) {
220 return BaseShortcutManagerTest.this.injectXmlMetaData(activityInfo, key);
221 }
Makoto Onuki2d895c32016-12-02 15:48:40 -0800222
223 public void sendIntentSender(IntentSender intent) {
224 // Placeholder for spying.
225 }
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700226 }
227
228 /** ShortcutService with injection override methods. */
229 protected final class ShortcutServiceTestable extends ShortcutService {
230 final ServiceContext mContext;
231 IUidObserver mUidObserver;
232
233 public ShortcutServiceTestable(ServiceContext context, Looper looper) {
Makoto Onukiee6b6e42016-06-29 17:34:02 -0700234 super(context, looper, /* onyForPackageManagerApis */ false);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700235 mContext = context;
236 }
237
238 @Override
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700239 public String injectGetLocaleTagsForUser(@UserIdInt int userId) {
240 return mInjectedLocale.toLanguageTag();
241 }
242
243 @Override
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700244 boolean injectShouldPerformVerification() {
245 return true; // Always verify during unit tests.
246 }
247
248 @Override
249 String injectShortcutManagerConstants() {
250 return ConfigConstants.KEY_RESET_INTERVAL_SEC + "=" + (INTERVAL / 1000) + ","
251 + ConfigConstants.KEY_MAX_SHORTCUTS + "=" + MAX_SHORTCUTS + ","
252 + ConfigConstants.KEY_MAX_UPDATES_PER_INTERVAL + "="
253 + MAX_UPDATES_PER_INTERVAL + ","
254 + ConfigConstants.KEY_MAX_ICON_DIMENSION_DP + "=" + MAX_ICON_DIMENSION + ","
255 + ConfigConstants.KEY_MAX_ICON_DIMENSION_DP_LOWRAM + "="
256 + MAX_ICON_DIMENSION_LOWRAM + ","
257 + ConfigConstants.KEY_ICON_FORMAT + "=PNG,"
258 + ConfigConstants.KEY_ICON_QUALITY + "=100";
259 }
260
261 @Override
262 long injectClearCallingIdentity() {
263 return mContext.injectClearCallingIdentity();
264 }
265
266 @Override
267 void injectRestoreCallingIdentity(long token) {
268 mContext.injectRestoreCallingIdentity(token);
269 }
270
271 @Override
272 int injectDipToPixel(int dip) {
273 return dip;
274 }
275
276 @Override
277 long injectCurrentTimeMillis() {
278 return mInjectedCurrentTimeMillis;
279 }
280
281 @Override
282 long injectElapsedRealtime() {
283 // TODO This should be kept separately from mInjectedCurrentTimeMillis, since
284 // this should increase even if we rewind mInjectedCurrentTimeMillis in some tests.
285 return mInjectedCurrentTimeMillis - START_TIME;
286 }
287
288 @Override
Makoto Onuki475c3652017-05-08 14:29:03 -0700289 long injectUptimeMillis() {
290 return mInjectedCurrentTimeMillis - START_TIME - mDeepSleepTime;
291 }
292
293 @Override
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700294 int injectBinderCallingUid() {
295 return mInjectedCallingUid;
296 }
297
298 @Override
Makoto Onuki7d0fa812018-02-21 11:24:43 -0800299 int injectBinderCallingPid() {
300 // Note it's not used in tests, so just return a "random" value.
301 return mInjectedCallingUid * 123;
302 }
303
304 @Override
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700305 int injectGetPackageUid(String packageName, int userId) {
306 return getInjectedPackageInfo(packageName, userId, false).applicationInfo.uid;
307 }
308
309 @Override
310 File injectSystemDataPath() {
311 return new File(mInjectedFilePathRoot, "system");
312 }
313
314 @Override
315 File injectUserDataPath(@UserIdInt int userId) {
316 return new File(mInjectedFilePathRoot, "user-" + userId);
317 }
318
319 @Override
320 void injectValidateIconResPackage(ShortcutInfo shortcut, Icon icon) {
321 // Can't check
322 }
323
324 @Override
325 boolean injectIsLowRamDevice() {
326 return mInjectedIsLowRamDevice;
327 }
328
329 @Override
330 void injectRegisterUidObserver(IUidObserver observer, int which) {
331 mUidObserver = observer;
332 }
333
334 @Override
Makoto Onuki634cecb2017-10-13 17:10:48 -0700335 boolean hasShortcutHostPermission(@NonNull String callingPackage, int userId,
336 int callingPid, int callingUid) {
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700337 return mDefaultLauncherChecker.test(callingPackage, userId);
338 }
339
340 @Override
Makoto Onuki7d0fa812018-02-21 11:24:43 -0800341 boolean injectHasUnlimitedShortcutsApiCallsPermission(int callingPid, int callingUid) {
342 return mInjectHasUnlimitedShortcutsApiCallsPermission;
343 }
344
345 @Override
Makoto Onuki2d895c32016-12-02 15:48:40 -0800346 ComponentName getDefaultLauncher(@UserIdInt int userId) {
347 final ComponentName activity = mDefaultLauncher.get(userId);
348 if (activity != null) {
349 return activity;
350 }
351 return super.getDefaultLauncher(userId);
352 }
353
354 @Override
Makoto Onukiee6b6e42016-06-29 17:34:02 -0700355 PackageInfo injectPackageInfoWithUninstalled(String packageName, @UserIdInt int userId,
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700356 boolean getSignatures) {
357 return getInjectedPackageInfo(packageName, userId, getSignatures);
358 }
359
360 @Override
Makoto Onukiee6b6e42016-06-29 17:34:02 -0700361 ApplicationInfo injectApplicationInfoWithUninstalled(
362 String packageName, @UserIdInt int userId) {
363 PackageInfo pi = injectPackageInfoWithUninstalled(
364 packageName, userId, /* getSignatures= */ false);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700365 return pi != null ? pi.applicationInfo : null;
366 }
367
368 @Override
Makoto Onukiee6b6e42016-06-29 17:34:02 -0700369 List<PackageInfo> injectGetPackagesWithUninstalled(@UserIdInt int userId) {
370 return BaseShortcutManagerTest.this.getInstalledPackagesWithUninstalled(userId);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700371 }
372
373 @Override
Makoto Onukiee6b6e42016-06-29 17:34:02 -0700374 ActivityInfo injectGetActivityInfoWithMetadataWithUninstalled(ComponentName activity,
Makoto Onukib08790c2016-06-23 14:05:46 -0700375 @UserIdInt int userId) {
376 final PackageInfo pi = mContext.injectGetActivitiesWithMetadata(
377 activity.getPackageName(), userId);
378 if (pi == null || pi.activities == null) {
379 return null;
380 }
381 for (ActivityInfo ai : pi.activities) {
382 if (!mEnabledActivityChecker.test(ai.getComponentName(), userId)) {
383 continue;
384 }
385 if (activity.equals(ai.getComponentName())) {
386 return ai;
387 }
388 }
389 return null;
390 }
391
392 @Override
393 boolean injectIsMainActivity(@NonNull ComponentName activity, int userId) {
394 if (!mEnabledActivityChecker.test(activity, userId)) {
395 return false;
396 }
397 return mMainActivityChecker.test(activity, userId);
398 }
399
400 @Override
401 List<ResolveInfo> injectGetMainActivities(@NonNull String packageName, int userId) {
402 final PackageInfo pi = mContext.injectGetActivitiesWithMetadata(
403 packageName, userId);
404 if (pi == null || pi.activities == null) {
405 return null;
406 }
407 final ArrayList<ResolveInfo> ret = new ArrayList<>(pi.activities.length);
408 for (int i = 0; i < pi.activities.length; i++) {
409 if (!mEnabledActivityChecker.test(pi.activities[i].getComponentName(), userId)) {
410 continue;
411 }
412 final ResolveInfo ri = new ResolveInfo();
413 ri.activityInfo = pi.activities[i];
414 ret.add(ri);
415 }
416
417 return ret;
418 }
419
420 @Override
421 ComponentName injectGetDefaultMainActivity(@NonNull String packageName, int userId) {
422 return mMainActivityFetcher.apply(packageName, userId);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700423 }
424
425 @Override
Makoto Onuki2d895c32016-12-02 15:48:40 -0800426 ComponentName injectGetPinConfirmationActivity(@NonNull String launcherPackageName,
Sunny Goyal7f7372a2017-01-24 11:53:54 -0800427 int launcherUserId, int requestType) {
Makoto Onuki2d895c32016-12-02 15:48:40 -0800428 return mPinConfirmActivityFetcher.apply(launcherPackageName, launcherUserId);
429 }
430
431 @Override
Makoto Onukiee6b6e42016-06-29 17:34:02 -0700432 boolean injectIsActivityEnabledAndExported(ComponentName activity, @UserIdInt int userId) {
433 return mEnabledActivityChecker.test(activity, userId);
434 }
435
436 @Override
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700437 XmlResourceParser injectXmlMetaData(ActivityInfo activityInfo, String key) {
438 return mContext.injectXmlMetaData(activityInfo, key);
439 }
440
441 @Override
Makoto Onuki157b1622016-06-02 16:13:10 -0700442 void injectPostToHandler(Runnable r) {
Makoto Onukia2241832016-07-06 13:28:37 -0700443 runOnHandler(r);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700444 }
445
446 @Override
Makoto Onuki085a05c2016-08-19 11:39:29 -0700447 void injectRunOnNewThread(Runnable r) {
448 runOnHandler(r);
449 }
450
451 @Override
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700452 void injectEnforceCallingPermission(String permission, String message) {
453 if (!mCallerPermissions.contains(permission)) {
454 throw new SecurityException("Missing permission: " + permission);
455 }
456 }
457
458 @Override
Makoto Onukib08790c2016-06-23 14:05:46 -0700459 boolean injectIsSafeModeEnabled() {
460 return mSafeMode;
461 }
462
463 @Override
Makoto Onuki33663282016-08-22 16:19:04 -0700464 String injectBuildFingerprint() {
465 return mInjectedBuildFingerprint;
466 }
467
468 @Override
Sunny Goyal87a563e2017-01-01 19:42:45 -0800469 void injectSendIntentSender(IntentSender intent, Intent extras) {
Makoto Onuki2d895c32016-12-02 15:48:40 -0800470 mContext.sendIntentSender(intent);
471 }
472
473 @Override
Makoto Onuki35559d62017-11-06 16:26:32 -0800474 boolean injectHasAccessShortcutsPermission(int callingPid, int callingUid) {
Makoto Onuki634cecb2017-10-13 17:10:48 -0700475 return mInjectCheckAccessShortcutsPermission;
476 }
477
478 @Override
Makoto Onukia2241832016-07-06 13:28:37 -0700479 void wtf(String message, Throwable th) {
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700480 // During tests, WTF is fatal.
Makoto Onukia2241832016-07-06 13:28:37 -0700481 fail(message + " exception: " + th + "\n" + Log.getStackTraceString(th));
482 }
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700483 }
484
485 /** ShortcutManager with injection override methods. */
486 protected class ShortcutManagerTestable extends ShortcutManager {
487 public ShortcutManagerTestable(Context context, ShortcutServiceTestable service) {
488 super(context, service);
489 }
490
491 @Override
492 protected int injectMyUserId() {
493 return UserHandle.getUserId(mInjectedCallingUid);
494 }
495
496 @Override
497 public boolean setDynamicShortcuts(@NonNull List<ShortcutInfo> shortcutInfoList) {
498 // Note to simulate the binder RPC, we need to clone the incoming arguments.
499 // Otherwise bad things will happen because they're mutable.
500 return super.setDynamicShortcuts(cloneShortcutList(shortcutInfoList));
501 }
502
503 @Override
504 public boolean addDynamicShortcuts(@NonNull List<ShortcutInfo> shortcutInfoList) {
505 // Note to simulate the binder RPC, we need to clone the incoming arguments.
506 return super.addDynamicShortcuts(cloneShortcutList(shortcutInfoList));
507 }
508
509 @Override
510 public boolean updateShortcuts(List<ShortcutInfo> shortcutInfoList) {
511 // Note to simulate the binder RPC, we need to clone the incoming arguments.
512 return super.updateShortcuts(cloneShortcutList(shortcutInfoList));
513 }
514 }
515
516 protected class LauncherAppImplTestable extends LauncherAppsImpl {
517 final ServiceContext mContext;
518
519 public LauncherAppImplTestable(ServiceContext context) {
520 super(context);
521 mContext = context;
522 }
523
524 @Override
525 public void verifyCallingPackage(String callingPackage) {
526 // SKIP
527 }
528
529 @Override
530 void postToPackageMonitorHandler(Runnable r) {
Makoto Onukia2241832016-07-06 13:28:37 -0700531 runOnHandler(r);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700532 }
533
534 @Override
535 int injectBinderCallingUid() {
536 return mInjectedCallingUid;
537 }
538
539 @Override
Makoto Onuki7d0fa812018-02-21 11:24:43 -0800540 int injectBinderCallingPid() {
541 // Note it's not used in tests, so just return a "random" value.
542 return mInjectedCallingUid * 123;
543 }
544
545 @Override
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700546 long injectClearCallingIdentity() {
547 final int prevCallingUid = mInjectedCallingUid;
548 mInjectedCallingUid = Process.SYSTEM_UID;
549 return prevCallingUid;
550 }
551
552 @Override
553 void injectRestoreCallingIdentity(long token) {
554 mInjectedCallingUid = (int) token;
555 }
556 }
557
558 protected class LauncherAppsTestable extends LauncherApps {
559 public LauncherAppsTestable(Context context, ILauncherApps service) {
560 super(context, service);
561 }
562 }
563
564 public static class ShortcutActivity extends Activity {
565 }
566
567 public static class ShortcutActivity2 extends Activity {
568 }
569
570 public static class ShortcutActivity3 extends Activity {
571 }
572
Makoto Onukia2241832016-07-06 13:28:37 -0700573 protected Looper mLooper;
574 protected Handler mHandler;
575
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700576 protected ServiceContext mServiceContext;
577 protected ClientContext mClientContext;
578
579 protected ShortcutServiceTestable mService;
580 protected ShortcutManagerTestable mManager;
581 protected ShortcutServiceInternal mInternal;
582
583 protected LauncherAppImplTestable mLauncherAppImpl;
584
585 // LauncherApps has per-instace state, so we need a differnt instance for each launcher.
586 protected final Map<Pair<Integer, String>, LauncherAppsTestable>
587 mLauncherAppsMap = new HashMap<>();
588 protected LauncherAppsTestable mLauncherApps; // Current one
589
590 protected File mInjectedFilePathRoot;
591
Makoto Onukib08790c2016-06-23 14:05:46 -0700592 protected boolean mSafeMode;
593
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700594 protected long mInjectedCurrentTimeMillis;
Makoto Onuki475c3652017-05-08 14:29:03 -0700595 protected long mDeepSleepTime; // Used to calculate "uptimeMillis".
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700596
597 protected boolean mInjectedIsLowRamDevice;
598
Makoto Onuki157b1622016-06-02 16:13:10 -0700599 protected Locale mInjectedLocale = Locale.ENGLISH;
600
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700601 protected int mInjectedCallingUid;
602 protected String mInjectedClientPackage;
603
604 protected Map<String, PackageInfo> mInjectedPackages;
605
606 protected Set<PackageWithUser> mUninstalledPackages;
Makoto Onuki82fb2eb2017-03-31 16:58:26 -0700607 protected Set<PackageWithUser> mDisabledPackages;
Makoto Onuki66e4a2b2017-01-23 11:37:45 -0800608 protected Set<PackageWithUser> mEphemeralPackages;
Makoto Onuki33663282016-08-22 16:19:04 -0700609 protected Set<String> mSystemPackages;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700610
611 protected PackageManager mMockPackageManager;
612 protected PackageManagerInternal mMockPackageManagerInternal;
613 protected UserManager mMockUserManager;
Sunny Goyal145c8f82018-02-15 14:27:09 -0800614 protected UserManagerInternal mMockUserManagerInternal;
Makoto Onukiac042502016-05-20 16:39:42 -0700615 protected UsageStatsManagerInternal mMockUsageStatsManagerInternal;
Makoto Onukiea11db12016-06-24 15:17:44 -0700616 protected ActivityManagerInternal mMockActivityManagerInternal;
Wale Ogunwale6767eae2018-05-03 15:52:51 -0700617 protected ActivityTaskManagerInternal mMockActivityTaskManagerInternal;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700618
619 protected static final String CALLING_PACKAGE_1 = "com.android.test.1";
620 protected static final int CALLING_UID_1 = 10001;
621
622 protected static final String CALLING_PACKAGE_2 = "com.android.test.2";
623 protected static final int CALLING_UID_2 = 10002;
624
625 protected static final String CALLING_PACKAGE_3 = "com.android.test.3";
626 protected static final int CALLING_UID_3 = 10003;
627
628 protected static final String CALLING_PACKAGE_4 = "com.android.test.4";
629 protected static final int CALLING_UID_4 = 10004;
630
631 protected static final String LAUNCHER_1 = "com.android.launcher.1";
632 protected static final int LAUNCHER_UID_1 = 10011;
633
634 protected static final String LAUNCHER_2 = "com.android.launcher.2";
635 protected static final int LAUNCHER_UID_2 = 10012;
636
637 protected static final String LAUNCHER_3 = "com.android.launcher.3";
638 protected static final int LAUNCHER_UID_3 = 10013;
639
640 protected static final String LAUNCHER_4 = "com.android.launcher.4";
641 protected static final int LAUNCHER_UID_4 = 10014;
642
643 protected static final int USER_0 = UserHandle.USER_SYSTEM;
644 protected static final int USER_10 = 10;
645 protected static final int USER_11 = 11;
Makoto Onukide3c16c2017-01-26 11:39:31 -0800646 protected static final int USER_P0 = 20; // profile of user 0 (MANAGED_PROFILE *not* set)
647 protected static final int USER_P1 = 21; // another profile of user 0 (MANAGED_PROFILE set)
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700648
649 protected static final UserHandle HANDLE_USER_0 = UserHandle.of(USER_0);
650 protected static final UserHandle HANDLE_USER_10 = UserHandle.of(USER_10);
651 protected static final UserHandle HANDLE_USER_11 = UserHandle.of(USER_11);
652 protected static final UserHandle HANDLE_USER_P0 = UserHandle.of(USER_P0);
Makoto Onukide3c16c2017-01-26 11:39:31 -0800653 protected static final UserHandle HANDLE_USER_P1 = UserHandle.of(USER_P1);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700654
655 protected static final UserInfo USER_INFO_0 = withProfileGroupId(
656 new UserInfo(USER_0, "user0",
Makoto Onuki2d895c32016-12-02 15:48:40 -0800657 UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY | UserInfo.FLAG_INITIALIZED), 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700658
659 protected static final UserInfo USER_INFO_10 =
660 new UserInfo(USER_10, "user10", UserInfo.FLAG_INITIALIZED);
661
662 protected static final UserInfo USER_INFO_11 =
663 new UserInfo(USER_11, "user11", UserInfo.FLAG_INITIALIZED);
664
Makoto Onukiaecbd032017-01-19 12:11:11 -0800665 /*
666 * Cheat: USER_P0 is a sub profile of USER_0, but it doesn't have the MANAGED_PROFILE flag set.
667 * Due to a change made to LauncherApps (b/34340531), work profile apps a no longer able
668 * to see the main profile, which would break tons of unit tests. We avoid it by not setting
669 * MANAGED_PROFILE for P0.
670 * We cover this negative case in CTS. (i.e. CTS has tests to make sure maanged profile
671 * can't access main profile's shortcuts.)
672 */
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700673 protected static final UserInfo USER_INFO_P0 = withProfileGroupId(
Makoto Onukiaecbd032017-01-19 12:11:11 -0800674 new UserInfo(USER_P0, "userP0", UserInfo.FLAG_INITIALIZED), 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700675
Makoto Onukide3c16c2017-01-26 11:39:31 -0800676 protected static final UserInfo USER_INFO_P1 = withProfileGroupId(
677 new UserInfo(USER_P1, "userP1",
678 UserInfo.FLAG_INITIALIZED | UserInfo.FLAG_MANAGED_PROFILE), 0);
679
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700680 protected BiPredicate<String, Integer> mDefaultLauncherChecker =
681 (callingPackage, userId) ->
682 LAUNCHER_1.equals(callingPackage) || LAUNCHER_2.equals(callingPackage)
683 || LAUNCHER_3.equals(callingPackage) || LAUNCHER_4.equals(callingPackage);
684
Makoto Onuki2d895c32016-12-02 15:48:40 -0800685 private final Map<Integer, ComponentName> mDefaultLauncher = new ArrayMap<>();
686
Makoto Onukib08790c2016-06-23 14:05:46 -0700687 protected BiPredicate<ComponentName, Integer> mMainActivityChecker =
688 (activity, userId) -> true;
689
690 protected BiFunction<String, Integer, ComponentName> mMainActivityFetcher =
691 (packageName, userId) -> new ComponentName(packageName, MAIN_ACTIVITY_CLASS);
692
Makoto Onuki2d895c32016-12-02 15:48:40 -0800693 protected BiFunction<String, Integer, ComponentName> mPinConfirmActivityFetcher =
694 (packageName, userId) -> new ComponentName(packageName, PIN_CONFIRM_ACTIVITY_CLASS);
695
Makoto Onukib08790c2016-06-23 14:05:46 -0700696 protected BiPredicate<ComponentName, Integer> mEnabledActivityChecker
697 = (activity, userId) -> true; // all activities are enabled.
698
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700699 protected static final long START_TIME = 1440000000101L;
700
701 protected static final long INTERVAL = 10000;
702
703 protected static final int MAX_SHORTCUTS = 10;
704
705 protected static final int MAX_UPDATES_PER_INTERVAL = 3;
706
707 protected static final int MAX_ICON_DIMENSION = 128;
708
709 protected static final int MAX_ICON_DIMENSION_LOWRAM = 32;
710
711 protected static final ShortcutQuery QUERY_ALL = new ShortcutQuery();
712
713 protected final ArrayList<String> mCallerPermissions = new ArrayList<>();
714
715 protected final HashMap<String, LinkedHashMap<ComponentName, Integer>> mActivityMetadataResId
716 = new HashMap<>();
717
Makoto Onukia2241832016-07-06 13:28:37 -0700718 protected final Map<Integer, UserInfo> mUserInfos = new HashMap<>();
719 protected final Map<Integer, Boolean> mRunningUsers = new HashMap<>();
720 protected final Map<Integer, Boolean> mUnlockedUsers = new HashMap<>();
721
Makoto Onuki0b9d1db2016-07-18 14:16:41 -0700722 protected static final String PACKAGE_SYSTEM_LAUNCHER = "com.android.systemlauncher";
723 protected static final String PACKAGE_SYSTEM_LAUNCHER_NAME = "systemlauncher_name";
724 protected static final int PACKAGE_SYSTEM_LAUNCHER_PRIORITY = 0;
725
726 protected static final String PACKAGE_FALLBACK_LAUNCHER = "com.android.settings";
727 protected static final String PACKAGE_FALLBACK_LAUNCHER_NAME = "fallback";
728 protected static final int PACKAGE_FALLBACK_LAUNCHER_PRIORITY = -999;
729
Makoto Onuki33663282016-08-22 16:19:04 -0700730 protected String mInjectedBuildFingerprint = "build1";
731
Makoto Onuki634cecb2017-10-13 17:10:48 -0700732 protected boolean mInjectCheckAccessShortcutsPermission = false;
733
Makoto Onuki7d0fa812018-02-21 11:24:43 -0800734 protected boolean mInjectHasUnlimitedShortcutsApiCallsPermission = false;
735
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700736 static {
737 QUERY_ALL.setQueryFlags(
Makoto Onuki4d6b87f2016-06-17 13:47:40 -0700738 ShortcutQuery.FLAG_GET_ALL_KINDS);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700739 }
740
741 @Override
742 protected void setUp() throws Exception {
743 super.setUp();
744
Makoto Onukia2241832016-07-06 13:28:37 -0700745 mLooper = Looper.getMainLooper();
746 mHandler = new Handler(mLooper);
747
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700748 mServiceContext = spy(new ServiceContext());
749 mClientContext = new ClientContext();
750
751 mMockPackageManager = mock(PackageManager.class);
752 mMockPackageManagerInternal = mock(PackageManagerInternal.class);
753 mMockUserManager = mock(UserManager.class);
Sunny Goyal145c8f82018-02-15 14:27:09 -0800754 mMockUserManagerInternal = mock(UserManagerInternal.class);
Makoto Onukiac042502016-05-20 16:39:42 -0700755 mMockUsageStatsManagerInternal = mock(UsageStatsManagerInternal.class);
Makoto Onukiea11db12016-06-24 15:17:44 -0700756 mMockActivityManagerInternal = mock(ActivityManagerInternal.class);
Wale Ogunwale6767eae2018-05-03 15:52:51 -0700757 mMockActivityTaskManagerInternal = mock(ActivityTaskManagerInternal.class);
Makoto Onukiac042502016-05-20 16:39:42 -0700758
759 LocalServices.removeServiceForTest(PackageManagerInternal.class);
760 LocalServices.addService(PackageManagerInternal.class, mMockPackageManagerInternal);
761 LocalServices.removeServiceForTest(UsageStatsManagerInternal.class);
762 LocalServices.addService(UsageStatsManagerInternal.class, mMockUsageStatsManagerInternal);
Makoto Onukiea11db12016-06-24 15:17:44 -0700763 LocalServices.removeServiceForTest(ActivityManagerInternal.class);
764 LocalServices.addService(ActivityManagerInternal.class, mMockActivityManagerInternal);
Wale Ogunwale6767eae2018-05-03 15:52:51 -0700765 LocalServices.addService(ActivityTaskManagerInternal.class, mMockActivityTaskManagerInternal);
Sunny Goyal145c8f82018-02-15 14:27:09 -0800766 LocalServices.removeServiceForTest(UserManagerInternal.class);
767 LocalServices.addService(UserManagerInternal.class, mMockUserManagerInternal);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700768
769 // Prepare injection values.
770
771 mInjectedCurrentTimeMillis = START_TIME;
772
773 mInjectedPackages = new HashMap<>();
774 addPackage(CALLING_PACKAGE_1, CALLING_UID_1, 1);
775 addPackage(CALLING_PACKAGE_2, CALLING_UID_2, 2);
776 addPackage(CALLING_PACKAGE_3, CALLING_UID_3, 3);
777 addPackage(CALLING_PACKAGE_4, CALLING_UID_4, 10);
778 addPackage(LAUNCHER_1, LAUNCHER_UID_1, 4);
779 addPackage(LAUNCHER_2, LAUNCHER_UID_2, 5);
780 addPackage(LAUNCHER_3, LAUNCHER_UID_3, 6);
781 addPackage(LAUNCHER_4, LAUNCHER_UID_4, 10);
782
783 // CALLING_PACKAGE_3 / LAUNCHER_3 are not backup target.
784 updatePackageInfo(CALLING_PACKAGE_3,
785 pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP);
786 updatePackageInfo(LAUNCHER_3,
787 pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP);
788
789 mUninstalledPackages = new HashSet<>();
Makoto Onuki82fb2eb2017-03-31 16:58:26 -0700790 mDisabledPackages = new HashSet<>();
Makoto Onuki33663282016-08-22 16:19:04 -0700791 mSystemPackages = new HashSet<>();
Makoto Onuki66e4a2b2017-01-23 11:37:45 -0800792 mEphemeralPackages = new HashSet<>();
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700793
794 mInjectedFilePathRoot = new File(getTestContext().getCacheDir(), "test-files");
795
796 deleteAllSavedFiles();
797
798 // Set up users.
Makoto Onukia2241832016-07-06 13:28:37 -0700799 mUserInfos.put(USER_0, USER_INFO_0);
800 mUserInfos.put(USER_10, USER_INFO_10);
801 mUserInfos.put(USER_11, USER_INFO_11);
802 mUserInfos.put(USER_P0, USER_INFO_P0);
Makoto Onukide3c16c2017-01-26 11:39:31 -0800803 mUserInfos.put(USER_P1, USER_INFO_P1);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700804
Sunny Goyal145c8f82018-02-15 14:27:09 -0800805 when(mMockUserManagerInternal.isUserUnlockingOrUnlocked(anyInt()))
806 .thenAnswer(inv -> {
Makoto Onuki9c850012016-07-26 15:50:50 -0700807 final int userId = (Integer) inv.getArguments()[0];
808 return b(mRunningUsers.get(userId)) && b(mUnlockedUsers.get(userId));
Sunny Goyal145c8f82018-02-15 14:27:09 -0800809 });
810 when(mMockUserManagerInternal.getProfileParentId(anyInt()))
811 .thenAnswer(inv -> {
Makoto Onuki2d895c32016-12-02 15:48:40 -0800812 final int userId = (Integer) inv.getArguments()[0];
813 final UserInfo ui = mUserInfos.get(userId);
814 assertNotNull(ui);
815 if (ui.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID) {
Sunny Goyal145c8f82018-02-15 14:27:09 -0800816 return userId;
Makoto Onuki2d895c32016-12-02 15:48:40 -0800817 }
818 final UserInfo parent = mUserInfos.get(ui.profileGroupId);
819 assertNotNull(parent);
Sunny Goyal145c8f82018-02-15 14:27:09 -0800820 return parent.id;
821 });
822
823 when(mMockUserManagerInternal.isProfileAccessible(anyInt(), anyInt(), anyString(),
824 anyBoolean())).thenAnswer(inv -> {
825 final int callingUserId = (Integer) inv.getArguments()[0];
826 final int targetUserId = (Integer) inv.getArguments()[1];
827 if (targetUserId == callingUserId) {
828 return true;
829 }
830 final UserInfo callingUserInfo = mUserInfos.get(callingUserId);
831 final UserInfo targetUserInfo = mUserInfos.get(targetUserId);
832 if (callingUserInfo == null || callingUserInfo.isManagedProfile()
833 || targetUserInfo == null || !targetUserInfo.isEnabled()) {
834 return false;
835 }
836 if (targetUserInfo.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID
837 && targetUserInfo.profileGroupId == callingUserInfo.profileGroupId) {
838 return true;
839 }
840 final boolean isExternal = (Boolean) inv.getArguments()[3];
841 if (!isExternal) {
842 return false;
843 }
844 throw new SecurityException(inv.getArguments()[2] + " for unrelated profile "
845 + targetUserId);
846 });
Makoto Onuki2d895c32016-12-02 15:48:40 -0800847
Makoto Onukie70b29e2018-04-03 16:44:39 -0700848 when(mMockUserManager.getUserInfo(anyInt())).thenAnswer(new AnswerWithSystemCheck<>(
849 inv -> mUserInfos.get((Integer) inv.getArguments()[0])));
Makoto Onuki33525d22016-08-03 15:45:24 -0700850 when(mMockActivityManagerInternal.getUidProcessState(anyInt())).thenReturn(
851 ActivityManager.PROCESS_STATE_CACHED_EMPTY);
852
Makoto Onuki9c850012016-07-26 15:50:50 -0700853 // User 0 and P0 are always running
Makoto Onukia2241832016-07-06 13:28:37 -0700854 mRunningUsers.put(USER_0, true);
855 mRunningUsers.put(USER_10, false);
856 mRunningUsers.put(USER_11, false);
Makoto Onuki9c850012016-07-26 15:50:50 -0700857 mRunningUsers.put(USER_P0, true);
Makoto Onukide3c16c2017-01-26 11:39:31 -0800858 mRunningUsers.put(USER_P1, true);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700859
Makoto Onukia2241832016-07-06 13:28:37 -0700860 // Unlock all users by default.
861 mUnlockedUsers.put(USER_0, true);
862 mUnlockedUsers.put(USER_10, true);
863 mUnlockedUsers.put(USER_11, true);
864 mUnlockedUsers.put(USER_P0, true);
Makoto Onukide3c16c2017-01-26 11:39:31 -0800865 mUnlockedUsers.put(USER_P1, true);
Makoto Onukia2241832016-07-06 13:28:37 -0700866
867 // Set up resources
Makoto Onuki157b1622016-06-02 16:13:10 -0700868 setUpAppResources();
869
870 // Start the service.
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700871 initService();
872 setCaller(CALLING_PACKAGE_1);
Makoto Onuki248a0ef2016-11-03 15:59:01 -0700873
874 if (ENABLE_DUMP) {
875 Log.d(TAG, "setUp done");
876 }
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700877 }
878
Makoto Onukie70b29e2018-04-03 16:44:39 -0700879 /**
880 * Returns a boolean but also checks if the current UID is SYSTEM_UID.
881 */
882 protected class AnswerWithSystemCheck<T> implements Answer<T> {
883 private final Function<InvocationOnMock, T> mChecker;
884
885 public AnswerWithSystemCheck(Function<InvocationOnMock, T> checker) {
886 mChecker = checker;
887 }
888
889 @Override
890 public T answer(InvocationOnMock invocation) throws Throwable {
891 assertEquals("Must be called on SYSTEM UID.",
892 Process.SYSTEM_UID, mInjectedCallingUid);
893 return mChecker.apply(invocation);
894 }
895 }
896
Makoto Onuki9c850012016-07-26 15:50:50 -0700897 private static boolean b(Boolean value) {
898 return (value != null && value);
899 }
900
Makoto Onuki157b1622016-06-02 16:13:10 -0700901 protected void setUpAppResources() throws Exception {
902 setUpAppResources(/* offset = */ 0);
903 }
904
905 protected void setUpAppResources(int ressIdOffset) throws Exception {
906 // ressIdOffset is used to adjust resource IDs to emulate the case where an updated app
907 // has resource IDs changed.
908
909 doAnswer(pmInvocation -> {
910 assertEquals(Process.SYSTEM_UID, mInjectedCallingUid);
911
912 final String packageName = (String) pmInvocation.getArguments()[0];
913 final int userId = (Integer) pmInvocation.getArguments()[1];
914
915 final Resources res = mock(Resources.class);
916
917 doAnswer(resInvocation -> {
918 final int argResId = (Integer) resInvocation.getArguments()[0];
919
920 return "string-" + packageName + "-user:" + userId + "-res:" + argResId
921 + "/" + mInjectedLocale;
922 }).when(res).getString(anyInt());
923
924 doAnswer(resInvocation -> {
925 final int resId = (Integer) resInvocation.getArguments()[0];
926
927 // Always use the "string" resource type. The type doesn't matter during the test.
928 return packageName + ":string/r" + resId;
929 }).when(res).getResourceName(anyInt());
930
931 doAnswer(resInvocation -> {
932 final String argResName = (String) resInvocation.getArguments()[0];
933 final String argType = (String) resInvocation.getArguments()[1];
934 final String argPackageName = (String) resInvocation.getArguments()[2];
935
936 // See the above code. getResourceName() will just use "r" + res ID as the entry
937 // name.
938 String entryName = argResName;
939 if (entryName.contains("/")) {
940 entryName = ShortcutInfo.getResourceEntryName(entryName);
941 }
942 return Integer.parseInt(entryName.substring(1)) + ressIdOffset;
Makoto Onukid0010c52017-03-30 14:17:35 -0700943 }).when(res).getIdentifier(anyStringOrNull(), anyStringOrNull(), anyStringOrNull());
Makoto Onuki157b1622016-06-02 16:13:10 -0700944 return res;
945 }).when(mMockPackageManager).getResourcesForApplicationAsUser(anyString(), anyInt());
946 }
947
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700948 protected static UserInfo withProfileGroupId(UserInfo in, int groupId) {
949 in.profileGroupId = groupId;
950 return in;
951 }
952
953 @Override
954 protected void tearDown() throws Exception {
955 if (DUMP_IN_TEARDOWN) dumpsysOnLogcat("Teardown");
956
957 shutdownServices();
958
959 super.tearDown();
960 }
961
962 protected Context getTestContext() {
963 return getInstrumentation().getContext();
964 }
965
Makoto Onukia1d38b32016-06-10 15:32:26 -0700966 protected ShortcutManager getManager() {
967 return mManager;
968 }
969
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700970 protected void deleteAllSavedFiles() {
971 // Empty the data directory.
972 if (mInjectedFilePathRoot.exists()) {
973 Assert.assertTrue("failed to delete dir",
974 FileUtils.deleteContents(mInjectedFilePathRoot));
975 }
976 mInjectedFilePathRoot.mkdirs();
977 }
978
979 /** (Re-) init the manager and the service. */
980 protected void initService() {
981 shutdownServices();
982
983 LocalServices.removeServiceForTest(ShortcutServiceInternal.class);
984
985 // Instantiate targets.
Makoto Onukia2241832016-07-06 13:28:37 -0700986 mService = new ShortcutServiceTestable(mServiceContext, mLooper);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700987 mManager = new ShortcutManagerTestable(mClientContext, mService);
988
989 mInternal = LocalServices.getService(ShortcutServiceInternal.class);
990
991 mLauncherAppImpl = new LauncherAppImplTestable(mServiceContext);
992 mLauncherApps = null;
993 mLauncherAppsMap.clear();
994
Makoto Onuki157b1622016-06-02 16:13:10 -0700995 // Send boot sequence events.
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700996 mService.onBootPhase(SystemService.PHASE_LOCK_SETTINGS_READY);
Makoto Onuki157b1622016-06-02 16:13:10 -0700997
Makoto Onuki157b1622016-06-02 16:13:10 -0700998 mService.onBootPhase(SystemService.PHASE_BOOT_COMPLETED);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700999 }
1000
1001 protected void shutdownServices() {
1002 if (mService != null) {
1003 // Flush all the unsaved data from the previous instance.
1004 mService.saveDirtyInfo();
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001005
1006 // Make sure everything is consistent.
1007 mService.verifyStates();
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001008 }
1009 LocalServices.removeServiceForTest(ShortcutServiceInternal.class);
1010
1011 mService = null;
1012 mManager = null;
1013 mInternal = null;
1014 mLauncherAppImpl = null;
1015 mLauncherApps = null;
1016 mLauncherAppsMap.clear();
1017 }
1018
Makoto Onukia2241832016-07-06 13:28:37 -07001019 protected void runOnHandler(Runnable r) {
1020 final long token = mServiceContext.injectClearCallingIdentity();
1021 try {
1022 r.run();
1023 } finally {
1024 mServiceContext.injectRestoreCallingIdentity(token);
1025 }
1026 }
1027
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001028 protected void addPackage(String packageName, int uid, int version) {
1029 addPackage(packageName, uid, version, packageName);
1030 }
1031
1032 protected Signature[] genSignatures(String... signatures) {
1033 final Signature[] sigs = new Signature[signatures.length];
1034 for (int i = 0; i < signatures.length; i++){
1035 sigs[i] = new Signature(signatures[i].getBytes());
1036 }
1037 return sigs;
1038 }
1039
1040 protected PackageInfo genPackage(String packageName, int uid, int version, String... signatures) {
1041 final PackageInfo pi = new PackageInfo();
1042 pi.packageName = packageName;
1043 pi.applicationInfo = new ApplicationInfo();
1044 pi.applicationInfo.uid = uid;
1045 pi.applicationInfo.flags = ApplicationInfo.FLAG_INSTALLED
1046 | ApplicationInfo.FLAG_ALLOW_BACKUP;
1047 pi.versionCode = version;
Patrick Baumannc2def582018-04-04 12:14:15 -07001048 pi.applicationInfo.setVersionCode(version);
Michal Karpinski528c3e52018-02-07 17:47:10 +00001049 pi.signatures = null;
Dan Cashman5c9f527e2018-04-03 16:42:23 -07001050 pi.signingInfo = new SigningInfo(
1051 new PackageParser.SigningDetails(
1052 genSignatures(signatures),
1053 PackageParser.SigningDetails.SignatureSchemeVersion.SIGNING_BLOCK_V3,
1054 null,
1055 null,
1056 null));
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001057 return pi;
1058 }
1059
1060 protected void addPackage(String packageName, int uid, int version, String... signatures) {
1061 mInjectedPackages.put(packageName, genPackage(packageName, uid, version, signatures));
1062 }
1063
1064 protected void updatePackageInfo(String packageName, Consumer<PackageInfo> c) {
1065 c.accept(mInjectedPackages.get(packageName));
1066 }
1067
1068 protected void updatePackageVersion(String packageName, int increment) {
1069 updatePackageInfo(packageName, pi -> {
1070 pi.versionCode += increment;
Patrick Baumannc2def582018-04-04 12:14:15 -07001071 pi.applicationInfo.setVersionCode(pi.applicationInfo.longVersionCode + increment);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001072 });
1073 }
1074
1075 protected void updatePackageLastUpdateTime(String packageName, long increment) {
1076 updatePackageInfo(packageName, pi -> {
1077 pi.lastUpdateTime += increment;
1078 });
1079 }
1080
Makoto Onuki085a05c2016-08-19 11:39:29 -07001081 protected void setPackageLastUpdateTime(String packageName, long value) {
1082 updatePackageInfo(packageName, pi -> {
1083 pi.lastUpdateTime = value;
1084 });
1085 }
1086
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001087 protected void uninstallPackage(int userId, String packageName) {
1088 if (ENABLE_DUMP) {
Makoto Onuki82fb2eb2017-03-31 16:58:26 -07001089 Log.v(TAG, "Uninstall package " + packageName + " / " + userId);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001090 }
1091 mUninstalledPackages.add(PackageWithUser.of(userId, packageName));
1092 }
1093
1094 protected void installPackage(int userId, String packageName) {
1095 if (ENABLE_DUMP) {
1096 Log.v(TAG, "Install package " + packageName + " / " + userId);
1097 }
1098 mUninstalledPackages.remove(PackageWithUser.of(userId, packageName));
1099 }
1100
Makoto Onuki82fb2eb2017-03-31 16:58:26 -07001101 protected void disablePackage(int userId, String packageName) {
1102 if (ENABLE_DUMP) {
1103 Log.v(TAG, "Disable package " + packageName + " / " + userId);
1104 }
1105 mDisabledPackages.add(PackageWithUser.of(userId, packageName));
1106 }
1107
1108 protected void enablePackage(int userId, String packageName) {
1109 if (ENABLE_DUMP) {
1110 Log.v(TAG, "Enable package " + packageName + " / " + userId);
1111 }
1112 mDisabledPackages.remove(PackageWithUser.of(userId, packageName));
1113 }
1114
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001115 PackageInfo getInjectedPackageInfo(String packageName, @UserIdInt int userId,
1116 boolean getSignatures) {
1117 final PackageInfo pi = mInjectedPackages.get(packageName);
1118 if (pi == null) return null;
1119
1120 final PackageInfo ret = new PackageInfo();
1121 ret.packageName = pi.packageName;
1122 ret.versionCode = pi.versionCode;
Dianne Hackborn3accca02013-09-20 09:32:11 -07001123 ret.versionCodeMajor = pi.versionCodeMajor;
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001124 ret.lastUpdateTime = pi.lastUpdateTime;
1125
1126 ret.applicationInfo = new ApplicationInfo(pi.applicationInfo);
1127 ret.applicationInfo.uid = UserHandle.getUid(userId, pi.applicationInfo.uid);
1128 ret.applicationInfo.packageName = pi.packageName;
1129
1130 if (mUninstalledPackages.contains(PackageWithUser.of(userId, packageName))) {
1131 ret.applicationInfo.flags &= ~ApplicationInfo.FLAG_INSTALLED;
1132 }
Makoto Onuki66e4a2b2017-01-23 11:37:45 -08001133 if (mEphemeralPackages.contains(PackageWithUser.of(userId, packageName))) {
Todd Kennedybe0b8892017-02-15 14:13:52 -08001134 ret.applicationInfo.privateFlags |= ApplicationInfo.PRIVATE_FLAG_INSTANT;
Makoto Onuki66e4a2b2017-01-23 11:37:45 -08001135 }
Makoto Onuki33663282016-08-22 16:19:04 -07001136 if (mSystemPackages.contains(packageName)) {
1137 ret.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
1138 }
Makoto Onuki82fb2eb2017-03-31 16:58:26 -07001139 ret.applicationInfo.enabled =
1140 !mDisabledPackages.contains(PackageWithUser.of(userId, packageName));
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001141
1142 if (getSignatures) {
Michal Karpinski528c3e52018-02-07 17:47:10 +00001143 ret.signatures = null;
Dan Cashman5c9f527e2018-04-03 16:42:23 -07001144 ret.signingInfo = pi.signingInfo;
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001145 }
1146
1147 return ret;
1148 }
1149
1150 protected void addApplicationInfo(PackageInfo pi, List<ApplicationInfo> list) {
1151 if (pi != null && pi.applicationInfo != null) {
1152 list.add(pi.applicationInfo);
1153 }
1154 }
1155
1156 protected List<ApplicationInfo> getInstalledApplications(int userId) {
1157 final ArrayList<ApplicationInfo> ret = new ArrayList<>();
1158
1159 addApplicationInfo(getInjectedPackageInfo(CALLING_PACKAGE_1, userId, false), ret);
1160 addApplicationInfo(getInjectedPackageInfo(CALLING_PACKAGE_2, userId, false), ret);
1161 addApplicationInfo(getInjectedPackageInfo(CALLING_PACKAGE_3, userId, false), ret);
1162 addApplicationInfo(getInjectedPackageInfo(CALLING_PACKAGE_4, userId, false), ret);
1163 addApplicationInfo(getInjectedPackageInfo(LAUNCHER_1, userId, false), ret);
1164 addApplicationInfo(getInjectedPackageInfo(LAUNCHER_2, userId, false), ret);
1165 addApplicationInfo(getInjectedPackageInfo(LAUNCHER_3, userId, false), ret);
1166 addApplicationInfo(getInjectedPackageInfo(LAUNCHER_4, userId, false), ret);
1167
1168 return ret;
1169 }
1170
Makoto Onuki6dd9fb72016-06-01 13:55:54 -07001171 private void addPackageInfo(PackageInfo pi, List<PackageInfo> list) {
1172 if (pi != null) {
1173 list.add(pi);
1174 }
1175 }
1176
Makoto Onukiee6b6e42016-06-29 17:34:02 -07001177 private List<PackageInfo> getInstalledPackagesWithUninstalled(int userId) {
Makoto Onuki6dd9fb72016-06-01 13:55:54 -07001178 final ArrayList<PackageInfo> ret = new ArrayList<>();
1179
1180 addPackageInfo(getInjectedPackageInfo(CALLING_PACKAGE_1, userId, false), ret);
1181 addPackageInfo(getInjectedPackageInfo(CALLING_PACKAGE_2, userId, false), ret);
1182 addPackageInfo(getInjectedPackageInfo(CALLING_PACKAGE_3, userId, false), ret);
1183 addPackageInfo(getInjectedPackageInfo(CALLING_PACKAGE_4, userId, false), ret);
1184 addPackageInfo(getInjectedPackageInfo(LAUNCHER_1, userId, false), ret);
1185 addPackageInfo(getInjectedPackageInfo(LAUNCHER_2, userId, false), ret);
1186 addPackageInfo(getInjectedPackageInfo(LAUNCHER_3, userId, false), ret);
1187 addPackageInfo(getInjectedPackageInfo(LAUNCHER_4, userId, false), ret);
1188
1189 return ret;
1190 }
1191
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001192 protected void addManifestShortcutResource(ComponentName activity, int resId) {
1193 final String packageName = activity.getPackageName();
1194 LinkedHashMap<ComponentName, Integer> map = mActivityMetadataResId.get(packageName);
1195 if (map == null) {
1196 map = new LinkedHashMap<>();
1197 mActivityMetadataResId.put(packageName, map);
1198 }
1199 map.put(activity, resId);
1200 }
1201
1202 protected PackageInfo injectGetActivitiesWithMetadata(String packageName, @UserIdInt int userId) {
1203 final PackageInfo ret = getInjectedPackageInfo(packageName, userId,
1204 /* getSignatures=*/ false);
1205
1206 final HashMap<ComponentName, Integer> activities = mActivityMetadataResId.get(packageName);
1207 if (activities != null) {
1208 final ArrayList<ActivityInfo> list = new ArrayList<>();
1209
1210 for (ComponentName cn : activities.keySet()) {
1211 ActivityInfo ai = new ActivityInfo();
1212 ai.packageName = cn.getPackageName();
1213 ai.name = cn.getClassName();
1214 ai.metaData = new Bundle();
1215 ai.metaData.putInt(ShortcutParser.METADATA_KEY, activities.get(cn));
Makoto Onukiee6b6e42016-06-29 17:34:02 -07001216 ai.applicationInfo = ret.applicationInfo;
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001217 list.add(ai);
1218 }
1219 ret.activities = list.toArray(new ActivityInfo[list.size()]);
1220 }
1221 return ret;
1222 }
1223
1224 protected XmlResourceParser injectXmlMetaData(ActivityInfo activityInfo, String key) {
1225 if (!ShortcutParser.METADATA_KEY.equals(key) || activityInfo.metaData == null) {
1226 return null;
1227 }
1228 final int resId = activityInfo.metaData.getInt(key);
1229 return getTestContext().getResources().getXml(resId);
1230 }
1231
1232 /** Replace the current calling package */
1233 protected void setCaller(String packageName, int userId) {
1234 mInjectedClientPackage = packageName;
1235 mInjectedCallingUid =
1236 Preconditions.checkNotNull(getInjectedPackageInfo(packageName, userId, false),
1237 "Unknown package").applicationInfo.uid;
1238
1239 // Set up LauncherApps for this caller.
1240 final Pair<Integer, String> key = Pair.create(userId, packageName);
1241 if (!mLauncherAppsMap.containsKey(key)) {
1242 mLauncherAppsMap.put(key, new LauncherAppsTestable(mClientContext, mLauncherAppImpl));
1243 }
1244 mLauncherApps = mLauncherAppsMap.get(key);
1245 }
1246
1247 protected void setCaller(String packageName) {
1248 setCaller(packageName, UserHandle.USER_SYSTEM);
1249 }
1250
1251 protected String getCallingPackage() {
1252 return mInjectedClientPackage;
1253 }
1254
Makoto Onuki2d895c32016-12-02 15:48:40 -08001255 /**
Makoto Onuki7d0fa812018-02-21 11:24:43 -08001256 * This controls {@link ShortcutService#hasShortcutHostPermission}, but
Makoto Onuki2d895c32016-12-02 15:48:40 -08001257 * not {@link ShortcutService#getDefaultLauncher(int)}. To control the later, use
1258 * {@link #setDefaultLauncher(int, ComponentName)}.
1259 */
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001260 protected void setDefaultLauncherChecker(BiPredicate<String, Integer> p) {
1261 mDefaultLauncherChecker = p;
1262 }
1263
Makoto Onuki2d895c32016-12-02 15:48:40 -08001264 /**
1265 * Set the default launcher. This will update {@link #mDefaultLauncherChecker} set by
1266 * {@link #setDefaultLauncherChecker} too.
1267 */
1268 protected void setDefaultLauncher(int userId, ComponentName launcherActivity) {
1269 mDefaultLauncher.put(userId, launcherActivity);
1270
1271 final BiPredicate<String, Integer> oldChecker = mDefaultLauncherChecker;
1272 mDefaultLauncherChecker = (checkPackageName, checkUserId) -> {
1273 if ((checkUserId == userId) && (launcherActivity != null)) {
1274 return launcherActivity.getPackageName().equals(checkPackageName);
1275 }
1276 return oldChecker.test(checkPackageName, checkUserId);
1277 };
1278 }
1279
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001280 protected void runWithCaller(String packageName, int userId, Runnable r) {
1281 final String previousPackage = mInjectedClientPackage;
1282 final int previousUserId = UserHandle.getUserId(mInjectedCallingUid);
1283
1284 setCaller(packageName, userId);
1285
1286 r.run();
1287
1288 setCaller(previousPackage, previousUserId);
1289 }
1290
Makoto Onuki157b1622016-06-02 16:13:10 -07001291 protected void runWithSystemUid(Runnable r) {
1292 final int origUid = mInjectedCallingUid;
1293 mInjectedCallingUid = Process.SYSTEM_UID;
1294 r.run();
1295 mInjectedCallingUid = origUid;
1296 }
1297
1298 protected void lookupAndFillInResourceNames(ShortcutInfo si) {
1299 runWithSystemUid(() -> si.lookupAndFillInResourceNames(
1300 mService.injectGetResourcesForApplicationAsUser(si.getPackage(), si.getUserId())));
1301 }
1302
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001303 protected int getCallingUserId() {
1304 return UserHandle.getUserId(mInjectedCallingUid);
1305 }
1306
1307 protected UserHandle getCallingUser() {
1308 return UserHandle.of(getCallingUserId());
1309 }
1310
1311 /** For debugging */
1312 protected void dumpsysOnLogcat() {
1313 dumpsysOnLogcat("");
1314 }
1315
1316 protected void dumpsysOnLogcat(String message) {
1317 dumpsysOnLogcat(message, false);
1318 }
1319
1320 protected void dumpsysOnLogcat(String message, boolean force) {
1321 if (force || !ENABLE_DUMP) return;
1322
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001323 Log.v(TAG, "Dumping ShortcutService: " + message);
Makoto Onuki50a320e2017-05-31 14:38:42 -07001324 for (String line : dumpsys("-u").split("\n")) {
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001325 Log.v(TAG, line);
1326 }
1327 }
1328
Makoto Onuki76269922016-07-15 14:58:54 -07001329 protected String dumpCheckin() {
Makoto Onuki50a320e2017-05-31 14:38:42 -07001330 return dumpsys("--checkin");
Makoto Onuki76269922016-07-15 14:58:54 -07001331 }
1332
Makoto Onuki50a320e2017-05-31 14:38:42 -07001333 protected String dumpsys(String... args) {
Makoto Onuki76269922016-07-15 14:58:54 -07001334 final ArrayList<String> origPermissions = new ArrayList<>(mCallerPermissions);
1335 mCallerPermissions.add(android.Manifest.permission.DUMP);
1336 try {
1337 final ByteArrayOutputStream out = new ByteArrayOutputStream();
1338 final PrintWriter pw = new PrintWriter(out);
Makoto Onukic4361e32017-04-03 11:24:25 -07001339 mService.dumpNoCheck(/* fd */ null, pw, args);
Makoto Onuki76269922016-07-15 14:58:54 -07001340 pw.close();
1341
1342 return out.toString();
1343 } finally {
1344 mCallerPermissions.clear();
1345 mCallerPermissions.addAll(origPermissions);
1346 }
1347 }
1348
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001349 /**
1350 * For debugging, dump arbitrary file on logcat.
1351 */
1352 protected void dumpFileOnLogcat(String path) {
1353 dumpFileOnLogcat(path, "");
1354 }
1355
1356 protected void dumpFileOnLogcat(String path, String message) {
1357 if (!ENABLE_DUMP) return;
1358
1359 Log.v(TAG, "Dumping file: " + path + " " + message);
1360 final StringBuilder sb = new StringBuilder();
1361 try (BufferedReader br = new BufferedReader(new FileReader(path))) {
1362 String line;
1363 while ((line = br.readLine()) != null) {
1364 Log.v(TAG, line);
1365 }
1366 } catch (Exception e) {
1367 Log.e(TAG, "Couldn't read file", e);
1368 fail("Exception " + e);
1369 }
1370 }
1371
1372 /**
1373 * For debugging, dump the main state file on logcat.
1374 */
1375 protected void dumpBaseStateFile() {
1376 mService.saveDirtyInfo();
1377 dumpFileOnLogcat(mInjectedFilePathRoot.getAbsolutePath()
1378 + "/system/" + ShortcutService.FILENAME_BASE_STATE);
1379 }
1380
1381 /**
1382 * For debugging, dump per-user state file on logcat.
1383 */
1384 protected void dumpUserFile(int userId) {
1385 dumpUserFile(userId, "");
1386 }
1387
1388 protected void dumpUserFile(int userId, String message) {
1389 mService.saveDirtyInfo();
1390 dumpFileOnLogcat(mInjectedFilePathRoot.getAbsolutePath()
1391 + "/user-" + userId
1392 + "/" + ShortcutService.FILENAME_USER_PACKAGES, message);
1393 }
1394
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001395 /**
Makoto Onukia01f4f02016-12-15 15:58:41 -08001396 * Make a shortcut with an ID only.
1397 */
1398 protected ShortcutInfo makeShortcutIdOnly(String id) {
1399 return new ShortcutInfo.Builder(mClientContext, id).build();
1400 }
1401
1402 /**
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001403 * Make a shortcut with an ID.
1404 */
1405 protected ShortcutInfo makeShortcut(String id) {
1406 return makeShortcut(
1407 id, "Title-" + id, /* activity =*/ null, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001408 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001409 }
1410
Makoto Onukia01f4f02016-12-15 15:58:41 -08001411 @Deprecated // Title was renamed to short label.
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001412 protected ShortcutInfo makeShortcutWithTitle(String id, String title) {
1413 return makeShortcut(
1414 id, title, /* activity =*/ null, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001415 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001416 }
1417
Makoto Onukia01f4f02016-12-15 15:58:41 -08001418 protected ShortcutInfo makeShortcutWithShortLabel(String id, String shortLabel) {
1419 return makeShortcut(
1420 id, shortLabel, /* activity =*/ null, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001421 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onukia01f4f02016-12-15 15:58:41 -08001422 }
1423
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001424 /**
1425 * Make a shortcut with an ID and timestamp.
1426 */
1427 protected ShortcutInfo makeShortcutWithTimestamp(String id, long timestamp) {
1428 final ShortcutInfo s = makeShortcut(
1429 id, "Title-" + id, /* activity =*/ null, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001430 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001431 s.setTimestamp(timestamp);
1432 return s;
1433 }
1434
1435 /**
1436 * Make a shortcut with an ID, a timestamp and an activity component
1437 */
1438 protected ShortcutInfo makeShortcutWithTimestampWithActivity(String id, long timestamp,
1439 ComponentName activity) {
1440 final ShortcutInfo s = makeShortcut(
1441 id, "Title-" + id, activity, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001442 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001443 s.setTimestamp(timestamp);
1444 return s;
1445 }
1446
1447 /**
1448 * Make a shortcut with an ID and icon.
1449 */
1450 protected ShortcutInfo makeShortcutWithIcon(String id, Icon icon) {
1451 return makeShortcut(
1452 id, "Title-" + id, /* activity =*/ null, icon,
Makoto Onuki99302b52017-03-29 12:42:26 -07001453 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001454 }
1455
1456 protected ShortcutInfo makePackageShortcut(String packageName, String id) {
1457 String origCaller = getCallingPackage();
1458
1459 setCaller(packageName);
1460 ShortcutInfo s = makeShortcut(
1461 id, "Title-" + id, /* activity =*/ null, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001462 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001463 setCaller(origCaller); // restore the caller
1464
1465 return s;
1466 }
1467
1468 /**
1469 * Make multiple shortcuts with IDs.
1470 */
1471 protected List<ShortcutInfo> makeShortcuts(String... ids) {
1472 final ArrayList<ShortcutInfo> ret = new ArrayList();
1473 for (String id : ids) {
1474 ret.add(makeShortcut(id));
1475 }
1476 return ret;
1477 }
1478
1479 protected ShortcutInfo.Builder makeShortcutBuilder() {
1480 return new ShortcutInfo.Builder(mClientContext);
1481 }
1482
1483 protected ShortcutInfo makeShortcutWithActivity(String id, ComponentName activity) {
1484 return makeShortcut(
1485 id, "Title-" + id, 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
Makoto Onukib5a012f2016-06-21 11:13:53 -07001489 protected ShortcutInfo makeShortcutWithIntent(String id, Intent intent) {
1490 return makeShortcut(
1491 id, "Title-" + id, /* activity =*/ null, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001492 intent, /* rank =*/ 0);
Makoto Onukib5a012f2016-06-21 11:13:53 -07001493 }
1494
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001495 protected ShortcutInfo makeShortcutWithActivityAndTitle(String id, ComponentName activity,
1496 String title) {
1497 return makeShortcut(
1498 id, title, activity, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001499 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001500 }
1501
1502 protected ShortcutInfo makeShortcutWithActivityAndRank(String id, ComponentName activity,
1503 int rank) {
1504 return makeShortcut(
1505 id, "Title-" + id, activity, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001506 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), rank);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001507 }
1508
1509 /**
1510 * Make a shortcut with details.
1511 */
1512 protected ShortcutInfo makeShortcut(String id, String title, ComponentName activity,
Makoto Onuki99302b52017-03-29 12:42:26 -07001513 Icon icon, Intent intent, int rank) {
Makoto Onukib5a012f2016-06-21 11:13:53 -07001514 final ShortcutInfo.Builder b = new ShortcutInfo.Builder(mClientContext, id)
Makoto Onuki255461f2017-01-10 11:47:25 -08001515 .setActivity(new ComponentName(mClientContext.getPackageName(), "main"))
Makoto Onukib5a012f2016-06-21 11:13:53 -07001516 .setShortLabel(title)
Makoto Onuki99302b52017-03-29 12:42:26 -07001517 .setRank(rank)
1518 .setIntent(intent);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001519 if (icon != null) {
1520 b.setIcon(icon);
1521 }
1522 if (activity != null) {
1523 b.setActivity(activity);
1524 }
1525 final ShortcutInfo s = b.build();
1526
1527 s.setTimestamp(mInjectedCurrentTimeMillis); // HACK
1528
1529 return s;
1530 }
1531
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001532 protected ShortcutInfo makeShortcutWithIntents(String id, Intent... intents) {
1533 return makeShortcut(
1534 id, "Title-" + id, /* activity =*/ null, /* icon =*/ null,
1535 intents, /* rank =*/ 0);
1536 }
1537
1538 /**
1539 * Make a shortcut with details.
1540 */
1541 protected ShortcutInfo makeShortcut(String id, String title, ComponentName activity,
1542 Icon icon, Intent[] intents, int rank) {
1543 final ShortcutInfo.Builder b = new ShortcutInfo.Builder(mClientContext, id)
Makoto Onuki255461f2017-01-10 11:47:25 -08001544 .setActivity(new ComponentName(mClientContext.getPackageName(), "main"))
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001545 .setShortLabel(title)
1546 .setRank(rank)
1547 .setIntents(intents);
1548 if (icon != null) {
1549 b.setIcon(icon);
1550 }
1551 if (activity != null) {
1552 b.setActivity(activity);
1553 }
1554 final ShortcutInfo s = b.build();
1555
1556 s.setTimestamp(mInjectedCurrentTimeMillis); // HACK
1557
1558 return s;
1559 }
1560
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001561 /**
Makoto Onukib5a012f2016-06-21 11:13:53 -07001562 * Make a shortcut with details.
1563 */
1564 protected ShortcutInfo makeShortcutWithExtras(String id, Intent intent,
1565 PersistableBundle extras) {
1566 final ShortcutInfo.Builder b = new ShortcutInfo.Builder(mClientContext, id)
Makoto Onuki255461f2017-01-10 11:47:25 -08001567 .setActivity(new ComponentName(mClientContext.getPackageName(), "main"))
Makoto Onukib5a012f2016-06-21 11:13:53 -07001568 .setShortLabel("title-" + id)
1569 .setExtras(extras)
1570 .setIntent(intent);
1571 final ShortcutInfo s = b.build();
1572
1573 s.setTimestamp(mInjectedCurrentTimeMillis); // HACK
1574
1575 return s;
1576 }
1577
1578 /**
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001579 * Make an intent.
1580 */
1581 protected Intent makeIntent(String action, Class<?> clazz, Object... bundleKeysAndValues) {
1582 final Intent intent = new Intent(action);
1583 intent.setComponent(makeComponent(clazz));
1584 intent.replaceExtras(makeBundle(bundleKeysAndValues));
1585 return intent;
1586 }
1587
1588 /**
1589 * Make an component name, with the client context.
1590 */
1591 @NonNull
1592 protected ComponentName makeComponent(Class<?> clazz) {
1593 return new ComponentName(mClientContext, clazz);
1594 }
1595
1596 @NonNull
1597 protected ShortcutInfo findById(List<ShortcutInfo> list, String id) {
1598 for (ShortcutInfo s : list) {
1599 if (s.getId().equals(id)) {
1600 return s;
1601 }
1602 }
1603 fail("Shortcut with id " + id + " not found");
1604 return null;
1605 }
1606
1607 protected void assertSystem() {
1608 assertEquals("Caller must be system", Process.SYSTEM_UID, mInjectedCallingUid);
1609 }
1610
1611 protected void assertResetTimes(long expectedLastResetTime, long expectedNextResetTime) {
1612 assertEquals(expectedLastResetTime, mService.getLastResetTimeLocked());
1613 assertEquals(expectedNextResetTime, mService.getNextResetTimeLocked());
1614 }
1615
1616 public static List<ShortcutInfo> assertAllNotHaveIcon(
1617 List<ShortcutInfo> actualShortcuts) {
1618 for (ShortcutInfo s : actualShortcuts) {
1619 assertNull("ID " + s.getId(), s.getIcon());
1620 }
1621 return actualShortcuts;
1622 }
1623
1624 @NonNull
1625 protected List<ShortcutInfo> assertAllHaveFlags(@NonNull List<ShortcutInfo> actualShortcuts,
1626 int shortcutFlags) {
1627 for (ShortcutInfo s : actualShortcuts) {
1628 assertTrue("ID " + s.getId() + " doesn't have flags " + shortcutFlags,
1629 s.hasFlags(shortcutFlags));
1630 }
1631 return actualShortcuts;
1632 }
1633
1634 protected ShortcutInfo getPackageShortcut(String packageName, String shortcutId, int userId) {
1635 return mService.getPackageShortcutForTest(packageName, shortcutId, userId);
1636 }
1637
1638 protected void assertShortcutExists(String packageName, String shortcutId, int userId) {
1639 assertTrue(getPackageShortcut(packageName, shortcutId, userId) != null);
1640 }
1641
1642 protected void assertShortcutNotExists(String packageName, String shortcutId, int userId) {
1643 assertTrue(getPackageShortcut(packageName, shortcutId, userId) == null);
1644 }
1645
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001646 protected Intent[] launchShortcutAndGetIntentsInner(Runnable shortcutStarter,
1647 @NonNull String packageName, @NonNull String shortcutId, int userId) {
Wale Ogunwale6767eae2018-05-03 15:52:51 -07001648 reset(mMockActivityTaskManagerInternal);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001649 shortcutStarter.run();
1650
1651 final ArgumentCaptor<Intent[]> intentsCaptor = ArgumentCaptor.forClass(Intent[].class);
Wale Ogunwale6767eae2018-05-03 15:52:51 -07001652 verify(mMockActivityTaskManagerInternal).startActivitiesAsPackage(
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001653 eq(packageName),
1654 eq(userId),
1655 intentsCaptor.capture(),
Makoto Onukid0010c52017-03-30 14:17:35 -07001656 anyOrNull(Bundle.class));
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001657 return intentsCaptor.getValue();
1658 }
1659
1660 protected Intent[] launchShortcutAndGetIntents(
1661 @NonNull String packageName, @NonNull String shortcutId, int userId) {
1662 return launchShortcutAndGetIntentsInner(
1663 () -> {
1664 mLauncherApps.startShortcut(packageName, shortcutId, null, null,
1665 UserHandle.of(userId));
1666 }, packageName, shortcutId, userId
1667 );
1668 }
1669
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001670 protected Intent launchShortcutAndGetIntent(
1671 @NonNull String packageName, @NonNull String shortcutId, int userId) {
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001672 final Intent[] intents = launchShortcutAndGetIntents(packageName, shortcutId, userId);
1673 assertEquals(1, intents.length);
1674 return intents[0];
1675 }
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001676
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001677 protected Intent[] launchShortcutAndGetIntents_withShortcutInfo(
1678 @NonNull String packageName, @NonNull String shortcutId, int userId) {
1679 return launchShortcutAndGetIntentsInner(
1680 () -> {
1681 mLauncherApps.startShortcut(
1682 getShortcutInfoAsLauncher(packageName, shortcutId, userId), null, null);
1683 }, packageName, shortcutId, userId
1684 );
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001685 }
1686
1687 protected Intent launchShortcutAndGetIntent_withShortcutInfo(
1688 @NonNull String packageName, @NonNull String shortcutId, int userId) {
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001689 final Intent[] intents = launchShortcutAndGetIntents_withShortcutInfo(
1690 packageName, shortcutId, userId);
1691 assertEquals(1, intents.length);
1692 return intents[0];
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001693 }
1694
1695 protected void assertShortcutLaunchable(@NonNull String packageName, @NonNull String shortcutId,
1696 int userId) {
1697 assertNotNull(launchShortcutAndGetIntent(packageName, shortcutId, userId));
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001698 }
1699
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001700 protected void assertShortcutNotLaunched(@NonNull String packageName,
1701 @NonNull String shortcutId, int userId) {
Wale Ogunwale6767eae2018-05-03 15:52:51 -07001702 reset(mMockActivityTaskManagerInternal);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001703 try {
1704 mLauncherApps.startShortcut(packageName, shortcutId, null, null,
1705 UserHandle.of(userId));
1706 fail("ActivityNotFoundException was not thrown");
1707 } catch (ActivityNotFoundException expected) {
1708 }
1709 // This shouldn't have been called.
Wale Ogunwale6767eae2018-05-03 15:52:51 -07001710 verify(mMockActivityTaskManagerInternal, times(0)).startActivitiesAsPackage(
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001711 anyString(),
1712 anyInt(),
1713 any(Intent[].class),
Makoto Onukid0010c52017-03-30 14:17:35 -07001714 anyOrNull(Bundle.class));
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001715 }
1716
1717 protected void assertStartShortcutThrowsException(@NonNull String packageName,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07001718 @NonNull String shortcutId, int userId, Class<?> expectedException) {
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07001719 Exception thrown = null;
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001720 try {
Makoto Onukid6880792016-06-29 13:37:43 -07001721 mLauncherApps.startShortcut(packageName, shortcutId, null, null,
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001722 UserHandle.of(userId));
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07001723 } catch (Exception e) {
1724 thrown = e;
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001725 }
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07001726 assertNotNull("Exception was not thrown", thrown);
1727 assertEquals("Exception type different", expectedException, thrown.getClass());
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001728 }
1729
1730 protected void assertBitmapDirectories(int userId, String... expectedDirectories) {
1731 final Set<String> expected = hashSet(set(expectedDirectories));
1732
1733 final Set<String> actual = new HashSet<>();
1734
1735 final File[] files = mService.getUserBitmapFilePath(userId).listFiles();
1736 if (files != null) {
1737 for (File child : files) {
1738 if (child.isDirectory()) {
1739 actual.add(child.getName());
1740 }
1741 }
1742 }
1743
1744 assertEquals(expected, actual);
1745 }
1746
1747 protected void assertBitmapFiles(int userId, String packageName, String... expectedFiles) {
1748 final Set<String> expected = hashSet(set(expectedFiles));
1749
1750 final Set<String> actual = new HashSet<>();
1751
1752 final File[] files = new File(mService.getUserBitmapFilePath(userId), packageName)
1753 .listFiles();
1754 if (files != null) {
1755 for (File child : files) {
1756 if (child.isFile()) {
1757 actual.add(child.getName());
1758 }
1759 }
1760 }
1761
1762 assertEquals(expected, actual);
1763 }
1764
1765 protected String getBitmapFilename(int userId, String packageName, String shortcutId) {
1766 final ShortcutInfo si = mService.getPackageShortcutForTest(packageName, shortcutId, userId);
1767 if (si == null) {
1768 return null;
1769 }
Makoto Onuki475c3652017-05-08 14:29:03 -07001770 mService.waitForBitmapSavesForTest();
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001771 return new File(si.getBitmapPath()).getName();
1772 }
1773
Makoto Onuki475c3652017-05-08 14:29:03 -07001774 protected String getBitmapAbsPath(int userId, String packageName, String shortcutId) {
1775 final ShortcutInfo si = mService.getPackageShortcutForTest(packageName, shortcutId, userId);
1776 if (si == null) {
1777 return null;
1778 }
1779 mService.waitForBitmapSavesForTest();
1780 return new File(si.getBitmapPath()).getAbsolutePath();
1781 }
1782
Makoto Onukif3ba2e02016-07-12 09:18:50 -07001783 /**
1784 * @return all shortcuts stored internally for the caller. This reflects the *internal* view
1785 * of shortcuts, which may be different from what {@link #getCallerVisibleShortcuts} would
1786 * return, because getCallerVisibleShortcuts() will get shortcuts from the proper "front door"
1787 * which performs some extra checks, like {@link ShortcutPackage#onRestored}.
1788 */
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001789 protected List<ShortcutInfo> getCallerShortcuts() {
1790 final ShortcutPackage p = mService.getPackageShortcutForTest(
1791 getCallingPackage(), getCallingUserId());
1792 return p == null ? null : p.getAllShortcutsForTest();
1793 }
1794
Makoto Onukif3ba2e02016-07-12 09:18:50 -07001795 /**
1796 * @return all shortcuts owned by caller that are actually visible via ShortcutManager.
1797 * See also {@link #getCallerShortcuts}.
1798 */
1799 protected List<ShortcutInfo> getCallerVisibleShortcuts() {
1800 final ArrayList<ShortcutInfo> ret = new ArrayList<>();
1801 ret.addAll(mManager.getDynamicShortcuts());
1802 ret.addAll(mManager.getPinnedShortcuts());
1803 ret.addAll(mManager.getManifestShortcuts());
1804 return ret;
1805 }
1806
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001807 protected ShortcutInfo getCallerShortcut(String shortcutId) {
1808 return getPackageShortcut(getCallingPackage(), shortcutId, getCallingUserId());
1809 }
1810
1811 protected List<ShortcutInfo> getLauncherShortcuts(String launcher, int userId, int queryFlags) {
1812 final List<ShortcutInfo>[] ret = new List[1];
1813 runWithCaller(launcher, userId, () -> {
1814 final ShortcutQuery q = new ShortcutQuery();
1815 q.setQueryFlags(queryFlags);
1816 ret[0] = mLauncherApps.getShortcuts(q, UserHandle.of(userId));
1817 });
1818 return ret[0];
1819 }
1820
1821 protected List<ShortcutInfo> getLauncherPinnedShortcuts(String launcher, int userId) {
1822 return getLauncherShortcuts(launcher, userId, ShortcutQuery.FLAG_GET_PINNED);
1823 }
1824
Makoto Onukia01f4f02016-12-15 15:58:41 -08001825 protected List<ShortcutInfo> getShortcutAsLauncher(int targetUserId) {
1826 final ShortcutQuery q = new ShortcutQuery();
1827 q.setQueryFlags(ShortcutQuery.FLAG_MATCH_DYNAMIC | ShortcutQuery.FLAG_MATCH_DYNAMIC
1828 | ShortcutQuery.FLAG_MATCH_PINNED);
1829 return mLauncherApps.getShortcuts(q, UserHandle.of(targetUserId));
1830 }
1831
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001832 protected ShortcutInfo getShortcutInfoAsLauncher(String packageName, String shortcutId,
1833 int userId) {
1834 final List<ShortcutInfo> infoList =
1835 mLauncherApps.getShortcutInfo(packageName, list(shortcutId),
1836 UserHandle.of(userId));
1837 assertEquals("No shortcutInfo found (or too many of them)", 1, infoList.size());
1838 return infoList.get(0);
1839 }
1840
Makoto Onukiee6b6e42016-06-29 17:34:02 -07001841 protected Intent genPackageAddIntent(String packageName, int userId) {
1842 installPackage(userId, packageName);
1843
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001844 Intent i = new Intent(Intent.ACTION_PACKAGE_ADDED);
Makoto Onukiee6b6e42016-06-29 17:34:02 -07001845 i.setData(Uri.parse("package:" + packageName));
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001846 i.putExtra(Intent.EXTRA_USER_HANDLE, userId);
1847 return i;
1848 }
1849
1850 protected Intent genPackageDeleteIntent(String pakcageName, int userId) {
Makoto Onukiee6b6e42016-06-29 17:34:02 -07001851 uninstallPackage(userId, pakcageName);
1852
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001853 Intent i = new Intent(Intent.ACTION_PACKAGE_REMOVED);
1854 i.setData(Uri.parse("package:" + pakcageName));
1855 i.putExtra(Intent.EXTRA_USER_HANDLE, userId);
1856 return i;
1857 }
1858
1859 protected Intent genPackageUpdateIntent(String pakcageName, int userId) {
Makoto Onukiee6b6e42016-06-29 17:34:02 -07001860 installPackage(userId, pakcageName);
1861
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001862 Intent i = new Intent(Intent.ACTION_PACKAGE_ADDED);
1863 i.setData(Uri.parse("package:" + pakcageName));
1864 i.putExtra(Intent.EXTRA_USER_HANDLE, userId);
1865 i.putExtra(Intent.EXTRA_REPLACING, true);
1866 return i;
1867 }
1868
Makoto Onukib08790c2016-06-23 14:05:46 -07001869 protected Intent genPackageChangedIntent(String pakcageName, int userId) {
1870 Intent i = new Intent(Intent.ACTION_PACKAGE_CHANGED);
1871 i.setData(Uri.parse("package:" + pakcageName));
1872 i.putExtra(Intent.EXTRA_USER_HANDLE, userId);
1873 return i;
1874 }
1875
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001876 protected Intent genPackageDataClear(String packageName, int userId) {
1877 Intent i = new Intent(Intent.ACTION_PACKAGE_DATA_CLEARED);
1878 i.setData(Uri.parse("package:" + packageName));
1879 i.putExtra(Intent.EXTRA_USER_HANDLE, userId);
1880 return i;
1881 }
1882
1883 protected void assertExistsAndShadow(ShortcutPackageItem spi) {
1884 assertNotNull(spi);
1885 assertTrue(spi.getPackageInfo().isShadow());
1886 }
1887
1888 protected File makeFile(File baseDirectory, String... paths) {
1889 File ret = baseDirectory;
1890
1891 for (String path : paths) {
1892 ret = new File(ret, path);
1893 }
1894
1895 return ret;
1896 }
1897
1898 protected boolean bitmapDirectoryExists(String packageName, int userId) {
Makoto Onuki475c3652017-05-08 14:29:03 -07001899 mService.waitForBitmapSavesForTest();
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001900 final File path = new File(mService.getUserBitmapFilePath(userId), packageName);
1901 return path.isDirectory();
1902 }
1903 protected static ShortcutQuery buildQuery(long changedSince,
1904 String packageName, ComponentName componentName,
1905 /* @ShortcutQuery.QueryFlags */ int flags) {
1906 return buildQuery(changedSince, packageName, null, componentName, flags);
1907 }
1908
1909 protected static ShortcutQuery buildQuery(long changedSince,
1910 String packageName, List<String> shortcutIds, ComponentName componentName,
1911 /* @ShortcutQuery.QueryFlags */ int flags) {
1912 final ShortcutQuery q = new ShortcutQuery();
1913 q.setChangedSince(changedSince);
1914 q.setPackage(packageName);
1915 q.setShortcutIds(shortcutIds);
1916 q.setActivity(componentName);
1917 q.setQueryFlags(flags);
1918 return q;
1919 }
1920
1921 protected static ShortcutQuery buildAllQuery(String packageName) {
1922 final ShortcutQuery q = new ShortcutQuery();
1923 q.setPackage(packageName);
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07001924 q.setQueryFlags(ShortcutQuery.FLAG_GET_ALL_KINDS);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001925 return q;
1926 }
1927
1928 protected static ShortcutQuery buildPinnedQuery(String packageName) {
1929 final ShortcutQuery q = new ShortcutQuery();
1930 q.setPackage(packageName);
1931 q.setQueryFlags(ShortcutQuery.FLAG_GET_PINNED);
1932 return q;
1933 }
1934
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07001935 protected static ShortcutQuery buildQueryWithFlags(int queryFlags) {
1936 final ShortcutQuery q = new ShortcutQuery();
1937 q.setQueryFlags(queryFlags);
1938 return q;
1939 }
1940
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001941 protected void backupAndRestore() {
1942 int prevUid = mInjectedCallingUid;
1943
1944 mInjectedCallingUid = Process.SYSTEM_UID; // Only system can call it.
1945
1946 dumpsysOnLogcat("Before backup");
1947
1948 final byte[] payload = mService.getBackupPayload(USER_0);
1949 if (ENABLE_DUMP) {
1950 final String xml = new String(payload);
1951 Log.v(TAG, "Backup payload:");
1952 for (String line : xml.split("\n")) {
1953 Log.v(TAG, line);
1954 }
1955 }
1956
1957 // Before doing anything else, uninstall all packages.
1958 for (int userId : list(USER_0, USER_P0)) {
1959 for (String pkg : list(CALLING_PACKAGE_1, CALLING_PACKAGE_2, CALLING_PACKAGE_3,
1960 LAUNCHER_1, LAUNCHER_2, LAUNCHER_3)) {
1961 uninstallPackage(userId, pkg);
1962 }
1963 }
1964
1965 shutdownServices();
1966
1967 deleteAllSavedFiles();
1968
1969 initService();
1970 mService.applyRestore(payload, USER_0);
1971
1972 // handleUnlockUser will perform the gone package check, but it shouldn't remove
1973 // shadow information.
1974 mService.handleUnlockUser(USER_0);
1975
1976 dumpsysOnLogcat("After restore");
1977
1978 mInjectedCallingUid = prevUid;
1979 }
1980
1981 protected void prepareCrossProfileDataSet() {
Makoto Onuki9c850012016-07-26 15:50:50 -07001982 mRunningUsers.put(USER_10, true); // this test needs user 10.
1983
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001984 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
1985 assertTrue(mManager.setDynamicShortcuts(list(
1986 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"),
1987 makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6"))));
1988 });
1989 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
1990 assertTrue(mManager.setDynamicShortcuts(list(
1991 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"),
1992 makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6"))));
1993 });
1994 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
1995 assertTrue(mManager.setDynamicShortcuts(list(
1996 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"),
1997 makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6"))));
1998 });
1999 runWithCaller(CALLING_PACKAGE_4, USER_0, () -> {
2000 assertTrue(mManager.setDynamicShortcuts(list()));
2001 });
2002 runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
2003 assertTrue(mManager.setDynamicShortcuts(list(
2004 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"),
2005 makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6"))));
2006 });
2007 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
2008 assertTrue(mManager.setDynamicShortcuts(list(
2009 makeShortcut("x1"), makeShortcut("x2"), makeShortcut("x3"),
2010 makeShortcut("x4"), makeShortcut("x5"), makeShortcut("x6"))));
2011 });
2012
2013 runWithCaller(LAUNCHER_1, USER_0, () -> {
2014 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s1"), HANDLE_USER_0);
2015 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s1", "s2"), HANDLE_USER_0);
2016 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("s1", "s2", "s3"), HANDLE_USER_0);
2017
2018 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s1", "s4"), HANDLE_USER_P0);
2019 });
2020 runWithCaller(LAUNCHER_2, USER_0, () -> {
2021 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s2"), HANDLE_USER_0);
2022 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s2", "s3"), HANDLE_USER_0);
2023 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("s2", "s3", "s4"), HANDLE_USER_0);
2024
2025 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s2", "s5"), HANDLE_USER_P0);
2026 });
Makoto Onukif3ba2e02016-07-12 09:18:50 -07002027
2028 // Note LAUNCHER_3 has allowBackup=false.
Makoto Onuki51ab2b32016-06-02 11:03:51 -07002029 runWithCaller(LAUNCHER_3, USER_0, () -> {
2030 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s3"), HANDLE_USER_0);
2031 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s3", "s4"), HANDLE_USER_0);
2032 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("s3", "s4", "s5"), HANDLE_USER_0);
2033
2034 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s3", "s6"), HANDLE_USER_P0);
2035 });
2036 runWithCaller(LAUNCHER_4, USER_0, () -> {
2037 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list(), HANDLE_USER_0);
2038 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list(), HANDLE_USER_0);
2039 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list(), HANDLE_USER_0);
2040 mLauncherApps.pinShortcuts(CALLING_PACKAGE_4, list(), HANDLE_USER_0);
2041 });
2042
2043 // Launcher on a managed profile is referring ot user 0!
2044 runWithCaller(LAUNCHER_1, USER_P0, () -> {
2045 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s3", "s4"), HANDLE_USER_0);
2046 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s3", "s4", "s5"), HANDLE_USER_0);
2047 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("s3", "s4", "s5", "s6"),
2048 HANDLE_USER_0);
2049
2050 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s4", "s1"), HANDLE_USER_P0);
2051 });
2052 runWithCaller(LAUNCHER_1, USER_10, () -> {
2053 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("x4", "x5"), HANDLE_USER_10);
2054 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("x4", "x5", "x6"), HANDLE_USER_10);
2055 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("x4", "x5", "x6", "x1"),
2056 HANDLE_USER_10);
2057 });
2058
2059 // Then remove some dynamic shortcuts.
2060 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2061 assertTrue(mManager.setDynamicShortcuts(list(
2062 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2063 });
2064 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
2065 assertTrue(mManager.setDynamicShortcuts(list(
2066 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2067 });
2068 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
2069 assertTrue(mManager.setDynamicShortcuts(list(
2070 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2071 });
2072 runWithCaller(CALLING_PACKAGE_4, USER_0, () -> {
2073 assertTrue(mManager.setDynamicShortcuts(list()));
2074 });
2075 runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
2076 assertTrue(mManager.setDynamicShortcuts(list(
2077 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2078 });
2079 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
2080 assertTrue(mManager.setDynamicShortcuts(list(
2081 makeShortcut("x1"), makeShortcut("x2"), makeShortcut("x3"))));
2082 });
2083 }
Makoto Onukib5a012f2016-06-21 11:13:53 -07002084
2085 public static List<ShortcutInfo> assertAllHaveIconResId(
2086 List<ShortcutInfo> actualShortcuts) {
2087 for (ShortcutInfo s : actualShortcuts) {
2088 assertTrue("ID " + s.getId() + " not have icon res ID", s.hasIconResource());
2089 assertFalse("ID " + s.getId() + " shouldn't have icon FD", s.hasIconFile());
2090 }
2091 return actualShortcuts;
2092 }
2093
2094 public static List<ShortcutInfo> assertAllHaveIconFile(
2095 List<ShortcutInfo> actualShortcuts) {
2096 for (ShortcutInfo s : actualShortcuts) {
2097 assertFalse("ID " + s.getId() + " shouldn't have icon res ID", s.hasIconResource());
2098 assertTrue("ID " + s.getId() + " not have icon FD", s.hasIconFile());
2099 }
2100 return actualShortcuts;
2101 }
2102
2103 public static List<ShortcutInfo> assertAllHaveIcon(
2104 List<ShortcutInfo> actualShortcuts) {
2105 for (ShortcutInfo s : actualShortcuts) {
Makoto Onukia01f4f02016-12-15 15:58:41 -08002106 assertTrue("ID " + s.getId() + " has no icon ",
2107 s.hasIconFile() || s.hasIconResource() || s.getIcon() != null);
Makoto Onukib5a012f2016-06-21 11:13:53 -07002108 }
2109 return actualShortcuts;
2110 }
2111
2112 public static List<ShortcutInfo> assertAllStringsResolved(
2113 List<ShortcutInfo> actualShortcuts) {
2114 for (ShortcutInfo s : actualShortcuts) {
2115 assertTrue("ID " + s.getId(), s.hasStringResourcesResolved());
2116 }
2117 return actualShortcuts;
2118 }
Makoto Onuki76269922016-07-15 14:58:54 -07002119
2120 public String readTestAsset(String assetPath) throws IOException {
2121 final StringBuilder sb = new StringBuilder();
2122 try (BufferedReader br = new BufferedReader(
2123 new InputStreamReader(
2124 getTestContext().getResources().getAssets().open(assetPath)))) {
2125 String line;
2126 while ((line = br.readLine()) != null) {
2127 sb.append(line);
2128 sb.append(System.lineSeparator());
2129 }
2130 }
2131 return sb.toString();
2132 }
Makoto Onuki0b9d1db2016-07-18 14:16:41 -07002133
2134 protected void prepareGetHomeActivitiesAsUser(ComponentName preferred,
2135 List<ResolveInfo> candidates, int userId) {
2136 doAnswer(inv -> {
2137 ((List) inv.getArguments()[0]).addAll(candidates);
2138 return preferred;
2139 }).when(mMockPackageManagerInternal).getHomeActivitiesAsUser(any(List.class), eq(userId));
2140 }
2141
2142 protected static ComponentName cn(String packageName, String name) {
2143 return new ComponentName(packageName, name);
2144 }
2145
2146 protected static ResolveInfo ri(String packageName, String name, boolean isSystem, int priority) {
2147 final ResolveInfo ri = new ResolveInfo();
2148 ri.activityInfo = new ActivityInfo();
2149 ri.activityInfo.applicationInfo = new ApplicationInfo();
2150
2151 ri.activityInfo.packageName = packageName;
2152 ri.activityInfo.name = name;
2153 if (isSystem) {
2154 ri.activityInfo.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
2155 }
2156 ri.priority = priority;
2157 return ri;
2158 }
2159
2160 protected static ResolveInfo getSystemLauncher() {
2161 return ri(PACKAGE_SYSTEM_LAUNCHER, PACKAGE_SYSTEM_LAUNCHER_NAME, true,
2162 PACKAGE_SYSTEM_LAUNCHER_PRIORITY);
2163 }
2164
2165 protected static ResolveInfo getFallbackLauncher() {
2166 return ri(PACKAGE_FALLBACK_LAUNCHER, PACKAGE_FALLBACK_LAUNCHER_NAME, true,
2167 PACKAGE_FALLBACK_LAUNCHER_PRIORITY);
2168 }
Makoto Onukia01f4f02016-12-15 15:58:41 -08002169
Tony Maked6ef622017-12-07 16:36:16 +00002170 protected void makeUidForeground(int uid) {
Makoto Onukia01f4f02016-12-15 15:58:41 -08002171 try {
2172 mService.mUidObserver.onUidStateChanged(
Tony Maked6ef622017-12-07 16:36:16 +00002173 uid, ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE, 0);
2174 } catch (RemoteException e) {
2175 e.rethrowAsRuntimeException();
2176 }
2177 }
2178
2179 protected void makeCallerForeground() {
2180 makeUidForeground(mInjectedCallingUid);
2181 }
2182
2183 protected void makeUidBackground(int uid) {
2184 try {
2185 mService.mUidObserver.onUidStateChanged(
2186 uid, ActivityManager.PROCESS_STATE_TOP_SLEEPING, 0);
Makoto Onukia01f4f02016-12-15 15:58:41 -08002187 } catch (RemoteException e) {
2188 e.rethrowAsRuntimeException();
2189 }
2190 }
2191
2192 protected void makeCallerBackground() {
Tony Maked6ef622017-12-07 16:36:16 +00002193 makeUidBackground(mInjectedCallingUid);
Makoto Onukia01f4f02016-12-15 15:58:41 -08002194 }
2195
2196 protected void publishManifestShortcutsAsCaller(int resId) {
2197 addManifestShortcutResource(
2198 new ComponentName(getCallingPackage(), ShortcutActivity.class.getName()),
2199 resId);
2200 updatePackageVersion(getCallingPackage(), 1);
2201 mService.mPackageMonitor.onReceive(getTestContext(),
2202 genPackageAddIntent(getCallingPackage(), getCallingUserId()));
2203 }
Makoto Onuki50a320e2017-05-31 14:38:42 -07002204
2205 protected void assertFileNotExists(String path) {
2206 final File f = new File(mInjectedFilePathRoot, path);
2207 assertFalse("File shouldn't exist: " + f.getAbsolutePath(), f.exists());
2208 }
2209
2210 protected void assertFileExistsWithContent(String path) {
2211 final File f = new File(mInjectedFilePathRoot, path);
2212 assertTrue("File should exist: " + f.getAbsolutePath(), f.exists());
2213 assertTrue("File should be larger than 0b: " + f.getAbsolutePath(), f.length() > 0);
2214 }
Makoto Onuki51ab2b32016-06-02 11:03:51 -07002215}