[Wi-Fi] Refactor add wifi networks feature to use WifiNetworkSuggestion.
WifiConfiguration used in the feature of add apps Wi-Fi networks was Deprecated, need to be changed to use WifiNetworkSuggestion. Bug: 136472483 Test: Refactor AddAppNetworksFragmentTest to apply WifiNetowrkSuggestion Change-Id: I022d200905880f7f7bd367f5fa62fa88e785db02
This commit is contained in:
@@ -74,8 +74,8 @@ public class AddAppNetworksActivity extends FragmentActivity {
|
|||||||
void showAddNetworksFragment() {
|
void showAddNetworksFragment() {
|
||||||
// TODO: Check the new intent status.
|
// TODO: Check the new intent status.
|
||||||
mBundle.putString(KEY_CALLING_PACKAGE_NAME, getCallingPackage());
|
mBundle.putString(KEY_CALLING_PACKAGE_NAME, getCallingPackage());
|
||||||
mBundle.putParcelableArrayList(Settings.EXTRA_WIFI_CONFIGURATION_LIST,
|
mBundle.putParcelableArrayList(Settings.EXTRA_WIFI_NETWORK_LIST,
|
||||||
getIntent().getParcelableArrayListExtra(Settings.EXTRA_WIFI_CONFIGURATION_LIST));
|
getIntent().getParcelableArrayListExtra(Settings.EXTRA_WIFI_NETWORK_LIST));
|
||||||
|
|
||||||
final FragmentManager fragmentManager = getSupportFragmentManager();
|
final FragmentManager fragmentManager = getSupportFragmentManager();
|
||||||
Fragment fragment = fragmentManager.findFragmentByTag(TAG);
|
Fragment fragment = fragmentManager.findFragmentByTag(TAG);
|
||||||
|
@@ -27,6 +27,8 @@ import android.graphics.drawable.Drawable;
|
|||||||
import android.net.wifi.WifiConfiguration;
|
import android.net.wifi.WifiConfiguration;
|
||||||
import android.net.wifi.WifiConfiguration.KeyMgmt;
|
import android.net.wifi.WifiConfiguration.KeyMgmt;
|
||||||
import android.net.wifi.WifiManager;
|
import android.net.wifi.WifiManager;
|
||||||
|
import android.net.wifi.WifiNetworkSuggestion;
|
||||||
|
import android.net.wifi.hotspot2.PasspointConfiguration;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.Message;
|
import android.os.Message;
|
||||||
@@ -65,8 +67,7 @@ public class AddAppNetworksFragment extends InstrumentedFragment {
|
|||||||
// Possible result values in each item of the returned result list, which is used
|
// Possible result values in each item of the returned result list, which is used
|
||||||
// to inform the caller APP the processed result of each specified network.
|
// to inform the caller APP the processed result of each specified network.
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
static final int RESULT_NETWORK_INITIAL = -1; //initial value
|
static final int RESULT_NETWORK_SUCCESS = 0;
|
||||||
private static final int RESULT_NETWORK_SUCCESS = 0;
|
|
||||||
private static final int RESULT_NETWORK_ADD_ERROR = 1;
|
private static final int RESULT_NETWORK_ADD_ERROR = 1;
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
static final int RESULT_NETWORK_ALREADY_EXISTS = 2;
|
static final int RESULT_NETWORK_ALREADY_EXISTS = 2;
|
||||||
@@ -97,7 +98,7 @@ public class AddAppNetworksFragment extends InstrumentedFragment {
|
|||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
String mCallingPackageName;
|
String mCallingPackageName;
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
List<WifiConfiguration> mAllSpecifiedNetworksList;
|
List<WifiNetworkSuggestion> mAllSpecifiedNetworksList;
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
List<UiConfigurationItem> mUiToRequestedList;
|
List<UiConfigurationItem> mUiToRequestedList;
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
@@ -121,7 +122,8 @@ public class AddAppNetworksFragment extends InstrumentedFragment {
|
|||||||
case MESSAGE_START_SAVING_NETWORK:
|
case MESSAGE_START_SAVING_NETWORK:
|
||||||
mSaveButton.setEnabled(false);
|
mSaveButton.setEnabled(false);
|
||||||
// Save the proposed networks, start from first one.
|
// Save the proposed networks, start from first one.
|
||||||
saveNetwork(0);
|
mSavingIndex = 0;
|
||||||
|
saveNetwork(mSavingIndex);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case MESSAGE_SHOW_SAVED_AND_CONNECT_NETWORK:
|
case MESSAGE_SHOW_SAVED_AND_CONNECT_NETWORK:
|
||||||
@@ -192,7 +194,7 @@ public class AddAppNetworksFragment extends InstrumentedFragment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
mAllSpecifiedNetworksList =
|
mAllSpecifiedNetworksList =
|
||||||
bundle.getParcelableArrayList(Settings.EXTRA_WIFI_CONFIGURATION_LIST);
|
bundle.getParcelableArrayList(Settings.EXTRA_WIFI_NETWORK_LIST);
|
||||||
|
|
||||||
// If there is no network in the request intent or the requested networks exceed the
|
// If there is no network in the request intent or the requested networks exceed the
|
||||||
// maximum limit, then just finish activity.
|
// maximum limit, then just finish activity.
|
||||||
@@ -224,7 +226,7 @@ public class AddAppNetworksFragment extends InstrumentedFragment {
|
|||||||
setSingleNetworkSignalIcon();
|
setSingleNetworkSignalIcon();
|
||||||
// Show the SSID of the proposed network.
|
// Show the SSID of the proposed network.
|
||||||
((TextView) mLayoutView.findViewById(R.id.single_ssid)).setText(
|
((TextView) mLayoutView.findViewById(R.id.single_ssid)).setText(
|
||||||
mAllSpecifiedNetworksList.get(0).SSID);
|
mUiToRequestedList.get(0).mDisplayedSsid);
|
||||||
// Set the status view as gone when UI is initialized.
|
// Set the status view as gone when UI is initialized.
|
||||||
mSingleNetworkProcessingStatusView.setVisibility(View.GONE);
|
mSingleNetworkProcessingStatusView.setVisibility(View.GONE);
|
||||||
} else {
|
} else {
|
||||||
@@ -259,7 +261,7 @@ public class AddAppNetworksFragment extends InstrumentedFragment {
|
|||||||
mResultCodeArrayList = new ArrayList<>();
|
mResultCodeArrayList = new ArrayList<>();
|
||||||
|
|
||||||
for (int i = 0; i < networksSize; i++) {
|
for (int i = 0; i < networksSize; i++) {
|
||||||
mResultCodeArrayList.add(RESULT_NETWORK_INITIAL);
|
mResultCodeArrayList.add(RESULT_NETWORK_SUCCESS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -268,6 +270,53 @@ public class AddAppNetworksFragment extends InstrumentedFragment {
|
|||||||
? config.wepKeys[config.wepTxKeyIndex] : null;
|
? config.wepKeys[config.wepTxKeyIndex] : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isSavedPasspointConfiguration(
|
||||||
|
PasspointConfiguration specifiecPassPointConfiguration) {
|
||||||
|
return mWifiManager.getPasspointConfigurations().stream()
|
||||||
|
.filter(config -> config.equals(specifiecPassPointConfiguration))
|
||||||
|
.findFirst()
|
||||||
|
.isPresent();
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isSavedWifiConfiguration(WifiConfiguration specifiedConfig,
|
||||||
|
List<WifiConfiguration> savedWifiConfigurations) {
|
||||||
|
final String ssidWithQuotation = addQuotationIfNeeded(specifiedConfig.SSID);
|
||||||
|
final int authType = specifiedConfig.getAuthType();
|
||||||
|
// TODO: reformat to use lambda
|
||||||
|
for (WifiConfiguration privilegedWifiConfiguration : savedWifiConfigurations) {
|
||||||
|
// If SSID or security type is different, should be new network or need to be
|
||||||
|
// updated network, continue to check others.
|
||||||
|
if (!ssidWithQuotation.equals(privilegedWifiConfiguration.SSID)
|
||||||
|
|| authType != privilegedWifiConfiguration.getAuthType()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If specified network and saved network have same security types, we'll check
|
||||||
|
// more information according to their security type to judge if they are same.
|
||||||
|
switch (authType) {
|
||||||
|
case KeyMgmt.NONE:
|
||||||
|
final String wep = getWepKey(specifiedConfig);
|
||||||
|
final String savedWep = getWepKey(privilegedWifiConfiguration);
|
||||||
|
return TextUtils.equals(wep, savedWep);
|
||||||
|
case KeyMgmt.OWE:
|
||||||
|
return true;
|
||||||
|
case KeyMgmt.WPA_PSK:
|
||||||
|
case KeyMgmt.WPA2_PSK:
|
||||||
|
case KeyMgmt.SAE:
|
||||||
|
if (specifiedConfig.preSharedKey.equals(
|
||||||
|
privilegedWifiConfiguration.preSharedKey)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
// TODO: Check how to judge enterprise type.
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* For the APP specified networks, filter saved ones and mark those saved as existed. And
|
* For the APP specified networks, filter saved ones and mark those saved as existed. And
|
||||||
* prepare a new UiConfigurationItem list, which contains those new or need to be updated
|
* prepare a new UiConfigurationItem list, which contains those new or need to be updated
|
||||||
@@ -283,54 +332,42 @@ public class AddAppNetworksFragment extends InstrumentedFragment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
boolean foundInSavedList;
|
boolean foundInSavedList;
|
||||||
|
boolean foundError;
|
||||||
|
String displayedName = null;
|
||||||
int networkPositionInBundle = 0;
|
int networkPositionInBundle = 0;
|
||||||
for (WifiConfiguration specifiedConfig : mAllSpecifiedNetworksList) {
|
for (WifiNetworkSuggestion suggestion : mAllSpecifiedNetworksList) {
|
||||||
foundInSavedList = false;
|
foundInSavedList = false;
|
||||||
final String displayedSsid = removeDoubleQuotes(specifiedConfig.SSID);
|
foundError = false;
|
||||||
final String ssidWithQuotation = addQuotationIfNeeded(specifiedConfig.SSID);
|
|
||||||
final int authType = specifiedConfig.getAuthType();
|
|
||||||
|
|
||||||
for (WifiConfiguration privilegedWifiConfiguration : savedWifiConfigurations) {
|
/**
|
||||||
// If SSID or security type is different, should be new network or need to be
|
* If specified is passpoint network, need to check with the existing passpoint
|
||||||
// updated network.
|
* networks.
|
||||||
if (!ssidWithQuotation.equals(privilegedWifiConfiguration.SSID)
|
*/
|
||||||
|| authType != privilegedWifiConfiguration.getAuthType()) {
|
if (suggestion.passpointConfiguration != null) {
|
||||||
continue;
|
if (!suggestion.passpointConfiguration.validate()) {
|
||||||
}
|
foundError = true;
|
||||||
|
} else {
|
||||||
// If specified network and saved network have same security types, we'll check
|
foundInSavedList = isSavedPasspointConfiguration(
|
||||||
// more information according to their security type to judge if they are same.
|
suggestion.passpointConfiguration);
|
||||||
switch (authType) {
|
displayedName = suggestion.passpointConfiguration.getHomeSp().getFriendlyName();
|
||||||
case KeyMgmt.NONE:
|
|
||||||
final String wep = getWepKey(specifiedConfig);
|
|
||||||
final String savedWep = getWepKey(privilegedWifiConfiguration);
|
|
||||||
foundInSavedList = TextUtils.equals(wep, savedWep);
|
|
||||||
break;
|
|
||||||
case KeyMgmt.OWE:
|
|
||||||
foundInSavedList = true;
|
|
||||||
break;
|
|
||||||
case KeyMgmt.WPA_PSK:
|
|
||||||
case KeyMgmt.WPA2_PSK:
|
|
||||||
case KeyMgmt.SAE:
|
|
||||||
if (specifiedConfig.preSharedKey.equals(
|
|
||||||
privilegedWifiConfiguration.preSharedKey)) {
|
|
||||||
foundInSavedList = true;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
// TODO: Check how to judge enterprise type.
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
final WifiConfiguration specifiedConfig = suggestion.wifiConfiguration;
|
||||||
|
displayedName = removeDoubleQuotes(specifiedConfig.SSID);
|
||||||
|
foundInSavedList = isSavedWifiConfiguration(specifiedConfig,
|
||||||
|
savedWifiConfigurations);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (foundInSavedList) {
|
if (foundError) {
|
||||||
|
mResultCodeArrayList.set(networkPositionInBundle, RESULT_NETWORK_ADD_ERROR);
|
||||||
|
} else if (foundInSavedList) {
|
||||||
// If this requested network already in the saved networks, mark this item in the
|
// If this requested network already in the saved networks, mark this item in the
|
||||||
// result code list as existed.
|
// result code list as existed.
|
||||||
mResultCodeArrayList.set(networkPositionInBundle, RESULT_NETWORK_ALREADY_EXISTS);
|
mResultCodeArrayList.set(networkPositionInBundle, RESULT_NETWORK_ALREADY_EXISTS);
|
||||||
} else {
|
} else {
|
||||||
// Prepare to add to UI list to show to user
|
// Prepare to add to UI list to show to user
|
||||||
UiConfigurationItem uiConfigurationIcon = new UiConfigurationItem(displayedSsid,
|
UiConfigurationItem uiConfigurationIcon = new UiConfigurationItem(displayedName,
|
||||||
specifiedConfig, networkPositionInBundle);
|
suggestion, networkPositionInBundle);
|
||||||
mUiToRequestedList.add(uiConfigurationIcon);
|
mUiToRequestedList.add(uiConfigurationIcon);
|
||||||
}
|
}
|
||||||
networkPositionInBundle++;
|
networkPositionInBundle++;
|
||||||
@@ -433,12 +470,13 @@ public class AddAppNetworksFragment extends InstrumentedFragment {
|
|||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
static class UiConfigurationItem {
|
static class UiConfigurationItem {
|
||||||
public final String mDisplayedSsid;
|
public final String mDisplayedSsid;
|
||||||
public final WifiConfiguration mWifiConfiguration;
|
public final WifiNetworkSuggestion mWifiNetworkSuggestion;
|
||||||
public final int mIndex;
|
public final int mIndex;
|
||||||
|
|
||||||
UiConfigurationItem(String displayedSsid, WifiConfiguration wifiConfiguration, int index) {
|
UiConfigurationItem(String displayedSsid, WifiNetworkSuggestion wifiNetworkSuggestion,
|
||||||
|
int index) {
|
||||||
mDisplayedSsid = displayedSsid;
|
mDisplayedSsid = displayedSsid;
|
||||||
mWifiConfiguration = wifiConfiguration;
|
mWifiNetworkSuggestion = wifiNetworkSuggestion;
|
||||||
mIndex = index;
|
mIndex = index;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -497,9 +535,6 @@ public class AddAppNetworksFragment extends InstrumentedFragment {
|
|||||||
mSaveListener = new WifiManager.ActionListener() {
|
mSaveListener = new WifiManager.ActionListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onSuccess() {
|
public void onSuccess() {
|
||||||
// Set success into result list.
|
|
||||||
mResultCodeArrayList.set(mUiToRequestedList.get(mSavingIndex).mIndex,
|
|
||||||
RESULT_NETWORK_SUCCESS);
|
|
||||||
mAnyNetworkSavedSuccess = true;
|
mAnyNetworkSavedSuccess = true;
|
||||||
|
|
||||||
if (saveNextNetwork()) {
|
if (saveNextNetwork()) {
|
||||||
@@ -561,16 +596,34 @@ public class AddAppNetworksFragment extends InstrumentedFragment {
|
|||||||
* Call framework API to save single network.
|
* Call framework API to save single network.
|
||||||
*/
|
*/
|
||||||
private void saveNetwork(int index) {
|
private void saveNetwork(int index) {
|
||||||
final WifiConfiguration wifiConfiguration = mUiToRequestedList.get(
|
if (mUiToRequestedList.get(index).mWifiNetworkSuggestion.passpointConfiguration != null) {
|
||||||
index).mWifiConfiguration;
|
// Save passpoint, if no IllegalArgumentException, then treat it as success.
|
||||||
wifiConfiguration.SSID = addQuotationIfNeeded(wifiConfiguration.SSID);
|
try {
|
||||||
mSavingIndex = index;
|
mWifiManager.addOrUpdatePasspointConfiguration(mUiToRequestedList.get(
|
||||||
mWifiManager.save(wifiConfiguration, mSaveListener);
|
index).mWifiNetworkSuggestion.passpointConfiguration);
|
||||||
|
mAnyNetworkSavedSuccess = true;
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
mResultCodeArrayList.set(mUiToRequestedList.get(index).mIndex,
|
||||||
|
RESULT_NETWORK_ADD_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (saveNextNetwork()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Show saved or failed according to all results.
|
||||||
|
showSavedOrFail();
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
final WifiConfiguration wifiConfiguration = mUiToRequestedList.get(
|
||||||
|
index).mWifiNetworkSuggestion.wifiConfiguration;
|
||||||
|
wifiConfiguration.SSID = addQuotationIfNeeded(wifiConfiguration.SSID);
|
||||||
|
mWifiManager.save(wifiConfiguration, mSaveListener);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void connectNetwork(int index) {
|
private void connectNetwork(int index) {
|
||||||
final WifiConfiguration wifiConfiguration = mUiToRequestedList.get(
|
final WifiConfiguration wifiConfiguration = mUiToRequestedList.get(
|
||||||
index).mWifiConfiguration;
|
index).mWifiNetworkSuggestion.wifiConfiguration;
|
||||||
mWifiManager.connect(wifiConfiguration, null /* ActionListener */);
|
mWifiManager.connect(wifiConfiguration, null /* ActionListener */);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -581,7 +634,7 @@ public class AddAppNetworksFragment extends InstrumentedFragment {
|
|||||||
|
|
||||||
if (resultArrayList != null) {
|
if (resultArrayList != null) {
|
||||||
Intent intent = new Intent();
|
Intent intent = new Intent();
|
||||||
intent.putIntegerArrayListExtra(Settings.EXTRA_WIFI_CONFIGURATION_RESULT_LIST,
|
intent.putIntegerArrayListExtra(Settings.EXTRA_WIFI_NETWORK_RESULT_LIST,
|
||||||
(ArrayList<Integer>) resultArrayList);
|
(ArrayList<Integer>) resultArrayList);
|
||||||
mActivity.setResult(resultCode, intent);
|
mActivity.setResult(resultCode, intent);
|
||||||
}
|
}
|
||||||
|
@@ -23,6 +23,7 @@ import static org.mockito.Mockito.spy;
|
|||||||
|
|
||||||
import android.app.settings.SettingsEnums;
|
import android.app.settings.SettingsEnums;
|
||||||
import android.net.wifi.WifiConfiguration;
|
import android.net.wifi.WifiConfiguration;
|
||||||
|
import android.net.wifi.WifiNetworkSuggestion;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Parcelable;
|
import android.os.Parcelable;
|
||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
@@ -46,31 +47,32 @@ public class AddAppNetworksFragmentTest {
|
|||||||
private static final String FAKE_APP_NAME = "fake_app_name";
|
private static final String FAKE_APP_NAME = "fake_app_name";
|
||||||
private static final String FAKE_NEW_WPA_SSID = "fake_new_wpa_ssid";
|
private static final String FAKE_NEW_WPA_SSID = "fake_new_wpa_ssid";
|
||||||
private static final String FAKE_NEW_OPEN_SSID = "fake_new_open_ssid";
|
private static final String FAKE_NEW_OPEN_SSID = "fake_new_open_ssid";
|
||||||
|
private static final String FAKE_NEW_OPEN_SSID_WITH_QUOTE = "\"fake_new_open_ssid\"";
|
||||||
private static final String FAKE_NEW_SAVED_WPA_SSID = "\"fake_new_wpa_ssid\"";
|
private static final String FAKE_NEW_SAVED_WPA_SSID = "\"fake_new_wpa_ssid\"";
|
||||||
|
|
||||||
private AddAppNetworksFragment mAddAppNetworksFragment;
|
private AddAppNetworksFragment mAddAppNetworksFragment;
|
||||||
private List<WifiConfiguration> mFakedSpecifiedNetworksList;
|
private List<WifiNetworkSuggestion> mFakedSpecifiedNetworksList;
|
||||||
private List<WifiConfiguration> mFakeSavedNetworksList;
|
private List<WifiConfiguration> mFakeSavedNetworksList;
|
||||||
private WifiConfiguration mNewWpaConfigEntry;
|
private WifiNetworkSuggestion mNewWpaSuggestionEntry;
|
||||||
private WifiConfiguration mNewOpenConfigEntry;
|
private WifiNetworkSuggestion mNewOpenSuggestionEntry;
|
||||||
private WifiConfiguration mSavedWpaConfigEntry;
|
private WifiConfiguration mSavedWpaConfigurationEntry;
|
||||||
private Bundle mBundle;
|
private Bundle mBundle;
|
||||||
private ArrayList<Integer> mFakedResultArrayList = new ArrayList<>();
|
private ArrayList<Integer> mFakedResultArrayList = new ArrayList<>();
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
mAddAppNetworksFragment = spy(new AddAppNetworksFragment());
|
mAddAppNetworksFragment = spy(new AddAppNetworksFragment());
|
||||||
mNewWpaConfigEntry = generateWifiConfig(FAKE_NEW_WPA_SSID,
|
mNewWpaSuggestionEntry = generateRegularWifiSuggestion(FAKE_NEW_WPA_SSID,
|
||||||
WifiConfiguration.KeyMgmt.WPA_PSK, "\"1234567890\"");
|
WifiConfiguration.KeyMgmt.WPA_PSK, "1234567890");
|
||||||
mNewOpenConfigEntry = generateWifiConfig(FAKE_NEW_OPEN_SSID,
|
mNewOpenSuggestionEntry = generateRegularWifiSuggestion(FAKE_NEW_OPEN_SSID,
|
||||||
WifiConfiguration.KeyMgmt.NONE, null);
|
WifiConfiguration.KeyMgmt.NONE, null);
|
||||||
mSavedWpaConfigEntry = generateWifiConfig(FAKE_NEW_SAVED_WPA_SSID,
|
mSavedWpaConfigurationEntry = generateRegularWifiConfiguration(FAKE_NEW_SAVED_WPA_SSID,
|
||||||
WifiConfiguration.KeyMgmt.WPA_PSK, "\"1234567890\"");
|
WifiConfiguration.KeyMgmt.WPA_PSK, "\"1234567890\"");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void callingPackageName_onCreateView_shouldBeCorrect() {
|
public void callingPackageName_onCreateView_shouldBeCorrect() {
|
||||||
addOneSpecifiedNetworkConfig(mNewWpaConfigEntry);
|
addOneSpecifiedRegularNetworkSuggestion(mNewWpaSuggestionEntry);
|
||||||
setUpBundle(mFakedSpecifiedNetworksList);
|
setUpBundle(mFakedSpecifiedNetworksList);
|
||||||
setupFragment();
|
setupFragment();
|
||||||
|
|
||||||
@@ -79,7 +81,7 @@ public class AddAppNetworksFragmentTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void launchFragment_shouldShowSaveButton() {
|
public void launchFragment_shouldShowSaveButton() {
|
||||||
addOneSpecifiedNetworkConfig(mNewWpaConfigEntry);
|
addOneSpecifiedRegularNetworkSuggestion(mNewWpaSuggestionEntry);
|
||||||
setUpBundle(mFakedSpecifiedNetworksList);
|
setUpBundle(mFakedSpecifiedNetworksList);
|
||||||
setupFragment();
|
setupFragment();
|
||||||
|
|
||||||
@@ -88,7 +90,7 @@ public class AddAppNetworksFragmentTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void launchFragment_shouldShowCancelButton() {
|
public void launchFragment_shouldShowCancelButton() {
|
||||||
addOneSpecifiedNetworkConfig(mNewWpaConfigEntry);
|
addOneSpecifiedRegularNetworkSuggestion(mNewWpaSuggestionEntry);
|
||||||
setUpBundle(mFakedSpecifiedNetworksList);
|
setUpBundle(mFakedSpecifiedNetworksList);
|
||||||
setupFragment();
|
setupFragment();
|
||||||
|
|
||||||
@@ -97,7 +99,7 @@ public class AddAppNetworksFragmentTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void requestOneNetwork_shouldShowCorrectSSID() {
|
public void requestOneNetwork_shouldShowCorrectSSID() {
|
||||||
addOneSpecifiedNetworkConfig(mNewWpaConfigEntry);
|
addOneSpecifiedRegularNetworkSuggestion(mNewWpaSuggestionEntry);
|
||||||
setUpBundle(mFakedSpecifiedNetworksList);
|
setUpBundle(mFakedSpecifiedNetworksList);
|
||||||
setupFragment();
|
setupFragment();
|
||||||
TextView ssidView = (TextView) mAddAppNetworksFragment.mLayoutView.findViewById(
|
TextView ssidView = (TextView) mAddAppNetworksFragment.mLayoutView.findViewById(
|
||||||
@@ -108,7 +110,7 @@ public class AddAppNetworksFragmentTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void withNoExtra_requestNetwork_shouldFinished() {
|
public void withNoExtra_requestNetwork_shouldFinished() {
|
||||||
addOneSpecifiedNetworkConfig(null);
|
addOneSpecifiedRegularNetworkSuggestion(null);
|
||||||
setUpBundle(mFakedSpecifiedNetworksList);
|
setUpBundle(mFakedSpecifiedNetworksList);
|
||||||
setupFragment();
|
setupFragment();
|
||||||
|
|
||||||
@@ -119,13 +121,13 @@ public class AddAppNetworksFragmentTest {
|
|||||||
public void withOneHalfSavedNetworks_uiListAndResultListShouldBeCorrect() {
|
public void withOneHalfSavedNetworks_uiListAndResultListShouldBeCorrect() {
|
||||||
// Arrange
|
// Arrange
|
||||||
// Setup a fake saved network list and assign to fragment.
|
// Setup a fake saved network list and assign to fragment.
|
||||||
addOneSavedNetworkConfig(mSavedWpaConfigEntry);
|
addOneSavedNetworkConfiguration(mSavedWpaConfigurationEntry);
|
||||||
// Setup two specified networks and their results and assign to fragment.
|
// Setup two specified networks and their results and assign to fragment.
|
||||||
addOneSpecifiedNetworkConfig(mNewWpaConfigEntry);
|
addOneSpecifiedRegularNetworkSuggestion(mNewWpaSuggestionEntry);
|
||||||
addOneSpecifiedNetworkConfig(mNewOpenConfigEntry);
|
addOneSpecifiedRegularNetworkSuggestion(mNewOpenSuggestionEntry);
|
||||||
mAddAppNetworksFragment.mAllSpecifiedNetworksList = mFakedSpecifiedNetworksList;
|
mAddAppNetworksFragment.mAllSpecifiedNetworksList = mFakedSpecifiedNetworksList;
|
||||||
mFakedResultArrayList.add(mAddAppNetworksFragment.RESULT_NETWORK_INITIAL);
|
mFakedResultArrayList.add(mAddAppNetworksFragment.RESULT_NETWORK_SUCCESS);
|
||||||
mFakedResultArrayList.add(mAddAppNetworksFragment.RESULT_NETWORK_INITIAL);
|
mFakedResultArrayList.add(mAddAppNetworksFragment.RESULT_NETWORK_SUCCESS);
|
||||||
mAddAppNetworksFragment.mResultCodeArrayList = mFakedResultArrayList;
|
mAddAppNetworksFragment.mResultCodeArrayList = mFakedResultArrayList;
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
@@ -136,7 +138,8 @@ public class AddAppNetworksFragmentTest {
|
|||||||
assertThat(mAddAppNetworksFragment.mResultCodeArrayList.get(0)).isEqualTo(
|
assertThat(mAddAppNetworksFragment.mResultCodeArrayList.get(0)).isEqualTo(
|
||||||
mAddAppNetworksFragment.RESULT_NETWORK_ALREADY_EXISTS);
|
mAddAppNetworksFragment.RESULT_NETWORK_ALREADY_EXISTS);
|
||||||
assertThat(mAddAppNetworksFragment.mUiToRequestedList.get(
|
assertThat(mAddAppNetworksFragment.mUiToRequestedList.get(
|
||||||
0).mWifiConfiguration.SSID).isEqualTo(FAKE_NEW_OPEN_SSID);
|
0).mWifiNetworkSuggestion.wifiConfiguration.SSID).isEqualTo(
|
||||||
|
FAKE_NEW_OPEN_SSID_WITH_QUOTE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -147,13 +150,13 @@ public class AddAppNetworksFragmentTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getThreeNetworksNewIntent_shouldHaveThreeItemsInUiList() {
|
public void getThreeNetworksNewIntent_shouldHaveThreeItemsInUiList() {
|
||||||
addOneSpecifiedNetworkConfig(mNewWpaConfigEntry);
|
addOneSpecifiedRegularNetworkSuggestion(mNewWpaSuggestionEntry);
|
||||||
setUpBundle(mFakedSpecifiedNetworksList);
|
setUpBundle(mFakedSpecifiedNetworksList);
|
||||||
setupFragment();
|
setupFragment();
|
||||||
|
|
||||||
// Add two more networks and update framework bundle.
|
// Add two more networks and update framework bundle.
|
||||||
addOneSpecifiedNetworkConfig(mNewWpaConfigEntry);
|
addOneSpecifiedRegularNetworkSuggestion(mNewWpaSuggestionEntry);
|
||||||
addOneSpecifiedNetworkConfig(mNewOpenConfigEntry);
|
addOneSpecifiedRegularNetworkSuggestion(mNewOpenSuggestionEntry);
|
||||||
setUpBundle(mFakedSpecifiedNetworksList);
|
setUpBundle(mFakedSpecifiedNetworksList);
|
||||||
Bundle bundle = mAddAppNetworksFragment.getArguments();
|
Bundle bundle = mAddAppNetworksFragment.getArguments();
|
||||||
mAddAppNetworksFragment.createContent(bundle);
|
mAddAppNetworksFragment.createContent(bundle);
|
||||||
@@ -162,7 +165,7 @@ public class AddAppNetworksFragmentTest {
|
|||||||
assertThat(mAddAppNetworksFragment.mUiToRequestedList).hasSize(3);
|
assertThat(mAddAppNetworksFragment.mUiToRequestedList).hasSize(3);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addOneSavedNetworkConfig(@NonNull WifiConfiguration wifiConfiguration) {
|
private void addOneSavedNetworkConfiguration(@NonNull WifiConfiguration wifiConfiguration) {
|
||||||
if (mFakeSavedNetworksList == null) {
|
if (mFakeSavedNetworksList == null) {
|
||||||
mFakeSavedNetworksList = new ArrayList<>();
|
mFakeSavedNetworksList = new ArrayList<>();
|
||||||
}
|
}
|
||||||
@@ -170,20 +173,21 @@ public class AddAppNetworksFragmentTest {
|
|||||||
mFakeSavedNetworksList.add(wifiConfiguration);
|
mFakeSavedNetworksList.add(wifiConfiguration);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addOneSpecifiedNetworkConfig(@NonNull WifiConfiguration wifiConfiguration) {
|
private void addOneSpecifiedRegularNetworkSuggestion(
|
||||||
if (wifiConfiguration != null) {
|
@NonNull WifiNetworkSuggestion wifiNetworkSuggestion) {
|
||||||
|
if (wifiNetworkSuggestion != null) {
|
||||||
if (mFakedSpecifiedNetworksList == null) {
|
if (mFakedSpecifiedNetworksList == null) {
|
||||||
mFakedSpecifiedNetworksList = new ArrayList<>();
|
mFakedSpecifiedNetworksList = new ArrayList<>();
|
||||||
}
|
}
|
||||||
mFakedSpecifiedNetworksList.add(wifiConfiguration);
|
mFakedSpecifiedNetworksList.add(wifiNetworkSuggestion);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setUpBundle(List<WifiConfiguration> allFakedNetworksList) {
|
private void setUpBundle(List<WifiNetworkSuggestion> allFakedNetworksList) {
|
||||||
// Set up bundle.
|
// Set up bundle.
|
||||||
final Bundle bundle = new Bundle();
|
final Bundle bundle = new Bundle();
|
||||||
bundle.putString(AddAppNetworksActivity.KEY_CALLING_PACKAGE_NAME, FAKE_APP_NAME);
|
bundle.putString(AddAppNetworksActivity.KEY_CALLING_PACKAGE_NAME, FAKE_APP_NAME);
|
||||||
bundle.putParcelableArrayList(Settings.EXTRA_WIFI_CONFIGURATION_LIST,
|
bundle.putParcelableArrayList(Settings.EXTRA_WIFI_NETWORK_LIST,
|
||||||
(ArrayList<? extends Parcelable>) allFakedNetworksList);
|
(ArrayList<? extends Parcelable>) allFakedNetworksList);
|
||||||
doReturn(bundle).when(mAddAppNetworksFragment).getArguments();
|
doReturn(bundle).when(mAddAppNetworksFragment).getArguments();
|
||||||
}
|
}
|
||||||
@@ -192,7 +196,8 @@ public class AddAppNetworksFragmentTest {
|
|||||||
FragmentController.setupFragment(mAddAppNetworksFragment);
|
FragmentController.setupFragment(mAddAppNetworksFragment);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static WifiConfiguration generateWifiConfig(String ssid, int securityType,
|
private static WifiConfiguration generateRegularWifiConfiguration(String ssid, int
|
||||||
|
securityType,
|
||||||
String password) {
|
String password) {
|
||||||
final WifiConfiguration config = new WifiConfiguration();
|
final WifiConfiguration config = new WifiConfiguration();
|
||||||
config.SSID = ssid;
|
config.SSID = ssid;
|
||||||
@@ -203,4 +208,29 @@ public class AddAppNetworksFragmentTest {
|
|||||||
}
|
}
|
||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static WifiNetworkSuggestion generateRegularWifiSuggestion(String ssid,
|
||||||
|
int securityType,
|
||||||
|
String password) {
|
||||||
|
WifiNetworkSuggestion suggestion = null;
|
||||||
|
|
||||||
|
switch (securityType) {
|
||||||
|
case WifiConfiguration.KeyMgmt.NONE:
|
||||||
|
suggestion = new WifiNetworkSuggestion.Builder()
|
||||||
|
.setSsid(ssid)
|
||||||
|
.build();
|
||||||
|
break;
|
||||||
|
case WifiConfiguration.KeyMgmt.WPA_PSK:
|
||||||
|
suggestion = new WifiNetworkSuggestion.Builder()
|
||||||
|
.setSsid(ssid)
|
||||||
|
.setWpa2Passphrase(password)
|
||||||
|
.build();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return suggestion;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user