Upload upstream chromium 114.0.5735.31
[platform/framework/web/chromium-efl.git] / components / prefs / testing_pref_store.cc
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/strings/string_piece.h"
13 #include "base/values.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 TestingPrefStore::TestingPrefStore()
17     : read_only_(true),
18       read_success_(true),
19       read_error_(PersistentPrefStore::PREF_READ_ERROR_NONE),
20       block_async_read_(false),
21       pending_async_read_(false),
22       init_complete_(false),
23       committed_(true) {}
24
25 bool TestingPrefStore::GetValue(base::StringPiece key,
26                                 const base::Value** value) const {
27   return prefs_.GetValue(key, value);
28 }
29
30 base::Value::Dict TestingPrefStore::GetValues() const {
31   return prefs_.AsDict();
32 }
33
34 bool TestingPrefStore::GetMutableValue(const std::string& key,
35                                        base::Value** value) {
36   return prefs_.GetValue(key, value);
37 }
38
39 void TestingPrefStore::AddObserver(PrefStore::Observer* observer) {
40   observers_.AddObserver(observer);
41 }
42
43 void TestingPrefStore::RemoveObserver(PrefStore::Observer* observer) {
44   observers_.RemoveObserver(observer);
45 }
46
47 bool TestingPrefStore::HasObservers() const {
48   return !observers_.empty();
49 }
50
51 bool TestingPrefStore::IsInitializationComplete() const {
52   return init_complete_;
53 }
54
55 void TestingPrefStore::SetValue(const std::string& key,
56                                 base::Value value,
57                                 uint32_t flags) {
58   if (prefs_.SetValue(key, std::move(value))) {
59     committed_ = false;
60     NotifyPrefValueChanged(key);
61   }
62 }
63
64 void TestingPrefStore::SetValueSilently(const std::string& key,
65                                         base::Value value,
66                                         uint32_t flags) {
67   CheckPrefIsSerializable(key, value);
68   if (prefs_.SetValue(key, std::move(value)))
69     committed_ = false;
70 }
71
72 void TestingPrefStore::RemoveValue(const std::string& key, uint32_t flags) {
73   if (prefs_.RemoveValue(key)) {
74     committed_ = false;
75     NotifyPrefValueChanged(key);
76   }
77 }
78
79 void TestingPrefStore::RemoveValuesByPrefixSilently(const std::string& prefix) {
80   prefs_.ClearWithPrefix(prefix);
81 }
82
83 bool TestingPrefStore::ReadOnly() const {
84   return read_only_;
85 }
86
87 PersistentPrefStore::PrefReadError TestingPrefStore::GetReadError() const {
88   return read_error_;
89 }
90
91 PersistentPrefStore::PrefReadError TestingPrefStore::ReadPrefs() {
92   NotifyInitializationCompleted();
93   return read_error_;
94 }
95
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;
101   else
102     NotifyInitializationCompleted();
103 }
104
105 void TestingPrefStore::CommitPendingWrite(
106     base::OnceClosure reply_callback,
107     base::OnceClosure synchronous_done_callback) {
108   committed_ = true;
109   PersistentPrefStore::CommitPendingWrite(std::move(reply_callback),
110                                           std::move(synchronous_done_callback));
111 }
112
113 void TestingPrefStore::SchedulePendingLossyWrites() {}
114
115 void TestingPrefStore::SetInitializationCompleted() {
116   NotifyInitializationCompleted();
117 }
118
119 void TestingPrefStore::NotifyPrefValueChanged(const std::string& key) {
120   for (Observer& observer : observers_)
121     observer.OnPrefValueChanged(key);
122 }
123
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_);
131 }
132
133 void TestingPrefStore::ReportValueChanged(const std::string& key,
134                                           uint32_t flags) {
135   const base::Value* value = nullptr;
136   if (prefs_.GetValue(key, &value))
137     CheckPrefIsSerializable(key, *value);
138
139   for (Observer& observer : observers_)
140     observer.OnPrefValueChanged(key);
141 }
142
143 void TestingPrefStore::SetString(const std::string& key,
144                                  const std::string& value) {
145   SetValue(key, base::Value(value), DEFAULT_PREF_WRITE_FLAGS);
146 }
147
148 void TestingPrefStore::SetInteger(const std::string& key, int value) {
149   SetValue(key, base::Value(value), DEFAULT_PREF_WRITE_FLAGS);
150 }
151
152 void TestingPrefStore::SetBoolean(const std::string& key, bool value) {
153   SetValue(key, base::Value(value), DEFAULT_PREF_WRITE_FLAGS);
154 }
155
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)
160     return false;
161
162   if (value && stored_value->is_string()) {
163     *value = stored_value->GetString();
164     return true;
165   }
166   return stored_value->is_string();
167 }
168
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)
172     return false;
173
174   if (value && stored_value->is_int()) {
175     *value = stored_value->GetInt();
176     return true;
177   }
178   return stored_value->is_int();
179 }
180
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)
184     return false;
185
186   if (value && stored_value->is_bool()) {
187     *value = stored_value->GetBool();
188     return true;
189   }
190   return stored_value->is_bool();
191 }
192
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();
198 }
199
200 void TestingPrefStore::OnStoreDeletionFromDisk() {}
201
202 void TestingPrefStore::set_read_only(bool read_only) {
203   read_only_ = read_only;
204 }
205
206 void TestingPrefStore::set_read_success(bool read_success) {
207   DCHECK(!init_complete_);
208   read_success_ = read_success;
209 }
210
211 void TestingPrefStore::set_read_error(
212     PersistentPrefStore::PrefReadError read_error) {
213   DCHECK(!init_complete_);
214   read_error_ = read_error;
215 }
216
217 TestingPrefStore::~TestingPrefStore() {
218   for (auto& pref : prefs_)
219     CheckPrefIsSerializable(pref.first, pref.second);
220 }
221
222 void TestingPrefStore::CheckPrefIsSerializable(const std::string& key,
223                                                const base::Value& value) {
224   std::string json;
225   EXPECT_TRUE(base::JSONWriter::Write(value, &json))
226       << "Pref \"" << key << "\" is not serializable as JSON.";
227 }