Merge "Remove Settings robolectric tests which also have a junit test."

This commit is contained in:
Jeremy Goldman
2020-12-15 07:41:20 +00:00
committed by Android (Google) Code Review
32 changed files with 0 additions and 4175 deletions

View File

@@ -1,82 +0,0 @@
/*
* Copyright (C) 2017 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.datausage;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.Drawable;
import android.util.ArraySet;
import androidx.preference.Preference;
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;
@RunWith(RobolectricTestRunner.class)
public class AppPrefLoaderTest {
@Mock
private PackageManager mPackageManager;
private AppPrefLoader mLoader;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
final ArraySet<String> pkgs = new ArraySet<>(2);
pkgs.add("pkg0");
pkgs.add("pkg1");
mLoader = new AppPrefLoader(RuntimeEnvironment.application, pkgs, mPackageManager);
}
@Test
public void loadInBackground_packageNotFound_shouldReturnEmptySet()
throws NameNotFoundException {
when(mPackageManager.getApplicationInfo(anyString(), anyInt()))
.thenThrow(new NameNotFoundException());
assertThat(mLoader.loadInBackground()).isEmpty();
}
@Test
public void loadInBackground_shouldReturnPreference() throws NameNotFoundException {
ApplicationInfo info = mock(ApplicationInfo.class);
when(mPackageManager.getApplicationInfo(anyString(), anyInt())).thenReturn(info);
final Drawable drawable = mock(Drawable.class);
final String label = "Label1";
when(info.loadIcon(mPackageManager)).thenReturn(drawable);
when(info.loadLabel(mPackageManager)).thenReturn(label);
Preference preference = mLoader.loadInBackground().valueAt(0);
assertThat(preference.getTitle()).isEqualTo(label);
assertThat(preference.getIcon()).isEqualTo(drawable);
assertThat(preference.isSelectable()).isFalse();
}
}

View File

@@ -1,97 +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.datausage;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import android.content.Context;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import androidx.preference.PreferenceViewHolder;
import com.android.settings.network.ProxySubscriptionManager;
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;
@RunWith(RobolectricTestRunner.class)
public class CellDataPreferenceTest {
@Mock
private ProxySubscriptionManager mProxySubscriptionMgr;
@Mock
private SubscriptionManager mSubscriptionManager;
@Mock
private SubscriptionInfo mSubInfo;
private Context mContext;
private PreferenceViewHolder mHolder;
private CellDataPreference mPreference;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mPreference = new CellDataPreference(mContext, null) {
@Override
ProxySubscriptionManager getProxySubscriptionManager() {
return mProxySubscriptionMgr;
}
@Override
SubscriptionInfo getActiveSubscriptionInfo(int subId) {
return mSubInfo;
}
};
doNothing().when(mSubscriptionManager).setDefaultDataSubId(anyInt());
doReturn(mSubscriptionManager).when(mProxySubscriptionMgr).get();
doNothing().when(mProxySubscriptionMgr).addActiveSubscriptionsListener(any());
doNothing().when(mProxySubscriptionMgr).removeActiveSubscriptionsListener(any());
final LayoutInflater inflater = LayoutInflater.from(mContext);
final View view = inflater.inflate(mPreference.getLayoutResource(),
new LinearLayout(mContext), false);
mHolder = PreferenceViewHolder.createInstanceForTests(view);
}
@Test
public void noActiveSub_shouldDisable() {
mSubInfo = null;
mPreference.mOnSubscriptionsChangeListener.onChanged();
assertThat(mPreference.isEnabled()).isFalse();
}
@Test
public void hasActiveSub_shouldEnable() {
mPreference.mOnSubscriptionsChangeListener.onChanged();
assertThat(mPreference.isEnabled()).isTrue();
}
}

View File

@@ -1,144 +0,0 @@
package com.android.settings.datausage;
import static com.google.common.truth.Truth.assertThat;
import android.net.NetworkPolicy;
import android.net.NetworkTemplate;
import com.android.settingslib.net.DataUsageController.DataUsageInfo;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
public class DataUsageInfoControllerTest {
private static final int NEGATIVE = -1;
private static final int ZERO = 0;
private static final int POSITIVE_SMALL = 1;
private static final int POSITIVE_LARGE = 5;
private DataUsageInfoController mInfoController;
private DataUsageInfo info;
@Before
public void setUp() {
mInfoController = new DataUsageInfoController();
info = new DataUsageInfo();
}
@Test
public void testLowUsageLowWarning_LimitUsed() {
info.warningLevel = POSITIVE_SMALL;
info.limitLevel = POSITIVE_LARGE;
info.usageLevel = POSITIVE_SMALL;
assertThat(mInfoController.getSummaryLimit(info)).isEqualTo(info.limitLevel);
}
@Test
public void testLowUsageEqualWarning_LimitUsed() {
info.warningLevel = POSITIVE_LARGE;
info.limitLevel = POSITIVE_LARGE;
info.usageLevel = POSITIVE_SMALL;
assertThat(mInfoController.getSummaryLimit(info)).isEqualTo(info.limitLevel);
}
@Test
public void testNoLimitNoUsage_WarningUsed() {
info.warningLevel = POSITIVE_LARGE;
info.limitLevel = ZERO;
info.usageLevel = ZERO;
assertThat(mInfoController.getSummaryLimit(info)).isEqualTo(info.warningLevel);
}
@Test
public void testNoLimitLowUsage_WarningUsed() {
info.warningLevel = POSITIVE_LARGE;
info.limitLevel = ZERO;
info.usageLevel = POSITIVE_SMALL;
assertThat(mInfoController.getSummaryLimit(info)).isEqualTo(info.warningLevel);
}
@Test
public void testLowWarningNoLimit_UsageUsed() {
info.warningLevel = POSITIVE_SMALL;
info.limitLevel = ZERO;
info.usageLevel = POSITIVE_LARGE;
assertThat(mInfoController.getSummaryLimit(info)).isEqualTo(info.usageLevel);
}
@Test
public void testLowWarningLowLimit_UsageUsed() {
info.warningLevel = POSITIVE_SMALL;
info.limitLevel = POSITIVE_SMALL;
info.usageLevel = POSITIVE_LARGE;
assertThat(mInfoController.getSummaryLimit(info)).isEqualTo(info.usageLevel);
}
private NetworkPolicy getDefaultNetworkPolicy() {
NetworkTemplate template =
new NetworkTemplate(NetworkTemplate.MATCH_WIFI_WILDCARD, null, null);
int cycleDay = -1;
String cycleTimezone = "UTC";
long warningBytes = -1;
long limitBytes = -1;
return new NetworkPolicy(template, cycleDay, cycleTimezone, warningBytes, limitBytes, true);
}
@Test
public void testNullArguments_NoError() {
mInfoController.updateDataLimit(null, null);
mInfoController.updateDataLimit(info, null);
mInfoController.updateDataLimit(null, getDefaultNetworkPolicy());
}
@Test
public void testNegativeWarning_UpdatedToZero() {
NetworkPolicy policy = getDefaultNetworkPolicy();
policy.warningBytes = NEGATIVE;
mInfoController.updateDataLimit(info, policy);
assertThat(info.warningLevel).isEqualTo(ZERO);
}
@Test
public void testWarningZero_UpdatedToZero() {
NetworkPolicy policy = getDefaultNetworkPolicy();
policy.warningBytes = ZERO;
mInfoController.updateDataLimit(info, policy);
assertThat(info.warningLevel).isEqualTo(ZERO);
}
@Test
public void testWarningPositive_UpdatedToWarning() {
NetworkPolicy policy = getDefaultNetworkPolicy();
policy.warningBytes = POSITIVE_SMALL;
mInfoController.updateDataLimit(info, policy);
assertThat(info.warningLevel).isEqualTo(policy.warningBytes);
}
@Test
public void testLimitNegative_UpdatedToZero() {
NetworkPolicy policy = getDefaultNetworkPolicy();
policy.limitBytes = NEGATIVE;
mInfoController.updateDataLimit(info, policy);
assertThat(info.limitLevel).isEqualTo(ZERO);
}
@Test
public void testLimitZero_UpdatedToZero() {
NetworkPolicy policy = getDefaultNetworkPolicy();
policy.limitBytes = ZERO;
mInfoController.updateDataLimit(info, policy);
assertThat(info.limitLevel).isEqualTo(ZERO);
}
@Test
public void testLimitPositive_UpdatedToLimit() {
NetworkPolicy policy = getDefaultNetworkPolicy();
policy.limitBytes = POSITIVE_SMALL;
mInfoController.updateDataLimit(info, policy);
assertThat(info.limitLevel).isEqualTo(policy.limitBytes);
}
}

View File

@@ -1,86 +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.deviceinfo;
import static com.google.common.truth.Truth.assertThat;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.storage.VolumeRecord;
import android.widget.Button;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.FragmentActivity;
import com.android.settings.R;
import com.android.settings.deviceinfo.PrivateVolumeForget.ForgetConfirmFragment;
import com.android.settings.testutils.shadow.ShadowStorageManager;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.androidx.fragment.FragmentController;
@RunWith(RobolectricTestRunner.class)
@Config(shadows = ShadowStorageManager.class)
public class PrivateVolumeForgetTest {
private PrivateVolumeForget mFragment;
private FragmentActivity mActivity;
@Before
public void setUp() {
final Bundle bundle = new Bundle();
bundle.putString(VolumeRecord.EXTRA_FS_UUID, "id");
mFragment = FragmentController.of(new PrivateVolumeForget(), bundle)
.create()
.start()
.resume()
.visible()
.get();
mActivity = mFragment.getActivity();
}
@After
public void tearDown() {
ShadowStorageManager.reset();
}
@Test
public void OnClickListener_shouldCallForget() {
assertThat(ShadowStorageManager.isForgetCalled()).isFalse();
final Button confirm = mFragment.getView().findViewById(R.id.confirm);
confirm.performClick();
final ForgetConfirmFragment confirmFragment =
(ForgetConfirmFragment) mActivity.getSupportFragmentManager().findFragmentByTag(
PrivateVolumeForget.TAG_FORGET_CONFIRM);
assertThat(confirmFragment).isNotNull();
final AlertDialog dialog = (AlertDialog) confirmFragment.getDialog();
final Button forget = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
forget.performClick();
assertThat(ShadowStorageManager.isForgetCalled()).isTrue();
}
}

View File

@@ -1,182 +0,0 @@
/*
* Copyright (C) 2017 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.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageManager;
import android.provider.Settings;
import android.provider.SettingsSlicesContract;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;
import com.android.settings.AirplaneModeEnabler;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settingslib.RestrictedSwitchPreference;
import com.android.settingslib.core.lifecycle.Lifecycle;
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 org.robolectric.annotation.Config;
@RunWith(RobolectricTestRunner.class)
public class AirplaneModePreferenceControllerTest {
private static final int ON = 1;
private static final int OFF = 0;
@Mock
private PackageManager mPackageManager;
@Mock
private AirplaneModeEnabler mAirplaneModeEnabler;
private Context mContext;
private ContentResolver mResolver;
private PreferenceManager mPreferenceManager;
private PreferenceScreen mScreen;
private RestrictedSwitchPreference mPreference;
private AirplaneModePreferenceController mController;
private LifecycleOwner mLifecycleOwner;
private Lifecycle mLifecycle;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
FakeFeatureFactory.setupForTest();
mContext = spy(RuntimeEnvironment.application);
mResolver = RuntimeEnvironment.application.getContentResolver();
doReturn(mPackageManager).when(mContext).getPackageManager();
mController = new AirplaneModePreferenceController(mContext,
SettingsSlicesContract.KEY_AIRPLANE_MODE);
mPreferenceManager = new PreferenceManager(mContext);
mScreen = mPreferenceManager.createPreferenceScreen(mContext);
mPreference = new RestrictedSwitchPreference(mContext);
mPreference.setKey(SettingsSlicesContract.KEY_AIRPLANE_MODE);
mScreen.addPreference(mPreference);
mController.setFragment(null);
mLifecycleOwner = () -> mLifecycle;
mLifecycle = new Lifecycle(mLifecycleOwner);
mLifecycle.addObserver(mController);
}
@Test
public void getSliceUri_shouldUsePlatformAuthority() {
assertThat(mController.getSliceUri().getAuthority())
.isEqualTo(SettingsSlicesContract.AUTHORITY);
}
@Test
@Config(qualifiers = "mcc999")
public void airplaneModePreference_shouldNotBeAvailable_ifSetToNotVisible() {
assertThat(mController.getAvailabilityStatus())
.isNotEqualTo(BasePreferenceController.AVAILABLE);
mController.displayPreference(mScreen);
// This should not crash
mController.onStart();
mController.onStop();
}
@Test
public void airplaneModePreference_shouldNotBeAvailable_ifHasLeanbackFeature() {
when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_LEANBACK)).thenReturn(true);
assertThat(mController.getAvailabilityStatus())
.isNotEqualTo(BasePreferenceController.AVAILABLE);
mController.displayPreference(mScreen);
// This should not crash
mController.onStart();
mController.onStop();
}
@Test
public void airplaneModePreference_shouldBeAvailable_ifNoLeanbackFeature() {
when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_LEANBACK)).thenReturn(false);
assertThat(mController.getAvailabilityStatus())
.isEqualTo(BasePreferenceController.AVAILABLE);
}
@Test
public void airplaneModePreference_testSetValue_updatesCorrectly() {
// Set airplane mode ON by setChecked
mController.setAirplaneModeEnabler(mAirplaneModeEnabler);
assertThat(mController.setChecked(true)).isTrue();
// Check return value if set same status.
when(mAirplaneModeEnabler.isAirplaneModeOn()).thenReturn(true);
assertThat(mController.setChecked(true)).isFalse();
// Set to OFF
assertThat(mController.setChecked(false)).isTrue();
}
@Test
public void airplaneModePreference_testGetValue_correctValueReturned() {
// Set airplane mode ON
Settings.Global.putInt(mResolver, Settings.Global.AIRPLANE_MODE_ON, ON);
mController.displayPreference(mScreen);
mController.onStart();
assertThat(mController.isChecked()).isTrue();
Settings.Global.putInt(mResolver, Settings.Global.AIRPLANE_MODE_ON, OFF);
assertThat(mController.isChecked()).isFalse();
}
@Test
public void airplaneModePreference_testPreferenceUI_updatesCorrectly() {
// Airplane mode default off
Settings.Global.putInt(mResolver, Settings.Global.AIRPLANE_MODE_ON, OFF);
mController.displayPreference(mScreen);
mController.onStop();
assertThat(mPreference.isChecked()).isFalse();
mController.onAirplaneModeChanged(true);
assertThat(mPreference.isChecked()).isTrue();
}
@Test
public void isSliceable_returnsTrue() {
assertThat(mController.isSliceable()).isTrue();
}
@Test
public void isPublicSlice_returnsTrue() {
assertThat(mController.isPublicSlice()).isTrue();
}
}

View File

@@ -1,116 +0,0 @@
/*
* Copyright (C) 2019 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.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
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.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkRequest;
import android.os.Handler;
import com.android.settings.network.telephony.DataConnectivityListener;
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;
@RunWith(RobolectricTestRunner.class)
public class DataConnectivityListenerTest {
@Mock
private DataConnectivityListener.Client mClient;
@Mock
private ConnectivityManager mConnectivityManager;
@Mock
private Network mActiveNetwork;
private Context mContext;
private DataConnectivityListener mListener;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
when(mContext.getSystemService(ConnectivityManager.class)).thenReturn(mConnectivityManager);
when(mConnectivityManager.getActiveNetwork()).thenReturn(mActiveNetwork);
mListener = new DataConnectivityListener(mContext, mClient);
}
@Test
public void noStart_doesNotRegister() {
verify(mConnectivityManager, never()).registerNetworkCallback(any(NetworkRequest.class),
any(ConnectivityManager.NetworkCallback.class), any(Handler.class));
}
@Test
public void start_doesRegister() {
mListener.start();
verify(mConnectivityManager).registerNetworkCallback(any(NetworkRequest.class),
eq(mListener), any(Handler.class));
}
@Test
public void onCapabilitiesChanged_notActiveNetwork_noCallback() {
Network changedNetwork = mock(Network.class);
mListener.onCapabilitiesChanged(changedNetwork, mock(NetworkCapabilities.class));
verify(mClient, never()).onDataConnectivityChange();
}
@Test
public void onCapabilitiesChanged_activeNetwork_onDataConnectivityChangeFires() {
mListener.onCapabilitiesChanged(mActiveNetwork, mock(NetworkCapabilities.class));
verify(mClient).onDataConnectivityChange();
}
@Test
public void onLosing_notActiveNetwork_onDataConnectivityChangeFires() {
Network changedNetwork = mock(Network.class);
mListener.onLosing(changedNetwork, 500);
verify(mClient).onDataConnectivityChange();
}
@Test
public void onLosing_activeNetwork_onDataConnectivityChangeFires() {
mListener.onLosing(mActiveNetwork, 500);
verify(mClient).onDataConnectivityChange();
}
@Test
public void onLost_notActiveNetwork_onDataConnectivityChangeFires() {
Network changedNetwork = mock(Network.class);
mListener.onLost(changedNetwork);
verify(mClient).onDataConnectivityChange();
}
@Test
public void onLost_activeNetwork_onDataConnectivityChangeFires() {
mListener.onLost(mActiveNetwork);
verify(mClient).onDataConnectivityChange();
}
}

View File

@@ -1,86 +0,0 @@
/*
* Copyright (C) 2019 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.doNothing;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import android.content.Context;
import android.os.Looper;
import android.provider.Settings;
import androidx.lifecycle.Lifecycle;
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;
@RunWith(RobolectricTestRunner.class)
public class GlobalSettingsChangeListenerTest {
@Mock
private Lifecycle mLifecycle;
private Context mContext;
private GlobalSettingsChangeListener mListener;
private static final String SETTINGS_FIELD = Settings.Global.AIRPLANE_MODE_ON;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
mListener = spy(new GlobalSettingsChangeListener(Looper.getMainLooper(),
mContext, SETTINGS_FIELD) {
public void onChanged(String field) {}
});
doNothing().when(mLifecycle).addObserver(mListener);
doNothing().when(mLifecycle).removeObserver(mListener);
}
@Test
public void whenChanged_onChangedBeenCalled() {
mListener.onChange(false);
verify(mListener, times(1)).onChanged(SETTINGS_FIELD);
}
@Test
public void whenNotifyChangeBasedOnLifecycle_onStopEvent_onChangedNotCalled() {
mListener.notifyChangeBasedOn(mLifecycle);
mListener.onStart();
mListener.onChange(false);
verify(mListener, times(1)).onChanged(SETTINGS_FIELD);
mListener.onStop();
mListener.onChange(false);
verify(mListener, times(1)).onChanged(SETTINGS_FIELD);
mListener.onStart();
mListener.onChange(false);
verify(mListener, times(2)).onChanged(SETTINGS_FIELD);
}
}

View File

@@ -1,182 +0,0 @@
/*
* Copyright (C) 2016 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 androidx.lifecycle.Lifecycle.Event.ON_START;
import static androidx.lifecycle.Lifecycle.Event.ON_STOP;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.robolectric.shadow.api.Shadow.extract;
import android.content.Context;
import android.net.ConnectivityManager;
import android.os.UserManager;
import android.provider.Settings;
import android.provider.Settings.Global;
import android.telephony.PhoneStateListener;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.testutils.shadow.ShadowConnectivityManager;
import com.android.settings.testutils.shadow.ShadowUserManager;
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
import com.android.settingslib.RestrictedPreference;
import com.android.settingslib.core.lifecycle.Lifecycle;
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 org.robolectric.annotation.Config;
@RunWith(RobolectricTestRunner.class)
@Config(shadows = {ShadowConnectivityManager.class, ShadowUserManager.class})
public class MobileNetworkPreferenceControllerTest {
private Context mContext;
@Mock
private TelephonyManager mTelephonyManager;
@Mock
private SubscriptionManager mSubscriptionManager;
@Mock
private PreferenceScreen mScreen;
private Lifecycle mLifecycle;
private LifecycleOwner mLifecycleOwner;
private MobileNetworkPreferenceController mController;
private Preference mPreference;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
mLifecycleOwner = () -> mLifecycle;
mLifecycle = new Lifecycle(mLifecycleOwner);
when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
when(mContext.getSystemService(SubscriptionManager.class)).thenReturn(mSubscriptionManager);
mPreference = new Preference(mContext);
mPreference.setKey(MobileNetworkPreferenceController.KEY_MOBILE_NETWORK_SETTINGS);
}
@Test
public void secondaryUser_prefIsNotAvailable() {
ShadowUserManager userManager = extract(mContext.getSystemService(UserManager.class));
userManager.setIsAdminUser(false);
ShadowConnectivityManager connectivityManager =
extract(mContext.getSystemService(ConnectivityManager.class));
connectivityManager.setNetworkSupported(ConnectivityManager.TYPE_MOBILE, true);
mController = new MobileNetworkPreferenceController(mContext);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void wifiOnly_prefIsNotAvailable() {
ShadowUserManager userManager = extract(mContext.getSystemService(UserManager.class));
userManager.setIsAdminUser(true);
ShadowConnectivityManager connectivityManager =
extract(mContext.getSystemService(ConnectivityManager.class));
connectivityManager.setNetworkSupported(ConnectivityManager.TYPE_MOBILE, false);
mController = new MobileNetworkPreferenceController(mContext);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void goThroughLifecycle_isAvailable_shouldListenToServiceChange() {
mController = spy(new MobileNetworkPreferenceController(mContext));
mLifecycle.addObserver(mController);
doReturn(true).when(mController).isAvailable();
mLifecycle.handleLifecycleEvent(ON_START);
verify(mTelephonyManager).listen(mController.mPhoneStateListener,
PhoneStateListener.LISTEN_SERVICE_STATE);
mLifecycle.handleLifecycleEvent(ON_STOP);
verify(mTelephonyManager).listen(mController.mPhoneStateListener,
PhoneStateListener.LISTEN_NONE);
}
@Test
public void serviceStateChange_shouldUpdatePrefSummary() {
final String testCarrierName = "test";
final Preference mPreference = mock(Preference.class);
mController = spy(new MobileNetworkPreferenceController(mContext));
mLifecycle.addObserver(mController);
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
doReturn(true).when(mController).isAvailable();
// Display pref and go through lifecycle to set up listener.
mController.displayPreference(mScreen);
mLifecycle.handleLifecycleEvent(ON_START);
verify(mController).onStart();
verify(mTelephonyManager).listen(mController.mPhoneStateListener,
PhoneStateListener.LISTEN_SERVICE_STATE);
doReturn(testCarrierName).when(mController).getSummary();
mController.mPhoneStateListener.onServiceStateChanged(null);
// Carrier name should be set.
verify(mPreference).setSummary(testCarrierName);
}
@Test
public void airplaneModeTurnedOn_shouldDisablePreference() {
Settings.Global.putInt(mContext.getContentResolver(),
Global.AIRPLANE_MODE_ON, 1);
mController = spy(new MobileNetworkPreferenceController(mContext));
final RestrictedPreference mPreference = new RestrictedPreference(mContext);
mController.updateState(mPreference);
assertThat(mPreference.isEnabled()).isFalse();
}
@Test
public void airplaneModeTurnedOffAndNoUserRestriction_shouldEnablePreference() {
Settings.Global.putInt(mContext.getContentResolver(),
Global.AIRPLANE_MODE_ON, 0);
mController = spy(new MobileNetworkPreferenceController(mContext));
final RestrictedPreference mPreference = new RestrictedPreference(mContext);
mPreference.setDisabledByAdmin(null);
mController.updateState(mPreference);
assertThat(mPreference.isEnabled()).isTrue();
}
@Test
public void airplaneModeTurnedOffAndHasUserRestriction_shouldDisablePreference() {
Settings.Global.putInt(mContext.getContentResolver(),
Global.AIRPLANE_MODE_ON, 0);
mController = spy(new MobileNetworkPreferenceController(mContext));
final RestrictedPreference mPreference = new RestrictedPreference(mContext);
mPreference.setDisabledByAdmin(EnforcedAdmin.MULTIPLE_ENFORCED_ADMIN);
mController.updateState(mPreference);
assertThat(mPreference.isEnabled()).isFalse();
}
}

View File

@@ -1,40 +0,0 @@
/*
* Copyright (C) 2016 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 android.content.Context;
import com.android.settings.R;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class MobilePlanPreferenceControllerTest {
@Test
public void testNoProvisionStringFormattedCorrectly() {
final String operator = "test_operator";
final Context context = RuntimeEnvironment.application;
assertThat(context.getString(R.string.mobile_no_provisioning_url, operator, operator))
.contains(operator);
}
}

View File

@@ -1,180 +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;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.atLeastOnce;
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.SubscriptionManager;
import com.android.settings.wifi.WifiConnectionPreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;
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;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import java.util.List;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceScreen;
@RunWith(RobolectricTestRunner.class)
public class MultiNetworkHeaderControllerTest {
private static final String KEY_HEADER = "multi_network_header";
private static final int EXPANDED_CHILDREN_COUNT = 5;
@Mock
private PreferenceScreen mPreferenceScreen;
@Mock
private PreferenceCategory mPreferenceCategory;
@Mock
private WifiConnectionPreferenceController mWifiController;
@Mock
private SubscriptionsPreferenceController mSubscriptionsController;
@Mock
private SubscriptionManager mSubscriptionManager;
private Context mContext;
private LifecycleOwner mLifecycleOwner;
private Lifecycle mLifecycle;
private MultiNetworkHeaderController mHeaderController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
mLifecycleOwner = () -> mLifecycle;
mLifecycle = new Lifecycle(mLifecycleOwner);
when(mContext.getSystemService(SubscriptionManager.class)).thenReturn(mSubscriptionManager);
when(mPreferenceScreen.findPreference(eq(KEY_HEADER))).thenReturn(mPreferenceCategory);
when(mPreferenceCategory.getPreferenceCount()).thenReturn(3);
when(mPreferenceScreen.getInitialExpandedChildrenCount()).thenReturn(
EXPANDED_CHILDREN_COUNT);
mHeaderController = spy(new MultiNetworkHeaderController(mContext, KEY_HEADER));
doReturn(mWifiController).when(mHeaderController).createWifiController(mLifecycle);
doReturn(mSubscriptionsController).when(mHeaderController).createSubscriptionsController(
mLifecycle);
}
@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);
verify(mPreferenceCategory, never()).setVisible(eq(!setVisibleExpectedValue));
verify(mPreferenceCategory, atLeastOnce()).setVisible(eq(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();
ArgumentCaptor<Boolean> captor = ArgumentCaptor.forClass(Boolean.class);
verify(mPreferenceCategory, atLeastOnce()).setVisible(captor.capture());
List<Boolean> values = captor.getAllValues();
assertThat(values.get(values.size()-1)).isEqualTo(Boolean.TRUE);
ArgumentCaptor<Integer> expandedCountCaptor = ArgumentCaptor.forClass(Integer.class);
verify(mPreferenceScreen).setInitialExpandedChildrenCount(expandedCountCaptor.capture());
assertThat(expandedCountCaptor.getValue()).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();
ArgumentCaptor<Boolean> captor = ArgumentCaptor.forClass(Boolean.class);
verify(mPreferenceCategory, atLeastOnce()).setVisible(captor.capture());
List<Boolean> values = captor.getAllValues();
assertThat(values.get(values.size()-1)).isEqualTo(Boolean.FALSE);
ArgumentCaptor<Integer> expandedCountCaptor = ArgumentCaptor.forClass(Integer.class);
verify(mPreferenceScreen).setInitialExpandedChildrenCount(expandedCountCaptor.capture());
assertThat(expandedCountCaptor.getValue()).isEqualTo(EXPANDED_CHILDREN_COUNT);
}
@Test
public void onChildUpdated_noExpandedChildCountAndAvailable_doesNotSetExpandedCount() {
when(mPreferenceScreen.getInitialExpandedChildrenCount()).thenReturn(Integer.MAX_VALUE);
when(mSubscriptionsController.isAvailable()).thenReturn(false);
mHeaderController.init(mLifecycle);
mHeaderController.displayPreference(mPreferenceScreen);
when(mSubscriptionsController.isAvailable()).thenReturn(true);
mHeaderController.onChildrenUpdated();
verify(mPreferenceScreen, never()).setInitialExpandedChildrenCount(anyInt());
}
}

View File

@@ -1,61 +0,0 @@
/*
* Copyright (C) 2016 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 android.content.Context;
import android.provider.SearchIndexableResource;
import com.android.settingslib.drawer.CategoryKey;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import java.util.List;
@RunWith(RobolectricTestRunner.class)
public class NetworkDashboardFragmentTest {
private Context mContext;
private NetworkDashboardFragment mFragment;
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mFragment = new NetworkDashboardFragment();
}
@Test
public void getCategoryKey_isNetwork() {
assertThat(mFragment.getCategoryKey()).isEqualTo(CategoryKey.CATEGORY_NETWORK);
}
@Test
public void getXmlResourcesToIndex_shouldIncludeFragmentXml() {
final List<SearchIndexableResource> indexRes =
NetworkDashboardFragment.SEARCH_INDEX_DATA_PROVIDER.getXmlResourcesToIndex(
mContext,
true /* enabled */);
assertThat(indexRes).hasSize(1);
assertThat(indexRes.get(0).xmlResId).isEqualTo(mFragment.getPreferenceScreenResId());
}
}

