Index: tests/html/postmessage_structured_test.dart |
diff --git a/tests/html/postmessage_structured_test.dart b/tests/html/postmessage_structured_test.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..1a043c503c32893f9faec4709684f32aa51ed6a6 |
--- /dev/null |
+++ b/tests/html/postmessage_structured_test.dart |
@@ -0,0 +1,75 @@ |
+// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
+// for details. All rights reserved. Use of this source code is governed by a |
+// BSD-style license that can be found in the LICENSE file |
+ |
+#library('postmessage_js_test'); |
+#import('../../pkg/unittest/unittest.dart'); |
+#import('../../pkg/unittest/html_config.dart'); |
+#import('dart:html'); |
+ |
+injectSource(code) { |
+ final script = new ScriptElement(); |
+ script.type = 'text/javascript'; |
+ script.innerHTML = code; |
+ document.body.nodes.add(script); |
+} |
+ |
+ |
+main() { |
+ useHtmlConfiguration(); |
+ |
+ test('js-to-dart-postmessage', () { |
+ // Pass an object literal from JavaScript. It should be seen as a Dart |
+ // Map. |
+ |
+ final JS_CODE = """ |
+ window.postMessage({eggs: 3, recipient: 33}, '*'); |
+ """; |
+ var callback; |
+ var onSuccess = expectAsync1((e) { |
+ window.on.message.remove(callback); |
+ }); |
+ callback = (e) { |
vsm
2012/10/04 16:03:35
I think you want to guardAsync this code.
|
+ var data = e.data; |
+ if (data is String) return; // Messages from unit test protocol. |
+ expect(data is Map); |
+ expect(data['eggs'], equals(3)); |
+ onSuccess(e); |
+ }; |
+ window.on.message.add(callback); |
+ injectSource(JS_CODE); |
+ }); |
vsm
2012/10/04 16:03:35
Are cycles supported in the data? If so, perhaps
|
+ |
+ |
+ test('dart-to-js-to-dart-postmessage', () { |
+ // Pass dictionaries between Dart and JavaScript. |
+ |
+ final JS_CODE = """ |
+ window.addEventListener('message', handler); |
+ function handler(e) { |
+ var data = e.data; |
+ if (typeof data == 'string') return; |
+ if (data.recipient != 'JS') return |
+ var response = {recipient: 'DART'}; |
+ response[data['curry']] = 50; |
+ window.removeEventListener('message', handler); |
+ window.postMessage(response, '*'); |
+ } |
+ """; |
+ var callback; |
+ var onSuccess = expectAsync1((e) { |
+ window.on.message.remove(callback); |
+ }); |
+ callback = (e) { |
vsm
2012/10/04 16:03:35
ditto on guardAsync
|
+ var data = e.data; |
+ if (data is String) return; // Messages from unit test protocol. |
+ expect(data is Map); |
+ if (data['recipient'] != 'DART') return; // Hearing the sent message. |
+ expect(data['peas'], equals(50)); |
+ onSuccess(e); |
+ }; |
+ window.on.message.add(callback); |
+ injectSource(JS_CODE); |
+ window.postMessage({'recipient': 'JS', 'curry': 'peas'}, '*'); |
+ }); |
+} |