Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit ffb36e0

Browse files
[webview_flutter_android] Fix throwing StateError when onShowFileChooser was nonnull (#6995)
1 parent 5f44021 commit ffb36e0

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

packages/webview_flutter/webview_flutter_android/lib/src/android_webview.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@ class WebChromeClient extends JavaObject {
927927
Future<void> setSynchronousReturnValueForOnShowFileChooser(
928928
bool value,
929929
) {
930-
if (value && onShowFileChooser != null) {
930+
if (value && onShowFileChooser == null) {
931931
throw StateError(
932932
'Setting this to true requires `onShowFileChooser` to be nonnull.',
933933
);

packages/webview_flutter/webview_flutter_android/test/android_webview_test.dart

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,58 @@ void main() {
861861
expect(result[1], params);
862862
});
863863

864+
test('setSynchronousReturnValueForOnShowFileChooser', () {
865+
final MockTestWebChromeClientHostApi mockHostApi =
866+
MockTestWebChromeClientHostApi();
867+
TestWebChromeClientHostApi.setup(mockHostApi);
868+
869+
WebChromeClient.api =
870+
WebChromeClientHostApiImpl(instanceManager: instanceManager);
871+
872+
final WebChromeClient webChromeClient = WebChromeClient.detached();
873+
instanceManager.addHostCreatedInstance(webChromeClient, 2);
874+
875+
webChromeClient.setSynchronousReturnValueForOnShowFileChooser(false);
876+
877+
verify(
878+
mockHostApi.setSynchronousReturnValueForOnShowFileChooser(2, false),
879+
);
880+
});
881+
882+
test(
883+
'setSynchronousReturnValueForOnShowFileChooser throws StateError when onShowFileChooser is null',
884+
() {
885+
final MockTestWebChromeClientHostApi mockHostApi =
886+
MockTestWebChromeClientHostApi();
887+
TestWebChromeClientHostApi.setup(mockHostApi);
888+
889+
WebChromeClient.api =
890+
WebChromeClientHostApiImpl(instanceManager: instanceManager);
891+
892+
final WebChromeClient clientWithNullCallback =
893+
WebChromeClient.detached();
894+
instanceManager.addHostCreatedInstance(clientWithNullCallback, 2);
895+
896+
expect(
897+
() => clientWithNullCallback
898+
.setSynchronousReturnValueForOnShowFileChooser(true),
899+
throwsStateError,
900+
);
901+
902+
final WebChromeClient clientWithNonnullCallback =
903+
WebChromeClient.detached(
904+
onShowFileChooser: (_, __) async => <String>[],
905+
);
906+
instanceManager.addHostCreatedInstance(clientWithNonnullCallback, 3);
907+
908+
clientWithNonnullCallback
909+
.setSynchronousReturnValueForOnShowFileChooser(true);
910+
911+
verify(
912+
mockHostApi.setSynchronousReturnValueForOnShowFileChooser(3, true),
913+
);
914+
});
915+
864916
test('copy', () {
865917
expect(WebChromeClient.detached().copy(), isA<WebChromeClient>());
866918
});

0 commit comments

Comments
 (0)