Skip to content

Commit

Permalink
[PriceTracking] Move annotation toggle to Google services
Browse files Browse the repository at this point in the history
Move price annotation toggle from tab switcher menu dialog to Google
services. Will clean up the menu entry and dialog in a later CL.

Bug: 1307943
Change-Id: I79bec1e5bafee1871f0d83da53554d4d0b1acda5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3538760
Reviewed-by: David Trainor <dtrainor@chromium.org>
Commit-Queue: Zhiyuan Cai <zhiyuancai@chromium.org>
Cr-Commit-Position: refs/heads/main@{#985512}
  • Loading branch information
Zhiyuan Cai authored and Chromium LUCI CQ committed Mar 25, 2022
1 parent 66ebc2d commit 2460898
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 141 deletions.
Expand Up @@ -98,15 +98,21 @@ public static boolean isPriceTrackingEnabled() {
return getPriceTrackingEnabled() || getPriceTrackingNotificationsEnabled();
}

/**
* Update SharedPreferences when users turn on/off the feature tracking prices on tabs.
*/
// TODO(crbug.com/1307949): Clean up this api.
@Deprecated
public static void flipTrackPricesOnTabs() {
final boolean enableTrackPricesOnTabs = SHARED_PREFERENCES_MANAGER.readBoolean(
TRACK_PRICES_ON_TABS, isPriceTrackingEnabled());
SHARED_PREFERENCES_MANAGER.writeBoolean(TRACK_PRICES_ON_TABS, !enableTrackPricesOnTabs);
}

/**
* Update SharedPreferences when users turn on/off the feature tracking prices on tabs.
*/
public static void setTrackPricesOnTabsEnabled(boolean enabled) {
SHARED_PREFERENCES_MANAGER.writeBoolean(TRACK_PRICES_ON_TABS, enabled);
}

/**
* @return Whether the track prices on tabs is turned on by users.
*/
Expand Down Expand Up @@ -256,20 +262,19 @@ public static int getAnnotationsEnabledMetricsWindowDurationMilliSeconds() {
*/
public static boolean allowUsersToDisablePriceAnnotations() {
if (FeatureList.isInitialized()) {
return isPriceTrackingEnabled()
return isPriceTrackingEligible()
&& ChromeFeatureList.getFieldTrialParamByFeatureAsBoolean(
ChromeFeatureList.COMMERCE_PRICE_TRACKING,
ALLOW_DISABLE_PRICE_ANNOTATIONS_PARAM, true);
}
return isPriceTrackingEnabled();
return isPriceTrackingEligible();
}

// TODO(crbug.com/1307949): Clean up price tracking menu.
/**
* @return whether we should show the PriceTrackingSettings menu item in grid tab switcher.
*/
public static boolean shouldShowPriceTrackingMenu() {
return isPriceTrackingEligible()
&& (allowUsersToDisablePriceAnnotations()
|| getPriceTrackingNotificationsEnabled());
return false;
}
}
Expand Up @@ -45,6 +45,7 @@

import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.base.test.util.CriteriaHelper;
import org.chromium.base.test.util.DisabledTest;
import org.chromium.base.test.util.Feature;
import org.chromium.base.test.util.FlakyTest;
import org.chromium.base.test.util.Restriction;
Expand Down Expand Up @@ -73,6 +74,7 @@
"force-fieldtrials=Study/Group"})
@Restriction({UiRestriction.RESTRICTION_TYPE_PHONE, RESTRICTION_TYPE_NON_LOW_END_DEVICE})
@Features.DisableFeatures({ChromeFeatureList.START_SURFACE_ANDROID})
@DisabledTest(message = "crbug.com/1307949")
public class PriceTrackingDialogTest {
// clang-format on
private static final String BASE_PARAMS =
Expand Down
5 changes: 5 additions & 0 deletions chrome/android/java/res/xml/google_services_preferences.xml
Expand Up @@ -39,6 +39,11 @@
android:title="@string/prefs_autofill_assistant_title"
android:summary="@string/prefs_autofill_assistant_get_help_summary"
android:persistent="false"/>
<org.chromium.components.browser_ui.settings.ChromeSwitchPreference
android:key="price_tracking_annotations"
android:title="@string/track_prices_on_tabs"
android:summary="@string/track_prices_on_tabs_description"
android:persistent="false"/>
<org.chromium.components.browser_ui.settings.ChromeBasePreference
android:key="autofill_assistant_subsection"
android:title="@string/prefs_autofill_assistant_title"
Expand Down
Expand Up @@ -29,6 +29,7 @@
import org.chromium.chrome.browser.signin.services.IdentityServicesProvider;
import org.chromium.chrome.browser.signin.services.SigninManager;
import org.chromium.chrome.browser.signin.services.UnifiedConsentServiceBridge;
import org.chromium.chrome.browser.tasks.tab_management.PriceTrackingUtilities;
import org.chromium.chrome.browser.ui.signin.SignOutDialogFragment;
import org.chromium.components.autofill_assistant.AssistantFeatures;
import org.chromium.components.autofill_assistant.AutofillAssistantPreferencesUtil;
Expand Down Expand Up @@ -64,6 +65,8 @@ public class GoogleServicesSettings
public static final String PREF_AUTOFILL_ASSISTANT_SUBSECTION = "autofill_assistant_subsection";
@VisibleForTesting
public static final String PREF_METRICS_SETTINGS = "metrics_settings";
@VisibleForTesting
public static final String PREF_PRICE_TRACKING_ANNOTATIONS = "price_tracking_annotations";

private final PrefService mPrefService = UserPrefs.get(Profile.getLastUsedRegularProfile());
private final PrivacyPreferencesManagerImpl mPrivacyPrefManager =
Expand All @@ -75,6 +78,7 @@ public class GoogleServicesSettings
private ChromeSwitchPreference mSearchSuggestions;
private ChromeSwitchPreference mUsageAndCrashReporting;
private ChromeSwitchPreference mUrlKeyedAnonymizedData;
private ChromeSwitchPreference mPriceTrackingAnnotations;
private @Nullable ChromeSwitchPreference mAutofillAssistant;
private @Nullable Preference mContextualSearch;

Expand Down Expand Up @@ -137,6 +141,17 @@ public void onCreatePreferences(@Nullable Bundle savedInstanceState, String root
removePreference(getPreferenceScreen(), mContextualSearch);
mContextualSearch = null;
}

mPriceTrackingAnnotations =
(ChromeSwitchPreference) findPreference(PREF_PRICE_TRACKING_ANNOTATIONS);
if (!PriceTrackingUtilities.allowUsersToDisablePriceAnnotations()) {
removePreference(getPreferenceScreen(), mPriceTrackingAnnotations);
mPriceTrackingAnnotations = null;
} else {
mPriceTrackingAnnotations.setOnPreferenceChangeListener(this);
mPriceTrackingAnnotations.setManagedPreferenceDelegate(mManagedPreferenceDelegate);
}

updatePreferences();
}

