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