Avoid ANR when UI query network selection state
Moving the quert network selection state to back ground
and listening the service state to detect the betwork selection
changed.
Bug: 270652395
Test: atest AutoSelectPreferenceControllerTest
Change-Id: I81a597f28cf7ce25ff4eff5100bdb4d29c897a14
(cherry picked from commit 7b80b4ecd9
)
This commit is contained in:
committed by
SongFerng Wang
parent
7ab852b48e
commit
7b9c22348a
@@ -28,9 +28,11 @@ import android.app.ProgressDialog;
|
||||
import android.content.Context;
|
||||
import android.os.PersistableBundle;
|
||||
import android.telephony.CarrierConfigManager;
|
||||
import android.telephony.ServiceState;
|
||||
import android.telephony.SubscriptionManager;
|
||||
import android.telephony.TelephonyManager;
|
||||
|
||||
import androidx.lifecycle.Lifecycle;
|
||||
import androidx.preference.SwitchPreference;
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||
@@ -61,6 +63,10 @@ public class AutoSelectPreferenceControllerTest {
|
||||
private CarrierConfigCache mCarrierConfigCache;
|
||||
@Mock
|
||||
private ProgressDialog mProgressDialog;
|
||||
@Mock
|
||||
private ServiceState mTestServiceState;
|
||||
@Mock
|
||||
private Lifecycle mLifecycle;
|
||||
|
||||
private PersistableBundle mCarrierConfig;
|
||||
private AutoSelectPreferenceController mController;
|
||||
@@ -88,7 +94,16 @@ public class AutoSelectPreferenceControllerTest {
|
||||
mController = new AutoSelectPreferenceController(mContext, "auto_select");
|
||||
mController.mProgressDialog = mProgressDialog;
|
||||
mController.mSwitchPreference = mSwitchPreference;
|
||||
mController.init(SUB_ID);
|
||||
mController.init(mLifecycle, SUB_ID);
|
||||
sleepAfterInit();
|
||||
}
|
||||
|
||||
private void sleepAfterInit() {
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
} catch (Exception e) {
|
||||
fail("Sleep timeout " + e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -111,7 +126,8 @@ public class AutoSelectPreferenceControllerTest {
|
||||
|
||||
@Test
|
||||
public void updateState_isRoaming_enabled() {
|
||||
when(mTelephonyManager.getServiceState().getRoaming()).thenReturn(true);
|
||||
when(mTelephonyManager.getServiceState()).thenReturn(mTestServiceState);
|
||||
when(mTestServiceState.getRoaming()).thenReturn(true);
|
||||
|
||||
mController.updateState(mSwitchPreference);
|
||||
|
||||
@@ -120,7 +136,8 @@ public class AutoSelectPreferenceControllerTest {
|
||||
|
||||
@Test
|
||||
public void updateState_notRoamingWithAutoSelectOn_disabled() {
|
||||
when(mTelephonyManager.getServiceState().getRoaming()).thenReturn(false);
|
||||
when(mTelephonyManager.getServiceState()).thenReturn(mTestServiceState);
|
||||
when(mTestServiceState.getRoaming()).thenReturn(false);
|
||||
doReturn(OPERATOR_NAME).when(mTelephonyManager).getSimOperatorName();
|
||||
|
||||
mController.updateState(mSwitchPreference);
|
||||
@@ -136,6 +153,34 @@ public class AutoSelectPreferenceControllerTest {
|
||||
when(mCarrierConfigCache.getConfigForSubId(SUB_ID)).thenReturn(null);
|
||||
|
||||
// Should not crash
|
||||
mController.init(SUB_ID);
|
||||
mController.init(mLifecycle, SUB_ID);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateUiAutoSelectValue_serviceStateGetIsManualSelection_isCheckedFalse() {
|
||||
when(mTelephonyManager.getNetworkSelectionMode()).thenReturn(
|
||||
TelephonyManager.NETWORK_SELECTION_MODE_AUTO);
|
||||
when(mTestServiceState.getIsManualSelection()).thenReturn(true);
|
||||
mController.init(mLifecycle, SUB_ID);
|
||||
sleepAfterInit();
|
||||
|
||||
mController.updateUiAutoSelectValue(mTestServiceState);
|
||||
|
||||
assertThat(mController.isChecked()).isFalse();
|
||||
assertThat(mSwitchPreference.isChecked()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateUiAutoSelectValue_serviceStateGetIsAutoSelection_isCheckedTrue() {
|
||||
when(mTelephonyManager.getNetworkSelectionMode()).thenReturn(
|
||||
TelephonyManager.NETWORK_SELECTION_MODE_MANUAL);
|
||||
when(mTestServiceState.getIsManualSelection()).thenReturn(false);
|
||||
mController.init(mLifecycle, SUB_ID);
|
||||
sleepAfterInit();
|
||||
|
||||
mController.updateUiAutoSelectValue(mTestServiceState);
|
||||
|
||||
assertThat(mController.isChecked()).isTrue();
|
||||
assertThat(mSwitchPreference.isChecked()).isTrue();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user