Expand Down Expand Up @@ -208,6 +223,8 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
Profile.getLastUsedRegularProfile(), (boolean) newValue);
} else if (PREF_AUTOFILL_ASSISTANT.equals(key)) {
setAutofillAssistantSwitchValue((boolean) newValue);
} else if (PREF_PRICE_TRACKING_ANNOTATIONS.equals(key)) {
PriceTrackingUtilities.setTrackPricesOnTabsEnabled((boolean) newValue);
}
return true;
}
Expand All @@ -234,6 +251,10 @@ private void updatePreferences() {
mContextualSearch.setSummary(
isContextualSearchEnabled ? R.string.text_on : R.string.text_off);
}
if (mPriceTrackingAnnotations != null) {
mPriceTrackingAnnotations.setChecked(
PriceTrackingUtilities.isTrackPricesOnTabsEnabled());
}
}

private ChromeManagedPreferenceDelegate createManagedPreferenceDelegate() {
Expand Down
Expand Up @@ -23,7 +23,6 @@
import org.chromium.chrome.browser.flags.ChromeSwitches;
import org.chromium.chrome.browser.layouts.LayoutTestUtils;
import org.chromium.chrome.browser.layouts.LayoutType;
import org.chromium.chrome.browser.tasks.tab_management.PriceTrackingUtilities;
import org.chromium.chrome.browser.ui.appmenu.AppMenuTestSupport;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.chrome.test.ChromeTabbedActivityTestRule;
Expand Down Expand Up @@ -198,138 +197,6 @@ public void testGroupTabsIsEnabledWithStartSurface() throws Exception {
mActivityTestRule.getAppMenuCoordinator(), R.id.menu_group_tabs));
}

