OLD | NEW |
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 "chrome/browser/sync/test_profile_sync_service.h" | 5 #include "chrome/browser/sync/test_profile_sync_service.h" |
6 | 6 |
7 #include "chrome/browser/sync/abstract_profile_sync_service_test.h" | 7 #include "chrome/browser/sync/abstract_profile_sync_service_test.h" |
| 8 #include "chrome/browser/sync/engine/syncapi.h" |
| 9 #include "chrome/browser/sync/glue/data_type_controller.h" |
| 10 #include "chrome/browser/sync/glue/sync_backend_host.h" |
| 11 #include "chrome/browser/sync/profile_sync_factory.h" |
| 12 #include "chrome/browser/sync/sessions/session_state.h" |
| 13 #include "chrome/browser/sync/syncable/directory_manager.h" |
| 14 #include "chrome/browser/sync/syncable/syncable.h" |
| 15 #include "chrome/test/sync/test_http_bridge_factory.h" |
| 16 |
| 17 using browser_sync::ModelSafeRoutingInfo; |
| 18 using browser_sync::sessions::ErrorCounters; |
| 19 using browser_sync::sessions::SyncerStatus; |
| 20 using browser_sync::sessions::SyncSessionSnapshot; |
| 21 using syncable::DirectoryManager; |
| 22 using syncable::ModelType; |
| 23 using syncable::ScopedDirLookup; |
| 24 using sync_api::UserShare; |
| 25 |
| 26 ACTION_P(CallOnPaused, core) { |
| 27 core->OnPaused(); |
| 28 }; |
| 29 |
| 30 ACTION_P(CallOnResumed, core) { |
| 31 core->OnResumed(); |
| 32 } |
8 | 33 |
9 namespace browser_sync { | 34 namespace browser_sync { |
10 | 35 |
11 SyncBackendHostForProfileSyncTest::SyncBackendHostForProfileSyncTest( | 36 SyncBackendHostForProfileSyncTest::SyncBackendHostForProfileSyncTest( |
12 TestProfileSyncService* service, | |
13 Profile* profile, | 37 Profile* profile, |
14 Task* initial_condition_setup_task, | |
15 int num_expected_resumes, | 38 int num_expected_resumes, |
16 int num_expected_pauses, | 39 int num_expected_pauses, |
17 bool set_initial_sync_ended_on_init, | 40 bool set_initial_sync_ended_on_init, |
18 bool synchronous_init) | 41 bool synchronous_init) |
19 : browser_sync::SyncBackendHost(service, profile), | 42 : browser_sync::SyncBackendHost(profile), |
20 initial_condition_setup_task_(initial_condition_setup_task), | 43 synchronous_init_(synchronous_init) { |
21 set_initial_sync_ended_on_init_(set_initial_sync_ended_on_init), | |
22 synchronous_init_(synchronous_init), | |
23 test_service_(service) { | |
24 // By default, the RequestPause and RequestResume methods will | 44 // By default, the RequestPause and RequestResume methods will |
25 // send the confirmation notification and return true. | 45 // send the confirmation notification and return true. |
26 ON_CALL(*this, RequestPause()). | 46 ON_CALL(*this, RequestPause()). |
27 WillByDefault(testing::DoAll(CallOnPaused(core_), | 47 WillByDefault(testing::DoAll(CallOnPaused(core_), |
28 testing::Return(true))); | 48 testing::Return(true))); |
29 ON_CALL(*this, RequestResume()). | 49 ON_CALL(*this, RequestResume()). |
30 WillByDefault(testing::DoAll(CallOnResumed(core_), | 50 WillByDefault(testing::DoAll(CallOnResumed(core_), |
31 testing::Return(true))); | 51 testing::Return(true))); |
32 ON_CALL(*this, RequestNudge()).WillByDefault( | 52 ON_CALL(*this, RequestNudge()).WillByDefault( |
33 testing::Invoke(this, | 53 testing::Invoke(this, |
34 &SyncBackendHostForProfileSyncTest:: | 54 &SyncBackendHostForProfileSyncTest:: |
35 SimulateSyncCycleCompletedInitialSyncEnded)); | 55 SimulateSyncCycleCompletedInitialSyncEnded)); |
36 | 56 |
37 EXPECT_CALL(*this, RequestPause()).Times(num_expected_pauses); | 57 EXPECT_CALL(*this, RequestPause()).Times(num_expected_pauses); |
38 EXPECT_CALL(*this, RequestResume()).Times(num_expected_resumes); | 58 EXPECT_CALL(*this, RequestResume()).Times(num_expected_resumes); |
39 EXPECT_CALL(*this, | 59 EXPECT_CALL(*this, |
40 RequestNudge()).Times(set_initial_sync_ended_on_init ? 0 : 1); | 60 RequestNudge()).Times(set_initial_sync_ended_on_init ? 0 : 1); |
41 } | 61 } |
42 | 62 |
43 void | 63 void SyncBackendHostForProfileSyncTest::ConfigureDataTypes( |
44 SyncBackendHostForProfileSyncTest:: | 64 const DataTypeController::TypeMap& data_type_controllers, |
45 HandleInitializationCompletedOnFrontendLoop() { | 65 const syncable::ModelTypeSet& types, |
46 set_syncapi_initialized(); // Need to do this asap so task below works. | 66 CancelableTask* ready_task) { |
| 67 SetAutofillMigrationState(syncable::MIGRATED); |
| 68 SyncBackendHost::ConfigureDataTypes( |
| 69 data_type_controllers, types, ready_task); |
| 70 } |
| 71 |
| 72 void SyncBackendHostForProfileSyncTest:: |
| 73 SimulateSyncCycleCompletedInitialSyncEnded() { |
| 74 syncable::ModelTypeBitSet sync_ended; |
| 75 ModelSafeRoutingInfo enabled_types; |
| 76 GetModelSafeRoutingInfo(&enabled_types); |
| 77 std::string download_progress_markers[syncable::MODEL_TYPE_COUNT]; |
| 78 for (ModelSafeRoutingInfo::const_iterator i = enabled_types.begin(); |
| 79 i != enabled_types.end(); ++i) { |
| 80 sync_ended.set(i->first); |
| 81 } |
| 82 core_->HandleSyncCycleCompletedOnFrontendLoop(new SyncSessionSnapshot( |
| 83 SyncerStatus(), ErrorCounters(), 0, false, |
| 84 sync_ended, download_progress_markers, false, false, 0, 0, false)); |
| 85 } |
| 86 |
| 87 sync_api::HttpPostProviderFactory* |
| 88 SyncBackendHostForProfileSyncTest::MakeHttpBridgeFactory( |
| 89 URLRequestContextGetter* getter) { |
| 90 return new browser_sync::TestHttpBridgeFactory; |
| 91 } |
| 92 |
| 93 void SyncBackendHostForProfileSyncTest::InitCore( |
| 94 const Core::DoInitializeOptions& options) { |
| 95 std::wstring user = L"testuser"; |
| 96 core_loop()->PostTask( |
| 97 FROM_HERE, |
| 98 NewRunnableMethod(core_.get(), |
| 99 &SyncBackendHost::Core::DoInitializeForTest, |
| 100 user, |
| 101 options.http_bridge_factory, |
| 102 options.delete_sync_data_folder)); |
| 103 |
| 104 // TODO(akalin): Figure out a better way to do this. |
| 105 if (synchronous_init_) { |
| 106 // The SyncBackend posts a task to the current loop when |
| 107 // initialization completes. |
| 108 MessageLoop::current()->Run(); |
| 109 } |
| 110 } |
| 111 |
| 112 void SyncBackendHostForProfileSyncTest:: |
| 113 SetDefaultExpectationsForWorkerCreation(ProfileMock* profile) { |
| 114 EXPECT_CALL(*profile, GetPasswordStore(testing::_)). |
| 115 WillOnce(testing::Return((PasswordStore*)NULL)); |
| 116 } |
| 117 |
| 118 void SyncBackendHostForProfileSyncTest::SetHistoryServiceExpectations( |
| 119 ProfileMock* profile) { |
| 120 EXPECT_CALL(*profile, GetHistoryService(testing::_)). |
| 121 WillOnce(testing::Return((HistoryService*)NULL)); |
| 122 } |
| 123 |
| 124 } // namespace browser_sync |
| 125 |
| 126 browser_sync::TestIdFactory* TestProfileSyncService::id_factory() { |
| 127 return &id_factory_; |
| 128 } |
| 129 |
| 130 browser_sync::SyncBackendHostForProfileSyncTest* |
| 131 TestProfileSyncService::GetBackendForTest() { |
| 132 return static_cast<browser_sync::SyncBackendHostForProfileSyncTest*>( |
| 133 ProfileSyncService::GetBackendForTest()); |
| 134 } |
| 135 |
| 136 TestProfileSyncService::TestProfileSyncService( |
| 137 ProfileSyncFactory* factory, |
| 138 Profile* profile, |
| 139 const std::string& test_user, |
| 140 bool synchronous_backend_initialization, |
| 141 Task* initial_condition_setup_task) |
| 142 : ProfileSyncService(factory, profile, test_user), |
| 143 synchronous_backend_initialization_( |
| 144 synchronous_backend_initialization), |
| 145 synchronous_sync_configuration_(false), |
| 146 num_expected_resumes_(1), |
| 147 num_expected_pauses_(1), |
| 148 initial_condition_setup_task_(initial_condition_setup_task), |
| 149 set_initial_sync_ended_on_init_(true) { |
| 150 RegisterPreferences(); |
| 151 SetSyncSetupCompleted(); |
| 152 } |
| 153 |
| 154 TestProfileSyncService::~TestProfileSyncService() {} |
| 155 |
| 156 void TestProfileSyncService::SetInitialSyncEndedForEnabledTypes() { |
| 157 UserShare* user_share = GetUserShare(); |
| 158 DirectoryManager* dir_manager = user_share->dir_manager.get(); |
| 159 |
| 160 ScopedDirLookup dir(dir_manager, user_share->name); |
| 161 if (!dir.good()) |
| 162 FAIL(); |
| 163 |
| 164 ModelSafeRoutingInfo enabled_types; |
| 165 backend_->GetModelSafeRoutingInfo(&enabled_types); |
| 166 for (ModelSafeRoutingInfo::const_iterator i = enabled_types.begin(); |
| 167 i != enabled_types.end(); ++i) { |
| 168 dir->set_initial_sync_ended_for_type(i->first, true); |
| 169 } |
| 170 } |
| 171 |
| 172 void TestProfileSyncService::OnBackendInitialized() { |
| 173 // Set this so below code can access GetUserShare(). |
| 174 backend_initialized_ = true; |
47 | 175 |
48 // Set up any nodes the test wants around before model association. | 176 // Set up any nodes the test wants around before model association. |
49 if (initial_condition_setup_task_) { | 177 if (initial_condition_setup_task_) { |
50 initial_condition_setup_task_->Run(); | 178 initial_condition_setup_task_->Run(); |
| 179 initial_condition_setup_task_ = NULL; |
51 } | 180 } |
52 | 181 |
53 // Pretend we downloaded initial updates and set initial sync ended bits | 182 // Pretend we downloaded initial updates and set initial sync ended bits |
54 // if we were asked to. | 183 // if we were asked to. |
55 if (set_initial_sync_ended_on_init_) { | 184 if (set_initial_sync_ended_on_init_) { |
56 UserShare* user_share = core_->syncapi()->GetUserShare(); | 185 UserShare* user_share = GetUserShare(); |
57 DirectoryManager* dir_manager = user_share->dir_manager.get(); | 186 DirectoryManager* dir_manager = user_share->dir_manager.get(); |
58 | 187 |
59 ScopedDirLookup dir(dir_manager, user_share->name); | 188 ScopedDirLookup dir(dir_manager, user_share->name); |
60 if (!dir.good()) | 189 if (!dir.good()) |
61 FAIL(); | 190 FAIL(); |
62 | 191 |
63 if (!dir->initial_sync_ended_for_type(syncable::NIGORI)) { | 192 if (!dir->initial_sync_ended_for_type(syncable::NIGORI)) { |
64 ProfileSyncServiceTestHelper::CreateRoot( | 193 ProfileSyncServiceTestHelper::CreateRoot( |
65 syncable::NIGORI, test_service_, test_service_->id_factory()); | 194 syncable::NIGORI, GetUserShare(), |
| 195 id_factory()); |
66 } | 196 } |
67 | 197 |
68 SetInitialSyncEndedForEnabledTypes(); | 198 SetInitialSyncEndedForEnabledTypes(); |
69 } | 199 } |
70 | 200 |
71 SyncBackendHost::HandleInitializationCompletedOnFrontendLoop(); | 201 ProfileSyncService::OnBackendInitialized(); |
| 202 |
| 203 // TODO(akalin): Figure out a better way to do this. |
| 204 if (synchronous_backend_initialization_) { |
| 205 MessageLoop::current()->Quit(); |
| 206 } |
72 } | 207 } |
73 | 208 |
74 } // namespace browser_sync | 209 void TestProfileSyncService::Observe(NotificationType type, |
| 210 const NotificationSource& source, |
| 211 const NotificationDetails& details) { |
| 212 ProfileSyncService::Observe(type, source, details); |
| 213 if (type == NotificationType::SYNC_CONFIGURE_DONE && |
| 214 !synchronous_sync_configuration_) { |
| 215 MessageLoop::current()->Quit(); |
| 216 } |
| 217 } |
| 218 |
| 219 void TestProfileSyncService::set_num_expected_resumes(int times) { |
| 220 num_expected_resumes_ = times; |
| 221 } |
| 222 void TestProfileSyncService::set_num_expected_pauses(int num) { |
| 223 num_expected_pauses_ = num; |
| 224 } |
| 225 void TestProfileSyncService::dont_set_initial_sync_ended_on_init() { |
| 226 set_initial_sync_ended_on_init_ = false; |
| 227 } |
| 228 void TestProfileSyncService::set_synchronous_sync_configuration() { |
| 229 synchronous_sync_configuration_ = true; |
| 230 } |
75 | 231 |
76 void TestProfileSyncService::CreateBackend() { | 232 void TestProfileSyncService::CreateBackend() { |
77 backend_.reset(new browser_sync::SyncBackendHostForProfileSyncTest( | 233 backend_.reset(new browser_sync::SyncBackendHostForProfileSyncTest( |
78 this, profile(), | 234 profile(), |
79 initial_condition_setup_task_.release(), | |
80 num_expected_resumes_, num_expected_pauses_, | 235 num_expected_resumes_, num_expected_pauses_, |
81 set_initial_sync_ended_on_init_, | 236 set_initial_sync_ended_on_init_, |
82 synchronous_backend_initialization_)); | 237 synchronous_backend_initialization_)); |
83 } | 238 } |
| 239 |
| 240 std::string TestProfileSyncService::GetLsidForAuthBootstraping() { |
| 241 return "foo"; |
| 242 } |
OLD | NEW |