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 postmessage_js_test; | 5 library postmessage_js_test; |
6 import '../../pkg/unittest/lib/unittest.dart'; | 6 import '../../pkg/unittest/lib/unittest.dart'; |
7 import '../../pkg/unittest/lib/html_config.dart'; | 7 import '../../pkg/unittest/lib/html_config.dart'; |
8 import 'dart:html'; | 8 import 'dart:html'; |
9 import 'dart:collection'; // SplayTreeMap | 9 import 'dart:collection'; // SplayTreeMap |
10 import 'utils.dart'; | 10 import 'utils.dart'; |
11 | 11 |
12 injectSource(code) { | 12 injectSource(code) { |
13 final script = new ScriptElement(); | 13 final script = new ScriptElement(); |
14 script.type = 'text/javascript'; | 14 script.type = 'text/javascript'; |
15 script.innerHtml = code; | 15 script.innerHtml = code; |
16 document.body.nodes.add(script); | 16 document.body.nodes.add(script); |
17 } | 17 } |
18 | 18 |
19 main() { | 19 main() { |
20 useHtmlConfiguration(); | 20 useHtmlConfiguration(); |
21 | 21 |
22 test('js-to-dart-postmessage', () { | 22 test('js-to-dart-postmessage', () { |
23 // Pass an object literal from JavaScript. It should be seen as a Dart | 23 // Pass an object literal from JavaScript. It should be seen as a Dart |
24 // Map. | 24 // Map. |
25 | 25 |
26 final JS_CODE = """ | 26 final JS_CODE = """ |
27 window.postMessage({eggs: 3}, '*'); | 27 window.postMessage({eggs: 3}, '*'); |
28 """; | 28 """; |
29 var callback; | 29 var completed = false; |
30 var onSuccess = expectAsync1((e) { | 30 var subscription = null; |
31 window.on.message.remove(callback); | 31 subscription = window.onMessage.listen(expectAsyncUntil1( |
32 }); | 32 (e) { |
33 callback = (e) { | 33 var data = e.data; |
34 guardAsync(() { | 34 if (data is String) return; // Messages from unit test protocol. |
35 var data = e.data; | 35 completed = true; |
36 if (data is String) return; // Messages from unit test protocol. | 36 subscription.cancel(); |
37 expect(data, isMap); | 37 expect(data, isMap); |
38 expect(data['eggs'], equals(3)); | 38 expect(data['eggs'], equals(3)); |
39 onSuccess(e); | 39 }, |
40 }); | 40 () => completed)); |
41 }; | |
42 window.on.message.add(callback); | |
43 injectSource(JS_CODE); | 41 injectSource(JS_CODE); |
44 }); | 42 }); |
45 | 43 |
46 test('dart-to-js-to-dart-postmessage', () { | 44 test('dart-to-js-to-dart-postmessage', () { |
47 // Pass dictionaries between Dart and JavaScript. | 45 // Pass dictionaries between Dart and JavaScript. |
48 | 46 |
49 final JS_CODE = """ | 47 final JS_CODE = """ |
50 window.addEventListener('message', handler); | 48 window.addEventListener('message', handler); |
51 function handler(e) { | 49 function handler(e) { |
52 var data = e.data; | 50 var data = e.data; |
53 if (typeof data == 'string') return; | 51 if (typeof data == 'string') return; |
54 if (data.recipient != 'JS') return; | 52 if (data.recipient != 'JS') return; |
55 var response = {recipient: 'DART'}; | 53 var response = {recipient: 'DART'}; |
56 response[data['curry']] = 50; | 54 response[data['curry']] = 50; |
57 window.removeEventListener('message', handler); | 55 window.removeEventListener('message', handler); |
58 window.postMessage(response, '*'); | 56 window.postMessage(response, '*'); |
59 } | 57 } |
60 """; | 58 """; |
61 var callback; | 59 var completed = false; |
62 var onSuccess = expectAsync1((e) { | 60 var subscription = null; |
63 window.on.message.remove(callback); | 61 subscription = window.onMessage.listen(expectAsyncUntil1( |
64 }); | 62 (e) { |
65 callback = (e) { | 63 var data = e.data; |
66 guardAsync(() { | 64 if (data is String) return; // Messages from unit test protocol. |
67 var data = e.data; | 65 if (data['recipient'] != 'DART') return; // Hearing the sent message. |
68 if (data is String) return; // Messages from unit test protocol. | 66 completed = true; |
69 expect(data, isMap); | 67 subscription.cancel(); |
70 if (data['recipient'] != 'DART') return; // Hearing the sent messag e. | 68 expect(data, isMap); |
71 expect(data['peas'], equals(50)); | 69 expect(data['peas'], equals(50)); |
72 onSuccess(e); | 70 }, |
73 }); | 71 () => completed)); |
74 }; | |
75 window.on.message.add(callback); | |
76 injectSource(JS_CODE); | 72 injectSource(JS_CODE); |
77 window.postMessage({'recipient': 'JS', 'curry': 'peas'}, '*'); | 73 window.postMessage({'recipient': 'JS', 'curry': 'peas'}, '*'); |
78 }); | 74 }); |
79 | 75 |
80 go(testName, value) => | 76 go(testName, value) => |
81 test(testName, () { | 77 test(testName, () { |
82 // Round-trip graph from Dart to JavaScript and back. | 78 // Round-trip graph from Dart to JavaScript and back. |
83 | 79 |
84 final JS_CODE = """ | 80 final JS_CODE = """ |
85 window.addEventListener('message', handler); | 81 window.addEventListener('message', handler); |
86 function handler(e) { | 82 function handler(e) { |
87 var data = e.data; | 83 var data = e.data; |
88 if (typeof data == 'string') return; | 84 if (typeof data == 'string') return; |
89 if (data.recipient != 'JS') return; | 85 if (data.recipient != 'JS') return; |
90 window.console.log(data.data); | 86 window.console.log(data.data); |
91 var response = {recipient: 'DART', data: data.data}; | 87 var response = {recipient: 'DART', data: data.data}; |
92 window.removeEventListener('message', handler); | 88 window.removeEventListener('message', handler); |
93 window.postMessage(response, '*'); | 89 window.postMessage(response, '*'); |
94 } | 90 } |
95 """; | 91 """; |
96 var onSuccess = expectAsync0(() {}); | 92 var completed = false; |
97 callback(e) { | 93 var subscription = null; |
98 guardAsync(() { | 94 subscription = window.onMessage.listen(expectAsyncUntil1( |
99 var data = e.data; | 95 (e) { |
100 if (data is String) return; // Messages from unit test protoc ol. | 96 var data = e.data; |
101 expect(data, isMap); | 97 if (data is String) return; // Messages from unit test protocol . |
Emily Fortuna
2013/01/28 23:15:18
delete one space here and you can get this line ba
| |
102 if (data['recipient'] != 'DART') return; // Not for me. | 98 if (data['recipient'] != 'DART') return; // Not for me. |
103 var returnedValue = data['data']; | 99 completed = true; |
104 | 100 subscription.cancel(); |
105 window.on.message.remove(callback); | 101 expect(data, isMap); |
106 expect(returnedValue, isNot(same(value))); | 102 var returnedValue = data['data']; |
107 verifyGraph(value, returnedValue); | 103 expect(returnedValue, isNot(same(value))); |
108 onSuccess(); | 104 verifyGraph(value, returnedValue); |
109 }); | 105 }, |
110 }; | 106 () => completed)); |
111 window.on.message.add(callback); | |
112 injectSource(JS_CODE); | 107 injectSource(JS_CODE); |
113 window.postMessage({'recipient': 'JS', 'data': value}, '*'); | 108 window.postMessage({'recipient': 'JS', 'data': value}, '*'); |
114 }); | 109 }); |
115 | 110 |
116 var obj1 = {'a': 100, 'b': 's'}; | 111 var obj1 = {'a': 100, 'b': 's'}; |
117 var obj2 = {'x': obj1, 'y': obj1}; // DAG. | 112 var obj2 = {'x': obj1, 'y': obj1}; // DAG. |
118 | 113 |
119 var obj3 = {}; | 114 var obj3 = {}; |
120 obj3['a'] = 100; | 115 obj3['a'] = 100; |
121 obj3['b'] = obj3; // Cycle. | 116 obj3['b'] = obj3; // Cycle. |
122 | 117 |
123 var obj4 = new SplayTreeMap<String, dynamic>(); // Different implementation. | 118 var obj4 = new SplayTreeMap<String, dynamic>(); // Different implementation. |
124 obj4['a'] = 100; | 119 obj4['a'] = 100; |
125 obj4['b'] = 's'; | 120 obj4['b'] = 's'; |
126 | 121 |
127 var cyclic_list = [1, 2, 3]; | 122 var cyclic_list = [1, 2, 3]; |
128 cyclic_list[1] = cyclic_list; | 123 cyclic_list[1] = cyclic_list; |
129 | 124 |
130 go('test_simple_list', [1, 2, 3]); | 125 go('test_simple_list', [1, 2, 3]); |
131 go('test_map', obj1); | 126 go('test_map', obj1); |
132 go('test_DAG', obj2); | 127 go('test_DAG', obj2); |
133 go('test_cycle', obj3); | 128 go('test_cycle', obj3); |
134 go('test_simple_splay', obj4); | 129 go('test_simple_splay', obj4); |
135 go('const_array_1', const [const [1], const [2]]); | 130 go('const_array_1', const [const [1], const [2]]); |
136 go('const_array_dag', const [const [1], const [1]]); | 131 go('const_array_dag', const [const [1], const [1]]); |
137 go('array_deferred_copy', [1,2,3, obj3, obj3, 6]); | 132 go('array_deferred_copy', [1,2,3, obj3, obj3, 6]); |
138 go('array_deferred_copy_2', [1,2,3, [4, 5, obj3], [obj3, 6]]); | 133 go('array_deferred_copy_2', [1,2,3, [4, 5, obj3], [obj3, 6]]); |
139 go('cyclic_list', cyclic_list); | 134 go('cyclic_list', cyclic_list); |
140 } | 135 } |
OLD | NEW |