@Test
@SmallTest
@Feature({"Browser", "Main"})
@Features.EnableFeatures({ChromeFeatureList.COMMERCE_PRICE_TRACKING + "<Study"})
@Features.DisableFeatures({ChromeFeatureList.START_SURFACE_ANDROID})
@CommandLineFlags.Add({"force-fieldtrials=Study/Group",
"force-fieldtrial-params=Study.Group:enable_price_tracking/false"})
public void
testTrackPriceOnTabsIsDisabled() throws Exception {
TestThreadUtils.runOnUiThreadBlocking(() -> {
PriceTrackingUtilities.setIsSignedInAndSyncEnabledForTesting(true);
AppMenuTestSupport.showAppMenu(mActivityTestRule.getAppMenuCoordinator(), null, false);
});

assertNull(AppMenuTestSupport.getMenuItemPropertyModel(
mActivityTestRule.getAppMenuCoordinator(), R.id.track_prices_row_menu_id));
}

@Test
@SmallTest
@Feature({"Browser", "Main"})
@Features.EnableFeatures({ChromeFeatureList.COMMERCE_PRICE_TRACKING + "<Study"})
@Features.DisableFeatures({ChromeFeatureList.START_SURFACE_ANDROID})
@CommandLineFlags.Add({"force-fieldtrials=Study/Group",
"force-fieldtrial-params=Study.Group:enable_price_tracking/true"
+ "/allow_disable_price_annotations/true"})
public void
testTrackPriceOnTabsIsEnabled() throws Exception {
TestThreadUtils.runOnUiThreadBlocking(() -> {
PriceTrackingUtilities.setIsSignedInAndSyncEnabledForTesting(true);
AppMenuTestSupport.showAppMenu(mActivityTestRule.getAppMenuCoordinator(), null, false);
});

assertNotNull(AppMenuTestSupport.getMenuItemPropertyModel(
mActivityTestRule.getAppMenuCoordinator(), R.id.track_prices_row_menu_id));
}

@Test
@SmallTest
@Feature({"Browser", "Main"})
@Features.EnableFeatures({ChromeFeatureList.COMMERCE_PRICE_TRACKING + "<Study"})
@Features.DisableFeatures({ChromeFeatureList.START_SURFACE_ANDROID})
@CommandLineFlags.Add({"force-fieldtrials=Study/Group",
"force-fieldtrial-params=Study.Group:enable_price_tracking/true"
+ "/allow_disable_price_annotations/true"})
public void
testTrackPriceOnTabsIsDisabledInIncognitoMode() throws Exception {
TestThreadUtils.runOnUiThreadBlocking(() -> {
PriceTrackingUtilities.setIsSignedInAndSyncEnabledForTesting(true);
mActivityTestRule.getActivity().getTabModelSelector().selectModel(true);
AppMenuTestSupport.showAppMenu(mActivityTestRule.getAppMenuCoordinator(), null, false);
});

assertNull(AppMenuTestSupport.getMenuItemPropertyModel(
mActivityTestRule.getAppMenuCoordinator(), R.id.track_prices_row_menu_id));
}

