Skip to content

Commit 6b8f7c6

Browse files
committed
Test subscriptions
1 parent 9282a59 commit 6b8f7c6

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,8 @@ class WebCompatMessagingPlugin @Inject constructor(
174174
moshi.adapter(SubscriptionEvent::class.java).toJson(it)
175175
}
176176

177-
globalReplyProxy?.postMessage(subscriptionEvent)?.let {
178-
true
179-
} ?: false
177+
globalReplyProxy?.postMessage(subscriptionEvent)
178+
true
180179
}.getOrElse { false }
181180
}
182181
}

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

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,27 @@ import com.duckduckgo.contentscopescripts.api.GlobalContentScopeJsMessageHandler
2525
import com.duckduckgo.contentscopescripts.api.GlobalJsMessageHandler
2626
import com.duckduckgo.contentscopescripts.api.WebCompatContentScopeJsMessageHandlersPlugin
2727
import com.duckduckgo.contentscopescripts.impl.AdsJsContentScopeScripts
28+
import com.duckduckgo.contentscopescripts.impl.ContentScopeScriptsFeature
2829
import com.duckduckgo.js.messaging.api.JsMessage
30+
import com.duckduckgo.js.messaging.api.SubscriptionEventData
2931
import com.duckduckgo.js.messaging.api.WebCompatMessageHandler
3032
import com.duckduckgo.js.messaging.api.WebViewCompatMessageCallback
3133
import junit.framework.TestCase.assertEquals
3234
import junit.framework.TestCase.assertNull
3335
import kotlinx.coroutines.test.runTest
3436
import org.json.JSONObject
37+
import org.junit.Assert.assertFalse
38+
import org.junit.Assert.assertTrue
39+
import org.junit.Assert.assertFalse
40+
import org.junit.Assert.assertTrue
3541
import org.junit.Before
3642
import org.junit.Rule
3743
import org.junit.Test
3844
import org.junit.runner.RunWith
45+
import org.mockito.kotlin.any
3946
import org.mockito.kotlin.mock
47+
import org.mockito.kotlin.never
48+
import org.mockito.kotlin.verify
4049
import org.mockito.kotlin.whenever
4150

4251
@RunWith(AndroidJUnit4::class)
@@ -247,6 +256,65 @@ class WebCompatMessagingPluginTest {
247256
assertEquals("contentScopeAdsjs", capturedObjectName)
248257
}
249258

259+
// @Test
260+
// fun `when posting message and adsjs is disabled then do not post message`() = runTest {
261+
// whenever(adsJsContentScopeScripts.isEnabled()).thenReturn(false)
262+
// val eventData = SubscriptionEventData("feature", "subscription", JSONObject())
263+
// givenInterfaceIsRegistered()
264+
// processInitialPing()
265+
//
266+
// val result = testee.postMessage(eventData)
267+
//
268+
// verify(mockWebView, never()).safePostMessage(any(), any())
269+
// assertFalse(result)
270+
// }
271+
//
272+
// @Test
273+
// fun `when posting message and adsjs is enabled but webView not registered then do not post message`() = runTest {
274+
// whenever(adsJsContentScopeScripts.isEnabled()).thenReturn(true)
275+
// val eventData = SubscriptionEventData("feature", "subscription", JSONObject())
276+
// processInitialPing()
277+
//
278+
// val result = testee.postMessage(eventData)
279+
//
280+
// verify(mockWebView, never()).safePostMessage(any(), any())
281+
// assertFalse(result)
282+
// }
283+
//
284+
// @Test
285+
// fun `when posting message and adsjs is enabled but initialPing not processes then do not post message`() = runTest {
286+
// whenever(adsJsContentScopeScripts.isEnabled()).thenReturn(true)
287+
// val eventData = SubscriptionEventData("feature", "subscription", JSONObject())
288+
//
289+
// val result = testee.postMessage(eventData)
290+
//
291+
// verify(mockWebView, never()).safePostMessage(any(), any())
292+
// assertFalse(result)
293+
// }
294+
//
295+
// @Test
296+
// fun `when posting message after getting initialPing and adsjs is enabled then post message`() = runTest {
297+
// whenever(adsJsContentScopeScripts.isEnabled()).thenReturn(true)
298+
// val eventData = SubscriptionEventData("feature", "subscription", JSONObject())
299+
// givenInterfaceIsRegistered()
300+
// processInitialPing()
301+
// verify(mockWebView, never()).postWebMessage(any(), any())
302+
//
303+
// val result = testee.postMessage(eventData)
304+
//
305+
// verify(mockWebView).safePostMessage(any(), any())
306+
// assertTrue(result)
307+
// }
308+
309+
private fun processInitialPing() = runTest {
310+
val message = """
311+
{"context":"contentScopeScripts","featureName":"messaging","id":"debugId","method":"initialPing","params":{}}
312+
""".trimIndent()
313+
testee.process(message, callback, mockReplyProxy)
314+
}
315+
316+
317+
250318
private val callback = object : WebViewCompatMessageCallback() {
251319
var counter = 0
252320
override fun process(

0 commit comments

Comments
 (0)