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

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: Simplify identifier_store for Go and Dart 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:_mojo_for_test_only/expect.dart'; 10 import 'package:_mojo_for_test_only/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:_mojo_for_test_only/sample/sample_interfaces.mojom.dart' as samp le; 15 import 'package:_mojo_for_test_only/sample/sample_interfaces.mojom.dart' as samp le;
14 import 'package:_mojo_for_test_only/mojo/test/test_structs.mojom.dart' as struct s; 16 import 'package:_mojo_for_test_only/mojo/test/test_structs.mojom.dart' as struct s;
15 import 'package:_mojo_for_test_only/mojo/test/test_unions.mojom.dart' as unions; 17 import 'package:_mojo_for_test_only/mojo/test/test_unions.mojom.dart' as unions;
18 import 'package:_mojo_for_test_only/mojo/test/validation_test_interfaces.mojom.d art'
19 as validation;
16 import 'package:_mojo_for_test_only/mojo/test/rect.mojom.dart' as rect; 20 import 'package:_mojo_for_test_only/mojo/test/rect.mojom.dart' as rect;
17 import 'package:_mojo_for_test_only/mojo/test/serialization_test_structs.mojom.d art' 21 import 'package:_mojo_for_test_only/mojo/test/serialization_test_structs.mojom.d art'
18 as serialization; 22 as serialization;
19 import 'package:_mojo_for_test_only/regression_tests/regression_tests.mojom.dart ' 23 import 'package:_mojo_for_test_only/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 // Test that mojom type descriptors were generated correctly for validation's
455 // BoundsCheckTestInterface.
456 testValidateInterfaceType() {
457 // interface BoundsCheckTestInterface {
458 // Method0(uint8 param0) => (uint8 param0);
459 // Method1(uint8 param0);
460 // };
461
462 var testValidationDescriptor = validation.getAllMojomTypeDefinitions();
463 String interfaceID = "_validation_test_interfaces_BoundsCheckTestInterface__";
464 String shortName = "BoundsCheckTestInterface";
465 Map<int, String> methodMap = <int, String>{0: "Method0", 1: "Method1",};
466
467 mojom_types.UserDefinedType udt = testValidationDescriptor[interfaceID];
468 Expect.isNotNull(udt);
469
470 mojom_types.MojomInterface mi = udt.interfaceType;
471 Expect.isNotNull(mi);
472
473 _checkMojomInterface(mi, shortName, methodMap);
474
475 // The proxy and stub need to have a valid serviceDescription.
476 var bcti_p = new validation.BoundsCheckTestInterfaceProxyImpl.unbound();
477 var bcti_s = new validation.BoundsCheckTestInterfaceStub.unbound();
478
479 _checkServiceDescription(
480 bcti_p.serviceDescription, interfaceID, shortName, methodMap);
481 _checkServiceDescription(
482 bcti_s.serviceDescription, interfaceID, shortName, methodMap);
483 }
484
485 _checkServiceDescription(service_describer.ServiceDescription sd,
486 String interfaceID, String shortName, Map<int, String> methodMap) {
487 // Check the top level interface, which must pass _checkMojomInterface.
488 mojom_types.MojomInterface mi = sd.getTopLevelInterface();
489 _checkMojomInterface(mi, shortName, methodMap);
490
491 // Try out sd.GetTypeDefinition with the given interfaceID.
492 mojom_types.UserDefinedType udt = sd.getTypeDefinition(interfaceID);
493 Expect.isNotNull(udt.interfaceType);
494 _checkMojomInterface(udt.interfaceType, shortName, methodMap);
495
496 // Check all type definitions. Reflect-wise, all data inside should match the
497 // imported Descriptor.
498 var actualDescriptions = sd.getAllTypeDefinitions();
499 var expectedDescriptions = validation.getAllMojomTypeDefinitions();
500 Expect.mapEquals(actualDescriptions, expectedDescriptions);
501 }
502
503 _checkMojomInterface(mojom_types.MojomInterface mi, String shortName,
504 Map<int, String> methodMap) {
505 // check the generated short name.
506 Expect.isNotNull(mi.declData);
507 Expect.equals(mi.declData.shortName, shortName);
508
509 // Verify that the number of methods matches the expected ones.
510 Expect.equals(mi.methods.length, methodMap.length);
511
512 // Each MojomMethod must be named, typed, and "ordinal"ed correctly.
513 mi.methods.forEach((int ordinal, mojom_types.MojomMethod method) {
514 Expect.isNotNull(method.declData);
515 Expect.equals(methodMap[ordinal], method.declData.shortName);
516
517 // Special case each method since we know what's inside.
518 switch (ordinal) {
519 case 0: // Has request and response params.
520 // Request is a single uint8 input.
521 mojom_types.MojomStruct params = method.parameters;
522 Expect.equals(params.fields.length, 1);
523 Expect.equals(
524 params.fields[0].type.simpleType, mojom_types.SimpleType.UINT8);
525
526 // Response is a single uint8 output.
527 mojom_types.MojomStruct response = method.responseParams;
528 Expect.isNotNull(response);
529 Expect.equals(response.fields.length, 1);
530 Expect.equals(
531 response.fields[0].type.simpleType, mojom_types.SimpleType.UINT8);
532 break;
533 case 1: // Only has request params.
534 // Request is a single uint8 input.
535 mojom_types.MojomStruct params = method.parameters;
536 Expect.equals(params.fields.length, 1);
537 Expect.equals(
538 params.fields[0].type.simpleType, mojom_types.SimpleType.UINT8);
539
540 // Response is a single uint8 output.
541 mojom_types.MojomStruct response = method.responseParams;
542 Expect.isNull(response);
543 break;
544 default:
545 assert(false);
546 }
547 });
548 }
549
243 class CheckEnumCapsImpl implements regression.CheckEnumCaps { 550 class CheckEnumCapsImpl implements regression.CheckEnumCaps {
244 regression.CheckEnumCapsStub _stub; 551 regression.CheckEnumCapsStub _stub;
245 552
246 CheckEnumCapsImpl(core.MojoMessagePipeEndpoint endpoint) { 553 CheckEnumCapsImpl(core.MojoMessagePipeEndpoint endpoint) {
247 _stub = new regression.CheckEnumCapsStub.fromEndpoint(endpoint, this); 554 _stub = new regression.CheckEnumCapsStub.fromEndpoint(endpoint, this);
248 } 555 }
249 556
250 setEnumWithInternalAllCaps(regression.EnumWithInternalAllCaps e) {} 557 setEnumWithInternalAllCaps(regression.EnumWithInternalAllCaps e) {}
251 } 558 }
252 559
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
324 c.complete(true); 631 c.complete(true);
325 }); 632 });
326 }); 633 });
327 }); 634 });
328 return c.future; 635 return c.future;
329 } 636 }
330 637
331 main() async { 638 main() async {
332 testSerializeStructs(); 639 testSerializeStructs();
333 testUnions(); 640 testUnions();
641 testValidateMojomTypes();
334 await testEnums(); 642 await testEnums();
335 await testCallResponse(); 643 await testCallResponse();
336 await testAwaitCallResponse(); 644 await testAwaitCallResponse();
337 await runOnClosedTest(); 645 await runOnClosedTest();
338 await testRegression551(); 646 await testRegression551();
339 } 647 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698