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/memory/scoped_ptr.h"
11#include "base/observer_list.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,
27                              const Value** result) const;
28  virtual void AddObserver(PrefStore::Observer* observer);
29  virtual void RemoveObserver(PrefStore::Observer* observer);
30  virtual bool IsInitializationComplete() const;
31
32  // PersistentPrefStore overrides:
33  virtual ReadResult GetMutableValue(const std::string& key, Value** result);
34  virtual void ReportValueChanged(const std::string& key);
35  virtual void SetValue(const std::string& key, Value* value);
36  virtual void SetValueSilently(const std::string& key, Value* value);
37  virtual void RemoveValue(const std::string& key);
38  virtual bool ReadOnly() const;
39  virtual PersistentPrefStore::PrefReadError ReadPrefs();
40  virtual bool WritePrefs();
41  virtual void ScheduleWritePrefs() {}
42  virtual void CommitPendingWrite() {}
43
44  // Marks the store as having completed initialization.
45  void SetInitializationCompleted();
46
47  // Used for tests to trigger notifications explicitly.
48  void NotifyPrefValueChanged(const std::string& key);
49  void NotifyInitializationCompleted();
50
51  // Some convenience getters/setters.
52  void SetString(const std::string& key, const std::string& value);
53  void SetInteger(const std::string& key, int value);
54  void SetBoolean(const std::string& key, bool value);
55
56  bool GetString(const std::string& key, std::string* value) const;
57  bool GetInteger(const std::string& key, int* value) const;
58  bool GetBoolean(const std::string& key, bool* value) const;
59
60  // Getter and Setter methods for setting and getting the state of the
61  // |TestingPrefStore|.
62  virtual void set_read_only(bool read_only);
63  virtual void set_prefs_written(bool status);
64  virtual bool get_prefs_written();
65
66 private:
67  // Stores the preference values.
68  PrefValueMap prefs_;
69
70  // Flag that indicates if the PrefStore is read-only
71  bool read_only_;
72
73  // Flag that indicates if the method WritePrefs was called.
74  bool prefs_written_;
75
76  // Whether initialization has been completed.
77  bool init_complete_;
78
79  ObserverList<PrefStore::Observer, true> observers_;
80
81  DISALLOW_COPY_AND_ASSIGN(TestingPrefStore);
82};
83
84#endif  // CHROME_BROWSER_PREFS_TESTING_PREF_STORE_H_
85