For #24968: Add preference for enabling/disabling addresses autofill.

For #24968: Add preference for enabling/disabling addresses autofill.
pull/543/head
mcarare 2 years ago committed by mergify[bot]
parent 25075fe4b3
commit cb4f8ed0c0

@ -597,6 +597,9 @@ abstract class BaseBrowserFragment :
isCreditCardAutofillEnabled = {
context.settings().shouldAutofillCreditCardDetails
},
isAddressAutofillEnabled = {
context.settings().shouldAutofillAddressDetails && FeatureFlags.addressesFeature
},
loginExceptionStorage = context.components.core.loginExceptionStorage,
shareDelegate = object : ShareDelegate {
override fun showShareSheet(

@ -95,9 +95,11 @@ object GeckoProvider {
val geckoRuntime = GeckoRuntime.create(context, runtimeSettings)
geckoRuntime.autocompleteStorageDelegate = GeckoAutocompleteStorageDelegate(
GeckoCreditCardsAddressesStorageDelegate(autofillStorage) {
context.settings().shouldAutofillCreditCardDetails
},
GeckoCreditCardsAddressesStorageDelegate(
storage = autofillStorage,
isCreditCardAutofillEnabled = { context.settings().shouldAutofillCreditCardDetails },
isAddressAutofillEnabled = { context.settings().shouldAutofillAddressDetails }
),
GeckoLoginStorageDelegate(loginStorage)
)

@ -90,12 +90,30 @@ class AutofillSettingFragment : BiometricPromptPreferenceFragment() {
rootKey
)
updateSaveAndAutofillCardsSwitch()
updateSaveAndAutofillAddressesSwitch()
}
/**
* Updates save and autofill cards preference switch state depending on the saved user preference.
*/
internal fun updateSaveAndAutofillCardsSwitch() {
requirePreference<SwitchPreference>(R.string.pref_key_credit_cards_save_and_autofill_cards).apply {
isChecked = context.settings().shouldAutofillCreditCardDetails
onPreferenceChangeListener = SharedPreferenceUpdater()
}
}
/**
* Updates save and autofill addresses preference switch state depending on the saved user preference.
*/
internal fun updateSaveAndAutofillAddressesSwitch() {
requirePreference<SwitchPreference>(R.string.pref_key_addresses_save_and_autofill_addresses).apply {
isChecked = context.settings().shouldAutofillAddressDetails
onPreferenceChangeListener = SharedPreferenceUpdater()
}
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,

@ -1257,6 +1257,17 @@ class Settings(private val appContext: Context) : PreferencesHolder {
default = true
)
/**
* Stores the user choice from the "Autofill Addresses" settings for whether
* save and autofill addresses should be enabled or not.
* If set to `true` when the user focuses on address fields in a webpage an Android prompt is shown,
* allowing the selection of an address details to be automatically filled in the webpage fields.
*/
var shouldAutofillAddressDetails by booleanPreference(
appContext.getPreferenceKey(R.string.pref_key_addresses_save_and_autofill_addresses),
default = true
)
var showPocketRecommendationsFeature by lazyFeatureFlagPreference(
appContext.getPreferenceKey(R.string.pref_key_pocket_homescreen_recommendations),
featureFlag = FeatureFlags.isPocketRecommendationsFeatureEnabled(appContext),

@ -183,6 +183,8 @@
<string name="pref_key_credit_cards_manage_cards" translatable="false">pref_key_credit_cards_manage_cards</string>
<!-- Key for the "Manage addresses" preference in the "Autofill" fragment -->
<string name="pref_key_addresses_manage_addresses" translatable="false">pref_key_addresses_manage_addresses</string>
<!-- Key for the "Save and autofill addresses" preference in the "Autofill" fragment -->
<string name="pref_key_addresses_save_and_autofill_addresses" translatable="false">pref_key_addresses_save_and_autofill_addresses</string>
<!-- Privacy Settings -->
<string name="pref_key_open_links_in_a_private_tab" translatable="false">pref_key_open_links_in_a_private_tab</string>

@ -1496,6 +1496,11 @@
<string name="preferences_addresses_add_address">Add address</string>
<!-- Preference option for managing saved addresses -->
<string name="preferences_addresses_manage_addresses">Manage addresses</string>
<!-- Preference for saving and autofilling addresses -->
<string name="preferences_addresses_save_and_autofill_addresses">Save and autofill addresses</string>
<!-- Preference summary for saving and autofilling address data -->
<string name="preferences_addresses_save_and_autofill_addresses_summary">Include information like numbers, email and shipping addresses</string>
<!-- Title of the "Add card" screen -->
<string name="credit_cards_add_card">Add card</string>
<!-- Title of the "Edit card" screen -->

@ -8,6 +8,12 @@
<androidx.preference.PreferenceCategory
android:layout="@layout/preference_cat_style"
android:title="@string/preferences_addresses">
<SwitchPreference
android:defaultValue="true"
android:key="@string/pref_key_addresses_save_and_autofill_addresses"
android:summary="@string/preferences_addresses_save_and_autofill_addresses_summary"
android:title="@string/preferences_addresses_save_and_autofill_addresses" />
<Preference
android:key="@string/pref_key_addresses_manage_addresses"
android:title="@string/preferences_addresses_manage_addresses" />

@ -7,6 +7,7 @@ package org.mozilla.fenix.settings.autofill
import androidx.fragment.app.FragmentActivity
import androidx.navigation.NavController
import androidx.preference.Preference
import androidx.preference.SwitchPreference
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
@ -17,8 +18,10 @@ import mozilla.components.support.test.robolectric.testContext
import mozilla.components.support.test.rule.MainCoroutineRule
import mozilla.components.support.test.rule.runTestOnMain
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Rule
import org.junit.Test
@ -34,15 +37,17 @@ class AutofillSettingFragmentTest {
@get:Rule
val coroutinesTestRule = MainCoroutineRule()
private val testDispatcher = coroutinesTestRule.testDispatcher
private lateinit var autofillSettingFragment: AutofillSettingFragment
private val navController: NavController = mockk(relaxed = true)
@Before
fun setUp() = runTestOnMain {
every { testContext.components.settings } returns mockk(relaxed = true)
every { testContext.components.core } returns mockk(relaxed = true)
every { testContext.components.settings.addressFeature } returns true
every { testContext.components.settings.shouldAutofillCreditCardDetails } returns true
every { testContext.components.settings.shouldAutofillAddressDetails } returns true
autofillSettingFragment = AutofillSettingFragment()
@ -163,4 +168,60 @@ class AutofillSettingFragmentTest {
)
}
}
@Test
fun `GIVEN the autofill addresses feature is enabled THEN the addresses switch preference is checked`() = runTestOnMain {
every { testContext.components.settings.shouldAutofillAddressDetails } returns true
val autofillAddressesPreference = autofillSettingFragment.findPreference<SwitchPreference>(
autofillSettingFragment.getPreferenceKey(R.string.pref_key_addresses_save_and_autofill_addresses)
)
autofillSettingFragment.updateSaveAndAutofillAddressesSwitch()
assertNotNull(autofillAddressesPreference)
assertTrue(autofillAddressesPreference?.isChecked!!)
}
@Test
fun `GIVEN the autofill addresses feature is disabled THEN the addresses switch preference is NOT checked`() = runTestOnMain {
every { testContext.components.settings.shouldAutofillAddressDetails } returns false
val autofillAddressesPreference = autofillSettingFragment.findPreference<SwitchPreference>(
autofillSettingFragment.getPreferenceKey(R.string.pref_key_addresses_save_and_autofill_addresses)
)
autofillSettingFragment.updateSaveAndAutofillAddressesSwitch()
assertNotNull(autofillAddressesPreference)
assertFalse(autofillAddressesPreference?.isChecked!!)
}
@Test
fun `GIVEN the autofill cards feature is enabled THEN cards the switch preference is checked`() = runTestOnMain {
every { testContext.components.settings.shouldAutofillCreditCardDetails } returns true
val autofillCardsPreference = autofillSettingFragment.findPreference<SwitchPreference>(
autofillSettingFragment.getPreferenceKey(R.string.pref_key_credit_cards_save_and_autofill_cards)
)
autofillSettingFragment.updateSaveAndAutofillCardsSwitch()
assertNotNull(autofillCardsPreference)
assertTrue(autofillCardsPreference?.isChecked!!)
}
@Test
fun `GIVEN the autofill cards feature is disabled THEN the cards switch preference is NOT checked`() = runTestOnMain {
every { testContext.components.settings.shouldAutofillCreditCardDetails } returns false
val autofillCardsPreference = autofillSettingFragment.findPreference<SwitchPreference>(
autofillSettingFragment.getPreferenceKey(R.string.pref_key_credit_cards_save_and_autofill_cards)
)
autofillSettingFragment.updateSaveAndAutofillCardsSwitch()
assertNotNull(autofillCardsPreference)
assertFalse(autofillCardsPreference?.isChecked!!)
}
}

Loading…
Cancel
Save