1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
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 "chrome/browser/prefs/testing_pref_store.h"
6
7 #include "base/values.h"
8
TestingPrefStore()9 TestingPrefStore::TestingPrefStore()
10 : read_only_(true),
11 prefs_written_(false),
12 init_complete_(false) {}
13
~TestingPrefStore()14 TestingPrefStore::~TestingPrefStore() {}
15
GetValue(const std::string & key,const Value ** value) const16 PrefStore::ReadResult TestingPrefStore::GetValue(const std::string& key,
17 const Value** value) const {
18 return prefs_.GetValue(key, value) ? READ_OK : READ_NO_VALUE;
19 }
20
GetMutableValue(const std::string & key,Value ** value)21 PrefStore::ReadResult TestingPrefStore::GetMutableValue(const std::string& key,
22 Value** value) {
23 return prefs_.GetValue(key, value) ? READ_OK : READ_NO_VALUE;
24 }
25
AddObserver(PrefStore::Observer * observer)26 void TestingPrefStore::AddObserver(PrefStore::Observer* observer) {
27 observers_.AddObserver(observer);
28 }
29
RemoveObserver(PrefStore::Observer * observer)30 void TestingPrefStore::RemoveObserver(PrefStore::Observer* observer) {
31 observers_.RemoveObserver(observer);
32 }
33
IsInitializationComplete() const34 bool TestingPrefStore::IsInitializationComplete() const {
35 return init_complete_;
36 }
37
SetValue(const std::string & key,Value * value)38 void TestingPrefStore::SetValue(const std::string& key, Value* value) {
39 if (prefs_.SetValue(key, value))
40 NotifyPrefValueChanged(key);
41 }
42
SetValueSilently(const std::string & key,Value * value)43 void TestingPrefStore::SetValueSilently(const std::string& key, Value* value) {
44 prefs_.SetValue(key, value);
45 }
46
RemoveValue(const std::string & key)47 void TestingPrefStore::RemoveValue(const std::string& key) {
48 if (prefs_.RemoveValue(key))
49 NotifyPrefValueChanged(key);
50 }
51
ReadOnly() const52 bool TestingPrefStore::ReadOnly() const {
53 return read_only_;
54 }
55
ReadPrefs()56 PersistentPrefStore::PrefReadError TestingPrefStore::ReadPrefs() {
57 prefs_.Clear();
58 return PersistentPrefStore::PREF_READ_ERROR_NONE;
59 }
60
WritePrefs()61 bool TestingPrefStore::WritePrefs() {
62 prefs_written_ = true;
63 return prefs_written_;
64 }
65
SetInitializationCompleted()66 void TestingPrefStore::SetInitializationCompleted() {
67 init_complete_ = true;
68 NotifyInitializationCompleted();
69 }
70
NotifyPrefValueChanged(const std::string & key)71 void TestingPrefStore::NotifyPrefValueChanged(const std::string& key) {
72 FOR_EACH_OBSERVER(Observer, observers_, OnPrefValueChanged(key));
73 }
74
NotifyInitializationCompleted()75 void TestingPrefStore::NotifyInitializationCompleted() {
76 FOR_EACH_OBSERVER(Observer, observers_, OnInitializationCompleted());
77 }
78
ReportValueChanged(const std::string & key)79 void TestingPrefStore::ReportValueChanged(const std::string& key) {
80 FOR_EACH_OBSERVER(Observer, observers_, OnPrefValueChanged(key));
81 }
82
SetString(const std::string & key,const std::string & value)83 void TestingPrefStore::SetString(const std::string& key,
84 const std::string& value) {
85 SetValue(key, Value::CreateStringValue(value));
86 }
87
SetInteger(const std::string & key,int value)88 void TestingPrefStore::SetInteger(const std::string& key, int value) {
89 SetValue(key, Value::CreateIntegerValue(value));
90 }
91
SetBoolean(const std::string & key,bool value)92 void TestingPrefStore::SetBoolean(const std::string& key, bool value) {
93 SetValue(key, Value::CreateBooleanValue(value));
94 }
95
GetString(const std::string & key,std::string * value) const96 bool TestingPrefStore::GetString(const std::string& key,
97 std::string* value) const {
98 const Value* stored_value;
99 if (!prefs_.GetValue(key, &stored_value) || !stored_value)
100 return false;
101
102 return stored_value->GetAsString(value);
103 }
104
GetInteger(const std::string & key,int * value) const105 bool TestingPrefStore::GetInteger(const std::string& key, int* value) const {
106 const Value* stored_value;
107 if (!prefs_.GetValue(key, &stored_value) || !stored_value)
108 return false;
109
110 return stored_value->GetAsInteger(value);
111 }
112
GetBoolean(const std::string & key,bool * value) const113 bool TestingPrefStore::GetBoolean(const std::string& key, bool* value) const {
114 const Value* stored_value;
115 if (!prefs_.GetValue(key, &stored_value) || !stored_value)
116 return false;
117
118 return stored_value->GetAsBoolean(value);
119 }
120
set_read_only(bool read_only)121 void TestingPrefStore::set_read_only(bool read_only) {
122 read_only_ = read_only;
123 }
124
set_prefs_written(bool status)125 void TestingPrefStore::set_prefs_written(bool status) {
126 prefs_written_ = status;
127 }
128
get_prefs_written()129 bool TestingPrefStore::get_prefs_written() {
130 return prefs_written_;
131 }
132