This repository has been archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[webview_flutter] Fixes bug when onNavigationRequestCallback returns false #5981
Merged
fluttergithubbot
merged 2 commits into
flutter:main
from
Baseflow:webview_flutter/android_urlloading_bug
Jun 21, 2022
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
packages/webview_flutter/webview_flutter_android/CHANGELOG.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -2,6 +2,7 @@ | |||||||
// Use of this source code is governed by a BSD-style license that can be | ||||||||
// found in the LICENSE file. | ||||||||
|
||||||||
import 'dart:async'; | ||||||||
import 'dart:typed_data'; | ||||||||
|
||||||||
import 'package:flutter/widgets.dart'; | ||||||||
|
@@ -24,6 +25,7 @@ import 'webview_android_widget_test.mocks.dart'; | |||||||
android_webview.WebSettings, | ||||||||
android_webview.WebStorage, | ||||||||
android_webview.WebView, | ||||||||
android_webview.WebResourceRequest, | ||||||||
WebViewAndroidDownloadListener, | ||||||||
WebViewAndroidJavaScriptChannel, | ||||||||
WebViewAndroidWebChromeClient, | ||||||||
|
@@ -843,4 +845,194 @@ void main() { | |||||||
verify(mockPlatformHostApi.setWebContentsDebuggingEnabled(false)); | ||||||||
}); | ||||||||
}); | ||||||||
|
||||||||
group('WebViewAndroidWebViewClient', () { | ||||||||
test( | ||||||||
'urlLoading should call loadUrl when onNavigationRequestCallback returns true', | ||||||||
() { | ||||||||
final Completer<void> completer = Completer<void>(); | ||||||||
final WebViewAndroidWebViewClient webViewClient = | ||||||||
WebViewAndroidWebViewClient.handlesNavigation( | ||||||||
onPageStartedCallback: (_) {}, | ||||||||
onPageFinishedCallback: (_) {}, | ||||||||
onWebResourceErrorCallback: (_) {}, | ||||||||
onNavigationRequestCallback: ({ | ||||||||
required bool isForMainFrame, | ||||||||
required String url, | ||||||||
}) => | ||||||||
true, | ||||||||
loadUrl: (String url, Map<String, String>? headers) async { | ||||||||
completer.complete(); | ||||||||
}); | ||||||||
|
||||||||
webViewClient.urlLoading(MockWebView(), 'https://flutter.dev'); | ||||||||
expect(completer.isCompleted, isTrue); | ||||||||
}); | ||||||||
|
||||||||
test( | ||||||||
'urlLoading should call loadUrl when onNavigationRequestCallback returns a Future true', | ||||||||
() async { | ||||||||
final Completer<void> completer = Completer<void>(); | ||||||||
final WebViewAndroidWebViewClient webViewClient = | ||||||||
WebViewAndroidWebViewClient.handlesNavigation( | ||||||||
onPageStartedCallback: (_) {}, | ||||||||
onPageFinishedCallback: (_) {}, | ||||||||
onWebResourceErrorCallback: (_) {}, | ||||||||
onNavigationRequestCallback: ({ | ||||||||
required bool isForMainFrame, | ||||||||
required String url, | ||||||||
}) => | ||||||||
Future<bool>.value(true), | ||||||||
loadUrl: (String url, Map<String, String>? headers) async { | ||||||||
completer.complete(); | ||||||||
}); | ||||||||
|
||||||||
webViewClient.urlLoading(MockWebView(), 'https://flutter.dev'); | ||||||||
await completer.future; | ||||||||
expect(completer.isCompleted, isTrue); | ||||||||
}); | ||||||||
|
||||||||
test( | ||||||||
'urlLoading should not call laodUrl when onNavigationRequestCallback returns false', | ||||||||
() async { | ||||||||
final WebViewAndroidWebViewClient webViewClient = | ||||||||
WebViewAndroidWebViewClient.handlesNavigation( | ||||||||
onPageStartedCallback: (_) {}, | ||||||||
onPageFinishedCallback: (_) {}, | ||||||||
onWebResourceErrorCallback: (_) {}, | ||||||||
onNavigationRequestCallback: ({ | ||||||||
required bool isForMainFrame, | ||||||||
required String url, | ||||||||
}) => | ||||||||
false, | ||||||||
loadUrl: (String url, Map<String, String>? headers) async { | ||||||||
fail( | ||||||||
'loadUrl should not be called if onNavigationRequestCallback returns false.'); | ||||||||
}); | ||||||||
|
||||||||
webViewClient.urlLoading(MockWebView(), 'https://flutter.dev'); | ||||||||
}); | ||||||||
|
||||||||
test( | ||||||||
'urlLoading should not call loadUrl when onNavigationRequestCallback returns a Future false', | ||||||||
() { | ||||||||
final WebViewAndroidWebViewClient webViewClient = | ||||||||
WebViewAndroidWebViewClient.handlesNavigation( | ||||||||
onPageStartedCallback: (_) {}, | ||||||||
onPageFinishedCallback: (_) {}, | ||||||||
onWebResourceErrorCallback: (_) {}, | ||||||||
onNavigationRequestCallback: ({ | ||||||||
required bool isForMainFrame, | ||||||||
required String url, | ||||||||
}) => | ||||||||
Future<bool>.value(false), | ||||||||
loadUrl: (String url, Map<String, String>? headers) async { | ||||||||
fail( | ||||||||
'loadUrl should not be called if onNavigationRequestCallback returns false.'); | ||||||||
}); | ||||||||
|
||||||||
webViewClient.urlLoading(MockWebView(), 'https://flutter.dev'); | ||||||||
}); | ||||||||
|
||||||||
test( | ||||||||
'requestLoading should call loadUrl when onNavigationRequestCallback returns true', | ||||||||
() { | ||||||||
final Completer<void> completer = Completer<void>(); | ||||||||
final MockWebResourceRequest mockRequest = MockWebResourceRequest(); | ||||||||
when(mockRequest.isForMainFrame).thenReturn(true); | ||||||||
when(mockRequest.url).thenReturn('https://flutter.dev'); | ||||||||
final WebViewAndroidWebViewClient webViewClient = | ||||||||
WebViewAndroidWebViewClient.handlesNavigation( | ||||||||
onPageStartedCallback: (_) {}, | ||||||||
onPageFinishedCallback: (_) {}, | ||||||||
onWebResourceErrorCallback: (_) {}, | ||||||||
onNavigationRequestCallback: ({ | ||||||||
required bool isForMainFrame, | ||||||||
required String url, | ||||||||
}) => | ||||||||
true, | ||||||||
loadUrl: (String url, Map<String, String>? headers) async { | ||||||||
expect(url, 'https://flutter.dev'); | ||||||||
completer.complete(); | ||||||||
}); | ||||||||
|
||||||||
webViewClient.requestLoading(MockWebView(), mockRequest); | ||||||||
expect(completer.isCompleted, isTrue); | ||||||||
}); | ||||||||
|
||||||||
test( | ||||||||
'requestLoading should call loadUrl when onNavigationRequestCallback returns a Future true', | ||||||||
() async { | ||||||||
final Completer<void> completer = Completer<void>(); | ||||||||
final MockWebResourceRequest mockRequest = MockWebResourceRequest(); | ||||||||
when(mockRequest.isForMainFrame).thenReturn(true); | ||||||||
when(mockRequest.url).thenReturn('https://flutter.dev'); | ||||||||
final WebViewAndroidWebViewClient webViewClient = | ||||||||
WebViewAndroidWebViewClient.handlesNavigation( | ||||||||
onPageStartedCallback: (_) {}, | ||||||||
onPageFinishedCallback: (_) {}, | ||||||||
onWebResourceErrorCallback: (_) {}, | ||||||||
onNavigationRequestCallback: ({ | ||||||||
required bool isForMainFrame, | ||||||||
required String url, | ||||||||
}) => | ||||||||
Future<bool>.value(true), | ||||||||
loadUrl: (String url, Map<String, String>? headers) async { | ||||||||
expect(url, 'https://flutter.dev'); | ||||||||
completer.complete(); | ||||||||
}); | ||||||||
|
||||||||
webViewClient.requestLoading(MockWebView(), mockRequest); | ||||||||
await completer.future; | ||||||||
expect(completer.isCompleted, isTrue); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: same as above
Suggested change
|
||||||||
}); | ||||||||
|
||||||||
test( | ||||||||
'requestLoading should not call onLoadUrlCallback when onNavigationRequestCallback returns false', | ||||||||
() { | ||||||||
final MockWebResourceRequest mockRequest = MockWebResourceRequest(); | ||||||||
when(mockRequest.isForMainFrame).thenReturn(true); | ||||||||
when(mockRequest.url).thenReturn('https://flutter.dev'); | ||||||||
final WebViewAndroidWebViewClient webViewClient = | ||||||||
WebViewAndroidWebViewClient.handlesNavigation( | ||||||||
onPageStartedCallback: (_) {}, | ||||||||
onPageFinishedCallback: (_) {}, | ||||||||
onWebResourceErrorCallback: (_) {}, | ||||||||
onNavigationRequestCallback: ({ | ||||||||
required bool isForMainFrame, | ||||||||
required String url, | ||||||||
}) => | ||||||||
false, | ||||||||
loadUrl: (String url, Map<String, String>? headers) { | ||||||||
fail( | ||||||||
'loadUrl should not be called if onNavigationRequestCallback returns false.'); | ||||||||
}); | ||||||||
|
||||||||
webViewClient.requestLoading(MockWebView(), mockRequest); | ||||||||
}); | ||||||||
|
||||||||
test( | ||||||||
'requestLoading should not call onLoadUrlCallback when onNavigationRequestCallback returns a Future false', | ||||||||
() { | ||||||||
final MockWebResourceRequest mockRequest = MockWebResourceRequest(); | ||||||||
when(mockRequest.isForMainFrame).thenReturn(true); | ||||||||
when(mockRequest.url).thenReturn('https://flutter.dev'); | ||||||||
final WebViewAndroidWebViewClient webViewClient = | ||||||||
WebViewAndroidWebViewClient.handlesNavigation( | ||||||||
onPageStartedCallback: (_) {}, | ||||||||
onPageFinishedCallback: (_) {}, | ||||||||
onWebResourceErrorCallback: (_) {}, | ||||||||
onNavigationRequestCallback: ({ | ||||||||
required bool isForMainFrame, | ||||||||
required String url, | ||||||||
}) => | ||||||||
Future<bool>.value(false), | ||||||||
loadUrl: (String url, Map<String, String>? headers) { | ||||||||
fail( | ||||||||
'loadUrl should not be called if onNavigationRequestCallback returns false.'); | ||||||||
}); | ||||||||
|
||||||||
webViewClient.requestLoading(MockWebView(), mockRequest); | ||||||||
}); | ||||||||
}); | ||||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: You should be able to do