testing_pref_store.cc revision 21d179b334e59e9a3bfcaed4c4430bef1bc5759d
1// Copyright (c) 2010 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
14PrefStore::ReadResult TestingPrefStore::GetValue(const std::string& key,
15                                                 Value** value) const {
16  return prefs_.GetValue(key, value) ? READ_OK : READ_NO_VALUE;
17}
18
19void TestingPrefStore::AddObserver(PrefStore::Observer* observer) {
20  observers_.AddObserver(observer);
21}
22
23void TestingPrefStore::RemoveObserver(PrefStore::Observer* observer) {
24  observers_.RemoveObserver(observer);
25}
26
27void TestingPrefStore::SetValue(const std::string& key, Value* value) {
28  if (prefs_.SetValue(key, value))
29    NotifyPrefValueChanged(key);
30}
31
32void TestingPrefStore::SetValueSilently(const std::string& key, Value* value) {
33  prefs_.SetValue(key, value);
34}
35
36void TestingPrefStore::RemoveValue(const std::string& key) {
37  if (prefs_.RemoveValue(key))
38    NotifyPrefValueChanged(key);
39}
40
41PersistentPrefStore::PrefReadError TestingPrefStore::ReadPrefs() {
42  prefs_.Clear();
43  return PersistentPrefStore::PREF_READ_ERROR_NONE;
44}
45
46bool TestingPrefStore::WritePrefs() {
47  prefs_written_ = true;
48  return prefs_written_;
49}
50
51void TestingPrefStore::SetInitializationCompleted() {
52  init_complete_ = true;
53  NotifyInitializationCompleted();
54}
55
56void TestingPrefStore::NotifyPrefValueChanged(const std::string& key) {
57  FOR_EACH_OBSERVER(Observer, observers_, OnPrefValueChanged(key));
58}
59
60void TestingPrefStore::NotifyInitializationCompleted() {
61  FOR_EACH_OBSERVER(Observer, observers_, OnInitializationCompleted());
62}
63
64void TestingPrefStore::SetString(const std::string& key,
65                                 const std::string& value) {
66  SetValue(key, Value::CreateStringValue(value));
67}
68
69void TestingPrefStore::SetInteger(const std::string& key, int value) {
70  SetValue(key, Value::CreateIntegerValue(value));
71}
72
73void TestingPrefStore::SetBoolean(const std::string& key, bool value) {
74  SetValue(key, Value::CreateBooleanValue(value));
75}
76
77bool TestingPrefStore::GetString(const std::string& key,
78                                 std::string* value) const {
79  Value* stored_value;
80  if (!prefs_.GetValue(key, &stored_value) || !stored_value)
81    return false;
82
83  return stored_value->GetAsString(value);
84}
85
86bool TestingPrefStore::GetInteger(const std::string& key, int* value) const {
87  Value* stored_value;
88  if (!prefs_.GetValue(key, &stored_value) || !stored_value)
89    return false;
90
91  return stored_value->GetAsInteger(value);
92}
93
94bool TestingPrefStore::GetBoolean(const std::string& key, bool* value) const {
95  Value* stored_value;
96  if (!prefs_.GetValue(key, &stored_value) || !stored_value)
97    return false;
98
99  return stored_value->GetAsBoolean(value);
100}
101