Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkgs/test/lib/dart.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ window.onload = function() {
// This mimics a MultiChannel-formatted message.
var sendLoadException = function(message) {
window.parent.postMessage({
// TODO: https://github.com/dart-lang/test/issues/2065 - remove href
"href": window.location.href,
"data": [0, {"type": "loadException", "message": message}],
"exception": true,
Expand Down
11 changes: 11 additions & 0 deletions pkgs/test/lib/src/runner/browser/dom.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:js/js.dart';
class Window extends EventTarget {}

extension WindowExtension on Window {
external Window get parent;
external Location get location;
CSSStyleDeclaration? getComputedStyle(Element elt, [String? pseudoElt]) =>
js_util.callMethod(this, 'getComputedStyle', <Object>[
Expand Down Expand Up @@ -135,8 +136,18 @@ extension MessageEventExtension on MessageEvent {
external String get origin;
List<MessagePort> get ports =>
js_util.getProperty<List>(this, 'ports').cast<MessagePort>();

/// The source may be a `WindowProxy`, a `MessagePort`, or a `ServiceWorker`.
///
/// When a message is sent from an iframe through `window.parent.postMessage`
/// the source will be a `WindowProxy` which has the same methods as [Window].
external MessageEventSource source;
}

@JS()
@staticInterop
class MessageEventSource {}

@JS()
@staticInterop
class Location {}
Expand Down
9 changes: 2 additions & 7 deletions pkgs/test/lib/src/runner/browser/post_message_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@

import 'dart:js_util';

import 'package:js/js.dart';
import 'package:stream_channel/stream_channel.dart';

import 'dom.dart' as dom;

// Avoid using this from dart:html to work around dart-lang/sdk#32113.
@JS('window.parent.postMessage')
external void _postParentMessage(Object message, String targetOrigin);

/// Constructs a [StreamChannel] wrapping [MessageChannel] communication with
/// the host page.
StreamChannel<Object?> postMessageChannel() {
Expand Down Expand Up @@ -50,8 +45,8 @@ StreamChannel<Object?> postMessageChannel() {

// Send a ready message once we're listening so the host knows it's safe to
// start sending events.
// TODO(nweiz): Stop manually adding href here once issue 22554 is fixed.
_postParentMessage(
// TODO: https://github.com/dart-lang/test/issues/2065 - remove href
dom.window.parent.postMessage(
jsify({'href': dom.window.location.href, 'ready': true}) as Object,
dom.window.location.origin);

Expand Down
9 changes: 5 additions & 4 deletions pkgs/test/tool/host.dart
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,11 @@ StreamChannel<dynamic> _connectToIframe(String url, int id) {
// running, but it's good practice to check the origin anyway.
var message = event as dom.MessageEvent;
if (message.origin != dom.window.location.origin) return;

// TODO(nweiz): Stop manually checking href here once issue 22554 is
// fixed.
if (message.data['href'] != iframe.src) return;
// Disambiguate between frames for different test suites.
// Depending on the source type, the `location.href` may be missing.
var location = js_util.getProperty(message.source, 'location') as Object?;
if (location == null) return;
if (js_util.getProperty(location, 'href') != iframe.src) return;

message.stopPropagation();

Expand Down