testing_pref_store.h 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#ifndef CHROME_BROWSER_PREFS_TESTING_PREF_STORE_H_
6#define CHROME_BROWSER_PREFS_TESTING_PREF_STORE_H_
7#pragma once
8
9#include "base/basictypes.h"
10#include "base/observer_list.h"
11#include "base/scoped_ptr.h"
12#include "chrome/browser/prefs/pref_value_map.h"
13#include "chrome/common/persistent_pref_store.h"
14
15class DictionaryValue;
16
17// |TestingPrefStore| is a preference store implementation that allows tests to
18// explicitly manipulate the contents of the store, triggering notifications
19// where appropriate.
20class TestingPrefStore : public PersistentPrefStore {
21 public:
22  TestingPrefStore();
23  virtual ~TestingPrefStore() {}
24
25  // Overriden from PrefStore.
26  virtual ReadResult GetValue(const std::string& key, Value** result) const;
27  virtual void AddObserver(PrefStore::Observer* observer);
28  virtual void RemoveObserver(PrefStore::Observer* observer);
29  virtual bool IsInitializationComplete() const { return init_complete_; }
30
31  // PersistentPrefStore overrides:
32  virtual void SetValue(const std::string& key, Value* value);
33  virtual void SetValueSilently(const std::string& key, Value* value);
34  virtual void RemoveValue(const std::string& key);
35  virtual bool ReadOnly() const { return read_only_; }
36  virtual PersistentPrefStore::PrefReadError ReadPrefs();
37  virtual bool WritePrefs();
38  virtual void ScheduleWritePrefs() {}
39
40  // Marks the store as having completed initialization.
41  void SetInitializationCompleted();
42
43  // Used for tests to trigger notifications explicitly.
44  void NotifyPrefValueChanged(const std::string& key);
45  void NotifyInitializationCompleted();
46
47  // Some convenience getters/setters.
48  void SetString(const std::string& key, const std::string& value);
49  void SetInteger(const std::string& key, int value);
50  void SetBoolean(const std::string& key, bool value);
51
52  bool GetString(const std::string& key, std::string* value) const;
53  bool GetInteger(const std::string& key, int* value) const;
54  bool GetBoolean(const std::string& key, bool* value) const;
55
56  // Getter and Setter methods for setting and getting the state of the
57  // |TestingPrefStore|.
58  virtual void set_read_only(bool read_only) { read_only_ = read_only; }
59  virtual void set_prefs_written(bool status) { prefs_written_ = status; }
60  virtual bool get_prefs_written() { return prefs_written_; }
61
62 private:
63  // Stores the preference values.
64  PrefValueMap prefs_;
65
66  // Flag that indicates if the PrefStore is read-only
67  bool read_only_;
68
69  // Flag that indicates if the method WritePrefs was called.
70  bool prefs_written_;
71
72  // Whether initialization has been completed.
73  bool init_complete_;
74
75  ObserverList<PrefStore::Observer, true> observers_;
76
77  DISALLOW_COPY_AND_ASSIGN(TestingPrefStore);
78};
79
80#endif  // CHROME_BROWSER_PREFS_TESTING_PREF_STORE_H_
81