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

Commit 45aa574b authored by Chaohui Wang's avatar Chaohui Wang Committed by Android (Google) Code Review
Browse files

Merge "Show uncheck toggle when wep not supported" into main

parents bf0b93b1 c311354d
Loading
Loading
Loading
Loading
+51 −43
Original line number Diff line number Diff line
@@ -42,6 +42,8 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.asExecutor
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn

/** Controller that controls whether the WEP network can be connected. */
class WepNetworksPreferenceController(context: Context, preferenceKey: String) :
@@ -49,21 +51,28 @@ class WepNetworksPreferenceController(context: Context, preferenceKey: String) :

    var wifiManager = context.getSystemService(WifiManager::class.java)!!

    override fun getAvailabilityStatus() = if (Flags.androidVWifiApi()) AVAILABLE
    else UNSUPPORTED_ON_DEVICE
    override fun getAvailabilityStatus() =
        if (Flags.androidVWifiApi()) AVAILABLE else UNSUPPORTED_ON_DEVICE

    @Composable
    override fun Content() {
        val checked by wepAllowedFlow.flow.collectAsStateWithLifecycle(initialValue = null)
        val isWepSupported: Boolean? =
            isWepSupportedFlow.collectAsStateWithLifecycle(initialValue = null).value
        val isWepAllowed: Boolean? =
            wepAllowedFlow.flow.collectAsStateWithLifecycle(initialValue = null).value
        var openDialog by rememberSaveable { mutableStateOf(false) }
        val wifiInfo = wifiManager.connectionInfo
        SwitchPreference(object : SwitchPreferenceModel {
        SwitchPreference(
            object : SwitchPreferenceModel {
                override val title = stringResource(R.string.wifi_allow_wep_networks)
            override val summary = { getSummary() }
            override val checked = { checked }
                override val summary = { getSummary(isWepSupported) }
                override val checked = {
                    if (isWepSupported == true) isWepAllowed else isWepSupported
                }
                override val changeable: () -> Boolean
                get() = { carrierAllowed }
                    get() = { isWepSupported == true }

                override val onCheckedChange: (Boolean) -> Unit = { newChecked ->
                    val wifiInfo = wifiManager.connectionInfo
                    if (!newChecked && wifiInfo.currentSecurityType == WifiEntry.SECURITY_WEP) {
                        openDialog = true
                    } else {
@@ -75,40 +84,39 @@ class WepNetworksPreferenceController(context: Context, preferenceKey: String) :
        if (openDialog) {
            SettingsAlertDialogWithIcon(
                onDismissRequest = { openDialog = false },
                confirmButton = AlertDialogButton(
                    stringResource(R.string.sim_action_yes)
                ) {
                confirmButton =
                    AlertDialogButton(stringResource(R.string.sim_action_yes)) {
                        wifiManager.setWepAllowed(false)
                        wepAllowedFlow.override(false)
                        openDialog = false
                    },
                dismissButton =
                AlertDialogButton(
                    stringResource(R.string.wifi_cancel)
                ) { openDialog = false },
                    AlertDialogButton(stringResource(R.string.wifi_cancel)) { openDialog = false },
                title = stringResource(R.string.wifi_settings_wep_networks_disconnect_title),
                text = {
                    Text(
                        stringResource(R.string.wifi_settings_wep_networks_disconnect_summary),
                        modifier = Modifier.fillMaxWidth(),
                        textAlign = TextAlign.Center
                        textAlign = TextAlign.Center,
                    )
                })
        }
    }

    override fun getSummary(): String = mContext.getString(
        if (carrierAllowed) {
            R.string.wifi_allow_wep_networks_summary
        } else {
            R.string.wifi_allow_wep_networks_summary_carrier_not_allow
        }
    )
    private fun getSummary(isWepSupported: Boolean?): String =
        mContext.getString(
            when (isWepSupported) {
                true -> R.string.wifi_allow_wep_networks_summary
                false -> R.string.wifi_allow_wep_networks_summary_carrier_not_allow
                null -> R.string.summary_placeholder
            })

    private val carrierAllowed: Boolean
        get() = wifiManager.isWepSupported
    private val isWepSupportedFlow =
        flow { emit(wifiManager.isWepSupported) }.flowOn(Dispatchers.Default)

    val wepAllowedFlow = OverridableFlow(callbackFlow {
    val wepAllowedFlow =
        OverridableFlow(
            callbackFlow {
                wifiManager.queryWepAllowed(Dispatchers.Default.asExecutor(), ::trySend)

                awaitClose {}
+71 −42
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.settings.wifi

import android.content.Context
import android.net.wifi.WifiInfo
import android.net.wifi.WifiManager
import androidx.compose.ui.test.assertIsOff
import androidx.compose.ui.test.assertIsOn
@@ -30,7 +31,6 @@ import androidx.preference.PreferenceManager
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.settings.R
import com.android.settings.dashboard.DashboardFragment
import com.android.settings.spa.preference.ComposePreference
import com.android.settingslib.spa.testutils.onDialogText
import com.android.wifitrackerlib.WifiEntry
@@ -45,28 +45,30 @@ import org.mockito.kotlin.doAnswer
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock
import org.mockito.kotlin.spy
import org.mockito.kotlin.stub

@RunWith(AndroidJUnit4::class)
class WepNetworksPreferenceControllerTest {

    @get:Rule
    val composeTestRule = createComposeRule()
    @get:Rule val composeTestRule = createComposeRule()

    private var wepAllowed = true

    private var mockWifiInfo = mock<android.net.wifi.WifiInfo> {
        on { it.currentSecurityType } doReturn WifiEntry.SECURITY_EAP
        on { it.ssid } doReturn SSID
    private var mockWifiInfo =
        mock<WifiInfo> {
            on { currentSecurityType } doReturn WifiEntry.SECURITY_EAP
            on { ssid } doReturn SSID
        }

    private var mockWifiManager = mock<WifiManager> {
        on { queryWepAllowed(any(), any()) } doAnswer {
            @Suppress("UNCHECKED_CAST")
            val consumer = it.arguments[1] as Consumer<Boolean>
    private var mockWifiManager =
        mock<WifiManager> {
            on { queryWepAllowed(any(), any()) } doAnswer
                {
                    @Suppress("UNCHECKED_CAST") val consumer = it.arguments[1] as Consumer<Boolean>
                    consumer.accept(wepAllowed)
                }
        on { it.isWepSupported } doReturn true
        on { it.connectionInfo } doReturn mockWifiInfo
            on { isWepSupported } doReturn true
            on { connectionInfo } doReturn mockWifiInfo
        }

    private var context: Context =
@@ -85,74 +87,101 @@ class WepNetworksPreferenceControllerTest {
    }

    @Test
    fun wepAllowedTrue_turnOn() {
    fun isChecked_wepSupportedAndAllowed_isOn() {
        mockWifiManager.stub { on { isWepSupported } doReturn true }
        wepAllowed = true
        composeTestRule.setContent {
            controller.Content()
        }

        composeTestRule.onNodeWithText(context.getString(R.string.wifi_allow_wep_networks))
        composeTestRule.setContent { controller.Content() }

        composeTestRule
            .onNodeWithText(context.getString(R.string.wifi_allow_wep_networks))
            .assertIsOn()
    }

    @Test
    fun wepAllowedFalse_turnOff() {
    fun isChecked_wepSupportedAndNotAllowed_isOff() {
        mockWifiManager.stub { on { isWepSupported } doReturn true }
        wepAllowed = false
        composeTestRule.setContent {
            controller.Content()

        composeTestRule.setContent { controller.Content() }

        composeTestRule
            .onNodeWithText(context.getString(R.string.wifi_allow_wep_networks))
            .assertIsOff()
    }

        composeTestRule.onNodeWithText(context.getString(R.string.wifi_allow_wep_networks))
    @Test
    fun isChecked_wepNotSupportedAndAllowed_isOff() {
        mockWifiManager.stub { on { isWepSupported } doReturn false }
        wepAllowed = true

        composeTestRule.setContent { controller.Content() }

        composeTestRule
            .onNodeWithText(context.getString(R.string.wifi_allow_wep_networks))
            .assertIsOff()
    }

    @Test
    fun onClick_turnOn() {
    fun isChecked_wepNotSupportedAndNotAllowed_isOff() {
        mockWifiManager.stub { on { isWepSupported } doReturn false }
        wepAllowed = false
        composeTestRule.setContent {
            controller.Content()

        composeTestRule.setContent { controller.Content() }

        composeTestRule
            .onNodeWithText(context.getString(R.string.wifi_allow_wep_networks))
            .assertIsOff()
    }

    @Test
    fun onClick_turnOn() {
        wepAllowed = false
        composeTestRule.setContent { controller.Content() }

        composeTestRule.onRoot().performClick()
        composeTestRule.onNodeWithText(context.getString(R.string.wifi_allow_wep_networks))
        composeTestRule
            .onNodeWithText(context.getString(R.string.wifi_allow_wep_networks))
            .assertIsOn()
    }

    @Test
    fun onClick_turnOff() {
        wepAllowed = true
        composeTestRule.setContent {
            controller.Content()
        }
        composeTestRule.setContent { controller.Content() }

        composeTestRule.onRoot().performClick()
        composeTestRule.onNodeWithText(context.getString(R.string.wifi_allow_wep_networks))
        composeTestRule
            .onNodeWithText(context.getString(R.string.wifi_allow_wep_networks))
            .assertIsOff()
    }

    @Test
    fun whenClick_wepAllowed_openDialog() {
        wepAllowed = true
        Mockito.`when`(mockWifiInfo.currentSecurityType).thenReturn(WifiEntry.SECURITY_WEP)
        composeTestRule.setContent {
            controller.Content()
        mockWifiInfo.stub {
            on { currentSecurityType } doReturn WifiEntry.SECURITY_WEP
        }
        composeTestRule.setContent { controller.Content() }

        composeTestRule.onRoot().performClick()
        composeTestRule.onDialogText(context.getString(R.string.wifi_disconnect_button_text))
        composeTestRule
            .onDialogText(context.getString(R.string.wifi_disconnect_button_text))
            .isDisplayed()
    }

    @Test
    fun whenClick_wepDisallowed_openDialog() {
        wepAllowed = false
        Mockito.`when`(mockWifiInfo.currentSecurityType).thenReturn(WifiEntry.SECURITY_WEP)
        composeTestRule.setContent {
            controller.Content()
        mockWifiInfo.stub {
            on { currentSecurityType } doReturn WifiEntry.SECURITY_WEP
        }

        composeTestRule.setContent { controller.Content() }

        composeTestRule.onRoot().performClick()
        composeTestRule.onDialogText(context.getString(R.string.wifi_disconnect_button_text))
        composeTestRule
            .onDialogText(context.getString(R.string.wifi_disconnect_button_text))
            .isNotDisplayed()
    }