From d23fb23df822e882bdf0f25468359a3f2cae2b02 Mon Sep 17 00:00:00 2001 From: Nate Bosch Date: Fri, 11 Aug 2023 12:46:53 -0700 Subject: [PATCH] Send test message channel from the test frame (#131881) Towards https://github.com/dart-lang/test/issues/2065 The flutter test runner uses the copy of `host.dart.js` from the copy of `package:test` that surfaces in the pub solve for `flutter_tool`. This copy has been updated to allow either the old pattern of communication, or this new pattern. The new pattern removes an extra hop and use of the frame `window.onMessage` messages. --- .../flutter_tools/lib/src/web/bootstrap.dart | 27 +++++++------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/packages/flutter_tools/lib/src/web/bootstrap.dart b/packages/flutter_tools/lib/src/web/bootstrap.dart index 814a9a6809..7b44bec749 100644 --- a/packages/flutter_tools/lib/src/web/bootstrap.dart +++ b/packages/flutter_tools/lib/src/web/bootstrap.dart @@ -247,25 +247,16 @@ String generateTestEntrypoint({ StreamChannel serializeSuite(Function getMain(), {bool hidePrints = true}) => RemoteListener.start(getMain, hidePrints: hidePrints); StreamChannel postMessageChannel() { - var controller = StreamChannelController(sync: true); - window.onMessage.firstWhere((message) { - return message.origin == window.location.origin && message.data == "port"; - }).then((message) { - var port = message.ports.first; - var portSubscription = port.onMessage.listen((message) { - controller.local.sink.add(message.data); - }); - controller.local.stream.listen((data) { - port.postMessage({"data": data}); - }, onDone: () { - port.postMessage({"event": "done"}); - portSubscription.cancel(); - }); + var controller = StreamChannelController(sync: true); + var channel = MessageChannel(); + window.parent!.postMessage('port', window.location.origin, [channel.port2]); + + var portSubscription = channel.port1.onMessage.listen((message) { + controller.local.sink.add(message.data); }); - context['parent'].callMethod('postMessage', [ - JsObject.jsify({"href": window.location.href, "ready": true}), - window.location.origin, - ]); + controller.local.stream + .listen(channel.port1.postMessage, onDone: portSubscription.cancel); + return controller.foreign; } ''';