Skip to content

Commit 467e1ca

Browse files
committed
Fix tests
1 parent 95ab626 commit 467e1ca

File tree

2 files changed

+51
-54
lines changed

2 files changed

+51
-54
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ class WebViewCompatWebCompatMessagingPlugin @Inject constructor(
218218
appCoroutineScope.launch {
219219
if (!webViewCompatContentScopeScripts.isEnabled()) {
220220
contentScopeScriptsJsMessaging.sendSubscriptionEvent(subscriptionEventData)
221+
return@launch
221222
}
222223

223224
val subscriptionEvent = SubscriptionEvent(

content-scope-scripts/content-scope-scripts-impl/src/test/java/com/duckduckgo/contentscopescripts/impl/messaging/WebViewCompatWebCompatMessagingPluginTest.kt

Lines changed: 50 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import com.duckduckgo.contentscopescripts.impl.WebViewCompatContentScopeScripts
2727
import com.duckduckgo.js.messaging.api.JsMessage
2828
import com.duckduckgo.js.messaging.api.ProcessResult
2929
import com.duckduckgo.js.messaging.api.ProcessResult.SendToConsumer
30+
import com.duckduckgo.js.messaging.api.SubscriptionEventData
3031
import com.duckduckgo.js.messaging.api.WebViewCompatMessageCallback
3132
import com.duckduckgo.js.messaging.api.WebViewCompatMessageHandler
3233
import junit.framework.TestCase.assertEquals
@@ -36,6 +37,7 @@ import org.junit.Before
3637
import org.junit.Rule
3738
import org.junit.Test
3839
import org.junit.runner.RunWith
40+
import org.mockito.ArgumentMatchers.anyString
3941
import org.mockito.kotlin.any
4042
import org.mockito.kotlin.eq
4143
import org.mockito.kotlin.mock
@@ -218,61 +220,50 @@ class WebViewCompatWebCompatMessagingPluginTest {
218220
verify(mockWebViewCompatWrapper).removeWebMessageListener(mockWebView, "contentScopeAdsjs")
219221
}
220222

221-
// @Test
222-
// fun `when posting message and adsjs is disabled then do not post message`() = runTest {
223-
// whenever(adsJsContentScopeScripts.isEnabled()).thenReturn(false)
224-
// val eventData = SubscriptionEventData("feature", "subscription", JSONObject())
225-
// givenInterfaceIsRegistered()
226-
// processInitialPing()
227-
//
228-
// val result = testee.postMessage(eventData)
229-
//
230-
// verify(mockWebView, never()).safePostMessage(any(), any())
231-
// assertFalse(result)
232-
// }
233-
//
234-
// @Test
235-
// fun `when posting message and adsjs is enabled but webView not registered then do not post message`() = runTest {
236-
// whenever(adsJsContentScopeScripts.isEnabled()).thenReturn(true)
237-
// val eventData = SubscriptionEventData("feature", "subscription", JSONObject())
238-
// processInitialPing()
239-
//
240-
// val result = testee.postMessage(eventData)
241-
//
242-
// verify(mockWebView, never()).safePostMessage(any(), any())
243-
// assertFalse(result)
244-
// }
245-
//
246-
// @Test
247-
// fun `when posting message and adsjs is enabled but initialPing not processes then do not post message`() = runTest {
248-
// whenever(adsJsContentScopeScripts.isEnabled()).thenReturn(true)
249-
// val eventData = SubscriptionEventData("feature", "subscription", JSONObject())
250-
//
251-
// val result = testee.postMessage(eventData)
252-
//
253-
// verify(mockWebView, never()).safePostMessage(any(), any())
254-
// assertFalse(result)
255-
// }
256-
//
257-
// @Test
258-
// fun `when posting message after getting initialPing and adsjs is enabled then post message`() = runTest {
259-
// whenever(adsJsContentScopeScripts.isEnabled()).thenReturn(true)
260-
// val eventData = SubscriptionEventData("feature", "subscription", JSONObject())
261-
// givenInterfaceIsRegistered()
262-
// processInitialPing()
263-
// verify(mockWebView, never()).postWebMessage(any(), any())
264-
//
265-
// val result = testee.postMessage(eventData)
266-
//
267-
// verify(mockWebView).safePostMessage(any(), any())
268-
// assertTrue(result)
269-
// }
270-
271-
private fun processInitialPing() = runTest {
272-
val message = """
273-
{"context":"contentScopeScripts","featureName":"messaging","id":"debugId","method":"initialPing","params":{}}
223+
@Test
224+
fun `when posting message and adsjs is disabled then do not post message`() = runTest {
225+
whenever(webViewCompatContentScopeScripts.isEnabled()).thenReturn(false)
226+
val eventData = SubscriptionEventData("feature", "subscription", JSONObject())
227+
givenInterfaceIsRegistered()
228+
229+
testee.postMessage(eventData)
230+
231+
verify(mockReplyProxy, never()).postMessage(anyString())
232+
}
233+
234+
@Test
235+
fun `when posting message and adsjs is enabled but webView not registered then do not post message`() = runTest {
236+
whenever(webViewCompatContentScopeScripts.isEnabled()).thenReturn(true)
237+
val eventData = SubscriptionEventData("feature", "subscription", JSONObject())
238+
239+
testee.postMessage(eventData)
240+
241+
verify(mockReplyProxy, never()).postMessage(anyString())
242+
}
243+
244+
@Test
245+
fun `when posting message and adsjs is enabled but initialPing not processes then do not post message`() = runTest {
246+
whenever(webViewCompatContentScopeScripts.isEnabled()).thenReturn(true)
247+
val eventData = SubscriptionEventData("feature", "subscription", JSONObject())
248+
249+
testee.postMessage(eventData)
250+
251+
verify(mockReplyProxy, never()).postMessage(anyString())
252+
}
253+
254+
@Test
255+
fun `when posting message after getting initialPing and adsjs is enabled then post message`() = runTest {
256+
whenever(webViewCompatContentScopeScripts.isEnabled()).thenReturn(true)
257+
val eventData = SubscriptionEventData("feature", "subscription", JSONObject())
258+
givenInterfaceIsRegistered()
259+
val expectedMessage = """
260+
{"context":"contentScopeScripts","featureName":"feature","params":{},"subscriptionName":"subscription"}
274261
""".trimIndent()
275-
testee.process(message, callback, mockReplyProxy)
262+
263+
verify(mockWebView, never()).postWebMessage(any(), any())
264+
265+
testee.postMessage(eventData)
266+
verify(mockReplyProxy).postMessage(expectedMessage)
276267
}
277268

278269
private val callback = object : WebViewCompatMessageCallback {
@@ -290,5 +281,10 @@ class WebViewCompatWebCompatMessagingPluginTest {
290281

291282
private fun givenInterfaceIsRegistered() = runTest {
292283
testee.register(callback, mockWebView)
284+
val initialPingMessage =
285+
"""
286+
{"context":"contentScopeScripts","featureName":"messaging","id":"debugId","method":"initialPing","params":{}}
287+
""".trimIndent()
288+
testee.process(initialPingMessage, callback, mockReplyProxy)
293289
}
294290
}

0 commit comments

Comments
 (0)