[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
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 0000000..027a53b
--- /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 0000000..86488c6
--- /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 0000000..4f44a29
--- /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);
+    }
+}