@@ -23,6 +23,7 @@ import mozilla.components.support.test.eq
23
23
import mozilla.components.support.test.mock
24
24
import mozilla.components.support.test.robolectric.testContext
25
25
import mozilla.components.support.test.whenever
26
+ import org.json.JSONException
26
27
import org.json.JSONObject
27
28
import org.junit.Assert.assertEquals
28
29
import org.junit.Assert.assertFalse
@@ -155,6 +156,47 @@ class FxaWebChannelFeatureTest {
155
156
assertTrue(FxaWebChannelFeature .ports.containsValue(port))
156
157
}
157
158
159
+ @Test
160
+ fun `COMMAND_STATUS configured with CWTS must provided a boolean=true flag to the web-channel` () {
161
+ val sessionManager = mock<SessionManager >()
162
+ val accountManager = mock<FxaAccountManager >()
163
+ val session = mock<Session >()
164
+ val engineSession = mock<EngineSession >()
165
+ val ext = mock<WebExtension >()
166
+ val messageHandler = argumentCaptor<MessageHandler >()
167
+ val responseToTheWebChannel = argumentCaptor<JSONObject >()
168
+ val port = mock<Port >()
169
+ val expectedEngines = setOf (SyncEngine .HISTORY )
170
+
171
+ FxaWebChannelFeature .installedWebExt = ext
172
+
173
+ whenever(accountManager.supportedSyncEngines()).thenReturn(expectedEngines)
174
+ whenever(sessionManager.getOrCreateEngineSession(session)).thenReturn(engineSession)
175
+ whenever(port.engineSession).thenReturn(engineSession)
176
+
177
+ val webchannelFeature =
178
+ spy(FxaWebChannelFeature (testContext, null , mock(), sessionManager, accountManager, setOf (FxaCapability .CHOOSE_WHAT_TO_SYNC )))
179
+ webchannelFeature.onSessionAdded(session)
180
+ verify(ext).registerContentMessageHandler(
181
+ eq(engineSession),
182
+ eq(FxaWebChannelFeature .WEB_CHANNEL_EXTENSION_ID ),
183
+ messageHandler.capture()
184
+ )
185
+ messageHandler.value.onPortConnected(port)
186
+
187
+ val requestFromTheWebChannel = JSONObject (
188
+ """ {
189
+ "message":{
190
+ "command": "fxaccounts:fxa_status",
191
+ "messageId":123
192
+ }
193
+ }""" .trimIndent()
194
+ )
195
+ messageHandler.value.onPortMessage(requestFromTheWebChannel, mock())
196
+ verify(port).postMessage(responseToTheWebChannel.capture())
197
+ assertTrue(responseToTheWebChannel.value.getCWTSSupport()!! )
198
+ }
199
+
158
200
// Receiving and responding a fxa-status message if sync is configured with one engine
159
201
@Test
160
202
fun `COMMAND_STATUS configured with one engine must be provided to the web-channel` () {
@@ -197,8 +239,9 @@ class FxaWebChannelFeatureTest {
197
239
messageHandler.value.onPortMessage(requestFromTheWebChannel, mock())
198
240
verify(port).postMessage(responseToTheWebChannel.capture())
199
241
200
- val capabilitiesFromWebChannel = responseToTheWebChannel.value.getCapabilities ()
242
+ val capabilitiesFromWebChannel = responseToTheWebChannel.value.getSupportedEngines ()
201
243
assertTrue(capabilitiesFromWebChannel.size == 1 )
244
+ assertNull(responseToTheWebChannel.value.getCWTSSupport())
202
245
203
246
assertTrue(responseToTheWebChannel.value.isSignedInUserNull())
204
247
}
@@ -245,11 +288,12 @@ class FxaWebChannelFeatureTest {
245
288
messageHandler.value.onPortMessage(requestFromTheWebChannel, mock())
246
289
verify(port).postMessage(responseToTheWebChannel.capture())
247
290
248
- val capabilitiesFromWebChannel = responseToTheWebChannel.value.getCapabilities ()
291
+ val capabilitiesFromWebChannel = responseToTheWebChannel.value.getSupportedEngines ()
249
292
assertTrue(expectedEngines.all {
250
293
capabilitiesFromWebChannel.contains(it.nativeName)
251
294
})
252
295
296
+ assertNull(responseToTheWebChannel.value.getCWTSSupport())
253
297
assertTrue(responseToTheWebChannel.value.isSignedInUserNull())
254
298
}
255
299
@@ -298,11 +342,12 @@ class FxaWebChannelFeatureTest {
298
342
messageHandler.value.onPortMessage(requestFromTheWebChannel, mock())
299
343
verify(port).postMessage(responseToTheWebChannel.capture())
300
344
301
- val capabilitiesFromWebChannel = responseToTheWebChannel.value.getCapabilities ()
345
+ val capabilitiesFromWebChannel = responseToTheWebChannel.value.getSupportedEngines ()
302
346
assertTrue(expectedEngines.all {
303
347
capabilitiesFromWebChannel.contains(it.nativeName)
304
348
})
305
349
350
+ assertNull(responseToTheWebChannel.value.getCWTSSupport())
306
351
assertTrue(responseToTheWebChannel.value.isSignedInUserNull())
307
352
}
308
353
@@ -349,11 +394,12 @@ class FxaWebChannelFeatureTest {
349
394
messageHandler.value.onPortMessage(requestFromTheWebChannel, mock())
350
395
verify(port).postMessage(responseToTheWebChannel.capture())
351
396
352
- val capabilitiesFromWebChannel = responseToTheWebChannel.value.getCapabilities ()
397
+ val capabilitiesFromWebChannel = responseToTheWebChannel.value.getSupportedEngines ()
353
398
assertTrue(expectedEngines.all {
354
399
capabilitiesFromWebChannel.contains(it.nativeName)
355
400
})
356
401
402
+ assertNull(responseToTheWebChannel.value.getCWTSSupport())
357
403
assertTrue(responseToTheWebChannel.value.isSignedInUserNull())
358
404
}
359
405
@@ -400,11 +446,12 @@ class FxaWebChannelFeatureTest {
400
446
messageHandler.value.onPortMessage(requestFromTheWebChannel, mock())
401
447
verify(port).postMessage(responseToTheWebChannel.capture())
402
448
403
- val capabilitiesFromWebChannel = responseToTheWebChannel.value.getCapabilities ()
449
+ val capabilitiesFromWebChannel = responseToTheWebChannel.value.getSupportedEngines ()
404
450
assertTrue(expectedEngines.all {
405
451
capabilitiesFromWebChannel.contains(it.nativeName)
406
452
})
407
453
454
+ assertNull(responseToTheWebChannel.value.getCWTSSupport())
408
455
assertTrue(responseToTheWebChannel.value.isSignedInUserNull())
409
456
}
410
457
@@ -448,8 +495,8 @@ class FxaWebChannelFeatureTest {
448
495
messageHandler.value.onPortMessage(requestFromTheWebChannel, mock())
449
496
verify(port).postMessage(responseToTheWebChannel.capture())
450
497
451
- val capabilitiesFromWebChannel = responseToTheWebChannel.value.getCapabilities( )
452
-
498
+ assertNull( responseToTheWebChannel.value.getCWTSSupport() )
499
+ val capabilitiesFromWebChannel = responseToTheWebChannel.value.getSupportedEngines()
453
500
assertTrue(capabilitiesFromWebChannel.isEmpty())
454
501
}
455
502
@@ -589,7 +636,7 @@ class FxaWebChannelFeatureTest {
589
636
return webchannelFeature
590
637
}
591
638
592
- private fun JSONObject.getCapabilities (): List <String > {
639
+ private fun JSONObject.getSupportedEngines (): List <String > {
593
640
val engines = this .getJSONObject(" message" )
594
641
.getJSONObject(" data" )
595
642
.getJSONObject(" capabilities" )
@@ -602,6 +649,17 @@ class FxaWebChannelFeatureTest {
602
649
return list
603
650
}
604
651
652
+ private fun JSONObject.getCWTSSupport (): Boolean? {
653
+ return try {
654
+ this .getJSONObject(" message" )
655
+ .getJSONObject(" data" )
656
+ .getJSONObject(" capabilities" )
657
+ .getBoolean(" choose_what_to_sync" )
658
+ } catch (e: JSONException ) {
659
+ null
660
+ }
661
+ }
662
+
605
663
private fun JSONObject.isSignedInUserNull (): Boolean {
606
664
return this .getJSONObject(" message" )
607
665
.getJSONObject(" data" )
0 commit comments