import 'dart:async'; import 'dart:convert'; import 'dart:typed_data'; import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; import 'package:flutter/widgets.dart'; import 'package:flutter_inappwebview/flutter_inappwebview.dart'; import 'package:flutter_test/flutter_test.dart'; void audioPlaybackPolicy() { final shouldSkip = kIsWeb ? true : ![ TargetPlatform.android, TargetPlatform.iOS, TargetPlatform.macOS, ].contains(defaultTargetPlatform); group('Audio playback policy', () { String audioTestBase64 = ""; setUpAll(() async { final ByteData audioData = await rootBundle.load('test_assets/sample_audio.ogg'); final String base64AudioData = base64Encode(Uint8List.view(audioData.buffer)); final String audioTest = ''' Audio auto play '''; audioTestBase64 = base64Encode(const Utf8Encoder().convert(audioTest)); }); testWidgets('Auto media playback', (WidgetTester tester) async { Completer controllerCompleter = Completer(); Completer pageStarted = Completer(); Completer pageLoaded = Completer(); await tester.pumpWidget( Directionality( textDirection: TextDirection.ltr, child: InAppWebView( key: GlobalKey(), initialUrlRequest: URLRequest( url: WebUri( 'data:text/html;charset=utf-8;base64,$audioTestBase64')), onWebViewCreated: (controller) { controllerCompleter.complete(controller); }, initialSettings: InAppWebViewSettings( javaScriptEnabled: true, mediaPlaybackRequiresUserGesture: false), onLoadStart: (controller, url) { pageStarted.complete(); }, onLoadStop: (controller, url) { pageLoaded.complete(); }, ), ), ); InAppWebViewController controller = await controllerCompleter.future; await pageStarted.future; await pageLoaded.future; bool isPaused = await controller.evaluateJavascript(source: 'isPaused();'); expect(isPaused, false); controllerCompleter = Completer(); pageStarted = Completer(); pageLoaded = Completer(); // We change the key to re-create a new webview as we change the mediaPlaybackRequiresUserGesture await tester.pumpWidget( Directionality( textDirection: TextDirection.ltr, child: InAppWebView( key: GlobalKey(), initialUrlRequest: URLRequest( url: WebUri( 'data:text/html;charset=utf-8;base64,$audioTestBase64')), onWebViewCreated: (controller) { controllerCompleter.complete(controller); }, initialSettings: InAppWebViewSettings( javaScriptEnabled: true, mediaPlaybackRequiresUserGesture: true), onLoadStart: (controller, url) { pageStarted.complete(); }, onLoadStop: (controller, url) { pageLoaded.complete(); }, ), ), ); controller = await controllerCompleter.future; await pageStarted.future; await pageLoaded.future; isPaused = await controller.evaluateJavascript(source: 'isPaused();'); expect(isPaused, true); }); }, skip: shouldSkip); }