View File

@@ -1,130 +0,0 @@
/*
* Copyright (C) 2017 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.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.ComponentName;
import android.content.Context;
import android.net.NetworkScoreManager;
import android.net.NetworkScorerAppData;
import androidx.preference.Preference;
import com.android.settings.R;
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 org.robolectric.util.ReflectionHelpers;
import java.util.Collections;
@RunWith(RobolectricTestRunner.class)
public class NetworkScorerPickerPreferenceControllerTest {
private static final String TEST_SCORER_PACKAGE = "Test Package";
private static final String TEST_SCORER_CLASS = "Test Class";
private static final String TEST_SCORER_LABEL = "Test Label";
private Context mContext;
@Mock
private NetworkScoreManager mNetworkScorer;
private NetworkScorerPickerPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mController = new NetworkScorerPickerPreferenceController(mContext, "test_key");
ReflectionHelpers.setField(mController, "mNetworkScoreManager", mNetworkScorer);
}
@Test
public void testIsAvailable_shouldAlwaysReturnTrue() {
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void updateState_preferenceSetSummaryAsActiveScorerLabel() {
ComponentName scorer = new ComponentName(TEST_SCORER_PACKAGE, TEST_SCORER_CLASS);
NetworkScorerAppData scorerAppData = new NetworkScorerAppData(
0, scorer, TEST_SCORER_LABEL, null /* enableUseOpenWifiActivity */,
null /* networkAvailableNotificationChannelId */);
when(mNetworkScorer.getAllValidScorers())
.thenReturn(Collections.singletonList(scorerAppData));
when(mNetworkScorer.getActiveScorer()).thenReturn(scorerAppData);
Preference preference = mock(Preference.class);
mController.updateState(preference);
verify(preference).setSummary(TEST_SCORER_LABEL);
}
@Test
public void updateState_scorersAvailable_noActiveScorer_preferenceSetSummaryToNone() {
ComponentName scorer = new ComponentName(TEST_SCORER_PACKAGE, TEST_SCORER_CLASS);
NetworkScorerAppData scorerAppData = new NetworkScorerAppData(
0, scorer, TEST_SCORER_LABEL, null /* enableUseOpenWifiActivity */,
null /* networkAvailableNotificationChannelId */);
when(mNetworkScorer.getAllValidScorers())
.thenReturn(Collections.singletonList(scorerAppData));
when(mNetworkScorer.getActiveScorer()).thenReturn(null);
Preference preference = mock(Preference.class);
mController.updateState(preference);
verify(preference).setSummary(mContext.getString(
R.string.network_scorer_picker_none_preference));
}
@Test
public void updateState_scorersAvailable_preferenceEnabled() {
ComponentName scorer = new ComponentName(TEST_SCORER_PACKAGE, TEST_SCORER_CLASS);
NetworkScorerAppData scorerAppData = new NetworkScorerAppData(
0, scorer, TEST_SCORER_LABEL, null /* enableUseOpenWifiActivity */,
null /* networkAvailableNotificationChannelId */);
when(mNetworkScorer.getAllValidScorers())
.thenReturn(Collections.singletonList(scorerAppData));
Preference preference = mock(Preference.class);
mController.updateState(preference);
verify(preference).setEnabled(true);
}
@Test
public void updateState_noScorersAvailable_preferenceDisabled() {
when(mNetworkScorer.getAllValidScorers())
.thenReturn(Collections.emptyList());
Preference preference = mock(Preference.class);
mController.updateState(preference);
verify(preference).setEnabled(false);
verify(preference).setSummary(null);
}
}

