summaryrefslogtreecommitdiffstats
path: root/mojo/android/javatests/src
diff options
context:
space:
mode:
authorqsr <qsr@chromium.org>2014-08-27 08:18:43 -0700
committerCommit bot <commit-bot@chromium.org>2014-08-27 15:19:23 +0000
commit063e1e3acae7473399f5064c66296de55b0ac04b (patch)
treebe39acfeda277b1b7fe355df3f33e4bf633e85fd /mojo/android/javatests/src
parentfbcfb84493065cd31fd50a4cde1d9580b93d8211 (diff)
downloadchromium_src-063e1e3acae7473399f5064c66296de55b0ac04b.zip
chromium_src-063e1e3acae7473399f5064c66296de55b0ac04b.tar.gz
chromium_src-063e1e3acae7473399f5064c66296de55b0ac04b.tar.bz2
mojo: generate Proxies and Stubs for java bindings.
This CL allows to generate Stub and Proxy of mojo service in java. This allows to call interfaces method through message pipes. Committed: https://chromium.googlesource.com/chromium/src/+/b2ef91ee4abe584e712db7a51d47a1b6d9a96e57 Review URL: https://codereview.chromium.org/411913002 Cr-Commit-Position: refs/heads/master@{#292154}
Diffstat (limited to 'mojo/android/javatests/src')
-rw-r--r--mojo/android/javatests/src/org/chromium/mojo/bindings/BindingsTestUtils.java15
-rw-r--r--mojo/android/javatests/src/org/chromium/mojo/bindings/ConnectorTest.java11
-rw-r--r--mojo/android/javatests/src/org/chromium/mojo/bindings/InterfacesTest.java336
3 files changed, 354 insertions, 8 deletions
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 fa93d37..c9ffa02 100644
--- a/mojo/android/javatests/src/org/chromium/mojo/bindings/BindingsTestUtils.java
+++ b/mojo/android/javatests/src/org/chromium/mojo/bindings/BindingsTestUtils.java
@@ -21,7 +21,8 @@ public class BindingsTestUtils {
/**
* {@link MessageReceiver} that records any message it receives.
*/
- public static class RecordingMessageReceiver implements MessageReceiver {
+ public static class RecordingMessageReceiver extends SideEffectFreeCloseable
+ implements MessageReceiver {
public final List<MessageWithHeader> messages = new ArrayList<MessageWithHeader>();
@@ -60,15 +61,23 @@ public class BindingsTestUtils {
*/
public static class CapturingErrorHandler implements ConnectionErrorHandler {
- public MojoException exception = null;
+ private MojoException mLastMojoException = null;
/**
* @see ConnectionErrorHandler#onConnectionError(MojoException)
*/
@Override
public void onConnectionError(MojoException e) {
- exception = e;
+ mLastMojoException = e;
}
+
+ /**
+ * Returns the last recorded exception.
+ */
+ public MojoException getLastMojoException() {
+ return mLastMojoException;
+ }
+
}
/**
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 ff5b464..09871ca 100644
--- a/mojo/android/javatests/src/org/chromium/mojo/bindings/ConnectorTest.java
+++ b/mojo/android/javatests/src/org/chromium/mojo/bindings/ConnectorTest.java
@@ -51,7 +51,7 @@ public class ConnectorTest extends MojoTestCase {
mConnector.setErrorHandler(mErrorHandler);
mConnector.start();
mTestMessage = BindingsTestUtils.newRandomMessageWithHeader(DATA_LENGTH);
- assertNull(mErrorHandler.exception);
+ assertNull(mErrorHandler.getLastMojoException());
assertEquals(0, mReceiver.messages.size());
}
@@ -71,7 +71,7 @@ public class ConnectorTest extends MojoTestCase {
@SmallTest
public void testSendingMessage() {
mConnector.accept(mTestMessage);
- assertNull(mErrorHandler.exception);
+ assertNull(mErrorHandler.getLastMojoException());
ByteBuffer received = ByteBuffer.allocateDirect(DATA_LENGTH);
MessagePipeHandle.ReadMessageResult result = mHandle.readMessage(received, 0,
MessagePipeHandle.ReadFlags.NONE);
@@ -88,7 +88,7 @@ public class ConnectorTest extends MojoTestCase {
mHandle.writeMessage(mTestMessage.getMessage().buffer, new ArrayList<Handle>(),
MessagePipeHandle.WriteFlags.NONE);
nativeRunLoop(RUN_LOOP_TIMEOUT_MS);
- assertNull(mErrorHandler.exception);
+ assertNull(mErrorHandler.getLastMojoException());
assertEquals(1, mReceiver.messages.size());
MessageWithHeader received = mReceiver.messages.get(0);
assertEquals(0, received.getMessage().handles.size());
@@ -102,7 +102,8 @@ public class ConnectorTest extends MojoTestCase {
public void testErrors() {
mHandle.close();
nativeRunLoop(RUN_LOOP_TIMEOUT_MS);
- assertNotNull(mErrorHandler.exception);
- assertEquals(MojoResult.FAILED_PRECONDITION, mErrorHandler.exception.getMojoResult());
+ assertNotNull(mErrorHandler.getLastMojoException());
+ assertEquals(MojoResult.FAILED_PRECONDITION,
+ mErrorHandler.getLastMojoException().getMojoResult());
}
}
diff --git a/mojo/android/javatests/src/org/chromium/mojo/bindings/InterfacesTest.java b/mojo/android/javatests/src/org/chromium/mojo/bindings/InterfacesTest.java
new file mode 100644
index 0000000..8944b82
--- /dev/null
+++ b/mojo/android/javatests/src/org/chromium/mojo/bindings/InterfacesTest.java
@@ -0,0 +1,336 @@
+// 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.test.mojom.imported.ImportedInterface;
+import org.chromium.mojo.bindings.test.mojom.sample.Factory;
+import org.chromium.mojo.bindings.test.mojom.sample.FactoryClient;
+import org.chromium.mojo.bindings.test.mojom.sample.NamedObject;
+import org.chromium.mojo.bindings.test.mojom.sample.NamedObject.GetNameResponse;
+import org.chromium.mojo.bindings.test.mojom.sample.Request;
+import org.chromium.mojo.bindings.test.mojom.sample.Response;
+import org.chromium.mojo.system.DataPipe.ConsumerHandle;
+import org.chromium.mojo.system.MessagePipeHandle;
+import org.chromium.mojo.system.MojoException;
+import org.chromium.mojo.system.Pair;
+import org.chromium.mojo.system.impl.CoreImpl;
+
+import java.io.Closeable;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * Tests for interfaces / proxies / stubs generated for sample_factory.mojom.
+ */
+public class InterfacesTest extends MojoTestCase {
+
+ private static final long RUN_LOOP_TIMEOUT_MS = 25;
+
+ private final List<Closeable> mCloseablesToClose = new ArrayList<Closeable>();
+
+ /**
+ * Basic implementation of {@link NamedObject}.
+ */
+ public static class MockNamedObjectImpl extends CapturingErrorHandler implements NamedObject {
+
+ private String mName;
+
+ /**
+ * @see org.chromium.mojo.bindings.Interface#close()
+ */
+ @Override
+ public void close() {
+ }
+
+ @Override
+ public void setName(String name) {
+ mName = name;
+ }
+
+ @Override
+ public void getName(GetNameResponse callback) {
+ callback.call(mName);
+ }
+
+ public String getNameSynchronously() {
+ return mName;
+ }
+ }
+
+ /**
+ * Implementation of {@link GetNameResponse} keeping track of usage.
+ */
+ public static class RecordingGetNameResponse implements GetNameResponse {
+ private String mName;
+ private boolean mCalled;
+
+ public RecordingGetNameResponse() {
+ reset();
+ }
+
+ @Override
+ public void call(String name) {
+ mName = name;
+ mCalled = true;
+ }
+
+ public String getName() {
+ return mName;
+ }
+
+ public boolean wasCalled() {
+ return mCalled;
+ }
+
+ public void reset() {
+ mName = null;
+ mCalled = false;
+ }
+ }
+
+ /**
+ * Basic implementation of {@link Factory}.
+ */
+ public class MockFactoryImpl extends CapturingErrorHandler implements Factory {
+
+ private boolean mClosed = false;
+ private FactoryClient mFactoryClient;
+
+ public boolean isClosed() {
+ return mClosed;
+ }
+
+ @Override
+ public void setClient(FactoryClient client) {
+ mFactoryClient = client;
+ mCloseablesToClose.add(client);
+ }
+
+ /**
+ * @see org.chromium.mojo.bindings.Interface#close()
+ */
+ @Override
+ public void close() {
+ mClosed = true;
+ }
+
+ @Override
+ public void doStuff(Request request, MessagePipeHandle pipe) {
+ }
+
+ @Override
+ public void doStuff2(ConsumerHandle pipe) {
+ if (pipe != null) {
+ pipe.close();
+ }
+ mFactoryClient.didStuff2("Hello");
+ }
+
+ @Override
+ public void createNamedObject(InterfaceRequest<NamedObject> obj) {
+ NamedObject.MANAGER.bind(new MockNamedObjectImpl(), obj);
+ }
+
+ @Override
+ public void requestImportedInterface(InterfaceRequest<ImportedInterface> obj,
+ RequestImportedInterfaceResponse callback) {
+ throw new UnsupportedOperationException("Not implemented.");
+ }
+
+ @Override
+ public void takeImportedInterface(ImportedInterface obj,
+ TakeImportedInterfaceResponse callback) {
+ throw new UnsupportedOperationException("Not implemented.");
+ }
+ }
+
+ /**
+ * Basic implementation of {@link FactoryClient}.
+ */
+ public static class MockFactoryClientImpl implements FactoryClient {
+
+ private boolean mClosed = false;
+ private boolean mDidStuff2Called = false;
+
+ public boolean isClosed() {
+ return mClosed;
+ }
+
+ public boolean wasDidStuff2Called() {
+ return mDidStuff2Called;
+ }
+
+ /**
+ * @see org.chromium.mojo.bindings.Interface#close()
+ */
+ @Override
+ public void close() {
+ mClosed = true;
+ }
+
+ /**
+ * @see ConnectionErrorHandler#onConnectionError(MojoException)
+ */
+ @Override
+ public void onConnectionError(MojoException e) {
+ }
+
+ /**
+ * @see FactoryClient#didStuff(Response, java.lang.String)
+ */
+ @Override
+ public void didStuff(Response response, String text) {
+ }
+
+ /**
+ * @see FactoryClient#didStuff2(String)
+ */
+ @Override
+ public void didStuff2(String text) {
+ mDidStuff2Called = true;
+ }
+
+ }
+
+ /**
+ * @see MojoTestCase#tearDown()
+ */
+ @Override
+ protected void tearDown() throws Exception {
+ // Close the elements in the reverse order they were added. This is needed because it is an
+ // error to close the handle of a proxy without closing the proxy first.
+ Collections.reverse(mCloseablesToClose);
+ for (Closeable c : mCloseablesToClose) {
+ c.close();
+ }
+ super.tearDown();
+ }
+
+ private <I extends Interface, P extends Interface.Proxy> P newProxyOverPipe(
+ Interface.Manager<I, P> manager, I impl) {
+ Pair<MessagePipeHandle, MessagePipeHandle> handles =
+ CoreImpl.getInstance().createMessagePipe(null);
+ P proxy = manager.attachProxy(handles.first);
+ mCloseablesToClose.add(proxy);
+ manager.bind(impl, handles.second);
+ return proxy;
+ }
+
+ private <I extends InterfaceWithClient<C>, P extends InterfaceWithClient.Proxy<C>,
+ C extends Interface> P newProxyOverPipeWithClient(
+ InterfaceWithClient.Manager<I, P, C> manager, I impl, C client) {
+ Pair<MessagePipeHandle, MessagePipeHandle> handles =
+ CoreImpl.getInstance().createMessagePipe(null);
+ P proxy = manager.attachProxy(handles.first, client);
+ mCloseablesToClose.add(proxy);
+ manager.bind(impl, handles.second);
+ return proxy;
+ }
+
+ /**
+ * Check that the given proxy receives the calls. If |impl| is not null, also check that the
+ * calls are forwared to |impl|.
+ */
+ private void checkProxy(NamedObject.Proxy proxy, MockNamedObjectImpl impl) {
+ final String NAME = "hello world";
+ RecordingGetNameResponse callback = new RecordingGetNameResponse();
+ CapturingErrorHandler errorHandler = new CapturingErrorHandler();
+ proxy.setErrorHandler(errorHandler);
+
+ if (impl != null) {
+ assertNull(impl.getLastMojoException());
+ assertNull(impl.getNameSynchronously());
+ }
+
+ proxy.getName(callback);
+ nativeRunLoop(RUN_LOOP_TIMEOUT_MS);
+
+ assertNull(errorHandler.getLastMojoException());
+ assertTrue(callback.wasCalled());
+ assertNull(callback.getName());
+
+ callback.reset();
+ proxy.setName(NAME);
+ nativeRunLoop(RUN_LOOP_TIMEOUT_MS);
+
+ assertNull(errorHandler.getLastMojoException());
+ if (impl != null) {
+ assertNull(impl.getLastMojoException());
+ assertEquals(NAME, impl.getNameSynchronously());
+ }
+
+ proxy.getName(callback);
+ nativeRunLoop(RUN_LOOP_TIMEOUT_MS);
+
+ assertNull(errorHandler.getLastMojoException());
+ assertTrue(callback.wasCalled());
+ assertEquals(NAME, callback.getName());
+ }
+
+ @SmallTest
+ public void testProxyAndStub() {
+ MockNamedObjectImpl impl = new MockNamedObjectImpl();
+ NamedObject.Proxy proxy =
+ NamedObject.MANAGER.buildProxy(null, NamedObject.MANAGER.buildStub(null, impl));
+
+ checkProxy(proxy, impl);
+ }
+
+ @SmallTest
+ public void testProxyAndStubOverPipe() {
+ MockNamedObjectImpl impl = new MockNamedObjectImpl();
+ NamedObject.Proxy proxy = newProxyOverPipe(NamedObject.MANAGER, impl);
+
+ checkProxy(proxy, impl);
+ }
+
+ @SmallTest
+ public void testFactoryOverPipe() {
+ Factory.Proxy proxy = newProxyOverPipe(Factory.MANAGER, new MockFactoryImpl());
+ Pair<NamedObject.Proxy, InterfaceRequest<NamedObject>> request =
+ NamedObject.MANAGER.getInterfaceRequest(CoreImpl.getInstance());
+ mCloseablesToClose.add(request.first);
+ proxy.createNamedObject(request.second);
+
+ checkProxy(request.first, null);
+ }
+
+ @SmallTest
+ public void testInterfaceClosing() {
+ MockFactoryImpl impl = new MockFactoryImpl();
+ MockFactoryClientImpl client = new MockFactoryClientImpl();
+ Factory.Proxy proxy = newProxyOverPipeWithClient(
+ Factory.MANAGER, impl, client);
+
+ assertFalse(impl.isClosed());
+ assertFalse(client.isClosed());
+
+ proxy.close();
+ nativeRunLoop(RUN_LOOP_TIMEOUT_MS);
+
+ assertTrue(impl.isClosed());
+ assertTrue(client.isClosed());
+ }
+
+ @SmallTest
+ public void testClient() {
+ MockFactoryImpl impl = new MockFactoryImpl();
+ MockFactoryClientImpl client = new MockFactoryClientImpl();
+ Factory.Proxy proxy = newProxyOverPipeWithClient(
+ Factory.MANAGER, impl, client);
+ proxy.doStuff2(null);
+
+ assertFalse(client.wasDidStuff2Called());
+
+ nativeRunLoop(RUN_LOOP_TIMEOUT_MS);
+
+ assertTrue(client.wasDidStuff2Called());
+ }
+}