diff options
author | Rebecca Silberstein <silberst@google.com> | 2017-05-20 23:03:58 +0000 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2017-05-20 23:03:58 +0000 |
commit | 3a0eab454bb4596be85dcdad32501a248ec1baff (patch) | |
tree | 6ca8e6f5073b1353966bbdfaa2e960a450ae70d9 | |
parent | 6bd16f20d6e69021b9d7652cd6551dfe14e07a46 (diff) | |
parent | a3e8c64fb19018c0a5ed0c8faf1dfe22a49aa17b (diff) |
Merge "WifiServiceImpl: implement startLOHS" into oc-dev
am: a3e8c64fb1
Change-Id: I9664d22daa17be0afef54289d9bf48c5b908f0cf
-rw-r--r-- | service/java/com/android/server/wifi/WifiServiceImpl.java | 37 | ||||
-rw-r--r-- | tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java | 234 |
2 files changed, 163 insertions, 108 deletions
diff --git a/service/java/com/android/server/wifi/WifiServiceImpl.java b/service/java/com/android/server/wifi/WifiServiceImpl.java index 703294ee5..aa1316d4e 100644 --- a/service/java/com/android/server/wifi/WifiServiceImpl.java +++ b/service/java/com/android/server/wifi/WifiServiceImpl.java @@ -26,6 +26,7 @@ import static android.net.wifi.WifiManager.WIFI_AP_STATE_DISABLED; import static android.net.wifi.WifiManager.WIFI_AP_STATE_DISABLING; import static android.net.wifi.WifiManager.WIFI_AP_STATE_FAILED; +import static com.android.server.connectivity.tethering.IControlsTethering.STATE_LOCAL_ONLY; import static com.android.server.connectivity.tethering.IControlsTethering.STATE_TETHERED; import static com.android.server.wifi.LocalOnlyHotspotRequestInfo.HOTSPOT_NO_ERROR; import static com.android.server.wifi.WifiController.CMD_AIRPLANE_TOGGLED; @@ -930,6 +931,11 @@ public class WifiServiceImpl extends IWifiManager.Stub { mLog.trace("startSoftAp uid=%").c(Binder.getCallingUid()).flush(); + // TODO: determine if we need to stop softap and clean up state if a tethering request comes + // from the user while we are just setting up. For now, the second CMD_START_AP will be + // ignored in WifiStateMachine. This will still bring up tethering and the registered LOHS + // requests will be cleared when we get the interface ip tethered status. + return startSoftApInternal(wifiConfig, STATE_TETHERED); } @@ -1093,7 +1099,6 @@ public class WifiServiceImpl extends IWifiManager.Stub { * Temporary method used for testing while startLocalOnlyHotspot is not fully implemented. This * method allows unit tests to register callbacks directly for testing mechanisms triggered by * softap mode changes. - * TODO: remove when startLocalOnlyHotspot is implemented. */ @VisibleForTesting void registerLOHSForTest(int pid, LocalOnlyHotspotRequestInfo request) { @@ -1142,19 +1147,16 @@ public class WifiServiceImpl extends IWifiManager.Stub { mLog.trace("startLocalOnlyHotspot uid=% pid=%").c(uid).c(pid).flush(); - // check current mode to see if we can start localOnlyHotspot - boolean apDisabled = - mWifiStateMachine.syncGetWifiApState() == WifiManager.WIFI_AP_STATE_DISABLED; - synchronized (mLocalOnlyHotspotRequests) { - if (!apDisabled && mLocalOnlyHotspotRequests.isEmpty()) { + // check if we are currently tethering + if (mIfaceIpModes.contains(WifiManager.IFACE_IP_MODE_TETHERED)) { // Tethering is enabled, cannot start LocalOnlyHotspot mLog.trace("Cannot start localOnlyHotspot when WiFi Tethering is active."); return LocalOnlyHotspotCallback.ERROR_INCOMPATIBLE_MODE; } // does this caller already have a request? - LocalOnlyHotspotRequestInfo request = mLocalOnlyHotspotRequests.get(uid); + LocalOnlyHotspotRequestInfo request = mLocalOnlyHotspotRequests.get(pid); if (request != null) { mLog.trace("caller already has an active request"); throw new IllegalStateException( @@ -1164,24 +1166,26 @@ public class WifiServiceImpl extends IWifiManager.Stub { // now create the new LOHS request info object request = new LocalOnlyHotspotRequestInfo(binder, messenger, new LocalOnlyRequestorCallback()); - // TODO: move this below when start is implemented - mLocalOnlyHotspotRequests.put(uid, request); - // check if LOHS is already active, if so, record the request and trigger the callback + // check current operating state and take action if needed if (mIfaceIpModes.contains(WifiManager.IFACE_IP_MODE_LOCAL_ONLY)) { + // LOHS is already active, send out what is running try { - Slog.d(TAG, "LOHS already up, send started"); + mLog.trace("LOHS already up, trigger onStarted callback"); request.sendHotspotStartedMessage(mLocalOnlyHotspotConfig); - // TODO: move this return value out of the try when start is implemented - return LocalOnlyHotspotCallback.REQUEST_REGISTERED; } catch (RemoteException e) { - mLocalOnlyHotspotRequests.remove(uid); return LocalOnlyHotspotCallback.ERROR_GENERIC; } + } else if (mLocalOnlyHotspotRequests.isEmpty()) { + // this is the first request, then set up our config and start LOHS + mLocalOnlyHotspotConfig = + WifiApConfigStore.generateLocalOnlyHotspotConfig(mContext); + startSoftApInternal(mLocalOnlyHotspotConfig, STATE_LOCAL_ONLY); } - } - throw new UnsupportedOperationException("LocalOnlyHotspot is still in development"); + mLocalOnlyHotspotRequests.put(pid, request); + return LocalOnlyHotspotCallback.REQUEST_REGISTERED; + } } /** @@ -1224,6 +1228,7 @@ public class WifiServiceImpl extends IWifiManager.Stub { if (mLocalOnlyHotspotRequests.isEmpty()) { mLocalOnlyHotspotConfig = null; + updateInterfaceIpStateInternal(null, WifiManager.IFACE_IP_MODE_UNSPECIFIED); // if that was the last caller, then call stopSoftAp as WifiService long identity = Binder.clearCallingIdentity(); try { diff --git a/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java b/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java index 4646455a5..65869aad1 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java @@ -16,6 +16,7 @@ package com.android.server.wifi; +import static android.net.wifi.WifiManager.HOTSPOT_FAILED; import static android.net.wifi.WifiManager.HOTSPOT_STARTED; import static android.net.wifi.WifiManager.HOTSPOT_STOPPED; import static android.net.wifi.WifiManager.IFACE_IP_MODE_CONFIGURATION_ERROR; @@ -41,6 +42,8 @@ import static com.android.server.wifi.WifiController.CMD_WIFI_TOGGLED; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyString; @@ -58,6 +61,7 @@ import android.net.IpConfiguration; import android.net.wifi.ScanSettings; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiManager; +import android.net.wifi.WifiManager.LocalOnlyHotspotCallback; import android.os.Handler; import android.os.HandlerThread; import android.os.IBinder; @@ -741,19 +745,23 @@ public class WifiServiceImplTest { anyInt(), anyInt(), (ScanSettings) eq(null), any(WorkSource.class)); } - /** - * Verify that the call to startLocalOnlyHotspot throws the UnsupportedOperationException - * until the implementation is complete. - */ - @Test(expected = UnsupportedOperationException.class) - public void testStartLocalOnlyHotspotNotSupported() { + private void registerLOHSRequestFull() { // allow test to proceed without a permission check failure when(mSettingsStore.getLocationModeSetting(mContext)) .thenReturn(LOCATION_MODE_HIGH_ACCURACY); when(mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING)) .thenReturn(false); - when(mWifiStateMachine.syncGetWifiApState()).thenReturn(WifiManager.WIFI_AP_STATE_DISABLED); - mWifiServiceImpl.startLocalOnlyHotspot(mAppMessenger, mAppBinder); + int result = mWifiServiceImpl.startLocalOnlyHotspot(mAppMessenger, mAppBinder); + assertEquals(LocalOnlyHotspotCallback.REQUEST_REGISTERED, result); + } + + /** + * Verify that the call to startLocalOnlyHotspot returns REQUEST_REGISTERED when successfully + * called. + */ + @Test + public void testStartLocalOnlyHotspotSingleRegistrationReturnsRequestRegistered() { + registerLOHSRequestFull(); } /** @@ -798,7 +806,8 @@ public class WifiServiceImplTest { public void testHotspotDoesNotStartWhenAlreadyTethering() { when(mSettingsStore.getLocationModeSetting(mContext)) .thenReturn(LOCATION_MODE_HIGH_ACCURACY); - when(mWifiStateMachine.syncGetWifiApState()).thenReturn(WifiManager.WIFI_AP_STATE_ENABLED); + mWifiServiceImpl.updateInterfaceIpState(WIFI_IFACE_NAME, IFACE_IP_MODE_TETHERED); + mLooper.dispatchAll(); int returnCode = mWifiServiceImpl.startLocalOnlyHotspot(mAppMessenger, mAppBinder); assertEquals(ERROR_INCOMPATIBLE_MODE, returnCode); } @@ -812,12 +821,22 @@ public class WifiServiceImplTest { .thenReturn(LOCATION_MODE_HIGH_ACCURACY); when(mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING)) .thenReturn(true); - when(mWifiStateMachine.syncGetWifiApState()).thenReturn(WifiManager.WIFI_AP_STATE_ENABLED); int returnCode = mWifiServiceImpl.startLocalOnlyHotspot(mAppMessenger, mAppBinder); assertEquals(ERROR_TETHERING_DISALLOWED, returnCode); } /** + * Verify that callers can only have one registered LOHS request. + */ + @Test(expected = IllegalStateException.class) + public void testStartLocalOnlyHotspotThrowsExceptionWhenCallerAlreadyRegistered() { + registerLOHSRequestFull(); + + // now do the second request that will fail + mWifiServiceImpl.startLocalOnlyHotspot(mAppMessenger, mAppBinder); + } + + /** * Verify that the call to stopLocalOnlyHotspot does not do anything when there aren't any * registered callers. */ @@ -838,8 +857,7 @@ public class WifiServiceImplTest { // register a request that will remain after the stopLOHS call mWifiServiceImpl.registerLOHSForTest(mPid, mRequestInfo); - // make an additional request for this test... using the current pid - mWifiServiceImpl.registerLOHSForTest(Process.myPid(), mRequestInfo2); + registerLOHSRequestFull(); // Since we are calling with the same pid, the second register call will be removed mWifiServiceImpl.stopLocalOnlyHotspot(); @@ -853,10 +871,10 @@ public class WifiServiceImplTest { */ @Test public void testStopLocalOnlyHotspotTriggersSoftApStopWithOneRegisteredRequest() { - // register a request so we have something to stop - use request2 so the pid check matches - mWifiServiceImpl.registerLOHSForTest(Process.myPid(), mRequestInfo2); + registerLOHSRequestFull(); + verify(mWifiController) + .sendMessage(eq(CMD_SET_AP), eq(1), eq(0), any(WifiConfiguration.class)); - // allow test to proceed without a permission check failure mWifiServiceImpl.stopLocalOnlyHotspot(); // there is was only one request registered, we should tear down softap verify(mWifiController).sendMessage(eq(CMD_SET_AP), eq(0), eq(0)); @@ -898,8 +916,11 @@ public class WifiServiceImplTest { LocalOnlyRequestorCallback binderDeathCallback = mWifiServiceImpl.new LocalOnlyRequestorCallback(); + // registering a request directly from the test will not trigger a message to start + // softap mode mWifiServiceImpl.registerLOHSForTest(mPid, mRequestInfo); - mWifiServiceImpl.registerLOHSForTest(mPid2, mRequestInfo2); + + registerLOHSRequestFull(); binderDeathCallback.onLocalOnlyHotspotRequestorDeath(mRequestInfo); verify(mWifiController, never()).sendMessage(eq(CMD_SET_AP), anyInt(), anyInt()); @@ -932,12 +953,15 @@ public class WifiServiceImplTest { verify(mContext).registerReceiver(mBroadcastReceiverCaptor.capture(), (IntentFilter) argThat(new IntentFilterMatcher())); - mWifiServiceImpl.registerLOHSForTest(TEST_PID, mRequestInfo); + registerLOHSRequestFull(); TestUtil.sendWifiApStateChanged(mBroadcastReceiverCaptor.getValue(), mContext, WIFI_AP_STATE_FAILED, WIFI_AP_STATE_DISABLED, SAP_START_FAILURE_GENERAL); - - verify(mRequestInfo).sendHotspotFailedMessage(ERROR_GENERIC); + mLooper.dispatchAll(); + verify(mHandler).handleMessage(mMessageCaptor.capture()); + Message message = mMessageCaptor.getValue(); + assertEquals(HOTSPOT_FAILED, message.what); + assertEquals(ERROR_GENERIC, message.arg1); } /** @@ -953,12 +977,16 @@ public class WifiServiceImplTest { verify(mContext).registerReceiver(mBroadcastReceiverCaptor.capture(), (IntentFilter) argThat(new IntentFilterMatcher())); - mWifiServiceImpl.registerLOHSForTest(TEST_PID, mRequestInfo); + registerLOHSRequestFull(); TestUtil.sendWifiApStateChanged(mBroadcastReceiverCaptor.getValue(), mContext, WIFI_AP_STATE_FAILED, WIFI_AP_STATE_DISABLED, SAP_START_FAILURE_NO_CHANNEL); - verify(mRequestInfo).sendHotspotFailedMessage(ERROR_NO_CHANNEL); + mLooper.dispatchAll(); + verify(mHandler).handleMessage(mMessageCaptor.capture()); + Message message = mMessageCaptor.getValue(); + assertEquals(HOTSPOT_FAILED, message.what); + assertEquals(ERROR_NO_CHANNEL, message.arg1); } /** @@ -974,15 +1002,22 @@ public class WifiServiceImplTest { verify(mContext).registerReceiver(mBroadcastReceiverCaptor.capture(), (IntentFilter) argThat(new IntentFilterMatcher())); - mWifiServiceImpl.registerLOHSForTest(TEST_PID, mRequestInfo); + registerLOHSRequestFull(); mWifiServiceImpl.updateInterfaceIpState(WIFI_IFACE_NAME, IFACE_IP_MODE_LOCAL_ONLY); mLooper.dispatchAll(); + verify(mHandler).handleMessage(mMessageCaptor.capture()); + Message message = mMessageCaptor.getValue(); + assertEquals(HOTSPOT_STARTED, message.what); + reset(mHandler); TestUtil.sendWifiApStateChanged(mBroadcastReceiverCaptor.getValue(), mContext, WIFI_AP_STATE_DISABLING, WIFI_AP_STATE_ENABLED, HOTSPOT_NO_ERROR); - verify(mRequestInfo).sendHotspotStoppedMessage(); + mLooper.dispatchAll(); + verify(mHandler).handleMessage(mMessageCaptor.capture()); + message = mMessageCaptor.getValue(); + assertEquals(HOTSPOT_STOPPED, message.what); } @@ -999,15 +1034,22 @@ public class WifiServiceImplTest { verify(mContext).registerReceiver(mBroadcastReceiverCaptor.capture(), (IntentFilter) argThat(new IntentFilterMatcher())); - mWifiServiceImpl.registerLOHSForTest(TEST_PID, mRequestInfo); + registerLOHSRequestFull(); mWifiServiceImpl.updateInterfaceIpState(WIFI_IFACE_NAME, IFACE_IP_MODE_LOCAL_ONLY); mLooper.dispatchAll(); + verify(mHandler).handleMessage(mMessageCaptor.capture()); + Message message = mMessageCaptor.getValue(); + assertEquals(HOTSPOT_STARTED, message.what); + reset(mHandler); TestUtil.sendWifiApStateChanged(mBroadcastReceiverCaptor.getValue(), mContext, WIFI_AP_STATE_DISABLED, WIFI_AP_STATE_DISABLING, HOTSPOT_NO_ERROR); - verify(mRequestInfo).sendHotspotStoppedMessage(); + mLooper.dispatchAll(); + verify(mHandler).handleMessage(mMessageCaptor.capture()); + message = mMessageCaptor.getValue(); + assertEquals(HOTSPOT_STOPPED, message.what); } /** @@ -1023,11 +1065,13 @@ public class WifiServiceImplTest { verify(mContext).registerReceiver(mBroadcastReceiverCaptor.capture(), (IntentFilter) argThat(new IntentFilterMatcher())); - mWifiServiceImpl.registerLOHSForTest(TEST_PID, mRequestInfo); + registerLOHSRequestFull(); TestUtil.sendWifiApStateChanged(mBroadcastReceiverCaptor.getValue(), mContext, WIFI_AP_STATE_ENABLED, WIFI_AP_STATE_DISABLED, HOTSPOT_NO_ERROR); - verifyNoMoreInteractions(mRequestInfo); + + mLooper.dispatchAll(); + verifyNoMoreInteractions(mHandler); } /** @@ -1044,17 +1088,24 @@ public class WifiServiceImplTest { verify(mContext).registerReceiver(mBroadcastReceiverCaptor.capture(), (IntentFilter) argThat(new IntentFilterMatcher())); - mWifiServiceImpl.registerLOHSForTest(TEST_PID, mRequestInfo); + registerLOHSRequestFull(); mWifiServiceImpl.updateInterfaceIpState(WIFI_IFACE_NAME, IFACE_IP_MODE_LOCAL_ONLY); mLooper.dispatchAll(); + verify(mHandler).handleMessage(mMessageCaptor.capture()); + Message message = mMessageCaptor.getValue(); + assertEquals(HOTSPOT_STARTED, message.what); + reset(mHandler); TestUtil.sendWifiApStateChanged(mBroadcastReceiverCaptor.getValue(), mContext, WIFI_AP_STATE_DISABLING, WIFI_AP_STATE_ENABLED, HOTSPOT_NO_ERROR); TestUtil.sendWifiApStateChanged(mBroadcastReceiverCaptor.getValue(), mContext, WIFI_AP_STATE_DISABLED, WIFI_AP_STATE_DISABLING, HOTSPOT_NO_ERROR); - verify(mRequestInfo).sendHotspotStoppedMessage(); + mLooper.dispatchAll(); + verify(mHandler).handleMessage(mMessageCaptor.capture()); + message = mMessageCaptor.getValue(); + assertEquals(HOTSPOT_STOPPED, message.what); } /** @@ -1070,14 +1121,18 @@ public class WifiServiceImplTest { verify(mContext).registerReceiver(mBroadcastReceiverCaptor.capture(), (IntentFilter) argThat(new IntentFilterMatcher())); - mWifiServiceImpl.registerLOHSForTest(TEST_PID, mRequestInfo); + registerLOHSRequestFull(); TestUtil.sendWifiApStateChanged(mBroadcastReceiverCaptor.getValue(), mContext, WIFI_AP_STATE_FAILED, WIFI_AP_STATE_FAILED, ERROR_GENERIC); TestUtil.sendWifiApStateChanged(mBroadcastReceiverCaptor.getValue(), mContext, WIFI_AP_STATE_FAILED, WIFI_AP_STATE_FAILED, ERROR_GENERIC); - verify(mRequestInfo).sendHotspotFailedMessage(ERROR_GENERIC); + mLooper.dispatchAll(); + verify(mHandler).handleMessage(mMessageCaptor.capture()); + Message message = mMessageCaptor.getValue(); + assertEquals(HOTSPOT_FAILED, message.what); + assertEquals(ERROR_GENERIC, message.arg1); } /** @@ -1094,9 +1149,9 @@ public class WifiServiceImplTest { (IntentFilter) argThat(new IntentFilterMatcher())); // make an additional request for this test - LocalOnlyHotspotRequestInfo request2 = mock(LocalOnlyHotspotRequestInfo.class); mWifiServiceImpl.registerLOHSForTest(TEST_PID, mRequestInfo); - mWifiServiceImpl.registerLOHSForTest(TEST_PID2, request2); + + registerLOHSRequestFull(); TestUtil.sendWifiApStateChanged(mBroadcastReceiverCaptor.getValue(), mContext, WIFI_AP_STATE_FAILED, WIFI_AP_STATE_FAILED, ERROR_GENERIC); @@ -1104,7 +1159,11 @@ public class WifiServiceImplTest { WIFI_AP_STATE_FAILED, WIFI_AP_STATE_FAILED, ERROR_GENERIC); verify(mRequestInfo).sendHotspotFailedMessage(ERROR_GENERIC); - verify(request2).sendHotspotFailedMessage(ERROR_GENERIC); + mLooper.dispatchAll(); + verify(mHandler).handleMessage(mMessageCaptor.capture()); + Message message = mMessageCaptor.getValue(); + assertEquals(HOTSPOT_FAILED, message.what); + assertEquals(ERROR_GENERIC, message.arg1); } /** @@ -1121,16 +1180,17 @@ public class WifiServiceImplTest { verify(mContext).registerReceiver(mBroadcastReceiverCaptor.capture(), (IntentFilter) argThat(new IntentFilterMatcher())); - // make an additional request for this test - LocalOnlyHotspotRequestInfo request2 = mock(LocalOnlyHotspotRequestInfo.class); mWifiServiceImpl.registerLOHSForTest(TEST_PID, mRequestInfo); - mWifiServiceImpl.registerLOHSForTest(TEST_PID2, request2); + + registerLOHSRequestFull(); mWifiServiceImpl.updateInterfaceIpState(WIFI_IFACE_NAME, IFACE_IP_MODE_LOCAL_ONLY); mLooper.dispatchAll(); - verify(mRequestInfo).sendHotspotStartedMessage(any()); - verify(request2).sendHotspotStartedMessage(any()); + verify(mHandler).handleMessage(mMessageCaptor.capture()); + Message message = mMessageCaptor.getValue(); + assertEquals(HOTSPOT_STARTED, message.what); + reset(mHandler); TestUtil.sendWifiApStateChanged(mBroadcastReceiverCaptor.getValue(), mContext, WIFI_AP_STATE_DISABLING, WIFI_AP_STATE_ENABLED, HOTSPOT_NO_ERROR); @@ -1138,7 +1198,10 @@ public class WifiServiceImplTest { WIFI_AP_STATE_DISABLED, WIFI_AP_STATE_DISABLING, HOTSPOT_NO_ERROR); verify(mRequestInfo).sendHotspotStoppedMessage(); - verify(request2).sendHotspotStoppedMessage(); + mLooper.dispatchAll(); + verify(mHandler).handleMessage(mMessageCaptor.capture()); + message = mMessageCaptor.getValue(); + assertEquals(HOTSPOT_STOPPED, message.what); } /** @@ -1155,10 +1218,8 @@ public class WifiServiceImplTest { verify(mContext).registerReceiver(mBroadcastReceiverCaptor.capture(), (IntentFilter) argThat(new IntentFilterMatcher())); - // make an additional request for this test - LocalOnlyHotspotRequestInfo request2 = mock(LocalOnlyHotspotRequestInfo.class); mWifiServiceImpl.registerLOHSForTest(TEST_PID, mRequestInfo); - mWifiServiceImpl.registerLOHSForTest(TEST_PID2, request2); + mWifiServiceImpl.registerLOHSForTest(TEST_PID2, mRequestInfo2); TestUtil.sendWifiApStateChanged(mBroadcastReceiverCaptor.getValue(), mContext, WIFI_AP_STATE_DISABLING, WIFI_AP_STATE_ENABLED, HOTSPOT_NO_ERROR); @@ -1166,7 +1227,7 @@ public class WifiServiceImplTest { WIFI_AP_STATE_DISABLED, WIFI_AP_STATE_DISABLING, HOTSPOT_NO_ERROR); verify(mRequestInfo).sendHotspotFailedMessage(ERROR_GENERIC); - verify(request2).sendHotspotFailedMessage(ERROR_GENERIC); + verify(mRequestInfo2).sendHotspotFailedMessage(ERROR_GENERIC); } /** @@ -1190,17 +1251,19 @@ public class WifiServiceImplTest { @Test public void testRegisteredLocalOnlyHotspotRequestorsGetOnStartedCallbackWhenReady() throws Exception { - // make an additional request for this test - LocalOnlyHotspotRequestInfo request2 = mock(LocalOnlyHotspotRequestInfo.class); + registerLOHSRequestFull(); + mWifiServiceImpl.registerLOHSForTest(TEST_PID, mRequestInfo); - mWifiServiceImpl.registerLOHSForTest(TEST_PID2, request2); mWifiServiceImpl.updateInterfaceIpState(WIFI_IFACE_NAME, IFACE_IP_MODE_LOCAL_ONLY); mLooper.dispatchAll(); - // until startLOHS is implmented, this call will send a null config using the test-only - // registration - verify(mRequestInfo).sendHotspotStartedMessage(eq(null)); - verify(request2).sendHotspotStartedMessage(eq(null)); + verify(mRequestInfo).sendHotspotStartedMessage(any(WifiConfiguration.class)); + + mLooper.dispatchAll(); + verify(mHandler).handleMessage(mMessageCaptor.capture()); + Message message = mMessageCaptor.getValue(); + assertEquals(HOTSPOT_STARTED, message.what); + assertNotNull((WifiConfiguration) message.obj); } /** @@ -1212,18 +1275,18 @@ public class WifiServiceImplTest { throws Exception { mWifiServiceImpl.registerLOHSForTest(TEST_PID, mRequestInfo); - // allow test to proceed without a permission check failure - when(mSettingsStore.getLocationModeSetting(mContext)) - .thenReturn(LOCATION_MODE_HIGH_ACCURACY); mWifiServiceImpl.updateInterfaceIpState(WIFI_IFACE_NAME, IFACE_IP_MODE_LOCAL_ONLY); mLooper.dispatchAll(); - when(mWifiStateMachine.syncGetWifiApState()).thenReturn(WifiManager.WIFI_AP_STATE_DISABLED); - mWifiServiceImpl.startLocalOnlyHotspot(mAppMessenger, mAppBinder); + registerLOHSRequestFull(); + mLooper.dispatchAll(); verify(mHandler).handleMessage(mMessageCaptor.capture()); - assertEquals(HOTSPOT_STARTED, mMessageCaptor.getValue().what); + Message message = mMessageCaptor.getValue(); + assertEquals(HOTSPOT_STARTED, message.what); + // since the first request was registered out of band, the config will be null + assertNull((WifiConfiguration) message.obj); } /** @@ -1233,20 +1296,24 @@ public class WifiServiceImplTest { */ @Test public void testCallOnFailedLocalOnlyHotspotRequestWhenIpConfigFails() throws Exception { - mWifiServiceImpl.registerLOHSForTest(TEST_PID, mRequestInfo); + registerLOHSRequestFull(); mWifiServiceImpl.updateInterfaceIpState(WIFI_IFACE_NAME, IFACE_IP_MODE_CONFIGURATION_ERROR); mLooper.dispatchAll(); - verify(mRequestInfo).sendHotspotFailedMessage(ERROR_GENERIC); + verify(mHandler).handleMessage(mMessageCaptor.capture()); + Message message = mMessageCaptor.getValue(); + assertEquals(HOTSPOT_FAILED, message.what); + assertEquals(ERROR_GENERIC, message.arg1); // sendMessage should only happen once since the requestor should be unregistered - reset(mRequestInfo); + reset(mHandler); + // send HOTSPOT_FAILED message should only happen once since the requestor should be + // unregistered mWifiServiceImpl.updateInterfaceIpState(WIFI_IFACE_NAME, IFACE_IP_MODE_CONFIGURATION_ERROR); mLooper.dispatchAll(); - - verifyNoMoreInteractions(mRequestInfo); + verify(mHandler, never()).handleMessage(any(Message.class)); } /** @@ -1255,20 +1322,22 @@ public class WifiServiceImplTest { */ @Test public void testCallOnFailedLocalOnlyHotspotRequestWhenTetheringStarts() throws Exception { - mWifiServiceImpl.registerLOHSForTest(TEST_PID, mRequestInfo); + registerLOHSRequestFull(); mWifiServiceImpl.updateInterfaceIpState(WIFI_IFACE_NAME, IFACE_IP_MODE_TETHERED); mLooper.dispatchAll(); - verify(mRequestInfo).sendHotspotFailedMessage(ERROR_INCOMPATIBLE_MODE); + verify(mHandler).handleMessage(mMessageCaptor.capture()); + Message message = mMessageCaptor.getValue(); + assertEquals(HOTSPOT_FAILED, message.what); + assertEquals(ERROR_INCOMPATIBLE_MODE, message.arg1); // sendMessage should only happen once since the requestor should be unregistered - reset(mRequestInfo); + reset(mHandler); mWifiServiceImpl.updateInterfaceIpState(WIFI_IFACE_NAME, IFACE_IP_MODE_TETHERED); mLooper.dispatchAll(); - - verifyNoMoreInteractions(mRequestInfo); + verify(mHandler, never()).handleMessage(any(Message.class)); } /** @@ -1278,18 +1347,8 @@ public class WifiServiceImplTest { @Test public void testRegisterLocalOnlyHotspotRequestWhenStoppedDoesNotGetOnStoppedCallback() throws Exception { - // allow test to proceed without a permission check failure - when(mSettingsStore.getLocationModeSetting(mContext)) - .thenReturn(LOCATION_MODE_HIGH_ACCURACY); - when(mWifiStateMachine.syncGetWifiApState()).thenReturn(WifiManager.WIFI_AP_STATE_DISABLED); - - try { - mWifiServiceImpl.startLocalOnlyHotspot(mAppMessenger, mAppBinder); - mLooper.dispatchAll(); - } catch (UnsupportedOperationException e) { - // TODO: this is expected and the check will be removed when startLOHS is implemented. - // For now, this exception is a good sign that hotspot state was cleared. - } + registerLOHSRequestFull(); + mLooper.dispatchAll(); verify(mHandler, never()).handleMessage(any(Message.class)); } @@ -1310,15 +1369,10 @@ public class WifiServiceImplTest { // register a request so we don't drop the LOHS interface ip update mWifiServiceImpl.registerLOHSForTest(TEST_PID, mRequestInfo); - // allow test to proceed without a permission check failure - when(mSettingsStore.getLocationModeSetting(mContext)) - .thenReturn(LOCATION_MODE_HIGH_ACCURACY); mWifiServiceImpl.updateInterfaceIpState(WIFI_IFACE_NAME, IFACE_IP_MODE_LOCAL_ONLY); mLooper.dispatchAll(); - when(mWifiStateMachine.syncGetWifiApState()).thenReturn(WifiManager.WIFI_AP_STATE_DISABLED); - - mWifiServiceImpl.startLocalOnlyHotspot(mAppMessenger, mAppBinder); + registerLOHSRequestFull(); mLooper.dispatchAll(); verify(mHandler).handleMessage(mMessageCaptor.capture()); @@ -1341,13 +1395,9 @@ public class WifiServiceImplTest { Messenger messenger2 = new Messenger(mHandler); IBinder binder2 = mock(IBinder.class); - try { - mWifiServiceImpl.startLocalOnlyHotspot(messenger2, binder2); - mLooper.dispatchAll(); - } catch (UnsupportedOperationException e) { - // TODO: this is expected and the check will be removed when startLOHS is implemented. - // For now, this exception is a good sign that hotspot state was cleared. - } + int result = mWifiServiceImpl.startLocalOnlyHotspot(messenger2, binder2); + assertEquals(LocalOnlyHotspotCallback.REQUEST_REGISTERED, result); + mLooper.dispatchAll(); verify(mHandler, never()).handleMessage(any(Message.class)); } |