OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/sync/test_profile_sync_service.h" |
| 6 #include "chrome/browser/sync/abstract_profile_sync_service_test.h" |
| 7 |
| 8 namespace browser_sync { |
| 9 |
| 10 SyncBackendHostForProfileSyncTest::SyncBackendHostForProfileSyncTest( |
| 11 TestProfileSyncService* service, |
| 12 Profile* profile, |
| 13 const FilePath& profile_path, |
| 14 const DataTypeController::TypeMap& data_type_controllers, |
| 15 Task* initial_condition_setup_task, |
| 16 int num_expected_resumes, |
| 17 int num_expected_pauses, |
| 18 bool set_initial_sync_ended_on_init, |
| 19 bool synchronous_init) |
| 20 : browser_sync::SyncBackendHost(service, profile, profile_path, |
| 21 data_type_controllers), |
| 22 initial_condition_setup_task_(initial_condition_setup_task), |
| 23 set_initial_sync_ended_on_init_(set_initial_sync_ended_on_init), |
| 24 synchronous_init_(synchronous_init), |
| 25 test_service_(service) { |
| 26 // By default, the RequestPause and RequestResume methods will |
| 27 // send the confirmation notification and return true. |
| 28 ON_CALL(*this, RequestPause()). |
| 29 WillByDefault(testing::DoAll(CallOnPaused(core_), |
| 30 testing::Return(true))); |
| 31 ON_CALL(*this, RequestResume()). |
| 32 WillByDefault(testing::DoAll(CallOnResumed(core_), |
| 33 testing::Return(true))); |
| 34 ON_CALL(*this, RequestNudge()).WillByDefault(testing::Invoke(this, |
| 35 &SyncBackendHostForProfileSyncTest:: |
| 36 SimulateSyncCycleCompletedInitialSyncEnded)); |
| 37 |
| 38 EXPECT_CALL(*this, RequestPause()).Times(num_expected_pauses); |
| 39 EXPECT_CALL(*this, RequestResume()).Times(num_expected_resumes); |
| 40 EXPECT_CALL(*this, RequestNudge()). |
| 41 Times(set_initial_sync_ended_on_init ? 0 : 1); |
| 42 } |
| 43 |
| 44 void SyncBackendHostForProfileSyncTest::SetInitialSyncEndedForEnabledTypes() { |
| 45 UserShare* user_share = core_->syncapi()->GetUserShare(); |
| 46 DirectoryManager* dir_manager = user_share->dir_manager.get(); |
| 47 |
| 48 ScopedDirLookup dir(dir_manager, user_share->name); |
| 49 if (!dir.good()) |
| 50 FAIL(); |
| 51 |
| 52 ModelSafeRoutingInfo enabled_types; |
| 53 GetModelSafeRoutingInfo(&enabled_types); |
| 54 for (ModelSafeRoutingInfo::const_iterator i = enabled_types.begin(); |
| 55 i != enabled_types.end(); ++i) { |
| 56 dir->set_initial_sync_ended_for_type(i->first, true); |
| 57 } |
| 58 } |
| 59 |
| 60 void SyncBackendHostForProfileSyncTest:: |
| 61 HandleInitializationCompletedOnFrontendLoop() { |
| 62 set_syncapi_initialized(); // Need to do this asap so task below works. |
| 63 |
| 64 // Set up any nodes the test wants around before model association. |
| 65 if (initial_condition_setup_task_) { |
| 66 initial_condition_setup_task_->Run(); |
| 67 } |
| 68 |
| 69 // Pretend we downloaded initial updates and set initial sync ended bits |
| 70 // if we were asked to. |
| 71 if (set_initial_sync_ended_on_init_) { |
| 72 UserShare* user_share = core_->syncapi()->GetUserShare(); |
| 73 DirectoryManager* dir_manager = user_share->dir_manager.get(); |
| 74 |
| 75 ScopedDirLookup dir(dir_manager, user_share->name); |
| 76 if (!dir.good()) |
| 77 FAIL(); |
| 78 |
| 79 // Do this check as some tests load from storage and we don't want to add |
| 80 // twice. |
| 81 // TODO(tim): This is madness. Too much test setup code! |
| 82 if (!dir->initial_sync_ended_for_type(syncable::NIGORI)) { |
| 83 ProfileSyncServiceTestHelper::CreateRoot( |
| 84 syncable::NIGORI, test_service_, test_service_->id_factory()); |
| 85 } |
| 86 |
| 87 SetInitialSyncEndedForEnabledTypes(); |
| 88 } |
| 89 |
| 90 SyncBackendHost::HandleInitializationCompletedOnFrontendLoop(); |
| 91 } |
| 92 |
| 93 // Called when a nudge comes in. |
| 94 void SyncBackendHostForProfileSyncTest:: |
| 95 SimulateSyncCycleCompletedInitialSyncEnded() { |
| 96 syncable::ModelTypeBitSet sync_ended; |
| 97 ModelSafeRoutingInfo enabled_types; |
| 98 GetModelSafeRoutingInfo(&enabled_types); |
| 99 for (ModelSafeRoutingInfo::const_iterator i = enabled_types.begin(); |
| 100 i != enabled_types.end(); ++i) { |
| 101 sync_ended.set(i->first); |
| 102 } |
| 103 core_->HandleSyncCycleCompletedOnFrontendLoop(new SyncSessionSnapshot( |
| 104 SyncerStatus(), ErrorCounters(), 0, 0, false, |
| 105 sync_ended, false, false, 0, 0, false)); |
| 106 } |
| 107 |
| 108 void SyncBackendHostForProfileSyncTest::InitCore( |
| 109 const Core::DoInitializeOptions& options) { |
| 110 std::wstring user = L"testuser"; |
| 111 core_loop()->PostTask(FROM_HERE, |
| 112 NewRunnableMethod(core_.get(), |
| 113 &SyncBackendHost::Core::DoInitializeForTest, |
| 114 user, |
| 115 options.http_bridge_factory, |
| 116 options.delete_sync_data_folder)); |
| 117 |
| 118 // TODO(akalin): Figure out a better way to do this. |
| 119 if (synchronous_init_) { |
| 120 // The SyncBackend posts a task to the current loop when |
| 121 // initialization completes. |
| 122 MessageLoop::current()->Run(); |
| 123 } |
| 124 } |
| 125 |
| 126 void SyncBackendHostForProfileSyncTest:: |
| 127 SetDefaultExpectationsForWorkerCreation(ProfileMock* profile) { |
| 128 EXPECT_CALL(*profile, GetPasswordStore(testing::_)). |
| 129 WillOnce(testing::Return((PasswordStore*)NULL)); |
| 130 EXPECT_CALL(*profile, GetHistoryService(testing::_)). |
| 131 WillOnce(testing::Return((HistoryService*)NULL)); |
| 132 } |
| 133 |
| 134 } // namespace browser_sync |
| 135 |
| 136 TestProfileSyncService::TestProfileSyncService(ProfileSyncFactory* factory, |
| 137 Profile* profile, |
| 138 const std::string& test_user, |
| 139 bool synchronous_backend_initialization, |
| 140 Task* initial_condition_setup_task) |
| 141 : ProfileSyncService(factory, profile, |
| 142 !test_user.empty() ? |
| 143 test_user : ""), |
| 144 synchronous_backend_initialization_( |
| 145 synchronous_backend_initialization), |
| 146 synchronous_sync_configuration_(false), |
| 147 num_expected_resumes_(1), |
| 148 num_expected_pauses_(1), |
| 149 initial_condition_setup_task_(initial_condition_setup_task), |
| 150 set_initial_sync_ended_on_init_(true) { |
| 151 RegisterPreferences(); |
| 152 SetSyncSetupCompleted(); |
| 153 } |
| 154 |
| 155 void TestProfileSyncService::CreateBackend() { |
| 156 backend_.reset(new browser_sync::SyncBackendHostForProfileSyncTest( |
| 157 this, profile(), |
| 158 profile()->GetPath(), data_type_controllers(), |
| 159 initial_condition_setup_task_.release(), |
| 160 num_expected_resumes_, num_expected_pauses_, |
| 161 set_initial_sync_ended_on_init_, |
| 162 synchronous_backend_initialization_)); |
| 163 } |
| 164 |
| 165 void TestProfileSyncService::OnBackendInitialized() { |
| 166 ProfileSyncService::OnBackendInitialized(); |
| 167 // TODO(akalin): Figure out a better way to do this. |
| 168 if (synchronous_backend_initialization_) { |
| 169 MessageLoop::current()->Quit(); |
| 170 } |
| 171 } |
| 172 |
| 173 void TestProfileSyncService::Observe(NotificationType type, |
| 174 const NotificationSource& source, |
| 175 const NotificationDetails& details) { |
| 176 ProfileSyncService::Observe(type, source, details); |
| 177 if (type == NotificationType::SYNC_CONFIGURE_DONE && |
| 178 !synchronous_sync_configuration_) { |
| 179 MessageLoop::current()->Quit(); |
| 180 } |
| 181 } |
OLD | NEW |