Merge changes from topics "ecm-manager", "ecm-tests" into main
* changes: Replace ECM AppOps call with service Wired remaining SpecialAppAccess for ECM.
This commit is contained in:
@@ -38,6 +38,7 @@ import androidx.test.core.app.ApplicationProvider;
|
||||
|
||||
import com.android.settings.SettingsActivity;
|
||||
import com.android.settings.testutils.shadow.ShadowDevicePolicyManager;
|
||||
import com.android.settings.testutils.shadow.ShadowRestrictedLockUtilsInternal;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
|
||||
@@ -57,7 +58,10 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/** Tests for {@link AccessibilityDetailsSettingsFragment}. */
|
||||
@Config(shadows = ShadowDevicePolicyManager.class)
|
||||
@Config(shadows = {
|
||||
ShadowDevicePolicyManager.class,
|
||||
ShadowRestrictedLockUtilsInternal.class
|
||||
})
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class AccessibilityDetailsSettingsFragmentTest {
|
||||
private static final String PACKAGE_NAME = "com.foo.bar";
|
||||
|
@@ -52,6 +52,7 @@ import com.android.settings.testutils.XmlTestUtils;
|
||||
import com.android.settings.testutils.shadow.ShadowApplicationPackageManager;
|
||||
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
|
||||
import com.android.settings.testutils.shadow.ShadowBluetoothUtils;
|
||||
import com.android.settings.testutils.shadow.ShadowRestrictedLockUtilsInternal;
|
||||
import com.android.settings.testutils.shadow.ShadowUserManager;
|
||||
import com.android.settingslib.RestrictedPreference;
|
||||
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
||||
@@ -87,6 +88,7 @@ import java.util.List;
|
||||
ShadowUserManager.class,
|
||||
ShadowColorDisplayManager.class,
|
||||
ShadowApplicationPackageManager.class,
|
||||
ShadowRestrictedLockUtilsInternal.class,
|
||||
})
|
||||
public class AccessibilitySettingsTest {
|
||||
private static final String PACKAGE_NAME = "com.android.test";
|
||||
|
@@ -31,9 +31,14 @@ import android.content.pm.ActivityInfo;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.content.pm.ServiceInfo;
|
||||
import android.platform.test.annotations.RequiresFlagsEnabled;
|
||||
import android.platform.test.flag.junit.CheckFlagsRule;
|
||||
import android.platform.test.flag.junit.DeviceFlagsValueProvider;
|
||||
import android.platform.test.flag.junit.SetFlagsRule;
|
||||
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
|
||||
import com.android.settings.testutils.shadow.ShadowRestrictedLockUtilsInternal;
|
||||
import com.android.settingslib.RestrictedPreference;
|
||||
|
||||
import org.junit.Rule;
|
||||
@@ -45,6 +50,7 @@ import org.mockito.Spy;
|
||||
import org.mockito.junit.MockitoJUnit;
|
||||
import org.mockito.junit.MockitoRule;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.xmlpull.v1.XmlPullParserException;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -53,6 +59,9 @@ import java.util.List;
|
||||
|
||||
/** Test for {@link RestrictedPreferenceHelper}. */
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@Config(shadows = {
|
||||
ShadowRestrictedLockUtilsInternal.class
|
||||
})
|
||||
public class RestrictedPreferenceHelperTest {
|
||||
|
||||
private static final String PACKAGE_NAME = "com.android.test";
|
||||
@@ -72,6 +81,11 @@ public class RestrictedPreferenceHelperTest {
|
||||
private AccessibilityShortcutInfo mShortcutInfo;
|
||||
private final RestrictedPreferenceHelper mHelper = new RestrictedPreferenceHelper(mContext);
|
||||
|
||||
@Rule
|
||||
public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
|
||||
@Rule
|
||||
public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();
|
||||
|
||||
@Test
|
||||
public void createAccessibilityServicePreferenceList_hasOneInfo_containsSameKey() {
|
||||
final String key = COMPONENT_NAME.flattenToString();
|
||||
@@ -85,6 +99,37 @@ public class RestrictedPreferenceHelperTest {
|
||||
assertThat(preference.getKey()).isEqualTo(key);
|
||||
}
|
||||
|
||||
@Test
|
||||
@RequiresFlagsEnabled(value = {android.security.Flags.FLAG_EXTEND_ECM_TO_ALL_SETTINGS,
|
||||
android.permission.flags.Flags.FLAG_ENHANCED_CONFIRMATION_MODE_APIS_ENABLED})
|
||||
public void createAccessibilityServicePreferenceList_ecmRestricted_prefIsEcmRestricted() {
|
||||
ShadowRestrictedLockUtilsInternal.setEcmRestrictedPkgs(
|
||||
mServiceInfo.getResolveInfo().serviceInfo.packageName);
|
||||
final List<AccessibilityServiceInfo> infoList = new ArrayList<>(
|
||||
singletonList(mServiceInfo));
|
||||
|
||||
final List<RestrictedPreference> preferenceList =
|
||||
mHelper.createAccessibilityServicePreferenceList(infoList);
|
||||
final RestrictedPreference preference = preferenceList.get(0);
|
||||
|
||||
assertThat(preference.isDisabledByEcm()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@RequiresFlagsEnabled(value = {android.security.Flags.FLAG_EXTEND_ECM_TO_ALL_SETTINGS,
|
||||
android.permission.flags.Flags.FLAG_ENHANCED_CONFIRMATION_MODE_APIS_ENABLED})
|
||||
public void createAccessibilityServicePreferenceList_ecmNotRestricted_prefIsNotEcmRestricted() {
|
||||
ShadowRestrictedLockUtilsInternal.setEcmRestrictedPkgs();
|
||||
final List<AccessibilityServiceInfo> infoList = new ArrayList<>(
|
||||
singletonList(mServiceInfo));
|
||||
|
||||
final List<RestrictedPreference> preferenceList =
|
||||
mHelper.createAccessibilityServicePreferenceList(infoList);
|
||||
final RestrictedPreference preference = preferenceList.get(0);
|
||||
|
||||
assertThat(preference.isDisabledByEcm()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createAccessibilityActivityPreferenceList_hasOneInfo_containsSameKey() {
|
||||
final String key = COMPONENT_NAME.flattenToString();
|
||||
|
@@ -16,13 +16,35 @@
|
||||
|
||||
package com.android.settings.applications.specialaccess.premiumsms;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.doNothing;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import android.app.settings.SettingsEnums;
|
||||
import android.content.Context;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.os.Process;
|
||||
import android.telephony.SmsManager;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
|
||||
import androidx.preference.PreferenceManager;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
import androidx.preference.PreferenceViewHolder;
|
||||
import androidx.preference.R;
|
||||
|
||||
import com.android.internal.logging.nano.MetricsProto;
|
||||
import com.android.settings.datausage.AppStateDataUsageBridge;
|
||||
import com.android.settings.testutils.FakeFeatureFactory;
|
||||
import com.android.settings.testutils.shadow.ShadowRestrictedLockUtilsInternal;
|
||||
import com.android.settingslib.RestrictedDropDownPreference;
|
||||
import com.android.settingslib.applications.ApplicationsState;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -30,19 +52,28 @@ import org.junit.runner.RunWith;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.robolectric.util.ReflectionHelpers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@Config(shadows = {
|
||||
ShadowRestrictedLockUtilsInternal.class
|
||||
})
|
||||
public class PremiumSmsAccessTest {
|
||||
|
||||
private FakeFeatureFactory mFeatureFactory;
|
||||
private PremiumSmsAccess mFragment;
|
||||
private Context mContext;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mFeatureFactory = FakeFeatureFactory.setupForTest();
|
||||
mFragment = new PremiumSmsAccess();
|
||||
mFragment.onAttach(RuntimeEnvironment.application);
|
||||
mContext = RuntimeEnvironment.application;
|
||||
mFragment.onAttach(mContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -74,4 +105,89 @@ public class PremiumSmsAccessTest {
|
||||
"app",
|
||||
SmsManager.PREMIUM_SMS_CONSENT_ALWAYS_ALLOW);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onRebuildComplete_ecmRestricted_shouldBeDisabled() {
|
||||
mFragment = spy(mFragment);
|
||||
mContext = spy(mContext);
|
||||
LayoutInflater inflater = LayoutInflater.from(mContext);
|
||||
View view = inflater.inflate(R.layout.preference_dropdown, null);
|
||||
PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(view);
|
||||
|
||||
PreferenceManager preferenceManager = new PreferenceManager(mContext);
|
||||
PreferenceScreen preferenceScreen = spy(preferenceManager.createPreferenceScreen(mContext));
|
||||
doReturn(preferenceManager).when(mFragment).getPreferenceManager();
|
||||
doReturn(preferenceScreen).when(mFragment).getPreferenceScreen();
|
||||
final String testPkg = "com.example.disabled";
|
||||
doNothing().when(mContext).startActivity(any());
|
||||
ShadowRestrictedLockUtilsInternal.setEcmRestrictedPkgs(testPkg);
|
||||
|
||||
doAnswer((invocation) -> {
|
||||
final RestrictedDropDownPreference preference = invocation.getArgument(0);
|
||||
// Verify preference is disabled by ecm and the summary is changed accordingly.
|
||||
assertThat(preference.isDisabledByEcm()).isTrue();
|
||||
assertThat(preference.getSummary().toString()).isEqualTo(
|
||||
mContext.getString(
|
||||
com.android.settingslib.R.string.disabled_by_app_ops_text));
|
||||
preference.onBindViewHolder(holder);
|
||||
preference.performClick();
|
||||
// Verify that when the preference is clicked, ecm details intent is launched
|
||||
verify(mContext).startActivity(any());
|
||||
|
||||
return null;
|
||||
}).when(preferenceScreen).addPreference(any(RestrictedDropDownPreference.class));
|
||||
|
||||
mFragment.onRebuildComplete(createAppEntries(testPkg));
|
||||
verify(preferenceScreen).addPreference(any(RestrictedDropDownPreference.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onRebuildComplete_ecmNotRestricted_notDisabled() {
|
||||
mFragment = spy(mFragment);
|
||||
mContext = spy(mContext);
|
||||
LayoutInflater inflater = LayoutInflater.from(mContext);
|
||||
View view = inflater.inflate(R.layout.preference_dropdown, null);
|
||||
PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(view);
|
||||
|
||||
PreferenceManager preferenceManager = new PreferenceManager(mContext);
|
||||
PreferenceScreen preferenceScreen = spy(preferenceManager.createPreferenceScreen(mContext));
|
||||
doReturn(preferenceManager).when(mFragment).getPreferenceManager();
|
||||
doReturn(preferenceScreen).when(mFragment).getPreferenceScreen();
|
||||
final String testPkg = "com.example.enabled";
|
||||
ShadowRestrictedLockUtilsInternal.setEcmRestrictedPkgs();
|
||||
|
||||
|
||||
doAnswer((invocation) -> {
|
||||
final RestrictedDropDownPreference preference = invocation.getArgument(0);
|
||||
assertThat(preference.isDisabledByEcm()).isFalse();
|
||||
assertThat(preference.getSummary().toString()).isEqualTo("");
|
||||
preference.onBindViewHolder(holder);
|
||||
preference.performClick();
|
||||
// Verify that when the preference is clicked, ecm details intent is not launched
|
||||
verify(mContext, never()).startActivity(any());
|
||||
|
||||
return null;
|
||||
}).when(preferenceScreen).addPreference(any(RestrictedDropDownPreference.class));
|
||||
|
||||
mFragment.onRebuildComplete(createAppEntries(testPkg));
|
||||
verify(preferenceScreen).addPreference(any(RestrictedDropDownPreference.class));
|
||||
}
|
||||
|
||||
private ArrayList<ApplicationsState.AppEntry> createAppEntries(String... packageNames) {
|
||||
final ArrayList<ApplicationsState.AppEntry> appEntries = new ArrayList<>();
|
||||
for (int i = 0; i < packageNames.length; ++i) {
|
||||
final ApplicationInfo info = new ApplicationInfo();
|
||||
info.packageName = packageNames[i];
|
||||
info.uid = Process.FIRST_APPLICATION_UID + i;
|
||||
info.sourceDir = info.packageName;
|
||||
final ApplicationsState.AppEntry appEntry =
|
||||
spy(new ApplicationsState.AppEntry(mContext, info, i));
|
||||
appEntry.extraInfo = new AppStateDataUsageBridge
|
||||
.DataUsageState(false, false);
|
||||
doNothing().when(appEntry).ensureLabel(any(Context.class));
|
||||
ReflectionHelpers.setField(appEntry, "info", info);
|
||||
appEntries.add(appEntry);
|
||||
}
|
||||
return appEntries;
|
||||
}
|
||||
}
|
||||
|
@@ -25,6 +25,7 @@ import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.doNothing;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
@@ -163,6 +164,81 @@ public class UnrestrictedDataAccessPreferenceControllerTest {
|
||||
mController.onRebuildComplete(createAppEntries(testPkg1, testPkg2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onRebuildComplete_ecmRestricted_shouldBeDisabled() {
|
||||
mFragment = spy(new UnrestrictedDataAccess());
|
||||
mContext = spy(mContext);
|
||||
doNothing().when(mFragment).setLoading(anyBoolean(), anyBoolean());
|
||||
mController.setParentFragment(mFragment);
|
||||
mPreferenceManager = new PreferenceManager(mContext);
|
||||
mPreferenceScreen = spy(mPreferenceManager.createPreferenceScreen(mContext));
|
||||
doReturn(mPreferenceManager).when(mFragment).getPreferenceManager();
|
||||
doReturn(mPreferenceScreen).when(mFragment).getPreferenceScreen();
|
||||
doReturn(0).when(mPreferenceScreen).getPreferenceCount();
|
||||
final DataSaverBackend dataSaverBackend = mock(DataSaverBackend.class);
|
||||
ReflectionHelpers.setField(mController, "mDataSaverBackend", dataSaverBackend);
|
||||
ReflectionHelpers.setField(mController, "mScreen", mPreferenceScreen);
|
||||
|
||||
final String testPkg = "com.example.disabled";
|
||||
doNothing().when(mContext).startActivity(any());
|
||||
ShadowRestrictedLockUtilsInternal.setEcmRestrictedPkgs(testPkg);
|
||||
|
||||
doAnswer((invocation) -> {
|
||||
final UnrestrictedDataAccessPreference preference = invocation.getArgument(0);
|
||||
// Verify preference is disabled by ecm and the summary is changed accordingly.
|
||||
assertThat(preference.isDisabledByEcm()).isTrue();
|
||||
assertThat(preference.getSummary().toString()).isEqualTo(
|
||||
mContext.getString(
|
||||
com.android.settingslib.R.string.disabled_by_app_ops_text));
|
||||
assertThat(preference.isChecked()).isFalse();
|
||||
preference.performClick();
|
||||
// Verify that when the preference is clicked, ecm details intent is launched
|
||||
assertThat(preference.isChecked()).isFalse();
|
||||
verify(mContext).startActivity(any());
|
||||
|
||||
return null;
|
||||
}).when(mPreferenceScreen).addPreference(any(UnrestrictedDataAccessPreference.class));
|
||||
|
||||
mController.onRebuildComplete(createAppEntries(testPkg));
|
||||
verify(mPreferenceScreen).addPreference(any(UnrestrictedDataAccessPreference.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onRebuildComplete_ecmNotRestricted_notDisabled() {
|
||||
mFragment = spy(new UnrestrictedDataAccess());
|
||||
mContext = spy(mContext);
|
||||
doNothing().when(mFragment).setLoading(anyBoolean(), anyBoolean());
|
||||
mController.setParentFragment(mFragment);
|
||||
mPreferenceManager = new PreferenceManager(mContext);
|
||||
mPreferenceScreen = spy(mPreferenceManager.createPreferenceScreen(mContext));
|
||||
doReturn(mPreferenceManager).when(mFragment).getPreferenceManager();
|
||||
doReturn(mPreferenceScreen).when(mFragment).getPreferenceScreen();
|
||||
doReturn(0).when(mPreferenceScreen).getPreferenceCount();
|
||||
final DataSaverBackend dataSaverBackend = mock(DataSaverBackend.class);
|
||||
ReflectionHelpers.setField(mController, "mDataSaverBackend", dataSaverBackend);
|
||||
ReflectionHelpers.setField(mController, "mScreen", mPreferenceScreen);
|
||||
|
||||
final String testPkg = "com.example.enabled";
|
||||
doNothing().when(mContext).startActivity(any());
|
||||
ShadowRestrictedLockUtilsInternal.setEcmRestrictedPkgs();
|
||||
|
||||
doAnswer((invocation) -> {
|
||||
final UnrestrictedDataAccessPreference preference = invocation.getArgument(0);
|
||||
assertThat(preference.isDisabledByEcm()).isFalse();
|
||||
assertThat(preference.getSummary()).isEqualTo("");
|
||||
assertThat(preference.isChecked()).isFalse();
|
||||
preference.performClick();
|
||||
// Verify that when the preference is clicked, ecm details intent is not launched
|
||||
assertThat(preference.isChecked()).isTrue();
|
||||
verify(mContext, never()).startActivity(any());
|
||||
|
||||
return null;
|
||||
}).when(mPreferenceScreen).addPreference(any(UnrestrictedDataAccessPreference.class));
|
||||
|
||||
mController.onRebuildComplete(createAppEntries(testPkg));
|
||||
verify(mPreferenceScreen).addPreference(any(UnrestrictedDataAccessPreference.class));
|
||||
}
|
||||
|
||||
private ArrayList<AppEntry> createAppEntries(String... packageNames) {
|
||||
final ArrayList<AppEntry> appEntries = new ArrayList<>();
|
||||
for (int i = 0; i < packageNames.length; ++i) {
|
||||
|
@@ -15,9 +15,11 @@
|
||||
*/
|
||||
package com.android.settings.testutils.shadow;
|
||||
|
||||
import android.annotation.NonNull;
|
||||
import android.annotation.UserIdInt;
|
||||
import android.app.admin.DevicePolicyManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
|
||||
import com.android.internal.util.ArrayUtils;
|
||||
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
|
||||
@@ -38,6 +40,8 @@ public class ShadowRestrictedLockUtilsInternal {
|
||||
private static DevicePolicyManager sDevicePolicyManager;
|
||||
private static String[] sDisabledTypes;
|
||||
private static int sKeyguardDisabledFeatures;
|
||||
private static String[] sEcmRestrictedPkgs;
|
||||
private static boolean sAccessibilityServiceRestricted;
|
||||
|
||||
@Resetter
|
||||
public static void reset() {
|
||||
@@ -47,6 +51,8 @@ public class ShadowRestrictedLockUtilsInternal {
|
||||
sDisabledTypes = new String[0];
|
||||
sMaximumTimeToLockIsSet = false;
|
||||
sMteOverridden = false;
|
||||
sEcmRestrictedPkgs = new String[0];
|
||||
sAccessibilityServiceRestricted = false;
|
||||
}
|
||||
|
||||
@Implementation
|
||||
@@ -108,10 +114,35 @@ public class ShadowRestrictedLockUtilsInternal {
|
||||
return sMteOverridden ? new EnforcedAdmin() : null;
|
||||
}
|
||||
|
||||
public static EnforcedAdmin checkIfAccessibilityServiceDisallowed(Context context,
|
||||
String packageName, int userId) {
|
||||
return sAccessibilityServiceRestricted ? new EnforcedAdmin() : null;
|
||||
}
|
||||
|
||||
@Implementation
|
||||
public static Intent checkIfRequiresEnhancedConfirmation(@NonNull Context context,
|
||||
@NonNull String settingIdentifier, @NonNull String packageName) {
|
||||
if (ArrayUtils.contains(sEcmRestrictedPkgs, packageName)) {
|
||||
return new Intent();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Implementation
|
||||
public static boolean isEnhancedConfirmationRestricted(@NonNull Context context,
|
||||
@NonNull String settingIdentifier, @NonNull String packageName) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void setRestricted(boolean restricted) {
|
||||
sIsRestricted = restricted;
|
||||
}
|
||||
|
||||
public static void setEcmRestrictedPkgs(String... pkgs) {
|
||||
sEcmRestrictedPkgs = pkgs;
|
||||
}
|
||||
|
||||
public static void setRestrictedPkgs(String... pkgs) {
|
||||
sRestrictedPkgs = pkgs;
|
||||
}
|
||||
|
Reference in New Issue
Block a user