part of 'main.dart'; void webMessage() { final shouldSkip = kIsWeb ? true : ![ TargetPlatform.android, TargetPlatform.iOS, TargetPlatform.macOS, ].contains(defaultTargetPlatform); skippableGroup('WebMessage', () { skippableTestWidgets('WebMessageChannel', (WidgetTester tester) async { final Completer controllerCompleter = Completer(); final Completer webMessageCompleter = Completer(); await tester.pumpWidget( Directionality( textDirection: TextDirection.ltr, child: InAppWebView( key: GlobalKey(), initialData: InAppWebViewInitialData(data: """ WebMessageChannel Test
"""), onWebViewCreated: (controller) { controllerCompleter.complete(controller); }, onConsoleMessage: (controller, consoleMessage) { webMessageCompleter.complete(consoleMessage.message); }, onLoadStop: (controller, url) async { var webMessageChannel = await controller.createWebMessageChannel(); var port1 = webMessageChannel!.port1; var port2 = webMessageChannel.port2; await port1.setWebMessageCallback((message) async { await port1 .postMessage(WebMessage(data: message! + " and back")); }); await controller.postWebMessage( message: WebMessage(data: "capturePort", ports: [port2]), targetOrigin: WebUri("*")); await controller.evaluateJavascript( source: "document.getElementById('button').click();"); }, ), ), ); await controllerCompleter.future; final String message = await webMessageCompleter.future; expect(message, 'JavaScript To Native and back'); }); skippableTestWidgets('WebMessageListener', (WidgetTester tester) async { final Completer controllerCompleter = Completer(); final Completer pageLoaded = Completer(); final Completer webMessageCompleter = Completer(); await tester.pumpWidget( Directionality( textDirection: TextDirection.ltr, child: InAppWebView( key: GlobalKey(), onWebViewCreated: (controller) async { await controller.addWebMessageListener(WebMessageListener( jsObjectName: "myTestObj", allowedOriginRules: Set.from(["https://*.example.com"]), onPostMessage: (message, sourceOrigin, isMainFrame, replyProxy) { if (isMainFrame && (sourceOrigin.toString() + '/') == TEST_URL_EXAMPLE.toString()) { replyProxy.postMessage(message! + " and back"); } else { replyProxy.postMessage("Nope"); } }, )); controllerCompleter.complete(controller); }, onConsoleMessage: (controller, consoleMessage) { webMessageCompleter.complete(consoleMessage.message); }, onLoadStop: (controller, url) async { if (url.toString() == TEST_URL_EXAMPLE.toString()) { pageLoaded.complete(); } }, ), ), ); final controller = await controllerCompleter.future; await controller.loadUrl(urlRequest: URLRequest(url: TEST_URL_EXAMPLE)); await pageLoaded.future; await controller.evaluateJavascript(source: """ myTestObj.addEventListener('message', function(event) { console.log(event.data); }); myTestObj.postMessage('JavaScript To Native'); """); final String message = await webMessageCompleter.future; expect(message, 'JavaScript To Native and back'); }); }, skip: shouldSkip); }