| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef CHROME_BROWSER_SYNC_TEST_PROFILE_SYNC_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_SYNC_TEST_PROFILE_SYNC_SERVICE_H_ |
| 6 #define CHROME_BROWSER_SYNC_TEST_PROFILE_SYNC_SERVICE_H_ | 6 #define CHROME_BROWSER_SYNC_TEST_PROFILE_SYNC_SERVICE_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/message_loop.h" | 11 #include "chrome/browser/sync/glue/data_type_manager_impl.h" |
| 12 #include "chrome/browser/sync/engine/syncapi.h" | |
| 13 #include "chrome/browser/sync/profile_sync_factory.h" | |
| 14 #include "chrome/browser/sync/profile_sync_service.h" | 12 #include "chrome/browser/sync/profile_sync_service.h" |
| 15 #include "chrome/browser/sync/glue/data_type_controller.h" | |
| 16 #include "chrome/browser/sync/glue/data_type_manager_impl.h" | |
| 17 #include "chrome/browser/sync/glue/sync_backend_host.h" | |
| 18 #include "chrome/browser/sync/sessions/session_state.h" | |
| 19 #include "chrome/browser/sync/syncable/directory_manager.h" | |
| 20 #include "chrome/browser/sync/syncable/syncable.h" | |
| 21 #include "chrome/test/profile_mock.h" | 13 #include "chrome/test/profile_mock.h" |
| 22 #include "chrome/test/sync/test_http_bridge_factory.h" | |
| 23 #include "chrome/test/sync/engine/test_id_factory.h" | 14 #include "chrome/test/sync/engine/test_id_factory.h" |
| 24 #include "testing/gmock/include/gmock/gmock.h" | 15 #include "testing/gmock/include/gmock/gmock.h" |
| 25 | 16 |
| 26 class Profile; | 17 class Profile; |
| 18 class Task; |
| 27 class TestProfileSyncService; | 19 class TestProfileSyncService; |
| 28 | 20 |
| 29 using browser_sync::ModelSafeRoutingInfo; | |
| 30 using browser_sync::sessions::ErrorCounters; | |
| 31 using browser_sync::sessions::SyncerStatus; | |
| 32 using browser_sync::sessions::SyncSessionSnapshot; | |
| 33 using sync_api::UserShare; | |
| 34 using syncable::DirectoryManager; | |
| 35 using syncable::ModelType; | |
| 36 using syncable::ScopedDirLookup; | |
| 37 | |
| 38 ACTION_P(CallOnPaused, core) { | |
| 39 core->OnPaused(); | |
| 40 }; | |
| 41 | |
| 42 ACTION_P(CallOnResumed, core) { | |
| 43 core->OnResumed(); | |
| 44 } | |
| 45 | |
| 46 ACTION(ReturnNewDataTypeManager) { | 21 ACTION(ReturnNewDataTypeManager) { |
| 47 return new browser_sync::DataTypeManagerImpl(arg0, arg1); | 22 return new browser_sync::DataTypeManagerImpl(arg0, arg1); |
| 48 } | 23 } |
| 49 | 24 |
| 50 namespace browser_sync { | 25 namespace browser_sync { |
| 51 | 26 |
| 52 // Mocks out the SyncerThread operations (Pause/Resume) since no thread is | 27 // Mocks out the SyncerThread operations (Pause/Resume) since no thread is |
| 53 // running in these tests, and allows tests to provide a task on construction | 28 // running in these tests, and allows tests to provide a task on construction |
| 54 // to set up initial nodes to mock out an actual server initial sync | 29 // to set up initial nodes to mock out an actual server initial sync |
| 55 // download. | 30 // download. |
| 56 class SyncBackendHostForProfileSyncTest : public SyncBackendHost { | 31 class SyncBackendHostForProfileSyncTest : public SyncBackendHost { |
| 57 public: | 32 public: |
| 58 // |initial_condition_setup_task| can be used to populate nodes before the | |
| 59 // OnBackendInitialized callback fires. | |
| 60 // |set_initial_sync_ended_on_init| determines whether we pretend that a full | |
| 61 // initial download has occurred and set bits for enabled data types. If | |
| 62 // this is false, configuring data types will require a syncer nudge. | |
| 63 // |synchronous_init| causes initialization to block until the syncapi has | 33 // |synchronous_init| causes initialization to block until the syncapi has |
| 64 // completed setting itself up and called us back. | 34 // completed setting itself up and called us back. |
| 65 SyncBackendHostForProfileSyncTest( | 35 SyncBackendHostForProfileSyncTest( |
| 66 TestProfileSyncService* service, | |
| 67 Profile* profile, | 36 Profile* profile, |
| 68 Task* initial_condition_setup_task, | |
| 69 int num_expected_resumes, | 37 int num_expected_resumes, |
| 70 int num_expected_pauses, | 38 int num_expected_pauses, |
| 71 bool set_initial_sync_ended_on_init, | 39 bool set_initial_sync_ended_on_init, |
| 72 bool synchronous_init); | 40 bool synchronous_init); |
| 73 | 41 |
| 74 MOCK_METHOD0(RequestPause, bool()); | 42 MOCK_METHOD0(RequestPause, bool()); |
| 75 MOCK_METHOD0(RequestResume, bool()); | 43 MOCK_METHOD0(RequestResume, bool()); |
| 76 MOCK_METHOD0(RequestNudge, void()); | 44 MOCK_METHOD0(RequestNudge, void()); |
| 77 | 45 |
| 78 void SetInitialSyncEndedForEnabledTypes() { | |
| 79 UserShare* user_share = core_->syncapi()->GetUserShare(); | |
| 80 DirectoryManager* dir_manager = user_share->dir_manager.get(); | |
| 81 | |
| 82 ScopedDirLookup dir(dir_manager, user_share->name); | |
| 83 if (!dir.good()) | |
| 84 FAIL(); | |
| 85 | |
| 86 ModelSafeRoutingInfo enabled_types; | |
| 87 GetModelSafeRoutingInfo(&enabled_types); | |
| 88 for (ModelSafeRoutingInfo::const_iterator i = enabled_types.begin(); | |
| 89 i != enabled_types.end(); ++i) { | |
| 90 dir->set_initial_sync_ended_for_type(i->first, true); | |
| 91 } | |
| 92 } | |
| 93 | |
| 94 virtual void ConfigureDataTypes( | 46 virtual void ConfigureDataTypes( |
| 95 const DataTypeController::TypeMap& data_type_controllers, | 47 const DataTypeController::TypeMap& data_type_controllers, |
| 96 const syncable::ModelTypeSet& types, | 48 const syncable::ModelTypeSet& types, |
| 97 CancelableTask* ready_task) { | 49 CancelableTask* ready_task); |
| 98 SetAutofillMigrationState(syncable::MIGRATED); | |
| 99 SyncBackendHost::ConfigureDataTypes( | |
| 100 data_type_controllers, types, ready_task); | |
| 101 } | |
| 102 | 50 |
| 103 // Called when a nudge comes in. | 51 // Called when a nudge comes in. |
| 104 void SimulateSyncCycleCompletedInitialSyncEnded() { | 52 void SimulateSyncCycleCompletedInitialSyncEnded(); |
| 105 syncable::ModelTypeBitSet sync_ended; | |
| 106 ModelSafeRoutingInfo enabled_types; | |
| 107 GetModelSafeRoutingInfo(&enabled_types); | |
| 108 std::string download_progress_markers[syncable::MODEL_TYPE_COUNT]; | |
| 109 for (ModelSafeRoutingInfo::const_iterator i = enabled_types.begin(); | |
| 110 i != enabled_types.end(); ++i) { | |
| 111 sync_ended.set(i->first); | |
| 112 } | |
| 113 core_->HandleSyncCycleCompletedOnFrontendLoop(new SyncSessionSnapshot( | |
| 114 SyncerStatus(), ErrorCounters(), 0, false, | |
| 115 sync_ended, download_progress_markers, false, false, 0, 0, false)); | |
| 116 } | |
| 117 | |
| 118 virtual void HandleInitializationCompletedOnFrontendLoop(); | |
| 119 | 53 |
| 120 virtual sync_api::HttpPostProviderFactory* MakeHttpBridgeFactory( | 54 virtual sync_api::HttpPostProviderFactory* MakeHttpBridgeFactory( |
| 121 URLRequestContextGetter* getter) { | 55 URLRequestContextGetter* getter); |
| 122 return new browser_sync::TestHttpBridgeFactory; | |
| 123 } | |
| 124 | 56 |
| 125 virtual void InitCore(const Core::DoInitializeOptions& options) { | 57 virtual void InitCore(const Core::DoInitializeOptions& options); |
| 126 std::wstring user = L"testuser"; | |
| 127 core_loop()->PostTask(FROM_HERE, | |
| 128 NewRunnableMethod(core_.get(), | |
| 129 &SyncBackendHost::Core::DoInitializeForTest, | |
| 130 user, | |
| 131 options.http_bridge_factory, | |
| 132 options.delete_sync_data_folder)); | |
| 133 | 58 |
| 134 // TODO(akalin): Figure out a better way to do this. | 59 static void SetDefaultExpectationsForWorkerCreation(ProfileMock* profile); |
| 135 if (synchronous_init_) { | |
| 136 // The SyncBackend posts a task to the current loop when | |
| 137 // initialization completes. | |
| 138 MessageLoop::current()->Run(); | |
| 139 } | |
| 140 } | |
| 141 | 60 |
| 142 static void SetDefaultExpectationsForWorkerCreation(ProfileMock* profile) { | 61 static void SetHistoryServiceExpectations(ProfileMock* profile); |
| 143 EXPECT_CALL(*profile, GetPasswordStore(testing::_)). | |
| 144 WillOnce(testing::Return((PasswordStore*)NULL)); | |
| 145 } | |
| 146 | |
| 147 static void SetHistoryServiceExpectations(ProfileMock* profile) { | |
| 148 EXPECT_CALL(*profile, GetHistoryService(testing::_)). | |
| 149 WillOnce(testing::Return((HistoryService*)NULL)); | |
| 150 } | |
| 151 | 62 |
| 152 private: | 63 private: |
| 153 Task* initial_condition_setup_task_; | |
| 154 bool set_initial_sync_ended_on_init_; | |
| 155 bool synchronous_init_; | 64 bool synchronous_init_; |
| 156 TestProfileSyncService* test_service_; | |
| 157 }; | 65 }; |
| 158 | 66 |
| 159 } // namespace browser_sync | 67 } // namespace browser_sync |
| 160 | 68 |
| 161 class TestProfileSyncService : public ProfileSyncService { | 69 class TestProfileSyncService : public ProfileSyncService { |
| 162 public: | 70 public: |
| 71 // |initial_condition_setup_task| can be used to populate nodes |
| 72 // before the OnBackendInitialized callback fires. |
| 163 TestProfileSyncService(ProfileSyncFactory* factory, | 73 TestProfileSyncService(ProfileSyncFactory* factory, |
| 164 Profile* profile, | 74 Profile* profile, |
| 165 const std::string& test_user, | 75 const std::string& test_user, |
| 166 bool synchronous_backend_initialization, | 76 bool synchronous_backend_initialization, |
| 167 Task* initial_condition_setup_task) | 77 Task* initial_condition_setup_task); |
| 168 : ProfileSyncService(factory, profile, test_user), | |
| 169 synchronous_backend_initialization_( | |
| 170 synchronous_backend_initialization), | |
| 171 synchronous_sync_configuration_(false), | |
| 172 num_expected_resumes_(1), | |
| 173 num_expected_pauses_(1), | |
| 174 initial_condition_setup_task_(initial_condition_setup_task), | |
| 175 set_initial_sync_ended_on_init_(true) { | |
| 176 RegisterPreferences(); | |
| 177 SetSyncSetupCompleted(); | |
| 178 } | |
| 179 virtual ~TestProfileSyncService() { } | |
| 180 | 78 |
| 181 virtual void OnBackendInitialized() { | 79 virtual ~TestProfileSyncService(); |
| 182 ProfileSyncService::OnBackendInitialized(); | 80 |
| 183 // TODO(akalin): Figure out a better way to do this. | 81 void SetInitialSyncEndedForEnabledTypes(); |
| 184 if (synchronous_backend_initialization_) { | 82 |
| 185 MessageLoop::current()->Quit(); | 83 virtual void OnBackendInitialized(); |
| 186 } | |
| 187 } | |
| 188 | 84 |
| 189 virtual void Observe(NotificationType type, | 85 virtual void Observe(NotificationType type, |
| 190 const NotificationSource& source, | 86 const NotificationSource& source, |
| 191 const NotificationDetails& details) { | 87 const NotificationDetails& details); |
| 192 ProfileSyncService::Observe(type, source, details); | |
| 193 if (type == NotificationType::SYNC_CONFIGURE_DONE && | |
| 194 !synchronous_sync_configuration_) { | |
| 195 MessageLoop::current()->Quit(); | |
| 196 } | |
| 197 } | |
| 198 | 88 |
| 199 void set_num_expected_resumes(int times) { | 89 void set_num_expected_resumes(int times); |
| 200 num_expected_resumes_ = times; | 90 void set_num_expected_pauses(int num); |
| 201 } | |
| 202 void set_num_expected_pauses(int num) { | |
| 203 num_expected_pauses_ = num; | |
| 204 } | |
| 205 void dont_set_initial_sync_ended_on_init() { | |
| 206 set_initial_sync_ended_on_init_ = false; | |
| 207 } | |
| 208 void set_synchronous_sync_configuration() { | |
| 209 synchronous_sync_configuration_ = true; | |
| 210 } | |
| 211 | 91 |
| 212 browser_sync::TestIdFactory* id_factory() { return &id_factory_; } | 92 // If this is called, configuring data types will require a syncer |
| 93 // nudge. |
| 94 void dont_set_initial_sync_ended_on_init(); |
| 95 void set_synchronous_sync_configuration(); |
| 96 |
| 97 browser_sync::TestIdFactory* id_factory(); |
| 98 |
| 99 // Override of ProfileSyncService::GetBackendForTest() with a more |
| 100 // specific return type (since C++ supports covariant return types) |
| 101 // that is made public. |
| 102 virtual browser_sync::SyncBackendHostForProfileSyncTest* |
| 103 GetBackendForTest(); |
| 213 | 104 |
| 214 protected: | 105 protected: |
| 215 virtual void CreateBackend(); | 106 virtual void CreateBackend(); |
| 216 | 107 |
| 217 private: | 108 private: |
| 218 // When testing under ChromiumOS, this method must not return an empty | 109 // When testing under ChromiumOS, this method must not return an empty |
| 219 // value value in order for the profile sync service to start. | 110 // value value in order for the profile sync service to start. |
| 220 virtual std::string GetLsidForAuthBootstraping() { | 111 virtual std::string GetLsidForAuthBootstraping(); |
| 221 return "foo"; | 112 |
| 222 } | 113 browser_sync::TestIdFactory id_factory_; |
| 223 | 114 |
| 224 bool synchronous_backend_initialization_; | 115 bool synchronous_backend_initialization_; |
| 225 | 116 |
| 226 // Set to true when a mock data type manager is being used and the configure | 117 // Set to true when a mock data type manager is being used and the configure |
| 227 // step is performed synchronously. | 118 // step is performed synchronously. |
| 228 bool synchronous_sync_configuration_; | 119 bool synchronous_sync_configuration_; |
| 229 bool set_expect_resume_expectations_; | 120 bool set_expect_resume_expectations_; |
| 230 int num_expected_resumes_; | 121 int num_expected_resumes_; |
| 231 int num_expected_pauses_; | 122 int num_expected_pauses_; |
| 232 | 123 |
| 233 scoped_ptr<Task> initial_condition_setup_task_; | 124 Task* initial_condition_setup_task_; |
| 234 bool set_initial_sync_ended_on_init_; | 125 bool set_initial_sync_ended_on_init_; |
| 235 browser_sync::TestIdFactory id_factory_; | |
| 236 }; | 126 }; |
| 237 | 127 |
| 238 | 128 |
| 239 | 129 |
| 240 #endif // CHROME_BROWSER_SYNC_TEST_PROFILE_SYNC_SERVICE_H_ | 130 #endif // CHROME_BROWSER_SYNC_TEST_PROFILE_SYNC_SERVICE_H_ |
| OLD | NEW |