-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[webview_flutter_android] Fixes iframe navigation with onNavigationRequest
#6335
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
auto-submit
merged 4 commits into
flutter:main
from
petermnt:webview-android-navigation-delegate
May 1, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
cf619d0
Fixes iframe navigation being handled in the main frame when `Navigat…
petermnt 361c19d
Don't send the request to the flutter api for subframes. Also improve…
petermnt d1d3bd6
Handle not sending the request to the client in the controller
petermnt 9035847
Remove redundant test
petermnt 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 hidden or 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 hidden or 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 hidden or 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
190 changes: 190 additions & 0 deletions
190
...ndroid/android/src/test/java/io/flutter/plugins/webviewflutter/WebViewClientImplTest.java
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,190 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package io.flutter.plugins.webviewflutter; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import android.net.Uri; | ||
import android.webkit.WebResourceRequest; | ||
import android.webkit.WebResourceResponse; | ||
import android.webkit.WebView; | ||
import android.webkit.WebViewClient; | ||
import androidx.annotation.NonNull; | ||
import io.flutter.plugins.webviewflutter.WebViewClientHostApiImpl.WebViewClientCreator; | ||
import java.util.HashMap; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnit; | ||
import org.mockito.junit.MockitoRule; | ||
|
||
public class WebViewClientImplTest { | ||
@Rule public MockitoRule mockitoRule = MockitoJUnit.rule(); | ||
|
||
@Mock public WebViewClientFlutterApiImpl mockFlutterApi; | ||
|
||
@Mock public WebView mockWebView; | ||
|
||
InstanceManager instanceManager; | ||
WebViewClientHostApiImpl hostApiImpl; | ||
WebViewClientHostApiImpl.WebViewClientImpl webViewClient; | ||
|
||
@Before | ||
public void setUp() { | ||
instanceManager = InstanceManager.create(identifier -> {}); | ||
|
||
final WebViewClientCreator webViewClientCreator = | ||
new WebViewClientCreator() { | ||
@Override | ||
@NonNull | ||
public WebViewClient createWebViewClient( | ||
@NonNull WebViewClientFlutterApiImpl flutterApi) { | ||
webViewClient = new WebViewClientHostApiImpl.WebViewClientImpl(flutterApi); | ||
return webViewClient; | ||
} | ||
}; | ||
|
||
hostApiImpl = | ||
new WebViewClientHostApiImpl(instanceManager, webViewClientCreator, mockFlutterApi); | ||
hostApiImpl.create(1L); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
instanceManager.stopFinalizationListener(); | ||
} | ||
|
||
@Test | ||
public void onPageStarted() { | ||
webViewClient.onPageStarted(mockWebView, "https://www.google.com", null); | ||
verify(mockFlutterApi) | ||
.onPageStarted(eq(webViewClient), eq(mockWebView), eq("https://www.google.com"), any()); | ||
} | ||
|
||
@Test | ||
public void onReceivedError() { | ||
webViewClient.onReceivedError(mockWebView, 32, "description", "https://www.google.com"); | ||
verify(mockFlutterApi) | ||
.onReceivedError( | ||
eq(webViewClient), | ||
eq(mockWebView), | ||
eq(32L), | ||
eq("description"), | ||
eq("https://www.google.com"), | ||
any()); | ||
} | ||
|
||
@Test | ||
public void urlLoading() { | ||
webViewClient.shouldOverrideUrlLoading(mockWebView, "https://www.google.com"); | ||
verify(mockFlutterApi) | ||
.urlLoading(eq(webViewClient), eq(mockWebView), eq("https://www.google.com"), any()); | ||
} | ||
|
||
@Test | ||
public void urlLoadingForMainFrame() { | ||
webViewClient.setReturnValueForShouldOverrideUrlLoading(false); | ||
|
||
final WebResourceRequest mockRequest = mock(WebResourceRequest.class); | ||
when(mockRequest.isForMainFrame()).thenReturn(true); | ||
|
||
assertFalse(webViewClient.shouldOverrideUrlLoading(mockWebView, mockRequest)); | ||
verify(mockFlutterApi) | ||
.requestLoading(eq(webViewClient), eq(mockWebView), eq(mockRequest), any()); | ||
} | ||
|
||
@Test | ||
public void urlLoadingForMainFrameWithOverride() { | ||
webViewClient.setReturnValueForShouldOverrideUrlLoading(true); | ||
|
||
final WebResourceRequest mockRequest = mock(WebResourceRequest.class); | ||
when(mockRequest.isForMainFrame()).thenReturn(true); | ||
|
||
assertTrue(webViewClient.shouldOverrideUrlLoading(mockWebView, mockRequest)); | ||
verify(mockFlutterApi) | ||
.requestLoading(eq(webViewClient), eq(mockWebView), eq(mockRequest), any()); | ||
} | ||
|
||
@Test | ||
public void urlLoadingNotForMainFrame() { | ||
webViewClient.setReturnValueForShouldOverrideUrlLoading(false); | ||
|
||
final WebResourceRequest mockRequest = mock(WebResourceRequest.class); | ||
when(mockRequest.isForMainFrame()).thenReturn(false); | ||
|
||
assertFalse(webViewClient.shouldOverrideUrlLoading(mockWebView, mockRequest)); | ||
verify(mockFlutterApi) | ||
.requestLoading(eq(webViewClient), eq(mockWebView), eq(mockRequest), any()); | ||
} | ||
|
||
@Test | ||
public void urlLoadingNotForMainFrameWithOverride() { | ||
webViewClient.setReturnValueForShouldOverrideUrlLoading(true); | ||
|
||
final WebResourceRequest mockRequest = mock(WebResourceRequest.class); | ||
when(mockRequest.isForMainFrame()).thenReturn(false); | ||
|
||
assertFalse(webViewClient.shouldOverrideUrlLoading(mockWebView, mockRequest)); | ||
verify(mockFlutterApi) | ||
.requestLoading(eq(webViewClient), eq(mockWebView), eq(mockRequest), any()); | ||
} | ||
|
||
@Test | ||
public void convertWebResourceRequestWithNullHeaders() { | ||
final Uri mockUri = mock(Uri.class); | ||
when(mockUri.toString()).thenReturn(""); | ||
|
||
final WebResourceRequest mockRequest = mock(WebResourceRequest.class); | ||
when(mockRequest.getMethod()).thenReturn("method"); | ||
when(mockRequest.getUrl()).thenReturn(mockUri); | ||
when(mockRequest.isForMainFrame()).thenReturn(true); | ||
when(mockRequest.getRequestHeaders()).thenReturn(null); | ||
|
||
final GeneratedAndroidWebView.WebResourceRequestData data = | ||
WebViewClientFlutterApiImpl.createWebResourceRequestData(mockRequest); | ||
assertEquals(data.getRequestHeaders(), new HashMap<String, String>()); | ||
} | ||
|
||
@Test | ||
public void doUpdateVisitedHistory() { | ||
webViewClient.doUpdateVisitedHistory(mockWebView, "https://www.google.com", true); | ||
verify(mockFlutterApi) | ||
.doUpdateVisitedHistory( | ||
eq(webViewClient), eq(mockWebView), eq("https://www.google.com"), eq(true), any()); | ||
} | ||
|
||
@Test | ||
public void onReceivedHttpError() { | ||
final Uri mockUri = mock(Uri.class); | ||
when(mockUri.toString()).thenReturn(""); | ||
|
||
final WebResourceRequest mockRequest = mock(WebResourceRequest.class); | ||
when(mockRequest.getMethod()).thenReturn("method"); | ||
when(mockRequest.getUrl()).thenReturn(mockUri); | ||
when(mockRequest.isForMainFrame()).thenReturn(true); | ||
when(mockRequest.getRequestHeaders()).thenReturn(null); | ||
|
||
final WebResourceResponse mockResponse = mock(WebResourceResponse.class); | ||
when(mockResponse.getStatusCode()).thenReturn(404); | ||
|
||
webViewClient.onReceivedHttpError(mockWebView, mockRequest, mockResponse); | ||
verify(mockFlutterApi) | ||
.onReceivedHttpError( | ||
eq(webViewClient), | ||
eq(mockWebView), | ||
any(WebResourceRequest.class), | ||
any(WebResourceResponse.class), | ||
any()); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.