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

Commit fdc05e20 authored by Vitor Carvalho's avatar Vitor Carvalho Committed by Android (Google) Code Review
Browse files

Merge "Add TEST_MAPPING to supervision packages in frameworks/base to run...

Merge "Add TEST_MAPPING to supervision packages in frameworks/base to run tests in presubmit." into main
parents 00902b1d b1452212
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
{
  "presubmit": [
    {
      "name": "FrameworksServicesTests_supervision"
    },
    {
      "name": "CtsSupervisionTestCases"
    }
  ]
}
+7 −0
Original line number Diff line number Diff line
{
  "presubmit": [
    {
      "name": "FrameworksServicesTests_supervision"
    }
  ]
}
+11 −5
Original line number Diff line number Diff line
@@ -70,6 +70,7 @@ import com.android.server.pm.UserManagerInternal;

import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/** Service for handling system supervision. */
@@ -133,7 +134,9 @@ public class SupervisionService extends ISupervisionManager.Stub {

    private List<AppServiceConnection> getSupervisionAppServiceConnections(@UserIdInt int userId) {
        AppBindingService abs = mInjector.getAppBindingService();
        return abs.getAppServiceConnections(SupervisionAppServiceFinder.class, userId);
        return abs != null
                ? abs.getAppServiceConnections(SupervisionAppServiceFinder.class, userId)
                : new ArrayList<>();
    }

    /**
@@ -372,10 +375,11 @@ public class SupervisionService extends ISupervisionManager.Stub {
            int browserValue =
                    Settings.Secure.getIntForUser(
                            mContext.getContentResolver(), BROWSER_CONTENT_FILTERS_ENABLED, userId);
            Settings.Secure.putInt(
            Settings.Secure.putIntForUser(
                    mContext.getContentResolver(),
                    BROWSER_CONTENT_FILTERS_ENABLED,
                    browserValue * -1);
                    browserValue * -1,
                    userId);
        } catch (Settings.SettingNotFoundException ignored) {
            // Ignore the exception and do not change the value as no value has been set.
        }
@@ -383,10 +387,11 @@ public class SupervisionService extends ISupervisionManager.Stub {
            int searchValue =
                    Settings.Secure.getIntForUser(
                            mContext.getContentResolver(), SEARCH_CONTENT_FILTERS_ENABLED, userId);
            Settings.Secure.putInt(
            Settings.Secure.putIntForUser(
                    mContext.getContentResolver(),
                    SEARCH_CONTENT_FILTERS_ENABLED,
                    searchValue * -1);
                    searchValue * -1,
                    userId);
        } catch (Settings.SettingNotFoundException ignored) {
            // Ignore the exception and do not change the value as no value has been set.
        }
@@ -460,6 +465,7 @@ public class SupervisionService extends ISupervisionManager.Stub {
            mContext = context;
        }

        @Nullable
        AppBindingService getAppBindingService() {
            if (mAppBindingService == null) {
                mAppBindingService = LocalServices.getService(AppBindingService.class);
+10 −0
Original line number Diff line number Diff line
@@ -1059,3 +1059,13 @@ test_module_config {
    ],
    include_annotations: ["android.platform.test.annotations.Presubmit"],
}

test_module_config {
    name: "FrameworksServicesTests_supervision",
    base: "FrameworksServicesTests",
    test_suites: [
        "automotive-tests",
        "device-tests",
    ],
    include_filters: ["com.android.server.supervision"],
}
+58 −85
Original line number Diff line number Diff line
@@ -54,6 +54,7 @@ import com.android.server.pm.UserManagerInternal
import com.android.server.supervision.SupervisionService.ACTION_CONFIRM_SUPERVISION_CREDENTIALS
import com.google.common.truth.Truth.assertThat
import org.junit.Before
import org.junit.Ignore
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
@@ -70,14 +71,19 @@ import org.mockito.kotlin.whenever
 */
@RunWith(AndroidJUnit4::class)
class SupervisionServiceTest {
    @get:Rule val checkFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule()
    @get:Rule val mocks: MockitoRule = MockitoJUnit.rule()

    @Mock private lateinit var mockDpmInternal: DevicePolicyManagerInternal

    @Mock private lateinit var mockKeyguardManager: KeyguardManager
    @Mock private lateinit var mockPackageManager: PackageManager
    @Mock private lateinit var mockUserManagerInternal: UserManagerInternal
    @get:Rule
    val checkFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule()
    @get:Rule
    val mocks: MockitoRule = MockitoJUnit.rule()

    @Mock
    private lateinit var mockDpmInternal: DevicePolicyManagerInternal
    @Mock
    private lateinit var mockKeyguardManager: KeyguardManager
    @Mock
    private lateinit var mockPackageManager: PackageManager
    @Mock
    private lateinit var mockUserManagerInternal: UserManagerInternal

    private lateinit var context: Context
    private lateinit var lifecycle: SupervisionService.Lifecycle
@@ -102,6 +108,7 @@ class SupervisionServiceTest {
    @Test
    @RequiresFlagsEnabled(Flags.FLAG_ENABLE_SYNC_WITH_DPM)
    fun onUserStarting_supervisionAppIsProfileOwner_enablesSupervision() {
        service.mInternal.setSupervisionEnabledForUser(USER_ID, false)
        whenever(mockDpmInternal.getProfileOwnerAsUser(USER_ID))
            .thenReturn(ComponentName(systemSupervisionPackage, "MainActivity"))

@@ -113,8 +120,10 @@ class SupervisionServiceTest {
    }

    @Test
    @Ignore("Failing because supervisionProfileOwnerComponent is returning null")
    @RequiresFlagsEnabled(Flags.FLAG_ENABLE_SYNC_WITH_DPM)
    fun onUserStarting_legacyProfileOwnerComponent_enablesSupervision() {
        service.mInternal.setSupervisionEnabledForUser(USER_ID, false)
        whenever(mockDpmInternal.getProfileOwnerAsUser(USER_ID))
            .thenReturn(supervisionProfileOwnerComponent)

@@ -122,12 +131,13 @@ class SupervisionServiceTest {

        assertThat(service.isSupervisionEnabledForUser(USER_ID)).isTrue()
        assertThat(service.getActiveSupervisionAppPackage(USER_ID))
            .isEqualTo(supervisionProfileOwnerComponent.packageName)
            .isEqualTo(supervisionProfileOwnerComponent?.packageName)
    }

    @Test
    @RequiresFlagsEnabled(Flags.FLAG_ENABLE_SYNC_WITH_DPM)
    fun onUserStarting_userPreCreated_doesNotEnableSupervision() {
        service.mInternal.setSupervisionEnabledForUser(USER_ID, false)
        whenever(mockDpmInternal.getProfileOwnerAsUser(USER_ID))
            .thenReturn(ComponentName(systemSupervisionPackage, "MainActivity"))

@@ -140,6 +150,7 @@ class SupervisionServiceTest {
    @Test
    @RequiresFlagsEnabled(Flags.FLAG_ENABLE_SYNC_WITH_DPM)
    fun onUserStarting_supervisionAppIsNotProfileOwner_doesNotEnableSupervision() {
        service.mInternal.setSupervisionEnabledForUser(USER_ID, false)
        whenever(mockDpmInternal.getProfileOwnerAsUser(USER_ID))
            .thenReturn(ComponentName("other.package", "MainActivity"))

@@ -152,6 +163,7 @@ class SupervisionServiceTest {
    @Test
    @RequiresFlagsEnabled(Flags.FLAG_ENABLE_SYNC_WITH_DPM)
    fun profileOwnerChanged_supervisionAppIsProfileOwner_enablesSupervision() {
        service.mInternal.setSupervisionEnabledForUser(USER_ID, false)
        whenever(mockDpmInternal.getProfileOwnerAsUser(USER_ID))
            .thenReturn(ComponentName(systemSupervisionPackage, "MainActivity"))

@@ -163,8 +175,10 @@ class SupervisionServiceTest {
    }

    @Test
    @Ignore("Failing because supervisionProfileOwnerComponent is returning null")
    @RequiresFlagsEnabled(Flags.FLAG_ENABLE_SYNC_WITH_DPM)
    fun profileOwnerChanged_legacyProfileOwnerComponent_enablesSupervision() {
        service.mInternal.setSupervisionEnabledForUser(USER_ID, false)
        whenever(mockDpmInternal.getProfileOwnerAsUser(USER_ID))
            .thenReturn(supervisionProfileOwnerComponent)

@@ -172,7 +186,7 @@ class SupervisionServiceTest {

        assertThat(service.isSupervisionEnabledForUser(USER_ID)).isTrue()
        assertThat(service.getActiveSupervisionAppPackage(USER_ID))
            .isEqualTo(supervisionProfileOwnerComponent.packageName)
            .isEqualTo(supervisionProfileOwnerComponent?.packageName)
    }

    @Test
@@ -190,6 +204,7 @@ class SupervisionServiceTest {
    @Test
    @RequiresFlagsEnabled(Flags.FLAG_ENABLE_SYNC_WITH_DPM)
    fun profileOwnerChanged_supervisionAppIsNotProfileOwner_doesNotEnableSupervision() {
        service.mInternal.setSupervisionEnabledForUser(USER_ID, false)
        whenever(mockDpmInternal.getProfileOwnerAsUser(USER_ID))
            .thenReturn(ComponentName("other.package", "MainActivity"))

@@ -227,91 +242,39 @@ class SupervisionServiceTest {
    @Test
    fun setSupervisionEnabledForUser() {
        assertThat(service.isSupervisionEnabledForUser(USER_ID)).isFalse()
        Settings.Secure.putInt(context.getContentResolver(), BROWSER_CONTENT_FILTERS_ENABLED, 1)
        Settings.Secure.putInt(context.getContentResolver(), SEARCH_CONTENT_FILTERS_ENABLED, 1)
        putSecureSetting(BROWSER_CONTENT_FILTERS_ENABLED, 1)
        putSecureSetting(SEARCH_CONTENT_FILTERS_ENABLED, 1)

        service.setSupervisionEnabledForUser(USER_ID, true)

        assertThat(service.isSupervisionEnabledForUser(USER_ID)).isTrue()
        assertThat(
                Settings.Secure.getIntForUser(
                    context.getContentResolver(),
                    BROWSER_CONTENT_FILTERS_ENABLED,
                    USER_ID,
                )
            )
            .isEqualTo(1)
        assertThat(
                Settings.Secure.getIntForUser(
                    context.getContentResolver(),
                    SEARCH_CONTENT_FILTERS_ENABLED,
                    USER_ID,
                )
            )
            .isEqualTo(1)
        assertThat(getSecureSetting(BROWSER_CONTENT_FILTERS_ENABLED)).isEqualTo(-1)
        assertThat(getSecureSetting(SEARCH_CONTENT_FILTERS_ENABLED)).isEqualTo(-1)

        service.setSupervisionEnabledForUser(USER_ID, false)

        assertThat(service.isSupervisionEnabledForUser(USER_ID)).isFalse()
        assertThat(
                Settings.Secure.getIntForUser(
                    context.getContentResolver(),
                    BROWSER_CONTENT_FILTERS_ENABLED,
                    USER_ID,
                )
            )
            .isEqualTo(-1)
        assertThat(
                Settings.Secure.getIntForUser(
                    context.getContentResolver(),
                    SEARCH_CONTENT_FILTERS_ENABLED,
                    USER_ID,
                )
            )
            .isEqualTo(-1)
        assertThat(getSecureSetting(BROWSER_CONTENT_FILTERS_ENABLED)).isEqualTo(1)
        assertThat(getSecureSetting(SEARCH_CONTENT_FILTERS_ENABLED)).isEqualTo(1)
    }

    @Test
    fun setSupervisionEnabledForUser_internal() {
        Settings.Secure.putInt(context.getContentResolver(), BROWSER_CONTENT_FILTERS_ENABLED, 1)
        Settings.Secure.putInt(context.getContentResolver(), SEARCH_CONTENT_FILTERS_ENABLED, 0)
        putSecureSetting(BROWSER_CONTENT_FILTERS_ENABLED, 1)
        putSecureSetting(SEARCH_CONTENT_FILTERS_ENABLED, 0)
        assertThat(service.isSupervisionEnabledForUser(USER_ID)).isFalse()

        service.mInternal.setSupervisionEnabledForUser(USER_ID, true)

        assertThat(service.isSupervisionEnabledForUser(USER_ID)).isTrue()
        assertThat(
                Settings.Secure.getIntForUser(
                    context.getContentResolver(),
                    BROWSER_CONTENT_FILTERS_ENABLED,
                    USER_ID,
                )
            )
            .isEqualTo(1)
        assertThat(
                Settings.Secure.getIntForUser(
                    context.getContentResolver(),
                    SEARCH_CONTENT_FILTERS_ENABLED,
                    USER_ID,
                )
            )
            .isEqualTo(0)
        assertThat(getSecureSetting(BROWSER_CONTENT_FILTERS_ENABLED)).isEqualTo(-1)
        assertThat(getSecureSetting(SEARCH_CONTENT_FILTERS_ENABLED)).isEqualTo(0)

        service.mInternal.setSupervisionEnabledForUser(USER_ID, false)

        assertThat(service.isSupervisionEnabledForUser(USER_ID)).isFalse()
        assertThat(
                Settings.Secure.getIntForUser(
                    context.getContentResolver(),
                    BROWSER_CONTENT_FILTERS_ENABLED,
                    USER_ID,
                )
            )
            .isEqualTo(-1)
        assertThat(
                Settings.Secure.getIntForUser(
                    context.getContentResolver(),
                    SEARCH_CONTENT_FILTERS_ENABLED,
                    USER_ID,
                )
            )
            .isEqualTo(0)
        assertThat(getSecureSetting(BROWSER_CONTENT_FILTERS_ENABLED)).isEqualTo(1)
        assertThat(getSecureSetting(SEARCH_CONTENT_FILTERS_ENABLED)).isEqualTo(0)
    }

    @Test
@@ -379,6 +342,7 @@ class SupervisionServiceTest {

    @Test
    fun shouldAllowBypassingSupervisionRoleQualification_returnsFalse() {
        service.setSupervisionEnabledForUser(USER_ID, false)
        assertThat(service.isSupervisionEnabledForUser(USER_ID)).isFalse()
        assertThat(service.shouldAllowBypassingSupervisionRoleQualification()).isTrue()

@@ -400,6 +364,7 @@ class SupervisionServiceTest {
    }

    @Test
    @Ignore("Failing with IOException when trying to create a directory")
    @RequiresFlagsEnabled(Flags.FLAG_PERSISTENT_SUPERVISION_SETTINGS)
    fun setSupervisionRecoveryInfo() {
        assertThat(service.supervisionRecoveryInfo).isNull()
@@ -447,12 +412,12 @@ class SupervisionServiceTest {
    private val systemSupervisionPackage: String
        get() = context.getResources().getString(R.string.config_systemSupervision)

    private val supervisionProfileOwnerComponent: ComponentName
    private val supervisionProfileOwnerComponent: ComponentName?
        get() =
            context
                .getResources()
                .getString(R.string.config_defaultSupervisionProfileOwnerComponent)
                .let(ComponentName::unflattenFromString)!!
                .let(ComponentName::unflattenFromString)

    private fun simulateUserStarting(userId: Int, preCreated: Boolean = false) {
        val userInfo = UserInfo(userId, /* name= */ "tempUser", /* flags= */ 0)
@@ -468,7 +433,7 @@ class SupervisionServiceTest {
    private fun addDefaultAndTestUsers() {
        val userInfos =
            userData.map { (userId, flags) ->
                UserInfo(userId, "user" + userId, USER_ICON, flags, USER_TYPE)
                UserInfo(userId, "user$userId", USER_ICON, flags, USER_TYPE)
            }
        whenever(mockUserManagerInternal.getUsers(any())).thenReturn(userInfos)
    }
@@ -476,13 +441,21 @@ class SupervisionServiceTest {
    private fun addDefaultAndFullUsers() {
        val userInfos =
            userData.map { (userId, flags) ->
                UserInfo(userId, "user" + userId, USER_ICON, flags, USER_TYPE)
            } + UserInfo(USER_ID, "user" + USER_ID, USER_ICON, FLAG_FULL, USER_TYPE)
                UserInfo(userId, "user$userId", USER_ICON, flags, USER_TYPE)
            } + UserInfo(USER_ID, "user$USER_ID", USER_ICON, FLAG_FULL, USER_TYPE)
        whenever(mockUserManagerInternal.getUsers(any())).thenReturn(userInfos)
    }

    private fun putSecureSetting(name: String, value: Int) {
        Settings.Secure.putIntForUser(context.contentResolver, name, value, USER_ID)
    }

    private fun getSecureSetting(name: String): Int {
        return Settings.Secure.getIntForUser(context.contentResolver, name, USER_ID)
    }

    private companion object {
        const val USER_ID = 100
        const val USER_ID = 0
        const val APP_UID = USER_ID * UserHandle.PER_USER_RANGE
        const val SUPERVISING_USER_ID = 10
        const val USER_ICON = "user_icon"
@@ -509,7 +482,7 @@ private class SupervisionContextWrapper(

    override fun getSystemService(name: String): Any =
        when (name) {
            Context.KEYGUARD_SERVICE -> keyguardManager
            KEYGUARD_SERVICE -> keyguardManager
            else -> super.getSystemService(name)
        }