OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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 | 3 // BSD-style license that can be found in the LICENSE file |
4 | 4 |
5 #library('JsInteropFuncPassingTest'); | 5 #library('JsInteropFuncPassingTest'); |
6 #import('../../pkg/unittest/unittest.dart'); | 6 #import('../../pkg/unittest/unittest.dart'); |
7 #import('../../pkg/unittest/html_config.dart'); | 7 #import('../../pkg/unittest/html_config.dart'); |
8 #import('dart:html'); | 8 #import('dart:html'); |
9 #import('dart:isolate'); | 9 #import('dart:isolate'); |
10 | 10 |
11 injectSource(code) { | 11 injectSource(code) { |
12 final script = new ScriptElement(); | 12 final script = new ScriptElement(); |
13 script.type = 'text/javascript'; | 13 script.type = 'text/javascript'; |
14 script.innerHTML = code; | 14 script.innerHTML = code; |
15 document.body.nodes.add(script); | 15 document.body.nodes.add(script); |
16 } | 16 } |
17 | 17 |
18 var isolateTest = """ | 18 var dartToJsTest = """ |
19 var port = new ReceivePortSync(); | 19 var port = new ReceivePortSync(); |
20 port.receive(function (f) { | 20 port.receive(function (f) { |
21 return f('fromJS'); | 21 return f('fromJS'); |
22 }); | 22 }); |
23 window.registerPort('test', port.toSendPort()); | 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()); |
24 """; | 42 """; |
25 | 43 |
26 main() { | 44 main() { |
27 useHtmlConfiguration(); | 45 useHtmlConfiguration(); |
28 | 46 |
29 test('dart-to-js-function', () { | 47 test('dart-to-js-function', () { |
30 injectSource(isolateTest); | 48 injectSource(dartToJsTest); |
31 | 49 |
32 SendPortSync port = window.lookupPort('test'); | 50 SendPortSync port = window.lookupPort('test1'); |
33 var result = port.callSync((msg) { | 51 var result = port.callSync((msg) { |
34 Expect.equals('fromJS', msg); | 52 Expect.equals('fromJS', msg); |
35 return 'received'; | 53 return 'received'; |
36 }); | 54 }); |
37 Expect.equals('received', result); | 55 Expect.equals('received', result); |
38 }); | 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 }); |
39 } | 97 } |
OLD | NEW |