Skip to content

Commit 412351a

Browse files
committed
Fix tests
1 parent 910f174 commit 412351a

File tree

3 files changed

+67
-56
lines changed

3 files changed

+67
-56
lines changed

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ import kotlinx.coroutines.flow.MutableSharedFlow
101101
import kotlinx.coroutines.launch
102102
import kotlinx.coroutines.test.TestScope
103103
import kotlinx.coroutines.test.runTest
104+
import org.json.JSONObject
104105
import org.junit.Before
105106
import org.junit.Rule
106107
import org.junit.Test
@@ -377,6 +378,17 @@ class BrowserWebViewClientTest {
377378
assertFalse(fakeMessagingPlugins.plugin.registered)
378379
}
379380

381+
@Test
382+
fun whenPostMessageThenCallPostMessage() = runTest {
383+
val data = SubscriptionEventData("feature", "method", JSONObject())
384+
385+
assertFalse(fakeMessagingPlugins.plugin.messagePosted)
386+
387+
testee.postMessage(data)
388+
389+
assertTrue(fakeMessagingPlugins.plugin.messagePosted)
390+
}
391+
380392
@UiThreadTest
381393
@Test
382394
fun whenOnReceivedHttpAuthRequestThenListenerNotified() {
@@ -1338,6 +1350,9 @@ class BrowserWebViewClientTest {
13381350
var registered = false
13391351
private set
13401352

1353+
var messagePosted = false
1354+
private set
1355+
13411356
override fun unregister(webView: WebView) {
13421357
registered = false
13431358
}
@@ -1349,9 +1364,8 @@ class BrowserWebViewClientTest {
13491364
registered = true
13501365
}
13511366

1352-
// TODO (cbarreiro) Test message posting
13531367
override fun postMessage(subscriptionEventData: SubscriptionEventData) {
1354-
TODO("Not yet implemented")
1368+
messagePosted = true
13551369
}
13561370
}
13571371

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

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

222223
val subscriptionEvent = SubscriptionEvent(

content-scope-scripts/content-scope-scripts-impl/src/test/java/com/duckduckgo/contentscopescripts/impl/messaging/ContentScopeScriptsWebMessagingPluginTest.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 ContentScopeScriptsWebMessagingPluginTest {
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 ContentScopeScriptsWebMessagingPluginTest {
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)