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.
5 #include "components/prefs/testing_pref_store.h"
11 #include "base/json/json_writer.h"
12 #include "base/strings/string_piece.h"
13 #include "base/values.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 TestingPrefStore::TestingPrefStore()
19 read_error_(PersistentPrefStore::PREF_READ_ERROR_NONE),
20 block_async_read_(false),
21 pending_async_read_(false),
22 init_complete_(false),
25 bool TestingPrefStore::GetValue(base::StringPiece key,
26 const base::Value** value) const {
27 return prefs_.GetValue(key, value);
30 base::Value::Dict TestingPrefStore::GetValues() const {
31 return prefs_.AsDict();
34 bool TestingPrefStore::GetMutableValue(const std::string& key,
35 base::Value** value) {
36 return prefs_.GetValue(key, value);
39 void TestingPrefStore::AddObserver(PrefStore::Observer* observer) {
40 observers_.AddObserver(observer);
43 void TestingPrefStore::RemoveObserver(PrefStore::Observer* observer) {
44 observers_.RemoveObserver(observer);
47 bool TestingPrefStore::HasObservers() const {
48 return !observers_.empty();
51 bool TestingPrefStore::IsInitializationComplete() const {
52 return init_complete_;
55 void TestingPrefStore::SetValue(const std::string& key,
58 if (prefs_.SetValue(key, std::move(value))) {
60 NotifyPrefValueChanged(key);
64 void TestingPrefStore::SetValueSilently(const std::string& key,
67 CheckPrefIsSerializable(key, value);
68 if (prefs_.SetValue(key, std::move(value)))
72 void TestingPrefStore::RemoveValue(const std::string& key, uint32_t flags) {
73 if (prefs_.RemoveValue(key)) {
75 NotifyPrefValueChanged(key);
79 void TestingPrefStore::RemoveValuesByPrefixSilently(const std::string& prefix) {
80 prefs_.ClearWithPrefix(prefix);
83 bool TestingPrefStore::ReadOnly() const {
87 PersistentPrefStore::PrefReadError TestingPrefStore::GetReadError() const {
91 PersistentPrefStore::PrefReadError TestingPrefStore::ReadPrefs() {
92 NotifyInitializationCompleted();
96 void TestingPrefStore::ReadPrefsAsync(ReadErrorDelegate* error_delegate) {
97 DCHECK(!pending_async_read_);
98 error_delegate_.reset(error_delegate);
99 if (block_async_read_)
100 pending_async_read_ = true;
102 NotifyInitializationCompleted();
105 void TestingPrefStore::CommitPendingWrite(
106 base::OnceClosure reply_callback,
107 base::OnceClosure synchronous_done_callback) {
109 PersistentPrefStore::CommitPendingWrite(std::move(reply_callback),
110 std::move(synchronous_done_callback));
113 void TestingPrefStore::SchedulePendingLossyWrites() {}
115 void TestingPrefStore::SetInitializationCompleted() {
116 NotifyInitializationCompleted();
119 void TestingPrefStore::NotifyPrefValueChanged(const std::string& key) {
120 for (Observer& observer : observers_)
121 observer.OnPrefValueChanged(key);
124 void TestingPrefStore::NotifyInitializationCompleted() {
125 DCHECK(!init_complete_);
126 init_complete_ = true;
127 if (read_success_ && read_error_ != PREF_READ_ERROR_NONE && error_delegate_)
128 error_delegate_->OnError(read_error_);
129 for (Observer& observer : observers_)
130 observer.OnInitializationCompleted(read_success_);
133 void TestingPrefStore::ReportValueChanged(const std::string& key,
135 const base::Value* value = nullptr;
136 if (prefs_.GetValue(key, &value))
137 CheckPrefIsSerializable(key, *value);
139 for (Observer& observer : observers_)
140 observer.OnPrefValueChanged(key);
143 void TestingPrefStore::SetString(const std::string& key,
144 const std::string& value) {
145 SetValue(key, base::Value(value), DEFAULT_PREF_WRITE_FLAGS);
148 void TestingPrefStore::SetInteger(const std::string& key, int value) {
149 SetValue(key, base::Value(value), DEFAULT_PREF_WRITE_FLAGS);
152 void TestingPrefStore::SetBoolean(const std::string& key, bool value) {
153 SetValue(key, base::Value(value), DEFAULT_PREF_WRITE_FLAGS);
156 bool TestingPrefStore::GetString(const std::string& key,
157 std::string* value) const {
158 const base::Value* stored_value;
159 if (!prefs_.GetValue(key, &stored_value) || !stored_value)
162 if (value && stored_value->is_string()) {
163 *value = stored_value->GetString();
166 return stored_value->is_string();
169 bool TestingPrefStore::GetInteger(const std::string& key, int* value) const {
170 const base::Value* stored_value;
171 if (!prefs_.GetValue(key, &stored_value) || !stored_value)
174 if (value && stored_value->is_int()) {
175 *value = stored_value->GetInt();
178 return stored_value->is_int();
181 bool TestingPrefStore::GetBoolean(const std::string& key, bool* value) const {
182 const base::Value* stored_value;
183 if (!prefs_.GetValue(key, &stored_value) || !stored_value)
186 if (value && stored_value->is_bool()) {
187 *value = stored_value->GetBool();
190 return stored_value->is_bool();
193 void TestingPrefStore::SetBlockAsyncRead(bool block_async_read) {
194 DCHECK(!init_complete_);
195 block_async_read_ = block_async_read;
196 if (pending_async_read_ && !block_async_read_)
197 NotifyInitializationCompleted();
200 void TestingPrefStore::OnStoreDeletionFromDisk() {}
202 void TestingPrefStore::set_read_only(bool read_only) {
203 read_only_ = read_only;
206 void TestingPrefStore::set_read_success(bool read_success) {
207 DCHECK(!init_complete_);
208 read_success_ = read_success;
211 void TestingPrefStore::set_read_error(
212 PersistentPrefStore::PrefReadError read_error) {
213 DCHECK(!init_complete_);
214 read_error_ = read_error;
217 TestingPrefStore::~TestingPrefStore() {
218 for (auto& pref : prefs_)
219 CheckPrefIsSerializable(pref.first, pref.second);
222 void TestingPrefStore::CheckPrefIsSerializable(const std::string& key,
223 const base::Value& value) {
225 EXPECT_TRUE(base::JSONWriter::Write(value, &json))
226 << "Pref \"" << key << "\" is not serializable as JSON.";