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

Side by Side Diff: mojo/dart/unittests/embedder_tests/bindings_generation_test.dart

Issue 1539673003: Generate Mojom Types in Dart (Take 2) (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Merge with master Created 4 years, 10 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
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:mojo/bindings.dart' as bindings; 10 import 'package:mojo/bindings.dart' as bindings;
11 import 'package:mojo/core.dart' as core; 11 import 'package:mojo/core.dart' as core;
12 import 'package:mojo/mojo/bindings/types/mojom_types.mojom.dart' as mojom_types;
13 import 'package:mojo/mojo/bindings/types/service_describer.mojom.dart'
14 as service_describer;
12 import 'package:_mojo_for_test_only/expect.dart'; 15 import 'package:_mojo_for_test_only/expect.dart';
13 import 'package:_mojo_for_test_only/mojo/test/rect.mojom.dart' as rect; 16 import 'package:_mojo_for_test_only/mojo/test/rect.mojom.dart' as rect;
14 import 'package:_mojo_for_test_only/mojo/test/serialization_test_structs.mojom.d art' 17 import 'package:_mojo_for_test_only/mojo/test/serialization_test_structs.mojom.d art'
15 as serialization; 18 as serialization;
16 import 'package:_mojo_for_test_only/mojo/test/test_structs.mojom.dart' 19 import 'package:_mojo_for_test_only/mojo/test/test_structs.mojom.dart'
17 as structs; 20 as structs;
18 import 'package:_mojo_for_test_only/mojo/test/test_unions.mojom.dart' 21 import 'package:_mojo_for_test_only/mojo/test/test_unions.mojom.dart'
19 as unions; 22 as unions;
23 import 'package:_mojo_for_test_only/mojo/test/validation_test_interfaces.mojom.d art'
24 as validation;
20 import 'package:_mojo_for_test_only/regression_tests/regression_tests.mojom.dart ' 25 import 'package:_mojo_for_test_only/regression_tests/regression_tests.mojom.dart '
21 as regression; 26 as regression;
22 import 'package:_mojo_for_test_only/sample/sample_interfaces.mojom.dart' 27 import 'package:_mojo_for_test_only/sample/sample_interfaces.mojom.dart'
23 as sample; 28 as sample;
24 29
25 class ProviderImpl implements sample.Provider { 30 class ProviderImpl implements sample.Provider {
26 sample.ProviderStub _stub; 31 sample.ProviderStub _stub;
27 32
28 ProviderImpl(core.MojoMessagePipeEndpoint endpoint) { 33 ProviderImpl(core.MojoMessagePipeEndpoint endpoint) {
29 _stub = new sample.ProviderStub.fromEndpoint(endpoint, this); 34 _stub = new sample.ProviderStub.fromEndpoint(endpoint, this);
30 } 35 }
31 36
32 echoString(String a, Function responseFactory) => 37 echoString(String a, Function responseFactory) =>
33 new Future.value(responseFactory(a)); 38 new Future.value(responseFactory(a));
34 39
35 echoStrings(String a, String b, Function responseFactory) => 40 echoStrings(String a, String b, Function responseFactory) =>
36 new Future.value(responseFactory(a, b)); 41 new Future.value(responseFactory(a, b));
37 42
38 echoMessagePipeHanlde(core.MojoHandle a, Function responseFactory) => 43 echoMessagePipeHandle(core.MojoHandle a, Function responseFactory) =>
39 new Future.value(responseFactory(a)); 44 new Future.value(responseFactory(a));
40 45
41 echoEnum(sample.Enum a, Function responseFactory) => 46 echoEnum(sample.Enum a, Function responseFactory) =>
42 new Future.value(responseFactory(a)); 47 new Future.value(responseFactory(a));
43 } 48 }
44 49
45 void providerIsolate(core.MojoMessagePipeEndpoint endpoint) { 50 void providerIsolate(core.MojoMessagePipeEndpoint endpoint) {
46 new ProviderImpl(endpoint); 51 new ProviderImpl(endpoint);
47 } 52 }
48 53
(...skipping 333 matching lines...) Expand 10 before | Expand all | Expand 10 after
382 e = CamelCaseTestEnum.int8Thing; 387 e = CamelCaseTestEnum.int8Thing;
383 e = CamelCaseTestEnum.int16Thing; 388 e = CamelCaseTestEnum.int16Thing;
384 e = CamelCaseTestEnum.int32Th1Ng; 389 e = CamelCaseTestEnum.int32Th1Ng;
385 e = CamelCaseTestEnum.int64Th1ng; 390 e = CamelCaseTestEnum.int64Th1ng;
386 e = CamelCaseTestEnum.uint8TH1ng; 391 e = CamelCaseTestEnum.uint8TH1ng;
387 e = CamelCaseTestEnum.uint16tH1Ng; 392 e = CamelCaseTestEnum.uint16tH1Ng;
388 e = CamelCaseTestEnum.uint32Th1ng; 393 e = CamelCaseTestEnum.uint32Th1ng;
389 e = CamelCaseTestEnum.uint64Th1Ng; 394 e = CamelCaseTestEnum.uint64Th1Ng;
390 } 395 }
391 396
397 testValidateMojomTypes() {
398 testValidateEnumType();
399 testValidateStructType();
400 testValidateUnionType();
401 testValidateTestStructWithImportType();
402 testValidateInterfaceType();
403 }
404
405 // Test that mojom type descriptors were generated correctly for validation's
406 // BasicEnum.
407 testValidateEnumType() {
408 var testValidationDescriptor = validation.getAllMojomTypeDefinitions();
409 String enumID = "validation_test_interfaces_BasicEnum__";
410 String shortName = "BasicEnum";
411 String fullIdentifier = "mojo.test.BasicEnum";
412 Map<String, int> labelMap = <String, int>{
413 "A": 0,
414 "B": 1,
415 "C": 0,
416 "D": -3,
417 "E": 0xA,
418 };
419
420 // Extract the UserDefinedType from the descriptor using enumID.
421 mojom_types.UserDefinedType udt = testValidationDescriptor[enumID];
422 Expect.isNotNull(udt);
423
424 // The enumType must be present and declared properly.
425 mojom_types.MojomEnum me = udt.enumType;
426 Expect.isNotNull(me);
427 Expect.isNotNull(me.declData);
428 Expect.equals(me.declData.shortName, shortName);
429 Expect.equals(me.declData.fullIdentifier, fullIdentifier);
430
431 // Now compare the labels to verify that the enum labels match the expected
432 // ones.
433 Expect.equals(me.values.length, labelMap.length);
434 me.values.forEach((mojom_types.EnumValue ev) {
435 // Check that the declData is correct...
436 Expect.isNotNull(ev.declData);
437 Expect.isNotNull(labelMap[ev.declData.shortName]);
438
439 // Check that the enumTypeKey matches the enumID.
440 Expect.equals(ev.enumTypeKey, enumID);
441 Expect.equals(ev.intValue, labelMap[ev.declData.shortName]);
442 });
443 }
444
445 // Test that mojom type descriptors were generated correctly for validation's
446 // StructE.
447 testValidateStructType() {
448 var testValidationDescriptor = validation.getAllMojomTypeDefinitions();
449 String structID = "validation_test_interfaces_StructE__";
450 String shortName = "StructE";
451 String fullIdentifier = "mojo.test.StructE";
452 Map<int, String> expectedFields = <int, String>{
453 0: "StructD",
454 1: "DataPipeConsumer",
455 };
456
457 // Extract the UserDefinedType from the descriptor using structID.
458 mojom_types.UserDefinedType udt = testValidationDescriptor[structID];
459 Expect.isNotNull(udt);
460
461 // The structType must be present and declared properly.
462 mojom_types.MojomStruct ms = udt.structType;
463 Expect.isNotNull(ms);
464 Expect.isNotNull(ms.declData);
465 Expect.equals(ms.declData.shortName, shortName);
466 Expect.equals(ms.declData.fullIdentifier, fullIdentifier);
467
468 // Now compare the fields to verify that the struct fields match the expected
469 // ones.
470 Expect.equals(ms.fields.length, expectedFields.length);
471 int i = 0;
472 ms.fields.forEach((mojom_types.StructField field) {
473 // Check that the declData is correct...
474 Expect.isNotNull(field.declData);
475 Expect.equals(expectedFields[i], field.declData.shortName);
476
477 // Special case each field since we already know what should be inside.
478 switch (i) {
479 case 0: // This is a TypeReference to StructD.
480 mojom_types.Type t = field.type;
481 Expect.isNotNull(t.typeReference);
482
483 // Type key, identifier, and expected reference id should match up.
484 mojom_types.TypeReference tr = t.typeReference;
485 String expectedRefID = "validation_test_interfaces_StructD__";
486 Expect.equals(expectedRefID, tr.identifier);
487 Expect.equals(expectedRefID, tr.typeKey);
488 break;
489 case 1: // This is a non-nullable DataPipeConsumer HandleType.
490 mojom_types.Type t = field.type;
491 Expect.isNotNull(t.handleType);
492
493 mojom_types.HandleType ht = t.handleType;
494 Expect.isFalse(ht.nullable);
495 Expect.equals(mojom_types.HandleTypeKind.dataPipeConsumer, ht.kind);
496 break;
497 default:
498 assert(false);
499 }
500
501 i++;
502 });
503 }
504
505 // Test that mojom type descriptors were generated correctly for validation's
506 // UnionB.
507 testValidateUnionType() {
508 var testValidationDescriptor = validation.getAllMojomTypeDefinitions();
509 String unionID = "validation_test_interfaces_UnionB__";
510 String shortName = "UnionB";
511 String fullIdentifier = "mojo.test.UnionB";
512 Map<int, String> expectedFields = <int, String>{
513 0: "A",
514 1: "B",
515 2: "C",
516 3: "D",
517 };
518
519 // Extract the UserDefinedType from the descriptor using unionID.
520 mojom_types.UserDefinedType udt = testValidationDescriptor[unionID];
521 Expect.isNotNull(udt);
522
523 // The unionType must be present and declared properly.
524 mojom_types.MojomUnion mu = udt.unionType;
525 Expect.isNotNull(mu);
526 Expect.isNotNull(mu.declData);
527 Expect.equals(mu.declData.shortName, shortName);
528 Expect.equals(mu.declData.fullIdentifier, fullIdentifier);
529
530 // Now compare the fields to verify that the union fields match the expected
531 // ones.
532 Expect.equals(mu.fields.length, expectedFields.length);
533 mu.fields.forEach((mojom_types.UnionField field) {
534 int ordinal = field.tag;
535
536 // Check that the declData is correct...
537 Expect.isNotNull(field.declData);
538 Expect.equals(expectedFields[ordinal], field.declData.shortName);
539
540 // Special: It turns out that all types are simple types.
541 mojom_types.Type t = field.type;
542 Expect.isNotNull(t.simpleType);
543 mojom_types.SimpleType st = t.simpleType;
544
545 // Special case each field since we already know what should be inside.
546 switch (ordinal) {
547 case 0: // Uint16
548 Expect.equals(st, mojom_types.SimpleType.uint16);
549 break;
550 case 1: // Uint32
551 case 3:
552 Expect.equals(st, mojom_types.SimpleType.uint32);
553 break;
554 case 2: // Uint64
555 Expect.equals(st, mojom_types.SimpleType.uint64);
556 break;
557 default:
558 assert(false);
559 }
560 });
561 }
562
563 // Test that mojom type descriptors were generated correctly for validation's
564 // IncludingStruct, which contains a union imported from test_included_unions.
565 testValidateTestStructWithImportType() {
566 var testUnionsDescriptor = unions.getAllMojomTypeDefinitions();
567 String structID = "test_unions_IncludingStruct__";
568 String shortName = "IncludingStruct";
569 String fullIdentifier = "mojo.test.IncludingStruct";
570 Map<int, String> expectedFields = <int, String>{0: "A",};
571
572 // Extract the UserDefinedType from the descriptor using structID.
573 mojom_types.UserDefinedType udt = testUnionsDescriptor[structID];
574 Expect.isNotNull(udt);
575
576 // The structType must be present and declared properly.
577 mojom_types.MojomStruct ms = udt.structType;
578 Expect.isNotNull(ms);
579 Expect.isNotNull(ms.declData);
580 Expect.equals(ms.declData.shortName, shortName);
581 Expect.equals(ms.declData.fullIdentifier, fullIdentifier);
582
583 // Now compare the fields to verify that the struct fields match the expected
584 // ones.
585 Expect.equals(ms.fields.length, expectedFields.length);
586 int i = 0;
587 ms.fields.forEach((mojom_types.StructField field) {
588 // Check that the declData is correct...
589 Expect.isNotNull(field.declData);
590 Expect.equals(expectedFields[i], field.declData.shortName);
591
592 // Special case each field since we already know what should be inside.
593 switch (i) {
594 case 0: // This is a TypeReference to a Union.
595 mojom_types.Type t = field.type;
596 Expect.isNotNull(t.typeReference);
597
598 // Type key, identifier, and expected reference id should match up.
599 mojom_types.TypeReference tr = t.typeReference;
600 String expectedRefID = "test_included_unions_IncludedUnion__";
601 Expect.equals(expectedRefID, tr.identifier);
602 Expect.equals(expectedRefID, tr.typeKey);
603 break;
604 default:
605 assert(false);
606 }
607
608 i++;
609 });
610 }
611
612 // Test that mojom type descriptors were generated correctly for validation's
613 // BoundsCheckTestInterface.
614 testValidateInterfaceType() {
615 // interface BoundsCheckTestInterface {
616 // Method0(uint8 param0) => (uint8 param0);
617 // Method1(uint8 param0);
618 // };
619
620 var testValidationDescriptor = validation.getAllMojomTypeDefinitions();
621 String interfaceID = "validation_test_interfaces_BoundsCheckTestInterface__";
622 String shortName = "BoundsCheckTestInterface";
623 String fullIdentifier = "mojo.test.BoundsCheckTestInterface";
624 Map<int, String> methodMap = <int, String>{0: "Method0", 1: "Method1",};
625
626 mojom_types.UserDefinedType udt = testValidationDescriptor[interfaceID];
627 Expect.isNotNull(udt);
628
629 mojom_types.MojomInterface mi = udt.interfaceType;
630 Expect.isNotNull(mi);
631
632 _checkMojomInterface(mi, shortName, fullIdentifier, methodMap);
633
634 // The proxy and stub need to have a valid serviceDescription.
635 var bcti_p = new validation.BoundsCheckTestInterfaceProxy.unbound().impl;
636 var bcti_s = new validation.BoundsCheckTestInterfaceStub.unbound();
637
638 _checkServiceDescription(
639 bcti_p.serviceDescription, interfaceID, shortName, fullIdentifier,
640 methodMap);
641 _checkServiceDescription(
642 bcti_s.serviceDescription, interfaceID, shortName, fullIdentifier,
643 methodMap);
644 }
645
646 _checkServiceDescription(service_describer.ServiceDescription sd,
647 String interfaceID, String shortName, String fullIdentifier,
648 Map<int, String> methodMap) {
649 // Check the top level interface, which must pass _checkMojomInterface.
650 mojom_types.MojomInterface mi = sd.getTopLevelInterface();
651 _checkMojomInterface(mi, shortName, fullIdentifier, methodMap);
652
653 // Try out sd.GetTypeDefinition with the given interfaceID.
654 mojom_types.UserDefinedType udt = sd.getTypeDefinition(interfaceID);
655 Expect.isNotNull(udt.interfaceType);
656 _checkMojomInterface(udt.interfaceType, shortName, fullIdentifier, methodMap);
657
658 // Check all type definitions. Reflect-wise, all data inside should match the
659 // imported Descriptor.
660 var actualDescriptions = sd.getAllTypeDefinitions();
661 var expectedDescriptions = validation.getAllMojomTypeDefinitions();
662 Expect.mapEquals(actualDescriptions, expectedDescriptions);
663 }
664
665 _checkMojomInterface(mojom_types.MojomInterface mi, String shortName,
666 String fullIdentifier, Map<int, String> methodMap) {
667 // check the generated short name.
668 Expect.isNotNull(mi.declData);
669 Expect.equals(mi.declData.shortName, shortName);
670 Expect.equals(mi.declData.fullIdentifier, fullIdentifier);
671
672 // Verify that the number of methods matches the expected ones.
673 Expect.equals(mi.methods.length, methodMap.length);
674
675 // Each MojomMethod must be named, typed, and "ordinal"ed correctly.
676 mi.methods.forEach((int ordinal, mojom_types.MojomMethod method) {
677 Expect.isNotNull(method.declData);
678 Expect.equals(methodMap[ordinal], method.declData.shortName);
679
680 // Special case each method since we know what's inside.
681 switch (ordinal) {
682 case 0: // Has request and response params.
683 // Request is a single uint8 input.
684 mojom_types.MojomStruct params = method.parameters;
685 Expect.equals(params.fields.length, 1);
686 Expect.equals(
687 params.fields[0].type.simpleType, mojom_types.SimpleType.uint8);
688
689 // Response is a single uint8 output.
690 mojom_types.MojomStruct response = method.responseParams;
691 Expect.isNotNull(response);
692 Expect.equals(response.fields.length, 1);
693 Expect.equals(
694 response.fields[0].type.simpleType, mojom_types.SimpleType.uint8);
695 break;
696 case 1: // Only has request params.
697 // Request is a single uint8 input.
698 mojom_types.MojomStruct params = method.parameters;
699 Expect.equals(params.fields.length, 1);
700 Expect.equals(
701 params.fields[0].type.simpleType, mojom_types.SimpleType.uint8);
702
703 // Response is a single uint8 output.
704 mojom_types.MojomStruct response = method.responseParams;
705 Expect.isNull(response);
706 break;
707 default:
708 assert(false);
709 }
710 });
711 }
712
392 713
393 main() async { 714 main() async {
394 testSerializeStructs(); 715 testSerializeStructs();
395 testUnions(); 716 testUnions();
717 testValidateMojomTypes();
396 await testEnums(); 718 await testEnums();
397 await testCallResponse(); 719 await testCallResponse();
398 await testAwaitCallResponse(); 720 await testAwaitCallResponse();
399 await runOnClosedTest(); 721 await runOnClosedTest();
400 await testRegression551(); 722 await testRegression551();
401 await testServiceName(); 723 await testServiceName();
402 testCamelCase(); 724 testCamelCase();
403 } 725 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698