Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1c03c26b authored by Ang Li's avatar Ang Li
Browse files

Add tests for flag-gated intent extras in RestrictedLockUtils

Adds unit tests for the `getShowAdminSupportDetailsIntent(EnforcingAdmin)` method to verify its behavior with the `enforcingAdminExtraEnabled` and `enforcingAdminGetComponentNameEnabled` feature flags.

These tests ensure that the `EXTRA_ENFORCING_ADMIN` and `EXTRA_DEVICE_ADMIN` intent extras are correctly added or omitted based on the state of their respective flags. Scenarios for null admins and admins with or without a component name are also covered.

Please help fill out the survey for feedback: https://forms.gle/Qz9q5boo4h4tESyd7

Original Change: ag/35143890
Bug: 431235865
Flag: TEST_ONLY

ABTD Links: http://go/forrest-run/L28700030016847958

Change-Id: Ie2746dd6fdf6d4b209d7b188ff16eeb1bda4e0bf
parent 01a10553
Loading
Loading
Loading
Loading
+90 −0
Original line number Original line Diff line number Diff line
@@ -42,6 +42,7 @@ import android.app.admin.EnforcingAdmin;
import android.app.admin.RoleAuthority;
import android.app.admin.RoleAuthority;
import android.app.admin.SystemAuthority;
import android.app.admin.SystemAuthority;
import android.app.admin.UnknownAuthority;
import android.app.admin.UnknownAuthority;
import android.app.admin.flags.Flags;
import android.content.ComponentName;
import android.content.ComponentName;
import android.content.Context;
import android.content.Context;
import android.content.Intent;
import android.content.Intent;
@@ -53,6 +54,7 @@ import android.platform.test.annotations.RequiresFlagsDisabled;
import android.platform.test.annotations.RequiresFlagsEnabled;
import android.platform.test.annotations.RequiresFlagsEnabled;
import android.platform.test.flag.junit.CheckFlagsRule;
import android.platform.test.flag.junit.CheckFlagsRule;
import android.platform.test.flag.junit.DeviceFlagsValueProvider;
import android.platform.test.flag.junit.DeviceFlagsValueProvider;
import android.provider.Settings;


