[Settings Telephony] Carrier name API replacement

Replacing TelephonyManager.getNetworkOperatorName() by
Obtaining carrier name from SubscriptionInfo.getCarrierName().

TelephonyManager.getNetworkOperatorName() only be used when there's no
SubscriptionInfo can be found.

Bug: 140443508
Test: Manual testing
  atest MobileNetworkUtilsTest
  atest OpenNetworkSelectPagePreferenceControllerTest

Change-Id: I7a86395c86d31fe2ba54c04ac16b1a0ebfc843f3
This commit is contained in:
Bonian Chen
2019-09-09 15:10:13 +08:00
committed by Andy Chou
parent 5f10a9a9ea
commit 7a4c679790
6 changed files with 113 additions and 10 deletions

View File

@@ -22,6 +22,7 @@ import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
@@ -58,6 +59,10 @@ 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;
@@ -89,6 +94,7 @@ public class MobileNetworkUtilsTest {
mContext = spy(RuntimeEnvironment.application);
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);
@@ -102,10 +108,17 @@ public class MobileNetworkUtilsTest {
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(eq(true))).thenReturn(
Arrays.asList(mSubscriptionInfo1, mSubscriptionInfo2));
when(mSubscriptionManager.getAccessibleSubscriptionInfoList()).thenReturn(
Arrays.asList(mSubscriptionInfo1, mSubscriptionInfo2));
when(mTelephonyManager.getNetworkOperatorName()).thenReturn(
PLMN_FROM_TELEPHONY_MANAGER_API);
}
@Test
@@ -301,4 +314,24 @@ public class MobileNetworkUtilsTest {
TelephonyManager.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();
}
}

View File

@@ -17,7 +17,7 @@
package com.android.settings.network.telephony.gsm;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
@@ -26,12 +26,15 @@ 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 com.android.settings.R;
import com.android.settings.network.telephony.MobileNetworkUtils;
import java.util.Arrays;
import org.junit.Before;
import org.junit.Test;
@@ -54,6 +57,8 @@ public class OpenNetworkSelectPagePreferenceControllerTest {
private CarrierConfigManager mCarrierConfigManager;
@Mock
private ServiceState mServiceState;
@Mock
private SubscriptionInfo mSubscriptionInfo;
private PersistableBundle mCarrierConfig;
private OpenNetworkSelectPagePreferenceController mController;
@@ -75,6 +80,16 @@ public class OpenNetworkSelectPagePreferenceControllerTest {
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(eq(true))).thenReturn(
Arrays.asList(mSubscriptionInfo));
when(mSubscriptionManager.getAccessibleSubscriptionInfoList()).thenReturn(
Arrays.asList(mSubscriptionInfo));
when(mTelephonyManager.getNetworkOperatorName()).thenReturn(OPERATOR_NAME);
mPreference = new Preference(mContext);
mController = new OpenNetworkSelectPagePreferenceController(mContext,
"open_network_select");
@@ -94,7 +109,6 @@ public class OpenNetworkSelectPagePreferenceControllerTest {
@Test
public void getSummary_inService_returnOperatorName() {
when(mServiceState.getState()).thenReturn(ServiceState.STATE_IN_SERVICE);
doReturn(OPERATOR_NAME).when(mTelephonyManager).getNetworkOperatorName();
assertThat(mController.getSummary()).isEqualTo(OPERATOR_NAME);
}
@@ -102,7 +116,6 @@ public class OpenNetworkSelectPagePreferenceControllerTest {
@Test
public void getSummary_notInService_returnDisconnect() {
when(mServiceState.getState()).thenReturn(ServiceState.STATE_OUT_OF_SERVICE);
doReturn(OPERATOR_NAME).when(mTelephonyManager).getNetworkOperatorName();
assertThat(mController.getSummary()).isEqualTo(
mContext.getString(R.string.network_disconnected));