View File

@@ -1,117 +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;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
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 java.util.Arrays;
import java.util.List;
@RunWith(RobolectricTestRunner.class)
public class SubscriptionUtilTest {
@Mock
private Context mContext;
@Mock
private SubscriptionManager mSubMgr;
@Mock
private TelephonyManager mTelMgr;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
doReturn(mSubMgr).when(mContext).getSystemService(SubscriptionManager.class);
doReturn(mTelMgr).when(mContext).getSystemService(TelephonyManager.class);
when(mTelMgr.getUiccSlotsInfo()).thenReturn(null);
}
@Test
public void getAvailableSubscriptions_nullInfoFromSubscriptionManager_nonNullResult() {
when(mSubMgr.getAvailableSubscriptionInfoList()).thenReturn(null);
final List<SubscriptionInfo> subs = SubscriptionUtil.getAvailableSubscriptions(mContext);
assertThat(subs).isNotNull();
assertThat(subs).isEmpty();
}
@Test
public void getAvailableSubscriptions_oneSubscription_oneResult() {
final SubscriptionInfo info = mock(SubscriptionInfo.class);
when(mSubMgr.getAvailableSubscriptionInfoList()).thenReturn(Arrays.asList(info));
final List<SubscriptionInfo> subs = SubscriptionUtil.getAvailableSubscriptions(mContext);
assertThat(subs).isNotNull();
assertThat(subs).hasSize(1);
}
@Test
public void getAvailableSubscriptions_twoSubscriptions_twoResults() {
final SubscriptionInfo info1 = mock(SubscriptionInfo.class);
final SubscriptionInfo info2 = mock(SubscriptionInfo.class);
when(mSubMgr.getAvailableSubscriptionInfoList()).thenReturn(Arrays.asList(info1, info2));
final List<SubscriptionInfo> subs = SubscriptionUtil.getAvailableSubscriptions(mContext);
assertThat(subs).isNotNull();
assertThat(subs).hasSize(2);
}
@Test
public void getActiveSubscriptions_nullInfoFromSubscriptionManager_nonNullResult() {
when(mSubMgr.getActiveSubscriptionInfoList()).thenReturn(null);
final List<SubscriptionInfo> subs = SubscriptionUtil.getActiveSubscriptions(mSubMgr);
assertThat(subs).isNotNull();
assertThat(subs).isEmpty();
}
@Test
public void getActiveSubscriptions_oneSubscription_oneResult() {
final SubscriptionInfo info = mock(SubscriptionInfo.class);
when(mSubMgr.getActiveSubscriptionInfoList()).thenReturn(Arrays.asList(info));
final List<SubscriptionInfo> subs = SubscriptionUtil.getActiveSubscriptions(mSubMgr);
assertThat(subs).isNotNull();
assertThat(subs).hasSize(1);
}
@Test
public void getActiveSubscriptions_twoSubscriptions_twoResults() {
final SubscriptionInfo info1 = mock(SubscriptionInfo.class);
final SubscriptionInfo info2 = mock(SubscriptionInfo.class);
when(mSubMgr.getActiveSubscriptionInfoList()).thenReturn(
Arrays.asList(info1, info2));
final List<SubscriptionInfo> subs = SubscriptionUtil.getActiveSubscriptions(mSubMgr);
assertThat(subs).isNotNull();
assertThat(subs).hasSize(2);
}
@Test
public void isInactiveInsertedPSim_nullSubInfo_doesNotCrash() {
assertThat(SubscriptionUtil.isInactiveInsertedPSim(null)).isFalse();
}
}

View File

