forked from mozilla-mobile/firefox-android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GeckoEngineTest.kt
2592 lines (2036 loc) · 108 KB
/
GeckoEngineTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package mozilla.components.browser.engine.gecko
import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.os.Looper.getMainLooper
import androidx.test.ext.junit.runners.AndroidJUnit4
import mozilla.components.browser.engine.gecko.ext.getAntiTrackingPolicy
import mozilla.components.browser.engine.gecko.mediaquery.toGeckoValue
import mozilla.components.browser.engine.gecko.serviceworker.GeckoServiceWorkerDelegate
import mozilla.components.browser.engine.gecko.util.SpeculativeEngineSession
import mozilla.components.browser.engine.gecko.util.SpeculativeSessionObserver
import mozilla.components.browser.engine.gecko.webextension.GeckoWebExtensionException
import mozilla.components.browser.engine.gecko.webextension.mockNativeWebExtension
import mozilla.components.browser.engine.gecko.webextension.mockNativeWebExtensionMetaData
import mozilla.components.concept.engine.DefaultSettings
import mozilla.components.concept.engine.Engine
import mozilla.components.concept.engine.EngineSession
import mozilla.components.concept.engine.EngineSession.SafeBrowsingPolicy
import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy
import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy.CookiePolicy
import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy.TrackingCategory
import mozilla.components.concept.engine.UnsupportedSettingException
import mozilla.components.concept.engine.content.blocking.TrackerLog
import mozilla.components.concept.engine.mediaquery.PreferredColorScheme
import mozilla.components.concept.engine.serviceworker.ServiceWorkerDelegate
import mozilla.components.concept.engine.utils.EngineReleaseChannel
import mozilla.components.concept.engine.webextension.Action
import mozilla.components.concept.engine.webextension.WebExtension
import mozilla.components.concept.engine.webextension.WebExtensionDelegate
import mozilla.components.concept.engine.webextension.WebExtensionException
import mozilla.components.concept.engine.webextension.WebExtensionInstallException
import mozilla.components.support.test.any
import mozilla.components.support.test.argumentCaptor
import mozilla.components.support.test.eq
import mozilla.components.support.test.mock
import mozilla.components.support.test.robolectric.testContext
import mozilla.components.support.test.whenever
import mozilla.components.test.ReflectionUtils
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNotSame
import org.junit.Assert.assertNull
import org.junit.Assert.assertSame
import org.junit.Assert.assertTrue
import org.junit.Assert.fail
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers.anyBoolean
import org.mockito.ArgumentMatchers.anyFloat
import org.mockito.ArgumentMatchers.anyInt
import org.mockito.Mockito.doReturn
import org.mockito.Mockito.never
import org.mockito.Mockito.reset
import org.mockito.Mockito.spy
import org.mockito.Mockito.times
import org.mockito.Mockito.verify
import org.mozilla.geckoview.ContentBlocking
import org.mozilla.geckoview.ContentBlocking.CookieBehavior
import org.mozilla.geckoview.ContentBlockingController
import org.mozilla.geckoview.ContentBlockingController.Event
import org.mozilla.geckoview.GeckoResult
import org.mozilla.geckoview.GeckoRuntime
import org.mozilla.geckoview.GeckoRuntimeSettings
import org.mozilla.geckoview.GeckoSession
import org.mozilla.geckoview.GeckoWebExecutor
import org.mozilla.geckoview.OrientationController
import org.mozilla.geckoview.StorageController
import org.mozilla.geckoview.WebExtension.InstallException.ErrorCodes.ERROR_BLOCKLISTED
import org.mozilla.geckoview.WebExtension.InstallException.ErrorCodes.ERROR_CORRUPT_FILE
import org.mozilla.geckoview.WebExtension.InstallException.ErrorCodes.ERROR_FILE_ACCESS
import org.mozilla.geckoview.WebExtension.InstallException.ErrorCodes.ERROR_INCORRECT_HASH
import org.mozilla.geckoview.WebExtension.InstallException.ErrorCodes.ERROR_INCORRECT_ID
import org.mozilla.geckoview.WebExtension.InstallException.ErrorCodes.ERROR_NETWORK_FAILURE
import org.mozilla.geckoview.WebExtension.InstallException.ErrorCodes.ERROR_POSTPONED
import org.mozilla.geckoview.WebExtension.InstallException.ErrorCodes.ERROR_SIGNEDSTATE_REQUIRED
import org.mozilla.geckoview.WebExtension.InstallException.ErrorCodes.ERROR_UNEXPECTED_ADDON_TYPE
import org.mozilla.geckoview.WebExtension.InstallException.ErrorCodes.ERROR_USER_CANCELED
import org.mozilla.geckoview.WebExtensionController
import org.mozilla.geckoview.WebNotification
import org.mozilla.geckoview.WebPushController
import org.robolectric.Robolectric
import org.robolectric.Shadows.shadowOf
import java.io.IOException
import org.mozilla.geckoview.WebExtension as GeckoWebExtension
typealias GeckoInstallException = org.mozilla.geckoview.WebExtension.InstallException
@RunWith(AndroidJUnit4::class)
class GeckoEngineTest {
private lateinit var runtime: GeckoRuntime
private lateinit var context: Context
@Before
fun setup() {
runtime = mock()
whenever(runtime.settings).thenReturn(mock())
context = mock()
}
@Test
fun createView() {
assertTrue(
GeckoEngine(context, runtime = runtime).createView(
Robolectric.buildActivity(Activity::class.java).get(),
) is GeckoEngineView,
)
}
@Test
fun createSession() {
val engine = GeckoEngine(context, runtime = runtime)
assertTrue(engine.createSession() is GeckoEngineSession)
// Create a private speculative session and consume it
engine.speculativeCreateSession(private = true)
assertTrue(engine.speculativeConnectionFactory.hasSpeculativeSession())
var privateSpeculativeSession = engine.speculativeConnectionFactory.speculativeEngineSession!!.engineSession
assertSame(privateSpeculativeSession, engine.createSession(private = true))
assertFalse(engine.speculativeConnectionFactory.hasSpeculativeSession())
// Create a regular speculative session and make sure it is not returned
// if a private session is requested instead.
engine.speculativeCreateSession(private = false)
assertTrue(engine.speculativeConnectionFactory.hasSpeculativeSession())
privateSpeculativeSession = engine.speculativeConnectionFactory.speculativeEngineSession!!.engineSession
assertNotSame(privateSpeculativeSession, engine.createSession(private = true))
// Make sure previous (never used) speculative session is now closed
assertFalse(privateSpeculativeSession.geckoSession.isOpen)
assertFalse(engine.speculativeConnectionFactory.hasSpeculativeSession())
}
@Test
fun speculativeCreateSession() {
val engine = GeckoEngine(context, runtime = runtime)
assertNull(engine.speculativeConnectionFactory.speculativeEngineSession)
// Create a private speculative session
engine.speculativeCreateSession(private = true)
assertNotNull(engine.speculativeConnectionFactory.speculativeEngineSession)
val privateSpeculativeSession = engine.speculativeConnectionFactory.speculativeEngineSession!!
assertTrue(privateSpeculativeSession.engineSession.geckoSession.settings.usePrivateMode)
// Creating another private speculative session should have no effect as
// session hasn't been "consumed".
engine.speculativeCreateSession(private = true)
assertSame(privateSpeculativeSession, engine.speculativeConnectionFactory.speculativeEngineSession)
assertTrue(privateSpeculativeSession.engineSession.geckoSession.settings.usePrivateMode)
// Creating a non-private speculative session should affect prepared session
engine.speculativeCreateSession(private = false)
assertNotSame(privateSpeculativeSession, engine.speculativeConnectionFactory.speculativeEngineSession)
// Make sure previous (never used) speculative session is now closed
assertFalse(privateSpeculativeSession.engineSession.geckoSession.isOpen)
val regularSpeculativeSession = engine.speculativeConnectionFactory.speculativeEngineSession!!
assertFalse(regularSpeculativeSession.engineSession.geckoSession.settings.usePrivateMode)
}
@Test
fun clearSpeculativeSession() {
val engine = GeckoEngine(context, runtime = runtime)
assertNull(engine.speculativeConnectionFactory.speculativeEngineSession)
val mockEngineSession: GeckoEngineSession = mock()
val mockEngineSessionObserver: SpeculativeSessionObserver = mock()
engine.speculativeConnectionFactory.speculativeEngineSession =
SpeculativeEngineSession(mockEngineSession, mockEngineSessionObserver)
engine.clearSpeculativeSession()
verify(mockEngineSession).unregister(mockEngineSessionObserver)
verify(mockEngineSession).close()
assertNull(engine.speculativeConnectionFactory.speculativeEngineSession)
}
@Test
fun `createSession with contextId`() {
val engine = GeckoEngine(context, runtime = runtime)
// Create a speculative session with a context id and consume it
engine.speculativeCreateSession(private = false, contextId = "1")
assertNotNull(engine.speculativeConnectionFactory.speculativeEngineSession)
var newSpeculativeSession = engine.speculativeConnectionFactory.speculativeEngineSession!!.engineSession
assertSame(newSpeculativeSession, engine.createSession(private = false, contextId = "1"))
assertNull(engine.speculativeConnectionFactory.speculativeEngineSession)
// Create a regular speculative session and make sure it is not returned
// if a session with a context id is requested instead.
engine.speculativeCreateSession(private = false)
assertNotNull(engine.speculativeConnectionFactory.speculativeEngineSession)
newSpeculativeSession = engine.speculativeConnectionFactory.speculativeEngineSession!!.engineSession
assertNotSame(newSpeculativeSession, engine.createSession(private = false, contextId = "1"))
// Make sure previous (never used) speculative session is now closed
assertFalse(newSpeculativeSession.geckoSession.isOpen)
assertNull(engine.speculativeConnectionFactory.speculativeEngineSession)
}
@Test
fun name() {
assertEquals("Gecko", GeckoEngine(context, runtime = runtime).name())
}
@Test
fun settings() {
val defaultSettings = DefaultSettings()
val contentBlockingSettings = ContentBlocking.Settings.Builder().build()
val runtime = mock<GeckoRuntime>()
val runtimeSettings = mock<GeckoRuntimeSettings>()
whenever(runtimeSettings.javaScriptEnabled).thenReturn(true)
whenever(runtimeSettings.webFontsEnabled).thenReturn(true)
whenever(runtimeSettings.automaticFontSizeAdjustment).thenReturn(true)
whenever(runtimeSettings.fontInflationEnabled).thenReturn(true)
whenever(runtimeSettings.fontSizeFactor).thenReturn(1.0F)
whenever(runtimeSettings.forceUserScalableEnabled).thenReturn(false)
whenever(runtimeSettings.loginAutofillEnabled).thenReturn(false)
whenever(runtimeSettings.enterpriseRootsEnabled).thenReturn(false)
whenever(runtimeSettings.contentBlocking).thenReturn(contentBlockingSettings)
whenever(runtimeSettings.preferredColorScheme).thenReturn(GeckoRuntimeSettings.COLOR_SCHEME_SYSTEM)
whenever(runtime.settings).thenReturn(runtimeSettings)
val engine = GeckoEngine(context, runtime = runtime, defaultSettings = defaultSettings)
assertTrue(engine.settings.javascriptEnabled)
engine.settings.javascriptEnabled = false
verify(runtimeSettings).javaScriptEnabled = false
assertFalse(engine.settings.loginAutofillEnabled)
engine.settings.loginAutofillEnabled = true
verify(runtimeSettings).loginAutofillEnabled = true
assertFalse(engine.settings.enterpriseRootsEnabled)
engine.settings.enterpriseRootsEnabled = true
verify(runtimeSettings).enterpriseRootsEnabled = true
assertTrue(engine.settings.webFontsEnabled)
engine.settings.webFontsEnabled = false
verify(runtimeSettings).webFontsEnabled = false
assertTrue(engine.settings.automaticFontSizeAdjustment)
engine.settings.automaticFontSizeAdjustment = false
verify(runtimeSettings).automaticFontSizeAdjustment = false
assertTrue(engine.settings.fontInflationEnabled!!)
engine.settings.fontInflationEnabled = null
verify(runtimeSettings, never()).fontInflationEnabled = anyBoolean()
engine.settings.fontInflationEnabled = false
verify(runtimeSettings).fontInflationEnabled = false
assertEquals(1.0F, engine.settings.fontSizeFactor)
engine.settings.fontSizeFactor = null
verify(runtimeSettings, never()).fontSizeFactor = anyFloat()
engine.settings.fontSizeFactor = 2.0F
verify(runtimeSettings).fontSizeFactor = 2.0F
assertFalse(engine.settings.forceUserScalableContent)
engine.settings.forceUserScalableContent = true
verify(runtimeSettings).forceUserScalableEnabled = true
assertFalse(engine.settings.remoteDebuggingEnabled)
engine.settings.remoteDebuggingEnabled = true
verify(runtimeSettings).remoteDebuggingEnabled = true
assertFalse(engine.settings.testingModeEnabled)
engine.settings.testingModeEnabled = true
assertTrue(engine.settings.testingModeEnabled)
assertEquals(PreferredColorScheme.System, engine.settings.preferredColorScheme)
engine.settings.preferredColorScheme = PreferredColorScheme.Dark
verify(runtimeSettings).preferredColorScheme = PreferredColorScheme.Dark.toGeckoValue()
assertFalse(engine.settings.suspendMediaWhenInactive)
engine.settings.suspendMediaWhenInactive = true
assertEquals(true, engine.settings.suspendMediaWhenInactive)
assertNull(engine.settings.clearColor)
engine.settings.clearColor = Color.BLUE
assertEquals(Color.BLUE, engine.settings.clearColor)
// Specifying no ua-string default should result in GeckoView's default.
assertEquals(GeckoSession.getDefaultUserAgent(), engine.settings.userAgentString)
// It also should be possible to read and set a new default.
engine.settings.userAgentString = engine.settings.userAgentString + "-test"
assertEquals(GeckoSession.getDefaultUserAgent() + "-test", engine.settings.userAgentString)
assertEquals(null, engine.settings.trackingProtectionPolicy)
engine.settings.trackingProtectionPolicy = TrackingProtectionPolicy.strict()
val trackingStrictCategories = TrackingProtectionPolicy.strict().trackingCategories.sumOf { it.id }
val artificialCategory =
TrackingCategory.SCRIPTS_AND_SUB_RESOURCES.id
assertEquals(
trackingStrictCategories - artificialCategory,
contentBlockingSettings.antiTrackingCategories,
)
val safeStrictBrowsingCategories = SafeBrowsingPolicy.RECOMMENDED.id
assertEquals(safeStrictBrowsingCategories, contentBlockingSettings.safeBrowsingCategories)
engine.settings.safeBrowsingPolicy = arrayOf(SafeBrowsingPolicy.PHISHING)
assertEquals(SafeBrowsingPolicy.PHISHING.id, contentBlockingSettings.safeBrowsingCategories)
assertEquals(defaultSettings.trackingProtectionPolicy, TrackingProtectionPolicy.strict())
assertEquals(contentBlockingSettings.cookieBehavior, CookiePolicy.ACCEPT_FIRST_PARTY_AND_ISOLATE_OTHERS.id)
assertEquals(
contentBlockingSettings.cookieBehaviorPrivateMode,
CookiePolicy.ACCEPT_FIRST_PARTY_AND_ISOLATE_OTHERS.id,
)
assertEquals(contentBlockingSettings.cookieBannerMode, EngineSession.CookieBannerHandlingMode.DISABLED.mode)
assertEquals(contentBlockingSettings.cookieBannerModePrivateBrowsing, EngineSession.CookieBannerHandlingMode.DISABLED.mode)
assertEquals(contentBlockingSettings.cookieBannerDetectOnlyMode, engine.settings.cookieBannerHandlingDetectOnlyMode)
try {
engine.settings.domStorageEnabled
fail("Expected UnsupportedOperationException")
} catch (e: UnsupportedSettingException) { }
try {
engine.settings.domStorageEnabled = false
fail("Expected UnsupportedOperationException")
} catch (e: UnsupportedSettingException) { }
}
@Test
fun `the SCRIPTS_AND_SUB_RESOURCES tracking protection category must not be passed to gecko view`() {
val mockRuntime = mock<GeckoRuntime>()
val settings = spy(ContentBlocking.Settings.Builder().build())
whenever(mockRuntime.settings).thenReturn(mock())
whenever(mockRuntime.settings.contentBlocking).thenReturn(settings)
val engine = GeckoEngine(testContext, runtime = mockRuntime)
engine.settings.trackingProtectionPolicy = TrackingProtectionPolicy.strict()
val trackingStrictCategories = TrackingProtectionPolicy.strict().trackingCategories.sumOf { it.id }
val artificialCategory = TrackingCategory.SCRIPTS_AND_SUB_RESOURCES.id
assertEquals(
trackingStrictCategories - artificialCategory,
mockRuntime.settings.contentBlocking.antiTrackingCategories,
)
mockRuntime.settings.contentBlocking.setAntiTracking(0)
engine.settings.trackingProtectionPolicy = TrackingProtectionPolicy.select(
arrayOf(TrackingCategory.SCRIPTS_AND_SUB_RESOURCES),
)
assertEquals(0, mockRuntime.settings.contentBlocking.antiTrackingCategories)
}
@Test
fun `WHEN a strict tracking protection policy is set THEN the strict social list must be activated`() {
val mockRuntime = mock<GeckoRuntime>()
whenever(mockRuntime.settings).thenReturn(mock())
whenever(mockRuntime.settings.contentBlocking).thenReturn(mock())
val engine = GeckoEngine(testContext, runtime = mockRuntime)
engine.settings.trackingProtectionPolicy = TrackingProtectionPolicy.strict()
verify(mockRuntime.settings.contentBlocking).setStrictSocialTrackingProtection(true)
}
@Test
fun `WHEN a strict tracking protection policy is set THEN the setEnhancedTrackingProtectionLevel must be STRICT`() {
val mockRuntime = mock<GeckoRuntime>()
whenever(mockRuntime.settings).thenReturn(mock())
whenever(mockRuntime.settings.contentBlocking).thenReturn(mock())
val engine = GeckoEngine(testContext, runtime = mockRuntime)
engine.settings.trackingProtectionPolicy = TrackingProtectionPolicy.strict()
verify(mockRuntime.settings.contentBlocking).setEnhancedTrackingProtectionLevel(
ContentBlocking.EtpLevel.STRICT,
)
}
@Test
fun `WHEN an HTTPS-Only mode is set THEN allowInsecureConnections is getting set on GeckoRuntime`() {
val mockRuntime = mock<GeckoRuntime>()
whenever(mockRuntime.settings).thenReturn(mock())
val engine = GeckoEngine(testContext, runtime = mockRuntime)
reset(mockRuntime.settings)
engine.settings.httpsOnlyMode = Engine.HttpsOnlyMode.ENABLED_PRIVATE_ONLY
verify(mockRuntime.settings).allowInsecureConnections = GeckoRuntimeSettings.HTTPS_ONLY_PRIVATE
reset(mockRuntime.settings)
engine.settings.httpsOnlyMode = Engine.HttpsOnlyMode.ENABLED
verify(mockRuntime.settings).allowInsecureConnections = GeckoRuntimeSettings.HTTPS_ONLY
reset(mockRuntime.settings)
engine.settings.httpsOnlyMode = Engine.HttpsOnlyMode.DISABLED
verify(mockRuntime.settings).allowInsecureConnections = GeckoRuntimeSettings.ALLOW_ALL
}
@Test
fun `setAntiTracking is only invoked when the value is changed`() {
val mockRuntime = mock<GeckoRuntime>()
val settings = spy(ContentBlocking.Settings.Builder().build())
whenever(mockRuntime.settings).thenReturn(mock())
whenever(mockRuntime.settings.contentBlocking).thenReturn(settings)
val engine = GeckoEngine(testContext, runtime = mockRuntime)
val policy = TrackingProtectionPolicy.recommended()
engine.settings.trackingProtectionPolicy = policy
verify(mockRuntime.settings.contentBlocking).setAntiTracking(
policy.getAntiTrackingPolicy(),
)
reset(settings)
engine.settings.trackingProtectionPolicy = policy
verify(mockRuntime.settings.contentBlocking, never()).setAntiTracking(
policy.getAntiTrackingPolicy(),
)
}
@Test
fun `cookiePurging is only invoked when the value is changed`() {
val mockRuntime = mock<GeckoRuntime>()
val settings = spy(ContentBlocking.Settings.Builder().build())
whenever(mockRuntime.settings).thenReturn(mock())
whenever(mockRuntime.settings.contentBlocking).thenReturn(settings)
val engine = GeckoEngine(testContext, runtime = mockRuntime)
val policy = TrackingProtectionPolicy.recommended()
engine.settings.trackingProtectionPolicy = policy
verify(mockRuntime.settings.contentBlocking).setCookiePurging(policy.cookiePurging)
reset(settings)
engine.settings.trackingProtectionPolicy = policy
verify(mockRuntime.settings.contentBlocking, never()).setCookiePurging(policy.cookiePurging)
}
@Test
fun `setCookieBehavior is only invoked when the value is changed`() {
val mockRuntime = mock<GeckoRuntime>()
val settings = spy(ContentBlocking.Settings.Builder().build())
whenever(mockRuntime.settings).thenReturn(mock())
whenever(mockRuntime.settings.contentBlocking).thenReturn(settings)
whenever(mockRuntime.settings.contentBlocking.cookieBehavior).thenReturn(CookieBehavior.ACCEPT_NONE)
val engine = GeckoEngine(testContext, runtime = mockRuntime)
val policy = TrackingProtectionPolicy.recommended()
engine.settings.trackingProtectionPolicy = policy
verify(mockRuntime.settings.contentBlocking).setCookieBehavior(
policy.cookiePolicy.id,
)
reset(settings)
engine.settings.trackingProtectionPolicy = policy
verify(mockRuntime.settings.contentBlocking, never()).setCookieBehavior(
policy.cookiePolicy.id,
)
}
@Test
fun `setCookieBehavior private mode is only invoked when the value is changed`() {
val mockRuntime = mock<GeckoRuntime>()
val settings = spy(ContentBlocking.Settings.Builder().build())
whenever(mockRuntime.settings).thenReturn(mock())
whenever(mockRuntime.settings.contentBlocking).thenReturn(settings)
whenever(mockRuntime.settings.contentBlocking.cookieBehaviorPrivateMode).thenReturn(CookieBehavior.ACCEPT_NONE)
val engine = GeckoEngine(testContext, runtime = mockRuntime)
val policy = TrackingProtectionPolicy.recommended()
engine.settings.trackingProtectionPolicy = policy
verify(mockRuntime.settings.contentBlocking).setCookieBehaviorPrivateMode(
policy.cookiePolicy.id,
)
reset(settings)
engine.settings.trackingProtectionPolicy = policy
verify(mockRuntime.settings.contentBlocking, never()).setCookieBehaviorPrivateMode(
policy.cookiePolicy.id,
)
}
@Test
fun `setCookieBannerMode is only invoked when the value is changed`() {
val mockRuntime = mock<GeckoRuntime>()
val settings = spy(ContentBlocking.Settings.Builder().build())
whenever(mockRuntime.settings).thenReturn(mock())
whenever(mockRuntime.settings.contentBlocking).thenReturn(settings)
val engine = GeckoEngine(testContext, runtime = mockRuntime)
val policy = EngineSession.CookieBannerHandlingMode.REJECT_ALL
engine.settings.cookieBannerHandlingMode = policy
verify(mockRuntime.settings.contentBlocking).setCookieBannerMode(policy.mode)
reset(settings)
engine.settings.cookieBannerHandlingMode = policy
verify(mockRuntime.settings.contentBlocking, never()).setCookieBannerMode(policy.mode)
}
@Test
fun `setCookieBannerModePrivateBrowsing is only invoked when the value is changed`() {
val mockRuntime = mock<GeckoRuntime>()
val settings = spy(ContentBlocking.Settings.Builder().build())
whenever(mockRuntime.settings).thenReturn(mock())
whenever(mockRuntime.settings.contentBlocking).thenReturn(settings)
val engine = GeckoEngine(testContext, runtime = mockRuntime)
val policy = EngineSession.CookieBannerHandlingMode.REJECT_OR_ACCEPT_ALL
engine.settings.cookieBannerHandlingModePrivateBrowsing = policy
verify(mockRuntime.settings.contentBlocking).setCookieBannerModePrivateBrowsing(policy.mode)
reset(settings)
engine.settings.cookieBannerHandlingModePrivateBrowsing = policy
verify(mockRuntime.settings.contentBlocking, never()).setCookieBannerModePrivateBrowsing(policy.mode)
}
@Test
fun `setCookieBannerHandlingDetectOnlyMode is only invoked when the value is changed`() {
val mockRuntime = mock<GeckoRuntime>()
val settings = spy(ContentBlocking.Settings.Builder().build())
whenever(mockRuntime.settings).thenReturn(mock())
whenever(mockRuntime.settings.contentBlocking).thenReturn(settings)
val engine = GeckoEngine(testContext, runtime = mockRuntime)
engine.settings.cookieBannerHandlingDetectOnlyMode = true
verify(mockRuntime.settings.contentBlocking).setCookieBannerDetectOnlyMode(true)
reset(settings)
engine.settings.cookieBannerHandlingDetectOnlyMode = true
verify(mockRuntime.settings.contentBlocking, never()).setCookieBannerDetectOnlyMode(true)
}
@Test
fun `Cookie banner handling settings are aligned`() {
assertEquals(ContentBlocking.CookieBannerMode.COOKIE_BANNER_MODE_DISABLED, EngineSession.CookieBannerHandlingMode.DISABLED.mode)
assertEquals(ContentBlocking.CookieBannerMode.COOKIE_BANNER_MODE_REJECT, EngineSession.CookieBannerHandlingMode.REJECT_ALL.mode)
assertEquals(ContentBlocking.CookieBannerMode.COOKIE_BANNER_MODE_REJECT_OR_ACCEPT, EngineSession.CookieBannerHandlingMode.REJECT_OR_ACCEPT_ALL.mode)
}
@Test
fun `setEnhancedTrackingProtectionLevel MUST always be set to STRICT unless the tracking protection policy is none`() {
val mockRuntime = mock<GeckoRuntime>()
val settings = spy(ContentBlocking.Settings.Builder().build())
whenever(mockRuntime.settings).thenReturn(mock())
whenever(mockRuntime.settings.contentBlocking).thenReturn(settings)
val engine = GeckoEngine(testContext, runtime = mockRuntime)
engine.settings.trackingProtectionPolicy = TrackingProtectionPolicy.recommended()
verify(mockRuntime.settings.contentBlocking).setEnhancedTrackingProtectionLevel(
ContentBlocking.EtpLevel.STRICT,
)
reset(settings)
engine.settings.trackingProtectionPolicy = TrackingProtectionPolicy.recommended()
verify(mockRuntime.settings.contentBlocking, never()).setEnhancedTrackingProtectionLevel(
ContentBlocking.EtpLevel.STRICT,
)
reset(settings)
engine.settings.trackingProtectionPolicy = TrackingProtectionPolicy.strict()
verify(mockRuntime.settings.contentBlocking, never()).setEnhancedTrackingProtectionLevel(
ContentBlocking.EtpLevel.STRICT,
)
reset(settings)
engine.settings.trackingProtectionPolicy = TrackingProtectionPolicy.none()
verify(mockRuntime.settings.contentBlocking).setEnhancedTrackingProtectionLevel(
ContentBlocking.EtpLevel.NONE,
)
reset(settings)
engine.settings.trackingProtectionPolicy = TrackingProtectionPolicy.none()
verify(mockRuntime.settings.contentBlocking, never()).setEnhancedTrackingProtectionLevel(
ContentBlocking.EtpLevel.NONE,
)
reset(settings)
engine.settings.trackingProtectionPolicy = TrackingProtectionPolicy.strict()
verify(mockRuntime.settings.contentBlocking).setEnhancedTrackingProtectionLevel(
ContentBlocking.EtpLevel.STRICT,
)
}
@Test
fun `WHEN a non strict tracking protection policy is set THEN the strict social list must be disabled`() {
val mockRuntime = mock<GeckoRuntime>()
whenever(mockRuntime.settings).thenReturn(mock())
whenever(mockRuntime.settings.contentBlocking).thenReturn(mock())
whenever(mockRuntime.settings.contentBlocking.strictSocialTrackingProtection).thenReturn(true)
val engine = GeckoEngine(testContext, runtime = mockRuntime)
engine.settings.trackingProtectionPolicy = TrackingProtectionPolicy.recommended()
verify(mockRuntime.settings.contentBlocking).setStrictSocialTrackingProtection(false)
}
@Test
fun `WHEN strict social tracking protection is set to true THEN the strict social list must be activated`() {
val mockRuntime = mock<GeckoRuntime>()
whenever(mockRuntime.settings).thenReturn(mock())
whenever(mockRuntime.settings.contentBlocking).thenReturn(mock())
val engine = GeckoEngine(testContext, runtime = mockRuntime)
engine.settings.trackingProtectionPolicy = TrackingProtectionPolicy.select(
strictSocialTrackingProtection = true,
)
verify(mockRuntime.settings.contentBlocking).setStrictSocialTrackingProtection(true)
}
@Test
fun `WHEN strict social tracking protection is set to false THEN the strict social list must be disabled`() {
val mockRuntime = mock<GeckoRuntime>()
whenever(mockRuntime.settings).thenReturn(mock())
whenever(mockRuntime.settings.contentBlocking).thenReturn(mock())
whenever(mockRuntime.settings.contentBlocking.strictSocialTrackingProtection).thenReturn(true)
val engine = GeckoEngine(testContext, runtime = mockRuntime)
engine.settings.trackingProtectionPolicy = TrackingProtectionPolicy.select(
strictSocialTrackingProtection = false,
)
verify(mockRuntime.settings.contentBlocking).setStrictSocialTrackingProtection(false)
}
@Test
fun defaultSettings() {
val runtime = mock<GeckoRuntime>()
val runtimeSettings = mock<GeckoRuntimeSettings>()
val contentBlockingSettings = ContentBlocking.Settings.Builder().build()
whenever(runtimeSettings.javaScriptEnabled).thenReturn(true)
whenever(runtime.settings).thenReturn(runtimeSettings)
whenever(runtimeSettings.contentBlocking).thenReturn(contentBlockingSettings)
whenever(runtimeSettings.fontInflationEnabled).thenReturn(true)
val engine = GeckoEngine(
context,
DefaultSettings(
trackingProtectionPolicy = TrackingProtectionPolicy.strict(),
javascriptEnabled = false,
webFontsEnabled = false,
automaticFontSizeAdjustment = false,
fontInflationEnabled = false,
fontSizeFactor = 2.0F,
remoteDebuggingEnabled = true,
testingModeEnabled = true,
userAgentString = "test-ua",
preferredColorScheme = PreferredColorScheme.Light,
suspendMediaWhenInactive = true,
forceUserScalableContent = false,
),
runtime,
)
verify(runtimeSettings).javaScriptEnabled = false
verify(runtimeSettings).webFontsEnabled = false
verify(runtimeSettings).automaticFontSizeAdjustment = false
verify(runtimeSettings).fontInflationEnabled = false
verify(runtimeSettings).fontSizeFactor = 2.0F
verify(runtimeSettings).remoteDebuggingEnabled = true
verify(runtimeSettings).forceUserScalableEnabled = false
val trackingStrictCategories = TrackingProtectionPolicy.strict().trackingCategories.sumOf { it.id }
val artificialCategory =
TrackingCategory.SCRIPTS_AND_SUB_RESOURCES.id
assertEquals(
trackingStrictCategories - artificialCategory,
contentBlockingSettings.antiTrackingCategories,
)
assertEquals(SafeBrowsingPolicy.RECOMMENDED.id, contentBlockingSettings.safeBrowsingCategories)
assertEquals(CookiePolicy.ACCEPT_FIRST_PARTY_AND_ISOLATE_OTHERS.id, contentBlockingSettings.cookieBehavior)
assertEquals(
CookiePolicy.ACCEPT_FIRST_PARTY_AND_ISOLATE_OTHERS.id,
contentBlockingSettings.cookieBehaviorPrivateMode,
)
assertTrue(engine.settings.testingModeEnabled)
assertEquals("test-ua", engine.settings.userAgentString)
assertEquals(PreferredColorScheme.Light, engine.settings.preferredColorScheme)
assertTrue(engine.settings.suspendMediaWhenInactive)
engine.settings.safeBrowsingPolicy = arrayOf(SafeBrowsingPolicy.PHISHING)
engine.settings.trackingProtectionPolicy =
TrackingProtectionPolicy.select(
trackingCategories = arrayOf(TrackingCategory.AD),
cookiePolicy = CookiePolicy.ACCEPT_ONLY_FIRST_PARTY,
)
assertEquals(
TrackingCategory.AD.id,
contentBlockingSettings.antiTrackingCategories,
)
assertEquals(
SafeBrowsingPolicy.PHISHING.id,
contentBlockingSettings.safeBrowsingCategories,
)
assertEquals(
CookiePolicy.ACCEPT_ONLY_FIRST_PARTY.id,
contentBlockingSettings.cookieBehavior,
)
engine.settings.trackingProtectionPolicy = TrackingProtectionPolicy.none()
assertEquals(CookiePolicy.ACCEPT_ALL.id, contentBlockingSettings.cookieBehavior)
assertEquals(EngineSession.CookieBannerHandlingMode.DISABLED.mode, contentBlockingSettings.cookieBannerMode)
assertEquals(EngineSession.CookieBannerHandlingMode.DISABLED.mode, contentBlockingSettings.cookieBannerModePrivateBrowsing)
}
@Test
fun `speculativeConnect forwards call to executor`() {
val executor: GeckoWebExecutor = mock()
val engine = GeckoEngine(context, runtime = runtime, executorProvider = { executor })
engine.speculativeConnect("https://www.mozilla.org")
verify(executor).speculativeConnect("https://www.mozilla.org")
}
@Test
fun `install built-in web extension successfully`() {
val runtime = mock<GeckoRuntime>()
val extId = "test-webext"
val extUrl = "resource://android/assets/extensions/test"
val extensionController: WebExtensionController = mock()
whenever(runtime.webExtensionController).thenReturn(extensionController)
val engine = GeckoEngine(context, runtime = runtime)
var onSuccessCalled = false
var onErrorCalled = false
val result = GeckoResult<GeckoWebExtension>()
whenever(extensionController.ensureBuiltIn(extUrl, extId)).thenReturn(result)
engine.installWebExtension(
extId,
extUrl,
onSuccess = { onSuccessCalled = true },
onError = { _, _ -> onErrorCalled = true },
)
result.complete(mockNativeWebExtension(extId, extUrl))
shadowOf(getMainLooper()).idle()
val extUrlCaptor = argumentCaptor<String>()
val extIdCaptor = argumentCaptor<String>()
verify(extensionController).ensureBuiltIn(extUrlCaptor.capture(), extIdCaptor.capture())
assertEquals(extUrl, extUrlCaptor.value)
assertEquals(extId, extIdCaptor.value)
assertTrue(onSuccessCalled)
assertFalse(onErrorCalled)
}
@Test
fun `install external web extension successfully`() {
val runtime = mock<GeckoRuntime>()
val extId = "test-webext"
val extUrl = "https://addons.mozilla.org/firefox/downloads/file/123/some_web_ext.xpi"
val extensionController: WebExtensionController = mock()
whenever(runtime.webExtensionController).thenReturn(extensionController)
val engine = GeckoEngine(context, runtime = runtime)
var onSuccessCalled = false
var onErrorCalled = false
val result = GeckoResult<GeckoWebExtension>()
whenever(extensionController.install(any())).thenReturn(result)
engine.installWebExtension(
extId,
extUrl,
onSuccess = { onSuccessCalled = true },
onError = { _, _ -> onErrorCalled = true },
)
result.complete(mockNativeWebExtension(extId, extUrl))
shadowOf(getMainLooper()).idle()
val extCaptor = argumentCaptor<String>()
verify(extensionController).install(extCaptor.capture())
assertEquals(extUrl, extCaptor.value)
assertTrue(onSuccessCalled)
assertFalse(onErrorCalled)
}
@Test
fun `install built-in web extension failure`() {
val runtime = mock<GeckoRuntime>()
val extId = "test-webext"
val extUrl = "resource://android/assets/extensions/test"
val extensionController: WebExtensionController = mock()
whenever(runtime.webExtensionController).thenReturn(extensionController)
val engine = GeckoEngine(context, runtime = runtime)
var onErrorCalled = false
val expected = IOException()
val result = GeckoResult<GeckoWebExtension>()
var throwable: Throwable? = null
whenever(extensionController.ensureBuiltIn(extUrl, extId)).thenReturn(result)
engine.installWebExtension(extId, extUrl) { _, e ->
onErrorCalled = true
throwable = e
}
result.completeExceptionally(expected)
shadowOf(getMainLooper()).idle()
assertTrue(onErrorCalled)
assertTrue(throwable is GeckoWebExtensionException)
}
@Test
fun `install external web extension failure`() {
val runtime = mock<GeckoRuntime>()
val extId = "test-webext"
val extUrl = "https://addons.mozilla.org/firefox/downloads/file/123/some_web_ext.xpi"
val extensionController: WebExtensionController = mock()
whenever(runtime.webExtensionController).thenReturn(extensionController)
val engine = GeckoEngine(context, runtime = runtime)
var onErrorCalled = false
val expected = IOException()
val result = GeckoResult<GeckoWebExtension>()
var throwable: Throwable? = null
whenever(extensionController.install(any())).thenReturn(result)
engine.installWebExtension(extId, extUrl) { _, e ->
onErrorCalled = true
throwable = e
}
result.completeExceptionally(expected)
shadowOf(getMainLooper()).idle()
assertTrue(onErrorCalled)
assertTrue(throwable is GeckoWebExtensionException)
}
@Test
fun `uninstall web extension successfully`() {
val runtime = mock<GeckoRuntime>()
val extensionController: WebExtensionController = mock()
whenever(runtime.webExtensionController).thenReturn(extensionController)
val nativeExtension = mockNativeWebExtension("test-webext", "https://addons.mozilla.org/1/some_web_ext.xpi")
val ext = mozilla.components.browser.engine.gecko.webextension.GeckoWebExtension(
nativeExtension,
runtime,
)
val webExtensionsDelegate: WebExtensionDelegate = mock()
val engine = GeckoEngine(context, runtime = runtime)
engine.registerWebExtensionDelegate(webExtensionsDelegate)
var onSuccessCalled = false
var onErrorCalled = false
val result = GeckoResult<Void>()
whenever(extensionController.uninstall(any())).thenReturn(result)
engine.uninstallWebExtension(
ext,
onSuccess = { onSuccessCalled = true },
onError = { _, _ -> onErrorCalled = true },
)
result.complete(null)
shadowOf(getMainLooper()).idle()
val extCaptor = argumentCaptor<GeckoWebExtension>()
verify(extensionController).uninstall(extCaptor.capture())
assertSame(nativeExtension, extCaptor.value)
assertTrue(onSuccessCalled)
assertFalse(onErrorCalled)
}
@Test
fun `uninstall web extension failure`() {
val runtime = mock<GeckoRuntime>()
val extensionController: WebExtensionController = mock()
whenever(runtime.webExtensionController).thenReturn(extensionController)
val nativeExtension = mockNativeWebExtension(
"test-webext",
"https://addons.mozilla.org/firefox/downloads/file/123/some_web_ext.xpi",
)
val ext = mozilla.components.browser.engine.gecko.webextension.GeckoWebExtension(
nativeExtension,
runtime,
)
val webExtensionsDelegate: WebExtensionDelegate = mock()
val engine = GeckoEngine(context, runtime = runtime)
engine.registerWebExtensionDelegate(webExtensionsDelegate)
var onErrorCalled = false
val expected = IOException()
val result = GeckoResult<Void>()
var throwable: Throwable? = null
whenever(extensionController.uninstall(any())).thenReturn(result)
engine.uninstallWebExtension(ext) { _, e ->
onErrorCalled = true
throwable = e
}
result.completeExceptionally(expected)
shadowOf(getMainLooper()).idle()
assertTrue(onErrorCalled)
assertEquals(expected, throwable)
}
@Test
fun `web extension delegate handles installation of built-in extensions`() {
val runtime: GeckoRuntime = mock()
val webExtensionController: WebExtensionController = mock()
whenever(runtime.webExtensionController).thenReturn(webExtensionController)
val webExtensionsDelegate: WebExtensionDelegate = mock()
val engine = GeckoEngine(context, runtime = runtime)
engine.registerWebExtensionDelegate(webExtensionsDelegate)
val extId = "test-webext"
val extUrl = "resource://android/assets/extensions/test"
val result = GeckoResult<GeckoWebExtension>()
whenever(webExtensionController.ensureBuiltIn(extUrl, extId)).thenReturn(result)
engine.installWebExtension(extId, extUrl)
result.complete(mockNativeWebExtension(extId, extUrl))
shadowOf(getMainLooper()).idle()
val extCaptor = argumentCaptor<WebExtension>()
verify(webExtensionsDelegate).onInstalled(extCaptor.capture())
assertEquals(extId, extCaptor.value.id)
assertEquals(extUrl, extCaptor.value.url)
}
@Test
fun `web extension delegate handles installation of external extensions`() {
val runtime: GeckoRuntime = mock()
val webExtensionController: WebExtensionController = mock()
whenever(runtime.webExtensionController).thenReturn(webExtensionController)
val webExtensionsDelegate: WebExtensionDelegate = mock()
val engine = GeckoEngine(context, runtime = runtime)
engine.registerWebExtensionDelegate(webExtensionsDelegate)