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

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

Issue 2006093002: Dart: Futures -> Callbacks. (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Merge Created 4 years, 6 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 8
9 import 'package:mojo/bindings.dart' as bindings; 9 import 'package:mojo/bindings.dart' as bindings;
10 import 'package:mojo/core.dart' as core; 10 import 'package:mojo/core.dart' as core;
11 import 'package:_mojo_for_test_only/expect.dart'; 11 import 'package:_mojo_for_test_only/expect.dart';
12 import 'package:_mojo_for_test_only/sample/sample_interfaces.mojom.dart' as samp le; 12 import 'package:_mojo_for_test_only/sample/sample_interfaces.mojom.dart' as samp le;
13 13
14 // Bump this if sample_interfaces.mojom adds higher versions. 14 // Bump this if sample_interfaces.mojom adds higher versions.
15 const int maxVersion = 3; 15 const int maxVersion = 3;
16 16
17 // Implementation of IntegerAccessor. 17 // Implementation of IntegerAccessor.
18 class IntegerAccessorImpl implements sample.IntegerAccessor { 18 class IntegerAccessorImpl implements sample.IntegerAccessor {
19 // Some initial value. 19 // Some initial value.
20 int _value = 0; 20 int _value = 0;
21 21
22 dynamic getInteger([Function responseFactory = null]) => 22 void getInteger(Function response) {
23 responseFactory(_value, sample.Enum.value); 23 response(_value, sample.Enum.value);
24 }
24 25
25 void setInteger(int data, sample.Enum type) { 26 void setInteger(int data, sample.Enum type) {
26 Expect.equals(sample.Enum.value, type); 27 Expect.equals(sample.Enum.value, type);
27 // Update data. 28 // Update data.
28 _value = data; 29 _value = data;
29 } 30 }
30 } 31 }
31 32
32 // Returns [proxy, stub]. 33 // Returns [proxy, stub].
33 List buildConnectedProxyAndStub() { 34 List buildConnectedProxyAndStub() {
34 var pipe = new core.MojoMessagePipe(); 35 var pipe = new core.MojoMessagePipe();
35 var proxy = new sample.IntegerAccessorProxy.fromEndpoint(pipe.endpoints[0]); 36 var proxy = new sample.IntegerAccessorProxy.fromEndpoint(pipe.endpoints[0]);
36 var impl = new IntegerAccessorImpl(); 37 var impl = new IntegerAccessorImpl();
37 var stub = 38 var stub =
38 new sample.IntegerAccessorStub.fromEndpoint(pipe.endpoints[1], impl); 39 new sample.IntegerAccessorStub.fromEndpoint(pipe.endpoints[1], impl);
39 return [proxy, stub]; 40 return [proxy, stub];
40 } 41 }
41 42
42 void closeProxyAndStub(List ps) { 43 closeProxyAndStub(List ps) async {
43 var proxy = ps[0]; 44 var proxy = ps[0];
44 var stub = ps[1]; 45 var stub = ps[1];
45 proxy.close(); 46 await proxy.close();
46 stub.close(); 47 await stub.close();
47 } 48 }
48 49
49 testQueryVersion() async { 50 testQueryVersion() async {
50 var ps = buildConnectedProxyAndStub(); 51 var ps = buildConnectedProxyAndStub();
51 var proxy = ps[0]; 52 var proxy = ps[0];
52 // The version starts at 0. 53 // The version starts at 0.
53 Expect.equals(0, proxy.ctrl.version); 54 Expect.equals(0, proxy.ctrl.version);
54 // We are talking to an implementation that supports version maxVersion. 55 // We are talking to an implementation that supports version maxVersion.
55 var providedVersion = await proxy.ctrl.queryVersion(); 56 var providedVersion = await proxy.ctrl.queryVersion();
56 Expect.equals(maxVersion, providedVersion); 57 Expect.equals(maxVersion, providedVersion);
57 // The proxy's version has been updated. 58 // The proxy's version has been updated.
58 Expect.equals(providedVersion, proxy.ctrl.version); 59 Expect.equals(providedVersion, proxy.ctrl.version);
59 closeProxyAndStub(ps); 60 await closeProxyAndStub(ps);
60 } 61 }
61 62
62 testRequireVersionSuccess() async { 63 testRequireVersionSuccess() async {
63 var ps = buildConnectedProxyAndStub(); 64 var ps = buildConnectedProxyAndStub();
64 var proxy = ps[0]; 65 var proxy = ps[0];
65 Expect.equals(0, proxy.ctrl.version); 66 Expect.equals(0, proxy.ctrl.version);
66 // Require version maxVersion. 67 // Require version maxVersion.
67 proxy.ctrl.requireVersion(maxVersion); 68 proxy.ctrl.requireVersion(maxVersion);
69 Expect.equals(maxVersion, proxy.ctrl.version);
68 // Make a request and get a response. 70 // Make a request and get a response.
69 var response = await proxy.getInteger(); 71 var c = new Completer();
70 Expect.equals(0, response.data); 72 proxy.getInteger((int response, sample.Enum type) {
71 closeProxyAndStub(ps); 73 c.complete(response);
74 });
75 var response = await proxy.responseOrError(c.future);
76 Expect.equals(0, response);
77 await closeProxyAndStub(ps);
72 } 78 }
73 79
74 testRequireVersionDisconnect() async { 80 testRequireVersionDisconnect() async {
75 var ps = buildConnectedProxyAndStub(); 81 var ps = buildConnectedProxyAndStub();
76 var proxy = ps[0]; 82 var proxy = ps[0];
77 Expect.equals(0, proxy.ctrl.version); 83 Expect.equals(0, proxy.ctrl.version);
78 // Require version maxVersion. 84 // Require version maxVersion.
79 proxy.ctrl.requireVersion(maxVersion); 85 proxy.ctrl.requireVersion(maxVersion);
80 Expect.equals(maxVersion, proxy.ctrl.version); 86 Expect.equals(maxVersion, proxy.ctrl.version);
81 // Set integer. 87 // Set integer.
82 proxy.setInteger(34, sample.Enum.value); 88 proxy.setInteger(34, sample.Enum.value);
83 // Get integer. 89 // Get integer.
84 var response = await proxy.getInteger(); 90 var c = new Completer();
85 Expect.equals(34, response.data); 91 proxy.getInteger((int response, sample.Enum type) {
92 c.complete(response);
93 });
94 var response = await proxy.responseOrError(c.future);
95 Expect.equals(34, response);
86 // Require version maxVersion + 1 96 // Require version maxVersion + 1
87 proxy.ctrl.requireVersion(maxVersion + 1); 97 proxy.ctrl.requireVersion(maxVersion + 1);
88 // Version number is updated synchronously. 98 // Version number is updated synchronously.
89 Expect.equals(maxVersion + 1, proxy.ctrl.version); 99 Expect.equals(maxVersion + 1, proxy.ctrl.version);
90 // Get integer, expect a failure. 100 // Get integer, expect a failure.
91 bool exceptionCaught = false; 101 bool exceptionCaught = false;
102 c = new Completer();
103 proxy.getInteger((int response, sample.Enum type) {
104 Expect.fail('Should ahve an exception.');
105 });
92 try { 106 try {
93 response = await proxy.getInteger(); 107 response = await proxy.responseOrError(c.future);
94 Expect.fail('Should have an exception.'); 108 Expect.fail('Should have an exception.');
95 } catch(e) { 109 } catch(e) {
96 exceptionCaught = true; 110 exceptionCaught = true;
97 } 111 }
98 Expect.isTrue(exceptionCaught); 112 Expect.isTrue(exceptionCaught);
99 closeProxyAndStub(ps); 113 await closeProxyAndStub(ps);
100 } 114 }
101 115
102 main() async { 116 main() async {
103 await testQueryVersion(); 117 await testQueryVersion();
104 await testRequireVersionSuccess(); 118 await testRequireVersionSuccess();
105 await testRequireVersionDisconnect(); 119 await testRequireVersionDisconnect();
106 } 120 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698