Integrate admin controls for content protection

Bug: 323038631
Test: End-to-end with TestDPC, new units
Change-Id: I98d52fdd95fca023b8eb576c97d563a1f3cab464
This commit is contained in:
Nino Jagar
2024-03-06 01:17:52 +00:00
parent 5cbc60dcf3
commit 65eb00f0f3
6 changed files with 740 additions and 153 deletions

View File

@@ -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);
}
}