Fix failing tests

Fixed failing tests in InteractAcrossProfilesDetailsTest and removed
unused code in InteractAcrossProfilesSettingsTest.

Test: atest com.android.settings.applications.specialaccess.interactacrossprofiles.InteractAcrossProfilesDetailsTest
Test: atest com.android.settings.applications.specialaccess.interactacrossprofiles.InteractAcrossProfilesSettingsTest
Fixes: 190186823
Change-Id: I52c498b9eb9c09401548e557770cd6434de49a2c
This commit is contained in:
kholoud mohamed
2021-06-08 15:35:16 +01:00
committed by Kholoud Mohamed
parent 562ad29721
commit be0a96bd23
2 changed files with 18 additions and 32 deletions

View File

@@ -20,11 +20,10 @@ import static com.google.common.truth.Truth.assertThat;
import static org.robolectric.Shadows.shadowOf; import static org.robolectric.Shadows.shadowOf;
import android.app.AppOpsManager;
import android.content.Context; import android.content.Context;
import android.content.PermissionChecker;
import android.content.pm.CrossProfileApps; import android.content.pm.CrossProfileApps;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.pm.PermissionInfo;
import android.content.pm.UserInfo; import android.content.pm.UserInfo;
import android.os.UserManager; import android.os.UserManager;
@@ -37,19 +36,18 @@ import com.google.common.collect.ImmutableList;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner; import org.robolectric.RobolectricTestRunner;
import org.robolectric.shadows.ShadowPermissionChecker;
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
public class InteractAcrossProfilesDetailsTest { public class InteractAcrossProfilesDetailsTest {
private static final int PERSONAL_PROFILE_ID = 0; private static final int PERSONAL_PROFILE_ID = 0;
private static final int WORK_PROFILE_ID = 10; private static final int WORK_PROFILE_ID = 10;
private static final int PACKAGE_UID = 0;
private static final String CROSS_PROFILE_PACKAGE_NAME = "crossProfilePackage"; private static final String CROSS_PROFILE_PACKAGE_NAME = "crossProfilePackage";
public static final String INTERACT_ACROSS_PROFILES_PERMISSION = public static final String INTERACT_ACROSS_PROFILES_PERMISSION =
"android.permission.INTERACT_ACROSS_PROFILES"; "android.permission.INTERACT_ACROSS_PROFILES";
private final Context mContext = ApplicationProvider.getApplicationContext(); private final Context mContext = ApplicationProvider.getApplicationContext();
private final AppOpsManager mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
private final PackageManager mPackageManager = mContext.getPackageManager(); private final PackageManager mPackageManager = mContext.getPackageManager();
private final UserManager mUserManager = mContext.getSystemService(UserManager.class); private final UserManager mUserManager = mContext.getSystemService(UserManager.class);
private final CrossProfileApps mCrossProfileApps = mContext.getSystemService( private final CrossProfileApps mCrossProfileApps = mContext.getSystemService(
@@ -68,10 +66,10 @@ public class InteractAcrossProfilesDetailsTest {
WORK_PROFILE_ID, ImmutableList.of(CROSS_PROFILE_PACKAGE_NAME)); WORK_PROFILE_ID, ImmutableList.of(CROSS_PROFILE_PACKAGE_NAME));
shadowOf(mCrossProfileApps).addCrossProfilePackage( shadowOf(mCrossProfileApps).addCrossProfilePackage(
CROSS_PROFILE_PACKAGE_NAME); CROSS_PROFILE_PACKAGE_NAME);
String appOp = AppOpsManager.permissionToOp(INTERACT_ACROSS_PROFILES_PERMISSION); ShadowPermissionChecker.setResult(
shadowOf(mAppOpsManager).setMode( CROSS_PROFILE_PACKAGE_NAME,
appOp, PACKAGE_UID, CROSS_PROFILE_PACKAGE_NAME, AppOpsManager.MODE_ALLOWED); INTERACT_ACROSS_PROFILES_PERMISSION,
shadowOf(mPackageManager).addPermissionInfo(createCrossProfilesPermissionInfo()); PermissionChecker.PERMISSION_GRANTED);
assertThat(InteractAcrossProfilesDetails.getPreferenceSummary( assertThat(InteractAcrossProfilesDetails.getPreferenceSummary(
mContext, CROSS_PROFILE_PACKAGE_NAME)) mContext, CROSS_PROFILE_PACKAGE_NAME))
@@ -91,11 +89,10 @@ public class InteractAcrossProfilesDetailsTest {
WORK_PROFILE_ID, ImmutableList.of(CROSS_PROFILE_PACKAGE_NAME)); WORK_PROFILE_ID, ImmutableList.of(CROSS_PROFILE_PACKAGE_NAME));
shadowOf(mCrossProfileApps).addCrossProfilePackage( shadowOf(mCrossProfileApps).addCrossProfilePackage(
CROSS_PROFILE_PACKAGE_NAME); CROSS_PROFILE_PACKAGE_NAME);
String appOp = AppOpsManager.permissionToOp(INTERACT_ACROSS_PROFILES_PERMISSION); ShadowPermissionChecker.setResult(
shadowOf(mAppOpsManager).setMode( CROSS_PROFILE_PACKAGE_NAME,
appOp, PACKAGE_UID, CROSS_PROFILE_PACKAGE_NAME, AppOpsManager.MODE_IGNORED); INTERACT_ACROSS_PROFILES_PERMISSION,
shadowOf(mPackageManager).addPermissionInfo(createCrossProfilesPermissionInfo()); PermissionChecker.PERMISSION_SOFT_DENIED);
assertThat(InteractAcrossProfilesDetails.getPreferenceSummary( assertThat(InteractAcrossProfilesDetails.getPreferenceSummary(
mContext, CROSS_PROFILE_PACKAGE_NAME)) mContext, CROSS_PROFILE_PACKAGE_NAME))
.isEqualTo(mContext.getString( .isEqualTo(mContext.getString(
@@ -114,11 +111,4 @@ public class InteractAcrossProfilesDetailsTest {
.isEqualTo(mContext.getString( .isEqualTo(mContext.getString(
R.string.interact_across_profiles_summary_not_allowed)); R.string.interact_across_profiles_summary_not_allowed));
} }
private PermissionInfo createCrossProfilesPermissionInfo() {
PermissionInfo permissionInfo = new PermissionInfo();
permissionInfo.name = INTERACT_ACROSS_PROFILES_PERMISSION;
permissionInfo.protectionLevel = PermissionInfo.PROTECTION_FLAG_APPOP;
return permissionInfo;
}
} }

View File

@@ -21,7 +21,6 @@ import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.robolectric.Shadows.shadowOf; import static org.robolectric.Shadows.shadowOf;
import android.app.AppOpsManager;
import android.content.Context; import android.content.Context;
import android.content.PermissionChecker; import android.content.PermissionChecker;
import android.content.pm.ApplicationInfo; import android.content.pm.ApplicationInfo;
@@ -51,7 +50,6 @@ public class InteractAcrossProfilesSettingsTest {
private static final int PERSONAL_PROFILE_ID = 0; private static final int PERSONAL_PROFILE_ID = 0;
private static final int WORK_PROFILE_ID = 10; private static final int WORK_PROFILE_ID = 10;
private static final int WORK_UID = UserHandle.PER_USER_RANGE * WORK_PROFILE_ID; private static final int WORK_UID = UserHandle.PER_USER_RANGE * WORK_PROFILE_ID;
private static final int PACKAGE_UID = 0;
private static final String PERSONAL_CROSS_PROFILE_PACKAGE = "personalCrossProfilePackage"; private static final String PERSONAL_CROSS_PROFILE_PACKAGE = "personalCrossProfilePackage";
private static final String PERSONAL_NON_CROSS_PROFILE_PACKAGE = private static final String PERSONAL_NON_CROSS_PROFILE_PACKAGE =
@@ -71,7 +69,6 @@ public class InteractAcrossProfilesSettingsTest {
private final UserManager mUserManager = mContext.getSystemService(UserManager.class); private final UserManager mUserManager = mContext.getSystemService(UserManager.class);
private final CrossProfileApps mCrossProfileApps = private final CrossProfileApps mCrossProfileApps =
mContext.getSystemService(CrossProfileApps.class); mContext.getSystemService(CrossProfileApps.class);
private final AppOpsManager mAppOpsManager = mContext.getSystemService(AppOpsManager.class);
@Test @Test
public void collectConfigurableApps_fromPersonal_returnsCombinedPackages() { public void collectConfigurableApps_fromPersonal_returnsCombinedPackages() {
@@ -84,8 +81,8 @@ public class InteractAcrossProfilesSettingsTest {
PERSONAL_PROFILE_ID, PERSONAL_PROFILE_INSTALLED_PACKAGES); PERSONAL_PROFILE_ID, PERSONAL_PROFILE_INSTALLED_PACKAGES);
shadowOf(mPackageManager).setInstalledPackagesForUserId( shadowOf(mPackageManager).setInstalledPackagesForUserId(
WORK_PROFILE_ID, WORK_PROFILE_INSTALLED_PACKAGES); WORK_PROFILE_ID, WORK_PROFILE_INSTALLED_PACKAGES);
installCrossProfilePackage(PERSONAL_PROFILE_ID, PERSONAL_CROSS_PROFILE_PACKAGE); installCrossProfilePackage(PERSONAL_CROSS_PROFILE_PACKAGE);
installCrossProfilePackage(WORK_PROFILE_ID, WORK_CROSS_PROFILE_PACKAGE); installCrossProfilePackage(WORK_CROSS_PROFILE_PACKAGE);
List<Pair<ApplicationInfo, UserHandle>> apps = List<Pair<ApplicationInfo, UserHandle>> apps =
InteractAcrossProfilesSettings.collectConfigurableApps( InteractAcrossProfilesSettings.collectConfigurableApps(
@@ -110,8 +107,8 @@ public class InteractAcrossProfilesSettingsTest {
PERSONAL_PROFILE_ID, PERSONAL_PROFILE_INSTALLED_PACKAGES); PERSONAL_PROFILE_ID, PERSONAL_PROFILE_INSTALLED_PACKAGES);
shadowOf(mPackageManager).setInstalledPackagesForUserId( shadowOf(mPackageManager).setInstalledPackagesForUserId(
WORK_PROFILE_ID, WORK_PROFILE_INSTALLED_PACKAGES); WORK_PROFILE_ID, WORK_PROFILE_INSTALLED_PACKAGES);
installCrossProfilePackage(PERSONAL_PROFILE_ID, PERSONAL_CROSS_PROFILE_PACKAGE); installCrossProfilePackage(PERSONAL_CROSS_PROFILE_PACKAGE);
installCrossProfilePackage(WORK_PROFILE_ID, WORK_CROSS_PROFILE_PACKAGE); installCrossProfilePackage(WORK_CROSS_PROFILE_PACKAGE);
List<Pair<ApplicationInfo, UserHandle>> apps = List<Pair<ApplicationInfo, UserHandle>> apps =
InteractAcrossProfilesSettings.collectConfigurableApps( InteractAcrossProfilesSettings.collectConfigurableApps(
@@ -130,7 +127,7 @@ public class InteractAcrossProfilesSettingsTest {
PERSONAL_PROFILE_ID, "personal-profile"/* name */, 0/* flags */); PERSONAL_PROFILE_ID, "personal-profile"/* name */, 0/* flags */);
shadowOf(mPackageManager).setInstalledPackagesForUserId( shadowOf(mPackageManager).setInstalledPackagesForUserId(
PERSONAL_PROFILE_ID, PERSONAL_PROFILE_INSTALLED_PACKAGES); PERSONAL_PROFILE_ID, PERSONAL_PROFILE_INSTALLED_PACKAGES);
installCrossProfilePackage(PERSONAL_PROFILE_ID, PERSONAL_CROSS_PROFILE_PACKAGE); installCrossProfilePackage(PERSONAL_CROSS_PROFILE_PACKAGE);
List<Pair<ApplicationInfo, UserHandle>> apps = List<Pair<ApplicationInfo, UserHandle>> apps =
InteractAcrossProfilesSettings.collectConfigurableApps( InteractAcrossProfilesSettings.collectConfigurableApps(
@@ -150,10 +147,9 @@ public class InteractAcrossProfilesSettingsTest {
PERSONAL_PROFILE_ID, PERSONAL_PROFILE_INSTALLED_PACKAGES); PERSONAL_PROFILE_ID, PERSONAL_PROFILE_INSTALLED_PACKAGES);
shadowOf(mPackageManager).setInstalledPackagesForUserId( shadowOf(mPackageManager).setInstalledPackagesForUserId(
WORK_PROFILE_ID, WORK_PROFILE_INSTALLED_PACKAGES); WORK_PROFILE_ID, WORK_PROFILE_INSTALLED_PACKAGES);
installCrossProfilePackage(PERSONAL_PROFILE_ID, PERSONAL_CROSS_PROFILE_PACKAGE); installCrossProfilePackage(PERSONAL_CROSS_PROFILE_PACKAGE);
installCrossProfilePackage(WORK_PROFILE_ID, WORK_CROSS_PROFILE_PACKAGE); installCrossProfilePackage(WORK_CROSS_PROFILE_PACKAGE);
shadowOf(mCrossProfileApps).addCrossProfilePackage(PERSONAL_CROSS_PROFILE_PACKAGE); shadowOf(mCrossProfileApps).addCrossProfilePackage(PERSONAL_CROSS_PROFILE_PACKAGE);
String appOp = AppOpsManager.permissionToOp(INTERACT_ACROSS_PROFILES_PERMISSION);
ShadowPermissionChecker.setResult( ShadowPermissionChecker.setResult(
PERSONAL_CROSS_PROFILE_PACKAGE, PERSONAL_CROSS_PROFILE_PACKAGE,
INTERACT_ACROSS_PROFILES_PERMISSION, INTERACT_ACROSS_PROFILES_PERMISSION,
@@ -169,7 +165,7 @@ public class InteractAcrossProfilesSettingsTest {
assertThat(numOfApps).isEqualTo(1); assertThat(numOfApps).isEqualTo(1);
} }
private void installCrossProfilePackage(int profileId, String packageName) { private void installCrossProfilePackage(String packageName) {
PackageInfo personalPackageInfo = shadowOf(mPackageManager).getInternalMutablePackageInfo( PackageInfo personalPackageInfo = shadowOf(mPackageManager).getInternalMutablePackageInfo(
packageName); packageName);
personalPackageInfo.requestedPermissions = new String[]{ personalPackageInfo.requestedPermissions = new String[]{