Commit b26a47d4 authored by Mathias Carlen's avatar Mathias Carlen Committed by Commit Bot

[Autofill Assistant] Introduce a preference switch.

This change introduces an on/off switch which is off by default and is
inspired by SavePasswordsPreferences. It is tied to a shared preference,
lives entirely in Java and gates instantiation of the UiController.

R=gogerald@chromium.org

Bug: 806868
Change-Id: I849b6c5568a0cf7bb787dea2e4e73dc8abbf2c75
Reviewed-on: https://chromium-review.googlesource.com/c/1301443
Commit-Queue: Mathias Carlen <mcarlen@chromium.org>
Reviewed-by: default avatarTheresa <twellington@chromium.org>
Reviewed-by: default avatarGanggui Tang <gogerald@chromium.org>
Cr-Commit-Position: refs/heads/master@{#604182}
parent 1252ba16
......@@ -44,65 +44,70 @@
android:key="autofill_addresses"
android:order="7"
android:title="@string/autofill_addresses_settings_title"/>
<org.chromium.chrome.browser.preferences.ChromeBasePreference
android:fragment="org.chromium.chrome.browser.preferences.autofill_assistant.AutofillAssistantPreferences"
android:key="autofill_assistant"
android:order="8"
android:title="@string/prefs_autofill_assistant_title"/>
<Preference
android:fragment="org.chromium.chrome.browser.preferences.NotificationsPreferences"
android:key="notifications"
android:order="8"
android:order="9"
android:title="@string/prefs_notifications"/>
<Preference
android:fragment="org.chromium.chrome.browser.preferences.ContextualSuggestionsPreference"
android:key="contextual_suggestions"
android:order="9"
android:order="10"
android:title="@string/prefs_contextual_suggestions"/>
<Preference
android:fragment="org.chromium.chrome.browser.preferences.HomepagePreferences"
android:key="homepage"
android:order="10"
android:order="11"
android:title="@string/options_homepage_title"/>
<PreferenceCategory
android:key="advanced_section"
android:order="11"
android:order="12"
android:title="@string/prefs_section_advanced"/>
<Preference
android:fragment="org.chromium.chrome.browser.preferences.privacy.PrivacyPreferences"
android:key="privacy"
android:order="12"
android:order="13"
android:title="@string/prefs_privacy"/>
<Preference
android:fragment="org.chromium.chrome.browser.preferences.AccessibilityPreferences"
android:key="accessibility"
android:order="13"
android:order="14"
android:title="@string/prefs_accessibility"/>
<Preference
android:fragment="org.chromium.chrome.browser.preferences.website.SiteSettingsPreferences"
android:key="content_settings"
android:order="14"
android:order="15"
android:title="@string/prefs_site_settings"/>
<Preference
android:fragment="org.chromium.chrome.browser.preferences.languages.LanguagesPreferences"
android:key="languages"
android:order="15"
android:order="16"
android:title="@string/prefs_languages"/>
<org.chromium.chrome.browser.preferences.ChromeBasePreference
android:fragment="org.chromium.chrome.browser.preferences.datareduction.DataReductionPreferences"
android:key="data_reduction"
android:order="16"
android:order="17"
android:title="@string/data_reduction_title"/>
<org.chromium.chrome.browser.preferences.ChromeBasePreference
android:fragment="org.chromium.chrome.browser.preferences.download.DownloadPreferences"
android:key="downloads"
android:order="17"
android:order="18"
android:title="@string/menu_downloads"/>
<Preference
android:fragment="org.chromium.chrome.browser.preferences.developer.DeveloperPreferences"
android:key="developer"
android:order="18"
android:order="19"
android:title="@string/prefs_developer"/>
<Preference
android:fragment="org.chromium.chrome.browser.preferences.AboutChromePreferences"
android:key="about_chrome"
android:order="19"
android:order="20"
android:title="@string/prefs_about_chrome"/>
</PreferenceScreen>
......@@ -9,11 +9,13 @@ import android.os.Bundle;
import android.support.annotation.Nullable;
import org.chromium.base.Callback;
import org.chromium.base.ContextUtils;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.chrome.browser.autofill.PersonalDataManager;
import org.chromium.chrome.browser.customtabs.CustomTabActivity;
import org.chromium.chrome.browser.payments.AutofillAssistantPaymentRequest;
import org.chromium.chrome.browser.preferences.autofill_assistant.AutofillAssistantPreferences;
import org.chromium.chrome.browser.snackbar.SnackbarManager;
import org.chromium.chrome.browser.tab.EmptyTabObserver;
import org.chromium.chrome.browser.tab.Tab;
......@@ -82,7 +84,9 @@ public class AutofillAssistantUiController implements AutofillAssistantUiDelegat
public static boolean isConfigured(Bundle intentExtras) {
return getBooleanParameter(intentExtras, PARAMETER_ENABLED)
&& !VariationsAssociatedData.getVariationParamValue(STUDY_NAME, URL_PARAMETER_NAME)
.isEmpty();
.isEmpty()
&& ContextUtils.getAppSharedPreferences().getBoolean(
AutofillAssistantPreferences.PREF_AUTOFILL_ASSISTANT_SWITCH, false);
}
/**
......
......@@ -45,6 +45,7 @@ public class MainPreferences extends PreferenceFragment
public static final String PREF_LANGUAGES = "languages";
public static final String PREF_DOWNLOADS = "downloads";
public static final String PREF_DEVELOPER = "developer";
public static final String PREF_AUTOFILL_ASSISTANT = "autofill_assistant";
public static final String AUTOFILL_GUID = "guid";
// Needs to be in sync with kSettingsOrigin[] in
......@@ -158,6 +159,11 @@ public class MainPreferences extends PreferenceFragment
if (!ChromeFeatureList.isEnabled(ChromeFeatureList.DEVELOPER_PREFERENCES)) {
getPreferenceScreen().removePreference(findPreference(PREF_DEVELOPER));
}
// This checks whether Autofill Assistant is enabled.
if (!ChromeFeatureList.isEnabled(ChromeFeatureList.AUTOFILL_ASSISTANT)) {
getPreferenceScreen().removePreference(findPreference(PREF_AUTOFILL_ASSISTANT));
}
}
/**
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.browser.preferences.autofill_assistant;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceFragment;
import org.chromium.base.ContextUtils;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.preferences.ChromeSwitchPreference;
/** The "Autofill Assistant" preferences screen in Settings. */
public class AutofillAssistantPreferences extends PreferenceFragment {
/** Autofill Assistant switch preference key name. */
public static final String PREF_AUTOFILL_ASSISTANT_SWITCH = "autofill_assistant_switch";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setTitle(R.string.prefs_autofill_assistant_title);
setPreferenceScreen(getPreferenceManager().createPreferenceScreen(getActivity()));
}
@Override
public void onResume() {
super.onResume();
createAutofillAssistantSwitch();
}
private void createAutofillAssistantSwitch() {
ChromeSwitchPreference autofillAssistantSwitch =
new ChromeSwitchPreference(getActivity(), null);
autofillAssistantSwitch.setKey(PREF_AUTOFILL_ASSISTANT_SWITCH);
autofillAssistantSwitch.setTitle(R.string.prefs_autofill_assistant_switch);
autofillAssistantSwitch.setSummaryOn(R.string.text_on);
autofillAssistantSwitch.setSummaryOff(R.string.text_off);
autofillAssistantSwitch.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
ContextUtils.getAppSharedPreferences()
.edit()
.putBoolean(PREF_AUTOFILL_ASSISTANT_SWITCH, (boolean) newValue)
.apply();
return true;
}
});
getPreferenceScreen().addPreference(autofillAssistantSwitch);
// Note: setting the switch state before the preference is added to the screen results in
// some odd behavior where the switch state doesn't always match the internal enabled state
// (e.g. the switch will say "On" when it is really turned off), so .setChecked() should be
// called after .addPreference()
autofillAssistantSwitch.setChecked(ContextUtils.getAppSharedPreferences().getBoolean(
PREF_AUTOFILL_ASSISTANT_SWITCH, false));
}
}
file://components/autofill_assistant/OWNERS
\ No newline at end of file
......@@ -3926,6 +3926,14 @@ However, you aren’t invisible. Going private doesn’t hide your browsing from
<message name="IDS_AUTOFILL_ASSISTANT_STOPPED" desc="Text label that is shown when stopping the Autofill Assistant. DURATION_SECONDS is a number representing a duration in seconds, which is why it is appended with 's'.">
Autofill Assistant will stop in <ph name="DURATION_SECONDS">%1$s<ex>3</ex></ph>s…
</message>
<!-- Autofill Assistant preferences -->
<message name="IDS_PREFS_AUTOFILL_ASSISTANT_TITLE" desc="Title for the Autofill Assistant preferences screen. [CHAR-LIMIT=32]">
Autofill Assistant
</message>
<message name="IDS_PREFS_AUTOFILL_ASSISTANT_SWITCH" desc="Title for the switch toggling whether Autofill Assistant is enabled. [CHAR-LIMIT=32]">
Automate
</message>
</messages>
</release>
</grit>
......@@ -1262,6 +1262,7 @@ chrome_java_sources = [
"java/src/org/chromium/chrome/browser/preferences/autofill/AutofillProfileEditorPreference.java",
"java/src/org/chromium/chrome/browser/preferences/autofill/AutofillServerProfilePreferences.java",
"java/src/org/chromium/chrome/browser/preferences/autofill/CreditCardNumberFormattingTextWatcher.java",
"java/src/org/chromium/chrome/browser/preferences/autofill_assistant/AutofillAssistantPreferences.java",
"java/src/org/chromium/chrome/browser/preferences/datareduction/DataReductionMainMenuItem.java",
"java/src/org/chromium/chrome/browser/preferences/datareduction/DataReductionSiteBreakdownView.java",
"java/src/org/chromium/chrome/browser/preferences/datareduction/DataReductionPreferences.java",
......@@ -2093,6 +2094,7 @@ chrome_test_java_sources = [
"javatests/src/org/chromium/chrome/browser/preferences/PreferencesTest.java",
"javatests/src/org/chromium/chrome/browser/preferences/autofill/AutofillProfilesFragmentTest.java",
"javatests/src/org/chromium/chrome/browser/preferences/autofill/AutofillTestRule.java",
"javatests/src/org/chromium/chrome/browser/preferences/autofill_assistant/AutofillAssistantPreferencesTest.java",
"javatests/src/org/chromium/chrome/browser/preferences/datareduction/DataReductionPromoUtilsTest.java",
"javatests/src/org/chromium/chrome/browser/preferences/datareduction/DataReductionStatsPreferenceTest.java",
"javatests/src/org/chromium/chrome/browser/preferences/password/SavePasswordsPreferencesTest.java",
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.browser.preferences.autofill_assistant;
import android.support.test.InstrumentationRegistry;
import android.support.test.espresso.intent.rule.IntentsTestRule;
import android.support.test.filters.SmallTest;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;
import org.junit.runner.RunWith;
import org.chromium.base.ContextUtils;
import org.chromium.base.ThreadUtils;
import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.base.test.util.Feature;
import org.chromium.chrome.browser.ChromeFeatureList;
import org.chromium.chrome.browser.history.HistoryActivity;
import org.chromium.chrome.browser.preferences.ChromeSwitchPreference;
import org.chromium.chrome.browser.preferences.MainPreferences;
import org.chromium.chrome.browser.preferences.Preferences;
import org.chromium.chrome.browser.preferences.PreferencesTest;
import org.chromium.chrome.browser.test.ChromeBrowserTestRule;
import org.chromium.chrome.test.util.browser.Features;
import org.chromium.chrome.test.util.browser.Features.DisableFeatures;
import org.chromium.chrome.test.util.browser.Features.EnableFeatures;
/**
* Tests for the "Autofill Assisatnt" settings screen.
*/
@RunWith(BaseJUnit4ClassRunner.class)
public class AutofillAssistantPreferencesTest {
@Rule
public final ChromeBrowserTestRule mBrowserTestRule = new ChromeBrowserTestRule();
@Rule
public TestRule mProcessor = new Features.InstrumentationProcessor();
@Rule
public IntentsTestRule<HistoryActivity> mHistoryActivityTestRule =
new IntentsTestRule<>(HistoryActivity.class, false, false);
/**
* Set the |PREF_AUTOFILL_ASSISTANT_SWITCH| shared preference to the given |value|.
* @param value The value to set the preference to.
*/
private void setAutofillAssistantSwitch(boolean value) {
ContextUtils.getAppSharedPreferences()
.edit()
.putBoolean(AutofillAssistantPreferences.PREF_AUTOFILL_ASSISTANT_SWITCH, value)
.apply();
}
/**
* Get the |PREF_AUTOFILL_ASSISTANT_SWITCH| shared preference.
* @param defaultValue The default value to use if the preference does not exist.
* @return The value of the shared preference.
*/
private boolean getAutofillAssistantSwitch(boolean defaultValue) {
return ContextUtils.getAppSharedPreferences().getBoolean(
AutofillAssistantPreferences.PREF_AUTOFILL_ASSISTANT_SWITCH, defaultValue);
}
/**
* Ensure that the on/off switch in "Autofill Assistant" settings works.
*/
@Test
@SmallTest
@Feature({"Preferences"})
@EnableFeatures(ChromeFeatureList.AUTOFILL_ASSISTANT)
public void testAutofillAssistantSwitch() throws Exception {
ThreadUtils.runOnUiThreadBlocking(new Runnable() {
@Override
public void run() {
setAutofillAssistantSwitch(true);
}
});
final Preferences preferences =
PreferencesTest.startPreferences(InstrumentationRegistry.getInstrumentation(),
AutofillAssistantPreferences.class.getName());
ThreadUtils.runOnUiThreadBlocking(new Runnable() {
@Override
public void run() {
AutofillAssistantPreferences autofillAssistantPrefs =
(AutofillAssistantPreferences) preferences.getFragmentForTest();
ChromeSwitchPreference onOffSwitch =
(ChromeSwitchPreference) autofillAssistantPrefs.findPreference(
AutofillAssistantPreferences.PREF_AUTOFILL_ASSISTANT_SWITCH);
Assert.assertTrue(onOffSwitch.isChecked());
PreferencesTest.clickPreference(autofillAssistantPrefs, onOffSwitch);
Assert.assertFalse(getAutofillAssistantSwitch(true));
PreferencesTest.clickPreference(autofillAssistantPrefs, onOffSwitch);
Assert.assertTrue(getAutofillAssistantSwitch(false));
preferences.finish();
setAutofillAssistantSwitch(false);
}
});
final Preferences preferences2 =
PreferencesTest.startPreferences(InstrumentationRegistry.getInstrumentation(),
AutofillAssistantPreferences.class.getName());
ThreadUtils.runOnUiThreadBlocking(new Runnable() {
@Override
public void run() {
AutofillAssistantPreferences autofillAssistantPrefs =
(AutofillAssistantPreferences) preferences2.getFragmentForTest();
ChromeSwitchPreference onOffSwitch =
(ChromeSwitchPreference) autofillAssistantPrefs.findPreference(
AutofillAssistantPreferences.PREF_AUTOFILL_ASSISTANT_SWITCH);
Assert.assertFalse(onOffSwitch.isChecked());
}
});
}
/**
* Ensure that the "Autofill Assistant" setting is shown when the feature is enabled.
*/
@Test
@SmallTest
@Feature({"Preferences"})
@EnableFeatures(ChromeFeatureList.AUTOFILL_ASSISTANT)
public void testAutofillAssistantPreferenceEnabled() throws Exception {
final Preferences preferences = PreferencesTest.startPreferences(
InstrumentationRegistry.getInstrumentation(), MainPreferences.class.getName());
ThreadUtils.runOnUiThreadBlocking(new Runnable() {
@Override
public void run() {
MainPreferences mainPrefs = (MainPreferences) preferences.getFragmentForTest();
Assert.assertThat(mainPrefs.findPreference(MainPreferences.PREF_AUTOFILL_ASSISTANT),
is(not(nullValue())));
}
});
}
/**
* Ensure that the "Autofill Assistant" setting is not shown when the feature is disabled.
*/
@Test
@SmallTest
@Feature({"Preferences"})
@DisableFeatures(ChromeFeatureList.AUTOFILL_ASSISTANT)
public void testAutofillAssistantPreferenceDisabled() throws Exception {
final Preferences preferences = PreferencesTest.startPreferences(
InstrumentationRegistry.getInstrumentation(), MainPreferences.class.getName());
ThreadUtils.runOnUiThreadBlocking(new Runnable() {
@Override
public void run() {
MainPreferences mainPrefs = (MainPreferences) preferences.getFragmentForTest();
Assert.assertThat(mainPrefs.findPreference(MainPreferences.PREF_AUTOFILL_ASSISTANT),
is(nullValue()));
}
});
}
}
file://components/autofill_assistant/OWNERS
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment