| 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_individual_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 useHtmlIndividualConfiguration(); |
| 21 | 21 |
| 22 test('js-to-dart-postmessage', () { | 22 go(testName, value) => |
| 23 test(testName, () { |
| 24 // Round-trip graph from Dart to JavaScript and back. |
| 25 |
| 26 final JS_CODE = """ |
| 27 window.addEventListener('message', handler); |
| 28 function handler(e) { |
| 29 var data = e.data; |
| 30 if (typeof data == 'string') return; |
| 31 if (data.recipient != 'JS') return; |
| 32 window.console.log(data.data); |
| 33 var response = {recipient: 'DART', data: data.data}; |
| 34 window.removeEventListener('message', handler); |
| 35 window.postMessage(response, '*'); |
| 36 } |
| 37 """; |
| 38 var completed = false; |
| 39 var subscription = null; |
| 40 subscription = window.onMessage.listen(expectAsyncUntil1( |
| 41 (e) { |
| 42 var data = e.data; |
| 43 if (data is String) return; // Messages from unit test protocol. |
| 44 if (data['recipient'] != 'DART') return; // Not for me. |
| 45 completed = true; |
| 46 subscription.cancel(); |
| 47 expect(data, isMap); |
| 48 var returnedValue = data['data']; |
| 49 expect(returnedValue, isNot(same(value))); |
| 50 verifyGraph(value, returnedValue); |
| 51 }, |
| 52 () => completed)); |
| 53 injectSource(JS_CODE); |
| 54 window.postMessage({'recipient': 'JS', 'data': value}, '*'); |
| 55 }); |
| 56 |
| 57 group('primitives', () { |
| 58 test('js-to-dart-postmessage', () { |
| 23 // Pass an object literal from JavaScript. It should be seen as a Dart | 59 // Pass an object literal from JavaScript. It should be seen as a Dart |
| 24 // Map. | 60 // Map. |
| 25 | 61 |
| 26 final JS_CODE = """ | 62 final JS_CODE = """ |
| 27 window.postMessage({eggs: 3}, '*'); | 63 window.postMessage({eggs: 3}, '*'); |
| 28 """; | 64 """; |
| 29 var completed = false; | 65 var completed = false; |
| 30 var subscription = null; | 66 var subscription = null; |
| 31 subscription = window.onMessage.listen(expectAsyncUntil1( | 67 subscription = window.onMessage.listen(expectAsyncUntil1( |
| 32 (e) { | 68 (e) { |
| 33 var data = e.data; | 69 var data = e.data; |
| 34 if (data is String) return; // Messages from unit test protocol. | 70 if (data is String) return; // Messages from unit test protocol. |
| 35 completed = true; | 71 completed = true; |
| 36 subscription.cancel(); | 72 subscription.cancel(); |
| 37 expect(data, isMap); | 73 expect(data, isMap); |
| 38 expect(data['eggs'], equals(3)); | 74 expect(data['eggs'], equals(3)); |
| 39 }, | 75 }, |
| 40 () => completed)); | 76 () => completed)); |
| 41 injectSource(JS_CODE); | 77 injectSource(JS_CODE); |
| 42 }); | 78 }); |
| 43 | 79 |
| 44 test('dart-to-js-to-dart-postmessage', () { | 80 test('dart-to-js-to-dart-postmessage', () { |
| 45 // Pass dictionaries between Dart and JavaScript. | 81 // Pass dictionaries between Dart and JavaScript. |
| 46 | 82 |
| 47 final JS_CODE = """ | 83 final JS_CODE = """ |
| 48 window.addEventListener('message', handler); | 84 window.addEventListener('message', handler); |
| 49 function handler(e) { | 85 function handler(e) { |
| 50 var data = e.data; | 86 var data = e.data; |
| 51 if (typeof data == 'string') return; | 87 if (typeof data == 'string') return; |
| 52 if (data.recipient != 'JS') return; | 88 if (data.recipient != 'JS') return; |
| 53 var response = {recipient: 'DART'}; | 89 var response = {recipient: 'DART'}; |
| 54 response[data['curry']] = 50; | 90 response[data['curry']] = 50; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 66 completed = true; | 102 completed = true; |
| 67 subscription.cancel(); | 103 subscription.cancel(); |
| 68 expect(data, isMap); | 104 expect(data, isMap); |
| 69 expect(data['peas'], equals(50)); | 105 expect(data['peas'], equals(50)); |
| 70 }, | 106 }, |
| 71 () => completed)); | 107 () => completed)); |
| 72 injectSource(JS_CODE); | 108 injectSource(JS_CODE); |
| 73 window.postMessage({'recipient': 'JS', 'curry': 'peas'}, '*'); | 109 window.postMessage({'recipient': 'JS', 'curry': 'peas'}, '*'); |
| 74 }); | 110 }); |
| 75 | 111 |
| 76 go(testName, value) => | 112 var obj1 = {'a': 100, 'b': 's'}; |
| 77 test(testName, () { | 113 var obj2 = {'x': obj1, 'y': obj1}; // DAG. |
| 78 // Round-trip graph from Dart to JavaScript and back. | |
| 79 | 114 |
| 80 final JS_CODE = """ | 115 var obj3 = {}; |
| 81 window.addEventListener('message', handler); | 116 obj3['a'] = 100; |
| 82 function handler(e) { | 117 obj3['b'] = obj3; // Cycle. |
| 83 var data = e.data; | |
| 84 if (typeof data == 'string') return; | |
| 85 if (data.recipient != 'JS') return; | |
| 86 window.console.log(data.data); | |
| 87 var response = {recipient: 'DART', data: data.data}; | |
| 88 window.removeEventListener('message', handler); | |
| 89 window.postMessage(response, '*'); | |
| 90 } | |
| 91 """; | |
| 92 var completed = false; | |
| 93 var subscription = null; | |
| 94 subscription = window.onMessage.listen(expectAsyncUntil1( | |
| 95 (e) { | |
| 96 var data = e.data; | |
| 97 if (data is String) return; // Messages from unit test protocol. | |
| 98 if (data['recipient'] != 'DART') return; // Not for me. | |
| 99 completed = true; | |
| 100 subscription.cancel(); | |
| 101 expect(data, isMap); | |
| 102 var returnedValue = data['data']; | |
| 103 expect(returnedValue, isNot(same(value))); | |
| 104 verifyGraph(value, returnedValue); | |
| 105 }, | |
| 106 () => completed)); | |
| 107 injectSource(JS_CODE); | |
| 108 window.postMessage({'recipient': 'JS', 'data': value}, '*'); | |
| 109 }); | |
| 110 | 118 |
| 111 var obj1 = {'a': 100, 'b': 's'}; | 119 var obj4 = new SplayTreeMap<String, dynamic>(); // Different implementation
. |
| 112 var obj2 = {'x': obj1, 'y': obj1}; // DAG. | 120 obj4['a'] = 100; |
| 121 obj4['b'] = 's'; |
| 113 | 122 |
| 114 var obj3 = {}; | 123 var cyclic_list = [1, 2, 3]; |
| 115 obj3['a'] = 100; | 124 cyclic_list[1] = cyclic_list; |
| 116 obj3['b'] = obj3; // Cycle. | |
| 117 | 125 |
| 118 var obj4 = new SplayTreeMap<String, dynamic>(); // Different implementation. | 126 go('test_simple_list', [1, 2, 3]); |
| 119 obj4['a'] = 100; | 127 go('test_map', obj1); |
| 120 obj4['b'] = 's'; | 128 go('test_DAG', obj2); |
| 129 go('test_cycle', obj3); |
| 130 go('test_simple_splay', obj4); |
| 131 go('const_array_1', const [const [1], const [2]]); |
| 132 go('const_array_dag', const [const [1], const [1]]); |
| 133 go('array_deferred_copy', [1,2,3, obj3, obj3, 6]); |
| 134 go('array_deferred_copy_2', [1,2,3, [4, 5, obj3], [obj3, 6]]); |
| 135 go('cyclic_list', cyclic_list); |
| 136 }); |
| 121 | 137 |
| 122 var cyclic_list = [1, 2, 3]; | 138 group('typed_arrays', () { |
| 123 cyclic_list[1] = cyclic_list; | 139 var array_buffer = new ArrayBuffer(16); |
| 140 var view_a = new Float32Array.fromBuffer(array_buffer, 0, 4); |
| 141 var view_b = new Uint8Array.fromBuffer(array_buffer, 1, 13); |
| 142 var typed_arrays_list = [view_a, array_buffer, view_b]; |
| 124 | 143 |
| 125 var array_buffer = new ArrayBuffer(16); | 144 // Note that FF is failing this test because in the sent message: |
| 126 var view_a = new Float32Array.fromBuffer(array_buffer, 0, 4); | 145 // view_a.buffer == array_buffer |
| 127 var view_b = new Uint8Array.fromBuffer(array_buffer, 1, 13); | 146 // But in the response: |
| 128 var typed_arrays_list = [view_a, array_buffer, view_b]; | 147 // view_a.buffer != array_buffer |
| 148 go('typed_arrays_list', typed_arrays_list); |
| 149 }); |
| 129 | 150 |
| 130 go('test_simple_list', [1, 2, 3]); | |
| 131 go('test_map', obj1); | |
| 132 go('test_DAG', obj2); | |
| 133 go('test_cycle', obj3); | |
| 134 go('test_simple_splay', obj4); | |
| 135 go('const_array_1', const [const [1], const [2]]); | |
| 136 go('const_array_dag', const [const [1], const [1]]); | |
| 137 go('array_deferred_copy', [1,2,3, obj3, obj3, 6]); | |
| 138 go('array_deferred_copy_2', [1,2,3, [4, 5, obj3], [obj3, 6]]); | |
| 139 go('cyclic_list', cyclic_list); | |
| 140 go('typed_arrays_list', typed_arrays_list); | |
| 141 } | 151 } |
| OLD | NEW |