This repository was 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] Add download listener to Android webview #4322
Merged
fluttergithubbot
merged 4 commits into
flutter:master
from
renefloor:feature/setDownloadListener
Sep 10, 2021
Merged
Changes from 1 commit
Commits
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
24 changes: 24 additions & 0 deletions
24
...tter/android/src/main/java/io/flutter/plugins/webviewflutter/FlutterDownloadListener.java
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package io.flutter.plugins.webviewflutter; | ||
|
||
import android.webkit.DownloadListener; | ||
import android.webkit.WebView; | ||
|
||
/** DownloadListener to notify the {@link FlutterWebViewClient} of download starts */ | ||
public class FlutterDownloadListener implements DownloadListener { | ||
final private FlutterWebViewClient webViewClient; | ||
private WebView webView; | ||
|
||
public FlutterDownloadListener(FlutterWebViewClient webViewClient){ | ||
this.webViewClient = webViewClient; | ||
} | ||
|
||
/** Sets the {@link WebView} that the result of the navigation delegate will be send to. */ | ||
public void setWebView(WebView webView){ | ||
this.webView = webView; | ||
} | ||
|
||
@Override | ||
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) { | ||
webViewClient.notifyDownload(webView, url); | ||
} | ||
} |
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
40 changes: 40 additions & 0 deletions
40
.../android/src/test/java/io/flutter/plugins/webviewflutter/FlutterDownloadListenerTest.java
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package io.flutter.plugins.webviewflutter; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.ArgumentMatchers.nullable; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
|
||
import android.webkit.WebView; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class FlutterDownloadListenerTest { | ||
private FlutterWebViewClient webViewClient; | ||
private WebView webView; | ||
|
||
@Before | ||
public void before() { | ||
webViewClient = mock(FlutterWebViewClient.class); | ||
webView = mock(WebView.class); | ||
} | ||
|
||
@Test | ||
public void onDownloadStart_should_notify_webViewClient(){ | ||
String url = "testurl.com"; | ||
FlutterDownloadListener downloadListener = new FlutterDownloadListener(webViewClient); | ||
downloadListener.onDownloadStart(url, "test", "inline", "data/text", 0); | ||
verify(webViewClient).notifyDownload(nullable(WebView.class), eq(url)); | ||
} | ||
|
||
@Test | ||
public void onDownloadStart_should_pass_webView(){ | ||
FlutterDownloadListener downloadListener = new FlutterDownloadListener(webViewClient); | ||
downloadListener.setWebView(webView); | ||
downloadListener.onDownloadStart("testurl.com", "test", "inline", "data/text", 0); | ||
verify(webViewClient).notifyDownload(eq(webView), anyString()); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...ter/android/src/test/java/io/flutter/plugins/webviewflutter/FlutterWebViewClientTest.java
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package io.flutter.plugins.webviewflutter; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
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.verifyNoInteractions; | ||
|
||
import android.webkit.WebView; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.ArgumentCaptor; | ||
|
||
import java.util.HashMap; | ||
|
||
import io.flutter.plugin.common.MethodChannel; | ||
|
||
public class FlutterWebViewClientTest { | ||
|
||
MethodChannel mockMethodChannel; | ||
WebView mockWebView; | ||
@Before | ||
public void before() { | ||
mockMethodChannel = mock(MethodChannel.class); | ||
mockWebView = mock(WebView.class); | ||
} | ||
|
||
@Test | ||
public void notify_download_should_notifyOnNavigationRequest_when_navigationDelegate_is_set() { | ||
final String url = "testurl.com"; | ||
|
||
FlutterWebViewClient client = new FlutterWebViewClient(mockMethodChannel); | ||
client.createWebViewClient(true); | ||
|
||
client.notifyDownload(mockWebView, url); | ||
ArgumentCaptor<Object> argumentCaptor = ArgumentCaptor.forClass(Object.class); | ||
verify(mockMethodChannel).invokeMethod(eq("navigationRequest"), argumentCaptor.capture(), any(MethodChannel.Result.class)); | ||
HashMap<String, Object> map = (HashMap<String, Object>) argumentCaptor.getValue(); | ||
assertEquals(map.get("url"), url); | ||
assertEquals(map.get("isForMainFrame"), true); | ||
} | ||
|
||
@Test | ||
public void notify_download_should_not_notifyOnNavigationRequest_when_navigationDelegate_is_not_set() { | ||
final String url = "testurl.com"; | ||
|
||
FlutterWebViewClient client = new FlutterWebViewClient(mockMethodChannel); | ||
client.createWebViewClient(false); | ||
|
||
client.notifyDownload(mockWebView, url); | ||
verifyNoInteractions(mockMethodChannel); | ||
} | ||
} |
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
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.
I'm not really happy that the webview depends on the downloadlistener and the other way around, but this is also how the
FlutterWebChromeClient
works. This is less visible as that just uses a property of the parent class.