Merge "Add policy transparency for metered data related settings."

This commit is contained in:
Sudheer Shanka
2018-01-29 22:01:35 +00:00
committed by Android (Google) Code Review
6 changed files with 267 additions and 21 deletions

View File

@@ -29,8 +29,8 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.pm.PackageManager;
import android.net.NetworkPolicyManager;
import android.os.Bundle;
import android.support.v14.preference.SwitchPreference;
import android.support.v7.preference.PreferenceManager;
import android.support.v7.preference.PreferenceScreen;
import android.util.ArraySet;
@@ -40,8 +40,11 @@ import com.android.settings.TestConfig;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.ShadowEntityHeaderController;
import com.android.settings.testutils.shadow.ShadowRestrictedLockUtils;
import com.android.settings.widget.EntityHeaderController;
import com.android.settingslib.AppItem;
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
import com.android.settingslib.RestrictedSwitchPreference;
import com.android.settingslib.wrapper.PackageManagerWrapper;
import org.junit.After;
@@ -57,7 +60,10 @@ import org.robolectric.util.ReflectionHelpers;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION,
shadows = ShadowEntityHeaderController.class)
shadows = {
ShadowEntityHeaderController.class,
ShadowRestrictedLockUtils.class
})
public class AppDataUsageTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
@@ -134,7 +140,7 @@ public class AppDataUsageTest {
public void changePreference_backgroundData_shouldUpdateUI() {
mFragment = spy(new AppDataUsage());
final AppItem appItem = new AppItem(123456789);
final SwitchPreference pref = mock(SwitchPreference.class);
final RestrictedSwitchPreference pref = mock(RestrictedSwitchPreference.class);
final DataSaverBackend dataSaverBackend = mock(DataSaverBackend.class);
ReflectionHelpers.setField(mFragment, "mAppItem", appItem);
ReflectionHelpers.setField(mFragment, "mRestrictBackground", pref);
@@ -146,4 +152,31 @@ public class AppDataUsageTest {
verify(mFragment).updatePrefs();
}
@Test
public void updatePrefs_restrictedByAdmin_shouldDisablePreference() {
mFragment = spy(new AppDataUsage());
final int testUid = 123123;
final AppItem appItem = new AppItem(testUid);
final RestrictedSwitchPreference restrictBackgroundPref
= mock(RestrictedSwitchPreference.class);
final RestrictedSwitchPreference unrestrictedDataPref
= mock(RestrictedSwitchPreference.class);
final DataSaverBackend dataSaverBackend = mock(DataSaverBackend.class);
final NetworkPolicyManager networkPolicyManager = mock(NetworkPolicyManager.class);
ReflectionHelpers.setField(mFragment, "mAppItem", appItem);
ReflectionHelpers.setField(mFragment, "mRestrictBackground", restrictBackgroundPref);
ReflectionHelpers.setField(mFragment, "mUnrestrictedData", unrestrictedDataPref);
ReflectionHelpers.setField(mFragment, "mDataSaverBackend", dataSaverBackend);
ReflectionHelpers.setField(mFragment.services, "mPolicyManager", networkPolicyManager);
ShadowRestrictedLockUtils.setRestricted(true);
doReturn(NetworkPolicyManager.POLICY_NONE).when(networkPolicyManager)
.getUidPolicy(testUid);
mFragment.updatePrefs();
verify(restrictBackgroundPref).setDisabledByAdmin(any(EnforcedAdmin.class));
verify(unrestrictedDataPref).setDisabledByAdmin(any(EnforcedAdmin.class));
}
}

View File

@@ -16,41 +16,68 @@
package com.android.settings.datausage;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.os.Process;
import android.support.v7.preference.PreferenceManager;
import android.support.v7.preference.PreferenceScreen;
import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.R;
import com.android.settings.TestConfig;
import com.android.settings.datausage.AppStateDataUsageBridge.DataUsageState;
import com.android.settings.datausage.UnrestrictedDataAccess.AccessPreference;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settingslib.applications.ApplicationsState;
import com.android.settings.testutils.shadow.ShadowRestrictedLockUtils;
import com.android.settingslib.applications.ApplicationsState.AppEntry;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;
import java.util.ArrayList;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION,
shadows = {
ShadowRestrictedLockUtils.class
})
public class UnrestrictedDataAccessTest {
@Mock
private ApplicationsState.AppEntry mAppEntry;
private AppEntry mAppEntry;
private UnrestrictedDataAccess mFragment;
private FakeFeatureFactory mFeatureFactory;
@Mock
private PreferenceScreen mPreferenceScreen;
@Mock
private PreferenceManager mPreferenceManager;
@Mock
private DataSaverBackend mDataSaverBackend;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mFeatureFactory = FakeFeatureFactory.setupForTest();
mFragment = new UnrestrictedDataAccess();
mFragment = spy(new UnrestrictedDataAccess());
}
@Test
@@ -80,4 +107,66 @@ public class UnrestrictedDataAccessTest {
eq(MetricsProto.MetricsEvent.APP_SPECIAL_PERMISSION_UNL_DATA_DENY), eq("app"));
}
@Test
public void testOnRebuildComplete_restricted_shouldBeDisabled() {
final Context context = RuntimeEnvironment.application;
doReturn(context).when(mFragment).getContext();
doReturn(context).when(mPreferenceManager).getContext();
doReturn(true).when(mFragment).shouldAddPreference(any(AppEntry.class));
doNothing().when(mFragment).setLoading(anyBoolean(), anyBoolean());
doReturn(mPreferenceScreen).when(mFragment).getPreferenceScreen();
doReturn(mPreferenceManager).when(mFragment).getPreferenceManager();
ReflectionHelpers.setField(mFragment, "mDataSaverBackend", mDataSaverBackend);
final String testPkg1 = "com.example.one";
final String testPkg2 = "com.example.two";
ShadowRestrictedLockUtils.setRestrictedPkgs(testPkg2);
doAnswer((invocation) -> {
final AccessPreference preference = invocation.getArgument(0);
final AppEntry entry = preference.getEntryForTest();
// Verify preference is disabled by admin and the summary is changed accordingly.
if (testPkg1.equals(entry.info.packageName)) {
assertThat(preference.isDisabledByAdmin()).isFalse();
assertThat(preference.getSummary()).isEqualTo("");
} else if (testPkg2.equals(entry.info.packageName)) {
assertThat(preference.isDisabledByAdmin()).isTrue();
assertThat(preference.getSummary()).isEqualTo(
context.getString(R.string.disabled_by_admin));
}
assertThat(preference.isChecked()).isFalse();
preference.performClick();
// Verify that when the preference is clicked, support details intent is launched
// if the preference is disabled by admin, otherwise the switch is toggled.
if (testPkg1.equals(entry.info.packageName)) {
assertThat(preference.isChecked()).isTrue();
assertThat(ShadowRestrictedLockUtils.hasAdminSupportDetailsIntentLaunched())
.isFalse();
} else if (testPkg2.equals(entry.info.packageName)) {
assertThat(preference.isChecked()).isFalse();
assertThat(ShadowRestrictedLockUtils.hasAdminSupportDetailsIntentLaunched())
.isTrue();
}
ShadowRestrictedLockUtils.clearAdminSupportDetailsIntentLaunch();
return null;
}).when(mPreferenceScreen).addPreference(any(AccessPreference.class));
mFragment.onRebuildComplete(createAppEntries(testPkg1, testPkg2));
}
private ArrayList<AppEntry> createAppEntries(String... packageNames) {
final ArrayList<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 AppEntry appEntry = spy(new AppEntry(RuntimeEnvironment.application,
info, i));
appEntry.extraInfo = new DataUsageState(false, false);
doNothing().when(appEntry).ensureLabel(any(Context.class));
ReflectionHelpers.setField(appEntry, "info", info);
appEntries.add(appEntry);
}
return appEntries;
}
}