Index: mojo/android/javatests/src/org/chromium/mojo/bindings/BindingsTest.java |
diff --git a/mojo/android/javatests/src/org/chromium/mojo/bindings/BindingsTest.java b/mojo/android/javatests/src/org/chromium/mojo/bindings/BindingsTest.java |
index 3b805bfc73c1524646ed875bd361a00fc4a4e670..c6acbc1a7971f7b562a0b6d82e55eb33881e8db4 100644 |
--- a/mojo/android/javatests/src/org/chromium/mojo/bindings/BindingsTest.java |
+++ b/mojo/android/javatests/src/org/chromium/mojo/bindings/BindingsTest.java |
@@ -6,72 +6,427 @@ package org.chromium.mojo.bindings; |
import android.test.suitebuilder.annotation.SmallTest; |
-import junit.framework.TestCase; |
- |
-import org.chromium.mojo.HandleMock; |
-import org.chromium.mojo.bindings.test.mojom.imported.Color; |
-import org.chromium.mojo.bindings.test.mojom.imported.Point; |
-import org.chromium.mojo.bindings.test.mojom.imported.Shape; |
-import org.chromium.mojo.bindings.test.mojom.imported.Thing; |
+import org.chromium.mojo.MojoTestCase; |
+import org.chromium.mojo.bindings.test.mojom.imported.ImportedInterface; |
import org.chromium.mojo.bindings.test.mojom.sample.Bar; |
import org.chromium.mojo.bindings.test.mojom.sample.Bar.Type; |
-import org.chromium.mojo.bindings.test.mojom.sample.DefaultsTest; |
-import org.chromium.mojo.bindings.test.mojom.sample.Enum; |
+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.Foo; |
-import org.chromium.mojo.bindings.test.mojom.sample.InterfaceConstants; |
+import org.chromium.mojo.bindings.test.mojom.sample.NamedObject; |
+import org.chromium.mojo.bindings.test.mojom.sample.Port; |
+import org.chromium.mojo.bindings.test.mojom.sample.Provider; |
+import org.chromium.mojo.bindings.test.mojom.sample.ProviderClient; |
+import org.chromium.mojo.bindings.test.mojom.sample.Request; |
+import org.chromium.mojo.bindings.test.mojom.sample.Response; |
import org.chromium.mojo.bindings.test.mojom.sample.SampleServiceConstants; |
+import org.chromium.mojo.bindings.test.mojom.sample.Service; |
+import org.chromium.mojo.bindings.test.mojom.sample.StructWithHoleV1; |
+import org.chromium.mojo.bindings.test.mojom.sample.StructWithHoleV2; |
+import org.chromium.mojo.system.AsyncWaiter; |
+import org.chromium.mojo.system.Core; |
+import org.chromium.mojo.system.DataPipe; |
import org.chromium.mojo.system.DataPipe.ConsumerHandle; |
+import org.chromium.mojo.system.DataPipe.CreateOptions; |
import org.chromium.mojo.system.DataPipe.ProducerHandle; |
+import org.chromium.mojo.system.Handle; |
import org.chromium.mojo.system.MessagePipeHandle; |
+import org.chromium.mojo.system.MojoException; |
+import org.chromium.mojo.system.Pair; |
+import org.chromium.mojo.system.SharedBufferHandle; |
+import org.chromium.mojo.system.UntypedHandle; |
+import org.chromium.mojo.system.impl.CoreImpl; |
+import java.lang.ref.WeakReference; |
+import java.lang.reflect.Array; |
import java.lang.reflect.Field; |
import java.lang.reflect.Modifier; |
-import java.util.Arrays; |
+import java.nio.ByteBuffer; |
+import java.nio.ByteOrder; |
+import java.util.ArrayList; |
+import java.util.Collections; |
+import java.util.List; |
/** |
* Testing generated classes and associated features. |
*/ |
-public class BindingsTest extends TestCase { |
+public class BindingsTest extends MojoTestCase { |
+ |
+ private static class BaseErrorHandler implements ConnectionErrorHandler { |
+ |
+ private MojoException lastException = null; |
+ |
+ public MojoException getLastException() { |
+ return lastException; |
+ } |
+ |
+ /** |
+ * @see ConnectionErrorHandler#onConnectionError(MojoException) |
+ */ |
+ @Override |
+ public void onConnectionError(MojoException e) { |
+ lastException = e; |
+ } |
- /** |
- * Create a new typical Bar instance. |
- */ |
- private static Bar newBar() { |
- Bar bar = new Bar(); |
- bar.alpha = (byte) 0x01; |
- bar.beta = (byte) 0x02; |
- bar.gamma = (byte) 0x03; |
- bar.type = Type.TYPE_BOTH; |
- return bar; |
} |
- /** |
- * Check that 2 Bar instances are equals. |
- */ |
- private static void assertBarEquals(Bar bar, Bar bar2) { |
- if (bar == bar2) { |
+ private class CoreWrapper implements Core { |
+ private List<WeakReference<Handle>> mHandles = new ArrayList<WeakReference<Handle>>(); |
+ private final Core mCore; |
+ |
+ /** |
+ * TODO(qsr): |
+ * |
+ * @param mCore |
+ */ |
+ public CoreWrapper(Core core) { |
+ this.mCore = core; |
+ } |
+ |
+ /** |
+ * TODO(qsr): |
+ * |
+ * @return |
+ * @see Core#getTimeTicksNow() |
+ */ |
+ public long getTimeTicksNow() { |
+ return mCore.getTimeTicksNow(); |
+ } |
+ |
+ /** |
+ * TODO(qsr): |
+ * |
+ * @param handle |
+ * @param flags |
+ * @param deadline |
+ * @return |
+ * @see Core#wait(org.chromium.mojo.system.Handle, org.chromium.mojo.system.Core.WaitFlags, |
+ * long) |
+ */ |
+ public int wait(Handle handle, WaitFlags flags, long deadline) { |
+ return mCore.wait(handle, flags, deadline); |
+ } |
+ |
+ /** |
+ * TODO(qsr): |
+ * |
+ * @param handles |
+ * @param deadline |
+ * @return |
+ * @see Core#waitMany(java.util.List, long) |
+ */ |
+ public WaitManyResult waitMany(List<Pair<Handle, WaitFlags>> handles, long deadline) { |
+ return mCore.waitMany(handles, deadline); |
+ } |
+ |
+ /** |
+ * TODO(qsr): |
+ * |
+ * @return |
+ * @see Core#createMessagePipe() |
+ */ |
+ public Pair<MessagePipeHandle, MessagePipeHandle> createMessagePipe() { |
+ Pair<MessagePipeHandle, MessagePipeHandle> handles = mCore.createMessagePipe(); |
+ mHandles.add(new WeakReference<Handle>(handles.first)); |
+ mHandles.add(new WeakReference<Handle>(handles.second)); |
+ return handles; |
+ } |
+ |
+ /** |
+ * TODO(qsr): |
+ * |
+ * @param options |
+ * @return |
+ * @see Core#createDataPipe(org.chromium.mojo.system.DataPipe.CreateOptions) |
+ */ |
+ public Pair<ProducerHandle, ConsumerHandle> createDataPipe(CreateOptions options) { |
+ return mCore.createDataPipe(options); |
+ } |
+ |
+ /** |
+ * TODO(qsr): |
+ * |
+ * @param options |
+ * @param numBytes |
+ * @return |
+ * @see Core#createSharedBuffer(org.chromium.mojo.system.SharedBufferHandle.CreateOptions, |
+ * long) |
+ */ |
+ public SharedBufferHandle createSharedBuffer( |
+ org.chromium.mojo.system.SharedBufferHandle.CreateOptions options, long numBytes) { |
+ return mCore.createSharedBuffer(options, numBytes); |
+ } |
+ |
+ /** |
+ * TODO(qsr): |
+ * |
+ * @return |
+ * @see Core#getDefaultAsyncWaiter() |
+ */ |
+ public AsyncWaiter getDefaultAsyncWaiter() { |
+ return mCore.getDefaultAsyncWaiter(); |
+ } |
+ |
+ public boolean waitForHandles(long timeout) { |
+ long startTime = System.currentTimeMillis(); |
+ while (System.currentTimeMillis() - startTime < timeout) { |
+ List<WeakReference<Handle>> liveHandles = new ArrayList<WeakReference<Handle>>(); |
+ for (WeakReference<Handle> weak : mHandles) { |
+ if (weak.get() != null) { |
+ liveHandles.add(weak); |
+ } |
+ } |
+ if (liveHandles.isEmpty()) { |
+ return true; |
+ } |
+ mHandles = liveHandles; |
+ System.gc(); |
+ System.runFinalization(); |
+ nativeRunLoop(1); |
+ } |
+ return false; |
+ } |
+ } |
+ |
+ private static class HandleMock implements UntypedHandle, MessagePipeHandle, |
+ ProducerHandle, ConsumerHandle, SharedBufferHandle { |
+ |
+ /** |
+ * @see Handle#close() |
+ */ |
+ @Override |
+ public void close() { |
+ // Do nothing. |
+ } |
+ |
+ /** |
+ * @see Handle#wait(Core.WaitFlags, long) |
+ */ |
+ @Override |
+ public int wait(Core.WaitFlags flags, long deadline) { |
+ // Do nothing. |
+ return 0; |
+ } |
+ |
+ /** |
+ * @see Handle#isValid() |
+ */ |
+ @Override |
+ public boolean isValid() { |
+ // Do nothing. |
+ return true; |
+ } |
+ |
+ /** |
+ * @see Handle#toUntypedHandle() |
+ */ |
+ @Override |
+ public UntypedHandle toUntypedHandle() { |
+ return this; |
+ } |
+ |
+ /** |
+ * @see Handle#getCore() |
+ */ |
+ @Override |
+ public Core getCore() { |
+ return CoreImpl.getInstance(); |
+ } |
+ |
+ /** |
+ * @see Handle#pass() |
+ */ |
+ @Override |
+ public HandleMock pass() { |
+ return this; |
+ } |
+ |
+ /** |
+ * @see ConsumerHandle#discardData(int, DataPipe.ReadFlags) |
+ */ |
+ @Override |
+ public int discardData(int numBytes, DataPipe.ReadFlags flags) { |
+ // Do nothing. |
+ return 0; |
+ } |
+ |
+ /** |
+ * @see ConsumerHandle#readData(java.nio.ByteBuffer, DataPipe.ReadFlags) |
+ */ |
+ @Override |
+ public int readData(ByteBuffer elements, |
+ DataPipe.ReadFlags flags) { |
+ // Do nothing. |
+ return 0; |
+ } |
+ |
+ /** |
+ * @see ConsumerHandle#beginReadData(int, DataPipe.ReadFlags) |
+ */ |
+ @Override |
+ public ByteBuffer beginReadData(int numBytes, |
+ DataPipe.ReadFlags flags) { |
+ // Do nothing. |
+ return null; |
+ } |
+ |
+ /** |
+ * @see ConsumerHandle#endReadData(int) |
+ */ |
+ @Override |
+ public void endReadData(int numBytesRead) { |
+ // Do nothing. |
+ } |
+ |
+ /** |
+ * @see ProducerHandle#writeData(java.nio.ByteBuffer, DataPipe.WriteFlags) |
+ */ |
+ @Override |
+ public int writeData(ByteBuffer elements, |
+ DataPipe.WriteFlags flags) { |
+ // Do nothing. |
+ return 0; |
+ } |
+ |
+ /** |
+ * @see ProducerHandle#beginWriteData(int, DataPipe.WriteFlags) |
+ */ |
+ @Override |
+ public ByteBuffer beginWriteData(int numBytes, |
+ DataPipe.WriteFlags flags) { |
+ // Do nothing. |
+ return null; |
+ } |
+ |
+ /** |
+ * @see ProducerHandle#endWriteData(int) |
+ */ |
+ @Override |
+ public void endWriteData(int numBytesWritten) { |
+ // Do nothing. |
+ } |
+ |
+ /** |
+ * @see MessagePipeHandle#writeMessage(java.nio.ByteBuffer, java.util.List, |
+ * MessagePipeHandle.WriteFlags) |
+ */ |
+ @Override |
+ public void writeMessage(ByteBuffer bytes, List<? extends Handle> handles, |
+ WriteFlags flags) { |
+ // Do nothing. |
+ } |
+ |
+ /** |
+ * @see MessagePipeHandle#readMessage(java.nio.ByteBuffer, int, |
+ * MessagePipeHandle.ReadFlags) |
+ */ |
+ @Override |
+ public ReadMessageResult readMessage(ByteBuffer bytes, int maxNumberOfHandles, |
+ ReadFlags flags) { |
+ // Do nothing. |
+ return null; |
+ } |
+ |
+ /** |
+ * @see UntypedHandle#toMessagePipeHandle() |
+ */ |
+ @Override |
+ public MessagePipeHandle toMessagePipeHandle() { |
+ return this; |
+ } |
+ |
+ /** |
+ * @see UntypedHandle#toDataPipeConsumerHandle() |
+ */ |
+ @Override |
+ public ConsumerHandle toDataPipeConsumerHandle() { |
+ return this; |
+ } |
+ |
+ /** |
+ * @see UntypedHandle#toDataPipeProducerHandle() |
+ */ |
+ @Override |
+ public ProducerHandle toDataPipeProducerHandle() { |
+ return this; |
+ } |
+ |
+ /** |
+ * @see UntypedHandle#toSharedBufferHandle() |
+ */ |
+ @Override |
+ public SharedBufferHandle toSharedBufferHandle() { |
+ return this; |
+ } |
+ |
+ /** |
+ * @see SharedBufferHandle#duplicate(SharedBufferHandle.DuplicateOptions) |
+ */ |
+ @Override |
+ public SharedBufferHandle duplicate(DuplicateOptions options) { |
+ // Do nothing. |
+ return null; |
+ } |
+ |
+ /** |
+ * @see SharedBufferHandle#map(long, long, SharedBufferHandle.MapFlags) |
+ */ |
+ @Override |
+ public ByteBuffer map(long offset, long numBytes, MapFlags flags) { |
+ // Do nothing. |
+ return null; |
+ } |
+ |
+ /** |
+ * @see SharedBufferHandle#unmap(java.nio.ByteBuffer) |
+ */ |
+ @Override |
+ public void unmap(ByteBuffer buffer) { |
+ // Do nothing. |
+ } |
+ |
+ } |
+ |
+ private static void assertArrayEquals(Object a, Object b) { |
+ if (a == b) { |
return; |
} |
- assertTrue(bar != null && bar2 != null); |
- assertEquals(bar.alpha, bar2.alpha); |
- assertEquals(bar.beta, bar2.beta); |
- assertEquals(bar.gamma, bar2.gamma); |
- assertEquals(bar.type, bar2.type); |
+ assertNotNull(a); |
+ assertNotNull(b); |
+ int length = Array.getLength(a); |
+ assertEquals(length, Array.getLength(b)); |
+ for (int i = 0; i < length; ++i) { |
+ Object v1 = Array.get(a, i); |
+ Object v2 = Array.get(b, i); |
+ if (v1 == null || v2 == null || !v1.getClass().isArray() |
+ || !v2.getClass().isArray()) { |
+ assertEquals(v1, v2); |
+ } else { |
+ assertArrayEquals(v1, v2); |
+ } |
+ |
+ } |
+ |
} |
- /** |
- * Create a new typical Foo instance. |
- */ |
- private static Foo createFoo() { |
+ private Foo createFoo() { |
Foo foo = new Foo(); |
foo.name = "HELLO WORLD"; |
- foo.arrayOfArrayOfBools = new boolean[][] { |
- { true, false, true }, null, {}, { false }, { true } }; |
+ boolean[][] b = new boolean[5][]; |
+ b[0] = new boolean[3]; |
+ b[0][0] = true; |
+ b[0][2] = true; |
+ foo.arrayOfArrayOfBools = b; |
foo.bar = newBar(); |
foo.a = true; |
foo.c = true; |
- foo.data = new byte[] { 0x01, 0x02, 0x03 }; |
- foo.extraBars = new Bar[] { newBar(), newBar() }; |
+ foo.data = new byte[] { |
+ 0x01, 0x02, 0x03 }; |
+ Bar[] bars = new Bar[2]; |
+ for (int i = 0; i < bars.length; ++i) { |
+ bars[i] = newBar(); |
+ } |
+ foo.extraBars = bars; |
String[][][] strings = new String[3][2][1]; |
for (int i0 = 0; i0 < strings.length; ++i0) { |
for (int i1 = 0; i1 < strings[i0].length; ++i1) { |
@@ -95,9 +450,6 @@ public class BindingsTest extends TestCase { |
return foo; |
} |
- /** |
- * Check that 2 Foo instances are equals. |
- */ |
private static void assertFooEquals(Foo foo1, Foo foo2) { |
assertEquals(foo1.a, foo2.a); |
assertEquals(foo1.b, foo2.b); |
@@ -105,13 +457,13 @@ public class BindingsTest extends TestCase { |
assertEquals(foo1.name, foo2.name); |
assertEquals(foo1.x, foo2.x); |
assertEquals(foo1.y, foo2.y); |
- TestCase.assertTrue(Arrays.deepEquals(foo1.arrayOfArrayOfBools, foo2.arrayOfArrayOfBools)); |
+ assertArrayEquals(foo1.arrayOfArrayOfBools, foo2.arrayOfArrayOfBools); |
assertBarEquals(foo1.bar, foo2.bar); |
- assertTrue(Arrays.equals(foo1.data, foo2.data)); |
- TestCase.assertTrue(Arrays.deepEquals(foo1.multiArrayOfStrings, foo2.multiArrayOfStrings)); |
+ assertArrayEquals(foo1.data, foo2.data); |
+ assertArrayEquals(foo1.multiArrayOfStrings, foo2.multiArrayOfStrings); |
assertEquals(foo1.source, foo2.source); |
- TestCase.assertTrue(Arrays.deepEquals(foo1.inputStreams, foo2.inputStreams)); |
- TestCase.assertTrue(Arrays.deepEquals(foo1.outputStreams, foo2.outputStreams)); |
+ assertArrayEquals(foo1.inputStreams, foo2.inputStreams); |
+ assertArrayEquals(foo1.outputStreams, foo2.outputStreams); |
if (foo1.extraBars != foo2.extraBars) { |
assertEquals(foo1.extraBars.length, foo2.extraBars.length); |
for (int i = 0; i < foo1.extraBars.length; ++i) { |
@@ -120,115 +472,512 @@ public class BindingsTest extends TestCase { |
} |
} |
- private static <T> void checkConstantField( |
- Field field, Class<T> expectedClass, T value) throws IllegalAccessException { |
- assertEquals(expectedClass, field.getType()); |
- assertEquals(Modifier.FINAL, field.getModifiers() & Modifier.FINAL); |
- assertEquals(Modifier.STATIC, field.getModifiers() & Modifier.STATIC); |
- assertEquals(value, field.get(null)); |
+ private static void assertBarEquals(Bar bar, Bar bar2) { |
+ if (bar == bar2) { |
+ return; |
+ } |
+ assertTrue(bar != null && bar2 != null); |
+ assertEquals(bar.alpha, bar2.alpha); |
+ assertEquals(bar.beta, bar2.beta); |
+ assertEquals(bar.gamma, bar2.gamma); |
+ assertEquals(bar.type, bar2.type); |
} |
- private static <T> void checkField(Field field, Class<T> expectedClass, |
- Object object, T value) throws IllegalArgumentException, IllegalAccessException { |
- assertEquals(expectedClass, field.getType()); |
- assertEquals(0, field.getModifiers() & Modifier.FINAL); |
- assertEquals(0, field.getModifiers() & Modifier.STATIC); |
- assertEquals(value, field.get(object)); |
+ private Bar newBar() { |
+ Bar bar = new Bar(); |
+ bar.alpha = (byte) 0x01; |
+ bar.beta = (byte) 0x02; |
+ bar.gamma = (byte) 0x03; |
+ bar.type = Type.TYPE_BOTH; |
+ return bar; |
} |
/** |
- * Testing constants are correctly generated. |
+ * Testing that a simple struct is identical to itself after being serialized then deserialized. |
*/ |
@SmallTest |
- public void testConstants() throws NoSuchFieldException, SecurityException, |
- IllegalAccessException { |
- checkConstantField(SampleServiceConstants.class.getField("TWELVE"), byte.class, (byte) 12); |
- checkConstantField(InterfaceConstants.class.getField("LONG"), long.class, 4405L); |
+ public void testFooSerializationDeserialization() { |
+ Foo foo = createFoo(); |
+ Message serialization = BindingsTestUtils.serialize(null, foo); |
+ assertEquals(ByteOrder.nativeOrder(), serialization.buffer.order()); |
+ for (Handle handle : serialization.handles) { |
+ assertNotNull(handle); |
+ } |
+ Foo foo2 = Foo.deserialize(serialization); |
+ assertFooEquals(foo, foo2); |
} |
/** |
- * Testing enums are correctly generated. |
+ * Testing that a message with not field is deserialized as a default instance. |
*/ |
@SmallTest |
- public void testEnums() throws NoSuchFieldException, SecurityException, |
- IllegalAccessException { |
- checkConstantField(Color.class.getField("COLOR_RED"), int.class, 0); |
- checkConstantField(Color.class.getField("COLOR_BLACK"), int.class, 1); |
+ public void testMissingFields() { |
+ ByteBuffer emptyMessage = ByteBuffer.allocateDirect(8); |
+ emptyMessage.order(ByteOrder.nativeOrder()); |
+ emptyMessage.putInt(0, 8); |
+ emptyMessage.putInt(4, 0); |
+ Foo foo = Foo.deserialize( |
+ new Message(emptyMessage, Collections.<Handle> emptyList())); |
+ assertFooEquals(new Foo(), foo); |
+ } |
- checkConstantField(Enum.class.getField("ENUM_VALUE"), int.class, 0); |
+ /** |
+ * Testing default values on a struct with holes deserialized from an older version. |
+ */ |
+ @SmallTest |
+ public void testStructWithHoleOldToNew() { |
+ StructWithHoleV1 s1 = new StructWithHoleV1(); |
+ assertEquals(1, s1.v1); |
+ assertEquals(2, s1.v2); |
+ StructWithHoleV2 s2 = StructWithHoleV2.deserialize( |
+ BindingsTestUtils.serialize(null, s1)); |
+ assertEquals(1, s2.v1); |
+ assertEquals(2, s2.v2); |
+ assertEquals(3, s2.v3); |
+ } |
- checkConstantField(Shape.class.getField("SHAPE_RECTANGLE"), int.class, 1); |
- checkConstantField(Shape.class.getField("SHAPE_CIRCLE"), int.class, 2); |
- checkConstantField(Shape.class.getField("SHAPE_TRIANGLE"), int.class, 3); |
+ /** |
+ * Testing default values on a struct with holes deserialized from an newer version. |
+ */ |
+ @SmallTest |
+ public void testStructWithHoleNewToOld() { |
+ StructWithHoleV2 s1 = new StructWithHoleV2(); |
+ s1.v1 = 2; |
+ s1.v2 = 3; |
+ s1.v3 = 4; |
+ assertEquals(2, s1.v1); |
+ assertEquals(3, s1.v2); |
+ assertEquals(4, s1.v3); |
+ StructWithHoleV1 s2 = StructWithHoleV1.deserialize( |
+ BindingsTestUtils.serialize(null, s1)); |
+ assertEquals(2, s2.v1); |
+ assertEquals(3, s2.v2); |
+ StructWithHoleV2 s3 = StructWithHoleV2.deserialize( |
+ BindingsTestUtils.serialize(null, s2)); |
+ assertEquals(2, s3.v1); |
+ assertEquals(3, s3.v2); |
} |
/** |
- * Testing default values on structs. |
- * |
- * @throws IllegalAccessException |
- * @throws IllegalArgumentException |
+ * Testing constants are correctly generated. |
*/ |
@SmallTest |
- public void testStructDefaults() throws NoSuchFieldException, SecurityException, |
- IllegalArgumentException, IllegalAccessException { |
- // Check default values. |
- DefaultsTest test = new DefaultsTest(); |
- |
- checkField(DefaultsTest.class.getField("a0"), byte.class, test, (byte) -12); |
- checkField(DefaultsTest.class.getField("a1"), byte.class, test, (byte) 12); |
- checkField(DefaultsTest.class.getField("a2"), short.class, test, (short) 1234); |
- checkField(DefaultsTest.class.getField("a3"), short.class, test, (short) 34567); |
- checkField(DefaultsTest.class.getField("a4"), int.class, test, 123456); |
- checkField(DefaultsTest.class.getField("a6"), long.class, test, 111111111111L); |
- checkField(DefaultsTest.class.getField("a8"), int.class, test, 0x12345); |
- checkField(DefaultsTest.class.getField("a9"), int.class, test, -0x12345); |
- checkField(DefaultsTest.class.getField("a10"), int.class, test, 1234); |
- checkField(DefaultsTest.class.getField("a11"), boolean.class, test, true); |
- checkField(DefaultsTest.class.getField("a12"), boolean.class, test, false); |
- checkField(DefaultsTest.class.getField("a13"), float.class, test, (float) 123.25); |
- checkField(DefaultsTest.class.getField("a14"), double.class, test, 1234567890.123); |
- checkField(DefaultsTest.class.getField("a15"), double.class, test, 1E10); |
- checkField(DefaultsTest.class.getField("a16"), double.class, test, -1.2E+20); |
- checkField(DefaultsTest.class.getField("a17"), double.class, test, +1.23E-20); |
- checkField(DefaultsTest.class.getField("a18"), byte[].class, test, null); |
- checkField(DefaultsTest.class.getField("a19"), String.class, test, null); |
- checkField(DefaultsTest.class.getField("a20"), int.class, test, Bar.Type.TYPE_BOTH); |
- checkField(DefaultsTest.class.getField("a21"), Point.class, test, null); |
- |
- assertNotNull(test.a22); |
- checkField(DefaultsTest.class.getField("a22"), Thing.class, test, test.a22); |
+ public void testConstants() throws NoSuchFieldException, SecurityException { |
+ assertEquals(12, SampleServiceConstants.TWELVE); |
+ Field threeField = SampleServiceConstants.class.getField("TWELVE"); |
+ assertEquals(byte.class, threeField.getType()); |
+ assertEquals(Modifier.FINAL, threeField.getModifiers() & Modifier.FINAL); |
+ assertEquals(Modifier.STATIC, threeField.getModifiers() & Modifier.STATIC); |
+ |
+ assertEquals("Fooby", Foo.FOOBY); |
+ assertEquals(1, Service.FAVORITE_BAZ); |
+ } |
+ |
+ static class ObjectImpl extends BaseErrorHandler implements NamedObject { |
+ |
+ private String mName; |
+ |
+ /** |
+ * @see NamedObject#setName(java.lang.String) |
+ */ |
+ @Override |
+ public void setName(String name) { |
+ mName = name; |
+ } |
+ |
+ /** |
+ * @see NamedObject#getName(GetNameResponse) |
+ */ |
+ @Override |
+ public void getName(GetNameResponse callback) { |
+ callback.call(mName); |
+ } |
+ |
+ } |
+ |
+ static class FactoryImpl extends BaseErrorHandler implements Factory { |
+ private FactoryClient mFactoryClient; |
+ |
+ private boolean mDoStuffCalled = false; |
+ |
+ private boolean mDoStuff2Called = false; |
+ |
+ @Override |
+ public void doStuff(Request request, MessagePipeHandle pipe) { |
+ mDoStuffCalled = true; |
+ mFactoryClient.didStuff(null, "doStuff"); |
+ } |
+ |
+ @Override |
+ public void doStuff2(ConsumerHandle pipe) { |
+ mDoStuff2Called = true; |
+ mFactoryClient.didStuff2("doStuff2"); |
+ } |
+ |
+ /** |
+ * @return the mDoStuffCalled |
+ */ |
+ public boolean isDoStuffCalled() { |
+ return mDoStuffCalled; |
+ } |
+ |
+ /** |
+ * @return the mDoStuff2Called |
+ */ |
+ public boolean isDoStuff2Called() { |
+ return mDoStuff2Called; |
+ } |
+ |
+ /** |
+ * @see Factory#createObject(NamedObject) |
+ */ |
+ @Override |
+ public void createNamedObject( |
+ InterfaceRequest<NamedObject> object) { |
+ NamedObject.BUILDER.bind(object, new ObjectImpl()); |
+ } |
+ |
+ /** |
+ * @see InterfaceWithClient#setClient(Interface) |
+ */ |
+ @Override |
+ public void setClient(FactoryClient client) { |
+ this.mFactoryClient = client; |
+ } |
+ |
+ /** |
+ * @see Factory#requestImportedInterface(InterfaceRequest, |
+ * Factory.RequestImportedInterfaceResponse) |
+ */ |
+ @Override |
+ public void requestImportedInterface(InterfaceRequest<ImportedInterface> obj, |
+ RequestImportedInterfaceResponse callback) { |
+ // TODO(qsr): Auto-generated method stub |
+ |
+ } |
+ |
+ /** |
+ * @see Factory#takeImportedInterface(ImportedInterface, |
+ * Factory.TakeImportedInterfaceResponse) |
+ */ |
+ @Override |
+ public void takeImportedInterface(ImportedInterface obj, |
+ TakeImportedInterfaceResponse callback) { |
+ // TODO(qsr): Auto-generated method stub |
+ |
+ } |
+ |
} |
/** |
- * Testing generation of the Foo class. |
- * |
- * @throws IllegalAccessException |
+ * TODO(qsr): |
*/ |
@SmallTest |
- public void testFooGeneration() throws NoSuchFieldException, SecurityException, |
- IllegalAccessException { |
- // Checking Foo constants. |
- checkConstantField(Foo.class.getField("FOOBY"), String.class, "Fooby"); |
+ public void testOutOfBoundMessage() { |
+ try { |
+ Foo.deserialize( |
+ new Message(ByteBuffer.allocateDirect(0), new ArrayList<Handle>())); |
+ fail("Exception should have been thrown"); |
+ } catch (DeserializationException expected) { |
+ } |
- // Checking Foo default values. |
- Foo foo = new Foo(); |
- checkField(Foo.class.getField("name"), String.class, foo, Foo.FOOBY); |
+ try { |
+ Foo foo = createFoo(); |
+ Message s = BindingsTestUtils.serialize(null, foo); |
+ s.buffer.putLong(24, s.buffer.limit()); |
+ Foo.deserialize(s); |
+ fail("Exception should have been thrown"); |
+ } catch (DeserializationException expected) { |
+ } |
- assertNotNull(foo.source); |
- assertFalse(foo.source.isValid()); |
- checkField(Foo.class.getField("source"), MessagePipeHandle.class, foo, foo.source); |
+ try { |
+ Foo foo = createFoo(); |
+ Message s = BindingsTestUtils.serialize(null, foo); |
+ Foo.deserialize(new Message(s.buffer, new ArrayList<Handle>())); |
+ fail("Exception should have been thrown"); |
+ } catch (DeserializationException e) { |
+ } |
} |
/** |
- * Testing serialization of the Foo class. |
+ * TODO(qsr): |
*/ |
@SmallTest |
- public void testFooSerialization() { |
- // Checking serialization and deserialization of a Foo object. |
- Foo typicalFoo = createFoo(); |
- Message serializedFoo = typicalFoo.serialize(null); |
- Foo deserializedFoo = Foo.deserialize(serializedFoo); |
- assertFooEquals(typicalFoo, deserializedFoo); |
+ public void testStubAndProxy() { |
+ Factory impl = new FactoryImpl(); |
+ FactoryClient client = new FactoryClientImpl(); |
+ impl.setClient(client); |
+ Factory proxy = Factory.BUILDER.buildProxy(null, Factory.BUILDER.buildStub(null, impl)); |
+ |
+ Request request = new Request(); |
+ request.x = 5; |
+ proxy.doStuff(request, null); |
+ } |
+ |
+ class FactoryClientImpl extends BaseErrorHandler implements FactoryClient { |
+ |
+ private String mText = null; |
+ private boolean mDidStuffCalled = false; |
+ private String mText2 = null; |
+ private boolean mDidStuff2Called = false; |
+ |
+ /** |
+ * @see FactoryClient#didStuff(Response, String) |
+ */ |
+ @Override |
+ public void didStuff(Response response, String text) { |
+ mText = text; |
+ mDidStuffCalled = true; |
+ } |
+ |
+ /** |
+ * @see org.chromium.mojo.bindings.test.sample.FactoryClient#didStuff2(java.lang.String) |
+ */ |
+ @Override |
+ public void didStuff2(String text) { |
+ mText2 = text; |
+ mDidStuff2Called = true; |
+ } |
+ |
+ /** |
+ * @return the mText |
+ */ |
+ public String getText() { |
+ return mText; |
+ } |
+ |
+ /** |
+ * @return the mDidStuffCalled |
+ */ |
+ public boolean isDidStuffCalled() { |
+ return mDidStuffCalled; |
+ } |
+ |
+ /** |
+ * @return the mText2 |
+ */ |
+ public String getText2() { |
+ return mText2; |
+ } |
+ |
+ /** |
+ * @return the mDidStuff2Called |
+ */ |
+ public boolean isDidStuff2Called() { |
+ return mDidStuff2Called; |
+ } |
+ |
+ } |
+ |
+ @SmallTest |
+ public void testClients() { |
+ Core core = CoreImpl.getInstance(); |
+ Pair<MessagePipeHandle, MessagePipeHandle> handles = core.createMessagePipe(); |
+ FactoryImpl impl = new FactoryImpl(); |
+ FactoryClientImpl client = new FactoryClientImpl(); |
+ Factory.BUILDER.bind(handles.first, impl); |
+ Factory factory = Factory.BUILDER.connect(handles.second, client); |
+ nativeRunLoop(50); |
+ assertFalse(impl.isDoStuffCalled()); |
+ assertFalse(impl.isDoStuff2Called()); |
+ assertFalse(client.isDidStuffCalled()); |
+ assertFalse(client.isDidStuff2Called()); |
+ factory.doStuff(null, null); |
+ nativeRunLoop(50); |
+ assertTrue(impl.isDoStuffCalled()); |
+ assertFalse(impl.isDoStuff2Called()); |
+ assertTrue(client.isDidStuffCalled()); |
+ assertEquals("doStuff", client.getText()); |
+ assertFalse(client.isDidStuff2Called()); |
+ factory.doStuff2(null); |
+ nativeRunLoop(50); |
+ assertTrue(impl.isDoStuffCalled()); |
+ assertTrue(impl.isDoStuff2Called()); |
+ assertTrue(client.isDidStuffCalled()); |
+ assertEquals("doStuff", client.getText()); |
+ assertTrue(client.isDidStuff2Called()); |
+ assertEquals("doStuff2", client.getText2()); |
+ |
+ Pair<NamedObject.Proxy, InterfaceRequest<NamedObject>> ir = |
+ NamedObject.BUILDER.getInterfaceRequest(core); |
+ NamedObject.Proxy o = ir.first; |
+ o.setName("Hello World"); |
+ StringCallback f1 = new StringCallback(); |
+ o.getName(f1); |
+ factory.createNamedObject(ir.second); |
+ nativeRunLoop(50); |
+ assertTrue(f1.isCalled()); |
+ assertEquals("Hello World", f1.getValue()); |
+ } |
+ |
+ static class ProviderImpl extends BaseErrorHandler implements Provider { |
+ |
+ @Override |
+ public void echoString(String a, EchoStringResponse callback) { |
+ callback.call(a); |
+ } |
+ |
+ @Override |
+ public void echoStrings(String a, String b, EchoStringsResponse callback) { |
+ callback.call(a, b); |
+ } |
+ |
+ @Override |
+ public void echoMessagePipeHandle(MessagePipeHandle a, |
+ EchoMessagePipeHandleResponse callback) { |
+ callback.call(a); |
+ } |
+ |
+ @Override |
+ public void echoEnum(int a, EchoEnumResponse callback) { |
+ callback.call(a); |
+ } |
+ |
+ /** |
+ * @see InterfaceWithClient#setClient(Interface) |
+ */ |
+ @Override |
+ public void setClient(ProviderClient client) { |
+ // Not useful. |
+ } |
+ |
+ } |
+ |
+ class StringCallback implements NamedObject.GetNameResponse { |
+ |
+ private Object mValue; |
+ private boolean mCalled = false; |
+ |
+ @Override |
+ public void call(String t1) { |
+ mValue = t1; |
+ mCalled = true; |
+ } |
+ |
+ /** |
+ * @return the value |
+ */ |
+ public Object getValue() { |
+ return mValue; |
+ } |
+ |
+ /** |
+ * @return the called |
+ */ |
+ public boolean isCalled() { |
+ return mCalled; |
+ } |
+ } |
+ |
+ class Callback1Mock implements Provider.EchoEnumResponse { |
+ |
+ private Object mValue; |
+ private boolean mCalled = false; |
+ |
+ @Override |
+ public void call(Integer t1) { |
+ mValue = t1; |
+ mCalled = true; |
+ } |
+ |
+ /** |
+ * @return the value |
+ */ |
+ public Object getValue() { |
+ return mValue; |
+ } |
+ |
+ /** |
+ * @return the called |
+ */ |
+ public boolean isCalled() { |
+ return mCalled; |
+ } |
+ |
+ } |
+ |
+ @SmallTest |
+ public void testProvider() { |
+ Provider proxy = new Provider.Proxy(null, new Provider.Stub(null, new ProviderImpl())); |
+ Callback1Mock callback = new Callback1Mock(); |
+ proxy.echoEnum(4, callback); |
+ assertTrue(callback.isCalled()); |
+ assertEquals(Integer.valueOf(4), callback.getValue()); |
+ } |
+ |
+ @SmallTest |
+ public void testProvider2() { |
+ Core core = CoreImpl.getInstance(); |
+ Pair<MessagePipeHandle, MessagePipeHandle> handles = core.createMessagePipe(); |
+ Provider.BUILDER.bind(handles.first, new ProviderImpl()); |
+ Provider proxy = Provider.BUILDER.connect(handles.second); |
+ Callback1Mock callback = new Callback1Mock(); |
+ proxy.echoEnum(4, callback); |
+ assertFalse(callback.isCalled()); |
+ nativeRunLoop(50); |
+ assertTrue(callback.isCalled()); |
+ assertEquals(Integer.valueOf(4), callback.getValue()); |
+ proxy = null; |
+ } |
+ |
+ class PortImpl extends BaseErrorHandler implements Port { |
+ |
+ private String mMessage = null; |
+ |
+ /** |
+ * @return the mMessage |
+ */ |
+ public String getMessage() { |
+ return mMessage; |
+ } |
+ |
+ /** |
+ * @see org.chromium.mojo.bindings.test.sample.Port#postMessage(java.lang.String, |
+ * org.chromium.mojo.bindings.test.sample.Port) |
+ */ |
+ @Override |
+ public void postMessage(String messageText, Port port) { |
+ if (messageText.equals("")) { |
+ port.postMessage("Hello", null); |
+ } else { |
+ mMessage = messageText; |
+ } |
+ } |
+ |
+ } |
+ |
+ public void foo(Core core) { |
+ Pair<MessagePipeHandle, MessagePipeHandle> handles = core.createMessagePipe(); |
+ Port.BUILDER.bind(handles.first, new PortImpl()); |
+ Port proxy = Port.BUILDER.connect(handles.second); |
+ PortImpl p2 = new PortImpl(); |
+ assertNull(p2.getMessage()); |
+ proxy.postMessage("", p2); |
+ assertNull(p2.getMessage()); |
+ nativeRunLoop(50); |
+ assertEquals("Hello", p2.getMessage()); |
+ // r1.close(); |
+ // nativeRunLoop(50); |
+ } |
+ |
+ @SmallTest |
+ public void testSendingInterface() { |
+ CoreWrapper core = new CoreWrapper(CoreImpl.getInstance()); |
+ { |
+ foo(core); |
+ } |
+ assertTrue(core.waitForHandles(500)); |
+ /* |
+ * Log.e("TEST", "1"); System.gc(); System.gc(); System.gc(); System.gc(); System.gc(); |
+ * System.gc(); System.gc(); System.gc(); System.gc(); System.gc(); System.gc(); |
+ * System.gc(); System.gc(); System.gc(); System.gc(); System.gc(); System.gc(); |
+ * System.gc(); System.gc(); System.gc(); System.gc(); System.gc(); System.gc(); |
+ * System.runFinalization(); nativeRunLoop(50); System.gc(); System.gc(); System.gc(); |
+ * System.gc(); System.gc(); System.gc(); System.gc(); System.gc(); System.gc(); |
+ * System.gc(); System.gc(); System.gc(); System.gc(); System.gc(); System.gc(); |
+ * System.gc(); System.gc(); System.gc(); System.gc(); System.gc(); System.gc(); |
+ * System.gc(); System.gc(); System.gc(); System.runFinalization(); Log.e("TEST", "2"); |
+ */ |
} |
} |