Integrate admin controls for content protection
Bug: 323038631 Test: End-to-end with TestDPC, new units Change-Id: I98d52fdd95fca023b8eb576c97d563a1f3cab464
This commit is contained in:
@@ -16,47 +16,73 @@
|
||||
|
||||
package com.android.settings.security;
|
||||
|
||||
import static android.view.contentprotection.flags.Flags.FLAG_MANAGE_DEVICE_POLICY_ENABLED;
|
||||
|
||||
import static com.android.internal.R.string.config_defaultContentProtectionService;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.app.admin.DevicePolicyManager;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.UserInfo;
|
||||
import android.os.UserHandle;
|
||||
import android.os.UserManager;
|
||||
import android.platform.test.flag.junit.SetFlagsRule;
|
||||
import android.provider.DeviceConfig;
|
||||
import android.view.contentcapture.ContentCaptureManager;
|
||||
|
||||
import com.android.settings.testutils.shadow.ShadowDeviceConfig;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.mockito.junit.MockitoJUnit;
|
||||
import org.mockito.junit.MockitoRule;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@Config(
|
||||
shadows = {
|
||||
ShadowDeviceConfig.class,
|
||||
})
|
||||
@Config(shadows = {ShadowDeviceConfig.class})
|
||||
public class ContentProtectionPreferenceUtilsTest {
|
||||
|
||||
private static final String PACKAGE_NAME = "com.test.package";
|
||||
|
||||
private static final ComponentName COMPONENT_NAME =
|
||||
new ComponentName(PACKAGE_NAME, "TestClass");
|
||||
|
||||
private String mConfigDefaultContentProtectionService = COMPONENT_NAME.flattenToString();
|
||||
private static final UserHandle USER_HANDLE = UserHandle.of(111);
|
||||
|
||||
private static final int PROCESS_USER_ID = 222;
|
||||
|
||||
private final String mConfigDefaultContentProtectionService = COMPONENT_NAME.flattenToString();
|
||||
|
||||
@Rule public final MockitoRule mMockitoRule = MockitoJUnit.rule();
|
||||
|
||||
@Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
|
||||
|
||||
@Mock private Context mMockContext;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
}
|
||||
@Mock private Context mMockUserContext;
|
||||
|
||||
@Mock private UserManager mMockUserManager;
|
||||
|
||||
@Mock private DevicePolicyManager mMockDevicePolicyManager;
|
||||
|
||||
@Mock private UserInfo mMockUserInfo;
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
@@ -134,7 +160,6 @@ public class ContentProtectionPreferenceUtilsTest {
|
||||
assertThat(ContentProtectionPreferenceUtils.isAvailable(mMockContext)).isFalse();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void isAvailable_bothDisabled_false() {
|
||||
DeviceConfig.setProperty(
|
||||
@@ -145,4 +170,113 @@ public class ContentProtectionPreferenceUtilsTest {
|
||||
|
||||
assertThat(ContentProtectionPreferenceUtils.isAvailable(mMockContext)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getManagedProfile_noProfiles() {
|
||||
when(mMockContext.getSystemService(UserManager.class)).thenReturn(mMockUserManager);
|
||||
when(mMockUserManager.getUserProfiles()).thenReturn(List.of());
|
||||
|
||||
UserHandle actual = ContentProtectionPreferenceUtils.getManagedProfile(mMockContext);
|
||||
|
||||
assertThat(actual).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getManagedProfile_notManaged() {
|
||||
when(mMockContext.getSystemService(UserManager.class)).thenReturn(mMockUserManager);
|
||||
when(mMockUserManager.getUserProfiles()).thenReturn(List.of(USER_HANDLE));
|
||||
when(mMockUserManager.getProcessUserId()).thenReturn(PROCESS_USER_ID);
|
||||
when(mMockUserManager.getUserInfo(USER_HANDLE.getIdentifier())).thenReturn(mMockUserInfo);
|
||||
|
||||
UserHandle actual = ContentProtectionPreferenceUtils.getManagedProfile(mMockContext);
|
||||
|
||||
assertThat(actual).isNull();
|
||||
verify(mMockUserInfo).isManagedProfile();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getManagedProfile_managed() {
|
||||
when(mMockContext.getSystemService(UserManager.class)).thenReturn(mMockUserManager);
|
||||
when(mMockUserManager.getUserProfiles()).thenReturn(List.of(USER_HANDLE));
|
||||
when(mMockUserManager.getProcessUserId()).thenReturn(PROCESS_USER_ID);
|
||||
when(mMockUserManager.getUserInfo(USER_HANDLE.getIdentifier())).thenReturn(mMockUserInfo);
|
||||
when(mMockUserInfo.isManagedProfile()).thenReturn(true);
|
||||
|
||||
UserHandle actual = ContentProtectionPreferenceUtils.getManagedProfile(mMockContext);
|
||||
|
||||
assertThat(actual).isEqualTo(USER_HANDLE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getContentProtectionPolicy_flagDisabled_managedProfileNull() {
|
||||
mSetFlagsRule.disableFlags(FLAG_MANAGE_DEVICE_POLICY_ENABLED);
|
||||
|
||||
int actual =
|
||||
ContentProtectionPreferenceUtils.getContentProtectionPolicy(
|
||||
mMockContext, /* managedProfile= */ null);
|
||||
|
||||
assertThat(actual).isEqualTo(DevicePolicyManager.CONTENT_PROTECTION_DISABLED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getContentProtectionPolicy_flagDisabled_managedProfileNotNull() {
|
||||
mSetFlagsRule.disableFlags(FLAG_MANAGE_DEVICE_POLICY_ENABLED);
|
||||
|
||||
int actual =
|
||||
ContentProtectionPreferenceUtils.getContentProtectionPolicy(
|
||||
mMockContext, USER_HANDLE);
|
||||
|
||||
assertThat(actual).isEqualTo(DevicePolicyManager.CONTENT_PROTECTION_DISABLED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getContentProtectionPolicy_flagEnabled_managedProfileNull() throws Exception {
|
||||
mSetFlagsRule.enableFlags(FLAG_MANAGE_DEVICE_POLICY_ENABLED);
|
||||
when(mMockContext.getSystemService(DevicePolicyManager.class))
|
||||
.thenReturn(mMockDevicePolicyManager);
|
||||
when(mMockDevicePolicyManager.getContentProtectionPolicy(/* admin= */ null))
|
||||
.thenReturn(DevicePolicyManager.CONTENT_PROTECTION_ENABLED);
|
||||
|
||||
int actual =
|
||||
ContentProtectionPreferenceUtils.getContentProtectionPolicy(
|
||||
mMockContext, /* managedProfile= */ null);
|
||||
|
||||
assertThat(actual).isEqualTo(DevicePolicyManager.CONTENT_PROTECTION_ENABLED);
|
||||
verify(mMockContext, never()).createPackageContextAsUser(anyString(), anyInt(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getContentProtectionPolicy_flagEnabled_managedProfileNotNull() throws Exception {
|
||||
mSetFlagsRule.enableFlags(FLAG_MANAGE_DEVICE_POLICY_ENABLED);
|
||||
when(mMockContext.getPackageName()).thenReturn(PACKAGE_NAME);
|
||||
when(mMockContext.createPackageContextAsUser(PACKAGE_NAME, /* flags= */ 0, USER_HANDLE))
|
||||
.thenReturn(mMockUserContext);
|
||||
when(mMockUserContext.getSystemService(DevicePolicyManager.class))
|
||||
.thenReturn(mMockDevicePolicyManager);
|
||||
when(mMockDevicePolicyManager.getContentProtectionPolicy(/* admin= */ null))
|
||||
.thenReturn(DevicePolicyManager.CONTENT_PROTECTION_ENABLED);
|
||||
|
||||
int actual =
|
||||
ContentProtectionPreferenceUtils.getContentProtectionPolicy(
|
||||
mMockContext, USER_HANDLE);
|
||||
|
||||
assertThat(actual).isEqualTo(DevicePolicyManager.CONTENT_PROTECTION_ENABLED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getContentProtectionPolicy_flagEnabled_managedProfileNotNull_nameNotFound()
|
||||
throws Exception {
|
||||
mSetFlagsRule.enableFlags(FLAG_MANAGE_DEVICE_POLICY_ENABLED);
|
||||
when(mMockContext.getPackageName()).thenReturn(PACKAGE_NAME);
|
||||
when(mMockContext.createPackageContextAsUser(PACKAGE_NAME, /* flags= */ 0, USER_HANDLE))
|
||||
.thenThrow(new PackageManager.NameNotFoundException());
|
||||
|
||||
assertThrows(
|
||||
IllegalStateException.class,
|
||||
() ->
|
||||
ContentProtectionPreferenceUtils.getContentProtectionPolicy(
|
||||
mMockContext, USER_HANDLE));
|
||||
|
||||
verify(mMockContext, never()).getSystemService(DevicePolicyManager.class);
|
||||
}
|
||||
}
|
||||
|
@@ -15,20 +15,25 @@
|
||||
*/
|
||||
package com.android.settings.security;
|
||||
|
||||
import static android.view.contentprotection.flags.Flags.FLAG_MANAGE_DEVICE_POLICY_ENABLED;
|
||||
|
||||
import static com.android.settings.core.BasePreferenceController.AVAILABLE;
|
||||
import static com.android.settings.security.ContentProtectionTogglePreferenceController.KEY_CONTENT_PROTECTION_PREFERENCE;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.app.admin.DevicePolicyManager;
|
||||
import android.content.Context;
|
||||
import android.os.UserHandle;
|
||||
import android.platform.test.flag.junit.SetFlagsRule;
|
||||
import android.provider.Settings;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
|
||||
@@ -42,37 +47,40 @@ import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.mockito.junit.MockitoJUnit;
|
||||
import org.mockito.junit.MockitoRule;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@Config(
|
||||
shadows = {
|
||||
ShadowUtils.class,
|
||||
})
|
||||
@Config(shadows = {ShadowUtils.class})
|
||||
public class ContentProtectionTogglePreferenceControllerTest {
|
||||
|
||||
@Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
|
||||
@Rule public final MockitoRule mMockitoRule = MockitoJUnit.rule();
|
||||
|
||||
@Mock private PreferenceScreen mMockScreen;
|
||||
@Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
|
||||
|
||||
private RestrictedLockUtils.EnforcedAdmin mAdmin;
|
||||
private SettingsMainSwitchPreference mSwitchPreference;
|
||||
private final Context mContext = ApplicationProvider.getApplicationContext();
|
||||
private ContentProtectionTogglePreferenceController mController;
|
||||
|
||||
@Mock private PreferenceScreen mMockPreferenceScreen;
|
||||
|
||||
@Mock private SettingsMainSwitchPreference mMockSwitchPreference;
|
||||
|
||||
@Nullable private RestrictedLockUtils.EnforcedAdmin mEnforcedAdmin;
|
||||
|
||||
@DevicePolicyManager.ContentProtectionPolicy
|
||||
private int mContentProtectionPolicy = DevicePolicyManager.CONTENT_PROTECTION_DISABLED;
|
||||
|
||||
private TestContentProtectionTogglePreferenceController mController;
|
||||
|
||||
private int mSettingBackupValue;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mController = new TestContentProtectionTogglePreferenceController();
|
||||
mSwitchPreference = new SettingsMainSwitchPreference(mContext);
|
||||
when(mMockScreen.findPreference(mController.getPreferenceKey()))
|
||||
.thenReturn(mSwitchPreference);
|
||||
SettingsMainSwitchPreference switchPreference = new SettingsMainSwitchPreference(mContext);
|
||||
when(mMockPreferenceScreen.findPreference(mController.getPreferenceKey()))
|
||||
.thenReturn(switchPreference);
|
||||
mSettingBackupValue = getContentProtectionGlobalSetting();
|
||||
Settings.Global.putInt(mContext.getContentResolver(), KEY_CONTENT_PROTECTION_PREFERENCE, 0);
|
||||
}
|
||||
@@ -87,89 +95,223 @@ public class ContentProtectionTogglePreferenceControllerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isAvailable_alwaysAvailable() {
|
||||
public void constructor_flagDisabled_doesNotFetchData() {
|
||||
mSetFlagsRule.disableFlags(FLAG_MANAGE_DEVICE_POLICY_ENABLED);
|
||||
mController = new TestContentProtectionTogglePreferenceController();
|
||||
|
||||
assertThat(mController.mCounterGetManagedProfile).isEqualTo(0);
|
||||
assertThat(mController.mCounterGetEnforcedAdmin).isEqualTo(0);
|
||||
assertThat(mController.mCounterGetContentProtectionPolicy).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void constructor_flagEnabled_fetchesData() {
|
||||
mSetFlagsRule.enableFlags(FLAG_MANAGE_DEVICE_POLICY_ENABLED);
|
||||
mController = new TestContentProtectionTogglePreferenceController();
|
||||
|
||||
assertThat(mController.mCounterGetManagedProfile).isEqualTo(1);
|
||||
assertThat(mController.mCounterGetEnforcedAdmin).isEqualTo(1);
|
||||
assertThat(mController.mCounterGetContentProtectionPolicy).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAvailabilityStatus_available() {
|
||||
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
|
||||
assertThat(mController.isAvailable()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void displayPreference() {
|
||||
setUpFullyManagedMode();
|
||||
SettingsMainSwitchPreference mockSwitchPreference =
|
||||
mock(SettingsMainSwitchPreference.class);
|
||||
when(mMockScreen.findPreference(any())).thenReturn(mockSwitchPreference);
|
||||
when(mockSwitchPreference.getKey()).thenReturn(mController.getPreferenceKey());
|
||||
|
||||
mController = new TestContentProtectionTogglePreferenceController();
|
||||
mController.displayPreference(mMockScreen);
|
||||
|
||||
assertThat(mockSwitchPreference).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_notFullyManagedMode_enabled() {
|
||||
SettingsMainSwitchPreference mockSwitchPreference =
|
||||
mock(SettingsMainSwitchPreference.class);
|
||||
when(mMockScreen.findPreference(any())).thenReturn(mockSwitchPreference);
|
||||
when(mockSwitchPreference.getKey()).thenReturn(mController.getPreferenceKey());
|
||||
|
||||
mController = new TestContentProtectionTogglePreferenceController();
|
||||
mController.displayPreference(mMockScreen);
|
||||
mController.updateState(mockSwitchPreference);
|
||||
|
||||
verify(mockSwitchPreference, never()).setDisabledByAdmin(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_fullyManagedMode_disabled() {
|
||||
setUpFullyManagedMode();
|
||||
SettingsMainSwitchPreference mockSwitchPreference =
|
||||
mock(SettingsMainSwitchPreference.class);
|
||||
when(mMockScreen.findPreference(any())).thenReturn(mockSwitchPreference);
|
||||
when(mockSwitchPreference.getKey()).thenReturn(mController.getPreferenceKey());
|
||||
|
||||
mController = new TestContentProtectionTogglePreferenceController();
|
||||
mController.displayPreference(mMockScreen);
|
||||
mController.updateState(mockSwitchPreference);
|
||||
|
||||
verify(mockSwitchPreference).setDisabledByAdmin(mAdmin);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isChecked_settingTurnOn() {
|
||||
public void isChecked_noEnforcedAdmin_readsSettingsTrue() {
|
||||
Settings.Global.putInt(mContext.getContentResolver(), KEY_CONTENT_PROTECTION_PREFERENCE, 1);
|
||||
|
||||
assertThat(mController.isChecked()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isChecked_fullyManagedMode_settingTurnOff() {
|
||||
setUpFullyManagedMode();
|
||||
Settings.Global.putInt(mContext.getContentResolver(), KEY_CONTENT_PROTECTION_PREFERENCE, 1);
|
||||
SettingsMainSwitchPreference mockSwitchPreference =
|
||||
mock(SettingsMainSwitchPreference.class);
|
||||
when(mMockScreen.findPreference(any())).thenReturn(mockSwitchPreference);
|
||||
when(mockSwitchPreference.getKey()).thenReturn(mController.getPreferenceKey());
|
||||
|
||||
mController = new TestContentProtectionTogglePreferenceController();
|
||||
mController.displayPreference(mMockScreen);
|
||||
mController.updateState(mockSwitchPreference);
|
||||
|
||||
assertThat(mController.isChecked()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isChecked_settingTurnOff() {
|
||||
public void isChecked_noEnforcedAdmin_readsSettingsFalse() {
|
||||
Settings.Global.putInt(
|
||||
mContext.getContentResolver(), KEY_CONTENT_PROTECTION_PREFERENCE, -1);
|
||||
|
||||
assertThat(mController.isChecked()).isFalse();
|
||||
assertThat(getContentProtectionGlobalSetting()).isEqualTo(-1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isChecked_settingDefaultOn() {
|
||||
public void isChecked_noEnforcedAdmin_readsSettingsDefaultTrue() {
|
||||
assertThat(mController.isChecked()).isTrue();
|
||||
assertThat(getContentProtectionGlobalSetting()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isChecked_enforcedAdmin_flagDisabled_false() {
|
||||
mSetFlagsRule.disableFlags(FLAG_MANAGE_DEVICE_POLICY_ENABLED);
|
||||
mEnforcedAdmin = new RestrictedLockUtils.EnforcedAdmin();
|
||||
Settings.Global.putInt(mContext.getContentResolver(), KEY_CONTENT_PROTECTION_PREFERENCE, 1);
|
||||
setupForUpdateState();
|
||||
mController.updateState(mMockSwitchPreference);
|
||||
|
||||
assertThat(mController.isChecked()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isChecked_enforcedAdmin_flagEnabled_policyDisabled_false() {
|
||||
mSetFlagsRule.enableFlags(FLAG_MANAGE_DEVICE_POLICY_ENABLED);
|
||||
mEnforcedAdmin = new RestrictedLockUtils.EnforcedAdmin();
|
||||
mContentProtectionPolicy = DevicePolicyManager.CONTENT_PROTECTION_DISABLED;
|
||||
Settings.Global.putInt(mContext.getContentResolver(), KEY_CONTENT_PROTECTION_PREFERENCE, 1);
|
||||
mController = new TestContentProtectionTogglePreferenceController();
|
||||
|
||||
assertThat(mController.isChecked()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isChecked_enforcedAdmin_flagEnabled_policyEnabled_true() {
|
||||
mSetFlagsRule.enableFlags(FLAG_MANAGE_DEVICE_POLICY_ENABLED);
|
||||
mEnforcedAdmin = new RestrictedLockUtils.EnforcedAdmin();
|
||||
mContentProtectionPolicy = DevicePolicyManager.CONTENT_PROTECTION_ENABLED;
|
||||
Settings.Global.putInt(
|
||||
mContext.getContentResolver(), KEY_CONTENT_PROTECTION_PREFERENCE, -1);
|
||||
mController = new TestContentProtectionTogglePreferenceController();
|
||||
|
||||
assertThat(mController.isChecked()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isChecked_enforcedAdmin_flagEnabled_policyNotControlled_readsSettingsTrue() {
|
||||
mSetFlagsRule.enableFlags(FLAG_MANAGE_DEVICE_POLICY_ENABLED);
|
||||
mEnforcedAdmin = new RestrictedLockUtils.EnforcedAdmin();
|
||||
mContentProtectionPolicy = DevicePolicyManager.CONTENT_PROTECTION_NOT_CONTROLLED_BY_POLICY;
|
||||
Settings.Global.putInt(mContext.getContentResolver(), KEY_CONTENT_PROTECTION_PREFERENCE, 1);
|
||||
mController = new TestContentProtectionTogglePreferenceController();
|
||||
|
||||
assertThat(mController.isChecked()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isChecked_enforcedAdmin_flagEnabled_policyNotControlled_readsSettingsFalse() {
|
||||
mSetFlagsRule.enableFlags(FLAG_MANAGE_DEVICE_POLICY_ENABLED);
|
||||
mEnforcedAdmin = new RestrictedLockUtils.EnforcedAdmin();
|
||||
mContentProtectionPolicy = DevicePolicyManager.CONTENT_PROTECTION_NOT_CONTROLLED_BY_POLICY;
|
||||
Settings.Global.putInt(
|
||||
mContext.getContentResolver(), KEY_CONTENT_PROTECTION_PREFERENCE, -1);
|
||||
mController = new TestContentProtectionTogglePreferenceController();
|
||||
|
||||
assertThat(mController.isChecked()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isChecked_enforcedAdmin_flagEnabled_policyNotControlled_readsSettingsDefaultTrue() {
|
||||
mSetFlagsRule.enableFlags(FLAG_MANAGE_DEVICE_POLICY_ENABLED);
|
||||
mEnforcedAdmin = new RestrictedLockUtils.EnforcedAdmin();
|
||||
mContentProtectionPolicy = DevicePolicyManager.CONTENT_PROTECTION_NOT_CONTROLLED_BY_POLICY;
|
||||
mController = new TestContentProtectionTogglePreferenceController();
|
||||
|
||||
assertThat(mController.isChecked()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void displayPreference() {
|
||||
setupForDisplayPreference();
|
||||
|
||||
mController.displayPreference(mMockPreferenceScreen);
|
||||
|
||||
verify(mMockSwitchPreference).addOnSwitchChangeListener(mController);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_flagDisabled_noEnforcedAdmin() {
|
||||
mSetFlagsRule.disableFlags(FLAG_MANAGE_DEVICE_POLICY_ENABLED);
|
||||
setupForUpdateState();
|
||||
|
||||
mController.updateState(mMockSwitchPreference);
|
||||
|
||||
assertThat(mController.mCounterGetEnforcedAdmin).isEqualTo(1);
|
||||
verify(mMockSwitchPreference, never()).setDisabledByAdmin(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_flagDisabled_enforcedAdmin() {
|
||||
mSetFlagsRule.disableFlags(FLAG_MANAGE_DEVICE_POLICY_ENABLED);
|
||||
mEnforcedAdmin = new RestrictedLockUtils.EnforcedAdmin();
|
||||
setupForUpdateState();
|
||||
|
||||
mController.updateState(mMockSwitchPreference);
|
||||
|
||||
assertThat(mController.mCounterGetEnforcedAdmin).isEqualTo(1);
|
||||
verify(mMockSwitchPreference).setDisabledByAdmin(mEnforcedAdmin);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_flagEnabled_noEnforcedAdmin_policyDisabled() {
|
||||
mSetFlagsRule.enableFlags(FLAG_MANAGE_DEVICE_POLICY_ENABLED);
|
||||
mContentProtectionPolicy = DevicePolicyManager.CONTENT_PROTECTION_DISABLED;
|
||||
setupForUpdateState();
|
||||
|
||||
mController.updateState(mMockSwitchPreference);
|
||||
|
||||
assertThat(mController.mCounterGetEnforcedAdmin).isEqualTo(1);
|
||||
verify(mMockSwitchPreference, never()).setDisabledByAdmin(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_flagEnabled_noEnforcedAdmin_policyEnabled() {
|
||||
mSetFlagsRule.enableFlags(FLAG_MANAGE_DEVICE_POLICY_ENABLED);
|
||||
mContentProtectionPolicy = DevicePolicyManager.CONTENT_PROTECTION_ENABLED;
|
||||
setupForUpdateState();
|
||||
|
||||
mController.updateState(mMockSwitchPreference);
|
||||
|
||||
assertThat(mController.mCounterGetEnforcedAdmin).isEqualTo(1);
|
||||
verify(mMockSwitchPreference, never()).setDisabledByAdmin(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_flagEnabled_noEnforcedAdmin_policyNotControlled() {
|
||||
mSetFlagsRule.enableFlags(FLAG_MANAGE_DEVICE_POLICY_ENABLED);
|
||||
mContentProtectionPolicy = DevicePolicyManager.CONTENT_PROTECTION_NOT_CONTROLLED_BY_POLICY;
|
||||
setupForUpdateState();
|
||||
|
||||
mController.updateState(mMockSwitchPreference);
|
||||
|
||||
assertThat(mController.mCounterGetEnforcedAdmin).isEqualTo(1);
|
||||
verify(mMockSwitchPreference, never()).setDisabledByAdmin(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_flagEnabled_enforcedAdmin_policyDisabled() {
|
||||
mSetFlagsRule.enableFlags(FLAG_MANAGE_DEVICE_POLICY_ENABLED);
|
||||
mEnforcedAdmin = new RestrictedLockUtils.EnforcedAdmin();
|
||||
mContentProtectionPolicy = DevicePolicyManager.CONTENT_PROTECTION_DISABLED;
|
||||
setupForUpdateState();
|
||||
|
||||
mController.updateState(mMockSwitchPreference);
|
||||
|
||||
assertThat(mController.mCounterGetEnforcedAdmin).isEqualTo(1);
|
||||
verify(mMockSwitchPreference).setDisabledByAdmin(mEnforcedAdmin);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_flagEnabled_enforcedAdmin_policyEnabled() {
|
||||
mSetFlagsRule.enableFlags(FLAG_MANAGE_DEVICE_POLICY_ENABLED);
|
||||
mEnforcedAdmin = new RestrictedLockUtils.EnforcedAdmin();
|
||||
mContentProtectionPolicy = DevicePolicyManager.CONTENT_PROTECTION_ENABLED;
|
||||
setupForUpdateState();
|
||||
|
||||
mController.updateState(mMockSwitchPreference);
|
||||
|
||||
assertThat(mController.mCounterGetEnforcedAdmin).isEqualTo(1);
|
||||
verify(mMockSwitchPreference).setDisabledByAdmin(mEnforcedAdmin);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_flagEnabled_enforcedAdmin_policyNotControlled() {
|
||||
mSetFlagsRule.enableFlags(FLAG_MANAGE_DEVICE_POLICY_ENABLED);
|
||||
mEnforcedAdmin = new RestrictedLockUtils.EnforcedAdmin();
|
||||
mContentProtectionPolicy = DevicePolicyManager.CONTENT_PROTECTION_NOT_CONTROLLED_BY_POLICY;
|
||||
setupForUpdateState();
|
||||
|
||||
mController.updateState(mMockSwitchPreference);
|
||||
|
||||
assertThat(mController.mCounterGetEnforcedAdmin).isEqualTo(1);
|
||||
verify(mMockSwitchPreference, never()).setDisabledByAdmin(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -193,20 +335,49 @@ public class ContentProtectionTogglePreferenceControllerTest {
|
||||
mContext.getContentResolver(), KEY_CONTENT_PROTECTION_PREFERENCE, 0);
|
||||
}
|
||||
|
||||
private void setUpFullyManagedMode() {
|
||||
mAdmin = new RestrictedLockUtils.EnforcedAdmin();
|
||||
private void setupForDisplayPreference() {
|
||||
when(mMockPreferenceScreen.findPreference(any())).thenReturn(mMockSwitchPreference);
|
||||
when(mMockSwitchPreference.getKey()).thenReturn(mController.getPreferenceKey());
|
||||
mController = new TestContentProtectionTogglePreferenceController();
|
||||
}
|
||||
|
||||
private void setupForUpdateState() {
|
||||
setupForDisplayPreference();
|
||||
mController.displayPreference(mMockPreferenceScreen);
|
||||
}
|
||||
|
||||
private class TestContentProtectionTogglePreferenceController
|
||||
extends ContentProtectionTogglePreferenceController {
|
||||
|
||||
public int mCounterGetManagedProfile;
|
||||
|
||||
public int mCounterGetEnforcedAdmin;
|
||||
|
||||
public int mCounterGetContentProtectionPolicy;
|
||||
|
||||
TestContentProtectionTogglePreferenceController() {
|
||||
super(ContentProtectionTogglePreferenceControllerTest.this.mContext, "key");
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
protected UserHandle getManagedProfile() {
|
||||
mCounterGetManagedProfile++;
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
protected RestrictedLockUtils.EnforcedAdmin getEnforcedAdmin() {
|
||||
return mAdmin;
|
||||
mCounterGetEnforcedAdmin++;
|
||||
return mEnforcedAdmin;
|
||||
}
|
||||
|
||||
@Override
|
||||
@DevicePolicyManager.ContentProtectionPolicy
|
||||
protected int getContentProtectionPolicy(@Nullable UserHandle userHandle) {
|
||||
mCounterGetContentProtectionPolicy++;
|
||||
return mContentProtectionPolicy;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -16,19 +16,22 @@
|
||||
|
||||
package com.android.settings.security;
|
||||
|
||||
import static android.view.contentprotection.flags.Flags.FLAG_MANAGE_DEVICE_POLICY_ENABLED;
|
||||
|
||||
import static com.android.settings.core.BasePreferenceController.AVAILABLE;
|
||||
import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.app.admin.DevicePolicyManager;
|
||||
import android.content.Context;
|
||||
import android.os.UserHandle;
|
||||
import android.platform.test.flag.junit.SetFlagsRule;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
@@ -39,55 +42,169 @@ import com.android.settingslib.RestrictedLockUtils;
|
||||
import com.android.settingslib.RestrictedSwitchPreference;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.mockito.junit.MockitoJUnit;
|
||||
import org.mockito.junit.MockitoRule;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class ContentProtectionWorkSwitchControllerTest {
|
||||
|
||||
private static final UserHandle TEST_USER_HANDLE = UserHandle.of(10);
|
||||
|
||||
@Rule public final MockitoRule mMockitoRule = MockitoJUnit.rule();
|
||||
|
||||
@Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
|
||||
|
||||
private final Context mContext = ApplicationProvider.getApplicationContext();
|
||||
|
||||
@Mock private PreferenceScreen mMockPreferenceScreen;
|
||||
private ContentProtectionWorkSwitchController mController;
|
||||
private UserHandle mManagedProfileUserHandle;
|
||||
private RestrictedLockUtils.EnforcedAdmin mEnforcedAdmin;
|
||||
|
||||
@Mock private RestrictedSwitchPreference mMockSwitchPreference;
|
||||
|
||||
@Nullable private UserHandle mManagedProfileUserHandle;
|
||||
|
||||
@Nullable private RestrictedLockUtils.EnforcedAdmin mEnforcedAdmin;
|
||||
|
||||
@DevicePolicyManager.ContentProtectionPolicy
|
||||
private int mContentProtectionPolicy = DevicePolicyManager.CONTENT_PROTECTION_DISABLED;
|
||||
|
||||
private TestContentProtectionWorkSwitchController mController;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mController = new TestContentProtectionWorkSwitchController();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isAvailable_managedProfile_available() {
|
||||
public void constructor_flagDisabled_doesNotFetchData() {
|
||||
mSetFlagsRule.disableFlags(FLAG_MANAGE_DEVICE_POLICY_ENABLED);
|
||||
mController = new TestContentProtectionWorkSwitchController();
|
||||
|
||||
assertThat(mController.mCounterGetManagedProfile).isEqualTo(0);
|
||||
assertThat(mController.mCounterGetEnforcedAdmin).isEqualTo(0);
|
||||
assertThat(mController.mCounterGetContentProtectionPolicy).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void constructor_flagEnabled_fetchesManagedProfile() {
|
||||
mSetFlagsRule.enableFlags(FLAG_MANAGE_DEVICE_POLICY_ENABLED);
|
||||
mController = new TestContentProtectionWorkSwitchController();
|
||||
|
||||
assertThat(mController.mCounterGetManagedProfile).isEqualTo(1);
|
||||
assertThat(mController.mCounterGetEnforcedAdmin).isEqualTo(0);
|
||||
assertThat(mController.mCounterGetContentProtectionPolicy).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void constructor_flagEnabled_withManagedProfile_fetchesPolicy() {
|
||||
mSetFlagsRule.enableFlags(FLAG_MANAGE_DEVICE_POLICY_ENABLED);
|
||||
mManagedProfileUserHandle = TEST_USER_HANDLE;
|
||||
mController = new TestContentProtectionWorkSwitchController();
|
||||
|
||||
assertThat(mController.mCounterGetManagedProfile).isEqualTo(1);
|
||||
assertThat(mController.mCounterGetEnforcedAdmin).isEqualTo(0);
|
||||
assertThat(mController.mCounterGetContentProtectionPolicy).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAvailabilityStatus_flagDisabled_managedProfile_available() {
|
||||
mSetFlagsRule.disableFlags(FLAG_MANAGE_DEVICE_POLICY_ENABLED);
|
||||
mManagedProfileUserHandle = TEST_USER_HANDLE;
|
||||
mController = new TestContentProtectionWorkSwitchController();
|
||||
|
||||
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
|
||||
assertThat(mController.isAvailable()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isAvailable_noManagedProfile_notAvailable() {
|
||||
mManagedProfileUserHandle = null;
|
||||
public void getAvailabilityStatus_flagDisabled_noManagedProfile_unavailable() {
|
||||
mSetFlagsRule.disableFlags(FLAG_MANAGE_DEVICE_POLICY_ENABLED);
|
||||
mController = new TestContentProtectionWorkSwitchController();
|
||||
|
||||
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
|
||||
assertThat(mController.isAvailable()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isChecked_noManagedProfile_alwaysOff() {
|
||||
mManagedProfileUserHandle = null;
|
||||
public void getAvailabilityStatus_flagEnabled_managedProfile_policyDisabled_available() {
|
||||
mSetFlagsRule.enableFlags(FLAG_MANAGE_DEVICE_POLICY_ENABLED);
|
||||
mManagedProfileUserHandle = TEST_USER_HANDLE;
|
||||
mContentProtectionPolicy = DevicePolicyManager.CONTENT_PROTECTION_DISABLED;
|
||||
mController = new TestContentProtectionWorkSwitchController();
|
||||
|
||||
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
|
||||
assertThat(mController.isAvailable()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAvailabilityStatus_flagEnabled_managedProfile_policyEnabled_available() {
|
||||
mSetFlagsRule.enableFlags(FLAG_MANAGE_DEVICE_POLICY_ENABLED);
|
||||
mManagedProfileUserHandle = TEST_USER_HANDLE;
|
||||
mContentProtectionPolicy = DevicePolicyManager.CONTENT_PROTECTION_ENABLED;
|
||||
mController = new TestContentProtectionWorkSwitchController();
|
||||
|
||||
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
|
||||
assertThat(mController.isAvailable()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAvailabilityStatus_flagEnabled_managedProfile_policyNotControlled_unavailable() {
|
||||
mSetFlagsRule.enableFlags(FLAG_MANAGE_DEVICE_POLICY_ENABLED);
|
||||
mManagedProfileUserHandle = TEST_USER_HANDLE;
|
||||
mContentProtectionPolicy = DevicePolicyManager.CONTENT_PROTECTION_NOT_CONTROLLED_BY_POLICY;
|
||||
mController = new TestContentProtectionWorkSwitchController();
|
||||
|
||||
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
|
||||
assertThat(mController.isAvailable()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAvailabilityStatus_flagEnabled_noManagedProfile_unavailable() {
|
||||
mSetFlagsRule.enableFlags(FLAG_MANAGE_DEVICE_POLICY_ENABLED);
|
||||
mController = new TestContentProtectionWorkSwitchController();
|
||||
|
||||
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
|
||||
assertThat(mController.isAvailable()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isChecked_flagDisabled_false() {
|
||||
mSetFlagsRule.disableFlags(FLAG_MANAGE_DEVICE_POLICY_ENABLED);
|
||||
mController = new TestContentProtectionWorkSwitchController();
|
||||
|
||||
assertThat(mController.isChecked()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isChecked_managedProfile_alwaysOff() {
|
||||
public void isChecked_flagEnabled_policyEnabled_true() {
|
||||
mSetFlagsRule.enableFlags(FLAG_MANAGE_DEVICE_POLICY_ENABLED);
|
||||
mManagedProfileUserHandle = TEST_USER_HANDLE;
|
||||
mContentProtectionPolicy = DevicePolicyManager.CONTENT_PROTECTION_ENABLED;
|
||||
mController = new TestContentProtectionWorkSwitchController();
|
||||
|
||||
assertThat(mController.isChecked()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isChecked_flagEnabled_policyDisabled_false() {
|
||||
mSetFlagsRule.enableFlags(FLAG_MANAGE_DEVICE_POLICY_ENABLED);
|
||||
mManagedProfileUserHandle = TEST_USER_HANDLE;
|
||||
mContentProtectionPolicy = DevicePolicyManager.CONTENT_PROTECTION_DISABLED;
|
||||
mController = new TestContentProtectionWorkSwitchController();
|
||||
|
||||
assertThat(mController.isChecked()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isChecked_flagEnabled_policyNotControlled_false() {
|
||||
mSetFlagsRule.enableFlags(FLAG_MANAGE_DEVICE_POLICY_ENABLED);
|
||||
mManagedProfileUserHandle = TEST_USER_HANDLE;
|
||||
mContentProtectionPolicy = DevicePolicyManager.CONTENT_PROTECTION_NOT_CONTROLLED_BY_POLICY;
|
||||
mController = new TestContentProtectionWorkSwitchController();
|
||||
|
||||
assertThat(mController.isChecked()).isFalse();
|
||||
}
|
||||
@@ -99,50 +216,72 @@ public class ContentProtectionWorkSwitchControllerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void displayPreference_managedProfile_disabled() {
|
||||
public void displayPreference_flagDisabled_managedProfile_disabledByAdmin() {
|
||||
mSetFlagsRule.disableFlags(FLAG_MANAGE_DEVICE_POLICY_ENABLED);
|
||||
mManagedProfileUserHandle = TEST_USER_HANDLE;
|
||||
mEnforcedAdmin = new RestrictedLockUtils.EnforcedAdmin();
|
||||
RestrictedSwitchPreference mockSwitchPreference = mock(RestrictedSwitchPreference.class);
|
||||
when(mMockPreferenceScreen.findPreference(any())).thenReturn(mockSwitchPreference);
|
||||
when(mockSwitchPreference.getKey()).thenReturn(mController.getPreferenceKey());
|
||||
setupForDisplayPreference();
|
||||
|
||||
mController.displayPreference(mMockPreferenceScreen);
|
||||
|
||||
assertThat(mController.isAvailable()).isTrue();
|
||||
verify(mockSwitchPreference).setDisabledByAdmin(mEnforcedAdmin);
|
||||
verify(mMockSwitchPreference).setDisabledByAdmin(mEnforcedAdmin);
|
||||
assertThat(mController.mCounterGetManagedProfile).isEqualTo(3);
|
||||
assertThat(mController.mCounterGetEnforcedAdmin).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void displayPreference_noManagedProfile_notDisabled() {
|
||||
mManagedProfileUserHandle = null;
|
||||
public void displayPreference_flagDisabled_noManagedProfile_notDisabledByAdmin() {
|
||||
mSetFlagsRule.disableFlags(FLAG_MANAGE_DEVICE_POLICY_ENABLED);
|
||||
setupForDisplayPreference();
|
||||
|
||||
mController.displayPreference(mMockPreferenceScreen);
|
||||
|
||||
verify(mMockSwitchPreference, never()).setDisabledByAdmin(any());
|
||||
assertThat(mController.mCounterGetManagedProfile).isEqualTo(3);
|
||||
assertThat(mController.mCounterGetEnforcedAdmin).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void displayPreference_flagEnabled_managedProfile_disabledByAdmin() {
|
||||
mSetFlagsRule.enableFlags(FLAG_MANAGE_DEVICE_POLICY_ENABLED);
|
||||
mManagedProfileUserHandle = TEST_USER_HANDLE;
|
||||
mEnforcedAdmin = new RestrictedLockUtils.EnforcedAdmin();
|
||||
RestrictedSwitchPreference mockSwitchPreference = mock(RestrictedSwitchPreference.class);
|
||||
when(mMockPreferenceScreen.findPreference(any())).thenReturn(mockSwitchPreference);
|
||||
when(mockSwitchPreference.getKey()).thenReturn(mController.getPreferenceKey());
|
||||
setupForDisplayPreference();
|
||||
|
||||
mController.displayPreference(mMockPreferenceScreen);
|
||||
|
||||
assertThat(mController.isAvailable()).isFalse();
|
||||
verify(mockSwitchPreference, never()).setDisabledByAdmin(any());
|
||||
verify(mMockSwitchPreference).setDisabledByAdmin(mEnforcedAdmin);
|
||||
assertThat(mController.mCounterGetManagedProfile).isEqualTo(1);
|
||||
assertThat(mController.mCounterGetEnforcedAdmin).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void displayPreference_noEnforcedAdmin_notDisabled() {
|
||||
mManagedProfileUserHandle = null;
|
||||
mEnforcedAdmin = null;
|
||||
RestrictedSwitchPreference mockSwitchPreference = mock(RestrictedSwitchPreference.class);
|
||||
when(mMockPreferenceScreen.findPreference(any())).thenReturn(mockSwitchPreference);
|
||||
when(mockSwitchPreference.getKey()).thenReturn(mController.getPreferenceKey());
|
||||
public void displayPreference_flagEnabled_noManagedProfile_notDisabledByAdmin() {
|
||||
mSetFlagsRule.enableFlags(FLAG_MANAGE_DEVICE_POLICY_ENABLED);
|
||||
setupForDisplayPreference();
|
||||
|
||||
mController.displayPreference(mMockPreferenceScreen);
|
||||
|
||||
assertThat(mController.isAvailable()).isFalse();
|
||||
verify(mockSwitchPreference, never()).setDisabledByAdmin(any());
|
||||
verify(mMockSwitchPreference, never()).setDisabledByAdmin(any());
|
||||
assertThat(mController.mCounterGetManagedProfile).isEqualTo(1);
|
||||
assertThat(mController.mCounterGetEnforcedAdmin).isEqualTo(0);
|
||||
}
|
||||
|
||||
private void setupForDisplayPreference() {
|
||||
when(mMockPreferenceScreen.findPreference(any())).thenReturn(mMockSwitchPreference);
|
||||
when(mMockSwitchPreference.getKey()).thenReturn(mController.getPreferenceKey());
|
||||
mController = new TestContentProtectionWorkSwitchController();
|
||||
}
|
||||
|
||||
private class TestContentProtectionWorkSwitchController
|
||||
extends ContentProtectionWorkSwitchController {
|
||||
|
||||
public int mCounterGetManagedProfile;
|
||||
|
||||
public int mCounterGetEnforcedAdmin;
|
||||
|
||||
public int mCounterGetContentProtectionPolicy;
|
||||
|
||||
TestContentProtectionWorkSwitchController() {
|
||||
super(ContentProtectionWorkSwitchControllerTest.this.mContext, "key");
|
||||
}
|
||||
@@ -150,14 +289,23 @@ public class ContentProtectionWorkSwitchControllerTest {
|
||||
@Override
|
||||
@Nullable
|
||||
protected UserHandle getManagedProfile() {
|
||||
mCounterGetManagedProfile++;
|
||||
return mManagedProfileUserHandle;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
protected RestrictedLockUtils.EnforcedAdmin getEnforcedAdmin(
|
||||
@NonNull UserHandle managedProfile) {
|
||||
@NonNull UserHandle userHandle) {
|
||||
mCounterGetEnforcedAdmin++;
|
||||
return mEnforcedAdmin;
|
||||
}
|
||||
|
||||
@Override
|
||||
@DevicePolicyManager.ContentProtectionPolicy
|
||||
protected int getContentProtectionPolicy(@Nullable UserHandle userHandle) {
|
||||
mCounterGetContentProtectionPolicy++;
|
||||
return mContentProtectionPolicy;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user