[fenix] For https://github.com/mozilla-mobile/fenix/issues/18175 - Add a new total protection ETP cookies policy
This should only add the new option in settings based on a Nimbus experiment.pull/600/head
parent
19bcd56a09
commit
733dce16d6
@ -0,0 +1,47 @@
|
||||
/* 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 org.mozilla.fenix.settings
|
||||
|
||||
import android.content.Context
|
||||
import android.util.AttributeSet
|
||||
import org.mozilla.fenix.R
|
||||
import org.mozilla.fenix.ext.settings
|
||||
|
||||
/**
|
||||
* Custom [DropDownListPreference] that automatically builds the list of available options for the
|
||||
* custom Enhanced Tracking Protection option depending on the current Nimbus experiments.
|
||||
*/
|
||||
class CustomEtpCookiesOptionsDropDownListPreference @JvmOverloads constructor(
|
||||
context: Context,
|
||||
attrs: AttributeSet? = null
|
||||
) : DropDownListPreference(context, attrs) {
|
||||
init {
|
||||
with(context) {
|
||||
entries = arrayOf(
|
||||
getString(R.string.preference_enhanced_tracking_protection_custom_cookies_1),
|
||||
getString(R.string.preference_enhanced_tracking_protection_custom_cookies_2),
|
||||
getString(R.string.preference_enhanced_tracking_protection_custom_cookies_3),
|
||||
getString(R.string.preference_enhanced_tracking_protection_custom_cookies_4),
|
||||
)
|
||||
|
||||
entryValues = arrayOf(
|
||||
getString(R.string.social),
|
||||
getString(R.string.unvisited),
|
||||
getString(R.string.third_party),
|
||||
getString(R.string.all),
|
||||
)
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
if (context.settings().enabledTotalCookieProtectionSetting) {
|
||||
// If the new "Total cookie protection" should be shown it must be first item.
|
||||
entries = arrayOf(getString(R.string.preference_enhanced_tracking_protection_custom_cookies_5)) +
|
||||
entries as Array<String>
|
||||
entryValues = arrayOf(getString(R.string.total_protection)) + entryValues as Array<String>
|
||||
}
|
||||
}
|
||||
|
||||
setDefaultValue(entryValues.first())
|
||||
}
|
||||
}
|
@ -0,0 +1,86 @@
|
||||
/* 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 org.mozilla.fenix.settings
|
||||
|
||||
import android.content.Context
|
||||
import androidx.preference.Preference
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import io.mockk.mockkStatic
|
||||
import mozilla.components.support.test.robolectric.testContext
|
||||
import org.junit.Assert.assertArrayEquals
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mozilla.fenix.R
|
||||
import org.mozilla.fenix.ext.settings
|
||||
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
|
||||
|
||||
@RunWith(FenixRobolectricTestRunner::class)
|
||||
class CustomEtpCookiesOptionsDropDownListPreferenceTest {
|
||||
@Test
|
||||
fun `GIVEN total cookie protection is enabled WHEN using this preference THEN show the total cookie protection option`() {
|
||||
val expectedEntries = arrayOf(
|
||||
testContext.getString(R.string.preference_enhanced_tracking_protection_custom_cookies_5)
|
||||
) + defaultEntries
|
||||
val expectedValues = arrayOf(testContext.getString(R.string.total_protection)) + defaultValues
|
||||
|
||||
mockkStatic("org.mozilla.fenix.ext.ContextKt") {
|
||||
every { any<Context>().settings() } returns mockk {
|
||||
every { enabledTotalCookieProtectionSetting } returns true
|
||||
}
|
||||
|
||||
val preference = CustomEtpCookiesOptionsDropDownListPreference(testContext)
|
||||
|
||||
assertArrayEquals(expectedEntries, preference.entries)
|
||||
assertArrayEquals(expectedValues, preference.entryValues)
|
||||
assertEquals(expectedValues[0], preference.getDefaultValue())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `GIVEN total cookie protection is disabled WHEN using this preference THEN don't show the total cookie protection option`() {
|
||||
mockkStatic("org.mozilla.fenix.ext.ContextKt") {
|
||||
every { any<Context>().settings() } returns mockk {
|
||||
every { enabledTotalCookieProtectionSetting } returns false
|
||||
}
|
||||
|
||||
val preference = CustomEtpCookiesOptionsDropDownListPreference(testContext)
|
||||
|
||||
assertArrayEquals(defaultEntries, preference.entries)
|
||||
assertArrayEquals(defaultValues, preference.entryValues)
|
||||
assertEquals(defaultValues[0], preference.getDefaultValue())
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Use reflection to get the private member holding the default value set for this preference.
|
||||
*/
|
||||
private fun CustomEtpCookiesOptionsDropDownListPreference.getDefaultValue(): String {
|
||||
return Preference::class.java
|
||||
.getDeclaredField("mDefaultValue").let { field ->
|
||||
field.isAccessible = true
|
||||
return@let field.get(this) as String
|
||||
}
|
||||
}
|
||||
|
||||
private val defaultEntries = with(testContext) {
|
||||
arrayOf(
|
||||
getString(R.string.preference_enhanced_tracking_protection_custom_cookies_1),
|
||||
getString(R.string.preference_enhanced_tracking_protection_custom_cookies_2),
|
||||
getString(R.string.preference_enhanced_tracking_protection_custom_cookies_3),
|
||||
getString(R.string.preference_enhanced_tracking_protection_custom_cookies_4),
|
||||
)
|
||||
}
|
||||
|
||||
private val defaultValues = with(testContext) {
|
||||
arrayOf(
|
||||
getString(R.string.social),
|
||||
getString(R.string.unvisited),
|
||||
getString(R.string.third_party),
|
||||
getString(R.string.all),
|
||||
)
|
||||
}
|
||||
}
|
@ -0,0 +1,79 @@
|
||||
/* 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 org.mozilla.fenix.trackingprotection
|
||||
|
||||
import android.content.Context
|
||||
import androidx.fragment.app.FragmentActivity
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import io.mockk.mockkStatic
|
||||
import mozilla.components.support.test.robolectric.testContext
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mozilla.fenix.R
|
||||
import org.mozilla.fenix.ext.settings
|
||||
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
|
||||
import org.mozilla.fenix.trackingprotection.TrackingProtectionMode.CUSTOM
|
||||
import org.robolectric.Robolectric
|
||||
|
||||
@RunWith(FenixRobolectricTestRunner::class)
|
||||
class TrackingProtectionBlockingFragmentTest {
|
||||
@Test
|
||||
fun `GIVEN total cookie protection is enabled WHEN showing details about the protection options THEN show update details for tracking protection`() {
|
||||
val expectedTitle = testContext.getString(R.string.etp_cookies_title_2)
|
||||
val expectedDescription = testContext.getString(R.string.etp_cookies_description_2)
|
||||
|
||||
mockkStatic("org.mozilla.fenix.ext.ContextKt") {
|
||||
every { any<Context>().settings() } returns mockk(relaxed = true) {
|
||||
every { enabledTotalCookieProtectionSetting } returns true
|
||||
}
|
||||
|
||||
val fragment = createFragment()
|
||||
|
||||
val cookiesCategory = fragment.binding.categoryCookies
|
||||
assertEquals(expectedTitle, cookiesCategory.trackingProtectionCategoryTitle.text)
|
||||
assertEquals(expectedDescription, cookiesCategory.trackingProtectionCategoryItemDescription.text)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `GIVEN total cookie protection is not enabled WHEN showing details about the protection options THEN show the default details for tracking protection`() {
|
||||
val expectedTitle = testContext.getString(R.string.etp_cookies_title)
|
||||
val expectedDescription = testContext.getString(R.string.etp_cookies_description)
|
||||
|
||||
mockkStatic("org.mozilla.fenix.ext.ContextKt") {
|
||||
every { any<Context>().settings() } returns mockk(relaxed = true) {
|
||||
every { enabledTotalCookieProtectionSetting } returns false
|
||||
}
|
||||
|
||||
val fragment = createFragment()
|
||||
|
||||
val cookiesCategory = fragment.binding.categoryCookies
|
||||
assertEquals(expectedTitle, cookiesCategory.trackingProtectionCategoryTitle.text)
|
||||
assertEquals(expectedDescription, cookiesCategory.trackingProtectionCategoryItemDescription.text)
|
||||
}
|
||||
}
|
||||
|
||||
private fun createFragment(): TrackingProtectionBlockingFragment {
|
||||
// Create and attach the fragment ourself instead of using "createAddedTestFragment"
|
||||
// to prevent having "onResume -> showToolbar" called.
|
||||
|
||||
val activity = Robolectric.buildActivity(FragmentActivity::class.java)
|
||||
.create()
|
||||
.start()
|
||||
.get()
|
||||
val fragment = TrackingProtectionBlockingFragment().apply {
|
||||
arguments = TrackingProtectionBlockingFragmentArgs(
|
||||
protectionMode = CUSTOM
|
||||
).toBundle()
|
||||
}
|
||||
activity.supportFragmentManager.beginTransaction()
|
||||
.add(fragment, "test")
|
||||
.commitNow()
|
||||
|
||||
return fragment
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue