From f0e2b6de2c42d3981da3b3605105c213074278c9 Mon Sep 17 00:00:00 2001 From: Nate Bosch Date: Mon, 23 Oct 2023 12:08:10 -0700 Subject: [PATCH] Update to the latest package:test (#46592) - Update the pinned version of `test` to the latest published. - Add support for reading the `source.location` of messages to the JS interop library. - Update `host.dart` to support the new communication pattern with the test frame. See https://github.com/dart-lang/test/issues/2065 - Use `Runtime.edge` for the edge browser. We may deprecate or remove the constant. Edge is a more appropriate value for this usage. --- lib/web_ui/dev/edge.dart | 2 +- lib/web_ui/lib/src/engine/dom.dart | 25 +++++++++++ lib/web_ui/pubspec.yaml | 4 +- web_sdk/pubspec.yaml | 2 +- .../web_engine_tester/lib/static/host.dart | 43 +++++++++---------- web_sdk/web_engine_tester/pubspec.yaml | 2 +- 6 files changed, 51 insertions(+), 27 deletions(-) diff --git a/lib/web_ui/dev/edge.dart b/lib/web_ui/dev/edge.dart index 032071ae8b3dc..db264f459135b 100644 --- a/lib/web_ui/dev/edge.dart +++ b/lib/web_ui/dev/edge.dart @@ -24,7 +24,7 @@ class EdgeEnvironment implements BrowserEnvironment { } @override - Runtime get packageTestRuntime => Runtime.internetExplorer; + Runtime get packageTestRuntime => Runtime.edge; @override Future prepare() async { diff --git a/lib/web_ui/lib/src/engine/dom.dart b/lib/web_ui/lib/src/engine/dom.dart index 733a43c82fddf..9eab879b37c63 100644 --- a/lib/web_ui/lib/src/engine/dom.dart +++ b/lib/web_ui/lib/src/engine/dom.dart @@ -3027,6 +3027,31 @@ extension DomMessageEventExtension on DomMessageEvent { @JS('origin') external JSString get _origin; String get origin => _origin.toDart; + + /// 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]. + DomMessageEventSource get source => js_util.getProperty(this, 'source'); + + List get ports => + js_util.getProperty>(this, 'ports').cast(); +} + +@JS() +@staticInterop +class DomMessageEventSource {} + +extension DomMEssageEventSourceExtension on DomMessageEventSource { + external DomMessageEventLocation? get location; +} + +@JS() +@staticInterop +class DomMessageEventLocation {} + +extension DomMessageEventSourceExtension on DomMessageEventLocation { + external String? get href; } @JS() diff --git a/lib/web_ui/pubspec.yaml b/lib/web_ui/pubspec.yaml index 42d44d2894061..1b53ead9bd9c8 100644 --- a/lib/web_ui/pubspec.yaml +++ b/lib/web_ui/pubspec.yaml @@ -27,7 +27,7 @@ dev_dependencies: http: 1.1.0 http_multi_server: any image: 3.0.1 - matcher: 0.12.14 + matcher: 0.12.16 package_config: any path: 1.8.0 pool: any @@ -38,7 +38,7 @@ dev_dependencies: shelf_web_socket: any stack_trace: any stream_channel: 2.1.1 - test: 1.22.1 + test: 1.24.8 test_api: any test_core: any typed_data: any diff --git a/web_sdk/pubspec.yaml b/web_sdk/pubspec.yaml index e6932e6ea68bc..9d413a1cc5992 100644 --- a/web_sdk/pubspec.yaml +++ b/web_sdk/pubspec.yaml @@ -13,7 +13,7 @@ dev_dependencies: # up-to-date instead of a version from pub, which may not have the latest # language features enabled. analyzer: any - test: 1.22.1 + test: 1.24.8 dependency_overrides: # Must include all transitive dependencies from the "any" packages above. _fe_analyzer_shared: diff --git a/web_sdk/web_engine_tester/lib/static/host.dart b/web_sdk/web_engine_tester/lib/static/host.dart index bbd59c215fae9..9c8f0d8106b40 100644 --- a/web_sdk/web_engine_tester/lib/static/host.dart +++ b/web_sdk/web_engine_tester/lib/static/host.dart @@ -207,14 +207,8 @@ StreamChannel _connectToIframe(String url, int id) { ..height = '1000'; domDocument.body!.appendChild(iframe); - // Use this to communicate securely with the iframe. - final DomMessageChannel channel = createDomMessageChannel(); final StreamChannelController controller = StreamChannelController(sync: true); - // Use this to avoid sending a message to the iframe before it's sent a - // message to us. This ensures that no messages get dropped on the floor. - final Completer readyCompleter = Completer(); - final List domSubscriptions = []; final List> streamSubscriptions = >[]; _domSubscriptions[id] = domSubscriptions; @@ -229,20 +223,35 @@ StreamChannel _connectToIframe(String url, int id) { if (message.origin != domWindow.location.origin) { return; } - - if (message.data['href'] != iframe.src) { + if (message.source.location?.href != iframe.src) { return; } message.stopPropagation(); - if (message.data['ready'] == true) { + if (message.data == 'port') { + final DomMessagePort port = message.ports.first; + domSubscriptions.add( + DomSubscription(port, 'message',(DomEvent event) { + controller.local.sink.add((event as DomMessageEvent).data); + })); + port.start(); + streamSubscriptions.add(controller.local.stream.listen(port.postMessage)); + } else if (message.data['ready'] == true) { // This message indicates that the iframe is actively listening for // events, so the message channel's second port can now be transferred. + final DomMessageChannel channel = createDomMessageChannel(); + channel.port1.start(); channel.port2.start(); - iframe.contentWindow.postMessage('port', domWindow.location.origin, - [channel.port2]); - readyCompleter.complete(); + iframe.contentWindow.postMessage( + 'port', domWindow.location.origin, [channel.port2]); + domSubscriptions + .add(DomSubscription(channel.port1, 'message', (DomEvent message) { + controller.local.sink.add((message as DomMessageEvent).data['data']); + })); + + streamSubscriptions + .add(controller.local.stream.listen(channel.port1.postMessage)); } else if (message.data['exception'] == true) { // This message from `dart.js` indicates that an exception occurred // loading the test. @@ -250,16 +259,6 @@ StreamChannel _connectToIframe(String url, int id) { } })); - channel.port1.start(); - domSubscriptions.add(DomSubscription(channel.port1, 'message', - (DomEvent message) { - controller.local.sink.add((message as DomMessageEvent).data['data']); - })); - - streamSubscriptions.add(controller.local.stream.listen((dynamic message) async { - await readyCompleter.future; - channel.port1.postMessage(message); - })); return controller.foreign; } diff --git a/web_sdk/web_engine_tester/pubspec.yaml b/web_sdk/web_engine_tester/pubspec.yaml index dbb9dccf4d881..dadc9948addb8 100644 --- a/web_sdk/web_engine_tester/pubspec.yaml +++ b/web_sdk/web_engine_tester/pubspec.yaml @@ -7,7 +7,7 @@ environment: dependencies: js: 0.6.4 stream_channel: 2.1.1 - test: 1.22.1 + test: 1.24.8 webkit_inspection_protocol: 1.0.0 stack_trace: 1.10.0 ui: