summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorRebecca Silberstein <silberst@google.com>2016-02-22 15:21:21 -0800
committerRebecca Silberstein <silberst@google.com>2016-02-23 08:53:45 -0800
commit89901c4bde80282b02a786e580f9bc75984bd4de (patch)
tree35e2b31ad3196ac816e813d82089c4cbc01ccd6d /tests
parentbc4548f2f5bc9cb5f72c405454a480ccb470b86e (diff)
Create blocking call for AutoDispatch test.
Add a blocking call to the testAutoDispatchWithSingleMessage test in MockLooperTest. AutoDispatch is not intended to be used with non blocking calls and this corrects the test. BUG=27278081 Change-Id: I783f763a1b438bf307484276436497e534be3906
Diffstat (limited to 'tests')
-rw-r--r--tests/wifitests/src/com/android/server/wifi/MockLooperTest.java51
1 files changed, 39 insertions, 12 deletions
diff --git a/tests/wifitests/src/com/android/server/wifi/MockLooperTest.java b/tests/wifitests/src/com/android/server/wifi/MockLooperTest.java
index 61989008b..74a73d6d4 100644
--- a/tests/wifitests/src/com/android/server/wifi/MockLooperTest.java
+++ b/tests/wifitests/src/com/android/server/wifi/MockLooperTest.java
@@ -18,12 +18,14 @@ package com.android.server.wifi;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import android.os.Handler;
+import android.os.Looper;
import android.os.Message;
import android.test.suitebuilder.annotation.SmallTest;
@@ -262,25 +264,50 @@ public class MockLooperTest {
/**
* Test AutoDispatch for a single message.
- * Enable AutoDispatch, add message, confirm it was sent and the state was cleaned up.
+ * This test would ideally use the Channel sendMessageSynchronously. At this time, the setup to
+ * get a working test channel is cumbersome. Until this is fixed, we substitute with a
+ * sendMessage followed by a blocking call. The main test thread blocks until the test handler
+ * receives the test message (messageA) and sets a boolean true. Once the boolean is true, the
+ * main thread will exit the busy wait loop, stop autoDispatch and check the assert.
+ *
+ * Enable AutoDispatch, add message, block on message being handled and stop AutoDispatch.
* <p>
- * Expected: get message and have cleaned up state.
+ * Expected: handleMessage is called for messageA and stopAutoDispatch is called.
*/
@Test
public void testAutoDispatchWithSingleMessage() {
- final int messageA = 1;
-
- InOrder inOrder = inOrder(mHandlerSpy);
- ArgumentCaptor<Message> messageCaptor = ArgumentCaptor.forClass(Message.class);
+ final int mLoopSleepTimeMs = 5;
- mMockLooper.startAutoDispatch();
- mHandlerSpy.sendMessage(mHandler.obtainMessage(messageA));
- mMockLooper.stopAutoDispatch();
+ final int messageA = 1;
- inOrder.verify(mHandlerSpy).handleMessage(messageCaptor.capture());
- collector.checkThat("1: messageA", messageA, equalTo(messageCaptor.getValue().what));
+ MockLooper mockLooper = new MockLooper();
+ class TestHandler extends Handler {
+ public volatile boolean handledMessage = false;
+ TestHandler(Looper looper) {
+ super(looper);
+ }
+
+ @Override
+ public void handleMessage(Message msg) {
+ if (msg.what == messageA) {
+ handledMessage = true;
+ }
+ }
+ }
- inOrder.verify(mHandlerSpy, never()).handleMessage(any(Message.class));
+ TestHandler testHandler = new TestHandler(mockLooper.getLooper());
+ mockLooper.startAutoDispatch();
+ testHandler.sendMessage(testHandler.obtainMessage(messageA));
+ while (!testHandler.handledMessage) {
+ // Block until message is handled
+ try {
+ Thread.sleep(mLoopSleepTimeMs);
+ } catch (InterruptedException e) {
+ // Interrupted while sleeping.
+ }
+ }
+ mockLooper.stopAutoDispatch();
+ assertTrue("TestHandler should have received messageA", testHandler.handledMessage);
}
/**