import static android.net.wifi.WifiManager.LocalOnlyHotspotCallback.ERROR_NO_CHANNEL;
import static android.net.wifi.WifiManager.LocalOnlyHotspotCallback.ERROR_TETHERING_DISALLOWED;
import static android.net.wifi.WifiManager.LocalOnlyHotspotCallback.REQUEST_REGISTERED;
+import static android.net.wifi.WifiManager.SAP_START_FAILURE_GENERAL;
+import static android.net.wifi.WifiManager.SAP_START_FAILURE_NO_CHANNEL;
+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_ENABLED;
+import static android.net.wifi.WifiManager.WIFI_AP_STATE_ENABLING;
+import static android.net.wifi.WifiManager.WIFI_AP_STATE_FAILED;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import android.net.wifi.WifiManager.LocalOnlyHotspotObserver;
import android.net.wifi.WifiManager.LocalOnlyHotspotReservation;
import android.net.wifi.WifiManager.LocalOnlyHotspotSubscription;
+import android.net.wifi.WifiManager.SoftApCallback;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
@Mock ApplicationInfo mApplicationInfo;
@Mock WifiConfiguration mApConfig;
@Mock IBinder mAppBinder;
+ @Mock SoftApCallback mSoftApCallback;
private Handler mHandler;
private TestLooper mLooper;
}
/**
+ * Verify an IllegalArgumentException is thrown if callback is not provided.
+ */
+ @Test(expected = IllegalArgumentException.class)
+ public void registerSoftApCallbackThrowsIllegalArgumentExceptionOnNullArgumentForCallback() {
+ mWifiManager.registerSoftApCallback(null, mHandler);
+ }
+
+ /**
+ * Verify main looper is used when handler is not provided.
+ */
+ @Test
+ public void registerSoftApCallbackUsesMainLooperOnNullArgumentForHandler() {
+ when(mContext.getMainLooper()).thenReturn(mLooper.getLooper());
+ mWifiManager.registerSoftApCallback(mSoftApCallback, null);
+ verify(mContext).getMainLooper();
+ }
+
+ /**
+ * Verify the call to registerSoftApCallback goes to WifiServiceImpl.
+ */
+ @Test
+ public void registerSoftApCallbackCallGoesToWifiServiceImpl() throws Exception {
+ mWifiManager.registerSoftApCallback(mSoftApCallback, mHandler);
+ verify(mWifiService).registerSoftApCallback(any(IBinder.class),
+ any(ISoftApCallback.Stub.class));
+ }
+
+ /*
+ * Verify client provided callback is being called through callback proxy
+ */
+ @Test
+ public void softApCallbackProxyCallsOnStateChanged() throws Exception {
+ ArgumentCaptor<ISoftApCallback.Stub> callbackCaptor =
+ ArgumentCaptor.forClass(ISoftApCallback.Stub.class);
+ mWifiManager.registerSoftApCallback(mSoftApCallback, mHandler);
+ verify(mWifiService).registerSoftApCallback(any(IBinder.class), callbackCaptor.capture());
+
+ callbackCaptor.getValue().onStateChanged(WIFI_AP_STATE_ENABLED, 0);
+ mLooper.dispatchAll();
+ verify(mSoftApCallback).onStateChanged(WIFI_AP_STATE_ENABLED, 0);
+ }
+
+ /*
+ * Verify client provided callback is being called through callback proxy
+ */
+ @Test
+ public void softApCallbackProxyCallsOnNumClientsChanged() throws Exception {
+ ArgumentCaptor<ISoftApCallback.Stub> callbackCaptor =
+ ArgumentCaptor.forClass(ISoftApCallback.Stub.class);
+ mWifiManager.registerSoftApCallback(mSoftApCallback, mHandler);
+ verify(mWifiService).registerSoftApCallback(any(IBinder.class), callbackCaptor.capture());
+
+ final int testNumClients = 3;
+ callbackCaptor.getValue().onNumClientsChanged(testNumClients);
+ mLooper.dispatchAll();
+ verify(mSoftApCallback).onNumClientsChanged(testNumClients);
+ }
+
+ /*
+ * Verify client provided callback is being called through callback proxy on multiple events
+ */
+ @Test
+ public void softApCallbackProxyCallsOnMultipleUpdates() throws Exception {
+ ArgumentCaptor<ISoftApCallback.Stub> callbackCaptor =
+ ArgumentCaptor.forClass(ISoftApCallback.Stub.class);
+ mWifiManager.registerSoftApCallback(mSoftApCallback, mHandler);
+ verify(mWifiService).registerSoftApCallback(any(IBinder.class), callbackCaptor.capture());
+
+ final int testNumClients = 5;
+ callbackCaptor.getValue().onStateChanged(WIFI_AP_STATE_ENABLING, 0);
+ callbackCaptor.getValue().onNumClientsChanged(testNumClients);
+ callbackCaptor.getValue().onStateChanged(WIFI_AP_STATE_FAILED, SAP_START_FAILURE_GENERAL);
+
+ mLooper.dispatchAll();
+ verify(mSoftApCallback).onStateChanged(WIFI_AP_STATE_ENABLING, 0);
+ verify(mSoftApCallback).onNumClientsChanged(testNumClients);
+ verify(mSoftApCallback).onStateChanged(WIFI_AP_STATE_FAILED, SAP_START_FAILURE_GENERAL);
+ }
+
+ /*
+ * Verify client provided callback is being called on the correct thread
+ */
+ @Test
+ public void softApCallbackIsCalledOnCorrectThread() throws Exception {
+ ArgumentCaptor<ISoftApCallback.Stub> callbackCaptor =
+ ArgumentCaptor.forClass(ISoftApCallback.Stub.class);
+ TestLooper altLooper = new TestLooper();
+ Handler altHandler = new Handler(altLooper.getLooper());
+ mWifiManager.registerSoftApCallback(mSoftApCallback, altHandler);
+ verify(mWifiService).registerSoftApCallback(any(IBinder.class), callbackCaptor.capture());
+
+ callbackCaptor.getValue().onStateChanged(WIFI_AP_STATE_ENABLED, 0);
+ altLooper.dispatchAll();
+ verify(mSoftApCallback).onStateChanged(WIFI_AP_STATE_ENABLED, 0);
+ }
+
+ /**
+ * Verify that the handler provided by the caller is used for registering soft AP callback.
+ */
+ @Test
+ public void testCorrectLooperIsUsedForSoftApCallbackHandler() throws Exception {
+ mWifiManager.registerSoftApCallback(mSoftApCallback, mHandler);
+ mLooper.dispatchAll();
+ verify(mWifiService).registerSoftApCallback(any(IBinder.class),
+ any(ISoftApCallback.Stub.class));
+ verify(mContext, never()).getMainLooper();
+ }
+
+ /**
* Verify that the handler provided by the caller is used for the observer.
*/
@Test