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