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 org.junit.Assert;
10 import org.junit.Test;
11 import org.junit.runner.RunWith;
12 
13 import org.chromium.base.test.BaseJUnit4ClassRunner;
14 import org.chromium.mojo.HandleMock;
15 import org.chromium.mojo.bindings.test.mojom.imported.Color;
16 import org.chromium.mojo.bindings.test.mojom.imported.Point;
17 import org.chromium.mojo.bindings.test.mojom.imported.Shape;
18 import org.chromium.mojo.bindings.test.mojom.imported.Thing;
19 import org.chromium.mojo.bindings.test.mojom.sample.Bar;
20 import org.chromium.mojo.bindings.test.mojom.sample.Bar.Type;
21 import org.chromium.mojo.bindings.test.mojom.sample.DefaultsTest;
22 import org.chromium.mojo.bindings.test.mojom.sample.Enum;
23 import org.chromium.mojo.bindings.test.mojom.sample.Foo;
24 import org.chromium.mojo.bindings.test.mojom.sample.InterfaceConstants;
25 import org.chromium.mojo.bindings.test.mojom.sample.SampleServiceConstants;
26 import org.chromium.mojo.bindings.test.mojom.test_structs.EmptyStruct;
27 import org.chromium.mojo.bindings.test.mojom.test_structs.Rect;
28 import org.chromium.mojo.system.DataPipe.ConsumerHandle;
29 import org.chromium.mojo.system.DataPipe.ProducerHandle;
30 import org.chromium.mojo.system.MessagePipeHandle;
31 
32 import java.lang.reflect.Field;
33 import java.lang.reflect.Modifier;
34 
35 /**
36  * Testing generated classes and associated features.
37  */
38 @RunWith(BaseJUnit4ClassRunner.class)
39 public class BindingsTest {
40     /**
41      * Create a new typical Bar instance.
42      */
newBar()43     private static Bar newBar() {
44         Bar bar = new Bar();
45         bar.alpha = (byte) 0x01;
46         bar.beta = (byte) 0x02;
47         bar.gamma = (byte) 0x03;
48         bar.type = Type.BOTH;
49         return bar;
50     }
51 
52     /**
53      * Create a new typical Foo instance.
54      */
createFoo()55     private static Foo createFoo() {
56         Foo foo = new Foo();
57         foo.name = "HELLO WORLD";
58         foo.arrayOfArrayOfBools = new boolean[][] {{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(Field field, Class<T> expectedClass, T value)
97             throws IllegalAccessException {
98         Assert.assertEquals(expectedClass, field.getType());
99         Assert.assertEquals(Modifier.FINAL, field.getModifiers() & Modifier.FINAL);
100         Assert.assertEquals(Modifier.STATIC, field.getModifiers() & Modifier.STATIC);
101         Assert.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, Object object, T value)
105             throws IllegalArgumentException, IllegalAccessException {
106         Assert.assertEquals(expectedClass, field.getType());
107         Assert.assertEquals(0, field.getModifiers() & Modifier.FINAL);
108         Assert.assertEquals(0, field.getModifiers() & Modifier.STATIC);
109         Assert.assertEquals(value, field.get(object));
110     }
111 
112     /**
113      * Testing constants are correctly generated.
114      */
115     @Test
116     @SmallTest
testConstants()117     public void testConstants()
118             throws NoSuchFieldException, SecurityException, IllegalAccessException {
119         checkConstantField(SampleServiceConstants.class.getField("TWELVE"), byte.class, (byte) 12);
120         checkConstantField(InterfaceConstants.class.getField("LONG"), long.class, 4405L);
121     }
122 
123     /**
124      * Testing enums are correctly generated.
125      */
126     @Test
127     @SmallTest
testEnums()128     public void testEnums() throws NoSuchFieldException, SecurityException, IllegalAccessException {
129         checkConstantField(Color.class.getField("RED"), int.class, 0);
130         checkConstantField(Color.class.getField("BLACK"), int.class, 1);
131 
132         checkConstantField(Enum.class.getField("VALUE"), int.class, 0);
133 
134         checkConstantField(Shape.class.getField("RECTANGLE"), int.class, 1);
135         checkConstantField(Shape.class.getField("CIRCLE"), int.class, 2);
136         checkConstantField(Shape.class.getField("TRIANGLE"), int.class, 3);
137     }
138 
139     /**
140      * Testing default values on structs.
141      *
142      * @throws IllegalAccessException
143      * @throws IllegalArgumentException
144      */
145     @Test
146     @SmallTest
testStructDefaults()147     public void testStructDefaults() throws NoSuchFieldException, SecurityException,
148                                             IllegalArgumentException, IllegalAccessException {
149         // Check default values.
150         DefaultsTest test = new DefaultsTest();
151 
152         checkField(DefaultsTest.class.getField("a0"), byte.class, test, (byte) -12);
153         checkField(DefaultsTest.class.getField("a1"), byte.class, test, (byte) 12);
154         checkField(DefaultsTest.class.getField("a2"), short.class, test, (short) 1234);
155         checkField(DefaultsTest.class.getField("a3"), short.class, test, (short) 34567);
156         checkField(DefaultsTest.class.getField("a4"), int.class, test, 123456);
157         checkField(DefaultsTest.class.getField("a5"), int.class, test, (int) 3456789012L);
158         checkField(DefaultsTest.class.getField("a6"), long.class, test, -111111111111L);
159         // -8446744073709551617 == 9999999999999999999 - 2 ^ 64.
160         checkField(DefaultsTest.class.getField("a7"), long.class, test, -8446744073709551617L);
161         checkField(DefaultsTest.class.getField("a8"), int.class, test, 0x12345);
162         checkField(DefaultsTest.class.getField("a9"), int.class, test, -0x12345);
163         checkField(DefaultsTest.class.getField("a10"), int.class, test, 1234);
164         checkField(DefaultsTest.class.getField("a11"), boolean.class, test, true);
165         checkField(DefaultsTest.class.getField("a12"), boolean.class, test, false);
166         checkField(DefaultsTest.class.getField("a13"), float.class, test, (float) 123.25);
167         checkField(DefaultsTest.class.getField("a14"), double.class, test, 1234567890.123);
168         checkField(DefaultsTest.class.getField("a15"), double.class, test, 1E10);
169         checkField(DefaultsTest.class.getField("a16"), double.class, test, -1.2E+20);
170         checkField(DefaultsTest.class.getField("a17"), double.class, test, +1.23E-20);
171         checkField(DefaultsTest.class.getField("a18"), byte[].class, test, null);
172         checkField(DefaultsTest.class.getField("a19"), String.class, test, null);
173         checkField(DefaultsTest.class.getField("a20"), int.class, test, Bar.Type.BOTH);
174         checkField(DefaultsTest.class.getField("a21"), Point.class, test, null);
175 
176         Assert.assertNotNull(test.a22);
177         checkField(DefaultsTest.class.getField("a22"), Thing.class, test, test.a22);
178         checkField(DefaultsTest.class.getField("a23"), long.class, test, -1L);
179         checkField(DefaultsTest.class.getField("a24"), long.class, test, 0x123456789L);
180         checkField(DefaultsTest.class.getField("a25"), long.class, test, -0x123456789L);
181     }
182 
183     /**
184      * Testing generation of the Foo class.
185      *
186      * @throws IllegalAccessException
187      */
188     @Test
189     @SmallTest
testFooGeneration()190     public void testFooGeneration()
191             throws NoSuchFieldException, SecurityException, IllegalAccessException {
192         // Checking Foo constants.
193         checkConstantField(Foo.class.getField("FOOBY"), String.class, "Fooby");
194 
195         // Checking Foo default values.
196         Foo foo = new Foo();
197         checkField(Foo.class.getField("name"), String.class, foo, Foo.FOOBY);
198 
199         Assert.assertNotNull(foo.source);
200         Assert.assertFalse(foo.source.isValid());
201         checkField(Foo.class.getField("source"), MessagePipeHandle.class, foo, foo.source);
202     }
203 
204     /**
205      * Testing serialization of the Foo class.
206      */
207     @Test
208     @SmallTest
testFooSerialization()209     public void testFooSerialization() {
210         // Checking serialization and deserialization of a Foo object.
211         Foo typicalFoo = createFoo();
212         Message serializedFoo = typicalFoo.serialize(null);
213         Foo deserializedFoo = Foo.deserialize(serializedFoo);
214         Assert.assertTrue(BindingsTestUtils.structsEqual(typicalFoo, deserializedFoo));
215     }
216 
217     /**
218      * Testing serialization of the EmptyStruct class.
219      */
220     @Test
221     @SmallTest
testEmptyStructSerialization()222     public void testEmptyStructSerialization() {
223         // Checking serialization and deserialization of a EmptyStruct object.
224         Message serializedStruct = new EmptyStruct().serialize(null);
225         EmptyStruct emptyStruct = EmptyStruct.deserialize(serializedStruct);
226         Assert.assertNotNull(emptyStruct);
227     }
228 }
229