blob: df2b3efeb94c98a330ce6fe19651aeef5f9c6446 [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;
Philip P. Moltmannee295092020-02-10 08:46:26 -080027import static org.mockito.ArgumentMatchers.isNull;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070028import static org.mockito.Matchers.any;
Makoto Onuki157b1622016-06-02 16:13:10 -070029import static org.mockito.Matchers.anyInt;
30import static org.mockito.Matchers.anyString;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070031import static org.mockito.Matchers.eq;
32import static org.mockito.Mockito.doAnswer;
33import static org.mockito.Mockito.mock;
34import static org.mockito.Mockito.reset;
35import static org.mockito.Mockito.spy;
Makoto Onukiff14f732016-06-30 17:07:25 -070036import static org.mockito.Mockito.times;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070037import static org.mockito.Mockito.verify;
38import static org.mockito.Mockito.when;
39
40import android.annotation.NonNull;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070041import android.annotation.UserIdInt;
42import android.app.Activity;
Makoto Onuki33525d22016-08-03 15:45:24 -070043import android.app.ActivityManager;
Makoto Onukiea11db12016-06-24 15:17:44 -070044import android.app.ActivityManagerInternal;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070045import android.app.IUidObserver;
Mehdi Alizadeh14242af2018-12-20 20:11:35 -080046import android.app.Person;
Varun Shah2546cef2019-01-11 15:50:54 -080047import android.app.admin.DevicePolicyManager;
Makoto Onukiac042502016-05-20 16:39:42 -070048import android.app.usage.UsageStatsManagerInternal;
Makoto Onuki440a1ea2016-07-20 14:21:18 -070049import android.content.ActivityNotFoundException;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070050import android.content.BroadcastReceiver;
51import android.content.ComponentName;
52import android.content.Context;
53import android.content.Intent;
54import android.content.IntentFilter;
Makoto Onuki2d895c32016-12-02 15:48:40 -080055import android.content.IntentSender;
Mehdi Alizadehae808ff2020-01-21 13:39:53 -080056import android.content.LocusId;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070057import android.content.pm.ActivityInfo;
58import android.content.pm.ApplicationInfo;
59import android.content.pm.ILauncherApps;
60import android.content.pm.LauncherApps;
61import android.content.pm.LauncherApps.ShortcutQuery;
62import android.content.pm.PackageInfo;
63import android.content.pm.PackageManager;
64import android.content.pm.PackageManagerInternal;
Dan Cashman5c9f527e2018-04-03 16:42:23 -070065import android.content.pm.PackageParser;
Makoto Onukib08790c2016-06-23 14:05:46 -070066import android.content.pm.ResolveInfo;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070067import android.content.pm.ShortcutInfo;
68import android.content.pm.ShortcutManager;
69import android.content.pm.ShortcutServiceInternal;
70import android.content.pm.Signature;
Dan Cashman5c9f527e2018-04-03 16:42:23 -070071import android.content.pm.SigningInfo;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070072import android.content.pm.UserInfo;
73import android.content.res.Resources;
74import android.content.res.XmlResourceParser;
75import android.graphics.drawable.Icon;
76import android.net.Uri;
77import android.os.Bundle;
78import android.os.FileUtils;
79import android.os.Handler;
80import android.os.Looper;
Makoto Onukib5a012f2016-06-21 11:13:53 -070081import android.os.PersistableBundle;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070082import android.os.Process;
Makoto Onukia01f4f02016-12-15 15:58:41 -080083import android.os.RemoteException;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070084import android.os.UserHandle;
85import android.os.UserManager;
Sunny Goyal145c8f82018-02-15 14:27:09 -080086import android.os.UserManagerInternal;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070087import android.test.InstrumentationTestCase;
88import android.test.mock.MockContext;
Makoto Onuki2d895c32016-12-02 15:48:40 -080089import android.util.ArrayMap;
Makoto Onuki51ab2b32016-06-02 11:03:51 -070090import android.util.Log;
91import android.util.Pair;
92
Makoto Onuki51ab2b32016-06-02 11:03:51 -070093import com.android.server.LocalServices;
94import com.android.server.SystemService;
95import com.android.server.pm.LauncherAppsService.LauncherAppsImpl;
96import com.android.server.pm.ShortcutUser.PackageWithUser;
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -070097import com.android.server.wm.ActivityTaskManagerInternal;
Mehdi Alizadeh32774622018-11-05 17:32:01 -080098
Makoto Onuki51ab2b32016-06-02 11:03:51 -070099import org.junit.Assert;
100import org.mockito.ArgumentCaptor;
Makoto Onukie70b29e2018-04-03 16:44:39 -0700101import org.mockito.invocation.InvocationOnMock;
102import org.mockito.stubbing.Answer;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700103
104import java.io.BufferedReader;
105import java.io.ByteArrayOutputStream;
106import java.io.File;
107import java.io.FileReader;
Makoto Onuki76269922016-07-15 14:58:54 -0700108import java.io.IOException;
109import java.io.InputStreamReader;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700110import java.io.PrintWriter;
111import java.util.ArrayList;
112import java.util.HashMap;
113import java.util.HashSet;
114import java.util.LinkedHashMap;
115import java.util.List;
Makoto Onuki157b1622016-06-02 16:13:10 -0700116import java.util.Locale;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700117import java.util.Map;
Daulet Zhanguzinfdb98192019-12-20 18:11:26 +0000118import java.util.Objects;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700119import java.util.Set;
Makoto Onukib08790c2016-06-23 14:05:46 -0700120import java.util.function.BiFunction;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700121import java.util.function.BiPredicate;
122import java.util.function.Consumer;
Makoto Onukie70b29e2018-04-03 16:44:39 -0700123import java.util.function.Function;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700124
125public abstract class BaseShortcutManagerTest extends InstrumentationTestCase {
126 protected static final String TAG = "ShortcutManagerTest";
127
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700128 protected static final boolean DUMP_IN_TEARDOWN = false; // DO NOT SUBMIT WITH true
129
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700130 /**
131 * Whether to enable dump or not. Should be only true when debugging to avoid bugs where
132 * dump affecting the behavior.
133 */
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700134 protected static final boolean ENABLE_DUMP = false // DO NOT SUBMIT WITH true
135 || DUMP_IN_TEARDOWN || ShortcutService.DEBUG;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700136
137 protected static final String[] EMPTY_STRINGS = new String[0]; // Just for readability.
138
Makoto Onukib08790c2016-06-23 14:05:46 -0700139 protected static final String MAIN_ACTIVITY_CLASS = "MainActivity";
Makoto Onuki2d895c32016-12-02 15:48:40 -0800140 protected static final String PIN_CONFIRM_ACTIVITY_CLASS = "PinConfirmActivity";
Makoto Onukib08790c2016-06-23 14:05:46 -0700141
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700142 // public for mockito
143 public class BaseContext extends MockContext {
144 @Override
145 public Object getSystemService(String name) {
146 switch (name) {
147 case Context.USER_SERVICE:
148 return mMockUserManager;
Varun Shah2546cef2019-01-11 15:50:54 -0800149 case Context.DEVICE_POLICY_SERVICE:
150 return mMockDevicePolicyManager;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700151 }
Varun Shah2546cef2019-01-11 15:50:54 -0800152 throw new UnsupportedOperationException("Couldn't find system service: " + name);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700153 }
154
155 @Override
156 public String getSystemServiceName(Class<?> serviceClass) {
157 return getTestContext().getSystemServiceName(serviceClass);
158 }
159
160 @Override
161 public PackageManager getPackageManager() {
162 return mMockPackageManager;
163 }
164
165 @Override
166 public Resources getResources() {
167 return getTestContext().getResources();
168 }
169
170 @Override
171 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
172 IntentFilter filter, String broadcastPermission, Handler scheduler) {
173 // ignore.
174 return null;
175 }
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700176
177 @Override
178 public void unregisterReceiver(BroadcastReceiver receiver) {
179 // ignore.
180 }
Makoto Onuki2d895c32016-12-02 15:48:40 -0800181
182 @Override
183 public void startActivityAsUser(Intent intent, UserHandle user) {
184 // ignore, use spy to intercept it.
185 }
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700186 }
187
188 /** Context used in the client side */
189 public class ClientContext extends BaseContext {
190 @Override
191 public String getPackageName() {
192 return mInjectedClientPackage;
193 }
194
195 @Override
196 public int getUserId() {
197 return getCallingUserId();
198 }
199 }
200
201 /** Context used in the service side */
202 public class ServiceContext extends BaseContext {
203 long injectClearCallingIdentity() {
204 final int prevCallingUid = mInjectedCallingUid;
205 mInjectedCallingUid = Process.SYSTEM_UID;
206 return prevCallingUid;
207 }
208
209 void injectRestoreCallingIdentity(long token) {
210 mInjectedCallingUid = (int) token;
211 }
212
213 @Override
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700214 public int getUserId() {
215 return UserHandle.USER_SYSTEM;
216 }
217
218 public PackageInfo injectGetActivitiesWithMetadata(
219 String packageName, @UserIdInt int userId) {
220 return BaseShortcutManagerTest.this.injectGetActivitiesWithMetadata(packageName, userId);
221 }
222
223 public XmlResourceParser injectXmlMetaData(ActivityInfo activityInfo, String key) {
224 return BaseShortcutManagerTest.this.injectXmlMetaData(activityInfo, key);
225 }
Makoto Onuki2d895c32016-12-02 15:48:40 -0800226
227 public void sendIntentSender(IntentSender intent) {
228 // Placeholder for spying.
229 }
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700230 }
231
232 /** ShortcutService with injection override methods. */
233 protected final class ShortcutServiceTestable extends ShortcutService {
234 final ServiceContext mContext;
235 IUidObserver mUidObserver;
236
237 public ShortcutServiceTestable(ServiceContext context, Looper looper) {
Makoto Onukiee6b6e42016-06-29 17:34:02 -0700238 super(context, looper, /* onyForPackageManagerApis */ false);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700239 mContext = context;
240 }
241
242 @Override
Makoto Onuki4e6cef42016-07-13 16:14:01 -0700243 public String injectGetLocaleTagsForUser(@UserIdInt int userId) {
244 return mInjectedLocale.toLanguageTag();
245 }
246
247 @Override
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700248 boolean injectShouldPerformVerification() {
249 return true; // Always verify during unit tests.
250 }
251
252 @Override
253 String injectShortcutManagerConstants() {
254 return ConfigConstants.KEY_RESET_INTERVAL_SEC + "=" + (INTERVAL / 1000) + ","
255 + ConfigConstants.KEY_MAX_SHORTCUTS + "=" + MAX_SHORTCUTS + ","
256 + ConfigConstants.KEY_MAX_UPDATES_PER_INTERVAL + "="
257 + MAX_UPDATES_PER_INTERVAL + ","
258 + ConfigConstants.KEY_MAX_ICON_DIMENSION_DP + "=" + MAX_ICON_DIMENSION + ","
259 + ConfigConstants.KEY_MAX_ICON_DIMENSION_DP_LOWRAM + "="
260 + MAX_ICON_DIMENSION_LOWRAM + ","
261 + ConfigConstants.KEY_ICON_FORMAT + "=PNG,"
262 + ConfigConstants.KEY_ICON_QUALITY + "=100";
263 }
264
265 @Override
266 long injectClearCallingIdentity() {
267 return mContext.injectClearCallingIdentity();
268 }
269
270 @Override
271 void injectRestoreCallingIdentity(long token) {
272 mContext.injectRestoreCallingIdentity(token);
273 }
274
275 @Override
276 int injectDipToPixel(int dip) {
277 return dip;
278 }
279
280 @Override
281 long injectCurrentTimeMillis() {
282 return mInjectedCurrentTimeMillis;
283 }
284
285 @Override
286 long injectElapsedRealtime() {
287 // TODO This should be kept separately from mInjectedCurrentTimeMillis, since
288 // this should increase even if we rewind mInjectedCurrentTimeMillis in some tests.
289 return mInjectedCurrentTimeMillis - START_TIME;
290 }
291
292 @Override
Makoto Onuki475c3652017-05-08 14:29:03 -0700293 long injectUptimeMillis() {
294 return mInjectedCurrentTimeMillis - START_TIME - mDeepSleepTime;
295 }
296
297 @Override
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700298 int injectBinderCallingUid() {
299 return mInjectedCallingUid;
300 }
301
302 @Override
Makoto Onuki7d0fa812018-02-21 11:24:43 -0800303 int injectBinderCallingPid() {
304 // Note it's not used in tests, so just return a "random" value.
305 return mInjectedCallingUid * 123;
306 }
307
308 @Override
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700309 int injectGetPackageUid(String packageName, int userId) {
310 return getInjectedPackageInfo(packageName, userId, false).applicationInfo.uid;
311 }
312
313 @Override
314 File injectSystemDataPath() {
315 return new File(mInjectedFilePathRoot, "system");
316 }
317
318 @Override
319 File injectUserDataPath(@UserIdInt int userId) {
320 return new File(mInjectedFilePathRoot, "user-" + userId);
321 }
322
323 @Override
324 void injectValidateIconResPackage(ShortcutInfo shortcut, Icon icon) {
325 // Can't check
326 }
327
328 @Override
329 boolean injectIsLowRamDevice() {
330 return mInjectedIsLowRamDevice;
331 }
332
333 @Override
334 void injectRegisterUidObserver(IUidObserver observer, int which) {
335 mUidObserver = observer;
336 }
337
338 @Override
Makoto Onuki634cecb2017-10-13 17:10:48 -0700339 boolean hasShortcutHostPermission(@NonNull String callingPackage, int userId,
340 int callingPid, int callingUid) {
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700341 return mDefaultLauncherChecker.test(callingPackage, userId);
342 }
343
344 @Override
Makoto Onuki7d0fa812018-02-21 11:24:43 -0800345 boolean injectHasUnlimitedShortcutsApiCallsPermission(int callingPid, int callingUid) {
346 return mInjectHasUnlimitedShortcutsApiCallsPermission;
347 }
348
349 @Override
Makoto Onuki2d895c32016-12-02 15:48:40 -0800350 ComponentName getDefaultLauncher(@UserIdInt int userId) {
351 final ComponentName activity = mDefaultLauncher.get(userId);
352 if (activity != null) {
353 return activity;
354 }
355 return super.getDefaultLauncher(userId);
356 }
357
358 @Override
Makoto Onukiee6b6e42016-06-29 17:34:02 -0700359 PackageInfo injectPackageInfoWithUninstalled(String packageName, @UserIdInt int userId,
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700360 boolean getSignatures) {
361 return getInjectedPackageInfo(packageName, userId, getSignatures);
362 }
363
364 @Override
Makoto Onukiee6b6e42016-06-29 17:34:02 -0700365 ApplicationInfo injectApplicationInfoWithUninstalled(
366 String packageName, @UserIdInt int userId) {
367 PackageInfo pi = injectPackageInfoWithUninstalled(
368 packageName, userId, /* getSignatures= */ false);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700369 return pi != null ? pi.applicationInfo : null;
370 }
371
372 @Override
Makoto Onukiee6b6e42016-06-29 17:34:02 -0700373 List<PackageInfo> injectGetPackagesWithUninstalled(@UserIdInt int userId) {
374 return BaseShortcutManagerTest.this.getInstalledPackagesWithUninstalled(userId);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700375 }
376
377 @Override
Makoto Onukiee6b6e42016-06-29 17:34:02 -0700378 ActivityInfo injectGetActivityInfoWithMetadataWithUninstalled(ComponentName activity,
Makoto Onukib08790c2016-06-23 14:05:46 -0700379 @UserIdInt int userId) {
380 final PackageInfo pi = mContext.injectGetActivitiesWithMetadata(
381 activity.getPackageName(), userId);
382 if (pi == null || pi.activities == null) {
383 return null;
384 }
385 for (ActivityInfo ai : pi.activities) {
386 if (!mEnabledActivityChecker.test(ai.getComponentName(), userId)) {
387 continue;
388 }
389 if (activity.equals(ai.getComponentName())) {
390 return ai;
391 }
392 }
393 return null;
394 }
395
396 @Override
397 boolean injectIsMainActivity(@NonNull ComponentName activity, int userId) {
398 if (!mEnabledActivityChecker.test(activity, userId)) {
399 return false;
400 }
401 return mMainActivityChecker.test(activity, userId);
402 }
403
404 @Override
405 List<ResolveInfo> injectGetMainActivities(@NonNull String packageName, int userId) {
406 final PackageInfo pi = mContext.injectGetActivitiesWithMetadata(
407 packageName, userId);
408 if (pi == null || pi.activities == null) {
409 return null;
410 }
411 final ArrayList<ResolveInfo> ret = new ArrayList<>(pi.activities.length);
412 for (int i = 0; i < pi.activities.length; i++) {
413 if (!mEnabledActivityChecker.test(pi.activities[i].getComponentName(), userId)) {
414 continue;
415 }
416 final ResolveInfo ri = new ResolveInfo();
417 ri.activityInfo = pi.activities[i];
418 ret.add(ri);
419 }
420
421 return ret;
422 }
423
424 @Override
425 ComponentName injectGetDefaultMainActivity(@NonNull String packageName, int userId) {
426 return mMainActivityFetcher.apply(packageName, userId);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700427 }
428
429 @Override
Makoto Onuki2d895c32016-12-02 15:48:40 -0800430 ComponentName injectGetPinConfirmationActivity(@NonNull String launcherPackageName,
Sunny Goyal7f7372a2017-01-24 11:53:54 -0800431 int launcherUserId, int requestType) {
Makoto Onuki2d895c32016-12-02 15:48:40 -0800432 return mPinConfirmActivityFetcher.apply(launcherPackageName, launcherUserId);
433 }
434
435 @Override
Makoto Onukiee6b6e42016-06-29 17:34:02 -0700436 boolean injectIsActivityEnabledAndExported(ComponentName activity, @UserIdInt int userId) {
437 return mEnabledActivityChecker.test(activity, userId);
438 }
439
440 @Override
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700441 XmlResourceParser injectXmlMetaData(ActivityInfo activityInfo, String key) {
442 return mContext.injectXmlMetaData(activityInfo, key);
443 }
444
445 @Override
Makoto Onuki157b1622016-06-02 16:13:10 -0700446 void injectPostToHandler(Runnable r) {
Makoto Onukia2241832016-07-06 13:28:37 -0700447 runOnHandler(r);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700448 }
449
450 @Override
Makoto Onuki085a05c2016-08-19 11:39:29 -0700451 void injectRunOnNewThread(Runnable r) {
452 runOnHandler(r);
453 }
454
455 @Override
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700456 void injectEnforceCallingPermission(String permission, String message) {
457 if (!mCallerPermissions.contains(permission)) {
458 throw new SecurityException("Missing permission: " + permission);
459 }
460 }
461
462 @Override
Makoto Onukib08790c2016-06-23 14:05:46 -0700463 boolean injectIsSafeModeEnabled() {
464 return mSafeMode;
465 }
466
467 @Override
Makoto Onuki33663282016-08-22 16:19:04 -0700468 String injectBuildFingerprint() {
469 return mInjectedBuildFingerprint;
470 }
471
472 @Override
Sunny Goyal87a563e2017-01-01 19:42:45 -0800473 void injectSendIntentSender(IntentSender intent, Intent extras) {
Makoto Onuki2d895c32016-12-02 15:48:40 -0800474 mContext.sendIntentSender(intent);
475 }
476
477 @Override
Makoto Onuki35559d62017-11-06 16:26:32 -0800478 boolean injectHasAccessShortcutsPermission(int callingPid, int callingUid) {
Makoto Onuki634cecb2017-10-13 17:10:48 -0700479 return mInjectCheckAccessShortcutsPermission;
480 }
481
482 @Override
Makoto Onukia2241832016-07-06 13:28:37 -0700483 void wtf(String message, Throwable th) {
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700484 // During tests, WTF is fatal.
Makoto Onukia2241832016-07-06 13:28:37 -0700485 fail(message + " exception: " + th + "\n" + Log.getStackTraceString(th));
486 }
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700487 }
488
489 /** ShortcutManager with injection override methods. */
490 protected class ShortcutManagerTestable extends ShortcutManager {
491 public ShortcutManagerTestable(Context context, ShortcutServiceTestable service) {
492 super(context, service);
493 }
494
495 @Override
496 protected int injectMyUserId() {
497 return UserHandle.getUserId(mInjectedCallingUid);
498 }
499
500 @Override
501 public boolean setDynamicShortcuts(@NonNull List<ShortcutInfo> shortcutInfoList) {
502 // Note to simulate the binder RPC, we need to clone the incoming arguments.
503 // Otherwise bad things will happen because they're mutable.
504 return super.setDynamicShortcuts(cloneShortcutList(shortcutInfoList));
505 }
506
507 @Override
508 public boolean addDynamicShortcuts(@NonNull List<ShortcutInfo> shortcutInfoList) {
509 // Note to simulate the binder RPC, we need to clone the incoming arguments.
510 return super.addDynamicShortcuts(cloneShortcutList(shortcutInfoList));
511 }
512
513 @Override
514 public boolean updateShortcuts(List<ShortcutInfo> shortcutInfoList) {
515 // Note to simulate the binder RPC, we need to clone the incoming arguments.
516 return super.updateShortcuts(cloneShortcutList(shortcutInfoList));
517 }
518 }
519
520 protected class LauncherAppImplTestable extends LauncherAppsImpl {
521 final ServiceContext mContext;
522
523 public LauncherAppImplTestable(ServiceContext context) {
524 super(context);
525 mContext = context;
526 }
527
528 @Override
529 public void verifyCallingPackage(String callingPackage) {
530 // SKIP
531 }
532
533 @Override
534 void postToPackageMonitorHandler(Runnable r) {
Makoto Onukia2241832016-07-06 13:28:37 -0700535 runOnHandler(r);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700536 }
537
538 @Override
539 int injectBinderCallingUid() {
540 return mInjectedCallingUid;
541 }
542
543 @Override
Makoto Onuki7d0fa812018-02-21 11:24:43 -0800544 int injectBinderCallingPid() {
545 // Note it's not used in tests, so just return a "random" value.
546 return mInjectedCallingUid * 123;
547 }
548
549 @Override
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700550 long injectClearCallingIdentity() {
551 final int prevCallingUid = mInjectedCallingUid;
552 mInjectedCallingUid = Process.SYSTEM_UID;
553 return prevCallingUid;
554 }
555
556 @Override
557 void injectRestoreCallingIdentity(long token) {
558 mInjectedCallingUid = (int) token;
559 }
Mehdi Alizadeh0de8c292020-01-21 17:27:26 -0800560
561 @Override
562 boolean injectHasAccessShortcutsPermission(int callingPid, int callingUid) {
563 return true;
564 }
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700565 }
566
567 protected class LauncherAppsTestable extends LauncherApps {
568 public LauncherAppsTestable(Context context, ILauncherApps service) {
569 super(context, service);
570 }
571 }
572
573 public static class ShortcutActivity extends Activity {
574 }
575
576 public static class ShortcutActivity2 extends Activity {
577 }
578
579 public static class ShortcutActivity3 extends Activity {
580 }
581
Makoto Onukia2241832016-07-06 13:28:37 -0700582 protected Looper mLooper;
583 protected Handler mHandler;
584
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700585 protected ServiceContext mServiceContext;
586 protected ClientContext mClientContext;
587
588 protected ShortcutServiceTestable mService;
589 protected ShortcutManagerTestable mManager;
590 protected ShortcutServiceInternal mInternal;
591
592 protected LauncherAppImplTestable mLauncherAppImpl;
593
594 // LauncherApps has per-instace state, so we need a differnt instance for each launcher.
595 protected final Map<Pair<Integer, String>, LauncherAppsTestable>
596 mLauncherAppsMap = new HashMap<>();
597 protected LauncherAppsTestable mLauncherApps; // Current one
598
599 protected File mInjectedFilePathRoot;
600
Makoto Onukib08790c2016-06-23 14:05:46 -0700601 protected boolean mSafeMode;
602
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700603 protected long mInjectedCurrentTimeMillis;
Makoto Onuki475c3652017-05-08 14:29:03 -0700604 protected long mDeepSleepTime; // Used to calculate "uptimeMillis".
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700605
606 protected boolean mInjectedIsLowRamDevice;
607
Makoto Onuki157b1622016-06-02 16:13:10 -0700608 protected Locale mInjectedLocale = Locale.ENGLISH;
609
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700610 protected int mInjectedCallingUid;
611 protected String mInjectedClientPackage;
612
613 protected Map<String, PackageInfo> mInjectedPackages;
614
615 protected Set<PackageWithUser> mUninstalledPackages;
Makoto Onuki82fb2eb2017-03-31 16:58:26 -0700616 protected Set<PackageWithUser> mDisabledPackages;
Makoto Onuki66e4a2b2017-01-23 11:37:45 -0800617 protected Set<PackageWithUser> mEphemeralPackages;
Makoto Onuki33663282016-08-22 16:19:04 -0700618 protected Set<String> mSystemPackages;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700619
620 protected PackageManager mMockPackageManager;
621 protected PackageManagerInternal mMockPackageManagerInternal;
622 protected UserManager mMockUserManager;
Varun Shah2546cef2019-01-11 15:50:54 -0800623 protected DevicePolicyManager mMockDevicePolicyManager;
Sunny Goyal145c8f82018-02-15 14:27:09 -0800624 protected UserManagerInternal mMockUserManagerInternal;
Makoto Onukiac042502016-05-20 16:39:42 -0700625 protected UsageStatsManagerInternal mMockUsageStatsManagerInternal;
Makoto Onukiea11db12016-06-24 15:17:44 -0700626 protected ActivityManagerInternal mMockActivityManagerInternal;
Wale Ogunwale6767eae2018-05-03 15:52:51 -0700627 protected ActivityTaskManagerInternal mMockActivityTaskManagerInternal;
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700628
629 protected static final String CALLING_PACKAGE_1 = "com.android.test.1";
630 protected static final int CALLING_UID_1 = 10001;
631
632 protected static final String CALLING_PACKAGE_2 = "com.android.test.2";
633 protected static final int CALLING_UID_2 = 10002;
634
635 protected static final String CALLING_PACKAGE_3 = "com.android.test.3";
636 protected static final int CALLING_UID_3 = 10003;
637
638 protected static final String CALLING_PACKAGE_4 = "com.android.test.4";
639 protected static final int CALLING_UID_4 = 10004;
640
641 protected static final String LAUNCHER_1 = "com.android.launcher.1";
642 protected static final int LAUNCHER_UID_1 = 10011;
643
644 protected static final String LAUNCHER_2 = "com.android.launcher.2";
645 protected static final int LAUNCHER_UID_2 = 10012;
646
647 protected static final String LAUNCHER_3 = "com.android.launcher.3";
648 protected static final int LAUNCHER_UID_3 = 10013;
649
650 protected static final String LAUNCHER_4 = "com.android.launcher.4";
651 protected static final int LAUNCHER_UID_4 = 10014;
652
653 protected static final int USER_0 = UserHandle.USER_SYSTEM;
654 protected static final int USER_10 = 10;
655 protected static final int USER_11 = 11;
Makoto Onukide3c16c2017-01-26 11:39:31 -0800656 protected static final int USER_P0 = 20; // profile of user 0 (MANAGED_PROFILE *not* set)
657 protected static final int USER_P1 = 21; // another profile of user 0 (MANAGED_PROFILE set)
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700658
659 protected static final UserHandle HANDLE_USER_0 = UserHandle.of(USER_0);
660 protected static final UserHandle HANDLE_USER_10 = UserHandle.of(USER_10);
661 protected static final UserHandle HANDLE_USER_11 = UserHandle.of(USER_11);
662 protected static final UserHandle HANDLE_USER_P0 = UserHandle.of(USER_P0);
Makoto Onukide3c16c2017-01-26 11:39:31 -0800663 protected static final UserHandle HANDLE_USER_P1 = UserHandle.of(USER_P1);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700664
665 protected static final UserInfo USER_INFO_0 = withProfileGroupId(
666 new UserInfo(USER_0, "user0",
Makoto Onuki2d895c32016-12-02 15:48:40 -0800667 UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY | UserInfo.FLAG_INITIALIZED), 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700668
669 protected static final UserInfo USER_INFO_10 =
670 new UserInfo(USER_10, "user10", UserInfo.FLAG_INITIALIZED);
671
672 protected static final UserInfo USER_INFO_11 =
673 new UserInfo(USER_11, "user11", UserInfo.FLAG_INITIALIZED);
674
Makoto Onukiaecbd032017-01-19 12:11:11 -0800675 /*
676 * Cheat: USER_P0 is a sub profile of USER_0, but it doesn't have the MANAGED_PROFILE flag set.
677 * Due to a change made to LauncherApps (b/34340531), work profile apps a no longer able
678 * to see the main profile, which would break tons of unit tests. We avoid it by not setting
679 * MANAGED_PROFILE for P0.
680 * We cover this negative case in CTS. (i.e. CTS has tests to make sure maanged profile
681 * can't access main profile's shortcuts.)
682 */
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700683 protected static final UserInfo USER_INFO_P0 = withProfileGroupId(
Makoto Onukiaecbd032017-01-19 12:11:11 -0800684 new UserInfo(USER_P0, "userP0", UserInfo.FLAG_INITIALIZED), 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700685
Makoto Onukide3c16c2017-01-26 11:39:31 -0800686 protected static final UserInfo USER_INFO_P1 = withProfileGroupId(
687 new UserInfo(USER_P1, "userP1",
688 UserInfo.FLAG_INITIALIZED | UserInfo.FLAG_MANAGED_PROFILE), 0);
689
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700690 protected BiPredicate<String, Integer> mDefaultLauncherChecker =
691 (callingPackage, userId) ->
692 LAUNCHER_1.equals(callingPackage) || LAUNCHER_2.equals(callingPackage)
693 || LAUNCHER_3.equals(callingPackage) || LAUNCHER_4.equals(callingPackage);
694
Makoto Onuki2d895c32016-12-02 15:48:40 -0800695 private final Map<Integer, ComponentName> mDefaultLauncher = new ArrayMap<>();
696
Makoto Onukib08790c2016-06-23 14:05:46 -0700697 protected BiPredicate<ComponentName, Integer> mMainActivityChecker =
698 (activity, userId) -> true;
699
700 protected BiFunction<String, Integer, ComponentName> mMainActivityFetcher =
701 (packageName, userId) -> new ComponentName(packageName, MAIN_ACTIVITY_CLASS);
702
Makoto Onuki2d895c32016-12-02 15:48:40 -0800703 protected BiFunction<String, Integer, ComponentName> mPinConfirmActivityFetcher =
704 (packageName, userId) -> new ComponentName(packageName, PIN_CONFIRM_ACTIVITY_CLASS);
705
Makoto Onukib08790c2016-06-23 14:05:46 -0700706 protected BiPredicate<ComponentName, Integer> mEnabledActivityChecker
707 = (activity, userId) -> true; // all activities are enabled.
708
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700709 protected static final long START_TIME = 1440000000101L;
710
711 protected static final long INTERVAL = 10000;
712
713 protected static final int MAX_SHORTCUTS = 10;
714
715 protected static final int MAX_UPDATES_PER_INTERVAL = 3;
716
717 protected static final int MAX_ICON_DIMENSION = 128;
718
719 protected static final int MAX_ICON_DIMENSION_LOWRAM = 32;
720
721 protected static final ShortcutQuery QUERY_ALL = new ShortcutQuery();
722
723 protected final ArrayList<String> mCallerPermissions = new ArrayList<>();
724
725 protected final HashMap<String, LinkedHashMap<ComponentName, Integer>> mActivityMetadataResId
726 = new HashMap<>();
727
Makoto Onukia2241832016-07-06 13:28:37 -0700728 protected final Map<Integer, UserInfo> mUserInfos = new HashMap<>();
729 protected final Map<Integer, Boolean> mRunningUsers = new HashMap<>();
730 protected final Map<Integer, Boolean> mUnlockedUsers = new HashMap<>();
731
Makoto Onuki0b9d1db2016-07-18 14:16:41 -0700732 protected static final String PACKAGE_SYSTEM_LAUNCHER = "com.android.systemlauncher";
733 protected static final String PACKAGE_SYSTEM_LAUNCHER_NAME = "systemlauncher_name";
734 protected static final int PACKAGE_SYSTEM_LAUNCHER_PRIORITY = 0;
735
736 protected static final String PACKAGE_FALLBACK_LAUNCHER = "com.android.settings";
737 protected static final String PACKAGE_FALLBACK_LAUNCHER_NAME = "fallback";
738 protected static final int PACKAGE_FALLBACK_LAUNCHER_PRIORITY = -999;
739
Makoto Onuki33663282016-08-22 16:19:04 -0700740 protected String mInjectedBuildFingerprint = "build1";
741
Makoto Onuki634cecb2017-10-13 17:10:48 -0700742 protected boolean mInjectCheckAccessShortcutsPermission = false;
743
Makoto Onuki7d0fa812018-02-21 11:24:43 -0800744 protected boolean mInjectHasUnlimitedShortcutsApiCallsPermission = false;
745
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700746 static {
747 QUERY_ALL.setQueryFlags(
Makoto Onuki4d6b87f2016-06-17 13:47:40 -0700748 ShortcutQuery.FLAG_GET_ALL_KINDS);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700749 }
750
751 @Override
752 protected void setUp() throws Exception {
753 super.setUp();
754
Makoto Onukia2241832016-07-06 13:28:37 -0700755 mLooper = Looper.getMainLooper();
756 mHandler = new Handler(mLooper);
757
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700758 mServiceContext = spy(new ServiceContext());
759 mClientContext = new ClientContext();
760
761 mMockPackageManager = mock(PackageManager.class);
762 mMockPackageManagerInternal = mock(PackageManagerInternal.class);
763 mMockUserManager = mock(UserManager.class);
Varun Shah2546cef2019-01-11 15:50:54 -0800764 mMockDevicePolicyManager = mock(DevicePolicyManager.class);
Sunny Goyal145c8f82018-02-15 14:27:09 -0800765 mMockUserManagerInternal = mock(UserManagerInternal.class);
Makoto Onukiac042502016-05-20 16:39:42 -0700766 mMockUsageStatsManagerInternal = mock(UsageStatsManagerInternal.class);
Makoto Onukiea11db12016-06-24 15:17:44 -0700767 mMockActivityManagerInternal = mock(ActivityManagerInternal.class);
Wale Ogunwale6767eae2018-05-03 15:52:51 -0700768 mMockActivityTaskManagerInternal = mock(ActivityTaskManagerInternal.class);
Makoto Onukiac042502016-05-20 16:39:42 -0700769
770 LocalServices.removeServiceForTest(PackageManagerInternal.class);
771 LocalServices.addService(PackageManagerInternal.class, mMockPackageManagerInternal);
772 LocalServices.removeServiceForTest(UsageStatsManagerInternal.class);
773 LocalServices.addService(UsageStatsManagerInternal.class, mMockUsageStatsManagerInternal);
Makoto Onukiea11db12016-06-24 15:17:44 -0700774 LocalServices.removeServiceForTest(ActivityManagerInternal.class);
775 LocalServices.addService(ActivityManagerInternal.class, mMockActivityManagerInternal);
Mehdi Alizadeh32774622018-11-05 17:32:01 -0800776 LocalServices.removeServiceForTest(ActivityTaskManagerInternal.class);
Wale Ogunwale6767eae2018-05-03 15:52:51 -0700777 LocalServices.addService(ActivityTaskManagerInternal.class, mMockActivityTaskManagerInternal);
Sunny Goyal145c8f82018-02-15 14:27:09 -0800778 LocalServices.removeServiceForTest(UserManagerInternal.class);
779 LocalServices.addService(UserManagerInternal.class, mMockUserManagerInternal);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700780
781 // Prepare injection values.
782
783 mInjectedCurrentTimeMillis = START_TIME;
784
785 mInjectedPackages = new HashMap<>();
786 addPackage(CALLING_PACKAGE_1, CALLING_UID_1, 1);
787 addPackage(CALLING_PACKAGE_2, CALLING_UID_2, 2);
788 addPackage(CALLING_PACKAGE_3, CALLING_UID_3, 3);
789 addPackage(CALLING_PACKAGE_4, CALLING_UID_4, 10);
790 addPackage(LAUNCHER_1, LAUNCHER_UID_1, 4);
791 addPackage(LAUNCHER_2, LAUNCHER_UID_2, 5);
792 addPackage(LAUNCHER_3, LAUNCHER_UID_3, 6);
793 addPackage(LAUNCHER_4, LAUNCHER_UID_4, 10);
794
795 // CALLING_PACKAGE_3 / LAUNCHER_3 are not backup target.
796 updatePackageInfo(CALLING_PACKAGE_3,
797 pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP);
798 updatePackageInfo(LAUNCHER_3,
799 pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP);
800
801 mUninstalledPackages = new HashSet<>();
Makoto Onuki82fb2eb2017-03-31 16:58:26 -0700802 mDisabledPackages = new HashSet<>();
Makoto Onuki33663282016-08-22 16:19:04 -0700803 mSystemPackages = new HashSet<>();
Makoto Onuki66e4a2b2017-01-23 11:37:45 -0800804 mEphemeralPackages = new HashSet<>();
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700805
806 mInjectedFilePathRoot = new File(getTestContext().getCacheDir(), "test-files");
807
808 deleteAllSavedFiles();
809
810 // Set up users.
Makoto Onukia2241832016-07-06 13:28:37 -0700811 mUserInfos.put(USER_0, USER_INFO_0);
812 mUserInfos.put(USER_10, USER_INFO_10);
813 mUserInfos.put(USER_11, USER_INFO_11);
814 mUserInfos.put(USER_P0, USER_INFO_P0);
Makoto Onukide3c16c2017-01-26 11:39:31 -0800815 mUserInfos.put(USER_P1, USER_INFO_P1);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700816
Sunny Goyal145c8f82018-02-15 14:27:09 -0800817 when(mMockUserManagerInternal.isUserUnlockingOrUnlocked(anyInt()))
818 .thenAnswer(inv -> {
Makoto Onuki9c850012016-07-26 15:50:50 -0700819 final int userId = (Integer) inv.getArguments()[0];
820 return b(mRunningUsers.get(userId)) && b(mUnlockedUsers.get(userId));
Sunny Goyal145c8f82018-02-15 14:27:09 -0800821 });
822 when(mMockUserManagerInternal.getProfileParentId(anyInt()))
823 .thenAnswer(inv -> {
Makoto Onuki2d895c32016-12-02 15:48:40 -0800824 final int userId = (Integer) inv.getArguments()[0];
825 final UserInfo ui = mUserInfos.get(userId);
826 assertNotNull(ui);
827 if (ui.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID) {
Sunny Goyal145c8f82018-02-15 14:27:09 -0800828 return userId;
Makoto Onuki2d895c32016-12-02 15:48:40 -0800829 }
830 final UserInfo parent = mUserInfos.get(ui.profileGroupId);
831 assertNotNull(parent);
Sunny Goyal145c8f82018-02-15 14:27:09 -0800832 return parent.id;
833 });
834
835 when(mMockUserManagerInternal.isProfileAccessible(anyInt(), anyInt(), anyString(),
836 anyBoolean())).thenAnswer(inv -> {
837 final int callingUserId = (Integer) inv.getArguments()[0];
838 final int targetUserId = (Integer) inv.getArguments()[1];
839 if (targetUserId == callingUserId) {
840 return true;
841 }
842 final UserInfo callingUserInfo = mUserInfos.get(callingUserId);
843 final UserInfo targetUserInfo = mUserInfos.get(targetUserId);
844 if (callingUserInfo == null || callingUserInfo.isManagedProfile()
845 || targetUserInfo == null || !targetUserInfo.isEnabled()) {
846 return false;
847 }
848 if (targetUserInfo.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID
849 && targetUserInfo.profileGroupId == callingUserInfo.profileGroupId) {
850 return true;
851 }
852 final boolean isExternal = (Boolean) inv.getArguments()[3];
853 if (!isExternal) {
854 return false;
855 }
856 throw new SecurityException(inv.getArguments()[2] + " for unrelated profile "
857 + targetUserId);
858 });
Makoto Onuki2d895c32016-12-02 15:48:40 -0800859
Makoto Onukie70b29e2018-04-03 16:44:39 -0700860 when(mMockUserManager.getUserInfo(anyInt())).thenAnswer(new AnswerWithSystemCheck<>(
861 inv -> mUserInfos.get((Integer) inv.getArguments()[0])));
Makoto Onuki33525d22016-08-03 15:45:24 -0700862 when(mMockActivityManagerInternal.getUidProcessState(anyInt())).thenReturn(
863 ActivityManager.PROCESS_STATE_CACHED_EMPTY);
864
Makoto Onuki9c850012016-07-26 15:50:50 -0700865 // User 0 and P0 are always running
Makoto Onukia2241832016-07-06 13:28:37 -0700866 mRunningUsers.put(USER_0, true);
867 mRunningUsers.put(USER_10, false);
868 mRunningUsers.put(USER_11, false);
Makoto Onuki9c850012016-07-26 15:50:50 -0700869 mRunningUsers.put(USER_P0, true);
Makoto Onukide3c16c2017-01-26 11:39:31 -0800870 mRunningUsers.put(USER_P1, true);
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700871
Makoto Onukia2241832016-07-06 13:28:37 -0700872 // Unlock all users by default.
873 mUnlockedUsers.put(USER_0, true);
874 mUnlockedUsers.put(USER_10, true);
875 mUnlockedUsers.put(USER_11, true);
876 mUnlockedUsers.put(USER_P0, true);
Makoto Onukide3c16c2017-01-26 11:39:31 -0800877 mUnlockedUsers.put(USER_P1, true);
Makoto Onukia2241832016-07-06 13:28:37 -0700878
879 // Set up resources
Makoto Onuki157b1622016-06-02 16:13:10 -0700880 setUpAppResources();
881
882 // Start the service.
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700883 initService();
884 setCaller(CALLING_PACKAGE_1);
Makoto Onuki248a0ef2016-11-03 15:59:01 -0700885
886 if (ENABLE_DUMP) {
887 Log.d(TAG, "setUp done");
888 }
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700889 }
890
Makoto Onukie70b29e2018-04-03 16:44:39 -0700891 /**
892 * Returns a boolean but also checks if the current UID is SYSTEM_UID.
893 */
894 protected class AnswerWithSystemCheck<T> implements Answer<T> {
895 private final Function<InvocationOnMock, T> mChecker;
896
897 public AnswerWithSystemCheck(Function<InvocationOnMock, T> checker) {
898 mChecker = checker;
899 }
900
901 @Override
902 public T answer(InvocationOnMock invocation) throws Throwable {
903 assertEquals("Must be called on SYSTEM UID.",
904 Process.SYSTEM_UID, mInjectedCallingUid);
905 return mChecker.apply(invocation);
906 }
907 }
908
Makoto Onuki9c850012016-07-26 15:50:50 -0700909 private static boolean b(Boolean value) {
910 return (value != null && value);
911 }
912
Makoto Onuki157b1622016-06-02 16:13:10 -0700913 protected void setUpAppResources() throws Exception {
914 setUpAppResources(/* offset = */ 0);
915 }
916
917 protected void setUpAppResources(int ressIdOffset) throws Exception {
918 // ressIdOffset is used to adjust resource IDs to emulate the case where an updated app
919 // has resource IDs changed.
920
921 doAnswer(pmInvocation -> {
922 assertEquals(Process.SYSTEM_UID, mInjectedCallingUid);
923
924 final String packageName = (String) pmInvocation.getArguments()[0];
925 final int userId = (Integer) pmInvocation.getArguments()[1];
926
927 final Resources res = mock(Resources.class);
928
929 doAnswer(resInvocation -> {
930 final int argResId = (Integer) resInvocation.getArguments()[0];
931
932 return "string-" + packageName + "-user:" + userId + "-res:" + argResId
933 + "/" + mInjectedLocale;
934 }).when(res).getString(anyInt());
935
936 doAnswer(resInvocation -> {
937 final int resId = (Integer) resInvocation.getArguments()[0];
938
939 // Always use the "string" resource type. The type doesn't matter during the test.
940 return packageName + ":string/r" + resId;
941 }).when(res).getResourceName(anyInt());
942
943 doAnswer(resInvocation -> {
944 final String argResName = (String) resInvocation.getArguments()[0];
945 final String argType = (String) resInvocation.getArguments()[1];
946 final String argPackageName = (String) resInvocation.getArguments()[2];
947
948 // See the above code. getResourceName() will just use "r" + res ID as the entry
949 // name.
950 String entryName = argResName;
951 if (entryName.contains("/")) {
952 entryName = ShortcutInfo.getResourceEntryName(entryName);
953 }
954 return Integer.parseInt(entryName.substring(1)) + ressIdOffset;
Makoto Onukid0010c52017-03-30 14:17:35 -0700955 }).when(res).getIdentifier(anyStringOrNull(), anyStringOrNull(), anyStringOrNull());
Makoto Onuki157b1622016-06-02 16:13:10 -0700956 return res;
957 }).when(mMockPackageManager).getResourcesForApplicationAsUser(anyString(), anyInt());
958 }
959
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700960 protected static UserInfo withProfileGroupId(UserInfo in, int groupId) {
961 in.profileGroupId = groupId;
962 return in;
963 }
964
965 @Override
966 protected void tearDown() throws Exception {
967 if (DUMP_IN_TEARDOWN) dumpsysOnLogcat("Teardown");
968
969 shutdownServices();
970
971 super.tearDown();
972 }
973
974 protected Context getTestContext() {
975 return getInstrumentation().getContext();
976 }
977
Mehdi Alizadeh39ed1b02018-11-26 19:24:11 -0800978 protected Context getClientContext() {
979 return mClientContext;
980 }
981
Makoto Onukia1d38b32016-06-10 15:32:26 -0700982 protected ShortcutManager getManager() {
983 return mManager;
984 }
985
Makoto Onuki51ab2b32016-06-02 11:03:51 -0700986 protected void deleteAllSavedFiles() {
987 // Empty the data directory.
988 if (mInjectedFilePathRoot.exists()) {
989 Assert.assertTrue("failed to delete dir",
990 FileUtils.deleteContents(mInjectedFilePathRoot));
991 }
992 mInjectedFilePathRoot.mkdirs();
993 }
994
995 /** (Re-) init the manager and the service. */
996 protected void initService() {
997 shutdownServices();
998
999 LocalServices.removeServiceForTest(ShortcutServiceInternal.class);
1000
1001 // Instantiate targets.
Makoto Onukia2241832016-07-06 13:28:37 -07001002 mService = new ShortcutServiceTestable(mServiceContext, mLooper);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001003 mManager = new ShortcutManagerTestable(mClientContext, mService);
1004
1005 mInternal = LocalServices.getService(ShortcutServiceInternal.class);
1006
1007 mLauncherAppImpl = new LauncherAppImplTestable(mServiceContext);
1008 mLauncherApps = null;
1009 mLauncherAppsMap.clear();
1010
Makoto Onuki157b1622016-06-02 16:13:10 -07001011 // Send boot sequence events.
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001012 mService.onBootPhase(SystemService.PHASE_LOCK_SETTINGS_READY);
Makoto Onuki157b1622016-06-02 16:13:10 -07001013
Makoto Onuki157b1622016-06-02 16:13:10 -07001014 mService.onBootPhase(SystemService.PHASE_BOOT_COMPLETED);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001015 }
1016
1017 protected void shutdownServices() {
1018 if (mService != null) {
1019 // Flush all the unsaved data from the previous instance.
1020 mService.saveDirtyInfo();
Makoto Onuki9e1f5592016-06-08 12:30:23 -07001021
1022 // Make sure everything is consistent.
1023 mService.verifyStates();
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001024 }
1025 LocalServices.removeServiceForTest(ShortcutServiceInternal.class);
1026
1027 mService = null;
1028 mManager = null;
1029 mInternal = null;
1030 mLauncherAppImpl = null;
1031 mLauncherApps = null;
1032 mLauncherAppsMap.clear();
1033 }
1034
Makoto Onukia2241832016-07-06 13:28:37 -07001035 protected void runOnHandler(Runnable r) {
1036 final long token = mServiceContext.injectClearCallingIdentity();
1037 try {
1038 r.run();
1039 } finally {
1040 mServiceContext.injectRestoreCallingIdentity(token);
1041 }
1042 }
1043
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001044 protected void addPackage(String packageName, int uid, int version) {
1045 addPackage(packageName, uid, version, packageName);
1046 }
1047
1048 protected Signature[] genSignatures(String... signatures) {
1049 final Signature[] sigs = new Signature[signatures.length];
1050 for (int i = 0; i < signatures.length; i++){
1051 sigs[i] = new Signature(signatures[i].getBytes());
1052 }
1053 return sigs;
1054 }
1055
1056 protected PackageInfo genPackage(String packageName, int uid, int version, String... signatures) {
1057 final PackageInfo pi = new PackageInfo();
1058 pi.packageName = packageName;
1059 pi.applicationInfo = new ApplicationInfo();
1060 pi.applicationInfo.uid = uid;
1061 pi.applicationInfo.flags = ApplicationInfo.FLAG_INSTALLED
1062 | ApplicationInfo.FLAG_ALLOW_BACKUP;
1063 pi.versionCode = version;
Patrick Baumannc2def582018-04-04 12:14:15 -07001064 pi.applicationInfo.setVersionCode(version);
Michal Karpinski528c3e52018-02-07 17:47:10 +00001065 pi.signatures = null;
Dan Cashman5c9f527e2018-04-03 16:42:23 -07001066 pi.signingInfo = new SigningInfo(
1067 new PackageParser.SigningDetails(
1068 genSignatures(signatures),
1069 PackageParser.SigningDetails.SignatureSchemeVersion.SIGNING_BLOCK_V3,
1070 null,
Dan Cashman5c9f527e2018-04-03 16:42:23 -07001071 null));
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001072 return pi;
1073 }
1074
1075 protected void addPackage(String packageName, int uid, int version, String... signatures) {
1076 mInjectedPackages.put(packageName, genPackage(packageName, uid, version, signatures));
1077 }
1078
1079 protected void updatePackageInfo(String packageName, Consumer<PackageInfo> c) {
1080 c.accept(mInjectedPackages.get(packageName));
1081 }
1082
1083 protected void updatePackageVersion(String packageName, int increment) {
1084 updatePackageInfo(packageName, pi -> {
1085 pi.versionCode += increment;
Patrick Baumannc2def582018-04-04 12:14:15 -07001086 pi.applicationInfo.setVersionCode(pi.applicationInfo.longVersionCode + increment);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001087 });
1088 }
1089
1090 protected void updatePackageLastUpdateTime(String packageName, long increment) {
1091 updatePackageInfo(packageName, pi -> {
1092 pi.lastUpdateTime += increment;
1093 });
1094 }
1095
Makoto Onuki085a05c2016-08-19 11:39:29 -07001096 protected void setPackageLastUpdateTime(String packageName, long value) {
1097 updatePackageInfo(packageName, pi -> {
1098 pi.lastUpdateTime = value;
1099 });
1100 }
1101
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001102 protected void uninstallPackage(int userId, String packageName) {
1103 if (ENABLE_DUMP) {
Makoto Onuki82fb2eb2017-03-31 16:58:26 -07001104 Log.v(TAG, "Uninstall package " + packageName + " / " + userId);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001105 }
1106 mUninstalledPackages.add(PackageWithUser.of(userId, packageName));
1107 }
1108
1109 protected void installPackage(int userId, String packageName) {
1110 if (ENABLE_DUMP) {
1111 Log.v(TAG, "Install package " + packageName + " / " + userId);
1112 }
1113 mUninstalledPackages.remove(PackageWithUser.of(userId, packageName));
1114 }
1115
Makoto Onuki82fb2eb2017-03-31 16:58:26 -07001116 protected void disablePackage(int userId, String packageName) {
1117 if (ENABLE_DUMP) {
1118 Log.v(TAG, "Disable package " + packageName + " / " + userId);
1119 }
1120 mDisabledPackages.add(PackageWithUser.of(userId, packageName));
1121 }
1122
1123 protected void enablePackage(int userId, String packageName) {
1124 if (ENABLE_DUMP) {
1125 Log.v(TAG, "Enable package " + packageName + " / " + userId);
1126 }
1127 mDisabledPackages.remove(PackageWithUser.of(userId, packageName));
1128 }
1129
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001130 PackageInfo getInjectedPackageInfo(String packageName, @UserIdInt int userId,
1131 boolean getSignatures) {
1132 final PackageInfo pi = mInjectedPackages.get(packageName);
1133 if (pi == null) return null;
1134
1135 final PackageInfo ret = new PackageInfo();
1136 ret.packageName = pi.packageName;
1137 ret.versionCode = pi.versionCode;
Dianne Hackborn3accca02013-09-20 09:32:11 -07001138 ret.versionCodeMajor = pi.versionCodeMajor;
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001139 ret.lastUpdateTime = pi.lastUpdateTime;
1140
1141 ret.applicationInfo = new ApplicationInfo(pi.applicationInfo);
1142 ret.applicationInfo.uid = UserHandle.getUid(userId, pi.applicationInfo.uid);
1143 ret.applicationInfo.packageName = pi.packageName;
1144
1145 if (mUninstalledPackages.contains(PackageWithUser.of(userId, packageName))) {
1146 ret.applicationInfo.flags &= ~ApplicationInfo.FLAG_INSTALLED;
1147 }
Makoto Onuki66e4a2b2017-01-23 11:37:45 -08001148 if (mEphemeralPackages.contains(PackageWithUser.of(userId, packageName))) {
Todd Kennedybe0b8892017-02-15 14:13:52 -08001149 ret.applicationInfo.privateFlags |= ApplicationInfo.PRIVATE_FLAG_INSTANT;
Makoto Onuki66e4a2b2017-01-23 11:37:45 -08001150 }
Makoto Onuki33663282016-08-22 16:19:04 -07001151 if (mSystemPackages.contains(packageName)) {
1152 ret.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
1153 }
Makoto Onuki82fb2eb2017-03-31 16:58:26 -07001154 ret.applicationInfo.enabled =
1155 !mDisabledPackages.contains(PackageWithUser.of(userId, packageName));
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001156
1157 if (getSignatures) {
Michal Karpinski528c3e52018-02-07 17:47:10 +00001158 ret.signatures = null;
Dan Cashman5c9f527e2018-04-03 16:42:23 -07001159 ret.signingInfo = pi.signingInfo;
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001160 }
1161
1162 return ret;
1163 }
1164
1165 protected void addApplicationInfo(PackageInfo pi, List<ApplicationInfo> list) {
1166 if (pi != null && pi.applicationInfo != null) {
1167 list.add(pi.applicationInfo);
1168 }
1169 }
1170
1171 protected List<ApplicationInfo> getInstalledApplications(int userId) {
1172 final ArrayList<ApplicationInfo> ret = new ArrayList<>();
1173
1174 addApplicationInfo(getInjectedPackageInfo(CALLING_PACKAGE_1, userId, false), ret);
1175 addApplicationInfo(getInjectedPackageInfo(CALLING_PACKAGE_2, userId, false), ret);
1176 addApplicationInfo(getInjectedPackageInfo(CALLING_PACKAGE_3, userId, false), ret);
1177 addApplicationInfo(getInjectedPackageInfo(CALLING_PACKAGE_4, userId, false), ret);
1178 addApplicationInfo(getInjectedPackageInfo(LAUNCHER_1, userId, false), ret);
1179 addApplicationInfo(getInjectedPackageInfo(LAUNCHER_2, userId, false), ret);
1180 addApplicationInfo(getInjectedPackageInfo(LAUNCHER_3, userId, false), ret);
1181 addApplicationInfo(getInjectedPackageInfo(LAUNCHER_4, userId, false), ret);
1182
1183 return ret;
1184 }
1185
Makoto Onuki6dd9fb72016-06-01 13:55:54 -07001186 private void addPackageInfo(PackageInfo pi, List<PackageInfo> list) {
1187 if (pi != null) {
1188 list.add(pi);
1189 }
1190 }
1191
Makoto Onukiee6b6e42016-06-29 17:34:02 -07001192 private List<PackageInfo> getInstalledPackagesWithUninstalled(int userId) {
Makoto Onuki6dd9fb72016-06-01 13:55:54 -07001193 final ArrayList<PackageInfo> ret = new ArrayList<>();
1194
1195 addPackageInfo(getInjectedPackageInfo(CALLING_PACKAGE_1, userId, false), ret);
1196 addPackageInfo(getInjectedPackageInfo(CALLING_PACKAGE_2, userId, false), ret);
1197 addPackageInfo(getInjectedPackageInfo(CALLING_PACKAGE_3, userId, false), ret);
1198 addPackageInfo(getInjectedPackageInfo(CALLING_PACKAGE_4, userId, false), ret);
1199 addPackageInfo(getInjectedPackageInfo(LAUNCHER_1, userId, false), ret);
1200 addPackageInfo(getInjectedPackageInfo(LAUNCHER_2, userId, false), ret);
1201 addPackageInfo(getInjectedPackageInfo(LAUNCHER_3, userId, false), ret);
1202 addPackageInfo(getInjectedPackageInfo(LAUNCHER_4, userId, false), ret);
1203
1204 return ret;
1205 }
1206
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001207 protected void addManifestShortcutResource(ComponentName activity, int resId) {
1208 final String packageName = activity.getPackageName();
1209 LinkedHashMap<ComponentName, Integer> map = mActivityMetadataResId.get(packageName);
1210 if (map == null) {
1211 map = new LinkedHashMap<>();
1212 mActivityMetadataResId.put(packageName, map);
1213 }
1214 map.put(activity, resId);
1215 }
1216
1217 protected PackageInfo injectGetActivitiesWithMetadata(String packageName, @UserIdInt int userId) {
1218 final PackageInfo ret = getInjectedPackageInfo(packageName, userId,
1219 /* getSignatures=*/ false);
1220
1221 final HashMap<ComponentName, Integer> activities = mActivityMetadataResId.get(packageName);
1222 if (activities != null) {
1223 final ArrayList<ActivityInfo> list = new ArrayList<>();
1224
1225 for (ComponentName cn : activities.keySet()) {
1226 ActivityInfo ai = new ActivityInfo();
1227 ai.packageName = cn.getPackageName();
1228 ai.name = cn.getClassName();
1229 ai.metaData = new Bundle();
1230 ai.metaData.putInt(ShortcutParser.METADATA_KEY, activities.get(cn));
Makoto Onukiee6b6e42016-06-29 17:34:02 -07001231 ai.applicationInfo = ret.applicationInfo;
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001232 list.add(ai);
1233 }
1234 ret.activities = list.toArray(new ActivityInfo[list.size()]);
1235 }
1236 return ret;
1237 }
1238
1239 protected XmlResourceParser injectXmlMetaData(ActivityInfo activityInfo, String key) {
1240 if (!ShortcutParser.METADATA_KEY.equals(key) || activityInfo.metaData == null) {
1241 return null;
1242 }
1243 final int resId = activityInfo.metaData.getInt(key);
1244 return getTestContext().getResources().getXml(resId);
1245 }
1246
1247 /** Replace the current calling package */
1248 protected void setCaller(String packageName, int userId) {
1249 mInjectedClientPackage = packageName;
1250 mInjectedCallingUid =
Daulet Zhanguzinfdb98192019-12-20 18:11:26 +00001251 Objects.requireNonNull(getInjectedPackageInfo(packageName, userId, false),
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001252 "Unknown package").applicationInfo.uid;
1253
1254 // Set up LauncherApps for this caller.
1255 final Pair<Integer, String> key = Pair.create(userId, packageName);
1256 if (!mLauncherAppsMap.containsKey(key)) {
1257 mLauncherAppsMap.put(key, new LauncherAppsTestable(mClientContext, mLauncherAppImpl));
1258 }
1259 mLauncherApps = mLauncherAppsMap.get(key);
1260 }
1261
1262 protected void setCaller(String packageName) {
1263 setCaller(packageName, UserHandle.USER_SYSTEM);
1264 }
1265
1266 protected String getCallingPackage() {
1267 return mInjectedClientPackage;
1268 }
1269
Makoto Onuki2d895c32016-12-02 15:48:40 -08001270 /**
Makoto Onuki7d0fa812018-02-21 11:24:43 -08001271 * This controls {@link ShortcutService#hasShortcutHostPermission}, but
Makoto Onuki2d895c32016-12-02 15:48:40 -08001272 * not {@link ShortcutService#getDefaultLauncher(int)}. To control the later, use
1273 * {@link #setDefaultLauncher(int, ComponentName)}.
1274 */
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001275 protected void setDefaultLauncherChecker(BiPredicate<String, Integer> p) {
1276 mDefaultLauncherChecker = p;
1277 }
1278
Makoto Onuki2d895c32016-12-02 15:48:40 -08001279 /**
1280 * Set the default launcher. This will update {@link #mDefaultLauncherChecker} set by
1281 * {@link #setDefaultLauncherChecker} too.
1282 */
1283 protected void setDefaultLauncher(int userId, ComponentName launcherActivity) {
1284 mDefaultLauncher.put(userId, launcherActivity);
1285
1286 final BiPredicate<String, Integer> oldChecker = mDefaultLauncherChecker;
1287 mDefaultLauncherChecker = (checkPackageName, checkUserId) -> {
1288 if ((checkUserId == userId) && (launcherActivity != null)) {
1289 return launcherActivity.getPackageName().equals(checkPackageName);
1290 }
1291 return oldChecker.test(checkPackageName, checkUserId);
1292 };
1293 }
1294
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001295 protected void runWithCaller(String packageName, int userId, Runnable r) {
1296 final String previousPackage = mInjectedClientPackage;
1297 final int previousUserId = UserHandle.getUserId(mInjectedCallingUid);
1298
1299 setCaller(packageName, userId);
1300
1301 r.run();
1302
1303 setCaller(previousPackage, previousUserId);
1304 }
1305
Makoto Onuki157b1622016-06-02 16:13:10 -07001306 protected void runWithSystemUid(Runnable r) {
1307 final int origUid = mInjectedCallingUid;
1308 mInjectedCallingUid = Process.SYSTEM_UID;
1309 r.run();
1310 mInjectedCallingUid = origUid;
1311 }
1312
1313 protected void lookupAndFillInResourceNames(ShortcutInfo si) {
1314 runWithSystemUid(() -> si.lookupAndFillInResourceNames(
1315 mService.injectGetResourcesForApplicationAsUser(si.getPackage(), si.getUserId())));
1316 }
1317
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001318 protected int getCallingUserId() {
1319 return UserHandle.getUserId(mInjectedCallingUid);
1320 }
1321
1322 protected UserHandle getCallingUser() {
1323 return UserHandle.of(getCallingUserId());
1324 }
1325
1326 /** For debugging */
1327 protected void dumpsysOnLogcat() {
1328 dumpsysOnLogcat("");
1329 }
1330
1331 protected void dumpsysOnLogcat(String message) {
1332 dumpsysOnLogcat(message, false);
1333 }
1334
1335 protected void dumpsysOnLogcat(String message, boolean force) {
1336 if (force || !ENABLE_DUMP) return;
1337
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001338 Log.v(TAG, "Dumping ShortcutService: " + message);
Makoto Onuki50a320e2017-05-31 14:38:42 -07001339 for (String line : dumpsys("-u").split("\n")) {
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001340 Log.v(TAG, line);
1341 }
1342 }
1343
Makoto Onuki76269922016-07-15 14:58:54 -07001344 protected String dumpCheckin() {
Makoto Onuki50a320e2017-05-31 14:38:42 -07001345 return dumpsys("--checkin");
Makoto Onuki76269922016-07-15 14:58:54 -07001346 }
1347
Makoto Onuki50a320e2017-05-31 14:38:42 -07001348 protected String dumpsys(String... args) {
Makoto Onuki76269922016-07-15 14:58:54 -07001349 final ArrayList<String> origPermissions = new ArrayList<>(mCallerPermissions);
1350 mCallerPermissions.add(android.Manifest.permission.DUMP);
1351 try {
1352 final ByteArrayOutputStream out = new ByteArrayOutputStream();
1353 final PrintWriter pw = new PrintWriter(out);
Makoto Onukic4361e32017-04-03 11:24:25 -07001354 mService.dumpNoCheck(/* fd */ null, pw, args);
Makoto Onuki76269922016-07-15 14:58:54 -07001355 pw.close();
1356
1357 return out.toString();
1358 } finally {
1359 mCallerPermissions.clear();
1360 mCallerPermissions.addAll(origPermissions);
1361 }
1362 }
1363
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001364 /**
1365 * For debugging, dump arbitrary file on logcat.
1366 */
1367 protected void dumpFileOnLogcat(String path) {
1368 dumpFileOnLogcat(path, "");
1369 }
1370
1371 protected void dumpFileOnLogcat(String path, String message) {
1372 if (!ENABLE_DUMP) return;
1373
1374 Log.v(TAG, "Dumping file: " + path + " " + message);
1375 final StringBuilder sb = new StringBuilder();
1376 try (BufferedReader br = new BufferedReader(new FileReader(path))) {
1377 String line;
1378 while ((line = br.readLine()) != null) {
1379 Log.v(TAG, line);
1380 }
1381 } catch (Exception e) {
1382 Log.e(TAG, "Couldn't read file", e);
1383 fail("Exception " + e);
1384 }
1385 }
1386
1387 /**
1388 * For debugging, dump the main state file on logcat.
1389 */
1390 protected void dumpBaseStateFile() {
1391 mService.saveDirtyInfo();
1392 dumpFileOnLogcat(mInjectedFilePathRoot.getAbsolutePath()
1393 + "/system/" + ShortcutService.FILENAME_BASE_STATE);
1394 }
1395
1396 /**
1397 * For debugging, dump per-user state file on logcat.
1398 */
1399 protected void dumpUserFile(int userId) {
1400 dumpUserFile(userId, "");
1401 }
1402
1403 protected void dumpUserFile(int userId, String message) {
1404 mService.saveDirtyInfo();
1405 dumpFileOnLogcat(mInjectedFilePathRoot.getAbsolutePath()
1406 + "/user-" + userId
1407 + "/" + ShortcutService.FILENAME_USER_PACKAGES, message);
1408 }
1409
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001410 /**
Makoto Onukia01f4f02016-12-15 15:58:41 -08001411 * Make a shortcut with an ID only.
1412 */
1413 protected ShortcutInfo makeShortcutIdOnly(String id) {
1414 return new ShortcutInfo.Builder(mClientContext, id).build();
1415 }
1416
1417 /**
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001418 * Make a shortcut with an ID.
1419 */
1420 protected ShortcutInfo makeShortcut(String id) {
1421 return makeShortcut(
1422 id, "Title-" + id, /* activity =*/ null, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001423 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001424 }
1425
Makoto Onukia01f4f02016-12-15 15:58:41 -08001426 @Deprecated // Title was renamed to short label.
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001427 protected ShortcutInfo makeShortcutWithTitle(String id, String title) {
1428 return makeShortcut(
1429 id, title, /* 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 }
1432
Makoto Onukia01f4f02016-12-15 15:58:41 -08001433 protected ShortcutInfo makeShortcutWithShortLabel(String id, String shortLabel) {
1434 return makeShortcut(
1435 id, shortLabel, /* activity =*/ null, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001436 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onukia01f4f02016-12-15 15:58:41 -08001437 }
1438
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001439 /**
1440 * Make a shortcut with an ID and timestamp.
1441 */
1442 protected ShortcutInfo makeShortcutWithTimestamp(String id, long timestamp) {
1443 final ShortcutInfo s = makeShortcut(
1444 id, "Title-" + id, /* activity =*/ null, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001445 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001446 s.setTimestamp(timestamp);
1447 return s;
1448 }
1449
1450 /**
1451 * Make a shortcut with an ID, a timestamp and an activity component
1452 */
1453 protected ShortcutInfo makeShortcutWithTimestampWithActivity(String id, long timestamp,
1454 ComponentName activity) {
1455 final ShortcutInfo s = makeShortcut(
1456 id, "Title-" + id, activity, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001457 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001458 s.setTimestamp(timestamp);
1459 return s;
1460 }
1461
1462 /**
1463 * Make a shortcut with an ID and icon.
1464 */
1465 protected ShortcutInfo makeShortcutWithIcon(String id, Icon icon) {
1466 return makeShortcut(
1467 id, "Title-" + id, /* activity =*/ null, icon,
Makoto Onuki99302b52017-03-29 12:42:26 -07001468 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001469 }
1470
1471 protected ShortcutInfo makePackageShortcut(String packageName, String id) {
1472 String origCaller = getCallingPackage();
1473
1474 setCaller(packageName);
1475 ShortcutInfo s = makeShortcut(
1476 id, "Title-" + id, /* activity =*/ null, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001477 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001478 setCaller(origCaller); // restore the caller
1479
1480 return s;
1481 }
1482
1483 /**
1484 * Make multiple shortcuts with IDs.
1485 */
1486 protected List<ShortcutInfo> makeShortcuts(String... ids) {
1487 final ArrayList<ShortcutInfo> ret = new ArrayList();
1488 for (String id : ids) {
1489 ret.add(makeShortcut(id));
1490 }
1491 return ret;
1492 }
1493
1494 protected ShortcutInfo.Builder makeShortcutBuilder() {
1495 return new ShortcutInfo.Builder(mClientContext);
1496 }
1497
1498 protected ShortcutInfo makeShortcutWithActivity(String id, ComponentName activity) {
1499 return makeShortcut(
1500 id, "Title-" + id, activity, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001501 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001502 }
1503
Makoto Onukib5a012f2016-06-21 11:13:53 -07001504 protected ShortcutInfo makeShortcutWithIntent(String id, Intent intent) {
1505 return makeShortcut(
1506 id, "Title-" + id, /* activity =*/ null, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001507 intent, /* rank =*/ 0);
Makoto Onukib5a012f2016-06-21 11:13:53 -07001508 }
1509
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001510 protected ShortcutInfo makeShortcutWithActivityAndTitle(String id, ComponentName activity,
1511 String title) {
1512 return makeShortcut(
1513 id, title, activity, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001514 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001515 }
1516
1517 protected ShortcutInfo makeShortcutWithActivityAndRank(String id, ComponentName activity,
1518 int rank) {
1519 return makeShortcut(
1520 id, "Title-" + id, activity, /* icon =*/ null,
Makoto Onuki99302b52017-03-29 12:42:26 -07001521 makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), rank);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001522 }
1523
1524 /**
1525 * Make a shortcut with details.
1526 */
1527 protected ShortcutInfo makeShortcut(String id, String title, ComponentName activity,
Makoto Onuki99302b52017-03-29 12:42:26 -07001528 Icon icon, Intent intent, int rank) {
Makoto Onukib5a012f2016-06-21 11:13:53 -07001529 final ShortcutInfo.Builder b = new ShortcutInfo.Builder(mClientContext, id)
Makoto Onuki255461f2017-01-10 11:47:25 -08001530 .setActivity(new ComponentName(mClientContext.getPackageName(), "main"))
Makoto Onukib5a012f2016-06-21 11:13:53 -07001531 .setShortLabel(title)
Makoto Onuki99302b52017-03-29 12:42:26 -07001532 .setRank(rank)
1533 .setIntent(intent);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001534 if (icon != null) {
1535 b.setIcon(icon);
1536 }
1537 if (activity != null) {
1538 b.setActivity(activity);
1539 }
1540 final ShortcutInfo s = b.build();
1541
1542 s.setTimestamp(mInjectedCurrentTimeMillis); // HACK
1543
1544 return s;
1545 }
1546
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001547 protected ShortcutInfo makeShortcutWithIntents(String id, Intent... intents) {
1548 return makeShortcut(
1549 id, "Title-" + id, /* activity =*/ null, /* icon =*/ null,
1550 intents, /* rank =*/ 0);
1551 }
1552
1553 /**
1554 * Make a shortcut with details.
1555 */
1556 protected ShortcutInfo makeShortcut(String id, String title, ComponentName activity,
1557 Icon icon, Intent[] intents, int rank) {
1558 final ShortcutInfo.Builder b = new ShortcutInfo.Builder(mClientContext, id)
Makoto Onuki255461f2017-01-10 11:47:25 -08001559 .setActivity(new ComponentName(mClientContext.getPackageName(), "main"))
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001560 .setShortLabel(title)
1561 .setRank(rank)
1562 .setIntents(intents);
1563 if (icon != null) {
1564 b.setIcon(icon);
1565 }
1566 if (activity != null) {
1567 b.setActivity(activity);
1568 }
1569 final ShortcutInfo s = b.build();
1570
1571 s.setTimestamp(mInjectedCurrentTimeMillis); // HACK
1572
1573 return s;
1574 }
1575
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001576 /**
Makoto Onukib5a012f2016-06-21 11:13:53 -07001577 * Make a shortcut with details.
1578 */
1579 protected ShortcutInfo makeShortcutWithExtras(String id, Intent intent,
1580 PersistableBundle extras) {
1581 final ShortcutInfo.Builder b = new ShortcutInfo.Builder(mClientContext, id)
Makoto Onuki255461f2017-01-10 11:47:25 -08001582 .setActivity(new ComponentName(mClientContext.getPackageName(), "main"))
Makoto Onukib5a012f2016-06-21 11:13:53 -07001583 .setShortLabel("title-" + id)
1584 .setExtras(extras)
1585 .setIntent(intent);
1586 final ShortcutInfo s = b.build();
1587
1588 s.setTimestamp(mInjectedCurrentTimeMillis); // HACK
1589
1590 return s;
1591 }
1592
1593 /**
Mehdi Alizadeha20781c2019-04-25 14:52:02 -07001594 * Make a shortcut with an ID and Category.
1595 */
1596 protected ShortcutInfo makeShortcutWithCategory(String id, Set<String> categories) {
1597 final ShortcutInfo.Builder b = new ShortcutInfo.Builder(mClientContext, id)
1598 .setActivity(new ComponentName(mClientContext.getPackageName(), "main"))
1599 .setShortLabel("title-" + id)
1600 .setIntent(makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class))
1601 .setCategories(categories);
1602 final ShortcutInfo s = b.build();
1603
1604 s.setTimestamp(mInjectedCurrentTimeMillis); // HACK
1605
1606 return s;
1607 }
1608
1609 /**
Mehdi Alizadehae808ff2020-01-21 13:39:53 -08001610 * Make a shortcut with an ID and a locus ID.
1611 */
1612 protected ShortcutInfo makeShortcutWithLocusId(String id, LocusId locusId) {
1613 final ShortcutInfo.Builder b = new ShortcutInfo.Builder(mClientContext, id)
1614 .setActivity(new ComponentName(mClientContext.getPackageName(), "main"))
1615 .setShortLabel("title-" + id)
1616 .setIntent(makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class))
1617 .setLocusId(locusId);
1618 final ShortcutInfo s = b.build();
1619
1620 s.setTimestamp(mInjectedCurrentTimeMillis); // HACK
1621
1622 return s;
1623 }
1624
1625 /**
Mehdi Alizadeh0de8c292020-01-21 17:27:26 -08001626 * Make a long lived shortcut with an ID.
1627 */
1628 protected ShortcutInfo makeLongLivedShortcut(String id) {
1629 final ShortcutInfo.Builder b = new ShortcutInfo.Builder(mClientContext, id)
1630 .setActivity(new ComponentName(mClientContext.getPackageName(), "main"))
1631 .setShortLabel("title-" + id)
1632 .setIntent(makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class))
1633 .setLongLived(true);
1634 final ShortcutInfo s = b.build();
1635
1636 s.setTimestamp(mInjectedCurrentTimeMillis); // HACK
1637
1638 return s;
1639 }
1640
1641 /**
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001642 * Make an intent.
1643 */
1644 protected Intent makeIntent(String action, Class<?> clazz, Object... bundleKeysAndValues) {
1645 final Intent intent = new Intent(action);
1646 intent.setComponent(makeComponent(clazz));
1647 intent.replaceExtras(makeBundle(bundleKeysAndValues));
1648 return intent;
1649 }
1650
1651 /**
Mehdi Alizadeh14242af2018-12-20 20:11:35 -08001652 * Make a Person.
1653 */
1654 protected Person makePerson(CharSequence name, String key, String uri) {
1655 final Person.Builder builder = new Person.Builder();
1656 return builder.setName(name).setKey(key).setUri(uri).build();
1657 }
1658
1659 /**
Mehdi Alizadehae808ff2020-01-21 13:39:53 -08001660 * Make a LocusId.
1661 */
1662 protected LocusId makeLocusId(String id) {
1663 return new LocusId(id);
1664 }
1665
1666 /**
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001667 * Make an component name, with the client context.
1668 */
1669 @NonNull
1670 protected ComponentName makeComponent(Class<?> clazz) {
1671 return new ComponentName(mClientContext, clazz);
1672 }
1673
1674 @NonNull
1675 protected ShortcutInfo findById(List<ShortcutInfo> list, String id) {
1676 for (ShortcutInfo s : list) {
1677 if (s.getId().equals(id)) {
1678 return s;
1679 }
1680 }
1681 fail("Shortcut with id " + id + " not found");
1682 return null;
1683 }
1684
1685 protected void assertSystem() {
1686 assertEquals("Caller must be system", Process.SYSTEM_UID, mInjectedCallingUid);
1687 }
1688
1689 protected void assertResetTimes(long expectedLastResetTime, long expectedNextResetTime) {
1690 assertEquals(expectedLastResetTime, mService.getLastResetTimeLocked());
1691 assertEquals(expectedNextResetTime, mService.getNextResetTimeLocked());
1692 }
1693
1694 public static List<ShortcutInfo> assertAllNotHaveIcon(
1695 List<ShortcutInfo> actualShortcuts) {
1696 for (ShortcutInfo s : actualShortcuts) {
1697 assertNull("ID " + s.getId(), s.getIcon());
1698 }
1699 return actualShortcuts;
1700 }
1701
1702 @NonNull
1703 protected List<ShortcutInfo> assertAllHaveFlags(@NonNull List<ShortcutInfo> actualShortcuts,
1704 int shortcutFlags) {
1705 for (ShortcutInfo s : actualShortcuts) {
1706 assertTrue("ID " + s.getId() + " doesn't have flags " + shortcutFlags,
1707 s.hasFlags(shortcutFlags));
1708 }
1709 return actualShortcuts;
1710 }
1711
1712 protected ShortcutInfo getPackageShortcut(String packageName, String shortcutId, int userId) {
1713 return mService.getPackageShortcutForTest(packageName, shortcutId, userId);
1714 }
1715
1716 protected void assertShortcutExists(String packageName, String shortcutId, int userId) {
1717 assertTrue(getPackageShortcut(packageName, shortcutId, userId) != null);
1718 }
1719
1720 protected void assertShortcutNotExists(String packageName, String shortcutId, int userId) {
1721 assertTrue(getPackageShortcut(packageName, shortcutId, userId) == null);
1722 }
1723
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001724 protected Intent[] launchShortcutAndGetIntentsInner(Runnable shortcutStarter,
1725 @NonNull String packageName, @NonNull String shortcutId, int userId) {
Wale Ogunwale6767eae2018-05-03 15:52:51 -07001726 reset(mMockActivityTaskManagerInternal);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001727 shortcutStarter.run();
1728
1729 final ArgumentCaptor<Intent[]> intentsCaptor = ArgumentCaptor.forClass(Intent[].class);
Wale Ogunwale6767eae2018-05-03 15:52:51 -07001730 verify(mMockActivityTaskManagerInternal).startActivitiesAsPackage(
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001731 eq(packageName),
Philip P. Moltmannee295092020-02-10 08:46:26 -08001732 isNull(),
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001733 eq(userId),
1734 intentsCaptor.capture(),
Makoto Onukid0010c52017-03-30 14:17:35 -07001735 anyOrNull(Bundle.class));
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001736 return intentsCaptor.getValue();
1737 }
1738
1739 protected Intent[] launchShortcutAndGetIntents(
1740 @NonNull String packageName, @NonNull String shortcutId, int userId) {
1741 return launchShortcutAndGetIntentsInner(
1742 () -> {
1743 mLauncherApps.startShortcut(packageName, shortcutId, null, null,
1744 UserHandle.of(userId));
1745 }, packageName, shortcutId, userId
1746 );
1747 }
1748
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001749 protected Intent launchShortcutAndGetIntent(
1750 @NonNull String packageName, @NonNull String shortcutId, int userId) {
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001751 final Intent[] intents = launchShortcutAndGetIntents(packageName, shortcutId, userId);
1752 assertEquals(1, intents.length);
1753 return intents[0];
1754 }
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001755
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001756 protected Intent[] launchShortcutAndGetIntents_withShortcutInfo(
1757 @NonNull String packageName, @NonNull String shortcutId, int userId) {
1758 return launchShortcutAndGetIntentsInner(
1759 () -> {
1760 mLauncherApps.startShortcut(
1761 getShortcutInfoAsLauncher(packageName, shortcutId, userId), null, null);
1762 }, packageName, shortcutId, userId
1763 );
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001764 }
1765
1766 protected Intent launchShortcutAndGetIntent_withShortcutInfo(
1767 @NonNull String packageName, @NonNull String shortcutId, int userId) {
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001768 final Intent[] intents = launchShortcutAndGetIntents_withShortcutInfo(
1769 packageName, shortcutId, userId);
1770 assertEquals(1, intents.length);
1771 return intents[0];
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001772 }
1773
1774 protected void assertShortcutLaunchable(@NonNull String packageName, @NonNull String shortcutId,
1775 int userId) {
1776 assertNotNull(launchShortcutAndGetIntent(packageName, shortcutId, userId));
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001777 }
1778
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001779 protected void assertShortcutNotLaunched(@NonNull String packageName,
1780 @NonNull String shortcutId, int userId) {
Wale Ogunwale6767eae2018-05-03 15:52:51 -07001781 reset(mMockActivityTaskManagerInternal);
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001782 try {
1783 mLauncherApps.startShortcut(packageName, shortcutId, null, null,
1784 UserHandle.of(userId));
1785 fail("ActivityNotFoundException was not thrown");
1786 } catch (ActivityNotFoundException expected) {
1787 }
1788 // This shouldn't have been called.
Wale Ogunwale6767eae2018-05-03 15:52:51 -07001789 verify(mMockActivityTaskManagerInternal, times(0)).startActivitiesAsPackage(
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001790 anyString(),
Philip P. Moltmannee295092020-02-10 08:46:26 -08001791 isNull(),
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001792 anyInt(),
1793 any(Intent[].class),
Makoto Onukid0010c52017-03-30 14:17:35 -07001794 anyOrNull(Bundle.class));
Makoto Onuki440a1ea2016-07-20 14:21:18 -07001795 }
1796
1797 protected void assertStartShortcutThrowsException(@NonNull String packageName,
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07001798 @NonNull String shortcutId, int userId, Class<?> expectedException) {
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07001799 Exception thrown = null;
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001800 try {
Makoto Onukid6880792016-06-29 13:37:43 -07001801 mLauncherApps.startShortcut(packageName, shortcutId, null, null,
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001802 UserHandle.of(userId));
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07001803 } catch (Exception e) {
1804 thrown = e;
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001805 }
Makoto Onuki83f6d2d2016-07-11 14:30:19 -07001806 assertNotNull("Exception was not thrown", thrown);
1807 assertEquals("Exception type different", expectedException, thrown.getClass());
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001808 }
1809
1810 protected void assertBitmapDirectories(int userId, String... expectedDirectories) {
1811 final Set<String> expected = hashSet(set(expectedDirectories));
1812
1813 final Set<String> actual = new HashSet<>();
1814
1815 final File[] files = mService.getUserBitmapFilePath(userId).listFiles();
1816 if (files != null) {
1817 for (File child : files) {
1818 if (child.isDirectory()) {
1819 actual.add(child.getName());
1820 }
1821 }
1822 }
1823
1824 assertEquals(expected, actual);
1825 }
1826
1827 protected void assertBitmapFiles(int userId, String packageName, String... expectedFiles) {
1828 final Set<String> expected = hashSet(set(expectedFiles));
1829
1830 final Set<String> actual = new HashSet<>();
1831
1832 final File[] files = new File(mService.getUserBitmapFilePath(userId), packageName)
1833 .listFiles();
1834 if (files != null) {
1835 for (File child : files) {
1836 if (child.isFile()) {
1837 actual.add(child.getName());
1838 }
1839 }
1840 }
1841
1842 assertEquals(expected, actual);
1843 }
1844
1845 protected String getBitmapFilename(int userId, String packageName, String shortcutId) {
1846 final ShortcutInfo si = mService.getPackageShortcutForTest(packageName, shortcutId, userId);
1847 if (si == null) {
1848 return null;
1849 }
Makoto Onuki475c3652017-05-08 14:29:03 -07001850 mService.waitForBitmapSavesForTest();
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001851 return new File(si.getBitmapPath()).getName();
1852 }
1853
Makoto Onuki475c3652017-05-08 14:29:03 -07001854 protected String getBitmapAbsPath(int userId, String packageName, String shortcutId) {
1855 final ShortcutInfo si = mService.getPackageShortcutForTest(packageName, shortcutId, userId);
1856 if (si == null) {
1857 return null;
1858 }
1859 mService.waitForBitmapSavesForTest();
1860 return new File(si.getBitmapPath()).getAbsolutePath();
1861 }
1862
Makoto Onukif3ba2e02016-07-12 09:18:50 -07001863 /**
1864 * @return all shortcuts stored internally for the caller. This reflects the *internal* view
1865 * of shortcuts, which may be different from what {@link #getCallerVisibleShortcuts} would
1866 * return, because getCallerVisibleShortcuts() will get shortcuts from the proper "front door"
1867 * which performs some extra checks, like {@link ShortcutPackage#onRestored}.
1868 */
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001869 protected List<ShortcutInfo> getCallerShortcuts() {
1870 final ShortcutPackage p = mService.getPackageShortcutForTest(
1871 getCallingPackage(), getCallingUserId());
1872 return p == null ? null : p.getAllShortcutsForTest();
1873 }
1874
Makoto Onukif3ba2e02016-07-12 09:18:50 -07001875 /**
Mehdi Alizadeh32774622018-11-05 17:32:01 -08001876 * @return all share targets stored internally for the caller.
1877 */
1878 protected List<ShareTargetInfo> getCallerShareTargets() {
1879 final ShortcutPackage p = mService.getPackageShortcutForTest(
1880 getCallingPackage(), getCallingUserId());
1881 return p == null ? null : p.getAllShareTargetsForTest();
1882 }
1883
1884 /**
Mehdi Alizadeha20781c2019-04-25 14:52:02 -07001885 * @return the number of shortcuts stored internally for the caller that can be used as a share
1886 * target in the ShareSheet. Such shortcuts have a matching category with at least one of the
1887 * defined ShareTargets from the app's Xml resource.
1888 */
1889 protected int getCallerSharingShortcutCount() {
1890 final ShortcutPackage p = mService.getPackageShortcutForTest(
1891 getCallingPackage(), getCallingUserId());
1892 return p == null ? 0 : p.getSharingShortcutCount();
1893 }
1894
1895 /**
Makoto Onukif3ba2e02016-07-12 09:18:50 -07001896 * @return all shortcuts owned by caller that are actually visible via ShortcutManager.
1897 * See also {@link #getCallerShortcuts}.
1898 */
1899 protected List<ShortcutInfo> getCallerVisibleShortcuts() {
1900 final ArrayList<ShortcutInfo> ret = new ArrayList<>();
1901 ret.addAll(mManager.getDynamicShortcuts());
1902 ret.addAll(mManager.getPinnedShortcuts());
1903 ret.addAll(mManager.getManifestShortcuts());
1904 return ret;
1905 }
1906
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001907 protected ShortcutInfo getCallerShortcut(String shortcutId) {
1908 return getPackageShortcut(getCallingPackage(), shortcutId, getCallingUserId());
1909 }
1910
1911 protected List<ShortcutInfo> getLauncherShortcuts(String launcher, int userId, int queryFlags) {
1912 final List<ShortcutInfo>[] ret = new List[1];
1913 runWithCaller(launcher, userId, () -> {
1914 final ShortcutQuery q = new ShortcutQuery();
1915 q.setQueryFlags(queryFlags);
1916 ret[0] = mLauncherApps.getShortcuts(q, UserHandle.of(userId));
1917 });
1918 return ret[0];
1919 }
1920
1921 protected List<ShortcutInfo> getLauncherPinnedShortcuts(String launcher, int userId) {
1922 return getLauncherShortcuts(launcher, userId, ShortcutQuery.FLAG_GET_PINNED);
1923 }
1924
Makoto Onukia01f4f02016-12-15 15:58:41 -08001925 protected List<ShortcutInfo> getShortcutAsLauncher(int targetUserId) {
1926 final ShortcutQuery q = new ShortcutQuery();
1927 q.setQueryFlags(ShortcutQuery.FLAG_MATCH_DYNAMIC | ShortcutQuery.FLAG_MATCH_DYNAMIC
1928 | ShortcutQuery.FLAG_MATCH_PINNED);
1929 return mLauncherApps.getShortcuts(q, UserHandle.of(targetUserId));
1930 }
1931
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001932 protected ShortcutInfo getShortcutInfoAsLauncher(String packageName, String shortcutId,
1933 int userId) {
1934 final List<ShortcutInfo> infoList =
1935 mLauncherApps.getShortcutInfo(packageName, list(shortcutId),
1936 UserHandle.of(userId));
1937 assertEquals("No shortcutInfo found (or too many of them)", 1, infoList.size());
1938 return infoList.get(0);
1939 }
1940
Makoto Onukiee6b6e42016-06-29 17:34:02 -07001941 protected Intent genPackageAddIntent(String packageName, int userId) {
1942 installPackage(userId, packageName);
1943
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001944 Intent i = new Intent(Intent.ACTION_PACKAGE_ADDED);
Makoto Onukiee6b6e42016-06-29 17:34:02 -07001945 i.setData(Uri.parse("package:" + packageName));
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001946 i.putExtra(Intent.EXTRA_USER_HANDLE, userId);
1947 return i;
1948 }
1949
1950 protected Intent genPackageDeleteIntent(String pakcageName, int userId) {
Makoto Onukiee6b6e42016-06-29 17:34:02 -07001951 uninstallPackage(userId, pakcageName);
1952
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001953 Intent i = new Intent(Intent.ACTION_PACKAGE_REMOVED);
1954 i.setData(Uri.parse("package:" + pakcageName));
1955 i.putExtra(Intent.EXTRA_USER_HANDLE, userId);
1956 return i;
1957 }
1958
1959 protected Intent genPackageUpdateIntent(String pakcageName, int userId) {
Makoto Onukiee6b6e42016-06-29 17:34:02 -07001960 installPackage(userId, pakcageName);
1961
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001962 Intent i = new Intent(Intent.ACTION_PACKAGE_ADDED);
1963 i.setData(Uri.parse("package:" + pakcageName));
1964 i.putExtra(Intent.EXTRA_USER_HANDLE, userId);
1965 i.putExtra(Intent.EXTRA_REPLACING, true);
1966 return i;
1967 }
1968
Makoto Onukib08790c2016-06-23 14:05:46 -07001969 protected Intent genPackageChangedIntent(String pakcageName, int userId) {
1970 Intent i = new Intent(Intent.ACTION_PACKAGE_CHANGED);
1971 i.setData(Uri.parse("package:" + pakcageName));
1972 i.putExtra(Intent.EXTRA_USER_HANDLE, userId);
1973 return i;
1974 }
1975
Makoto Onuki51ab2b32016-06-02 11:03:51 -07001976 protected Intent genPackageDataClear(String packageName, int userId) {
1977 Intent i = new Intent(Intent.ACTION_PACKAGE_DATA_CLEARED);
1978 i.setData(Uri.parse("package:" + packageName));
1979 i.putExtra(Intent.EXTRA_USER_HANDLE, userId);
1980 return i;
1981 }
1982
1983 protected void assertExistsAndShadow(ShortcutPackageItem spi) {
1984 assertNotNull(spi);
1985 assertTrue(spi.getPackageInfo().isShadow());
1986 }
1987
1988 protected File makeFile(File baseDirectory, String... paths) {
1989 File ret = baseDirectory;
1990
1991 for (String path : paths) {
1992 ret = new File(ret, path);
1993 }
1994
1995 return ret;
1996 }
1997
1998 protected boolean bitmapDirectoryExists(String packageName, int userId) {
Makoto Onuki475c3652017-05-08 14:29:03 -07001999 mService.waitForBitmapSavesForTest();
Makoto Onuki51ab2b32016-06-02 11:03:51 -07002000 final File path = new File(mService.getUserBitmapFilePath(userId), packageName);
2001 return path.isDirectory();
2002 }
2003 protected static ShortcutQuery buildQuery(long changedSince,
2004 String packageName, ComponentName componentName,
2005 /* @ShortcutQuery.QueryFlags */ int flags) {
Mehdi Alizadehae808ff2020-01-21 13:39:53 -08002006 return buildQuery(changedSince, packageName, null, null, componentName, flags);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07002007 }
2008
2009 protected static ShortcutQuery buildQuery(long changedSince,
Mehdi Alizadehae808ff2020-01-21 13:39:53 -08002010 String packageName, List<String> shortcutIds, List<LocusId> locusIds,
2011 ComponentName componentName, /* @ShortcutQuery.QueryFlags */ int flags) {
Makoto Onuki51ab2b32016-06-02 11:03:51 -07002012 final ShortcutQuery q = new ShortcutQuery();
2013 q.setChangedSince(changedSince);
2014 q.setPackage(packageName);
2015 q.setShortcutIds(shortcutIds);
Mehdi Alizadehae808ff2020-01-21 13:39:53 -08002016 q.setLocusIds(locusIds);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07002017 q.setActivity(componentName);
2018 q.setQueryFlags(flags);
2019 return q;
2020 }
2021
2022 protected static ShortcutQuery buildAllQuery(String packageName) {
2023 final ShortcutQuery q = new ShortcutQuery();
2024 q.setPackage(packageName);
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07002025 q.setQueryFlags(ShortcutQuery.FLAG_GET_ALL_KINDS);
Makoto Onuki51ab2b32016-06-02 11:03:51 -07002026 return q;
2027 }
2028
2029 protected static ShortcutQuery buildPinnedQuery(String packageName) {
2030 final ShortcutQuery q = new ShortcutQuery();
2031 q.setPackage(packageName);
2032 q.setQueryFlags(ShortcutQuery.FLAG_GET_PINNED);
2033 return q;
2034 }
2035
Makoto Onuki4d6b87f2016-06-17 13:47:40 -07002036 protected static ShortcutQuery buildQueryWithFlags(int queryFlags) {
2037 final ShortcutQuery q = new ShortcutQuery();
2038 q.setQueryFlags(queryFlags);
2039 return q;
2040 }
2041
Makoto Onuki51ab2b32016-06-02 11:03:51 -07002042 protected void backupAndRestore() {
2043 int prevUid = mInjectedCallingUid;
2044
2045 mInjectedCallingUid = Process.SYSTEM_UID; // Only system can call it.
2046
2047 dumpsysOnLogcat("Before backup");
2048
2049 final byte[] payload = mService.getBackupPayload(USER_0);
2050 if (ENABLE_DUMP) {
2051 final String xml = new String(payload);
2052 Log.v(TAG, "Backup payload:");
2053 for (String line : xml.split("\n")) {
2054 Log.v(TAG, line);
2055 }
2056 }
2057
2058 // Before doing anything else, uninstall all packages.
2059 for (int userId : list(USER_0, USER_P0)) {
2060 for (String pkg : list(CALLING_PACKAGE_1, CALLING_PACKAGE_2, CALLING_PACKAGE_3,
2061 LAUNCHER_1, LAUNCHER_2, LAUNCHER_3)) {
2062 uninstallPackage(userId, pkg);
2063 }
2064 }
2065
2066 shutdownServices();
2067
2068 deleteAllSavedFiles();
2069
2070 initService();
2071 mService.applyRestore(payload, USER_0);
2072
2073 // handleUnlockUser will perform the gone package check, but it shouldn't remove
2074 // shadow information.
2075 mService.handleUnlockUser(USER_0);
2076
2077 dumpsysOnLogcat("After restore");
2078
2079 mInjectedCallingUid = prevUid;
2080 }
2081
2082 protected void prepareCrossProfileDataSet() {
Makoto Onuki9c850012016-07-26 15:50:50 -07002083 mRunningUsers.put(USER_10, true); // this test needs user 10.
2084
Makoto Onuki51ab2b32016-06-02 11:03:51 -07002085 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2086 assertTrue(mManager.setDynamicShortcuts(list(
2087 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"),
2088 makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6"))));
2089 });
2090 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
2091 assertTrue(mManager.setDynamicShortcuts(list(
2092 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"),
2093 makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6"))));
2094 });
2095 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
2096 assertTrue(mManager.setDynamicShortcuts(list(
2097 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"),
2098 makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6"))));
2099 });
2100 runWithCaller(CALLING_PACKAGE_4, USER_0, () -> {
2101 assertTrue(mManager.setDynamicShortcuts(list()));
2102 });
2103 runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
2104 assertTrue(mManager.setDynamicShortcuts(list(
2105 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"),
2106 makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6"))));
2107 });
2108 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
2109 assertTrue(mManager.setDynamicShortcuts(list(
2110 makeShortcut("x1"), makeShortcut("x2"), makeShortcut("x3"),
2111 makeShortcut("x4"), makeShortcut("x5"), makeShortcut("x6"))));
2112 });
2113
2114 runWithCaller(LAUNCHER_1, USER_0, () -> {
2115 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s1"), HANDLE_USER_0);
2116 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s1", "s2"), HANDLE_USER_0);
2117 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("s1", "s2", "s3"), HANDLE_USER_0);
2118
2119 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s1", "s4"), HANDLE_USER_P0);
2120 });
2121 runWithCaller(LAUNCHER_2, USER_0, () -> {
2122 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s2"), HANDLE_USER_0);
2123 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s2", "s3"), HANDLE_USER_0);
2124 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("s2", "s3", "s4"), HANDLE_USER_0);
2125
2126 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s2", "s5"), HANDLE_USER_P0);
2127 });
Makoto Onukif3ba2e02016-07-12 09:18:50 -07002128
2129 // Note LAUNCHER_3 has allowBackup=false.
Makoto Onuki51ab2b32016-06-02 11:03:51 -07002130 runWithCaller(LAUNCHER_3, USER_0, () -> {
2131 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s3"), HANDLE_USER_0);
2132 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s3", "s4"), HANDLE_USER_0);
2133 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("s3", "s4", "s5"), HANDLE_USER_0);
2134
2135 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s3", "s6"), HANDLE_USER_P0);
2136 });
2137 runWithCaller(LAUNCHER_4, USER_0, () -> {
2138 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list(), HANDLE_USER_0);
2139 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list(), HANDLE_USER_0);
2140 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list(), HANDLE_USER_0);
2141 mLauncherApps.pinShortcuts(CALLING_PACKAGE_4, list(), HANDLE_USER_0);
2142 });
2143
2144 // Launcher on a managed profile is referring ot user 0!
2145 runWithCaller(LAUNCHER_1, USER_P0, () -> {
2146 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s3", "s4"), HANDLE_USER_0);
2147 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s3", "s4", "s5"), HANDLE_USER_0);
2148 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("s3", "s4", "s5", "s6"),
2149 HANDLE_USER_0);
2150
2151 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s4", "s1"), HANDLE_USER_P0);
2152 });
2153 runWithCaller(LAUNCHER_1, USER_10, () -> {
2154 mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("x4", "x5"), HANDLE_USER_10);
2155 mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("x4", "x5", "x6"), HANDLE_USER_10);
2156 mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("x4", "x5", "x6", "x1"),
2157 HANDLE_USER_10);
2158 });
2159
2160 // Then remove some dynamic shortcuts.
2161 runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
2162 assertTrue(mManager.setDynamicShortcuts(list(
2163 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2164 });
2165 runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
2166 assertTrue(mManager.setDynamicShortcuts(list(
2167 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2168 });
2169 runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
2170 assertTrue(mManager.setDynamicShortcuts(list(
2171 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2172 });
2173 runWithCaller(CALLING_PACKAGE_4, USER_0, () -> {
2174 assertTrue(mManager.setDynamicShortcuts(list()));
2175 });
2176 runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
2177 assertTrue(mManager.setDynamicShortcuts(list(
2178 makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
2179 });
2180 runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
2181 assertTrue(mManager.setDynamicShortcuts(list(
2182 makeShortcut("x1"), makeShortcut("x2"), makeShortcut("x3"))));
2183 });
2184 }
Makoto Onukib5a012f2016-06-21 11:13:53 -07002185
2186 public static List<ShortcutInfo> assertAllHaveIconResId(
2187 List<ShortcutInfo> actualShortcuts) {
2188 for (ShortcutInfo s : actualShortcuts) {
2189 assertTrue("ID " + s.getId() + " not have icon res ID", s.hasIconResource());
2190 assertFalse("ID " + s.getId() + " shouldn't have icon FD", s.hasIconFile());
2191 }
2192 return actualShortcuts;
2193 }
2194
2195 public static List<ShortcutInfo> assertAllHaveIconFile(
2196 List<ShortcutInfo> actualShortcuts) {
2197 for (ShortcutInfo s : actualShortcuts) {
2198 assertFalse("ID " + s.getId() + " shouldn't have icon res ID", s.hasIconResource());
2199 assertTrue("ID " + s.getId() + " not have icon FD", s.hasIconFile());
2200 }
2201 return actualShortcuts;
2202 }
2203
2204 public static List<ShortcutInfo> assertAllHaveIcon(
2205 List<ShortcutInfo> actualShortcuts) {
2206 for (ShortcutInfo s : actualShortcuts) {
Makoto Onukia01f4f02016-12-15 15:58:41 -08002207 assertTrue("ID " + s.getId() + " has no icon ",
2208 s.hasIconFile() || s.hasIconResource() || s.getIcon() != null);
Makoto Onukib5a012f2016-06-21 11:13:53 -07002209 }
2210 return actualShortcuts;
2211 }
2212
2213 public static List<ShortcutInfo> assertAllStringsResolved(
2214 List<ShortcutInfo> actualShortcuts) {
2215 for (ShortcutInfo s : actualShortcuts) {
2216 assertTrue("ID " + s.getId(), s.hasStringResourcesResolved());
2217 }
2218 return actualShortcuts;
2219 }
Makoto Onuki76269922016-07-15 14:58:54 -07002220
2221 public String readTestAsset(String assetPath) throws IOException {
2222 final StringBuilder sb = new StringBuilder();
2223 try (BufferedReader br = new BufferedReader(
2224 new InputStreamReader(
2225 getTestContext().getResources().getAssets().open(assetPath)))) {
2226 String line;
2227 while ((line = br.readLine()) != null) {
2228 sb.append(line);
2229 sb.append(System.lineSeparator());
2230 }
2231 }
2232 return sb.toString();
2233 }
Makoto Onuki0b9d1db2016-07-18 14:16:41 -07002234
2235 protected void prepareGetHomeActivitiesAsUser(ComponentName preferred,
2236 List<ResolveInfo> candidates, int userId) {
2237 doAnswer(inv -> {
2238 ((List) inv.getArguments()[0]).addAll(candidates);
2239 return preferred;
2240 }).when(mMockPackageManagerInternal).getHomeActivitiesAsUser(any(List.class), eq(userId));
2241 }
2242
2243 protected static ComponentName cn(String packageName, String name) {
2244 return new ComponentName(packageName, name);
2245 }
2246
2247 protected static ResolveInfo ri(String packageName, String name, boolean isSystem, int priority) {
2248 final ResolveInfo ri = new ResolveInfo();
2249 ri.activityInfo = new ActivityInfo();
2250 ri.activityInfo.applicationInfo = new ApplicationInfo();
2251
2252 ri.activityInfo.packageName = packageName;
2253 ri.activityInfo.name = name;
2254 if (isSystem) {
2255 ri.activityInfo.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
2256 }
2257 ri.priority = priority;
2258 return ri;
2259 }
2260
2261 protected static ResolveInfo getSystemLauncher() {
2262 return ri(PACKAGE_SYSTEM_LAUNCHER, PACKAGE_SYSTEM_LAUNCHER_NAME, true,
2263 PACKAGE_SYSTEM_LAUNCHER_PRIORITY);
2264 }
2265
2266 protected static ResolveInfo getFallbackLauncher() {
2267 return ri(PACKAGE_FALLBACK_LAUNCHER, PACKAGE_FALLBACK_LAUNCHER_NAME, true,
2268 PACKAGE_FALLBACK_LAUNCHER_PRIORITY);
2269 }
Makoto Onukia01f4f02016-12-15 15:58:41 -08002270
Tony Maked6ef622017-12-07 16:36:16 +00002271 protected void makeUidForeground(int uid) {
Makoto Onukia01f4f02016-12-15 15:58:41 -08002272 try {
2273 mService.mUidObserver.onUidStateChanged(
Hui Yu26969322019-08-21 14:56:35 -07002274 uid, ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE, 0,
2275 ActivityManager.PROCESS_CAPABILITY_NONE);
Tony Maked6ef622017-12-07 16:36:16 +00002276 } catch (RemoteException e) {
2277 e.rethrowAsRuntimeException();
2278 }
2279 }
2280
2281 protected void makeCallerForeground() {
2282 makeUidForeground(mInjectedCallingUid);
2283 }
2284
2285 protected void makeUidBackground(int uid) {
2286 try {
2287 mService.mUidObserver.onUidStateChanged(
Hui Yu26969322019-08-21 14:56:35 -07002288 uid, ActivityManager.PROCESS_STATE_TOP_SLEEPING, 0,
2289 ActivityManager.PROCESS_CAPABILITY_NONE);
Makoto Onukia01f4f02016-12-15 15:58:41 -08002290 } catch (RemoteException e) {
2291 e.rethrowAsRuntimeException();
2292 }
2293 }
2294
2295 protected void makeCallerBackground() {
Tony Maked6ef622017-12-07 16:36:16 +00002296 makeUidBackground(mInjectedCallingUid);
Makoto Onukia01f4f02016-12-15 15:58:41 -08002297 }
2298
2299 protected void publishManifestShortcutsAsCaller(int resId) {
2300 addManifestShortcutResource(
2301 new ComponentName(getCallingPackage(), ShortcutActivity.class.getName()),
2302 resId);
2303 updatePackageVersion(getCallingPackage(), 1);
2304 mService.mPackageMonitor.onReceive(getTestContext(),
2305 genPackageAddIntent(getCallingPackage(), getCallingUserId()));
2306 }
Makoto Onuki50a320e2017-05-31 14:38:42 -07002307
2308 protected void assertFileNotExists(String path) {
2309 final File f = new File(mInjectedFilePathRoot, path);
2310 assertFalse("File shouldn't exist: " + f.getAbsolutePath(), f.exists());
2311 }
2312
2313 protected void assertFileExistsWithContent(String path) {
2314 final File f = new File(mInjectedFilePathRoot, path);
2315 assertTrue("File should exist: " + f.getAbsolutePath(), f.exists());
2316 assertTrue("File should be larger than 0b: " + f.getAbsolutePath(), f.length() > 0);
2317 }
Makoto Onuki51ab2b32016-06-02 11:03:51 -07002318}