iosWebViewFix/example/integration_test/in_app_webview/audio_playback_policy.dart

125 lines
4.1 KiB
Dart
Raw Normal View History

2022-04-27 23:10:20 +00:00
import 'dart:async';
import 'dart:convert';
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() {
2022-04-29 11:39:28 +00:00
final shouldSkip = kIsWeb
? true
: ![
TargetPlatform.android,
TargetPlatform.iOS,
TargetPlatform.macOS,
].contains(defaultTargetPlatform);
2022-04-27 23:10:20 +00:00
group('Audio playback policy', () {
String audioTestBase64 = "";
setUpAll(() async {
final ByteData audioData =
2022-04-29 11:39:28 +00:00
await rootBundle.load('test_assets/sample_audio.ogg');
2022-04-27 23:10:20 +00:00
final String base64AudioData =
2022-04-29 11:39:28 +00:00
base64Encode(Uint8List.view(audioData.buffer));
2022-04-27 23:10:20 +00:00
final String audioTest = '''
<!DOCTYPE html><html>
<head><title>Audio auto play</title>
<script type="text/javascript">
function play() {
var audio = document.getElementById("audio");
audio.play();
}
function isPaused() {
var audio = document.getElementById("audio");
return audio.paused;
}
</script>
</head>
<body onload="play();">
<audio controls id="audio">
<source src="data:audio/ogg;charset=utf-8;base64,$base64AudioData">
</audio>
</body>
</html>
''';
audioTestBase64 = base64Encode(const Utf8Encoder().convert(audioTest));
});
testWidgets('Auto media playback', (WidgetTester tester) async {
Completer<InAppWebViewController> controllerCompleter =
2022-04-29 11:39:28 +00:00
Completer<InAppWebViewController>();
2022-04-27 23:10:20 +00:00
Completer<void> pageStarted = Completer<void>();
Completer<void> pageLoaded = Completer<void>();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: InAppWebView(
key: GlobalKey(),
initialUrlRequest: URLRequest(
url: Uri.parse(
'data:text/html;charset=utf-8;base64,$audioTestBase64')),
onWebViewCreated: (controller) {
controllerCompleter.complete(controller);
},
initialSettings: InAppWebViewSettings(
2022-04-29 11:39:28 +00:00
javaScriptEnabled: true,
mediaPlaybackRequiresUserGesture: false),
2022-04-27 23:10:20 +00:00
onLoadStart: (controller, url) {
pageStarted.complete();
},
onLoadStop: (controller, url) {
pageLoaded.complete();
},
),
),
);
InAppWebViewController controller = await controllerCompleter.future;
await pageStarted.future;
await pageLoaded.future;
bool isPaused =
2022-04-29 11:39:28 +00:00
await controller.evaluateJavascript(source: 'isPaused();');
2022-04-27 23:10:20 +00:00
expect(isPaused, false);
controllerCompleter = Completer<InAppWebViewController>();
pageStarted = Completer<void>();
pageLoaded = Completer<void>();
// 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: Uri.parse(
'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);
2022-04-29 11:39:28 +00:00
}