From c00fedc6395dfe852b389df1e5ad87f015e43360 Mon Sep 17 00:00:00 2001 From: Antony Sargent Date: Tue, 18 Jul 2017 15:33:18 -0700 Subject: [PATCH] Allow open password dialog for wrong-password wifi access points When we're failing to connect to a wifi access point due to an incorrect password, we want to allow an intent from a notification to open up the wifi settings page and bring up the dialog for entering a different password. We already have code in settings to do this for not-yet-saved access points, so this CL just changes it slightly to also allow it for saved access points. Unfortunately WifiSettings can't be tested with Robolectric due to it not supporting PreferenceScreen, so this adds a test to WifiSettingsUiTest. There were some existing test failures in that file which I've fixed while I was in there: -The TestAccessPointBuilder class wasn't being found at runtime because it was getting stripped out at build time due to not being used in settings. -The changingSecurityStateOnApShouldNotCauseMultipleListItems test was asserting that we don't end up with multiple entries for the same SSID in the access point list when changing the security state for the AP, but it was accidentally passing multiple AP's with the same name the first time. Bug: 33245941 Test: runtest --path WifiSettingsUiTest.java Change-Id: I16c9c8b0d8380a0e26f9b23df6a8d012af6a2476 Merged-In: I929ca6892242059df157c01d6e9ea30e8d1c5e78 --- .../android/settings/wifi/WifiSettings.java | 27 +++- .../settings/wifi/WifiSettingsUiTest.java | 137 ++++++++++++++---- 2 files changed, 134 insertions(+), 30 deletions(-) diff --git a/src/com/android/settings/wifi/WifiSettings.java b/src/com/android/settings/wifi/WifiSettings.java index bd35851feac..4b8a2e38e6c 100644 --- a/src/com/android/settings/wifi/WifiSettings.java +++ b/src/com/android/settings/wifi/WifiSettings.java @@ -146,8 +146,9 @@ public class WifiSettings extends RestrictedSettingsFragment // account creation outside of setup wizard. private static final String EXTRA_ENABLE_NEXT_ON_CONNECT = "wifi_enable_next_on_connect"; // This string extra specifies a network to open the connect dialog on, so the user can enter - // network credentials. This is used by quick settings for secured networks. - private static final String EXTRA_START_CONNECT_SSID = "wifi_start_connect_ssid"; + // network credentials. This is used by quick settings for secured networks, among other + // things. + public static final String EXTRA_START_CONNECT_SSID = "wifi_start_connect_ssid"; // should Next button only be enabled when we have a connection? private boolean mEnableNextOnConnection; @@ -726,6 +727,21 @@ public class WifiSettings extends RestrictedSettingsFragment changeNextButtonState(mWifiTracker.isConnected()); } + /** Helper method to return whether an AccessPoint is disabled due to a wrong password */ + private static boolean isDisabledByWrongPassword(AccessPoint accessPoint) { + WifiConfiguration config = accessPoint.getConfig(); + if (config == null) { + return false; + } + WifiConfiguration.NetworkSelectionStatus networkStatus = + config.getNetworkSelectionStatus(); + if (networkStatus == null || networkStatus.isNetworkEnabled()) { + return false; + } + int reason = networkStatus.getNetworkSelectionDisableReason(); + return WifiConfiguration.NetworkSelectionStatus.DISABLED_BY_WRONG_PASSWORD == reason; + } + private void updateAccessPointPreferences() { // in case state has changed if (!mWifiManager.isWifiEnabled()) { @@ -761,10 +777,11 @@ public class WifiSettings extends RestrictedSettingsFragment preference.setKey(key); preference.setOrder(index); if (mOpenSsid != null && mOpenSsid.equals(accessPoint.getSsidStr()) - && !accessPoint.isSaved() && accessPoint.getSecurity() != AccessPoint.SECURITY_NONE) { - onPreferenceTreeClick(preference); - mOpenSsid = null; + if (!accessPoint.isSaved() || isDisabledByWrongPassword(accessPoint)) { + onPreferenceTreeClick(preference); + mOpenSsid = null; + } } mAccessPointsPreferenceCategory.addPreference(preference); accessPoint.setListener(WifiSettings.this); diff --git a/tests/unit/src/com/android/settings/wifi/WifiSettingsUiTest.java b/tests/unit/src/com/android/settings/wifi/WifiSettingsUiTest.java index a85d5917d15..cbd9546c229 100644 --- a/tests/unit/src/com/android/settings/wifi/WifiSettingsUiTest.java +++ b/tests/unit/src/com/android/settings/wifi/WifiSettingsUiTest.java @@ -22,6 +22,7 @@ import static android.support.test.espresso.assertion.ViewAssertions.matches; import static android.support.test.espresso.matcher.ViewMatchers.Visibility.VISIBLE; import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; import static android.support.test.espresso.matcher.ViewMatchers.withEffectiveVisibility; +import static android.support.test.espresso.matcher.ViewMatchers.withId; import static android.support.test.espresso.matcher.ViewMatchers.withText; import static com.google.common.truth.Truth.assertThat; @@ -43,6 +44,7 @@ import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.net.wifi.WifiSsid; +import android.provider.Settings; import android.support.test.InstrumentationRegistry; import android.support.test.rule.ActivityTestRule; import android.support.test.runner.AndroidJUnit4; @@ -67,24 +69,25 @@ import java.util.List; @RunWith(AndroidJUnit4.class) public class WifiSettingsUiTest { - - // TODO(b/37714546): Investigate why resource ids are not resolving correctly in the test apk, - // then remove this manual string entry - /** R.string.wifi_configure_settings_preference_title */ - private static final String WIFI_PREFERENCES = "Wi\u2011Fi preferences"; - /** R.string.wifi_saved_access_points_label */ - private static final String SAVED_NETWORKS = "Saved networks"; - /** R.string.wifi_empty_list_wifi_off */ - private static final String WIFI_OFF_MESSAGE = "To see available networks, turn Wi\u2011Fi on."; - /** R.string.wifi_display_status_connected */ - private static final String CONNECTED = "Connected"; - private static final String TEST_SSID = "\"Test Ssid\""; private static final String TEST_UNQUOTED_SSID = "Test Ssid"; private static final String TEST_BSSID = "0a:08:5c:67:89:00"; private static final int TEST_RSSI = 123; private static final int TEST_NETWORK_ID = 1; + // Keys used to lookup resources by name (see the resourceId/resourceString helper methods). + private static final String ID = "id"; + private static final String STRING = "string"; + private static final String WIFI_CONFIGURE_SETTINGS_PREFERENCE_TITLE = + "wifi_configure_settings_preference_title"; + private static final String WIFI_SAVED_ACCESS_POINTS_LABEL = "wifi_saved_access_points_label"; + private static final String WIFI_EMPTY_LIST_WIFI_OFF = "wifi_empty_list_wifi_off"; + private static final String WIFI_DISPLAY_STATUS_CONNECTED = "wifi_display_status_connected"; + private static final String WIFI_PASSWORD = "wifi_password"; + private static final String WIFI_SHOW_PASSWORD = "wifi_show_password"; + private static final String PASSWORD_LAYOUT = "password_layout"; + private static final String PASSWORD = "password"; + @Mock private WifiTracker mWifiTracker; @Mock @@ -104,6 +107,21 @@ public class WifiSettingsUiTest { when(mWifiTracker.getManager()).thenReturn(mWifiManager); } + /** + * Helper to get around the problem that directly accessing settings resource id's from + * com.android.settings.R via R.(type).(name) (eg R.id.password or + * R.string.wifi_configure_settings_preference_title) may not work due to mismatched resource + * ids. See b/37714546 and b/63546650. + */ + private int resourceId(String type, String name) { + return mContext.getResources().getIdentifier(name, type, mContext.getPackageName()); + } + + /** Similar to {@link #resourceId}, but for accessing R.string. values. */ + private String resourceString(String name) { + return mContext.getResources().getString(resourceId(STRING, name)); + } + private void setupConnectedAccessPoint() { WifiConfiguration config = new WifiConfiguration(); config.SSID = TEST_SSID; @@ -123,14 +141,20 @@ public class WifiSettingsUiTest { assertThat(accessPoint.getBssid()).isEqualTo(TEST_BSSID); assertThat(accessPoint.getNetworkInfo()).isNotNull(); assertThat(accessPoint.isActive()).isTrue(); - assertThat(accessPoint.getSettingsSummary()).isEqualTo(CONNECTED); + assertThat(accessPoint.getSettingsSummary()).isEqualTo( + resourceString(WIFI_DISPLAY_STATUS_CONNECTED)); when(mWifiTracker.getAccessPoints()).thenReturn( Lists.asList(accessPoint, new AccessPoint[]{})); } - private void launchActivity() { - mActivityRule.launchActivity(new Intent("android.settings.WIFI_SETTINGS")); + /** Launch the activity via an Intent with a String extra. */ + private void launchActivity(String extraName, String extraValue) { + Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS); + if (extraName != null && extraValue != null) { + intent.putExtra(extraName, extraValue); + } + mActivityRule.launchActivity(intent); verify(mWifiTracker).getManager(); @@ -140,6 +164,11 @@ public class WifiSettingsUiTest { assertThat(mWifiListener).isNotNull(); } + /** Helper to launch the activity with no extra. */ + private void launchActivity() { + launchActivity(null, null); + } + private void setWifiState(int wifiState) { when(mWifiManager.getWifiState()).thenReturn(wifiState); when(mWifiManager.isWifiEnabled()).thenReturn(wifiState == WifiManager.WIFI_STATE_ENABLED); @@ -159,7 +188,8 @@ public class WifiSettingsUiTest { public void shouldShowWifiPreferences() { launchActivity(); - onView(withText(WIFI_PREFERENCES)).check(matches(isDisplayed())); + onView(withText(resourceId(STRING, WIFI_CONFIGURE_SETTINGS_PREFERENCE_TITLE))).check( + matches(isDisplayed())); } @Test @@ -169,7 +199,8 @@ public class WifiSettingsUiTest { launchActivity(); - onView(withText(SAVED_NETWORKS)).check(matches(not(isDisplayed()))); + onView(withText(resourceId(STRING, WIFI_SAVED_ACCESS_POINTS_LABEL))).check( + matches(not(isDisplayed()))); } @Test @@ -179,7 +210,8 @@ public class WifiSettingsUiTest { launchActivity(); - onView(withText(SAVED_NETWORKS)).check(doesNotExist()); + onView(withText(resourceId(STRING, WIFI_SAVED_ACCESS_POINTS_LABEL))).check( + doesNotExist()); } @Test @@ -189,7 +221,7 @@ public class WifiSettingsUiTest { launchActivity(); - onView(allOf(withText(SAVED_NETWORKS), + onView(allOf(withText(resourceId(STRING, WIFI_SAVED_ACCESS_POINTS_LABEL)), withEffectiveVisibility(VISIBLE))).check(matches(isDisplayed())); } @@ -200,7 +232,8 @@ public class WifiSettingsUiTest { launchActivity(); callOnWifiStateChanged(WifiManager.WIFI_STATE_DISABLED); - onView(withText(startsWith(WIFI_OFF_MESSAGE))).check(matches(isDisplayed())); + onView(withText(startsWith(resourceString(WIFI_EMPTY_LIST_WIFI_OFF)))).check( + matches(isDisplayed())); } @Test @@ -210,7 +243,8 @@ public class WifiSettingsUiTest { launchActivity(); callOnWifiStateChanged(WifiManager.WIFI_STATE_ENABLED); - onView(withText(startsWith(WIFI_OFF_MESSAGE))).check(doesNotExist()); + onView(withText(startsWith(resourceString(WIFI_EMPTY_LIST_WIFI_OFF)))).check( + doesNotExist()); } @Test @@ -221,7 +255,8 @@ public class WifiSettingsUiTest { launchActivity(); - onView(withText(CONNECTED)).check(matches(isDisplayed())); + onView(withText(resourceString(WIFI_DISPLAY_STATUS_CONNECTED))).check( + matches(isDisplayed())); } @Test @@ -232,7 +267,8 @@ public class WifiSettingsUiTest { launchActivity(); - onView(withText(CONNECTED)).check(matches(isDisplayed())); + onView(withText(resourceString(WIFI_DISPLAY_STATUS_CONNECTED))).check( + matches(isDisplayed())); verify(mWifiTracker).forceUpdate(); Activity activity = mActivityRule.getActivity(); @@ -247,7 +283,9 @@ public class WifiSettingsUiTest { public void changingSecurityStateOnApShouldNotCauseMultipleListItems() { setWifiState(WifiManager.WIFI_STATE_ENABLED); TestAccessPointBuilder builder = new TestAccessPointBuilder(mContext) - .setSsid(TEST_SSID).setSecurity(AccessPoint.SECURITY_NONE); + .setSsid(TEST_SSID) + .setSecurity(AccessPoint.SECURITY_NONE) + .setRssi(TEST_RSSI); AccessPoint open = builder.build(); builder.setSecurity(AccessPoint.SECURITY_EAP); @@ -258,7 +296,7 @@ public class WifiSettingsUiTest { // Return a different security state each time getAccessPoints is invoked when(mWifiTracker.getAccessPoints()) - .thenReturn(Lists.newArrayList(open, eap)) + .thenReturn(Lists.newArrayList(open)) .thenReturn(Lists.newArrayList(eap)) .thenReturn(Lists.newArrayList(wep)); @@ -272,4 +310,53 @@ public class WifiSettingsUiTest { mWifiListener.onAccessPointsChanged(); onView(withText(TEST_SSID)).check(matches(isDisplayed())); } + + @Test + public void wrongPasswordSavedNetwork() { + setWifiState(WifiManager.WIFI_STATE_ENABLED); + + // Set up an AccessPoint that is disabled due to incorrect password. + WifiConfiguration config = new WifiConfiguration(); + config.SSID = TEST_SSID; + config.BSSID = TEST_BSSID; + config.networkId = TEST_NETWORK_ID; + config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); + + WifiConfiguration.NetworkSelectionStatus selectionStatus = + new WifiConfiguration.NetworkSelectionStatus(); + selectionStatus.setNetworkSelectionDisableReason( + WifiConfiguration.NetworkSelectionStatus.DISABLED_BY_WRONG_PASSWORD); + selectionStatus.setNetworkSelectionStatus( + WifiConfiguration.NetworkSelectionStatus.NETWORK_SELECTION_TEMPORARY_DISABLED); + config.setNetworkSelectionStatus(selectionStatus); + + WifiInfo wifiInfo = new WifiInfo(); + wifiInfo.setSSID(WifiSsid.createFromAsciiEncoded(TEST_UNQUOTED_SSID)); + wifiInfo.setBSSID(TEST_BSSID); + wifiInfo.setRssi(TEST_RSSI); + wifiInfo.setNetworkId(TEST_NETWORK_ID); + AccessPoint accessPoint = new AccessPoint(mContext, config); + accessPoint.update(config, wifiInfo, null); + + // Make sure we've set up our access point correctly. + assertThat(accessPoint.getSsidStr()).isEqualTo(TEST_UNQUOTED_SSID); + assertThat(accessPoint.getBssid()).isEqualTo(TEST_BSSID); + assertThat(accessPoint.isActive()).isFalse(); + assertThat(accessPoint.getConfig()).isNotNull(); + WifiConfiguration.NetworkSelectionStatus networkStatus = + accessPoint.getConfig().getNetworkSelectionStatus(); + assertThat(networkStatus).isNotNull(); + assertThat(networkStatus.isNetworkEnabled()).isFalse(); + assertThat(networkStatus.getNetworkSelectionDisableReason()).isEqualTo( + WifiConfiguration.NetworkSelectionStatus.DISABLED_BY_WRONG_PASSWORD); + + when(mWifiTracker.getAccessPoints()).thenReturn(Lists.newArrayList(accessPoint)); + launchActivity(WifiSettings.EXTRA_START_CONNECT_SSID, accessPoint.getSsidStr()); + + // Make sure that the password dialog is visible. + onView(withText(resourceId(STRING, WIFI_PASSWORD))).check(matches(isDisplayed())); + onView(withText(resourceId(STRING, WIFI_SHOW_PASSWORD))).check(matches(isDisplayed())); + onView(withId(resourceId(ID, PASSWORD_LAYOUT))).check(matches(isDisplayed())); + onView(withId(resourceId(ID, PASSWORD))).check(matches(isDisplayed())); + } }