Prevent brief flash of "Error" in hotspot status text

When toggling the master preference to turn on wifi hotspot, it briefly
changes the status text to "Error" because we have a catch-all in a
function monitoring wifi state changes for unexpected status codes, and
we weren't expecting to see the status of WIFI_AP_STATE_ENABLING before
the status of WIFI_AP_STATE_ENABLED.

Bug: 64811203
Test: make RunSettingsRoboTests
Change-Id: Ifba7abadbfba9ce93cc524b17232c65570f0f428
This commit is contained in:
Antony Sargent
2017-09-11 16:43:58 -07:00
parent adef5b23e0
commit a2559ab128
2 changed files with 32 additions and 12 deletions

View File

@@ -139,6 +139,7 @@ public class WifiTetherPreferenceController extends AbstractPreferenceController
private void handleWifiApStateChanged(int state, int reason) { private void handleWifiApStateChanged(int state, int reason) {
switch (state) { switch (state) {
case WifiManager.WIFI_AP_STATE_ENABLING:
case WifiManager.WIFI_AP_STATE_ENABLED: case WifiManager.WIFI_AP_STATE_ENABLED:
/** /**
* Summary on enable is handled by tether * Summary on enable is handled by tether

View File

@@ -161,12 +161,7 @@ public class WifiTetherPreferenceControllerTest {
@Test @Test
public void testReceiver_apStateChangedToDisabled_shouldUpdatePreferenceSummary() { public void testReceiver_apStateChangedToDisabled_shouldUpdatePreferenceSummary() {
mController.displayPreference(mScreen); mController.displayPreference(mScreen);
final BroadcastReceiver receiver = ReflectionHelpers.getField(mController, "mReceiver"); receiveApStateChangedBroadcast(WifiManager.WIFI_AP_STATE_DISABLED);
final Intent broadcast = new Intent(WifiManager.WIFI_AP_STATE_CHANGED_ACTION);
broadcast.putExtra(WifiManager.EXTRA_WIFI_AP_STATE, WifiManager.WIFI_AP_STATE_DISABLED);
receiver.onReceive(RuntimeEnvironment.application, broadcast);
assertThat(mPreference.getSummary().toString()).isEqualTo( assertThat(mPreference.getSummary().toString()).isEqualTo(
RuntimeEnvironment.application.getString(R.string.wifi_hotspot_off_subtext)); RuntimeEnvironment.application.getString(R.string.wifi_hotspot_off_subtext));
} }
@@ -174,16 +169,28 @@ public class WifiTetherPreferenceControllerTest {
@Test @Test
public void testReceiver_apStateChangedToDisabling_shouldUpdatePreferenceSummary() { public void testReceiver_apStateChangedToDisabling_shouldUpdatePreferenceSummary() {
mController.displayPreference(mScreen); mController.displayPreference(mScreen);
final BroadcastReceiver receiver = ReflectionHelpers.getField(mController, "mReceiver"); receiveApStateChangedBroadcast(WifiManager.WIFI_AP_STATE_DISABLING);
final Intent broadcast = new Intent(WifiManager.WIFI_AP_STATE_CHANGED_ACTION);
broadcast.putExtra(WifiManager.EXTRA_WIFI_AP_STATE, WifiManager.WIFI_AP_STATE_DISABLING);
receiver.onReceive(RuntimeEnvironment.application, broadcast);
assertThat(mPreference.getSummary().toString()).isEqualTo( assertThat(mPreference.getSummary().toString()).isEqualTo(
RuntimeEnvironment.application.getString(R.string.wifi_tether_stopping)); RuntimeEnvironment.application.getString(R.string.wifi_tether_stopping));
} }
@Test
public void testReceiver_apStateChangedToEnablingOrEnabled_shouldNotUpdatePreferenceSummary() {
mController.displayPreference(mScreen);
receiveApStateChangedBroadcast(WifiManager.WIFI_AP_STATE_DISABLED);
assertThat(mPreference.getSummary().toString()).isEqualTo(
RuntimeEnvironment.application.getString(R.string.wifi_hotspot_off_subtext));
// When turning on the hotspot, we receive STATE_ENABLING followed by STATE_ENABLED. Neither
// of these should change the summary.
receiveApStateChangedBroadcast(WifiManager.WIFI_AP_STATE_ENABLING);
assertThat(mPreference.getSummary().toString()).isEqualTo(
RuntimeEnvironment.application.getString(R.string.wifi_hotspot_off_subtext));
receiveApStateChangedBroadcast(WifiManager.WIFI_AP_STATE_ENABLED);
assertThat(mPreference.getSummary().toString()).isEqualTo(
RuntimeEnvironment.application.getString(R.string.wifi_hotspot_off_subtext));
}
@Test @Test
public void testReceiver_goingToAirplaneMode_shouldClearPreferenceSummary() { public void testReceiver_goingToAirplaneMode_shouldClearPreferenceSummary() {
final ContentResolver cr = mock(ContentResolver.class); final ContentResolver cr = mock(ContentResolver.class);
@@ -248,4 +255,16 @@ public class WifiTetherPreferenceControllerTest {
onStopCalled = true; onStopCalled = true;
} }
} }
/**
* Helper to cause the controller to receive a WIFI_AP_STATE_CHANGED_ACTION with a specific
* state.
* @param state - the state, as specified by one of the WifiManager.WIFI_AP_STATE_* values
*/
private void receiveApStateChangedBroadcast(int state) {
final BroadcastReceiver receiver = ReflectionHelpers.getField(mController, "mReceiver");
final Intent broadcast = new Intent(WifiManager.WIFI_AP_STATE_CHANGED_ACTION);
broadcast.putExtra(WifiManager.EXTRA_WIFI_AP_STATE, state);
receiver.onReceive(RuntimeEnvironment.application, broadcast);
}
} }