Refactor CdmaSystemSelectListPreference

Most logic is copied from previous ListPreference however
1. Create PreferenceController for it
2. Extract API to check whether to display CMDA options
3. In controller, provide API to force display dialog

Bug: 114749736
Test: RunSettingsRoboTests
Change-Id: Ic40d2ac1dc1854df9c90d912ea9fc822531b413d
This commit is contained in:
jackqdyulei
2018-10-09 16:08:44 -07:00
parent 8eec6ff917
commit b820483fad
9 changed files with 414 additions and 231 deletions

View File

@@ -18,7 +18,6 @@ package com.android.settings.network.telephony;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.nullable;
@@ -32,11 +31,14 @@ import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.provider.Settings;
import android.telecom.PhoneAccountHandle;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import com.android.internal.telephony.PhoneConstants;
import com.android.settings.R;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.Before;
@@ -142,4 +144,22 @@ public class MobileNetworkUtilsTest {
assertThat(MobileNetworkUtils.buildPhoneAccountConfigureIntent(mContext,
mPhoneAccountHandle)).isNotNull();
}
@Test
public void isCdmaOptions_phoneTypeCdma_returnTrue() {
doReturn(PhoneConstants.PHONE_TYPE_CDMA).when(mTelephonyManager).getPhoneType();
assertThat(MobileNetworkUtils.isCdmaOptions(mContext, SUB_ID_1)).isTrue();
}
@Test
public void isCdmaOptions_worldModeWithGsmWcdma_returnTrue() {
doReturn(PhoneConstants.PHONE_TYPE_GSM).when(mTelephonyManager).getPhoneType();
doReturn("true").when(mContext).getString(R.string.config_world_mode);
Settings.Global.putInt(mContext.getContentResolver(),
android.provider.Settings.Global.PREFERRED_NETWORK_MODE + SUB_ID_1,
TelephonyManager.NETWORK_MODE_LTE_GSM_WCDMA);
assertThat(MobileNetworkUtils.isCdmaOptions(mContext, SUB_ID_1)).isTrue();
}
}

View File

@@ -0,0 +1,122 @@
/*
* Copyright (C) 2018 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.cdma;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import android.content.Context;
import android.provider.Settings;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import androidx.preference.ListPreference;
import androidx.preference.PreferenceManager;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
@RunWith(SettingsRobolectricTestRunner.class)
public class CdmaSystemSelectPreferenceControllerTest {
private static final int SUB_ID = 2;
@Mock
private PreferenceManager mPreferenceManager;
@Mock
private TelephonyManager mTelephonyManager;
@Mock
private TelephonyManager mInvalidTelephonyManager;
@Mock
private SubscriptionManager mSubscriptionManager;
private CdmaSystemSelectPreferenceController mController;
private ListPreference mPreference;
private Context mContext;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
doReturn(mTelephonyManager).when(mContext).getSystemService(Context.TELEPHONY_SERVICE);
doReturn(mSubscriptionManager).when(mContext).getSystemService(SubscriptionManager.class);
doReturn(mTelephonyManager).when(mTelephonyManager).createForSubscriptionId(SUB_ID);
doReturn(mInvalidTelephonyManager).when(mTelephonyManager).createForSubscriptionId(
SubscriptionManager.INVALID_SUBSCRIPTION_ID);
mPreference = new ListPreference(mContext);
mController = new CdmaSystemSelectPreferenceController(mContext, "mobile_data");
mController.init(mPreferenceManager, SUB_ID);
mController.mPreference = mPreference;
mPreference.setKey(mController.getPreferenceKey());
}
@Test
public void onPreferenceChange_selectHome_returnHomeMode() {
Settings.Global.putInt(mContext.getContentResolver(),
Settings.Global.CDMA_ROAMING_MODE,
TelephonyManager.CDMA_ROAMING_MODE_ANY);
doReturn(true).when(mTelephonyManager).setCdmaRoamingMode(anyInt());
mController.onPreferenceChange(mPreference,
Integer.toString(TelephonyManager.CDMA_ROAMING_MODE_HOME));
assertThat(Settings.Global.getInt(mContext.getContentResolver(),
Settings.Global.CDMA_ROAMING_MODE,
TelephonyManager.CDMA_ROAMING_MODE_ANY)).isEqualTo(
TelephonyManager.CDMA_ROAMING_MODE_HOME);
}
@Test
public void updateState_stateHome_displayHome() {
doReturn(TelephonyManager.CDMA_ROAMING_MODE_HOME).when(
mTelephonyManager).getCdmaRoamingMode();
mController.updateState(mPreference);
assertThat(mPreference.getValue()).isEqualTo(
Integer.toString(TelephonyManager.CDMA_ROAMING_MODE_HOME));
}
@Test
public void updateState_stateOther_resetToDefault() {
Settings.Global.putInt(mContext.getContentResolver(),
Settings.Global.CDMA_ROAMING_MODE,
TelephonyManager.CDMA_ROAMING_MODE_HOME);
doReturn(TelephonyManager.CDMA_ROAMING_MODE_AFFILIATED).when(
mTelephonyManager).getCdmaRoamingMode();
mController.updateState(mPreference);
assertThat(mPreference.getValue()).isEqualTo(
Integer.toString(TelephonyManager.CDMA_ROAMING_MODE_ANY));
assertThat(Settings.Global.getInt(mContext.getContentResolver(),
Settings.Global.CDMA_ROAMING_MODE,
TelephonyManager.CDMA_ROAMING_MODE_HOME)).isEqualTo(
TelephonyManager.CDMA_ROAMING_MODE_ANY);
}
}