Add WEPNetworksPreferenceController.
Test: Visual Test Fix: 317453032 Change-Id: Ifce83ffa32eeb0d1245b7c5e61c3cad3f85fa14e
This commit is contained in:
@@ -34,6 +34,11 @@
|
||||
settings:keywords="@string/keywords_wifi_notify_open_networks"
|
||||
settings:controller="com.android.settings.wifi.NotifyOpenNetworksPreferenceController"/>
|
||||
|
||||
<com.android.settings.spa.preference.ComposePreference
|
||||
android:key="allow_wep_networks"
|
||||
android:title="@string/wifi_allow_wep_networks"
|
||||
settings:controller="com.android.settings.wifi.WepNetworksPreferenceController"/>
|
||||
|
||||
<SwitchPreferenceCompat
|
||||
android:key="wifi_cellular_data_fallback"
|
||||
android:title="@string/wifi_cellular_data_fallback_title"
|
||||
|
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Copyright (C) 2024 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settings.wifi
|
||||
|
||||
import android.content.Context
|
||||
import android.net.wifi.WifiManager
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import androidx.preference.Preference
|
||||
import androidx.preference.PreferenceScreen
|
||||
import com.android.settings.R
|
||||
import com.android.settings.spa.preference.ComposePreferenceController
|
||||
import com.android.settingslib.spa.framework.compose.OverridableFlow
|
||||
import com.android.settingslib.spa.widget.preference.SwitchPreference
|
||||
import com.android.settingslib.spa.widget.preference.SwitchPreferenceModel
|
||||
import com.android.wifi.flags.Flags
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.asExecutor
|
||||
import kotlinx.coroutines.channels.awaitClose
|
||||
import kotlinx.coroutines.flow.callbackFlow
|
||||
|
||||
/** Controller that controls whether the Wi-Fi Wakeup feature should be enabled. */
|
||||
class WepNetworksPreferenceController(context: Context, preferenceKey: String) :
|
||||
ComposePreferenceController(context, preferenceKey) {
|
||||
|
||||
private lateinit var preference: Preference
|
||||
|
||||
var wifiManager = context.getSystemService(WifiManager::class.java)!!
|
||||
|
||||
override fun displayPreference(screen: PreferenceScreen) {
|
||||
super.displayPreference(screen)
|
||||
preference = screen.findPreference(preferenceKey)!!
|
||||
}
|
||||
|
||||
override fun getAvailabilityStatus() = if (Flags.wepUsage()) AVAILABLE
|
||||
else UNSUPPORTED_ON_DEVICE
|
||||
|
||||
@Composable
|
||||
override fun Content() {
|
||||
val checked by wepAllowedFlow.flow.collectAsStateWithLifecycle(initialValue = null)
|
||||
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 ->
|
||||
wifiManager.setWepAllowed(newChecked)
|
||||
wepAllowedFlow.override(newChecked)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
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 val carrierAllowed: Boolean
|
||||
get() = wifiManager.isWepSupported
|
||||
|
||||
val wepAllowedFlow = OverridableFlow(callbackFlow {
|
||||
wifiManager.queryWepAllowed(Dispatchers.Default.asExecutor(), ::trySend)
|
||||
|
||||
awaitClose { }
|
||||
})
|
||||
}
|
@@ -0,0 +1,125 @@
|
||||
/*
|
||||
* Copyright (C) 2024 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settings.wifi
|
||||
|
||||
import android.content.Context
|
||||
import android.net.wifi.WifiManager
|
||||
import androidx.compose.ui.test.assertIsOff
|
||||
import androidx.compose.ui.test.assertIsOn
|
||||
import androidx.compose.ui.test.junit4.createComposeRule
|
||||
import androidx.compose.ui.test.onNodeWithText
|
||||
import androidx.compose.ui.test.onRoot
|
||||
import androidx.compose.ui.test.performClick
|
||||
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.spa.preference.ComposePreference
|
||||
import java.util.function.Consumer
|
||||
import org.junit.Before
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.kotlin.any
|
||||
import org.mockito.kotlin.doAnswer
|
||||
import org.mockito.kotlin.doReturn
|
||||
import org.mockito.kotlin.mock
|
||||
import org.mockito.kotlin.spy
|
||||
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class WepNetworksPreferenceControllerTest {
|
||||
|
||||
@get:Rule
|
||||
val composeTestRule = createComposeRule()
|
||||
|
||||
private var wepAllowed = true
|
||||
|
||||
private val 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
|
||||
}
|
||||
|
||||
private var context: Context =
|
||||
spy(ApplicationProvider.getApplicationContext()) {
|
||||
on { getSystemService(WifiManager::class.java) } doReturn mockWifiManager
|
||||
}
|
||||
private var controller = WepNetworksPreferenceController(context, TEST_KEY)
|
||||
|
||||
private val preference = ComposePreference(context).apply { key = TEST_KEY }
|
||||
private val preferenceScreen = PreferenceManager(context).createPreferenceScreen(context)
|
||||
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
preferenceScreen.addPreference(preference)
|
||||
controller.displayPreference(preferenceScreen)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun wepAllowedTrue_turnOn() {
|
||||
wepAllowed = true
|
||||
composeTestRule.setContent {
|
||||
controller.Content()
|
||||
}
|
||||
composeTestRule.onNodeWithText(context.getString(R.string.wifi_allow_wep_networks))
|
||||
.assertIsOn()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun wepAllowedFalse_turnOff() {
|
||||
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.onRoot().performClick()
|
||||
|
||||
composeTestRule.onNodeWithText(context.getString(R.string.wifi_allow_wep_networks))
|
||||
.assertIsOn()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun onClick_turnOff() {
|
||||
wepAllowed = true
|
||||
composeTestRule.setContent {
|
||||
controller.Content()
|
||||
}
|
||||
|
||||
composeTestRule.onRoot().performClick()
|
||||
|
||||
composeTestRule.onNodeWithText(context.getString(R.string.wifi_allow_wep_networks))
|
||||
.assertIsOff()
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val TEST_KEY = "test_key"
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user