@@ -1,147 +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.android.settings.core.BasePreferenceController.AVAILABLE;
import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doNothing;
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.Intent;
import android.os.PersistableBundle;
import android.provider.Settings;
import android.telephony.CarrierConfigManager;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import com.android.settings.network.apn.ApnSettings;
import com.android.settingslib.RestrictedPreference;
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;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class ApnPreferenceControllerTest {
private static final int SUB_ID = 2;
@Mock
private TelephonyManager mTelephonyManager;
@Mock
private TelephonyManager mInvalidTelephonyManager;
@Mock
private SubscriptionManager mSubscriptionManager;
@Mock
private CarrierConfigManager mCarrierConfigManager;
private ApnPreferenceController mController;
private RestrictedPreference mPreference;
private Context mContext;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
doReturn(mTelephonyManager).when(mContext).getSystemService(Context.TELEPHONY_SERVICE);
doReturn(mSubscriptionManager).when(mContext).getSystemService(SubscriptionManager.class);
doReturn(mTelephonyManager).when(mTelephonyManager).createForSubscriptionId(SUB_ID);
doReturn(mInvalidTelephonyManager).when(mTelephonyManager).createForSubscriptionId(
SubscriptionManager.INVALID_SUBSCRIPTION_ID);
doReturn(mCarrierConfigManager).when(mContext).getSystemService(CarrierConfigManager.class);
mPreference = new RestrictedPreference(mContext);
mController = new ApnPreferenceController(mContext, "mobile_data");
mController.init(SUB_ID);
mController.setPreference(mPreference);
mController.mCarrierConfigManager = mCarrierConfigManager;
mPreference.setKey(mController.getPreferenceKey());
}
@Test
public void getAvailabilityStatus_apnSettingsNotSupported_returnUnavailable() {
doReturn(TelephonyManager.PHONE_TYPE_CDMA).when(mTelephonyManager).getPhoneType();
final PersistableBundle bundle = new PersistableBundle();
bundle.putBoolean(CarrierConfigManager.KEY_SHOW_APN_SETTING_CDMA_BOOL, false);
doReturn(bundle).when(mCarrierConfigManager).getConfigForSubId(SUB_ID);
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
}
@Test
public void getAvailabilityStatus_apnSettingsSupportedWithCDMA_returnAvailable() {
doReturn(TelephonyManager.PHONE_TYPE_CDMA).when(mTelephonyManager).getPhoneType();
final PersistableBundle bundle = new PersistableBundle();
bundle.putBoolean(CarrierConfigManager.KEY_SHOW_APN_SETTING_CDMA_BOOL, true);
doReturn(bundle).when(mCarrierConfigManager).getConfigForSubId(SUB_ID);
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
}
@Test
public void getAvailabilityStatus_apnSettingsSupportedWithGsm_returnAvailable() {
doReturn(TelephonyManager.PHONE_TYPE_GSM).when(mTelephonyManager).getPhoneType();
final PersistableBundle bundle = new PersistableBundle();
bundle.putBoolean(CarrierConfigManager.KEY_APN_EXPAND_BOOL, true);
doReturn(bundle).when(mCarrierConfigManager).getConfigForSubId(SUB_ID);
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
}
@Test
public void getAvailabilityStatus_carrierConfigNull_returnUnavailable() {
doReturn(TelephonyManager.PHONE_TYPE_GSM).when(mTelephonyManager).getPhoneType();
when(mCarrierConfigManager.getConfigForSubId(SUB_ID)).thenReturn(null);
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
}
@Test
public void getAvailabilityStatus_hideCarrierNetworkSettings_returnUnavailable() {
doReturn(TelephonyManager.PHONE_TYPE_GSM).when(mTelephonyManager).getPhoneType();
final PersistableBundle bundle = new PersistableBundle();
bundle.putBoolean(CarrierConfigManager.KEY_APN_EXPAND_BOOL, true);
bundle.putBoolean(CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL, true);
doReturn(bundle).when(mCarrierConfigManager).getConfigForSubId(SUB_ID);
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
}
@Test
public void handPreferenceTreeClick_fireIntent() {
ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
doNothing().when(mContext).startActivity(captor.capture());
mController.handlePreferenceTreeClick(mPreference);
final Intent intent = captor.getValue();
assertThat(intent.getAction()).isEqualTo(Settings.ACTION_APN_SETTINGS);
assertThat(intent.getIntExtra(ApnSettings.SUB_ID, 0)).isEqualTo(SUB_ID);
}
}

View File

@@ -1,73 +0,0 @@
/*
* Copyright (C) 2019 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 android.os.PersistableBundle;
import android.telephony.CarrierConfigManager;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.Shadows;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowCarrierConfigManager;
@RunWith(RobolectricTestRunner.class)
@Config(shadows = ShadowCarrierConfigManager.class)
public class CarrierSettingsVersionPreferenceControllerTest {
private ShadowCarrierConfigManager mCarrierConfigManager;
private CarrierSettingsVersionPreferenceController mController;
private int mSubscriptionId = 1234;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
final Context context = RuntimeEnvironment.application;
mController = new CarrierSettingsVersionPreferenceController(context, "mock_key");
mController.init(mSubscriptionId);
mCarrierConfigManager = Shadows.shadowOf(
context.getSystemService(CarrierConfigManager.class));
}
@Test
public void getSummary_nullConfig_noCrash() {
mCarrierConfigManager.setConfigForSubId(mSubscriptionId, null);
assertThat(mController.getSummary()).isNull();
}
@Test
public void getSummary_nullVersionString_noCrash() {
mCarrierConfigManager.setConfigForSubId(mSubscriptionId, new PersistableBundle());
assertThat(mController.getSummary()).isNull();
}
@Test
public void getSummary_hasVersionString_correctSummary() {
final PersistableBundle bundle = new PersistableBundle();
bundle.putString(CarrierConfigManager.KEY_CARRIER_CONFIG_VERSION_STRING,
"test_version_123");
mCarrierConfigManager.setConfigForSubId(mSubscriptionId, bundle);
assertThat(mController.getSummary()).isEqualTo("test_version_123");
}
}

View File

@@ -1,135 +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.android.settings.core.BasePreferenceController.AVAILABLE;
import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.PersistableBundle;
import android.provider.Settings;
import android.telephony.CarrierConfigManager;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import androidx.preference.Preference;
import com.android.settingslib.RestrictedPreference;
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;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class DataServiceSetupPreferenceControllerTest {
private static final int SUB_ID = 2;
private static final String SETUP_URL = "url://tmp_url:^1";
@Mock
private TelephonyManager mTelephonyManager;
@Mock
private TelephonyManager mInvalidTelephonyManager;
@Mock
private CarrierConfigManager mCarrierConfigManager;
private PersistableBundle mCarrierConfig;
private DataServiceSetupPreferenceController mController;
private Preference mPreference;
private Context mContext;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
doReturn(mTelephonyManager).when(mContext).getSystemService(Context.TELEPHONY_SERVICE);
doReturn(mTelephonyManager).when(mTelephonyManager).createForSubscriptionId(SUB_ID);
doReturn(mInvalidTelephonyManager).when(mTelephonyManager).createForSubscriptionId(
SubscriptionManager.INVALID_SUBSCRIPTION_ID);
doReturn(mCarrierConfigManager).when(mContext).getSystemService(CarrierConfigManager.class);
Settings.Global.putString(mContext.getContentResolver(),
Settings.Global.SETUP_PREPAID_DATA_SERVICE_URL, SETUP_URL);
mCarrierConfig = new PersistableBundle();
doReturn(mCarrierConfig).when(mCarrierConfigManager).getConfigForSubId(SUB_ID);
mPreference = new RestrictedPreference(mContext);
mController = new DataServiceSetupPreferenceController(mContext, "data_service_setup");
mController.init(SUB_ID);
mPreference.setKey(mController.getPreferenceKey());
}
@Test
public void getAvailabilityStatus_allConfigOn_returnAvailable() {
doReturn(true).when(mTelephonyManager).isLteCdmaEvdoGsmWcdmaEnabled();
mCarrierConfig.putBoolean(CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL,
false);
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
}
@Test
public void getAvailabilityStatus_missUrl_returnUnavailable() {
Settings.Global.putString(mContext.getContentResolver(),
Settings.Global.SETUP_PREPAID_DATA_SERVICE_URL, "");
doReturn(true).when(mTelephonyManager).isLteCdmaEvdoGsmWcdmaEnabled();
mCarrierConfig.putBoolean(CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL,
false);
mController = new DataServiceSetupPreferenceController(mContext, "data_service_setup");
mController.init(SUB_ID);
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
}
@Test
public void getAvailabilityStatus_notCdma_returnUnavailable() {
doReturn(false).when(mTelephonyManager).isLteCdmaEvdoGsmWcdmaEnabled();
mCarrierConfig.putBoolean(CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL,
false);
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
}
@Test
public void handlePreferenceTreeClick_startActivity() {
ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
doNothing().when(mContext).startActivity(captor.capture());
mController.handlePreferenceTreeClick(mPreference);
final Intent intent = captor.getValue();
assertThat(intent.getAction()).isEqualTo(Intent.ACTION_VIEW);
assertThat(intent.getData()).isEqualTo(
Uri.parse(TextUtils.expandTemplate(SETUP_URL, "").toString()));
}
}

View File

@@ -1,91 +0,0 @@
/*
* Copyright (C) 2019 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.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import com.android.settings.network.SubscriptionUtil;
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 DisableSimFooterPreferenceControllerTest {
private static final String PREF_KEY = "pref_key";
private static final int SUB_ID = 111;
@Mock
private SubscriptionInfo mInfo;
private Context mContext;
@Mock
private SubscriptionManager mSubscriptionManager;
private DisableSimFooterPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
doReturn(mSubscriptionManager).when(mContext).getSystemService(SubscriptionManager.class);
when(mInfo.getSubscriptionId()).thenReturn(SUB_ID);
SubscriptionUtil.setAvailableSubscriptionsForTesting(Arrays.asList(mInfo));
mController = new DisableSimFooterPreferenceController(mContext, PREF_KEY);
}
@Test
public void isAvailable_noInit_notAvailable() {
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void isAvailable_eSIM_notAvailable() {
when(mInfo.isEmbedded()).thenReturn(true);
mController.init(SUB_ID);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void isAvailable_pSIM_available_cannot_disable_pSIM() {
when(mInfo.isEmbedded()).thenReturn(false);
mController.init(SUB_ID);
doReturn(false).when(mSubscriptionManager).canDisablePhysicalSubscription();
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void isAvailable_pSIM_available_can_disable_pSIM() {
when(mInfo.isEmbedded()).thenReturn(false);
mController.init(SUB_ID);
doReturn(true).when(mSubscriptionManager).canDisablePhysicalSubscription();
assertThat(mController.isAvailable()).isFalse();
}
}

View File

@@ -1,294 +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 android.app.slice.Slice.EXTRA_TOGGLE_STATE;
import static android.app.slice.Slice.HINT_TITLE;
import static android.app.slice.SliceItem.FORMAT_TEXT;
import static com.android.settings.Utils.SETTINGS_PACKAGE_NAME;
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.verify;
import static org.mockito.Mockito.when;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.telephony.CarrierConfigManager;
import android.telephony.SubscriptionManager;
import android.telephony.ims.ProvisioningManager;
import androidx.slice.Slice;
import androidx.slice.SliceItem;
import androidx.slice.SliceMetadata;
import androidx.slice.SliceProvider;
import androidx.slice.core.SliceAction;
import androidx.slice.core.SliceQuery;
import androidx.slice.widget.SliceLiveData;
import com.android.settings.R;
import com.android.settings.network.ims.MockVolteQueryImsState;
import com.android.settings.slices.CustomSliceRegistry;
import com.android.settings.slices.SettingsSliceProvider;
import com.android.settings.slices.SliceBroadcastReceiver;
import com.android.settings.slices.SlicesFeatureProvider;
import com.android.settings.testutils.FakeFeatureFactory;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.ShadowSubscriptionManager;
import java.util.List;
@RunWith(RobolectricTestRunner.class)
public class Enhanced4gLteSliceHelperTest {
private static final int SUB_ID = 1;
@Mock
private CarrierConfigManager mMockCarrierConfigManager;
@Mock
private ProvisioningManager mProvisioningManager;
private ShadowSubscriptionManager mShadowSubscriptionManager;
private MockVolteQueryImsState mQueryImsState;
private Context mContext;
private FakeEnhanced4gLteSliceHelper mEnhanced4gLteSliceHelper;
private SettingsSliceProvider mProvider;
private SliceBroadcastReceiver mReceiver;
private FakeFeatureFactory mFeatureFactory;
private SlicesFeatureProvider mSlicesFeatureProvider;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mShadowSubscriptionManager = Shadow.extract(mContext.getSystemService(
SubscriptionManager.class));
mShadowSubscriptionManager.setDefaultVoiceSubscriptionId(SUB_ID);
mFeatureFactory = FakeFeatureFactory.setupForTest();
mSlicesFeatureProvider = mFeatureFactory.getSlicesFeatureProvider();
//setup for SettingsSliceProvider tests
mProvider = spy(new SettingsSliceProvider());
doReturn(mContext).when(mProvider).getContext();
mProvider.onCreateSliceProvider();
//setup for SliceBroadcastReceiver test
mReceiver = spy(new SliceBroadcastReceiver());
mQueryImsState = spy(new MockVolteQueryImsState(mContext, SUB_ID));
mQueryImsState.setEnabledByPlatform(true);
mQueryImsState.setIsProvisionedOnDevice(true);
mQueryImsState.setIsTtyOnVolteEnabled(true);
mQueryImsState.setServiceStateReady(true);
mQueryImsState.setIsEnabledByUser(true);
mEnhanced4gLteSliceHelper = spy(new FakeEnhanced4gLteSliceHelper(mContext));
doReturn(mQueryImsState).when(mEnhanced4gLteSliceHelper).queryImsState(anyInt());
// Set-up specs for SliceMetadata.
SliceProvider.setSpecs(SliceLiveData.SUPPORTED_SPECS);
}
@Test
public void test_CreateEnhanced4gLteSlice_invalidSubId() {
mQueryImsState.setEnabledByPlatform(false);
mQueryImsState.setIsProvisionedOnDevice(false);
mQueryImsState.setIsEnabledByUser(false);
mShadowSubscriptionManager.setDefaultVoiceSubscriptionId(-1);
final Slice slice = mEnhanced4gLteSliceHelper.createEnhanced4gLteSlice(
CustomSliceRegistry.ENHANCED_4G_SLICE_URI);
assertThat(slice).isNull();
}
@Test
public void test_CreateEnhanced4gLteSlice_enhanced4gLteNotSupported() {
mQueryImsState.setEnabledByPlatform(false);
final Slice slice = mEnhanced4gLteSliceHelper.createEnhanced4gLteSlice(
CustomSliceRegistry.ENHANCED_4G_SLICE_URI);
assertThat(mEnhanced4gLteSliceHelper.getDefaultVoiceSubId()).isEqualTo(1);
assertThat(slice).isNull();
}
@Test
public void test_CreateEnhanced4gLteSlice_success() {
mQueryImsState.setEnabledByPlatform(true);
mQueryImsState.setIsProvisionedOnDevice(true);
when(mMockCarrierConfigManager.getConfigForSubId(1)).thenReturn(null);
final Slice slice = mEnhanced4gLteSliceHelper.createEnhanced4gLteSlice(
CustomSliceRegistry.ENHANCED_4G_SLICE_URI);
assertThat(mEnhanced4gLteSliceHelper.getDefaultVoiceSubId()).isEqualTo(1);
testEnhanced4gLteSettingsToggleSlice(slice);
}
@Test
public void test_SettingSliceProvider_getsRightSliceEnhanced4gLte() {
mQueryImsState.setEnabledByPlatform(true);
mQueryImsState.setIsProvisionedOnDevice(true);
when(mMockCarrierConfigManager.getConfigForSubId(1)).thenReturn(null);
when(mSlicesFeatureProvider.getNewEnhanced4gLteSliceHelper(mContext))
.thenReturn(mEnhanced4gLteSliceHelper);
final Slice slice = mProvider.onBindSlice(CustomSliceRegistry.ENHANCED_4G_SLICE_URI);
assertThat(mEnhanced4gLteSliceHelper.getDefaultVoiceSubId()).isEqualTo(1);
testEnhanced4gLteSettingsToggleSlice(slice);
}
@Test
@Ignore
public void test_SliceBroadcastReceiver_toggleOffEnhanced4gLte() {
mQueryImsState.setEnabledByPlatform(true);
mQueryImsState.setIsProvisionedOnDevice(true);
mQueryImsState.setIsEnabledByUser(false);
when(mSlicesFeatureProvider.getNewEnhanced4gLteSliceHelper(mContext))
.thenReturn(mEnhanced4gLteSliceHelper);
final ArgumentCaptor<Boolean> mEnhanced4gLteSettingCaptor = ArgumentCaptor.forClass(
Boolean.class);
// turn on Enhanced4gLte setting
final Intent intent = new Intent(Enhanced4gLteSliceHelper.ACTION_ENHANCED_4G_LTE_CHANGED);
intent.putExtra(EXTRA_TOGGLE_STATE, true);
// change the setting
mReceiver.onReceive(mContext, intent);
verify(mEnhanced4gLteSliceHelper).setEnhanced4gLteModeSetting(anyInt(),
mEnhanced4gLteSettingCaptor.capture());
// assert the change
assertThat(mEnhanced4gLteSettingCaptor.getValue()).isTrue();
}
private void testEnhanced4gLteSettingsUnavailableSlice(Slice slice,
PendingIntent expectedPrimaryAction) {
final SliceMetadata metadata = SliceMetadata.from(mContext, slice);
//Check there is no toggle action
final List<SliceAction> toggles = metadata.getToggles();
assertThat(toggles).isEmpty();
// Check whether the primary action is to open Enhanced4gLte settings activity
final PendingIntent primaryPendingIntent =
metadata.getPrimaryAction().getAction();
assertThat(primaryPendingIntent).isEqualTo(expectedPrimaryAction);
// Check the title
final List<SliceItem> sliceItems = slice.getItems();
assertTitle(sliceItems, mContext.getString(R.string.enhanced_4g_lte_mode_title));
}
private void testEnhanced4gLteSettingsToggleSlice(Slice slice) {
final SliceMetadata metadata = SliceMetadata.from(mContext, slice);
final List<SliceAction> toggles = metadata.getToggles();
assertThat(toggles).hasSize(1);
final SliceAction mainToggleAction = toggles.get(0);
// Check intent in Toggle Action
final PendingIntent togglePendingIntent = mainToggleAction.getAction();
final PendingIntent expectedToggleIntent = getBroadcastIntent(
Enhanced4gLteSliceHelper.ACTION_ENHANCED_4G_LTE_CHANGED);
assertThat(togglePendingIntent).isEqualTo(expectedToggleIntent);
// Check primary intent
final PendingIntent primaryPendingIntent = metadata.getPrimaryAction().getAction();
final PendingIntent expectedPendingIntent =
getActivityIntent(Enhanced4gLteSliceHelper.ACTION_MOBILE_NETWORK_SETTINGS_ACTIVITY);
assertThat(primaryPendingIntent).isEqualTo(expectedPendingIntent);
// Check the title
final List<SliceItem> sliceItems = slice.getItems();
assertTitle(sliceItems, mContext.getString(R.string.enhanced_4g_lte_mode_title));
}
private PendingIntent getBroadcastIntent(String action) {
final Intent intent = new Intent(action);
intent.setClass(mContext, SliceBroadcastReceiver.class);
return PendingIntent.getBroadcast(mContext, 0 /* requestCode */, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
}
private PendingIntent getActivityIntent(String action) {
final Intent intent = new Intent(action);
intent.setPackage(SETTINGS_PACKAGE_NAME);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
return PendingIntent.getActivity(mContext, 0 /* requestCode */, intent, 0 /* flags */);
}
private void assertTitle(List<SliceItem> sliceItems, String title) {
boolean hasTitle = false;
for (SliceItem item : sliceItems) {
final List<SliceItem> titleItems = SliceQuery.findAll(item, FORMAT_TEXT, HINT_TITLE,
null /* non-hints */);
if (titleItems == null) {
continue;
}
hasTitle = true;
for (SliceItem subTitleItem : titleItems) {
assertThat(subTitleItem.getText()).isEqualTo(title);
}
}
assertThat(hasTitle).isTrue();
}
private class FakeEnhanced4gLteSliceHelper extends Enhanced4gLteSliceHelper {
int mSubId = SUB_ID;
FakeEnhanced4gLteSliceHelper(Context context) {
super(context);
}
@Override
protected CarrierConfigManager getCarrierConfigManager() {
return mMockCarrierConfigManager;
}
protected int getDefaultVoiceSubId() {
return mSubId;
}
private void setDefaultVoiceSubId(int id) {
mSubId = id;
}
}
}

