Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 "testing/gtest/include/gtest/gtest.h" | 5 #include "base/file_util.h" |
| 6 | |
| 7 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/prefs/pref_service.h" | 7 #include "base/prefs/pref_service.h" |
| 9 #include "chrome/browser/chrome_notification_types.h" | 8 #include "chrome/browser/chrome_notification_types.h" |
| 10 #include "chrome/browser/signin/fake_signin_manager.h" | 9 #include "chrome/browser/signin/fake_signin_manager.h" |
| 11 #include "chrome/browser/signin/profile_oauth2_token_service.h" | 10 #include "chrome/browser/signin/profile_oauth2_token_service.h" |
| 12 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 11 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 13 #include "chrome/browser/signin/signin_manager.h" | 12 #include "chrome/browser/signin/signin_manager.h" |
| 14 #include "chrome/browser/signin/signin_manager_factory.h" | 13 #include "chrome/browser/signin/signin_manager_factory.h" |
| 15 #include "chrome/browser/signin/token_service.h" | 14 #include "chrome/browser/signin/token_service.h" |
| 16 #include "chrome/browser/signin/token_service_factory.h" | 15 #include "chrome/browser/signin/token_service_factory.h" |
| 17 #include "chrome/browser/sync/fake_oauth2_token_service.h" | 16 #include "chrome/browser/sync/fake_oauth2_token_service.h" |
| 18 #include "chrome/browser/sync/glue/data_type_manager.h" | 17 #include "chrome/browser/sync/glue/data_type_manager.h" |
| 19 #include "chrome/browser/sync/glue/data_type_manager_mock.h" | 18 #include "chrome/browser/sync/glue/data_type_manager_mock.h" |
| 20 #include "chrome/browser/sync/glue/sync_backend_host_mock.h" | 19 #include "chrome/browser/sync/glue/sync_backend_host_mock.h" |
| 21 #include "chrome/browser/sync/profile_sync_components_factory_mock.h" | 20 #include "chrome/browser/sync/profile_sync_components_factory_mock.h" |
| 22 #include "chrome/browser/sync/profile_sync_service_factory.h" | 21 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 23 #include "chrome/browser/sync/profile_sync_test_util.h" | 22 #include "chrome/browser/sync/profile_sync_test_util.h" |
| 24 #include "chrome/browser/sync/sync_prefs.h" | 23 #include "chrome/browser/sync/sync_prefs.h" |
| 25 #include "chrome/common/pref_names.h" | 24 #include "chrome/common/pref_names.h" |
| 26 #include "chrome/test/base/testing_profile.h" | 25 #include "chrome/test/base/testing_profile.h" |
| 27 #include "content/public/browser/notification_service.h" | 26 #include "content/public/browser/notification_service.h" |
| 28 #include "content/public/browser/notification_source.h" | 27 #include "content/public/browser/notification_source.h" |
| 29 #include "content/public/test/test_browser_thread_bundle.h" | 28 #include "content/public/test/test_browser_thread_bundle.h" |
| 30 #include "content/public/test/test_utils.h" | 29 #include "content/public/test/test_utils.h" |
| 31 #include "google_apis/gaia/gaia_auth_consumer.h" | 30 #include "google_apis/gaia/gaia_auth_consumer.h" |
| 32 #include "google_apis/gaia/gaia_constants.h" | 31 #include "google_apis/gaia/gaia_constants.h" |
| 33 #include "google_apis/gaia/oauth2_token_service.h" | 32 #include "google_apis/gaia/oauth2_token_service.h" |
| 34 #include "testing/gmock/include/gmock/gmock.h" | 33 #include "testing/gmock/include/gmock/gmock.h" |
| 34 #include "testing/gtest/include/gtest/gtest.h" | |
| 35 | 35 |
| 36 using browser_sync::DataTypeManager; | 36 using browser_sync::DataTypeManager; |
| 37 using browser_sync::DataTypeManagerMock; | 37 using browser_sync::DataTypeManagerMock; |
| 38 using browser_sync::SyncBackendHostMock; | 38 using browser_sync::SyncBackendHostMock; |
| 39 using content::BrowserThread; | 39 using content::BrowserThread; |
| 40 using testing::_; | 40 using testing::_; |
| 41 using testing::AnyNumber; | 41 using testing::AnyNumber; |
| 42 using testing::DoAll; | 42 using testing::DoAll; |
| 43 using testing::InvokeArgument; | 43 using testing::InvokeArgument; |
| 44 using testing::Mock; | 44 using testing::Mock; |
| (...skipping 508 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 553 profile_->GetPrefs()->ClearPref(prefs::kSyncHasSetupCompleted); | 553 profile_->GetPrefs()->ClearPref(prefs::kSyncHasSetupCompleted); |
| 554 | 554 |
| 555 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); | 555 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); |
| 556 IssueTestTokens(); | 556 IssueTestTokens(); |
| 557 | 557 |
| 558 sync_->SetSetupInProgress(true); | 558 sync_->SetSetupInProgress(true); |
| 559 sync_->Initialize(); | 559 sync_->Initialize(); |
| 560 sync_->SetSetupInProgress(false); | 560 sync_->SetSetupInProgress(false); |
| 561 EXPECT_FALSE(sync_->sync_initialized()); | 561 EXPECT_FALSE(sync_->sync_initialized()); |
| 562 } | 562 } |
| 563 | |
| 564 // Make sure that if sync is not configured yet but there are still files | |
| 565 // lingering in "Sync Data" folder then sync cleans folder and initializes | |
| 566 // successfully. | |
| 567 TEST_F(ProfileSyncServiceStartupTest, TestStartupWithOldSyncData) { | |
|
rlarocque
2013/11/11 22:30:46
I don't understand how this works.
The ProfileSyn
| |
| 568 const char* nonsense1 = "reginald"; | |
| 569 const char* nonsense2 = "beartato"; | |
| 570 const char* nonsense3 = "harrison"; | |
| 571 base::FilePath temp_directory = | |
| 572 profile_->GetPath().AppendASCII("Sync Data"); | |
| 573 base::FilePath sync_file1 = | |
| 574 temp_directory.AppendASCII("BookmarkSyncSettings.sqlite3"); | |
| 575 base::FilePath sync_file2 = temp_directory.AppendASCII("SyncData.sqlite3"); | |
| 576 base::FilePath sync_file3 = temp_directory.AppendASCII("nonsense_file"); | |
| 577 ASSERT_TRUE(file_util::CreateDirectory(temp_directory)); | |
| 578 ASSERT_NE(-1, | |
| 579 file_util::WriteFile(sync_file1, nonsense1, strlen(nonsense1))); | |
| 580 ASSERT_NE(-1, | |
| 581 file_util::WriteFile(sync_file2, nonsense2, strlen(nonsense2))); | |
| 582 ASSERT_NE(-1, | |
| 583 file_util::WriteFile(sync_file3, nonsense3, strlen(nonsense3))); | |
| 584 | |
| 585 // Pre load the tokens | |
| 586 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, | |
| 587 "test_user@gmail.com"); | |
| 588 SigninManagerFactory::GetForProfile(profile_.get())->Initialize( | |
| 589 profile_.get(), NULL); | |
| 590 CreateSyncService(); | |
| 591 DataTypeManagerMock* data_type_manager = SetUpDataTypeManager(); | |
| 592 EXPECT_CALL(*data_type_manager, Configure(_, _)); | |
| 593 EXPECT_CALL(*data_type_manager, state()). | |
| 594 WillRepeatedly(Return(DataTypeManager::CONFIGURED)); | |
| 595 EXPECT_CALL(*data_type_manager, Stop()).Times(1); | |
| 596 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); | |
| 597 | |
| 598 profile_->GetPrefs()->ClearPref(prefs::kSyncHasSetupCompleted); | |
| 599 sync_->set_storage_option(syncer::STORAGE_ON_DISK); | |
| 600 sync_->SetSetupInProgress(true); | |
| 601 | |
| 602 IssueTestTokens(); | |
| 603 | |
| 604 sync_->Initialize(); | |
| 605 | |
| 606 sync_->SetSetupInProgress(false); | |
| 607 | |
| 608 EXPECT_TRUE(sync_->sync_initialized()); | |
| 609 | |
| 610 // This file should have been deleted when the whole directory was nuked. | |
| 611 EXPECT_FALSE(base::PathExists(sync_file3)); | |
| 612 EXPECT_FALSE(base::PathExists(sync_file1)); | |
| 613 | |
| 614 // This will still exist, but the text should have changed. | |
| 615 EXPECT_TRUE(base::PathExists(sync_file2)); | |
| 616 std::string file2text; | |
| 617 EXPECT_TRUE(base::ReadFileToString(sync_file2, &file2text)); | |
| 618 EXPECT_NE(file2text.compare(nonsense2), 0); | |
| 619 | |
| 620 } | |
| 621 | |
| 622 // Simulates a scenario where a database is corrupted and it is impossible to | |
| 623 // recreate it. This test is useful mainly when it is run under valgrind. Its | |
| 624 // expectations are not very interesting. | |
| 625 TEST_F(ProfileSyncServiceStartupTest, FailToOpenDatabase) { | |
| 626 // Pre load the tokens | |
| 627 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, | |
| 628 "test_user@gmail.com"); | |
| 629 SigninManagerFactory::GetForProfile(profile_.get())->Initialize( | |
| 630 profile_.get(), NULL); | |
| 631 CreateSyncService(); | |
| 632 sync_->set_storage_option(syncer::STORAGE_INVALID); | |
|
rlarocque
2013/11/11 22:30:46
I think your patch is a bit behind trunk. sync_ i
| |
| 633 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); | |
| 634 | |
| 635 IssueTestTokens(); | |
| 636 | |
| 637 sync_->Initialize(); | |
| 638 | |
| 639 EXPECT_FALSE(sync_->sync_initialized()); | |
| 640 } | |
| 641 | |
| 642 // This setup will allow the database to exist, but leave it empty. The attempt | |
| 643 // to download control types will silently fail (no downloads have any effect in | |
| 644 // these tests). The sync_backend_host will notice this and inform the profile | |
| 645 // sync service of the failure to initialize the backed. | |
| 646 TEST_F(ProfileSyncServiceStartupTest, FailToDownloadControlTypes) { | |
| 647 // Pre load the tokens | |
| 648 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, | |
| 649 "test_user@gmail.com"); | |
| 650 SigninManagerFactory::GetForProfile(profile_.get())->Initialize( | |
| 651 profile_.get(), NULL); | |
| 652 CreateSyncService(); | |
| 653 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); | |
| 654 | |
| 655 sync_->dont_set_initial_sync_ended_on_init(); | |
| 656 IssueTestTokens(); | |
| 657 | |
| 658 sync_->Initialize(); | |
| 659 | |
| 660 EXPECT_FALSE(sync_->sync_initialized()); | |
| 661 } | |
| OLD | NEW |