diff --git a/src/com/android/settings/wifi/WepNetworksPreferenceController.kt b/src/com/android/settings/wifi/WepNetworksPreferenceController.kt index 2ce4bcd4d4f..bad7201128c 100644 --- a/src/com/android/settings/wifi/WepNetworksPreferenceController.kt +++ b/src/com/android/settings/wifi/WepNetworksPreferenceController.kt @@ -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,68 +51,74 @@ 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 { - override val title = stringResource(R.string.wifi_allow_wep_networks) - override val summary = { getSummary() } - override val checked = { checked } - override val changeable: () -> Boolean - get() = { carrierAllowed } - override val onCheckedChange: (Boolean) -> Unit = { newChecked -> - if (!newChecked && wifiInfo.currentSecurityType == WifiEntry.SECURITY_WEP) { - openDialog = true - } else { - wifiManager.setWepAllowed(newChecked) - wepAllowedFlow.override(newChecked) + SwitchPreference( + object : SwitchPreferenceModel { + override val title = stringResource(R.string.wifi_allow_wep_networks) + override val summary = { getSummary(isWepSupported) } + override val checked = { + if (isWepSupported == true) isWepAllowed else isWepSupported } - } - }) + override val changeable: () -> Boolean + get() = { isWepSupported == true } + + override val onCheckedChange: (Boolean) -> Unit = { newChecked -> + val wifiInfo = wifiManager.connectionInfo + if (!newChecked && wifiInfo.currentSecurityType == WifiEntry.SECURITY_WEP) { + openDialog = true + } else { + wifiManager.setWepAllowed(newChecked) + wepAllowedFlow.override(newChecked) + } + } + }) if (openDialog) { SettingsAlertDialogWithIcon( onDismissRequest = { openDialog = false }, - confirmButton = AlertDialogButton( - stringResource(R.string.sim_action_yes) - ) { - wifiManager.setWepAllowed(false) - wepAllowedFlow.override(false) - openDialog = false - }, + 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 { - wifiManager.queryWepAllowed(Dispatchers.Default.asExecutor(), ::trySend) + val wepAllowedFlow = + OverridableFlow( + callbackFlow { + wifiManager.queryWepAllowed(Dispatchers.Default.asExecutor(), ::trySend) - awaitClose { } - }) -} \ No newline at end of file + awaitClose {} + }) +} diff --git a/tests/spa_unit/src/com/android/settings/wifi/WepNetworksPreferenceControllerTest.kt b/tests/spa_unit/src/com/android/settings/wifi/WepNetworksPreferenceControllerTest.kt index 49e6a171efd..9183096fd7b 100644 --- a/tests/spa_unit/src/com/android/settings/wifi/WepNetworksPreferenceControllerTest.kt +++ b/tests/spa_unit/src/com/android/settings/wifi/WepNetworksPreferenceControllerTest.kt @@ -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,29 +45,31 @@ 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 { - on { it.currentSecurityType } doReturn WifiEntry.SECURITY_EAP - on { it.ssid } doReturn SSID - } - - private var mockWifiManager = mock { - on { queryWepAllowed(any(), any()) } doAnswer { - @Suppress("UNCHECKED_CAST") - val consumer = it.arguments[1] as Consumer - consumer.accept(wepAllowed) + private var mockWifiInfo = + mock { + on { currentSecurityType } doReturn WifiEntry.SECURITY_EAP + on { ssid } doReturn SSID + } + + private var mockWifiManager = + mock { + on { queryWepAllowed(any(), any()) } doAnswer + { + @Suppress("UNCHECKED_CAST") val consumer = it.arguments[1] as Consumer + consumer.accept(wepAllowed) + } + on { isWepSupported } doReturn true + on { connectionInfo } doReturn mockWifiInfo } - on { it.isWepSupported } doReturn true - on { it.connectionInfo } doReturn mockWifiInfo - } private var context: Context = spy(ApplicationProvider.getApplicationContext()) { @@ -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.onNodeWithText(context.getString(R.string.wifi_allow_wep_networks)) + composeTestRule.setContent { controller.Content() } + + composeTestRule + .onNodeWithText(context.getString(R.string.wifi_allow_wep_networks)) + .assertIsOff() + } + + @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 isChecked_wepNotSupportedAndNotAllowed_isOff() { + mockWifiManager.stub { on { isWepSupported } doReturn false } + wepAllowed = false + + 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.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() } @@ -160,4 +189,4 @@ class WepNetworksPreferenceControllerTest { const val TEST_KEY = "test_key" const val SSID = "ssid" } -} \ No newline at end of file +}