testing_pref_store.h revision dc0f95d653279beabeb9817299e2902918ba123e
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#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;
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;
36  virtual PersistentPrefStore::PrefReadError ReadPrefs();
37  virtual bool WritePrefs();
38  virtual void ScheduleWritePrefs() {}
39  // TODO(battre) remove this function
40  virtual void ReportValueChanged(const std::string& key);
41
42  // Marks the store as having completed initialization.
43  void SetInitializationCompleted();
44
45  // Used for tests to trigger notifications explicitly.
46  void NotifyPrefValueChanged(const std::string& key);
47  void NotifyInitializationCompleted();
48
49  // Some convenience getters/setters.
50  void SetString(const std::string& key, const std::string& value);
51  void SetInteger(const std::string& key, int value);
52  void SetBoolean(const std::string& key, bool value);
53
54  bool GetString(const std::string& key, std::string* value) const;
55  bool GetInteger(const std::string& key, int* value) const;
56  bool GetBoolean(const std::string& key, bool* value) const;
57
58  // Getter and Setter methods for setting and getting the state of the
59  // |TestingPrefStore|.
60  virtual void set_read_only(bool read_only);
61  virtual void set_prefs_written(bool status);
62  virtual bool get_prefs_written();
63
64 private:
65  // Stores the preference values.
66  PrefValueMap prefs_;
67
68  // Flag that indicates if the PrefStore is read-only
69  bool read_only_;
70
71  // Flag that indicates if the method WritePrefs was called.
72  bool prefs_written_;
73
74  // Whether initialization has been completed.
75  bool init_complete_;
76
77  ObserverList<PrefStore::Observer, true> observers_;
78
79  DISALLOW_COPY_AND_ASSIGN(TestingPrefStore);
80};
81
82#endif  // CHROME_BROWSER_PREFS_TESTING_PREF_STORE_H_
83