[Settings] Code refactor - NetworkSelectSettingsTest

Move NetworkSelectSettingsTest.java from robolectric into JUnit.

Bug: 189742455
Test: local, JUnit
Change-Id: I72066cc04ea6bdc8f7c68351e649926350fe41c9
This commit is contained in:
Bonian Chen
2021-05-19 18:19:11 +08:00
parent f17a812842
commit e96b889d5b
5 changed files with 320 additions and 180 deletions

View File

@@ -145,6 +145,8 @@ public final class CellInfoUtil {
final CellIdentity cid = getCellIdentity(cellInfo);
String mcc = null;
String mnc = null;
CharSequence alphaLong = null;
CharSequence alphaShort = null;
if (cid != null) {
if (cid instanceof CellIdentityGsm) {
mcc = ((CellIdentityGsm) cid).getMccString();
@@ -162,10 +164,13 @@ public final class CellInfoUtil {
mcc = ((CellIdentityNr) cid).getMccString();
mnc = ((CellIdentityNr) cid).getMncString();
}
alphaLong = cid.getOperatorAlphaLong();
alphaShort = cid.getOperatorAlphaShort();
}
return String.format(
"{CellType = %s, isRegistered = %b, mcc = %s, mnc = %s, alphaL = %s, alphaS = %s}",
cellType, cellInfo.isRegistered(), mcc, mnc,
cid.getOperatorAlphaLong(), cid.getOperatorAlphaShort());
alphaLong, alphaShort);
}
}

View File

