Skip to content

Commit 48b6103

Browse files
authored
Apply lifecycle checks and fixes to addDocumentStartJavaScript (#6863)
Task/Issue URL: https://app.asana.com/1/137249556945/task/1211506668810055?focus=true ### Description * Check WebView attached before calling `WebViewCompat` and `postMessage` methods * Check coroutine active before calling `WebViewCompat` and `postMessage` methods * Launch WebViewCompat` and `postMessage` methods with `lifecycleScope` or `viewModelScope` whenever possible ### Steps to test this PR _Feature 1_ - [x] Smoke test blob downloads - [x] Smoke test app with `useNewWebCompatApis` enabled. Requires fresh install after setting setting `const val PRIVACY_REMOTE_CONFIG_URL = "https://duckduckgo.github.io/privacy-configuration/pr-3816/v4/android-config.json"` - [x] Run tests in https://privacy-test-pages.site/privacy-protections/gpc/. Make sure `frame JS API ` is true _Message replies_ - [x] Launch https://w3c.github.io/web-share/demos/share-files.html - [x] Click `Share` - [x] Cancel native popup - [x] Check message `Error sharing: AbortError: Share canceled` is displayed ### UI changes n/a
1 parent d6fd524 commit 48b6103

File tree

27 files changed

+1481
-939
lines changed

27 files changed

+1481
-939
lines changed

app/src/androidTest/java/com/duckduckgo/app/browser/BrowserTabViewModelTest.kt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ import com.duckduckgo.browser.api.autocomplete.AutoComplete.AutoCompleteSuggesti
215215
import com.duckduckgo.browser.api.autocomplete.AutoComplete.AutoCompleteSuggestion.AutoCompleteUrlSuggestion.AutoCompleteSwitchToTabSuggestion
216216
import com.duckduckgo.browser.api.autocomplete.AutoCompleteSettings
217217
import com.duckduckgo.browser.api.brokensite.BrokenSiteContext
218+
import com.duckduckgo.browser.api.webviewcompat.WebViewCompatWrapper
218219
import com.duckduckgo.browser.ui.omnibar.OmnibarPosition.BOTTOM
219220
import com.duckduckgo.browser.ui.omnibar.OmnibarPosition.TOP
220221
import com.duckduckgo.common.test.CoroutineTestRule
@@ -253,6 +254,7 @@ import com.duckduckgo.feature.toggles.api.Toggle
253254
import com.duckduckgo.feature.toggles.api.Toggle.State
254255
import com.duckduckgo.history.api.HistoryEntry.VisitedPage
255256
import com.duckduckgo.history.api.NavigationHistory
257+
import com.duckduckgo.js.messaging.api.AddDocumentStartJavaScriptPlugin
256258
import com.duckduckgo.js.messaging.api.JsCallbackData
257259
import com.duckduckgo.malicioussiteprotection.api.MaliciousSiteProtection.Feed
258260
import com.duckduckgo.malicioussiteprotection.api.MaliciousSiteProtection.Feed.MALWARE
@@ -600,6 +602,12 @@ class BrowserTabViewModelTest {
600602

601603
private var isFullSiteAddressEnabled = true
602604

605+
private val mockWebViewCompatWrapper: WebViewCompatWrapper = mock()
606+
607+
private val mockWebView: WebView = mock()
608+
609+
private val fakeAddDocumentStartJavaScriptPlugins = FakeAddDocumentStartJavaScriptPluginPoint()
610+
603611
@Before
604612
fun before() =
605613
runTest {
@@ -823,6 +831,8 @@ class BrowserTabViewModelTest {
823831
nonHttpAppLinkChecker = nonHttpAppLinkChecker,
824832
externalIntentProcessingState = mockExternalIntentProcessingState,
825833
vpnMenuStateProvider = mockVpnMenuStateProvider,
834+
webViewCompatWrapper = mockWebViewCompatWrapper,
835+
addDocumentStartJavascriptPlugins = fakeAddDocumentStartJavaScriptPlugins,
826836
)
827837

828838
testee.loadData("abc", null, false, false)
@@ -3594,6 +3604,7 @@ class BrowserTabViewModelTest {
35943604
val enabled = false
35953605

35963606
testee.requestFileDownload(
3607+
webView = mockWebView,
35973608
url = blobUrl,
35983609
contentDisposition = null,
35993610
mimeType = mime,
@@ -3614,6 +3625,7 @@ class BrowserTabViewModelTest {
36143625
val enabled = true
36153626

36163627
testee.requestFileDownload(
3628+
webView = mockWebView,
36173629
url = blobUrl,
36183630
contentDisposition = null,
36193631
mimeType = mime,
@@ -3630,6 +3642,7 @@ class BrowserTabViewModelTest {
36303642
val mime = "application/plain"
36313643

36323644
testee.requestFileDownload(
3645+
webView = mockWebView,
36333646
url = normalUrl,
36343647
contentDisposition = null,
36353648
mimeType = mime,
@@ -7425,6 +7438,15 @@ class BrowserTabViewModelTest {
74257438
assertNull("SERP logo should be cleared when navigating to non-DuckDuckGo URL", omnibarViewState().serpLogo)
74267439
}
74277440

7441+
@Test
7442+
fun whenConfigureWebViewThenCallAddDocumentStartJavaScript() {
7443+
assertEquals(0, fakeAddDocumentStartJavaScriptPlugins.plugin.countInitted)
7444+
7445+
testee.addDocumentStartJavaScript(mockWebView)
7446+
7447+
assertEquals(1, fakeAddDocumentStartJavaScriptPlugins.plugin.countInitted)
7448+
}
7449+
74287450
private fun aCredential(): LoginCredentials = LoginCredentials(domain = null, username = null, password = null)
74297451

74307452
private fun assertShowHistoryCommandSent(expectedStackSize: Int) {
@@ -7711,4 +7733,19 @@ class BrowserTabViewModelTest {
77117733
) : CustomHeadersProvider {
77127734
override fun getCustomHeaders(url: String): Map<String, String> = headers
77137735
}
7736+
7737+
class FakeAddDocumentStartJavaScriptPlugin : AddDocumentStartJavaScriptPlugin {
7738+
var countInitted = 0
7739+
private set
7740+
7741+
override suspend fun addDocumentStartJavaScript(webView: WebView) {
7742+
countInitted++
7743+
}
7744+
}
7745+
7746+
class FakeAddDocumentStartJavaScriptPluginPoint : PluginPoint<AddDocumentStartJavaScriptPlugin> {
7747+
val plugin = FakeAddDocumentStartJavaScriptPlugin()
7748+
7749+
override fun getPlugins() = listOf(plugin)
7750+
}
77147751
}

0 commit comments

Comments
 (0)