OLD | NEW |
| (Empty) |
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 define([ | |
6 "gin/test/expect", | |
7 "mojo/public/interfaces/bindings/tests/rect.mojom", | |
8 "mojo/public/interfaces/bindings/tests/test_structs.mojom", | |
9 "mojo/public/js/codec", | |
10 "mojo/public/js/validator", | |
11 ], function(expect, | |
12 rect, | |
13 testStructs, | |
14 codec, | |
15 validator) { | |
16 | |
17 function testConstructors() { | |
18 var r = new rect.Rect(); | |
19 expect(r).toEqual(new rect.Rect({x:0, y:0, width:0, height:0})); | |
20 expect(r).toEqual(new rect.Rect({foo:100, bar:200})); | |
21 | |
22 r.x = 10; | |
23 r.y = 20; | |
24 r.width = 30; | |
25 r.height = 40; | |
26 var rp = new testStructs.RectPair({first: r, second: r}); | |
27 expect(rp.first).toEqual(r); | |
28 expect(rp.second).toEqual(r); | |
29 | |
30 expect(new testStructs.RectPair({second: r}).first).toBeNull(); | |
31 | |
32 var nr = new testStructs.NamedRegion(); | |
33 expect(nr.name).toBeNull(); | |
34 expect(nr.rects).toBeNull(); | |
35 expect(nr).toEqual(new testStructs.NamedRegion({})); | |
36 | |
37 nr.name = "foo"; | |
38 nr.rects = [r, r, r]; | |
39 expect(nr).toEqual(new testStructs.NamedRegion({ | |
40 name: "foo", | |
41 rects: [r, r, r], | |
42 })); | |
43 | |
44 var e = new testStructs.EmptyStruct(); | |
45 expect(e).toEqual(new testStructs.EmptyStruct({foo:123})); | |
46 } | |
47 | |
48 function testNoDefaultFieldValues() { | |
49 var s = new testStructs.NoDefaultFieldValues(); | |
50 expect(s.f0).toEqual(false); | |
51 | |
52 // f1 - f10, number type fields | |
53 for (var i = 1; i <= 10; i++) | |
54 expect(s["f" + i]).toEqual(0); | |
55 | |
56 // f11,12 strings, f13-22 handles, f23-f26 arrays, f27,28 structs | |
57 for (var i = 11; i <= 28; i++) | |
58 expect(s["f" + i]).toBeNull(); | |
59 } | |
60 | |
61 function testDefaultFieldValues() { | |
62 var s = new testStructs.DefaultFieldValues(); | |
63 expect(s.f0).toEqual(true); | |
64 | |
65 // f1 - f12, number type fields | |
66 for (var i = 1; i <= 12; i++) | |
67 expect(s["f" + i]).toEqual(100); | |
68 | |
69 // f13,14 "foo" | |
70 for (var i = 13; i <= 14; i++) | |
71 expect(s["f" + i]).toEqual("foo"); | |
72 | |
73 // f15,16 a default instance of Rect | |
74 var r = new rect.Rect(); | |
75 expect(s.f15).toEqual(r); | |
76 expect(s.f16).toEqual(r); | |
77 } | |
78 | |
79 function testScopedConstants() { | |
80 expect(testStructs.ScopedConstants.TEN).toEqual(10); | |
81 expect(testStructs.ScopedConstants.ALSO_TEN).toEqual(10); | |
82 expect(testStructs.ScopedConstants.TEN_TOO).toEqual(10); | |
83 | |
84 expect(testStructs.ScopedConstants.EType.E0).toEqual(0); | |
85 expect(testStructs.ScopedConstants.EType.E1).toEqual(1); | |
86 expect(testStructs.ScopedConstants.EType.E2).toEqual(10); | |
87 expect(testStructs.ScopedConstants.EType.E3).toEqual(10); | |
88 expect(testStructs.ScopedConstants.EType.E4).toEqual(11); | |
89 | |
90 var s = new testStructs.ScopedConstants(); | |
91 expect(s.f0).toEqual(0); | |
92 expect(s.f1).toEqual(1); | |
93 expect(s.f2).toEqual(10); | |
94 expect(s.f3).toEqual(10); | |
95 expect(s.f4).toEqual(11); | |
96 expect(s.f5).toEqual(10); | |
97 expect(s.f6).toEqual(10); | |
98 } | |
99 | |
100 function structEncodeDecode(struct) { | |
101 var structClass = struct.constructor; | |
102 var builder = new codec.MessageBuilder(1234, structClass.encodedSize); | |
103 builder.encodeStruct(structClass, struct); | |
104 var message = builder.finish(); | |
105 | |
106 var messageValidator = new validator.Validator(message); | |
107 var err = structClass.validate(messageValidator, codec.kMessageHeaderSize); | |
108 expect(err).toEqual(validator.validationError.NONE); | |
109 | |
110 var reader = new codec.MessageReader(message); | |
111 return reader.decodeStruct(structClass); | |
112 } | |
113 | |
114 function testMapKeyTypes() { | |
115 var mapFieldsStruct = new testStructs.MapKeyTypes({ | |
116 f0: new Map([[true, false], [false, true]]), // map<bool, bool> | |
117 f1: new Map([[0, 0], [1, 127], [-1, -128]]), // map<int8, int8> | |
118 f2: new Map([[0, 0], [1, 127], [2, 255]]), // map<uint8, uint8> | |
119 f3: new Map([[0, 0], [1, 32767], [2, -32768]]), // map<int16, int16> | |
120 f4: new Map([[0, 0], [1, 32768], [2, 0xFFFF]]), // map<uint16, uint16> | |
121 f5: new Map([[0, 0], [1, 32767], [2, -32768]]), // map<int32, int32> | |
122 f6: new Map([[0, 0], [1, 32768], [2, 0xFFFF]]), // map<uint32, uint32> | |
123 f7: new Map([[0, 0], [1, 32767], [2, -32768]]), // map<int64, int64> | |
124 f8: new Map([[0, 0], [1, 32768], [2, 0xFFFF]]), // map<uint64, uint64> | |
125 f9: new Map([[1000.5, -50000], [100.5, 5000]]), // map<float, float> | |
126 f10: new Map([[-100.5, -50000], [0, 50000000]]), // map<double, double> | |
127 f11: new Map([["one", "two"], ["free", "four"]]), // map<string, string> | |
128 }); | |
129 var decodedStruct = structEncodeDecode(mapFieldsStruct); | |
130 expect(decodedStruct.f0).toEqual(mapFieldsStruct.f0); | |
131 expect(decodedStruct.f1).toEqual(mapFieldsStruct.f1); | |
132 expect(decodedStruct.f2).toEqual(mapFieldsStruct.f2); | |
133 expect(decodedStruct.f3).toEqual(mapFieldsStruct.f3); | |
134 expect(decodedStruct.f4).toEqual(mapFieldsStruct.f4); | |
135 expect(decodedStruct.f5).toEqual(mapFieldsStruct.f5); | |
136 expect(decodedStruct.f6).toEqual(mapFieldsStruct.f6); | |
137 expect(decodedStruct.f7).toEqual(mapFieldsStruct.f7); | |
138 expect(decodedStruct.f8).toEqual(mapFieldsStruct.f8); | |
139 expect(decodedStruct.f9).toEqual(mapFieldsStruct.f9); | |
140 expect(decodedStruct.f10).toEqual(mapFieldsStruct.f10); | |
141 expect(decodedStruct.f11).toEqual(mapFieldsStruct.f11); | |
142 } | |
143 | |
144 function testMapValueTypes() { | |
145 var mapFieldsStruct = new testStructs.MapValueTypes({ | |
146 // array<string>> | |
147 f0: new Map([["a", ["b", "c"]], ["d", ["e"]]]), | |
148 // array<string>?> | |
149 f1: new Map([["a", null], ["b", ["c", "d"]]]), | |
150 // array<string?>> | |
151 f2: new Map([["a", [null]], ["b", [null, "d"]]]), | |
152 // array<string,2>> | |
153 f3: new Map([["a", ["1", "2"]], ["b", ["1", "2"]]]), | |
154 // array<array<string, 1>?> | |
155 f4: new Map([["a", [["1"]]], ["b", [null]]]), | |
156 // array<array<string, 2>, 1>> | |
157 f5: new Map([["a", [["1", "2"]]]]), | |
158 // map<string, Rect?> | |
159 f6: new Map([["a", null]]), | |
160 // map<string, map<string, string>> | |
161 f7: new Map([["a", new Map([["b", "c"]])]]), | |
162 // map<string, array<map<string, string>>> | |
163 f8: new Map([["a", [new Map([["b", "c"]])]]]), | |
164 }); | |
165 var decodedStruct = structEncodeDecode(mapFieldsStruct); | |
166 expect(decodedStruct.f0).toEqual(mapFieldsStruct.f0); | |
167 expect(decodedStruct.f1).toEqual(mapFieldsStruct.f1); | |
168 expect(decodedStruct.f2).toEqual(mapFieldsStruct.f2); | |
169 expect(decodedStruct.f3).toEqual(mapFieldsStruct.f3); | |
170 expect(decodedStruct.f4).toEqual(mapFieldsStruct.f4); | |
171 expect(decodedStruct.f5).toEqual(mapFieldsStruct.f5); | |
172 expect(decodedStruct.f6).toEqual(mapFieldsStruct.f6); | |
173 expect(decodedStruct.f7).toEqual(mapFieldsStruct.f7); | |
174 expect(decodedStruct.f8).toEqual(mapFieldsStruct.f8); | |
175 } | |
176 | |
177 function testFloatNumberValues() { | |
178 var decodedStruct = structEncodeDecode(new testStructs.FloatNumberValues); | |
179 expect(decodedStruct.f0).toEqual(testStructs.FloatNumberValues.V0); | |
180 expect(decodedStruct.f1).toEqual(testStructs.FloatNumberValues.V1); | |
181 expect(decodedStruct.f2).toEqual(testStructs.FloatNumberValues.V2); | |
182 expect(decodedStruct.f3).toEqual(testStructs.FloatNumberValues.V3); | |
183 expect(decodedStruct.f4).toEqual(testStructs.FloatNumberValues.V4); | |
184 expect(decodedStruct.f5).toEqual(testStructs.FloatNumberValues.V5); | |
185 expect(decodedStruct.f6).toEqual(testStructs.FloatNumberValues.V6); | |
186 expect(decodedStruct.f7).toEqual(testStructs.FloatNumberValues.V7); | |
187 expect(decodedStruct.f8).toEqual(testStructs.FloatNumberValues.V8); | |
188 expect(decodedStruct.f9).toEqual(testStructs.FloatNumberValues.V9); | |
189 } | |
190 | |
191 function testIntegerNumberValues() { | |
192 var decodedStruct = structEncodeDecode(new testStructs.IntegerNumberValues); | |
193 expect(decodedStruct.f0).toEqual(testStructs.IntegerNumberValues.V0); | |
194 expect(decodedStruct.f1).toEqual(testStructs.IntegerNumberValues.V1); | |
195 expect(decodedStruct.f2).toEqual(testStructs.IntegerNumberValues.V2); | |
196 expect(decodedStruct.f3).toEqual(testStructs.IntegerNumberValues.V3); | |
197 expect(decodedStruct.f4).toEqual(testStructs.IntegerNumberValues.V4); | |
198 expect(decodedStruct.f5).toEqual(testStructs.IntegerNumberValues.V5); | |
199 expect(decodedStruct.f6).toEqual(testStructs.IntegerNumberValues.V6); | |
200 expect(decodedStruct.f7).toEqual(testStructs.IntegerNumberValues.V7); | |
201 expect(decodedStruct.f8).toEqual(testStructs.IntegerNumberValues.V8); | |
202 expect(decodedStruct.f9).toEqual(testStructs.IntegerNumberValues.V9); | |
203 expect(decodedStruct.f10).toEqual(testStructs.IntegerNumberValues.V10); | |
204 expect(decodedStruct.f11).toEqual(testStructs.IntegerNumberValues.V11); | |
205 expect(decodedStruct.f12).toEqual(testStructs.IntegerNumberValues.V12); | |
206 expect(decodedStruct.f13).toEqual(testStructs.IntegerNumberValues.V13); | |
207 expect(decodedStruct.f14).toEqual(testStructs.IntegerNumberValues.V14); | |
208 expect(decodedStruct.f15).toEqual(testStructs.IntegerNumberValues.V15); | |
209 expect(decodedStruct.f16).toEqual(testStructs.IntegerNumberValues.V16); | |
210 expect(decodedStruct.f17).toEqual(testStructs.IntegerNumberValues.V17); | |
211 expect(decodedStruct.f18).toEqual(testStructs.IntegerNumberValues.V18); | |
212 expect(decodedStruct.f19).toEqual(testStructs.IntegerNumberValues.V19); | |
213 } | |
214 | |
215 function testUnsignedNumberValues() { | |
216 var decodedStruct = | |
217 structEncodeDecode(new testStructs.UnsignedNumberValues); | |
218 expect(decodedStruct.f0).toEqual(testStructs.UnsignedNumberValues.V0); | |
219 expect(decodedStruct.f1).toEqual(testStructs.UnsignedNumberValues.V1); | |
220 expect(decodedStruct.f2).toEqual(testStructs.UnsignedNumberValues.V2); | |
221 expect(decodedStruct.f3).toEqual(testStructs.UnsignedNumberValues.V3); | |
222 expect(decodedStruct.f4).toEqual(testStructs.UnsignedNumberValues.V4); | |
223 expect(decodedStruct.f5).toEqual(testStructs.UnsignedNumberValues.V5); | |
224 expect(decodedStruct.f6).toEqual(testStructs.UnsignedNumberValues.V6); | |
225 expect(decodedStruct.f7).toEqual(testStructs.UnsignedNumberValues.V7); | |
226 expect(decodedStruct.f8).toEqual(testStructs.UnsignedNumberValues.V8); | |
227 expect(decodedStruct.f9).toEqual(testStructs.UnsignedNumberValues.V9); | |
228 expect(decodedStruct.f10).toEqual(testStructs.UnsignedNumberValues.V10); | |
229 expect(decodedStruct.f11).toEqual(testStructs.UnsignedNumberValues.V11); | |
230 } | |
231 | |
232 | |
233 function testBitArrayValues() { | |
234 var bitArraysStruct = new testStructs.BitArrayValues({ | |
235 // array<bool, 1> f0; | |
236 f0: [true], | |
237 // array<bool, 7> f1; | |
238 f1: [true, false, true, false, true, false, true], | |
239 // array<bool, 9> f2; | |
240 f2: [true, false, true, false, true, false, true, false, true], | |
241 // array<bool> f3; | |
242 f3: [true, false, true, false, true, false, true, false], | |
243 // array<array<bool>> f4; | |
244 f4: [[true], [false], [true, false], [true, false, true, false]], | |
245 // array<array<bool>?> f5; | |
246 f5: [[true], null, null, [true, false, true, false]], | |
247 // array<array<bool, 2>?> f6; | |
248 f6: [[true, false], [true, false], [true, false]], | |
249 }); | |
250 var decodedStruct = structEncodeDecode(bitArraysStruct); | |
251 expect(decodedStruct.f0).toEqual(bitArraysStruct.f0); | |
252 expect(decodedStruct.f1).toEqual(bitArraysStruct.f1); | |
253 expect(decodedStruct.f2).toEqual(bitArraysStruct.f2); | |
254 expect(decodedStruct.f3).toEqual(bitArraysStruct.f3); | |
255 expect(decodedStruct.f4).toEqual(bitArraysStruct.f4); | |
256 expect(decodedStruct.f5).toEqual(bitArraysStruct.f5); | |
257 expect(decodedStruct.f6).toEqual(bitArraysStruct.f6); | |
258 } | |
259 | |
260 testConstructors(); | |
261 testNoDefaultFieldValues(); | |
262 testDefaultFieldValues(); | |
263 testScopedConstants(); | |
264 testMapKeyTypes(); | |
265 testMapValueTypes(); | |
266 testFloatNumberValues(); | |
267 testIntegerNumberValues(); | |
268 testUnsignedNumberValues(); | |
269 testBitArrayValues(); | |
270 this.result = "PASS"; | |
271 }); | |
OLD | NEW |