diff options
author | 2025-03-13 11:13:43 +0000 | |
---|---|---|
committer | 2025-03-13 14:20:49 +0000 | |
commit | 2d0669f14bf84d60bfcdc331d726178a7d573d4c (patch) | |
tree | 565cd147dfa80ec27a10ff638044d0b59fafe4c4 | |
parent | 165058c81c7d402cf6580e57f5efb7dd009e941d (diff) |
Properly handle x-profile checks
Bug: b/402367358
Test: atest DocumentsUITests
Flag: EXEMPT bug_fix
Change-Id: I79d9fc9e11940b478534e2fc226012ca4064f6b2
-rw-r--r-- | src/com/android/documentsui/UserManagerState.java | 264 | ||||
-rw-r--r-- | src/com/android/documentsui/base/UserId.java | 7 | ||||
-rw-r--r-- | tests/unit/com/android/documentsui/UserManagerStateTest.java | 196 |
3 files changed, 207 insertions, 260 deletions
diff --git a/src/com/android/documentsui/UserManagerState.java b/src/com/android/documentsui/UserManagerState.java index d2ddae615..1023c8c1d 100644 --- a/src/com/android/documentsui/UserManagerState.java +++ b/src/com/android/documentsui/UserManagerState.java @@ -22,7 +22,6 @@ import static com.android.documentsui.DevicePolicyResources.Drawables.Style.SOLI import static com.android.documentsui.DevicePolicyResources.Drawables.WORK_PROFILE_ICON; import static com.android.documentsui.DevicePolicyResources.Strings.PERSONAL_TAB; import static com.android.documentsui.DevicePolicyResources.Strings.WORK_TAB; -import static com.android.documentsui.base.SharedMinimal.DEBUG; import android.Manifest; import android.annotation.SuppressLint; @@ -263,35 +262,13 @@ public interface UserManagerState { } synchronized (mCanForwardToProfileIdMap) { if (!mCanForwardToProfileIdMap.containsKey(userId)) { - - UserHandle handle = UserHandle.of(userId.getIdentifier()); - - // Decide if to use the parent's access, or this handle's access. - if (isCrossProfileContentSharingStrategyDelegatedFromParent(handle)) { - UserHandle parentHandle = mUserManager.getProfileParent(handle); - // Couldn't resolve parent to check access, so fail closed. - if (parentHandle == null) { - mCanForwardToProfileIdMap.put(userId, false); - } else if (mCurrentUser.getIdentifier() - == parentHandle.getIdentifier()) { - // Check if the parent is the current user, if so this profile - // is also accessible. - mCanForwardToProfileIdMap.put(userId, true); - - } else { - UserId parent = UserId.of(parentHandle); - mCanForwardToProfileIdMap.put( - userId, - doesCrossProfileForwardingActivityExistForUser( - mCurrentStateIntent, parent)); - } - } else { - // Update the profile map for this profile. - mCanForwardToProfileIdMap.put( - userId, - doesCrossProfileForwardingActivityExistForUser( - mCurrentStateIntent, userId)); - } + mCanForwardToProfileIdMap.put( + userId, + isCrossProfileAllowedToUser( + mContext, + mCurrentStateIntent, + UserId.CURRENT_USER, + userId)); } } } else { @@ -331,43 +308,37 @@ public interface UserManagerState { if (mUserManager == null) { Log.e(TAG, "cannot obtain user manager"); - result.add(mCurrentUser); return result; } final List<UserHandle> userProfiles = mUserManager.getUserProfiles(); - if (userProfiles.size() < 2) { - result.add(mCurrentUser); - return result; - } - if (SdkLevel.isAtLeastV()) { - getUserIdsInternalPostV(userProfiles, result); - } else { - getUserIdsInternalPreV(userProfiles, result); - } - return result; - } + result.add(mCurrentUser); + boolean currentUserIsManaged = + mUserManager.isManagedProfile(mCurrentUser.getIdentifier()); - @SuppressLint("NewApi") - private void getUserIdsInternalPostV(List<UserHandle> userProfiles, List<UserId> result) { - for (UserHandle userHandle : userProfiles) { - if (userHandle.getIdentifier() == ActivityManager.getCurrentUser()) { - result.add(UserId.of(userHandle)); + for (UserHandle handle : userProfiles) { + if (SdkLevel.isAtLeastV()) { + if (!isProfileAllowed(handle)) { + continue; + } } else { - // Out of all the profiles returned by user manager the profiles that are - // returned should satisfy both the following conditions: - // 1. It has user property SHOW_IN_SHARING_SURFACES_SEPARATE - // 2. Quite mode is not enabled, if it is enabled then the profile's user - // property is not SHOW_IN_QUIET_MODE_HIDDEN - if (isProfileAllowed(userHandle)) { - result.add(UserId.of(userHandle)); + // Only allow managed profiles + the parent user on lower than V. + if (currentUserIsManaged + && mUserManager.getProfileParent(mCurrentUser.getUserHandle()) + == handle) { + // Intentionally empty so that this profile gets added. + } else if (!mUserManager.isManagedProfile(handle.getIdentifier())) { + continue; } } + + // Ensure the system user doesn't get added twice. + if (result.contains(UserId.of(handle))) continue; + result.add(UserId.of(handle)); } - if (result.isEmpty()) { - result.add(mCurrentUser); - } + + return result; } /** @@ -444,33 +415,6 @@ public interface UserManagerState { return false; } - private void getUserIdsInternalPreV(List<UserHandle> userProfiles, List<UserId> result) { - result.add(mCurrentUser); - UserId systemUser = null; - UserId managedUser = null; - for (UserHandle userHandle : userProfiles) { - if (userHandle.isSystem()) { - systemUser = UserId.of(userHandle); - } else if (mUserManager.isManagedProfile(userHandle.getIdentifier())) { - managedUser = UserId.of(userHandle); - } - } - if (mCurrentUser.isSystem() && managedUser != null) { - result.add(managedUser); - } else if (mCurrentUser.isManagedProfile(mUserManager) && systemUser != null) { - result.add(0, systemUser); - } else { - if (DEBUG) { - Log.w( - TAG, - "The current user " - + UserId.CURRENT_USER - + " is neither system nor managed user. has system user: " - + (systemUser != null)); - } - } - } - private void getUserIdToLabelMapInternal() { if (SdkLevel.isAtLeastV()) { getUserIdToLabelMapInternalPostV(); @@ -651,50 +595,124 @@ public interface UserManagerState { */ private void getCanForwardToProfileIdMapInternal(Intent intent) { - Map<UserId, Boolean> profileIsAccessibleToProcessOwner = new HashMap<>(); + synchronized (mCanForwardToProfileIdMap) { + mCanForwardToProfileIdMap.clear(); + for (UserId userId : getUserIds()) { + mCanForwardToProfileIdMap.put( + userId, + isCrossProfileAllowedToUser( + mContext, intent, mCurrentUser, userId)); + } + } + } - List<UserId> delegatedFromParent = new ArrayList<>(); + /** + * Determines if the provided UserIds support CrossProfile content sharing. + * + * <p>This method accepts a pair of user handles (from/to) and determines if CrossProfile + * access is permitted between those two profiles. + * + * <p>There are differences is on how the access is determined based on the platform SDK: + * + * <p>For Platform SDK < V: + * + * <p>A check for CrossProfileIntentForwarders in the origin (from) profile that target the + * destination (to) profile. If such a forwarder exists, then access is allowed, and denied + * otherwise. + * + * <p>For Platform SDK >= V: + * + * <p>The method now takes into account access delegation, which was first added in Android + * V. + * + * <p>For profiles that set the [CROSS_PROFILE_CONTENT_SHARING_DELEGATE_FROM_PARENT] + * property in its [UserProperties], its parent profile will be substituted in for its side + * of the check. + * + * <p>ex. For access checks between a Managed (from) and Private (to) profile, where: - + * Managed does not delegate to its parent - Private delegates to its parent + * + * <p>The following logic is performed: Managed -> parent(Private) + * + * <p>The same check in the other direction would yield: parent(Private) -> Managed + * + * <p>Note how the private profile is never actually used for either side of the check, + * since it is delegating its access check to the parent. And thus, if Managed can access + * the parent, it can also access the private. + * + * @param context Current context object, for switching user contexts. + * @param intent The current intent the Photopicker is running under. + * @param fromUser The Origin profile, where the user is coming from + * @param toUser The destination profile, where the user is attempting to go to. + * @return Whether CrossProfile content sharing is supported in this handle. + */ + private boolean isCrossProfileAllowedToUser( + Context context, Intent intent, UserId fromUser, UserId toUser) { - for (UserId userId : getUserIds()) { + // Early exit conditions, accessing self. + // NOTE: It is also possible to reach this state if this method is recursively checking + // from: parent(A) to:parent(B) where A and B are both children of the same parent. + if (fromUser.getIdentifier() == toUser.getIdentifier()) { + return true; + } - // Early exit, self is always accessible. - if (userId.getIdentifier() == mCurrentUser.getIdentifier()) { - profileIsAccessibleToProcessOwner.put(userId, true); - continue; - } + // Decide if we should use actual from or parent(from) + UserHandle currentFromUser = + getProfileToCheckCrossProfileAccess(fromUser.getUserHandle()); - // CrossProfileContentSharingStrategyDelegatedFromParent is only V+ sdks. - if (SdkLevel.isAtLeastV() - && isCrossProfileContentSharingStrategyDelegatedFromParent( - UserHandle.of(userId.getIdentifier()))) { - delegatedFromParent.add(userId); - continue; - } + // Decide if we should use actual to or parent(to) + UserHandle currentToUser = getProfileToCheckCrossProfileAccess(toUser.getUserHandle()); - // Check for cross profile & add to the map. - profileIsAccessibleToProcessOwner.put( - userId, doesCrossProfileForwardingActivityExistForUser(intent, userId)); + // When the from/to has changed from the original parameters, recursively restart the + // checks with the new from/to handles. + if (fromUser.getIdentifier() != currentFromUser.getIdentifier() + || toUser.getIdentifier() != currentToUser.getIdentifier()) { + return isCrossProfileAllowedToUser( + context, intent, UserId.of(currentFromUser), UserId.of(currentToUser)); } - // For profiles that delegate their access to the parent, set the access for - // those profiles - // equal to the same as their parent. - for (UserId userId : delegatedFromParent) { - UserHandle parent = - mUserManager.getProfileParent(UserHandle.of(userId.getIdentifier())); - profileIsAccessibleToProcessOwner.put( - userId, - profileIsAccessibleToProcessOwner.getOrDefault( - UserId.of(parent), /* default= */ false)); - } + PackageManager pm = context.getPackageManager(); + return doesCrossProfileIntentForwarderExist(intent, pm, fromUser, toUser); + } - synchronized (mCanForwardToProfileIdMap) { - mCanForwardToProfileIdMap.clear(); - for (Map.Entry<UserId, Boolean> entry : - profileIsAccessibleToProcessOwner.entrySet()) { - mCanForwardToProfileIdMap.put(entry.getKey(), entry.getValue()); + /** + * Determines if the target UserHandle delegates its content sharing to its parent. + * + * @param userHandle The target handle to check delegation for. + * @return TRUE if V+ and the handle delegates to parent. False otherwise. + */ + private boolean isCrossProfileStrategyDelegatedToParent(UserHandle userHandle) { + if (SdkLevel.isAtLeastV()) { + if (mUserManager == null) { + Log.e(TAG, "Cannot obtain user manager"); + return false; + } + UserProperties userProperties = mUserManager.getUserProperties(userHandle); + if (userProperties.getCrossProfileContentSharingStrategy() + == userProperties.CROSS_PROFILE_CONTENT_SHARING_DELEGATE_FROM_PARENT) { + return true; } } + return false; + } + + /** + * Acquires the correct {@link UserHandle} which should be used for CrossProfile access + * checks. + * + * @param userHandle the origin handle. + * @return The UserHandle that should be used for cross profile access checks. In the event + * the origin handle delegates its access, this may not be the same handle as the origin + * handle. + */ + private UserHandle getProfileToCheckCrossProfileAccess(UserHandle userHandle) { + if (mUserManager == null) { + Log.e(TAG, "Cannot obtain user manager"); + return null; + } + return isCrossProfileStrategyDelegatedToParent(userHandle) + ? mUserManager.getProfileParent(userHandle) + : userHandle; } /** @@ -706,16 +724,18 @@ public interface UserManagerState { * @return whether a CrossProfileIntentForwardingActivity could be found for the given * intent, and user. */ - private boolean doesCrossProfileForwardingActivityExistForUser( - Intent intent, UserId targetUserId) { + private boolean doesCrossProfileIntentForwarderExist( + Intent intent, PackageManager pm, UserId fromUser, UserId targetUserId) { - final PackageManager pm = mContext.getPackageManager(); final Intent intentToCheck = (Intent) intent.clone(); intentToCheck.setComponent(null); intentToCheck.setPackage(null); for (ResolveInfo resolveInfo : - pm.queryIntentActivities(intentToCheck, PackageManager.MATCH_DEFAULT_ONLY)) { + pm.queryIntentActivitiesAsUser( + intentToCheck, + PackageManager.MATCH_DEFAULT_ONLY, + fromUser.getUserHandle())) { if (resolveInfo.isCrossProfileIntentForwarderActivity()) { /* diff --git a/src/com/android/documentsui/base/UserId.java b/src/com/android/documentsui/base/UserId.java index 21842917a..7aff61e9b 100644 --- a/src/com/android/documentsui/base/UserId.java +++ b/src/com/android/documentsui/base/UserId.java @@ -95,6 +95,13 @@ public final class UserId { } /** + * Return this User's {@link UserHandle}. + */ + public UserHandle getUserHandle() { + return mUserHandle; + } + + /** * Return a package manager instance of this user. */ public PackageManager getPackageManager(Context context) { diff --git a/tests/unit/com/android/documentsui/UserManagerStateTest.java b/tests/unit/com/android/documentsui/UserManagerStateTest.java index 9d629c574..04102a429 100644 --- a/tests/unit/com/android/documentsui/UserManagerStateTest.java +++ b/tests/unit/com/android/documentsui/UserManagerStateTest.java @@ -23,6 +23,7 @@ import static com.android.documentsui.DevicePolicyResources.Strings.WORK_TAB; import static com.google.common.truth.Truth.assertWithMessage; +import static org.junit.Assume.assumeFalse; import static org.junit.Assume.assumeTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; @@ -225,7 +226,7 @@ public class UserManagerStateTest { @Test public void testGetUserIds_allProfilesCurrentUserSystem_allShowInSharingSurfacesSeparate() { - if (!SdkLevel.isAtLeastV()) return; + assumeTrue(SdkLevel.isAtLeastV()); UserId currentUser = UserId.of(mSystemUser); initializeUserManagerState( currentUser, @@ -240,7 +241,7 @@ public class UserManagerStateTest { @Test public void testGetUserIds_allProfilesCurrentUserManaged_allShowInSharingSurfacesSeparate() { - if (!SdkLevel.isAtLeastV()) return; + assumeTrue(SdkLevel.isAtLeastV()); UserId currentUser = UserId.of(mManagedUser); initializeUserManagerState( currentUser, @@ -255,7 +256,7 @@ public class UserManagerStateTest { @Test public void testGetUserIds_allProfilesCurrentUserPrivate_allShowInSharingSurfacesSeparate() { - if (!SdkLevel.isAtLeastV()) return; + assumeTrue(SdkLevel.isAtLeastV()); UserId currentUser = UserId.of(mPrivateUser); initializeUserManagerState( currentUser, @@ -289,7 +290,7 @@ public class UserManagerStateTest { @Test public void testGetUserIds_systemAndPrivateUserCurrentUserSystem_returnsBoth() { - if (!SdkLevel.isAtLeastV()) return; + assumeTrue(SdkLevel.isAtLeastV()); UserId currentUser = UserId.of(mSystemUser); initializeUserManagerState(currentUser, Lists.newArrayList(mSystemUser, mPrivateUser)); @@ -300,7 +301,7 @@ public class UserManagerStateTest { @Test public void testGetUserIds_systemAndPrivateUserCurrentUserPrivate_returnsBoth() { - if (!SdkLevel.isAtLeastV()) return; + assumeTrue(SdkLevel.isAtLeastV()); UserId currentUser = UserId.of(mPrivateUser); initializeUserManagerState(currentUser, Lists.newArrayList(mSystemUser, mPrivateUser)); @@ -310,116 +311,6 @@ public class UserManagerStateTest { } @Test - public void testGetUserIds_systemAndOtherUserCurrentUserOtherPreV_returnsCurrentUser() { - if (SdkLevel.isAtLeastV()) return; - UserId currentUser = UserId.of(mOtherUser); - initializeUserManagerState(currentUser, Lists.newArrayList(mSystemUser, mOtherUser)); - - assertWithMessage("getUserIds returns unexpected list of user ids") - .that(mUserManagerState.getUserIds()) - .containsExactly(currentUser); - } - - @Test - public void testGetUserIds_systemAndOtherUserCurrentUserOtherPostV_returnsSystemUser() { - if (!SdkLevel.isAtLeastV()) return; - UserId currentUser = UserId.of(mOtherUser); - initializeUserManagerState(currentUser, Lists.newArrayList(mSystemUser, mOtherUser)); - - assertWithMessage("getUserIds returns unexpected list of user ids") - .that(mUserManagerState.getUserIds()) - .containsExactly(UserId.of(mSystemUser)); - } - - @Test - public void testGetUserIds_normalAndOtherUserCurrentUserNormal_returnsCurrentUser() { - // since both users do not have show in sharing surfaces separate, returns - // current user - UserId currentUser = UserId.of(mNormalUser); - initializeUserManagerState(currentUser, Lists.newArrayList(mOtherUser, mNormalUser)); - - assertWithMessage("getUserIds returns unexpected list of user ids") - .that(mUserManagerState.getUserIds()) - .containsExactly(UserId.of(mNormalUser)); - } - - @Test - public void testGetUserIds_systemAndManagedUserCurrentUserSystem_returnsBothInOrder() { - // Returns the both if there are system and managed users. - if (SdkLevel.isAtLeastV()) return; - UserId currentUser = UserId.of(mSystemUser); - initializeUserManagerState(currentUser, Lists.newArrayList(mSystemUser, mManagedUser)); - assertWithMessage("getUserIds returns unexpected list of user ids") - .that(mUserManagerState.getUserIds()) - .containsExactly(UserId.of(mSystemUser), UserId.of(mManagedUser)) - .inOrder(); - } - - @Test - public void testGetUserIds_systemAndManagedUserCurrentUserManaged_returnsBothInOrder() { - // Returns the both if there are system and managed users. - if (SdkLevel.isAtLeastV()) return; - UserId currentUser = UserId.of(mManagedUser); - initializeUserManagerState(currentUser, Lists.newArrayList(mSystemUser, mManagedUser)); - assertWithMessage("getUserIds returns unexpected list of user ids") - .that(mUserManagerState.getUserIds()) - .containsExactly(UserId.of(mSystemUser), UserId.of(mManagedUser)) - .inOrder(); - } - - @Test - public void testGetUserIds_managedAndSystemUserCurrentUserSystem_returnsBothInOrder() { - // Returns the both if there are system and managed users, regardless of input - // list order. - if (SdkLevel.isAtLeastV()) return; - UserId currentUser = UserId.of(mSystemUser); - initializeUserManagerState(currentUser, Lists.newArrayList(mManagedUser, mSystemUser)); - assertWithMessage("getUserIds returns unexpected list of user ids") - .that(mUserManagerState.getUserIds()) - .containsExactly(UserId.of(mSystemUser), UserId.of(mManagedUser)) - .inOrder(); - } - - @Test - public void testGetUserIds_otherAndManagedUserCurrentUserOtherPreV_returnsCurrentUser() { - // When there is no system user, returns the current user. - // This is a case theoretically can happen but we don't expect. So we return the - // current - // user only. - if (SdkLevel.isAtLeastV()) return; - UserId currentUser = UserId.of(mOtherUser); - initializeUserManagerState(currentUser, Lists.newArrayList(mOtherUser, mManagedUser)); - assertWithMessage("getUserIds returns unexpected list of user ids") - .that(mUserManagerState.getUserIds()) - .containsExactly(currentUser); - } - - @Test - public void testGetUserIds_otherAndManagedUserCurrentUserOtherPostV_returnsManagedUser() { - // Only the users with show in sharing surfaces separate are eligible to be - // returned - if (!SdkLevel.isAtLeastV()) return; - UserId currentUser = UserId.of(mOtherUser); - initializeUserManagerState(currentUser, Lists.newArrayList(mOtherUser, mManagedUser)); - assertWithMessage("getUserIds returns unexpected list of user ids") - .that(mUserManagerState.getUserIds()) - .containsExactly(UserId.of(mManagedUser)); - } - - @Test - public void testGetUserIds_otherAndManagedUserCurrentUserManaged_returnsCurrentUser() { - // When there is no system user, returns the current user. - // This is a case theoretically can happen, but we don't expect. So we return - // the current - // user only. - UserId currentUser = UserId.of(mManagedUser); - initializeUserManagerState(currentUser, Lists.newArrayList(mOtherUser, mManagedUser)); - assertWithMessage("getUserIds returns unexpected list of user ids") - .that(mUserManagerState.getUserIds()) - .containsExactly(currentUser); - } - - @Test public void testGetUserIds_unsupportedDeviceCurrent_returnsCurrentUser() { // This test only tests for Android R or later. This test case always passes // before R. @@ -457,7 +348,8 @@ public class UserManagerStateTest { initializeUserManagerState(currentUser, Lists.newArrayList(mSystemUser, mManagedUser)); final List<ResolveInfo> mMockResolveInfoList = Lists.newArrayList(mMockInfoManagedUser); - when(mMockPackageManager.queryIntentActivities(any(Intent.class), anyInt())) + when(mMockPackageManager.queryIntentActivitiesAsUser( + any(Intent.class), anyInt(), eq(mSystemUser))) .thenReturn(mMockResolveInfoList); Map<UserId, Boolean> expectedCanForwardToProfileIdMap = new HashMap<>(); @@ -471,7 +363,7 @@ public class UserManagerStateTest { @Test public void testGetCanForwardToProfileIdMap_systemUserCanAlwaysForwardToPrivate() { - if (!SdkLevel.isAtLeastV()) return; + assumeTrue(SdkLevel.isAtLeastV()); UserId currentUser = UserId.of(mSystemUser); initializeUserManagerState(currentUser, Lists.newArrayList(mSystemUser, mPrivateUser)); @@ -497,8 +389,8 @@ public class UserManagerStateTest { .thenReturn(mMockResolveInfoList); } else { initializeUserManagerState(currentUser, Lists.newArrayList(mSystemUser, mManagedUser)); - when(mMockPackageManager.queryIntentActivities( - mMockIntent, PackageManager.MATCH_DEFAULT_ONLY)) + when(mMockPackageManager.queryIntentActivitiesAsUser( + mMockIntent, PackageManager.MATCH_DEFAULT_ONLY, mSystemUser)) .thenReturn(mMockResolveInfoList); } @@ -515,10 +407,13 @@ public class UserManagerStateTest { } @Test - public void testGetCanForwardToProfileIdMap_managedCanForwardToAll() { + public void testGetCanForwardToProfileIdMap_managedCanForwardToAllVPlus() { + assumeTrue(SdkLevel.isAtLeastV()); + UserId currentUser = UserId.of(mManagedUser); final List<ResolveInfo> mMockResolveInfoList = Lists.newArrayList(mMockInfoPrimaryUser); - when(mMockPackageManager.queryIntentActivities(any(Intent.class), anyInt())) + when(mMockPackageManager.queryIntentActivitiesAsUser( + any(Intent.class), anyInt(), eq(mManagedUser))) .thenReturn(mMockResolveInfoList); initializeUserManagerState( @@ -535,10 +430,31 @@ public class UserManagerStateTest { } @Test + public void testGetCanForwardToProfileIdMap_managedCanForwardToAllUMinus() { + assumeFalse(SdkLevel.isAtLeastV()); + + UserId currentUser = UserId.of(mManagedUser); + final List<ResolveInfo> mMockResolveInfoList = Lists.newArrayList(mMockInfoPrimaryUser); + when(mMockPackageManager.queryIntentActivitiesAsUser( + any(Intent.class), anyInt(), eq(mManagedUser))) + .thenReturn(mMockResolveInfoList); + + initializeUserManagerState( + currentUser, Lists.newArrayList(mSystemUser, mManagedUser)); + + Map<UserId, Boolean> expectedCanForwardToProfileIdMap = new HashMap<>(); + expectedCanForwardToProfileIdMap.put(UserId.of(mSystemUser), true); + expectedCanForwardToProfileIdMap.put(UserId.of(mManagedUser), true); + + assertWithMessage("getCanForwardToProfileIdMap returns incorrect mappings") + .that(mUserManagerState.getCanForwardToProfileIdMap(mMockIntent)) + .isEqualTo(expectedCanForwardToProfileIdMap); + } + + @Test public void testGetCanForwardToProfileIdMap_managedCanNotForwardToAll() { UserId currentUser = UserId.of(mManagedUser); - final List<ResolveInfo> mMockResolveInfoList = - Lists.newArrayList(mMockInfoPrivateUser, mMockInfoPrimaryUser); + final List<ResolveInfo> mMockResolveInfoList = Lists.newArrayList(mMockInfoPrimaryUser); if (SdkLevel.isAtLeastV()) { initializeUserManagerState( @@ -567,12 +483,14 @@ public class UserManagerStateTest { @Test public void testGetCanForwardToProfileIdMap_privateCanForwardToAll() { + assumeTrue(SdkLevel.isAtLeastV()); UserId currentUser = UserId.of(mPrivateUser); initializeUserManagerState( currentUser, Lists.newArrayList(mSystemUser, mManagedUser, mPrivateUser)); final List<ResolveInfo> mMockResolveInfoList = Lists.newArrayList(mMockInfoPrimaryUser, mMockInfoManagedUser); - when(mMockPackageManager.queryIntentActivities(any(Intent.class), anyInt())) + when(mMockPackageManager.queryIntentActivitiesAsUser( + any(Intent.class), anyInt(), eq(mSystemUser))) .thenReturn(mMockResolveInfoList); Map<UserId, Boolean> expectedCanForwardToProfileIdMap = new HashMap<>(); @@ -587,6 +505,7 @@ public class UserManagerStateTest { @Test public void testGetCanForwardToProfileIdMap_privateCanNotForwardToManagedUser() { + assumeTrue(SdkLevel.isAtLeastV()); UserId currentUser = UserId.of(mPrivateUser); initializeUserManagerState( currentUser, Lists.newArrayList(mSystemUser, mManagedUser, mPrivateUser)); @@ -607,7 +526,7 @@ public class UserManagerStateTest { @Test public void testGetCanForwardToProfileIdMap_privateCanAlwaysForwardToSystemUser() { - if (!SdkLevel.isAtLeastV()) return; + assumeTrue(SdkLevel.isAtLeastV()); UserId currentUser = UserId.of(mPrivateUser); initializeUserManagerState(currentUser, Lists.newArrayList(mSystemUser, mPrivateUser)); @@ -626,7 +545,7 @@ public class UserManagerStateTest { @Test public void testOnProfileStatusChange_anyIntentActionForManagedProfile() { - if (!SdkLevel.isAtLeastV()) return; + assumeTrue(SdkLevel.isAtLeastV()); UserId currentUser = UserId.of(mSystemUser); initializeUserManagerState( currentUser, Lists.newArrayList(mSystemUser, mManagedUser, mPrivateUser)); @@ -653,7 +572,7 @@ public class UserManagerStateTest { @Test public void testOnProfileStatusChange_actionProfileUnavailableForPrivateProfile() { - if (!SdkLevel.isAtLeastV()) return; + assumeTrue(SdkLevel.isAtLeastV()); UserId currentUser = UserId.of(mSystemUser); UserId managedUser = UserId.of(mManagedUser); UserId privateUser = UserId.of(mPrivateUser); @@ -666,8 +585,7 @@ public class UserManagerStateTest { currentUser, Lists.newArrayList(mSystemUser, mManagedUser, mPrivateUser)); // UserManagerState#mUserId and UserManagerState#mCanForwardToProfileIdMap will - // empty - // by default if the getters of these member variables have not been called + // empty by default if the getters of these member variables have not been called List<UserId> userIdsBeforeIntent = new ArrayList<>(mUserManagerState.getUserIds()); Map<UserId, Boolean> canForwardToProfileIdMapBeforeIntent = new HashMap<>(mUserManagerState.getCanForwardToProfileIdMap(mMockIntent)); @@ -694,12 +612,12 @@ public class UserManagerStateTest { @Test public void testOnProfileStatusChange_actionProfileAvailable_profileInitialised() { - if (!SdkLevel.isAtLeastV()) return; + assumeTrue(SdkLevel.isAtLeastV()); UserId currentUser = UserId.of(mSystemUser); UserId managedUser = UserId.of(mManagedUser); UserId privateUser = UserId.of(mPrivateUser); final List<ResolveInfo> mMockResolveInfoList = - Lists.newArrayList(mMockInfoManagedUser, mMockInfoPrivateUser); + Lists.newArrayList(mMockInfoManagedUser); when(mMockPackageManager.queryIntentActivitiesAsUser( mMockIntent, PackageManager.MATCH_DEFAULT_ONLY, mSystemUser)) .thenReturn(mMockResolveInfoList); @@ -748,7 +666,8 @@ public class UserManagerStateTest { final List<ResolveInfo> mMockResolveInfoList = Lists.newArrayList(mMockInfoManagedUser); - when(mMockPackageManager.queryIntentActivities(any(Intent.class), anyInt())) + when(mMockPackageManager.queryIntentActivitiesAsUser( + any(Intent.class), anyInt(), eq(mSystemUser))) .thenReturn(mMockResolveInfoList); initializeUserManagerState(currentUser, Lists.newArrayList(mSystemUser, mManagedUser)); @@ -780,13 +699,14 @@ public class UserManagerStateTest { @Test public void testOnProfileStatusChange_actionProfileAvailable_profileNotInitialised() { - if (!SdkLevel.isAtLeastV()) return; + assumeTrue(SdkLevel.isAtLeastV()); UserId currentUser = UserId.of(mSystemUser); UserId managedUser = UserId.of(mManagedUser); UserId privateUser = UserId.of(mPrivateUser); final List<ResolveInfo> mMockResolveInfoList = Lists.newArrayList(mMockInfoManagedUser, mMockInfoPrivateUser); - when(mMockPackageManager.queryIntentActivities(any(Intent.class), anyInt())) + when(mMockPackageManager.queryIntentActivitiesAsUser( + any(Intent.class), anyInt(), eq(mSystemUser))) .thenReturn(mMockResolveInfoList); when(mMockUserManager.getProfileParent(UserHandle.of(privateUser.getIdentifier()))) @@ -836,7 +756,7 @@ public class UserManagerStateTest { @Test public void testGetUserIdToLabelMap_systemUserAndManagedUser_PreV() { - if (SdkLevel.isAtLeastV()) return; + assumeFalse(SdkLevel.isAtLeastV()); UserId currentUser = UserId.of(mSystemUser); initializeUserManagerState(currentUser, Lists.newArrayList(mSystemUser, mManagedUser)); if (SdkLevel.isAtLeastT()) { @@ -860,7 +780,7 @@ public class UserManagerStateTest { @Test public void testGetUserIdToLabelMap_systemUserManagedUserPrivateUser_PostV() { - if (!SdkLevel.isAtLeastV()) return; + assumeTrue(SdkLevel.isAtLeastV()); UserId currentUser = UserId.of(mSystemUser); initializeUserManagerState( currentUser, Lists.newArrayList(mSystemUser, mManagedUser, mPrivateUser)); @@ -891,7 +811,7 @@ public class UserManagerStateTest { @Test public void testGetUserIdToBadgeMap_systemUserManagedUser_PreV() { - if (SdkLevel.isAtLeastV()) return; + assumeFalse(SdkLevel.isAtLeastV()); UserId currentUser = UserId.of(mSystemUser); initializeUserManagerState(currentUser, Lists.newArrayList(mSystemUser, mManagedUser)); Drawable workBadge = mock(Drawable.class); @@ -919,7 +839,7 @@ public class UserManagerStateTest { @Test public void testGetUserIdToBadgeMap_systemUserManagedUserPrivateUser_PostV() { - if (!SdkLevel.isAtLeastV()) return; + assumeTrue(SdkLevel.isAtLeastV()); UserId currentUser = UserId.of(mSystemUser); initializeUserManagerState( currentUser, Lists.newArrayList(mSystemUser, mManagedUser, mPrivateUser)); |