1 package com.android.server.pm;
2 
3 import static com.google.common.truth.Truth.assertThat;
4 
5 import static org.mockito.ArgumentMatchers.any;
6 import static org.mockito.ArgumentMatchers.anyBoolean;
7 import static org.mockito.ArgumentMatchers.anyInt;
8 import static org.mockito.ArgumentMatchers.anyLong;
9 import static org.mockito.ArgumentMatchers.anyString;
10 import static org.mockito.ArgumentMatchers.eq;
11 import static org.mockito.ArgumentMatchers.nullable;
12 import static org.mockito.Mockito.doAnswer;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.never;
16 import static org.mockito.Mockito.verify;
17 import static org.mockito.Mockito.when;
18 import static org.testng.Assert.assertThrows;
19 
20 import android.Manifest;
21 import android.app.ActivityManager;
22 import android.app.ActivityManagerInternal;
23 import android.app.ActivityOptions;
24 import android.app.AppOpsManager;
25 import android.app.IApplicationThread;
26 import android.app.admin.DevicePolicyManagerInternal;
27 import android.content.AttributionSourceState;
28 import android.content.ComponentName;
29 import android.content.Context;
30 import android.content.Intent;
31 import android.content.PermissionChecker;
32 import android.content.pm.ActivityInfo;
33 import android.content.pm.ApplicationInfo;
34 import android.content.pm.CrossProfileAppsInternal;
35 import android.content.pm.IPackageManager;
36 import android.content.pm.PackageInfo;
37 import android.content.pm.PackageManager;
38 import android.content.pm.PackageManagerInternal;
39 import android.content.pm.PermissionInfo;
40 import android.content.pm.ResolveInfo;
41 import android.os.Binder;
42 import android.os.Bundle;
43 import android.os.IBinder;
44 import android.os.UserHandle;
45 import android.os.UserManager;
46 import android.permission.PermissionCheckerManager;
47 import android.permission.PermissionManager;
48 import android.platform.test.annotations.Presubmit;
49 import android.util.SparseArray;
50 
51 import com.android.internal.util.FunctionalUtils.ThrowingRunnable;
52 import com.android.internal.util.FunctionalUtils.ThrowingSupplier;
53 import com.android.server.LocalServices;
54 import com.android.server.pm.permission.PermissionManagerService;
55 import com.android.server.wm.ActivityTaskManagerInternal;
56 
57 import org.junit.Before;
58 import org.junit.Test;
59 import org.junit.runner.RunWith;
60 import org.mockito.Mock;
61 import org.mockito.junit.MockitoJUnitRunner;
62 
63 import java.util.ArrayList;
64 import java.util.Collections;
65 import java.util.List;
66 
67 /**
68  * Build/Install/Run:
69  * atest FrameworksServicesTests:com.android.server.pm.CrossProfileAppsServiceImplTest
70  */
71 @Presubmit
72 @RunWith(MockitoJUnitRunner.class)
73 public class CrossProfileAppsServiceImplTest {
74     private static final String PACKAGE_ONE = "com.one";
75     private static final String FEATURE_ID = "feature.one";
76     private static final int PACKAGE_ONE_UID = 1111;
77     private static final ComponentName ACTIVITY_COMPONENT =
78             new ComponentName("com.one", "test");
79 
80     private static final String PACKAGE_TWO = "com.two";
81     private static final int PACKAGE_TWO_UID = 2222;
82 
83     private static final int PRIMARY_USER = 0;
84     private static final int PROFILE_OF_PRIMARY_USER = 10;
85     private static final int SECONDARY_USER = 11;
86 
87     @Mock
88     private Context mContext;
89     @Mock
90     private UserManager mUserManager;
91     @Mock
92     private PackageManager mPackageManager;
93     @Mock
94     private PackageManagerInternal mPackageManagerInternal;
95     @Mock
96     private AppOpsManager mAppOpsManager;
97     @Mock
98     private ActivityManagerInternal mActivityManagerInternal;
99     @Mock
100     private ActivityTaskManagerInternal mActivityTaskManagerInternal;
101     @Mock
102     private IPackageManager mIPackageManager;
103     @Mock
104     private DevicePolicyManagerInternal mDevicePolicyManagerInternal;
105 
106     private TestInjector mTestInjector;
107     private ActivityInfo mActivityInfo;
108     private CrossProfileAppsServiceImpl mCrossProfileAppsServiceImpl;
109     private IApplicationThread mIApplicationThread;
110 
111     private SparseArray<Boolean> mUserEnabled = new SparseArray<>();
112 
113     @Before
initCrossProfileAppsServiceImpl()114     public void initCrossProfileAppsServiceImpl() {
115         mTestInjector = new TestInjector();
116         LocalServices.removeServiceForTest(CrossProfileAppsInternal.class);
117         mCrossProfileAppsServiceImpl = new CrossProfileAppsServiceImpl(mContext, mTestInjector);
118         when(mContext.getPackageManager()).thenReturn(mPackageManager);
119     }
120 
121     @Before
setupEnabledProfiles()122     public void setupEnabledProfiles() {
123         mUserEnabled.put(PRIMARY_USER, true);
124         mUserEnabled.put(PROFILE_OF_PRIMARY_USER, true);
125         mUserEnabled.put(SECONDARY_USER, true);
126 
127         when(mUserManager.getEnabledProfileIds(anyInt())).thenAnswer(
128                 invocation -> {
129                     List<Integer> users = new ArrayList<>();
130                     final int targetUser = invocation.getArgument(0);
131                     users.add(targetUser);
132 
133                     int profileUserId = -1;
134                     if (targetUser == PRIMARY_USER) {
135                         profileUserId = PROFILE_OF_PRIMARY_USER;
136                     } else if (targetUser == PROFILE_OF_PRIMARY_USER) {
137                         profileUserId = PRIMARY_USER;
138                     }
139 
140                     if (profileUserId != -1 && mUserEnabled.get(profileUserId)) {
141                         users.add(profileUserId);
142                     }
143                     return users.stream().mapToInt(i -> i).toArray();
144                 });
145     }
146 
147     @Before
setupCaller()148     public void setupCaller() {
149         mTestInjector.setCallingUid(PACKAGE_ONE_UID);
150         mTestInjector.setCallingUserId(PRIMARY_USER);
151     }
152 
153     @Before
setupPackage()154     public void setupPackage() throws Exception {
155         // PACKAGE_ONE are installed in all users.
156         mockAppsInstalled(PACKAGE_ONE, PRIMARY_USER, true);
157         mockAppsInstalled(PACKAGE_ONE, PROFILE_OF_PRIMARY_USER, true);
158         mockAppsInstalled(PACKAGE_ONE, SECONDARY_USER, true);
159 
160         // Packages are resolved to their corresponding UID.
161         doAnswer(invocation -> {
162             final int uid = invocation.getArgument(0);
163             final String packageName = invocation.getArgument(1);
164             if (uid == PACKAGE_ONE_UID && PACKAGE_ONE.equals(packageName)) {
165                 return null;
166             } else if (uid ==PACKAGE_TWO_UID && PACKAGE_TWO.equals(packageName)) {
167                 return null;
168             }
169             throw new SecurityException("Not matching");
170         }).when(mAppOpsManager).checkPackage(anyInt(), anyString());
171 
172         // The intent is resolved to the ACTIVITY_COMPONENT.
173         mockActivityLaunchIntentResolvedTo(ACTIVITY_COMPONENT);
174     }
175 
176     @Test
getTargetUserProfiles_fromPrimaryUser_installed()177     public void getTargetUserProfiles_fromPrimaryUser_installed() throws Exception {
178         List<UserHandle> targetProfiles =
179                 mCrossProfileAppsServiceImpl.getTargetUserProfiles(PACKAGE_ONE);
180         assertThat(targetProfiles).containsExactly(UserHandle.of(PROFILE_OF_PRIMARY_USER));
181     }
182 
183     @Test
getTargetUserProfiles_fromPrimaryUser_notInstalled()184     public void getTargetUserProfiles_fromPrimaryUser_notInstalled() throws Exception {
185         mockAppsInstalled(PACKAGE_ONE, PROFILE_OF_PRIMARY_USER, false);
186 
187         List<UserHandle> targetProfiles =
188                 mCrossProfileAppsServiceImpl.getTargetUserProfiles(PACKAGE_ONE);
189         assertThat(targetProfiles).isEmpty();
190     }
191 
192     @Test
getTargetUserProfiles_fromPrimaryUser_userNotEnabled()193     public void getTargetUserProfiles_fromPrimaryUser_userNotEnabled() throws Exception {
194         mUserEnabled.put(PROFILE_OF_PRIMARY_USER, false);
195 
196         List<UserHandle> targetProfiles =
197                 mCrossProfileAppsServiceImpl.getTargetUserProfiles(PACKAGE_ONE);
198         assertThat(targetProfiles).isEmpty();
199     }
200 
201     @Test
getTargetUserProfiles_fromSecondaryUser()202     public void getTargetUserProfiles_fromSecondaryUser() throws Exception {
203         mTestInjector.setCallingUserId(SECONDARY_USER);
204 
205         List<UserHandle> targetProfiles =
206                 mCrossProfileAppsServiceImpl.getTargetUserProfiles(PACKAGE_ONE);
207         assertThat(targetProfiles).isEmpty();
208     }
209 
210     @Test
getTargetUserProfiles_fromProfile_installed()211     public void getTargetUserProfiles_fromProfile_installed() throws Exception {
212         mTestInjector.setCallingUserId(PROFILE_OF_PRIMARY_USER);
213 
214         List<UserHandle> targetProfiles =
215                 mCrossProfileAppsServiceImpl.getTargetUserProfiles(PACKAGE_ONE);
216         assertThat(targetProfiles).containsExactly(UserHandle.of(PRIMARY_USER));
217     }
218 
219     @Test
getTargetUserProfiles_fromProfile_notInstalled()220     public void getTargetUserProfiles_fromProfile_notInstalled() throws Exception {
221         mTestInjector.setCallingUserId(PROFILE_OF_PRIMARY_USER);
222         mockAppsInstalled(PACKAGE_ONE, PRIMARY_USER, false);
223 
224         List<UserHandle> targetProfiles =
225                 mCrossProfileAppsServiceImpl.getTargetUserProfiles(PACKAGE_ONE);
226         assertThat(targetProfiles).isEmpty();
227     }
228 
229     @Test(expected = SecurityException.class)
getTargetUserProfiles_fakeCaller()230     public void getTargetUserProfiles_fakeCaller() throws Exception {
231         mCrossProfileAppsServiceImpl.getTargetUserProfiles(PACKAGE_TWO);
232     }
233 
234     @Test
startActivityAsUser_currentUser()235     public void startActivityAsUser_currentUser() throws Exception {
236         assertThrows(
237                 SecurityException.class,
238                 () ->
239                         mCrossProfileAppsServiceImpl.startActivityAsUser(
240                                 mIApplicationThread,
241                                 PACKAGE_ONE,
242                                 FEATURE_ID,
243                                 ACTIVITY_COMPONENT,
244                                 UserHandle.of(PRIMARY_USER).getIdentifier(),
245                                 true,
246                                 /* targetTask */ null,
247                                 /* options */ null));
248 
249         verify(mActivityTaskManagerInternal, never())
250                 .startActivityAsUser(
251                         nullable(IApplicationThread.class),
252                         anyString(),
253                         nullable(String.class),
254                         any(Intent.class),
255                         nullable(IBinder.class),
256                         anyInt(),
257                         nullable(Bundle.class),
258                         anyInt());
259     }
260 
261     @Test
startAnyActivityAsUser_currentUser()262     public void startAnyActivityAsUser_currentUser() {
263         assertThrows(
264                 SecurityException.class,
265                 () ->
266                         mCrossProfileAppsServiceImpl.startActivityAsUser(
267                                 mIApplicationThread,
268                                 PACKAGE_ONE,
269                                 FEATURE_ID,
270                                 ACTIVITY_COMPONENT,
271                                 UserHandle.of(PRIMARY_USER).getIdentifier(),
272                                 false,
273                                 /* targetTask */ null,
274                                 /* options */ null));
275 
276         verify(mActivityTaskManagerInternal, never())
277                 .startActivityAsUser(
278                         nullable(IApplicationThread.class),
279                         anyString(),
280                         nullable(String.class),
281                         any(Intent.class),
282                         nullable(IBinder.class),
283                         anyInt(),
284                         nullable(Bundle.class),
285                         anyInt());
286     }
287 
288     @Test
startActivityAsUser_profile_notInstalled()289     public void startActivityAsUser_profile_notInstalled() throws Exception {
290         mockAppsInstalled(PACKAGE_ONE, PROFILE_OF_PRIMARY_USER, false);
291 
292         assertThrows(
293                 SecurityException.class,
294                 () ->
295                         mCrossProfileAppsServiceImpl.startActivityAsUser(
296                                 mIApplicationThread,
297                                 PACKAGE_ONE,
298                                 FEATURE_ID,
299                                 ACTIVITY_COMPONENT,
300                                 UserHandle.of(PROFILE_OF_PRIMARY_USER).getIdentifier(),
301                                 true,
302                                 /* targetTask */ null,
303                                 /* options */ null));
304 
305         verify(mActivityTaskManagerInternal, never())
306                 .startActivityAsUser(
307                         nullable(IApplicationThread.class),
308                         anyString(),
309                         nullable(String.class),
310                         any(Intent.class),
311                         nullable(IBinder.class),
312                         anyInt(),
313                         nullable(Bundle.class),
314                         anyInt());
315     }
316 
317     @Test
startAnyActivityAsUser_profile_notInstalled()318     public void startAnyActivityAsUser_profile_notInstalled() {
319         mockAppsInstalled(PACKAGE_ONE, PROFILE_OF_PRIMARY_USER, false);
320 
321         assertThrows(
322                 SecurityException.class,
323                 () ->
324                         mCrossProfileAppsServiceImpl.startActivityAsUser(
325                                 mIApplicationThread,
326                                 PACKAGE_ONE,
327                                 FEATURE_ID,
328                                 ACTIVITY_COMPONENT,
329                                 UserHandle.of(PROFILE_OF_PRIMARY_USER).getIdentifier(),
330                                 false,
331                                 /* targetTask */ null,
332                                 /* options */ null));
333 
334         verify(mActivityTaskManagerInternal, never())
335                 .startActivityAsUser(
336                         nullable(IApplicationThread.class),
337                         anyString(),
338                         nullable(String.class),
339                         any(Intent.class),
340                         nullable(IBinder.class),
341                         anyInt(),
342                         nullable(Bundle.class),
343                         anyInt());
344     }
345 
346     @Test
startActivityAsUser_profile_fakeCaller()347     public void startActivityAsUser_profile_fakeCaller() throws Exception {
348         assertThrows(
349                 SecurityException.class,
350                 () ->
351                         mCrossProfileAppsServiceImpl.startActivityAsUser(
352                                 mIApplicationThread,
353                                 PACKAGE_TWO,
354                                 FEATURE_ID,
355                                 ACTIVITY_COMPONENT,
356                                 UserHandle.of(PROFILE_OF_PRIMARY_USER).getIdentifier(),
357                                 true,
358                                 /* targetTask */ null,
359                                 /* options */ null));
360 
361         verify(mActivityTaskManagerInternal, never())
362                 .startActivityAsUser(
363                         nullable(IApplicationThread.class),
364                         anyString(),
365                         nullable(String.class),
366                         any(Intent.class),
367                         nullable(IBinder.class),
368                         anyInt(),
369                         nullable(Bundle.class),
370                         anyInt());
371     }
372 
373     @Test
startAnyActivityAsUser_profile_fakeCaller()374     public void startAnyActivityAsUser_profile_fakeCaller() {
375         assertThrows(
376                 SecurityException.class,
377                 () ->
378                         mCrossProfileAppsServiceImpl.startActivityAsUser(
379                                 mIApplicationThread,
380                                 PACKAGE_TWO,
381                                 FEATURE_ID,
382                                 ACTIVITY_COMPONENT,
383                                 UserHandle.of(PROFILE_OF_PRIMARY_USER).getIdentifier(),
384                                 false,
385                                 /* targetTask */ null,
386                                 /* options */ null));
387 
388         verify(mActivityTaskManagerInternal, never())
389                 .startActivityAsUser(
390                         nullable(IApplicationThread.class),
391                         anyString(),
392                         nullable(String.class),
393                         any(Intent.class),
394                         nullable(IBinder.class),
395                         anyInt(),
396                         nullable(Bundle.class),
397                         anyInt());
398     }
399 
400     @Test
startActivityAsUser_profile_notExported()401     public void startActivityAsUser_profile_notExported() throws Exception {
402         mActivityInfo.exported = false;
403 
404         assertThrows(
405                 SecurityException.class,
406                 () ->
407                         mCrossProfileAppsServiceImpl.startActivityAsUser(
408                                 mIApplicationThread,
409                                 PACKAGE_ONE,
410                                 FEATURE_ID,
411                                 ACTIVITY_COMPONENT,
412                                 UserHandle.of(PROFILE_OF_PRIMARY_USER).getIdentifier(),
413                                 true,
414                                 /* targetTask */ null,
415                                 /* options */ null));
416 
417         verify(mActivityTaskManagerInternal, never())
418                 .startActivityAsUser(
419                         nullable(IApplicationThread.class),
420                         anyString(),
421                         nullable(String.class),
422                         any(Intent.class),
423                         nullable(IBinder.class),
424                         anyInt(),
425                         nullable(Bundle.class),
426                         anyInt());
427     }
428 
429     @Test
startAnyActivityAsUser_profile_notExported()430     public void startAnyActivityAsUser_profile_notExported() {
431         try {
432             when(mPackageManager.getPermissionInfo(anyString(), anyInt()))
433                     .thenReturn(new PermissionInfo());
434         } catch (PackageManager.NameNotFoundException ignored) {
435         }
436         mActivityInfo.exported = false;
437 
438 
439         // There's a bug in static mocking if the APK is large - so here is the next best thing...
440         doReturn(Context.PERMISSION_CHECKER_SERVICE).when(mContext)
441                 .getSystemServiceName(PermissionCheckerManager.class);
442         PermissionCheckerManager permissionCheckerManager = mock(PermissionCheckerManager.class);
443         doReturn(PermissionChecker.PERMISSION_HARD_DENIED).when(permissionCheckerManager)
444                 .checkPermission(eq(Manifest.permission.INTERACT_ACROSS_PROFILES), any(
445                         AttributionSourceState.class), anyString(), anyBoolean(), anyBoolean(),
446                         anyBoolean(), anyInt());
447         doReturn(permissionCheckerManager).when(mContext).getSystemService(
448                 Context.PERMISSION_CHECKER_SERVICE);
449 
450         assertThrows(
451                 SecurityException.class,
452                 () ->
453                         mCrossProfileAppsServiceImpl.startActivityAsUser(
454                                 mIApplicationThread,
455                                 PACKAGE_ONE,
456                                 FEATURE_ID,
457                                 ACTIVITY_COMPONENT,
458                                 UserHandle.of(PROFILE_OF_PRIMARY_USER).getIdentifier(),
459                                 false,
460                                 /* targetTask */ null,
461                                 /* options */ null));
462 
463         verify(mActivityTaskManagerInternal, never())
464                 .startActivityAsUser(
465                         nullable(IApplicationThread.class),
466                         anyString(),
467                         nullable(String.class),
468                         any(Intent.class),
469                         nullable(IBinder.class),
470                         anyInt(),
471                         nullable(Bundle.class),
472                         anyInt());
473     }
474 
475     @Test
startActivityAsUser_profile_anotherPackage()476     public void startActivityAsUser_profile_anotherPackage() throws Exception {
477         assertThrows(
478                 SecurityException.class,
479                 () ->
480                         mCrossProfileAppsServiceImpl.startActivityAsUser(
481                                 mIApplicationThread,
482                                 PACKAGE_ONE,
483                                 FEATURE_ID,
484                                 new ComponentName(PACKAGE_TWO, "test"),
485                                 UserHandle.of(PROFILE_OF_PRIMARY_USER).getIdentifier(),
486                                 true,
487                                 /* targetTask */ null,
488                                 /* options */ null));
489 
490         verify(mActivityTaskManagerInternal, never())
491                 .startActivityAsUser(
492                         nullable(IApplicationThread.class),
493                         anyString(),
494                         nullable(String.class),
495                         any(Intent.class),
496                         nullable(IBinder.class),
497                         anyInt(),
498                         nullable(Bundle.class),
499                         anyInt());
500     }
501 
502     @Test
startAnyActivityAsUser_profile_anotherPackage()503     public void startAnyActivityAsUser_profile_anotherPackage() {
504         assertThrows(
505                 SecurityException.class,
506                 () ->
507                         mCrossProfileAppsServiceImpl.startActivityAsUser(
508                                 mIApplicationThread,
509                                 PACKAGE_ONE,
510                                 FEATURE_ID,
511                                 new ComponentName(PACKAGE_TWO, "test"),
512                                 UserHandle.of(PROFILE_OF_PRIMARY_USER).getIdentifier(),
513                                 false,
514                                 /* targetTask */ null,
515                                 /* options */ null));
516 
517         verify(mActivityTaskManagerInternal, never())
518                 .startActivityAsUser(
519                         nullable(IApplicationThread.class),
520                         anyString(),
521                         nullable(String.class),
522                         any(Intent.class),
523                         nullable(IBinder.class),
524                         anyInt(),
525                         nullable(Bundle.class),
526                         anyInt());
527     }
528 
529     @Test
startActivityAsUser_secondaryUser()530     public void startActivityAsUser_secondaryUser() throws Exception {
531         assertThrows(
532                 SecurityException.class,
533                 () ->
534                         mCrossProfileAppsServiceImpl.startActivityAsUser(
535                                 mIApplicationThread,
536                                 PACKAGE_ONE,
537                                 FEATURE_ID,
538                                 ACTIVITY_COMPONENT,
539                                 UserHandle.of(SECONDARY_USER).getIdentifier(),
540                                 true,
541                                 /* targetTask */ null,
542                                 /* options */ null));
543 
544         verify(mActivityTaskManagerInternal, never())
545                 .startActivityAsUser(
546                         nullable(IApplicationThread.class),
547                         anyString(),
548                         nullable(String.class),
549                         any(Intent.class),
550                         nullable(IBinder.class),
551                         anyInt(),
552                         nullable(Bundle.class),
553                         anyInt());
554     }
555 
556     @Test
startAnyActivityAsUser_secondaryUser()557     public void startAnyActivityAsUser_secondaryUser() {
558         assertThrows(
559                 SecurityException.class,
560                 () ->
561                         mCrossProfileAppsServiceImpl.startActivityAsUser(
562                                 mIApplicationThread,
563                                 PACKAGE_ONE,
564                                 FEATURE_ID,
565                                 ACTIVITY_COMPONENT,
566                                 UserHandle.of(SECONDARY_USER).getIdentifier(),
567                                 false,
568                                 /* targetTask */ null,
569                                 /* options */ null));
570 
571         verify(mActivityTaskManagerInternal, never())
572                 .startActivityAsUser(
573                         nullable(IApplicationThread.class),
574                         anyString(),
575                         nullable(String.class),
576                         any(Intent.class),
577                         nullable(IBinder.class),
578                         anyInt(),
579                         nullable(Bundle.class),
580                         anyInt());
581     }
582 
583     @Test
startActivityAsUser_fromProfile_success()584     public void startActivityAsUser_fromProfile_success() throws Exception {
585         mTestInjector.setCallingUserId(PROFILE_OF_PRIMARY_USER);
586 
587         mCrossProfileAppsServiceImpl.startActivityAsUser(
588                 mIApplicationThread,
589                 PACKAGE_ONE,
590                 FEATURE_ID,
591                 ACTIVITY_COMPONENT,
592                 UserHandle.of(PRIMARY_USER).getIdentifier(),
593                 true,
594                 /* targetTask */ null,
595                 /* options */ null);
596 
597         verify(mActivityTaskManagerInternal)
598                 .startActivityAsUser(
599                         nullable(IApplicationThread.class),
600                         eq(PACKAGE_ONE),
601                         eq(FEATURE_ID),
602                         any(Intent.class),
603                         nullable(IBinder.class),
604                         anyInt(),
605                         nullable(Bundle.class),
606                         eq(PRIMARY_USER));
607     }
608 
609     @Test
startActivityAsUser_sameTask_fromProfile_success()610     public void startActivityAsUser_sameTask_fromProfile_success() throws Exception {
611         mTestInjector.setCallingUserId(PROFILE_OF_PRIMARY_USER);
612 
613         Bundle options = ActivityOptions.makeOpenCrossProfileAppsAnimation().toBundle();
614         Binder targetTask = new Binder();
615         mCrossProfileAppsServiceImpl.startActivityAsUser(
616                 mIApplicationThread,
617                 PACKAGE_ONE,
618                 FEATURE_ID,
619                 ACTIVITY_COMPONENT,
620                 UserHandle.of(PRIMARY_USER).getIdentifier(),
621                 true,
622                 targetTask,
623                 options);
624         verify(mActivityTaskManagerInternal)
625                 .startActivityAsUser(
626                         nullable(IApplicationThread.class),
627                         eq(PACKAGE_ONE),
628                         eq(FEATURE_ID),
629                         any(Intent.class),
630                         eq(targetTask),
631                         anyInt(),
632                         eq(options),
633                         eq(PRIMARY_USER));
634     }
635 
mockAppsInstalled(String packageName, int user, boolean installed)636     private void mockAppsInstalled(String packageName, int user, boolean installed) {
637         when(mPackageManagerInternal.getPackageInfo(
638                 eq(packageName),
639                 anyLong(),
640                 anyInt(),
641                 eq(user)))
642                 .thenReturn(installed ? createInstalledPackageInfo() : null);
643     }
644 
createInstalledPackageInfo()645     private PackageInfo createInstalledPackageInfo() {
646         PackageInfo packageInfo = new PackageInfo();
647         packageInfo.applicationInfo = new ApplicationInfo();
648         packageInfo.applicationInfo.enabled = true;
649         return packageInfo;
650     }
651 
mockActivityLaunchIntentResolvedTo(ComponentName componentName)652     private void mockActivityLaunchIntentResolvedTo(ComponentName componentName) {
653         ResolveInfo resolveInfo = new ResolveInfo();
654         ActivityInfo activityInfo = new ActivityInfo();
655         activityInfo.packageName = componentName.getPackageName();
656         activityInfo.name = componentName.getClassName();
657         activityInfo.exported = true;
658         resolveInfo.activityInfo = activityInfo;
659         mActivityInfo = activityInfo;
660 
661         when(mPackageManagerInternal.queryIntentActivities(
662                 any(Intent.class), nullable(String.class), anyLong(), anyInt(), anyInt()))
663                 .thenReturn(Collections.singletonList(resolveInfo));
664     }
665 
666     private class TestInjector implements CrossProfileAppsServiceImpl.Injector {
667         private int mCallingUid;
668         private int mCallingUserId;
669         private int mCallingPid;
670 
setCallingUid(int uid)671         public void setCallingUid(int uid) {
672             mCallingUid = uid;
673         }
674 
setCallingPid(int pid)675         public void setCallingPid(int pid) {
676             mCallingPid = pid;
677         }
678 
setCallingUserId(int userId)679         public void setCallingUserId(int userId) {
680             mCallingUserId = userId;
681         }
682 
683         @Override
getCallingUid()684         public int getCallingUid() {
685             return mCallingUid;
686         }
687 
688         @Override
getCallingPid()689         public int getCallingPid() {
690             return mCallingPid;
691         }
692 
693         @Override
getCallingUserId()694         public int getCallingUserId() {
695             return mCallingUserId;
696         }
697 
698         @Override
getCallingUserHandle()699         public UserHandle getCallingUserHandle() {
700             return UserHandle.of(mCallingUserId);
701         }
702 
703         @Override
clearCallingIdentity()704         public long clearCallingIdentity() {
705             return 0;
706         }
707 
708         @Override
restoreCallingIdentity(long token)709         public void restoreCallingIdentity(long token) {
710         }
711 
712         @Override
withCleanCallingIdentity(ThrowingRunnable action)713         public void withCleanCallingIdentity(ThrowingRunnable action) {
714             action.run();
715         }
716 
717         @Override
withCleanCallingIdentity(ThrowingSupplier<T> action)718         public <T> T withCleanCallingIdentity(ThrowingSupplier<T> action) {
719             return action.get();
720         }
721 
722         @Override
getUserManager()723         public UserManager getUserManager() {
724             return mUserManager;
725         }
726 
727         @Override
getPackageManagerInternal()728         public PackageManagerInternal getPackageManagerInternal() {
729             return mPackageManagerInternal;
730         }
731 
732         @Override
getPackageManager()733         public PackageManager getPackageManager() {
734             return mPackageManager;
735         }
736 
737         @Override
getAppOpsManager()738         public AppOpsManager getAppOpsManager() {
739             return mAppOpsManager;
740         }
741 
742         @Override
getActivityManagerInternal()743         public ActivityManagerInternal getActivityManagerInternal() {
744             return mActivityManagerInternal;
745         }
746 
747         @Override
getActivityTaskManagerInternal()748         public ActivityTaskManagerInternal getActivityTaskManagerInternal() {
749             return mActivityTaskManagerInternal;
750         }
751 
752         @Override
getIPackageManager()753         public IPackageManager getIPackageManager() {
754             return mIPackageManager;
755         }
756 
757         @Override
getDevicePolicyManagerInternal()758         public DevicePolicyManagerInternal getDevicePolicyManagerInternal() {
759             return mDevicePolicyManagerInternal;
760         }
761 
762         @Override
sendBroadcastAsUser(Intent intent, UserHandle user)763         public void sendBroadcastAsUser(Intent intent, UserHandle user) {
764             mContext.sendBroadcastAsUser(intent, user);
765         }
766 
767         @Override
checkComponentPermission( String permission, int uid, int owningUid, boolean exported)768         public int checkComponentPermission(
769                 String permission, int uid, int owningUid, boolean exported) {
770             return ActivityManager.checkComponentPermission(permission, uid, owningUid, exported);
771         }
772 
773         @Override
killUid(int uid)774         public void killUid(int uid) {
775             PermissionManagerService.killUid(
776                     UserHandle.getAppId(uid),
777                     UserHandle.getUserId(uid),
778                     PermissionManager.KILL_APP_REASON_PERMISSIONS_REVOKED);
779         }
780     }
781 }