View File

@@ -1,78 +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.doNothing;
import static org.mockito.Mockito.spy;
import static org.robolectric.Shadows.shadowOf;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.telephony.euicc.EuiccManager;
import androidx.preference.Preference;
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.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowTelephonyManager;
@RunWith(AndroidJUnit4.class)
public class EuiccPreferenceControllerTest {
private static final int SUB_ID = 2;
private TelephonyManager mTelephonyManager;
private ShadowTelephonyManager mShadowTelephonyManager;
private EuiccPreferenceController mController;
private Preference mPreference;
private Context mContext;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application.getBaseContext());
mTelephonyManager = mContext.getSystemService(TelephonyManager.class);
mShadowTelephonyManager = shadowOf(mTelephonyManager);
mShadowTelephonyManager.setTelephonyManagerForSubscriptionId(SUB_ID, 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);
}
}

View File

@@ -1,125 +0,0 @@
/*
* Copyright (C) 2019 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;
import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE;
import static com.google.common.truth.Truth.assertThat;
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.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.telephony.data.ApnSetting;
import androidx.preference.SwitchPreference;
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 org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowSubscriptionManager;
@RunWith(RobolectricTestRunner.class)
@Config(shadows = ShadowSubscriptionManager.class)
public class MmsMessagePreferenceControllerTest {
private static final int SUB_ID = 2;
@Mock
private TelephonyManager mTelephonyManager;
@Mock
private SubscriptionManager mSubscriptionManager;
private MmsMessagePreferenceController mController;
private SwitchPreference mPreference;
private Context mContext;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
when(mContext.getSystemService(TelephonyManager.class)).thenReturn(mTelephonyManager);
when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
when(mContext.getSystemService(SubscriptionManager.class)).thenReturn(mSubscriptionManager);
when(mTelephonyManager.createForSubscriptionId(SUB_ID)).thenReturn(mTelephonyManager);
mPreference = new SwitchPreference(mContext);
mController = new MmsMessagePreferenceController(mContext, "mms_message");
ShadowSubscriptionManager.setDefaultDataSubscriptionId(SUB_ID);
mController.init(SUB_ID);
mPreference.setKey(mController.getPreferenceKey());
}
@Test
public void getAvailabilityStatus_invalidSubscription_returnUnavailable() {
mController.init(SubscriptionManager.INVALID_SUBSCRIPTION_ID);
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
}
@Test
public void getAvailabilityStatus_mobileDataOn_returnUnavailable() {
when(mTelephonyManager.isDataEnabled()).thenReturn(true);
assertThat(mController.getAvailabilityStatus(SUB_ID)).isEqualTo(CONDITIONALLY_UNAVAILABLE);
}
@Test
public void getAvailabilityStatus_meteredOff_returnUnavailable() {
when(mTelephonyManager.isApnMetered(ApnSetting.TYPE_MMS)).thenReturn(false);
assertThat(mController.getAvailabilityStatus(SUB_ID)).isEqualTo(CONDITIONALLY_UNAVAILABLE);
}
@Test
public void getAvailabilityStatus_mobileDataOffWithValidSubId_returnAvailable() {
mController.init(SUB_ID);
when(mTelephonyManager.isDataEnabled()).thenReturn(false);
when(mTelephonyManager.isApnMetered(ApnSetting.TYPE_MMS)).thenReturn(true);
assertThat(mController.getAvailabilityStatus(SUB_ID)).isEqualTo(AVAILABLE);
}
@Test
public void isChecked_returnDataFromTelephonyManager() {
when(mTelephonyManager.isDataEnabledForApn(ApnSetting.TYPE_MMS)).thenReturn(false);
assertThat(mController.isChecked()).isFalse();
when(mTelephonyManager.isDataEnabledForApn(ApnSetting.TYPE_MMS)).thenReturn(true);
assertThat(mController.isChecked()).isTrue();
}
@Test
public void setChecked_setDataIntoSubscriptionManager() {
mController.setChecked(true);
verify(mTelephonyManager).setMobileDataPolicyEnabledStatus(
TelephonyManager.MOBILE_DATA_POLICY_MMS_ALWAYS_ALLOWED, true);
mController.setChecked(false);
verify(mTelephonyManager).setMobileDataPolicyEnabledStatus(
TelephonyManager.MOBILE_DATA_POLICY_MMS_ALWAYS_ALLOWED, false);
}
}

View File

@@ -1,182 +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.android.settings.core.BasePreferenceController.AVAILABLE_UNSEARCHABLE;
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.verify;
import android.content.Context;
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 com.android.settings.R;
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 org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowSubscriptionManager;
@RunWith(RobolectricTestRunner.class)
@Config(shadows = ShadowSubscriptionManager.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(RuntimeEnvironment.application);
doReturn(mTelephonyManager).when(mContext).getSystemService(Context.TELEPHONY_SERVICE);
doReturn(mSubscriptionManager).when(mContext).getSystemService(SubscriptionManager.class);
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");
ShadowSubscriptionManager.setDefaultDataSubscriptionId(SUB_ID);
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);
doReturn(true).when(mSubscriptionManager).isActiveSubscriptionId(SUB_ID_OTHER);
ShadowSubscriptionManager.setDefaultDataSubscriptionId(SUB_ID_OTHER);
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;
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(mContext.getString(R.string.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(mContext.getString(R.string.mobile_data_settings_summary));
}
}

View File

@@ -1,335 +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.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.nullable;
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.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.PersistableBundle;
import android.provider.Settings;
import android.telecom.PhoneAccountHandle;
import android.telephony.CarrierConfigManager;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import com.android.settings.network.telephony.TelephonyConstants.TelephonyManagerConstants;
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.ArrayList;
import java.util.Arrays;
@RunWith(RobolectricTestRunner.class)
public class MobileNetworkUtilsTest {
private static final String PACKAGE_NAME = "com.android.app";
private static final int SUB_ID_1 = 1;
private static final int SUB_ID_2 = 2;
private static final int SUB_ID_INVALID = -1;
private static final String PLMN_FROM_TELEPHONY_MANAGER_API = "testPlmn";
private static final String PLMN_FROM_SUB_ID_1 = "testPlmnSub1";
private static final String PLMN_FROM_SUB_ID_2 = "testPlmnSub2";
@Mock
private TelephonyManager mTelephonyManager;
@Mock
private TelephonyManager mTelephonyManager2;
@Mock
private SubscriptionManager mSubscriptionManager;
@Mock
private SubscriptionInfo mSubscriptionInfo1;
@Mock
private SubscriptionInfo mSubscriptionInfo2;
@Mock
private PackageManager mPackageManager;
@Mock
private PhoneAccountHandle mPhoneAccountHandle;
@Mock
private ComponentName mComponentName;
@Mock
private ResolveInfo mResolveInfo;
@Mock
private CarrierConfigManager mCarrierConfigManager;
private Context mContext;
private PersistableBundle mCarrierConfig;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
when(mContext.getSystemService(SubscriptionManager.class)).thenReturn(mSubscriptionManager);
when(mContext.getSystemService(TelephonyManager.class)).thenReturn(mTelephonyManager);
when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
when(mTelephonyManager.createForSubscriptionId(SUB_ID_1)).thenReturn(mTelephonyManager);
when(mTelephonyManager.createForSubscriptionId(SUB_ID_2)).thenReturn(mTelephonyManager2);
when(mContext.getPackageManager()).thenReturn(mPackageManager);
when(mPhoneAccountHandle.getComponentName()).thenReturn(mComponentName);
when(mComponentName.getPackageName()).thenReturn(PACKAGE_NAME);
when(mContext.getSystemService(CarrierConfigManager.class)).thenReturn(
mCarrierConfigManager);
mCarrierConfig = new PersistableBundle();
when(mCarrierConfigManager.getConfigForSubId(SUB_ID_1)).thenReturn(mCarrierConfig);
when(mSubscriptionInfo1.getSubscriptionId()).thenReturn(SUB_ID_1);
when(mSubscriptionInfo1.getCarrierName()).thenReturn(PLMN_FROM_SUB_ID_1);
when(mSubscriptionInfo2.getSubscriptionId()).thenReturn(SUB_ID_2);
when(mSubscriptionInfo2.getCarrierName()).thenReturn(PLMN_FROM_SUB_ID_2);
when(mSubscriptionManager.getActiveSubscriptionInfoList()).thenReturn(
Arrays.asList(mSubscriptionInfo1, mSubscriptionInfo2));
when(mSubscriptionManager.getAccessibleSubscriptionInfoList()).thenReturn(
Arrays.asList(mSubscriptionInfo1, mSubscriptionInfo2));
when(mTelephonyManager.getNetworkOperatorName()).thenReturn(
PLMN_FROM_TELEPHONY_MANAGER_API);
}
@Test
public void setMobileDataEnabled_setEnabled_enabled() {
MobileNetworkUtils.setMobileDataEnabled(mContext, SUB_ID_1, true, false);
verify(mTelephonyManager).setDataEnabled(true);
verify(mTelephonyManager2, never()).setDataEnabled(anyBoolean());
}
@Test
public void setMobileDataEnabled_setDisabled_disabled() {
MobileNetworkUtils.setMobileDataEnabled(mContext, SUB_ID_2, true, false);
verify(mTelephonyManager2).setDataEnabled(true);
verify(mTelephonyManager, never()).setDataEnabled(anyBoolean());
}
@Test
public void setMobileDataEnabled_disableOtherSubscriptions() {
MobileNetworkUtils.setMobileDataEnabled(mContext, SUB_ID_1, true, true);
verify(mTelephonyManager).setDataEnabled(true);
verify(mTelephonyManager2).setDataEnabled(false);
}
@Test
public void buildConfigureIntent_nullHandle_returnNull() {
assertThat(MobileNetworkUtils.buildPhoneAccountConfigureIntent(mContext, null)).isNull();
}
@Test
public void buildConfigureIntent_noActivityHandleIntent_returnNull() {
when(mPackageManager.queryIntentActivities(nullable(Intent.class), anyInt()))
.thenReturn(new ArrayList<>());
assertThat(MobileNetworkUtils.buildPhoneAccountConfigureIntent(mContext,
mPhoneAccountHandle)).isNull();
}
@Test
public void buildConfigureIntent_hasActivityHandleIntent_returnIntent() {
when(mPackageManager.queryIntentActivities(nullable(Intent.class), anyInt()))
.thenReturn(Arrays.asList(mResolveInfo));
assertThat(MobileNetworkUtils.buildPhoneAccountConfigureIntent(mContext,
mPhoneAccountHandle)).isNotNull();
}
@Test
public void isCdmaOptions_phoneTypeCdma_returnTrue() {
when(mTelephonyManager.getPhoneType()).thenReturn(TelephonyManager.PHONE_TYPE_CDMA);
assertThat(MobileNetworkUtils.isCdmaOptions(mContext, SUB_ID_1)).isTrue();
}
@Test
public void isCdmaOptions_worldModeWithGsmWcdma_returnTrue() {
when(mTelephonyManager.getPhoneType()).thenReturn(TelephonyManager.PHONE_TYPE_GSM);
mCarrierConfig.putBoolean(CarrierConfigManager.KEY_WORLD_MODE_ENABLED_BOOL, true);
Settings.Global.putInt(mContext.getContentResolver(),
android.provider.Settings.Global.PREFERRED_NETWORK_MODE + SUB_ID_1,
TelephonyManagerConstants.NETWORK_MODE_LTE_GSM_WCDMA);
assertThat(MobileNetworkUtils.isCdmaOptions(mContext, SUB_ID_1)).isTrue();
}
@Test
public void isCdmaOptions_carrierWorldModeWithoutHideCarrier_returnTrue() {
when(mTelephonyManager.getPhoneType()).thenReturn(TelephonyManager.PHONE_TYPE_GSM);
mCarrierConfig.putBoolean(CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL,
false);
mCarrierConfig.putBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL, true);
assertThat(MobileNetworkUtils.isCdmaOptions(mContext, SUB_ID_1)).isTrue();
}
@Test
public void getSearchableSubscriptionId_oneActive_returnValid() {
when(mSubscriptionManager.getActiveSubscriptionInfoList()).thenReturn(
Arrays.asList(mSubscriptionInfo1));
assertThat(MobileNetworkUtils.getSearchableSubscriptionId(mContext)).isEqualTo(SUB_ID_1);
}
@Test
public void getSearchableSubscriptionId_nonActive_returnInvalid() {
when(mSubscriptionManager.getActiveSubscriptionInfoList()).thenReturn(new ArrayList<>());
assertThat(MobileNetworkUtils.getSearchableSubscriptionId(mContext))
.isEqualTo(SubscriptionManager.INVALID_SUBSCRIPTION_ID);
}
@Test
public void shouldDisplayNetworkSelectOptions_HideCarrierNetwork_returnFalse() {
mCarrierConfig.putBoolean(CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL,
true);
assertThat(MobileNetworkUtils.shouldDisplayNetworkSelectOptions(mContext, SUB_ID_1))
.isFalse();
}
@Test
public void shouldDisplayNetworkSelectOptions_allCheckPass_returnTrue() {
mCarrierConfig.putBoolean(CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL,
false);
mCarrierConfig.putBoolean(CarrierConfigManager.KEY_OPERATOR_SELECTION_EXPAND_BOOL, true);
mCarrierConfig.putBoolean(CarrierConfigManager.KEY_CSP_ENABLED_BOOL, false);
when(mTelephonyManager.getPhoneType()).thenReturn(TelephonyManager.PHONE_TYPE_GSM);
assertThat(MobileNetworkUtils.shouldDisplayNetworkSelectOptions(mContext, SUB_ID_1))
.isTrue();
}
@Test
public void shouldSpeciallyUpdateGsmCdma_notWorldMode_returnFalse() {
mCarrierConfig.putBoolean(CarrierConfigManager.KEY_WORLD_MODE_ENABLED_BOOL, false);
mCarrierConfig.putBoolean(CarrierConfigManager.KEY_SUPPORT_TDSCDMA_BOOL, false);
assertThat(MobileNetworkUtils.shouldSpeciallyUpdateGsmCdma(mContext, SUB_ID_1)).isFalse();
}
@Test
public void shouldSpeciallyUpdateGsmCdma_supportTdscdma_returnFalse() {
mCarrierConfig.putBoolean(CarrierConfigManager.KEY_WORLD_MODE_ENABLED_BOOL, true);
mCarrierConfig.putBoolean(CarrierConfigManager.KEY_SUPPORT_TDSCDMA_BOOL, true);
assertThat(MobileNetworkUtils.shouldSpeciallyUpdateGsmCdma(mContext, SUB_ID_1)).isFalse();
}
@Test
public void shouldSpeciallyUpdateGsmCdma_ModeLteTdscdmaGsm_returnTrue() {
mCarrierConfig.putBoolean(CarrierConfigManager.KEY_WORLD_MODE_ENABLED_BOOL, true);
mCarrierConfig.putBoolean(CarrierConfigManager.KEY_SUPPORT_TDSCDMA_BOOL, false);
Settings.Global.putInt(mContext.getContentResolver(),
android.provider.Settings.Global.PREFERRED_NETWORK_MODE + SUB_ID_1,
TelephonyManagerConstants.NETWORK_MODE_LTE_TDSCDMA_GSM);
assertThat(MobileNetworkUtils.shouldSpeciallyUpdateGsmCdma(mContext, SUB_ID_1)).isTrue();
}
@Test
public void shouldSpeciallyUpdateGsmCdma_ModeLteTdscdmaGsmWcdma_returnTrue() {
mCarrierConfig.putBoolean(CarrierConfigManager.KEY_WORLD_MODE_ENABLED_BOOL, true);
mCarrierConfig.putBoolean(CarrierConfigManager.KEY_SUPPORT_TDSCDMA_BOOL, false);
Settings.Global.putInt(mContext.getContentResolver(),
android.provider.Settings.Global.PREFERRED_NETWORK_MODE + SUB_ID_1,
TelephonyManagerConstants.NETWORK_MODE_LTE_TDSCDMA_GSM_WCDMA);
assertThat(MobileNetworkUtils.shouldSpeciallyUpdateGsmCdma(mContext, SUB_ID_1)).isTrue();
}
@Test
public void shouldSpeciallyUpdateGsmCdma_ModeLteTdscdma_returnTrue() {
mCarrierConfig.putBoolean(CarrierConfigManager.KEY_WORLD_MODE_ENABLED_BOOL, true);
mCarrierConfig.putBoolean(CarrierConfigManager.KEY_SUPPORT_TDSCDMA_BOOL, false);
Settings.Global.putInt(mContext.getContentResolver(),
android.provider.Settings.Global.PREFERRED_NETWORK_MODE + SUB_ID_1,
TelephonyManagerConstants.NETWORK_MODE_LTE_TDSCDMA);
assertThat(MobileNetworkUtils.shouldSpeciallyUpdateGsmCdma(mContext, SUB_ID_1)).isTrue();
}
@Test
public void shouldSpeciallyUpdateGsmCdma_ModeLteTdscdmaWcdma_returnTrue() {
mCarrierConfig.putBoolean(CarrierConfigManager.KEY_WORLD_MODE_ENABLED_BOOL, true);
mCarrierConfig.putBoolean(CarrierConfigManager.KEY_SUPPORT_TDSCDMA_BOOL, false);
Settings.Global.putInt(mContext.getContentResolver(),
android.provider.Settings.Global.PREFERRED_NETWORK_MODE + SUB_ID_1,
TelephonyManagerConstants.NETWORK_MODE_LTE_TDSCDMA_WCDMA);
assertThat(MobileNetworkUtils.shouldSpeciallyUpdateGsmCdma(mContext, SUB_ID_1)).isTrue();
}
@Test
public void shouldSpeciallyUpdateGsmCdma_ModeLteTdscdmaCdmaEvdoGsmWcdma_returnTrue() {
mCarrierConfig.putBoolean(CarrierConfigManager.KEY_WORLD_MODE_ENABLED_BOOL, true);
mCarrierConfig.putBoolean(CarrierConfigManager.KEY_SUPPORT_TDSCDMA_BOOL, false);
Settings.Global.putInt(mContext.getContentResolver(),
android.provider.Settings.Global.PREFERRED_NETWORK_MODE + SUB_ID_1,
TelephonyManagerConstants.NETWORK_MODE_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA);
assertThat(MobileNetworkUtils.shouldSpeciallyUpdateGsmCdma(mContext, SUB_ID_1)).isTrue();
}
@Test
public void shouldSpeciallyUpdateGsmCdma_ModeLteCdmaEvdoGsmWcdma_returnTrue() {
mCarrierConfig.putBoolean(CarrierConfigManager.KEY_WORLD_MODE_ENABLED_BOOL, true);
mCarrierConfig.putBoolean(CarrierConfigManager.KEY_SUPPORT_TDSCDMA_BOOL, false);
Settings.Global.putInt(mContext.getContentResolver(),
android.provider.Settings.Global.PREFERRED_NETWORK_MODE + SUB_ID_1,
TelephonyManagerConstants.NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA);
assertThat(MobileNetworkUtils.shouldSpeciallyUpdateGsmCdma(mContext, SUB_ID_1)).isTrue();
}
@Test
public void getCurrentCarrierNameForDisplay_withoutValidSubId_returnNetworkOperatorName() {
assertThat(MobileNetworkUtils.getCurrentCarrierNameForDisplay(
mContext, SUB_ID_INVALID)).isEqualTo(PLMN_FROM_TELEPHONY_MANAGER_API);
}
@Test
public void getCurrentCarrierNameForDisplay_withValidSubId_returnCurrentCarrierName() {
assertThat(MobileNetworkUtils.getCurrentCarrierNameForDisplay(
mContext, SUB_ID_1)).isEqualTo(PLMN_FROM_SUB_ID_1);
assertThat(MobileNetworkUtils.getCurrentCarrierNameForDisplay(
mContext, SUB_ID_2)).isEqualTo(PLMN_FROM_SUB_ID_2);
}
@Test
public void getCurrentCarrierNameForDisplay_withoutSubId_returnNotNull() {
assertThat(MobileNetworkUtils.getCurrentCarrierNameForDisplay(
mContext)).isNotNull();
}
}

View File

@@ -1,107 +0,0 @@
/*
* 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.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.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import org.junit.Before;
import org.junit.Ignore;
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;
@RunWith(RobolectricTestRunner.class)
public class NrDisabledInDsdsFooterPreferenceControllerTest {
private static final String PREF_KEY = "pref_key";
private static final int SUB_ID = 111;
private Context mContext;
@Mock
private TelephonyManager mTelephonyManager;
@Mock
private SubscriptionManager mSubscriptionManager;
private NrDisabledInDsdsFooterPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
doReturn(mTelephonyManager).when(mContext).getSystemService(Context.TELEPHONY_SERVICE);
doReturn(mSubscriptionManager).when(mContext).getSystemService(
Context.TELEPHONY_SUBSCRIPTION_SERVICE);
doReturn(mTelephonyManager).when(mTelephonyManager).createForSubscriptionId(anyInt());
mController = new NrDisabledInDsdsFooterPreferenceController(mContext, PREF_KEY);
}
@Test
public void isAvailable_noInit_notAvailable() {
assertThat(mController.isAvailable()).isFalse();
}
@Test
@Ignore
public void isAvailable_dataOnAndDsdsAnd5GSupported_Available() {
when(mTelephonyManager.getSupportedRadioAccessFamily())
.thenReturn(TelephonyManager.NETWORK_TYPE_BITMASK_NR);
when(mSubscriptionManager.getActiveSubscriptionIdList()).thenReturn(new int[] {1, 2});
when(mTelephonyManager.isDataEnabled()).thenReturn(true);
mController.init(SUB_ID);
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void isAvailable_5gNotSupported_notAvailable() {
when(mTelephonyManager.getSupportedRadioAccessFamily())
.thenReturn(TelephonyManager.NETWORK_TYPE_BITMASK_LTE);
when(mSubscriptionManager.getActiveSubscriptionIdList()).thenReturn(new int[] {1, 2});
when(mTelephonyManager.isDataEnabled()).thenReturn(true);
mController.init(SUB_ID);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void isAvailable_mobileDataOff_notAvailable() {
when(mTelephonyManager.getSupportedRadioAccessFamily())
.thenReturn(TelephonyManager.NETWORK_TYPE_BITMASK_NR);
when(mSubscriptionManager.getActiveSubscriptionIdList()).thenReturn(new int[] {1, 2});
when(mTelephonyManager.isDataEnabled()).thenReturn(false);
mController.init(SUB_ID);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void isAvailable_singleSimMode_notAvailable() {
when(mTelephonyManager.getSupportedRadioAccessFamily())
.thenReturn(TelephonyManager.NETWORK_TYPE_BITMASK_NR);
when(mSubscriptionManager.getActiveSubscriptionIdList()).thenReturn(new int[] {1});
when(mTelephonyManager.isDataEnabled()).thenReturn(true);
mController.init(SUB_ID);
assertThat(mController.isAvailable()).isFalse();
}
}

View File

@@ -1,167 +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.android.settings.core.BasePreferenceController.AVAILABLE;
import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE;
import static com.android.settings.network.telephony.MobileNetworkUtils.getRafFromNetworkType;
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.os.PersistableBundle;
import android.provider.Settings;
import android.telephony.CarrierConfigManager;
import android.telephony.ServiceState;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import androidx.preference.ListPreference;
import com.android.settings.R;
import com.android.settings.network.telephony.TelephonyConstants.TelephonyManagerConstants;
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;
@RunWith(RobolectricTestRunner.class)
public class PreferredNetworkModePreferenceControllerTest {
private static final int SUB_ID = 2;
@Mock
private TelephonyManager mTelephonyManager;
@Mock
private TelephonyManager mInvalidTelephonyManager;
@Mock
private CarrierConfigManager mCarrierConfigManager;
@Mock
private ServiceState mServiceState;
private PersistableBundle mPersistableBundle;
private PreferredNetworkModePreferenceController mController;
private ListPreference mPreference;
private Context mContext;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
doReturn(mTelephonyManager).when(mContext).getSystemService(Context.TELEPHONY_SERVICE);
doReturn(mTelephonyManager).when(mContext).getSystemService(TelephonyManager.class);
doReturn(mCarrierConfigManager).when(mContext).getSystemService(CarrierConfigManager.class);
doReturn(mTelephonyManager).when(mTelephonyManager).createForSubscriptionId(SUB_ID);
doReturn(mInvalidTelephonyManager).when(mTelephonyManager).createForSubscriptionId(
SubscriptionManager.INVALID_SUBSCRIPTION_ID);
doReturn(mServiceState).when(mTelephonyManager).getServiceState();
mPersistableBundle = new PersistableBundle();
doReturn(mPersistableBundle).when(mCarrierConfigManager).getConfigForSubId(SUB_ID);
mPreference = new ListPreference(mContext);
mPreference.setEntries(R.array.preferred_network_mode_choices);
mPreference.setEntryValues(R.array.preferred_network_mode_values);
mController = new PreferredNetworkModePreferenceController(mContext, "mobile_data");
mController.init(SUB_ID);
mPreference.setKey(mController.getPreferenceKey());
}
@Test
public void getAvailabilityStatus_hideCarrierNetworkSettings_returnUnavailable() {
mPersistableBundle.putBoolean(CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL,
true);
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
}
@Test
public void getAvailabilityStatus_worldPhone_returnAvailable() {
mPersistableBundle.putBoolean(CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL,
false);
mPersistableBundle.putBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL, true);
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
}
@Test
public void getAvailabilityStatus_hidePreferredNetworkType_returnUnavailable() {
mPersistableBundle.putBoolean(CarrierConfigManager.KEY_HIDE_PREFERRED_NETWORK_TYPE_BOOL,
true);
when(mServiceState.getState()).thenReturn(ServiceState.STATE_OUT_OF_SERVICE);
when(mServiceState.getDataRegistrationState()).thenReturn(
ServiceState.STATE_OUT_OF_SERVICE);
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
when(mServiceState.getState()).thenReturn(ServiceState.STATE_IN_SERVICE);
when(mServiceState.getDataRegistrationState()).thenReturn(ServiceState.STATE_IN_SERVICE);
when(mServiceState.getRoaming()).thenReturn(false);
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
when(mServiceState.getRoaming()).thenReturn(true);
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
}
@Test
public void updateState_updateByNetworkMode() {
Settings.Global.putInt(mContext.getContentResolver(),
Settings.Global.PREFERRED_NETWORK_MODE + SUB_ID,
TelephonyManagerConstants.NETWORK_MODE_TDSCDMA_GSM_WCDMA);
mController.updateState(mPreference);
assertThat(mPreference.getValue()).isEqualTo(
String.valueOf(TelephonyManagerConstants.NETWORK_MODE_TDSCDMA_GSM_WCDMA));
assertThat(mPreference.getSummary()).isEqualTo(
mContext.getString(R.string.preferred_network_mode_tdscdma_gsm_wcdma_summary));
}
@Test
public void onPreferenceChange_updateSuccess() {
doReturn(true).when(mTelephonyManager).setPreferredNetworkTypeBitmask(
getRafFromNetworkType(TelephonyManagerConstants.NETWORK_MODE_LTE_TDSCDMA));
mController.onPreferenceChange(mPreference,
String.valueOf(TelephonyManagerConstants.NETWORK_MODE_LTE_TDSCDMA));
assertThat(Settings.Global.getInt(mContext.getContentResolver(),
Settings.Global.PREFERRED_NETWORK_MODE + SUB_ID, 0)).isEqualTo(
TelephonyManagerConstants.NETWORK_MODE_LTE_TDSCDMA);
}
@Test
public void onPreferenceChange_updateFail() {
doReturn(false).when(mTelephonyManager).setPreferredNetworkTypeBitmask(
getRafFromNetworkType(TelephonyManagerConstants.NETWORK_MODE_LTE_TDSCDMA));
mController.onPreferenceChange(mPreference,
String.valueOf(TelephonyManagerConstants.NETWORK_MODE_LTE_TDSCDMA));
assertThat(Settings.Global.getInt(mContext.getContentResolver(),
Settings.Global.PREFERRED_NETWORK_MODE + SUB_ID, 0)).isNotEqualTo(
TelephonyManagerConstants.NETWORK_MODE_LTE_TDSCDMA);
}
}

View File

@@ -1,164 +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.ArgumentMatchers.anyBoolean;
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.os.PersistableBundle;
import android.telephony.CarrierConfigManager;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.RestrictedSwitchPreference;
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;
@RunWith(RobolectricTestRunner.class)
public class RoamingPreferenceControllerTest {
private static final int SUB_ID = 2;
@Mock
private FragmentManager mFragmentManager;
@Mock
private TelephonyManager mTelephonyManager;
@Mock
private TelephonyManager mInvalidTelephonyManager;
@Mock
private SubscriptionManager mSubscriptionManager;
@Mock
private FragmentTransaction mFragmentTransaction;
@Mock
private CarrierConfigManager mCarrierConfigManager;
private RoamingPreferenceController mController;
private RestrictedSwitchPreference mPreference;
private Context mContext;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
doReturn(mTelephonyManager).when(mContext).getSystemService(Context.TELEPHONY_SERVICE);
doReturn(mSubscriptionManager).when(mContext).getSystemService(SubscriptionManager.class);
doReturn(mCarrierConfigManager).when(mContext).getSystemService(CarrierConfigManager.class);
doReturn(mTelephonyManager).when(mTelephonyManager).createForSubscriptionId(SUB_ID);
doReturn(mInvalidTelephonyManager).when(mTelephonyManager).createForSubscriptionId(
SubscriptionManager.INVALID_SUBSCRIPTION_ID);
doReturn(mFragmentTransaction).when(mFragmentManager).beginTransaction();
mPreference = spy(new RestrictedSwitchPreference(mContext));
mController = spy(new RoamingPreferenceController(mContext, "roaming"));
mController.init(mFragmentManager, SUB_ID);
mPreference.setKey(mController.getPreferenceKey());
}
@Test
public void getAvailabilityStatus_validSubId_returnAvailable() {
assertThat(mController.getAvailabilityStatus()).isEqualTo(
BasePreferenceController.AVAILABLE);
}
@Test
public void getAvailabilityStatus_invalidSubId_returnUnsearchable() {
mController.init(mFragmentManager, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
assertThat(mController.getAvailabilityStatus()).isEqualTo(
BasePreferenceController.AVAILABLE_UNSEARCHABLE);
}
@Test
public void isDialogNeeded_roamingDisabledWithoutFlag_returnTrue() {
final PersistableBundle bundle = new PersistableBundle();
bundle.putBoolean(CarrierConfigManager.KEY_DISABLE_CHARGE_INDICATION_BOOL, false);
doReturn(bundle).when(mCarrierConfigManager).getConfigForSubId(SUB_ID);
doReturn(false).when(mTelephonyManager).isDataRoamingEnabled();
assertThat(mController.isDialogNeeded()).isTrue();
}
@Test
public void isDialogNeeded_roamingEnabled_returnFalse() {
doReturn(true).when(mTelephonyManager).isDataRoamingEnabled();
assertThat(mController.isDialogNeeded()).isFalse();
}
@Test
public void setChecked_needDialog_showDialog() {
doReturn(true).when(mController).isDialogNeeded();
mController.setChecked(true);
verify(mFragmentManager).beginTransaction();
}
@Test
public void updateState_invalidSubId_disabled() {
mController.init(mFragmentManager, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
mController.updateState(mPreference);
assertThat(mPreference.isEnabled()).isFalse();
}
@Test
public void updateState_validSubId_enabled() {
doReturn(true).when(mTelephonyManager).isDataRoamingEnabled();
mController.updateState(mPreference);
assertThat(mPreference.isEnabled()).isTrue();
assertThat(mPreference.isChecked()).isTrue();
}
@Test
public void updateState_isNotDisabledByAdmin_shouldInvokeSetEnabled() {
when(mPreference.isDisabledByAdmin()).thenReturn(false);
mController.updateState(mPreference);
verify(mPreference).setEnabled(anyBoolean());
}
@Test
public void updateState_isDisabledByAdmin_shouldNotInvokeSetEnabled() {
when(mPreference.isDisabledByAdmin()).thenReturn(true);
mController.updateState(mPreference);
verify(mPreference, never()).setEnabled(anyBoolean());
}
}

View File

@@ -1,98 +0,0 @@
/*
* Copyright (C) 2019 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.spy;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
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 TelephonyBasePreferenceControllerTest {
private static final int VALID_SUB_ID = 1;
@Mock
private SubscriptionManager mSubscriptionManager;
@Mock
private SubscriptionInfo mSubscriptionInfo;
private TestPreferenceController mPreferenceController;
private Context mContext;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
when(mContext.getSystemService(SubscriptionManager.class))
.thenReturn(mSubscriptionManager);
when(mSubscriptionInfo.getSubscriptionId()).thenReturn(VALID_SUB_ID);
mPreferenceController = new TestPreferenceController(mContext, "prefKey");
}
@Test
public void isAvailable_validSubIdSet_returnTrue() {
mPreferenceController.init(VALID_SUB_ID);
assertThat(mPreferenceController.isAvailable()).isTrue();
}
@Test
public void isAvailable_noIdSetHoweverHasDefaultOne_returnTrue() {
when(mSubscriptionManager.getActiveSubscriptionInfoList()).thenReturn(
Arrays.asList(mSubscriptionInfo));
assertThat(mPreferenceController.isAvailable()).isTrue();
}
@Test
public void isAvailable_noDefaultAndNoSet_returnFalse() {
assertThat(mPreferenceController.isAvailable()).isFalse();
}
/**
* Test preference controller for {@link TelephonyBasePreferenceController}
*/
public class TestPreferenceController extends TelephonyBasePreferenceController {
public TestPreferenceController(Context context, String prefKey) {
super(context, prefKey);
}
public void init(int subId) {
mSubId = subId;
}
@Override
public int getAvailabilityStatus(int subId) {
return subId == VALID_SUB_ID ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
}
}
}

