Skip to content

Commit 6cff872

Browse files
committed
Fix tests
1 parent ff9006d commit 6cff872

File tree

3 files changed

+144
-39
lines changed

3 files changed

+144
-39
lines changed

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ import com.duckduckgo.app.statistics.pixels.Pixel
6767
import com.duckduckgo.autoconsent.api.Autoconsent
6868
import com.duckduckgo.autofill.api.BrowserAutofill
6969
import com.duckduckgo.autofill.api.InternalTestUserChecker
70+
import com.duckduckgo.browser.api.AddDocumentStartJavaScriptPlugin
7071
import com.duckduckgo.browser.api.JsInjectorPlugin
7172
import com.duckduckgo.browser.api.WebViewVersionProvider
7273
import com.duckduckgo.common.test.CoroutineTestRule
@@ -355,10 +356,10 @@ class BrowserWebViewClientTest {
355356
@UiThreadTest
356357
@Test
357358
fun whenConfigureWebViewThenInjectJsCode() {
358-
assertEquals(0, fakeAddDocumentStartJavaScriptPlugins.plugin.countInitted)
359+
assertEquals(0, fakeAddDocumentStartJavaScriptPlugins.plugin.addDocumentStartJavaScript().countInitted)
359360
val mockCallback = mock<WebViewCompatMessageCallback>()
360361
testee.configureWebView(DuckDuckGoWebView(context), mockCallback)
361-
assertEquals(1, fakeAddDocumentStartJavaScriptPlugins.plugin.countInitted)
362+
assertEquals(1, fakeAddDocumentStartJavaScriptPlugins.plugin.addDocumentStartJavaScript().countInitted)
362363
}
363364

364365
@UiThreadTest
@@ -1330,8 +1331,16 @@ class BrowserWebViewClientTest {
13301331
const val EXAMPLE_URL = "https://example.com"
13311332
}
13321333

1333-
class FakeAddDocumentStartJavaScript : AddDocumentStartJavaScript {
1334+
class FakeAddDocumentStartJavaScriptPlugin : AddDocumentStartJavaScriptPlugin {
1335+
1336+
private val addDocumentStartJavaScript = FakeAddDocumentStartJavaScript()
1337+
1338+
override fun addDocumentStartJavaScript(): FakeAddDocumentStartJavaScript {
1339+
return addDocumentStartJavaScript
1340+
}
1341+
}
13341342

1343+
class FakeAddDocumentStartJavaScript : AddDocumentStartJavaScript {
13351344
var countInitted = 0
13361345
private set
13371346

@@ -1342,9 +1351,9 @@ class BrowserWebViewClientTest {
13421351
}
13431352
}
13441353

1345-
class FakeAddDocumentStartJavaScriptPluginPoint : PluginPoint<AddDocumentStartJavaScript> {
1354+
class FakeAddDocumentStartJavaScriptPluginPoint : PluginPoint<AddDocumentStartJavaScriptPlugin> {
13461355

1347-
val plugin = FakeAddDocumentStartJavaScript()
1356+
val plugin = FakeAddDocumentStartJavaScriptPlugin()
13481357

13491358
override fun getPlugins() = listOf(plugin)
13501359
}
Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
package com.duckduckgo.contentscopescripts.impl
22

33
import android.webkit.WebView
4-
import com.duckduckgo.app.browser.api.WebViewCapabilityChecker
5-
import com.duckduckgo.browser.api.webviewcompat.WebViewCompatWrapper
64
import com.duckduckgo.common.test.CoroutineTestRule
75
import com.duckduckgo.contentscopescripts.api.contentscopeExperiments.ContentScopeExperiments
6+
import com.duckduckgo.js.messaging.api.AddDocumentStartJavaScript
7+
import com.duckduckgo.js.messaging.api.AddDocumentStartScriptDelegate
88
import kotlinx.coroutines.test.runTest
99
import org.junit.Before
1010
import org.junit.Rule
1111
import org.junit.Test
1212
import org.mockito.kotlin.any
1313
import org.mockito.kotlin.mock
14-
import org.mockito.kotlin.never
1514
import org.mockito.kotlin.verify
1615
import org.mockito.kotlin.whenever
1716

@@ -21,56 +20,33 @@ class ContentScopeScriptsAddDocumentStartJavaScriptTest {
2120
var coroutineRule = CoroutineTestRule()
2221

2322
private val mockWebViewCompatContentScopeScripts: WebViewCompatContentScopeScripts = mock()
24-
private val mockWebViewCapabilityChecker: WebViewCapabilityChecker = mock()
25-
private val mockWebViewCompatWrapper: WebViewCompatWrapper = mock()
2623
private val mockWebView: WebView = mock()
2724
private val mockActiveContentScopeExperiments: ContentScopeExperiments = mock()
25+
private val mockAddDocumentStartScriptDelegate: AddDocumentStartScriptDelegate = mock()
26+
private val mockAddDocumentStartJavaScript: AddDocumentStartJavaScript = mock()
2827

2928
private lateinit var testee: ContentScopeScriptsAddDocumentStartJavaScript
3029

3130
@Before
3231
fun setUp() = runTest {
3332
whenever(mockActiveContentScopeExperiments.getActiveExperiments()).thenReturn(listOf())
33+
whenever(mockAddDocumentStartScriptDelegate.createPlugin(any())).thenReturn(mockAddDocumentStartJavaScript)
3434
testee = ContentScopeScriptsAddDocumentStartJavaScript(
3535
mockWebViewCompatContentScopeScripts,
36-
coroutineRule.testDispatcherProvider,
37-
mockWebViewCapabilityChecker,
38-
mockWebViewCompatWrapper,
3936
mockActiveContentScopeExperiments,
40-
coroutineRule.testScope,
37+
mockAddDocumentStartScriptDelegate,
4138
)
4239
}
4340

4441
@Test
45-
fun whenFeatureIsEnabledAndCapabilitySupportedThenCallScriptInjectionWithCorrectParams() = runTest {
46-
whenever(mockWebViewCompatContentScopeScripts.isEnabled()).thenReturn(true)
47-
whenever(mockWebViewCapabilityChecker.isSupported(any())).thenReturn(true)
48-
whenever(mockWebViewCompatContentScopeScripts.getScript(any())).thenReturn("script")
49-
50-
testee.addDocumentStartJavaScript(mockWebView)
51-
52-
verify(mockWebViewCompatWrapper).addDocumentStartJavaScript(mockWebView, "script", setOf("*"))
53-
}
54-
55-
@Test
56-
fun whenFeatureIsDisabledAndCapabilitySupportedThenDoNotCallScriptInjection() = runTest {
57-
whenever(mockWebViewCompatContentScopeScripts.isEnabled()).thenReturn(false)
58-
whenever(mockWebViewCapabilityChecker.isSupported(any())).thenReturn(true)
59-
whenever(mockWebViewCompatContentScopeScripts.getScript(any())).thenReturn("script")
60-
42+
fun whenAddDocumentStartJavaScriptCalledThenDelegateToCreatedPlugin() = runTest {
6143
testee.addDocumentStartJavaScript(mockWebView)
6244

63-
verify(mockWebViewCompatWrapper, never()).addDocumentStartJavaScript(any(), any(), any())
45+
verify(mockAddDocumentStartJavaScript).addDocumentStartJavaScript(mockWebView)
6446
}
6547

6648
@Test
67-
fun whenFeatureIsEnabledAndCapabilityNotSupportedThenDoNotCallScriptInjection() = runTest {
68-
whenever(mockWebViewCompatContentScopeScripts.isEnabled()).thenReturn(true)
69-
whenever(mockWebViewCapabilityChecker.isSupported(any())).thenReturn(false)
70-
whenever(mockWebViewCompatContentScopeScripts.getScript(any())).thenReturn("script")
71-
72-
testee.addDocumentStartJavaScript(mockWebView)
73-
74-
verify(mockWebViewCompatWrapper, never()).addDocumentStartJavaScript(any(), any(), any())
49+
fun whenConstructedThenCreatePluginWithCorrectStrategy() = runTest {
50+
verify(mockAddDocumentStartScriptDelegate).createPlugin(any())
7551
}
7652
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package com.duckduckgo.js.messaging.impl
2+
3+
import android.webkit.WebView
4+
import androidx.webkit.ScriptHandler
5+
import com.duckduckgo.app.browser.api.WebViewCapabilityChecker
6+
import com.duckduckgo.browser.api.webviewcompat.WebViewCompatWrapper
7+
import com.duckduckgo.common.test.CoroutineTestRule
8+
import com.duckduckgo.common.utils.DispatcherProvider
9+
import com.duckduckgo.js.messaging.api.AddDocumentStartJavaScript
10+
import com.duckduckgo.js.messaging.api.AddDocumentStartJavaScriptScriptStrategy
11+
import kotlinx.coroutines.test.runTest
12+
import org.junit.Before
13+
import org.junit.Rule
14+
import org.junit.Test
15+
import org.mockito.kotlin.any
16+
import org.mockito.kotlin.mock
17+
import org.mockito.kotlin.never
18+
import org.mockito.kotlin.verify
19+
import org.mockito.kotlin.whenever
20+
21+
class RealAddDocumentStartScriptDelegateTest {
22+
23+
@get:Rule
24+
var coroutineRule = CoroutineTestRule()
25+
26+
private val mockWebViewCapabilityChecker: WebViewCapabilityChecker = mock()
27+
private val mockWebViewCompatWrapper: WebViewCompatWrapper = mock()
28+
private val mockWebView: WebView = mock()
29+
private val mockScriptHandler: ScriptHandler = mock()
30+
private val mockDispatcherProvider: DispatcherProvider = mock()
31+
32+
private lateinit var testee: RealAddDocumentStartScriptDelegate
33+
private lateinit var plugin: AddDocumentStartJavaScript
34+
35+
@Before
36+
fun setUp() = runTest {
37+
whenever(mockDispatcherProvider.main()).thenReturn(coroutineRule.testDispatcher)
38+
testee = RealAddDocumentStartScriptDelegate(
39+
mockWebViewCapabilityChecker,
40+
coroutineRule.testScope,
41+
mockDispatcherProvider,
42+
mockWebViewCompatWrapper,
43+
)
44+
}
45+
46+
@Test
47+
fun whenFeatureEnabledAndCapabilitySupportedThenInjectScript() = runTest {
48+
val mockStrategy = createMockStrategy(canInject = true, scriptString = "test script")
49+
plugin = testee.createPlugin(mockStrategy)
50+
whenever(mockWebViewCapabilityChecker.isSupported(any())).thenReturn(true)
51+
whenever(mockWebViewCompatWrapper.addDocumentStartJavaScript(any(), any(), any())).thenReturn(mockScriptHandler)
52+
53+
plugin.addDocumentStartJavaScript(mockWebView)
54+
55+
verify(mockWebViewCompatWrapper).addDocumentStartJavaScript(mockWebView, "test script", setOf("*"))
56+
}
57+
58+
@Test
59+
fun whenFeatureDisabledThenDoNotInjectScript() = runTest {
60+
val mockStrategy = createMockStrategy(canInject = false, scriptString = "test script")
61+
plugin = testee.createPlugin(mockStrategy)
62+
whenever(mockWebViewCapabilityChecker.isSupported(any())).thenReturn(true)
63+
64+
plugin.addDocumentStartJavaScript(mockWebView)
65+
66+
verify(mockWebViewCompatWrapper, never()).addDocumentStartJavaScript(any(), any(), any())
67+
}
68+
69+
@Test
70+
fun whenCapabilityNotSupportedThenDoNotInjectScript() = runTest {
71+
val mockStrategy = createMockStrategy(canInject = true, scriptString = "test script")
72+
plugin = testee.createPlugin(mockStrategy)
73+
whenever(mockWebViewCapabilityChecker.isSupported(any())).thenReturn(false)
74+
75+
plugin.addDocumentStartJavaScript(mockWebView)
76+
77+
verify(mockWebViewCompatWrapper, never()).addDocumentStartJavaScript(any(), any(), any())
78+
}
79+
80+
@Test
81+
fun whenScriptStringSameAsCurrentThenDoNotReinject() = runTest {
82+
val mockStrategy = createMockStrategy(canInject = true, scriptString = "test script")
83+
plugin = testee.createPlugin(mockStrategy)
84+
whenever(mockWebViewCapabilityChecker.isSupported(any())).thenReturn(true)
85+
whenever(mockWebViewCompatWrapper.addDocumentStartJavaScript(any(), any(), any())).thenReturn(mockScriptHandler)
86+
87+
plugin.addDocumentStartJavaScript(mockWebView)
88+
plugin.addDocumentStartJavaScript(mockWebView)
89+
90+
verify(mockWebViewCompatWrapper).addDocumentStartJavaScript(mockWebView, "test script", setOf("*"))
91+
}
92+
93+
@Test
94+
fun whenScriptStringDifferentThenRemoveOldAndInjectNew() = runTest {
95+
val mockStrategy: AddDocumentStartJavaScriptScriptStrategy = mock()
96+
whenever(mockStrategy.canInject()).thenReturn(true)
97+
whenever(mockStrategy.getScriptString()).thenReturn("script 1")
98+
whenever(mockStrategy.allowedOriginRules).thenReturn(setOf("*"))
99+
plugin = testee.createPlugin(mockStrategy)
100+
whenever(mockWebViewCapabilityChecker.isSupported(any())).thenReturn(true)
101+
whenever(mockWebViewCompatWrapper.addDocumentStartJavaScript(any(), any(), any())).thenReturn(mockScriptHandler)
102+
103+
plugin.addDocumentStartJavaScript(mockWebView)
104+
105+
whenever(mockStrategy.getScriptString()).thenReturn("script 2")
106+
107+
plugin.addDocumentStartJavaScript(mockWebView)
108+
109+
verify(mockScriptHandler).remove()
110+
verify(mockWebViewCompatWrapper).addDocumentStartJavaScript(mockWebView, "script 2", setOf("*"))
111+
}
112+
113+
private fun createMockStrategy(canInject: Boolean, scriptString: String): AddDocumentStartJavaScriptScriptStrategy {
114+
return object : AddDocumentStartJavaScriptScriptStrategy {
115+
override suspend fun canInject(): Boolean = canInject
116+
override suspend fun getScriptString(): String = scriptString
117+
override val allowedOriginRules: Set<String> = setOf("*")
118+
}
119+
}
120+
}

0 commit comments

Comments
 (0)