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('postmessage_js_test'); | |
6 #import('../../pkg/unittest/unittest.dart'); | |
7 #import('../../pkg/unittest/html_config.dart'); | |
8 #import('dart:html'); | |
9 | |
10 injectSource(code) { | |
11 final script = new ScriptElement(); | |
12 script.type = 'text/javascript'; | |
13 script.innerHTML = code; | |
14 document.body.nodes.add(script); | |
15 } | |
16 | |
17 | |
18 main() { | |
19 useHtmlConfiguration(); | |
20 | |
21 test('js-to-dart-postmessage', () { | |
22 // Pass an object literal from JavaScript. It should be seen as a Dart | |
23 // Map. | |
24 | |
25 final JS_CODE = """ | |
26 window.postMessage({eggs: 3, recipient: 33}, '*'); | |
27 """; | |
28 var callback; | |
29 var onSuccess = expectAsync1((e) { | |
30 window.on.message.remove(callback); | |
31 }); | |
32 callback = (e) { | |
vsm
2012/10/04 16:03:35
I think you want to guardAsync this code.
| |
33 var data = e.data; | |
34 if (data is String) return; // Messages from unit test protocol. | |
35 expect(data is Map); | |
36 expect(data['eggs'], equals(3)); | |
37 onSuccess(e); | |
38 }; | |
39 window.on.message.add(callback); | |
40 injectSource(JS_CODE); | |
41 }); | |
vsm
2012/10/04 16:03:35
Are cycles supported in the data? If so, perhaps
| |
42 | |
43 | |
44 test('dart-to-js-to-dart-postmessage', () { | |
45 // Pass dictionaries between Dart and JavaScript. | |
46 | |
47 final JS_CODE = """ | |
48 window.addEventListener('message', handler); | |
49 function handler(e) { | |
50 var data = e.data; | |
51 if (typeof data == 'string') return; | |
52 if (data.recipient != 'JS') return | |
53 var response = {recipient: 'DART'}; | |
54 response[data['curry']] = 50; | |
55 window.removeEventListener('message', handler); | |
56 window.postMessage(response, '*'); | |
57 } | |
58 """; | |
59 var callback; | |
60 var onSuccess = expectAsync1((e) { | |
61 window.on.message.remove(callback); | |
62 }); | |
63 callback = (e) { | |
vsm
2012/10/04 16:03:35
ditto on guardAsync
| |
64 var data = e.data; | |
65 if (data is String) return; // Messages from unit test protocol. | |
66 expect(data is Map); | |
67 if (data['recipient'] != 'DART') return; // Hearing the sent message. | |
68 expect(data['peas'], equals(50)); | |
69 onSuccess(e); | |
70 }; | |
71 window.on.message.add(callback); | |
72 injectSource(JS_CODE); | |
73 window.postMessage({'recipient': 'JS', 'curry': 'peas'}, '*'); | |
74 }); | |
75 } | |
OLD | NEW |