Add ethernet tether option into AllInOneTetherSettings

Create controller for ethernet tether preference in
AllInOneTetherSettings and corresponding test.

Test: AllInOneTetherSettingsTest; UsbTetherPreferenceControllerTest;
BluetoothTetherPreferenceControllerTest;
EthernetTetherPreferenceControllerTest;
WifiTetherDisablePreferenceControllerTest; TetherEnablerTest
Bug: 153690620

Change-Id: I8918d5c8a82c521b00eb3c712af80c2041778595
This commit is contained in:
Zhen Zhang
2020-04-13 17:02:07 -07:00
parent 12a838d1f6
commit c10ec56e0f
17 changed files with 823 additions and 317 deletions

View File

@@ -16,12 +16,13 @@
package com.android.settings;
import static com.android.settings.AllInOneTetherSettings.BLUETOOTH_TETHER_KEY;
import static com.android.settings.AllInOneTetherSettings.ETHERNET_TETHER_KEY;
import static com.android.settings.AllInOneTetherSettings.EXPANDED_CHILD_COUNT_DEFAULT;
import static com.android.settings.AllInOneTetherSettings.EXPANDED_CHILD_COUNT_WITHOUT_WIFI_CONFIG;
import static com.android.settings.AllInOneTetherSettings.EXPANDED_CHILD_COUNT_MAX;
import static com.android.settings.AllInOneTetherSettings.EXPANDED_CHILD_COUNT_WITH_SECURITY_NON;
import static com.android.settings.network.TetherEnabler.BLUETOOTH_TETHER_KEY;
import static com.android.settings.network.TetherEnabler.USB_TETHER_KEY;
import static com.android.settings.network.TetherEnabler.WIFI_TETHER_DISABLE_KEY;
import static com.android.settings.AllInOneTetherSettings.USB_TETHER_KEY;
import static com.android.settings.AllInOneTetherSettings.WIFI_TETHER_DISABLE_KEY;
import static com.google.common.truth.Truth.assertThat;
@@ -37,6 +38,9 @@ import android.os.UserHandle;
import android.os.UserManager;
import android.util.FeatureFlagUtils;
import androidx.preference.PreferenceGroup;
import androidx.preference.PreferenceScreen;
import com.android.settings.core.FeatureFlags;
import com.android.settings.testutils.shadow.ShadowWifiManager;
import com.android.settings.wifi.tether.WifiTetherAutoOffPreferenceController;
@@ -62,6 +66,7 @@ public class AllInOneTetherSettingsTest {
private static final String[] WIFI_REGEXS = {"wifi_regexs"};
private static final String[] USB_REGEXS = {"usb_regexs"};
private static final String[] BT_REGEXS = {"bt_regexs"};
private static final String[] ETHERNET_REGEXS = {"ethernet_regexs"};
private Context mContext;
private AllInOneTetherSettings mAllInOneTetherSettings;
@@ -72,6 +77,10 @@ public class AllInOneTetherSettingsTest {
private UserManager mUserManager;
@Mock
private WifiTetherSecurityPreferenceController mSecurityPreferenceController;
@Mock
private PreferenceScreen mPreferenceScreen;
@Mock
private PreferenceGroup mWifiTetherGroup;
@Before
public void setUp() {
@@ -83,14 +92,16 @@ public class AllInOneTetherSettingsTest {
doReturn(WIFI_REGEXS).when(mConnectivityManager).getTetherableWifiRegexs();
doReturn(USB_REGEXS).when(mConnectivityManager).getTetherableUsbRegexs();
doReturn(BT_REGEXS).when(mConnectivityManager).getTetherableBluetoothRegexs();
doReturn(ETHERNET_REGEXS).when(mConnectivityManager).getTetherableIfaces();
doReturn(mUserManager).when(mContext).getSystemService(Context.USER_SERVICE);
// Assume the feature is enabled for most test cases.
FeatureFlagUtils.setEnabled(mContext, FeatureFlags.TETHER_ALL_IN_ONE, true);
mAllInOneTetherSettings = new AllInOneTetherSettings();
mAllInOneTetherSettings = spy(new AllInOneTetherSettings());
doReturn(mPreferenceScreen).when(mAllInOneTetherSettings).getPreferenceScreen();
ReflectionHelpers.setField(mAllInOneTetherSettings, "mLifecycle", mock(Lifecycle.class));
ReflectionHelpers.setField(mAllInOneTetherSettings, "mSecurityPreferenceController",
mSecurityPreferenceController);
ReflectionHelpers.setField(mAllInOneTetherSettings, "mWifiTetherGroup", mWifiTetherGroup);
}
@Test
@@ -110,6 +121,7 @@ public class AllInOneTetherSettingsTest {
assertThat(niks).doesNotContain(AllInOneTetherSettings.KEY_WIFI_TETHER_SECURITY);
assertThat(niks).doesNotContain(BLUETOOTH_TETHER_KEY);
assertThat(niks).doesNotContain(USB_TETHER_KEY);
assertThat(niks).doesNotContain(ETHERNET_TETHER_KEY);
// This key should be returned because it's not visible by default.
assertThat(niks).contains(WIFI_TETHER_DISABLE_KEY);
@@ -131,6 +143,7 @@ public class AllInOneTetherSettingsTest {
assertThat(niks).contains(WIFI_TETHER_DISABLE_KEY);
assertThat(niks).contains(BLUETOOTH_TETHER_KEY);
assertThat(niks).contains(USB_TETHER_KEY);
assertThat(niks).contains(ETHERNET_TETHER_KEY);
}
@Test
@@ -149,6 +162,7 @@ public class AllInOneTetherSettingsTest {
assertThat(niks).contains(WIFI_TETHER_DISABLE_KEY);
assertThat(niks).doesNotContain(BLUETOOTH_TETHER_KEY);
assertThat(niks).doesNotContain(USB_TETHER_KEY);
assertThat(niks).doesNotContain(ETHERNET_TETHER_KEY);
}
@Test
@@ -167,29 +181,31 @@ public class AllInOneTetherSettingsTest {
}
@Test
public void getInitialExpandedChildCount_shouldShowWifiConfigWithSecurity() {
ReflectionHelpers.setField(mAllInOneTetherSettings, "mWifiTethering", true);
public void getInitialChildCount_withSecurity() {
when(mSecurityPreferenceController.getSecurityType())
.thenReturn(SoftApConfiguration.SECURITY_TYPE_WPA2_PSK);
assertThat(mAllInOneTetherSettings.getInitialExpandedChildCount())
.isEqualTo(EXPANDED_CHILD_COUNT_DEFAULT);
assertThat(mAllInOneTetherSettings.getInitialExpandedChildCount()).isEqualTo(
EXPANDED_CHILD_COUNT_DEFAULT);
}
@Test
public void getInitialExpandedChildCount_shouldShowWifiConfigWithoutSecurity() {
ReflectionHelpers.setField(mAllInOneTetherSettings, "mWifiTethering", true);
public void getInitialChildCount_withoutSecurity() {
when(mSecurityPreferenceController.getSecurityType())
.thenReturn(SoftApConfiguration.SECURITY_TYPE_OPEN);
assertThat(mAllInOneTetherSettings.getInitialExpandedChildCount())
.isEqualTo(EXPANDED_CHILD_COUNT_WITH_SECURITY_NON);
assertThat(mAllInOneTetherSettings.getInitialExpandedChildCount()).isEqualTo(
EXPANDED_CHILD_COUNT_WITH_SECURITY_NON);
}
@Test
public void getInitialExpandedChildCount_shouldNotShowWifiConfig() {
ReflectionHelpers.setField(mAllInOneTetherSettings, "mWifiTethering", false);
ReflectionHelpers.setField(mAllInOneTetherSettings, "mBluetoothTethering", true);
public void getInitialExpandedChildCount_expandAllChild() {
assertThat(mAllInOneTetherSettings.getInitialExpandedChildCount())
.isEqualTo(EXPANDED_CHILD_COUNT_WITHOUT_WIFI_CONFIG);
.isNotEqualTo(EXPANDED_CHILD_COUNT_MAX);
ReflectionHelpers.setField(mAllInOneTetherSettings, "mShouldShowWifiConfig", false);
assertThat(mAllInOneTetherSettings.getInitialExpandedChildCount())
.isEqualTo(EXPANDED_CHILD_COUNT_MAX);
ReflectionHelpers.setField(mAllInOneTetherSettings, "mShouldShowWifiConfig", true);
assertThat(mAllInOneTetherSettings.getInitialExpandedChildCount())
.isEqualTo(EXPANDED_CHILD_COUNT_MAX);
}
private void setupIsTetherAvailable(boolean returnValue) {

View File

@@ -16,6 +16,14 @@
package com.android.settings.network;
import static com.android.settings.network.TetherEnabler.TETHERING_BLUETOOTH_ON;
import static com.android.settings.network.TetherEnabler.TETHERING_ETHERNET_ON;
import static com.android.settings.network.TetherEnabler.TETHERING_OFF;
import static com.android.settings.network.TetherEnabler.TETHERING_USB_ON;
import static com.android.settings.network.TetherEnabler.TETHERING_WIFI_ON;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
@@ -27,6 +35,7 @@ import android.bluetooth.BluetoothPan;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import com.android.settings.R;
import com.android.settings.widget.MasterSwitchPreference;
import org.junit.Before;
@@ -34,14 +43,72 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.ParameterizedRobolectricTestRunner;
import org.robolectric.util.ReflectionHelpers;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
@RunWith(RobolectricTestRunner.class)
@RunWith(ParameterizedRobolectricTestRunner.class)
public class AllInOneTetherPreferenceControllerTest {
@ParameterizedRobolectricTestRunner.Parameters(name = "TetherState: {0}")
public static List params() {
return Arrays.asList(new Object[][] {
{TETHERING_OFF, R.string.tether_settings_summary_off},
{TETHERING_WIFI_ON, R.string.tether_settings_summary_hotspot_only},
{TETHERING_USB_ON, R.string.tether_settings_summary_usb_tethering_only},
{TETHERING_BLUETOOTH_ON, R.string.tether_settings_summary_bluetooth_tethering_only},
{TETHERING_ETHERNET_ON, R.string.tether_settings_summary_ethernet_tethering_only},
{
TETHERING_WIFI_ON | TETHERING_USB_ON,
R.string.tether_settings_summary_hotspot_and_usb
},
{
TETHERING_WIFI_ON | TETHERING_BLUETOOTH_ON,
R.string.tether_settings_summary_hotspot_and_bluetooth
},
{
TETHERING_WIFI_ON | TETHERING_ETHERNET_ON,
R.string.tether_settings_summary_hotspot_and_ethernet
},
{
TETHERING_USB_ON | TETHERING_BLUETOOTH_ON,
R.string.tether_settings_summary_usb_and_bluetooth
},
{
TETHERING_USB_ON | TETHERING_ETHERNET_ON,
R.string.tether_settings_summary_usb_and_ethernet
},
{
TETHERING_BLUETOOTH_ON | TETHERING_ETHERNET_ON,
R.string.tether_settings_summary_bluetooth_and_ethernet
},
{
TETHERING_WIFI_ON | TETHERING_USB_ON | TETHERING_BLUETOOTH_ON,
R.string.tether_settings_summary_hotspot_and_usb_and_bluetooth
},
{
TETHERING_WIFI_ON | TETHERING_USB_ON | TETHERING_ETHERNET_ON,
R.string.tether_settings_summary_hotspot_and_usb_and_ethernet
},
{
TETHERING_WIFI_ON | TETHERING_BLUETOOTH_ON | TETHERING_ETHERNET_ON,
R.string.tether_settings_summary_hotspot_and_bluetooth_and_ethernet
},
{
TETHERING_USB_ON | TETHERING_BLUETOOTH_ON | TETHERING_ETHERNET_ON,
R.string.tether_settings_summary_usb_and_bluetooth_and_ethernet
},
{
TETHERING_WIFI_ON | TETHERING_USB_ON | TETHERING_BLUETOOTH_ON
| TETHERING_ETHERNET_ON,
R.string.tether_settings_summary_all
}
});
}
@Mock
private Context mContext;
@Mock
@@ -50,6 +117,13 @@ public class AllInOneTetherPreferenceControllerTest {
private MasterSwitchPreference mPreference;
private AllInOneTetherPreferenceController mController;
private final int mTetherState;
private final int mSummaryResId;
public AllInOneTetherPreferenceControllerTest(int tetherState, int summaryResId) {
mTetherState = tetherState;
mSummaryResId = summaryResId;
}
@Before
public void setUp() {
@@ -90,4 +164,10 @@ public class AllInOneTetherPreferenceControllerTest {
verify(mBluetoothAdapter).closeProfileProxy(BluetoothProfile.PAN, pan);
}
@Test
public void getSummary_afterTetherStateChanged() {
mController.onTetherStateUpdated(mTetherState);
assertThat(mController.getSummary()).isEqualTo(mContext.getString(mSummaryResId));
}
}

View File

@@ -16,8 +16,6 @@
package com.android.settings.network;
import static com.android.settings.network.TetherEnabler.BLUETOOTH_TETHER_KEY;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
@@ -26,6 +24,7 @@ import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.TetheringManager;
@@ -62,7 +61,7 @@ public class BluetoothTetherPreferenceControllerTest {
when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(
mConnectivityManager);
when(mConnectivityManager.getTetherableBluetoothRegexs()).thenReturn(new String[] {""});
mController = new BluetoothTetherPreferenceController(mContext, BLUETOOTH_TETHER_KEY);
mController = new BluetoothTetherPreferenceController(mContext, "BLUETOOTH");
mController.setTetherEnabler(mTetherEnabler);
ReflectionHelpers.setField(mController, "mPreference", mSwitchPreference);
}
@@ -98,14 +97,17 @@ public class BluetoothTetherPreferenceControllerTest {
}
@Test
public void display_availableChangedCorrectly() {
when(mConnectivityManager.getTetherableBluetoothRegexs()).thenReturn(new String[] {""});
assertThat(mController.isAvailable()).isTrue();
public void shouldShow_noBluetoothTetherable() {
when(mConnectivityManager.getTetherableBluetoothRegexs()).thenReturn(new String[0]);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void shouldEnable_transientState() {
ReflectionHelpers.setField(mController, "mBluetoothState",
BluetoothAdapter.STATE_TURNING_OFF);
assertThat(mController.shouldEnable()).isFalse();
}
@Test
public void setChecked_shouldStartBluetoothTethering() {
mController.setChecked(true);

View File

@@ -0,0 +1,141 @@
/*
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.network;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.eq;
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.EthernetManager;
import android.net.TetheringManager;
import androidx.preference.SwitchPreference;
import androidx.test.core.app.ApplicationProvider;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class EthernetTetherPreferenceControllerTest {
@Rule
public MockitoRule mocks = MockitoJUnit.rule();
@Mock
private ConnectivityManager mConnectivityManager;
@Mock
private EthernetManager mEthernetManager;
@Mock
private TetherEnabler mTetherEnabler;
private Context mContext;
private EthernetTetherPreferenceController mController;
private SwitchPreference mPreference;
private static final String ETHERNET_REGEX = "ethernet";
@Before
public void setUp() {
mContext = spy(ApplicationProvider.getApplicationContext());
mPreference = spy(SwitchPreference.class);
when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE))
.thenReturn(mConnectivityManager);
when(mConnectivityManager.getTetherableIfaces()).thenReturn(new String[]{ETHERNET_REGEX});
when(mContext.getSystemService(Context.ETHERNET_SERVICE)).thenReturn(mEthernetManager);
mController = new EthernetTetherPreferenceController(mContext, "ethernet");
mController.setTetherEnabler(mTetherEnabler);
ReflectionHelpers.setField(mController, "mEthernetRegex", ETHERNET_REGEX);
ReflectionHelpers.setField(mController, "mPreference", mPreference);
}
@Test
public void lifecycle_shouldRegisterReceiverOnStart() {
mController.onStart();
verify(mEthernetManager).addListener(eq(mController.mEthernetListener));
}
@Test
public void lifecycle_shouldAddListenerOnResume() {
mController.onResume();
verify(mTetherEnabler).addListener(mController);
}
@Test
public void lifecycle_shouldRemoveListenerOnPause() {
mController.onPause();
verify(mTetherEnabler).removeListener(mController);
}
@Test
public void lifecycle_shouldUnregisterReceiverOnStop() {
mController.onStart();
EthernetManager.Listener listener = mController.mEthernetListener;
mController.onStop();
verify(mEthernetManager).removeListener(eq(listener));
assertThat(mController.mEthernetListener).isNull();
}
@Test
public void shouldEnable_noTetherable() {
when(mConnectivityManager.getTetherableIfaces()).thenReturn(new String[0]);
assertThat(mController.shouldEnable()).isFalse();
}
@Test
public void shouldShow_noEthernetInterface() {
ReflectionHelpers.setField(mController, "mEthernetRegex", "");
assertThat(mController.shouldShow()).isFalse();
}
@Test
public void setChecked_shouldStartEthernetTethering() {
mController.setChecked(true);
verify(mTetherEnabler).startTethering(TetheringManager.TETHERING_ETHERNET);
}
@Test
public void setUnchecked_shouldStopEthernetTethering() {
mController.setChecked(false);
verify(mTetherEnabler).stopTethering(TetheringManager.TETHERING_ETHERNET);
}
@Test
public void switch_shouldCheckedWhenEthernetTethering() {
mController.onTetherStateUpdated(TetherEnabler.TETHERING_ETHERNET_ON);
assertThat(mController.isChecked()).isTrue();
}
@Test
public void switch_shouldUnCheckedWhenEthernetNotTethering() {
mController.onTetherStateUpdated(TetherEnabler.TETHERING_OFF);
assertThat(mController.isChecked()).isFalse();
}
}

View File

@@ -16,6 +16,17 @@
package com.android.settings.network;
import static android.net.ConnectivityManager.TETHERING_BLUETOOTH;
import static android.net.ConnectivityManager.TETHERING_USB;
import static android.net.ConnectivityManager.TETHERING_WIFI;
import static android.net.TetheringManager.TETHERING_ETHERNET;
import static com.android.settings.network.TetherEnabler.TETHERING_BLUETOOTH_ON;
import static com.android.settings.network.TetherEnabler.TETHERING_ETHERNET_ON;
import static com.android.settings.network.TetherEnabler.TETHERING_OFF;
import static com.android.settings.network.TetherEnabler.TETHERING_USB_ON;
import static com.android.settings.network.TetherEnabler.TETHERING_WIFI_ON;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
@@ -63,6 +74,8 @@ public class TetherEnablerTest {
@Mock
private ConnectivityManager mConnectivityManager;
@Mock
private TetheringManager mTetheringManager;
@Mock
private NetworkPolicyManager mNetworkPolicyManager;
@Mock
private BluetoothPan mBluetoothPan;
@@ -85,6 +98,7 @@ public class TetherEnablerTest {
when(context.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifiManager);
when(context.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(
mConnectivityManager);
when(context.getSystemService(Context.TETHERING_SERVICE)).thenReturn(mTetheringManager);
when(context.getSystemService(Context.NETWORK_POLICY_SERVICE)).thenReturn(
mNetworkPolicyManager);
when(mConnectivityManager.getTetherableIfaces()).thenReturn(new String[0]);
@@ -104,6 +118,23 @@ public class TetherEnablerTest {
assertThat(mSwitchBar.isChecked()).isTrue();
}
@Test
public void lifecycle_onStart_shoudRegisterTetheringEventCallback() {
mEnabler.onStart();
verify(mTetheringManager).registerTetheringEventCallback(any(),
eq(mEnabler.mTetheringEventCallback));
}
@Test
public void lifecycle_onStop_shouldUnregisterTetheringEventCallback() {
mEnabler.onStart();
TetheringManager.TetheringEventCallback callback = mEnabler.mTetheringEventCallback;
mEnabler.onStop();
verify(mTetheringManager).unregisterTetheringEventCallback(callback);
assertThat(mEnabler.mTetheringEventCallback).isNull();
}
@Test
public void lifecycle_onStop_resetBluetoothTetheringStoppedByUser() {
mEnabler.onStart();
@@ -143,13 +174,40 @@ public class TetherEnablerTest {
@Test
public void onSwitchToggled_onlyStartsWifiTetherWhenNeeded() {
when(mWifiManager.isWifiApEnabled()).thenReturn(true);
doReturn(TETHERING_WIFI_ON).when(mEnabler).getTetheringState(null /* tethered */);
mEnabler.onSwitchToggled(true);
verify(mConnectivityManager, never()).startTethering(anyInt(), anyBoolean(), any(), any());
doReturn(false).when(mWifiManager).isWifiApEnabled();
doReturn(TETHERING_OFF).when(mEnabler).getTetheringState(null /* tethered */);
mEnabler.onSwitchToggled(true);
verify(mConnectivityManager).startTethering(anyInt(), anyBoolean(), any(), any());
verify(mConnectivityManager).startTethering(eq(TETHERING_WIFI), anyBoolean(), any(), any());
verify(mConnectivityManager, never()).startTethering(eq(TETHERING_USB), anyBoolean(), any(),
any());
verify(mConnectivityManager, never()).startTethering(eq(TETHERING_BLUETOOTH), anyBoolean(),
any(), any());
verify(mConnectivityManager, never()).startTethering(eq(TETHERING_ETHERNET), anyBoolean(),
any(), any());
}
@Test
public void onSwitchToggled_stopAllTetheringInterfaces() {
mEnabler.onStart();
doReturn(TETHERING_WIFI_ON).when(mEnabler).getTetheringState(null /* tethered */);
mEnabler.onSwitchToggled(false);
verify(mConnectivityManager).stopTethering(TETHERING_WIFI);
doReturn(TETHERING_USB_ON).when(mEnabler).getTetheringState(null /* tethered */);
mEnabler.onSwitchToggled(false);
verify(mConnectivityManager).stopTethering(TETHERING_USB);
doReturn(TETHERING_BLUETOOTH_ON).when(mEnabler).getTetheringState(null /* tethered */);
mEnabler.onSwitchToggled(false);
verify(mConnectivityManager).stopTethering(TETHERING_BLUETOOTH);
doReturn(TETHERING_ETHERNET_ON).when(mEnabler).getTetheringState(null /* tethered */);
mEnabler.onSwitchToggled(false);
verify(mConnectivityManager).stopTethering(TETHERING_ETHERNET);
}
@Test
@@ -169,8 +227,7 @@ public class TetherEnablerTest {
public void stopTethering_setBluetoothTetheringStoppedByUserAndUpdateState() {
mSwitchWidgetController.setListener(mEnabler);
mSwitchWidgetController.startListening();
int state = TetherEnabler.TETHERING_BLUETOOTH_ON;
doReturn(state).when(mEnabler).getTetheringState(null /* tethered */);
doReturn(TETHERING_BLUETOOTH_ON).when(mEnabler).getTetheringState(null /* tethered */);
mEnabler.stopTethering(TetheringManager.TETHERING_BLUETOOTH);
assertThat(mEnabler.mBluetoothTetheringStoppedByUser).isTrue();
@@ -238,4 +295,20 @@ public class TetherEnablerTest {
mEnabler.removeListener(listener);
assertThat(mEnabler.mListeners).doesNotContain(listener);
}
@Test
public void isTethering_shouldReturnCorrectly() {
assertThat(TetherEnabler.isTethering(TETHERING_WIFI_ON, TETHERING_WIFI)).isTrue();
assertThat(TetherEnabler.isTethering(~TETHERING_WIFI_ON, TETHERING_WIFI)).isFalse();
assertThat(TetherEnabler.isTethering(TETHERING_USB_ON, TETHERING_USB)).isTrue();
assertThat(TetherEnabler.isTethering(~TETHERING_USB_ON, TETHERING_USB)).isFalse();
assertThat(TetherEnabler.isTethering(TETHERING_BLUETOOTH_ON, TETHERING_BLUETOOTH)).isTrue();
assertThat(TetherEnabler.isTethering(~TETHERING_BLUETOOTH_ON, TETHERING_BLUETOOTH))
.isFalse();
assertThat(TetherEnabler.isTethering(TETHERING_ETHERNET_ON, TETHERING_ETHERNET)).isTrue();
assertThat(TetherEnabler.isTethering(~TETHERING_ETHERNET_ON, TETHERING_ETHERNET)).isFalse();
}
}

View File

@@ -16,8 +16,6 @@
package com.android.settings.network;
import static com.android.settings.network.TetherEnabler.USB_TETHER_KEY;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
@@ -61,7 +59,7 @@ public class UsbTetherPreferenceControllerTest {
when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(
mConnectivityManager);
when(mConnectivityManager.getTetherableUsbRegexs()).thenReturn(new String[]{""});
mController = new UsbTetherPreferenceController(mContext, USB_TETHER_KEY);
mController = new UsbTetherPreferenceController(mContext, "USB");
mController.setTetherEnabler(mTetherEnabler);
mSwitchPreference = spy(SwitchPreference.class);
ReflectionHelpers.setField(mController, "mPreference", mSwitchPreference);
@@ -95,12 +93,15 @@ public class UsbTetherPreferenceControllerTest {
}
@Test
public void display_availableChangedCorrectly() {
when(mConnectivityManager.getTetherableUsbRegexs()).thenReturn(new String[]{""});
assertThat(mController.isAvailable()).isTrue();
public void shouldShow_noTetherableUsb() {
when(mConnectivityManager.getTetherableUsbRegexs()).thenReturn(new String[0]);
assertThat(mController.isAvailable()).isFalse();
assertThat(mController.shouldShow()).isFalse();
}
@Test
public void shouldEnable_noUsbConnected() {
ReflectionHelpers.setField(mController, "mUsbConnected", false);
assertThat(mController.shouldEnable()).isFalse();
}
@Test

View File

@@ -16,13 +16,15 @@
package com.android.settings.network;
import static com.android.settings.network.TetherEnabler.WIFI_TETHER_DISABLE_KEY;
import static com.android.settings.AllInOneTetherSettings.WIFI_TETHER_DISABLE_KEY;
import static com.android.settings.network.TetherEnabler.TETHERING_BLUETOOTH_ON;
import static com.android.settings.network.TetherEnabler.TETHERING_ETHERNET_ON;
import static com.android.settings.network.TetherEnabler.TETHERING_OFF;
import static com.android.settings.network.TetherEnabler.TETHERING_USB_ON;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
@@ -32,17 +34,48 @@ import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;
import androidx.test.core.app.ApplicationProvider;
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.ParameterizedRobolectricTestRunner;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
import java.util.Arrays;
import java.util.List;
@RunWith(ParameterizedRobolectricTestRunner.class)
public class WifiTetherDisablePreferenceControllerTest {
@ParameterizedRobolectricTestRunner.Parameters(name = "TetherState: {0}")
public static List params() {
return Arrays.asList(new Object[][] {
{TETHERING_OFF, R.string.summary_placeholder},
{TETHERING_USB_ON, R.string.disable_wifi_hotspot_when_usb_on},
{TETHERING_BLUETOOTH_ON, R.string.disable_wifi_hotspot_when_bluetooth_on},
{TETHERING_ETHERNET_ON, R.string.disable_wifi_hotspot_when_ethernet_on},
{
TETHERING_USB_ON | TETHERING_BLUETOOTH_ON,
R.string.disable_wifi_hotspot_when_usb_and_bluetooth_on
},
{
TETHERING_USB_ON | TETHERING_ETHERNET_ON,
R.string.disable_wifi_hotspot_when_usb_and_ethernet_on
},
{
TETHERING_BLUETOOTH_ON | TETHERING_ETHERNET_ON,
R.string.disable_wifi_hotspot_when_bluetooth_and_ethernet_on
},
{
TETHERING_USB_ON | TETHERING_BLUETOOTH_ON | TETHERING_ETHERNET_ON,
R.string.disable_wifi_hotspot_when_usb_and_bluetooth_and_ethernet_on
}
});
}
@Mock
private ConnectivityManager mConnectivityManager;
@Mock
@@ -53,6 +86,13 @@ public class WifiTetherDisablePreferenceControllerTest {
private SwitchPreference mPreference;
private Context mContext;
private WifiTetherDisablePreferenceController mController;
private final int mTetherState;
private final int mSummaryResId;
public WifiTetherDisablePreferenceControllerTest(int tetherState, int summaryResId) {
mTetherState = tetherState;
mSummaryResId = summaryResId;
}
@Before
public void setUp() {
@@ -71,21 +111,16 @@ public class WifiTetherDisablePreferenceControllerTest {
}
@Test
public void display_availableChangedCorrectly() {
public void shouldShow_noTetherableWifi() {
when(mConnectivityManager.getTetherableWifiRegexs()).thenReturn(new String[0]);
assertThat(mController.isAvailable()).isFalse();
when(mConnectivityManager.getTetherableWifiRegexs()).thenReturn(new String[]{"test"});
ReflectionHelpers.setField(mController, "mBluetoothTethering", false);
ReflectionHelpers.setField(mController, "mUsbTethering", false);
assertThat(mController.isAvailable()).isFalse();
assertThat(mController.shouldShow()).isFalse();
}
@Test
public void switch_shouldListenToUsbAndBluetooth() {
public void onTetherStateUpdated_visibilityChangeCorrectly() {
int state = TetherEnabler.TETHERING_BLUETOOTH_ON;
mController.onTetherStateUpdated(state);
verify(mPreference).setVisible(eq(true));
assertThat(mController.shouldShow()).isTrue();
state |= TetherEnabler.TETHERING_USB_ON;
mController.onTetherStateUpdated(state);
@@ -97,6 +132,12 @@ public class WifiTetherDisablePreferenceControllerTest {
state = TetherEnabler.TETHERING_OFF;
mController.onTetherStateUpdated(state);
verify(mPreference).setVisible(eq(false));
assertThat(mController.shouldShow()).isFalse();
}
@Test
public void getSummary_onTetherStateUpdated() {
mController.onTetherStateUpdated(mTetherState);
assertThat(mController.getSummary()).isEqualTo(mContext.getString(mSummaryResId));
}
}