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 "base/file_util.h" | 5 #include "base/file_util.h" |
6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
8 #include "base/values.h" | 8 #include "base/values.h" |
9 #include "chrome/browser/signin/signin_manager.h" | 9 #include "chrome/browser/signin/signin_manager.h" |
10 #include "chrome/browser/signin/signin_manager_factory.h" | 10 #include "chrome/browser/signin/signin_manager_factory.h" |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
75 io_thread_.Stop(); | 75 io_thread_.Stop(); |
76 file_thread_.Stop(); | 76 file_thread_.Stop(); |
77 // Ensure that the sync objects destruct to avoid memory leaks. | 77 // Ensure that the sync objects destruct to avoid memory leaks. |
78 ui_loop_.RunAllPending(); | 78 ui_loop_.RunAllPending(); |
79 } | 79 } |
80 | 80 |
81 // TODO(akalin): Refactor the StartSyncService*() functions below. | 81 // TODO(akalin): Refactor the StartSyncService*() functions below. |
82 | 82 |
83 void StartSyncService() { | 83 void StartSyncService() { |
84 StartSyncServiceAndSetInitialSyncEnded( | 84 StartSyncServiceAndSetInitialSyncEnded( |
85 true, true, false, true, true, false); | 85 true, true, false, true, true, syncer::STORAGE_IN_MEMORY); |
tim (not reviewing)
2012/07/31 21:14:36
+100
| |
86 } | 86 } |
87 | 87 |
88 void StartSyncServiceAndSetInitialSyncEnded( | 88 void StartSyncServiceAndSetInitialSyncEnded( |
89 bool set_initial_sync_ended, | 89 bool set_initial_sync_ended, |
90 bool issue_auth_token, | 90 bool issue_auth_token, |
91 bool synchronous_sync_configuration, | 91 bool synchronous_sync_configuration, |
92 bool sync_setup_completed, | 92 bool sync_setup_completed, |
93 bool expect_create_dtm, | 93 bool expect_create_dtm, |
94 bool use_real_database) { | 94 syncer::StorageOption storage_option) { |
95 if (!service_.get()) { | 95 if (!service_.get()) { |
96 SigninManager* signin = | 96 SigninManager* signin = |
97 SigninManagerFactory::GetForProfile(profile_.get()); | 97 SigninManagerFactory::GetForProfile(profile_.get()); |
98 signin->SetAuthenticatedUsername("test"); | 98 signin->SetAuthenticatedUsername("test"); |
99 ProfileSyncComponentsFactoryMock* factory = | 99 ProfileSyncComponentsFactoryMock* factory = |
100 new ProfileSyncComponentsFactoryMock(); | 100 new ProfileSyncComponentsFactoryMock(); |
101 service_.reset(new TestProfileSyncService( | 101 service_.reset(new TestProfileSyncService( |
102 factory, | 102 factory, |
103 profile_.get(), | 103 profile_.get(), |
104 signin, | 104 signin, |
105 ProfileSyncService::AUTO_START, | 105 ProfileSyncService::AUTO_START, |
106 true, | 106 true, |
107 base::Closure())); | 107 base::Closure())); |
108 if (!set_initial_sync_ended) | 108 if (!set_initial_sync_ended) |
109 service_->dont_set_initial_sync_ended_on_init(); | 109 service_->dont_set_initial_sync_ended_on_init(); |
110 if (synchronous_sync_configuration) | 110 if (synchronous_sync_configuration) |
111 service_->set_synchronous_sync_configuration(); | 111 service_->set_synchronous_sync_configuration(); |
112 if (use_real_database) | 112 service_->set_storage_option(storage_option); |
113 service_->set_use_real_database(); | |
114 if (!sync_setup_completed) | 113 if (!sync_setup_completed) |
115 profile_->GetPrefs()->SetBoolean(prefs::kSyncHasSetupCompleted, false); | 114 profile_->GetPrefs()->SetBoolean(prefs::kSyncHasSetupCompleted, false); |
116 | 115 |
117 if (expect_create_dtm) { | 116 if (expect_create_dtm) { |
118 // Register the bookmark data type. | 117 // Register the bookmark data type. |
119 EXPECT_CALL(*factory, CreateDataTypeManager(_, _)). | 118 EXPECT_CALL(*factory, CreateDataTypeManager(_, _)). |
120 WillOnce(ReturnNewDataTypeManager()); | 119 WillOnce(ReturnNewDataTypeManager()); |
121 } else { | 120 } else { |
122 EXPECT_CALL(*factory, CreateDataTypeManager(_, _)). | 121 EXPECT_CALL(*factory, CreateDataTypeManager(_, _)). |
123 Times(0); | 122 Times(0); |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
246 EXPECT_TRUE(service_->GetBackendForTest() != NULL); | 245 EXPECT_TRUE(service_->GetBackendForTest() != NULL); |
247 | 246 |
248 syncer::JsController* js_controller = service_->GetJsController(); | 247 syncer::JsController* js_controller = service_->GetJsController(); |
249 StrictMock<syncer::MockJsEventHandler> event_handler; | 248 StrictMock<syncer::MockJsEventHandler> event_handler; |
250 js_controller->AddJsEventHandler(&event_handler); | 249 js_controller->AddJsEventHandler(&event_handler); |
251 js_controller->RemoveJsEventHandler(&event_handler); | 250 js_controller->RemoveJsEventHandler(&event_handler); |
252 } | 251 } |
253 | 252 |
254 TEST_F(ProfileSyncServiceTest, | 253 TEST_F(ProfileSyncServiceTest, |
255 JsControllerHandlersDelayedBackendInitialization) { | 254 JsControllerHandlersDelayedBackendInitialization) { |
256 StartSyncServiceAndSetInitialSyncEnded(true, false, false, true, true, false); | 255 StartSyncServiceAndSetInitialSyncEnded(true, false, false, true, true, |
256 syncer::STORAGE_IN_MEMORY); | |
257 | 257 |
258 StrictMock<syncer::MockJsEventHandler> event_handler; | 258 StrictMock<syncer::MockJsEventHandler> event_handler; |
259 EXPECT_CALL(event_handler, HandleJsEvent(_, _)).Times(AtLeast(1)); | 259 EXPECT_CALL(event_handler, HandleJsEvent(_, _)).Times(AtLeast(1)); |
260 | 260 |
261 EXPECT_EQ(NULL, service_->GetBackendForTest()); | 261 EXPECT_EQ(NULL, service_->GetBackendForTest()); |
262 EXPECT_FALSE(service_->sync_initialized()); | 262 EXPECT_FALSE(service_->sync_initialized()); |
263 | 263 |
264 syncer::JsController* js_controller = service_->GetJsController(); | 264 syncer::JsController* js_controller = service_->GetJsController(); |
265 js_controller->AddJsEventHandler(&event_handler); | 265 js_controller->AddJsEventHandler(&event_handler); |
266 // Since we're doing synchronous initialization, backend should be | 266 // Since we're doing synchronous initialization, backend should be |
(...skipping 20 matching lines...) Expand all Loading... | |
287 reply_handler.AsWeakHandle()); | 287 reply_handler.AsWeakHandle()); |
288 } | 288 } |
289 | 289 |
290 // This forces the sync thread to process the message and reply. | 290 // This forces the sync thread to process the message and reply. |
291 service_.reset(); | 291 service_.reset(); |
292 ui_loop_.RunAllPending(); | 292 ui_loop_.RunAllPending(); |
293 } | 293 } |
294 | 294 |
295 TEST_F(ProfileSyncServiceTest, | 295 TEST_F(ProfileSyncServiceTest, |
296 JsControllerProcessJsMessageBasicDelayedBackendInitialization) { | 296 JsControllerProcessJsMessageBasicDelayedBackendInitialization) { |
297 StartSyncServiceAndSetInitialSyncEnded(true, false, false, true, true, false); | 297 StartSyncServiceAndSetInitialSyncEnded(true, false, false, true, true, |
298 syncer::STORAGE_IN_MEMORY); | |
298 | 299 |
299 StrictMock<syncer::MockJsReplyHandler> reply_handler; | 300 StrictMock<syncer::MockJsReplyHandler> reply_handler; |
300 | 301 |
301 ListValue arg_list1; | 302 ListValue arg_list1; |
302 arg_list1.Append(Value::CreateBooleanValue(false)); | 303 arg_list1.Append(Value::CreateBooleanValue(false)); |
303 syncer::JsArgList args1(&arg_list1); | 304 syncer::JsArgList args1(&arg_list1); |
304 EXPECT_CALL(reply_handler, | 305 EXPECT_CALL(reply_handler, |
305 HandleJsReply("getNotificationState", HasArgs(args1))); | 306 HandleJsReply("getNotificationState", HasArgs(args1))); |
306 | 307 |
307 { | 308 { |
(...skipping 21 matching lines...) Expand all Loading... | |
329 FilePath sync_file2 = temp_directory.AppendASCII("SyncData.sqlite3"); | 330 FilePath sync_file2 = temp_directory.AppendASCII("SyncData.sqlite3"); |
330 FilePath sync_file3 = temp_directory.AppendASCII("nonsense_file"); | 331 FilePath sync_file3 = temp_directory.AppendASCII("nonsense_file"); |
331 ASSERT_TRUE(file_util::CreateDirectory(temp_directory)); | 332 ASSERT_TRUE(file_util::CreateDirectory(temp_directory)); |
332 ASSERT_NE(-1, | 333 ASSERT_NE(-1, |
333 file_util::WriteFile(sync_file1, nonsense1, strlen(nonsense1))); | 334 file_util::WriteFile(sync_file1, nonsense1, strlen(nonsense1))); |
334 ASSERT_NE(-1, | 335 ASSERT_NE(-1, |
335 file_util::WriteFile(sync_file2, nonsense2, strlen(nonsense2))); | 336 file_util::WriteFile(sync_file2, nonsense2, strlen(nonsense2))); |
336 ASSERT_NE(-1, | 337 ASSERT_NE(-1, |
337 file_util::WriteFile(sync_file3, nonsense3, strlen(nonsense3))); | 338 file_util::WriteFile(sync_file3, nonsense3, strlen(nonsense3))); |
338 | 339 |
339 StartSyncServiceAndSetInitialSyncEnded(false, false, true, false, true, true); | 340 StartSyncServiceAndSetInitialSyncEnded(false, false, true, false, true, |
341 syncer::STORAGE_ON_DISK); | |
340 EXPECT_FALSE(service_->HasSyncSetupCompleted()); | 342 EXPECT_FALSE(service_->HasSyncSetupCompleted()); |
341 EXPECT_FALSE(service_->sync_initialized()); | 343 EXPECT_FALSE(service_->sync_initialized()); |
342 | 344 |
343 // Since we're doing synchronous initialization, backend should be | 345 // Since we're doing synchronous initialization, backend should be |
344 // initialized by this call. | 346 // initialized by this call. |
345 IssueTestTokens(); | 347 IssueTestTokens(); |
346 | 348 |
347 // Stop the service so we can read the new Sync Data files that were | 349 // Stop the service so we can read the new Sync Data files that were |
348 // created. | 350 // created. |
349 service_.reset(); | 351 service_.reset(); |
350 | 352 |
351 // This file should have been deleted when the whole directory was nuked. | 353 // This file should have been deleted when the whole directory was nuked. |
352 ASSERT_FALSE(file_util::PathExists(sync_file3)); | 354 ASSERT_FALSE(file_util::PathExists(sync_file3)); |
353 ASSERT_FALSE(file_util::PathExists(sync_file1)); | 355 ASSERT_FALSE(file_util::PathExists(sync_file1)); |
354 | 356 |
355 // This will still exist, but the text should have changed. | 357 // This will still exist, but the text should have changed. |
356 ASSERT_TRUE(file_util::PathExists(sync_file2)); | 358 ASSERT_TRUE(file_util::PathExists(sync_file2)); |
357 std::string file2text; | 359 std::string file2text; |
358 ASSERT_TRUE(file_util::ReadFileToString(sync_file2, &file2text)); | 360 ASSERT_TRUE(file_util::ReadFileToString(sync_file2, &file2text)); |
359 ASSERT_NE(file2text.compare(nonsense2), 0); | 361 ASSERT_NE(file2text.compare(nonsense2), 0); |
360 } | 362 } |
361 | 363 |
362 // Disabled because of crbug.com/109668. | 364 // Simulates a scenario where a database is corrupted and it is impossible to |
363 TEST_F(ProfileSyncServiceTest, DISABLED_CorruptDatabase) { | 365 // recreate it. This test is useful mainly when it is run under valgrind. Its |
364 const char* nonesense = "not a database"; | 366 // expectations are not very interesting. |
365 | 367 TEST_F(ProfileSyncServiceTest, FailToOpenDatabase) { |
366 FilePath temp_directory = profile_->GetPath().AppendASCII("Sync Data"); | 368 StartSyncServiceAndSetInitialSyncEnded(false, true, true, true, false, |
367 FilePath sync_db_file = temp_directory.AppendASCII("SyncData.sqlite3"); | 369 syncer::STORAGE_INVALID); |
368 | |
369 ASSERT_TRUE(file_util::CreateDirectory(temp_directory)); | |
370 ASSERT_NE(-1, | |
371 file_util::WriteFile(sync_db_file, nonesense, strlen(nonesense))); | |
372 | |
373 // Initialize with HasSyncSetupCompleted() set to true and InitialSyncEnded | |
374 // false. This is to model the scenario that would result when opening the | |
375 // sync database fails. | |
376 StartSyncServiceAndSetInitialSyncEnded(false, true, true, true, false, true); | |
377 | 370 |
378 // The backend is not ready. Ensure the PSS knows this. | 371 // The backend is not ready. Ensure the PSS knows this. |
379 EXPECT_FALSE(service_->sync_initialized()); | 372 EXPECT_FALSE(service_->sync_initialized()); |
380 | |
381 // Ensure we will be prepared to initialize a fresh DB next time. | |
382 EXPECT_FALSE(service_->HasSyncSetupCompleted()); | |
383 } | 373 } |
384 | 374 |
385 } // namespace | 375 } // namespace |
386 | |
387 } // namespace browser_sync | 376 } // namespace browser_sync |
OLD | NEW |