Refresh "Choose network" summary when service state changes

This summary display service connection state, which should be refreshed
when service state changes.

Fix: 313026209
Test: manual - on Mobile Settings
Test: unit test
Change-Id: I6ca2f89e05f21460a7db055f037919b6ebd19182
This commit is contained in:
Chaohui Wang
2023-12-19 18:10:12 +08:00
parent 72d638e681
commit 28b85b5810
13 changed files with 622 additions and 281 deletions

View File

@@ -0,0 +1,113 @@
/*
* Copyright (C) 2023 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.network.telephony
import android.content.Context
import android.telephony.TelephonyCallback
import android.telephony.TelephonyManager
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.settingslib.spa.testutils.firstWithTimeoutOrNull
import com.android.settingslib.spa.testutils.toListWithTimeout
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
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 AllowedNetworkTypesFlowTest {
private var allowedNetworkTypesListener: TelephonyCallback.AllowedNetworkTypesListener? = null
private val mockTelephonyManager = mock<TelephonyManager> {
on { createForSubscriptionId(SUB_ID) } doReturn mock
on { registerTelephonyCallback(any(), any()) } doAnswer {
allowedNetworkTypesListener =
it.arguments[1] as TelephonyCallback.AllowedNetworkTypesListener
}
}
private val context: Context = spy(ApplicationProvider.getApplicationContext()) {
on { getSystemService(TelephonyManager::class.java) } doReturn mockTelephonyManager
}
@Test
fun allowedNetworkTypesFlow_initial_notSndInitialValue() = runBlocking {
val flow = context.allowedNetworkTypesFlow(SUB_ID)
val state = flow.firstWithTimeoutOrNull()
assertThat(state).isNull()
}
@Test
fun allowedNetworkTypesFlow_userReasonChanged_sendChanged(): Unit = runBlocking {
val listDeferred = async {
context.allowedNetworkTypesFlow(SUB_ID).toListWithTimeout()
}
delay(100)
allowedNetworkTypesListener?.onAllowedNetworkTypesChanged(
TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER,
ALLOWED_NETWORK_TYPE,
)
assertThat(listDeferred.await()).containsExactly(ALLOWED_NETWORK_TYPE)
}
@Test
fun allowedNetworkTypesFlow_carrierReasonChanged_sendChanged(): Unit = runBlocking {
val listDeferred = async {
context.allowedNetworkTypesFlow(SUB_ID).toListWithTimeout()
}
delay(100)
allowedNetworkTypesListener?.onAllowedNetworkTypesChanged(
TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_CARRIER,
ALLOWED_NETWORK_TYPE,
)
assertThat(listDeferred.await()).containsExactly(ALLOWED_NETWORK_TYPE)
}
@Test
fun allowedNetworkTypesFlow_powerReasonChanged_notSendChanged() = runBlocking {
val listDeferred = async {
context.allowedNetworkTypesFlow(SUB_ID).toListWithTimeout()
}
delay(100)
allowedNetworkTypesListener?.onAllowedNetworkTypesChanged(
TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_POWER,
ALLOWED_NETWORK_TYPE,
)
assertThat(listDeferred.await()).isEmpty()
}
private companion object {
const val SUB_ID = 1
const val ALLOWED_NETWORK_TYPE = 10L
}
}

View File

@@ -0,0 +1,80 @@
/*
* Copyright (C) 2023 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.network.telephony
import android.content.Context
import android.telephony.ServiceState
import android.telephony.TelephonyCallback
import android.telephony.TelephonyManager
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.settingslib.spa.testutils.firstWithTimeoutOrNull
import com.android.settingslib.spa.testutils.toListWithTimeout
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
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 ServiceStateFlowTest {
private var serviceStateListener: TelephonyCallback.ServiceStateListener? = null
private val mockTelephonyManager = mock<TelephonyManager> {
on { createForSubscriptionId(SUB_ID) } doReturn mock
on { registerTelephonyCallback(any(), any()) } doAnswer {
serviceStateListener = it.arguments[1] as TelephonyCallback.ServiceStateListener
serviceStateListener?.onServiceStateChanged(ServiceState())
}
}
private val context: Context = spy(ApplicationProvider.getApplicationContext()) {
on { getSystemService(TelephonyManager::class.java) } doReturn mockTelephonyManager
}
@Test
fun serviceStateFlow_initial_sndInitialValue() = runBlocking {
val flow = context.serviceStateFlow(SUB_ID)
val state = flow.firstWithTimeoutOrNull()
assertThat(state).isNotNull()
}
@Test
fun serviceStateFlow_changed_sendChanged(): Unit = runBlocking {
val listDeferred = async {
context.serviceStateFlow(SUB_ID).toListWithTimeout()
}
delay(100)
serviceStateListener?.onServiceStateChanged(ServiceState())
assertThat(listDeferred.await()).hasSize(2)
}
private companion object {
const val SUB_ID = 1
}
}

View File

@@ -0,0 +1,76 @@
/*
* Copyright (C) 2023 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.network.telephony
import android.content.Context
import android.telephony.TelephonyCallback
import android.telephony.TelephonyManager
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.settingslib.spa.testutils.firstWithTimeoutOrNull
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.runBlocking
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
import org.mockito.kotlin.verify
@RunWith(AndroidJUnit4::class)
class TelephonyRepositoryTest {
private var telephonyCallback: TelephonyCallback? = null
private val mockTelephonyManager = mock<TelephonyManager> {
on { createForSubscriptionId(SUB_ID) } doReturn mock
on { registerTelephonyCallback(any(), any()) } doAnswer {
telephonyCallback = it.arguments[1] as TelephonyCallback
}
}
private val context: Context = spy(ApplicationProvider.getApplicationContext()) {
on { getSystemService(TelephonyManager::class.java) } doReturn mockTelephonyManager
}
@Test
fun telephonyCallbackFlow_callbackRegistered() = runBlocking {
val flow = context.telephonyCallbackFlow<Unit>(SUB_ID) {
object : TelephonyCallback() {}
}
flow.firstWithTimeoutOrNull()
assertThat(telephonyCallback).isNotNull()
}
@Test
fun telephonyCallbackFlow_callbackUnregistered() = runBlocking {
val flow = context.telephonyCallbackFlow<Unit>(SUB_ID) {
object : TelephonyCallback() {}
}
flow.firstWithTimeoutOrNull()
verify(mockTelephonyManager).unregisterTelephonyCallback(telephonyCallback!!)
}
private companion object {
const val SUB_ID = 1
}
}

View File

@@ -0,0 +1,118 @@
/*
* Copyright (C) 2023 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.network.telephony.gsm
import android.content.Context
import android.telephony.ServiceState
import android.telephony.SubscriptionInfo
import android.telephony.SubscriptionManager
import android.telephony.TelephonyManager
import androidx.lifecycle.testing.TestLifecycleOwner
import androidx.preference.Preference
import androidx.preference.PreferenceManager
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.settings.R
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.runBlocking
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.doAnswer
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock
import org.mockito.kotlin.spy
@RunWith(AndroidJUnit4::class)
class OpenNetworkSelectPagePreferenceControllerTest {
private val subscriptionInfo = mock<SubscriptionInfo> {
on { subscriptionId } doReturn SUB_ID
on { carrierName } doReturn OPERATOR_NAME
}
private val mockSubscriptionManager = mock<SubscriptionManager> {
on { activeSubscriptionInfoList } doAnswer { listOf(subscriptionInfo) }
}
private val context: Context = spy(ApplicationProvider.getApplicationContext()) {
on { getSystemService(SubscriptionManager::class.java) } doReturn mockSubscriptionManager
}
private val preference = Preference(context).apply { key = TEST_KEY }
private val preferenceScreen = PreferenceManager(context).createPreferenceScreen(context)
private val controller = OpenNetworkSelectPagePreferenceController(context, TEST_KEY)
private val serviceState = ServiceState()
@Before
fun setUp() {
preferenceScreen.addPreference(preference)
controller.apply {
init(
subId = SUB_ID,
allowedNetworkTypesFlow = emptyFlow(),
serviceStateFlow = flowOf(serviceState),
)
displayPreference(preferenceScreen)
}
}
@Test
fun isEnabled_modeManual_enabled() {
controller.onNetworkSelectModeUpdated(TelephonyManager.NETWORK_SELECTION_MODE_MANUAL)
assertThat(preference.isEnabled).isTrue()
}
@Test
fun isEnabled_modeAuto_disabled() {
controller.onNetworkSelectModeUpdated(TelephonyManager.NETWORK_SELECTION_MODE_AUTO)
assertThat(preference.isEnabled).isFalse()
}
@Test
fun summary_inService_isOperatorName() = runBlocking {
serviceState.state = ServiceState.STATE_IN_SERVICE
controller.onViewCreated(TestLifecycleOwner())
delay(100)
assertThat(preference.summary).isEqualTo(OPERATOR_NAME)
}
@Test
fun summary_notInService_isDisconnect() = runBlocking {
serviceState.state = ServiceState.STATE_OUT_OF_SERVICE
controller.onViewCreated(TestLifecycleOwner())
delay(100)
assertThat(preference.summary).isEqualTo(context.getString(R.string.network_disconnected))
}
private companion object {
const val TEST_KEY = "test_key"
const val SUB_ID = 2
const val OPERATOR_NAME = "T-mobile"
}
}

View File

@@ -1,124 +0,0 @@
/*
* Copyright (C) 2020 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.network.telephony.gsm;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.os.PersistableBundle;
import android.telephony.CarrierConfigManager;
import android.telephony.ServiceState;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import androidx.preference.Preference;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settings.testutils.ResourcesUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.util.Arrays;
@RunWith(AndroidJUnit4.class)
public class OpenNetworkSelectPagePreferenceControllerTest {
private static final int SUB_ID = 2;
private static final String OPERATOR_NAME = "T-mobile";
@Mock
private TelephonyManager mTelephonyManager;
@Mock
private SubscriptionManager mSubscriptionManager;
@Mock
private CarrierConfigManager mCarrierConfigManager;
@Mock
private ServiceState mServiceState;
@Mock
private SubscriptionInfo mSubscriptionInfo;
private PersistableBundle mCarrierConfig;
private OpenNetworkSelectPagePreferenceController mController;
private Preference mPreference;
private Context mContext;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(ApplicationProvider.getApplicationContext());
when(mContext.getSystemService(TelephonyManager.class)).thenReturn(mTelephonyManager);
when(mContext.getSystemService(SubscriptionManager.class)).thenReturn(mSubscriptionManager);
when(mContext.getSystemService(CarrierConfigManager.class)).thenReturn(
mCarrierConfigManager);
when(mTelephonyManager.createForSubscriptionId(SUB_ID)).thenReturn(mTelephonyManager);
when(mTelephonyManager.getServiceState()).thenReturn(mServiceState);
mCarrierConfig = new PersistableBundle();
when(mCarrierConfigManager.getConfigForSubId(SUB_ID)).thenReturn(mCarrierConfig);
when(mSubscriptionInfo.getSubscriptionId()).thenReturn(SUB_ID);
when(mSubscriptionInfo.getCarrierName()).thenReturn(OPERATOR_NAME);
when(mSubscriptionManager.getActiveSubscriptionInfoList()).thenReturn(
Arrays.asList(mSubscriptionInfo));
when(mTelephonyManager.getNetworkOperatorName()).thenReturn(OPERATOR_NAME);
mPreference = new Preference(mContext);
mController = new OpenNetworkSelectPagePreferenceController(mContext,
"open_network_select") {
@Override
public void updateState(Preference preference) {
super.updateState(mPreference);
}
};
mController.init(SUB_ID);
}
@Test
public void updateState_modeAuto_disabled() {
mController.onNetworkSelectModeUpdated(TelephonyManager.NETWORK_SELECTION_MODE_AUTO);
mController.updateState(mPreference);
assertThat(mPreference.isEnabled()).isFalse();
}
@Test
public void getSummary_inService_returnOperatorName() {
when(mServiceState.getState()).thenReturn(ServiceState.STATE_IN_SERVICE);
assertThat(mController.getSummary()).isEqualTo(OPERATOR_NAME);
}
@Test
public void getSummary_notInService_returnDisconnect() {
when(mServiceState.getState()).thenReturn(ServiceState.STATE_OUT_OF_SERVICE);
assertThat(mController.getSummary()).isEqualTo(
ResourcesUtils.getResourcesString(mContext, "network_disconnected"));
}
}