OLD | NEW |
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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 } | 52 } |
52 | 53 |
53 void ChangeStringPref(int index, | 54 void ChangeStringPref(int index, |
54 const char* pref_name, | 55 const char* pref_name, |
55 const std::string& new_value) { | 56 const std::string& new_value) { |
56 GetPrefs(index)->SetString(pref_name, new_value); | 57 GetPrefs(index)->SetString(pref_name, new_value); |
57 if (test()->use_verifier()) | 58 if (test()->use_verifier()) |
58 GetVerifierPrefs()->SetString(pref_name, new_value); | 59 GetVerifierPrefs()->SetString(pref_name, new_value); |
59 } | 60 } |
60 | 61 |
61 void AppendStringPref(int index, | |
62 const char* pref_name, | |
63 const std::string& append_value) { | |
64 ChangeStringPref(index, | |
65 pref_name, | |
66 GetPrefs(index)->GetString(pref_name) + append_value); | |
67 } | |
68 | |
69 void ChangeFilePathPref(int index, | 62 void ChangeFilePathPref(int index, |
70 const char* pref_name, | 63 const char* pref_name, |
71 const base::FilePath& new_value) { | 64 const base::FilePath& new_value) { |
72 GetPrefs(index)->SetFilePath(pref_name, new_value); | 65 GetPrefs(index)->SetFilePath(pref_name, new_value); |
73 if (test()->use_verifier()) | 66 if (test()->use_verifier()) |
74 GetVerifierPrefs()->SetFilePath(pref_name, new_value); | 67 GetVerifierPrefs()->SetFilePath(pref_name, new_value); |
75 } | 68 } |
76 | 69 |
77 void ChangeListPref(int index, | 70 void ChangeListPref(int index, |
78 const char* pref_name, | 71 const char* pref_name, |
(...skipping 21 matching lines...) Expand all Loading... |
100 | 93 |
101 bool BooleanPrefMatches(const char* pref_name) { | 94 bool BooleanPrefMatches(const char* pref_name) { |
102 bool reference_value; | 95 bool reference_value; |
103 if (test()->use_verifier()) { | 96 if (test()->use_verifier()) { |
104 reference_value = GetVerifierPrefs()->GetBoolean(pref_name); | 97 reference_value = GetVerifierPrefs()->GetBoolean(pref_name); |
105 } else { | 98 } else { |
106 reference_value = GetPrefs(0)->GetBoolean(pref_name); | 99 reference_value = GetPrefs(0)->GetBoolean(pref_name); |
107 } | 100 } |
108 for (int i = 0; i < test()->num_clients(); ++i) { | 101 for (int i = 0; i < test()->num_clients(); ++i) { |
109 if (reference_value != GetPrefs(i)->GetBoolean(pref_name)) { | 102 if (reference_value != GetPrefs(i)->GetBoolean(pref_name)) { |
110 LOG(ERROR) << "Boolean preference " << pref_name << " mismatched in" | 103 DVLOG(1) << "Boolean preference " << pref_name << " mismatched in" |
111 << " profile " << i << "."; | 104 << " profile " << i << "."; |
112 return false; | 105 return false; |
113 } | 106 } |
114 } | 107 } |
115 return true; | 108 return true; |
116 } | 109 } |
117 | 110 |
118 bool IntegerPrefMatches(const char* pref_name) { | 111 bool IntegerPrefMatches(const char* pref_name) { |
119 int reference_value; | 112 int reference_value; |
120 if (test()->use_verifier()) { | 113 if (test()->use_verifier()) { |
121 reference_value = GetVerifierPrefs()->GetInteger(pref_name); | 114 reference_value = GetVerifierPrefs()->GetInteger(pref_name); |
122 } else { | 115 } else { |
123 reference_value = GetPrefs(0)->GetInteger(pref_name); | 116 reference_value = GetPrefs(0)->GetInteger(pref_name); |
124 } | 117 } |
125 for (int i = 0; i < test()->num_clients(); ++i) { | 118 for (int i = 0; i < test()->num_clients(); ++i) { |
126 if (reference_value != GetPrefs(i)->GetInteger(pref_name)) { | 119 if (reference_value != GetPrefs(i)->GetInteger(pref_name)) { |
127 LOG(ERROR) << "Integer preference " << pref_name << " mismatched in" | 120 DVLOG(1) << "Integer preference " << pref_name << " mismatched in" |
128 << " profile " << i << "."; | 121 << " profile " << i << "."; |
129 return false; | 122 return false; |
130 } | 123 } |
131 } | 124 } |
132 return true; | 125 return true; |
133 } | 126 } |
134 | 127 |
135 bool Int64PrefMatches(const char* pref_name) { | 128 bool Int64PrefMatches(const char* pref_name) { |
136 int64 reference_value; | 129 int64 reference_value; |
137 if (test()->use_verifier()) { | 130 if (test()->use_verifier()) { |
138 reference_value = GetVerifierPrefs()->GetInt64(pref_name); | 131 reference_value = GetVerifierPrefs()->GetInt64(pref_name); |
139 } else { | 132 } else { |
140 reference_value = GetPrefs(0)->GetInt64(pref_name); | 133 reference_value = GetPrefs(0)->GetInt64(pref_name); |
141 } | 134 } |
142 for (int i = 0; i < test()->num_clients(); ++i) { | 135 for (int i = 0; i < test()->num_clients(); ++i) { |
143 if (reference_value != GetPrefs(i)->GetInt64(pref_name)) { | 136 if (reference_value != GetPrefs(i)->GetInt64(pref_name)) { |
144 LOG(ERROR) << "Integer preference " << pref_name << " mismatched in" | 137 DVLOG(1) << "Integer preference " << pref_name << " mismatched in" |
145 << " profile " << i << "."; | 138 << " profile " << i << "."; |
146 return false; | 139 return false; |
147 } | 140 } |
148 } | 141 } |
149 return true; | 142 return true; |
150 } | 143 } |
151 | 144 |
152 bool DoublePrefMatches(const char* pref_name) { | 145 bool DoublePrefMatches(const char* pref_name) { |
153 double reference_value; | 146 double reference_value; |
154 if (test()->use_verifier()) { | 147 if (test()->use_verifier()) { |
155 reference_value = GetVerifierPrefs()->GetDouble(pref_name); | 148 reference_value = GetVerifierPrefs()->GetDouble(pref_name); |
156 } else { | 149 } else { |
157 reference_value = GetPrefs(0)->GetDouble(pref_name); | 150 reference_value = GetPrefs(0)->GetDouble(pref_name); |
158 } | 151 } |
159 for (int i = 0; i < test()->num_clients(); ++i) { | 152 for (int i = 0; i < test()->num_clients(); ++i) { |
160 if (reference_value != GetPrefs(i)->GetDouble(pref_name)) { | 153 if (reference_value != GetPrefs(i)->GetDouble(pref_name)) { |
161 LOG(ERROR) << "Double preference " << pref_name << " mismatched in" | 154 DVLOG(1) << "Double preference " << pref_name << " mismatched in" |
162 << " profile " << i << "."; | 155 << " profile " << i << "."; |
163 return false; | 156 return false; |
164 } | 157 } |
165 } | 158 } |
166 return true; | 159 return true; |
167 } | 160 } |
168 | 161 |
169 bool StringPrefMatches(const char* pref_name) { | 162 bool StringPrefMatches(const char* pref_name) { |
170 std::string reference_value; | 163 std::string reference_value; |
171 if (test()->use_verifier()) { | 164 if (test()->use_verifier()) { |
172 reference_value = GetVerifierPrefs()->GetString(pref_name); | 165 reference_value = GetVerifierPrefs()->GetString(pref_name); |
173 } else { | 166 } else { |
174 reference_value = GetPrefs(0)->GetString(pref_name); | 167 reference_value = GetPrefs(0)->GetString(pref_name); |
175 } | 168 } |
176 for (int i = 0; i < test()->num_clients(); ++i) { | 169 for (int i = 0; i < test()->num_clients(); ++i) { |
177 if (reference_value != GetPrefs(i)->GetString(pref_name)) { | 170 if (reference_value != GetPrefs(i)->GetString(pref_name)) { |
178 LOG(ERROR) << "String preference " << pref_name << " mismatched in" | 171 DVLOG(1) << "String preference " << pref_name << " mismatched in" |
179 << " profile " << i << "."; | 172 << " profile " << i << "."; |
180 return false; | 173 return false; |
181 } | 174 } |
182 } | 175 } |
183 return true; | 176 return true; |
184 } | 177 } |
185 | 178 |
186 bool FilePathPrefMatches(const char* pref_name) { | 179 bool FilePathPrefMatches(const char* pref_name) { |
187 base::FilePath reference_value; | 180 base::FilePath reference_value; |
188 if (test()->use_verifier()) { | 181 if (test()->use_verifier()) { |
189 reference_value = GetVerifierPrefs()->GetFilePath(pref_name); | 182 reference_value = GetVerifierPrefs()->GetFilePath(pref_name); |
190 } else { | 183 } else { |
191 reference_value = GetPrefs(0)->GetFilePath(pref_name); | 184 reference_value = GetPrefs(0)->GetFilePath(pref_name); |
192 } | 185 } |
193 for (int i = 0; i < test()->num_clients(); ++i) { | 186 for (int i = 0; i < test()->num_clients(); ++i) { |
194 if (reference_value != GetPrefs(i)->GetFilePath(pref_name)) { | 187 if (reference_value != GetPrefs(i)->GetFilePath(pref_name)) { |
195 LOG(ERROR) << "base::FilePath preference " << pref_name | 188 DVLOG(1) << "base::FilePath preference " << pref_name << " mismatched in" |
196 << " mismatched in" << " profile " << i << "."; | 189 << " profile " << i << "."; |
197 return false; | 190 return false; |
198 } | 191 } |
199 } | 192 } |
200 return true; | 193 return true; |
201 } | 194 } |
202 | 195 |
203 bool ListPrefMatches(const char* pref_name) { | 196 bool ListPrefMatches(const char* pref_name) { |
204 const base::ListValue* reference_value; | 197 const base::ListValue* reference_value; |
205 if (test()->use_verifier()) { | 198 if (test()->use_verifier()) { |
206 reference_value = GetVerifierPrefs()->GetList(pref_name); | 199 reference_value = GetVerifierPrefs()->GetList(pref_name); |
207 } else { | 200 } else { |
208 reference_value = GetPrefs(0)->GetList(pref_name); | 201 reference_value = GetPrefs(0)->GetList(pref_name); |
209 } | 202 } |
210 for (int i = 0; i < test()->num_clients(); ++i) { | 203 for (int i = 0; i < test()->num_clients(); ++i) { |
211 if (!reference_value->Equals(GetPrefs(i)->GetList(pref_name))) { | 204 if (!reference_value->Equals(GetPrefs(i)->GetList(pref_name))) { |
212 LOG(ERROR) << "List preference " << pref_name << " mismatched in" | 205 DVLOG(1) << "List preference " << pref_name << " mismatched in" |
213 << " profile " << i << "."; | 206 << " profile " << i << "."; |
214 return false; | 207 return false; |
215 } | 208 } |
216 } | 209 } |
217 return true; | 210 return true; |
218 } | 211 } |
219 | 212 |
220 | 213 |
221 namespace { | 214 namespace { |
222 | 215 |
223 // Helper class used in the implementation of AwaitListPrefMatches. | 216 class PrefMatchChecker : public StatusChangeChecker { |
224 class ListPrefMatchStatusChecker : public MultiClientStatusChangeChecker { | |
225 public: | 217 public: |
226 explicit ListPrefMatchStatusChecker(const char* pref_name); | 218 explicit PrefMatchChecker(const char* path); |
227 virtual ~ListPrefMatchStatusChecker(); | 219 virtual ~PrefMatchChecker(); |
228 | 220 |
229 virtual bool IsExitConditionSatisfied() OVERRIDE; | 221 // StatusChangeChecker implementation. |
| 222 virtual bool IsExitConditionSatisfied() = 0; |
230 virtual std::string GetDebugMessage() const OVERRIDE; | 223 virtual std::string GetDebugMessage() const OVERRIDE; |
| 224 |
| 225 // Wait for condition to become true. |
| 226 void Wait(); |
| 227 |
| 228 protected: |
| 229 const char* GetPath() const; |
| 230 |
231 private: | 231 private: |
232 const char* pref_name_; | 232 void RegisterPrefListener(PrefService* pref_service); |
| 233 |
| 234 ScopedVector<PrefChangeRegistrar> pref_change_registrars_; |
| 235 const char* path_; |
233 }; | 236 }; |
234 | 237 |
235 ListPrefMatchStatusChecker::ListPrefMatchStatusChecker(const char* pref_name) | 238 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 } | 239 } |
245 | 240 |
246 std::string ListPrefMatchStatusChecker::GetDebugMessage() const { | 241 PrefMatchChecker::~PrefMatchChecker() { |
247 return "Waiting for matching preferences"; | 242 } |
| 243 |
| 244 std::string PrefMatchChecker::GetDebugMessage() const { |
| 245 return base::StringPrintf("Waiting for pref '%s' to match", GetPath()); |
| 246 } |
| 247 |
| 248 void PrefMatchChecker::Wait() { |
| 249 if (test()->use_verifier()) { |
| 250 RegisterPrefListener(GetVerifierPrefs()); |
| 251 } |
| 252 |
| 253 for (int i = 0; i < test()->num_clients(); ++i) { |
| 254 RegisterPrefListener(GetPrefs(i)); |
| 255 } |
| 256 |
| 257 if (IsExitConditionSatisfied()) { |
| 258 return; |
| 259 } |
| 260 |
| 261 StartBlockingWait(); |
| 262 } |
| 263 |
| 264 const char* PrefMatchChecker::GetPath() const { |
| 265 return path_; |
| 266 } |
| 267 |
| 268 void PrefMatchChecker::RegisterPrefListener(PrefService* pref_service) { |
| 269 scoped_ptr<PrefChangeRegistrar> registrar(new PrefChangeRegistrar()); |
| 270 registrar->Init(pref_service); |
| 271 registrar->Add(path_, |
| 272 base::Bind(&PrefMatchChecker::CheckExitCondition, |
| 273 base::Unretained(this))); |
| 274 pref_change_registrars_.push_back(registrar.release()); |
| 275 } |
| 276 |
| 277 // Helper class used in the implementation of AwaitListPrefMatches. |
| 278 class ListPrefMatchChecker : public PrefMatchChecker { |
| 279 public: |
| 280 explicit ListPrefMatchChecker(const char* path); |
| 281 virtual ~ListPrefMatchChecker(); |
| 282 |
| 283 // Implementation of PrefMatchChecker. |
| 284 virtual bool IsExitConditionSatisfied() OVERRIDE; |
| 285 }; |
| 286 |
| 287 ListPrefMatchChecker::ListPrefMatchChecker(const char* path) |
| 288 : PrefMatchChecker(path) { |
| 289 } |
| 290 |
| 291 ListPrefMatchChecker::~ListPrefMatchChecker() { |
| 292 } |
| 293 |
| 294 bool ListPrefMatchChecker::IsExitConditionSatisfied() { |
| 295 return ListPrefMatches(GetPath()); |
| 296 } |
| 297 |
| 298 // Helper class used in the implementation of AwaitBooleanPrefMatches. |
| 299 class BooleanPrefMatchChecker : public PrefMatchChecker { |
| 300 public: |
| 301 explicit BooleanPrefMatchChecker(const char* path); |
| 302 virtual ~BooleanPrefMatchChecker(); |
| 303 |
| 304 // Implementation of PrefMatchChecker. |
| 305 virtual bool IsExitConditionSatisfied() OVERRIDE; |
| 306 }; |
| 307 |
| 308 BooleanPrefMatchChecker::BooleanPrefMatchChecker(const char* path) |
| 309 : PrefMatchChecker(path) { |
| 310 } |
| 311 |
| 312 BooleanPrefMatchChecker::~BooleanPrefMatchChecker() { |
| 313 } |
| 314 |
| 315 bool BooleanPrefMatchChecker::IsExitConditionSatisfied() { |
| 316 return BooleanPrefMatches(GetPath()); |
| 317 } |
| 318 |
| 319 // Helper class used in the implementation of AwaitIntegerPrefMatches. |
| 320 class IntegerPrefMatchChecker : public PrefMatchChecker { |
| 321 public: |
| 322 explicit IntegerPrefMatchChecker(const char* path); |
| 323 virtual ~IntegerPrefMatchChecker(); |
| 324 |
| 325 // Implementation of PrefMatchChecker. |
| 326 virtual bool IsExitConditionSatisfied() OVERRIDE; |
| 327 }; |
| 328 |
| 329 IntegerPrefMatchChecker::IntegerPrefMatchChecker(const char* path) |
| 330 : PrefMatchChecker(path) { |
| 331 } |
| 332 |
| 333 IntegerPrefMatchChecker::~IntegerPrefMatchChecker() { |
| 334 } |
| 335 |
| 336 bool IntegerPrefMatchChecker::IsExitConditionSatisfied() { |
| 337 return IntegerPrefMatches(GetPath()); |
| 338 } |
| 339 |
| 340 // Helper class used in the implementation of AwaitStringPrefMatches. |
| 341 class StringPrefMatchChecker : public PrefMatchChecker { |
| 342 public: |
| 343 explicit StringPrefMatchChecker(const char* path); |
| 344 virtual ~StringPrefMatchChecker(); |
| 345 |
| 346 // Implementation of PrefMatchChecker. |
| 347 virtual bool IsExitConditionSatisfied() OVERRIDE; |
| 348 }; |
| 349 |
| 350 StringPrefMatchChecker::StringPrefMatchChecker(const char* path) |
| 351 : PrefMatchChecker(path) { |
| 352 } |
| 353 |
| 354 StringPrefMatchChecker::~StringPrefMatchChecker() { |
| 355 } |
| 356 |
| 357 bool StringPrefMatchChecker::IsExitConditionSatisfied() { |
| 358 return StringPrefMatches(GetPath()); |
248 } | 359 } |
249 | 360 |
250 } // namespace | 361 } // namespace |
251 | 362 |
252 bool AwaitListPrefMatches(const char* pref_name) { | 363 bool AwaitListPrefMatches(const char* pref_name) { |
253 ListPrefMatchStatusChecker checker(pref_name); | 364 ListPrefMatchChecker checker(pref_name); |
254 checker.Wait(); | 365 checker.Wait(); |
255 return !checker.TimedOut(); | 366 return !checker.TimedOut(); |
256 } | 367 } |
| 368 |
| 369 bool AwaitBooleanPrefMatches(const char* pref_name) { |
| 370 BooleanPrefMatchChecker checker(pref_name); |
| 371 checker.Wait(); |
| 372 return !checker.TimedOut(); |
| 373 } |
| 374 |
| 375 bool AwaitIntegerPrefMatches(const char* pref_name) { |
| 376 IntegerPrefMatchChecker checker(pref_name); |
| 377 checker.Wait(); |
| 378 return !checker.TimedOut(); |
| 379 } |
| 380 |
| 381 bool AwaitStringPrefMatches(const char* pref_name) { |
| 382 StringPrefMatchChecker checker(pref_name); |
| 383 checker.Wait(); |
| 384 return !checker.TimedOut(); |
| 385 } |
257 | 386 |
258 } // namespace preferences_helper | 387 } // namespace preferences_helper |
OLD | NEW |