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

Side by Side Diff: chrome/browser/sync/test/integration/preferences_helper.cc

Issue 315823002: sync: Refactor TwoClientPreferencesSyncTest (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Formatting fix + explicit constructors Created 6 years, 6 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) 2011 The Chromium Authors. All rights reserved. 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 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 #include "chrome/browser/sync/test/integration/preferences_helper.h" 5 #include "chrome/browser/sync/test/integration/preferences_helper.h"
6 6
7 #include "base/prefs/pref_change_registrar.h"
7 #include "base/prefs/pref_service.h" 8 #include "base/prefs/pref_service.h"
8 #include "base/prefs/scoped_user_pref_update.h" 9 #include "base/prefs/scoped_user_pref_update.h"
9 #include "base/values.h" 10 #include "base/strings/stringprintf.h"
10 #include "chrome/browser/profiles/profile.h" 11 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/sync/test/integration/multi_client_status_change_checke r.h" 12 #include "chrome/browser/sync/test/integration/multi_client_status_change_checke r.h"
12 #include "chrome/browser/sync/test/integration/profile_sync_service_harness.h" 13 #include "chrome/browser/sync/test/integration/profile_sync_service_harness.h"
13 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h" 14 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h"
14 #include "chrome/browser/sync/test/integration/sync_test.h" 15 #include "chrome/browser/sync/test/integration/sync_test.h"
15 16
16 using sync_datatype_helper::test; 17 using sync_datatype_helper::test;
17 18
18 namespace preferences_helper { 19 namespace preferences_helper {
19 20
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 101
101 bool BooleanPrefMatches(const char* pref_name) { 102 bool BooleanPrefMatches(const char* pref_name) {
102 bool reference_value; 103 bool reference_value;
103 if (test()->use_verifier()) { 104 if (test()->use_verifier()) {
104 reference_value = GetVerifierPrefs()->GetBoolean(pref_name); 105 reference_value = GetVerifierPrefs()->GetBoolean(pref_name);
105 } else { 106 } else {
106 reference_value = GetPrefs(0)->GetBoolean(pref_name); 107 reference_value = GetPrefs(0)->GetBoolean(pref_name);
107 } 108 }
108 for (int i = 0; i < test()->num_clients(); ++i) { 109 for (int i = 0; i < test()->num_clients(); ++i) {
109 if (reference_value != GetPrefs(i)->GetBoolean(pref_name)) { 110 if (reference_value != GetPrefs(i)->GetBoolean(pref_name)) {
110 LOG(ERROR) << "Boolean preference " << pref_name << " mismatched in" 111 DVLOG(1) << "Boolean preference " << pref_name << " mismatched in"
111 << " profile " << i << "."; 112 << " profile " << i << ".";
112 return false; 113 return false;
113 } 114 }
114 } 115 }
115 return true; 116 return true;
116 } 117 }
117 118
118 bool IntegerPrefMatches(const char* pref_name) { 119 bool IntegerPrefMatches(const char* pref_name) {
119 int reference_value; 120 int reference_value;
120 if (test()->use_verifier()) { 121 if (test()->use_verifier()) {
121 reference_value = GetVerifierPrefs()->GetInteger(pref_name); 122 reference_value = GetVerifierPrefs()->GetInteger(pref_name);
122 } else { 123 } else {
123 reference_value = GetPrefs(0)->GetInteger(pref_name); 124 reference_value = GetPrefs(0)->GetInteger(pref_name);
124 } 125 }
125 for (int i = 0; i < test()->num_clients(); ++i) { 126 for (int i = 0; i < test()->num_clients(); ++i) {
126 if (reference_value != GetPrefs(i)->GetInteger(pref_name)) { 127 if (reference_value != GetPrefs(i)->GetInteger(pref_name)) {
127 LOG(ERROR) << "Integer preference " << pref_name << " mismatched in" 128 DVLOG(1) << "Integer preference " << pref_name << " mismatched in"
128 << " profile " << i << "."; 129 << " profile " << i << ".";
129 return false; 130 return false;
130 } 131 }
131 } 132 }
132 return true; 133 return true;
133 } 134 }
134 135
135 bool Int64PrefMatches(const char* pref_name) { 136 bool Int64PrefMatches(const char* pref_name) {
136 int64 reference_value; 137 int64 reference_value;
137 if (test()->use_verifier()) { 138 if (test()->use_verifier()) {
138 reference_value = GetVerifierPrefs()->GetInt64(pref_name); 139 reference_value = GetVerifierPrefs()->GetInt64(pref_name);
139 } else { 140 } else {
140 reference_value = GetPrefs(0)->GetInt64(pref_name); 141 reference_value = GetPrefs(0)->GetInt64(pref_name);
141 } 142 }
142 for (int i = 0; i < test()->num_clients(); ++i) { 143 for (int i = 0; i < test()->num_clients(); ++i) {
143 if (reference_value != GetPrefs(i)->GetInt64(pref_name)) { 144 if (reference_value != GetPrefs(i)->GetInt64(pref_name)) {
144 LOG(ERROR) << "Integer preference " << pref_name << " mismatched in" 145 DVLOG(1) << "Integer preference " << pref_name << " mismatched in"
145 << " profile " << i << "."; 146 << " profile " << i << ".";
146 return false; 147 return false;
147 } 148 }
148 } 149 }
149 return true; 150 return true;
150 } 151 }
151 152
152 bool DoublePrefMatches(const char* pref_name) { 153 bool DoublePrefMatches(const char* pref_name) {
153 double reference_value; 154 double reference_value;
154 if (test()->use_verifier()) { 155 if (test()->use_verifier()) {
155 reference_value = GetVerifierPrefs()->GetDouble(pref_name); 156 reference_value = GetVerifierPrefs()->GetDouble(pref_name);
156 } else { 157 } else {
157 reference_value = GetPrefs(0)->GetDouble(pref_name); 158 reference_value = GetPrefs(0)->GetDouble(pref_name);
158 } 159 }
159 for (int i = 0; i < test()->num_clients(); ++i) { 160 for (int i = 0; i < test()->num_clients(); ++i) {
160 if (reference_value != GetPrefs(i)->GetDouble(pref_name)) { 161 if (reference_value != GetPrefs(i)->GetDouble(pref_name)) {
161 LOG(ERROR) << "Double preference " << pref_name << " mismatched in" 162 DVLOG(1) << "Double preference " << pref_name << " mismatched in"
162 << " profile " << i << "."; 163 << " profile " << i << ".";
163 return false; 164 return false;
164 } 165 }
165 } 166 }
166 return true; 167 return true;
167 } 168 }
168 169
169 bool StringPrefMatches(const char* pref_name) { 170 bool StringPrefMatches(const char* pref_name) {
170 std::string reference_value; 171 std::string reference_value;
171 if (test()->use_verifier()) { 172 if (test()->use_verifier()) {
172 reference_value = GetVerifierPrefs()->GetString(pref_name); 173 reference_value = GetVerifierPrefs()->GetString(pref_name);
173 } else { 174 } else {
174 reference_value = GetPrefs(0)->GetString(pref_name); 175 reference_value = GetPrefs(0)->GetString(pref_name);
175 } 176 }
176 for (int i = 0; i < test()->num_clients(); ++i) { 177 for (int i = 0; i < test()->num_clients(); ++i) {
177 if (reference_value != GetPrefs(i)->GetString(pref_name)) { 178 if (reference_value != GetPrefs(i)->GetString(pref_name)) {
178 LOG(ERROR) << "String preference " << pref_name << " mismatched in" 179 DVLOG(1) << "String preference " << pref_name << " mismatched in"
179 << " profile " << i << "."; 180 << " profile " << i << ".";
180 return false; 181 return false;
181 } 182 }
182 } 183 }
183 return true; 184 return true;
184 } 185 }
185 186
186 bool FilePathPrefMatches(const char* pref_name) { 187 bool FilePathPrefMatches(const char* pref_name) {
187 base::FilePath reference_value; 188 base::FilePath reference_value;
188 if (test()->use_verifier()) { 189 if (test()->use_verifier()) {
189 reference_value = GetVerifierPrefs()->GetFilePath(pref_name); 190 reference_value = GetVerifierPrefs()->GetFilePath(pref_name);
190 } else { 191 } else {
191 reference_value = GetPrefs(0)->GetFilePath(pref_name); 192 reference_value = GetPrefs(0)->GetFilePath(pref_name);
192 } 193 }
193 for (int i = 0; i < test()->num_clients(); ++i) { 194 for (int i = 0; i < test()->num_clients(); ++i) {
194 if (reference_value != GetPrefs(i)->GetFilePath(pref_name)) { 195 if (reference_value != GetPrefs(i)->GetFilePath(pref_name)) {
195 LOG(ERROR) << "base::FilePath preference " << pref_name 196 DVLOG(1) << "base::FilePath preference " << pref_name << " mismatched in"
196 << " mismatched in" << " profile " << i << "."; 197 << " profile " << i << ".";
197 return false; 198 return false;
198 } 199 }
199 } 200 }
200 return true; 201 return true;
201 } 202 }
202 203
203 bool ListPrefMatches(const char* pref_name) { 204 bool ListPrefMatches(const char* pref_name) {
204 const base::ListValue* reference_value; 205 const base::ListValue* reference_value;
205 if (test()->use_verifier()) { 206 if (test()->use_verifier()) {
206 reference_value = GetVerifierPrefs()->GetList(pref_name); 207 reference_value = GetVerifierPrefs()->GetList(pref_name);
207 } else { 208 } else {
208 reference_value = GetPrefs(0)->GetList(pref_name); 209 reference_value = GetPrefs(0)->GetList(pref_name);
209 } 210 }
210 for (int i = 0; i < test()->num_clients(); ++i) { 211 for (int i = 0; i < test()->num_clients(); ++i) {
211 if (!reference_value->Equals(GetPrefs(i)->GetList(pref_name))) { 212 if (!reference_value->Equals(GetPrefs(i)->GetList(pref_name))) {
212 LOG(ERROR) << "List preference " << pref_name << " mismatched in" 213 DVLOG(1) << "List preference " << pref_name << " mismatched in"
213 << " profile " << i << "."; 214 << " profile " << i << ".";
214 return false; 215 return false;
215 } 216 }
216 } 217 }
217 return true; 218 return true;
218 } 219 }
219 220
220 221
221 namespace { 222 namespace {
222 223
223 // Helper class used in the implementation of AwaitListPrefMatches. 224 class PrefMatchChecker : public StatusChangeChecker {
224 class ListPrefMatchStatusChecker : public MultiClientStatusChangeChecker {
225 public: 225 public:
226 explicit ListPrefMatchStatusChecker(const char* pref_name); 226 explicit PrefMatchChecker(const char* path);
227 virtual ~ListPrefMatchStatusChecker(); 227 virtual ~PrefMatchChecker();
228 228
229 virtual bool IsExitConditionSatisfied() OVERRIDE; 229 // StatusChangeChecker implementation.
230 virtual bool IsExitConditionSatisfied() = 0;
230 virtual std::string GetDebugMessage() const OVERRIDE; 231 virtual std::string GetDebugMessage() const OVERRIDE;
232
233 // Wait for condition to become true.
234 void Wait();
235
236 protected:
237 const char* GetPath() const;
238
231 private: 239 private:
232 const char* pref_name_; 240 void RegisterPrefListener(PrefService* pref_service);
241
242 ScopedVector<PrefChangeRegistrar> pref_change_registrars_;
243 const char* path_;
233 }; 244 };
234 245
235 ListPrefMatchStatusChecker::ListPrefMatchStatusChecker(const char* pref_name) 246 PrefMatchChecker::PrefMatchChecker(const char* path) : path_(path) {
236 : MultiClientStatusChangeChecker(
237 sync_datatype_helper::test()->GetSyncServices()),
238 pref_name_(pref_name) {}
239
240 ListPrefMatchStatusChecker::~ListPrefMatchStatusChecker() {}
241
242 bool ListPrefMatchStatusChecker::IsExitConditionSatisfied() {
243 return ListPrefMatches(pref_name_);
244 } 247 }
245 248
246 std::string ListPrefMatchStatusChecker::GetDebugMessage() const { 249 PrefMatchChecker::~PrefMatchChecker() {
247 return "Waiting for matching preferences"; 250 }
251
252 std::string PrefMatchChecker::GetDebugMessage() const {
253 return base::StringPrintf("Waiting for pref '%s' to match", GetPath());
254 }
255
256 void PrefMatchChecker::Wait() {
257 if (test()->use_verifier()) {
258 RegisterPrefListener(GetVerifierPrefs());
259 }
260
261 for (int i = 0; i < test()->num_clients(); ++i) {
262 RegisterPrefListener(GetPrefs(i));
263 }
264 }
265
266 const char* PrefMatchChecker::GetPath() const {
267 return path_;
268 }
269
270 void PrefMatchChecker::RegisterPrefListener(PrefService* pref_service) {
271 scoped_ptr<PrefChangeRegistrar> registrar(new PrefChangeRegistrar());
272 registrar->Init(pref_service);
273 registrar->Add(path_,
274 base::Bind(&PrefMatchChecker::CheckExitCondition,
275 base::Unretained(this)));
276 pref_change_registrars_.push_back(registrar.release());
277 }
278
279 // Helper class used in the implementation of AwaitListPrefMatches.
280 class ListPrefMatchChecker : public PrefMatchChecker {
281 public:
282 explicit ListPrefMatchChecker(const char* path);
283 virtual ~ListPrefMatchChecker();
284
285 // Implementation of PrefMatchChecker.
286 virtual bool IsExitConditionSatisfied() OVERRIDE;
287 };
288
289 ListPrefMatchChecker::ListPrefMatchChecker(const char* path)
290 : PrefMatchChecker(path) {
291 }
292
293 ListPrefMatchChecker::~ListPrefMatchChecker() {
294 }
295
296 bool ListPrefMatchChecker::IsExitConditionSatisfied() {
297 return ListPrefMatches(GetPath());
298 }
299
300 // Helper class used in the implementation of AwaitBooleanPrefMatches.
301 class BooleanPrefMatchChecker : public PrefMatchChecker {
302 public:
303 explicit BooleanPrefMatchChecker(const char* path);
304 virtual ~BooleanPrefMatchChecker();
305
306 // Implementation of PrefMatchChecker.
307 virtual bool IsExitConditionSatisfied() OVERRIDE;
308 };
309
310 BooleanPrefMatchChecker::BooleanPrefMatchChecker(const char* path)
311 : PrefMatchChecker(path) {
312 }
313
314 BooleanPrefMatchChecker::~BooleanPrefMatchChecker() {
315 }
316
317 bool BooleanPrefMatchChecker::IsExitConditionSatisfied() {
318 return BooleanPrefMatches(GetPath());
319 }
320
321 // Helper class used in the implementation of AwaitIntegerPrefMatches.
322 class IntegerPrefMatchChecker : public PrefMatchChecker {
323 public:
324 explicit IntegerPrefMatchChecker(const char* path);
325 virtual ~IntegerPrefMatchChecker();
326
327 // Implementation of PrefMatchChecker.
328 virtual bool IsExitConditionSatisfied() OVERRIDE;
329 };
330
331 IntegerPrefMatchChecker::IntegerPrefMatchChecker(const char* path)
332 : PrefMatchChecker(path) {
333 }
334
335 IntegerPrefMatchChecker::~IntegerPrefMatchChecker() {
336 }
337
338 bool IntegerPrefMatchChecker::IsExitConditionSatisfied() {
339 return IntegerPrefMatches(GetPath());
340 }
341
342 // Helper class used in the implementation of AwaitStringPrefMatches.
343 class StringPrefMatchChecker : public PrefMatchChecker {
344 public:
345 explicit StringPrefMatchChecker(const char* path);
346 virtual ~StringPrefMatchChecker();
347
348 // Implementation of PrefMatchChecker.
349 virtual bool IsExitConditionSatisfied() OVERRIDE;
350 };
351
352 StringPrefMatchChecker::StringPrefMatchChecker(const char* path)
353 : PrefMatchChecker(path) {
354 }
355
356 StringPrefMatchChecker::~StringPrefMatchChecker() {
357 }
358
359 bool StringPrefMatchChecker::IsExitConditionSatisfied() {
360 return StringPrefMatches(GetPath());
248 } 361 }
249 362
250 } // namespace 363 } // namespace
251 364
252 bool AwaitListPrefMatches(const char* pref_name) { 365 bool AwaitListPrefMatches(const char* pref_name) {
253 ListPrefMatchStatusChecker checker(pref_name); 366 ListPrefMatchChecker checker(pref_name);
254 checker.Wait(); 367 checker.Wait();
255 return !checker.TimedOut(); 368 return !checker.TimedOut();
256 } 369 }
370
371 bool AwaitBooleanPrefMatches(const char* pref_name) {
372 BooleanPrefMatchChecker checker(pref_name);
373 checker.Wait();
374 return !checker.TimedOut();
375 }
376
377 bool AwaitIntegerPrefMatches(const char* pref_name) {
378 IntegerPrefMatchChecker checker(pref_name);
379 checker.Wait();
380 return !checker.TimedOut();
381 }
382
383 bool AwaitStringPrefMatches(const char* pref_name) {
384 StringPrefMatchChecker checker(pref_name);
385 checker.Wait();
386 return !checker.TimedOut();
387 }
257 388
258 } // namespace preferences_helper 389 } // namespace preferences_helper
OLDNEW
« no previous file with comments | « chrome/browser/sync/test/integration/preferences_helper.h ('k') | chrome/browser/sync/test/integration/sync_test.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698