• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3 // found in the LICENSE file.
4 
5 package org.chromium.mojo.bindings;
6 
7 import android.support.test.filters.SmallTest;
8 
9 import junit.framework.TestCase;
10 
11 import org.chromium.mojo.HandleMock;
12 import org.chromium.mojo.bindings.test.mojom.imported.Color;
13 import org.chromium.mojo.bindings.test.mojom.imported.Point;
14 import org.chromium.mojo.bindings.test.mojom.imported.Shape;
15 import org.chromium.mojo.bindings.test.mojom.imported.Thing;
16 import org.chromium.mojo.bindings.test.mojom.sample.Bar;
17 import org.chromium.mojo.bindings.test.mojom.sample.Bar.Type;
18 import org.chromium.mojo.bindings.test.mojom.sample.DefaultsTest;
19 import org.chromium.mojo.bindings.test.mojom.sample.Enum;
20 import org.chromium.mojo.bindings.test.mojom.sample.Foo;
21 import org.chromium.mojo.bindings.test.mojom.sample.InterfaceConstants;
22 import org.chromium.mojo.bindings.test.mojom.sample.SampleServiceConstants;
23 import org.chromium.mojo.bindings.test.mojom.test_structs.EmptyStruct;
24 import org.chromium.mojo.bindings.test.mojom.test_structs.Rect;
25 import org.chromium.mojo.system.DataPipe.ConsumerHandle;
26 import org.chromium.mojo.system.DataPipe.ProducerHandle;
27 import org.chromium.mojo.system.MessagePipeHandle;
28 
29 import java.lang.reflect.Field;
30 import java.lang.reflect.Modifier;
31 import java.util.HashMap;
32 import java.util.Map;
33 
34 /**
35  * Testing generated classes and associated features.
36  */
37 public class BindingsTest extends TestCase {
38 
39     /**
40      * Create a new typical Bar instance.
41      */
newBar()42     private static Bar newBar() {
43         Bar bar = new Bar();
44         bar.alpha = (byte) 0x01;
45         bar.beta = (byte) 0x02;
46         bar.gamma = (byte) 0x03;
47         bar.type = Type.BOTH;
48         return bar;
49     }
50 
51     /**
52      * Create a new typical Foo instance.
53      */
createFoo()54     private static Foo createFoo() {
55         Foo foo = new Foo();
56         foo.name = "HELLO WORLD";
57         foo.arrayOfArrayOfBools = new boolean[][] {
58             { true, false, true }, { }, { }, { false }, { true } };
59         foo.bar = newBar();
60         foo.a = true;
61         foo.c = true;
62         foo.data = new byte[] { 0x01, 0x02, 0x03 };
63         foo.extraBars = new Bar[] { newBar(), newBar() };
64         String[][][] strings = new String[3][2][1];
65         for (int i0 = 0; i0 < strings.length; ++i0) {
66             for (int i1 = 0; i1 < strings[i0].length; ++i1) {
67                 for (int i2 = 0; i2 < strings[i0][i1].length; ++i2) {
68                     strings[i0][i1][i2] = "Hello(" + i0 + ", " + i1 + ", " + i2 + ")";
69                 }
70             }
71         }
72         foo.multiArrayOfStrings = strings;
73         ConsumerHandle[] inputStreams = new ConsumerHandle[5];
74         for (int i = 0; i < inputStreams.length; ++i) {
75             inputStreams[i] = new HandleMock();
76         }
77         foo.inputStreams = inputStreams;
78         ProducerHandle[] outputStreams = new ProducerHandle[3];
79         for (int i = 0; i < outputStreams.length; ++i) {
80             outputStreams[i] = new HandleMock();
81         }
82         foo.outputStreams = outputStreams;
83         foo.source = new HandleMock();
84         return foo;
85     }
86 
createRect(int x, int y, int width, int height)87     private static Rect createRect(int x, int y, int width, int height) {
88         Rect rect = new Rect();
89         rect.x = x;
90         rect.y = y;
91         rect.width = width;
92         rect.height = height;
93         return rect;
94     }
95 
checkConstantField( Field field, Class<T> expectedClass, T value)96     private static <T> void checkConstantField(
97             Field field, Class<T> expectedClass, T value) throws IllegalAccessException {
98         assertEquals(expectedClass, field.getType());
99         assertEquals(Modifier.FINAL, field.getModifiers() & Modifier.FINAL);
100         assertEquals(Modifier.STATIC, field.getModifiers() & Modifier.STATIC);
101         assertEquals(value, field.get(null));
102     }
103 
checkField(Field field, Class<T> expectedClass, Object object, T value)104     private static <T> void checkField(Field field, Class<T> expectedClass,
105             Object object, T value) throws IllegalArgumentException, IllegalAccessException {
106         assertEquals(expectedClass, field.getType());
107         assertEquals(0, field.getModifiers() & Modifier.FINAL);
108         assertEquals(0, field.getModifiers() & Modifier.STATIC);
109         assertEquals(value, field.get(object));
110     }
111 
112     /**
113      * Testing constants are correctly generated.
114      */
115     @SmallTest
testConstants()116     public void testConstants() throws NoSuchFieldException, SecurityException,
117             IllegalAccessException {
118         checkConstantField(SampleServiceConstants.class.getField("TWELVE"), byte.class, (byte) 12);
119         checkConstantField(InterfaceConstants.class.getField("LONG"), long.class, 4405L);
120     }
121 
122     /**
123      * Testing enums are correctly generated.
124      */
125     @SmallTest
testEnums()126     public void testEnums() throws NoSuchFieldException, SecurityException,
127             IllegalAccessException {
128         checkConstantField(Color.class.getField("RED"), int.class, 0);
129         checkConstantField(Color.class.getField("BLACK"), int.class, 1);
130 
131         checkConstantField(Enum.class.getField("VALUE"), int.class, 0);
132 
133         checkConstantField(Shape.class.getField("RECTANGLE"), int.class, 1);
134         checkConstantField(Shape.class.getField("CIRCLE"), int.class, 2);
135         checkConstantField(Shape.class.getField("TRIANGLE"), int.class, 3);
136     }
137 
138     /**
139      * Testing default values on structs.
140      *
141      * @throws IllegalAccessException
142      * @throws IllegalArgumentException
143      */
144     @SmallTest
testStructDefaults()145     public void testStructDefaults() throws NoSuchFieldException, SecurityException,
146             IllegalArgumentException, IllegalAccessException {
147         // Check default values.
148         DefaultsTest test = new DefaultsTest();
149 
150         checkField(DefaultsTest.class.getField("a0"), byte.class, test, (byte) -12);
151         checkField(DefaultsTest.class.getField("a1"), byte.class, test, (byte) 12);
152         checkField(DefaultsTest.class.getField("a2"), short.class, test, (short) 1234);
153         checkField(DefaultsTest.class.getField("a3"), short.class, test, (short) 34567);
154         checkField(DefaultsTest.class.getField("a4"), int.class, test, 123456);
155         checkField(DefaultsTest.class.getField("a5"), int.class, test, (int) 3456789012L);
156         checkField(DefaultsTest.class.getField("a6"), long.class, test, -111111111111L);
157         // -8446744073709551617 == 9999999999999999999 - 2 ^ 64.
158         checkField(DefaultsTest.class.getField("a7"), long.class, test, -8446744073709551617L);
159         checkField(DefaultsTest.class.getField("a8"), int.class, test, 0x12345);
160         checkField(DefaultsTest.class.getField("a9"), int.class, test, -0x12345);
161         checkField(DefaultsTest.class.getField("a10"), int.class, test, 1234);
162         checkField(DefaultsTest.class.getField("a11"), boolean.class, test, true);
163         checkField(DefaultsTest.class.getField("a12"), boolean.class, test, false);
164         checkField(DefaultsTest.class.getField("a13"), float.class, test, (float) 123.25);
165         checkField(DefaultsTest.class.getField("a14"), double.class, test, 1234567890.123);
166         checkField(DefaultsTest.class.getField("a15"), double.class, test, 1E10);
167         checkField(DefaultsTest.class.getField("a16"), double.class, test, -1.2E+20);
168         checkField(DefaultsTest.class.getField("a17"), double.class, test, +1.23E-20);
169         checkField(DefaultsTest.class.getField("a18"), byte[].class, test, null);
170         checkField(DefaultsTest.class.getField("a19"), String.class, test, null);
171         checkField(DefaultsTest.class.getField("a20"), int.class, test, Bar.Type.BOTH);
172         checkField(DefaultsTest.class.getField("a21"), Point.class, test, null);
173 
174         assertNotNull(test.a22);
175         checkField(DefaultsTest.class.getField("a22"), Thing.class, test, test.a22);
176         checkField(DefaultsTest.class.getField("a23"), long.class, test, -1L);
177         checkField(DefaultsTest.class.getField("a24"), long.class, test, 0x123456789L);
178         checkField(DefaultsTest.class.getField("a25"), long.class, test, -0x123456789L);
179     }
180 
181     /**
182      * Testing generation of the Foo class.
183      *
184      * @throws IllegalAccessException
185      */
186     @SmallTest
testFooGeneration()187     public void testFooGeneration() throws NoSuchFieldException, SecurityException,
188             IllegalAccessException {
189         // Checking Foo constants.
190         checkConstantField(Foo.class.getField("FOOBY"), String.class, "Fooby");
191 
192         // Checking Foo default values.
193         Foo foo = new Foo();
194         checkField(Foo.class.getField("name"), String.class, foo, Foo.FOOBY);
195 
196         assertNotNull(foo.source);
197         assertFalse(foo.source.isValid());
198         checkField(Foo.class.getField("source"), MessagePipeHandle.class, foo, foo.source);
199     }
200 
201     /**
202      * Testing serialization of the Foo class.
203      */
204     @SmallTest
testFooSerialization()205     public void testFooSerialization() {
206         // Checking serialization and deserialization of a Foo object.
207         Foo typicalFoo = createFoo();
208         Message serializedFoo = typicalFoo.serialize(null);
209         Foo deserializedFoo = Foo.deserialize(serializedFoo);
210         assertEquals(typicalFoo, deserializedFoo);
211     }
212 
213     /**
214      * Testing serialization of the EmptyStruct class.
215      */
216     @SmallTest
testEmptyStructSerialization()217     public void testEmptyStructSerialization() {
218         // Checking serialization and deserialization of a EmptyStruct object.
219         Message serializedStruct = new EmptyStruct().serialize(null);
220         EmptyStruct emptyStruct = EmptyStruct.deserialize(serializedStruct);
221         assertNotNull(emptyStruct);
222     }
223 
224     // In testing maps we want to make sure that the key used when inserting an
225     // item the key used when looking it up again are different objects. Java
226     // has default implementations of equals and hashCode that use reference
227     // equality and hashing, respectively, and that's not what we want for our
228     // mojom values.
229     @SmallTest
testHashMapStructKey()230     public void testHashMapStructKey() {
231         Map<Rect, Integer> map = new HashMap<>();
232         map.put(createRect(1, 2, 3, 4), 123);
233 
234         Rect key = createRect(1, 2, 3, 4);
235         assertNotNull(map.get(key));
236         assertEquals(123, map.get(key).intValue());
237 
238         map.remove(key);
239         assertTrue(map.isEmpty());
240     }
241 }
242