Skip to content

Commit 9ec7474

Browse files
committed
Fix tests
1 parent b8e0f50 commit 9ec7474

File tree

9 files changed

+261
-54
lines changed

9 files changed

+261
-54
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,12 +1187,12 @@ class BrowserWebViewClientTest {
11871187
var countFinished = 0
11881188
var countStarted = 0
11891189

1190-
override fun onInit(
1190+
override suspend fun onInit(
11911191
webView: WebView,
11921192
) {
11931193
}
11941194

1195-
override fun onPageStarted(
1195+
override suspend fun onPageStarted(
11961196
webView: WebView,
11971197
url: String?,
11981198
isDesktopMode: Boolean?,
@@ -1201,7 +1201,7 @@ class BrowserWebViewClientTest {
12011201
return listOf(mockToggle)
12021202
}
12031203

1204-
override fun onPageFinished(
1204+
override suspend fun onPageFinished(
12051205
webView: WebView,
12061206
url: String?,
12071207
) {

app/src/main/java/com/duckduckgo/app/browser/BrowserWebViewClient.kt

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,10 @@ class BrowserWebViewClient @Inject constructor(
462462
}
463463

464464
fun triggerJSInit(webView: WebView) {
465-
jsPlugins.getPlugins().forEach {
466-
it.onInit(webView)
465+
appCoroutineScope.launch {
466+
jsPlugins.getPlugins().forEach {
467+
it.onInit(webView)
468+
}
467469
}
468470
}
469471

@@ -474,11 +476,13 @@ class BrowserWebViewClient @Inject constructor(
474476

475477
// See https://app.asana.com/0/0/1206159443951489/f (WebView limitations)
476478
if (webView.progress == 100) {
477-
jsPlugins.getPlugins().forEach {
478-
it.onPageFinished(
479-
webView,
480-
url,
481-
)
479+
appCoroutineScope.launch {
480+
jsPlugins.getPlugins().forEach {
481+
it.onPageFinished(
482+
webView,
483+
url,
484+
)
485+
}
482486
}
483487

484488
url?.let {

browser-api/src/main/java/com/duckduckgo/browser/api/JsInjectorPlugin.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ interface JsInjectorPlugin {
2424
/**
2525
* On init of webview this is called and receives a [webView] instance.
2626
*/
27-
fun onInit(
27+
suspend fun onInit(
2828
webView: WebView,
2929
)
3030

3131
/**
3232
* This method is called during onPageStarted and receives a [webView] instance, the [url] of the website and the [site]
3333
*/
34-
fun onPageStarted(
34+
suspend fun onPageStarted(
3535
webView: WebView,
3636
url: String?,
3737
isDesktopMode: Boolean?,
@@ -40,7 +40,7 @@ interface JsInjectorPlugin {
4040
/**
4141
* This method is called during onPageFinished and receives a [webView] instance, the [url] of the website and the [site]
4242
*/
43-
fun onPageFinished(
43+
suspend fun onPageFinished(
4444
webView: WebView,
4545
url: String?,
4646
)

content-scope-scripts/content-scope-scripts-impl/src/main/java/com/duckduckgo/contentscopescripts/impl/ContentScopeScriptsJsInjectorPlugin.kt

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,68 +16,63 @@
1616

1717
package com.duckduckgo.contentscopescripts.impl
1818

19+
import android.annotation.SuppressLint
1920
import android.webkit.WebView
2021
import androidx.webkit.ScriptHandler
21-
import androidx.webkit.WebViewCompat
22-
import androidx.webkit.WebViewFeature
23-
import com.duckduckgo.app.di.AppCoroutineScope
2422
import com.duckduckgo.browser.api.JsInjectorPlugin
2523
import com.duckduckgo.common.utils.DispatcherProvider
2624
import com.duckduckgo.contentscopescripts.api.contentscopeExperiments.ContentScopeExperiments
2725
import com.duckduckgo.di.scopes.AppScope
2826
import com.duckduckgo.feature.toggles.api.Toggle
2927
import com.squareup.anvil.annotations.ContributesMultibinding
3028
import javax.inject.Inject
31-
import kotlinx.coroutines.CoroutineScope
32-
import kotlinx.coroutines.launch
3329
import kotlinx.coroutines.withContext
3430

3531
@ContributesMultibinding(AppScope::class)
3632
class ContentScopeScriptsJsInjectorPlugin @Inject constructor(
3733
private val coreContentScopeScripts: CoreContentScopeScripts,
3834
private val adsJsContentScopeScripts: AdsJsContentScopeScripts,
3935
private val contentScopeExperiments: ContentScopeExperiments,
40-
@AppCoroutineScope private val appCoroutineScope: CoroutineScope,
4136
private val dispatcherProvider: DispatcherProvider,
37+
private val webViewCompatWrapper: WebViewCompatWrapper,
4238
) : JsInjectorPlugin {
4339
private var script: ScriptHandler? = null
4440
private var currentScriptString: String? = null
4541

4642
private var activeExperiments: List<Toggle> = emptyList()
4743

48-
private fun reloadJSIfNeeded(
44+
@SuppressLint("RequiresFeature")
45+
private suspend fun reloadJSIfNeeded(
4946
webView: WebView,
5047
) {
51-
appCoroutineScope.launch(dispatcherProvider.io()) {
52-
activeExperiments = contentScopeExperiments.getActiveExperiments()
48+
activeExperiments = withContext(dispatcherProvider.io()) { contentScopeExperiments.getActiveExperiments() }
5349

54-
withContext(dispatcherProvider.main()) {
55-
if (!WebViewFeature.isFeatureSupported(WebViewFeature.DOCUMENT_START_SCRIPT)) {
56-
return@withContext
57-
}
58-
val scriptString = adsJsContentScopeScripts.getScript(activeExperiments)
59-
if (scriptString == currentScriptString) {
60-
return@withContext
61-
}
62-
script?.let {
63-
it.remove()
64-
script = null
65-
}
66-
if (coreContentScopeScripts.isEnabled()) {
67-
currentScriptString = scriptString
68-
script = WebViewCompat.addDocumentStartJavaScript(webView, scriptString, setOf("*"))
69-
}
50+
withContext(dispatcherProvider.main()) {
51+
if (!webViewCompatWrapper.isDocumentStartScriptSupported()) {
52+
return@withContext
53+
}
54+
val scriptString = adsJsContentScopeScripts.getScript(activeExperiments)
55+
if (scriptString == currentScriptString) {
56+
return@withContext
57+
}
58+
script?.let {
59+
it.remove()
60+
script = null
61+
}
62+
if (coreContentScopeScripts.isEnabled()) {
63+
currentScriptString = scriptString
64+
script = webViewCompatWrapper.addDocumentStartJavaScript(webView, scriptString, setOf("*"))
7065
}
7166
}
7267
}
7368

74-
override fun onInit(
69+
override suspend fun onInit(
7570
webView: WebView,
7671
) {
7772
reloadJSIfNeeded(webView)
7873
}
7974

80-
override fun onPageStarted(
75+
override suspend fun onPageStarted(
8176
webView: WebView,
8277
url: String?,
8378
isDesktopMode: Boolean?,
@@ -89,7 +84,7 @@ class ContentScopeScriptsJsInjectorPlugin @Inject constructor(
8984
return listOf()
9085
}
9186

92-
override fun onPageFinished(
87+
override suspend fun onPageFinished(
9388
webView: WebView,
9489
url: String?,
9590
) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (c) 2024 DuckDuckGo
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.duckduckgo.contentscopescripts.impl
18+
19+
import android.annotation.SuppressLint
20+
import androidx.webkit.ScriptHandler
21+
import androidx.webkit.WebViewCompat
22+
import androidx.webkit.WebViewFeature
23+
import com.duckduckgo.di.scopes.AppScope
24+
import com.squareup.anvil.annotations.ContributesBinding
25+
import javax.inject.Inject
26+
27+
@SuppressLint("RequiresFeature")
28+
@ContributesBinding(AppScope::class)
29+
class RealWebViewCompatWrapper @Inject constructor() : WebViewCompatWrapper {
30+
override fun isDocumentStartScriptSupported(): Boolean {
31+
return WebViewFeature.isFeatureSupported(WebViewFeature.DOCUMENT_START_SCRIPT)
32+
}
33+
34+
override fun addDocumentStartJavaScript(
35+
webView: android.webkit.WebView,
36+
script: String,
37+
allowedOriginRules: Set<String>,
38+
): ScriptHandler {
39+
return WebViewCompat.addDocumentStartJavaScript(webView, script, allowedOriginRules)
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) 2024 DuckDuckGo
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.duckduckgo.contentscopescripts.impl
18+
19+
import android.webkit.WebView
20+
import androidx.webkit.ScriptHandler
21+
22+
interface WebViewCompatWrapper {
23+
24+
fun isDocumentStartScriptSupported(): Boolean
25+
26+
fun addDocumentStartJavaScript(
27+
webView: WebView,
28+
script: String,
29+
allowedOriginRules: Set<String>,
30+
): ScriptHandler
31+
}

0 commit comments

Comments
 (0)