From a3458ecb3c1b1a7dae295a6c728242dcc06a04f3 Mon Sep 17 00:00:00 2001 From: Rebecca Silberstein Date: Fri, 5 May 2017 11:04:04 -0700 Subject: WifiStateMachine: properly update wifi enabling When switching modes, WifiStateMachine should only update the wifi state to enabling when actually heading to connect mode. This is currently not the case and we end up seeing the UI update the wifi icon to show that wifi is starting up. Bug: 37925273 Test: frameworks/opt/net/wifi/tests/wifitests/runtests.sh Change-Id: Ie3a092c7805b64316c42f2781b14deb3986d2572 --- .../android/server/wifi/WifiStateMachineTest.java | 48 +++++++++++++++++++--- 1 file changed, 42 insertions(+), 6 deletions(-) (limited to 'tests') diff --git a/tests/wifitests/src/com/android/server/wifi/WifiStateMachineTest.java b/tests/wifitests/src/com/android/server/wifi/WifiStateMachineTest.java index 20dd11757..2e365a048 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiStateMachineTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiStateMachineTest.java @@ -26,6 +26,7 @@ import android.app.ActivityManager; import android.app.test.MockAnswerUtil.AnswerWithArguments; import android.app.test.TestAlarmManager; import android.content.Context; +import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.UserInfo; import android.content.res.Resources; @@ -86,6 +87,7 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentCaptor; +import org.mockito.ArgumentMatcher; import org.mockito.InOrder; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -309,6 +311,7 @@ public class WifiStateMachineTest { MockWifiMonitor mWifiMonitor; TestIpManager mTestIpManager; TestLooper mLooper; + Context mContext; @Mock WifiScanner mWifiScanner; @Mock SupplicantStateTracker mSupplicantStateTracker; @@ -381,17 +384,17 @@ public class WifiStateMachineTest { FrameworkFacade factory = getFrameworkFacade(); - Context context = getContext(); + mContext = getContext(); Resources resources = getMockResources(); - when(context.getResources()).thenReturn(resources); + when(mContext.getResources()).thenReturn(resources); - when(factory.getIntegerSetting(context, + when(factory.getIntegerSetting(mContext, Settings.Global.WIFI_FREQUENCY_BAND, WifiManager.WIFI_FREQUENCY_BAND_AUTO)).thenReturn( WifiManager.WIFI_FREQUENCY_BAND_AUTO); - when(factory.makeApConfigStore(eq(context), eq(mBackupManagerProxy))) + when(factory.makeApConfigStore(eq(mContext), eq(mBackupManagerProxy))) .thenReturn(mApConfigStore); when(factory.makeSupplicantStateTracker( @@ -407,7 +410,7 @@ public class WifiStateMachineTest { when(mApInterface.asBinder()).thenReturn(mApInterfaceBinder); when(mClientInterface.asBinder()).thenReturn(mClientInterfaceBinder); - mWsm = new WifiStateMachine(context, factory, mLooper.getLooper(), + mWsm = new WifiStateMachine(mContext, factory, mLooper.getLooper(), mUserManager, mWifiInjector, mBackupManagerProxy, mCountryCode, mWifiNative); mWsmThread = getWsmHandlerThread(mWsm); @@ -430,7 +433,7 @@ public class WifiStateMachineTest { } }; - channel.connect(context, handler, mWsm.getMessenger()); + channel.connect(mContext, handler, mWsm.getMessenger()); mLooper.dispatchAll(); /* Now channel is supposed to be connected */ @@ -465,6 +468,9 @@ public class WifiStateMachineTest { public void loadComponentsInStaMode() throws Exception { startSupplicantAndDispatchMessages(); + verify(mContext).sendStickyBroadcastAsUser( + (Intent) argThat(new WifiEnablingStateIntentMatcher()), eq(UserHandle.ALL)); + assertEquals("DisconnectedState", getCurrentState().getName()); } @@ -484,6 +490,9 @@ public class WifiStateMachineTest { mWsm.setSupplicantRunning(true); mLooper.dispatchAll(); assertEquals("InitialState", getCurrentState().getName()); + // we should not be sending a wifi enabling update + verify(mContext, never()).sendStickyBroadcastAsUser( + (Intent) argThat(new WifiEnablingStateIntentMatcher()), any()); } @Test @@ -571,6 +580,9 @@ public class WifiStateMachineTest { assertEquals(WifiStateMachine.SCAN_ONLY_MODE, mWsm.getOperationalModeForTest()); assertEquals("ScanModeState", getCurrentState().getName()); assertEquals(WifiManager.WIFI_STATE_DISABLED, mWsm.syncGetWifiState()); + verify(mContext, never()).sendStickyBroadcastAsUser( + (Intent) argThat(new WifiEnablingStateIntentMatcher()), any()); + // switch to connect mode and verify wifi is reported as enabled mWsm.setOperationalMode(WifiStateMachine.CONNECT_MODE); @@ -578,6 +590,12 @@ public class WifiStateMachineTest { assertEquals("DisconnectedState", getCurrentState().getName()); assertEquals(WifiStateMachine.CONNECT_MODE, mWsm.getOperationalModeForTest()); assertEquals(WifiManager.WIFI_STATE_ENABLED, mWsm.syncGetWifiState()); + verify(mContext).sendStickyBroadcastAsUser( + (Intent) argThat(new WifiEnablingStateIntentMatcher()), eq(UserHandle.ALL)); + + // reset the expectations on mContext since we did get an expected broadcast, but we should + // not on the next transition + reset(mContext); // now go back to scan mode with "wifi disabled" to verify the reported wifi state. mWsm.setOperationalMode(WifiStateMachine.SCAN_ONLY_WITH_WIFI_OFF_MODE); @@ -586,6 +604,8 @@ public class WifiStateMachineTest { mWsm.getOperationalModeForTest()); assertEquals("ScanModeState", getCurrentState().getName()); assertEquals(WifiManager.WIFI_STATE_DISABLED, mWsm.syncGetWifiState()); + verify(mContext, never()).sendStickyBroadcastAsUser( + (Intent) argThat(new WifiEnablingStateIntentMatcher()), any()); // now go to AP mode mWsm.setSupplicantRunning(false); @@ -595,8 +615,22 @@ public class WifiStateMachineTest { mLooper.dispatchAll(); assertEquals("SoftApState", getCurrentState().getName()); assertEquals(WifiManager.WIFI_STATE_DISABLED, mWsm.syncGetWifiState()); + verify(mContext, never()).sendStickyBroadcastAsUser( + (Intent) argThat(new WifiEnablingStateIntentMatcher()), any()); } + private class WifiEnablingStateIntentMatcher implements ArgumentMatcher { + @Override + public boolean matches(Intent intent) { + if (WifiManager.WIFI_STATE_CHANGED_ACTION != intent.getAction()) { + // not the correct type + return false; + } + return WifiManager.WIFI_STATE_ENABLING + == intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, + WifiManager.WIFI_STATE_DISABLED); + } + } /** * Test that mode changes for WifiStateMachine in the InitialState are realized when supplicant @@ -620,6 +654,8 @@ public class WifiStateMachineTest { startSupplicantAndDispatchMessages(); assertEquals("ScanModeState", getCurrentState().getName()); + verify(mContext, never()).sendStickyBroadcastAsUser( + (Intent) argThat(new WifiEnablingStateIntentMatcher()), any()); } /** -- cgit v1.2.3