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

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

Issue 303643003: sync: Refactor autofill integration tests (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 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 #include "chrome/browser/sync/test/integration/autofill_helper.h" 5 #include "chrome/browser/sync/test/integration/autofill_helper.h"
6 6
7 #include "chrome/browser/autofill/personal_data_manager_factory.h" 7 #include "chrome/browser/autofill/personal_data_manager_factory.h"
8 #include "chrome/browser/chrome_notification_types.h" 8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/profiles/profile.h" 9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/sync/profile_sync_service.h" 10 #include "chrome/browser/sync/profile_sync_service.h"
11 #include "chrome/browser/sync/profile_sync_test_util.h" 11 #include "chrome/browser/sync/profile_sync_test_util.h"
12 #include "chrome/browser/sync/test/integration/multi_client_status_change_checke r.h"
12 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h" 13 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h"
13 #include "chrome/browser/sync/test/integration/sync_test.h" 14 #include "chrome/browser/sync/test/integration/sync_test.h"
14 #include "chrome/browser/webdata/web_data_service_factory.h" 15 #include "chrome/browser/webdata/web_data_service_factory.h"
15 #include "components/autofill/core/browser/autofill_profile.h" 16 #include "components/autofill/core/browser/autofill_profile.h"
16 #include "components/autofill/core/browser/autofill_test_utils.h" 17 #include "components/autofill/core/browser/autofill_test_utils.h"
17 #include "components/autofill/core/browser/autofill_type.h" 18 #include "components/autofill/core/browser/autofill_type.h"
18 #include "components/autofill/core/browser/personal_data_manager.h" 19 #include "components/autofill/core/browser/personal_data_manager.h"
19 #include "components/autofill/core/browser/personal_data_manager_observer.h" 20 #include "components/autofill/core/browser/personal_data_manager_observer.h"
20 #include "components/autofill/core/browser/webdata/autofill_entry.h" 21 #include "components/autofill/core/browser/webdata/autofill_entry.h"
21 #include "components/autofill/core/browser/webdata/autofill_table.h" 22 #include "components/autofill/core/browser/webdata/autofill_table.h"
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 it != all_entries.end(); ++it) { 230 it != all_entries.end(); ++it) {
230 all_keys.insert(*it); 231 all_keys.insert(*it);
231 } 232 }
232 return all_keys; 233 return all_keys;
233 } 234 }
234 235
235 bool KeysMatch(int profile_a, int profile_b) { 236 bool KeysMatch(int profile_a, int profile_b) {
236 return GetAllKeys(profile_a) == GetAllKeys(profile_b); 237 return GetAllKeys(profile_a) == GetAllKeys(profile_b);
237 } 238 }
238 239
240 namespace {
241
242 class KeysMatchStatusChecker : public MultiClientStatusChangeChecker {
243 public:
244 KeysMatchStatusChecker(int profile_a, int profile_b);
245 virtual ~KeysMatchStatusChecker();
246
247 virtual bool IsExitConditionSatisfied() OVERRIDE;
248 virtual std::string GetDebugMessage() const OVERRIDE;
249
250 private:
251 const int profile_a_;
252 const int profile_b_;
253 };
254
255 KeysMatchStatusChecker::KeysMatchStatusChecker(int profile_a, int profile_b)
256 : MultiClientStatusChangeChecker(
257 sync_datatype_helper::test()->GetSyncServices()),
258 profile_a_(profile_a),
259 profile_b_(profile_b) {
260 }
261
262 KeysMatchStatusChecker::~KeysMatchStatusChecker() {
263 }
264
265 bool KeysMatchStatusChecker::IsExitConditionSatisfied() {
266 return KeysMatch(profile_a_, profile_b_);
267 }
268
269 std::string KeysMatchStatusChecker::GetDebugMessage() const {
270 return "Waiting for matching autofill keys";
271 }
272
273 } // namespace
274
275 bool AwaitKeysMatch(int a, int b) {
276 KeysMatchStatusChecker checker(a, b);
277 checker.Wait();
278 return !checker.TimedOut();
279 }
280
239 void SetProfiles(int profile, std::vector<AutofillProfile>* autofill_profiles) { 281 void SetProfiles(int profile, std::vector<AutofillProfile>* autofill_profiles) {
240 MockPersonalDataManagerObserver observer; 282 MockPersonalDataManagerObserver observer;
241 EXPECT_CALL(observer, OnPersonalDataChanged()). 283 EXPECT_CALL(observer, OnPersonalDataChanged()).
242 WillOnce(QuitUIMessageLoop()); 284 WillOnce(QuitUIMessageLoop());
243 PersonalDataManager* pdm = GetPersonalDataManager(profile); 285 PersonalDataManager* pdm = GetPersonalDataManager(profile);
244 pdm->AddObserver(&observer); 286 pdm->AddObserver(&observer);
245 pdm->SetProfiles(autofill_profiles); 287 pdm->SetProfiles(autofill_profiles);
246 base::MessageLoop::current()->Run(); 288 base::MessageLoop::current()->Run();
247 pdm->RemoveObserver(&observer); 289 pdm->RemoveObserver(&observer);
248 } 290 }
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 } 347 }
306 348
307 int GetProfileCount(int profile) { 349 int GetProfileCount(int profile) {
308 return GetAllProfiles(profile).size(); 350 return GetAllProfiles(profile).size();
309 } 351 }
310 352
311 int GetKeyCount(int profile) { 353 int GetKeyCount(int profile) {
312 return GetAllKeys(profile).size(); 354 return GetAllKeys(profile).size();
313 } 355 }
314 356
315 bool ProfilesMatch(int profile_a, int profile_b) { 357 namespace {
316 const std::vector<AutofillProfile*>& autofill_profiles_a = 358
317 GetAllProfiles(profile_a); 359 bool ProfilesMatchImpl(
360 int profile_a,
361 const std::vector<AutofillProfile*>& autofill_profiles_a,
362 int profile_b,
363 const std::vector<AutofillProfile*>& autofill_profiles_b) {
318 std::map<std::string, AutofillProfile> autofill_profiles_a_map; 364 std::map<std::string, AutofillProfile> autofill_profiles_a_map;
319 for (size_t i = 0; i < autofill_profiles_a.size(); ++i) { 365 for (size_t i = 0; i < autofill_profiles_a.size(); ++i) {
320 const AutofillProfile* p = autofill_profiles_a[i]; 366 const AutofillProfile* p = autofill_profiles_a[i];
321 autofill_profiles_a_map[p->guid()] = *p; 367 autofill_profiles_a_map[p->guid()] = *p;
322 } 368 }
323 369
324 const std::vector<AutofillProfile*>& autofill_profiles_b =
325 GetAllProfiles(profile_b);
326 for (size_t i = 0; i < autofill_profiles_b.size(); ++i) { 370 for (size_t i = 0; i < autofill_profiles_b.size(); ++i) {
327 const AutofillProfile* p = autofill_profiles_b[i]; 371 const AutofillProfile* p = autofill_profiles_b[i];
328 if (!autofill_profiles_a_map.count(p->guid())) { 372 if (!autofill_profiles_a_map.count(p->guid())) {
329 LOG(ERROR) << "GUID " << p->guid() << " not found in profile " 373 DVLOG(1) << "GUID " << p->guid() << " not found in profile " << profile_b
330 << profile_b << "."; 374 << ".";
331 return false; 375 return false;
332 } 376 }
333 AutofillProfile* expected_profile = &autofill_profiles_a_map[p->guid()]; 377 AutofillProfile* expected_profile = &autofill_profiles_a_map[p->guid()];
334 expected_profile->set_guid(p->guid()); 378 expected_profile->set_guid(p->guid());
335 if (*expected_profile != *p) { 379 if (*expected_profile != *p) {
336 LOG(ERROR) << "Mismatch in profile with GUID " << p->guid() << "."; 380 DVLOG(1) << "Mismatch in profile with GUID " << p->guid() << ".";
337 return false; 381 return false;
338 } 382 }
339 autofill_profiles_a_map.erase(p->guid()); 383 autofill_profiles_a_map.erase(p->guid());
340 } 384 }
341 385
342 if (autofill_profiles_a_map.size()) { 386 if (autofill_profiles_a_map.size()) {
343 LOG(ERROR) << "Entries present in Profile " << profile_a 387 DVLOG(1) << "Entries present in Profile " << profile_a << " but not in "
344 << " but not in " << profile_b << "."; 388 << profile_b << ".";
345 return false; 389 return false;
346 } 390 }
347 return true; 391 return true;
348 } 392 }
349 393
394 } // namespace
395
396 bool ProfilesMatch(int profile_a, int profile_b) {
397 const std::vector<AutofillProfile*>& autofill_profiles_a =
398 GetAllProfiles(profile_a);
399 const std::vector<AutofillProfile*>& autofill_profiles_b =
400 GetAllProfiles(profile_b);
401 return ProfilesMatchImpl(
402 profile_a, autofill_profiles_a, profile_b, autofill_profiles_b);
403 }
404
350 bool AllProfilesMatch() { 405 bool AllProfilesMatch() {
351 for (int i = 1; i < test()->num_clients(); ++i) { 406 for (int i = 1; i < test()->num_clients(); ++i) {
352 if (!ProfilesMatch(0, i)) { 407 if (!ProfilesMatch(0, i)) {
353 LOG(ERROR) << "Profile " << i << "does not contain the same autofill " 408 DVLOG(1) << "Profile " << i << "does not contain the same autofill "
354 "profiles as profile 0."; 409 "profiles as profile 0.";
355 return false; 410 return false;
356 } 411 }
357 } 412 }
358 return true; 413 return true;
359 } 414 }
360 415
416 namespace {
417
418 class ProfilesMatchStatusChecker : public StatusChangeChecker,
419 public PersonalDataManagerObserver {
420 public:
421 ProfilesMatchStatusChecker(int profile_a, int profile_b);
422 virtual ~ProfilesMatchStatusChecker();
423
424 // StatusChangeChecker implementation.
425 virtual bool IsExitConditionSatisfied() OVERRIDE;
426 virtual std::string GetDebugMessage() const OVERRIDE;
427
428 // PersonalDataManager implementation.
429 virtual void OnPersonalDataChanged() OVERRIDE;
430
431 // Wait for conidtion to beome true.
432 void Wait();
433
434 private:
435 const int profile_a_;
436 const int profile_b_;
437 bool registered_;
438 };
439
440 ProfilesMatchStatusChecker::ProfilesMatchStatusChecker(int profile_a,
441 int profile_b)
442 : profile_a_(profile_a), profile_b_(profile_b), registered_(false) {
443 }
444
445 ProfilesMatchStatusChecker::~ProfilesMatchStatusChecker() {
446 PersonalDataManager* pdm_a = GetPersonalDataManager(profile_a_);
447 PersonalDataManager* pdm_b = GetPersonalDataManager(profile_b_);
448 if (registered_) {
449 pdm_a->RemoveObserver(this);
450 pdm_b->RemoveObserver(this);
451 }
452 }
453
454 bool ProfilesMatchStatusChecker::IsExitConditionSatisfied() {
455 PersonalDataManager* pdm_a = GetPersonalDataManager(profile_a_);
456 PersonalDataManager* pdm_b = GetPersonalDataManager(profile_b_);
457
458 const std::vector<AutofillProfile*>& autofill_profiles_a =
459 pdm_a->web_profiles();
460 const std::vector<AutofillProfile*>& autofill_profiles_b =
461 pdm_b->web_profiles();
462
463 return ProfilesMatchImpl(
464 profile_a_, autofill_profiles_a, profile_b_, autofill_profiles_b);
465 }
466
467 void ProfilesMatchStatusChecker::Wait() {
468 PersonalDataManager* pdm_a = GetPersonalDataManager(profile_a_);
469 PersonalDataManager* pdm_b = GetPersonalDataManager(profile_b_);
470
471 pdm_a->AddObserver(this);
472 pdm_b->AddObserver(this);
473
474 pdm_a->Refresh();
475 pdm_b->Refresh();
476
477 registered_ = true;
478
479 StartBlockingWait();
480 }
481
482 std::string ProfilesMatchStatusChecker::GetDebugMessage() const {
483 return "Waiting for matching autofill profiles";
484 }
485
486 void ProfilesMatchStatusChecker::OnPersonalDataChanged() {
487 CheckExitCondition();
488 }
489
490 } // namespace
491
492 bool AwaitProfilesMatch(int a, int b) {
493 ProfilesMatchStatusChecker checker(a, b);
494 checker.Wait();
495 return !checker.TimedOut();
496 }
497
361 } // namespace autofill_helper 498 } // namespace autofill_helper
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698