1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_SYNC_TEST_INTEGRATION_PREFERENCES_HELPER_H_
#define CHROME_BROWSER_SYNC_TEST_INTEGRATION_PREFERENCES_HELPER_H_
#include <stdint.h>
#include <memory>
#include <optional>
#include <string>
#include <vector>
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/values.h"
#include "chrome/browser/sync/test/integration/fake_server_match_status_checker.h"
#include "chrome/browser/sync/test/integration/single_client_status_change_checker.h"
#include "chrome/browser/sync/test/integration/status_change_checker.h"
#include "components/prefs/json_pref_store.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/sync/base/data_type.h"
#include "components/sync/protocol/preference_specifics.pb.h"
#include "components/sync/test/fake_server.h"
class PrefChangeRegistrar;
class PrefService;
class Profile;
namespace user_prefs {
class PrefRegistrySyncable;
}
namespace preferences_helper {
// Used to access the preferences within a particular sync profile.
PrefService* GetPrefs(int index);
// Provides access to the syncable pref registy of a profile.
user_prefs::PrefRegistrySyncable* GetRegistry(Profile* profile);
// Inverts the value of the boolean preference with name |pref_name| in the
// profile with index |index|.
void ChangeBooleanPref(int index, const char* pref_name);
// Changes the value of the integer preference with name |pref_name| in the
// profile with index |index| to |new_value|.
void ChangeIntegerPref(int index, const char* pref_name, int new_value);
// Changes the value of the string preference with name |pref_name| in the
// profile with index |index| to |new_value|.
void ChangeStringPref(int index,
const char* pref_name,
const std::string& new_value);
// Clears the value of the preference with name |pref_name| in the profile with
// index |index|.
void ClearPref(int index, const char* pref_name);
// Changes the value of the list preference with name |pref_name| in the
// profile with index |index| to |new_value|.
void ChangeListPref(int index,
const char* pref_name,
const base::Value::List& new_value);
// Used to verify that the boolean preference with name |pref_name| has the
// same value across all profiles.
[[nodiscard]] bool BooleanPrefMatches(const char* pref_name);
// Used to verify that the integer preference with name |pref_name| has the
// same value across all profiles.
[[nodiscard]] bool IntegerPrefMatches(const char* pref_name);
// Used to verify that the string preference with name |pref_name| has the
// same value across all profiles.
[[nodiscard]] bool StringPrefMatches(const char* pref_name);
// Used to verify that the list preference with name |pref_name| has the
// same value across all profiles.
[[nodiscard]] bool ListPrefMatches(const char* pref_name);
// Returns a server-side preference in FakeServer for |pref_name| or nullopt if
// no preference exists.
std::optional<sync_pb::PreferenceSpecifics> GetPreferenceInFakeServer(
syncer::DataType data_type,
const std::string& pref_name,
fake_server::FakeServer* fake_server);
// Converts `value` to the synced pref value, i.e. the value as it is sent via
// the specifics.
std::string ConvertPrefValueToValueInSpecifics(const base::Value& value);
} // namespace preferences_helper
// Checker that blocks until pref has the specified value.
class PrefValueChecker : public StatusChangeChecker {
public:
PrefValueChecker(PrefService* pref_service,
const char* path,
base::Value expected_value);
~PrefValueChecker() override;
bool IsExitConditionSatisfied(std::ostream* os) override;
private:
const char* path_;
const base::Value expected_value_;
const raw_ptr<PrefService> pref_service_;
PrefChangeRegistrar pref_change_registrar_;
};
// Abstract checker that takes care of registering for preference changes.
class PrefMatchChecker : public StatusChangeChecker {
public:
explicit PrefMatchChecker(const char* path);
~PrefMatchChecker() override;
// StatusChangeChecker implementation.
bool IsExitConditionSatisfied(std::ostream* os) override = 0;
protected:
const char* GetPath() const;
private:
void RegisterPrefListener(PrefService* pref_service);
std::vector<std::unique_ptr<PrefChangeRegistrar>> pref_change_registrars_;
const char* path_;
};
// Matcher that blocks until the specified list pref matches on all clients.
class ListPrefMatchChecker : public PrefMatchChecker {
public:
explicit ListPrefMatchChecker(const char* path);
// PrefMatchChecker implementation.
bool IsExitConditionSatisfied(std::ostream* os) override;
};
// Matcher that blocks until the specified boolean pref matches on all clients.
class BooleanPrefMatchChecker : public PrefMatchChecker {
public:
explicit BooleanPrefMatchChecker(const char* path);
// PrefMatchChecker implementation.
bool IsExitConditionSatisfied(std::ostream* os) override;
};
// Matcher that blocks until the specified integer pref matches on all clients.
class IntegerPrefMatchChecker : public PrefMatchChecker {
public:
explicit IntegerPrefMatchChecker(const char* path);
// PrefMatchChecker implementation.
bool IsExitConditionSatisfied(std::ostream* os) override;
};
// Matcher that blocks until the specified string pref matches on all clients.
class StringPrefMatchChecker : public PrefMatchChecker {
public:
explicit StringPrefMatchChecker(const char* path);
// PrefMatchChecker implementation.
bool IsExitConditionSatisfied(std::ostream* os) override;
};
// Matcher that blocks until the specified pref is cleared on all clients.
class ClearedPrefMatchChecker : public PrefMatchChecker {
public:
explicit ClearedPrefMatchChecker(const char* path);
// PrefMatchChecker implementation.
bool IsExitConditionSatisfied(std::ostream* os) override;
};
// Waits until GetPreferenceInFakeServer() returns an expected value.
class FakeServerPrefMatchesValueChecker
: public fake_server::FakeServerMatchStatusChecker {
public:
FakeServerPrefMatchesValueChecker(syncer::DataType data_type,
const std::string& pref_name,
const std::string& expected_value);
protected:
// StatusChangeChecker overrides.
bool IsExitConditionSatisfied(std::ostream* os) override;
private:
const syncer::DataType data_type_;
const std::string pref_name_;
const std::string expected_value_;
};
#endif // CHROME_BROWSER_SYNC_TEST_INTEGRATION_PREFERENCES_HELPER_H_
|