diff options
author | qsr@chromium.org <qsr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-06-25 11:52:30 +0000 |
---|---|---|
committer | qsr@chromium.org <qsr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-06-25 11:52:30 +0000 |
commit | 56d6a92c1a19e2b7a7a8b23ca09b077a3e2cbd39 (patch) | |
tree | 1be4a063306fdb29f07b8846061c84a37c4a7fad /mojo/android/javatests | |
parent | 8aa0dd9e099da00a79d359b3440012de1ee284d0 (diff) | |
download | chromium_src-56d6a92c1a19e2b7a7a8b23ca09b077a3e2cbd39.zip chromium_src-56d6a92c1a19e2b7a7a8b23ca09b077a3e2cbd39.tar.gz chromium_src-56d6a92c1a19e2b7a7a8b23ca09b077a3e2cbd39.tar.bz2 |
Generate java bindings for structs.
This CL introduces bindings for struct. It only generates fields and not
serialization/deserialization methods.
R=rmcilroy@chromium.org, viettrungluu@chromium.org
Review URL: https://codereview.chromium.org/306893010
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@279677 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'mojo/android/javatests')
3 files changed, 305 insertions, 29 deletions
diff --git a/mojo/android/javatests/src/org/chromium/mojo/HandleMock.java b/mojo/android/javatests/src/org/chromium/mojo/HandleMock.java new file mode 100644 index 0000000..82b0f45 --- /dev/null +++ b/mojo/android/javatests/src/org/chromium/mojo/HandleMock.java @@ -0,0 +1,211 @@ +// 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; + +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.ProducerHandle; +import org.chromium.mojo.system.Handle; +import org.chromium.mojo.system.MessagePipeHandle; +import org.chromium.mojo.system.MojoResult; +import org.chromium.mojo.system.SharedBufferHandle; +import org.chromium.mojo.system.UntypedHandle; +import org.chromium.mojo.system.impl.CoreImpl; + +import java.nio.ByteBuffer; +import java.util.List; + +/** + * A mock handle, that does nothing. + */ +public 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 MojoResult.OK; + } + + /** + * @see Handle#isValid() + */ + @Override + public boolean isValid() { + return true; + } + + /** + * @see Handle#toUntypedHandle() + */ + @Override + public UntypedHandle toUntypedHandle() { + return this; + } + + /** + * @see org.chromium.mojo.system.Handle#getCore() + */ + @Override + public Core getCore() { + return CoreImpl.getInstance(); + } + + /** + * @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 new ReadMessageResult(); + } + + /** + * @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. + } + +} 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 27fba62..b084b7c 100644 --- a/mojo/android/javatests/src/org/chromium/mojo/bindings/BindingsTest.java +++ b/mojo/android/javatests/src/org/chromium/mojo/bindings/BindingsTest.java @@ -8,11 +8,17 @@ import android.test.suitebuilder.annotation.SmallTest; import junit.framework.TestCase; -import org.chromium.mojo.bindings.test.imported.Color; -import org.chromium.mojo.bindings.test.imported.Shape; -import org.chromium.mojo.bindings.test.sample.Enum; -import org.chromium.mojo.bindings.test.sample.InterfaceConstants; -import org.chromium.mojo.bindings.test.sample.SampleServiceConstants; +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.bindings.test.mojom.sample.Bar; +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.Foo; +import org.chromium.mojo.bindings.test.mojom.sample.InterfaceConstants; +import org.chromium.mojo.bindings.test.mojom.sample.SampleServiceConstants; +import org.chromium.mojo.system.MessagePipeHandle; import java.lang.reflect.Field; import java.lang.reflect.Modifier; @@ -22,43 +28,102 @@ import java.lang.reflect.Modifier; */ public class BindingsTest extends TestCase { - private static void checkConstantField(Field field, Class<?> expectedClass) { + 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 <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)); } /** * Testing constants are correctly generated. */ @SmallTest - public void testConstants() throws NoSuchFieldException, SecurityException { - assertEquals(12, SampleServiceConstants.TWELVE); - checkConstantField(SampleServiceConstants.class.getField("TWELVE"), byte.class); - - assertEquals(4405, InterfaceConstants.LONG); - checkConstantField(InterfaceConstants.class.getField("LONG"), long.class); + public void testConstants() throws NoSuchFieldException, SecurityException, + IllegalAccessException { + checkConstantField(SampleServiceConstants.class.getField("TWELVE"), byte.class, (byte) 12); + checkConstantField(InterfaceConstants.class.getField("LONG"), long.class, 4405L); } /** * Testing enums are correctly generated. */ @SmallTest - public void testEnums() throws NoSuchFieldException, SecurityException { - assertEquals(0, Color.COLOR_RED); - assertEquals(1, Color.COLOR_BLACK); - checkConstantField(Color.class.getField("COLOR_BLACK"), int.class); - checkConstantField(Color.class.getField("COLOR_RED"), int.class); - - assertEquals(0, Enum.ENUM_VALUE); - checkConstantField(Enum.class.getField("ENUM_VALUE"), int.class); - - assertEquals(1, Shape.SHAPE_RECTANGLE); - assertEquals(2, Shape.SHAPE_CIRCLE); - assertEquals(3, Shape.SHAPE_TRIANGLE); - checkConstantField(Shape.class.getField("SHAPE_RECTANGLE"), int.class); - checkConstantField(Shape.class.getField("SHAPE_CIRCLE"), int.class); - checkConstantField(Shape.class.getField("SHAPE_TRIANGLE"), int.class); + 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); + + checkConstantField(Enum.class.getField("ENUM_VALUE"), int.class, 0); + + 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 structs. + * + * @throws IllegalAccessException + * @throws IllegalArgumentException + */ + @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); + } + + /** + * Testing generation of the Foo class. + * + * @throws IllegalAccessException + */ + @SmallTest + public void testFooGeneration() throws NoSuchFieldException, SecurityException, + IllegalAccessException { + // Checking Foo constants. + checkConstantField(Foo.class.getField("FOOBY"), String.class, "Fooby"); + + // Checking Foo default values. + Foo foo = new Foo(); + checkField(Foo.class.getField("name"), String.class, foo, Foo.FOOBY); + + assertNotNull(foo.source); + assertFalse(foo.source.isValid()); + checkField(Foo.class.getField("source"), MessagePipeHandle.class, foo, foo.source); + } } diff --git a/mojo/android/javatests/src/org/chromium/mojo/system/impl/CoreImplTest.java b/mojo/android/javatests/src/org/chromium/mojo/system/impl/CoreImplTest.java index 0e4d4d7..928c4ef 100644 --- a/mojo/android/javatests/src/org/chromium/mojo/system/impl/CoreImplTest.java +++ b/mojo/android/javatests/src/org/chromium/mojo/system/impl/CoreImplTest.java @@ -493,7 +493,7 @@ public class CoreImplTest extends MojoTestCase { @SmallTest public void testInvalidHandle() { Core core = CoreImpl.getInstance(); - Handle handle = new InvalidHandle(); + Handle handle = InvalidHandle.INSTANCE; // Checking wait. boolean exception = false; @@ -713,7 +713,7 @@ public class CoreImplTest extends MojoTestCase { assertEquals(Integer.MIN_VALUE, asyncWaiterResult.getResult()); assertEquals(null, asyncWaiterResult.getException()); - core.getDefaultAsyncWaiter().asyncWait(new InvalidHandle(), Core.WaitFlags.READABLE, + core.getDefaultAsyncWaiter().asyncWait(InvalidHandle.INSTANCE, Core.WaitFlags.READABLE, Core.DEADLINE_INFINITE, asyncWaiterResult); assertEquals(Integer.MIN_VALUE, asyncWaiterResult.getResult()); assertEquals(null, asyncWaiterResult.getException()); |