Skip to content

Commit

Permalink
Test BraveManageSyncSettingsTest
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexeyBarabash committed Dec 23, 2024
1 parent fd01a46 commit 421afb8
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<Boolean> sIsChromeOSForTesting = Optional.empty();

private void verboseIfEnabled(String message) {
if (!mVerboseSyncPasswordsPref) {
Expand Down Expand Up @@ -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);
Expand All @@ -128,11 +138,11 @@ private void showScreenLockToast() {
.show();
}

private void overrideWithAuthConfirmationSyncPasswords() {
public void overrideWithAuthConfirmationSyncPasswords() {
overrideWithAuthConfirmation(mPrefSyncPasswords);
}

private void overrideWithAuthConfirmationSyncEverything() {
public void overrideWithAuthConfirmationSyncEverything() {
overrideWithAuthConfirmation(mSyncEverything);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<BraveManageSyncSettings> 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();
}
}
4 changes: 4 additions & 0 deletions test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 421afb8

Please sign in to comment.