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

This commit is contained in:
Chaohui Wang
2024-08-05 02:02:20 +00:00
committed by Android (Google) Code Review
2 changed files with 123 additions and 86 deletions

View File

@@ -42,6 +42,8 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.asExecutor import kotlinx.coroutines.asExecutor
import kotlinx.coroutines.channels.awaitClose import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.callbackFlow 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. */ /** Controller that controls whether the WEP network can be connected. */
class WepNetworksPreferenceController(context: Context, preferenceKey: String) : class WepNetworksPreferenceController(context: Context, preferenceKey: String) :
@@ -49,68 +51,74 @@ class WepNetworksPreferenceController(context: Context, preferenceKey: String) :
var wifiManager = context.getSystemService(WifiManager::class.java)!! var wifiManager = context.getSystemService(WifiManager::class.java)!!
override fun getAvailabilityStatus() = if (Flags.androidVWifiApi()) AVAILABLE override fun getAvailabilityStatus() =
else UNSUPPORTED_ON_DEVICE if (Flags.androidVWifiApi()) AVAILABLE else UNSUPPORTED_ON_DEVICE
@Composable @Composable
override fun Content() { 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) } var openDialog by rememberSaveable { mutableStateOf(false) }
val wifiInfo = wifiManager.connectionInfo SwitchPreference(
SwitchPreference(object : SwitchPreferenceModel { object : SwitchPreferenceModel {
override val title = stringResource(R.string.wifi_allow_wep_networks) override val title = stringResource(R.string.wifi_allow_wep_networks)
override val summary = { getSummary() } override val summary = { getSummary(isWepSupported) }
override val checked = { checked } override val checked = {
override val changeable: () -> Boolean if (isWepSupported == true) isWepAllowed else isWepSupported
get() = { carrierAllowed }
override val onCheckedChange: (Boolean) -> Unit = { newChecked ->
if (!newChecked && wifiInfo.currentSecurityType == WifiEntry.SECURITY_WEP) {
openDialog = true
} else {
wifiManager.setWepAllowed(newChecked)
wepAllowedFlow.override(newChecked)
} }
} 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) { if (openDialog) {
SettingsAlertDialogWithIcon( SettingsAlertDialogWithIcon(
onDismissRequest = { openDialog = false }, onDismissRequest = { openDialog = false },
confirmButton = AlertDialogButton( confirmButton =
stringResource(R.string.sim_action_yes) AlertDialogButton(stringResource(R.string.sim_action_yes)) {
) { wifiManager.setWepAllowed(false)
wifiManager.setWepAllowed(false) wepAllowedFlow.override(false)
wepAllowedFlow.override(false) openDialog = false
openDialog = false },
},
dismissButton = dismissButton =
AlertDialogButton( AlertDialogButton(stringResource(R.string.wifi_cancel)) { openDialog = false },
stringResource(R.string.wifi_cancel)
) { openDialog = false },
title = stringResource(R.string.wifi_settings_wep_networks_disconnect_title), title = stringResource(R.string.wifi_settings_wep_networks_disconnect_title),
text = { text = {
Text( Text(
stringResource(R.string.wifi_settings_wep_networks_disconnect_summary), stringResource(R.string.wifi_settings_wep_networks_disconnect_summary),
modifier = Modifier.fillMaxWidth(), modifier = Modifier.fillMaxWidth(),
textAlign = TextAlign.Center textAlign = TextAlign.Center,
) )
}) })
} }
} }
override fun getSummary(): String = mContext.getString( private fun getSummary(isWepSupported: Boolean?): String =
if (carrierAllowed) { mContext.getString(
R.string.wifi_allow_wep_networks_summary when (isWepSupported) {
} else { true -> R.string.wifi_allow_wep_networks_summary
R.string.wifi_allow_wep_networks_summary_carrier_not_allow false -> R.string.wifi_allow_wep_networks_summary_carrier_not_allow
} null -> R.string.summary_placeholder
) })
private val carrierAllowed: Boolean private val isWepSupportedFlow =
get() = wifiManager.isWepSupported flow { emit(wifiManager.isWepSupported) }.flowOn(Dispatchers.Default)
val wepAllowedFlow = OverridableFlow(callbackFlow { val wepAllowedFlow =
wifiManager.queryWepAllowed(Dispatchers.Default.asExecutor(), ::trySend) OverridableFlow(
callbackFlow {
wifiManager.queryWepAllowed(Dispatchers.Default.asExecutor(), ::trySend)
awaitClose { } awaitClose {}
}) })
} }

View File

