| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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 <set> | 5 #include <set> |
| 6 #include <string> | 6 #include <string> |
| 7 #include <utility> |
| 7 #include <vector> | 8 #include <vector> |
| 8 | 9 |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 11 |
| 11 #include "base/bind.h" | 12 #include "base/bind.h" |
| 12 #include "base/bind_helpers.h" | 13 #include "base/bind_helpers.h" |
| 13 #include "base/callback.h" | 14 #include "base/callback.h" |
| 14 #include "base/compiler_specific.h" | 15 #include "base/compiler_specific.h" |
| 15 #include "base/location.h" | 16 #include "base/location.h" |
| 16 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
| 17 #include "base/memory/scoped_ptr.h" | 18 #include "base/memory/scoped_ptr.h" |
| 18 #include "base/message_loop/message_loop.h" | 19 #include "base/message_loop/message_loop.h" |
| 19 #include "base/strings/string16.h" | 20 #include "base/strings/string16.h" |
| 20 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
| 21 #include "base/synchronization/waitable_event.h" | 22 #include "base/synchronization/waitable_event.h" |
| 22 #include "base/time/time.h" | 23 #include "base/time/time.h" |
| 23 #include "chrome/browser/autofill/personal_data_manager_factory.h" | 24 #include "chrome/browser/autofill/personal_data_manager_factory.h" |
| 25 #include "chrome/browser/prefs/pref_service_syncable.h" |
| 24 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h" | 26 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h" |
| 25 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h" | 27 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h" |
| 26 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 28 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 27 #include "chrome/browser/signin/signin_manager_factory.h" | 29 #include "chrome/browser/signin/signin_manager_factory.h" |
| 28 #include "chrome/browser/sync/abstract_profile_sync_service_test.h" | 30 #include "chrome/browser/sync/abstract_profile_sync_service_test.h" |
| 29 #include "chrome/browser/sync/glue/autofill_data_type_controller.h" | 31 #include "chrome/browser/sync/glue/autofill_data_type_controller.h" |
| 30 #include "chrome/browser/sync/glue/autofill_profile_data_type_controller.h" | 32 #include "chrome/browser/sync/glue/autofill_profile_data_type_controller.h" |
| 31 #include "chrome/browser/sync/glue/generic_change_processor.h" | 33 #include "chrome/browser/sync/glue/generic_change_processor.h" |
| 32 #include "chrome/browser/sync/glue/shared_change_processor.h" | 34 #include "chrome/browser/sync/glue/shared_change_processor.h" |
| 33 #include "chrome/browser/sync/profile_sync_components_factory.h" | 35 #include "chrome/browser/sync/profile_sync_components_factory.h" |
| 34 #include "chrome/browser/sync/profile_sync_service.h" | 36 #include "chrome/browser/sync/profile_sync_service.h" |
| 35 #include "chrome/browser/sync/profile_sync_service_factory.h" | 37 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 36 #include "chrome/browser/sync/profile_sync_test_util.h" | 38 #include "chrome/browser/sync/profile_sync_test_util.h" |
| 37 #include "chrome/browser/sync/test_profile_sync_service.h" | 39 #include "chrome/browser/sync/test_profile_sync_service.h" |
| 38 #include "chrome/browser/webdata/autocomplete_syncable_service.h" | 40 #include "chrome/browser/webdata/autocomplete_syncable_service.h" |
| 39 #include "chrome/browser/webdata/web_data_service_factory.h" | 41 #include "chrome/browser/webdata/web_data_service_factory.h" |
| 42 #include "chrome/test/base/testing_browser_process.h" |
| 40 #include "chrome/test/base/testing_profile.h" | 43 #include "chrome/test/base/testing_profile.h" |
| 44 #include "chrome/test/base/testing_profile_manager.h" |
| 41 #include "components/autofill/core/browser/autofill_test_utils.h" | 45 #include "components/autofill/core/browser/autofill_test_utils.h" |
| 42 #include "components/autofill/core/browser/personal_data_manager.h" | 46 #include "components/autofill/core/browser/personal_data_manager.h" |
| 43 #include "components/autofill/core/browser/webdata/autofill_change.h" | 47 #include "components/autofill/core/browser/webdata/autofill_change.h" |
| 44 #include "components/autofill/core/browser/webdata/autofill_entry.h" | 48 #include "components/autofill/core/browser/webdata/autofill_entry.h" |
| 45 #include "components/autofill/core/browser/webdata/autofill_profile_syncable_ser
vice.h" | 49 #include "components/autofill/core/browser/webdata/autofill_profile_syncable_ser
vice.h" |
| 46 #include "components/autofill/core/browser/webdata/autofill_table.h" | 50 #include "components/autofill/core/browser/webdata/autofill_table.h" |
| 47 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" | 51 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" |
| 48 #include "components/signin/core/browser/signin_manager.h" | 52 #include "components/signin/core/browser/signin_manager.h" |
| 49 #include "components/sync_driver/data_type_controller.h" | 53 #include "components/sync_driver/data_type_controller.h" |
| 50 #include "components/webdata/common/web_data_service_test_util.h" | 54 #include "components/webdata/common/web_data_service_test_util.h" |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 using testing::Return; | 106 using testing::Return; |
| 103 | 107 |
| 104 class HistoryService; | 108 class HistoryService; |
| 105 | 109 |
| 106 namespace syncable { | 110 namespace syncable { |
| 107 class Id; | 111 class Id; |
| 108 } | 112 } |
| 109 | 113 |
| 110 namespace { | 114 namespace { |
| 111 | 115 |
| 116 const char kTestProfileName[] = "test-profile"; |
| 117 |
| 112 void RunAndSignal(const base::Closure& cb, WaitableEvent* event) { | 118 void RunAndSignal(const base::Closure& cb, WaitableEvent* event) { |
| 113 cb.Run(); | 119 cb.Run(); |
| 114 event->Signal(); | 120 event->Signal(); |
| 115 } | 121 } |
| 116 | 122 |
| 117 } // namespace | 123 } // namespace |
| 118 | 124 |
| 119 class AutofillTableMock : public AutofillTable { | 125 class AutofillTableMock : public AutofillTable { |
| 120 public: | 126 public: |
| 121 AutofillTableMock() : AutofillTable("en-US") {} | 127 AutofillTableMock() : AutofillTable("en-US") {} |
| (...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 464 // DataTypeDebugInfoListener implementation. | 470 // DataTypeDebugInfoListener implementation. |
| 465 virtual void OnDataTypeConfigureComplete( | 471 virtual void OnDataTypeConfigureComplete( |
| 466 const std::vector<syncer::DataTypeConfigurationStats>& | 472 const std::vector<syncer::DataTypeConfigurationStats>& |
| 467 configuration_stats) OVERRIDE { | 473 configuration_stats) OVERRIDE { |
| 468 ASSERT_EQ(1u, configuration_stats.size()); | 474 ASSERT_EQ(1u, configuration_stats.size()); |
| 469 association_stats_ = configuration_stats[0].association_stats; | 475 association_stats_ = configuration_stats[0].association_stats; |
| 470 } | 476 } |
| 471 | 477 |
| 472 protected: | 478 protected: |
| 473 ProfileSyncServiceAutofillTest() | 479 ProfileSyncServiceAutofillTest() |
| 474 : debug_ptr_factory_(this) { | 480 : profile_manager_(TestingBrowserProcess::GetGlobal()), |
| 481 debug_ptr_factory_(this) { |
| 475 } | 482 } |
| 476 virtual ~ProfileSyncServiceAutofillTest() { | 483 virtual ~ProfileSyncServiceAutofillTest() { |
| 477 } | 484 } |
| 478 | 485 |
| 479 AutofillProfileFactory profile_factory_; | 486 AutofillProfileFactory profile_factory_; |
| 480 AutofillEntryFactory entry_factory_; | 487 AutofillEntryFactory entry_factory_; |
| 481 | 488 |
| 482 AbstractAutofillFactory* GetFactory(syncer::ModelType type) { | 489 AbstractAutofillFactory* GetFactory(syncer::ModelType type) { |
| 483 if (type == syncer::AUTOFILL) { | 490 if (type == syncer::AUTOFILL) { |
| 484 return &entry_factory_; | 491 return &entry_factory_; |
| 485 } else if (type == syncer::AUTOFILL_PROFILE) { | 492 } else if (type == syncer::AUTOFILL_PROFILE) { |
| 486 return &profile_factory_; | 493 return &profile_factory_; |
| 487 } else { | 494 } else { |
| 488 NOTREACHED(); | 495 NOTREACHED(); |
| 489 return NULL; | 496 return NULL; |
| 490 } | 497 } |
| 491 } | 498 } |
| 492 | 499 |
| 493 virtual void SetUp() OVERRIDE { | 500 virtual void SetUp() OVERRIDE { |
| 494 AbstractProfileSyncServiceTest::SetUp(); | 501 AbstractProfileSyncServiceTest::SetUp(); |
| 495 TestingProfile::Builder builder; | 502 ASSERT_TRUE(profile_manager_.SetUp()); |
| 496 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(), | 503 TestingProfile::TestingFactories testing_factories; |
| 497 BuildAutoIssuingFakeProfileOAuth2TokenService); | 504 testing_factories.push_back(std::make_pair( |
| 498 profile_ = builder.Build().Pass(); | 505 ProfileOAuth2TokenServiceFactory::GetInstance(), |
| 506 BuildAutoIssuingFakeProfileOAuth2TokenService)); |
| 507 profile_ = profile_manager_.CreateTestingProfile( |
| 508 kTestProfileName, |
| 509 scoped_ptr<PrefServiceSyncable>(), |
| 510 base::UTF8ToUTF16(kTestProfileName), |
| 511 0, |
| 512 std::string(), |
| 513 testing_factories); |
| 499 web_database_.reset(new WebDatabaseFake(&autofill_table_)); | 514 web_database_.reset(new WebDatabaseFake(&autofill_table_)); |
| 500 MockWebDataServiceWrapper* wrapper = | 515 MockWebDataServiceWrapper* wrapper = |
| 501 static_cast<MockWebDataServiceWrapper*>( | 516 static_cast<MockWebDataServiceWrapper*>( |
| 502 WebDataServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 517 WebDataServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
| 503 profile_.get(), BuildMockWebDataServiceWrapper)); | 518 profile_, BuildMockWebDataServiceWrapper)); |
| 504 web_data_service_ = | 519 web_data_service_ = |
| 505 static_cast<WebDataServiceFake*>(wrapper->GetAutofillWebData().get()); | 520 static_cast<WebDataServiceFake*>(wrapper->GetAutofillWebData().get()); |
| 506 web_data_service_->SetDatabase(web_database_.get()); | 521 web_data_service_->SetDatabase(web_database_.get()); |
| 507 | 522 |
| 508 personal_data_manager_ = static_cast<MockPersonalDataManager*>( | 523 personal_data_manager_ = static_cast<MockPersonalDataManager*>( |
| 509 autofill::PersonalDataManagerFactory::GetInstance() | 524 autofill::PersonalDataManagerFactory::GetInstance() |
| 510 ->SetTestingFactoryAndUse(profile_.get(), | 525 ->SetTestingFactoryAndUse(profile_, |
| 511 MockPersonalDataManager::Build)); | 526 MockPersonalDataManager::Build)); |
| 512 | 527 |
| 513 EXPECT_CALL(*personal_data_manager_, LoadProfiles()).Times(1); | 528 EXPECT_CALL(*personal_data_manager_, LoadProfiles()).Times(1); |
| 514 EXPECT_CALL(*personal_data_manager_, LoadCreditCards()).Times(1); | 529 EXPECT_CALL(*personal_data_manager_, LoadCreditCards()).Times(1); |
| 515 | 530 |
| 516 personal_data_manager_->Init( | 531 personal_data_manager_->Init( |
| 517 WebDataServiceFactory::GetAutofillWebDataForProfile( | 532 WebDataServiceFactory::GetAutofillWebDataForProfile( |
| 518 profile_.get(), Profile::EXPLICIT_ACCESS), | 533 profile_, Profile::EXPLICIT_ACCESS), |
| 519 profile_->GetPrefs(), | 534 profile_->GetPrefs(), |
| 520 profile_->IsOffTheRecord()); | 535 profile_->IsOffTheRecord()); |
| 521 | 536 |
| 522 web_data_service_->StartSyncableService(); | 537 web_data_service_->StartSyncableService(); |
| 523 } | 538 } |
| 524 | 539 |
| 525 virtual void TearDown() OVERRIDE { | 540 virtual void TearDown() OVERRIDE { |
| 526 // Note: The tear down order is important. | 541 // Note: The tear down order is important. |
| 527 ProfileSyncServiceFactory::GetInstance()->SetTestingFactory( | 542 ProfileSyncServiceFactory::GetInstance()->SetTestingFactory(profile_, NULL); |
| 528 profile_.get(), NULL); | |
| 529 web_data_service_->ShutdownOnUIThread(); | 543 web_data_service_->ShutdownOnUIThread(); |
| 530 web_data_service_->ShutdownSyncableService(); | 544 web_data_service_->ShutdownSyncableService(); |
| 531 web_data_service_ = NULL; | 545 web_data_service_ = NULL; |
| 532 // To prevent a leak, fully release TestURLRequestContext to ensure its | 546 // To prevent a leak, fully release TestURLRequestContext to ensure its |
| 533 // destruction on the IO message loop. | 547 // destruction on the IO message loop. |
| 534 profile_.reset(); | 548 profile_ = NULL; |
| 549 profile_manager_.DeleteTestingProfile(kTestProfileName); |
| 535 AbstractProfileSyncServiceTest::TearDown(); | 550 AbstractProfileSyncServiceTest::TearDown(); |
| 536 } | 551 } |
| 537 | 552 |
| 538 | |
| 539 int GetSyncCount(syncer::ModelType type) { | 553 int GetSyncCount(syncer::ModelType type) { |
| 540 syncer::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare()); | 554 syncer::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare()); |
| 541 syncer::ReadNode node(&trans); | 555 syncer::ReadNode node(&trans); |
| 542 if (node.InitByTagLookup(syncer::ModelTypeToRootTag(type)) != | 556 if (node.InitByTagLookup(syncer::ModelTypeToRootTag(type)) != |
| 543 syncer::BaseNode::INIT_OK) | 557 syncer::BaseNode::INIT_OK) |
| 544 return 0; | 558 return 0; |
| 545 return node.GetTotalNodeCount() - 1; | 559 return node.GetTotalNodeCount() - 1; |
| 546 } | 560 } |
| 547 | 561 |
| 548 void StartSyncService(const base::Closure& callback, | 562 void StartSyncService(const base::Closure& callback, |
| 549 bool will_fail_association, | 563 bool will_fail_association, |
| 550 syncer::ModelType type) { | 564 syncer::ModelType type) { |
| 551 AbstractAutofillFactory* factory = GetFactory(type); | 565 AbstractAutofillFactory* factory = GetFactory(type); |
| 552 SigninManagerBase* signin = | 566 SigninManagerBase* signin = SigninManagerFactory::GetForProfile(profile_); |
| 553 SigninManagerFactory::GetForProfile(profile_.get()); | |
| 554 signin->SetAuthenticatedUsername("test_user@gmail.com"); | 567 signin->SetAuthenticatedUsername("test_user@gmail.com"); |
| 555 sync_service_ = TestProfileSyncService::BuildAutoStartAsyncInit( | 568 sync_service_ = TestProfileSyncService::BuildAutoStartAsyncInit(profile_, |
| 556 profile_.get(), callback); | 569 callback); |
| 557 | 570 |
| 558 ProfileSyncComponentsFactoryMock* components = | 571 ProfileSyncComponentsFactoryMock* components = |
| 559 sync_service_->components_factory_mock(); | 572 sync_service_->components_factory_mock(); |
| 560 DataTypeController* data_type_controller = | 573 DataTypeController* data_type_controller = |
| 561 factory->CreateDataTypeController(components, | 574 factory->CreateDataTypeController(components, profile_, sync_service_); |
| 562 profile_.get(), | |
| 563 sync_service_); | |
| 564 factory->SetExpectation(components, | 575 factory->SetExpectation(components, |
| 565 sync_service_, | 576 sync_service_, |
| 566 web_data_service_.get(), | 577 web_data_service_.get(), |
| 567 data_type_controller); | 578 data_type_controller); |
| 568 | 579 |
| 569 EXPECT_CALL(*components, CreateDataTypeManager(_, _, _, _, _, _)). | 580 EXPECT_CALL(*components, CreateDataTypeManager(_, _, _, _, _, _)). |
| 570 WillOnce(ReturnNewDataTypeManagerWithDebugListener( | 581 WillOnce(ReturnNewDataTypeManagerWithDebugListener( |
| 571 syncer::MakeWeakHandle(debug_ptr_factory_.GetWeakPtr()))); | 582 syncer::MakeWeakHandle(debug_ptr_factory_.GetWeakPtr()))); |
| 572 | 583 |
| 573 EXPECT_CALL(*personal_data_manager_, IsDataLoaded()). | 584 EXPECT_CALL(*personal_data_manager_, IsDataLoaded()). |
| 574 WillRepeatedly(Return(true)); | 585 WillRepeatedly(Return(true)); |
| 575 | 586 |
| 576 // We need tokens to get the tests going | 587 // We need tokens to get the tests going |
| 577 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_.get()) | 588 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_) |
| 578 ->UpdateCredentials("test_user@gmail.com", "oauth2_login_token"); | 589 ->UpdateCredentials("test_user@gmail.com", "oauth2_login_token"); |
| 579 | 590 |
| 580 sync_service_->RegisterDataTypeController(data_type_controller); | 591 sync_service_->RegisterDataTypeController(data_type_controller); |
| 581 sync_service_->Initialize(); | 592 sync_service_->Initialize(); |
| 582 base::MessageLoop::current()->Run(); | 593 base::MessageLoop::current()->Run(); |
| 583 | 594 |
| 584 // It's possible this test triggered an unrecoverable error, in which case | 595 // It's possible this test triggered an unrecoverable error, in which case |
| 585 // we can't get the sync count. | 596 // we can't get the sync count. |
| 586 if (sync_service_->ShouldPushChanges()) { | 597 if (sync_service_->ShouldPushChanges()) { |
| 587 EXPECT_EQ(GetSyncCount(type), | 598 EXPECT_EQ(GetSyncCount(type), |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 736 static AutofillEntry MakeAutofillEntry(const char* name, | 747 static AutofillEntry MakeAutofillEntry(const char* name, |
| 737 const char* value, | 748 const char* value, |
| 738 int time_shift) { | 749 int time_shift) { |
| 739 return MakeAutofillEntry(name, value, time_shift, -1); | 750 return MakeAutofillEntry(name, value, time_shift, -1); |
| 740 } | 751 } |
| 741 | 752 |
| 742 friend class AddAutofillHelper<AutofillEntry>; | 753 friend class AddAutofillHelper<AutofillEntry>; |
| 743 friend class AddAutofillHelper<AutofillProfile>; | 754 friend class AddAutofillHelper<AutofillProfile>; |
| 744 friend class FakeServerUpdater; | 755 friend class FakeServerUpdater; |
| 745 | 756 |
| 746 scoped_ptr<TestingProfile> profile_; | 757 TestingProfileManager profile_manager_; |
| 758 TestingProfile* profile_; |
| 747 AutofillTableMock autofill_table_; | 759 AutofillTableMock autofill_table_; |
| 748 scoped_ptr<WebDatabaseFake> web_database_; | 760 scoped_ptr<WebDatabaseFake> web_database_; |
| 749 scoped_refptr<WebDataServiceFake> web_data_service_; | 761 scoped_refptr<WebDataServiceFake> web_data_service_; |
| 750 MockPersonalDataManager* personal_data_manager_; | 762 MockPersonalDataManager* personal_data_manager_; |
| 751 syncer::DataTypeAssociationStats association_stats_; | 763 syncer::DataTypeAssociationStats association_stats_; |
| 752 base::WeakPtrFactory<DataTypeDebugInfoListener> debug_ptr_factory_; | 764 base::WeakPtrFactory<DataTypeDebugInfoListener> debug_ptr_factory_; |
| 753 }; | 765 }; |
| 754 | 766 |
| 755 template <class T> | 767 template <class T> |
| 756 class AddAutofillHelper { | 768 class AddAutofillHelper { |
| (...skipping 636 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1393 std::vector<AutofillEntry> sync_entries; | 1405 std::vector<AutofillEntry> sync_entries; |
| 1394 std::vector<AutofillProfile> sync_profiles; | 1406 std::vector<AutofillProfile> sync_profiles; |
| 1395 ASSERT_TRUE(GetAutofillEntriesFromSyncDB(&sync_entries, &sync_profiles)); | 1407 ASSERT_TRUE(GetAutofillEntriesFromSyncDB(&sync_entries, &sync_profiles)); |
| 1396 EXPECT_EQ(3U, sync_entries.size()); | 1408 EXPECT_EQ(3U, sync_entries.size()); |
| 1397 EXPECT_EQ(0U, sync_profiles.size()); | 1409 EXPECT_EQ(0U, sync_profiles.size()); |
| 1398 for (size_t i = 0; i < sync_entries.size(); i++) { | 1410 for (size_t i = 0; i < sync_entries.size(); i++) { |
| 1399 DVLOG(1) << "Entry " << i << ": " << sync_entries[i].key().name() | 1411 DVLOG(1) << "Entry " << i << ": " << sync_entries[i].key().name() |
| 1400 << ", " << sync_entries[i].key().value(); | 1412 << ", " << sync_entries[i].key().value(); |
| 1401 } | 1413 } |
| 1402 } | 1414 } |
| OLD | NEW |