SettingsTest.kt 14.7 KB
Newer Older
1
2
3
/* 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/. */
Denys M's avatar
Denys M committed
4
5
6
7

package org.mozilla.fenix.utils

import mozilla.components.feature.sitepermissions.SitePermissionsRules
8
import mozilla.components.feature.sitepermissions.SitePermissionsRules.Action.ALLOWED
Denys M's avatar
Denys M committed
9
10
import mozilla.components.feature.sitepermissions.SitePermissionsRules.Action.ASK_TO_ALLOW
import mozilla.components.feature.sitepermissions.SitePermissionsRules.Action.BLOCKED
11
import mozilla.components.feature.sitepermissions.SitePermissionsRules.AutoplayAction
Denys M's avatar
Denys M committed
12
13
14
15
16
17
18
import mozilla.components.support.test.robolectric.testContext
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
19
import org.mozilla.fenix.BuildConfig
20
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
21
import org.mozilla.fenix.settings.PhoneFeature
22
import org.mozilla.fenix.settings.deletebrowsingdata.DeleteBrowsingDataOnQuitType
Denys M's avatar
Denys M committed
23

24
@RunWith(FenixRobolectricTestRunner::class)
Denys M's avatar
Denys M committed
25
26
27
28
class SettingsTest {

    lateinit var settings: Settings

29
30
31
32
33
34
35
36
37
    private val defaultPermissions = SitePermissionsRules(
        camera = ASK_TO_ALLOW,
        location = ASK_TO_ALLOW,
        microphone = ASK_TO_ALLOW,
        notification = ASK_TO_ALLOW,
        autoplayAudible = AutoplayAction.BLOCKED,
        autoplayInaudible = AutoplayAction.BLOCKED
    )

Denys M's avatar
Denys M committed
38
39
    @Before
    fun setUp() {
40
        settings = Settings(testContext)
Denys M's avatar
Denys M committed
41
42
    }

43
    @Test
44
    fun launchLinksInPrivateTab() {
45
46
        // When just created
        // Then
47
        assertTrue(settings.openLinksInAPrivateTab)
48
49

        // When
50
        settings.openLinksInAPrivateTab = false
51
52

        // Then
53
        assertFalse(settings.openLinksInAPrivateTab)
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
    @Test
    fun clearDataOnQuit() {
        // When just created
        // Then
        assertFalse(settings.shouldDeleteBrowsingDataOnQuit)

        // When
        settings.shouldDeleteBrowsingDataOnQuit = true

        // Then
        assertTrue(settings.shouldDeleteBrowsingDataOnQuit)

        // When
        settings.shouldDeleteBrowsingDataOnQuit = false

        // Then
        assertFalse(settings.shouldDeleteBrowsingDataOnQuit)
    }

    @Test
    fun clearAnyDataOnQuit() {
        // When just created
        // Then
        assertFalse(settings.shouldDeleteAnyDataOnQuit())

        // When
82
        settings.setDeleteDataOnQuit(DeleteBrowsingDataOnQuitType.TABS, true)
83
84
85
86
87

        // Then
        assertTrue(settings.shouldDeleteAnyDataOnQuit())

        // When
88
        settings.setDeleteDataOnQuit(DeleteBrowsingDataOnQuitType.PERMISSIONS, true)
89
90
91
92
93

        // Then
        assertTrue(settings.shouldDeleteAnyDataOnQuit())

        // When
94
95
        settings.setDeleteDataOnQuit(DeleteBrowsingDataOnQuitType.TABS, false)
        settings.setDeleteDataOnQuit(DeleteBrowsingDataOnQuitType.PERMISSIONS, false)
96
97
98
99
100

        // Then
        assertFalse(settings.shouldDeleteAnyDataOnQuit())
    }

Denys M's avatar
Denys M committed
101
102
103
104
105
106
107
    @Test
    fun defaultSearchEngineName() {
        // When just created
        // Then
        assertEquals("", settings.defaultSearchEngineName)

        // When
108
        settings.defaultSearchEngineName = "Mozilla"
Denys M's avatar
Denys M committed
109
110
111
112
113
114
115
116
117
118
119
120

        // Then
        assertEquals("Mozilla", settings.defaultSearchEngineName)
    }

    @Test
    fun isRemoteDebuggingEnabled() {
        // When just created
        // Then
        assertFalse(settings.isRemoteDebuggingEnabled)
    }

121
122
123
124
125
126
127
128
129
130
131
132
133
134
    @Test
    fun canShowCfrTest() {
        // When just created
        // Then
        assertEquals(0L, settings.lastCfrShownTimeInMillis)
        assertTrue(settings.canShowCfr)

        // When
        settings.lastCfrShownTimeInMillis = System.currentTimeMillis()

        // Then
        assertFalse(settings.canShowCfr)
    }

Denys M's avatar
Denys M committed
135
136
137
138
    @Test
    fun isTelemetryEnabled() {
        // When just created
        // Then
139
140
141
142
143
        if (BuildConfig.DATA_COLLECTION_DISABLED) {
            assertFalse(settings.isTelemetryEnabled)
        } else {
            assertTrue(settings.isTelemetryEnabled)
        }
Denys M's avatar
Denys M committed
144
145
    }

146
147
148
149
    @Test
    fun showLoginsDialogWarningSync() {
        // When just created
        // Then
150
        assertEquals(0, settings.loginsSecureWarningSyncCount.value)
151
152
153
154
155

        // When
        settings.incrementShowLoginsSecureWarningSyncCount()

        // Then
156
        assertEquals(1, settings.loginsSecureWarningSyncCount.value)
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
    }

    @Test
    fun shouldShowLoginsDialogWarningSync() {
        // When just created
        // Then
        assertTrue(settings.shouldShowSecurityPinWarningSync)

        // When
        settings.incrementShowLoginsSecureWarningSyncCount()

        // Then
        assertFalse(settings.shouldShowSecurityPinWarningSync)
    }

    @Test
    fun showLoginsDialogWarning() {
        // When just created
        // Then
176
        assertEquals(0, settings.loginsSecureWarningCount.value)
177
178
179
180
181

        // When
        settings.incrementShowLoginsSecureWarningCount()

        // Then
182
        assertEquals(1, settings.loginsSecureWarningCount.value)
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
    }

    @Test
    fun shouldShowLoginsDialogWarning() {
        // When just created
        // Then
        assertTrue(settings.shouldShowSecurityPinWarning)

        // When
        settings.incrementShowLoginsSecureWarningCount()

        // Then
        assertFalse(settings.shouldShowSecurityPinWarning)
    }

Denys M's avatar
Denys M committed
198
199
200
201
202
203
204
    @Test
    fun shouldUseLightTheme() {
        // When just created
        // Then
        assertFalse(settings.shouldUseLightTheme)

        // When
205
        settings.shouldUseLightTheme = true
Denys M's avatar
Denys M committed
206
207
208
209
210

        // Then
        assertTrue(settings.shouldUseLightTheme)
    }

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
    @Test
    fun shouldManuallyCloseTabs() {
        // When just created
        // Then
        assertTrue(settings.manuallyCloseTabs)

        // When
        settings.manuallyCloseTabs = false

        // Then
        assertFalse(settings.shouldUseLightTheme)
    }

    @Test
    fun getTabTimeout() {
        // When just created
        // Then
        assertTrue(settings.manuallyCloseTabs)

        // When
        settings.manuallyCloseTabs = false
        settings.closeTabsAfterOneDay = true

        // Then
        assertEquals(settings.getTabTimeout(), Settings.ONE_DAY_MS)

        // When
        settings.closeTabsAfterOneDay = false
        settings.closeTabsAfterOneWeek = true

        // Then
        assertEquals(settings.getTabTimeout(), Settings.ONE_WEEK_MS)

        // When
        settings.closeTabsAfterOneWeek = false
        settings.closeTabsAfterOneMonth = true

        // Then
        assertEquals(settings.getTabTimeout(), Settings.ONE_MONTH_MS)
    }

Denys M's avatar
Denys M committed
252
253
254
255
256
257
258
    @Test
    fun shouldUseAutoSize() {
        // When just created
        // Then
        assertTrue(settings.shouldUseAutoSize)

        // When
259
        settings.shouldUseAutoSize = false
Denys M's avatar
Denys M committed
260
261
262
263
264

        // Then
        assertFalse(settings.shouldUseAutoSize)
    }

265
266
267
268
269
270
271
272
273
274
275
276
277
    @Test
    fun shouldAutofill() {
        // When just created
        // Then
        assertTrue(settings.shouldAutofillLogins)

        // When
        settings.shouldAutofillLogins = false

        // Then
        assertFalse(settings.shouldAutofillLogins)
    }

Denys M's avatar
Denys M committed
278
279
280
281
282
283
284
    @Test
    fun fontSizeFactor() {
        // When just created
        // Then
        assertEquals(1f, settings.fontSizeFactor)

        // When
285
        settings.fontSizeFactor = 2f
Denys M's avatar
Denys M committed
286
287
288
289
290
291

        // Then
        assertEquals(2f, settings.fontSizeFactor)
    }

    @Test
292
    fun shouldShowClipboardSuggestion() {
Denys M's avatar
Denys M committed
293
294
        // When just created
        // Then
295
296
297
        assertTrue(settings.shouldShowClipboardSuggestions)
    }

298
299
300
301
    @Test
    fun shouldShowSearchShortcuts() {
        // When just created
        // Then
302
        assertFalse(settings.shouldShowSearchShortcuts)
303
304
    }

305
306
307
308
309
310
311
312
313
314
315
316
    @Test
    fun shouldShowHistorySuggestions() {
        // When just created
        // Then
        assertTrue(settings.shouldShowHistorySuggestions)
    }

    @Test
    fun shouldShowBookmarkSuggestions() {
        // When just created
        // Then
        assertTrue(settings.shouldShowBookmarkSuggestions)
Denys M's avatar
Denys M committed
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
    }

    @Test
    fun shouldUseDarkTheme() {
        // When just created
        // Then
        assertFalse(settings.shouldUseDarkTheme)
    }

    @Test
    fun shouldFollowDeviceTheme() {
        // When just created
        // Then
        assertFalse(settings.shouldFollowDeviceTheme)

        // When
333
        settings.shouldFollowDeviceTheme = true
Denys M's avatar
Denys M committed
334
335
336
337
338
339
340
341
342
343
344
345

        // Then
        assertTrue(settings.shouldFollowDeviceTheme)
    }

    @Test
    fun shouldUseTrackingProtection() {
        // When
        // Then
        assertTrue(settings.shouldUseTrackingProtection)

        // When
346
        settings.shouldUseTrackingProtection = false
Denys M's avatar
Denys M committed
347
348
349
350
351

        // Then
        assertFalse(settings.shouldUseTrackingProtection)
    }

352
353
354
355
356
357
358
359
360
361
362
363
364
    @Test
    fun shouldShowCollectionsPlaceholderOnHome() {
        // When
        // Then
        assertTrue(settings.showCollectionsPlaceholderOnHome)

        // When
        settings.showCollectionsPlaceholderOnHome = false

        // Then
        assertFalse(settings.showCollectionsPlaceholderOnHome)
    }

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
    @Test
    fun shouldSetOpenInAppOpened() {
        // When
        // Then
        assertFalse(settings.openInAppOpened)

        // When
        settings.openInAppOpened = true

        // Then
        assertTrue(settings.openInAppOpened)
    }

    @Test
    fun shouldSetInstallPwaOpened() {
        // When
        // Then
        assertFalse(settings.installPwaOpened)

        // When
        settings.installPwaOpened = true

        // Then
        assertTrue(settings.installPwaOpened)
    }

391
392
393
394
    @Test
    fun shouldUseTrackingProtectionStrict() {
        // When
        // Then
395
        assertFalse(settings.useStrictTrackingProtection)
396
397
    }

Denys M's avatar
Denys M committed
398
399
400
401
402
403
404
405
406
407
408
    @Test
    fun shouldUseAutoBatteryTheme() {
        // When just created
        // Then
        assertFalse(settings.shouldUseAutoBatteryTheme)
    }

    @Test
    fun showSearchSuggestions() {
        // When just created
        // Then
409
        assertTrue(settings.shouldShowSearchSuggestions)
Denys M's avatar
Denys M committed
410
411
    }

412
413
414
415
    @Test
    fun showPwaFragment() {
        // When just created
        // Then
416
        assertFalse(settings.shouldShowPwaCfr)
417
418
419
420
421

        // When visited once
        settings.incrementVisitedInstallableCount()

        // Then
422
        assertFalse(settings.shouldShowPwaCfr)
423
424
425
426
427

        // When visited twice
        settings.incrementVisitedInstallableCount()

        // Then
428
        assertFalse(settings.shouldShowPwaCfr)
429
430
431
432
433

        // When visited thrice
        settings.incrementVisitedInstallableCount()

        // Then
434
        assertFalse(settings.shouldShowPwaCfr)
435
436
    }

Denys M's avatar
Denys M committed
437
438
439
440
    @Test
    fun sitePermissionsPhoneFeatureCameraAction() {
        // When just created
        // Then
441
442
443
444
        assertEquals(
            ASK_TO_ALLOW,
            settings.getSitePermissionsPhoneFeatureAction(PhoneFeature.CAMERA)
        )
Denys M's avatar
Denys M committed
445
446

        // When
447
        settings.setSitePermissionsPhoneFeatureAction(PhoneFeature.CAMERA, BLOCKED)
Denys M's avatar
Denys M committed
448
449

        // Then
450
        assertEquals(BLOCKED, settings.getSitePermissionsPhoneFeatureAction(PhoneFeature.CAMERA))
Denys M's avatar
Denys M committed
451
452
453
454
455
456
    }

    @Test
    fun sitePermissionsPhoneFeatureMicrophoneAction() {
        // When just created
        // Then
457
458
459
460
        assertEquals(
            ASK_TO_ALLOW,
            settings.getSitePermissionsPhoneFeatureAction(PhoneFeature.MICROPHONE)
        )
Denys M's avatar
Denys M committed
461
462

        // When
463
        settings.setSitePermissionsPhoneFeatureAction(PhoneFeature.MICROPHONE, BLOCKED)
Denys M's avatar
Denys M committed
464
465

        // Then
466
467
468
469
        assertEquals(
            BLOCKED,
            settings.getSitePermissionsPhoneFeatureAction(PhoneFeature.MICROPHONE)
        )
Denys M's avatar
Denys M committed
470
471
472
473
474
475
    }

    @Test
    fun sitePermissionsPhoneFeatureNotificationAction() {
        // When just created
        // Then
476
477
478
479
        assertEquals(
            ASK_TO_ALLOW,
            settings.getSitePermissionsPhoneFeatureAction(PhoneFeature.NOTIFICATION)
        )
Denys M's avatar
Denys M committed
480
481

        // When
482
        settings.setSitePermissionsPhoneFeatureAction(PhoneFeature.NOTIFICATION, BLOCKED)
Denys M's avatar
Denys M committed
483
484

        // Then
485
486
487
488
        assertEquals(
            BLOCKED,
            settings.getSitePermissionsPhoneFeatureAction(PhoneFeature.NOTIFICATION)
        )
Denys M's avatar
Denys M committed
489
490
491
492
493
494
    }

    @Test
    fun sitePermissionsPhoneFeatureLocation() {
        // When just created
        // Then
495
496
497
498
        assertEquals(
            ASK_TO_ALLOW,
            settings.getSitePermissionsPhoneFeatureAction(PhoneFeature.LOCATION)
        )
Denys M's avatar
Denys M committed
499
500

        // When
501
        settings.setSitePermissionsPhoneFeatureAction(PhoneFeature.LOCATION, BLOCKED)
Denys M's avatar
Denys M committed
502
503

        // Then
504
        assertEquals(BLOCKED, settings.getSitePermissionsPhoneFeatureAction(PhoneFeature.LOCATION))
Denys M's avatar
Denys M committed
505
506
507
508
509
510
511
    }

    @Test
    fun getSitePermissionsCustomSettingsRules_default() {
        // When just created
        // Then
        assertEquals(
512
            defaultPermissions,
Denys M's avatar
Denys M committed
513
514
515
516
517
518
519
            settings.getSitePermissionsCustomSettingsRules()
        )
    }

    @Test
    fun getSitePermissionsCustomSettingsRules_camera() {
        // When
520
        settings.setSitePermissionsPhoneFeatureAction(PhoneFeature.CAMERA, BLOCKED)
Denys M's avatar
Denys M committed
521
522
523

        // Then
        assertEquals(
524
            defaultPermissions.copy(camera = BLOCKED),
Denys M's avatar
Denys M committed
525
526
527
528
529
530
531
            settings.getSitePermissionsCustomSettingsRules()
        )
    }

    @Test
    fun getSitePermissionsCustomSettingsRules_notification() {
        // When
532
        settings.setSitePermissionsPhoneFeatureAction(PhoneFeature.NOTIFICATION, BLOCKED)
Denys M's avatar
Denys M committed
533
534
535

        // Then
        assertEquals(
536
            defaultPermissions.copy(notification = BLOCKED),
Denys M's avatar
Denys M committed
537
538
539
540
541
542
543
            settings.getSitePermissionsCustomSettingsRules()
        )
    }

    @Test
    fun getSitePermissionsCustomSettingsRules_location() {
        // When
544
        settings.setSitePermissionsPhoneFeatureAction(PhoneFeature.LOCATION, BLOCKED)
Denys M's avatar
Denys M committed
545
546
547

        // Then
        assertEquals(
548
            defaultPermissions.copy(location = BLOCKED),
Denys M's avatar
Denys M committed
549
550
551
552
553
554
555
            settings.getSitePermissionsCustomSettingsRules()
        )
    }

    @Test
    fun getSitePermissionsCustomSettingsRules_microphone() {
        // When
556
        settings.setSitePermissionsPhoneFeatureAction(PhoneFeature.MICROPHONE, BLOCKED)
Denys M's avatar
Denys M committed
557
558
559

        // Then
        assertEquals(
560
            defaultPermissions.copy(microphone = BLOCKED),
561
562
563
564
565
566
567
568
569
            settings.getSitePermissionsCustomSettingsRules()
        )
    }

    @Test
    fun getSitePermissionsCustomSettingsRules_autoplayAudible() {
        settings.setSitePermissionsPhoneFeatureAction(PhoneFeature.AUTOPLAY_AUDIBLE, ALLOWED)

        assertEquals(
570
            defaultPermissions.copy(autoplayAudible = ALLOWED),
571
572
573
574
575
576
577
578
579
            settings.getSitePermissionsCustomSettingsRules()
        )
    }

    @Test
    fun getSitePermissionsCustomSettingsRules_autoplayInaudible() {
        settings.setSitePermissionsPhoneFeatureAction(PhoneFeature.AUTOPLAY_INAUDIBLE, ALLOWED)

        assertEquals(
580
            defaultPermissions.copy(autoplayInaudible = ALLOWED),
Denys M's avatar
Denys M committed
581
582
583
584
            settings.getSitePermissionsCustomSettingsRules()
        )
    }
}