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 9bd93b7518c9..23c406931341 100644 --- a/android/java/org/chromium/chrome/browser/sync/settings/BraveManageSyncSettings.java +++ b/android/java/org/chromium/chrome/browser/sync/settings/BraveManageSyncSettings.java @@ -6,6 +6,7 @@ package org.chromium.chrome.browser.sync.settings; import android.app.Activity; +import android.content.pm.PackageManager; import android.os.Bundle; import android.text.Spannable; import android.text.SpannableString; @@ -24,12 +25,11 @@ 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; -/** - * See org.brave.bytecode.BraveManageSyncSettingsClassAdapter - */ +/** See org.brave.bytecode.BraveManageSyncSettingsClassAdapter */ public class BraveManageSyncSettings extends ManageSyncSettings { private static final String TAG = "BMSS"; @@ -49,6 +49,11 @@ public class BraveManageSyncSettings extends ManageSyncSettings { private static final String VERBOSE_SYNC_PASSWORDS_PREF_COMMAND_LINE_KEY = "verbose_sync_passwords_pref"; + // Android Runtime for Chrome + 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) { return; @@ -105,8 +110,27 @@ public void onCreatePreferences(@Nullable Bundle savedInstanceState, String root mPrefSyncPasswords = findPreference(PREF_SYNC_PASSWORDS); assert mPrefSyncPasswords != null : "Something has changed in the upstream!"; - overrideWithAuthConfirmationSyncPasswords(); - overrideWithAuthConfirmationSyncEverything(); + // We cannot require Android screenlock if browser runs at ChromeOS + // Google App Runtime emulator, because it is managed by ChromeOS and + // not by the Android subsystem + if (!isRunningOnChromeOS()) { + overrideWithAuthConfirmationSyncPasswords(); + overrideWithAuthConfirmationSyncEverything(); + } + } + + @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); } private void showScreenLockToast() { 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..4aa8bab847b8 --- /dev/null +++ b/android/javatests/org/chromium/chrome/browser/sync/BraveManageSyncSettingsTest.java @@ -0,0 +1,85 @@ +/* Copyright (c) 2024 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",