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

Side by Side Diff: mojo/public/dart/bindings/test/codec_test.dart

Issue 674383002: Initial work on Dart bindings for Mojo. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Merge. Work on templates. Created 6 years, 1 month 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/dart/bindings/lib/src/interface.dart ('k') | mojo/public/dart/system/lib/core.dart » ('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 import 'dart:typed_data';
6
7 import 'package:mojo/public/dart/utils/lib/expect.dart';
8 import 'package:mojo/public/dart/bindings/lib/bindings.dart' as bindings;
9
10 //import '../../tests/expect.dart';
11 //import '../lib/bindings.dart' as bindings;
12
13 class TestBar implements bindings.MojoType<TestBar> {
14 static const int TYPE_VERTICAL = 1;
15 static const int TYPE_HORIZONTAL = TYPE_VERTICAL + 1;
16 static const int TYPE_BOTH = TYPE_VERTICAL + 1;
17 static const int TYPE_INVALID = TYPE_BOTH + 1;
18
19 int alpha = 0xff;
20 int beta = 0;
21 int gamma = 0;
22 int type = TYPE_VERTICAL;
23
24 TestBar();
25
26 static const int encodedSize = bindings.kStructHeaderSize + 8;
27
28 static TestBar decode(bindings.MojoDecoder decoder) {
29 var val = new TestBar();
30 var num_bytes = decoder.readUint32();
31 var num_fields = decoder.readUint32();
32 val.alpha = decoder.decodeStruct(bindings.Uint8);
33 val.beta = decoder.decodeStruct(bindings.Uint8);
34 val.gamma = decoder.decodeStruct(bindings.Uint8);
35 decoder.skip(1);
36 val.type = decoder.decodeStruct(bindings.Int32);
37 return val;
38 }
39
40 static void encode(bindings.MojoEncoder encoder, TestBar val) {
41 encoder.writeUint32(encodedSize);
42 encoder.writeUint32(4);
43 encoder.encodeStruct(bindings.Uint8, val.alpha);
44 encoder.encodeStruct(bindings.Uint8, val.beta);
45 encoder.encodeStruct(bindings.Uint8, val.gamma);
46 encoder.skip(1);
47 encoder.encodeStruct(bindings.Int32, val.type);
48 }
49
50 String toString() {
51 return "alpha=$alpha, beta=$beta, gamma=$gamma, type=$type";
52 }
53 }
54
55
56 void testBar() {
57 var bar = new TestBar();
58 bar.alpha = 1;
59 bar.beta = 2;
60 bar.gamma = 3;
61 bar.type = 0x08070605;
62
63 int name = 42;
64 int payload_size = TestBar.encodedSize;
65
66 var builder = new bindings.MessageBuilder(name, payload_size);
67 builder.encodeStruct(TestBar, bar);
68 var message = builder.finish();
69
70 var expectedMemory = new Uint8List.fromList([
71 16, 0, 0, 0,
72 2, 0, 0, 0,
73 42, 0, 0, 0,
74 0, 0, 0, 0,
75
76 16, 0, 0, 0,
77 4, 0, 0, 0,
78
79 1, 2, 3, 0,
80 5, 6, 7, 8,
81 ]);
82
83 var actualMemory = message.buffer.buffer.asUint8List();
84 Expect.listEquals(expectedMemory, actualMemory);
85
86 var reader = new bindings.MessageReader(message);
87
88 Expect.equals(payload_size, reader.payload_size);
89 Expect.equals(name, reader.name);
90
91 var bar2 = reader.decodeStruct(TestBar);
92
93 Expect.equals(bar.alpha, bar2.alpha);
94 Expect.equals(bar.beta, bar2.beta);
95 Expect.equals(bar.gamma, bar2.gamma);
96 Expect.equals(bar.type, bar2.type);
97 }
98
99
100 class TestFoo implements bindings.MojoType<TestFoo> {
101 static const String kFooby = "Fooby";
102
103 int x = 0;
104 int y = 0;
105 bool a = true;
106 bool b = false;
107 bool c = false;
108 int source = null;
109 TestBar bar = null;
110 List<int> data = null;
111 List<TestBar> extra_bars = null;
112 String name = kFooby;
113 List<int> input_streams = null;
114 List<int> output_streams = null;
115 List<List<bool>> array_of_array_of_bools = null;
116 List<List<List<String>>> multi_array_of_strings = null;
117 List<bool> array_of_bools = null;
118
119 TestFoo();
120
121 static const int encodedSize = bindings.kStructHeaderSize + 88;
122
123 static TestFoo decode(bindings.MojoDecoder decoder) {
124 int packed;
125 var val = new TestFoo();
126 var num_bytes = decoder.readUint32();
127 var num_fields = decoder.readUint32();
128 val.x = decoder.decodeStruct(bindings.Int32);
129 val.y = decoder.decodeStruct(bindings.Int32);
130 packed = decoder.readUint8();
131 val.a = ((packed >> 0) & 1) != 0 ? true : false;
132 val.b = ((packed >> 1) & 1) != 0 ? true : false;
133 val.c = ((packed >> 2) & 1) != 0 ? true : false;
134 decoder.skip(1);
135 decoder.skip(1);
136 decoder.skip(1);
137 val.source = decoder.decodeStruct(bindings.NullableHandle);
138 val.bar = decoder.decodeStructPointer(TestBar);
139 val.data = decoder.decodeArrayPointer(bindings.Uint8);
140 val.extra_bars =
141 decoder.decodeArrayPointer(new bindings.PointerTo(TestBar));
142 val.name = decoder.decodeStruct(bindings.MojoString);
143 val.input_streams = decoder.decodeArrayPointer(bindings.Handle);
144 val.output_streams = decoder.decodeArrayPointer(bindings.Handle);
145 val.array_of_array_of_bools =
146 decoder.decodeArrayPointer(new bindings.ArrayOf(bindings.PackedBool));
147 val.multi_array_of_strings = decoder.decodeArrayPointer(
148 new bindings.ArrayOf(new bindings.ArrayOf(bindings.MojoString)));
149 val.array_of_bools = decoder.decodeArrayPointer(bindings.PackedBool);
150 return val;
151 }
152
153 static void encode(bindings.MojoEncoder encoder, TestFoo val) {
154 int packed = 0;
155 encoder.writeUint32(encodedSize);
156 encoder.writeUint32(15);
157 encoder.encodeStruct(bindings.Int32, val.x);
158 encoder.encodeStruct(bindings.Int32, val.y);
159 packed |= (val.a ? 1 : 0) << 0;
160 packed |= (val.b ? 1 : 0) << 1;
161 packed |= (val.c ? 1 : 0) << 2;
162 encoder.writeUint8(packed);
163 encoder.skip(1);
164 encoder.skip(1);
165 encoder.skip(1);
166 encoder.encodeStruct(bindings.NullableHandle, val.source);
167 encoder.encodeStructPointer(TestBar, val.bar);
168 encoder.encodeArrayPointer(bindings.Uint8, val.data);
169 encoder.encodeArrayPointer(new bindings.PointerTo(TestBar), val.extra_bars);
170 encoder.encodeStruct(bindings.MojoString, val.name);
171 encoder.encodeArrayPointer(bindings.Handle, val.input_streams);
172 encoder.encodeArrayPointer(bindings.Handle, val.output_streams);
173 encoder.encodeArrayPointer(
174 new bindings.ArrayOf(bindings.PackedBool), val.array_of_array_of_bools);
175 encoder.encodeArrayPointer(
176 new bindings.ArrayOf(new bindings.ArrayOf(bindings.MojoString)),
177 val.multi_array_of_strings);
178 encoder.encodeArrayPointer(bindings.PackedBool, val.array_of_bools);
179 }
180 }
181
182
183 void testFoo() {
184 var foo = new TestFoo();
185 foo.x = 0x212B4D5;
186 foo.y = 0x16E93;
187 foo.a = true;
188 foo.b = false;
189 foo.c = true;
190 foo.bar = new TestBar();
191 foo.bar.alpha = 91;
192 foo.bar.beta = 82;
193 foo.bar.gamma = 73;
194 foo.data = [
195 4, 5, 6, 7, 8,
196 ];
197 foo.extra_bars = [
198 new TestBar(), new TestBar(), new TestBar(),
199 ];
200 for (int i = 0; i < foo.extra_bars.length; ++i) {
201 foo.extra_bars[i].alpha = 1 * i;
202 foo.extra_bars[i].beta = 2 * i;
203 foo.extra_bars[i].gamma = 3 * i;
204 }
205 foo.name = "I am a banana";
206 // This is supposed to be a handle, but we fake it with an integer.
207 foo.source = 23423782;
208 foo.array_of_array_of_bools = [
209 [true], [false, true]
210 ];
211 foo.array_of_bools = [
212 true, false, true, false, true, false, true, true
213 ];
214
215
216 var name = 31;
217 var payload_size = 304;
218
219 var builder = new bindings.MessageBuilder(name, payload_size);
220 builder.encodeStruct(TestFoo, foo);
221
222 var message = builder.finish();
223
224 var expectedMemory = new Uint8List.fromList([
225 /* 0: */ 16, 0, 0, 0, 2, 0, 0, 0,
226 /* 8: */ 31, 0, 0, 0, 0, 0, 0, 0,
227 /* 16: */ 96, 0, 0, 0, 15, 0, 0, 0,
228 /* 24: */ 0xD5, 0xB4, 0x12, 0x02, 0x93, 0x6E, 0x01, 0,
229 /* 32: */ 5, 0, 0, 0, 0, 0, 0, 0,
230 /* 40: */ 72, 0, 0, 0, 0, 0, 0, 0,
231 ]);
232 // TODO(abarth): Test more of the message's raw memory.
233 var allActualMemory = message.buffer.buffer.asUint8List();
234 var actualMemory = allActualMemory.sublist(0, expectedMemory.length);
235 Expect.listEquals(expectedMemory, actualMemory);
236
237 var expectedHandles = [
238 23423782,
239 ];
240
241 Expect.listEquals(expectedHandles, message.handles);
242
243 var reader = new bindings.MessageReader(message);
244
245 Expect.equals(payload_size, reader.payload_size);
246 Expect.equals(name, reader.name);
247
248 var foo2 = reader.decodeStruct(TestFoo);
249
250 Expect.equals(foo.x, foo2.x);
251 Expect.equals(foo.y, foo2.y);
252
253 Expect.equals(foo.a, foo2.a);
254 Expect.equals(foo.b, foo2.b);
255 Expect.equals(foo.c, foo2.c);
256
257 Expect.equals(foo.bar.alpha, foo2.bar.alpha);
258 Expect.equals(foo.bar.beta, foo2.bar.beta);
259 Expect.equals(foo.bar.gamma, foo2.bar.gamma);
260 Expect.equals(foo.bar.type, foo2.bar.type);
261 Expect.listEquals(foo.data, foo2.data);
262
263 for (int i = 0; i < foo2.extra_bars.length; i++) {
264 Expect.equals(foo.extra_bars[i].alpha, foo2.extra_bars[i].alpha);
265 Expect.equals(foo.extra_bars[i].beta, foo2.extra_bars[i].beta);
266 Expect.equals(foo.extra_bars[i].gamma, foo2.extra_bars[i].gamma);
267 Expect.equals(foo.extra_bars[i].type, foo2.extra_bars[i].type);
268 }
269
270 Expect.equals(foo.name, foo2.name);
271 Expect.equals(foo.source, foo2.source);
272
273 Expect.listEquals(foo.array_of_bools, foo2.array_of_bools);
274 for (int i = 0; i < foo2.array_of_array_of_bools.length; i++) {
275 Expect.listEquals(foo.array_of_array_of_bools[i],
276 foo2.array_of_array_of_bools[i]);
277 }
278 }
279
280
281 class TestRect implements bindings.MojoType<TestRect> {
282 int x = 0;
283 int y = 0;
284 int width = 0;
285 int height = 0;
286
287 TestRect();
288
289 static const int encodedSize = bindings.kStructHeaderSize + 16;
290
291 static TestRect decode(bindings.MojoDecoder decoder) {
292 var val = new TestRect();
293 var num_bytes = decoder.readUint32();
294 var num_fields = decoder.readUint32();
295 val.x = decoder.decodeStruct(bindings.Int32);
296 val.y = decoder.decodeStruct(bindings.Int32);
297 val.width = decoder.decodeStruct(bindings.Int32);
298 val.height = decoder.decodeStruct(bindings.Int32);
299 return val;
300 }
301
302 static void encode(bindings.MojoEncoder encoder, TestRect val) {
303 encoder.writeUint32(encodedSize);
304 encoder.writeUint32(4);
305 encoder.encodeStruct(bindings.Int32, val.x);
306 encoder.encodeStruct(bindings.Int32, val.y);
307 encoder.encodeStruct(bindings.Int32, val.width);
308 encoder.encodeStruct(bindings.Int32, val.height);
309 }
310
311 bool operator ==(TestRect other) {
312 return (x == other.x) && (y == other.y) && (width == other.width) &&
313 (height == other.height);
314 }
315 }
316
317
318 TestRect createRect(int x, int y, int width, int height) {
319 var r = new TestRect();
320 r.x = x;
321 r.y = y;
322 r.width = width;
323 r.height = height;
324 return r;
325 }
326
327
328 class TestNamedRegion implements bindings.MojoType<TestNamedRegion> {
329 String name = null;
330 List<TestRect> rects = null;
331
332 TestNamedRegion();
333
334 static const int encodedSize = bindings.kStructHeaderSize + 16;
335
336 static TestNamedRegion decode(bindings.MojoDecoder decoder) {
337 var val = new TestNamedRegion();
338 var num_bytes = decoder.readUint32();
339 var num_fields = decoder.readUint32();
340 val.name = decoder.decodeStruct(bindings.NullableMojoString);
341 val.rects = decoder.decodeArrayPointer(new bindings.PointerTo(TestRect));
342 return val;
343 }
344
345 static void encode(bindings.MojoEncoder encoder, TestNamedRegion val) {
346 encoder.writeUint32(TestNamedRegion.encodedSize);
347 encoder.writeUint32(2);
348 encoder.encodeStruct(bindings.NullableMojoString, val.name);
349 encoder.encodeArrayPointer(new bindings.PointerTo(TestRect), val.rects);
350 }
351 }
352
353
354 testNamedRegion() {
355 var r = new TestNamedRegion();
356 r.name = "rectangle";
357 r.rects = [createRect(1, 2, 3, 4), createRect(10, 20, 30, 40)];
358
359 var builder = new bindings.MessageBuilder(1, TestNamedRegion.encodedSize);
360 builder.encodeStruct(TestNamedRegion, r);
361 var reader = new bindings.MessageReader(builder.finish());
362 var result = reader.decodeStruct(TestNamedRegion);
363
364 Expect.equals("rectangle", result.name);
365 Expect.equals(createRect(1, 2, 3, 4), result.rects[0]);
366 Expect.equals(createRect(10, 20, 30, 40), result.rects[1]);
367 }
368
369
370 void testAlign() {
371 List aligned = [
372 0, // 0
373 8, // 1
374 8, // 2
375 8, // 3
376 8, // 4
377 8, // 5
378 8, // 6
379 8, // 7
380 8, // 8
381 16, // 9
382 16, // 10
383 16, // 11
384 16, // 12
385 16, // 13
386 16, // 14
387 16, // 15
388 16, // 16
389 24, // 17
390 24, // 18
391 24, // 19
392 24, // 20
393 ];
394 for (int i = 0; i < aligned.length; ++i) {
395 Expect.equals(bindings.align(i), aligned[i]);
396 }
397 }
398
399 void encodeDecode(Object t, Object input, Object expected, [int encoded_size]) {
400 int name = 42;
401 int payload_size =
402 (encoded_size != null) ? encoded_size : bindings.getEncodedSize(t);
403
404 var builder = new bindings.MessageBuilder(name, payload_size);
405 builder.encodeStruct(t, input);
406 var message = builder.finish();
407
408 var reader = new bindings.MessageReader(message);
409 Expect.equals(payload_size, reader.payload_size);
410 Expect.equals(name, reader.name);
411
412 var result = reader.decodeStruct(t);
413 Expect.equals(expected, result);
414 }
415
416
417 void testTypes() {
418 encodeDecode(bindings.MojoString, "banana", "banana", 24);
419 encodeDecode(bindings.NullableMojoString, null, null, 8);
420 encodeDecode(bindings.Int8, -1, -1);
421 encodeDecode(bindings.Int8, 0xff, -1);
422 encodeDecode(bindings.Int16, -1, -1);
423 encodeDecode(bindings.Int16, 0xff, 0xff);
424 encodeDecode(bindings.Int16, 0xffff, -1);
425 encodeDecode(bindings.Int32, -1, -1);
426 encodeDecode(bindings.Int32, 0xffff, 0xffff);
427 encodeDecode(bindings.Int32, 0xffffffff, -1);
428 encodeDecode(bindings.Float, 1.0, 1.0);
429 encodeDecode(bindings.Double, 1.0, 1.0);
430 }
431
432
433 testUtf8() {
434 var str = "B\u03ba\u1f79"; // some UCS-2 codepoints
435 var name = 42;
436 var payload_size = 24;
437
438 var builder = new bindings.MessageBuilder(name, payload_size);
439 var encoder = builder.createEncoder(8);
440 encoder.encodeStringPointer(str);
441 var message = builder.finish();
442 var expectedMemory = new Uint8List.fromList([
443 /* 0: */ 16, 0, 0, 0, 2, 0, 0, 0,
444 /* 8: */ 42, 0, 0, 0, 0, 0, 0, 0,
445 /* 16: */ 8, 0, 0, 0, 0, 0, 0, 0,
446 /* 24: */ 14, 0, 0, 0, 6, 0, 0, 0,
447 /* 32: */ 0x42, 0xCE, 0xBA, 0xE1, 0xBD, 0xB9, 0, 0,
448 ]);
449 var actualMemory = message.buffer.buffer.asUint8List();
450 Expect.equals(expectedMemory.length, actualMemory.length);
451 Expect.listEquals(expectedMemory, actualMemory);
452
453 var reader = new bindings.MessageReader(message);
454 Expect.equals(payload_size, reader.payload_size);
455 Expect.equals(name, reader.name);
456
457 var str2 = reader.decoder.decodeStringPointer();
458 Expect.equals(str, str2);
459 }
460
461
462 main() {
463 testAlign();
464 testTypes();
465 testBar();
466 testFoo();
467 testNamedRegion();
468 testUtf8();
469 }
OLDNEW
« no previous file with comments | « mojo/public/dart/bindings/lib/src/interface.dart ('k') | mojo/public/dart/system/lib/core.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698