Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(650)

Side by Side Diff: chrome/test/base/testing_pref_service.h

Issue 12223091: Split testing_pref_service.h/.cc into two parts, generic vs. Chrome-specific. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address review comments. Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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_TEST_BASE_TESTING_PREF_SERVICE_H_ 5 #ifndef CHROME_TEST_BASE_TESTING_PREF_SERVICE_H_
6 #define CHROME_TEST_BASE_TESTING_PREF_SERVICE_H_ 6 #define CHROME_TEST_BASE_TESTING_PREF_SERVICE_H_
7 7
8 #include "base/memory/ref_counted.h" 8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/prefs/pref_registry.h" 10 #include "base/prefs/pref_registry.h"
11 #include "base/prefs/pref_service.h" 11 #include "base/prefs/pref_service.h"
12 #include "base/prefs/testing_pref_store.h" 12 #include "base/prefs/testing_pref_store.h"
13 #include "chrome/browser/prefs/pref_service_syncable.h"
14 13
15 class PrefModelAssociator;
16 class PrefNotifierImpl; 14 class PrefNotifierImpl;
17 class PrefRegistrySimple; 15 class PrefRegistrySimple;
18 class PrefRegistrySyncable;
19 class TestingBrowserProcess;
20 class TestingPrefStore; 16 class TestingPrefStore;
21 17
22 // A PrefService subclass for testing. It operates totally in memory and 18 // A PrefService subclass for testing. It operates totally in memory and
23 // provides additional API for manipulating preferences at the different levels 19 // provides additional API for manipulating preferences at the different levels
24 // (managed, extension, user) conveniently. 20 // (managed, extension, user) conveniently.
25 // 21 //
26 // Use this via its specializations, TestingPrefServiceSimple and 22 // Use this via its specializations, e.g. TestingPrefServiceSimple.
27 // TestingPrefServiceSyncable. 23 template <class SuperPrefService, class ConstructionPrefRegistry=PrefRegistry>
Mattias Nissler (ping if slow) 2013/02/12 17:34:47 nit: Other code seems to put spaces around = Afte
Jói 2013/02/12 19:45:34 Done.
28 template <class SuperPrefService>
29 class TestingPrefServiceBase : public SuperPrefService { 24 class TestingPrefServiceBase : public SuperPrefService {
30 public: 25 public:
31 virtual ~TestingPrefServiceBase(); 26 virtual ~TestingPrefServiceBase();
32 27
33 // Read the value of a preference from the managed layer. Returns NULL if the 28 // Read the value of a preference from the managed layer. Returns NULL if the
34 // preference is not defined at the managed layer. 29 // preference is not defined at the managed layer.
35 const Value* GetManagedPref(const char* path) const; 30 const Value* GetManagedPref(const char* path) const;
36 31
37 // Set a preference on the managed layer and fire observers if the preference 32 // Set a preference on the managed layer and fire observers if the preference
38 // changed. Assumes ownership of |value|. 33 // changed. Assumes ownership of |value|.
39 void SetManagedPref(const char* path, Value* value); 34 void SetManagedPref(const char* path, Value* value);
40 35
41 // Clear the preference on the managed layer and fire observers if the 36 // Clear the preference on the managed layer and fire observers if the
42 // preference has been defined previously. 37 // preference has been defined previously.
43 void RemoveManagedPref(const char* path); 38 void RemoveManagedPref(const char* path);
44 39
45 // Similar to the above, but for user preferences. 40 // Similar to the above, but for user preferences.
46 const Value* GetUserPref(const char* path) const; 41 const Value* GetUserPref(const char* path) const;
47 void SetUserPref(const char* path, Value* value); 42 void SetUserPref(const char* path, Value* value);
48 void RemoveUserPref(const char* path); 43 void RemoveUserPref(const char* path);
49 44
50 // Similar to the above, but for recommended policy preferences. 45 // Similar to the above, but for recommended policy preferences.
51 const Value* GetRecommendedPref(const char* path) const; 46 const Value* GetRecommendedPref(const char* path) const;
52 void SetRecommendedPref(const char* path, Value* value); 47 void SetRecommendedPref(const char* path, Value* value);
53 void RemoveRecommendedPref(const char* path); 48 void RemoveRecommendedPref(const char* path);
54 49
50 // Do-nothing implementation for TestingPrefService.
51 static void HandleReadError(PersistentPrefStore::PrefReadError error) {}
52
55 protected: 53 protected:
56 TestingPrefServiceBase( 54 TestingPrefServiceBase(
57 TestingPrefStore* managed_prefs, 55 TestingPrefStore* managed_prefs,
58 TestingPrefStore* user_prefs, 56 TestingPrefStore* user_prefs,
59 TestingPrefStore* recommended_prefs, 57 TestingPrefStore* recommended_prefs,
60 PrefRegistry* pref_registry, 58 ConstructionPrefRegistry* pref_registry,
61 PrefNotifierImpl* pref_notifier); 59 PrefNotifierImpl* pref_notifier);
62 60
63 private: 61 private:
64 // Reads the value of the preference indicated by |path| from |pref_store|. 62 // Reads the value of the preference indicated by |path| from |pref_store|.
65 // Returns NULL if the preference was not found. 63 // Returns NULL if the preference was not found.
66 const Value* GetPref(TestingPrefStore* pref_store, const char* path) const; 64 const Value* GetPref(TestingPrefStore* pref_store, const char* path) const;
67 65
68 // Sets the value for |path| in |pref_store|. 66 // Sets the value for |path| in |pref_store|.
69 void SetPref(TestingPrefStore* pref_store, const char* path, Value* value); 67 void SetPref(TestingPrefStore* pref_store, const char* path, Value* value);
70 68
(...skipping 19 matching lines...) Expand all
90 // an existing TestingPrefServiceSimple instance. On a production 88 // an existing TestingPrefServiceSimple instance. On a production
91 // PrefService you would do all registrations before constructing 89 // PrefService you would do all registrations before constructing
92 // it, passing it a PrefRegistry via its constructor (or via 90 // it, passing it a PrefRegistry via its constructor (or via
93 // e.g. PrefServiceBuilder). 91 // e.g. PrefServiceBuilder).
94 PrefRegistrySimple* registry(); 92 PrefRegistrySimple* registry();
95 93
96 private: 94 private:
97 DISALLOW_COPY_AND_ASSIGN(TestingPrefServiceSimple); 95 DISALLOW_COPY_AND_ASSIGN(TestingPrefServiceSimple);
98 }; 96 };
99 97
100 // Test version of PrefServiceSyncable.
101 class TestingPrefServiceSyncable
102 : public TestingPrefServiceBase<PrefServiceSyncable> {
103 public:
104 TestingPrefServiceSyncable();
105 virtual ~TestingPrefServiceSyncable();
106
107 // This is provided as a convenience; on a production PrefService
108 // you would do all registrations before constructing it, passing it
109 // a PrefRegistry via its constructor (or via
110 // e.g. PrefServiceBuilder).
111 PrefRegistrySyncable* registry();
112
113 private:
114 DISALLOW_COPY_AND_ASSIGN(TestingPrefServiceSyncable);
115 };
116
117 // Helper class to temporarily set up a |local_state| in the global
118 // TestingBrowserProcess (for most unit tests it's NULL).
119 class ScopedTestingLocalState {
120 public:
121 explicit ScopedTestingLocalState(TestingBrowserProcess* browser_process);
122 ~ScopedTestingLocalState();
123
124 TestingPrefServiceSimple* Get() {
125 return &local_state_;
126 }
127
128 private:
129 TestingBrowserProcess* browser_process_;
130 TestingPrefServiceSimple local_state_;
131
132 DISALLOW_COPY_AND_ASSIGN(ScopedTestingLocalState);
133 };
134
135 template<> 98 template<>
136 TestingPrefServiceBase<PrefService>::TestingPrefServiceBase( 99 TestingPrefServiceBase<PrefService>::TestingPrefServiceBase(
137 TestingPrefStore* managed_prefs, 100 TestingPrefStore* managed_prefs,
138 TestingPrefStore* user_prefs, 101 TestingPrefStore* user_prefs,
139 TestingPrefStore* recommended_prefs, 102 TestingPrefStore* recommended_prefs,
140 PrefRegistry* pref_registry, 103 PrefRegistry* pref_registry,
141 PrefNotifierImpl* pref_notifier); 104 PrefNotifierImpl* pref_notifier);
142 105
143 template<> 106 template<class SuperPrefService, class ConstructionPrefRegistry>
144 TestingPrefServiceBase<PrefServiceSyncable>::TestingPrefServiceBase( 107 TestingPrefServiceBase<
145 TestingPrefStore* managed_prefs, 108 SuperPrefService, ConstructionPrefRegistry>::~TestingPrefServiceBase() {
146 TestingPrefStore* user_prefs,
147 TestingPrefStore* recommended_prefs,
148 PrefRegistry* pref_registry,
149 PrefNotifierImpl* pref_notifier);
150
151 template<class SuperPrefService>
152 TestingPrefServiceBase<SuperPrefService>::~TestingPrefServiceBase() {
153 } 109 }
154 110
155 template<class SuperPrefService> 111 template<class SuperPrefService, class ConstructionPrefRegistry>
156 const Value* TestingPrefServiceBase<SuperPrefService>::GetManagedPref( 112 const Value* TestingPrefServiceBase<
157 const char* path) const { 113 SuperPrefService, ConstructionPrefRegistry>::GetManagedPref(
114 const char* path) const {
158 return GetPref(managed_prefs_, path); 115 return GetPref(managed_prefs_, path);
159 } 116 }
160 117
161 template<class SuperPrefService> 118 template<class SuperPrefService, class ConstructionPrefRegistry>
162 void TestingPrefServiceBase<SuperPrefService>::SetManagedPref( 119 void TestingPrefServiceBase<
163 const char* path, Value* value) { 120 SuperPrefService, ConstructionPrefRegistry>::SetManagedPref(
121 const char* path, Value* value) {
164 SetPref(managed_prefs_, path, value); 122 SetPref(managed_prefs_, path, value);
165 } 123 }
166 124
167 template<class SuperPrefService> 125 template<class SuperPrefService, class ConstructionPrefRegistry>
168 void TestingPrefServiceBase<SuperPrefService>::RemoveManagedPref( 126 void TestingPrefServiceBase<
169 const char* path) { 127 SuperPrefService, ConstructionPrefRegistry>::RemoveManagedPref(
128 const char* path) {
170 RemovePref(managed_prefs_, path); 129 RemovePref(managed_prefs_, path);
171 } 130 }
172 131
173 template<class SuperPrefService> 132 template<class SuperPrefService, class ConstructionPrefRegistry>
174 const Value* TestingPrefServiceBase<SuperPrefService>::GetUserPref( 133 const Value* TestingPrefServiceBase<
175 const char* path) const { 134 SuperPrefService, ConstructionPrefRegistry>::GetUserPref(
135 const char* path) const {
176 return GetPref(user_prefs_, path); 136 return GetPref(user_prefs_, path);
177 } 137 }
178 138
179 template<class SuperPrefService> 139 template<class SuperPrefService, class ConstructionPrefRegistry>
180 void TestingPrefServiceBase<SuperPrefService>::SetUserPref( 140 void TestingPrefServiceBase<
181 const char* path, Value* value) { 141 SuperPrefService, ConstructionPrefRegistry>::SetUserPref(
142 const char* path, Value* value) {
182 SetPref(user_prefs_, path, value); 143 SetPref(user_prefs_, path, value);
183 } 144 }
184 145
185 template<class SuperPrefService> 146 template<class SuperPrefService, class ConstructionPrefRegistry>
186 void TestingPrefServiceBase<SuperPrefService>::RemoveUserPref( 147 void TestingPrefServiceBase<
187 const char* path) { 148 SuperPrefService, ConstructionPrefRegistry>::RemoveUserPref(
149 const char* path) {
188 RemovePref(user_prefs_, path); 150 RemovePref(user_prefs_, path);
189 } 151 }
190 152
191 template<class SuperPrefService> 153 template<class SuperPrefService, class ConstructionPrefRegistry>
192 const Value* TestingPrefServiceBase<SuperPrefService>::GetRecommendedPref( 154 const Value* TestingPrefServiceBase<
193 const char* path) const { 155 SuperPrefService, ConstructionPrefRegistry>::GetRecommendedPref(
156 const char* path) const {
194 return GetPref(recommended_prefs_, path); 157 return GetPref(recommended_prefs_, path);
195 } 158 }
196 159
197 template<class SuperPrefService> 160 template<class SuperPrefService, class ConstructionPrefRegistry>
198 void TestingPrefServiceBase<SuperPrefService>::SetRecommendedPref( 161 void TestingPrefServiceBase<
199 const char* path, Value* value) { 162 SuperPrefService, ConstructionPrefRegistry>::SetRecommendedPref(
163 const char* path, Value* value) {
200 SetPref(recommended_prefs_, path, value); 164 SetPref(recommended_prefs_, path, value);
201 } 165 }
202 166
203 template<class SuperPrefService> 167 template<class SuperPrefService, class ConstructionPrefRegistry>
204 void TestingPrefServiceBase<SuperPrefService>::RemoveRecommendedPref( 168 void TestingPrefServiceBase<
205 const char* path) { 169 SuperPrefService, ConstructionPrefRegistry>::RemoveRecommendedPref(
170 const char* path) {
206 RemovePref(recommended_prefs_, path); 171 RemovePref(recommended_prefs_, path);
207 } 172 }
208 173
209 template<class SuperPrefService> 174 template<class SuperPrefService, class ConstructionPrefRegistry>
210 const Value* TestingPrefServiceBase<SuperPrefService>::GetPref( 175 const Value* TestingPrefServiceBase<
211 TestingPrefStore* pref_store, const char* path) const { 176 SuperPrefService, ConstructionPrefRegistry>::GetPref(
177 TestingPrefStore* pref_store, const char* path) const {
212 const Value* res; 178 const Value* res;
213 return pref_store->GetValue(path, &res) ? res : NULL; 179 return pref_store->GetValue(path, &res) ? res : NULL;
214 } 180 }
215 181
216 template<class SuperPrefService> 182 template<class SuperPrefService, class ConstructionPrefRegistry>
217 void TestingPrefServiceBase<SuperPrefService>::SetPref( 183 void TestingPrefServiceBase<
218 TestingPrefStore* pref_store, const char* path, Value* value) { 184 SuperPrefService, ConstructionPrefRegistry>::SetPref(
185 TestingPrefStore* pref_store, const char* path, Value* value) {
219 pref_store->SetValue(path, value); 186 pref_store->SetValue(path, value);
220 } 187 }
221 188
222 template<class SuperPrefService> 189 template<class SuperPrefService, class ConstructionPrefRegistry>
223 void TestingPrefServiceBase<SuperPrefService>::RemovePref( 190 void TestingPrefServiceBase<
224 TestingPrefStore* pref_store, const char* path) { 191 SuperPrefService, ConstructionPrefRegistry>::RemovePref(
192 TestingPrefStore* pref_store, const char* path) {
225 pref_store->RemoveValue(path); 193 pref_store->RemoveValue(path);
226 } 194 }
227 195
228 #endif // CHROME_TEST_BASE_TESTING_PREF_SERVICE_H_ 196 #endif // CHROME_TEST_BASE_TESTING_PREF_SERVICE_H_
OLDNEW
« no previous file with comments | « chrome/test/base/scoped_testing_local_state.cc ('k') | chrome/test/base/testing_pref_service.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698