Merge "Wifi Setting: Use SoftApConfiguration for Tether Setting"

This commit is contained in:
TreeHugger Robot
2019-12-06 03:29:52 +00:00
committed by Android (Google) Code Review
18 changed files with 191 additions and 177 deletions

View File

@@ -27,7 +27,7 @@ import static org.mockito.Mockito.when;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.SoftApConfiguration;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.provider.Settings;
@@ -71,9 +71,9 @@ public class DeviceNamePreferenceControllerTest {
mContext = RuntimeEnvironment.application;
mPreference = new ValidatedEditTextPreference(mContext);
when(mScreen.findPreference(anyString())).thenReturn(mPreference);
final WifiConfiguration configuration = new WifiConfiguration();
configuration.SSID = "test-ap";
when(mWifiManager.getWifiApConfiguration()).thenReturn(configuration);
final SoftApConfiguration configuration =
new SoftApConfiguration.Builder().setSsid("test-ap").build();
when(mWifiManager.getSoftApConfiguration()).thenReturn(configuration);
mController = new DeviceNamePreferenceController(mContext, "test_key");
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
@@ -137,9 +137,10 @@ public class DeviceNamePreferenceControllerTest {
mController.displayPreference(mScreen);
mController.onPreferenceChange(mPreference, TESTING_STRING);
ArgumentCaptor<WifiConfiguration> captor = ArgumentCaptor.forClass(WifiConfiguration.class);
verify(mWifiManager).setWifiApConfiguration(captor.capture());
assertThat(captor.getValue().SSID).isEqualTo(TESTING_STRING);
ArgumentCaptor<SoftApConfiguration> captor =
ArgumentCaptor.forClass(SoftApConfiguration.class);
verify(mWifiManager).setSoftApConfiguration(captor.capture());
assertThat(captor.getValue().getSsid()).isEqualTo(TESTING_STRING);
}
@Test

View File

@@ -19,7 +19,7 @@ package com.android.settings.homepage.contextualcards.conditional;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.SoftApConfiguration;
import android.net.wifi.WifiManager;
import com.android.settings.homepage.contextualcards.ContextualCard;
@@ -53,7 +53,7 @@ public class HotspotConditionControllerTest {
@Test
public void buildContextualCard_hasWifiAp_shouldHaveWifiApSsid() {
setupWifiApConfiguration();
setupSoftApConfiguration();
final ContextualCard card = mController.buildContextualCard();
@@ -67,9 +67,9 @@ public class HotspotConditionControllerTest {
assertThat(card.getSummaryText()).isEqualTo("");
}
private void setupWifiApConfiguration() {
final WifiConfiguration wifiApConfig = new WifiConfiguration();
wifiApConfig.SSID = WIFI_AP_SSID;
mContext.getSystemService(WifiManager.class).setWifiApConfiguration(wifiApConfig);
private void setupSoftApConfiguration() {
final SoftApConfiguration wifiApConfig = new SoftApConfiguration.Builder()
.setSsid(WIFI_AP_SSID).build();
mContext.getSystemService(WifiManager.class).setSoftApConfiguration(wifiApConfig);
}
}

View File

@@ -18,6 +18,7 @@ package com.android.settings.testutils.shadow;
import static org.robolectric.RuntimeEnvironment.application;
import android.net.wifi.SoftApConfiguration;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.net.wifi.hotspot2.PasspointConfiguration;
@@ -37,16 +38,16 @@ public class ShadowWifiManager extends org.robolectric.shadows.ShadowWifiManager
private List<PasspointConfiguration> mPasspointConfiguration;
public WifiConfiguration savedWifiConfig;
private WifiConfiguration mSavedApConfig;
private SoftApConfiguration mSavedApConfig;
@Implementation
protected WifiConfiguration getWifiApConfiguration() {
protected SoftApConfiguration getSoftApConfiguration() {
return mSavedApConfig;
}
@Implementation
protected boolean setWifiApConfiguration(WifiConfiguration wifiConfig) {
mSavedApConfig = wifiConfig;
protected boolean setSoftApConfiguration(SoftApConfiguration softApConfig) {
mSavedApConfig = softApConfig;
return true;
}

View File

@@ -26,7 +26,7 @@ import static org.mockito.Mockito.when;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.SoftApConfiguration;
import android.net.wifi.WifiManager;
import androidx.preference.ListPreference;
@@ -72,9 +72,8 @@ public class WifiTetherApBandPreferenceControllerTest {
when(mConnectivityManager.getTetherableWifiRegexs()).thenReturn(new String[]{"1", "2"});
when(mContext.getResources()).thenReturn(RuntimeEnvironment.application.getResources());
when(mScreen.findPreference(anyString())).thenReturn(mPreference);
WifiConfiguration config = new WifiConfiguration();
config.apBand = WifiConfiguration.AP_BAND_ANY;
when(mWifiManager.getWifiApConfiguration()).thenReturn(new WifiConfiguration());
when(mWifiManager.getSoftApConfiguration()).thenReturn(
new SoftApConfiguration.Builder().build());
when(mWifiManager.isDualModeSupported()).thenReturn(false);
mController = new WifiTetherApBandPreferenceController(mContext, mListener);
@@ -123,7 +122,7 @@ public class WifiTetherApBandPreferenceControllerTest {
mController.displayPreference(mScreen);
// -1 is WifiConfiguration.AP_BAND_ANY, for 'Auto' option. This should be prevented from
// -1 is SoftApConfiguration.BAND_ANY, for 'Auto' option. This should be prevented from
// being set since it is invalid for this configuration
mController.onPreferenceChange(mPreference, "-1");
assertThat(mController.getBandIndex()).isEqualTo(1);
@@ -151,7 +150,7 @@ public class WifiTetherApBandPreferenceControllerTest {
mController.displayPreference(mScreen);
// -1 is WifiConfiguration.AP_BAND_ANY, for 'Auto' option.
// -1 is SoftApConfiguration.BAND_ANY, for 'Auto' option.
mController.onPreferenceChange(mPreference, "-1");
assertThat(mController.getBandIndex()).isEqualTo(-1);
assertThat(mPreference.getSummary()).isEqualTo(ALL_BANDS);

View File

@@ -25,7 +25,7 @@ import static org.mockito.Mockito.when;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.SoftApConfiguration;
import android.net.wifi.WifiManager;
import androidx.preference.PreferenceScreen;
@@ -59,18 +59,17 @@ public class WifiTetherPasswordPreferenceControllerTest {
private WifiTetherPasswordPreferenceController mController;
private ValidatedEditTextPreference mPreference;
private WifiConfiguration mConfig;
private SoftApConfiguration mConfig;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mPreference = new ValidatedEditTextPreference(RuntimeEnvironment.application);
mConfig = new WifiConfiguration();
mConfig.SSID = "test_1234";
mConfig.preSharedKey = "test_password";
mConfig = new SoftApConfiguration.Builder().setSsid("test_1234")
.setWpa2Passphrase("test_password").build();
when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifiManager);
when(mWifiManager.getWifiApConfiguration()).thenReturn(mConfig);
when(mWifiManager.getSoftApConfiguration()).thenReturn(mConfig);
when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE))
.thenReturn(mConnectivityManager);
when(mConnectivityManager.getTetherableWifiRegexs()).thenReturn(new String[]{"1", "2"});
@@ -84,19 +83,19 @@ public class WifiTetherPasswordPreferenceControllerTest {
public void displayPreference_shouldStylePreference() {
mController.displayPreference(mScreen);
assertThat(mPreference.getText()).isEqualTo(mConfig.preSharedKey);
assertThat(mPreference.getSummary()).isEqualTo(mConfig.preSharedKey);
assertThat(mPreference.getText()).isEqualTo(mConfig.getWpa2Passphrase());
assertThat(mPreference.getSummary()).isEqualTo(mConfig.getWpa2Passphrase());
}
@Test
public void changePreference_shouldUpdateValue() {
mController.displayPreference(mScreen);
mController.onPreferenceChange(mPreference, VALID_PASS);
assertThat(mController.getPasswordValidated(WifiConfiguration.KeyMgmt.WPA2_PSK))
assertThat(mController.getPasswordValidated(SoftApConfiguration.SECURITY_TYPE_WPA2_PSK))
.isEqualTo(VALID_PASS);
mController.onPreferenceChange(mPreference, VALID_PASS2);
assertThat(mController.getPasswordValidated(WifiConfiguration.KeyMgmt.WPA2_PSK))
assertThat(mController.getPasswordValidated(SoftApConfiguration.SECURITY_TYPE_WPA2_PSK))
.isEqualTo(VALID_PASS2);
verify(mListener, times(2)).onTetherConfigUpdated(mController);
@@ -107,19 +106,19 @@ public class WifiTetherPasswordPreferenceControllerTest {
// Set controller password to anything and verify is set.
mController.displayPreference(mScreen);
mController.onPreferenceChange(mPreference, VALID_PASS);
assertThat(mController.getPasswordValidated(WifiConfiguration.KeyMgmt.WPA2_PSK))
assertThat(mController.getPasswordValidated(SoftApConfiguration.SECURITY_TYPE_WPA2_PSK))
.isEqualTo(VALID_PASS);
// Create a new config using different password
final WifiConfiguration config = new WifiConfiguration();
config.preSharedKey = VALID_PASS2;
when(mWifiManager.getWifiApConfiguration()).thenReturn(config);
final SoftApConfiguration config = new SoftApConfiguration.Builder()
.setWpa2Passphrase(VALID_PASS2).build();
when(mWifiManager.getSoftApConfiguration()).thenReturn(config);
// Call updateDisplay and verify it's changed.
mController.updateDisplay();
assertThat(mController.getPasswordValidated(WifiConfiguration.KeyMgmt.WPA2_PSK))
.isEqualTo(config.preSharedKey);
assertThat(mPreference.getSummary()).isEqualTo(config.preSharedKey);
assertThat(mController.getPasswordValidated(SoftApConfiguration.SECURITY_TYPE_WPA2_PSK))
.isEqualTo(config.getWpa2Passphrase());
assertThat(mPreference.getSummary()).isEqualTo(config.getWpa2Passphrase());
}
@Test
@@ -127,13 +126,13 @@ public class WifiTetherPasswordPreferenceControllerTest {
// Set controller password to anything and verify is set.
mController.displayPreference(mScreen);
mController.onPreferenceChange(mPreference, VALID_PASS);
assertThat(mController.getPasswordValidated(WifiConfiguration.KeyMgmt.WPA2_PSK))
assertThat(mController.getPasswordValidated(SoftApConfiguration.SECURITY_TYPE_WPA2_PSK))
.isEqualTo(VALID_PASS);
// Create a new config using different password
final WifiConfiguration config = new WifiConfiguration();
config.preSharedKey = VALID_PASS2;
when(mWifiManager.getWifiApConfiguration()).thenReturn(config);
final SoftApConfiguration config = new SoftApConfiguration.Builder()
.setWpa2Passphrase(VALID_PASS2).build();
when(mWifiManager.getSoftApConfiguration()).thenReturn(config);
// Call updateDisplay and verify it's changed.
mController.updateDisplay();

View File

@@ -19,18 +19,13 @@ package com.android.settings.wifi.tether;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.SoftApConfiguration;
import android.net.wifi.WifiManager;
import android.provider.Settings;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.PreferenceScreen;
@@ -49,7 +44,6 @@ import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
@Config(shadows = {
@@ -67,8 +61,7 @@ public class WifiTetherPreferenceControllerTest {
private WifiManager mWifiManager;
@Mock
private PreferenceScreen mScreen;
@Mock
private WifiConfiguration mWifiConfiguration;
private SoftApConfiguration mSoftApConfiguration;
private WifiTetherPreferenceController mController;
private Lifecycle mLifecycle;
@@ -88,8 +81,8 @@ public class WifiTetherPreferenceControllerTest {
.thenReturn(mConnectivityManager);
when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifiManager);
when(mScreen.findPreference(anyString())).thenReturn(mPreference);
when(mWifiManager.getWifiApConfiguration()).thenReturn(mWifiConfiguration);
mWifiConfiguration.SSID = SSID;
mSoftApConfiguration = new SoftApConfiguration.Builder().setSsid(SSID).build();
when(mWifiManager.getSoftApConfiguration()).thenReturn(mSoftApConfiguration);
when(mConnectivityManager.getTetherableWifiRegexs()).thenReturn(new String[]{"1", "2"});
mController = new WifiTetherPreferenceController(mContext, mLifecycle,

View File

@@ -25,14 +25,11 @@ import static org.mockito.Mockito.when;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiConfiguration.KeyMgmt;
import android.net.wifi.SoftApConfiguration;
import android.net.wifi.WifiManager;
import androidx.preference.PreferenceScreen;
import com.android.settings.widget.ValidatedEditTextPreference;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -76,7 +73,7 @@ public class WifiTetherSSIDPreferenceControllerTest {
@Test
public void displayPreference_noWifiConfig_shouldDisplayDefaultSSID() {
when(mWifiManager.getWifiApConfiguration()).thenReturn(null);
when(mWifiManager.getSoftApConfiguration()).thenReturn(null);
mController.displayPreference(mScreen);
assertThat(mController.getSSID())
@@ -85,12 +82,12 @@ public class WifiTetherSSIDPreferenceControllerTest {
@Test
public void displayPreference_hasCustomWifiConfig_shouldDisplayCustomSSID() {
final WifiConfiguration config = new WifiConfiguration();
config.SSID = "test_1234";
when(mWifiManager.getWifiApConfiguration()).thenReturn(config);
final SoftApConfiguration config = new SoftApConfiguration.Builder()
.setSsid("test_1234").build();
when(mWifiManager.getSoftApConfiguration()).thenReturn(config);
mController.displayPreference(mScreen);
assertThat(mController.getSSID()).isEqualTo(config.SSID);
assertThat(mController.getSSID()).isEqualTo(config.getSsid());
}
@Test
@@ -113,23 +110,22 @@ public class WifiTetherSSIDPreferenceControllerTest {
assertThat(mController.getSSID()).isEqualTo("1");
// Create a new config using different SSID
final WifiConfiguration config = new WifiConfiguration();
config.SSID = "test_1234";
when(mWifiManager.getWifiApConfiguration()).thenReturn(config);
final SoftApConfiguration config = new SoftApConfiguration.Builder()
.setSsid("test_1234").build();
when(mWifiManager.getSoftApConfiguration()).thenReturn(config);
// Call updateDisplay and verify it's changed.
mController.updateDisplay();
assertThat(mController.getSSID()).isEqualTo(config.SSID);
assertThat(mPreference.getSummary()).isEqualTo(config.SSID);
assertThat(mController.getSSID()).isEqualTo(config.getSsid());
assertThat(mPreference.getSummary()).isEqualTo(config.getSsid());
}
@Test
public void displayPreference_wifiApDisabled_shouldHideQrCodeIcon() {
when(mWifiManager.isWifiApEnabled()).thenReturn(false);
final WifiConfiguration config = new WifiConfiguration();
config.SSID = "test_1234";
config.allowedKeyManagement.set(KeyMgmt.WPA2_PSK);
when(mWifiManager.getWifiApConfiguration()).thenReturn(config);
final SoftApConfiguration config = new SoftApConfiguration.Builder()
.setSsid("test_1234").setWpa2Passphrase("test_password").build();
when(mWifiManager.getSoftApConfiguration()).thenReturn(config);
mController.displayPreference(mScreen);
assertThat(mController.isQrCodeButtonAvailable()).isEqualTo(false);
@@ -138,10 +134,9 @@ public class WifiTetherSSIDPreferenceControllerTest {
@Test
public void displayPreference_wifiApEnabled_shouldShowQrCodeIcon() {
when(mWifiManager.isWifiApEnabled()).thenReturn(true);
final WifiConfiguration config = new WifiConfiguration();
config.SSID = "test_1234";
config.allowedKeyManagement.set(KeyMgmt.WPA2_PSK);
when(mWifiManager.getWifiApConfiguration()).thenReturn(config);
final SoftApConfiguration config = new SoftApConfiguration.Builder()
.setSsid("test_1234").setWpa2Passphrase("test_password").build();
when(mWifiManager.getSoftApConfiguration()).thenReturn(config);
mController.displayPreference(mScreen);
assertThat(mController.isQrCodeButtonAvailable()).isEqualTo(true);

View File

@@ -8,7 +8,7 @@ import static org.mockito.Mockito.when;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.SoftApConfiguration;
import android.net.wifi.WifiManager;
import androidx.preference.ListPreference;
@@ -25,8 +25,9 @@ import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class WifiTetherSecurityPreferenceControllerTest {
private static final String WPA2_PSK = String.valueOf(WifiConfiguration.KeyMgmt.WPA2_PSK);
private static final String NONE = String.valueOf(WifiConfiguration.KeyMgmt.NONE);
private static final String WPA2_PSK =
String.valueOf(SoftApConfiguration.SECURITY_TYPE_WPA2_PSK);
private static final String NONE = String.valueOf(SoftApConfiguration.SECURITY_TYPE_OPEN);
@Mock
private WifiTetherBasePreferenceController.OnTetherConfigUpdateListener mListener;
private Context mContext;
@@ -38,19 +39,17 @@ public class WifiTetherSecurityPreferenceControllerTest {
private PreferenceScreen mScreen;
private WifiTetherSecurityPreferenceController mController;
private ListPreference mPreference;
private WifiConfiguration mConfig;
private SoftApConfiguration mConfig;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mConfig = new WifiConfiguration();
mConfig.SSID = "test_1234";
mConfig.preSharedKey = "test_password";
mConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA2_PSK);
mConfig = new SoftApConfiguration.Builder().setSsid("test_1234")
.setWpa2Passphrase("test_password").build();
mContext = spy(RuntimeEnvironment.application);
when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifiManager);
when(mWifiManager.getWifiApConfiguration()).thenReturn(mConfig);
when(mWifiManager.getSoftApConfiguration()).thenReturn(mConfig);
when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE))
.thenReturn(mConnectivityManager);
when(mConnectivityManager.getTetherableWifiRegexs()).thenReturn(new String[]{"1", "2"});
@@ -64,35 +63,41 @@ public class WifiTetherSecurityPreferenceControllerTest {
@Test
public void onPreferenceChange_securityValueUpdated() {
mController.onPreferenceChange(mPreference, WPA2_PSK);
assertThat(mController.getSecurityType()).isEqualTo(WifiConfiguration.KeyMgmt.WPA2_PSK);
assertThat(mController.getSecurityType()).isEqualTo(
SoftApConfiguration.SECURITY_TYPE_WPA2_PSK);
assertThat(mPreference.getSummary().toString()).isEqualTo("WPA2-Personal");
mController.onPreferenceChange(mPreference, NONE);
assertThat(mController.getSecurityType()).isEqualTo(WifiConfiguration.KeyMgmt.NONE);
assertThat(mController.getSecurityType()).isEqualTo(
SoftApConfiguration.SECURITY_TYPE_OPEN);
assertThat(mPreference.getSummary().toString()).isEqualTo("None");
}
@Test
public void updateDisplay_preferenceUpdated() {
// test defaulting to WPA2-Personal on new config
when(mWifiManager.getWifiApConfiguration()).thenReturn(null);
when(mWifiManager.getSoftApConfiguration()).thenReturn(null);
mController.updateDisplay();
assertThat(mController.getSecurityType()).isEqualTo(WifiConfiguration.KeyMgmt.WPA2_PSK);
assertThat(mController.getSecurityType()).isEqualTo(
SoftApConfiguration.SECURITY_TYPE_WPA2_PSK);
assertThat(mPreference.getSummary().toString()).isEqualTo("WPA2-Personal");
// test open tether network
when(mWifiManager.getWifiApConfiguration()).thenReturn(mConfig);
mConfig.allowedKeyManagement.clear();
mConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
SoftApConfiguration config = new SoftApConfiguration.Builder(mConfig)
.setWpa2Passphrase(null).build();
when(mWifiManager.getSoftApConfiguration()).thenReturn(config);
mController.updateDisplay();
assertThat(mController.getSecurityType()).isEqualTo(WifiConfiguration.KeyMgmt.NONE);
assertThat(mController.getSecurityType()).isEqualTo(
SoftApConfiguration.SECURITY_TYPE_OPEN);
assertThat(mPreference.getSummary().toString()).isEqualTo("None");
// test WPA2-Personal tether network
mConfig.allowedKeyManagement.clear();
mConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA2_PSK);
SoftApConfiguration config2 = new SoftApConfiguration.Builder(mConfig)
.setWpa2Passphrase("test_password").build();
when(mWifiManager.getSoftApConfiguration()).thenReturn(config2);
mController.updateDisplay();
assertThat(mController.getSecurityType()).isEqualTo(WifiConfiguration.KeyMgmt.WPA2_PSK);
assertThat(mController.getSecurityType()).isEqualTo(
SoftApConfiguration.SECURITY_TYPE_WPA2_PSK);
assertThat(mPreference.getSummary().toString()).isEqualTo("WPA2-Personal");
}
}