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

Commit b51940b4 authored by Chaohui Wang's avatar Chaohui Wang
Browse files

Add enabled to AlertDialogButton

Also add enabled to RestrictedMenuItem.

Bug: 342627537
Test: unit test
Change-Id: I6122a5141f5918bf4650dee41211d8256df55022
parent 0cde3ea0
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ import androidx.compose.ui.window.DialogProperties

data class AlertDialogButton(
    val text: String,
    val enabled: Boolean = true,
    val onClick: () -> Unit = {},
)

@@ -114,6 +115,7 @@ private fun AlertDialogPresenter.Button(button: AlertDialogButton) {
            close()
            button.onClick()
        },
        enabled = button.enabled,
    ) {
        Text(button.text)
    }
+26 −2
Original line number Diff line number Diff line
@@ -20,6 +20,8 @@ import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.assertIsEnabled
import androidx.compose.ui.test.assertIsNotEnabled
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.performClick
import androidx.test.ext.junit.runners.AndroidJUnit4
@@ -67,7 +69,18 @@ class SettingsAlertDialogTest {
            rememberAlertDialogPresenter(confirmButton = AlertDialogButton(CONFIRM_TEXT))
        }

        composeTestRule.onDialogText(CONFIRM_TEXT).assertIsDisplayed()
        composeTestRule.onDialogText(CONFIRM_TEXT).assertIsDisplayed().assertIsEnabled()
    }

    @Test
    fun confirmButton_disabled() {
        setAndOpenDialog {
            rememberAlertDialogPresenter(
                confirmButton = AlertDialogButton(text = CONFIRM_TEXT, enabled = false)
            )
        }

        composeTestRule.onDialogText(CONFIRM_TEXT).assertIsDisplayed().assertIsNotEnabled()
    }

    @Test
@@ -90,7 +103,18 @@ class SettingsAlertDialogTest {
            rememberAlertDialogPresenter(dismissButton = AlertDialogButton(DISMISS_TEXT))
        }

        composeTestRule.onDialogText(DISMISS_TEXT).assertIsDisplayed()
        composeTestRule.onDialogText(DISMISS_TEXT).assertIsDisplayed().assertIsEnabled()
    }

    @Test
    fun dismissButton_disabled() {
        setAndOpenDialog {
            rememberAlertDialogPresenter(
                dismissButton = AlertDialogButton(text = DISMISS_TEXT, enabled = false)
            )
        }

        composeTestRule.onDialogText(DISMISS_TEXT).assertIsDisplayed().assertIsNotEnabled()
    }

    @Test
+4 −2
Original line number Diff line number Diff line
@@ -30,22 +30,24 @@ import com.android.settingslib.spaprivileged.model.enterprise.rememberRestricted
@Composable
fun MoreOptionsScope.RestrictedMenuItem(
    text: String,
    enabled: Boolean = true,
    restrictions: Restrictions,
    onClick: () -> Unit,
) {
    RestrictedMenuItemImpl(text, restrictions, onClick, ::RestrictionsProviderImpl)
    RestrictedMenuItemImpl(text, enabled, restrictions, onClick, ::RestrictionsProviderImpl)
}

@VisibleForTesting
@Composable
internal fun MoreOptionsScope.RestrictedMenuItemImpl(
    text: String,
    enabled: Boolean = true,
    restrictions: Restrictions,
    onClick: () -> Unit,
    restrictionsProviderFactory: RestrictionsProviderFactory,
) {
    val restrictedMode = restrictionsProviderFactory.rememberRestrictedMode(restrictions).value
    MenuItem(text = text, enabled = restrictedMode !== BaseUserRestricted) {
    MenuItem(text = text, enabled = enabled && restrictedMode !== BaseUserRestricted) {
        when (restrictedMode) {
            is BlockedByAdmin -> restrictedMode.sendShowAdminSupportDetailsIntent()
            is BlockedByEcm -> restrictedMode.showRestrictedSettingsDetails()
+11 −1
Original line number Diff line number Diff line
@@ -48,6 +48,15 @@ class RestrictedMenuItemTest {

    private var menuItemOnClickIsCalled = false

    @Test
    fun whenDisabled() {
        val restrictions = Restrictions(userId = USER_ID, keys = emptyList())

        setContent(restrictions, enabled = false)

        composeTestRule.onNodeWithText(TEXT).assertIsDisplayed().assertIsNotEnabled()
    }

    @Test
    fun whenRestrictionsKeysIsEmpty_enabled() {
        val restrictions = Restrictions(userId = USER_ID, keys = emptyList())
@@ -153,13 +162,14 @@ class RestrictedMenuItemTest {
        assertThat(menuItemOnClickIsCalled).isFalse()
    }

    private fun setContent(restrictions: Restrictions) {
    private fun setContent(restrictions: Restrictions, enabled: Boolean = true) {
        val fakeMoreOptionsScope = object : MoreOptionsScope() {
            override fun dismiss() {}
        }
        composeTestRule.setContent {
            fakeMoreOptionsScope.RestrictedMenuItemImpl(
                text = TEXT,
                enabled = enabled,
                restrictions = restrictions,
                onClick = { menuItemOnClickIsCalled = true },
                restrictionsProviderFactory = { _, _ -> fakeRestrictionsProvider },