@Test
@SmallTest
@Feature({"Browser", "Main"})
@Features.EnableFeatures({ChromeFeatureList.COMMERCE_PRICE_TRACKING + "<Study"})
@Features.DisableFeatures({ChromeFeatureList.START_SURFACE_ANDROID})
@CommandLineFlags.Add({"force-fieldtrials=Study/Group",
"force-fieldtrial-params=Study.Group:enable_price_tracking/true"
+ "/allow_disable_price_annotations/true"})
public void
testTrackPriceOnTabsIsDisabledIfSyncDisabledOrNotSignedIn() throws Exception {
TestThreadUtils.runOnUiThreadBlocking(() -> {
PriceTrackingUtilities.setIsSignedInAndSyncEnabledForTesting(false);
AppMenuTestSupport.showAppMenu(mActivityTestRule.getAppMenuCoordinator(), null, false);
});

assertNull(AppMenuTestSupport.getMenuItemPropertyModel(
mActivityTestRule.getAppMenuCoordinator(), R.id.track_prices_row_menu_id));
}

@Test
@SmallTest
@Feature({"Browser", "Main"})
@Features.EnableFeatures({ChromeFeatureList.COMMERCE_PRICE_TRACKING + "<Study"})
@Features.DisableFeatures({ChromeFeatureList.START_SURFACE_ANDROID})
@CommandLineFlags.Add({"force-fieldtrials=Study/Group",
"force-fieldtrial-params=Study.Group:enable_price_tracking/true"
+ "/allow_disable_price_annotations/false/enable_price_notification/false"})
public void
testTrackPriceOnTabsIsDisabledIfNoSettingsAvailable() throws Exception {
TestThreadUtils.runOnUiThreadBlocking(() -> {
PriceTrackingUtilities.setIsSignedInAndSyncEnabledForTesting(true);
AppMenuTestSupport.showAppMenu(mActivityTestRule.getAppMenuCoordinator(), null, false);
});

assertNull(AppMenuTestSupport.getMenuItemPropertyModel(
mActivityTestRule.getAppMenuCoordinator(), R.id.track_prices_row_menu_id));
}

@Test
@SmallTest
@Feature({"Browser", "Main"})
@Features.EnableFeatures({ChromeFeatureList.START_SURFACE_ANDROID,
ChromeFeatureList.COMMERCE_PRICE_TRACKING + "<Study"})
@CommandLineFlags.Add({"force-fieldtrials=Study/Group",
"force-fieldtrial-params=Study.Group:enable_price_tracking/false"})
public void
testTrackPriceOnTabsIsDisabledWithStartSurface() throws Exception {
TestThreadUtils.runOnUiThreadBlocking(() -> {
PriceTrackingUtilities.setIsSignedInAndSyncEnabledForTesting(true);
AppMenuTestSupport.showAppMenu(mActivityTestRule.getAppMenuCoordinator(), null, false);
});

assertNull(AppMenuTestSupport.getMenuItemPropertyModel(
mActivityTestRule.getAppMenuCoordinator(), R.id.track_prices_row_menu_id));
}

@Test
@SmallTest
@Feature({"Browser", "Main"})
@Features.EnableFeatures({ChromeFeatureList.START_SURFACE_ANDROID,
ChromeFeatureList.COMMERCE_PRICE_TRACKING + "<Study"})
@CommandLineFlags.Add({"force-fieldtrials=Study/Group",
"force-fieldtrial-params=Study.Group:enable_price_tracking/true"
+ "/allow_disable_price_annotations/true"})
public void
testTrackPriceOnTabsIsEnabledWithStartSurface() throws Exception {
TestThreadUtils.runOnUiThreadBlocking(() -> {
PriceTrackingUtilities.setIsSignedInAndSyncEnabledForTesting(true);
AppMenuTestSupport.showAppMenu(mActivityTestRule.getAppMenuCoordinator(), null, false);
});

assertNotNull(AppMenuTestSupport.getMenuItemPropertyModel(
mActivityTestRule.getAppMenuCoordinator(), R.id.track_prices_row_menu_id));
}

