OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file | |
4 | |
5 #library('JsInteropFuncPassingTest'); | |
6 #import('../../pkg/unittest/unittest.dart'); | |
7 #import('../../pkg/unittest/html_config.dart'); | |
8 #import('dart:html'); | |
9 #import('dart:isolate'); | |
10 | |
11 injectSource(code) { | |
12 final script = new ScriptElement(); | |
13 script.type = 'text/javascript'; | |
14 script.innerHTML = code; | |
15 document.body.nodes.add(script); | |
16 } | |
17 | |
18 var dartToJsTest = """ | |
19 var port = new ReceivePortSync(); | |
20 port.receive(function (f) { | |
21 return f('fromJS'); | |
22 }); | |
23 window.registerPort('test1', port.toSendPort()); | |
24 """; | |
25 | |
26 var jsToDartTest = """ | |
27 var port1 = window.lookupPort('test2a'); | |
28 var result = port1.callSync(function (x) { | |
29 return x*2; | |
30 }); | |
31 | |
32 var port2 = window.lookupPort('test2b'); | |
33 port2.callSync(result); | |
34 """; | |
35 | |
36 var dartToJsToDartTest = """ | |
37 var port = new ReceivePortSync(); | |
38 port.receive(function (f) { | |
39 return f; | |
40 }); | |
41 window.registerPort('test3', port.toSendPort()); | |
42 """; | |
43 | |
44 main() { | |
45 useHtmlConfiguration(); | |
46 | |
47 test('dart-to-js-function', () { | |
48 injectSource(dartToJsTest); | |
49 | |
50 SendPortSync port = window.lookupPort('test1'); | |
51 var result = port.callSync((msg) { | |
52 Expect.equals('fromJS', msg); | |
53 return 'received'; | |
54 }); | |
55 Expect.equals('received', result); | |
56 }); | |
57 | |
58 test('js-to-dart-function', () { | |
59 var port1 = new ReceivePortSync(); | |
60 int invoked1 = 0; | |
61 port1.receive((f) { | |
62 ++invoked1; | |
63 var data = f(21); | |
64 expect(data, equals(42)); | |
65 return 'fromDart'; | |
66 }); | |
67 window.registerPort('test2a', port1.toSendPort()); | |
68 | |
69 var port2 = new ReceivePortSync(); | |
70 int invoked2 = 0; | |
71 port2.receive((x) { | |
72 ++invoked2; | |
73 expect(x, equals('fromDart')); | |
74 }); | |
75 window.registerPort('test2b', port2.toSendPort()); | |
76 | |
77 injectSource(jsToDartTest); | |
78 | |
79 expect(1, equals(invoked1)); | |
80 expect(1, equals(invoked2)); | |
81 }); | |
82 | |
83 test('dart-to-js-to-dart-function', () { | |
84 injectSource(dartToJsToDartTest); | |
85 | |
86 validate(x) { | |
87 expect(x, equals('fromCaller')); | |
88 return 'fromCallee'; | |
89 } | |
90 | |
91 SendPortSync port = window.lookupPort('test3'); | |
92 var f = port.callSync(validate); | |
93 var result = f('fromCaller'); | |
94 Expect.equals('fromCallee', result); | |
95 | |
96 }); | |
97 } | |
OLD | NEW |