#include "components/sync_preferences/pref_model_associator.h"
#include <memory>
#include <unordered_set>
#include "base/json/json_string_value_serializer.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_refptr.h"
#include "base/test/gtest_util.h"
#include "base/test/scoped_feature_list.h"
#include "base/values.h"
#include "components/prefs/mock_pref_change_callback.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "components/prefs/testing_pref_store.h"
#include "components/sync/base/features.h"
#include "components/sync/protocol/entity_specifics.pb.h"
#include "components/sync/protocol/preference_specifics.pb.h"
#include "components/sync/test/fake_sync_change_processor.h"
#include "components/sync_preferences/pref_model_associator_client.h"
#include "components/sync_preferences/pref_service_mock_factory.h"
#include "components/sync_preferences/pref_service_syncable.h"
#include "components/sync_preferences/preferences_merge_helper.h"
#include "components/sync_preferences/syncable_prefs_database.h"
#include "components/sync_preferences/test_syncable_prefs_database.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace sync_preferences {
namespace {
NotNull;
const char kStringPrefName[] = …;
const char kListPrefName[] = …;
const char kDictionaryPrefName[] = …;
const char kCustomMergePrefName[] = …;
const char kStringPriorityPrefName[] = …;
#if BUILDFLAG(IS_CHROMEOS_ASH)
const char kStringOsPrefName[] = "os.pref.string";
const char kStringOsPriorityPrefName[] = "os.priority.pref.string";
#endif
const TestSyncablePrefsDatabase::PrefsMap kSyncablePrefsDatabase = …;
syncer::SyncData CreateRemoteSyncData(const std::string& name,
base::ValueView value) { … }
class TestPrefModelAssociatorClient : public PrefModelAssociatorClient { … };
scoped_refptr<user_prefs::PrefRegistrySyncable> CreatePrefRegistry() { … }
std::unique_ptr<PrefServiceSyncable> CreatePrefService(
scoped_refptr<PrefModelAssociatorClient> client,
scoped_refptr<user_prefs::PrefRegistrySyncable> pref_registry,
scoped_refptr<PersistentPrefStore> user_prefs) { … }
class AbstractPreferenceMergeTest : public testing::Test { … };
CustomPreferenceMergeTest;
TEST_F(CustomPreferenceMergeTest, ClientMergesCustomPreference) { … }
class ListPreferenceMergeTest : public AbstractPreferenceMergeTest { … };
TEST_F(ListPreferenceMergeTest, NotListOrDictionary) { … }
TEST_F(ListPreferenceMergeTest, LocalEmpty) { … }
TEST_F(ListPreferenceMergeTest, ServerNull) { … }
TEST_F(ListPreferenceMergeTest, ServerEmpty) { … }
TEST_F(ListPreferenceMergeTest, ServerCorrupt) { … }
TEST_F(ListPreferenceMergeTest, Merge) { … }
TEST_F(ListPreferenceMergeTest, Duplicates) { … }
TEST_F(ListPreferenceMergeTest, Equals) { … }
class DictionaryPreferenceMergeTest : public AbstractPreferenceMergeTest { … };
TEST_F(DictionaryPreferenceMergeTest, LocalEmpty) { … }
TEST_F(DictionaryPreferenceMergeTest, ServerNull) { … }
TEST_F(DictionaryPreferenceMergeTest, ServerEmpty) { … }
TEST_F(DictionaryPreferenceMergeTest, ServerCorrupt) { … }
TEST_F(DictionaryPreferenceMergeTest, MergeNoConflicts) { … }
TEST_F(DictionaryPreferenceMergeTest, MergeConflicts) { … }
TEST_F(DictionaryPreferenceMergeTest, MergeValueToDictionary) { … }
TEST_F(DictionaryPreferenceMergeTest, Equal) { … }
TEST_F(DictionaryPreferenceMergeTest, ConflictButServerWins) { … }
class IndividualPreferenceMergeTest : public AbstractPreferenceMergeTest { … };
TEST_F(IndividualPreferenceMergeTest, ListPreference) { … }
class SyncablePrefsDatabaseTest : public testing::Test { … };
TEST_F(SyncablePrefsDatabaseTest, ShouldAllowRegisteringSyncablePrefs) { … }
TEST_F(SyncablePrefsDatabaseTest, ShouldAllowRegisteringSyncablePriorityPrefs) { … }
#if BUILDFLAG(IS_CHROMEOS_ASH)
TEST_F(SyncablePrefsDatabaseTest, ShouldAllowRegisteringSyncableOSPrefs) {
pref_registry_->RegisterStringPref(
kStringOsPrefName, std::string(),
user_prefs::PrefRegistrySyncable::SYNCABLE_OS_PREF);
EXPECT_THAT(pref_service_->FindPreference(kStringOsPrefName), NotNull());
}
TEST_F(SyncablePrefsDatabaseTest,
ShouldAllowRegisteringSyncableOSPriorityPrefs) {
pref_registry_->RegisterStringPref(
kStringOsPriorityPrefName, std::string(),
user_prefs::PrefRegistrySyncable::SYNCABLE_OS_PRIORITY_PREF);
EXPECT_THAT(pref_service_->FindPreference(kStringOsPriorityPrefName),
NotNull());
}
#endif
SyncablePrefsDatabaseDeathTest;
TEST_F(SyncablePrefsDatabaseDeathTest, ShouldFailRegisteringIllegalPrefs) { … }
TEST_F(SyncablePrefsDatabaseDeathTest,
ShouldFailRegisteringIllegalPriorityPrefs) { … }
#if BUILDFLAG(IS_CHROMEOS_ASH)
TEST_F(SyncablePrefsDatabaseDeathTest, ShouldFailRegisteringIllegalOSPrefs) {
const std::string kIllegalStringPrefName = "not-allowed_string_pref";
const std::string kIllegalListPrefName = "not-allowed_list_pref";
const std::string kIllegalDictPrefName = "not-allowed_dict_pref";
const std::string kExpectedErrorMessageHint = "syncable prefs allowlist";
EXPECT_DCHECK_DEATH_WITH(
pref_registry_->RegisterStringPref(
kIllegalStringPrefName, std::string(),
user_prefs::PrefRegistrySyncable::SYNCABLE_OS_PREF),
kExpectedErrorMessageHint);
EXPECT_DCHECK_DEATH_WITH(
pref_registry_->RegisterListPref(
kIllegalListPrefName,
user_prefs::PrefRegistrySyncable::SYNCABLE_OS_PREF),
kExpectedErrorMessageHint);
EXPECT_DCHECK_DEATH_WITH(
pref_registry_->RegisterDictionaryPref(
kIllegalDictPrefName,
user_prefs::PrefRegistrySyncable::SYNCABLE_OS_PREF),
kExpectedErrorMessageHint);
}
TEST_F(SyncablePrefsDatabaseDeathTest,
ShouldFailRegisteringIllegalOSPriorityPrefs) {
const std::string kIllegalStringPrefName = "not-allowed_string_pref";
const std::string kIllegalListPrefName = "not-allowed_list_pref";
const std::string kIllegalDictPrefName = "not-allowed_dict_pref";
const std::string kExpectedErrorMessageHint = "syncable prefs allowlist";
EXPECT_DCHECK_DEATH_WITH(
pref_registry_->RegisterStringPref(
kIllegalStringPrefName, std::string(),
user_prefs::PrefRegistrySyncable::SYNCABLE_OS_PRIORITY_PREF),
kExpectedErrorMessageHint);
EXPECT_DCHECK_DEATH_WITH(
pref_registry_->RegisterListPref(
kIllegalListPrefName,
user_prefs::PrefRegistrySyncable::SYNCABLE_OS_PRIORITY_PREF),
kExpectedErrorMessageHint);
EXPECT_DCHECK_DEATH_WITH(
pref_registry_->RegisterDictionaryPref(
kIllegalDictPrefName,
user_prefs::PrefRegistrySyncable::SYNCABLE_OS_PRIORITY_PREF),
kExpectedErrorMessageHint);
}
#endif
TEST_F(SyncablePrefsDatabaseDeathTest,
ShouldFailRegisteringPrefsWithInvalidType) { … }
class PrefModelAssociatorWithPreferencesAccountStorageTest
: public testing::Test { … };
TEST_F(PrefModelAssociatorWithPreferencesAccountStorageTest,
ShouldNotNotifyUponInitIfSameValueExistsInLocalStoreOnly) { … }
TEST_F(PrefModelAssociatorWithPreferencesAccountStorageTest,
ShouldNotNotifyUponInitIfSameValueExistsInAccountStoreOnly) { … }
TEST_F(PrefModelAssociatorWithPreferencesAccountStorageTest,
ShouldNotifyUponInitIfDifferentValueExistsInLocalStoreOnly) { … }
TEST_F(PrefModelAssociatorWithPreferencesAccountStorageTest,
ShouldNotifyUponInitIfDifferentValueExistsInAccountStoreOnly) { … }
}
}