@@ -28,9 +28,12 @@ import com.duckduckgo.contentscopescripts.impl.AdsJsContentScopeScripts
28
28
import com.duckduckgo.js.messaging.api.AdsjsJsMessageCallback
29
29
import com.duckduckgo.js.messaging.api.AdsjsMessageHandler
30
30
import com.duckduckgo.js.messaging.api.JsMessage
31
+ import com.duckduckgo.js.messaging.api.SubscriptionEventData
31
32
import junit.framework.TestCase.assertEquals
32
33
import kotlinx.coroutines.test.runTest
33
34
import org.json.JSONObject
35
+ import org.junit.Assert.assertFalse
36
+ import org.junit.Assert.assertTrue
34
37
import org.junit.Before
35
38
import org.junit.Rule
36
39
import org.junit.Test
@@ -221,6 +224,67 @@ class AdsjsContentScopeScriptsJsMessagingTest {
221
224
verify(mockWebView).safeRemoveWebMessageListener(any())
222
225
}
223
226
227
+ @Test
228
+ fun `when posting message and adsjs is disabled then do not post message` () = runTest {
229
+ whenever(adsJsContentScopeScripts.isEnabled()).thenReturn(false )
230
+ val eventData = SubscriptionEventData (" feature" , " subscription" , JSONObject ())
231
+ register()
232
+ processInitialPing()
233
+
234
+ val result = contentScopeScriptsJsMessaging.postMessage(eventData)
235
+
236
+ verify(mockWebView, never()).safePostMessage(any(), any())
237
+ assertFalse(result)
238
+ }
239
+
240
+ @Test
241
+ fun `when posting message and adsjs is enabled but webView not registered then do not post message` () = runTest {
242
+ whenever(adsJsContentScopeScripts.isEnabled()).thenReturn(true )
243
+ val eventData = SubscriptionEventData (" feature" , " subscription" , JSONObject ())
244
+ processInitialPing()
245
+
246
+ val result = contentScopeScriptsJsMessaging.postMessage(eventData)
247
+
248
+ verify(mockWebView, never()).safePostMessage(any(), any())
249
+ assertFalse(result)
250
+ }
251
+
252
+ @Test
253
+ fun `when posting message and adsjs is enabled but initialPing not processes then do not post message` () = runTest {
254
+ whenever(adsJsContentScopeScripts.isEnabled()).thenReturn(true )
255
+ val eventData = SubscriptionEventData (" feature" , " subscription" , JSONObject ())
256
+
257
+ val result = contentScopeScriptsJsMessaging.postMessage(eventData)
258
+
259
+ verify(mockWebView, never()).safePostMessage(any(), any())
260
+ assertFalse(result)
261
+ }
262
+
263
+ @Test
264
+ fun `when posting message after getting initialPing and adsjs is enabled then post message` () = runTest {
265
+ whenever(adsJsContentScopeScripts.isEnabled()).thenReturn(true )
266
+ val eventData = SubscriptionEventData (" feature" , " subscription" , JSONObject ())
267
+ register()
268
+ processInitialPing()
269
+ verify(mockWebView, never()).postWebMessage(any(), any())
270
+
271
+ val result = contentScopeScriptsJsMessaging.postMessage(eventData)
272
+
273
+ verify(mockWebView).safePostMessage(any(), any())
274
+ assertTrue(result)
275
+ }
276
+
277
+ private fun register () = runTest {
278
+ contentScopeScriptsJsMessaging.register(mockWebView, callback)
279
+ }
280
+
281
+ private fun processInitialPing () = runTest {
282
+ val message = """
283
+ {"context":"contentScopeScripts","featureName":"messaging","id":"debugId","method":"initialPing","params":{}}
284
+ """ .trimIndent()
285
+ contentScopeScriptsJsMessaging.process(message, callback, mockReplyProxy)
286
+ }
287
+
224
288
private val callback = object : AdsjsJsMessageCallback {
225
289
var counter = 0
226
290
override fun process (
0 commit comments