diff --git a/android/java/org/chromium/chrome/browser/sync/settings/BraveManageSyncSettings.java b/android/java/org/chromium/chrome/browser/sync/settings/BraveManageSyncSettings.java index 8a6c62485d75..8ea6231a77b3 100644 --- a/android/java/org/chromium/chrome/browser/sync/settings/BraveManageSyncSettings.java +++ b/android/java/org/chromium/chrome/browser/sync/settings/BraveManageSyncSettings.java @@ -25,6 +25,7 @@ import org.chromium.components.browser_ui.settings.ChromeSwitchPreference; import org.chromium.ui.widget.Toast; +import java.util.Optional; import java.util.Timer; import java.util.TimerTask; @@ -50,6 +51,7 @@ public class BraveManageSyncSettings extends ManageSyncSettings { public static final String ARC_FEATURE = "org.chromium.arc"; public static final String ARC_DEVICE_MANAGEMENT_FEATURE = "org.chromium.arc.device_management"; + private static Optional sIsChromeOSForTesting = Optional.empty(); private void verboseIfEnabled(String message) { if (!mVerboseSyncPasswordsPref) { @@ -116,7 +118,15 @@ public void onCreatePreferences(@Nullable Bundle savedInstanceState, String root } } - private static boolean isRunningOnChromeOS() { + @VisibleForTesting + public static void setIsRunningOnChromeOSForTesting(Boolean isRunningOnChromeOS) { + sIsChromeOSForTesting = Optional.of(isRunningOnChromeOS); + } + + private static Boolean isRunningOnChromeOS() { + if (sIsChromeOSForTesting.isPresent()) { + return sIsChromeOSForTesting.get(); + } PackageManager pm = ContextUtils.getApplicationContext().getPackageManager(); return pm.hasSystemFeature(ARC_FEATURE) || pm.hasSystemFeature(ARC_DEVICE_MANAGEMENT_FEATURE); @@ -128,11 +138,11 @@ private void showScreenLockToast() { .show(); } - private void overrideWithAuthConfirmationSyncPasswords() { + public void overrideWithAuthConfirmationSyncPasswords() { overrideWithAuthConfirmation(mPrefSyncPasswords); } - private void overrideWithAuthConfirmationSyncEverything() { + public void overrideWithAuthConfirmationSyncEverything() { overrideWithAuthConfirmation(mSyncEverything); } diff --git a/android/javatests/org/chromium/chrome/browser/sync/BraveManageSyncSettingsTest.java b/android/javatests/org/chromium/chrome/browser/sync/BraveManageSyncSettingsTest.java new file mode 100644 index 000000000000..bbf5620c8aa9 --- /dev/null +++ b/android/javatests/org/chromium/chrome/browser/sync/BraveManageSyncSettingsTest.java @@ -0,0 +1,85 @@ +/* Copyright (c) 2020 The Brave Authors. All rights reserved. + * 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 https://mozilla.org/MPL/2.0/. */ + +package org.chromium.chrome.browser.sync; + +import androidx.preference.Preference; +import androidx.test.filters.SmallTest; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.chromium.base.test.util.CommandLineFlags; +import org.chromium.base.test.util.DoNotBatch; +import org.chromium.base.test.util.Feature; +import org.chromium.base.test.util.Features.DisableFeatures; +import org.chromium.chrome.browser.flags.ChromeFeatureList; +import org.chromium.chrome.browser.flags.ChromeSwitches; +import org.chromium.chrome.browser.settings.SettingsActivity; +import org.chromium.chrome.browser.settings.SettingsActivityTestRule; +import org.chromium.chrome.browser.sync.settings.BraveManageSyncSettings; +import org.chromium.chrome.browser.sync.settings.ManageSyncSettings; +import org.chromium.chrome.test.ChromeJUnit4ClassRunner; +import org.chromium.components.browser_ui.settings.ChromeSwitchPreference; + +/** Tests for BraveManageSyncSettings. */ +@RunWith(ChromeJUnit4ClassRunner.class) +@CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE}) +@DoNotBatch(reason = "TODO(crbug.com/40743432): SyncTestRule doesn't support batching.") +public class BraveManageSyncSettingsTest { + private SettingsActivity mSettingsActivity; + + private final SettingsActivityTestRule mSettingsActivityTestRule = + new SettingsActivityTestRule<>(BraveManageSyncSettings.class); + + @Before + public void setUp() {} + + @Test + @SmallTest + @Feature({"Sync"}) + @DisableFeatures({ChromeFeatureList.REPLACE_SYNC_PROMOS_WITH_SIGN_IN_PROMOS}) + public void syncEverythingOrPasswordsHandlerIsOriginalOnChromeOS() { + syncEverythingOrPasswordsOverridden(true, false); + } + + @Test + @SmallTest + @Feature({"Sync"}) + @DisableFeatures({ChromeFeatureList.REPLACE_SYNC_PROMOS_WITH_SIGN_IN_PROMOS}) + public void syncEverythingOrPasswordsHandlerOverriddenOnNonChromeOS() { + syncEverythingOrPasswordsOverridden(false, true); + } + + void syncEverythingOrPasswordsOverridden( + Boolean isChromeOS, Boolean handlerShouldBeOverridden) { + BraveManageSyncSettings.setIsRunningOnChromeOSForTesting(isChromeOS); + BraveManageSyncSettings fragment = startManageSyncPreferences(); + + ChromeSwitchPreference prefSyncPasswords = + fragment.findPreference(ManageSyncSettings.PREF_SYNC_PASSWORDS); + ChromeSwitchPreference syncEverything = + fragment.findPreference(ManageSyncSettings.PREF_SYNC_EVERYTHING); + + Preference.OnPreferenceChangeListener origSyncPasswordsListner = + prefSyncPasswords.getOnPreferenceChangeListener(); + Preference.OnPreferenceChangeListener origSyncEverythingListner = + syncEverything.getOnPreferenceChangeListener(); + + Assert.assertEquals( + handlerShouldBeOverridden, + origSyncPasswordsListner != (Preference.OnPreferenceChangeListener) fragment); + Assert.assertEquals( + handlerShouldBeOverridden, + origSyncEverythingListner != (Preference.OnPreferenceChangeListener) fragment); + } + + private BraveManageSyncSettings startManageSyncPreferences() { + mSettingsActivity = mSettingsActivityTestRule.startSettingsActivity(); + return mSettingsActivityTestRule.getFragment(); + } +} diff --git a/test/BUILD.gn b/test/BUILD.gn index 937692b026eb..25b5f54c8acc 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -1249,6 +1249,7 @@ if (is_android) { "//brave/android/javatests/org/chromium/chrome/browser/privacy/settings/BravePrivacySettingsTest.java", "//brave/android/javatests/org/chromium/chrome/browser/safe_browsing/settings/BraveSafeBrowsingSettingsFragmentTest.java", "//brave/android/javatests/org/chromium/chrome/browser/safety_check/BraveSafetyCheckSettingsFragmentTest.java", + "//brave/android/javatests/org/chromium/chrome/browser/sync/BraveManageSyncSettingsTest.java", ] deps = [ @@ -1266,6 +1267,7 @@ if (is_android) { "//chrome/browser/contextmenu:java", "//chrome/browser/data_sharing:tab_group_ui_java", "//chrome/browser/feed/android:java", + "//chrome/browser/flags:java", "//chrome/browser/fullscreen/android:java", "//chrome/browser/hub:java", "//chrome/browser/password_manager/android:java", @@ -1288,6 +1290,7 @@ if (is_android) { "//chrome/browser/ui/android/toolbar:java", "//chrome/browser/ui/messages/android:java", "//chrome/browser/user_education:java", + "//chrome/test:test_support_java", "//chrome/test/android:chrome_java_integration_test_support", "//chrome/test/android:chrome_java_test_support_common", "//components/browser_ui/bottomsheet/android:java", @@ -1315,6 +1318,7 @@ if (is_android) { "//components/sync/android:sync_java", "//components/variations/android:variations_java", "//content/public/android:content_full_java", + "//third_party/androidx:androidx_annotation_annotation_java", "//third_party/androidx:androidx_appcompat_appcompat_java", "//third_party/androidx:androidx_fragment_fragment_java", "//third_party/androidx:androidx_preference_preference_java",