fix TrustAgentsPreferenceControllerTest
Removed non-platform Robolectric shadow in favor of setting up fake package/services directly with ShadowApplicationPackageManager. Bug: 313612480 Test: atest "SettingsRoboTests:TrustAgentsPreferenceControllerTest" --host Flag: TEST_ONLY Change-Id: Iadcb5bcb977201e2f394b699621bfb34657a7820
This commit is contained in:
@@ -21,14 +21,10 @@ import static com.google.common.truth.Truth.assertThat;
|
|||||||
import android.app.admin.DevicePolicyManager;
|
import android.app.admin.DevicePolicyManager;
|
||||||
import android.content.ComponentName;
|
import android.content.ComponentName;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.IntentFilter;
|
||||||
import android.content.pm.ApplicationInfo;
|
import android.content.pm.PackageInfo;
|
||||||
import android.content.pm.PackageManager;
|
|
||||||
import android.content.pm.ResolveInfo;
|
|
||||||
import android.content.pm.ServiceInfo;
|
|
||||||
import android.service.trust.TrustAgentService;
|
import android.service.trust.TrustAgentService;
|
||||||
|
|
||||||
import android.text.TextUtils;
|
|
||||||
import androidx.preference.PreferenceManager;
|
import androidx.preference.PreferenceManager;
|
||||||
import androidx.preference.PreferenceScreen;
|
import androidx.preference.PreferenceScreen;
|
||||||
|
|
||||||
@@ -38,34 +34,30 @@ import com.android.settings.testutils.shadow.ShadowLockPatternUtils;
|
|||||||
import com.android.settings.testutils.shadow.ShadowRestrictedLockUtilsInternal;
|
import com.android.settings.testutils.shadow.ShadowRestrictedLockUtilsInternal;
|
||||||
import com.android.settingslib.RestrictedSwitchPreference;
|
import com.android.settingslib.RestrictedSwitchPreference;
|
||||||
|
|
||||||
import org.junit.After;
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Ignore;
|
|
||||||
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.RuntimeEnvironment;
|
import org.robolectric.RuntimeEnvironment;
|
||||||
import org.robolectric.Shadows;
|
import org.robolectric.Shadows;
|
||||||
import org.robolectric.annotation.Config;
|
import org.robolectric.annotation.Config;
|
||||||
import org.robolectric.annotation.Implementation;
|
|
||||||
import org.robolectric.annotation.Implements;
|
|
||||||
import org.robolectric.shadows.ShadowApplicationPackageManager;
|
import org.robolectric.shadows.ShadowApplicationPackageManager;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
@Config(shadows = {
|
@Config(shadows = {
|
||||||
ShadowLockPatternUtils.class,
|
ShadowLockPatternUtils.class,
|
||||||
ShadowRestrictedLockUtilsInternal.class,
|
ShadowRestrictedLockUtilsInternal.class,
|
||||||
ShadowDevicePolicyManager.class,
|
ShadowDevicePolicyManager.class, ShadowApplicationPackageManager.class
|
||||||
ShadowApplicationPackageManager.class,
|
|
||||||
TrustAgentsPreferenceControllerTest.ShadowTrustAgentManager.class
|
|
||||||
})
|
})
|
||||||
public class TrustAgentsPreferenceControllerTest {
|
public class TrustAgentsPreferenceControllerTest {
|
||||||
|
private static final ComponentName TRUST_AGENT_A = new ComponentName(
|
||||||
private static final Intent TEST_INTENT =
|
"test.data.packageA", "clzAAA");
|
||||||
new Intent(TrustAgentService.SERVICE_INTERFACE);
|
private static final ComponentName TRUST_AGENT_B = new ComponentName(
|
||||||
|
"test.data.packageB", "clzBBB");
|
||||||
|
private static final ComponentName TRUST_AGENT_C = new ComponentName(
|
||||||
|
"test.data.packageC", "clzCCC");
|
||||||
|
private static final ComponentName TRUST_AGENT_D = new ComponentName(
|
||||||
|
"test.data.packageD", "clzDDD");
|
||||||
|
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
private ShadowApplicationPackageManager mPackageManager;
|
private ShadowApplicationPackageManager mPackageManager;
|
||||||
@@ -84,11 +76,6 @@ public class TrustAgentsPreferenceControllerTest {
|
|||||||
mPreferenceScreen.setKey("pref_key");
|
mPreferenceScreen.setKey("pref_key");
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
|
||||||
public void tearDown() {
|
|
||||||
ShadowTrustAgentManager.clearPermissionGrantedList();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getAvailabilityStatus_byDefault_shouldBeShown() {
|
public void getAvailabilityStatus_byDefault_shouldBeShown() {
|
||||||
assertThat(mController.getAvailabilityStatus())
|
assertThat(mController.getAvailabilityStatus())
|
||||||
@@ -97,8 +84,7 @@ public class TrustAgentsPreferenceControllerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void onStart_noTrustAgent_shouldNotAddPreference() {
|
public void onStart_noTrustAgent_shouldNotAddPreference() {
|
||||||
final List<ResolveInfo> availableAgents = createFakeAvailableAgents();
|
installFakeAvailableAgents(/* grantPermission= */ false);
|
||||||
mPackageManager.setResolveInfosForIntent(TEST_INTENT, availableAgents);
|
|
||||||
|
|
||||||
mController.displayPreference(mPreferenceScreen);
|
mController.displayPreference(mPreferenceScreen);
|
||||||
mController.onStart();
|
mController.onStart();
|
||||||
@@ -106,57 +92,34 @@ public class TrustAgentsPreferenceControllerTest {
|
|||||||
assertThat(mPreferenceScreen.getPreferenceCount()).isEqualTo(0);
|
assertThat(mPreferenceScreen.getPreferenceCount()).isEqualTo(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Ignore("b/313612480")
|
|
||||||
@Test
|
@Test
|
||||||
public void
|
public void onStart_uninstalledTrustAgent_shouldRemoveOnePreferenceAndLeaveTwoPreferences() {
|
||||||
onStart_hasAUninstalledTrustAgent_shouldRemoveOnePreferenceAndLeaveTwoPreferences() {
|
installFakeAvailableAgents(/* grantPermission= */ true);
|
||||||
final List<ResolveInfo> availableAgents = createFakeAvailableAgents();
|
|
||||||
final ResolveInfo uninstalledTrustAgent = availableAgents.get(0);
|
|
||||||
|
|
||||||
for (ResolveInfo rInfo : availableAgents) {
|
|
||||||
ShadowTrustAgentManager.grantPermissionToResolveInfo(rInfo);
|
|
||||||
}
|
|
||||||
mPackageManager.setResolveInfosForIntent(TEST_INTENT, availableAgents);
|
|
||||||
mController.displayPreference(mPreferenceScreen);
|
mController.displayPreference(mPreferenceScreen);
|
||||||
mController.onStart();
|
mController.onStart();
|
||||||
availableAgents.remove(uninstalledTrustAgent);
|
uninstallAgent(TRUST_AGENT_A);
|
||||||
|
|
||||||
mPackageManager.setResolveInfosForIntent(TEST_INTENT, availableAgents);
|
|
||||||
mController.onStart();
|
mController.onStart();
|
||||||
|
|
||||||
assertThat(mPreferenceScreen.getPreferenceCount()).isEqualTo(2);
|
assertThat(mPreferenceScreen.getPreferenceCount()).isEqualTo(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Ignore("b/313612480")
|
|
||||||
@Test
|
@Test
|
||||||
public void onStart_hasANewTrustAgent_shouldAddOnePreferenceAndHaveFourPreferences() {
|
public void onStart_hasANewTrustAgent_shouldAddOnePreferenceAndHaveFourPreferences() {
|
||||||
final List<ResolveInfo> availableAgents = createFakeAvailableAgents();
|
installFakeAvailableAgents(/* grantPermission= */ true);
|
||||||
final ComponentName newComponentName = new ComponentName("test.data.packageD", "clzDDD");
|
|
||||||
final ResolveInfo newTrustAgent = createFakeResolveInfo(newComponentName);
|
|
||||||
for (ResolveInfo rInfo : availableAgents) {
|
|
||||||
ShadowTrustAgentManager.grantPermissionToResolveInfo(rInfo);
|
|
||||||
}
|
|
||||||
mPackageManager.setResolveInfosForIntent(TEST_INTENT, availableAgents);
|
|
||||||
mController.displayPreference(mPreferenceScreen);
|
mController.displayPreference(mPreferenceScreen);
|
||||||
mController.onStart();
|
mController.onStart();
|
||||||
availableAgents.add(newTrustAgent);
|
installFakeAvailableAgent(TRUST_AGENT_D, /* grantPermission= */ true);
|
||||||
ShadowTrustAgentManager.grantPermissionToResolveInfo(newTrustAgent);
|
|
||||||
|
|
||||||
mPackageManager.setResolveInfosForIntent(TEST_INTENT, availableAgents);
|
|
||||||
mController.onStart();
|
mController.onStart();
|
||||||
|
|
||||||
assertThat(mPreferenceScreen.getPreferenceCount()).isEqualTo(4);
|
assertThat(mPreferenceScreen.getPreferenceCount()).isEqualTo(4);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Ignore("b/313612480")
|
|
||||||
@Test
|
@Test
|
||||||
public void onStart_hasUnrestrictedTrustAgent_shouldAddThreeChangeablePreferences() {
|
public void onStart_hasUnrestrictedTrustAgent_shouldAddThreeChangeablePreferences() {
|
||||||
ShadowRestrictedLockUtilsInternal.setKeyguardDisabledFeatures(0);
|
ShadowRestrictedLockUtilsInternal.setKeyguardDisabledFeatures(0);
|
||||||
final List<ResolveInfo> availableAgents = createFakeAvailableAgents();
|
installFakeAvailableAgents(/* grantPermission= */ true);
|
||||||
for (ResolveInfo rInfo : availableAgents) {
|
|
||||||
ShadowTrustAgentManager.grantPermissionToResolveInfo(rInfo);
|
|
||||||
}
|
|
||||||
mPackageManager.setResolveInfosForIntent(TEST_INTENT, availableAgents);
|
|
||||||
|
|
||||||
mController.displayPreference(mPreferenceScreen);
|
mController.displayPreference(mPreferenceScreen);
|
||||||
mController.onStart();
|
mController.onStart();
|
||||||
@@ -169,14 +132,9 @@ public class TrustAgentsPreferenceControllerTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Ignore("b/313612480")
|
|
||||||
@Test
|
@Test
|
||||||
public void onStart_hasRestrictedTructAgent_shouldAddThreeUnchangeablePreferences() {
|
public void onStart_hasRestrictedTrustAgent_shouldAddThreeUnchangeablePreferences() {
|
||||||
final List<ResolveInfo> availableAgents = createFakeAvailableAgents();
|
installFakeAvailableAgents(/* grantPermission= */ true);
|
||||||
for (ResolveInfo rInfo : availableAgents) {
|
|
||||||
ShadowTrustAgentManager.grantPermissionToResolveInfo(rInfo);
|
|
||||||
}
|
|
||||||
mPackageManager.setResolveInfosForIntent(TEST_INTENT, availableAgents);
|
|
||||||
ShadowRestrictedLockUtilsInternal.setKeyguardDisabledFeatures(
|
ShadowRestrictedLockUtilsInternal.setKeyguardDisabledFeatures(
|
||||||
DevicePolicyManager.KEYGUARD_DISABLE_TRUST_AGENTS);
|
DevicePolicyManager.KEYGUARD_DISABLE_TRUST_AGENTS);
|
||||||
|
|
||||||
@@ -191,51 +149,30 @@ public class TrustAgentsPreferenceControllerTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<ResolveInfo> createFakeAvailableAgents() {
|
private void installFakeAvailableAgents(boolean grantPermission) {
|
||||||
final List<ComponentName> componentNames = new ArrayList<>();
|
installFakeAvailableAgent(TRUST_AGENT_A, grantPermission);
|
||||||
componentNames.add(new ComponentName("test.data.packageA", "clzAAA"));
|
installFakeAvailableAgent(TRUST_AGENT_B, grantPermission);
|
||||||
componentNames.add(new ComponentName("test.data.packageB", "clzBBB"));
|
installFakeAvailableAgent(TRUST_AGENT_C, grantPermission);
|
||||||
componentNames.add(new ComponentName("test.data.packageC", "clzCCC"));
|
|
||||||
final List<ResolveInfo> result = new ArrayList<>();
|
|
||||||
for (ComponentName cn : componentNames) {
|
|
||||||
final ResolveInfo ri = createFakeResolveInfo(cn);
|
|
||||||
result.add(ri);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private ResolveInfo createFakeResolveInfo(ComponentName cn) {
|
private void installFakeAvailableAgent(ComponentName name,
|
||||||
final ResolveInfo ri = new ResolveInfo();
|
boolean grantPermission) {
|
||||||
ri.serviceInfo = new ServiceInfo();
|
mPackageManager.addServiceIfNotPresent(name);
|
||||||
ri.serviceInfo.packageName = cn.getPackageName();
|
mPackageManager.addIntentFilterForService(name,
|
||||||
ri.serviceInfo.name = cn.getClassName();
|
new IntentFilter(TrustAgentService.SERVICE_INTERFACE));
|
||||||
ri.serviceInfo.applicationInfo = new ApplicationInfo();
|
if (!grantPermission) {
|
||||||
ri.serviceInfo.applicationInfo.packageName = cn.getPackageName();
|
return;
|
||||||
ri.serviceInfo.applicationInfo.name = cn.getClassName();
|
}
|
||||||
return ri;
|
PackageInfo pkgInfo = mPackageManager.getInternalMutablePackageInfo(
|
||||||
|
name.getPackageName());
|
||||||
|
pkgInfo.requestedPermissions =
|
||||||
|
new String[]{android.Manifest.permission.PROVIDE_TRUST_AGENT};
|
||||||
|
pkgInfo.requestedPermissionsFlags =
|
||||||
|
new int[]{PackageInfo.REQUESTED_PERMISSION_GRANTED};
|
||||||
}
|
}
|
||||||
|
|
||||||
@Implements(TrustAgentManager.class)
|
private void uninstallAgent(ComponentName name) {
|
||||||
public static class ShadowTrustAgentManager {
|
mPackageManager.removeService(name);
|
||||||
private final static List<ResolveInfo> sPermissionGrantedList = new ArrayList<>();
|
mPackageManager.removePackage(name.getPackageName());
|
||||||
|
|
||||||
@Implementation
|
|
||||||
protected boolean shouldProvideTrust(ResolveInfo resolveInfo, PackageManager pm) {
|
|
||||||
for (ResolveInfo info : sPermissionGrantedList) {
|
|
||||||
if (info.serviceInfo.equals(resolveInfo.serviceInfo)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void grantPermissionToResolveInfo(ResolveInfo rInfo) {
|
|
||||||
sPermissionGrantedList.add(rInfo);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void clearPermissionGrantedList() {
|
|
||||||
sPermissionGrantedList.clear();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user