View File

@@ -1,67 +0,0 @@
/*
* 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 org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class TelephonyTogglePreferenceControllerTest {
private Context mContext;
private FakeTelephonyToggle mFakeTelephonyToggle;
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
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;
}
}
}

View File

@@ -1,70 +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.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 android.content.Context;
import android.telephony.TelephonyManager;
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;
@RunWith(RobolectricTestRunner.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(RuntimeEnvironment.application);
doReturn(mTelephonyManager).when(mContext).getSystemService(Context.TELEPHONY_SERVICE);
doReturn(mTelephonyManager).when(mContext).getSystemService(TelephonyManager.class);
doReturn(mTelephonyManager).when(mTelephonyManager).createForSubscriptionId(SUB_ID);
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);
}
}

View File

@@ -1,130 +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.cdma;
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.os.PersistableBundle;
import android.os.SystemProperties;
import android.provider.Settings;
import android.telephony.CarrierConfigManager;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import androidx.preference.ListPreference;
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;
@RunWith(RobolectricTestRunner.class)
public class CdmaSubscriptionPreferenceControllerTest {
private static final int SUB_ID = 2;
@Mock
private PreferenceManager mPreferenceManager;
@Mock
private TelephonyManager mTelephonyManager;
@Mock
private TelephonyManager mInvalidTelephonyManager;
@Mock
private SubscriptionManager mSubscriptionManager;
@Mock
private CarrierConfigManager mCarrierConfigManager;
private CdmaSubscriptionPreferenceController mController;
private ListPreference mPreference;
private PersistableBundle mCarrierConfig;
private Context mContext;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
doReturn(mTelephonyManager).when(mContext).getSystemService(Context.TELEPHONY_SERVICE);
doReturn(mSubscriptionManager).when(mContext).getSystemService(SubscriptionManager.class);
doReturn(mTelephonyManager).when(mTelephonyManager).createForSubscriptionId(SUB_ID);
doReturn(mInvalidTelephonyManager).when(mTelephonyManager).createForSubscriptionId(
SubscriptionManager.INVALID_SUBSCRIPTION_ID);
doReturn(mCarrierConfigManager).when(mContext).getSystemService(CarrierConfigManager.class);
mCarrierConfig = new PersistableBundle();
when(mCarrierConfigManager.getConfigForSubId(SUB_ID)).thenReturn(mCarrierConfig);
mPreference = new ListPreference(mContext);
mController = new CdmaSubscriptionPreferenceController(mContext, "mobile_data");
mController.init(mPreferenceManager, SUB_ID);
mController.mPreference = mPreference;
mPreference.setKey(mController.getPreferenceKey());
}
@Test
public void onPreferenceChange_selectNV_returnNVMode() {
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();
}
}

View File

@@ -1,145 +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.cdma;
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.os.PersistableBundle;
import android.provider.Settings;
import android.telephony.CarrierConfigManager;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import androidx.preference.ListPreference;
import androidx.preference.PreferenceManager;
import com.android.settings.network.telephony.TelephonyConstants.TelephonyManagerConstants;
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;
@RunWith(RobolectricTestRunner.class)
public class CdmaSystemSelectPreferenceControllerTest {
private static final int SUB_ID = 2;
@Mock
private PreferenceManager mPreferenceManager;
@Mock
private TelephonyManager mTelephonyManager;
@Mock
private TelephonyManager mInvalidTelephonyManager;
@Mock
private SubscriptionManager mSubscriptionManager;
@Mock
private CarrierConfigManager mCarrierConfigManager;
private CdmaSystemSelectPreferenceController mController;
private ListPreference mPreference;
private PersistableBundle mCarrierConfig;
private Context mContext;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
doReturn(mTelephonyManager).when(mContext).getSystemService(Context.TELEPHONY_SERVICE);
doReturn(mSubscriptionManager).when(mContext).getSystemService(SubscriptionManager.class);
doReturn(mTelephonyManager).when(mTelephonyManager).createForSubscriptionId(SUB_ID);
doReturn(mInvalidTelephonyManager).when(mTelephonyManager).createForSubscriptionId(
SubscriptionManager.INVALID_SUBSCRIPTION_ID);
doReturn(mCarrierConfigManager).when(mContext).getSystemService(CarrierConfigManager.class);
mCarrierConfig = new PersistableBundle();
when(mCarrierConfigManager.getConfigForSubId(SUB_ID)).thenReturn(mCarrierConfig);
mPreference = new ListPreference(mContext);
mController = new CdmaSystemSelectPreferenceController(mContext, "mobile_data");
mController.init(mPreferenceManager, SUB_ID);
mController.mPreference = mPreference;
mPreference.setKey(mController.getPreferenceKey());
}
@Test
public void onPreferenceChange_selectHome_returnHomeMode() {
Settings.Global.putInt(mContext.getContentResolver(),
Settings.Global.CDMA_ROAMING_MODE,
TelephonyManager.CDMA_ROAMING_MODE_ANY);
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);
}
}

View File

@@ -1,136 +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.gsm;
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.verify;
import static org.mockito.Mockito.when;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.PersistableBundle;
import android.telephony.CarrierConfigManager;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.SwitchPreference;
import com.android.settings.R;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class AutoSelectPreferenceControllerTest {
private static final int SUB_ID = 2;
private static final String OPERATOR_NAME = "T-mobile";
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private TelephonyManager mTelephonyManager;
@Mock
private SubscriptionManager mSubscriptionManager;
@Mock
private CarrierConfigManager mCarrierConfigManager;
@Mock
private ProgressDialog mProgressDialog;
private PersistableBundle mCarrierConfig;
private AutoSelectPreferenceController mController;
private SwitchPreference mSwitchPreference;
private Context mContext;
private LifecycleOwner mLifecycleOwner;
private Lifecycle mLifecycle;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
mLifecycleOwner = () -> mLifecycle;
mLifecycle = new Lifecycle(mLifecycleOwner);
when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
when(mContext.getSystemService(SubscriptionManager.class)).thenReturn(mSubscriptionManager);
when(mContext.getSystemService(CarrierConfigManager.class)).thenReturn(
mCarrierConfigManager);
when(mTelephonyManager.createForSubscriptionId(SUB_ID)).thenReturn(mTelephonyManager);
mCarrierConfig = new PersistableBundle();
mCarrierConfig.putBoolean(CarrierConfigManager.KEY_ONLY_AUTO_SELECT_IN_HOME_NETWORK_BOOL,
true);
when(mCarrierConfigManager.getConfigForSubId(SUB_ID)).thenReturn(mCarrierConfig);
mSwitchPreference = new SwitchPreference(mContext);
mController = new AutoSelectPreferenceController(mContext, "auto_select");
mController.mProgressDialog = mProgressDialog;
mController.mSwitchPreference = mSwitchPreference;
mController.init(mLifecycle, SUB_ID);
}
@Test
public void setChecked_isChecked_showProgressDialog() {
when(mTelephonyManager.getNetworkSelectionMode()).thenReturn(
TelephonyManager.NETWORK_SELECTION_MODE_AUTO);
assertThat(mController.setChecked(true)).isFalse();
Robolectric.flushBackgroundThreadScheduler();
verify(mProgressDialog).show();
verify(mTelephonyManager).setNetworkSelectionModeAutomatic();
}
@Test
public void updateState_isRoaming_enabled() {
when(mTelephonyManager.getServiceState().getRoaming()).thenReturn(true);
mController.updateState(mSwitchPreference);
assertThat(mSwitchPreference.isEnabled()).isTrue();
}
@Test
public void updateState_notRoamingWithAutoSelectOn_disabled() {
when(mTelephonyManager.getServiceState().getRoaming()).thenReturn(false);
doReturn(OPERATOR_NAME).when(mTelephonyManager).getSimOperatorName();
mController.updateState(mSwitchPreference);
assertThat(mSwitchPreference.isEnabled()).isFalse();
assertThat(mSwitchPreference.getSummary()).isEqualTo(
mContext.getString(R.string.manual_mode_disallowed_summary,
mTelephonyManager.getSimOperatorName()));
}
@Test
public void init_carrierConfigNull_shouldNotCrash() {
when(mCarrierConfigManager.getConfigForSubId(SUB_ID)).thenReturn(null);
// Should not crash
mController.init(mLifecycle, SUB_ID);
}
}

View File

@@ -1,128 +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.gsm;
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.PersistableBundle;
import android.telephony.CarrierConfigManager;
import android.telephony.ServiceState;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.Preference;
import com.android.settings.R;
import com.android.settingslib.core.lifecycle.Lifecycle;
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 OpenNetworkSelectPagePreferenceControllerTest {
private static final int SUB_ID = 2;
private static final String OPERATOR_NAME = "T-mobile";
@Mock
private TelephonyManager mTelephonyManager;
@Mock
private SubscriptionManager mSubscriptionManager;
@Mock
private CarrierConfigManager mCarrierConfigManager;
@Mock
private ServiceState mServiceState;
@Mock
private SubscriptionInfo mSubscriptionInfo;
private PersistableBundle mCarrierConfig;
private OpenNetworkSelectPagePreferenceController mController;
private Preference mPreference;
private Context mContext;
private LifecycleOwner mLifecycleOwner;
private Lifecycle mLifecycle;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
mLifecycleOwner = () -> mLifecycle;
mLifecycle = new Lifecycle(mLifecycleOwner);
when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
when(mContext.getSystemService(SubscriptionManager.class)).thenReturn(mSubscriptionManager);
when(mContext.getSystemService(CarrierConfigManager.class)).thenReturn(
mCarrierConfigManager);
when(mTelephonyManager.createForSubscriptionId(SUB_ID)).thenReturn(mTelephonyManager);
when(mTelephonyManager.getServiceState()).thenReturn(mServiceState);
mCarrierConfig = new PersistableBundle();
when(mCarrierConfigManager.getConfigForSubId(SUB_ID)).thenReturn(mCarrierConfig);
when(mSubscriptionInfo.getSubscriptionId()).thenReturn(SUB_ID);
when(mSubscriptionInfo.getCarrierName()).thenReturn(OPERATOR_NAME);
when(mSubscriptionManager.getActiveSubscriptionInfoList()).thenReturn(
Arrays.asList(mSubscriptionInfo));
when(mSubscriptionManager.getAccessibleSubscriptionInfoList()).thenReturn(
Arrays.asList(mSubscriptionInfo));
when(mTelephonyManager.getNetworkOperatorName()).thenReturn(OPERATOR_NAME);
mPreference = new Preference(mContext);
mController = new OpenNetworkSelectPagePreferenceController(mContext,
"open_network_select");
mController.init(mLifecycle, SUB_ID);
}
@Test
public void updateState_modeAuto_disabled() {
when(mTelephonyManager.getNetworkSelectionMode()).thenReturn(
TelephonyManager.NETWORK_SELECTION_MODE_AUTO);
mController.updateState(mPreference);
assertThat(mPreference.isEnabled()).isFalse();
}
@Test
public void getSummary_inService_returnOperatorName() {
when(mServiceState.getState()).thenReturn(ServiceState.STATE_IN_SERVICE);
assertThat(mController.getSummary()).isEqualTo(OPERATOR_NAME);
}
@Test
public void getSummary_notInService_returnDisconnect() {
when(mServiceState.getState()).thenReturn(ServiceState.STATE_OUT_OF_SERVICE);
assertThat(mController.getSummary()).isEqualTo(
mContext.getString(R.string.network_disconnected));
}
}