1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/prefs/testing_pref_store.h"
6
7 #include <memory>
8 #include <string>
9 #include <utility>
10
11 #include "base/json/json_writer.h"
12 #include "base/run_loop.h"
13 #include "base/strings/string_piece.h"
14 #include "base/values.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 namespace {
18
19 class ChangedValueWaiter : public PrefStore::Observer {
20 public:
ChangedValueWaiter(scoped_refptr<PrefStore> store,std::string key)21 ChangedValueWaiter(scoped_refptr<PrefStore> store, std::string key)
22 : store_(std::move(store)), key_(std::move(key)) {
23 store_->AddObserver(this);
24
25 const base::Value* old_value = nullptr;
26 if (store_->GetValue(key_, &old_value)) {
27 old_value_ = old_value->Clone();
28 }
29 }
30
~ChangedValueWaiter()31 ~ChangedValueWaiter() override { store_->RemoveObserver(this); }
32
Wait()33 void Wait() { run_loop_.Run(); }
34
35 private:
QuitRunLoopIfNewValueIsPresent()36 void QuitRunLoopIfNewValueIsPresent() {
37 std::optional<base::Value> new_value;
38 {
39 const base::Value* value = nullptr;
40 if (store_->GetValue(key_, &value)) {
41 new_value = value->Clone();
42 }
43 }
44
45 if (new_value != old_value_) {
46 run_loop_.Quit();
47 }
48 }
49
OnInitializationCompleted(bool succeeded)50 void OnInitializationCompleted(bool succeeded) override {
51 QuitRunLoopIfNewValueIsPresent();
52 }
53
OnPrefValueChanged(const std::string & key)54 void OnPrefValueChanged(const std::string& key) override {
55 if (key == key_) {
56 QuitRunLoopIfNewValueIsPresent();
57 }
58 }
59
60 scoped_refptr<PrefStore> store_;
61 std::string key_;
62 std::optional<base::Value> old_value_;
63 base::RunLoop run_loop_;
64 };
65
66 } // namespace
67
TestingPrefStore()68 TestingPrefStore::TestingPrefStore()
69 : read_only_(true),
70 read_success_(true),
71 read_error_(PersistentPrefStore::PREF_READ_ERROR_NONE),
72 block_async_read_(false),
73 pending_async_read_(false),
74 init_complete_(false),
75 committed_(true) {}
76
GetValue(base::StringPiece key,const base::Value ** value) const77 bool TestingPrefStore::GetValue(base::StringPiece key,
78 const base::Value** value) const {
79 return prefs_.GetValue(key, value);
80 }
81
GetValues() const82 base::Value::Dict TestingPrefStore::GetValues() const {
83 return prefs_.AsDict();
84 }
85
GetMutableValue(const std::string & key,base::Value ** value)86 bool TestingPrefStore::GetMutableValue(const std::string& key,
87 base::Value** value) {
88 return prefs_.GetValue(key, value);
89 }
90
AddObserver(PrefStore::Observer * observer)91 void TestingPrefStore::AddObserver(PrefStore::Observer* observer) {
92 observers_.AddObserver(observer);
93 }
94
RemoveObserver(PrefStore::Observer * observer)95 void TestingPrefStore::RemoveObserver(PrefStore::Observer* observer) {
96 observers_.RemoveObserver(observer);
97 }
98
HasObservers() const99 bool TestingPrefStore::HasObservers() const {
100 return !observers_.empty();
101 }
102
IsInitializationComplete() const103 bool TestingPrefStore::IsInitializationComplete() const {
104 return init_complete_;
105 }
106
SetValue(const std::string & key,base::Value value,uint32_t flags)107 void TestingPrefStore::SetValue(const std::string& key,
108 base::Value value,
109 uint32_t flags) {
110 if (prefs_.SetValue(key, std::move(value))) {
111 committed_ = false;
112 NotifyPrefValueChanged(key);
113 }
114 }
115
SetValueSilently(const std::string & key,base::Value value,uint32_t flags)116 void TestingPrefStore::SetValueSilently(const std::string& key,
117 base::Value value,
118 uint32_t flags) {
119 CheckPrefIsSerializable(key, value);
120 if (prefs_.SetValue(key, std::move(value)))
121 committed_ = false;
122 }
123
RemoveValue(const std::string & key,uint32_t flags)124 void TestingPrefStore::RemoveValue(const std::string& key, uint32_t flags) {
125 if (prefs_.RemoveValue(key)) {
126 committed_ = false;
127 NotifyPrefValueChanged(key);
128 }
129 }
130
RemoveValuesByPrefixSilently(const std::string & prefix)131 void TestingPrefStore::RemoveValuesByPrefixSilently(const std::string& prefix) {
132 prefs_.ClearWithPrefix(prefix);
133 }
134
ReadOnly() const135 bool TestingPrefStore::ReadOnly() const {
136 return read_only_;
137 }
138
GetReadError() const139 PersistentPrefStore::PrefReadError TestingPrefStore::GetReadError() const {
140 return read_error_;
141 }
142
ReadPrefs()143 PersistentPrefStore::PrefReadError TestingPrefStore::ReadPrefs() {
144 NotifyInitializationCompleted();
145 return read_error_;
146 }
147
ReadPrefsAsync(ReadErrorDelegate * error_delegate)148 void TestingPrefStore::ReadPrefsAsync(ReadErrorDelegate* error_delegate) {
149 DCHECK(!pending_async_read_);
150 error_delegate_.reset(error_delegate);
151 if (block_async_read_)
152 pending_async_read_ = true;
153 else
154 NotifyInitializationCompleted();
155 }
156
CommitPendingWrite(base::OnceClosure reply_callback,base::OnceClosure synchronous_done_callback)157 void TestingPrefStore::CommitPendingWrite(
158 base::OnceClosure reply_callback,
159 base::OnceClosure synchronous_done_callback) {
160 committed_ = true;
161 PersistentPrefStore::CommitPendingWrite(std::move(reply_callback),
162 std::move(synchronous_done_callback));
163 }
164
SchedulePendingLossyWrites()165 void TestingPrefStore::SchedulePendingLossyWrites() {}
166
SetInitializationCompleted()167 void TestingPrefStore::SetInitializationCompleted() {
168 NotifyInitializationCompleted();
169 }
170
NotifyPrefValueChanged(const std::string & key)171 void TestingPrefStore::NotifyPrefValueChanged(const std::string& key) {
172 for (Observer& observer : observers_)
173 observer.OnPrefValueChanged(key);
174 }
175
NotifyInitializationCompleted()176 void TestingPrefStore::NotifyInitializationCompleted() {
177 DCHECK(!init_complete_);
178 init_complete_ = true;
179 if (read_success_ && read_error_ != PREF_READ_ERROR_NONE && error_delegate_)
180 error_delegate_->OnError(read_error_);
181 for (Observer& observer : observers_)
182 observer.OnInitializationCompleted(read_success_);
183 }
184
ReportValueChanged(const std::string & key,uint32_t flags)185 void TestingPrefStore::ReportValueChanged(const std::string& key,
186 uint32_t flags) {
187 const base::Value* value = nullptr;
188 if (prefs_.GetValue(key, &value))
189 CheckPrefIsSerializable(key, *value);
190
191 for (Observer& observer : observers_)
192 observer.OnPrefValueChanged(key);
193 }
194
SetString(const std::string & key,const std::string & value)195 void TestingPrefStore::SetString(const std::string& key,
196 const std::string& value) {
197 SetValue(key, base::Value(value), DEFAULT_PREF_WRITE_FLAGS);
198 }
199
SetInteger(const std::string & key,int value)200 void TestingPrefStore::SetInteger(const std::string& key, int value) {
201 SetValue(key, base::Value(value), DEFAULT_PREF_WRITE_FLAGS);
202 }
203
SetBoolean(const std::string & key,bool value)204 void TestingPrefStore::SetBoolean(const std::string& key, bool value) {
205 SetValue(key, base::Value(value), DEFAULT_PREF_WRITE_FLAGS);
206 }
207
GetString(const std::string & key,std::string * value) const208 bool TestingPrefStore::GetString(const std::string& key,
209 std::string* value) const {
210 const base::Value* stored_value;
211 if (!prefs_.GetValue(key, &stored_value) || !stored_value)
212 return false;
213
214 if (value && stored_value->is_string()) {
215 *value = stored_value->GetString();
216 return true;
217 }
218 return stored_value->is_string();
219 }
220
GetInteger(const std::string & key,int * value) const221 bool TestingPrefStore::GetInteger(const std::string& key, int* value) const {
222 const base::Value* stored_value;
223 if (!prefs_.GetValue(key, &stored_value) || !stored_value)
224 return false;
225
226 if (value && stored_value->is_int()) {
227 *value = stored_value->GetInt();
228 return true;
229 }
230 return stored_value->is_int();
231 }
232
GetBoolean(const std::string & key,bool * value) const233 bool TestingPrefStore::GetBoolean(const std::string& key, bool* value) const {
234 const base::Value* stored_value;
235 if (!prefs_.GetValue(key, &stored_value) || !stored_value)
236 return false;
237
238 if (value && stored_value->is_bool()) {
239 *value = stored_value->GetBool();
240 return true;
241 }
242 return stored_value->is_bool();
243 }
244
SetBlockAsyncRead(bool block_async_read)245 void TestingPrefStore::SetBlockAsyncRead(bool block_async_read) {
246 DCHECK(!init_complete_);
247 block_async_read_ = block_async_read;
248 if (pending_async_read_ && !block_async_read_)
249 NotifyInitializationCompleted();
250 }
251
WaitUntilValueChanges(std::string key)252 void TestingPrefStore::WaitUntilValueChanges(std::string key) {
253 ChangedValueWaiter waiter(this, std::move(key));
254 waiter.Wait();
255 }
256
WaitForValue(std::string key,base::Value expected_value)257 void TestingPrefStore::WaitForValue(std::string key,
258 base::Value expected_value) {
259 while (true) {
260 const base::Value* curr_value = nullptr;
261 if (GetValue(key, &curr_value) && *curr_value == expected_value) {
262 break;
263 }
264
265 WaitUntilValueChanges(key);
266 }
267 }
268
OnStoreDeletionFromDisk()269 void TestingPrefStore::OnStoreDeletionFromDisk() {}
270
set_read_only(bool read_only)271 void TestingPrefStore::set_read_only(bool read_only) {
272 read_only_ = read_only;
273 }
274
set_read_success(bool read_success)275 void TestingPrefStore::set_read_success(bool read_success) {
276 DCHECK(!init_complete_);
277 read_success_ = read_success;
278 }
279
set_read_error(PersistentPrefStore::PrefReadError read_error)280 void TestingPrefStore::set_read_error(
281 PersistentPrefStore::PrefReadError read_error) {
282 DCHECK(!init_complete_);
283 read_error_ = read_error;
284 }
285
~TestingPrefStore()286 TestingPrefStore::~TestingPrefStore() {
287 for (auto& pref : prefs_)
288 CheckPrefIsSerializable(pref.first, pref.second);
289 }
290
CheckPrefIsSerializable(const std::string & key,const base::Value & value)291 void TestingPrefStore::CheckPrefIsSerializable(const std::string& key,
292 const base::Value& value) {
293 std::string json;
294 EXPECT_TRUE(base::JSONWriter::Write(value, &json))
295 << "Pref \"" << key << "\" is not serializable as JSON.";
296 }
297