diff --git a/app/src/main/java/org/mozilla/fenix/components/TrackingProtectionPolicyFactory.kt b/app/src/main/java/org/mozilla/fenix/components/TrackingProtectionPolicyFactory.kt index d101c16d1..9a0853e43 100644 --- a/app/src/main/java/org/mozilla/fenix/components/TrackingProtectionPolicyFactory.kt +++ b/app/src/main/java/org/mozilla/fenix/components/TrackingProtectionPolicyFactory.kt @@ -4,7 +4,10 @@ package org.mozilla.fenix.components +import androidx.annotation.VisibleForTesting import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy +import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicyForSessionTypes +import org.mozilla.fenix.Config import org.mozilla.fenix.utils.Settings /** @@ -34,9 +37,9 @@ class TrackingProtectionPolicyFactory(private val settings: Settings) { } return when { - normalMode && privateMode -> trackingProtectionPolicy - normalMode && !privateMode -> trackingProtectionPolicy.forRegularSessionsOnly() - !normalMode && privateMode -> trackingProtectionPolicy.forPrivateSessionsOnly() + normalMode && privateMode -> trackingProtectionPolicy.adaptPolicyToChannel() + normalMode && !privateMode -> trackingProtectionPolicy.adaptPolicyToChannel().forRegularSessionsOnly() + !normalMode && privateMode -> trackingProtectionPolicy.adaptPolicyToChannel().forPrivateSessionsOnly() else -> TrackingProtectionPolicy.none() } } @@ -44,7 +47,8 @@ class TrackingProtectionPolicyFactory(private val settings: Settings) { private fun createCustomTrackingProtectionPolicy(): TrackingProtectionPolicy { return TrackingProtectionPolicy.select( cookiePolicy = getCustomCookiePolicy(), - trackingCategories = getCustomTrackingCategories() + trackingCategories = getCustomTrackingCategories(), + cookiePurging = Config.channel.isNightlyOrDebug ).let { if (settings.blockTrackingContentSelectionInCustomTrackingProtection == "private") { it.forPrivateSessionsOnly() @@ -91,3 +95,13 @@ class TrackingProtectionPolicyFactory(private val settings: Settings) { return categories.toTypedArray() } } + +@VisibleForTesting +internal fun TrackingProtectionPolicyForSessionTypes.adaptPolicyToChannel(): TrackingProtectionPolicyForSessionTypes { + return TrackingProtectionPolicy.select( + trackingCategories = trackingCategories, + cookiePolicy = cookiePolicy, + strictSocialTrackingProtection = strictSocialTrackingProtection, + cookiePurging = Config.channel.isNightlyOrDebug + ) +} diff --git a/app/src/test/java/org/mozilla/fenix/components/TrackingProtectionPolicyFactoryTest.kt b/app/src/test/java/org/mozilla/fenix/components/TrackingProtectionPolicyFactoryTest.kt index 2344bf4b8..9289859d2 100644 --- a/app/src/test/java/org/mozilla/fenix/components/TrackingProtectionPolicyFactoryTest.kt +++ b/app/src/test/java/org/mozilla/fenix/components/TrackingProtectionPolicyFactoryTest.kt @@ -2,7 +2,9 @@ package org.mozilla.fenix.components import io.mockk.every import io.mockk.mockk +import io.mockk.mockkObject import mozilla.components.concept.engine.EngineSession +import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy import mozilla.components.support.test.robolectric.testContext import org.junit.Assert.assertArrayEquals import org.junit.Assert.assertEquals @@ -10,7 +12,9 @@ import org.junit.Assert.assertFalse import org.junit.Assert.assertTrue import org.junit.Test import org.junit.runner.RunWith +import org.mozilla.fenix.Config import org.mozilla.fenix.R +import org.mozilla.fenix.ReleaseChannel import org.mozilla.fenix.utils.Settings import org.mozilla.fenix.helpers.FenixRobolectricTestRunner @@ -87,6 +91,56 @@ class TrackingProtectionPolicyFactoryTest { expected.assertPolicyEquals(always, checkPrivacy = false) } + @Test + fun `cookiePurging must be available ONLY in nightly or debug`() { + mockkObject(Config) + for (channel in ReleaseChannel.values()) { + every { Config.channel } returns channel + + val shouldCookiePurgingActive = channel.isNightlyOrDebug + val customSetting = + settingsForCustom(shouldBlockCookiesInCustom = true, blockCookiesSelection = "all") + val stringSetting = mockSettings(useStrict = true) + val recommendedSetting = mockSettings(useTrackingProtection = true) + + for (setting in arrayOf(recommendedSetting, stringSetting, customSetting)) { + val factory = TrackingProtectionPolicyFactory(setting) + val privateOnly = + factory.createTrackingProtectionPolicy(normalMode = false, privateMode = true) + val normalOnly = + factory.createTrackingProtectionPolicy(normalMode = true, privateMode = false) + val always = + factory.createTrackingProtectionPolicy(normalMode = true, privateMode = true) + + assertEquals(shouldCookiePurgingActive, privateOnly.cookiePurging) + assertEquals(shouldCookiePurgingActive, normalOnly.cookiePurging) + assertEquals(shouldCookiePurgingActive, always.cookiePurging) + } + } + } + + @Test + fun `adaptPolicyToChannel MUST only update properties that have changed per given channel`() { + mockkObject(Config) + + val policies = arrayOf( + TrackingProtectionPolicy.strict(), TrackingProtectionPolicy.recommended(), + TrackingProtectionPolicy.select() + ) + + for (channel in ReleaseChannel.values()) { + every { Config.channel } returns channel + + val shouldCookiePurgingActive = channel.isNightlyOrDebug + + for (policy in policies) { + val adaptedPolicy = policy.adaptPolicyToChannel() + policy.assertPolicyEquals(adaptedPolicy, checkPrivacy = false) + assertEquals(shouldCookiePurgingActive, adaptedPolicy.cookiePurging) + } + } + } + @Test fun `GIVEN custom policy WHEN cookie policy social THEN tracking policy should have cookie policy allow non-trackers`() { val expected = EngineSession.TrackingProtectionPolicy.select(