summaryrefslogtreecommitdiffstats
path: root/mojo/android/javatests
diff options
context:
space:
mode:
authorqsr@chromium.org <qsr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-07-17 13:32:30 +0000
committerqsr@chromium.org <qsr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-07-17 13:32:30 +0000
commitfe85f4fbeea023ae4084a102b8617d5433db57e0 (patch)
tree95e81a55c2518361909735faea72f4c0cd6622b3 /mojo/android/javatests
parent765d9bc7315e3938d1cebcd5e4c5bd3c8f1c4a9c (diff)
downloadchromium_src-fe85f4fbeea023ae4084a102b8617d5433db57e0.zip
chromium_src-fe85f4fbeea023ae4084a102b8617d5433db57e0.tar.gz
chromium_src-fe85f4fbeea023ae4084a102b8617d5433db57e0.tar.bz2
Adding a router class to handle messages that expect responses.
This also introduce the notion of message header to allow routing. R=rmcilroy@chromium.org Review URL: https://codereview.chromium.org/371603003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@283767 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'mojo/android/javatests')
-rw-r--r--mojo/android/javatests/src/org/chromium/mojo/TestUtils.java2
-rw-r--r--mojo/android/javatests/src/org/chromium/mojo/bindings/BindingsTestUtils.java51
-rw-r--r--mojo/android/javatests/src/org/chromium/mojo/bindings/ConnectorTest.java15
-rw-r--r--mojo/android/javatests/src/org/chromium/mojo/bindings/MessageHeaderTest.java69
-rw-r--r--mojo/android/javatests/src/org/chromium/mojo/bindings/MessageWithHeaderTest.java (renamed from mojo/android/javatests/src/org/chromium/mojo/bindings/MessageTest.java)33
-rw-r--r--mojo/android/javatests/src/org/chromium/mojo/bindings/RouterTest.java134
6 files changed, 272 insertions, 32 deletions
diff --git a/mojo/android/javatests/src/org/chromium/mojo/TestUtils.java b/mojo/android/javatests/src/org/chromium/mojo/TestUtils.java
index ad13ca9..2134a9f 100644
--- a/mojo/android/javatests/src/org/chromium/mojo/TestUtils.java
+++ b/mojo/android/javatests/src/org/chromium/mojo/TestUtils.java
@@ -5,6 +5,7 @@
package org.chromium.mojo;
import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
import java.util.Random;
/**
@@ -22,6 +23,7 @@ public final class TestUtils {
RANDOM.setSeed(size);
RANDOM.nextBytes(bytes);
ByteBuffer data = ByteBuffer.allocateDirect(size);
+ data.order(ByteOrder.nativeOrder());
data.put(bytes);
data.flip();
return data;
diff --git a/mojo/android/javatests/src/org/chromium/mojo/bindings/BindingsTestUtils.java b/mojo/android/javatests/src/org/chromium/mojo/bindings/BindingsTestUtils.java
index 3fcee86..fa93d37 100644
--- a/mojo/android/javatests/src/org/chromium/mojo/bindings/BindingsTestUtils.java
+++ b/mojo/android/javatests/src/org/chromium/mojo/bindings/BindingsTestUtils.java
@@ -4,8 +4,12 @@
package org.chromium.mojo.bindings;
+import org.chromium.mojo.TestUtils;
+import org.chromium.mojo.system.Handle;
import org.chromium.mojo.system.MojoException;
+import org.chromium.mojo.system.Pair;
+import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
@@ -19,32 +23,65 @@ public class BindingsTestUtils {
*/
public static class RecordingMessageReceiver implements MessageReceiver {
- public final List<Message> messages = new ArrayList<Message>();
+ public final List<MessageWithHeader> messages = new ArrayList<MessageWithHeader>();
/**
- * @see MessageReceiver#accept(Message)
+ * @see MessageReceiver#accept(MessageWithHeader)
*/
@Override
- public boolean accept(Message message) {
+ public boolean accept(MessageWithHeader message) {
messages.add(message);
return true;
}
}
/**
- * {@link Connector.ErrorHandler} that records any error it received.
+ * {@link MessageReceiverWithResponder} that records any message it receives.
*/
- public static class CapturingErrorHandler implements Connector.ErrorHandler {
+ public static class RecordingMessageReceiverWithResponder extends RecordingMessageReceiver
+ implements MessageReceiverWithResponder {
+
+ public final List<Pair<MessageWithHeader, MessageReceiver>> messagesWithReceivers =
+ new ArrayList<Pair<MessageWithHeader, MessageReceiver>>();
+
+ /**
+ * @see MessageReceiverWithResponder#acceptWithResponder(MessageWithHeader,
+ * MessageReceiver)
+ */
+ @Override
+ public boolean acceptWithResponder(MessageWithHeader message, MessageReceiver responder) {
+ messagesWithReceivers.add(Pair.create(message, responder));
+ return true;
+ }
+ }
+
+ /**
+ * {@link ConnectionErrorHandler} that records any error it received.
+ */
+ public static class CapturingErrorHandler implements ConnectionErrorHandler {
public MojoException exception = null;
/**
- * @see Connector.ErrorHandler#onError(MojoException)
+ * @see ConnectionErrorHandler#onConnectionError(MojoException)
*/
@Override
- public void onError(MojoException e) {
+ public void onConnectionError(MojoException e) {
exception = e;
}
}
+ /**
+ * Creates a new valid {@link MessageWithHeader}.
+ */
+ public static MessageWithHeader newRandomMessageWithHeader(int size) {
+ assert size > 16;
+ ByteBuffer message = TestUtils.newRandomBuffer(size);
+ int[] headerAsInts = { 16, 2, 0, 0 };
+ for (int i = 0; i < 4; ++i) {
+ message.putInt(4 * i, headerAsInts[i]);
+ }
+ message.position(0);
+ return new MessageWithHeader(new Message(message, new ArrayList<Handle>()));
+ }
}
diff --git a/mojo/android/javatests/src/org/chromium/mojo/bindings/ConnectorTest.java b/mojo/android/javatests/src/org/chromium/mojo/bindings/ConnectorTest.java
index 21ac624..ff5b464 100644
--- a/mojo/android/javatests/src/org/chromium/mojo/bindings/ConnectorTest.java
+++ b/mojo/android/javatests/src/org/chromium/mojo/bindings/ConnectorTest.java
@@ -7,7 +7,6 @@ package org.chromium.mojo.bindings;
import android.test.suitebuilder.annotation.SmallTest;
import org.chromium.mojo.MojoTestCase;
-import org.chromium.mojo.TestUtils;
import org.chromium.mojo.bindings.BindingsTestUtils.CapturingErrorHandler;
import org.chromium.mojo.bindings.BindingsTestUtils.RecordingMessageReceiver;
import org.chromium.mojo.system.Core;
@@ -31,7 +30,7 @@ public class ConnectorTest extends MojoTestCase {
private MessagePipeHandle mHandle;
private Connector mConnector;
- private Message mTestMessage;
+ private MessageWithHeader mTestMessage;
private RecordingMessageReceiver mReceiver;
private CapturingErrorHandler mErrorHandler;
@@ -51,7 +50,7 @@ public class ConnectorTest extends MojoTestCase {
mErrorHandler = new CapturingErrorHandler();
mConnector.setErrorHandler(mErrorHandler);
mConnector.start();
- mTestMessage = new Message(TestUtils.newRandomBuffer(DATA_LENGTH), new ArrayList<Handle>());
+ mTestMessage = BindingsTestUtils.newRandomMessageWithHeader(DATA_LENGTH);
assertNull(mErrorHandler.exception);
assertEquals(0, mReceiver.messages.size());
}
@@ -78,7 +77,7 @@ public class ConnectorTest extends MojoTestCase {
MessagePipeHandle.ReadFlags.NONE);
assertEquals(MojoResult.OK, result.getMojoResult());
assertEquals(DATA_LENGTH, result.getMessageSize());
- assertEquals(mTestMessage.buffer, received);
+ assertEquals(mTestMessage.getMessage().buffer, received);
}
/**
@@ -86,14 +85,14 @@ public class ConnectorTest extends MojoTestCase {
*/
@SmallTest
public void testReceivingMessage() {
- mHandle.writeMessage(mTestMessage.buffer, new ArrayList<Handle>(),
+ mHandle.writeMessage(mTestMessage.getMessage().buffer, new ArrayList<Handle>(),
MessagePipeHandle.WriteFlags.NONE);
nativeRunLoop(RUN_LOOP_TIMEOUT_MS);
assertNull(mErrorHandler.exception);
assertEquals(1, mReceiver.messages.size());
- Message received = mReceiver.messages.get(0);
- assertEquals(0, received.handles.size());
- assertEquals(mTestMessage.buffer, received.buffer);
+ MessageWithHeader received = mReceiver.messages.get(0);
+ assertEquals(0, received.getMessage().handles.size());
+ assertEquals(mTestMessage.getMessage().buffer, received.getMessage().buffer);
}
/**
diff --git a/mojo/android/javatests/src/org/chromium/mojo/bindings/MessageHeaderTest.java b/mojo/android/javatests/src/org/chromium/mojo/bindings/MessageHeaderTest.java
new file mode 100644
index 0000000..caf4b8b
--- /dev/null
+++ b/mojo/android/javatests/src/org/chromium/mojo/bindings/MessageHeaderTest.java
@@ -0,0 +1,69 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.mojo.bindings;
+
+import android.test.suitebuilder.annotation.SmallTest;
+
+import junit.framework.TestCase;
+
+import org.chromium.mojo.bindings.test.mojom.imported.Point;
+
+/**
+ * Testing internal classes of interfaces.
+ */
+public class MessageHeaderTest extends TestCase {
+
+ /**
+ * Testing that headers are identical after being serialized/deserialized.
+ */
+ @SmallTest
+ public void testSimpleMessageHeader() {
+ final int xValue = 1;
+ final int yValue = 2;
+ final int type = 6;
+ Point p = new Point();
+ p.x = xValue;
+ p.y = yValue;
+ MessageWithHeader message = p.serializeWithHeader(null, new MessageHeader(type));
+
+ MessageHeader header = message.getHeader();
+ assertTrue(header.validateHeader(type, 0));
+ assertEquals(type, header.getType());
+ assertEquals(0, header.getFlags());
+
+ Point p2 = Point.deserialize(message.getPayload());
+ assertNotNull(p2);
+ assertEquals(p.x, p2.x);
+ assertEquals(p.y, p2.y);
+ }
+
+ /**
+ * Testing that headers are identical after being serialized/deserialized.
+ */
+ @SmallTest
+ public void testMessageWithRequestIdHeader() {
+ final int xValue = 1;
+ final int yValue = 2;
+ final int type = 6;
+ final long requestId = 0x1deadbeafL;
+ Point p = new Point();
+ p.x = xValue;
+ p.y = yValue;
+ MessageWithHeader message = p.serializeWithHeader(null,
+ new MessageHeader(type, MessageHeader.MESSAGE_EXPECTS_RESPONSE_FLAG, 0));
+ message.setRequestId(requestId);
+
+ MessageHeader header = message.getHeader();
+ assertTrue(header.validateHeader(type, MessageHeader.MESSAGE_EXPECTS_RESPONSE_FLAG));
+ assertEquals(type, header.getType());
+ assertEquals(MessageHeader.MESSAGE_EXPECTS_RESPONSE_FLAG, header.getFlags());
+ assertEquals(requestId, header.getRequestId());
+
+ Point p2 = Point.deserialize(message.getPayload());
+ assertNotNull(p2);
+ assertEquals(p.x, p2.x);
+ assertEquals(p.y, p2.y);
+ }
+}
diff --git a/mojo/android/javatests/src/org/chromium/mojo/bindings/MessageTest.java b/mojo/android/javatests/src/org/chromium/mojo/bindings/MessageWithHeaderTest.java
index ab2e871..7f42445 100644
--- a/mojo/android/javatests/src/org/chromium/mojo/bindings/MessageTest.java
+++ b/mojo/android/javatests/src/org/chromium/mojo/bindings/MessageWithHeaderTest.java
@@ -7,7 +7,6 @@ package org.chromium.mojo.bindings;
import android.test.suitebuilder.annotation.SmallTest;
import org.chromium.mojo.MojoTestCase;
-import org.chromium.mojo.TestUtils;
import org.chromium.mojo.bindings.BindingsTestUtils.RecordingMessageReceiver;
import org.chromium.mojo.system.Core;
import org.chromium.mojo.system.DataPipe;
@@ -24,9 +23,9 @@ import java.util.Arrays;
import java.util.List;
/**
- * Testing {@link Message}.
+ * Testing {@link MessageWithHeader}.
*/
-public class MessageTest extends MojoTestCase {
+public class MessageWithHeaderTest extends MojoTestCase {
private static final int DATA_SIZE = 1024;
@@ -43,7 +42,7 @@ public class MessageTest extends MojoTestCase {
protected void setUp() throws Exception {
super.setUp();
Core core = CoreImpl.getInstance();
- mData = TestUtils.newRandomBuffer(DATA_SIZE);
+ mData = BindingsTestUtils.newRandomMessageWithHeader(DATA_SIZE).getMessage().buffer;
mMessageReceiver = new RecordingMessageReceiver();
mHandles = core.createMessagePipe(new MessagePipeHandle.CreateOptions());
Pair<DataPipe.ProducerHandle, DataPipe.ConsumerHandle> datapipe = core.createDataPipe(null);
@@ -64,43 +63,43 @@ public class MessageTest extends MojoTestCase {
}
/**
- * Testing {@link Message#readAndDispatchMessage(MessagePipeHandle, MessageReceiver)}
+ * Testing {@link MessageWithHeader#readAndDispatchMessage(MessagePipeHandle, MessageReceiver)}
*/
@SmallTest
public void testReadAndDispatchMessage() {
mHandles.first.writeMessage(mData, mHandlesToSend, MessagePipeHandle.WriteFlags.NONE);
assertEquals(MojoResult.OK,
- Message.readAndDispatchMessage(mHandles.second, mMessageReceiver));
+ MessageWithHeader.readAndDispatchMessage(mHandles.second, mMessageReceiver));
assertEquals(1, mMessageReceiver.messages.size());
- Message message = mMessageReceiver.messages.get(0);
- mHandlesToClose.addAll(message.handles);
- assertEquals(mData, message.buffer);
- assertEquals(2, message.handles.size());
- for (Handle handle : message.handles) {
+ MessageWithHeader message = mMessageReceiver.messages.get(0);
+ mHandlesToClose.addAll(message.getMessage().handles);
+ assertEquals(mData, message.getMessage().buffer);
+ assertEquals(2, message.getMessage().handles.size());
+ for (Handle handle : message.getMessage().handles) {
assertTrue(handle.isValid());
}
}
/**
- * Testing {@link Message#readAndDispatchMessage(MessagePipeHandle, MessageReceiver)} with no
- * message available.
+ * Testing {@link MessageWithHeader#readAndDispatchMessage(MessagePipeHandle, MessageReceiver)}
+ * with no message available.
*/
@SmallTest
public void testReadAndDispatchMessageOnEmptyHandle() {
assertEquals(MojoResult.SHOULD_WAIT,
- Message.readAndDispatchMessage(mHandles.second, mMessageReceiver));
+ MessageWithHeader.readAndDispatchMessage(mHandles.second, mMessageReceiver));
assertEquals(0, mMessageReceiver.messages.size());
}
/**
- * Testing {@link Message#readAndDispatchMessage(MessagePipeHandle, MessageReceiver)} on closed
- * handle.
+ * Testing {@link MessageWithHeader#readAndDispatchMessage(MessagePipeHandle, MessageReceiver)}
+ * on closed handle.
*/
@SmallTest
public void testReadAndDispatchMessageOnClosedHandle() {
mHandles.first.close();
try {
- Message.readAndDispatchMessage(mHandles.second, mMessageReceiver);
+ MessageWithHeader.readAndDispatchMessage(mHandles.second, mMessageReceiver);
fail("MojoException should have been thrown");
} catch (MojoException expected) {
assertEquals(MojoResult.FAILED_PRECONDITION, expected.getMojoResult());
diff --git a/mojo/android/javatests/src/org/chromium/mojo/bindings/RouterTest.java b/mojo/android/javatests/src/org/chromium/mojo/bindings/RouterTest.java
new file mode 100644
index 0000000..58d3c71
--- /dev/null
+++ b/mojo/android/javatests/src/org/chromium/mojo/bindings/RouterTest.java
@@ -0,0 +1,134 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.mojo.bindings;
+
+import android.test.suitebuilder.annotation.SmallTest;
+
+import org.chromium.mojo.MojoTestCase;
+import org.chromium.mojo.bindings.BindingsTestUtils.CapturingErrorHandler;
+import org.chromium.mojo.bindings.BindingsTestUtils.RecordingMessageReceiverWithResponder;
+import org.chromium.mojo.system.Core;
+import org.chromium.mojo.system.Handle;
+import org.chromium.mojo.system.MessagePipeHandle;
+import org.chromium.mojo.system.MojoResult;
+import org.chromium.mojo.system.Pair;
+import org.chromium.mojo.system.impl.CoreImpl;
+
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+
+/**
+ * Testing {@link Router}
+ */
+public class RouterTest extends MojoTestCase {
+
+ private static final long RUN_LOOP_TIMEOUT_MS = 25;
+
+ private MessagePipeHandle mHandle;
+ private Router mRouter;
+ private RecordingMessageReceiverWithResponder mReceiver;
+ private CapturingErrorHandler mErrorHandler;
+
+ /**
+ * @see MojoTestCase#setUp()
+ */
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ Core core = CoreImpl.getInstance();
+ Pair<MessagePipeHandle, MessagePipeHandle> handles = core.createMessagePipe(null);
+ mHandle = handles.first;
+ mRouter = new RouterImpl(handles.second);
+ mReceiver = new RecordingMessageReceiverWithResponder();
+ mRouter.setIncomingMessageReceiver(mReceiver);
+ mErrorHandler = new CapturingErrorHandler();
+ mRouter.setErrorHandler(mErrorHandler);
+ mRouter.start();
+ }
+
+ /**
+ * Testing sending a message via the router that expected a response.
+ */
+ @SmallTest
+ public void testSendingToRouterWithResponse() {
+ final int requestMessageType = 0xdead;
+ final int responseMessageType = 0xbeaf;
+
+ // Sending a message expecting a response.
+ MessageHeader header = new MessageHeader(requestMessageType,
+ MessageHeader.MESSAGE_EXPECTS_RESPONSE_FLAG, 0);
+ Encoder encoder = new Encoder(CoreImpl.getInstance(), header.getSize());
+ header.encode(encoder);
+ MessageWithHeader headerMessage = new MessageWithHeader(encoder.getMessage());
+ mRouter.acceptWithResponder(headerMessage, mReceiver);
+ ByteBuffer receiveBuffer = ByteBuffer.allocateDirect(header.getSize());
+ MessagePipeHandle.ReadMessageResult result = mHandle.readMessage(receiveBuffer, 0,
+ MessagePipeHandle.ReadFlags.NONE);
+
+ assertEquals(MojoResult.OK, result.getMojoResult());
+ MessageHeader receivedHeader = new MessageWithHeader(
+ new Message(receiveBuffer, new ArrayList<Handle>()))
+ .getHeader();
+
+ assertEquals(header.getType(), receivedHeader.getType());
+ assertEquals(header.getFlags(), receivedHeader.getFlags());
+ assertTrue(receivedHeader.getRequestId() != 0);
+
+ // Sending the response.
+ MessageHeader responseHeader = new MessageHeader(responseMessageType,
+ MessageHeader.MESSAGE_IS_RESPONSE_FLAG, receivedHeader.getRequestId());
+ encoder = new Encoder(CoreImpl.getInstance(), header.getSize());
+ responseHeader.encode(encoder);
+ Message responseMessage = encoder.getMessage();
+ mHandle.writeMessage(responseMessage.buffer, new ArrayList<Handle>(),
+ MessagePipeHandle.WriteFlags.NONE);
+ nativeRunLoop(RUN_LOOP_TIMEOUT_MS);
+
+ assertEquals(1, mReceiver.messages.size());
+ MessageWithHeader receivedResponseMessage = mReceiver.messages.get(0);
+ assertEquals(MessageHeader.MESSAGE_IS_RESPONSE_FLAG,
+ receivedResponseMessage.getHeader().getFlags());
+ assertEquals(responseMessage.buffer, receivedResponseMessage.getMessage().buffer);
+ }
+
+ /**
+ * Testing receiving a message via the router that expected a response.
+ */
+ @SmallTest
+ public void testReceivingViaRouterWithResponse() {
+ final int requestMessageType = 0xdead;
+ final int responseMessageType = 0xbeef;
+ final int requestId = 0xdeadbeaf;
+
+ // Sending a message expecting a response.
+ MessageHeader header = new MessageHeader(requestMessageType,
+ MessageHeader.MESSAGE_EXPECTS_RESPONSE_FLAG, requestId);
+ Encoder encoder = new Encoder(CoreImpl.getInstance(), header.getSize());
+ header.encode(encoder);
+ Message headerMessage = encoder.getMessage();
+ mHandle.writeMessage(headerMessage.buffer, new ArrayList<Handle>(),
+ MessagePipeHandle.WriteFlags.NONE);
+ nativeRunLoop(RUN_LOOP_TIMEOUT_MS);
+
+ assertEquals(1, mReceiver.messagesWithReceivers.size());
+ Pair<MessageWithHeader, MessageReceiver> receivedMessage =
+ mReceiver.messagesWithReceivers.get(0);
+ assertEquals(headerMessage.buffer, receivedMessage.first.getMessage().buffer);
+
+ // Sending the response.
+ MessageHeader responseHeader = new MessageHeader(responseMessageType,
+ MessageHeader.MESSAGE_EXPECTS_RESPONSE_FLAG, requestId);
+ encoder = new Encoder(CoreImpl.getInstance(), header.getSize());
+ responseHeader.encode(encoder);
+ MessageWithHeader responseHeaderMessage = new MessageWithHeader(encoder.getMessage());
+ receivedMessage.second.accept(responseHeaderMessage);
+ ByteBuffer receivedResponseMessage = ByteBuffer.allocateDirect(responseHeader.getSize());
+ MessagePipeHandle.ReadMessageResult result = mHandle.readMessage(receivedResponseMessage, 0,
+ MessagePipeHandle.ReadFlags.NONE);
+
+ assertEquals(MojoResult.OK, result.getMojoResult());
+ assertEquals(responseHeaderMessage.getMessage().buffer, receivedResponseMessage);
+ }
+}