b101a7d002
Change-Id: Ib33169c3e0ffc870915c189404074a1dea472546 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/196548 Reviewed-by: Bob Nystrom <rnystrom@google.com> Commit-Queue: Leaf Petersen <leafp@google.com>
56 lines
1.4 KiB
Dart
56 lines
1.4 KiB
Dart
// Copyright (c) 2019, 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.
|
|
|
|
// @dart = 2.9
|
|
|
|
@JS()
|
|
library postmessage_anonymous_test;
|
|
|
|
import 'dart:async';
|
|
import 'dart:html';
|
|
import 'package:expect/expect.dart';
|
|
import 'package:js/js.dart';
|
|
|
|
const String 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', msg: data.msg};
|
|
window.removeEventListener('message', handler);
|
|
window.postMessage(response, '*');
|
|
}
|
|
""";
|
|
|
|
const String TEST_MSG = "hello world";
|
|
|
|
@JS()
|
|
@anonymous
|
|
class Message {
|
|
external String get recipient;
|
|
external String get msg;
|
|
external factory Message({String recipient, String msg});
|
|
}
|
|
|
|
main() {
|
|
var subscription;
|
|
subscription = window.onMessage.listen((e) {
|
|
var data = e.data;
|
|
if (data is String) return;
|
|
if (data['recipient'] != 'DART') return;
|
|
subscription.cancel();
|
|
Expect.equals(TEST_MSG, data['msg']);
|
|
});
|
|
injectSource(JS_CODE);
|
|
window.postMessage(Message(recipient: 'JS', msg: TEST_MSG), '*');
|
|
}
|
|
|
|
void injectSource(String code) {
|
|
final script = new ScriptElement();
|
|
script.type = 'text/javascript';
|
|
script.innerHtml = code;
|
|
document.body.append(script);
|
|
}
|