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

Side by Side Diff: chrome/browser/password_manager/password_store_x_unittest.cc

Issue 8438034: Moving the SignalingTask class to base/test. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Addressed comments Created 9 years, 1 month 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 "base/basictypes.h" 5 #include "base/basictypes.h"
6 #include "base/bind.h" 6 #include "base/bind.h"
7 #include "base/file_util.h" 7 #include "base/file_util.h"
8 #include "base/platform_file.h" 8 #include "base/platform_file.h"
9 #include "base/scoped_temp_dir.h" 9 #include "base/scoped_temp_dir.h"
10 #include "base/stl_util.h" 10 #include "base/stl_util.h"
11 #include "base/string_util.h" 11 #include "base/string_util.h"
12 #include "base/stringprintf.h" 12 #include "base/stringprintf.h"
13 #include "base/synchronization/waitable_event.h" 13 #include "base/synchronization/waitable_event.h"
14 #include "base/test/signaling_task.h"
14 #include "base/time.h" 15 #include "base/time.h"
15 #include "base/utf_string_conversions.h" 16 #include "base/utf_string_conversions.h"
16 #include "chrome/browser/password_manager/password_form_data.h" 17 #include "chrome/browser/password_manager/password_form_data.h"
17 #include "chrome/browser/password_manager/password_store_change.h" 18 #include "chrome/browser/password_manager/password_store_change.h"
18 #include "chrome/browser/password_manager/password_store_consumer.h" 19 #include "chrome/browser/password_manager/password_store_consumer.h"
19 #include "chrome/browser/password_manager/password_store_x.h" 20 #include "chrome/browser/password_manager/password_store_x.h"
20 #include "chrome/browser/prefs/pref_service.h" 21 #include "chrome/browser/prefs/pref_service.h"
21 #include "chrome/browser/webdata/web_data_service.h" 22 #include "chrome/browser/webdata/web_data_service.h"
22 #include "chrome/common/chrome_notification_types.h" 23 #include "chrome/common/chrome_notification_types.h"
23 #include "chrome/common/pref_names.h" 24 #include "chrome/common/pref_names.h"
24 #include "chrome/test/base/signaling_task.h"
25 #include "chrome/test/base/testing_browser_process.h" 25 #include "chrome/test/base/testing_browser_process.h"
26 #include "chrome/test/base/testing_profile.h" 26 #include "chrome/test/base/testing_profile.h"
27 #include "content/public/browser/notification_details.h" 27 #include "content/public/browser/notification_details.h"
28 #include "content/public/browser/notification_registrar.h" 28 #include "content/public/browser/notification_registrar.h"
29 #include "content/public/browser/notification_source.h" 29 #include "content/public/browser/notification_source.h"
30 #include "content/test/notification_observer_mock.h" 30 #include "content/test/notification_observer_mock.h"
31 #include "content/test/test_browser_thread.h" 31 #include "content/test/test_browser_thread.h"
32 #include "testing/gmock/include/gmock/gmock.h" 32 #include "testing/gmock/include/gmock/gmock.h"
33 #include "testing/gtest/include/gtest/gtest.h" 33 #include "testing/gtest/include/gtest/gtest.h"
34 34
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after
352 } 352 }
353 for (VectorOfForms::iterator it = expected_blacklisted.begin(); 353 for (VectorOfForms::iterator it = expected_blacklisted.begin();
354 it != expected_blacklisted.end(); ++it) { 354 it != expected_blacklisted.end(); ++it) {
355 wds_->AddLogin(**it); 355 wds_->AddLogin(**it);
356 } 356 }
357 357
358 // The WDS schedules tasks to run on the DB thread so we schedule yet another 358 // The WDS schedules tasks to run on the DB thread so we schedule yet another
359 // task to notify us that it's safe to carry on with the test. 359 // task to notify us that it's safe to carry on with the test.
360 WaitableEvent done(false, false); 360 WaitableEvent done(false, false);
361 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, 361 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
362 new SignalingTask(&done)); 362 new base::SignalingTask(&done));
363 done.Wait(); 363 done.Wait();
364 364
365 // Initializing the PasswordStore should trigger a migration. 365 // Initializing the PasswordStore should trigger a migration.
366 scoped_refptr<PasswordStoreX> store( 366 scoped_refptr<PasswordStoreX> store(
367 new PasswordStoreX(login_db_.release(), 367 new PasswordStoreX(login_db_.release(),
368 profile_.get(), 368 profile_.get(),
369 wds_.get(), 369 wds_.get(),
370 GetBackend())); 370 GetBackend()));
371 store->Init(); 371 store->Init();
372 372
373 // Check that the migration preference has not been initialized. 373 // Check that the migration preference has not been initialized.
374 ASSERT_TRUE(NULL == profile_->GetPrefs()->FindPreference( 374 ASSERT_TRUE(NULL == profile_->GetPrefs()->FindPreference(
375 prefs::kLoginDatabaseMigrated)); 375 prefs::kLoginDatabaseMigrated));
376 376
377 // Again, the WDS schedules tasks to run on the DB thread, so schedule a task 377 // Again, the WDS schedules tasks to run on the DB thread, so schedule a task
378 // to signal us when it is safe to continue. 378 // to signal us when it is safe to continue.
379 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, 379 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
380 new SignalingTask(&done)); 380 new base::SignalingTask(&done));
381 done.Wait(); 381 done.Wait();
382 382
383 // Let the WDS callbacks proceed so the logins can be migrated. 383 // Let the WDS callbacks proceed so the logins can be migrated.
384 MessageLoop::current()->RunAllPending(); 384 MessageLoop::current()->RunAllPending();
385 385
386 MockPasswordStoreConsumer consumer; 386 MockPasswordStoreConsumer consumer;
387 387
388 // Make sure we quit the MessageLoop even if the test fails. 388 // Make sure we quit the MessageLoop even if the test fails.
389 ON_CALL(consumer, OnPasswordStoreRequestDone(_, _)) 389 ON_CALL(consumer, OnPasswordStoreRequestDone(_, _))
390 .WillByDefault(QuitUIMessageLoop()); 390 .WillByDefault(QuitUIMessageLoop());
(...skipping 24 matching lines...) Expand all
415 MockWebDataServiceConsumer wds_consumer; 415 MockWebDataServiceConsumer wds_consumer;
416 416
417 // No autofillable logins should be left in the WDS. 417 // No autofillable logins should be left in the WDS.
418 EXPECT_CALL(wds_consumer, 418 EXPECT_CALL(wds_consumer,
419 OnWebDataServiceRequestDone(_, EmptyWDResult())); 419 OnWebDataServiceRequestDone(_, EmptyWDResult()));
420 420
421 wds_->GetAutofillableLogins(&wds_consumer); 421 wds_->GetAutofillableLogins(&wds_consumer);
422 422
423 // Wait for the WDS methods to execute on the DB thread. 423 // Wait for the WDS methods to execute on the DB thread.
424 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, 424 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
425 new SignalingTask(&done)); 425 new base::SignalingTask(&done));
426 done.Wait(); 426 done.Wait();
427 427
428 // Handle the callback from the WDS. 428 // Handle the callback from the WDS.
429 MessageLoop::current()->RunAllPending(); 429 MessageLoop::current()->RunAllPending();
430 430
431 // Likewise, no blacklisted logins should be left in the WDS. 431 // Likewise, no blacklisted logins should be left in the WDS.
432 EXPECT_CALL(wds_consumer, 432 EXPECT_CALL(wds_consumer,
433 OnWebDataServiceRequestDone(_, EmptyWDResult())); 433 OnWebDataServiceRequestDone(_, EmptyWDResult()));
434 434
435 wds_->GetBlacklistLogins(&wds_consumer); 435 wds_->GetBlacklistLogins(&wds_consumer);
436 436
437 // Wait for the WDS methods to execute on the DB thread. 437 // Wait for the WDS methods to execute on the DB thread.
438 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, 438 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
439 new SignalingTask(&done)); 439 new base::SignalingTask(&done));
440 done.Wait(); 440 done.Wait();
441 441
442 // Handle the callback from the WDS. 442 // Handle the callback from the WDS.
443 MessageLoop::current()->RunAllPending(); 443 MessageLoop::current()->RunAllPending();
444 444
445 STLDeleteElements(&expected_autofillable); 445 STLDeleteElements(&expected_autofillable);
446 STLDeleteElements(&expected_blacklisted); 446 STLDeleteElements(&expected_blacklisted);
447 447
448 // Public in PasswordStore, protected in PasswordStoreX. 448 // Public in PasswordStore, protected in PasswordStoreX.
449 static_cast<PasswordStore*>(store)->Shutdown(); 449 static_cast<PasswordStore*>(store)->Shutdown();
(...skipping 22 matching lines...) Expand all
472 // Populate the WDS with logins that should be migrated. 472 // Populate the WDS with logins that should be migrated.
473 for (VectorOfForms::iterator it = unexpected_autofillable.begin(); 473 for (VectorOfForms::iterator it = unexpected_autofillable.begin();
474 it != unexpected_autofillable.end(); ++it) { 474 it != unexpected_autofillable.end(); ++it) {
475 wds_->AddLogin(**it); 475 wds_->AddLogin(**it);
476 } 476 }
477 477
478 // The WDS schedules tasks to run on the DB thread so we schedule yet another 478 // The WDS schedules tasks to run on the DB thread so we schedule yet another
479 // task to notify us that it's safe to carry on with the test. 479 // task to notify us that it's safe to carry on with the test.
480 WaitableEvent done(false, false); 480 WaitableEvent done(false, false);
481 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, 481 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
482 new SignalingTask(&done)); 482 new base::SignalingTask(&done));
483 done.Wait(); 483 done.Wait();
484 484
485 // Pretend that the migration has already taken place. 485 // Pretend that the migration has already taken place.
486 profile_->GetPrefs()->RegisterBooleanPref(prefs::kLoginDatabaseMigrated, 486 profile_->GetPrefs()->RegisterBooleanPref(prefs::kLoginDatabaseMigrated,
487 true, 487 true,
488 PrefService::UNSYNCABLE_PREF); 488 PrefService::UNSYNCABLE_PREF);
489 489
490 // Initializing the PasswordStore shouldn't trigger a migration. 490 // Initializing the PasswordStore shouldn't trigger a migration.
491 scoped_refptr<PasswordStoreX> store( 491 scoped_refptr<PasswordStoreX> store(
492 new PasswordStoreX(login_db_.release(), 492 new PasswordStoreX(login_db_.release(),
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
557 Pointee(ElementsAreArray( 557 Pointee(ElementsAreArray(
558 expected_add_changes))))); 558 expected_add_changes)))));
559 559
560 // Adding a login should trigger a notification. 560 // Adding a login should trigger a notification.
561 store->AddLogin(*form); 561 store->AddLogin(*form);
562 562
563 // The PasswordStore schedules tasks to run on the DB thread so we schedule 563 // The PasswordStore schedules tasks to run on the DB thread so we schedule
564 // yet another task to notify us that it's safe to carry on with the test. 564 // yet another task to notify us that it's safe to carry on with the test.
565 WaitableEvent done(false, false); 565 WaitableEvent done(false, false);
566 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, 566 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
567 new SignalingTask(&done)); 567 new base::SignalingTask(&done));
568 done.Wait(); 568 done.Wait();
569 569
570 // Change the password. 570 // Change the password.
571 form->password_value = WideToUTF16(L"a different password"); 571 form->password_value = WideToUTF16(L"a different password");
572 572
573 const PasswordStoreChange expected_update_changes[] = { 573 const PasswordStoreChange expected_update_changes[] = {
574 PasswordStoreChange(PasswordStoreChange::UPDATE, *form), 574 PasswordStoreChange(PasswordStoreChange::UPDATE, *form),
575 }; 575 };
576 576
577 EXPECT_CALL(helper->observer(), 577 EXPECT_CALL(helper->observer(),
578 Observe(int(chrome::NOTIFICATION_LOGINS_CHANGED), 578 Observe(int(chrome::NOTIFICATION_LOGINS_CHANGED),
579 content::Source<PasswordStore>(store), 579 content::Source<PasswordStore>(store),
580 Property(&content::Details<const PasswordStoreChangeList>::ptr, 580 Property(&content::Details<const PasswordStoreChangeList>::ptr,
581 Pointee(ElementsAreArray( 581 Pointee(ElementsAreArray(
582 expected_update_changes))))); 582 expected_update_changes)))));
583 583
584 // Updating the login with the new password should trigger a notification. 584 // Updating the login with the new password should trigger a notification.
585 store->UpdateLogin(*form); 585 store->UpdateLogin(*form);
586 586
587 // Wait for PasswordStore to send the notification. 587 // Wait for PasswordStore to send the notification.
588 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, 588 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
589 new SignalingTask(&done)); 589 new base::SignalingTask(&done));
590 done.Wait(); 590 done.Wait();
591 591
592 const PasswordStoreChange expected_delete_changes[] = { 592 const PasswordStoreChange expected_delete_changes[] = {
593 PasswordStoreChange(PasswordStoreChange::REMOVE, *form), 593 PasswordStoreChange(PasswordStoreChange::REMOVE, *form),
594 }; 594 };
595 595
596 EXPECT_CALL(helper->observer(), 596 EXPECT_CALL(helper->observer(),
597 Observe(int(chrome::NOTIFICATION_LOGINS_CHANGED), 597 Observe(int(chrome::NOTIFICATION_LOGINS_CHANGED),
598 content::Source<PasswordStore>(store), 598 content::Source<PasswordStore>(store),
599 Property(&content::Details<const PasswordStoreChangeList>::ptr, 599 Property(&content::Details<const PasswordStoreChangeList>::ptr,
600 Pointee(ElementsAreArray( 600 Pointee(ElementsAreArray(
601 expected_delete_changes))))); 601 expected_delete_changes)))));
602 602
603 // Deleting the login should trigger a notification. 603 // Deleting the login should trigger a notification.
604 store->RemoveLogin(*form); 604 store->RemoveLogin(*form);
605 605
606 // Wait for PasswordStore to send the notification. 606 // Wait for PasswordStore to send the notification.
607 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, 607 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
608 new SignalingTask(&done)); 608 new base::SignalingTask(&done));
609 done.Wait(); 609 done.Wait();
610 610
611 // Public in PasswordStore, protected in PasswordStoreX. 611 // Public in PasswordStore, protected in PasswordStoreX.
612 static_cast<PasswordStore*>(store)->Shutdown(); 612 static_cast<PasswordStore*>(store)->Shutdown();
613 } 613 }
614 614
615 TEST_P(PasswordStoreXTest, NativeMigration) { 615 TEST_P(PasswordStoreXTest, NativeMigration) {
616 VectorOfForms expected_autofillable; 616 VectorOfForms expected_autofillable;
617 InitExpectedForms(true, 50, &expected_autofillable); 617 InitExpectedForms(true, 50, &expected_autofillable);
618 618
(...skipping 21 matching lines...) Expand all
640 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, 640 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
641 base::IgnoreReturn<bool>(base::Bind( 641 base::IgnoreReturn<bool>(base::Bind(
642 &LoginDatabase::AddLogin, 642 &LoginDatabase::AddLogin,
643 base::Unretained(login_db), **it))); 643 base::Unretained(login_db), **it)));
644 } 644 }
645 645
646 // Schedule another task on the DB thread to notify us that it's safe to 646 // Schedule another task on the DB thread to notify us that it's safe to
647 // carry on with the test. 647 // carry on with the test.
648 WaitableEvent done(false, false); 648 WaitableEvent done(false, false);
649 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, 649 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
650 new SignalingTask(&done)); 650 new base::SignalingTask(&done));
651 done.Wait(); 651 done.Wait();
652 652
653 // Get the new size of the login DB file. We expect it to be larger. 653 // Get the new size of the login DB file. We expect it to be larger.
654 base::PlatformFileInfo db_file_full_info; 654 base::PlatformFileInfo db_file_full_info;
655 ASSERT_TRUE(file_util::GetFileInfo(login_db_file, &db_file_full_info)); 655 ASSERT_TRUE(file_util::GetFileInfo(login_db_file, &db_file_full_info));
656 EXPECT_GT(db_file_full_info.size, db_file_start_info.size); 656 EXPECT_GT(db_file_full_info.size, db_file_start_info.size);
657 657
658 // Pretend that the WDS migration has already taken place. 658 // Pretend that the WDS migration has already taken place.
659 profile_->GetPrefs()->RegisterBooleanPref(prefs::kLoginDatabaseMigrated, 659 profile_->GetPrefs()->RegisterBooleanPref(prefs::kLoginDatabaseMigrated,
660 true, 660 true,
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
705 OnLoginDatabaseQueryDone( 705 OnLoginDatabaseQueryDone(
706 ContainsAllPasswordForms(expected_autofillable))) 706 ContainsAllPasswordForms(expected_autofillable)))
707 .WillOnce(WithArg<0>(STLDeleteElements0())); 707 .WillOnce(WithArg<0>(STLDeleteElements0()));
708 } 708 }
709 709
710 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, 710 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
711 new LoginDatabaseQueryTask(login_db, true, &ld_return)); 711 new LoginDatabaseQueryTask(login_db, true, &ld_return));
712 712
713 // Wait for the login DB methods to execute on the DB thread. 713 // Wait for the login DB methods to execute on the DB thread.
714 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, 714 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
715 new SignalingTask(&done)); 715 new base::SignalingTask(&done));
716 done.Wait(); 716 done.Wait();
717 717
718 if (GetParam() == WORKING_BACKEND) { 718 if (GetParam() == WORKING_BACKEND) {
719 // Likewise, no blacklisted logins should be left in the login DB. 719 // Likewise, no blacklisted logins should be left in the login DB.
720 EXPECT_CALL(ld_return, 720 EXPECT_CALL(ld_return,
721 OnLoginDatabaseQueryDone(ContainsAllPasswordForms(empty))); 721 OnLoginDatabaseQueryDone(ContainsAllPasswordForms(empty)));
722 } else { 722 } else {
723 // The blacklisted logins should still be in the login DB. 723 // The blacklisted logins should still be in the login DB.
724 EXPECT_CALL(ld_return, 724 EXPECT_CALL(ld_return,
725 OnLoginDatabaseQueryDone( 725 OnLoginDatabaseQueryDone(
726 ContainsAllPasswordForms(expected_blacklisted))) 726 ContainsAllPasswordForms(expected_blacklisted)))
727 .WillOnce(WithArg<0>(STLDeleteElements0())); 727 .WillOnce(WithArg<0>(STLDeleteElements0()));
728 } 728 }
729 729
730 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, 730 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
731 new LoginDatabaseQueryTask(login_db, false, &ld_return)); 731 new LoginDatabaseQueryTask(login_db, false, &ld_return));
732 732
733 // Wait for the login DB methods to execute on the DB thread. 733 // Wait for the login DB methods to execute on the DB thread.
734 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, 734 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
735 new SignalingTask(&done)); 735 new base::SignalingTask(&done));
736 done.Wait(); 736 done.Wait();
737 737
738 if (GetParam() == WORKING_BACKEND) { 738 if (GetParam() == WORKING_BACKEND) {
739 // If the migration succeeded, then not only should there be no logins left 739 // If the migration succeeded, then not only should there be no logins left
740 // in the login DB, but also the file should have been deleted and then 740 // in the login DB, but also the file should have been deleted and then
741 // recreated. We approximate checking for this by checking that the file 741 // recreated. We approximate checking for this by checking that the file
742 // size is equal to the size before we populated it, even though it was 742 // size is equal to the size before we populated it, even though it was
743 // larger after populating it. 743 // larger after populating it.
744 base::PlatformFileInfo db_file_end_info; 744 base::PlatformFileInfo db_file_end_info;
745 ASSERT_TRUE(file_util::GetFileInfo(login_db_file, &db_file_end_info)); 745 ASSERT_TRUE(file_util::GetFileInfo(login_db_file, &db_file_end_info));
746 EXPECT_EQ(db_file_start_info.size, db_file_end_info.size); 746 EXPECT_EQ(db_file_start_info.size, db_file_end_info.size);
747 } 747 }
748 748
749 STLDeleteElements(&expected_autofillable); 749 STLDeleteElements(&expected_autofillable);
750 STLDeleteElements(&expected_blacklisted); 750 STLDeleteElements(&expected_blacklisted);
751 751
752 // Public in PasswordStore, protected in PasswordStoreX. 752 // Public in PasswordStore, protected in PasswordStoreX.
753 static_cast<PasswordStore*>(store)->Shutdown(); 753 static_cast<PasswordStore*>(store)->Shutdown();
754 } 754 }
755 755
756 INSTANTIATE_TEST_CASE_P(NoBackend, 756 INSTANTIATE_TEST_CASE_P(NoBackend,
757 PasswordStoreXTest, 757 PasswordStoreXTest,
758 testing::Values(NO_BACKEND)); 758 testing::Values(NO_BACKEND));
759 INSTANTIATE_TEST_CASE_P(FailingBackend, 759 INSTANTIATE_TEST_CASE_P(FailingBackend,
760 PasswordStoreXTest, 760 PasswordStoreXTest,
761 testing::Values(FAILING_BACKEND)); 761 testing::Values(FAILING_BACKEND));
762 INSTANTIATE_TEST_CASE_P(WorkingBackend, 762 INSTANTIATE_TEST_CASE_P(WorkingBackend,
763 PasswordStoreXTest, 763 PasswordStoreXTest,
764 testing::Values(WORKING_BACKEND)); 764 testing::Values(WORKING_BACKEND));
OLDNEW
« no previous file with comments | « chrome/browser/password_manager/password_store_win_unittest.cc ('k') | chrome/browser/sync/signin_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698