Fix failures on com.android.settings.network.ApnEditorTest

Bug: 152376182
Test: make RunSettingsRoboTests
Change-Id: Ib718a4ed0163e07be748857a0dc7b2af92036c3f
diff --git a/src/com/android/settings/network/ApnEditor.java b/src/com/android/settings/network/ApnEditor.java
index 6de2550..5da6e2c 100644
--- a/src/com/android/settings/network/ApnEditor.java
+++ b/src/com/android/settings/network/ApnEditor.java
@@ -130,7 +130,8 @@
 
     private boolean mNewApn;
     private int mSubId;
-    private ProxySubscriptionManager mProxySubscriptionMgr;
+    @VisibleForTesting
+    ProxySubscriptionManager mProxySubscriptionMgr;
     private int mBearerInitialVal = 0;
     private String mMvnoTypeStr;
     private String mMvnoMatchDataStr;
@@ -263,34 +264,7 @@
     public void onCreate(Bundle icicle) {
         super.onCreate(icicle);
 
-        // enable ProxySubscriptionMgr with Lifecycle support for all controllers
-        // live within this fragment
-        mProxySubscriptionMgr = ProxySubscriptionManager.getInstance(getContext());
-        mProxySubscriptionMgr.setLifecycle(getLifecycle());
-
-        addPreferencesFromResource(R.xml.apn_editor);
-
-        sNotSet = getResources().getString(R.string.apn_not_set);
-        mName = (EditTextPreference) findPreference("apn_name");
-        mApn = (EditTextPreference) findPreference("apn_apn");
-        mProxy = (EditTextPreference) findPreference("apn_http_proxy");
-        mPort = (EditTextPreference) findPreference("apn_http_port");
-        mUser = (EditTextPreference) findPreference("apn_user");
-        mServer = (EditTextPreference) findPreference("apn_server");
-        mPassword = (EditTextPreference) findPreference(KEY_PASSWORD);
-        mMmsProxy = (EditTextPreference) findPreference("apn_mms_proxy");
-        mMmsPort = (EditTextPreference) findPreference("apn_mms_port");
-        mMmsc = (EditTextPreference) findPreference("apn_mmsc");
-        mMcc = (EditTextPreference) findPreference("apn_mcc");
-        mMnc = (EditTextPreference) findPreference("apn_mnc");
-        mApnType = (EditTextPreference) findPreference("apn_type");
-        mAuthType = (ListPreference) findPreference(KEY_AUTH_TYPE);
-        mProtocol = (ListPreference) findPreference(KEY_PROTOCOL);
-        mRoamingProtocol = (ListPreference) findPreference(KEY_ROAMING_PROTOCOL);
-        mCarrierEnabled = (SwitchPreference) findPreference(KEY_CARRIER_ENABLED);
-        mBearerMulti = (MultiSelectListPreference) findPreference(KEY_BEARER_MULTI);
-        mMvnoType = (ListPreference) findPreference(KEY_MVNO_TYPE);
-        mMvnoMatchData = (EditTextPreference) findPreference("mvno_match_data");
+        setLifecycleForAllControllers();
 
         final Intent intent = getIntent();
         final String action = intent.getAction();
@@ -298,47 +272,11 @@
             finish();
             return;
         }
-
         mSubId = intent.getIntExtra(ApnSettings.SUB_ID,
                 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
-        mReadOnlyApn = false;
-        mReadOnlyApnTypes = null;
-        mReadOnlyApnFields = null;
 
-        final CarrierConfigManager configManager = (CarrierConfigManager)
-                getSystemService(Context.CARRIER_CONFIG_SERVICE);
-        if (configManager != null) {
-            final PersistableBundle b = configManager.getConfigForSubId(mSubId);
-            if (b != null) {
-                mReadOnlyApnTypes = b.getStringArray(
-                        CarrierConfigManager.KEY_READ_ONLY_APN_TYPES_STRING_ARRAY);
-                if (!ArrayUtils.isEmpty(mReadOnlyApnTypes)) {
-                    Log.d(TAG,
-                            "onCreate: read only APN type: " + Arrays.toString(mReadOnlyApnTypes));
-                }
-                mReadOnlyApnFields = b.getStringArray(
-                        CarrierConfigManager.KEY_READ_ONLY_APN_FIELDS_STRING_ARRAY);
-
-                mDefaultApnTypes = b.getStringArray(
-                        CarrierConfigManager.KEY_APN_SETTINGS_DEFAULT_APN_TYPES_STRING_ARRAY);
-                if (!ArrayUtils.isEmpty(mDefaultApnTypes)) {
-                    Log.d(TAG, "onCreate: default apn types: " + Arrays.toString(mDefaultApnTypes));
-                }
-
-                mDefaultApnProtocol = b.getString(
-                        CarrierConfigManager.Apn.KEY_SETTINGS_DEFAULT_PROTOCOL_STRING);
-                if (!TextUtils.isEmpty(mDefaultApnProtocol)) {
-                    Log.d(TAG, "onCreate: default apn protocol: " + mDefaultApnProtocol);
-                }
-
-                mDefaultApnRoamingProtocol = b.getString(
-                        CarrierConfigManager.Apn.KEY_SETTINGS_DEFAULT_ROAMING_PROTOCOL_STRING);
-                if (!TextUtils.isEmpty(mDefaultApnRoamingProtocol)) {
-                    Log.d(TAG, "onCreate: default apn roaming protocol: "
-                            + mDefaultApnRoamingProtocol);
-                }
-            }
-        }
+        initApnEditorUi();
+        getCarrierCustomizedConfig();
 
         Uri uri = null;
         if (action.equals(Intent.ACTION_EDIT)) {
@@ -391,6 +329,17 @@
         }
     }
 
