Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(981)

Side by Side Diff: mojo/public/js/struct_unittests.js

Issue 2250183003: Make the fuchsia mojo/public repo the source of truth. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/public/js/router.js ('k') | mojo/public/js/support.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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
83 expect(testStructs.ScopedConstants.EType.E0).toEqual(0);
84 expect(testStructs.ScopedConstants.EType.E1).toEqual(1);
85 expect(testStructs.ScopedConstants.EType.E2).toEqual(10);
86 expect(testStructs.ScopedConstants.EType.E3).toEqual(10);
87 expect(testStructs.ScopedConstants.EType.E4).toEqual(11);
88
89 var s = new testStructs.ScopedConstants();
90 expect(s.f0).toEqual(0);
91 expect(s.f1).toEqual(1);
92 expect(s.f2).toEqual(10);
93 expect(s.f3).toEqual(10);
94 expect(s.f4).toEqual(11);
95 expect(s.f5).toEqual(10);
96 expect(s.f6).toEqual(10);
97 }
98
99 function structEncodeDecode(struct) {
100 var structClass = struct.constructor;
101 var builder = new codec.MessageBuilder(1234, structClass.encodedSize);
102 builder.encodeStruct(structClass, struct);
103 var message = builder.finish();
104
105 var messageValidator = new validator.Validator(message);
106 var err = structClass.validate(messageValidator, codec.kMessageHeaderSize);
107 expect(err).toEqual(validator.validationError.NONE);
108
109 var reader = new codec.MessageReader(message);
110 return reader.decodeStruct(structClass);
111 }
112
113 function testMapKeyTypes() {
114 var mapFieldsStruct = new testStructs.MapKeyTypes({
115 f0: new Map([[true, false], [false, true]]), // map<bool, bool>
116 f1: new Map([[0, 0], [1, 127], [-1, -128]]), // map<int8, int8>
117 f2: new Map([[0, 0], [1, 127], [2, 255]]), // map<uint8, uint8>
118 f3: new Map([[0, 0], [1, 32767], [2, -32768]]), // map<int16, int16>
119 f4: new Map([[0, 0], [1, 32768], [2, 0xFFFF]]), // map<uint16, uint16>
120 f5: new Map([[0, 0], [1, 32767], [2, -32768]]), // map<int32, int32>
121 f6: new Map([[0, 0], [1, 32768], [2, 0xFFFF]]), // map<uint32, uint32>
122 f7: new Map([[0, 0], [1, 32767], [2, -32768]]), // map<int64, int64>
123 f8: new Map([[0, 0], [1, 32768], [2, 0xFFFF]]), // map<uint64, uint64>
124 f9: new Map([[1000.5, -50000], [100.5, 5000]]), // map<float, float>
125 f10: new Map([[-100.5, -50000], [0, 50000000]]), // map<double, double>
126 f11: new Map([["one", "two"], ["free", "four"]]), // map<string, string>
127 });
128 var decodedStruct = structEncodeDecode(mapFieldsStruct);
129 expect(decodedStruct.f0).toEqual(mapFieldsStruct.f0);
130 expect(decodedStruct.f1).toEqual(mapFieldsStruct.f1);
131 expect(decodedStruct.f2).toEqual(mapFieldsStruct.f2);
132 expect(decodedStruct.f3).toEqual(mapFieldsStruct.f3);
133 expect(decodedStruct.f4).toEqual(mapFieldsStruct.f4);
134 expect(decodedStruct.f5).toEqual(mapFieldsStruct.f5);
135 expect(decodedStruct.f6).toEqual(mapFieldsStruct.f6);
136 expect(decodedStruct.f7).toEqual(mapFieldsStruct.f7);
137 expect(decodedStruct.f8).toEqual(mapFieldsStruct.f8);
138 expect(decodedStruct.f9).toEqual(mapFieldsStruct.f9);
139 expect(decodedStruct.f10).toEqual(mapFieldsStruct.f10);
140 expect(decodedStruct.f11).toEqual(mapFieldsStruct.f11);
141 }
142
143 function testMapValueTypes() {
144 var mapFieldsStruct = new testStructs.MapValueTypes({
145 // map<string, array<string>>
146 f0: new Map([["a", ["b", "c"]], ["d", ["e"]]]),
147 // map<string, array<string>?>
148 f1: new Map([["a", null], ["b", ["c", "d"]]]),
149 // map<string, array<string?>>
150 f2: new Map([["a", [null]], ["b", [null, "d"]]]),
151 // map<string, array<string,2>>
152 f3: new Map([["a", ["1", "2"]], ["b", ["1", "2"]]]),
153 // map<string, array<array<string, 2>?>>
154 f4: new Map([["a", [["1", "2"]]], ["b", [null]]]),
155 // map<string, array<array<string, 2>, 1>>
156 f5: new Map([["a", [["1", "2"]]]]),
157 // map<string, Rect?>
158 f6: new Map([["a", null]]),
159 // map<string, map<string, string>>
160 f7: new Map([["a", new Map([["b", "c"]])]]),
161 // map<string, array<map<string, string>>>
162 f8: new Map([["a", [new Map([["b", "c"]])]]]),
163 // map<string, handle>
164 f9: new Map([["a", 1234]]),
165 // map<string, array<handle>>
166 f10: new Map([["a", [1234, 5678]]]),
167 // map<string, map<string, handle>>
168 f11: new Map([["a", new Map([["b", 1234]])]]),
169 });
170 var decodedStruct = structEncodeDecode(mapFieldsStruct);
171 expect(decodedStruct.f0).toEqual(mapFieldsStruct.f0);
172 expect(decodedStruct.f1).toEqual(mapFieldsStruct.f1);
173 expect(decodedStruct.f2).toEqual(mapFieldsStruct.f2);
174 expect(decodedStruct.f3).toEqual(mapFieldsStruct.f3);
175 expect(decodedStruct.f4).toEqual(mapFieldsStruct.f4);
176 expect(decodedStruct.f5).toEqual(mapFieldsStruct.f5);
177 expect(decodedStruct.f6).toEqual(mapFieldsStruct.f6);
178 expect(decodedStruct.f7).toEqual(mapFieldsStruct.f7);
179 expect(decodedStruct.f8).toEqual(mapFieldsStruct.f8);
180 expect(decodedStruct.f9).toEqual(mapFieldsStruct.f9);
181 expect(decodedStruct.f10).toEqual(mapFieldsStruct.f10);
182 expect(decodedStruct.f11).toEqual(mapFieldsStruct.f11);
183 }
184
185 function testFloatNumberValues() {
186 var decodedStruct = structEncodeDecode(new testStructs.FloatNumberValues);
187 expect(decodedStruct.f0).toEqual(testStructs.FloatNumberValues.V0);
188 expect(decodedStruct.f1).toEqual(testStructs.FloatNumberValues.V1);
189 expect(decodedStruct.f2).toEqual(testStructs.FloatNumberValues.V2);
190 expect(decodedStruct.f3).toEqual(testStructs.FloatNumberValues.V3);
191 expect(decodedStruct.f4).toEqual(testStructs.FloatNumberValues.V4);
192 expect(decodedStruct.f5).toEqual(testStructs.FloatNumberValues.V5);
193 expect(decodedStruct.f6).toEqual(testStructs.FloatNumberValues.V6);
194 expect(decodedStruct.f7).toEqual(testStructs.FloatNumberValues.V7);
195 expect(decodedStruct.f8).toEqual(testStructs.FloatNumberValues.V8);
196 expect(decodedStruct.f9).toEqual(testStructs.FloatNumberValues.V9);
197 }
198
199 function testIntegerNumberValues() {
200 var decodedStruct = structEncodeDecode(new testStructs.IntegerNumberValues);
201 expect(decodedStruct.f0).toEqual(testStructs.IntegerNumberValues.V0);
202 expect(decodedStruct.f1).toEqual(testStructs.IntegerNumberValues.V1);
203 expect(decodedStruct.f2).toEqual(testStructs.IntegerNumberValues.V2);
204 expect(decodedStruct.f3).toEqual(testStructs.IntegerNumberValues.V3);
205 expect(decodedStruct.f4).toEqual(testStructs.IntegerNumberValues.V4);
206 expect(decodedStruct.f5).toEqual(testStructs.IntegerNumberValues.V5);
207 expect(decodedStruct.f6).toEqual(testStructs.IntegerNumberValues.V6);
208 expect(decodedStruct.f7).toEqual(testStructs.IntegerNumberValues.V7);
209 expect(decodedStruct.f8).toEqual(testStructs.IntegerNumberValues.V8);
210 expect(decodedStruct.f9).toEqual(testStructs.IntegerNumberValues.V9);
211 expect(decodedStruct.f10).toEqual(testStructs.IntegerNumberValues.V10);
212 expect(decodedStruct.f11).toEqual(testStructs.IntegerNumberValues.V11);
213 expect(decodedStruct.f12).toEqual(testStructs.IntegerNumberValues.V12);
214 expect(decodedStruct.f13).toEqual(testStructs.IntegerNumberValues.V13);
215 expect(decodedStruct.f14).toEqual(testStructs.IntegerNumberValues.V14);
216 expect(decodedStruct.f15).toEqual(testStructs.IntegerNumberValues.V15);
217 expect(decodedStruct.f16).toEqual(testStructs.IntegerNumberValues.V16);
218 expect(decodedStruct.f17).toEqual(testStructs.IntegerNumberValues.V17);
219 expect(decodedStruct.f18).toEqual(testStructs.IntegerNumberValues.V18);
220 expect(decodedStruct.f19).toEqual(testStructs.IntegerNumberValues.V19);
221 }
222
223 function testUnsignedNumberValues() {
224 var decodedStruct =
225 structEncodeDecode(new testStructs.UnsignedNumberValues);
226 expect(decodedStruct.f0).toEqual(testStructs.UnsignedNumberValues.V0);
227 expect(decodedStruct.f1).toEqual(testStructs.UnsignedNumberValues.V1);
228 expect(decodedStruct.f2).toEqual(testStructs.UnsignedNumberValues.V2);
229 expect(decodedStruct.f3).toEqual(testStructs.UnsignedNumberValues.V3);
230 expect(decodedStruct.f4).toEqual(testStructs.UnsignedNumberValues.V4);
231 expect(decodedStruct.f5).toEqual(testStructs.UnsignedNumberValues.V5);
232 expect(decodedStruct.f6).toEqual(testStructs.UnsignedNumberValues.V6);
233 expect(decodedStruct.f7).toEqual(testStructs.UnsignedNumberValues.V7);
234 expect(decodedStruct.f8).toEqual(testStructs.UnsignedNumberValues.V8);
235 expect(decodedStruct.f9).toEqual(testStructs.UnsignedNumberValues.V9);
236 expect(decodedStruct.f10).toEqual(testStructs.UnsignedNumberValues.V10);
237 expect(decodedStruct.f11).toEqual(testStructs.UnsignedNumberValues.V11);
238 }
239
240
241 function testBitArrayValues() {
242 var bitArraysStruct = new testStructs.BitArrayValues({
243 // array<bool, 1> f0;
244 f0: [true],
245 // array<bool, 7> f1;
246 f1: [true, false, true, false, true, false, true],
247 // array<bool, 9> f2;
248 f2: [true, false, true, false, true, false, true, false, true],
249 // array<bool> f3;
250 f3: [true, false, true, false, true, false, true, false],
251 // array<array<bool>> f4;
252 f4: [[true], [false], [true, false], [true, false, true, false]],
253 // array<array<bool>?> f5;
254 f5: [[true], null, null, [true, false, true, false]],
255 // array<array<bool, 2>?> f6;
256 f6: [[true, false], [true, false], [true, false]],
257 });
258 var decodedStruct = structEncodeDecode(bitArraysStruct);
259 expect(decodedStruct.f0).toEqual(bitArraysStruct.f0);
260 expect(decodedStruct.f1).toEqual(bitArraysStruct.f1);
261 expect(decodedStruct.f2).toEqual(bitArraysStruct.f2);
262 expect(decodedStruct.f3).toEqual(bitArraysStruct.f3);
263 expect(decodedStruct.f4).toEqual(bitArraysStruct.f4);
264 expect(decodedStruct.f5).toEqual(bitArraysStruct.f5);
265 expect(decodedStruct.f6).toEqual(bitArraysStruct.f6);
266 }
267
268 testConstructors();
269 testNoDefaultFieldValues();
270 testDefaultFieldValues();
271 testScopedConstants();
272 testMapKeyTypes();
273 testMapValueTypes();
274 testFloatNumberValues();
275 testIntegerNumberValues();
276 testUnsignedNumberValues();
277 testBitArrayValues();
278 this.result = "PASS";
279 });
OLDNEW
« no previous file with comments | « mojo/public/js/router.js ('k') | mojo/public/js/support.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698