summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorWei Wang <weiwa@google.com>2016-02-16 23:39:12 -0800
committerWei Wang <weiwa@google.com>2016-02-19 01:20:03 -0800
commitc3b22ef2e6bbccd048e1012160b75d14353ab894 (patch)
tree8b7aca5e83000591ac0dc0da22dfc947e3c7ad4c /tests
parenta2a3c5080bb6ccf6afbb8599d00b948963bfb23a (diff)
Add test case for duplicate listeners in WifiScanner.
Bug:26663377 Change-Id: I89c7936b215251c2afb20a49de1d03184e9b4b21
Diffstat (limited to 'tests')
-rw-r--r--tests/wifitests/src/com/android/server/wifi/BidirectionalAsyncChannelServer.java86
-rw-r--r--tests/wifitests/src/com/android/server/wifi/WifiScannerTest.java110
2 files changed, 196 insertions, 0 deletions
diff --git a/tests/wifitests/src/com/android/server/wifi/BidirectionalAsyncChannelServer.java b/tests/wifitests/src/com/android/server/wifi/BidirectionalAsyncChannelServer.java
new file mode 100644
index 000000000..6cc0e907c
--- /dev/null
+++ b/tests/wifitests/src/com/android/server/wifi/BidirectionalAsyncChannelServer.java
@@ -0,0 +1,86 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.wifi;
+
+import android.content.Context;
+import android.os.Handler;
+import android.os.Looper;
+import android.os.Message;
+import android.os.Messenger;
+import android.util.Log;
+
+import com.android.internal.util.AsyncChannel;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Provides an interface for the server side implementation of a bidirectional channel as described
+ * in {@link com.android.internal.util.AsyncChannel}.
+ */
+public class BidirectionalAsyncChannelServer {
+
+ private static final String TAG = "BidirectionalAsyncChannelServer";
+
+ // Keeps track of incoming clients, which are identifiable by their messengers.
+ private final Map<Messenger, AsyncChannel> mClients = new HashMap<>();
+
+ private Messenger mMessenger;
+
+ public BidirectionalAsyncChannelServer(final Context context, final Looper looper,
+ final Handler messageHandler) {
+ Handler handler = new Handler(looper) {
+ @Override
+ public void handleMessage(Message msg) {
+ AsyncChannel channel = mClients.get(msg.replyTo);
+ switch (msg.what) {
+ case AsyncChannel.CMD_CHANNEL_FULL_CONNECTION:
+ if (channel != null) {
+ Log.d(TAG, "duplicate client connection: " + msg.sendingUid);
+ channel.replyToMessage(msg,
+ AsyncChannel.CMD_CHANNEL_FULLY_CONNECTED,
+ AsyncChannel.STATUS_FULL_CONNECTION_REFUSED_ALREADY_CONNECTED);
+ } else {
+ channel = new AsyncChannel();
+ mClients.put(msg.replyTo, channel);
+ channel.connected(context, this, msg.replyTo);
+ channel.replyToMessage(msg, AsyncChannel.CMD_CHANNEL_FULLY_CONNECTED,
+ AsyncChannel.STATUS_SUCCESSFUL);
+ }
+ break;
+ case AsyncChannel.CMD_CHANNEL_DISCONNECT:
+ channel.disconnect();
+ break;
+
+ case AsyncChannel.CMD_CHANNEL_DISCONNECTED:
+ mClients.remove(msg.replyTo);
+ break;
+
+ default:
+ messageHandler.handleMessage(msg);
+ break;
+ }
+ }
+ };
+ mMessenger = new Messenger(handler);
+ }
+
+ public Messenger getMessenger() {
+ return mMessenger;
+ }
+
+}
diff --git a/tests/wifitests/src/com/android/server/wifi/WifiScannerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiScannerTest.java
new file mode 100644
index 000000000..34140b521
--- /dev/null
+++ b/tests/wifitests/src/com/android/server/wifi/WifiScannerTest.java
@@ -0,0 +1,110 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.wifi;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.atLeastOnce;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.validateMockitoUsage;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.content.Context;
+import android.net.wifi.IWifiScanner;
+import android.net.wifi.WifiScanner;
+import android.net.wifi.WifiScanner.BssidInfo;
+import android.net.wifi.WifiScanner.BssidListener;
+import android.os.Handler;
+import android.os.Message;
+import android.test.suitebuilder.annotation.SmallTest;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+/**
+ * Unit tests for {@link WifiScanner}.
+ */
+@SmallTest
+public class WifiScannerTest {
+ @Mock
+ private Context mContext;
+ @Mock
+ private IWifiScanner mService;
+ @Mock
+ private BssidListener mBssidListener;
+
+ private WifiScanner mWifiScanner;
+ private MockLooper mLooper;
+ private Handler mHandler;
+
+ /**
+ * Setup before tests.
+ */
+ @Before
+ public void setUp() throws Exception {
+ MockitoAnnotations.initMocks(this);
+ mLooper = new MockLooper();
+ mHandler = mock(Handler.class);
+ BidirectionalAsyncChannelServer server = new BidirectionalAsyncChannelServer(
+ mContext, mLooper.getLooper(), mHandler);
+ when(mService.getMessenger()).thenReturn(server.getMessenger());
+ mWifiScanner = new WifiScanner(mContext, mService, mLooper.getLooper(), false);
+ mLooper.dispatchAll();
+ }
+
+ /**
+ * Clean up after tests.
+ */
+ @After
+ public void cleanup() {
+ validateMockitoUsage();
+ }
+
+ private void verifySetHotlistMessage(Handler handler) {
+ ArgumentCaptor<Message> messageCaptor = ArgumentCaptor.forClass(Message.class);
+ verify(handler, atLeastOnce()).handleMessage(messageCaptor.capture());
+ assertEquals("message.what is not CMD_SET_HOTLIST",
+ WifiScanner.CMD_SET_HOTLIST,
+ messageCaptor.getValue().what);
+ }
+
+ /**
+ * Test duplicate listeners for bssid tracking.
+ */
+ @Test
+ public void testStartTrackingBssidsDuplicateListeners() throws Exception {
+ BssidInfo[] bssids = new BssidInfo[] {
+ new BssidInfo()
+ };
+
+ // First start tracking succeeds.
+ mWifiScanner.startTrackingBssids(bssids, -100, mBssidListener);
+ mLooper.dispatchAll();
+ verifySetHotlistMessage(mHandler);
+
+ // Second start tracking should fail.
+ mWifiScanner.startTrackingBssids(bssids, -100, mBssidListener);
+ mLooper.dispatchAll();
+ verify(mBssidListener).onFailure(eq(WifiScanner.REASON_DUPLICATE_REQEUST), anyString());
+ }
+}