Skip to content

Commit 512911d

Browse files
committed
Test subscriptions
1 parent 05b1f7c commit 512911d

File tree

2 files changed

+71
-5
lines changed

2 files changed

+71
-5
lines changed

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ class WebCompatMessagingPlugin @Inject constructor(
5353
private val globalHandlers: PluginPoint<GlobalContentScopeJsMessageHandlersPlugin>,
5454
private val adsJsContentScopeScripts: AdsJsContentScopeScripts,
5555
private val dispatcherProvider: DispatcherProvider,
56-
private val contentScopeScriptsFeature: ContentScopeScriptsFeature,
5756
) : WebMessagingPlugin {
5857

5958
private val moshi = Moshi.Builder().add(JSONObjectAdapter()).build()
@@ -162,7 +161,7 @@ class WebCompatMessagingPlugin @Inject constructor(
162161

163162
//TODO (cbarreiro) temporary, remove
164163
val newWebCompatApisEnabled = runBlocking {
165-
contentScopeScriptsFeature.useNewWebCompatApis().isEnabled()
164+
adsJsContentScopeScripts.isEnabled()
166165
}
167166

168167
if (!newWebCompatApisEnabled) {
@@ -178,9 +177,8 @@ class WebCompatMessagingPlugin @Inject constructor(
178177
moshi.adapter(SubscriptionEvent::class.java).toJson(it)
179178
}
180179

181-
globalReplyProxy?.postMessage(subscriptionEvent)?.let {
182-
true
183-
} ?: false
180+
globalReplyProxy?.postMessage(subscriptionEvent)
181+
true
184182
}.getOrElse { false }
185183
}
186184
}

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)