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 #import('dart:coreimpl'); // SplayTreeMap |
| 10 #import('utils.dart'); |
| 11 |
| 12 injectSource(code) { |
| 13 final script = new ScriptElement(); |
| 14 script.type = 'text/javascript'; |
| 15 script.innerHTML = code; |
| 16 document.body.nodes.add(script); |
| 17 } |
| 18 |
| 19 main() { |
| 20 useHtmlConfiguration(); |
| 21 |
| 22 test('js-to-dart-postmessage', () { |
| 23 // Pass an object literal from JavaScript. It should be seen as a Dart |
| 24 // Map. |
| 25 |
| 26 final JS_CODE = """ |
| 27 window.postMessage({eggs: 3}, '*'); |
| 28 """; |
| 29 var callback; |
| 30 var onSuccess = expectAsync1((e) { |
| 31 window.on.message.remove(callback); |
| 32 }); |
| 33 callback = (e) { |
| 34 guardAsync(() { |
| 35 var data = e.data; |
| 36 if (data is String) return; // Messages from unit test protocol. |
| 37 expect(data is Map); |
| 38 expect(data['eggs'], equals(3)); |
| 39 onSuccess(e); |
| 40 }); |
| 41 }; |
| 42 window.on.message.add(callback); |
| 43 injectSource(JS_CODE); |
| 44 }); |
| 45 |
| 46 test('dart-to-js-to-dart-postmessage', () { |
| 47 // Pass dictionaries between Dart and JavaScript. |
| 48 |
| 49 final JS_CODE = """ |
| 50 window.addEventListener('message', handler); |
| 51 function handler(e) { |
| 52 var data = e.data; |
| 53 if (typeof data == 'string') return; |
| 54 if (data.recipient != 'JS') return; |
| 55 var response = {recipient: 'DART'}; |
| 56 response[data['curry']] = 50; |
| 57 window.removeEventListener('message', handler); |
| 58 window.postMessage(response, '*'); |
| 59 } |
| 60 """; |
| 61 var callback; |
| 62 var onSuccess = expectAsync1((e) { |
| 63 window.on.message.remove(callback); |
| 64 }); |
| 65 callback = (e) { |
| 66 guardAsync(() { |
| 67 var data = e.data; |
| 68 if (data is String) return; // Messages from unit test protocol. |
| 69 expect(data is Map); |
| 70 if (data['recipient'] != 'DART') return; // Hearing the sent messag
e. |
| 71 expect(data['peas'], equals(50)); |
| 72 onSuccess(e); |
| 73 }); |
| 74 }; |
| 75 window.on.message.add(callback); |
| 76 injectSource(JS_CODE); |
| 77 window.postMessage({'recipient': 'JS', 'curry': 'peas'}, '*'); |
| 78 }); |
| 79 |
| 80 go(testName, value) => |
| 81 test(testName, () { |
| 82 // Round-trip graph from Dart to JavaScript and back. |
| 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 window.console.log(data.data); |
| 91 var response = {recipient: 'DART', data: data.data}; |
| 92 window.removeEventListener('message', handler); |
| 93 window.postMessage(response, '*'); |
| 94 } |
| 95 """; |
| 96 var onSuccess = expectAsync0(() {}); |
| 97 callback(e) { |
| 98 guardAsync(() { |
| 99 var data = e.data; |
| 100 if (data is String) return; // Messages from unit test protoc
ol. |
| 101 expect(data is Map); |
| 102 if (data['recipient'] != 'DART') return; // Not for me. |
| 103 var returnedValue = data['data']; |
| 104 |
| 105 window.on.message.remove(callback); |
| 106 expect(returnedValue, isNot(same(value))); |
| 107 verifyGraph(value, returnedValue); |
| 108 onSuccess(); |
| 109 }); |
| 110 }; |
| 111 window.on.message.add(callback); |
| 112 injectSource(JS_CODE); |
| 113 window.postMessage({'recipient': 'JS', 'data': value}, '*'); |
| 114 }); |
| 115 |
| 116 var obj1 = {'a': 100, 'b': 's'}; |
| 117 var obj2 = {'x': obj1, 'y': obj1}; // DAG. |
| 118 |
| 119 var obj3 = {}; |
| 120 obj3['a'] = 100; |
| 121 obj3['b'] = obj3; // Cycle. |
| 122 |
| 123 var obj4 = new SplayTreeMap<String, Dynamic>(); // Different implementation. |
| 124 obj4['a'] = 100; |
| 125 obj4['b'] = 's'; |
| 126 |
| 127 var cyclic_list = [1, 2, 3]; |
| 128 cyclic_list[1] = cyclic_list; |
| 129 |
| 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 } |
OLD | NEW |