import org.junit.Before;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Rule;
@@ -495,6 +497,94 @@ public class RestrictedLockUtilsTest {
                .isNull();
                .isNull();
    }
    }


    private static final ComponentName ENFORCING_ADMIN_COMPONENT =
            new ComponentName("test.pkg", "test.class");
    private static final UserHandle ENFORCING_ADMIN_USER_HANDLE = UserHandle.of(10);
    private static final EnforcingAdmin ENFORCING_ADMIN = new EnforcingAdmin(
            ENFORCING_ADMIN_COMPONENT.getPackageName(),
            UnknownAuthority.UNKNOWN_AUTHORITY,
            ENFORCING_ADMIN_USER_HANDLE,
            ENFORCING_ADMIN_COMPONENT);
    private static final EnforcingAdmin ENFORCING_ADMIN_NO_COMPONENT = new EnforcingAdmin(
            ENFORCING_ADMIN_COMPONENT.getPackageName(),
            UnknownAuthority.UNKNOWN_AUTHORITY,
            ENFORCING_ADMIN_USER_HANDLE,
            /* componentName= */ null);

    @Test
    public void getShowAdminSupportDetailsIntent_withEnforcingAdmin_nullAdmin() {
        // Call the method with a null admin
        Intent intent = RestrictedLockUtils.getShowAdminSupportDetailsIntent((EnforcingAdmin) null);

        // Verify the intent action is correct
        assertThat(intent.getAction()).isEqualTo(Settings.ACTION_SHOW_ADMIN_SUPPORT_DETAILS);
        // Verify no extras are added
        assertThat(intent.getExtras()).isNull();
    }

    @Test
    @RequiresFlagsEnabled(Flags.FLAG_ENFORCING_ADMIN_EXTRA_ENABLED)
    public void getShowAdminSupportDetailsIntent_withEnforcingAdmin_extraFlagEnabled() {
        // Call the method
        Intent intent = RestrictedLockUtils.getShowAdminSupportDetailsIntent(ENFORCING_ADMIN);

        // Verify the intent contains the EXTRA_ENFORCING_ADMIN extra
        assertThat(intent.getParcelableExtra(DevicePolicyManager.EXTRA_ENFORCING_ADMIN,
                EnforcingAdmin.class)).isEqualTo(ENFORCING_ADMIN);
    }

    @Test
    @RequiresFlagsDisabled(Flags.FLAG_ENFORCING_ADMIN_EXTRA_ENABLED)
    public void getShowAdminSupportDetailsIntent_withEnforcingAdmin_extraFlagDisabled() {
        // Call the method
        Intent intent = RestrictedLockUtils.getShowAdminSupportDetailsIntent(ENFORCING_ADMIN);

        // Verify the intent does not contain the EXTRA_ENFORCING_ADMIN extra
        assertThat(intent.hasExtra(DevicePolicyManager.EXTRA_ENFORCING_ADMIN)).isFalse();
    }

    @Test
    @RequiresFlagsEnabled(Flags.FLAG_ENFORCING_ADMIN_GET_COMPONENT_NAME_ENABLED)
    public void getShowAdminSupportDetailsIntent_withEnforcingAdmin_componentNameFlagEnabled() {
        // Call the method
        Intent intent = RestrictedLockUtils.getShowAdminSupportDetailsIntent(ENFORCING_ADMIN);

        // Verify the intent contains the EXTRA_DEVICE_ADMIN extra
        assertThat(intent.getParcelableExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
                ComponentName.class)).isEqualTo(ENFORCING_ADMIN_COMPONENT);
    }

    @Test
    @RequiresFlagsEnabled(Flags.FLAG_ENFORCING_ADMIN_GET_COMPONENT_NAME_ENABLED)
    public void getShowAdminSupportDetailsIntent_withEnforcingAdmin_componentNameNull_flagEnabled() {
        // Call the method with an admin that has no component name
        Intent intent = RestrictedLockUtils.getShowAdminSupportDetailsIntent(
                ENFORCING_ADMIN_NO_COMPONENT);

        // Verify the intent does not contain the EXTRA_DEVICE_ADMIN extra
        assertThat(intent.hasExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN)).isFalse();
    }

    @Test
    @RequiresFlagsDisabled(Flags.FLAG_ENFORCING_ADMIN_GET_COMPONENT_NAME_ENABLED)
    public void getShowAdminSupportDetailsIntent_withEnforcingAdmin_componentNameFlagDisabled() {
        // Call the method
        Intent intent = RestrictedLockUtils.getShowAdminSupportDetailsIntent(ENFORCING_ADMIN);

        // Verify the intent does not contain the EXTRA_DEVICE_ADMIN extra
        assertThat(intent.hasExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN)).isFalse();
    }

    @Test
    public void getShowAdminSupportDetailsIntent_withEnforcingAdmin_alwaysHasUserExtra() {
        // Call the method
        Intent intent = RestrictedLockUtils.getShowAdminSupportDetailsIntent(ENFORCING_ADMIN);

        // Verify the intent always contains the user handle extra
        assertThat(intent.getParcelableExtra(Intent.EXTRA_USER, UserHandle.class))
                .isEqualTo(ENFORCING_ADMIN_USER_HANDLE);
    }

    private UserInfo setUpUser(int userId, ComponentName[] admins) {
    private UserInfo setUpUser(int userId, ComponentName[] admins) {
        UserInfo userInfo = new UserInfo(userId, "primary", 0);
        UserInfo userInfo = new UserInfo(userId, "primary", 0);
        when(mUserManager.getUserInfo(userId)).thenReturn(userInfo);
        when(mUserManager.getUserInfo(userId)).thenReturn(userInfo);