Skip to content

Commit

Permalink
Update to the latest package:test (#46592)
Browse files Browse the repository at this point in the history
- 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 dart-lang/test#2065
- Use `Runtime.edge` for the edge browser. We may deprecate or remove
  the constant. Edge is a more appropriate value for this usage.
  • Loading branch information
natebosch authored and harryterkelsen committed Oct 23, 2023
1 parent 20cd4bb commit f0e2b6d
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 27 deletions.
2 changes: 1 addition & 1 deletion lib/web_ui/dev/edge.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class EdgeEnvironment implements BrowserEnvironment {
}

@override
Runtime get packageTestRuntime => Runtime.internetExplorer;
Runtime get packageTestRuntime => Runtime.edge;

@override
Future<void> prepare() async {
Expand Down
25 changes: 25 additions & 0 deletions lib/web_ui/lib/src/engine/dom.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<DomMessagePort> get ports =>
js_util.getProperty<List<Object?>>(this, 'ports').cast<DomMessagePort>();
}

@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()
Expand Down
4 changes: 2 additions & 2 deletions lib/web_ui/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion web_sdk/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
43 changes: 21 additions & 22 deletions web_sdk/web_engine_tester/lib/static/host.dart
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,8 @@ StreamChannel<dynamic> _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<dynamic> controller = StreamChannelController<dynamic>(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<dynamic> readyCompleter = Completer<dynamic>();

final List<DomSubscription> domSubscriptions = <DomSubscription>[];
final List<StreamSubscription<dynamic>> streamSubscriptions = <StreamSubscription<dynamic>>[];
_domSubscriptions[id] = domSubscriptions;
Expand All @@ -229,37 +223,42 @@ StreamChannel<dynamic> _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,
<DomMessagePort>[channel.port2]);
readyCompleter.complete();
iframe.contentWindow.postMessage(
'port', domWindow.location.origin, <DomMessagePort>[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.
controller.local.sink.add(message.data['data']);
}
}));

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;
}
2 changes: 1 addition & 1 deletion web_sdk/web_engine_tester/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit f0e2b6d

Please sign in to comment.