Add toggle for Wi-Fi hotspot

- Add Wi-Fi hotspot toggle with vertical divider

- Disable preference and toggle when Data Saver is enabled

Bug: 245569117
Test: manual test
make RunSettingsRoboTests ROBOTEST_FILTER=TetherSettingsTest
make RunSettingsRoboTests ROBOTEST_FILTER=WifiTetherPreferenceControllerTest
atest -c TetheringManagerModelTest

Change-Id: Ic2baf7d3d0a7bf9527da38d24ecc511b7c91265a
This commit is contained in:
Weng Su
2023-01-13 07:52:23 +08:00
parent a65999cc60
commit f779e6c9d3
7 changed files with 276 additions and 71 deletions

View File

@@ -55,6 +55,7 @@ import androidx.preference.SwitchPreference;
import com.android.settings.R;
import com.android.settings.core.FeatureFlags;
import com.android.settings.wifi.tether.WifiTetherPreferenceController;
import com.android.settingslib.RestrictedSwitchPreference;
import org.junit.Before;
@@ -80,6 +81,18 @@ public class TetherSettingsTest {
private UserManager mUserManager;
@Mock
private TetheringManager mTetheringManager;
@Mock
private WifiTetherPreferenceController mWifiTetherPreferenceController;
@Mock
private RestrictedSwitchPreference mUsbTether;
@Mock
private SwitchPreference mBluetoothTether;
@Mock
private SwitchPreference mEthernetTether;
@Mock
private Preference mDataSaverFooter;
TetherSettings mTetherSettings;
@Before
public void setUp() throws Exception {
@@ -100,6 +113,14 @@ public class TetherSettingsTest {
when(mTetheringManager.getTetherableUsbRegexs()).thenReturn(new String[0]);
when(mTetheringManager.getTetherableBluetoothRegexs()).thenReturn(new String[0]);
mTetherSettings = spy(new TetherSettings());
mTetherSettings.mContext = mContext;
mTetherSettings.mWifiTetherPreferenceController = mWifiTetherPreferenceController;
mTetherSettings.mUsbTether = mUsbTether;
mTetherSettings.mBluetoothTether = mBluetoothTether;
mTetherSettings.mEthernetTether = mEthernetTether;
mTetherSettings.mDataSaverFooter = mDataSaverFooter;
}
@Test
@@ -184,16 +205,14 @@ public class TetherSettingsTest {
@Test
public void testSetFooterPreferenceTitle_isStaApConcurrencySupported_showStaApString() {
final TetherSettings spyTetherSettings = spy(new TetherSettings());
spyTetherSettings.mContext = mContext;
final Preference mockPreference = mock(Preference.class);
when(spyTetherSettings.findPreference(TetherSettings.KEY_TETHER_PREFS_TOP_INTRO))
when(mTetherSettings.findPreference(TetherSettings.KEY_TETHER_PREFS_TOP_INTRO))
.thenReturn(mockPreference);
final WifiManager mockWifiManager = mock(WifiManager.class);
when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mockWifiManager);
when(mockWifiManager.isStaApConcurrencySupported()).thenReturn(true);
spyTetherSettings.setTopIntroPreferenceTitle();
mTetherSettings.setTopIntroPreferenceTitle();
verify(mockPreference, never()).setTitle(R.string.tethering_footer_info);
verify(mockPreference).setTitle(R.string.tethering_footer_info_sta_ap_concurrency);
@@ -201,25 +220,23 @@ public class TetherSettingsTest {
@Test
public void testBluetoothState_updateBluetoothState_bluetoothTetheringStateOn() {
final TetherSettings spyTetherSettings = spy(new TetherSettings());
spyTetherSettings.mContext = mContext;
spyTetherSettings.mTm = mTetheringManager;
mTetherSettings.mTm = mTetheringManager;
final SwitchPreference mockSwitchPreference = mock(SwitchPreference.class);
when(spyTetherSettings.findPreference(TetherSettings.KEY_ENABLE_BLUETOOTH_TETHERING))
when(mTetherSettings.findPreference(TetherSettings.KEY_ENABLE_BLUETOOTH_TETHERING))
.thenReturn(mockSwitchPreference);
final FragmentActivity mockActivity = mock(FragmentActivity.class);
when(spyTetherSettings.getActivity()).thenReturn(mockActivity);
when(mTetherSettings.getActivity()).thenReturn(mockActivity);
final ArgumentCaptor<BroadcastReceiver> captor =
ArgumentCaptor.forClass(BroadcastReceiver.class);
when(mockActivity.registerReceiver(captor.capture(), any(IntentFilter.class)))
.thenReturn(null);
// Bluetooth tethering state is on
when(spyTetherSettings.getBluetoothState()).thenReturn(BluetoothAdapter.STATE_ON);
when(spyTetherSettings.isBluetoothTetheringOn()).thenReturn(true);
when(mTetherSettings.getBluetoothState()).thenReturn(BluetoothAdapter.STATE_ON);
when(mTetherSettings.isBluetoothTetheringOn()).thenReturn(true);
spyTetherSettings.setupTetherPreference();
spyTetherSettings.registerReceiver();
updateOnlyBluetoothState(spyTetherSettings);
mTetherSettings.setupTetherPreference();
mTetherSettings.registerReceiver();
updateOnlyBluetoothState(mTetherSettings);
// Simulate Bluetooth tethering state changed
final BroadcastReceiver receiver = captor.getValue();
@@ -234,25 +251,23 @@ public class TetherSettingsTest {
@Test
public void testBluetoothState_updateBluetoothState_bluetoothTetheringStateOff() {
final TetherSettings spyTetherSettings = spy(new TetherSettings());
spyTetherSettings.mContext = mContext;
spyTetherSettings.mTm = mTetheringManager;
mTetherSettings.mTm = mTetheringManager;
final SwitchPreference mockSwitchPreference = mock(SwitchPreference.class);
when(spyTetherSettings.findPreference(TetherSettings.KEY_ENABLE_BLUETOOTH_TETHERING))
when(mTetherSettings.findPreference(TetherSettings.KEY_ENABLE_BLUETOOTH_TETHERING))
.thenReturn(mockSwitchPreference);
final FragmentActivity mockActivity = mock(FragmentActivity.class);
when(spyTetherSettings.getActivity()).thenReturn(mockActivity);
when(mTetherSettings.getActivity()).thenReturn(mockActivity);
final ArgumentCaptor<BroadcastReceiver> captor =
ArgumentCaptor.forClass(BroadcastReceiver.class);
when(mockActivity.registerReceiver(captor.capture(), any(IntentFilter.class)))
.thenReturn(null);
// Bluetooth tethering state is off
when(spyTetherSettings.getBluetoothState()).thenReturn(BluetoothAdapter.STATE_ON);
when(spyTetherSettings.isBluetoothTetheringOn()).thenReturn(false);
when(mTetherSettings.getBluetoothState()).thenReturn(BluetoothAdapter.STATE_ON);
when(mTetherSettings.isBluetoothTetheringOn()).thenReturn(false);
spyTetherSettings.setupTetherPreference();
spyTetherSettings.registerReceiver();
updateOnlyBluetoothState(spyTetherSettings);
mTetherSettings.setupTetherPreference();
mTetherSettings.registerReceiver();
updateOnlyBluetoothState(mTetherSettings);
// Simulate Bluetooth tethering state changed
final BroadcastReceiver receiver = captor.getValue();
@@ -268,16 +283,14 @@ public class TetherSettingsTest {
@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))
when(mTetherSettings.findPreference(TetherSettings.KEY_USB_TETHER_SETTINGS))
.thenReturn(tetheringPreference);
spyTetherSettings.mContext = mContext;
spyTetherSettings.mTm = mTetheringManager;
spyTetherSettings.setupTetherPreference();
spyTetherSettings.mUsbRegexs = tethered;
mTetherSettings.mTm = mTetheringManager;
mTetherSettings.setupTetherPreference();
mTetherSettings.mUsbRegexs = tethered;
spyTetherSettings.updateUsbState(tethered);
mTetherSettings.updateUsbState(tethered);
verify(tetheringPreference).setEnabled(true);
verify(tetheringPreference).setChecked(true);
@@ -286,16 +299,14 @@ public class TetherSettingsTest {
@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))
when(mTetherSettings.findPreference(TetherSettings.KEY_USB_TETHER_SETTINGS))
.thenReturn(tetheringPreference);
spyTetherSettings.mContext = mContext;
spyTetherSettings.mTm = mTetheringManager;
spyTetherSettings.setupTetherPreference();
spyTetherSettings.mUsbRegexs = tethered;
mTetherSettings.mTm = mTetheringManager;
mTetherSettings.setupTetherPreference();
mTetherSettings.mUsbRegexs = tethered;
spyTetherSettings.updateUsbState(new String[0]);
mTetherSettings.updateUsbState(new String[0]);
verify(tetheringPreference).setEnabled(false);
verify(tetheringPreference).setChecked(false);
@@ -362,6 +373,20 @@ public class TetherSettingsTest {
verify(tetheringPreference, times(2)).setEnabled(true);
}
@Test
public void onDataSaverChanged_dataSaverEnabled_setToController() {
mTetherSettings.onDataSaverChanged(true);
verify(mWifiTetherPreferenceController).setDataSaverEnabled(true);
}
@Test
public void onDataSaverChanged_dataSaverDisabled_setToController() {
mTetherSettings.onDataSaverChanged(false);
verify(mWifiTetherPreferenceController).setDataSaverEnabled(false);
}
private void updateOnlyBluetoothState(TetherSettings tetherSettings) {
doReturn(mTetheringManager).when(mContext)
.getSystemService(Context.TETHERING_SERVICE);
@@ -391,21 +416,19 @@ public class TetherSettingsTest {
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))
when(mTetherSettings.findPreference(TetherSettings.KEY_USB_TETHER_SETTINGS))
.thenReturn(preference);
when(spyTetherSettings.findPreference(TetherSettings.KEY_ENABLE_BLUETOOTH_TETHERING))
when(mTetherSettings.findPreference(TetherSettings.KEY_ENABLE_BLUETOOTH_TETHERING))
.thenReturn(mockSwitchPreference);
spyTetherSettings.mContext = mContext;
spyTetherSettings.mTm = mTetheringManager;
when(spyTetherSettings.getActivity()).thenReturn(activity);
mTetherSettings.mTm = mTetheringManager;
when(mTetherSettings.getActivity()).thenReturn(activity);
when(activity.registerReceiver(captor.capture(), any(IntentFilter.class)))
.thenReturn(null);
spyTetherSettings.setupTetherPreference();
spyTetherSettings.registerReceiver();
updateOnlyBluetoothState(spyTetherSettings);
mTetherSettings.setupTetherPreference();
mTetherSettings.registerReceiver();
updateOnlyBluetoothState(mTetherSettings);
}
}

View File

@@ -16,6 +16,8 @@
package com.android.settings.network.tether;
import static android.net.TetheringManager.TETHERING_WIFI;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
@@ -92,4 +94,18 @@ public class TetheringManagerModelTest {
assertThat(mModel.mTetheredInterfaces.getValue()).isEqualTo(mInterfaces);
}
@Test
public void startTethering_startTetheringToTetheringManager() {
mModel.startTethering(TETHERING_WIFI);
verify(mTetheringManager).startTethering(eq(TETHERING_WIFI), any(), any());
}
@Test
public void stopTethering_stopTetheringToTetheringManager() {
mModel.stopTethering(TETHERING_WIFI);
verify(mTetheringManager).stopTethering(eq(TETHERING_WIFI));
}
}

View File

@@ -16,11 +16,15 @@
package com.android.settings.wifi.tether;
import static android.net.TetheringManager.TETHERING_WIFI;
import static android.net.wifi.WifiManager.WIFI_AP_STATE_ENABLED;
import static com.android.settings.wifi.WifiUtils.setCanShowWifiHotspotCached;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
@@ -30,7 +34,9 @@ import android.net.wifi.WifiManager;
import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.network.tether.TetheringManagerModel;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.widget.SwitchWidgetController;
import com.android.settingslib.PrimarySwitchPreference;
import com.android.settingslib.core.lifecycle.Lifecycle;
@@ -66,6 +72,10 @@ public class WifiTetherPreferenceControllerTest {
private WifiManager mWifiManager;
@Mock
private PreferenceScreen mScreen;
@Mock
TetheringManagerModel mTetheringManagerModel;
@Mock
private SwitchWidgetController mSwitch;
private SoftApConfiguration mSoftApConfiguration;
private WifiTetherPreferenceController mController;
@@ -80,10 +90,13 @@ public class WifiTetherPreferenceControllerTest {
when(mScreen.findPreference(anyString())).thenReturn(mPreference);
mSoftApConfiguration = new SoftApConfiguration.Builder().setSsid(SSID).build();
when(mWifiManager.getSoftApConfiguration()).thenReturn(mSoftApConfiguration);
when(mWifiManager.getWifiApState()).thenReturn(WIFI_AP_STATE_ENABLED);
mController = new WifiTetherPreferenceController(mContext, mLifecycle, mWifiManager,
false /* initSoftApManager */, true /* isWifiTetheringAllow */);
false /* initSoftApManager */, true /* isWifiTetheringAllow */,
mTetheringManagerModel);
mController.displayPreference(mScreen);
mController.mSwitch = mSwitch;
}
@Test
@@ -103,7 +116,8 @@ public class WifiTetherPreferenceControllerTest {
@Test
public void displayPreference_wifiTetheringNotAllowed_shouldDisable() {
mController = new WifiTetherPreferenceController(mContext, mLifecycle, mWifiManager,
false /* initSoftApManager */, false /* isWifiTetheringAllow */);
false /* initSoftApManager */, false /* isWifiTetheringAllow */,
mTetheringManagerModel);
mController.displayPreference(mScreen);
@@ -114,7 +128,8 @@ public class WifiTetherPreferenceControllerTest {
@Test
public void displayPreference_wifiTetheringAllowed_shouldEnable() {
mController = new WifiTetherPreferenceController(mContext, mLifecycle, mWifiManager,
false /* initSoftApManager */, true /* isWifiTetheringAllow */);
false /* initSoftApManager */, true /* isWifiTetheringAllow */,
mTetheringManagerModel);
mController.displayPreference(mScreen);
@@ -130,7 +145,7 @@ public class WifiTetherPreferenceControllerTest {
@Test
public void testHandleWifiApStateChanged_stateEnabled_showEnabledSummary() {
mController.handleWifiApStateChanged(WifiManager.WIFI_AP_STATE_ENABLED, 0 /* reason */);
mController.handleWifiApStateChanged(WIFI_AP_STATE_ENABLED, 0 /* reason */);
assertThat(mPreference.getSummary()).isEqualTo("Pixel is active");
}
@@ -150,6 +165,53 @@ public class WifiTetherPreferenceControllerTest {
"Not sharing internet or content with other devices");
}
@Test
public void handleWifiApStateChanged_stateDisabled_setSwitchUnchecked() {
mController.handleWifiApStateChanged(WifiManager.WIFI_AP_STATE_DISABLED, 0 /* reason */);
verify(mSwitch).setChecked(false);
}
@Test
public void handleWifiApStateChanged_stateEnabled_setSwitchChecked() {
mController.handleWifiApStateChanged(WIFI_AP_STATE_ENABLED, 0 /* reason */);
verify(mSwitch).setChecked(true);
}
@Test
public void setDataSaverEnabled_setEnabled_setPrefDisabled() {
mController.setDataSaverEnabled(true);
assertThat(mPreference.isEnabled()).isFalse();
verify(mSwitch).setEnabled(false);
}
@Test
public void setDataSaverEnabled_setDisabled_setPrefEnabled() {
mController.setDataSaverEnabled(false);
assertThat(mPreference.isEnabled()).isTrue();
verify(mSwitch).setEnabled(true);
}
@Test
public void onSwitchToggled_isChecked_startTethering() {
boolean ret = mController.onSwitchToggled(true);
verify(mTetheringManagerModel).startTethering(TETHERING_WIFI);
assertThat(ret).isTrue();
}
@Test
public void onSwitchToggled_isUnchecked_stopTethering() {
boolean ret = mController.onSwitchToggled(false);
verify(mTetheringManagerModel).stopTethering(TETHERING_WIFI);
assertThat(ret).isTrue();
}
@Implements(WifiTetherSettings.class)
public static final class ShadowWifiTetherSettings {