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

Side by Side Diff: mojo/dart/test/bindings_generation_test.dart

Issue 1433183002: Generate Mojom Types for Dart (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Removed assert that breaks dart unit test Created 5 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 import 'dart:async'; 5 import 'dart:async';
6 import 'dart:isolate'; 6 import 'dart:isolate';
7 import 'dart:typed_data'; 7 import 'dart:typed_data';
8 import 'dart:convert'; 8 import 'dart:convert';
9 9
10 import 'package:_testing/expect.dart'; 10 import 'package:_testing/expect.dart';
11 import 'package:mojo/bindings.dart' as bindings; 11 import 'package:mojo/bindings.dart' as bindings;
12 import 'package:mojo/core.dart' as core; 12 import 'package:mojo/core.dart' as core;
13 import 'package:mojo/mojo/mojom_types.mojom.dart' as mojom_types;
14 import 'package:mojo/mojo/service_describer.mojom.dart' as service_describer;
13 import 'package:mojom/sample/sample_interfaces.mojom.dart' as sample; 15 import 'package:mojom/sample/sample_interfaces.mojom.dart' as sample;
14 import 'package:mojom/mojo/test/test_structs.mojom.dart' as structs; 16 import 'package:mojom/mojo/test/test_structs.mojom.dart' as structs;
15 import 'package:mojom/mojo/test/test_unions.mojom.dart' as unions; 17 import 'package:mojom/mojo/test/test_unions.mojom.dart' as unions;
18 import 'package:mojom/mojo/test/validation_test_interfaces.mojom.dart'
19 as validation;
16 import 'package:mojom/mojo/test/rect.mojom.dart' as rect; 20 import 'package:mojom/mojo/test/rect.mojom.dart' as rect;
17 import 'package:mojom/mojo/test/serialization_test_structs.mojom.dart' 21 import 'package:mojom/mojo/test/serialization_test_structs.mojom.dart'
18 as serialization; 22 as serialization;
19 import 'package:mojom/regression_tests/regression_tests.mojom.dart' 23 import 'package:mojom/regression_tests/regression_tests.mojom.dart'
20 as regression; 24 as regression;
21 25
22 class ProviderImpl implements sample.Provider { 26 class ProviderImpl implements sample.Provider {
23 sample.ProviderStub _stub; 27 sample.ProviderStub _stub;
24 28
25 ProviderImpl(core.MojoMessagePipeEndpoint endpoint) { 29 ProviderImpl(core.MojoMessagePipeEndpoint endpoint) {
26 _stub = new sample.ProviderStub.fromEndpoint(endpoint, this); 30 _stub = new sample.ProviderStub.fromEndpoint(endpoint, this);
27 } 31 }
28 32
29 echoString(String a, Function responseFactory) => 33 echoString(String a, Function responseFactory) =>
30 new Future.value(responseFactory(a)); 34 new Future.value(responseFactory(a));
31 35
32 echoStrings(String a, String b, Function responseFactory) => 36 echoStrings(String a, String b, Function responseFactory) =>
33 new Future.value(responseFactory(a, b)); 37 new Future.value(responseFactory(a, b));
34 38
35 echoMessagePipeHanlde(core.MojoHandle a, Function responseFactory) => 39 echoMessagePipeHandle(core.MojoHandle a, Function responseFactory) =>
36 new Future.value(responseFactory(a)); 40 new Future.value(responseFactory(a));
37 41
38 echoEnum(sample.Enum a, Function responseFactory) => 42 echoEnum(sample.Enum a, Function responseFactory) =>
39 new Future.value(responseFactory(a)); 43 new Future.value(responseFactory(a));
40 } 44 }
41 45
42 void providerIsolate(core.MojoMessagePipeEndpoint endpoint) { 46 void providerIsolate(core.MojoMessagePipeEndpoint endpoint) {
43 new ProviderImpl(endpoint); 47 new ProviderImpl(endpoint);
44 } 48 }
45 49
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
233 testSerializePodUnions(); 237 testSerializePodUnions();
234 testSerializeStructInUnion(); 238 testSerializeStructInUnion();
235 testSerializeArrayInUnion(); 239 testSerializeArrayInUnion();
236 testSerializeMapInUnion(); 240 testSerializeMapInUnion();
237 testSerializeUnionInArray(); 241 testSerializeUnionInArray();
238 testSerializeUnionInMap(); 242 testSerializeUnionInMap();
239 testSerializeUnionInUnion(); 243 testSerializeUnionInUnion();
240 testUnionsToString(); 244 testUnionsToString();
241 } 245 }
242 246
247 testValidateMojomTypes() {
248 testValidateEnumType();
249 testValidateStructType();
250 testValidateUnionType();
251 testValidateTestStructWithImportType();
252 testValidateInterfaceType();
253 }
254
255 // Test that mojom type descriptors were generated correctly for validation's
256 // BasicEnum.
257 testValidateEnumType() {
258 var testValidationDescriptor = validation.getAllMojomTypeDefinitions();
259 String enumID = "_validation_test_interfaces_BasicEnum__";
260 String shortName = "BasicEnum";
261 Map<String, int> labelMap = <String, int>{
262 "A": 0,
263 "B": 1,
264 "C": 0,
265 "D": -3,
266 "E": 0xA,
267 };
268
269 // Extract the UserDefinedType from the descriptor using enumID.
270 mojom_types.UserDefinedType udt = testValidationDescriptor[enumID];
271 Expect.isNotNull(udt);
272
273 // The enumType must be present and declared properly.
274 mojom_types.MojomEnum me = udt.enumType;
275 Expect.isNotNull(me);
276 Expect.isNotNull(me.declData);
277 Expect.equals(me.declData.shortName, shortName);
278
279 // Now compare the labels to verify that the enum labels match the expected
280 // ones.
281 Expect.equals(me.values.length, labelMap.length);
282 me.values.forEach((mojom_types.EnumValue ev) {
283 // Check that the declData is correct...
284 Expect.isNotNull(ev.declData);
285 Expect.isNotNull(labelMap[ev.declData.shortName]);
286
287 // Check that the enumTypeKey matches the enumID.
288 Expect.equals(ev.enumTypeKey, enumID);
289 Expect.equals(ev.intValue, labelMap[ev.declData.shortName]);
290 });
291 }
292
293 // Test that mojom type descriptors were generated correctly for validation's
294 // StructE.
295 testValidateStructType() {
296 var testValidationDescriptor = validation.getAllMojomTypeDefinitions();
297 String structID = "_validation_test_interfaces_StructE__";
298 String shortName = "StructE";
299 Map<int, String> expectedFields = <int, String>{
300 0: "StructD",
301 1: "DataPipeConsumer",
302 };
303
304 // Extract the UserDefinedType from the descriptor using structID.
305 mojom_types.UserDefinedType udt = testValidationDescriptor[structID];
306 Expect.isNotNull(udt);
307
308 // The structType must be present and declared properly.
309 mojom_types.MojomStruct ms = udt.structType;
310 Expect.isNotNull(ms);
311 Expect.isNotNull(ms.declData);
312 Expect.equals(ms.declData.shortName, shortName);
313
314 // Now compare the fields to verify that the struct fields match the expected
315 // ones.
316 Expect.equals(ms.fields.length, expectedFields.length);
317 int i = 0;
318 ms.fields.forEach((mojom_types.StructField field) {
319 // Check that the declData is correct...
320 Expect.isNotNull(field.declData);
321 Expect.equals(expectedFields[i], field.declData.shortName);
322
323 // Special case each field since we already know what should be inside.
324 switch (i) {
325 case 0: // This is a TypeReference to StructD.
326 mojom_types.Type t = field.type;
327 Expect.isNotNull(t.typeReference);
328
329 // Type key, identifier, and expected reference id should match up.
330 mojom_types.TypeReference tr = t.typeReference;
331 String expectedRefID = "_validation_test_interfaces_StructD__";
332 Expect.equals(expectedRefID, tr.identifier);
333 Expect.equals(expectedRefID, tr.typeKey);
334 break;
335 case 1: // This is a non-nullable DataPipeConsumer HandleType.
336 mojom_types.Type t = field.type;
337 Expect.isNotNull(t.handleType);
338
339 mojom_types.HandleType ht = t.handleType;
340 Expect.isFalse(ht.nullable);
341 Expect.equals(mojom_types.HandleTypeKind.DATA_PIPE_CONSUMER, ht.kind);
342 break;
343 default:
344 assert(false);
345 }
346
347 i++;
348 });
349 }
350
351 // Test that mojom type descriptors were generated correctly for validation's
352 // UnionB.
353 testValidateUnionType() {
354 var testValidationDescriptor = validation.getAllMojomTypeDefinitions();
355 String unionID = "_validation_test_interfaces_UnionB__";
356 String shortName = "UnionB";
357 Map<int, String> expectedFields = <int, String>{
358 0: "A",
359 1: "B",
360 2: "C",
361 3: "D",
362 };
363
364 // Extract the UserDefinedType from the descriptor using unionID.
365 mojom_types.UserDefinedType udt = testValidationDescriptor[unionID];
366 Expect.isNotNull(udt);
367
368 // The unionType must be present and declared properly.
369 mojom_types.MojomUnion mu = udt.unionType;
370 Expect.isNotNull(mu);
371 Expect.isNotNull(mu.declData);
372 Expect.equals(mu.declData.shortName, shortName);
373
374 // Now compare the fields to verify that the union fields match the expected
375 // ones.
376 Expect.equals(mu.fields.length, expectedFields.length);
377 mu.fields.forEach((mojom_types.UnionField field) {
378 int ordinal = field.tag;
379
380 // Check that the declData is correct...
381 Expect.isNotNull(field.declData);
382 Expect.equals(expectedFields[ordinal], field.declData.shortName);
383
384 // Special: It turns out that all types are simple types.
385 mojom_types.Type t = field.type;
386 Expect.isNotNull(t.simpleType);
387 mojom_types.SimpleType st = t.simpleType;
388
389 // Special case each field since we already know what should be inside.
390 switch (ordinal) {
391 case 0: // Uint16
392 Expect.equals(st, mojom_types.SimpleType.UINT16);
393 break;
394 case 1: // Uint32
395 case 3:
396 Expect.equals(st, mojom_types.SimpleType.UINT32);
397 break;
398 case 2: // Uint64
399 Expect.equals(st, mojom_types.SimpleType.UINT64);
400 break;
401 default:
402 assert(false);
403 }
404 });
405 }
406
407 // Test that mojom type descriptors were generated correctly for validation's
408 // IncludingStruct, which contains a union imported from test_included_unions.
409 testValidateTestStructWithImportType() {
410 var testUnionsDescriptor = unions.getAllMojomTypeDefinitions();
411 String structID = "_test_unions_IncludingStruct__";
412 String shortName = "IncludingStruct";
413 Map<int, String> expectedFields = <int, String>{0: "A",};
414
415 // Extract the UserDefinedType from the descriptor using structID.
416 mojom_types.UserDefinedType udt = testUnionsDescriptor[structID];
417 Expect.isNotNull(udt);
418
419 // The structType must be present and declared properly.
420 mojom_types.MojomStruct ms = udt.structType;
421 Expect.isNotNull(ms);
422 Expect.isNotNull(ms.declData);
423 Expect.equals(ms.declData.shortName, shortName);
424
425 // Now compare the fields to verify that the struct fields match the expected
426 // ones.
427 Expect.equals(ms.fields.length, expectedFields.length);
428 int i = 0;
429 ms.fields.forEach((mojom_types.StructField field) {
430 // Check that the declData is correct...
431 Expect.isNotNull(field.declData);
432 Expect.equals(expectedFields[i], field.declData.shortName);
433
434 // Special case each field since we already know what should be inside.
435 switch (i) {
436 case 0: // This is a TypeReference to a Union.
437 mojom_types.Type t = field.type;
438 Expect.isNotNull(t.typeReference);
439
440 // Type key, identifier, and expected reference id should match up.
441 mojom_types.TypeReference tr = t.typeReference;
442 String expectedRefID = "_test_included_unions_IncludedUnion__";
443 Expect.equals(expectedRefID, tr.identifier);
444 Expect.equals(expectedRefID, tr.typeKey);
445 break;
446 default:
447 assert(false);
448 }
449
450 i++;
451 });
452 }
453
454 // Used by the testValidateInterfaceType test.
455 class _BoundsCheckTestInterfaceFakeImplementation
456 extends validation.BoundsCheckTestInterface {
457 dynamic method0(int param0, [Function responseFactory = null]) {}
458 void method1(int param0) {}
459 }
460
461 // Test that mojom type descriptors were generated correctly for validation's
462 // BoundsCheckTestInterface.
463 testValidateInterfaceType() {
464 // interface BoundsCheckTestInterface {
465 // Method0(uint8 param0) => (uint8 param0);
466 // Method1(uint8 param0);
467 // };
468
469 var testValidationDescriptor = validation.getAllMojomTypeDefinitions();
470 String interfaceID = "_validation_test_interfaces_BoundsCheckTestInterface__";
471 String shortName = "BoundsCheckTestInterface";
472 Map<int, String> methodMap = <int, String>{0: "Method0", 1: "Method1",};
473
474 mojom_types.UserDefinedType udt = testValidationDescriptor[interfaceID];
475 Expect.isNotNull(udt);
476
477 mojom_types.MojomInterface mi = udt.interfaceType;
478 Expect.isNotNull(mi);
479
480 _checkMojomInterface(mi, shortName, methodMap);
481
482 var bcti_p = new validation.BoundsCheckTestInterfaceProxyImpl.unbound();
483 var bcti_f = new _BoundsCheckTestInterfaceFakeImplementation();
484
485 _checkServiceDescription(
486 bcti_p.serviceDescription, interfaceID, shortName, methodMap);
487 _checkServiceDescription(
488 bcti_f.serviceDescription, interfaceID, shortName, methodMap);
489 }
490
491 _checkServiceDescription(service_describer.ServiceDescription sd,
492 String interfaceID, String shortName, Map<int, String> methodMap) {
493 // Check the top level interface, which must pass _checkMojomInterface.
494 mojom_types.MojomInterface mi = sd.getTopLevelInterface();
495 _checkMojomInterface(mi, shortName, methodMap);
496
497 // Try out sd.GetTypeDefinition with the given interfaceID.
498 mojom_types.UserDefinedType udt = sd.getTypeDefinition(interfaceID);
499 Expect.isNotNull(udt.interfaceType);
500 _checkMojomInterface(udt.interfaceType, shortName, methodMap);
501
502 // Check all type definitions. Reflect-wise, all data inside should match the
503 // imported Descriptor.
504 var actualDescriptions = sd.getAllTypeDefinitions();
505 var expectedDescriptions = validation.getAllMojomTypeDefinitions();
506 Expect.mapEquals(actualDescriptions, expectedDescriptions);
507 }
508
509 _checkMojomInterface(mojom_types.MojomInterface mi, String shortName,
510 Map<int, String> methodMap) {
511 // check the generated short name.
512 Expect.isNotNull(mi.declData);
513 Expect.equals(mi.declData.shortName, shortName);
514
515 // Verify that the number of methods matches the expected ones.
516 Expect.equals(mi.methods.length, methodMap.length);
517
518 // Each MojomMethod must be named, typed, and "ordinal"ed correctly.
519 mi.methods.forEach((int ordinal, mojom_types.MojomMethod method) {
520 Expect.isNotNull(method.declData);
521 Expect.equals(methodMap[ordinal], method.declData.shortName);
522
523 // Special case each method since we know what's inside.
524 switch (ordinal) {
525 case 0: // Has request and response params.
526 // Request is a single uint8 input.
527 mojom_types.MojomStruct params = method.parameters;
528 Expect.equals(params.fields.length, 1);
529 Expect.equals(
530 params.fields[0].type.simpleType, mojom_types.SimpleType.UINT8);
531
532 // Response is a single uint8 output.
533 mojom_types.MojomStruct response = method.responseParams;
534 Expect.isNotNull(response);
535 Expect.equals(response.fields.length, 1);
536 Expect.equals(
537 response.fields[0].type.simpleType, mojom_types.SimpleType.UINT8);
538 break;
539 case 1: // Only has request params.
540 // Request is a single uint8 input.
541 mojom_types.MojomStruct params = method.parameters;
542 Expect.equals(params.fields.length, 1);
543 Expect.equals(
544 params.fields[0].type.simpleType, mojom_types.SimpleType.UINT8);
545
546 // Response is a single uint8 output.
547 mojom_types.MojomStruct response = method.responseParams;
548 Expect.isNull(response);
549 break;
550 default:
551 assert(false);
552 }
553 });
554 }
555
243 class CheckEnumCapsImpl implements regression.CheckEnumCaps { 556 class CheckEnumCapsImpl implements regression.CheckEnumCaps {
244 regression.CheckEnumCapsStub _stub; 557 regression.CheckEnumCapsStub _stub;
245 558
246 CheckEnumCapsImpl(core.MojoMessagePipeEndpoint endpoint) { 559 CheckEnumCapsImpl(core.MojoMessagePipeEndpoint endpoint) {
247 _stub = new regression.CheckEnumCapsStub.fromEndpoint(endpoint, this); 560 _stub = new regression.CheckEnumCapsStub.fromEndpoint(endpoint, this);
248 } 561 }
249 562
250 setEnumWithInternalAllCaps(regression.EnumWithInternalAllCaps e) {} 563 setEnumWithInternalAllCaps(regression.EnumWithInternalAllCaps e) {}
251 } 564 }
252 565
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
294 proxy.impl.onError = (_) => testCompleter.complete(true); 607 proxy.impl.onError = (_) => testCompleter.complete(true);
295 Isolate.spawn(closingProviderIsolate, pipe.endpoints[1]); 608 Isolate.spawn(closingProviderIsolate, pipe.endpoints[1]);
296 return testCompleter.future.then((b) { 609 return testCompleter.future.then((b) {
297 Expect.isTrue(b); 610 Expect.isTrue(b);
298 }); 611 });
299 } 612 }
300 613
301 main() async { 614 main() async {
302 testSerializeStructs(); 615 testSerializeStructs();
303 testUnions(); 616 testUnions();
617 testValidateMojomTypes();
304 await testEnums(); 618 await testEnums();
305 await testCallResponse(); 619 await testCallResponse();
306 await testAwaitCallResponse(); 620 await testAwaitCallResponse();
307 await runOnClosedTest(); 621 await runOnClosedTest();
308 } 622 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698