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.test.suitebuilder.annotation.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.system.DataPipe.ConsumerHandle; 25 import org.chromium.mojo.system.DataPipe.ProducerHandle; 26 import org.chromium.mojo.system.MessagePipeHandle; 27 28 import java.lang.reflect.Field; 29 import java.lang.reflect.Modifier; 30 31 /** 32 * Testing generated classes and associated features. 33 */ 34 public class BindingsTest extends TestCase { 35 36 /** 37 * Create a new typical Bar instance. 38 */ newBar()39 private static Bar newBar() { 40 Bar bar = new Bar(); 41 bar.alpha = (byte) 0x01; 42 bar.beta = (byte) 0x02; 43 bar.gamma = (byte) 0x03; 44 bar.type = Type.BOTH; 45 return bar; 46 } 47 48 /** 49 * Create a new typical Foo instance. 50 */ createFoo()51 private static Foo createFoo() { 52 Foo foo = new Foo(); 53 foo.name = "HELLO WORLD"; 54 foo.arrayOfArrayOfBools = new boolean[][] { 55 { true, false, true }, { }, { }, { false }, { true } }; 56 foo.bar = newBar(); 57 foo.a = true; 58 foo.c = true; 59 foo.data = new byte[] { 0x01, 0x02, 0x03 }; 60 foo.extraBars = new Bar[] { newBar(), newBar() }; 61 String[][][] strings = new String[3][2][1]; 62 for (int i0 = 0; i0 < strings.length; ++i0) { 63 for (int i1 = 0; i1 < strings[i0].length; ++i1) { 64 for (int i2 = 0; i2 < strings[i0][i1].length; ++i2) { 65 strings[i0][i1][i2] = "Hello(" + i0 + ", " + i1 + ", " + i2 + ")"; 66 } 67 } 68 } 69 foo.multiArrayOfStrings = strings; 70 ConsumerHandle[] inputStreams = new ConsumerHandle[5]; 71 for (int i = 0; i < inputStreams.length; ++i) { 72 inputStreams[i] = new HandleMock(); 73 } 74 foo.inputStreams = inputStreams; 75 ProducerHandle[] outputStreams = new ProducerHandle[3]; 76 for (int i = 0; i < outputStreams.length; ++i) { 77 outputStreams[i] = new HandleMock(); 78 } 79 foo.outputStreams = outputStreams; 80 foo.source = new HandleMock(); 81 return foo; 82 } 83 checkConstantField( Field field, Class<T> expectedClass, T value)84 private static <T> void checkConstantField( 85 Field field, Class<T> expectedClass, T value) throws IllegalAccessException { 86 assertEquals(expectedClass, field.getType()); 87 assertEquals(Modifier.FINAL, field.getModifiers() & Modifier.FINAL); 88 assertEquals(Modifier.STATIC, field.getModifiers() & Modifier.STATIC); 89 assertEquals(value, field.get(null)); 90 } 91 checkField(Field field, Class<T> expectedClass, Object object, T value)92 private static <T> void checkField(Field field, Class<T> expectedClass, 93 Object object, T value) throws IllegalArgumentException, IllegalAccessException { 94 assertEquals(expectedClass, field.getType()); 95 assertEquals(0, field.getModifiers() & Modifier.FINAL); 96 assertEquals(0, field.getModifiers() & Modifier.STATIC); 97 assertEquals(value, field.get(object)); 98 } 99 100 /** 101 * Testing constants are correctly generated. 102 */ 103 @SmallTest testConstants()104 public void testConstants() throws NoSuchFieldException, SecurityException, 105 IllegalAccessException { 106 checkConstantField(SampleServiceConstants.class.getField("TWELVE"), byte.class, (byte) 12); 107 checkConstantField(InterfaceConstants.class.getField("LONG"), long.class, 4405L); 108 } 109 110 /** 111 * Testing enums are correctly generated. 112 */ 113 @SmallTest testEnums()114 public void testEnums() throws NoSuchFieldException, SecurityException, 115 IllegalAccessException { 116 checkConstantField(Color.class.getField("RED"), int.class, 0); 117 checkConstantField(Color.class.getField("BLACK"), int.class, 1); 118 119 checkConstantField(Enum.class.getField("VALUE"), int.class, 0); 120 121 checkConstantField(Shape.class.getField("RECTANGLE"), int.class, 1); 122 checkConstantField(Shape.class.getField("CIRCLE"), int.class, 2); 123 checkConstantField(Shape.class.getField("TRIANGLE"), int.class, 3); 124 } 125 126 /** 127 * Testing default values on structs. 128 * 129 * @throws IllegalAccessException 130 * @throws IllegalArgumentException 131 */ 132 @SmallTest testStructDefaults()133 public void testStructDefaults() throws NoSuchFieldException, SecurityException, 134 IllegalArgumentException, IllegalAccessException { 135 // Check default values. 136 DefaultsTest test = new DefaultsTest(); 137 138 checkField(DefaultsTest.class.getField("a0"), byte.class, test, (byte) -12); 139 checkField(DefaultsTest.class.getField("a1"), byte.class, test, (byte) 12); 140 checkField(DefaultsTest.class.getField("a2"), short.class, test, (short) 1234); 141 checkField(DefaultsTest.class.getField("a3"), short.class, test, (short) 34567); 142 checkField(DefaultsTest.class.getField("a4"), int.class, test, 123456); 143 checkField(DefaultsTest.class.getField("a5"), int.class, test, (int) 3456789012L); 144 checkField(DefaultsTest.class.getField("a6"), long.class, test, -111111111111L); 145 // -8446744073709551617 == 9999999999999999999 - 2 ^ 64. 146 checkField(DefaultsTest.class.getField("a7"), long.class, test, -8446744073709551617L); 147 checkField(DefaultsTest.class.getField("a8"), int.class, test, 0x12345); 148 checkField(DefaultsTest.class.getField("a9"), int.class, test, -0x12345); 149 checkField(DefaultsTest.class.getField("a10"), int.class, test, 1234); 150 checkField(DefaultsTest.class.getField("a11"), boolean.class, test, true); 151 checkField(DefaultsTest.class.getField("a12"), boolean.class, test, false); 152 checkField(DefaultsTest.class.getField("a13"), float.class, test, (float) 123.25); 153 checkField(DefaultsTest.class.getField("a14"), double.class, test, 1234567890.123); 154 checkField(DefaultsTest.class.getField("a15"), double.class, test, 1E10); 155 checkField(DefaultsTest.class.getField("a16"), double.class, test, -1.2E+20); 156 checkField(DefaultsTest.class.getField("a17"), double.class, test, +1.23E-20); 157 checkField(DefaultsTest.class.getField("a18"), byte[].class, test, null); 158 checkField(DefaultsTest.class.getField("a19"), String.class, test, null); 159 checkField(DefaultsTest.class.getField("a20"), int.class, test, Bar.Type.BOTH); 160 checkField(DefaultsTest.class.getField("a21"), Point.class, test, null); 161 162 assertNotNull(test.a22); 163 checkField(DefaultsTest.class.getField("a22"), Thing.class, test, test.a22); 164 checkField(DefaultsTest.class.getField("a23"), long.class, test, -1L); 165 checkField(DefaultsTest.class.getField("a24"), long.class, test, 0x123456789L); 166 checkField(DefaultsTest.class.getField("a25"), long.class, test, -0x123456789L); 167 } 168 169 /** 170 * Testing generation of the Foo class. 171 * 172 * @throws IllegalAccessException 173 */ 174 @SmallTest testFooGeneration()175 public void testFooGeneration() throws NoSuchFieldException, SecurityException, 176 IllegalAccessException { 177 // Checking Foo constants. 178 checkConstantField(Foo.class.getField("FOOBY"), String.class, "Fooby"); 179 180 // Checking Foo default values. 181 Foo foo = new Foo(); 182 checkField(Foo.class.getField("name"), String.class, foo, Foo.FOOBY); 183 184 assertNotNull(foo.source); 185 assertFalse(foo.source.isValid()); 186 checkField(Foo.class.getField("source"), MessagePipeHandle.class, foo, foo.source); 187 } 188 189 /** 190 * Testing serialization of the Foo class. 191 */ 192 @SmallTest testFooSerialization()193 public void testFooSerialization() { 194 // Checking serialization and deserialization of a Foo object. 195 Foo typicalFoo = createFoo(); 196 Message serializedFoo = typicalFoo.serialize(null); 197 Foo deserializedFoo = Foo.deserialize(serializedFoo); 198 assertEquals(typicalFoo, deserializedFoo); 199 } 200 201 /** 202 * Testing serialization of the EmptyStruct class. 203 */ 204 @SmallTest testEmptyStructSerialization()205 public void testEmptyStructSerialization() { 206 // Checking serialization and deserialization of a EmptyStruct object. 207 Message serializedStruct = new EmptyStruct().serialize(null); 208 EmptyStruct emptyStruct = EmptyStruct.deserialize(serializedStruct); 209 assertNotNull(emptyStruct); 210 } 211 212 } 213