+    /**
+     * Enable ProxySubscriptionMgr with Lifecycle support for all controllers
+     * live within this fragment
+     */
+    private void setLifecycleForAllControllers() {
+        if (mProxySubscriptionMgr == null) {
+            mProxySubscriptionMgr = ProxySubscriptionManager.getInstance(getContext());
+        }
+        mProxySubscriptionMgr.setLifecycle(getLifecycle());
+    }
+
     @Override
     public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
         super.onViewStateRestored(savedInstanceState);
@@ -1279,32 +1228,102 @@
             if (TextUtils.isEmpty(userEnteredApnType) && !ArrayUtils.isEmpty(mDefaultApnTypes)) {
                 apnTypeList = mDefaultApnTypes;
             }
-
-            final StringBuilder editableApnTypes = new StringBuilder();
-            final List<String> readOnlyApnTypes = Arrays.asList(mReadOnlyApnTypes);
-            boolean first = true;
-            for (String apnType : apnTypeList) {
-                // add APN type if it is not read-only and is not wild-cardable
-                if (!readOnlyApnTypes.contains(apnType)
-                        && !apnType.equals(APN_TYPE_IA)
-                        && !apnType.equals(APN_TYPE_EMERGENCY)
-                        && !apnType.equals(APN_TYPE_MCX)) {
-                    if (first) {
-                        first = false;
-                    } else {
-                        editableApnTypes.append(",");
-                    }
-                    editableApnTypes.append(apnType);
-                }
-            }
-            userEnteredApnType = editableApnTypes.toString();
+            userEnteredApnType = getEditableApnType(apnTypeList);
             Log.d(TAG, "getUserEnteredApnType: changed apn type to editable apn types: "
                     + userEnteredApnType);
         }
-
         return userEnteredApnType;
     }
 
