OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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_TEST_BASE_TESTING_PREF_SERVICE_H_ | |
6 #define CHROME_TEST_BASE_TESTING_PREF_SERVICE_H_ | |
7 | |
8 #include "base/memory/ref_counted.h" | |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "base/prefs/pref_registry.h" | |
11 #include "base/prefs/pref_service.h" | |
12 #include "base/prefs/testing_pref_store.h" | |
13 | |
14 class PrefNotifierImpl; | |
15 class PrefRegistrySimple; | |
16 class TestingPrefStore; | |
17 | |
18 // A PrefService subclass for testing. It operates totally in memory and | |
19 // provides additional API for manipulating preferences at the different levels | |
20 // (managed, extension, user) conveniently. | |
21 // | |
22 // Use this via its specializations, e.g. TestingPrefServiceSimple. | |
23 template <class SuperPrefService, class ConstructionPrefRegistry> | |
24 class TestingPrefServiceBase : public SuperPrefService { | |
25 public: | |
26 virtual ~TestingPrefServiceBase(); | |
27 | |
28 // Read the value of a preference from the managed layer. Returns NULL if the | |
29 // preference is not defined at the managed layer. | |
30 const Value* GetManagedPref(const char* path) const; | |
31 | |
32 // Set a preference on the managed layer and fire observers if the preference | |
33 // changed. Assumes ownership of |value|. | |
34 void SetManagedPref(const char* path, Value* value); | |
35 | |
36 // Clear the preference on the managed layer and fire observers if the | |
37 // preference has been defined previously. | |
38 void RemoveManagedPref(const char* path); | |
39 | |
40 // Similar to the above, but for user preferences. | |
41 const Value* GetUserPref(const char* path) const; | |
42 void SetUserPref(const char* path, Value* value); | |
43 void RemoveUserPref(const char* path); | |
44 | |
45 // Similar to the above, but for recommended policy preferences. | |
46 const Value* GetRecommendedPref(const char* path) const; | |
47 void SetRecommendedPref(const char* path, Value* value); | |
48 void RemoveRecommendedPref(const char* path); | |
49 | |
50 // Do-nothing implementation for TestingPrefService. | |
51 static void HandleReadError(PersistentPrefStore::PrefReadError error) {} | |
52 | |
53 protected: | |
54 TestingPrefServiceBase( | |
55 TestingPrefStore* managed_prefs, | |
56 TestingPrefStore* user_prefs, | |
57 TestingPrefStore* recommended_prefs, | |
58 ConstructionPrefRegistry* pref_registry, | |
59 PrefNotifierImpl* pref_notifier); | |
60 | |
61 private: | |
62 // Reads the value of the preference indicated by |path| from |pref_store|. | |
63 // Returns NULL if the preference was not found. | |
64 const Value* GetPref(TestingPrefStore* pref_store, const char* path) const; | |
65 | |
66 // Sets the value for |path| in |pref_store|. | |
67 void SetPref(TestingPrefStore* pref_store, const char* path, Value* value); | |
68 | |
69 // Removes the preference identified by |path| from |pref_store|. | |
70 void RemovePref(TestingPrefStore* pref_store, const char* path); | |
71 | |
72 // Pointers to the pref stores our value store uses. | |
73 scoped_refptr<TestingPrefStore> managed_prefs_; | |
74 scoped_refptr<TestingPrefStore> user_prefs_; | |
75 scoped_refptr<TestingPrefStore> recommended_prefs_; | |
76 | |
77 DISALLOW_COPY_AND_ASSIGN(TestingPrefServiceBase); | |
78 }; | |
79 | |
80 // Test version of PrefService. | |
81 class TestingPrefServiceSimple | |
82 : public TestingPrefServiceBase<PrefService, PrefRegistry> { | |
83 public: | |
84 TestingPrefServiceSimple(); | |
85 virtual ~TestingPrefServiceSimple(); | |
86 | |
87 // This is provided as a convenience for registering preferences on | |
88 // an existing TestingPrefServiceSimple instance. On a production | |
89 // PrefService you would do all registrations before constructing | |
90 // it, passing it a PrefRegistry via its constructor (or via | |
91 // e.g. PrefServiceBuilder). | |
92 PrefRegistrySimple* registry(); | |
93 | |
94 private: | |
95 DISALLOW_COPY_AND_ASSIGN(TestingPrefServiceSimple); | |
96 }; | |
97 | |
98 template<> | |
99 TestingPrefServiceBase<PrefService, PrefRegistry>::TestingPrefServiceBase( | |
100 TestingPrefStore* managed_prefs, | |
101 TestingPrefStore* user_prefs, | |
102 TestingPrefStore* recommended_prefs, | |
103 PrefRegistry* pref_registry, | |
104 PrefNotifierImpl* pref_notifier); | |
105 | |
106 template<class SuperPrefService, class ConstructionPrefRegistry> | |
107 TestingPrefServiceBase< | |
108 SuperPrefService, ConstructionPrefRegistry>::~TestingPrefServiceBase() { | |
109 } | |
110 | |
111 template<class SuperPrefService, class ConstructionPrefRegistry> | |
112 const Value* TestingPrefServiceBase< | |
113 SuperPrefService, ConstructionPrefRegistry>::GetManagedPref( | |
114 const char* path) const { | |
115 return GetPref(managed_prefs_, path); | |
116 } | |
117 | |
118 template<class SuperPrefService, class ConstructionPrefRegistry> | |
119 void TestingPrefServiceBase< | |
120 SuperPrefService, ConstructionPrefRegistry>::SetManagedPref( | |
121 const char* path, Value* value) { | |
122 SetPref(managed_prefs_, path, value); | |
123 } | |
124 | |
125 template<class SuperPrefService, class ConstructionPrefRegistry> | |
126 void TestingPrefServiceBase< | |
127 SuperPrefService, ConstructionPrefRegistry>::RemoveManagedPref( | |
128 const char* path) { | |
129 RemovePref(managed_prefs_, path); | |
130 } | |
131 | |
132 template<class SuperPrefService, class ConstructionPrefRegistry> | |
133 const Value* TestingPrefServiceBase< | |
134 SuperPrefService, ConstructionPrefRegistry>::GetUserPref( | |
135 const char* path) const { | |
136 return GetPref(user_prefs_, path); | |
137 } | |
138 | |
139 template<class SuperPrefService, class ConstructionPrefRegistry> | |
140 void TestingPrefServiceBase< | |
141 SuperPrefService, ConstructionPrefRegistry>::SetUserPref( | |
142 const char* path, Value* value) { | |
143 SetPref(user_prefs_, path, value); | |
144 } | |
145 | |
146 template<class SuperPrefService, class ConstructionPrefRegistry> | |
147 void TestingPrefServiceBase< | |
148 SuperPrefService, ConstructionPrefRegistry>::RemoveUserPref( | |
149 const char* path) { | |
150 RemovePref(user_prefs_, path); | |
151 } | |
152 | |
153 template<class SuperPrefService, class ConstructionPrefRegistry> | |
154 const Value* TestingPrefServiceBase< | |
155 SuperPrefService, ConstructionPrefRegistry>::GetRecommendedPref( | |
156 const char* path) const { | |
157 return GetPref(recommended_prefs_, path); | |
158 } | |
159 | |
160 template<class SuperPrefService, class ConstructionPrefRegistry> | |
161 void TestingPrefServiceBase< | |
162 SuperPrefService, ConstructionPrefRegistry>::SetRecommendedPref( | |
163 const char* path, Value* value) { | |
164 SetPref(recommended_prefs_, path, value); | |
165 } | |
166 | |
167 template<class SuperPrefService, class ConstructionPrefRegistry> | |
168 void TestingPrefServiceBase< | |
169 SuperPrefService, ConstructionPrefRegistry>::RemoveRecommendedPref( | |
170 const char* path) { | |
171 RemovePref(recommended_prefs_, path); | |
172 } | |
173 | |
174 template<class SuperPrefService, class ConstructionPrefRegistry> | |
175 const Value* TestingPrefServiceBase< | |
176 SuperPrefService, ConstructionPrefRegistry>::GetPref( | |
177 TestingPrefStore* pref_store, const char* path) const { | |
178 const Value* res; | |
179 return pref_store->GetValue(path, &res) ? res : NULL; | |
180 } | |
181 | |
182 template<class SuperPrefService, class ConstructionPrefRegistry> | |
183 void TestingPrefServiceBase< | |
184 SuperPrefService, ConstructionPrefRegistry>::SetPref( | |
185 TestingPrefStore* pref_store, const char* path, Value* value) { | |
186 pref_store->SetValue(path, value); | |
187 } | |
188 | |
189 template<class SuperPrefService, class ConstructionPrefRegistry> | |
190 void TestingPrefServiceBase< | |
191 SuperPrefService, ConstructionPrefRegistry>::RemovePref( | |
192 TestingPrefStore* pref_store, const char* path) { | |
193 pref_store->RemoveValue(path); | |
194 } | |
195 | |
196 #endif // CHROME_TEST_BASE_TESTING_PREF_SERVICE_H_ | |
OLD | NEW |