OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_PREFS_TESTING_PREF_STORE_H_ | 5 #ifndef CHROME_BROWSER_PREFS_TESTING_PREF_STORE_H_ |
6 #define CHROME_BROWSER_PREFS_TESTING_PREF_STORE_H_ | 6 #define CHROME_BROWSER_PREFS_TESTING_PREF_STORE_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/observer_list.h" |
10 #include "base/scoped_ptr.h" | 11 #include "base/scoped_ptr.h" |
11 #include "chrome/common/pref_store_base.h" | 12 #include "chrome/browser/prefs/pref_value_map.h" |
| 13 #include "chrome/common/persistent_pref_store.h" |
12 | 14 |
13 class DictionaryValue; | 15 class DictionaryValue; |
14 | 16 |
15 // |TestingPrefStore| is a stub implementation of the |PrefStore| interface. | 17 // |TestingPrefStore| is a preference store implementation that allows tests to |
16 // It allows to get and set the state of the |PrefStore| as well as triggering | 18 // explicitly manipulate the contents of the store, triggering notifications |
17 // notifications. | 19 // where appropriate. |
18 class TestingPrefStore : public PrefStoreBase { | 20 class TestingPrefStore : public PersistentPrefStore { |
19 public: | 21 public: |
20 TestingPrefStore(); | 22 TestingPrefStore(); |
21 virtual ~TestingPrefStore() {} | 23 virtual ~TestingPrefStore() {} |
22 | 24 |
23 virtual DictionaryValue* prefs() const { return prefs_.get(); } | 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_; } |
24 | 30 |
25 virtual PrefStore::PrefReadError ReadPrefs(); | 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() {} |
26 | 39 |
27 virtual bool ReadOnly() const { return read_only_; } | 40 // Marks the store as having completed initialization. |
| 41 void SetInitializationCompleted(); |
28 | 42 |
29 virtual bool WritePrefs(); | 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; |
30 | 55 |
31 // Getter and Setter methods for setting and getting the state of the | 56 // Getter and Setter methods for setting and getting the state of the |
32 // |DummyPrefStore|. | 57 // |TestingPrefStore|. |
33 virtual void set_read_only(bool read_only) { read_only_ = read_only; } | 58 virtual void set_read_only(bool read_only) { read_only_ = read_only; } |
34 virtual void set_prefs(DictionaryValue* prefs) { prefs_.reset(prefs); } | |
35 virtual void set_prefs_written(bool status) { prefs_written_ = status; } | 59 virtual void set_prefs_written(bool status) { prefs_written_ = status; } |
36 virtual bool get_prefs_written() { return prefs_written_; } | 60 virtual bool get_prefs_written() { return prefs_written_; } |
37 | 61 |
38 // Publish these functions so testing code can call them. | |
39 virtual void NotifyPrefValueChanged(const std::string& key); | |
40 virtual void NotifyInitializationCompleted(); | |
41 | |
42 // Whether the store has completed all asynchronous initialization. | |
43 virtual bool IsInitializationComplete() { return init_complete_; } | |
44 | |
45 // Mark the store as having completed initialization. | |
46 void SetInitializationCompleted(); | |
47 | |
48 private: | 62 private: |
49 scoped_ptr<DictionaryValue> prefs_; | 63 // Stores the preference values. |
| 64 PrefValueMap prefs_; |
50 | 65 |
51 // Flag that indicates if the PrefStore is read-only | 66 // Flag that indicates if the PrefStore is read-only |
52 bool read_only_; | 67 bool read_only_; |
53 | 68 |
54 // Flag that indicates if the method WritePrefs was called. | 69 // Flag that indicates if the method WritePrefs was called. |
55 bool prefs_written_; | 70 bool prefs_written_; |
56 | 71 |
57 // Whether initialization has been completed. | 72 // Whether initialization has been completed. |
58 bool init_complete_; | 73 bool init_complete_; |
59 | 74 |
| 75 ObserverList<PrefStore::Observer, true> observers_; |
| 76 |
60 DISALLOW_COPY_AND_ASSIGN(TestingPrefStore); | 77 DISALLOW_COPY_AND_ASSIGN(TestingPrefStore); |
61 }; | 78 }; |
62 | 79 |
63 #endif // CHROME_BROWSER_PREFS_TESTING_PREF_STORE_H_ | 80 #endif // CHROME_BROWSER_PREFS_TESTING_PREF_STORE_H_ |
OLD | NEW |