Fix ANR in WifiCallingPreferenceController.getAvailabilityStatus

Move the following to background thread to avoid block main thread,
- MobileNetworkUtils.isWifiCallingEnabled(mContext, mSubId, null)
- MobileNetworkUtils.buildPhoneAccountConfigureIntent()
- getSummaryForWfcMode()
- Call State

Since WifiCallingPreferenceController no longer calculate availability
in getAvailabilityStatus(), also update the
CallingPreferenceCategoryController accordingly.

Also introduce ImsMmTelRepository for split business logic for easy
testing.

Fix: 292401934
Test: manual - on Mobile Settings
Test: unit test
Change-Id: If92e2c8f6e137e40b83e578294c03c1b917eef8e
This commit is contained in:
Chaohui Wang
2023-12-29 13:26:09 +08:00
parent 946f52b2a1
commit 355144675a
12 changed files with 597 additions and 495 deletions

View File

@@ -86,7 +86,8 @@ public class VideoCallingPreferenceControllerTest {
mPreference = new SwitchPreference(mContext);
mController = spy(new VideoCallingPreferenceController(mContext, "wifi_calling"));
mController.init(SUB_ID);
mController.init(
SUB_ID, new CallingPreferenceCategoryController(mContext, "calling_category"));
doReturn(mQueryImsState).when(mController).queryImsState(anyInt());
doReturn(mQueryVoLteState).when(mController).queryVoLteState(anyInt());
doReturn(true).when(mController).isImsSupported();

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 androidx.preference.PreferenceManager
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.settings.spa.preference.ComposePreference
import com.google.common.truth.Truth.assertThat
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class CallingPreferenceCategoryControllerTest {
private val context: Context = ApplicationProvider.getApplicationContext()
private val preference = ComposePreference(context).apply { key = TEST_KEY }
private val preferenceScreen = PreferenceManager(context).createPreferenceScreen(context)
private val controller = CallingPreferenceCategoryController(context, TEST_KEY)
@Before
fun setUp() {
preferenceScreen.addPreference(preference)
controller.displayPreference(preferenceScreen)
}
@Test
fun updateChildVisible_singleChildVisible_categoryVisible() {
controller.updateChildVisible(CHILD_A_KEY, true)
assertThat(preference.isVisible).isTrue()
}
@Test
fun updateChildVisible_singleChildNotVisible_categoryNotVisible() {
controller.updateChildVisible(CHILD_A_KEY, false)
assertThat(preference.isVisible).isFalse()
}
@Test
fun updateChildVisible_oneChildVisible_categoryVisible() {
controller.updateChildVisible(CHILD_A_KEY, true)
controller.updateChildVisible(CHILD_B_KEY, false)
assertThat(preference.isVisible).isTrue()
}
@Test
fun updateChildVisible_nonChildNotVisible_categoryNotVisible() {
controller.updateChildVisible(CHILD_A_KEY, false)
controller.updateChildVisible(CHILD_B_KEY, false)
assertThat(preference.isVisible).isFalse()
}
private companion object {
const val TEST_KEY = "test_key"
const val CHILD_A_KEY = "a"
const val CHILD_B_KEY = "b"
}
}

View File

@@ -0,0 +1,140 @@
/*
* 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.ComponentName
import android.content.Context
import android.content.Intent
import android.provider.Settings
import android.telecom.PhoneAccountHandle
import android.telecom.TelecomManager
import android.telephony.TelephonyManager
import android.telephony.ims.ImsMmTelManager
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.network.telephony.ims.ImsMmTelRepository
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.delay
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.doReturn
import org.mockito.kotlin.mock
import org.mockito.kotlin.spy
import org.mockito.kotlin.stub
@RunWith(AndroidJUnit4::class)
class WifiCallingPreferenceControllerTest {
private val mockTelecomManager = mock<TelecomManager>()
private val context: Context = spy(ApplicationProvider.getApplicationContext()) {
on { getSystemService(TelecomManager::class.java) } doReturn mockTelecomManager
}
private val preferenceIntent = Intent()
private val preference = Preference(context).apply {
key = TEST_KEY
intent = preferenceIntent
}
private val preferenceScreen = PreferenceManager(context).createPreferenceScreen(context)
private var callState = TelephonyManager.CALL_STATE_IDLE
private object FakeImsMmTelRepository : ImsMmTelRepository {
var wiFiMode = ImsMmTelManager.WIFI_MODE_UNKNOWN
override fun getWiFiCallingMode() = wiFiMode
}
private val callingPreferenceCategoryController =
CallingPreferenceCategoryController(context, "calling_category")
private val controller = WifiCallingPreferenceController(
context = context,
key = TEST_KEY,
callStateFlowFactory = { flowOf(callState) },
imsMmTelRepositoryFactory = { FakeImsMmTelRepository },
).init(subId = SUB_ID, callingPreferenceCategoryController)
@Before
fun setUp() {
preferenceScreen.addPreference(preference)
controller.displayPreference(preferenceScreen)
}
@Test
fun summary_noSimCallManager_setCorrectSummary() = runBlocking {
mockTelecomManager.stub {
on { getSimCallManagerForSubscription(SUB_ID) } doReturn null
}
FakeImsMmTelRepository.wiFiMode = ImsMmTelManager.WIFI_MODE_WIFI_ONLY
controller.onViewCreated(TestLifecycleOwner())
delay(100)
assertThat(preference.summary)
.isEqualTo(context.getString(com.android.internal.R.string.wfc_mode_wifi_only_summary))
}
@Test
fun summary_hasSimCallManager_summaryIsNull() = runBlocking {
mockTelecomManager.stub {
on { getSimCallManagerForSubscription(SUB_ID) } doReturn
PhoneAccountHandle(ComponentName("", ""), "")
}
controller.onViewCreated(TestLifecycleOwner())
delay(100)
assertThat(preference.summary).isNull()
}
@Test
fun isEnabled_callIdle_enabled() = runBlocking {
callState = TelephonyManager.CALL_STATE_IDLE
controller.onViewCreated(TestLifecycleOwner())
delay(100)
assertThat(preference.isEnabled).isTrue()
}
@Test
fun isEnabled_notCallIdle_disabled() = runBlocking {
callState = TelephonyManager.CALL_STATE_RINGING
controller.onViewCreated(TestLifecycleOwner())
delay(100)
assertThat(preference.isEnabled).isFalse()
}
@Test
fun displayPreference_setsSubscriptionIdOnIntent() = runBlocking {
assertThat(preference.intent!!.getIntExtra(Settings.EXTRA_SUB_ID, 0)).isEqualTo(SUB_ID)
}
private companion object {
const val TEST_KEY = "test_key"
const val SUB_ID = 2
}
}

View File

@@ -0,0 +1,115 @@
/*
* 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.ims
import android.content.Context
import android.telephony.CarrierConfigManager
import android.telephony.CarrierConfigManager.KEY_USE_WFC_HOME_NETWORK_MODE_IN_ROAMING_NETWORK_BOOL
import android.telephony.TelephonyManager
import android.telephony.ims.ImsMmTelManager
import androidx.core.os.persistableBundleOf
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.google.common.truth.Truth.assertThat
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock
import org.mockito.kotlin.spy
import org.mockito.kotlin.stub
@RunWith(AndroidJUnit4::class)
class ImsMmTelRepositoryTest {
private val mockTelephonyManager = mock<TelephonyManager> {
on { createForSubscriptionId(SUB_ID) } doReturn mock
}
private val mockCarrierConfigManager = mock<CarrierConfigManager>()
private val context: Context = spy(ApplicationProvider.getApplicationContext()) {
on { getSystemService(TelephonyManager::class.java) } doReturn mockTelephonyManager
on { getSystemService(CarrierConfigManager::class.java) } doReturn mockCarrierConfigManager
}
private val mockImsMmTelManager = mock<ImsMmTelManager> {
on { isVoWiFiSettingEnabled } doReturn true
on { getVoWiFiRoamingModeSetting() } doReturn ImsMmTelManager.WIFI_MODE_WIFI_PREFERRED
on { getVoWiFiModeSetting() } doReturn ImsMmTelManager.WIFI_MODE_CELLULAR_PREFERRED
}
private val repository = ImsMmTelRepositoryImpl(context, SUB_ID, mockImsMmTelManager)
@Test
fun getWiFiCallingMode_voWiFiSettingNotEnabled_returnUnknown() {
mockImsMmTelManager.stub {
on { isVoWiFiSettingEnabled } doReturn false
}
val wiFiCallingMode = repository.getWiFiCallingMode()
assertThat(wiFiCallingMode).isEqualTo(ImsMmTelManager.WIFI_MODE_UNKNOWN)
}
@Test
fun getWiFiCallingMode_roamingAndNotUseWfcHomeModeForRoaming_returnRoamingSetting() {
mockTelephonyManager.stub {
on { isNetworkRoaming } doReturn true
}
mockUseWfcHomeModeForRoaming(false)
val wiFiCallingMode = repository.getWiFiCallingMode()
assertThat(wiFiCallingMode).isEqualTo(mockImsMmTelManager.getVoWiFiRoamingModeSetting())
}
@Test
fun getWiFiCallingMode_roamingAndUseWfcHomeModeForRoaming_returnHomeSetting() {
mockTelephonyManager.stub {
on { isNetworkRoaming } doReturn true
}
mockUseWfcHomeModeForRoaming(true)
val wiFiCallingMode = repository.getWiFiCallingMode()
assertThat(wiFiCallingMode).isEqualTo(mockImsMmTelManager.getVoWiFiModeSetting())
}
@Test
fun getWiFiCallingMode_notRoaming_returnHomeSetting() {
mockTelephonyManager.stub {
on { isNetworkRoaming } doReturn false
}
val wiFiCallingMode = repository.getWiFiCallingMode()
assertThat(wiFiCallingMode).isEqualTo(mockImsMmTelManager.getVoWiFiModeSetting())
}
private fun mockUseWfcHomeModeForRoaming(config: Boolean) {
mockCarrierConfigManager.stub {
on {
getConfigForSubId(SUB_ID, KEY_USE_WFC_HOME_NETWORK_MODE_IN_ROAMING_NETWORK_BOOL)
} doReturn persistableBundleOf(
KEY_USE_WFC_HOME_NETWORK_MODE_IN_ROAMING_NETWORK_BOOL to config,
)
}
}
private companion object {
const val SUB_ID = 1
}
}

View File

@@ -1,227 +0,0 @@
/*
* Copyright (C) 2021 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 static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.Intent;
import android.os.PersistableBundle;
import android.provider.Settings;
import android.telecom.PhoneAccountHandle;
import android.telephony.CarrierConfigManager;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.telephony.ims.ImsMmTelManager;
import androidx.preference.Preference;
import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;
import androidx.test.annotation.UiThreadTest;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.internal.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.network.ims.MockWifiCallingQueryImsState;
import com.android.settings.network.ims.WifiCallingQueryImsState;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
@RunWith(AndroidJUnit4.class)
public class WifiCallingPreferenceControllerTest {
private static final int SUB_ID = 2;
@Mock
private SubscriptionManager mSubscriptionManager;
@Mock
private CarrierConfigManager mCarrierConfigManager;
@Mock
private TelephonyManager mTelephonyManager;
@Mock
private ImsMmTelManager mImsMmTelManager;
private PreferenceScreen mScreen;
private PreferenceManager mPreferenceManager;
private MockWifiCallingQueryImsState mQueryImsState;
private TestWifiCallingPreferenceController mController;
private Preference mPreference;
private Context mContext;
private PersistableBundle mCarrierConfig;
@Before
@UiThreadTest
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(ApplicationProvider.getApplicationContext());
when(mContext.getSystemService(SubscriptionManager.class)).thenReturn(mSubscriptionManager);
mQueryImsState = new MockWifiCallingQueryImsState(mContext, SUB_ID);
mQueryImsState.setIsEnabledByUser(true);
mQueryImsState.setIsProvisionedOnDevice(true);
mController = new TestWifiCallingPreferenceController(mContext, "wifi_calling");
mController.mCarrierConfigManager = mCarrierConfigManager;
mController.init(SUB_ID);
mController.mCallState = TelephonyManager.CALL_STATE_IDLE;
mCarrierConfig = new PersistableBundle();
when(mCarrierConfigManager.getConfigForSubId(SUB_ID)).thenReturn(mCarrierConfig);
mPreferenceManager = new PreferenceManager(mContext);
mScreen = mPreferenceManager.createPreferenceScreen(mContext);
mPreference = new Preference(mContext);
mPreference.setKey(mController.getPreferenceKey());
mScreen.addPreference(mPreference);
}
@Test
@UiThreadTest
public void updateState_noSimCallManager_setCorrectSummary() {
mController.mSimCallManager = null;
mQueryImsState.setIsEnabledByUser(true);
when(mImsMmTelManager.getVoWiFiRoamingModeSetting()).thenReturn(
ImsMmTelManager.WIFI_MODE_WIFI_ONLY);
when(mImsMmTelManager.getVoWiFiModeSetting()).thenReturn(
ImsMmTelManager.WIFI_MODE_WIFI_ONLY);
mController.updateState(mPreference);
assertThat(mPreference.getSummary()).isEqualTo(
mContext.getString(com.android.internal.R.string.wfc_mode_wifi_only_summary));
}
@Test
@UiThreadTest
public void updateState_notCallIdle_disable() {
mController.mCallState = TelephonyManager.CALL_STATE_RINGING;
mController.updateState(mPreference);
assertThat(mPreference.isEnabled()).isFalse();
}
@Test
@UiThreadTest
public void updateState_invalidPhoneAccountHandle_shouldNotCrash() {
mController.mSimCallManager = new PhoneAccountHandle(null /* invalid */, "");
//Should not crash
mController.updateState(mPreference);
}
@Test
@UiThreadTest
public void updateState_wfcNonRoamingByConfig() {
assertNull(mController.mSimCallManager);
mCarrierConfig.putBoolean(
CarrierConfigManager.KEY_USE_WFC_HOME_NETWORK_MODE_IN_ROAMING_NETWORK_BOOL, true);
mController.init(SUB_ID);
when(mImsMmTelManager.getVoWiFiRoamingModeSetting()).thenReturn(
ImsMmTelManager.WIFI_MODE_WIFI_PREFERRED);
when(mImsMmTelManager.getVoWiFiModeSetting()).thenReturn(
ImsMmTelManager.WIFI_MODE_CELLULAR_PREFERRED);
mQueryImsState.setIsEnabledByUser(true);
when(mTelephonyManager.isNetworkRoaming()).thenReturn(true);
mController.updateState(mPreference);
assertThat(mPreference.getSummary())
.isEqualTo(mContext.getString(R.string.wfc_mode_cellular_preferred_summary));
}
@Test
@UiThreadTest
public void updateState_wfcRoamingByConfig() {
assertNull(mController.mSimCallManager);
// useWfcHomeModeForRoaming is false by default. In order to check wfc in roaming mode. We
// need the device roaming, and not using home mode in roaming network.
when(mImsMmTelManager.getVoWiFiRoamingModeSetting()).thenReturn(
ImsMmTelManager.WIFI_MODE_WIFI_PREFERRED);
when(mImsMmTelManager.getVoWiFiModeSetting()).thenReturn(
ImsMmTelManager.WIFI_MODE_CELLULAR_PREFERRED);
mQueryImsState.setIsEnabledByUser(true);
when(mTelephonyManager.isNetworkRoaming()).thenReturn(true);
mController.updateState(mPreference);
assertThat(mPreference.getSummary())
.isEqualTo(mContext.getString(R.string.wfc_mode_wifi_preferred_summary));
}
@Test
@UiThreadTest
public void displayPreference_notAvailable_setPreferenceInvisible() {
mController.init(SubscriptionManager.INVALID_SUBSCRIPTION_ID);
when(mSubscriptionManager.getActiveSubscriptionInfoList()).thenReturn(null);
mController.displayPreference(mScreen);
assertThat(mController.getPreferenceKey()).isEqualTo("wifi_calling");
assertThat(mScreen.findPreference(mController.getPreferenceKey()).isVisible()).isFalse();
}
@Test
@Ignore
public void displayPreference_available_setsSubscriptionIdOnIntent() {
final Intent intent = new Intent();
mPreference.setIntent(intent);
mController.displayPreference(mScreen);
assertThat(intent.getIntExtra(Settings.EXTRA_SUB_ID,
SubscriptionManager.INVALID_SUBSCRIPTION_ID)).isEqualTo(SUB_ID);
}
@Test
@UiThreadTest
public void getAvailabilityStatus_noWiFiCalling_shouldReturnUnsupported() {
mController.init(SubscriptionManager.INVALID_SUBSCRIPTION_ID);
when(mSubscriptionManager.getActiveSubscriptionInfoList()).thenReturn(null);
assertThat(mController.getAvailabilityStatus()).isEqualTo(
BasePreferenceController.UNSUPPORTED_ON_DEVICE);
}
private class TestWifiCallingPreferenceController extends WifiCallingPreferenceController {
TestWifiCallingPreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
}
@Override
protected ImsMmTelManager getImsMmTelManager(int subId) {
return mImsMmTelManager;
}
@Override
protected TelephonyManager getTelephonyManager(Context context, int subId) {
return mTelephonyManager;
}
@Override
protected WifiCallingQueryImsState queryImsState(int subId) {
return mQueryImsState;
}
}
}