+    private String getEditableApnType(String[] apnTypeList) {
+        final StringBuilder editableApnTypes = new StringBuilder();
+        final List<String> readOnlyApnTypes = Arrays.asList(mReadOnlyApnTypes);
+        boolean first = true;
+        for (String apnType : apnTypeList) {
+            // add APN type if it is not read-only and is not wild-cardable
+            if (!readOnlyApnTypes.contains(apnType)
+                    && !apnType.equals(APN_TYPE_IA)
+                    && !apnType.equals(APN_TYPE_EMERGENCY)
+                    && !apnType.equals(APN_TYPE_MCX)) {
+                if (first) {
+                    first = false;
+                } else {
+                    editableApnTypes.append(",");
+                }
+                editableApnTypes.append(apnType);
+            }
+        }
+        return editableApnTypes.toString();
+    }
+
+    private void initApnEditorUi() {
+        addPreferencesFromResource(R.xml.apn_editor);
+
+        sNotSet = getResources().getString(R.string.apn_not_set);
+        mName = (EditTextPreference) findPreference("apn_name");
+        mApn = (EditTextPreference) findPreference("apn_apn");
+        mProxy = (EditTextPreference) findPreference("apn_http_proxy");
+        mPort = (EditTextPreference) findPreference("apn_http_port");
+        mUser = (EditTextPreference) findPreference("apn_user");
+        mServer = (EditTextPreference) findPreference("apn_server");
+        mPassword = (EditTextPreference) findPreference(KEY_PASSWORD);
+        mMmsProxy = (EditTextPreference) findPreference("apn_mms_proxy");
+        mMmsPort = (EditTextPreference) findPreference("apn_mms_port");
+        mMmsc = (EditTextPreference) findPreference("apn_mmsc");
+        mMcc = (EditTextPreference) findPreference("apn_mcc");
+        mMnc = (EditTextPreference) findPreference("apn_mnc");
+        mApnType = (EditTextPreference) findPreference("apn_type");
+        mAuthType = (ListPreference) findPreference(KEY_AUTH_TYPE);
+        mProtocol = (ListPreference) findPreference(KEY_PROTOCOL);
+        mRoamingProtocol = (ListPreference) findPreference(KEY_ROAMING_PROTOCOL);
+        mCarrierEnabled = (SwitchPreference) findPreference(KEY_CARRIER_ENABLED);
+        mBearerMulti = (MultiSelectListPreference) findPreference(KEY_BEARER_MULTI);
+        mMvnoType = (ListPreference) findPreference(KEY_MVNO_TYPE);
+        mMvnoMatchData = (EditTextPreference) findPreference("mvno_match_data");
+    }
+
+    private void getCarrierCustomizedConfig() {
+        mReadOnlyApn = false;
+        mReadOnlyApnTypes = null;
+        mReadOnlyApnFields = null;
+
+        final CarrierConfigManager configManager = (CarrierConfigManager)
+                getSystemService(Context.CARRIER_CONFIG_SERVICE);
+        if (configManager != null) {
+            final PersistableBundle b = configManager.getConfigForSubId(mSubId);
+            if (b != null) {
+                mReadOnlyApnTypes = b.getStringArray(
+                        CarrierConfigManager.KEY_READ_ONLY_APN_TYPES_STRING_ARRAY);
+                if (!ArrayUtils.isEmpty(mReadOnlyApnTypes)) {
+                    Log.d(TAG,
+                            "onCreate: read only APN type: " + Arrays.toString(mReadOnlyApnTypes));
+                }
+                mReadOnlyApnFields = b.getStringArray(
+                        CarrierConfigManager.KEY_READ_ONLY_APN_FIELDS_STRING_ARRAY);
+
+                mDefaultApnTypes = b.getStringArray(
+                        CarrierConfigManager.KEY_APN_SETTINGS_DEFAULT_APN_TYPES_STRING_ARRAY);
+
+                if (!ArrayUtils.isEmpty(mDefaultApnTypes)) {
+                    Log.d(TAG, "onCreate: default apn types: " + Arrays.toString(mDefaultApnTypes));
+                }
+
+                mDefaultApnProtocol = b.getString(
+                        CarrierConfigManager.Apn.KEY_SETTINGS_DEFAULT_PROTOCOL_STRING);
+                if (!TextUtils.isEmpty(mDefaultApnProtocol)) {
+                    Log.d(TAG, "onCreate: default apn protocol: " + mDefaultApnProtocol);
+                }
+
+                mDefaultApnRoamingProtocol = b.getString(
+                        CarrierConfigManager.Apn.KEY_SETTINGS_DEFAULT_ROAMING_PROTOCOL_STRING);
+                if (!TextUtils.isEmpty(mDefaultApnRoamingProtocol)) {
+                    Log.d(TAG, "onCreate: default apn roaming protocol: "
+                            + mDefaultApnRoamingProtocol);
+                }
+            }
+        }
+    }
+
     public static class ErrorDialog extends InstrumentedDialogFragment {
 
         public static void showError(ApnEditor editor) {
diff --git a/tests/robotests/src/com/android/settings/network/ApnEditorTest.java b/tests/robotests/src/com/android/settings/network/ApnEditorTest.java
index e291a43..20334e0 100644
--- a/tests/robotests/src/com/android/settings/network/ApnEditorTest.java
+++ b/tests/robotests/src/com/android/settings/network/ApnEditorTest.java
@@ -22,6 +22,7 @@
 import static org.mockito.ArgumentMatchers.anyInt;
 import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.verify;
@@ -99,6 +100,8 @@
 
     @Mock
     private FragmentActivity mActivity;
+    @Mock
+    private ProxySubscriptionManager mProxySubscriptionMgr;
 
     @Captor
     private ArgumentCaptor<Uri> mUriCaptor;
@@ -450,6 +453,8 @@
     @Test
     @Config(shadows = ShadowFragment.class)
     public void onCreate_noAction_shouldFinishAndNoCrash() {
+        ProxySubscriptionManager proxySubscriptionMgr = mock(ProxySubscriptionManager.class);
+        mApnEditorUT.mProxySubscriptionMgr = proxySubscriptionMgr;
         doReturn(new Intent()).when(mActivity).getIntent();
         doNothing().when(mApnEditorUT).addPreferencesFromResource(anyInt());