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 |
6 import 'package:unittest/unittest.dart'; | 7 import 'package:unittest/unittest.dart'; |
7 import 'package:unittest/html_individual_config.dart'; | 8 import 'package:unittest/html_individual_config.dart'; |
8 import 'dart:html'; | 9 import 'dart:html'; |
9 import 'dart:collection'; // SplayTreeMap | 10 import 'dart:collection'; // SplayTreeMap |
10 import 'dart:typed_data'; | 11 import 'dart:typed_data'; |
11 import 'utils.dart'; | 12 import 'utils.dart'; |
12 | 13 |
13 injectSource(code) { | 14 injectSource(code) { |
14 final script = new ScriptElement(); | 15 final script = new ScriptElement(); |
15 script.type = 'text/javascript'; | 16 script.type = 'text/javascript'; |
16 script.innerHtml = code; | 17 script.innerHtml = code; |
17 document.body.append(script); | 18 document.body.append(script); |
18 } | 19 } |
19 | 20 |
20 main() { | 21 main() { |
21 useHtmlIndividualConfiguration(); | 22 useHtmlIndividualConfiguration(); |
22 | 23 |
23 go(testName, value) => | 24 go(testName, value) => test(testName, () { |
24 test(testName, () { | 25 // Round-trip graph from Dart to JavaScript and back. |
25 // Round-trip graph from Dart to JavaScript and back. | |
26 | 26 |
27 final JS_CODE = """ | 27 final JS_CODE = """ |
28 window.addEventListener('message', handler); | 28 window.addEventListener('message', handler); |
29 function handler(e) { | 29 function handler(e) { |
30 var data = e.data; | 30 var data = e.data; |
31 if (typeof data == 'string') return; | 31 if (typeof data == 'string') return; |
32 if (data.recipient != 'JS') return; | 32 if (data.recipient != 'JS') return; |
33 window.console.log(data.data); | 33 window.console.log(data.data); |
34 var response = {recipient: 'DART', data: data.data}; | 34 var response = {recipient: 'DART', data: data.data}; |
35 window.removeEventListener('message', handler); | 35 window.removeEventListener('message', handler); |
36 window.postMessage(response, '*'); | 36 window.postMessage(response, '*'); |
37 } | 37 } |
38 """; | 38 """; |
39 var completed = false; | 39 var completed = false; |
40 var subscription = null; | 40 var subscription = null; |
41 subscription = window.onMessage.listen(expectAsyncUntil( | 41 subscription = window.onMessage.listen(expectAsyncUntil((e) { |
42 (e) { | 42 var data = e.data; |
43 var data = e.data; | 43 if (data is String) return; // Messages from unit test protocol. |
44 if (data is String) return; // Messages from unit test protocol. | 44 if (data['recipient'] != 'DART') return; // Not for me. |
45 if (data['recipient'] != 'DART') return; // Not for me. | 45 completed = true; |
46 completed = true; | 46 subscription.cancel(); |
47 subscription.cancel(); | 47 expect(data, isMap); |
48 expect(data, isMap); | 48 var returnedValue = data['data']; |
49 var returnedValue = data['data']; | 49 expect(returnedValue, isNot(same(value))); |
50 expect(returnedValue, isNot(same(value))); | 50 verifyGraph(value, returnedValue); |
51 verifyGraph(value, returnedValue); | 51 }, () => completed)); |
52 }, | 52 injectSource(JS_CODE); |
53 () => completed)); | 53 window.postMessage({'recipient': 'JS', 'data': value}, '*'); |
54 injectSource(JS_CODE); | 54 }); |
55 window.postMessage({'recipient': 'JS', 'data': value}, '*'); | |
56 }); | |
57 | 55 |
58 group('primitives', () { | 56 group('primitives', () { |
59 test('js-to-dart-postmessage', () { | 57 test('js-to-dart-postmessage', () { |
60 // Pass an object literal from JavaScript. It should be seen as a Dart | 58 // Pass an object literal from JavaScript. It should be seen as a Dart |
61 // Map. | 59 // Map. |
62 | 60 |
63 final JS_CODE = """ | 61 final JS_CODE = """ |
64 window.postMessage({eggs: 3}, '*'); | 62 window.postMessage({eggs: 3}, '*'); |
65 """; | 63 """; |
66 var completed = false; | 64 var completed = false; |
67 var subscription = null; | 65 var subscription = null; |
68 subscription = window.onMessage.listen(expectAsyncUntil( | 66 subscription = window.onMessage.listen(expectAsyncUntil((e) { |
69 (e) { | 67 var data = e.data; |
70 var data = e.data; | 68 if (data is String) return; // Messages from unit test protocol. |
71 if (data is String) return; // Messages from unit test protocol. | 69 completed = true; |
72 completed = true; | 70 subscription.cancel(); |
73 subscription.cancel(); | 71 expect(data, isMap); |
74 expect(data, isMap); | 72 expect(data['eggs'], equals(3)); |
75 expect(data['eggs'], equals(3)); | 73 }, () => completed)); |
76 }, | |
77 () => completed)); | |
78 injectSource(JS_CODE); | 74 injectSource(JS_CODE); |
79 }); | 75 }); |
80 | 76 |
81 test('dart-to-js-to-dart-postmessage', () { | 77 test('dart-to-js-to-dart-postmessage', () { |
82 // Pass dictionaries between Dart and JavaScript. | 78 // Pass dictionaries between Dart and JavaScript. |
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 var response = {recipient: 'DART'}; | 86 var response = {recipient: 'DART'}; |
91 response[data['curry']] = 50; | 87 response[data['curry']] = 50; |
92 window.removeEventListener('message', handler); | 88 window.removeEventListener('message', handler); |
93 window.postMessage(response, '*'); | 89 window.postMessage(response, '*'); |
94 } | 90 } |
95 """; | 91 """; |
96 var completed = false; | 92 var completed = false; |
97 var subscription = null; | 93 var subscription = null; |
98 subscription = window.onMessage.listen(expectAsyncUntil( | 94 subscription = window.onMessage.listen(expectAsyncUntil((e) { |
99 (e) { | 95 var data = e.data; |
100 var data = e.data; | 96 if (data is String) return; // Messages from unit test protocol. |
101 if (data is String) return; // Messages from unit test protocol. | 97 if (data['recipient'] != 'DART') return; // Hearing the sent message. |
102 if (data['recipient'] != 'DART') return; // Hearing the sent message. | 98 completed = true; |
103 completed = true; | 99 subscription.cancel(); |
104 subscription.cancel(); | 100 expect(data, isMap); |
105 expect(data, isMap); | 101 expect(data['peas'], equals(50)); |
106 expect(data['peas'], equals(50)); | 102 }, () => completed)); |
107 }, | |
108 () => completed)); | |
109 injectSource(JS_CODE); | 103 injectSource(JS_CODE); |
110 window.postMessage({'recipient': 'JS', 'curry': 'peas'}, '*'); | 104 window.postMessage({'recipient': 'JS', 'curry': 'peas'}, '*'); |
111 }); | 105 }); |
112 | 106 |
113 var obj1 = {'a': 100, 'b': 's'}; | 107 var obj1 = {'a': 100, 'b': 's'}; |
114 var obj2 = {'x': obj1, 'y': obj1}; // DAG. | 108 var obj2 = {'x': obj1, 'y': obj1}; // DAG. |
115 | 109 |
116 var obj3 = {}; | 110 var obj3 = {}; |
117 obj3['a'] = 100; | 111 obj3['a'] = 100; |
118 obj3['b'] = obj3; // Cycle. | 112 obj3['b'] = obj3; // Cycle. |
119 | 113 |
120 var obj4 = new SplayTreeMap<String, dynamic>(); // Different implementation
. | 114 var obj4 = new SplayTreeMap<String, dynamic>(); // Different implementation. |
121 obj4['a'] = 100; | 115 obj4['a'] = 100; |
122 obj4['b'] = 's'; | 116 obj4['b'] = 's'; |
123 | 117 |
124 var cyclic_list = [1, 2, 3]; | 118 var cyclic_list = [1, 2, 3]; |
125 cyclic_list[1] = cyclic_list; | 119 cyclic_list[1] = cyclic_list; |
126 | 120 |
127 go('test_simple_list', [1, 2, 3]); | 121 go('test_simple_list', [1, 2, 3]); |
128 go('test_map', obj1); | 122 go('test_map', obj1); |
129 go('test_DAG', obj2); | 123 go('test_DAG', obj2); |
130 go('test_cycle', obj3); | 124 go('test_cycle', obj3); |
131 go('test_simple_splay', obj4); | 125 go('test_simple_splay', obj4); |
132 go('const_array_1', const [const [1], const [2]]); | 126 go('const_array_1', const [ |
133 go('const_array_dag', const [const [1], const [1]]); | 127 const [1], |
134 go('array_deferred_copy', [1,2,3, obj3, obj3, 6]); | 128 const [2] |
135 go('array_deferred_copy_2', [1,2,3, [4, 5, obj3], [obj3, 6]]); | 129 ]); |
| 130 go('const_array_dag', const [ |
| 131 const [1], |
| 132 const [1] |
| 133 ]); |
| 134 go('array_deferred_copy', [1, 2, 3, obj3, obj3, 6]); |
| 135 go('array_deferred_copy_2', [ |
| 136 1, |
| 137 2, |
| 138 3, |
| 139 [4, 5, obj3], |
| 140 [obj3, 6] |
| 141 ]); |
136 go('cyclic_list', cyclic_list); | 142 go('cyclic_list', cyclic_list); |
137 }); | 143 }); |
138 | 144 |
139 group('more_primitives', () { | 145 group('more_primitives', () { |
140 test('js-to-dart-null-prototype-eventdata', () { | 146 test('js-to-dart-null-prototype-eventdata', () { |
141 // Pass an object with a null prototype from JavaScript. | 147 // Pass an object with a null prototype from JavaScript. |
142 // It should be seen as a Dart Map. | 148 // It should be seen as a Dart Map. |
143 final JS_CODE = """ | 149 final JS_CODE = """ |
144 // Call anonymous function to create a local scope. | 150 // Call anonymous function to create a local scope. |
145 (function() { | 151 (function() { |
146 var o = Object.create(null); | 152 var o = Object.create(null); |
147 o.eggs = 3; | 153 o.eggs = 3; |
148 var foo = new MessageEvent('stuff', {data: o}); | 154 var foo = new MessageEvent('stuff', {data: o}); |
149 window.dispatchEvent(foo); | 155 window.dispatchEvent(foo); |
150 })(); | 156 })(); |
151 """; | 157 """; |
152 var completed = false; | 158 var completed = false; |
153 var subscription = null; | 159 var subscription = null; |
154 subscription = window.on['stuff'].listen(expectAsyncUntil( | 160 subscription = |
155 (MessageEvent e) { | 161 window.on['stuff'].listen(expectAsyncUntil((MessageEvent e) { |
156 var data = e.data; | 162 var data = e.data; |
157 if (data is String) return; // Messages from unit test protocol. | 163 if (data is String) return; // Messages from unit test protocol. |
158 completed = true; | 164 completed = true; |
159 subscription.cancel(); | 165 subscription.cancel(); |
160 expect(data, isMap); | 166 expect(data, isMap); |
161 expect(data['eggs'], equals(3)); | 167 expect(data['eggs'], equals(3)); |
162 }, | 168 }, () => completed)); |
163 () => completed)); | |
164 injectSource(JS_CODE); | 169 injectSource(JS_CODE); |
165 }); | 170 }); |
166 }); | 171 }); |
167 | 172 |
168 group('typed_arrays', () { | 173 group('typed_arrays', () { |
169 var array_buffer = new Uint8List(16).buffer; | 174 var array_buffer = new Uint8List(16).buffer; |
170 var view_a = new Float32List.view(array_buffer, 0, 4); | 175 var view_a = new Float32List.view(array_buffer, 0, 4); |
171 var view_b = new Uint8List.view(array_buffer, 1, 13); | 176 var view_b = new Uint8List.view(array_buffer, 1, 13); |
172 var typed_arrays_list = [view_a, array_buffer, view_b]; | 177 var typed_arrays_list = [view_a, array_buffer, view_b]; |
173 | 178 |
174 // Note that FF is failing this test because in the sent message: | 179 // Note that FF is failing this test because in the sent message: |
175 // view_a.buffer == array_buffer | 180 // view_a.buffer == array_buffer |
176 // But in the response: | 181 // But in the response: |
177 // view_a.buffer != array_buffer | 182 // view_a.buffer != array_buffer |
178 go('typed_arrays_list', typed_arrays_list); | 183 go('typed_arrays_list', typed_arrays_list); |
179 }); | 184 }); |
180 | 185 |
181 group('iframe', () { | 186 group('iframe', () { |
182 test('postMessage clones data', () { | 187 test('postMessage clones data', () { |
183 var iframe = new IFrameElement(); | 188 var iframe = new IFrameElement(); |
184 var future = iframe.onLoad.first.then((_) { | 189 var future = iframe.onLoad.first.then((_) { |
185 iframe.contentWindow.postMessage(new HashMap<String,num>(), '*'); | 190 iframe.contentWindow.postMessage(new HashMap<String, num>(), '*'); |
186 }); | 191 }); |
187 iframe.src = 'about:blank'; | 192 iframe.src = 'about:blank'; |
188 document.body.append(iframe); | 193 document.body.append(iframe); |
189 | 194 |
190 return future; | 195 return future; |
191 }); | 196 }); |
192 }); | 197 }); |
193 | |
194 } | 198 } |
OLD | NEW |