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.imported.Color; | 11 import org.chromium.mojo.bindings.test.mojom.imported.Color; |
12 import org.chromium.mojo.bindings.test.imported.Shape; | 12 import org.chromium.mojo.bindings.test.mojom.imported.Point; |
13 import org.chromium.mojo.bindings.test.sample.Enum; | 13 import org.chromium.mojo.bindings.test.mojom.imported.Shape; |
14 import org.chromium.mojo.bindings.test.sample.InterfaceConstants; | 14 import org.chromium.mojo.bindings.test.mojom.imported.Thing; |
15 import org.chromium.mojo.bindings.test.sample.SampleServiceConstants; | 15 import org.chromium.mojo.bindings.test.mojom.sample.Bar; |
| 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; |
16 | 22 |
17 import java.lang.reflect.Field; | 23 import java.lang.reflect.Field; |
18 import java.lang.reflect.Modifier; | 24 import java.lang.reflect.Modifier; |
19 | 25 |
20 /** | 26 /** |
21 * Testing generated classes and associated features. | 27 * Testing generated classes and associated features. |
22 */ | 28 */ |
23 public class BindingsTest extends TestCase { | 29 public class BindingsTest extends TestCase { |
24 | 30 |
25 private static void checkConstantField(Field field, Class<?> expectedClass)
{ | 31 private static <T> void checkConstantField( |
| 32 Field field, Class<T> expectedClass, T value) throws IllegalAccessEx
ception { |
26 assertEquals(expectedClass, field.getType()); | 33 assertEquals(expectedClass, field.getType()); |
27 assertEquals(Modifier.FINAL, field.getModifiers() & Modifier.FINAL); | 34 assertEquals(Modifier.FINAL, field.getModifiers() & Modifier.FINAL); |
28 assertEquals(Modifier.STATIC, field.getModifiers() & Modifier.STATIC); | 35 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)); |
29 } | 45 } |
30 | 46 |
31 /** | 47 /** |
32 * Testing constants are correctly generated. | 48 * Testing constants are correctly generated. |
33 */ | 49 */ |
34 @SmallTest | 50 @SmallTest |
35 public void testConstants() throws NoSuchFieldException, SecurityException { | 51 public void testConstants() throws NoSuchFieldException, SecurityException, |
36 assertEquals(12, SampleServiceConstants.TWELVE); | 52 IllegalAccessException { |
37 checkConstantField(SampleServiceConstants.class.getField("TWELVE"), byte
.class); | 53 checkConstantField(SampleServiceConstants.class.getField("TWELVE"), byte
.class, (byte) 12); |
38 | 54 checkConstantField(InterfaceConstants.class.getField("LONG"), long.class
, 4405L); |
39 assertEquals(4405, InterfaceConstants.LONG); | |
40 checkConstantField(InterfaceConstants.class.getField("LONG"), long.class
); | |
41 } | 55 } |
42 | 56 |
43 /** | 57 /** |
44 * Testing enums are correctly generated. | 58 * Testing enums are correctly generated. |
45 */ | 59 */ |
46 @SmallTest | 60 @SmallTest |
47 public void testEnums() throws NoSuchFieldException, SecurityException { | 61 public void testEnums() throws NoSuchFieldException, SecurityException, |
48 assertEquals(0, Color.COLOR_RED); | 62 IllegalAccessException { |
49 assertEquals(1, Color.COLOR_BLACK); | 63 checkConstantField(Color.class.getField("COLOR_RED"), int.class, 0); |
50 checkConstantField(Color.class.getField("COLOR_BLACK"), int.class); | 64 checkConstantField(Color.class.getField("COLOR_BLACK"), int.class, 1); |
51 checkConstantField(Color.class.getField("COLOR_RED"), int.class); | |
52 | 65 |
53 assertEquals(0, Enum.ENUM_VALUE); | 66 checkConstantField(Enum.class.getField("ENUM_VALUE"), int.class, 0); |
54 checkConstantField(Enum.class.getField("ENUM_VALUE"), int.class); | |
55 | 67 |
56 assertEquals(1, Shape.SHAPE_RECTANGLE); | 68 checkConstantField(Shape.class.getField("SHAPE_RECTANGLE"), int.class, 1
); |
57 assertEquals(2, Shape.SHAPE_CIRCLE); | 69 checkConstantField(Shape.class.getField("SHAPE_CIRCLE"), int.class, 2); |
58 assertEquals(3, Shape.SHAPE_TRIANGLE); | 70 checkConstantField(Shape.class.getField("SHAPE_TRIANGLE"), int.class, 3)
; |
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); | |
62 } | 71 } |
63 | 72 |
| 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 } |
64 } | 129 } |
OLD | NEW |