Merge "[Testing] Use AndroidJUnit4 instead in TelephonyBasePreferenceControllerTest"
diff --git a/src/com/android/settings/development/bluetooth/AbstractBluetoothDialogPreferenceController.java b/src/com/android/settings/development/bluetooth/AbstractBluetoothDialogPreferenceController.java
index 6bc9175..d66b8d8 100644
--- a/src/com/android/settings/development/bluetooth/AbstractBluetoothDialogPreferenceController.java
+++ b/src/com/android/settings/development/bluetooth/AbstractBluetoothDialogPreferenceController.java
@@ -16,6 +16,8 @@
package com.android.settings.development.bluetooth;
+import static android.bluetooth.BluetoothCodecConfig.CODEC_PRIORITY_HIGHEST;
+
import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothCodecConfig;
import android.bluetooth.BluetoothCodecStatus;
@@ -113,7 +115,7 @@
mBluetoothA2dpConfigStore.setSampleRate(config.getSampleRate());
mBluetoothA2dpConfigStore.setBitsPerSample(config.getBitsPerSample());
mBluetoothA2dpConfigStore.setChannelMode(config.getChannelMode());
- mBluetoothA2dpConfigStore.setCodecPriority(config.getCodecPriority());
+ mBluetoothA2dpConfigStore.setCodecPriority(CODEC_PRIORITY_HIGHEST);
mBluetoothA2dpConfigStore.setCodecSpecific1Value(config.getCodecSpecific1());
}
diff --git a/src/com/android/settings/network/telephony/Enhanced4gBasePreferenceController.java b/src/com/android/settings/network/telephony/Enhanced4gBasePreferenceController.java
index d6aed67..dc915af 100644
--- a/src/com/android/settings/network/telephony/Enhanced4gBasePreferenceController.java
+++ b/src/com/android/settings/network/telephony/Enhanced4gBasePreferenceController.java
@@ -225,6 +225,9 @@
if (SubscriptionManager.isValidSubscriptionId(subId)) {
mTelephonyManager = mTelephonyManager.createForSubscriptionId(subId);
}
+ // assign current call state so that it helps to show correct preference state even
+ // before first onCallStateChanged() by initial registration.
+ mCallState = mTelephonyManager.getCallState(subId);
mTelephonyManager.listen(this, PhoneStateListener.LISTEN_CALL_STATE);
final long supportedRadioBitmask = mTelephonyManager.getSupportedRadioAccessFamily();
diff --git a/src/com/android/settings/network/telephony/MobileNetworkActivity.java b/src/com/android/settings/network/telephony/MobileNetworkActivity.java
index a792824..6513384 100644
--- a/src/com/android/settings/network/telephony/MobileNetworkActivity.java
+++ b/src/com/android/settings/network/telephony/MobileNetworkActivity.java
@@ -81,7 +81,7 @@
mCurSubscriptionId = updateSubscriptionIndex;
mFragmentForceReload = (mCurSubscriptionId == oldSubId);
final SubscriptionInfo info = getSubscription();
- updateSubscriptions(info);
+ updateSubscriptions(info, null);
// If the subscription has changed or the new intent doesnt contain the opt in action,
// remove the old discovery dialog. If the activity is being recreated, we will see
@@ -132,7 +132,7 @@
// perform registration after mCurSubscriptionId been configured.
registerActiveSubscriptionsListener();
- updateSubscriptions(subscription);
+ updateSubscriptions(subscription, savedInstanceState);
}
@VisibleForTesting
@@ -154,7 +154,7 @@
public void onChanged() {
SubscriptionInfo info = getSubscription();
int oldSubIndex = mCurSubscriptionId;
- updateSubscriptions(info);
+ updateSubscriptions(info, null);
// Remove the dialog if the subscription associated with this activity changes.
if (info == null) {
@@ -209,14 +209,16 @@
}
@VisibleForTesting
- void updateSubscriptions(SubscriptionInfo subscription) {
+ void updateSubscriptions(SubscriptionInfo subscription, Bundle savedInstanceState) {
if (subscription == null) {
return;
}
final int subscriptionIndex = subscription.getSubscriptionId();
updateTitleAndNavigation(subscription);
- switchFragment(subscription);
+ if (savedInstanceState == null) {
+ switchFragment(subscription);
+ }
mCurSubscriptionId = subscriptionIndex;
mFragmentForceReload = false;
diff --git a/src/com/android/settings/network/telephony/PreferredNetworkModePreferenceController.java b/src/com/android/settings/network/telephony/PreferredNetworkModePreferenceController.java
index 83fa208..ff6ff59 100644
--- a/src/com/android/settings/network/telephony/PreferredNetworkModePreferenceController.java
+++ b/src/com/android/settings/network/telephony/PreferredNetworkModePreferenceController.java
@@ -153,7 +153,7 @@
if (mTelephonyManager.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA
|| mIsGlobalCdma
|| MobileNetworkUtils.isWorldMode(mContext, mSubId)) {
- return R.string.preferred_network_mode_global_summary;
+ return R.string.preferred_network_mode_lte_cdma_evdo_gsm_wcdma_summary;
} else {
return R.string.preferred_network_mode_lte_summary;
}
@@ -174,7 +174,7 @@
case TelephonyManagerConstants.NETWORK_MODE_NR_LTE_GSM_WCDMA:
return R.string.preferred_network_mode_nr_lte_gsm_wcdma_summary;
case TelephonyManagerConstants.NETWORK_MODE_NR_LTE_CDMA_EVDO_GSM_WCDMA:
- return R.string.preferred_network_mode_nr_lte_cdma_evdo_gsm_wcdma_summary;
+ return R.string.preferred_network_mode_global_summary;
case TelephonyManagerConstants.NETWORK_MODE_NR_LTE_WCDMA:
return R.string.preferred_network_mode_nr_lte_wcdma_summary;
case TelephonyManagerConstants.NETWORK_MODE_NR_LTE_TDSCDMA:
diff --git a/src/com/android/settings/network/telephony/VideoCallingPreferenceController.java b/src/com/android/settings/network/telephony/VideoCallingPreferenceController.java
index 3321fb3..fa8b47f 100644
--- a/src/com/android/settings/network/telephony/VideoCallingPreferenceController.java
+++ b/src/com/android/settings/network/telephony/VideoCallingPreferenceController.java
@@ -182,6 +182,9 @@
if (SubscriptionManager.isValidSubscriptionId(subId)) {
mTelephonyManager = mTelephonyManager.createForSubscriptionId(subId);
}
+ // assign current call state so that it helps to show correct preference state even
+ // before first onCallStateChanged() by initial registration.
+ mCallState = mTelephonyManager.getCallState(subId);
mTelephonyManager.listen(this, PhoneStateListener.LISTEN_CALL_STATE);
}
diff --git a/src/com/android/settings/network/telephony/WifiCallingPreferenceController.java b/src/com/android/settings/network/telephony/WifiCallingPreferenceController.java
index 32be36e..b38fe07 100644
--- a/src/com/android/settings/network/telephony/WifiCallingPreferenceController.java
+++ b/src/com/android/settings/network/telephony/WifiCallingPreferenceController.java
@@ -210,6 +210,9 @@
public void register(Context context, int subId) {
mTelephonyManager = getTelephonyManager(context, subId);
+ // assign current call state so that it helps to show correct preference state even
+ // before first onCallStateChanged() by initial registration.
+ mCallState = mTelephonyManager.getCallState(subId);
mTelephonyManager.listen(this, PhoneStateListener.LISTEN_CALL_STATE);
}
diff --git a/src/com/android/settings/wifi/OWNERS b/src/com/android/settings/wifi/OWNERS
index ab0af7c..5e20fd5 100644
--- a/src/com/android/settings/wifi/OWNERS
+++ b/src/com/android/settings/wifi/OWNERS
@@ -1,4 +1,5 @@
# Default reviewers for this and subdirectories.
-govenliu@google.com
+andychou@google.com
arcwang@google.com
-tmfang@google.com
+goldmanj@google.com
+wengsu@google.com
diff --git a/tests/robotests/src/com/android/settings/development/bluetooth/AbstractBluetoothDialogPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/development/bluetooth/AbstractBluetoothDialogPreferenceControllerTest.java
index afb1778..a12131d 100644
--- a/tests/robotests/src/com/android/settings/development/bluetooth/AbstractBluetoothDialogPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/development/bluetooth/AbstractBluetoothDialogPreferenceControllerTest.java
@@ -16,6 +16,8 @@
package com.android.settings.development.bluetooth;
+import static android.bluetooth.BluetoothCodecConfig.CODEC_PRIORITY_HIGHEST;
+
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.never;
@@ -199,7 +201,7 @@
verify(mBluetoothA2dpConfigStore).setSampleRate(mCodecConfigAAC.getSampleRate());
verify(mBluetoothA2dpConfigStore).setBitsPerSample(mCodecConfigAAC.getBitsPerSample());
verify(mBluetoothA2dpConfigStore).setChannelMode(mCodecConfigAAC.getChannelMode());
- verify(mBluetoothA2dpConfigStore).setCodecPriority(mCodecConfigAAC.getCodecPriority());
+ verify(mBluetoothA2dpConfigStore).setCodecPriority(CODEC_PRIORITY_HIGHEST);
verify(mBluetoothA2dpConfigStore).setCodecSpecific1Value(
mCodecConfigAAC.getCodecSpecific1());
}
diff --git a/tests/unit/Android.bp b/tests/unit/Android.bp
index 583b46e..7556cbe 100644
--- a/tests/unit/Android.bp
+++ b/tests/unit/Android.bp
@@ -17,6 +17,7 @@
"androidx.test.espresso.core",
"androidx.test.espresso.contrib-nodeps",
"androidx.test.espresso.intents-nodeps",
+ "androidx.test.ext.junit",
"mockito-target-minus-junit4",
"platform-test-annotations",
"truth-prebuilt",
diff --git a/tests/unit/src/com/android/settings/network/MobileDataEnabledListenerTest.java b/tests/unit/src/com/android/settings/network/MobileDataEnabledListenerTest.java
new file mode 100644
index 0000000..190dc3d
--- /dev/null
+++ b/tests/unit/src/com/android/settings/network/MobileDataEnabledListenerTest.java
@@ -0,0 +1,85 @@
+/*
+ * 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;
+
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+
+import android.content.Context;
+import android.net.Uri;
+import android.provider.Settings;
+
+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 MobileDataEnabledListenerTest {
+ private static final int SUB_ID_ONE = 111;
+ private static final int SUB_ID_TWO = 222;
+
+ @Mock
+ private MobileDataEnabledListener.Client mClient;
+
+ private Context mContext;
+ private MobileDataEnabledListener mListener;
+
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+ mContext = ApplicationProvider.getApplicationContext();
+ mListener = new MobileDataEnabledListener(mContext, mClient);
+ }
+
+ @Test
+ public void onMobileDataEnabledChange_firesCorrectly() {
+ mListener.start(SUB_ID_ONE);
+ final Uri uri = Settings.Global.getUriFor(Settings.Global.MOBILE_DATA + SUB_ID_ONE);
+
+ mContext.getContentResolver().notifyChange(uri, null);
+
+ verify(mClient).onMobileDataEnabledChange();
+ }
+
+ @Test
+ public void onMobileDataEnabledChange_doesNotFireAfterStop() {
+ mListener.start(SUB_ID_ONE);
+ mListener.stop();
+ final Uri uri = Settings.Global.getUriFor(Settings.Global.MOBILE_DATA + SUB_ID_ONE);
+
+ mContext.getContentResolver().notifyChange(uri, null);
+
+ verify(mClient, never()).onMobileDataEnabledChange();
+ }
+
+ @Test
+ public void onMobileDataEnabledChange_changedToDifferentId_firesCorrectly() {
+ mListener.start(SUB_ID_ONE);
+ mListener.stop();
+ mListener.start(SUB_ID_TWO);
+ final Uri uri = Settings.Global.getUriFor(Settings.Global.MOBILE_DATA + SUB_ID_TWO);
+
+ mContext.getContentResolver().notifyChange(uri, null);
+
+ verify(mClient).onMobileDataEnabledChange();
+ }
+}
diff --git a/tests/unit/src/com/android/settings/network/MultiNetworkHeaderControllerTest.java b/tests/unit/src/com/android/settings/network/MultiNetworkHeaderControllerTest.java
new file mode 100644
index 0000000..cdb82a6
--- /dev/null
+++ b/tests/unit/src/com/android/settings/network/MultiNetworkHeaderControllerTest.java
@@ -0,0 +1,174 @@
+/*
+ * 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;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.when;
+
+import android.content.Context;
+import android.os.Looper;
+import android.telephony.SubscriptionManager;
+
+import androidx.preference.PreferenceCategory;
+import androidx.preference.PreferenceManager;
+import androidx.preference.PreferenceScreen;
+import androidx.test.core.app.ApplicationProvider;
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+
+import com.android.settings.wifi.WifiConnectionPreferenceController;
+import com.android.settingslib.core.lifecycle.Lifecycle;
+
+import org.junit.Assert;
+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 MultiNetworkHeaderControllerTest {
+ private static final String KEY_HEADER = "multi_network_header";
+ private static final int EXPANDED_CHILDREN_COUNT = 5;
+
+ @Mock
+ private PreferenceCategory mPreferenceCategory;
+ @Mock
+ private WifiConnectionPreferenceController mWifiController;
+ @Mock
+ private SubscriptionsPreferenceController mSubscriptionsController;
+ @Mock
+ private SubscriptionManager mSubscriptionManager;
+ @Mock
+ private Lifecycle mLifecycle;
+
+ private Context mContext;
+ private PreferenceManager mPreferenceManager;
+ private PreferenceScreen mPreferenceScreen;
+ private MultiNetworkHeaderController mHeaderController;
+
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+ mContext = spy(ApplicationProvider.getApplicationContext());
+ when(mContext.getSystemService(SubscriptionManager.class)).thenReturn(mSubscriptionManager);
+
+ mHeaderController = new MultiNetworkHeaderController(mContext, KEY_HEADER) {
+ @Override
+ WifiConnectionPreferenceController createWifiController(Lifecycle lifecycle) {
+ return mWifiController;
+ }
+
+ @Override
+ SubscriptionsPreferenceController createSubscriptionsController(Lifecycle lifecycle) {
+ return mSubscriptionsController;
+ }
+ };
+
+ if (Looper.myLooper() == null) {
+ Looper.prepare();
+ }
+ mPreferenceManager = new PreferenceManager(mContext);
+ mPreferenceScreen = mPreferenceManager.createPreferenceScreen(mContext);
+ mPreferenceScreen.setInitialExpandedChildrenCount(EXPANDED_CHILDREN_COUNT);
+ when(mPreferenceCategory.getKey()).thenReturn(KEY_HEADER);
+ when(mPreferenceCategory.getPreferenceCount()).thenReturn(3);
+ mPreferenceScreen.addPreference(mPreferenceCategory);
+ }
+
+ @Test
+ public void isAvailable_beforeInitIsCalled_notAvailable() {
+ assertThat(mHeaderController.isAvailable()).isFalse();
+ }
+
+ // When calling displayPreference, the header itself should only be visible if the
+ // subscriptions controller says it is available. This is a helper for test cases of this logic.
+ private void displayPreferenceTest(boolean wifiAvailable, boolean subscriptionsAvailable,
+ boolean setVisibleExpectedValue) {
+ when(mWifiController.isAvailable()).thenReturn(wifiAvailable);
+ when(mSubscriptionsController.isAvailable()).thenReturn(subscriptionsAvailable);
+
+ mHeaderController.init(mLifecycle);
+ mHeaderController.displayPreference(mPreferenceScreen);
+ Assert.assertEquals(mPreferenceCategory.isVisible(), setVisibleExpectedValue);
+ }
+
+ @Test
+ public void displayPreference_bothNotAvailable_categoryIsNotVisible() {
+ displayPreferenceTest(false, false, false);
+ }
+
+ @Test
+ public void displayPreference_wifiAvailableButNotSubscriptions_categoryIsNotVisible() {
+ displayPreferenceTest(true, false, false);
+ }
+
+ @Test
+ public void displayPreference_subscriptionsAvailableButNotWifi_categoryIsVisible() {
+ displayPreferenceTest(false, true, true);
+ }
+
+ @Test
+ public void displayPreference_bothAvailable_categoryIsVisible() {
+ displayPreferenceTest(true, true, true);
+ }
+
+ @Test
+ public void onChildUpdated_subscriptionsBecameAvailable_categoryIsVisible() {
+ when(mSubscriptionsController.isAvailable()).thenReturn(false);
+ mHeaderController.init(mLifecycle);
+ mHeaderController.displayPreference(mPreferenceScreen);
+
+ when(mSubscriptionsController.isAvailable()).thenReturn(true);
+ mHeaderController.onChildrenUpdated();
+
+ Assert.assertTrue(mPreferenceCategory.isVisible());
+ assertThat(mPreferenceScreen.getInitialExpandedChildrenCount()).isEqualTo(
+ EXPANDED_CHILDREN_COUNT + mPreferenceCategory.getPreferenceCount());
+ }
+
+ @Test
+ public void onChildUpdated_subscriptionsBecameUnavailable_categoryIsNotVisible() {
+ when(mSubscriptionsController.isAvailable()).thenReturn(true);
+ mHeaderController.init(mLifecycle);
+ mHeaderController.displayPreference(mPreferenceScreen);
+
+ when(mSubscriptionsController.isAvailable()).thenReturn(false);
+ mHeaderController.onChildrenUpdated();
+
+ Assert.assertFalse(mPreferenceCategory.isVisible());
+ assertThat(mPreferenceScreen.getInitialExpandedChildrenCount()).isEqualTo(
+ EXPANDED_CHILDREN_COUNT);
+ }
+
+ @Test
+ public void onChildUpdated_noExpandedChildCountAndAvailable_doesNotSetExpandedCount() {
+ mPreferenceScreen.setInitialExpandedChildrenCount(Integer.MAX_VALUE);
+
+ when(mSubscriptionsController.isAvailable()).thenReturn(false);
+ mHeaderController.init(mLifecycle);
+ mHeaderController.displayPreference(mPreferenceScreen);
+
+ when(mSubscriptionsController.isAvailable()).thenReturn(true);
+ mHeaderController.onChildrenUpdated();
+
+ // Check that setInitialExpandedChildrenCount was never called.
+ Assert.assertEquals(mPreferenceScreen.getInitialExpandedChildrenCount(), Integer.MAX_VALUE);
+ }
+}
diff --git a/tests/unit/src/com/android/settings/network/telephony/EuiccPreferenceControllerTest.java b/tests/unit/src/com/android/settings/network/telephony/EuiccPreferenceControllerTest.java
new file mode 100644
index 0000000..f1ad2ff
--- /dev/null
+++ b/tests/unit/src/com/android/settings/network/telephony/EuiccPreferenceControllerTest.java
@@ -0,0 +1,76 @@
+/*
+ * 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;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.when;
+
+import android.content.Context;
+import android.content.Intent;
+import android.telephony.TelephonyManager;
+import android.telephony.euicc.EuiccManager;
+
+import androidx.preference.Preference;
+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.ArgumentCaptor;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+
+@RunWith(AndroidJUnit4.class)
+public class EuiccPreferenceControllerTest {
+ private static final int SUB_ID = 2;
+
+ @Mock private TelephonyManager mTelephonyManager;
+
+ private EuiccPreferenceController mController;
+ private Preference 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 = new Preference(mContext);
+ mController = new EuiccPreferenceController(mContext, "euicc");
+ mController.init(SUB_ID);
+ mPreference.setKey(mController.getPreferenceKey());
+ }
+
+ @Test
+ public void handlePreferenceTreeClick_startActivity() {
+ ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
+ doNothing().when(mContext).startActivity(captor.capture());
+
+ mController.handlePreferenceTreeClick(mPreference);
+
+ assertThat(captor.getValue().getAction()).isEqualTo(
+ EuiccManager.ACTION_MANAGE_EMBEDDED_SUBSCRIPTIONS);
+ }
+}
diff --git a/tests/unit/src/com/android/settings/network/telephony/MobileDataPreferenceControllerTest.java b/tests/unit/src/com/android/settings/network/telephony/MobileDataPreferenceControllerTest.java
new file mode 100644
index 0000000..0feabf2
--- /dev/null
+++ b/tests/unit/src/com/android/settings/network/telephony/MobileDataPreferenceControllerTest.java
@@ -0,0 +1,192 @@
+/*
+ * 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;
+
+import static com.android.settings.core.BasePreferenceController.AVAILABLE_UNSEARCHABLE;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Matchers.anyInt;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.app.Instrumentation;
+import android.content.Context;
+import android.content.res.Resources;
+import android.telephony.SubscriptionInfo;
+import android.telephony.SubscriptionManager;
+import android.telephony.TelephonyManager;
+
+import androidx.fragment.app.FragmentManager;
+import androidx.fragment.app.FragmentTransaction;
+import androidx.preference.SwitchPreference;
+import androidx.test.core.app.ApplicationProvider;
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+import androidx.test.platform.app.InstrumentationRegistry;
+
+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 MobileDataPreferenceControllerTest {
+ private static final int SUB_ID = 2;
+ private static final int SUB_ID_OTHER = 3;
+
+ @Mock
+ private FragmentManager mFragmentManager;
+ @Mock
+ private TelephonyManager mTelephonyManager;
+ @Mock
+ private TelephonyManager mInvalidTelephonyManager;
+ @Mock
+ private SubscriptionManager mSubscriptionManager;
+ @Mock
+ private SubscriptionInfo mSubscriptionInfo;
+ @Mock
+ private FragmentTransaction mFragmentTransaction;
+
+ private MobileDataPreferenceController mController;
+ private SwitchPreference mPreference;
+ private Context mContext;
+
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+
+ mContext = spy(ApplicationProvider.getApplicationContext());
+ doReturn(mTelephonyManager).when(mContext).getSystemService(Context.TELEPHONY_SERVICE);
+
+ when(mContext.getSystemService(SubscriptionManager.class)).thenReturn(mSubscriptionManager);
+ doReturn(mTelephonyManager).when(mTelephonyManager).createForSubscriptionId(SUB_ID);
+ doReturn(mInvalidTelephonyManager).when(mTelephonyManager).createForSubscriptionId(
+ SubscriptionManager.INVALID_SUBSCRIPTION_ID);
+ doReturn(mFragmentTransaction).when(mFragmentManager).beginTransaction();
+
+ mPreference = new SwitchPreference(mContext);
+ mController = new MobileDataPreferenceController(mContext, "mobile_data");
+ mController.init(mFragmentManager, SUB_ID);
+ mPreference.setKey(mController.getPreferenceKey());
+ }
+
+ @Test
+ public void getAvailabilityStatus_invalidSubscription_returnAvailableUnsearchable() {
+ mController.init(mFragmentManager, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
+
+ assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE_UNSEARCHABLE);
+ }
+
+ @Test
+ public void isDialogNeeded_disableSingleSim_returnFalse() {
+ doReturn(true).when(mTelephonyManager).isDataEnabled();
+ doReturn(mSubscriptionInfo).when(mSubscriptionManager).getActiveSubscriptionInfo(SUB_ID);
+ doReturn(1).when(mTelephonyManager).getActiveModemCount();
+
+ assertThat(mController.isDialogNeeded()).isFalse();
+ }
+
+ @Test
+ public void isDialogNeeded_enableNonDefaultSimInMultiSimMode_returnTrue() {
+ doReturn(false).when(mTelephonyManager).isDataEnabled();
+ doReturn(mSubscriptionInfo).when(mSubscriptionManager)
+ .getActiveSubscriptionInfo(SUB_ID);
+ // Ideally, it would be better if we could set the default data subscription to
+ // SUB_ID_OTHER, and set that as an active subscription id.
+ when(mSubscriptionManager.isActiveSubscriptionId(anyInt())).thenReturn(true);
+ doReturn(2).when(mTelephonyManager).getActiveModemCount();
+
+ assertThat(mController.isDialogNeeded()).isTrue();
+ assertThat(mController.mDialogType).isEqualTo(
+ MobileDataDialogFragment.TYPE_MULTI_SIM_DIALOG);
+ }
+
+ @Test
+ public void handlePreferenceTreeClick_needDialog_showDialog() {
+ mController.mNeedDialog = true;
+ final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
+
+ instrumentation.runOnMainSync(() -> {
+ mController.handlePreferenceTreeClick(mPreference);
+ });
+ verify(mFragmentManager).beginTransaction();
+ }
+
+ @Test
+ public void onPreferenceChange_singleSim_On_shouldEnableData() {
+ doReturn(true).when(mTelephonyManager).isDataEnabled();
+ doReturn(mSubscriptionInfo).when(mSubscriptionManager).getActiveSubscriptionInfo(SUB_ID);
+ doReturn(1).when(mTelephonyManager).getActiveModemCount();
+
+ mController.onPreferenceChange(mPreference, true);
+
+ verify(mTelephonyManager).setDataEnabled(true);
+ }
+
+ @Test
+ public void onPreferenceChange_multiSim_On_shouldEnableData() {
+ doReturn(true).when(mTelephonyManager).isDataEnabled();
+ doReturn(mSubscriptionInfo).when(mSubscriptionManager).getActiveSubscriptionInfo(SUB_ID);
+ doReturn(2).when(mTelephonyManager).getActiveModemCount();
+
+ mController.onPreferenceChange(mPreference, true);
+
+ verify(mTelephonyManager).setDataEnabled(true);
+ }
+
+ @Test
+ public void isChecked_returnUserDataEnabled() {
+ mController.init(mFragmentManager, SUB_ID);
+ assertThat(mController.isChecked()).isFalse();
+
+ doReturn(true).when(mTelephonyManager).isDataEnabled();
+ assertThat(mController.isChecked()).isTrue();
+ }
+
+ @Test
+ public void updateState_opportunistic_disabled() {
+ doReturn(mSubscriptionInfo).when(mSubscriptionManager).getActiveSubscriptionInfo(SUB_ID);
+ mController.init(mFragmentManager, SUB_ID);
+ doReturn(true).when(mSubscriptionInfo).isOpportunistic();
+ mController.updateState(mPreference);
+
+ assertThat(mPreference.isEnabled()).isFalse();
+ assertThat(mPreference.getSummary())
+ .isEqualTo(resourceString("mobile_data_settings_summary_auto_switch"));
+ }
+
+ @Test
+ public void updateState_notOpportunistic_enabled() {
+ doReturn(mSubscriptionInfo).when(mSubscriptionManager).getActiveSubscriptionInfo(SUB_ID);
+ mController.init(mFragmentManager, SUB_ID);
+ doReturn(false).when(mSubscriptionInfo).isOpportunistic();
+ mController.updateState(mPreference);
+
+ assertThat(mPreference.isEnabled()).isTrue();
+ assertThat(mPreference.getSummary())
+ .isEqualTo(resourceString("mobile_data_settings_summary"));
+ }
+
+ public String resourceString(String name) {
+ final Resources res = mContext.getResources();
+ return res.getString(res.getIdentifier(name, "string", mContext.getPackageName()));
+ }
+}
diff --git a/tests/unit/src/com/android/settings/network/telephony/TelephonyTogglePreferenceControllerTest.java b/tests/unit/src/com/android/settings/network/telephony/TelephonyTogglePreferenceControllerTest.java
new file mode 100644
index 0000000..e80a76e
--- /dev/null
+++ b/tests/unit/src/com/android/settings/network/telephony/TelephonyTogglePreferenceControllerTest.java
@@ -0,0 +1,68 @@
+/*
+ * 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;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.content.Context;
+
+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;
+
+@RunWith(AndroidJUnit4.class)
+public class TelephonyTogglePreferenceControllerTest {
+
+ private Context mContext;
+ private FakeTelephonyToggle mFakeTelephonyToggle;
+
+ @Before
+ public void setUp() {
+ mContext = ApplicationProvider.getApplicationContext();
+ mFakeTelephonyToggle = new FakeTelephonyToggle(mContext, "key");
+ }
+
+ @Test
+ public void isSliceable_byDefault_shouldReturnFalse() {
+ assertThat(mFakeTelephonyToggle.isSliceable()).isFalse();
+ }
+
+ private static class FakeTelephonyToggle extends TelephonyTogglePreferenceController {
+
+ private FakeTelephonyToggle(Context context, String preferenceKey) {
+ super(context, preferenceKey);
+ }
+
+ @Override
+ public boolean isChecked() {
+ return false;
+ }
+
+ @Override
+ public boolean setChecked(boolean isChecked) {
+ return false;
+ }
+
+ @Override
+ public int getAvailabilityStatus(int subId) {
+ return 0;
+ }
+ }
+}