private void verifyTabSwitcherMenu() {
assertNotNull(AppMenuTestSupport.getMenuItemPropertyModel(
mActivityTestRule.getAppMenuCoordinator(), R.id.new_tab_menu_id));
Expand Down
Expand Up @@ -30,6 +30,7 @@
import org.chromium.chrome.browser.settings.SettingsActivityTestRule;
import org.chromium.chrome.browser.signin.services.IdentityServicesProvider;
import org.chromium.chrome.browser.sync.settings.GoogleServicesSettings;
import org.chromium.chrome.browser.tasks.tab_management.PriceTrackingUtilities;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.chrome.test.ChromeTabbedActivityTestRule;
import org.chromium.chrome.test.util.browser.Features.DisableFeatures;
Expand Down Expand Up @@ -329,6 +330,74 @@ public void testMetricsSettingsShownFlagOn() {
});
}

@Test
@LargeTest
@Feature({"Preference"})
@EnableFeatures({ChromeFeatureList.COMMERCE_PRICE_TRACKING + "<Study"})
@CommandLineFlags.Add({"force-fieldtrials=Study/Group",
"force-fieldtrial-params=Study.Group:enable_price_tracking/true"
+ "/allow_disable_price_annotations/true"})
public void
testPriceTrackingAnnotations() {
TestThreadUtils.runOnUiThreadBlocking(
() -> PriceTrackingUtilities.setIsSignedInAndSyncEnabledForTesting(true));

final GoogleServicesSettings googleServicesSettings = startGoogleServicesSettings();

TestThreadUtils.runOnUiThreadBlocking(() -> {
ChromeSwitchPreference priceAnnotationsSwitch =
(ChromeSwitchPreference) googleServicesSettings.findPreference(
GoogleServicesSettings.PREF_PRICE_TRACKING_ANNOTATIONS);
Assert.assertTrue(priceAnnotationsSwitch.isVisible());
Assert.assertTrue(priceAnnotationsSwitch.isChecked());

priceAnnotationsSwitch.performClick();
Assert.assertFalse(PriceTrackingUtilities.isTrackPricesOnTabsEnabled());
priceAnnotationsSwitch.performClick();
Assert.assertTrue(PriceTrackingUtilities.isTrackPricesOnTabsEnabled());
});
}

@Test
@LargeTest
@Feature({"Preference"})
@EnableFeatures({ChromeFeatureList.COMMERCE_PRICE_TRACKING + "<Study"})
@CommandLineFlags.Add({"force-fieldtrials=Study/Group",
"force-fieldtrial-params=Study.Group:enable_price_tracking/true"
+ "/allow_disable_price_annotations/false"})
public void
testPriceTrackingAnnotations_FeatureDisabled() {
TestThreadUtils.runOnUiThreadBlocking(
() -> PriceTrackingUtilities.setIsSignedInAndSyncEnabledForTesting(true));

final GoogleServicesSettings googleServicesSettings = startGoogleServicesSettings();

TestThreadUtils.runOnUiThreadBlocking(() -> {
Assert.assertNull(googleServicesSettings.findPreference(
GoogleServicesSettings.PREF_PRICE_TRACKING_ANNOTATIONS));
});
}

@Test
@LargeTest
@Feature({"Preference"})
@EnableFeatures({ChromeFeatureList.COMMERCE_PRICE_TRACKING + "<Study"})
@CommandLineFlags.Add({"force-fieldtrials=Study/Group",
"force-fieldtrial-params=Study.Group:enable_price_tracking/true"
+ "/allow_disable_price_annotations/true"})
public void
testPriceTrackingAnnotations_NotSignedIn() {
TestThreadUtils.runOnUiThreadBlocking(
() -> PriceTrackingUtilities.setIsSignedInAndSyncEnabledForTesting(false));

final GoogleServicesSettings googleServicesSettings = startGoogleServicesSettings();

TestThreadUtils.runOnUiThreadBlocking(() -> {
Assert.assertNull(googleServicesSettings.findPreference(
GoogleServicesSettings.PREF_PRICE_TRACKING_ANNOTATIONS));
});
}

private void setAutofillAssistantSwitchValue(boolean newValue) {
AutofillAssistantPreferencesUtil.setAssistantEnabledPreference(newValue);
}
Expand Down

0 comments on commit 2460898

Please sign in to comment.