Merge "Fix "USB tethering" will auto turn on then off when turn off "USB tethering"" into sc-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
6f1bc2f52e
@@ -16,14 +16,20 @@
|
||||
|
||||
package com.android.settings;
|
||||
|
||||
import static android.content.Intent.ACTION_MEDIA_SHARED;
|
||||
import static android.content.Intent.ACTION_MEDIA_UNSHARED;
|
||||
import static android.hardware.usb.UsbManager.ACTION_USB_STATE;
|
||||
|
||||
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 static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -33,6 +39,7 @@ import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.hardware.usb.UsbManager;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.TetheringManager;
|
||||
import android.net.wifi.WifiManager;
|
||||
@@ -45,6 +52,7 @@ import androidx.preference.Preference;
|
||||
import androidx.preference.SwitchPreference;
|
||||
|
||||
import com.android.settings.core.FeatureFlags;
|
||||
import com.android.settingslib.RestrictedSwitchPreference;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -71,7 +79,7 @@ public class TetherSettingsTest {
|
||||
private TetheringManager mTetheringManager;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
public void setUp() throws Exception {
|
||||
mContext = spy(RuntimeEnvironment.application);
|
||||
|
||||
MockitoAnnotations.initMocks(this);
|
||||
@@ -81,6 +89,8 @@ public class TetherSettingsTest {
|
||||
.when(mContext).getSystemService(Context.USER_SERVICE);
|
||||
doReturn(mTetheringManager)
|
||||
.when(mContext).getSystemService(Context.TETHERING_SERVICE);
|
||||
doReturn(mContext).when(mContext).createPackageContextAsUser(
|
||||
any(String.class), anyInt(), any(UserHandle.class));
|
||||
|
||||
setupIsTetherAvailable(true);
|
||||
|
||||
@@ -161,7 +171,7 @@ public class TetherSettingsTest {
|
||||
@Test
|
||||
public void testSetFooterPreferenceTitle_isStaApConcurrencySupported_showStaApString() {
|
||||
final TetherSettings spyTetherSettings = spy(new TetherSettings());
|
||||
when(spyTetherSettings.getContext()).thenReturn(mContext);
|
||||
spyTetherSettings.mContext = mContext;
|
||||
final Preference mockPreference = mock(Preference.class);
|
||||
when(spyTetherSettings.findPreference(TetherSettings.KEY_TETHER_PREFS_TOP_INTRO))
|
||||
.thenReturn(mockPreference);
|
||||
@@ -178,7 +188,8 @@ public class TetherSettingsTest {
|
||||
@Test
|
||||
public void testBluetoothState_updateBluetoothState_bluetoothTetheringStateOn() {
|
||||
final TetherSettings spyTetherSettings = spy(new TetherSettings());
|
||||
when(spyTetherSettings.getContext()).thenReturn(mContext);
|
||||
spyTetherSettings.mContext = mContext;
|
||||
spyTetherSettings.mTm = mTetheringManager;
|
||||
final SwitchPreference mockSwitchPreference = mock(SwitchPreference.class);
|
||||
when(spyTetherSettings.findPreference(TetherSettings.KEY_ENABLE_BLUETOOTH_TETHERING))
|
||||
.thenReturn(mockSwitchPreference);
|
||||
@@ -210,7 +221,8 @@ public class TetherSettingsTest {
|
||||
@Test
|
||||
public void testBluetoothState_updateBluetoothState_bluetoothTetheringStateOff() {
|
||||
final TetherSettings spyTetherSettings = spy(new TetherSettings());
|
||||
when(spyTetherSettings.getContext()).thenReturn(mContext);
|
||||
spyTetherSettings.mContext = mContext;
|
||||
spyTetherSettings.mTm = mTetheringManager;
|
||||
final SwitchPreference mockSwitchPreference = mock(SwitchPreference.class);
|
||||
when(spyTetherSettings.findPreference(TetherSettings.KEY_ENABLE_BLUETOOTH_TETHERING))
|
||||
.thenReturn(mockSwitchPreference);
|
||||
@@ -239,14 +251,110 @@ public class TetherSettingsTest {
|
||||
verify(mockSwitchPreference).setChecked(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_usbTetheringIsEnabled_checksUsbTethering() {
|
||||
String [] tethered = {"rndis0"};
|
||||
TetherSettings spyTetherSettings = spy(new TetherSettings());
|
||||
RestrictedSwitchPreference tetheringPreference = mock(RestrictedSwitchPreference.class);
|
||||
when(spyTetherSettings.findPreference(TetherSettings.KEY_USB_TETHER_SETTINGS))
|
||||
.thenReturn(tetheringPreference);
|
||||
spyTetherSettings.mContext = mContext;
|
||||
spyTetherSettings.mTm = mTetheringManager;
|
||||
spyTetherSettings.setupTetherPreference();
|
||||
spyTetherSettings.mUsbRegexs = tethered;
|
||||
|
||||
spyTetherSettings.updateUsbState(tethered);
|
||||
|
||||
verify(tetheringPreference).setEnabled(true);
|
||||
verify(tetheringPreference).setChecked(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateState_usbTetheringIsDisabled_unchecksUsbTethering() {
|
||||
String [] tethered = {"rndis0"};
|
||||
TetherSettings spyTetherSettings = spy(new TetherSettings());
|
||||
RestrictedSwitchPreference tetheringPreference = mock(RestrictedSwitchPreference.class);
|
||||
when(spyTetherSettings.findPreference(TetherSettings.KEY_USB_TETHER_SETTINGS))
|
||||
.thenReturn(tetheringPreference);
|
||||
spyTetherSettings.mContext = mContext;
|
||||
spyTetherSettings.mTm = mTetheringManager;
|
||||
spyTetherSettings.setupTetherPreference();
|
||||
spyTetherSettings.mUsbRegexs = tethered;
|
||||
|
||||
spyTetherSettings.updateUsbState(new String[0]);
|
||||
|
||||
verify(tetheringPreference).setEnabled(false);
|
||||
verify(tetheringPreference).setChecked(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onReceive_usbIsConnected_tetheringPreferenceIsEnabled() {
|
||||
RestrictedSwitchPreference tetheringPreference = mock(RestrictedSwitchPreference.class);
|
||||
FragmentActivity mockActivity = mock(FragmentActivity.class);
|
||||
ArgumentCaptor<BroadcastReceiver> captor = ArgumentCaptor.forClass(BroadcastReceiver.class);
|
||||
setupUsbStateComponents(tetheringPreference, captor, mockActivity);
|
||||
|
||||
BroadcastReceiver receiver = captor.getValue();
|
||||
Intent usbStateChanged = new Intent(ACTION_USB_STATE);
|
||||
usbStateChanged.putExtra(UsbManager.USB_CONNECTED, true);
|
||||
receiver.onReceive(mockActivity, usbStateChanged);
|
||||
|
||||
verify(tetheringPreference).setEnabled(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onReceive_usbIsDisconnected_tetheringPreferenceIsDisabled() {
|
||||
RestrictedSwitchPreference tetheringPreference = mock(RestrictedSwitchPreference.class);
|
||||
FragmentActivity mockActivity = mock(FragmentActivity.class);
|
||||
ArgumentCaptor<BroadcastReceiver> captor = ArgumentCaptor.forClass(BroadcastReceiver.class);
|
||||
setupUsbStateComponents(tetheringPreference, captor, mockActivity);
|
||||
|
||||
BroadcastReceiver receiver = captor.getValue();
|
||||
Intent usbStateChanged = new Intent(ACTION_USB_STATE);
|
||||
usbStateChanged.putExtra(UsbManager.USB_CONNECTED, false);
|
||||
receiver.onReceive(mockActivity, usbStateChanged);
|
||||
|
||||
verify(tetheringPreference).setEnabled(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onReceive_mediaIsShared_tetheringPreferenceIsDisabled() {
|
||||
RestrictedSwitchPreference tetheringPreference = mock(RestrictedSwitchPreference.class);
|
||||
FragmentActivity mockActivity = mock(FragmentActivity.class);
|
||||
ArgumentCaptor<BroadcastReceiver> captor = ArgumentCaptor.forClass(BroadcastReceiver.class);
|
||||
setupUsbStateComponents(tetheringPreference, captor, mockActivity);
|
||||
|
||||
BroadcastReceiver receiver = captor.getValue();
|
||||
Intent mediaIsShared = new Intent(ACTION_MEDIA_SHARED);
|
||||
receiver.onReceive(mockActivity, mediaIsShared);
|
||||
|
||||
verify(tetheringPreference).setEnabled(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onReceive_mediaIsUnshared_tetheringPreferenceIsEnabled() {
|
||||
RestrictedSwitchPreference tetheringPreference = mock(RestrictedSwitchPreference.class);
|
||||
FragmentActivity mockActivity = mock(FragmentActivity.class);
|
||||
ArgumentCaptor<BroadcastReceiver> captor = ArgumentCaptor.forClass(BroadcastReceiver.class);
|
||||
setupUsbStateComponents(tetheringPreference, captor, mockActivity);
|
||||
|
||||
BroadcastReceiver receiver = captor.getValue();
|
||||
Intent mediaIsShared = new Intent(ACTION_MEDIA_UNSHARED);
|
||||
Intent usbStateChanged = new Intent(ACTION_USB_STATE);
|
||||
usbStateChanged.putExtra(UsbManager.USB_CONNECTED, true);
|
||||
receiver.onReceive(mockActivity, usbStateChanged);
|
||||
receiver.onReceive(mockActivity, mediaIsShared);
|
||||
|
||||
verify(tetheringPreference, times(2)).setEnabled(true);
|
||||
}
|
||||
|
||||
private void updateOnlyBluetoothState(TetherSettings tetherSettings) {
|
||||
doReturn(mTetheringManager).when(tetherSettings)
|
||||
.getSystemService(Context.TETHERING_SERVICE);
|
||||
when(mTetheringManager.getTetherableIfaces()).thenReturn(new String[0]);
|
||||
when(mTetheringManager.getTetheredIfaces()).thenReturn(new String[0]);
|
||||
when(mTetheringManager.getTetheringErroredIfaces()).thenReturn(new String[0]);
|
||||
doNothing().when(tetherSettings).updateUsbState(any(String[].class), any(String[].class),
|
||||
any(String[].class));
|
||||
doNothing().when(tetherSettings).updateUsbState(any(String[].class));
|
||||
doNothing().when(tetherSettings).updateEthernetState(any(String[].class),
|
||||
any(String[].class));
|
||||
}
|
||||
@@ -266,4 +374,24 @@ public class TetherSettingsTest {
|
||||
UserManager.DISALLOW_CONFIG_TETHERING, UserHandle.of(userId)))
|
||||
.thenReturn(!returnValue);
|
||||
}
|
||||
|
||||
private void setupUsbStateComponents(RestrictedSwitchPreference preference,
|
||||
ArgumentCaptor<BroadcastReceiver> captor, FragmentActivity activity) {
|
||||
TetherSettings spyTetherSettings = spy(new TetherSettings());
|
||||
SwitchPreference mockSwitchPreference = mock(SwitchPreference.class);
|
||||
|
||||
when(spyTetherSettings.findPreference(TetherSettings.KEY_USB_TETHER_SETTINGS))
|
||||
.thenReturn(preference);
|
||||
when(spyTetherSettings.findPreference(TetherSettings.KEY_ENABLE_BLUETOOTH_TETHERING))
|
||||
.thenReturn(mockSwitchPreference);
|
||||
spyTetherSettings.mContext = mContext;
|
||||
spyTetherSettings.mTm = mTetheringManager;
|
||||
when(spyTetherSettings.getActivity()).thenReturn(activity);
|
||||
when(activity.registerReceiver(captor.capture(), any(IntentFilter.class)))
|
||||
.thenReturn(null);
|
||||
|
||||
spyTetherSettings.setupTetherPreference();
|
||||
spyTetherSettings.registerReceiver();
|
||||
updateOnlyBluetoothState(spyTetherSettings);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user