17
17
package com.duckduckgo.contentscopescripts.impl.messaging
18
18
19
19
import androidx.test.ext.junit.runners.AndroidJUnit4
20
- import com.duckduckgo.app.browser.api.DuckDuckGoWebView
20
+ import androidx.webkit.WebViewCompat.WebMessageListener
21
21
import com.duckduckgo.common.test.CoroutineTestRule
22
22
import com.duckduckgo.common.utils.plugins.PluginPoint
23
23
import com.duckduckgo.contentscopescripts.api.GlobalContentScopeJsMessageHandlersPlugin
@@ -28,27 +28,24 @@ import com.duckduckgo.js.messaging.api.JsMessage
28
28
import com.duckduckgo.js.messaging.api.JsMessageCallback
29
29
import com.duckduckgo.js.messaging.api.WebCompatMessageHandler
30
30
import junit.framework.TestCase.assertEquals
31
+ import junit.framework.TestCase.assertNull
31
32
import kotlinx.coroutines.test.runTest
32
33
import org.json.JSONObject
33
34
import org.junit.Before
34
35
import org.junit.Rule
35
36
import org.junit.Test
36
37
import org.junit.runner.RunWith
37
- import org.mockito.kotlin.any
38
38
import org.mockito.kotlin.mock
39
- import org.mockito.kotlin.never
40
- import org.mockito.kotlin.verify
41
39
import org.mockito.kotlin.whenever
42
40
43
41
@RunWith(AndroidJUnit4 ::class )
44
- class AdsjsContentScopeScriptsJsMessagingTest {
42
+ class WebCompatMessagingPluginTest {
45
43
@get:Rule var coroutineRule = CoroutineTestRule ()
46
44
47
- private val mockWebView: DuckDuckGoWebView = mock()
48
45
private val adsJsContentScopeScripts: AdsJsContentScopeScripts = mock()
49
46
private val handlers: PluginPoint <WebCompatContentScopeJsMessageHandlersPlugin > = FakePluginPoint ()
50
47
private val globalHandlers: PluginPoint <GlobalContentScopeJsMessageHandlersPlugin > = FakeGlobalHandlersPluginPoint ()
51
- private lateinit var contentScopeScriptsJsMessaging : AdsjsContentScopeMessaging
48
+ private lateinit var testee : WebCompatMessagingPlugin
52
49
53
50
private class FakePluginPoint : PluginPoint <WebCompatContentScopeJsMessageHandlersPlugin > {
54
51
override fun getPlugins (): Collection <WebCompatContentScopeJsMessageHandlersPlugin > {
@@ -98,7 +95,7 @@ class AdsjsContentScopeScriptsJsMessagingTest {
98
95
@Before
99
96
fun setUp () = runTest {
100
97
whenever(adsJsContentScopeScripts.isEnabled()).thenReturn(true )
101
- contentScopeScriptsJsMessaging = AdsjsContentScopeMessaging (
98
+ testee = WebCompatMessagingPlugin (
102
99
handlers = handlers,
103
100
globalHandlers = globalHandlers,
104
101
adsJsContentScopeScripts = adsJsContentScopeScripts,
@@ -114,7 +111,7 @@ class AdsjsContentScopeScriptsJsMessagingTest {
114
111
{"context":"contentScopeScripts","featureName":"webCompat","id":"myId","method":"webShare","params":{}}
115
112
""" .trimIndent()
116
113
117
- contentScopeScriptsJsMessaging .process(message, callback)
114
+ testee .process(message, callback)
118
115
119
116
assertEquals(1 , callback.counter)
120
117
}
@@ -123,7 +120,7 @@ class AdsjsContentScopeScriptsJsMessagingTest {
123
120
fun `when processing unknown message do nothing` () = runTest {
124
121
givenInterfaceIsRegistered()
125
122
126
- contentScopeScriptsJsMessaging .process(" " , callback)
123
+ testee .process(" " , callback)
127
124
128
125
assertEquals(0 , callback.counter)
129
126
}
@@ -136,7 +133,7 @@ class AdsjsContentScopeScriptsJsMessagingTest {
136
133
{"context":"contentScopeScripts","featureName":"test","id":"myId","method":"webShare","params":{}}
137
134
""" .trimIndent()
138
135
139
- contentScopeScriptsJsMessaging .process(message, callback)
136
+ testee .process(message, callback)
140
137
141
138
assertEquals(0 , callback.counter)
142
139
}
@@ -149,7 +146,7 @@ class AdsjsContentScopeScriptsJsMessagingTest {
149
146
{"context":"contentScopeScripts","webCompat":"test","method":"webShare","params":{}}
150
147
""" .trimIndent()
151
148
152
- contentScopeScriptsJsMessaging .process(message, callback)
149
+ testee .process(message, callback)
153
150
154
151
assertEquals(0 , callback.counter)
155
152
}
@@ -162,7 +159,7 @@ class AdsjsContentScopeScriptsJsMessagingTest {
162
159
{"context":"contentScopeScripts","featureName":"debugFeature","id":"debugId","method":"addDebugFlag","params":{}}
163
160
""" .trimIndent()
164
161
165
- contentScopeScriptsJsMessaging .process(message, callback)
162
+ testee .process(message, callback)
166
163
167
164
assertEquals(1 , callback.counter)
168
165
}
@@ -171,36 +168,67 @@ class AdsjsContentScopeScriptsJsMessagingTest {
171
168
fun `when registering and adsjs is disabled then do not register` () = runTest {
172
169
whenever(adsJsContentScopeScripts.isEnabled()).thenReturn(false )
173
170
174
- contentScopeScriptsJsMessaging.register(mockWebView, callback)
171
+ var capturedObjectName: String? = null
172
+ var capturedAllowedOriginRules: Set <String >? = null
173
+ val registerer: suspend (objectName: String , allowedOriginRules: Set <String >, webMessageListener: WebMessageListener ) -> Boolean =
174
+ { objectName, allowedOriginRules, webMessageListener ->
175
+ capturedObjectName = objectName
176
+ capturedAllowedOriginRules = allowedOriginRules
177
+ true
178
+ }
179
+
180
+ testee.register(callback, registerer)
175
181
176
- verify(mockWebView, never()).safeAddWebMessageListener(any(), any(), any())
182
+ assertNull(capturedObjectName)
183
+ assertNull(capturedAllowedOriginRules)
177
184
}
178
185
179
186
@Test
180
187
fun `when registering and adsjs is enabled then register` () = runTest {
181
188
whenever(adsJsContentScopeScripts.isEnabled()).thenReturn(true )
182
189
183
- contentScopeScriptsJsMessaging.register(mockWebView, callback)
190
+ var capturedObjectName: String? = null
191
+ var capturedAllowedOriginRules: Set <String >? = null
192
+ val registerer: suspend (objectName: String , allowedOriginRules: Set <String >, webMessageListener: WebMessageListener ) -> Boolean =
193
+ { objectName, allowedOriginRules, webMessageListener ->
194
+ capturedObjectName = objectName
195
+ capturedAllowedOriginRules = allowedOriginRules
196
+ true
197
+ }
198
+
199
+ testee.register(callback, registerer)
184
200
185
- verify(mockWebView).safeAddWebMessageListener(any(), any(), any())
201
+ assertEquals(" contentScopeAdsjs" , capturedObjectName)
202
+ assertEquals(setOf (" *" ), capturedAllowedOriginRules)
186
203
}
187
204
188
205
@Test
189
206
fun `when unregistering and adsjs is disabled then do not unregister` () = runTest {
190
207
whenever(adsJsContentScopeScripts.isEnabled()).thenReturn(false )
208
+ var capturedObjectName: String? = null
209
+ val unregisterer: suspend (objectName: String ) -> Boolean =
210
+ { objectName ->
211
+ capturedObjectName = objectName
212
+ true
213
+ }
191
214
192
- contentScopeScriptsJsMessaging .unregister(mockWebView )
215
+ testee .unregister(unregisterer )
193
216
194
- verify(mockWebView, never()).safeRemoveWebMessageListener(any() )
217
+ assertNull(capturedObjectName )
195
218
}
196
219
197
220
@Test
198
221
fun `when unregistering and adsjs is enabled then unregister` () = runTest {
199
222
whenever(adsJsContentScopeScripts.isEnabled()).thenReturn(true )
223
+ var capturedObjectName: String? = null
224
+ val unregisterer: suspend (objectName: String ) -> Boolean = { objectName ->
225
+ capturedObjectName = objectName
226
+ true
227
+ }
200
228
201
- contentScopeScriptsJsMessaging .unregister(mockWebView )
229
+ testee .unregister(unregisterer )
202
230
203
- verify(mockWebView).safeRemoveWebMessageListener(any() )
231
+ assertEquals( " contentScopeAdsjs " , capturedObjectName )
204
232
}
205
233
206
234
private val callback = object : JsMessageCallback () {
@@ -211,6 +239,6 @@ class AdsjsContentScopeScriptsJsMessagingTest {
211
239
}
212
240
213
241
private fun givenInterfaceIsRegistered () = runTest {
214
- contentScopeScriptsJsMessaging .register(mockWebView, callback)
242
+ testee .register(callback) { _, _, _ -> true }
215
243
}
216
244
}
0 commit comments