1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_TEST_BASE_TESTING_PREF_SERVICE_H_
#define CHROME_TEST_BASE_TESTING_PREF_SERVICE_H_
#include "base/memory/ref_counted.h"
#include "base/prefs/testing_pref_store.h"
#include "chrome/browser/prefs/pref_service.h"
class DefaultPrefStore;
class PrefModelAssociator;
class PrefNotifierImpl;
class TestingBrowserProcess;
class TestingPrefStore;
// A PrefService subclass for testing. It operates totally in memory and
// provides additional API for manipulating preferences at the different levels
// (managed, extension, user) conveniently.
//
// Use this via its specializations, TestingPrefServiceSimple and
// TestingPrefServiceSyncable.
template <class SuperPrefService>
class TestingPrefServiceBase : public SuperPrefService {
public:
virtual ~TestingPrefServiceBase();
// Read the value of a preference from the managed layer. Returns NULL if the
// preference is not defined at the managed layer.
const Value* GetManagedPref(const char* path) const;
// Set a preference on the managed layer and fire observers if the preference
// changed. Assumes ownership of |value|.
void SetManagedPref(const char* path, Value* value);
// Clear the preference on the managed layer and fire observers if the
// preference has been defined previously.
void RemoveManagedPref(const char* path);
// Similar to the above, but for user preferences.
const Value* GetUserPref(const char* path) const;
void SetUserPref(const char* path, Value* value);
void RemoveUserPref(const char* path);
// Similar to the above, but for recommended policy preferences.
const Value* GetRecommendedPref(const char* path) const;
void SetRecommendedPref(const char* path, Value* value);
void RemoveRecommendedPref(const char* path);
protected:
TestingPrefServiceBase(
TestingPrefStore* managed_prefs,
TestingPrefStore* user_prefs,
TestingPrefStore* recommended_prefs,
DefaultPrefStore* default_store,
PrefNotifierImpl* pref_notifier);
private:
// Reads the value of the preference indicated by |path| from |pref_store|.
// Returns NULL if the preference was not found.
const Value* GetPref(TestingPrefStore* pref_store, const char* path) const;
// Sets the value for |path| in |pref_store|.
void SetPref(TestingPrefStore* pref_store, const char* path, Value* value);
// Removes the preference identified by |path| from |pref_store|.
void RemovePref(TestingPrefStore* pref_store, const char* path);
// Pointers to the pref stores our value store uses.
scoped_refptr<TestingPrefStore> managed_prefs_;
scoped_refptr<TestingPrefStore> user_prefs_;
scoped_refptr<TestingPrefStore> recommended_prefs_;
DISALLOW_COPY_AND_ASSIGN(TestingPrefServiceBase);
};
// Test version of PrefServiceSimple.
class TestingPrefServiceSimple
: public TestingPrefServiceBase<PrefServiceSimple> {
public:
TestingPrefServiceSimple();
virtual ~TestingPrefServiceSimple();
private:
DISALLOW_COPY_AND_ASSIGN(TestingPrefServiceSimple);
};
// Test version of PrefServiceSyncable.
class TestingPrefServiceSyncable
: public TestingPrefServiceBase<PrefServiceSyncable> {
public:
TestingPrefServiceSyncable();
virtual ~TestingPrefServiceSyncable();
private:
DISALLOW_COPY_AND_ASSIGN(TestingPrefServiceSyncable);
};
// Helper class to temporarily set up a |local_state| in the global
// TestingBrowserProcess (for most unit tests it's NULL).
class ScopedTestingLocalState {
public:
explicit ScopedTestingLocalState(TestingBrowserProcess* browser_process);
~ScopedTestingLocalState();
TestingPrefServiceSimple* Get() {
return &local_state_;
}
private:
TestingBrowserProcess* browser_process_;
TestingPrefServiceSimple local_state_;
DISALLOW_COPY_AND_ASSIGN(ScopedTestingLocalState);
};
template<>
TestingPrefServiceBase<PrefServiceSimple>::TestingPrefServiceBase(
TestingPrefStore* managed_prefs,
TestingPrefStore* user_prefs,
TestingPrefStore* recommended_prefs,
DefaultPrefStore* default_store,
PrefNotifierImpl* pref_notifier);
template<>
TestingPrefServiceBase<PrefServiceSyncable>::TestingPrefServiceBase(
TestingPrefStore* managed_prefs,
TestingPrefStore* user_prefs,
TestingPrefStore* recommended_prefs,
DefaultPrefStore* default_store,
PrefNotifierImpl* pref_notifier);
template<class SuperPrefService>
TestingPrefServiceBase<SuperPrefService>::~TestingPrefServiceBase() {
}
template<class SuperPrefService>
const Value* TestingPrefServiceBase<SuperPrefService>::GetManagedPref(
const char* path) const {
return GetPref(managed_prefs_, path);
}
template<class SuperPrefService>
void TestingPrefServiceBase<SuperPrefService>::SetManagedPref(
const char* path, Value* value) {
SetPref(managed_prefs_, path, value);
}
template<class SuperPrefService>
void TestingPrefServiceBase<SuperPrefService>::RemoveManagedPref(
const char* path) {
RemovePref(managed_prefs_, path);
}
template<class SuperPrefService>
const Value* TestingPrefServiceBase<SuperPrefService>::GetUserPref(
const char* path) const {
return GetPref(user_prefs_, path);
}
template<class SuperPrefService>
void TestingPrefServiceBase<SuperPrefService>::SetUserPref(
const char* path, Value* value) {
SetPref(user_prefs_, path, value);
}
template<class SuperPrefService>
void TestingPrefServiceBase<SuperPrefService>::RemoveUserPref(
const char* path) {
RemovePref(user_prefs_, path);
}
template<class SuperPrefService>
const Value* TestingPrefServiceBase<SuperPrefService>::GetRecommendedPref(
const char* path) const {
return GetPref(recommended_prefs_, path);
}
template<class SuperPrefService>
void TestingPrefServiceBase<SuperPrefService>::SetRecommendedPref(
const char* path, Value* value) {
SetPref(recommended_prefs_, path, value);
}
template<class SuperPrefService>
void TestingPrefServiceBase<SuperPrefService>::RemoveRecommendedPref(
const char* path) {
RemovePref(recommended_prefs_, path);
}
template<class SuperPrefService>
const Value* TestingPrefServiceBase<SuperPrefService>::GetPref(
TestingPrefStore* pref_store, const char* path) const {
const Value* res;
return pref_store->GetValue(path, &res) ? res : NULL;
}
template<class SuperPrefService>
void TestingPrefServiceBase<SuperPrefService>::SetPref(
TestingPrefStore* pref_store, const char* path, Value* value) {
pref_store->SetValue(path, value);
}
template<class SuperPrefService>
void TestingPrefServiceBase<SuperPrefService>::RemovePref(
TestingPrefStore* pref_store, const char* path) {
pref_store->RemoveValue(path);
}
#endif // CHROME_TEST_BASE_TESTING_PREF_SERVICE_H_
|