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 "chrome/browser/sync/glue/sync_backend_host.h" | 5 #include "chrome/browser/sync/glue/sync_backend_host.h" |
6 | 6 |
7 #include <cstddef> | 7 #include <cstddef> |
8 | 8 |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
12 #include "base/synchronization/waitable_event.h" | 12 #include "base/synchronization/waitable_event.h" |
13 #include "base/test/test_timeouts.h" | 13 #include "base/test/test_timeouts.h" |
14 #include "chrome/browser/invalidation/invalidator_storage.h" | 14 #include "chrome/browser/invalidation/invalidator_storage.h" |
15 #include "chrome/browser/prefs/pref_service_syncable.h" | 15 #include "chrome/browser/prefs/pref_service_syncable.h" |
16 #include "chrome/browser/sync/glue/device_info.h" | 16 #include "chrome/browser/sync/glue/device_info.h" |
17 #include "chrome/browser/sync/glue/synced_device_tracker.h" | 17 #include "chrome/browser/sync/glue/synced_device_tracker.h" |
18 #include "chrome/browser/sync/sync_prefs.h" | 18 #include "chrome/browser/sync/sync_prefs.h" |
19 #include "chrome/common/chrome_notification_types.h" | 19 #include "chrome/common/chrome_notification_types.h" |
20 #include "chrome/test/base/testing_profile.h" | 20 #include "chrome/test/base/testing_profile.h" |
21 #include "components/user_prefs/pref_registry_syncable.h" | 21 #include "components/user_prefs/pref_registry_syncable.h" |
22 #include "content/public/browser/notification_service.h" | 22 #include "content/public/browser/notification_service.h" |
23 #include "content/public/test/test_browser_thread.h" | 23 #include "content/public/test/test_browser_thread_bundle.h" |
| 24 #include "content/public/test/test_utils.h" |
24 #include "google/cacheinvalidation/include/types.h" | 25 #include "google/cacheinvalidation/include/types.h" |
25 #include "net/url_request/test_url_fetcher_factory.h" | 26 #include "net/url_request/test_url_fetcher_factory.h" |
26 #include "sync/internal_api/public/base/model_type.h" | 27 #include "sync/internal_api/public/base/model_type.h" |
27 #include "sync/internal_api/public/engine/model_safe_worker.h" | 28 #include "sync/internal_api/public/engine/model_safe_worker.h" |
28 #include "sync/internal_api/public/sync_manager_factory.h" | 29 #include "sync/internal_api/public/sync_manager_factory.h" |
29 #include "sync/internal_api/public/test/fake_sync_manager.h" | 30 #include "sync/internal_api/public/test/fake_sync_manager.h" |
30 #include "sync/internal_api/public/util/experiments.h" | 31 #include "sync/internal_api/public/util/experiments.h" |
31 #include "sync/notifier/invalidator_state.h" | 32 #include "sync/notifier/invalidator_state.h" |
32 #include "sync/notifier/object_id_invalidation_map_test_util.h" | 33 #include "sync/notifier/object_id_invalidation_map_test_util.h" |
33 #include "sync/protocol/encryption.pb.h" | 34 #include "sync/protocol/encryption.pb.h" |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 private: | 128 private: |
128 syncer::ModelTypeSet initial_sync_ended_types_; | 129 syncer::ModelTypeSet initial_sync_ended_types_; |
129 syncer::ModelTypeSet progress_marker_types_; | 130 syncer::ModelTypeSet progress_marker_types_; |
130 syncer::ModelTypeSet configure_fail_types_; | 131 syncer::ModelTypeSet configure_fail_types_; |
131 FakeSyncManager* fake_manager_; | 132 FakeSyncManager* fake_manager_; |
132 }; | 133 }; |
133 | 134 |
134 class SyncBackendHostTest : public testing::Test { | 135 class SyncBackendHostTest : public testing::Test { |
135 protected: | 136 protected: |
136 SyncBackendHostTest() | 137 SyncBackendHostTest() |
137 : ui_thread_(BrowserThread::UI, &ui_loop_), | 138 : thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD), |
138 db_thread_(BrowserThread::DB), | |
139 file_thread_(BrowserThread::FILE), | |
140 io_thread_(BrowserThread::IO), | |
141 fake_manager_(NULL) {} | 139 fake_manager_(NULL) {} |
142 | 140 |
143 virtual ~SyncBackendHostTest() {} | 141 virtual ~SyncBackendHostTest() {} |
144 | 142 |
145 virtual void SetUp() OVERRIDE { | 143 virtual void SetUp() OVERRIDE { |
146 io_thread_.StartIOThread(); | |
147 db_thread_.Start(); | |
148 file_thread_.Start(); | |
149 profile_.reset(new TestingProfile()); | 144 profile_.reset(new TestingProfile()); |
150 profile_->CreateRequestContext(); | |
151 sync_prefs_.reset(new SyncPrefs(profile_->GetPrefs())); | 145 sync_prefs_.reset(new SyncPrefs(profile_->GetPrefs())); |
152 backend_.reset(new SyncBackendHost( | 146 backend_.reset(new SyncBackendHost( |
153 profile_->GetDebugName(), | 147 profile_->GetDebugName(), |
154 profile_.get(), | 148 profile_.get(), |
155 sync_prefs_->AsWeakPtr())); | 149 sync_prefs_->AsWeakPtr())); |
156 credentials_.email = "user@example.com"; | 150 credentials_.email = "user@example.com"; |
157 credentials_.sync_token = "sync_token"; | 151 credentials_.sync_token = "sync_token"; |
158 | 152 |
159 // These types are always implicitly enabled. | 153 // These types are always implicitly enabled. |
160 enabled_types_.PutAll(syncer::ControlTypes()); | 154 enabled_types_.PutAll(syncer::ControlTypes()); |
(...skipping 13 matching lines...) Expand all Loading... |
174 virtual void TearDown() OVERRIDE { | 168 virtual void TearDown() OVERRIDE { |
175 if (backend_) { | 169 if (backend_) { |
176 backend_->StopSyncingForShutdown(); | 170 backend_->StopSyncingForShutdown(); |
177 backend_->Shutdown(false); | 171 backend_->Shutdown(false); |
178 } | 172 } |
179 backend_.reset(); | 173 backend_.reset(); |
180 sync_prefs_.reset(); | 174 sync_prefs_.reset(); |
181 profile_.reset(); | 175 profile_.reset(); |
182 // Pump messages posted by the sync thread (which may end up | 176 // Pump messages posted by the sync thread (which may end up |
183 // posting on the IO thread). | 177 // posting on the IO thread). |
184 ui_loop_.RunUntilIdle(); | 178 base::RunLoop().RunUntilIdle(); |
185 io_thread_.Stop(); | 179 content::RunAllPendingInMessageLoop(BrowserThread::IO); |
186 file_thread_.Stop(); | |
187 db_thread_.Stop(); | |
188 // Pump any messages posted by the IO thread. | 180 // Pump any messages posted by the IO thread. |
189 ui_loop_.RunUntilIdle(); | 181 base::RunLoop().RunUntilIdle(); |
190 } | 182 } |
191 | 183 |
192 // Synchronously initializes the backend. | 184 // Synchronously initializes the backend. |
193 void InitializeBackend(bool expect_success) { | 185 void InitializeBackend(bool expect_success) { |
194 EXPECT_CALL(mock_frontend_, OnBackendInitialized(_, _, expect_success)). | 186 EXPECT_CALL(mock_frontend_, OnBackendInitialized(_, _, expect_success)). |
195 WillOnce(InvokeWithoutArgs(QuitMessageLoop)); | 187 WillOnce(InvokeWithoutArgs(QuitMessageLoop)); |
196 backend_->Initialize(&mock_frontend_, | 188 backend_->Initialize(&mock_frontend_, |
197 scoped_ptr<base::Thread>(), | 189 scoped_ptr<base::Thread>(), |
198 syncer::WeakHandle<syncer::JsEventHandler>(), | 190 syncer::WeakHandle<syncer::JsEventHandler>(), |
199 GURL(std::string()), | 191 GURL(std::string()), |
200 credentials_, | 192 credentials_, |
201 true, | 193 true, |
202 &fake_manager_factory_, | 194 &fake_manager_factory_, |
203 &handler_, | 195 &handler_, |
204 NULL); | 196 NULL); |
205 ui_loop_.PostDelayedTask(FROM_HERE, | 197 base::RunLoop run_loop; |
206 ui_loop_.QuitClosure(), TestTimeouts::action_timeout()); | 198 BrowserThread::PostDelayedTask(BrowserThread::UI, FROM_HERE, |
207 ui_loop_.Run(); | 199 run_loop.QuitClosure(), |
| 200 TestTimeouts::action_timeout()); |
| 201 run_loop.Run(); |
208 // |fake_manager_factory_|'s fake_manager() is set on the sync | 202 // |fake_manager_factory_|'s fake_manager() is set on the sync |
209 // thread, but we can rely on the message loop barriers to | 203 // thread, but we can rely on the message loop barriers to |
210 // guarantee that we see the updated value. | 204 // guarantee that we see the updated value. |
211 fake_manager_ = fake_manager_factory_.fake_manager(); | 205 fake_manager_ = fake_manager_factory_.fake_manager(); |
212 DCHECK(fake_manager_); | 206 DCHECK(fake_manager_); |
213 } | 207 } |
214 | 208 |
215 // Synchronously configures the backend's datatypes. | 209 // Synchronously configures the backend's datatypes. |
216 void ConfigureDataTypes(syncer::ModelTypeSet types_to_add, | 210 void ConfigureDataTypes(syncer::ModelTypeSet types_to_add, |
217 syncer::ModelTypeSet types_to_remove) { | 211 syncer::ModelTypeSet types_to_remove) { |
218 BackendDataTypeConfigurer::DataTypeConfigStateMap config_state_map; | 212 BackendDataTypeConfigurer::DataTypeConfigStateMap config_state_map; |
219 BackendDataTypeConfigurer::SetDataTypesState( | 213 BackendDataTypeConfigurer::SetDataTypesState( |
220 BackendDataTypeConfigurer::CONFIGURE_ACTIVE, | 214 BackendDataTypeConfigurer::CONFIGURE_ACTIVE, |
221 types_to_add, | 215 types_to_add, |
222 &config_state_map); | 216 &config_state_map); |
223 BackendDataTypeConfigurer::SetDataTypesState( | 217 BackendDataTypeConfigurer::SetDataTypesState( |
224 BackendDataTypeConfigurer::DISABLED, | 218 BackendDataTypeConfigurer::DISABLED, |
225 types_to_remove, &config_state_map); | 219 types_to_remove, &config_state_map); |
226 | 220 |
227 types_to_add.PutAll(syncer::ControlTypes()); | 221 types_to_add.PutAll(syncer::ControlTypes()); |
228 backend_->ConfigureDataTypes( | 222 backend_->ConfigureDataTypes( |
229 syncer::CONFIGURE_REASON_RECONFIGURATION, | 223 syncer::CONFIGURE_REASON_RECONFIGURATION, |
230 config_state_map, | 224 config_state_map, |
231 base::Bind(&SyncBackendHostTest::DownloadReady, | 225 base::Bind(&SyncBackendHostTest::DownloadReady, |
232 base::Unretained(this)), | 226 base::Unretained(this)), |
233 base::Bind(&SyncBackendHostTest::OnDownloadRetry, | 227 base::Bind(&SyncBackendHostTest::OnDownloadRetry, |
234 base::Unretained(this))); | 228 base::Unretained(this))); |
235 ui_loop_.PostDelayedTask(FROM_HERE, | 229 base::RunLoop run_loop; |
236 ui_loop_.QuitClosure(), TestTimeouts::action_timeout()); | 230 BrowserThread::PostDelayedTask(BrowserThread::UI, FROM_HERE, |
237 ui_loop_.Run(); | 231 run_loop.QuitClosure(), |
| 232 TestTimeouts::action_timeout()); |
| 233 run_loop.Run(); |
238 } | 234 } |
239 | 235 |
240 void IssueRefreshRequest(syncer::ModelTypeSet types) { | 236 void IssueRefreshRequest(syncer::ModelTypeSet types) { |
241 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 237 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
242 | 238 |
243 syncer::ModelTypeInvalidationMap invalidation_map( | 239 syncer::ModelTypeInvalidationMap invalidation_map( |
244 ModelTypeSetToInvalidationMap(types, std::string())); | 240 ModelTypeSetToInvalidationMap(types, std::string())); |
245 | 241 |
246 content::NotificationService::current()->Notify( | 242 content::NotificationService::current()->Notify( |
247 chrome::NOTIFICATION_SYNC_REFRESH_LOCAL, | 243 chrome::NOTIFICATION_SYNC_REFRESH_LOCAL, |
248 content::Source<Profile>(profile_.get()), | 244 content::Source<Profile>(profile_.get()), |
249 content::Details<syncer::ModelTypeInvalidationMap>( | 245 content::Details<syncer::ModelTypeInvalidationMap>( |
250 &invalidation_map)); | 246 &invalidation_map)); |
251 } | 247 } |
252 | 248 |
253 protected: | 249 protected: |
254 void DownloadReady(syncer::ModelTypeSet succeeded_types, | 250 void DownloadReady(syncer::ModelTypeSet succeeded_types, |
255 syncer::ModelTypeSet failed_types) { | 251 syncer::ModelTypeSet failed_types) { |
256 base::MessageLoop::current()->Quit(); | 252 base::MessageLoop::current()->Quit(); |
257 } | 253 } |
258 | 254 |
259 void OnDownloadRetry() { | 255 void OnDownloadRetry() { |
260 NOTIMPLEMENTED(); | 256 NOTIMPLEMENTED(); |
261 } | 257 } |
262 | 258 |
263 base::MessageLoop ui_loop_; | 259 content::TestBrowserThreadBundle thread_bundle_; |
264 content::TestBrowserThread ui_thread_; | |
265 content::TestBrowserThread db_thread_; | |
266 content::TestBrowserThread file_thread_; | |
267 content::TestBrowserThread io_thread_; | |
268 StrictMock<MockSyncFrontend> mock_frontend_; | 260 StrictMock<MockSyncFrontend> mock_frontend_; |
269 syncer::SyncCredentials credentials_; | 261 syncer::SyncCredentials credentials_; |
270 syncer::TestUnrecoverableErrorHandler handler_; | 262 syncer::TestUnrecoverableErrorHandler handler_; |
271 scoped_ptr<TestingProfile> profile_; | 263 scoped_ptr<TestingProfile> profile_; |
272 scoped_ptr<SyncPrefs> sync_prefs_; | 264 scoped_ptr<SyncPrefs> sync_prefs_; |
273 scoped_ptr<SyncBackendHost> backend_; | 265 scoped_ptr<SyncBackendHost> backend_; |
274 FakeSyncManager* fake_manager_; | 266 FakeSyncManager* fake_manager_; |
275 FakeSyncManagerFactory fake_manager_factory_; | 267 FakeSyncManagerFactory fake_manager_factory_; |
276 syncer::ModelTypeSet enabled_types_; | 268 syncer::ModelTypeSet enabled_types_; |
277 }; | 269 }; |
(...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
696 TEST_F(SyncBackendHostTest, DownloadControlTypesRestart) { | 688 TEST_F(SyncBackendHostTest, DownloadControlTypesRestart) { |
697 sync_prefs_->SetSyncSetupCompleted(); | 689 sync_prefs_->SetSyncSetupCompleted(); |
698 InitializeBackend(true); | 690 InitializeBackend(true); |
699 EXPECT_EQ(syncer::CONFIGURE_REASON_NEWLY_ENABLED_DATA_TYPE, | 691 EXPECT_EQ(syncer::CONFIGURE_REASON_NEWLY_ENABLED_DATA_TYPE, |
700 fake_manager_->GetAndResetConfigureReason()); | 692 fake_manager_->GetAndResetConfigureReason()); |
701 } | 693 } |
702 | 694 |
703 } // namespace | 695 } // namespace |
704 | 696 |
705 } // namespace browser_sync | 697 } // namespace browser_sync |
OLD | NEW |