From 2e763e52cfc50818bed979f7c3f953c38718e292 Mon Sep 17 00:00:00 2001 From: changbetty Date: Mon, 17 Aug 2020 20:33:00 +0800 Subject: [PATCH 1/3] [Testing] Use AndroidJUnit4 instead in MobileNetworkUtilsTest 1. Use AndroidJunit4 instead of RobolectricTestRunner 2. Use ApplicationProvider instead of RuntimeEnvironment to get context 3. Change the Copy Right 4. Create the real object for PhoneAccountHandle and ComponentName Bug: 164989754 Test: atest -c MobileNetworkUtilsTest Change-Id: I1778832b0602062adb24928343c0c1d0e525942d Merged-In: I1778832b0602062adb24928343c0c1d0e525942d --- .../telephony/MobileNetworkUtilsTest.java | 336 ++++++++++++++++++ 1 file changed, 336 insertions(+) create mode 100644 tests/unit/src/com/android/settings/network/telephony/MobileNetworkUtilsTest.java diff --git a/tests/unit/src/com/android/settings/network/telephony/MobileNetworkUtilsTest.java b/tests/unit/src/com/android/settings/network/telephony/MobileNetworkUtilsTest.java new file mode 100644 index 00000000000..ee8157d6c60 --- /dev/null +++ b/tests/unit/src/com/android/settings/network/telephony/MobileNetworkUtilsTest.java @@ -0,0 +1,336 @@ +/* + * 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; + +import static com.google.common.truth.Truth.assertThat; + +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.nullable; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; +import android.os.PersistableBundle; +import android.provider.Settings; +import android.telecom.PhoneAccountHandle; +import android.telephony.CarrierConfigManager; +import android.telephony.SubscriptionInfo; +import android.telephony.SubscriptionManager; +import android.telephony.TelephonyManager; + +import androidx.test.core.app.ApplicationProvider; +import androidx.test.ext.junit.runners.AndroidJUnit4; + +import com.android.settings.network.telephony.TelephonyConstants.TelephonyManagerConstants; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.util.ArrayList; +import java.util.Arrays; + +@RunWith(AndroidJUnit4.class) +public class MobileNetworkUtilsTest { + private static final String PACKAGE_NAME = "com.android.app"; + private static final int SUB_ID_1 = 1; + private static final int SUB_ID_2 = 2; + private static final int SUB_ID_INVALID = -1; + private static final String PLMN_FROM_TELEPHONY_MANAGER_API = "testPlmn"; + private static final String PLMN_FROM_SUB_ID_1 = "testPlmnSub1"; + private static final String PLMN_FROM_SUB_ID_2 = "testPlmnSub2"; + + @Mock + private TelephonyManager mTelephonyManager; + @Mock + private TelephonyManager mTelephonyManager2; + @Mock + private SubscriptionManager mSubscriptionManager; + @Mock + private SubscriptionInfo mSubscriptionInfo1; + @Mock + private SubscriptionInfo mSubscriptionInfo2; + @Mock + private PackageManager mPackageManager; + @Mock + private ResolveInfo mResolveInfo; + @Mock + private CarrierConfigManager mCarrierConfigManager; + + private Context mContext; + private PersistableBundle mCarrierConfig; + private PhoneAccountHandle mPhoneAccountHandle; + private ComponentName mComponentName; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + + mContext = spy(ApplicationProvider.getApplicationContext()); + when(mContext.getSystemService(SubscriptionManager.class)).thenReturn(mSubscriptionManager); + when(mContext.getSystemService(TelephonyManager.class)).thenReturn(mTelephonyManager); + when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager); + when(mTelephonyManager.createForSubscriptionId(SUB_ID_1)).thenReturn(mTelephonyManager); + when(mTelephonyManager.createForSubscriptionId(SUB_ID_2)).thenReturn(mTelephonyManager2); + when(mContext.getPackageManager()).thenReturn(mPackageManager); + + when(mContext.getSystemService(CarrierConfigManager.class)).thenReturn( + mCarrierConfigManager); + + mCarrierConfig = new PersistableBundle(); + when(mCarrierConfigManager.getConfigForSubId(SUB_ID_1)).thenReturn(mCarrierConfig); + + when(mSubscriptionInfo1.getSubscriptionId()).thenReturn(SUB_ID_1); + when(mSubscriptionInfo1.getCarrierName()).thenReturn(PLMN_FROM_SUB_ID_1); + when(mSubscriptionInfo2.getSubscriptionId()).thenReturn(SUB_ID_2); + when(mSubscriptionInfo2.getCarrierName()).thenReturn(PLMN_FROM_SUB_ID_2); + + when(mSubscriptionManager.getActiveSubscriptionInfoList()).thenReturn( + Arrays.asList(mSubscriptionInfo1, mSubscriptionInfo2)); + when(mSubscriptionManager.getAccessibleSubscriptionInfoList()).thenReturn( + Arrays.asList(mSubscriptionInfo1, mSubscriptionInfo2)); + + when(mTelephonyManager.getNetworkOperatorName()).thenReturn( + PLMN_FROM_TELEPHONY_MANAGER_API); + } + + @Test + public void setMobileDataEnabled_setEnabled_enabled() { + MobileNetworkUtils.setMobileDataEnabled(mContext, SUB_ID_1, true, false); + + verify(mTelephonyManager).setDataEnabled(true); + verify(mTelephonyManager2, never()).setDataEnabled(anyBoolean()); + } + + @Test + public void setMobileDataEnabled_setDisabled_disabled() { + MobileNetworkUtils.setMobileDataEnabled(mContext, SUB_ID_2, true, false); + + verify(mTelephonyManager2).setDataEnabled(true); + verify(mTelephonyManager, never()).setDataEnabled(anyBoolean()); + } + + @Test + public void setMobileDataEnabled_disableOtherSubscriptions() { + MobileNetworkUtils.setMobileDataEnabled(mContext, SUB_ID_1, true, true); + + verify(mTelephonyManager).setDataEnabled(true); + verify(mTelephonyManager2).setDataEnabled(false); + } + + @Test + public void buildConfigureIntent_nullHandle_returnNull() { + assertThat(MobileNetworkUtils.buildPhoneAccountConfigureIntent(mContext, null)).isNull(); + } + + @Test + public void buildConfigureIntent_noActivityHandleIntent_returnNull() { + when(mPackageManager.queryIntentActivities(nullable(Intent.class), anyInt())) + .thenReturn(new ArrayList<>()); + + assertThat(MobileNetworkUtils.buildPhoneAccountConfigureIntent(mContext, + mPhoneAccountHandle)).isNull(); + } + + @Test + public void buildConfigureIntent_hasActivityHandleIntent_returnIntent() { + mComponentName = new ComponentName(PACKAGE_NAME, "testClass"); + mPhoneAccountHandle = new PhoneAccountHandle(mComponentName, ""); + when(mPackageManager.queryIntentActivities(nullable(Intent.class), anyInt())) + .thenReturn(Arrays.asList(mResolveInfo)); + + assertThat(MobileNetworkUtils.buildPhoneAccountConfigureIntent(mContext, + mPhoneAccountHandle)).isNotNull(); + } + + @Test + public void isCdmaOptions_phoneTypeCdma_returnTrue() { + when(mTelephonyManager.getPhoneType()).thenReturn(TelephonyManager.PHONE_TYPE_CDMA); + + assertThat(MobileNetworkUtils.isCdmaOptions(mContext, SUB_ID_1)).isTrue(); + } + + @Test + public void isCdmaOptions_worldModeWithGsmWcdma_returnTrue() { + when(mTelephonyManager.getPhoneType()).thenReturn(TelephonyManager.PHONE_TYPE_GSM); + mCarrierConfig.putBoolean(CarrierConfigManager.KEY_WORLD_MODE_ENABLED_BOOL, true); + + Settings.Global.putInt(mContext.getContentResolver(), + android.provider.Settings.Global.PREFERRED_NETWORK_MODE + SUB_ID_1, + TelephonyManagerConstants.NETWORK_MODE_LTE_GSM_WCDMA); + + assertThat(MobileNetworkUtils.isCdmaOptions(mContext, SUB_ID_1)).isTrue(); + } + + @Test + public void isCdmaOptions_carrierWorldModeWithoutHideCarrier_returnTrue() { + when(mTelephonyManager.getPhoneType()).thenReturn(TelephonyManager.PHONE_TYPE_GSM); + mCarrierConfig.putBoolean(CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL, + false); + mCarrierConfig.putBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL, true); + + assertThat(MobileNetworkUtils.isCdmaOptions(mContext, SUB_ID_1)).isTrue(); + } + + @Test + public void getSearchableSubscriptionId_oneActive_returnValid() { + when(mSubscriptionManager.getActiveSubscriptionInfoList()).thenReturn( + Arrays.asList(mSubscriptionInfo1)); + assertThat(MobileNetworkUtils.getSearchableSubscriptionId(mContext)).isEqualTo(SUB_ID_1); + } + + @Test + public void getSearchableSubscriptionId_nonActive_returnInvalid() { + when(mSubscriptionManager.getActiveSubscriptionInfoList()).thenReturn(new ArrayList<>()); + + assertThat(MobileNetworkUtils.getSearchableSubscriptionId(mContext)) + .isEqualTo(SubscriptionManager.INVALID_SUBSCRIPTION_ID); + } + + @Test + public void shouldDisplayNetworkSelectOptions_HideCarrierNetwork_returnFalse() { + mCarrierConfig.putBoolean(CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL, + true); + + assertThat(MobileNetworkUtils.shouldDisplayNetworkSelectOptions(mContext, SUB_ID_1)) + .isFalse(); + } + + @Test + public void shouldDisplayNetworkSelectOptions_allCheckPass_returnTrue() { + mCarrierConfig.putBoolean(CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL, + false); + mCarrierConfig.putBoolean(CarrierConfigManager.KEY_OPERATOR_SELECTION_EXPAND_BOOL, true); + mCarrierConfig.putBoolean(CarrierConfigManager.KEY_CSP_ENABLED_BOOL, false); + when(mTelephonyManager.getPhoneType()).thenReturn(TelephonyManager.PHONE_TYPE_GSM); + + assertThat(MobileNetworkUtils.shouldDisplayNetworkSelectOptions(mContext, SUB_ID_1)) + .isTrue(); + } + + @Test + public void shouldSpeciallyUpdateGsmCdma_notWorldMode_returnFalse() { + mCarrierConfig.putBoolean(CarrierConfigManager.KEY_WORLD_MODE_ENABLED_BOOL, false); + mCarrierConfig.putBoolean(CarrierConfigManager.KEY_SUPPORT_TDSCDMA_BOOL, false); + + assertThat(MobileNetworkUtils.shouldSpeciallyUpdateGsmCdma(mContext, SUB_ID_1)).isFalse(); + } + + @Test + public void shouldSpeciallyUpdateGsmCdma_supportTdscdma_returnFalse() { + when(mCarrierConfigManager.getConfig()).thenReturn(mCarrierConfig); + mCarrierConfig.putBoolean(CarrierConfigManager.KEY_WORLD_MODE_ENABLED_BOOL, true); + mCarrierConfig.putBoolean(CarrierConfigManager.KEY_SUPPORT_TDSCDMA_BOOL, true); + + assertThat(MobileNetworkUtils.shouldSpeciallyUpdateGsmCdma(mContext, SUB_ID_1)).isFalse(); + } + + @Test + public void shouldSpeciallyUpdateGsmCdma_ModeLteTdscdmaGsm_returnTrue() { + mCarrierConfig.putBoolean(CarrierConfigManager.KEY_WORLD_MODE_ENABLED_BOOL, true); + mCarrierConfig.putBoolean(CarrierConfigManager.KEY_SUPPORT_TDSCDMA_BOOL, false); + Settings.Global.putInt(mContext.getContentResolver(), + android.provider.Settings.Global.PREFERRED_NETWORK_MODE + SUB_ID_1, + TelephonyManagerConstants.NETWORK_MODE_LTE_TDSCDMA_GSM); + + assertThat(MobileNetworkUtils.shouldSpeciallyUpdateGsmCdma(mContext, SUB_ID_1)).isTrue(); + } + + @Test + public void shouldSpeciallyUpdateGsmCdma_ModeLteTdscdmaGsmWcdma_returnTrue() { + mCarrierConfig.putBoolean(CarrierConfigManager.KEY_WORLD_MODE_ENABLED_BOOL, true); + mCarrierConfig.putBoolean(CarrierConfigManager.KEY_SUPPORT_TDSCDMA_BOOL, false); + Settings.Global.putInt(mContext.getContentResolver(), + android.provider.Settings.Global.PREFERRED_NETWORK_MODE + SUB_ID_1, + TelephonyManagerConstants.NETWORK_MODE_LTE_TDSCDMA_GSM_WCDMA); + + assertThat(MobileNetworkUtils.shouldSpeciallyUpdateGsmCdma(mContext, SUB_ID_1)).isTrue(); + } + + @Test + public void shouldSpeciallyUpdateGsmCdma_ModeLteTdscdma_returnTrue() { + mCarrierConfig.putBoolean(CarrierConfigManager.KEY_WORLD_MODE_ENABLED_BOOL, true); + mCarrierConfig.putBoolean(CarrierConfigManager.KEY_SUPPORT_TDSCDMA_BOOL, false); + Settings.Global.putInt(mContext.getContentResolver(), + android.provider.Settings.Global.PREFERRED_NETWORK_MODE + SUB_ID_1, + TelephonyManagerConstants.NETWORK_MODE_LTE_TDSCDMA); + + assertThat(MobileNetworkUtils.shouldSpeciallyUpdateGsmCdma(mContext, SUB_ID_1)).isTrue(); + } + + @Test + public void shouldSpeciallyUpdateGsmCdma_ModeLteTdscdmaWcdma_returnTrue() { + mCarrierConfig.putBoolean(CarrierConfigManager.KEY_WORLD_MODE_ENABLED_BOOL, true); + mCarrierConfig.putBoolean(CarrierConfigManager.KEY_SUPPORT_TDSCDMA_BOOL, false); + Settings.Global.putInt(mContext.getContentResolver(), + android.provider.Settings.Global.PREFERRED_NETWORK_MODE + SUB_ID_1, + TelephonyManagerConstants.NETWORK_MODE_LTE_TDSCDMA_WCDMA); + + assertThat(MobileNetworkUtils.shouldSpeciallyUpdateGsmCdma(mContext, SUB_ID_1)).isTrue(); + } + + @Test + public void shouldSpeciallyUpdateGsmCdma_ModeLteTdscdmaCdmaEvdoGsmWcdma_returnTrue() { + mCarrierConfig.putBoolean(CarrierConfigManager.KEY_WORLD_MODE_ENABLED_BOOL, true); + mCarrierConfig.putBoolean(CarrierConfigManager.KEY_SUPPORT_TDSCDMA_BOOL, false); + Settings.Global.putInt(mContext.getContentResolver(), + android.provider.Settings.Global.PREFERRED_NETWORK_MODE + SUB_ID_1, + TelephonyManagerConstants.NETWORK_MODE_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA); + + assertThat(MobileNetworkUtils.shouldSpeciallyUpdateGsmCdma(mContext, SUB_ID_1)).isTrue(); + } + + @Test + public void shouldSpeciallyUpdateGsmCdma_ModeLteCdmaEvdoGsmWcdma_returnTrue() { + mCarrierConfig.putBoolean(CarrierConfigManager.KEY_WORLD_MODE_ENABLED_BOOL, true); + mCarrierConfig.putBoolean(CarrierConfigManager.KEY_SUPPORT_TDSCDMA_BOOL, false); + Settings.Global.putInt(mContext.getContentResolver(), + android.provider.Settings.Global.PREFERRED_NETWORK_MODE + SUB_ID_1, + TelephonyManagerConstants.NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA); + + assertThat(MobileNetworkUtils.shouldSpeciallyUpdateGsmCdma(mContext, SUB_ID_1)).isTrue(); + } + + @Test + public void getCurrentCarrierNameForDisplay_withoutValidSubId_returnNetworkOperatorName() { + assertThat(MobileNetworkUtils.getCurrentCarrierNameForDisplay( + mContext, SUB_ID_INVALID)).isEqualTo(PLMN_FROM_TELEPHONY_MANAGER_API); + } + + @Test + public void getCurrentCarrierNameForDisplay_withValidSubId_returnCurrentCarrierName() { + assertThat(MobileNetworkUtils.getCurrentCarrierNameForDisplay( + mContext, SUB_ID_1)).isEqualTo(PLMN_FROM_SUB_ID_1); + assertThat(MobileNetworkUtils.getCurrentCarrierNameForDisplay( + mContext, SUB_ID_2)).isEqualTo(PLMN_FROM_SUB_ID_2); + } + + @Test + public void getCurrentCarrierNameForDisplay_withoutSubId_returnNotNull() { + assertThat(MobileNetworkUtils.getCurrentCarrierNameForDisplay( + mContext)).isNotNull(); + } +} From fbb61ba4d0b88211c5b7322fc749d97a2872a698 Mon Sep 17 00:00:00 2001 From: changbetty Date: Fri, 21 Aug 2020 17:44:53 +0800 Subject: [PATCH 2/3] [Testing] Use AndroidJUnit4 instead in telephony/cdma/ test files 1. Use AndroidJunit4 instead of RobolectricTestRunner 2. Use ApplicationProvider instead of RuntimeEnvironment to get context 3. Change Copy Right 4. To get the original value and add @After to restore the data Bug: Test: atest -c CdmaListPreferenceTest atest -c CdmaSubscriptionPreferenceControllerTest atest -c CdmaSystemSelectPreferenceControllerTest Change-Id: I110727b643a6755ee888708bd1fb2a39cf524a2d Merged-In: I110727b643a6755ee888708bd1fb2a39cf524a2d --- .../cdma/CdmaListPreferenceTest.java | 69 ++++++++ ...aSubscriptionPreferenceControllerTest.java | 133 ++++++++++++++++ ...aSystemSelectPreferenceControllerTest.java | 149 ++++++++++++++++++ 3 files changed, 351 insertions(+) create mode 100644 tests/unit/src/com/android/settings/network/telephony/cdma/CdmaListPreferenceTest.java create mode 100644 tests/unit/src/com/android/settings/network/telephony/cdma/CdmaSubscriptionPreferenceControllerTest.java create mode 100644 tests/unit/src/com/android/settings/network/telephony/cdma/CdmaSystemSelectPreferenceControllerTest.java diff --git a/tests/unit/src/com/android/settings/network/telephony/cdma/CdmaListPreferenceTest.java b/tests/unit/src/com/android/settings/network/telephony/cdma/CdmaListPreferenceTest.java new file mode 100644 index 00000000000..027a53bb8c4 --- /dev/null +++ b/tests/unit/src/com/android/settings/network/telephony/cdma/CdmaListPreferenceTest.java @@ -0,0 +1,69 @@ +/* + * 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.cdma; + +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import android.content.Context; +import android.telephony.TelephonyManager; + +import androidx.preference.PreferenceManager; +import androidx.test.core.app.ApplicationProvider; +import androidx.test.ext.junit.runners.AndroidJUnit4; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +@RunWith(AndroidJUnit4.class) +public class CdmaListPreferenceTest { + private static final int SUB_ID = 2; + + @Mock + private TelephonyManager mTelephonyManager; + @Mock + private PreferenceManager mPreferenceManager; + + private CdmaListPreference mPreference; + private Context mContext; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + + mContext = spy(ApplicationProvider.getApplicationContext()); + when(mContext.getSystemService(TelephonyManager.class)).thenReturn(mTelephonyManager); + when(mTelephonyManager.createForSubscriptionId(SUB_ID)).thenReturn(mTelephonyManager); + mPreference = spy(new CdmaListPreference(mContext, null)); + mPreference.setSubId(SUB_ID); + } + + @Test + public void onClick_inEcm_doNothing() { + doReturn(true).when(mTelephonyManager).getEmergencyCallbackMode(); + + mPreference.onClick(); + + verify(mPreferenceManager, never()).showDialog(mPreference); + } +} diff --git a/tests/unit/src/com/android/settings/network/telephony/cdma/CdmaSubscriptionPreferenceControllerTest.java b/tests/unit/src/com/android/settings/network/telephony/cdma/CdmaSubscriptionPreferenceControllerTest.java new file mode 100644 index 00000000000..86488c655aa --- /dev/null +++ b/tests/unit/src/com/android/settings/network/telephony/cdma/CdmaSubscriptionPreferenceControllerTest.java @@ -0,0 +1,133 @@ +/* + * 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.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 static org.mockito.Mockito.when; + +import android.content.Context; +import android.os.SystemProperties; +import android.provider.Settings; +import android.telephony.TelephonyManager; + +import androidx.preference.ListPreference; +import androidx.preference.PreferenceManager; +import androidx.test.core.app.ApplicationProvider; +import androidx.test.ext.junit.runners.AndroidJUnit4; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +@RunWith(AndroidJUnit4.class) +public class CdmaSubscriptionPreferenceControllerTest { + private static final int SUB_ID = 2; + + @Mock + private PreferenceManager mPreferenceManager; + @Mock + private TelephonyManager mTelephonyManager; + + private CdmaSubscriptionPreferenceController mController; + private ListPreference mPreference; + private Context mContext; + private int mCdmaMode; + private String mSubscriptionsSupported; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + + mContext = spy(ApplicationProvider.getApplicationContext()); + when(mContext.getSystemService(TelephonyManager.class)).thenReturn(mTelephonyManager); + doReturn(mTelephonyManager).when(mTelephonyManager).createForSubscriptionId(SUB_ID); + + mPreference = new ListPreference(mContext); + mController = new CdmaSubscriptionPreferenceController(mContext, "mobile_data"); + mController.init(mPreferenceManager, SUB_ID); + mController.mPreference = mPreference; + mPreference.setKey(mController.getPreferenceKey()); + + mCdmaMode = Settings.Global.getInt(mContext.getContentResolver(), + Settings.Global.CDMA_SUBSCRIPTION_MODE, + TelephonyManager.CDMA_SUBSCRIPTION_RUIM_SIM); + + mSubscriptionsSupported = SystemProperties.get("ril.subscription.types"); + } + + @After + public void tearDown() { + Settings.Global.putInt(mContext.getContentResolver(), + Settings.Global.CDMA_SUBSCRIPTION_MODE, mCdmaMode); + + SystemProperties.set("ril.subscription.types", mSubscriptionsSupported); + } + + @Test + public void onPreferenceChange_selectNV_returnNVMode() { + doReturn(true).when(mTelephonyManager).setCdmaSubscriptionMode(anyInt()); + + mController.onPreferenceChange(mPreference, Integer.toString( + TelephonyManager.CDMA_SUBSCRIPTION_NV)); + + assertThat(Settings.Global.getInt(mContext.getContentResolver(), + Settings.Global.CDMA_SUBSCRIPTION_MODE, + TelephonyManager.CDMA_SUBSCRIPTION_RUIM_SIM)).isEqualTo( + TelephonyManager.CDMA_SUBSCRIPTION_NV); + } + + @Test + public void updateState_stateRUIM_displayRUIM() { + Settings.Global.putInt(mContext.getContentResolver(), + Settings.Global.CDMA_SUBSCRIPTION_MODE, TelephonyManager.CDMA_SUBSCRIPTION_NV); + + mController.updateState(mPreference); + + assertThat(mPreference.getValue()).isEqualTo(Integer.toString( + TelephonyManager.CDMA_SUBSCRIPTION_NV)); + } + + @Test + public void updateState_stateUnknown_doNothing() { + mPreference.setValue(Integer.toString(TelephonyManager.CDMA_SUBSCRIPTION_NV)); + Settings.Global.putInt(mContext.getContentResolver(), + Settings.Global.CDMA_SUBSCRIPTION_MODE, TelephonyManager.CDMA_SUBSCRIPTION_UNKNOWN); + + mController.updateState(mPreference); + + // Still NV mode + assertThat(mPreference.getValue()).isEqualTo(Integer.toString( + TelephonyManager.CDMA_SUBSCRIPTION_NV)); + } + + @Test + public void deviceSupportsNvAndRuim() { + SystemProperties.set("ril.subscription.types", "NV,RUIM"); + assertThat(mController.deviceSupportsNvAndRuim()).isTrue(); + + SystemProperties.set("ril.subscription.types", ""); + + assertThat(mController.deviceSupportsNvAndRuim()).isFalse(); + } +} diff --git a/tests/unit/src/com/android/settings/network/telephony/cdma/CdmaSystemSelectPreferenceControllerTest.java b/tests/unit/src/com/android/settings/network/telephony/cdma/CdmaSystemSelectPreferenceControllerTest.java new file mode 100644 index 00000000000..4f44a29aae3 --- /dev/null +++ b/tests/unit/src/com/android/settings/network/telephony/cdma/CdmaSystemSelectPreferenceControllerTest.java @@ -0,0 +1,149 @@ +/* + * 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.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 static org.mockito.Mockito.when; + +import android.content.Context; +import android.provider.Settings; +import android.telephony.TelephonyManager; + +import androidx.preference.ListPreference; +import androidx.preference.PreferenceManager; +import androidx.test.core.app.ApplicationProvider; +import androidx.test.ext.junit.runners.AndroidJUnit4; + +import com.android.settings.network.telephony.TelephonyConstants.TelephonyManagerConstants; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +@RunWith(AndroidJUnit4.class) +public class CdmaSystemSelectPreferenceControllerTest { + private static final int SUB_ID = 2; + + @Mock + private PreferenceManager mPreferenceManager; + @Mock + private TelephonyManager mTelephonyManager; + + private CdmaSystemSelectPreferenceController mController; + private ListPreference mPreference; + private Context mContext; + private int mCdmaRoamingMode; + private int mSettingsNetworkMode; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + + mContext = spy(ApplicationProvider.getApplicationContext()); + when(mContext.getSystemService(TelephonyManager.class)).thenReturn(mTelephonyManager); + doReturn(mTelephonyManager).when(mTelephonyManager).createForSubscriptionId(SUB_ID); + + mPreference = new ListPreference(mContext); + mController = new CdmaSystemSelectPreferenceController(mContext, "mobile_data"); + mController.init(mPreferenceManager, SUB_ID); + mController.mPreference = mPreference; + mPreference.setKey(mController.getPreferenceKey()); + + mCdmaRoamingMode = Settings.Global.getInt(mContext.getContentResolver(), + Settings.Global.CDMA_ROAMING_MODE, + TelephonyManager.CDMA_ROAMING_MODE_ANY); + + mSettingsNetworkMode = Settings.Global.getInt( + mContext.getContentResolver(), + Settings.Global.PREFERRED_NETWORK_MODE + SUB_ID, + TelephonyManager.DEFAULT_PREFERRED_NETWORK_MODE); + } + + @After + public void tearDown() { + Settings.Global.putInt(mContext.getContentResolver(), + Settings.Global.CDMA_ROAMING_MODE, mCdmaRoamingMode); + + Settings.Global.putInt(mContext.getContentResolver(), + Settings.Global.PREFERRED_NETWORK_MODE + SUB_ID, mSettingsNetworkMode); + } + + @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_LteGSMWcdma_disabled() { + doReturn(TelephonyManager.CDMA_ROAMING_MODE_HOME).when( + mTelephonyManager).getCdmaRoamingMode(); + Settings.Global.putInt(mContext.getContentResolver(), + Settings.Global.PREFERRED_NETWORK_MODE + SUB_ID, + TelephonyManagerConstants.NETWORK_MODE_LTE_GSM_WCDMA); + + mController.updateState(mPreference); + + assertThat(mPreference.isEnabled()).isFalse(); + } + + @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); + } +} From 3611c3e22f77b098d04c1427519cad749f311f6d Mon Sep 17 00:00:00 2001 From: Jeremy Goldman Date: Fri, 11 Dec 2020 20:09:31 +0800 Subject: [PATCH 3/3] Settings JUnit test for PreferredNetworkModeContentObserverTest Biggest difference here: the use of a MockContentProvider Bug: 175389659 Test: atest -c PreferredNetworkModeContentObserverTest Change-Id: I891620118f7e84aa1c7f9e0ccdaeb21a225dc31a Merged-In: I891620118f7e84aa1c7f9e0ccdaeb21a225dc31a --- ...eferredNetworkModeContentObserverTest.java | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) rename tests/{robotests => unit}/src/com/android/settings/network/PreferredNetworkModeContentObserverTest.java (79%) diff --git a/tests/robotests/src/com/android/settings/network/PreferredNetworkModeContentObserverTest.java b/tests/unit/src/com/android/settings/network/PreferredNetworkModeContentObserverTest.java similarity index 79% rename from tests/robotests/src/com/android/settings/network/PreferredNetworkModeContentObserverTest.java rename to tests/unit/src/com/android/settings/network/PreferredNetworkModeContentObserverTest.java index 657b8b3dc9d..e07a8f140e0 100644 --- a/tests/robotests/src/com/android/settings/network/PreferredNetworkModeContentObserverTest.java +++ b/tests/unit/src/com/android/settings/network/PreferredNetworkModeContentObserverTest.java @@ -16,41 +16,43 @@ package com.android.settings.network; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import android.content.ContentResolver; import android.content.Context; import android.provider.Settings; +import android.test.mock.MockContentResolver; + +import androidx.test.core.app.ApplicationProvider; +import androidx.test.ext.junit.runners.AndroidJUnit4; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import org.robolectric.RobolectricTestRunner; -import org.robolectric.RuntimeEnvironment; -@RunWith(RobolectricTestRunner.class) +@RunWith(AndroidJUnit4.class) public class PreferredNetworkModeContentObserverTest { private static final int SUB_ID = 1; - @Mock - private ContentResolver mResolver; - @Mock private Context mContext; + private MockContentResolver mResolver; + private PreferredNetworkModeContentObserver mPreferredNetworkModeContentObserver; + @Mock private PreferredNetworkModeContentObserver.OnPreferredNetworkModeChangedListener mListener; - private PreferredNetworkModeContentObserver mPreferredNetworkModeContentObserver; @Before public void setUp() { MockitoAnnotations.initMocks(this); - when(mContext.getContentResolver()).thenReturn(mResolver); + mContext = spy(ApplicationProvider.getApplicationContext()); + + mResolver = spy(new MockContentResolver(mContext)); + when(mContext.getContentResolver()).thenReturn(mResolver); mPreferredNetworkModeContentObserver = spy(new PreferredNetworkModeContentObserver(null)); } @@ -68,8 +70,8 @@ public class PreferredNetworkModeContentObserverTest { mPreferredNetworkModeContentObserver.register(mContext, SUB_ID); verify(mResolver).registerContentObserver( - Settings.Global.getUriFor(Settings.Global.PREFERRED_NETWORK_MODE + SUB_ID), false, - mPreferredNetworkModeContentObserver); + Settings.Global.getUriFor(Settings.Global.PREFERRED_NETWORK_MODE + SUB_ID), false, + mPreferredNetworkModeContentObserver); } @Test @@ -78,5 +80,4 @@ public class PreferredNetworkModeContentObserverTest { verify(mResolver).unregisterContentObserver(mPreferredNetworkModeContentObserver); } - }