@@ -17,6 +17,7 @@
package com.android.settings.wifi package com.android.settings.wifi
import android.content.Context import android.content.Context
import android.net.wifi.WifiInfo
import android.net.wifi.WifiManager import android.net.wifi.WifiManager
import androidx.compose.ui.test.assertIsOff import androidx.compose.ui.test.assertIsOff
import androidx.compose.ui.test.assertIsOn import androidx.compose.ui.test.assertIsOn
@@ -30,7 +31,6 @@ import androidx.preference.PreferenceManager
import androidx.test.core.app.ApplicationProvider import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.settings.R import com.android.settings.R
import com.android.settings.dashboard.DashboardFragment
import com.android.settings.spa.preference.ComposePreference import com.android.settings.spa.preference.ComposePreference
import com.android.settingslib.spa.testutils.onDialogText import com.android.settingslib.spa.testutils.onDialogText
import com.android.wifitrackerlib.WifiEntry import com.android.wifitrackerlib.WifiEntry
@@ -45,29 +45,31 @@ import org.mockito.kotlin.doAnswer
import org.mockito.kotlin.doReturn import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock import org.mockito.kotlin.mock
import org.mockito.kotlin.spy import org.mockito.kotlin.spy
import org.mockito.kotlin.stub
@RunWith(AndroidJUnit4::class) @RunWith(AndroidJUnit4::class)
class WepNetworksPreferenceControllerTest { class WepNetworksPreferenceControllerTest {
@get:Rule @get:Rule val composeTestRule = createComposeRule()
val composeTestRule = createComposeRule()
private var wepAllowed = true private var wepAllowed = true
private var mockWifiInfo = mock<android.net.wifi.WifiInfo> { private var mockWifiInfo =
on { it.currentSecurityType } doReturn WifiEntry.SECURITY_EAP mock<WifiInfo> {
on { it.ssid } doReturn SSID on { currentSecurityType } doReturn WifiEntry.SECURITY_EAP
} on { ssid } doReturn SSID
}
private var mockWifiManager = mock<WifiManager> {
on { queryWepAllowed(any(), any()) } doAnswer { private var mockWifiManager =
@Suppress("UNCHECKED_CAST") mock<WifiManager> {
val consumer = it.arguments[1] as Consumer<Boolean> on { queryWepAllowed(any(), any()) } doAnswer
consumer.accept(wepAllowed) {
@Suppress("UNCHECKED_CAST") val consumer = it.arguments[1] as Consumer<Boolean>
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 = private var context: Context =
spy(ApplicationProvider.getApplicationContext()) { spy(ApplicationProvider.getApplicationContext()) {
@@ -85,74 +87,101 @@ class WepNetworksPreferenceControllerTest {
} }
@Test @Test
fun wepAllowedTrue_turnOn() { fun isChecked_wepSupportedAndAllowed_isOn() {
mockWifiManager.stub { on { isWepSupported } doReturn true }
wepAllowed = 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() .assertIsOn()
} }
@Test @Test
fun wepAllowedFalse_turnOff() { fun isChecked_wepSupportedAndNotAllowed_isOff() {
mockWifiManager.stub { on { isWepSupported } doReturn true }
wepAllowed = false 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() .assertIsOff()
} }
@Test @Test
fun onClick_turnOn() { fun onClick_turnOn() {
wepAllowed = false wepAllowed = false
composeTestRule.setContent { composeTestRule.setContent { controller.Content() }
controller.Content()
}
composeTestRule.onRoot().performClick() composeTestRule.onRoot().performClick()
composeTestRule.onNodeWithText(context.getString(R.string.wifi_allow_wep_networks)) composeTestRule
.onNodeWithText(context.getString(R.string.wifi_allow_wep_networks))
.assertIsOn() .assertIsOn()
} }
@Test @Test
fun onClick_turnOff() { fun onClick_turnOff() {
wepAllowed = true wepAllowed = true
composeTestRule.setContent { composeTestRule.setContent { controller.Content() }
controller.Content()
}
composeTestRule.onRoot().performClick() composeTestRule.onRoot().performClick()
composeTestRule.onNodeWithText(context.getString(R.string.wifi_allow_wep_networks)) composeTestRule
.onNodeWithText(context.getString(R.string.wifi_allow_wep_networks))
.assertIsOff() .assertIsOff()
} }
@Test @Test
fun whenClick_wepAllowed_openDialog() { fun whenClick_wepAllowed_openDialog() {
wepAllowed = true wepAllowed = true
Mockito.`when`(mockWifiInfo.currentSecurityType).thenReturn(WifiEntry.SECURITY_WEP) mockWifiInfo.stub {
composeTestRule.setContent { on { currentSecurityType } doReturn WifiEntry.SECURITY_WEP
controller.Content()
} }
composeTestRule.setContent { controller.Content() }
composeTestRule.onRoot().performClick() composeTestRule.onRoot().performClick()
composeTestRule.onDialogText(context.getString(R.string.wifi_disconnect_button_text)) composeTestRule
.onDialogText(context.getString(R.string.wifi_disconnect_button_text))
.isDisplayed() .isDisplayed()
} }
@Test @Test
fun whenClick_wepDisallowed_openDialog() { fun whenClick_wepDisallowed_openDialog() {
wepAllowed = false wepAllowed = false
Mockito.`when`(mockWifiInfo.currentSecurityType).thenReturn(WifiEntry.SECURITY_WEP) mockWifiInfo.stub {
composeTestRule.setContent { on { currentSecurityType } doReturn WifiEntry.SECURITY_WEP
controller.Content()
} }
composeTestRule.setContent { controller.Content() }
composeTestRule.onRoot().performClick() composeTestRule.onRoot().performClick()
composeTestRule.onDialogText(context.getString(R.string.wifi_disconnect_button_text)) composeTestRule
.onDialogText(context.getString(R.string.wifi_disconnect_button_text))
.isNotDisplayed() .isNotDisplayed()
} }
@@ -160,4 +189,4 @@ class WepNetworksPreferenceControllerTest {
const val TEST_KEY = "test_key" const val TEST_KEY = "test_key"
const val SSID = "ssid" const val SSID = "ssid"
} }
} }