[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);
}