| 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/unittest.dart'); | 6 #import('../../pkg/unittest/unittest.dart'); |
| 7 #import('../../pkg/unittest/html_config.dart'); | 7 #import('../../pkg/unittest/html_config.dart'); |
| 8 #import('dart:html'); | 8 #import('dart:html'); |
| 9 #import('dart:coreimpl'); // SplayTreeMap | 9 #import('dart:coreimpl'); // 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 var isMap = new isInstanceOf<Map>('Map'); |
| 23 |
| 22 test('js-to-dart-postmessage', () { | 24 test('js-to-dart-postmessage', () { |
| 23 // Pass an object literal from JavaScript. It should be seen as a Dart | 25 // Pass an object literal from JavaScript. It should be seen as a Dart |
| 24 // Map. | 26 // Map. |
| 25 | 27 |
| 26 final JS_CODE = """ | 28 final JS_CODE = """ |
| 27 window.postMessage({eggs: 3}, '*'); | 29 window.postMessage({eggs: 3}, '*'); |
| 28 """; | 30 """; |
| 29 var callback; | 31 var callback; |
| 30 var onSuccess = expectAsync1((e) { | 32 var onSuccess = expectAsync1((e) { |
| 31 window.on.message.remove(callback); | 33 window.on.message.remove(callback); |
| 32 }); | 34 }); |
| 33 callback = (e) { | 35 callback = (e) { |
| 34 guardAsync(() { | 36 guardAsync(() { |
| 35 var data = e.data; | 37 var data = e.data; |
| 36 if (data is String) return; // Messages from unit test protocol. | 38 if (data is String) return; // Messages from unit test protocol. |
| 37 expect(data is Map); | 39 expect(data, isMap); |
| 38 expect(data['eggs'], equals(3)); | 40 expect(data['eggs'], equals(3)); |
| 39 onSuccess(e); | 41 onSuccess(e); |
| 40 }); | 42 }); |
| 41 }; | 43 }; |
| 42 window.on.message.add(callback); | 44 window.on.message.add(callback); |
| 43 injectSource(JS_CODE); | 45 injectSource(JS_CODE); |
| 44 }); | 46 }); |
| 45 | 47 |
| 46 test('dart-to-js-to-dart-postmessage', () { | 48 test('dart-to-js-to-dart-postmessage', () { |
| 47 // Pass dictionaries between Dart and JavaScript. | 49 // Pass dictionaries between Dart and JavaScript. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 59 } | 61 } |
| 60 """; | 62 """; |
| 61 var callback; | 63 var callback; |
| 62 var onSuccess = expectAsync1((e) { | 64 var onSuccess = expectAsync1((e) { |
| 63 window.on.message.remove(callback); | 65 window.on.message.remove(callback); |
| 64 }); | 66 }); |
| 65 callback = (e) { | 67 callback = (e) { |
| 66 guardAsync(() { | 68 guardAsync(() { |
| 67 var data = e.data; | 69 var data = e.data; |
| 68 if (data is String) return; // Messages from unit test protocol. | 70 if (data is String) return; // Messages from unit test protocol. |
| 69 expect(data is Map); | 71 expect(data, isMap); |
| 70 if (data['recipient'] != 'DART') return; // Hearing the sent messag
e. | 72 if (data['recipient'] != 'DART') return; // Hearing the sent messag
e. |
| 71 expect(data['peas'], equals(50)); | 73 expect(data['peas'], equals(50)); |
| 72 onSuccess(e); | 74 onSuccess(e); |
| 73 }); | 75 }); |
| 74 }; | 76 }; |
| 75 window.on.message.add(callback); | 77 window.on.message.add(callback); |
| 76 injectSource(JS_CODE); | 78 injectSource(JS_CODE); |
| 77 window.postMessage({'recipient': 'JS', 'curry': 'peas'}, '*'); | 79 window.postMessage({'recipient': 'JS', 'curry': 'peas'}, '*'); |
| 78 }); | 80 }); |
| 79 | 81 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 91 var response = {recipient: 'DART', data: data.data}; | 93 var response = {recipient: 'DART', data: data.data}; |
| 92 window.removeEventListener('message', handler); | 94 window.removeEventListener('message', handler); |
| 93 window.postMessage(response, '*'); | 95 window.postMessage(response, '*'); |
| 94 } | 96 } |
| 95 """; | 97 """; |
| 96 var onSuccess = expectAsync0(() {}); | 98 var onSuccess = expectAsync0(() {}); |
| 97 callback(e) { | 99 callback(e) { |
| 98 guardAsync(() { | 100 guardAsync(() { |
| 99 var data = e.data; | 101 var data = e.data; |
| 100 if (data is String) return; // Messages from unit test protoc
ol. | 102 if (data is String) return; // Messages from unit test protoc
ol. |
| 101 expect(data is Map); | 103 expect(data, isMap); |
| 102 if (data['recipient'] != 'DART') return; // Not for me. | 104 if (data['recipient'] != 'DART') return; // Not for me. |
| 103 var returnedValue = data['data']; | 105 var returnedValue = data['data']; |
| 104 | 106 |
| 105 window.on.message.remove(callback); | 107 window.on.message.remove(callback); |
| 106 expect(returnedValue, isNot(same(value))); | 108 expect(returnedValue, isNot(same(value))); |
| 107 verifyGraph(value, returnedValue); | 109 verifyGraph(value, returnedValue); |
| 108 onSuccess(); | 110 onSuccess(); |
| 109 }); | 111 }); |
| 110 }; | 112 }; |
| 111 window.on.message.add(callback); | 113 window.on.message.add(callback); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 131 go('test_map', obj1); | 133 go('test_map', obj1); |
| 132 go('test_DAG', obj2); | 134 go('test_DAG', obj2); |
| 133 go('test_cycle', obj3); | 135 go('test_cycle', obj3); |
| 134 go('test_simple_splay', obj4); | 136 go('test_simple_splay', obj4); |
| 135 go('const_array_1', const [const [1], const [2]]); | 137 go('const_array_1', const [const [1], const [2]]); |
| 136 go('const_array_dag', const [const [1], const [1]]); | 138 go('const_array_dag', const [const [1], const [1]]); |
| 137 go('array_deferred_copy', [1,2,3, obj3, obj3, 6]); | 139 go('array_deferred_copy', [1,2,3, obj3, obj3, 6]); |
| 138 go('array_deferred_copy_2', [1,2,3, [4, 5, obj3], [obj3, 6]]); | 140 go('array_deferred_copy_2', [1,2,3, [4, 5, obj3], [obj3, 6]]); |
| 139 go('cyclic_list', cyclic_list); | 141 go('cyclic_list', cyclic_list); |
| 140 } | 142 } |
| OLD | NEW |