| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.mojo.bindings; | 5 package org.chromium.mojo.bindings; |
| 6 | 6 |
| 7 import android.test.suitebuilder.annotation.SmallTest; | 7 import android.test.suitebuilder.annotation.SmallTest; |
| 8 | 8 |
| 9 import junit.framework.TestCase; | 9 import junit.framework.TestCase; |
| 10 | 10 |
| 11 import org.chromium.mojo.bindings.test.mojom.imported.Color; | 11 import org.chromium.mojo.bindings.test.imported.Color; |
| 12 import org.chromium.mojo.bindings.test.mojom.imported.Point; | 12 import org.chromium.mojo.bindings.test.imported.Shape; |
| 13 import org.chromium.mojo.bindings.test.mojom.imported.Shape; | 13 import org.chromium.mojo.bindings.test.sample.Enum; |
| 14 import org.chromium.mojo.bindings.test.mojom.imported.Thing; | 14 import org.chromium.mojo.bindings.test.sample.InterfaceConstants; |
| 15 import org.chromium.mojo.bindings.test.mojom.sample.Bar; | 15 import org.chromium.mojo.bindings.test.sample.SampleServiceConstants; |
| 16 import org.chromium.mojo.bindings.test.mojom.sample.DefaultsTest; | |
| 17 import org.chromium.mojo.bindings.test.mojom.sample.Enum; | |
| 18 import org.chromium.mojo.bindings.test.mojom.sample.Foo; | |
| 19 import org.chromium.mojo.bindings.test.mojom.sample.InterfaceConstants; | |
| 20 import org.chromium.mojo.bindings.test.mojom.sample.SampleServiceConstants; | |
| 21 import org.chromium.mojo.system.MessagePipeHandle; | |
| 22 | 16 |
| 23 import java.lang.reflect.Field; | 17 import java.lang.reflect.Field; |
| 24 import java.lang.reflect.Modifier; | 18 import java.lang.reflect.Modifier; |
| 25 | 19 |
| 26 /** | 20 /** |
| 27 * Testing generated classes and associated features. | 21 * Testing generated classes and associated features. |
| 28 */ | 22 */ |
| 29 public class BindingsTest extends TestCase { | 23 public class BindingsTest extends TestCase { |
| 30 | 24 |
| 31 private static <T> void checkConstantField( | 25 private static void checkConstantField(Field field, Class<?> expectedClass)
{ |
| 32 Field field, Class<T> expectedClass, T value) throws IllegalAccessEx
ception { | |
| 33 assertEquals(expectedClass, field.getType()); | 26 assertEquals(expectedClass, field.getType()); |
| 34 assertEquals(Modifier.FINAL, field.getModifiers() & Modifier.FINAL); | 27 assertEquals(Modifier.FINAL, field.getModifiers() & Modifier.FINAL); |
| 35 assertEquals(Modifier.STATIC, field.getModifiers() & Modifier.STATIC); | 28 assertEquals(Modifier.STATIC, field.getModifiers() & Modifier.STATIC); |
| 36 assertEquals(value, field.get(null)); | |
| 37 } | |
| 38 | |
| 39 private static <T> void checkField(Field field, Class<T> expectedClass, | |
| 40 Object object, T value) throws IllegalArgumentException, IllegalAcce
ssException { | |
| 41 assertEquals(expectedClass, field.getType()); | |
| 42 assertEquals(0, field.getModifiers() & Modifier.FINAL); | |
| 43 assertEquals(0, field.getModifiers() & Modifier.STATIC); | |
| 44 assertEquals(value, field.get(object)); | |
| 45 } | 29 } |
| 46 | 30 |
| 47 /** | 31 /** |
| 48 * Testing constants are correctly generated. | 32 * Testing constants are correctly generated. |
| 49 */ | 33 */ |
| 50 @SmallTest | 34 @SmallTest |
| 51 public void testConstants() throws NoSuchFieldException, SecurityException, | 35 public void testConstants() throws NoSuchFieldException, SecurityException { |
| 52 IllegalAccessException { | 36 assertEquals(12, SampleServiceConstants.TWELVE); |
| 53 checkConstantField(SampleServiceConstants.class.getField("TWELVE"), byte
.class, (byte) 12); | 37 checkConstantField(SampleServiceConstants.class.getField("TWELVE"), byte
.class); |
| 54 checkConstantField(InterfaceConstants.class.getField("LONG"), long.class
, 4405L); | 38 |
| 39 assertEquals(4405, InterfaceConstants.LONG); |
| 40 checkConstantField(InterfaceConstants.class.getField("LONG"), long.class
); |
| 55 } | 41 } |
| 56 | 42 |
| 57 /** | 43 /** |
| 58 * Testing enums are correctly generated. | 44 * Testing enums are correctly generated. |
| 59 */ | 45 */ |
| 60 @SmallTest | 46 @SmallTest |
| 61 public void testEnums() throws NoSuchFieldException, SecurityException, | 47 public void testEnums() throws NoSuchFieldException, SecurityException { |
| 62 IllegalAccessException { | 48 assertEquals(0, Color.COLOR_RED); |
| 63 checkConstantField(Color.class.getField("COLOR_RED"), int.class, 0); | 49 assertEquals(1, Color.COLOR_BLACK); |
| 64 checkConstantField(Color.class.getField("COLOR_BLACK"), int.class, 1); | 50 checkConstantField(Color.class.getField("COLOR_BLACK"), int.class); |
| 51 checkConstantField(Color.class.getField("COLOR_RED"), int.class); |
| 65 | 52 |
| 66 checkConstantField(Enum.class.getField("ENUM_VALUE"), int.class, 0); | 53 assertEquals(0, Enum.ENUM_VALUE); |
| 54 checkConstantField(Enum.class.getField("ENUM_VALUE"), int.class); |
| 67 | 55 |
| 68 checkConstantField(Shape.class.getField("SHAPE_RECTANGLE"), int.class, 1
); | 56 assertEquals(1, Shape.SHAPE_RECTANGLE); |
| 69 checkConstantField(Shape.class.getField("SHAPE_CIRCLE"), int.class, 2); | 57 assertEquals(2, Shape.SHAPE_CIRCLE); |
| 70 checkConstantField(Shape.class.getField("SHAPE_TRIANGLE"), int.class, 3)
; | 58 assertEquals(3, Shape.SHAPE_TRIANGLE); |
| 59 checkConstantField(Shape.class.getField("SHAPE_RECTANGLE"), int.class); |
| 60 checkConstantField(Shape.class.getField("SHAPE_CIRCLE"), int.class); |
| 61 checkConstantField(Shape.class.getField("SHAPE_TRIANGLE"), int.class); |
| 71 } | 62 } |
| 72 | 63 |
| 73 /** | |
| 74 * Testing default values on structs. | |
| 75 * | |
| 76 * @throws IllegalAccessException | |
| 77 * @throws IllegalArgumentException | |
| 78 */ | |
| 79 @SmallTest | |
| 80 public void testStructDefaults() throws NoSuchFieldException, SecurityExcept
ion, | |
| 81 IllegalArgumentException, IllegalAccessException { | |
| 82 // Check default values. | |
| 83 DefaultsTest test = new DefaultsTest(); | |
| 84 | |
| 85 checkField(DefaultsTest.class.getField("a0"), byte.class, test, (byte) -
12); | |
| 86 checkField(DefaultsTest.class.getField("a1"), byte.class, test, (byte) 1
2); | |
| 87 checkField(DefaultsTest.class.getField("a2"), short.class, test, (short)
1234); | |
| 88 checkField(DefaultsTest.class.getField("a3"), short.class, test, (short)
34567); | |
| 89 checkField(DefaultsTest.class.getField("a4"), int.class, test, 123456); | |
| 90 checkField(DefaultsTest.class.getField("a6"), long.class, test, 11111111
1111L); | |
| 91 checkField(DefaultsTest.class.getField("a8"), int.class, test, 0x12345); | |
| 92 checkField(DefaultsTest.class.getField("a9"), int.class, test, -0x12345)
; | |
| 93 checkField(DefaultsTest.class.getField("a10"), int.class, test, 1234); | |
| 94 checkField(DefaultsTest.class.getField("a11"), boolean.class, test, true
); | |
| 95 checkField(DefaultsTest.class.getField("a12"), boolean.class, test, fals
e); | |
| 96 checkField(DefaultsTest.class.getField("a13"), float.class, test, (float
) 123.25); | |
| 97 checkField(DefaultsTest.class.getField("a14"), double.class, test, 12345
67890.123); | |
| 98 checkField(DefaultsTest.class.getField("a15"), double.class, test, 1E10)
; | |
| 99 checkField(DefaultsTest.class.getField("a16"), double.class, test, -1.2E
+20); | |
| 100 checkField(DefaultsTest.class.getField("a17"), double.class, test, +1.23
E-20); | |
| 101 checkField(DefaultsTest.class.getField("a18"), byte[].class, test, null)
; | |
| 102 checkField(DefaultsTest.class.getField("a19"), String.class, test, null)
; | |
| 103 checkField(DefaultsTest.class.getField("a20"), int.class, test, Bar.Type
.TYPE_BOTH); | |
| 104 checkField(DefaultsTest.class.getField("a21"), Point.class, test, null); | |
| 105 | |
| 106 assertNotNull(test.a22); | |
| 107 checkField(DefaultsTest.class.getField("a22"), Thing.class, test, test.a
22); | |
| 108 } | |
| 109 | |
| 110 /** | |
| 111 * Testing generation of the Foo class. | |
| 112 * | |
| 113 * @throws IllegalAccessException | |
| 114 */ | |
| 115 @SmallTest | |
| 116 public void testFooGeneration() throws NoSuchFieldException, SecurityExcepti
on, | |
| 117 IllegalAccessException { | |
| 118 // Checking Foo constants. | |
| 119 checkConstantField(Foo.class.getField("FOOBY"), String.class, "Fooby"); | |
| 120 | |
| 121 // Checking Foo default values. | |
| 122 Foo foo = new Foo(); | |
| 123 checkField(Foo.class.getField("name"), String.class, foo, Foo.FOOBY); | |
| 124 | |
| 125 assertNotNull(foo.source); | |
| 126 assertFalse(foo.source.isValid()); | |
| 127 checkField(Foo.class.getField("source"), MessagePipeHandle.class, foo, f
oo.source); | |
| 128 } | |
| 129 } | 64 } |
| OLD | NEW |