@@ -36,6 +36,7 @@ import android.telephony.CellInfoWcdma;
import android.telephony.CellSignalStrength;
import android.util.Log;
import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
import com.android.internal.telephony.OperatorInfo;
@@ -89,7 +90,8 @@ public class NetworkOperatorPreference extends Preference {
updateCell(cellinfo, CellInfoUtil.getCellIdentity(cellinfo));
}
private void updateCell(CellInfo cellinfo, CellIdentity cellId) {
@VisibleForTesting
protected void updateCell(CellInfo cellinfo, CellIdentity cellId) {
mCellInfo = cellinfo;
mCellId = cellId;
refresh();

View File

@@ -36,6 +36,7 @@ import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import androidx.annotation.Keep;
import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
@@ -56,6 +57,7 @@ import java.util.concurrent.Executors;
/**
* "Choose network" settings UI for the Settings app.
*/
@Keep
public class NetworkSelectSettings extends DashboardFragment {
private static final String TAG = "NetworkSelectSettings";
@@ -67,8 +69,7 @@ public class NetworkSelectSettings extends DashboardFragment {
private static final String PREF_KEY_NETWORK_OPERATORS = "network_operators_preference";
@VisibleForTesting
PreferenceCategory mPreferenceCategory;
private PreferenceCategory mPreferenceCategory;
@VisibleForTesting
NetworkOperatorPreference mSelectedPreference;
private View mProgressHeader;
@@ -76,8 +77,7 @@ public class NetworkSelectSettings extends DashboardFragment {
@VisibleForTesting
List<CellInfo> mCellInfoList;
private int mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
@VisibleForTesting
TelephonyManager mTelephonyManager;
private TelephonyManager mTelephonyManager;
private List<String> mForbiddenPlmns;
private boolean mShow4GForLTE = false;
private NetworkScanHelper mNetworkScanHelper;
@@ -93,28 +93,74 @@ public class NetworkSelectSettings extends DashboardFragment {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
onCreateInitialization();
}
mUseNewApi = getContext().getResources().getBoolean(
com.android.internal.R.bool.config_enableNewAutoSelectNetworkUI);
@Keep
@VisibleForTesting
protected void onCreateInitialization() {
mUseNewApi = enableNewAutoSelectNetworkUI(getContext());
mSubId = getArguments().getInt(Settings.EXTRA_SUB_ID);
mPreferenceCategory = findPreference(PREF_KEY_NETWORK_OPERATORS);
mPreferenceCategory = getPreferenceCategory(PREF_KEY_NETWORK_OPERATORS);
mStatusMessagePreference = new Preference(getContext());
mStatusMessagePreference.setSelectable(false);
mSelectedPreference = null;
mTelephonyManager = getContext().getSystemService(TelephonyManager.class)
.createForSubscriptionId(mSubId);
mTelephonyManager = getTelephonyManager(getContext(), mSubId);
mNetworkScanHelper = new NetworkScanHelper(
mTelephonyManager, mCallback, mNetworkScanExecutor);
PersistableBundle bundle = ((CarrierConfigManager) getContext().getSystemService(
Context.CARRIER_CONFIG_SERVICE)).getConfigForSubId(mSubId);
PersistableBundle bundle = getCarrierConfigManager(getContext())
.getConfigForSubId(mSubId);
if (bundle != null) {
mShow4GForLTE = bundle.getBoolean(
CarrierConfigManager.KEY_SHOW_4G_FOR_LTE_DATA_ICON_BOOL);
}
mMetricsFeatureProvider = FeatureFactory
.getFactory(getContext()).getMetricsFeatureProvider();
mMetricsFeatureProvider = getMetricsFeatureProvider(getContext());
}
@Keep
@VisibleForTesting
protected boolean enableNewAutoSelectNetworkUI(Context context) {
return context.getResources().getBoolean(
com.android.internal.R.bool.config_enableNewAutoSelectNetworkUI);
}
@Keep
@VisibleForTesting
protected PreferenceCategory getPreferenceCategory(String preferenceKey) {
return findPreference(preferenceKey);
}
@Keep
@VisibleForTesting
protected TelephonyManager getTelephonyManager(Context context, int subscriptionId) {
return context.getSystemService(TelephonyManager.class)
.createForSubscriptionId(subscriptionId);
}
@Keep
@VisibleForTesting
protected CarrierConfigManager getCarrierConfigManager(Context context) {
return context.getSystemService(CarrierConfigManager.class);
}
@Keep
@VisibleForTesting
protected MetricsFeatureProvider getMetricsFeatureProvider(Context context) {
return FeatureFactory.getFactory(context).getMetricsFeatureProvider();
}
@Keep
@VisibleForTesting
protected boolean isPreferenceScreenEnabled() {
return getPreferenceScreen().isEnabled();
}
@Keep
@VisibleForTesting
protected void enablePreferenceScreen(boolean enable) {
getPreferenceScreen().setEnabled(enable);
}
@Override
@@ -146,8 +192,9 @@ public class NetworkSelectSettings extends DashboardFragment {
/**
* Update forbidden PLMNs from the USIM App
*/
@Keep
@VisibleForTesting
void updateForbiddenPlmns() {
protected void updateForbiddenPlmns() {
final String[] forbiddenPlmns = mTelephonyManager.getForbiddenPlmns();
mForbiddenPlmns = forbiddenPlmns != null
? Arrays.asList(forbiddenPlmns)
@@ -182,7 +229,7 @@ public class NetworkSelectSettings extends DashboardFragment {
setProgressBarVisible(true);
// Disable the screen until network is manually set
getPreferenceScreen().setEnabled(false);
enablePreferenceScreen(false);
mRequestIdManualNetworkSelect = getNewRequestId();
mWaitingForNumberOfScanResults = MIN_NUMBER_OF_SCAN_REQUIRED;
@@ -222,7 +269,7 @@ public class NetworkSelectSettings extends DashboardFragment {
final boolean isSucceed = (boolean) msg.obj;
stopNetworkQuery();
setProgressBarVisible(false);
getPreferenceScreen().setEnabled(true);
enablePreferenceScreen(true);
if (mSelectedPreference != null) {
mSelectedPreference.setSummary(isSucceed
@@ -233,38 +280,7 @@ public class NetworkSelectSettings extends DashboardFragment {
}
break;
case EVENT_NETWORK_SCAN_RESULTS:
final List<CellInfo> results = (List<CellInfo>) msg.obj;
if (mRequestIdManualNetworkScan < mRequestIdManualNetworkSelect) {
Log.d(TAG, "CellInfoList (drop): "
+ CellInfoUtil.cellInfoListToString(new ArrayList<>(results)));
break;
}
mWaitingForNumberOfScanResults--;
if ((mWaitingForNumberOfScanResults <= 0) && (!isResumed())) {
stopNetworkQuery();
}
mCellInfoList = new ArrayList<>(results);
Log.d(TAG, "CellInfoList: " + CellInfoUtil.cellInfoListToString(mCellInfoList));
if (mCellInfoList != null && mCellInfoList.size() != 0) {
final NetworkOperatorPreference connectedPref =
updateAllPreferenceCategory();
if (connectedPref != null) {
// update selected preference instance into connected preference
if (mSelectedPreference != null) {
mSelectedPreference = connectedPref;
}
} else if (!getPreferenceScreen().isEnabled()) {
if (connectedPref == null) {
mSelectedPreference.setSummary(R.string.network_connecting);
}
}
getPreferenceScreen().setEnabled(true);
} else if (getPreferenceScreen().isEnabled()) {
addMessagePreference(R.string.empty_networks_list);
// keep showing progress bar, it will be stopped when error or completed
setProgressBarVisible(true);
}
scanResultHandler((List<CellInfo>) msg.obj);
break;
case EVENT_NETWORK_SCAN_ERROR:
@@ -277,9 +293,9 @@ public class NetworkSelectSettings extends DashboardFragment {
if (mRequestIdManualNetworkScan < mRequestIdManualNetworkSelect) {
break;
}
if (!getPreferenceScreen().isEnabled()) {
if (!isPreferenceScreenEnabled()) {
clearPreferenceSummary();
getPreferenceScreen().setEnabled(true);
enablePreferenceScreen(true);
} else {
addMessagePreference(R.string.network_query_error);
}
@@ -295,9 +311,9 @@ public class NetworkSelectSettings extends DashboardFragment {
if (mRequestIdManualNetworkScan < mRequestIdManualNetworkSelect) {
break;
}
if (!getPreferenceScreen().isEnabled()) {
if (!isPreferenceScreenEnabled()) {
clearPreferenceSummary();
getPreferenceScreen().setEnabled(true);
enablePreferenceScreen(true);
} else if (mCellInfoList == null) {
// In case the scan timeout before getting any results
addMessagePreference(R.string.empty_networks_list);
@@ -327,13 +343,55 @@ public class NetworkSelectSettings extends DashboardFragment {
}
};
@Keep
@VisibleForTesting
protected void scanResultHandler(List<CellInfo> results) {
if (mRequestIdManualNetworkScan < mRequestIdManualNetworkSelect) {
Log.d(TAG, "CellInfoList (drop): "
+ CellInfoUtil.cellInfoListToString(new ArrayList<>(results)));
return;
}
mWaitingForNumberOfScanResults--;
if ((mWaitingForNumberOfScanResults <= 0) && (!isResumed())) {
stopNetworkQuery();
}
mCellInfoList = new ArrayList<>(results);
Log.d(TAG, "CellInfoList: " + CellInfoUtil.cellInfoListToString(mCellInfoList));
if (mCellInfoList != null && mCellInfoList.size() != 0) {
final NetworkOperatorPreference connectedPref =
updateAllPreferenceCategory();
if (connectedPref != null) {
// update selected preference instance into connected preference
if (mSelectedPreference != null) {
mSelectedPreference = connectedPref;
}
} else if (!isPreferenceScreenEnabled()) {
if (connectedPref == null) {
mSelectedPreference.setSummary(R.string.network_connecting);
}
}
enablePreferenceScreen(true);
} else if (isPreferenceScreenEnabled()) {
addMessagePreference(R.string.empty_networks_list);
// keep showing progress bar, it will be stopped when error or completed
setProgressBarVisible(true);
}
}
@Keep
@VisibleForTesting
protected NetworkOperatorPreference createNetworkOperatorPreference(CellInfo cellInfo) {
return new NetworkOperatorPreference(getPrefContext(),
cellInfo, mForbiddenPlmns, mShow4GForLTE);
}
/**
* Update the content of network operators list.
*
* @return preference which shows connected
*/
@VisibleForTesting
NetworkOperatorPreference updateAllPreferenceCategory() {
private NetworkOperatorPreference updateAllPreferenceCategory() {
int numberOfPreferences = mPreferenceCategory.getPreferenceCount();
// remove unused preferences
@@ -361,8 +419,7 @@ public class NetworkSelectSettings extends DashboardFragment {
}
if (pref == null) {
// add new preference
pref = new NetworkOperatorPreference(getPrefContext(),
cellInfo, mForbiddenPlmns, mShow4GForLTE);
pref = createNetworkOperatorPreference(cellInfo);
pref.setOrder(index);
mPreferenceCategory.addPreference(pref);
}

View File

@@ -1,122 +0,0 @@
/*
* 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;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.SharedPreferences;
import android.telephony.CellIdentityLte;
import android.telephony.CellIdentityWcdma;
import android.telephony.CellInfoLte;
import android.telephony.CellInfoWcdma;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceManager;
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;
import java.util.Arrays;
@RunWith(RobolectricTestRunner.class)
public class NetworkSelectSettingsTest {
private static final int SUB_ID = 2;
private static final String CARRIER_NAME1 = "CarrierName1";
private static final String CARRIER_NAME2 = "CarrierName2";
@Mock
private TelephonyManager mTelephonyManager;
@Mock
private SubscriptionManager mSubscriptionManager;
@Mock
private CellInfoWcdma mCellInfo1;
@Mock
private CellIdentityWcdma mCellId1;
@Mock
private CellInfoLte mCellInfo2;
@Mock
private CellIdentityLte mCellId2;
@Mock
private PreferenceManager mPreferenceManager;
@Mock
private SharedPreferences mSharedPreferences;
private Context mContext;
private PreferenceCategory mPreferenceCategory;
private NetworkSelectSettings mNetworkSelectSettings;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
when(mContext.getSystemService(SubscriptionManager.class)).thenReturn(mSubscriptionManager);
when(mTelephonyManager.createForSubscriptionId(SUB_ID)).thenReturn(mTelephonyManager);
when(mCellInfo1.isRegistered()).thenReturn(true);
when(mCellInfo1.getCellIdentity()).thenReturn(mCellId1);
when(mCellId1.getOperatorAlphaLong()).thenReturn(CARRIER_NAME1);
when(mCellInfo2.isRegistered()).thenReturn(false);
when(mCellInfo2.getCellIdentity()).thenReturn(mCellId2);
when(mCellId2.getOperatorAlphaLong()).thenReturn(CARRIER_NAME2);
doReturn(mSharedPreferences).when(mPreferenceManager).getSharedPreferences();
mPreferenceCategory = spy(new PreferenceCategory(mContext));
doReturn(mPreferenceManager).when(mPreferenceCategory).getPreferenceManager();
mNetworkSelectSettings = spy(new NetworkSelectSettings());
doReturn(mContext).when(mNetworkSelectSettings).getContext();
doReturn(mPreferenceManager).when(mNetworkSelectSettings).getPreferenceManager();
doReturn(mContext).when(mPreferenceManager).getContext();
mNetworkSelectSettings.mTelephonyManager = mTelephonyManager;
mNetworkSelectSettings.mPreferenceCategory = mPreferenceCategory;
mNetworkSelectSettings.mCellInfoList = Arrays.asList(mCellInfo1, mCellInfo2);
}
@Test
public void updateAllPreferenceCategory_correctOrderingPreference() {
mNetworkSelectSettings.updateAllPreferenceCategory();
assertThat(mPreferenceCategory.getPreferenceCount()).isEqualTo(2);
final NetworkOperatorPreference preference =
(NetworkOperatorPreference) mPreferenceCategory.getPreference(1);
assertThat(preference.getOperatorName()).isEqualTo(mCellId2.getOperatorAlphaLong());
}
@Test
public void updateForbiddenPlmns_forbiddenPlmnsNull_shouldNotCrash() {
when(mTelephonyManager.getForbiddenPlmns()).thenReturn(null);
// Should not Crash
mNetworkSelectSettings.updateForbiddenPlmns();
}
}

View File

@@ -0,0 +1,198 @@
/*
* 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.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.provider.Settings;
import android.telephony.CarrierConfigManager;
import android.telephony.CellIdentity;
import android.telephony.CellInfo;
import android.telephony.TelephonyManager;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;
import androidx.test.annotation.UiThreadTest;
import androidx.test.core.app.ApplicationProvider;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.util.Arrays;
public class NetworkSelectSettingsTest {
private static final int SUB_ID = 2;
private static final String CARRIER_NAME1 = "CarrierName1";
private static final String CARRIER_NAME2 = "CarrierName2";
@Mock
public Resources mResources;
@Mock
public TelephonyManager mTelephonyManager;
@Mock
public CarrierConfigManager mCarrierConfigManager;
@Mock
public MetricsFeatureProvider mMetricsFeatureProvider;
@Mock
public NetworkOperatorPreference mNetworkOperatorPreference1;
@Mock
public NetworkOperatorPreference mNetworkOperatorPreference2;
@Mock
private CellInfo mCellInfo1;
@Mock
private CellIdentity mCellId1;
@Mock
private CellInfo mCellInfo2;
@Mock
private CellIdentity mCellId2;
private PreferenceScreen mPreferenceScreen;
@Mock
public PreferenceManager mPreferenceManager;
public Context mContext;
public PreferenceCategory mPreferenceCategory;
private Bundle mInitArguments;
private TargetClass mNetworkSelectSettings;
@Before
@UiThreadTest
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(ApplicationProvider.getApplicationContext());
doReturn(mResources).when(mContext).getResources();
doReturn(mContext).when(mPreferenceManager).getContext();
mPreferenceCategory = spy(new PreferenceCategory(mContext));
doReturn(mPreferenceManager).when(mPreferenceCategory).getPreferenceManager();
doReturn(CARRIER_NAME1).when(mCellId1).getOperatorAlphaLong();
doReturn(CARRIER_NAME2).when(mCellId2).getOperatorAlphaLong();
mNetworkSelectSettings = spy(new TargetClass(this));
PersistableBundle config = new PersistableBundle();
config.putBoolean(CarrierConfigManager.KEY_SHOW_4G_FOR_LTE_DATA_ICON_BOOL, true);
doReturn(config).when(mCarrierConfigManager).getConfigForSubId(SUB_ID);
doReturn(TelephonyManager.DATA_CONNECTED).when(mTelephonyManager).getDataState();
}
public class TargetClass extends NetworkSelectSettings {
private NetworkSelectSettingsTest mTestEnv;
private boolean mIsPreferenceScreenEnabled;
public TargetClass(NetworkSelectSettingsTest env) {
mTestEnv = env;
Bundle bundle = new Bundle();
bundle.putInt(Settings.EXTRA_SUB_ID, SUB_ID);
setArguments(bundle);
}
@Override
public Context getContext() {
return mTestEnv.mContext;
}
@Override
public PreferenceManager getPreferenceManager() {
return mTestEnv.mPreferenceManager;
}
@Override
protected PreferenceCategory getPreferenceCategory(String preferenceKey) {
return mTestEnv.mPreferenceCategory;
}
@Override
protected TelephonyManager getTelephonyManager(Context context, int subscriptionId) {
return mTestEnv.mTelephonyManager;
}
@Override
protected CarrierConfigManager getCarrierConfigManager(Context context) {
return mTestEnv.mCarrierConfigManager;
}
@Override
protected MetricsFeatureProvider getMetricsFeatureProvider(Context context) {
return mTestEnv.mMetricsFeatureProvider;
}
@Override
protected boolean isPreferenceScreenEnabled() {
return mIsPreferenceScreenEnabled;
}
@Override
protected void enablePreferenceScreen(boolean enable) {
mIsPreferenceScreenEnabled = enable;
}
@Override
protected NetworkOperatorPreference
createNetworkOperatorPreference(CellInfo cellInfo) {
NetworkOperatorPreference pref = super.createNetworkOperatorPreference(cellInfo);
if (cellInfo == mTestEnv.mCellInfo1) {
pref.updateCell(cellInfo, mTestEnv.mCellId1);
} else if (cellInfo == mTestEnv.mCellInfo2) {
pref.updateCell(cellInfo, mTestEnv.mCellId2);
}
return pref;
}
}
@Test
@UiThreadTest
public void updateAllPreferenceCategory_correctOrderingPreference() {
mNetworkSelectSettings.onCreateInitialization();
mNetworkSelectSettings.enablePreferenceScreen(true);
mNetworkSelectSettings.scanResultHandler(Arrays.asList(mCellInfo1, mCellInfo2));
assertThat(mPreferenceCategory.getPreferenceCount()).isEqualTo(2);
final NetworkOperatorPreference preference =
(NetworkOperatorPreference) mPreferenceCategory.getPreference(1);
assertThat(preference.getOperatorName()).isEqualTo(mCellId2.getOperatorAlphaLong());
}
@Test
@UiThreadTest
public void updateForbiddenPlmns_forbiddenPlmnsNull_shouldNotCrash() {
when(mTelephonyManager.getForbiddenPlmns()).thenReturn(null);
mNetworkSelectSettings.onCreateInitialization();
mNetworkSelectSettings.enablePreferenceScreen(true);
// Should not Crash
mNetworkSelectSettings.updateForbiddenPlmns();
}
}