| 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 "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/net/gaia/token_service.h" | 9 #include "chrome/browser/net/gaia/token_service.h" |
| 10 #include "chrome/browser/sync/glue/bookmark_data_type_controller.h" | 10 #include "chrome/browser/sync/glue/bookmark_data_type_controller.h" |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 // posting on the IO thread). | 70 // posting on the IO thread). |
| 71 ui_loop_.RunAllPending(); | 71 ui_loop_.RunAllPending(); |
| 72 io_thread_.Stop(); | 72 io_thread_.Stop(); |
| 73 // Ensure that the sync objects destruct to avoid memory leaks. | 73 // Ensure that the sync objects destruct to avoid memory leaks. |
| 74 ui_loop_.RunAllPending(); | 74 ui_loop_.RunAllPending(); |
| 75 } | 75 } |
| 76 | 76 |
| 77 // TODO(akalin): Refactor the StartSyncService*() functions below. | 77 // TODO(akalin): Refactor the StartSyncService*() functions below. |
| 78 | 78 |
| 79 void StartSyncService() { | 79 void StartSyncService() { |
| 80 StartSyncServiceAndSetInitialSyncEnded(true, true, false, true); | 80 StartSyncServiceAndSetInitialSyncEnded(true, true, false, true, true); |
| 81 } | 81 } |
| 82 | 82 |
| 83 void StartSyncServiceAndSetInitialSyncEnded( | 83 void StartSyncServiceAndSetInitialSyncEnded( |
| 84 bool set_initial_sync_ended, | 84 bool set_initial_sync_ended, |
| 85 bool issue_auth_token, | 85 bool issue_auth_token, |
| 86 bool synchronous_sync_configuration, | 86 bool synchronous_sync_configuration, |
| 87 bool sync_setup_completed) { | 87 bool sync_setup_completed, |
| 88 bool expect_create_dtm) { |
| 88 if (!service_.get()) { | 89 if (!service_.get()) { |
| 89 // Set bootstrap to true and it will provide a logged in user for test | 90 // Set bootstrap to true and it will provide a logged in user for test |
| 90 service_.reset(new TestProfileSyncService(&factory_, | 91 service_.reset(new TestProfileSyncService(&factory_, |
| 91 profile_.get(), | 92 profile_.get(), |
| 92 "test", true, NULL)); | 93 "test", true, NULL)); |
| 93 if (!set_initial_sync_ended) | 94 if (!set_initial_sync_ended) |
| 94 service_->dont_set_initial_sync_ended_on_init(); | 95 service_->dont_set_initial_sync_ended_on_init(); |
| 95 if (synchronous_sync_configuration) | 96 if (synchronous_sync_configuration) |
| 96 service_->set_synchronous_sync_configuration(); | 97 service_->set_synchronous_sync_configuration(); |
| 97 if (!sync_setup_completed) | 98 if (!sync_setup_completed) |
| 98 profile_->GetPrefs()->SetBoolean(prefs::kSyncHasSetupCompleted, false); | 99 profile_->GetPrefs()->SetBoolean(prefs::kSyncHasSetupCompleted, false); |
| 99 | 100 |
| 100 // Register the bookmark data type. | 101 if (expect_create_dtm) { |
| 101 EXPECT_CALL(factory_, CreateDataTypeManager(_, _)). | 102 // Register the bookmark data type. |
| 102 WillOnce(ReturnNewDataTypeManager()); | 103 EXPECT_CALL(factory_, CreateDataTypeManager(_, _)). |
| 104 WillOnce(ReturnNewDataTypeManager()); |
| 105 } else { |
| 106 EXPECT_CALL(factory_, CreateDataTypeManager(_, _)).Times(0); |
| 107 } |
| 103 | 108 |
| 104 if (issue_auth_token) { | 109 if (issue_auth_token) { |
| 105 profile_->GetTokenService()->IssueAuthTokenForTest( | 110 profile_->GetTokenService()->IssueAuthTokenForTest( |
| 106 GaiaConstants::kSyncService, "token"); | 111 GaiaConstants::kSyncService, "token"); |
| 107 } | 112 } |
| 108 service_->Initialize(); | 113 service_->Initialize(); |
| 109 } | 114 } |
| 110 } | 115 } |
| 111 | 116 |
| 112 MessageLoop ui_loop_; | 117 MessageLoop ui_loop_; |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 EXPECT_TRUE(service_->GetBackendForTest() != NULL); | 190 EXPECT_TRUE(service_->GetBackendForTest() != NULL); |
| 186 | 191 |
| 187 JsController* js_controller = service_->GetJsController(); | 192 JsController* js_controller = service_->GetJsController(); |
| 188 StrictMock<MockJsEventHandler> event_handler; | 193 StrictMock<MockJsEventHandler> event_handler; |
| 189 js_controller->AddJsEventHandler(&event_handler); | 194 js_controller->AddJsEventHandler(&event_handler); |
| 190 js_controller->RemoveJsEventHandler(&event_handler); | 195 js_controller->RemoveJsEventHandler(&event_handler); |
| 191 } | 196 } |
| 192 | 197 |
| 193 TEST_F(ProfileSyncServiceTest, | 198 TEST_F(ProfileSyncServiceTest, |
| 194 JsControllerHandlersDelayedBackendInitialization) { | 199 JsControllerHandlersDelayedBackendInitialization) { |
| 195 StartSyncServiceAndSetInitialSyncEnded(true, false, false, true); | 200 StartSyncServiceAndSetInitialSyncEnded(true, false, false, true, true); |
| 196 | 201 |
| 197 StrictMock<MockJsEventHandler> event_handler; | 202 StrictMock<MockJsEventHandler> event_handler; |
| 198 EXPECT_CALL(event_handler, HandleJsEvent(_, _)).Times(AtLeast(1)); | 203 EXPECT_CALL(event_handler, HandleJsEvent(_, _)).Times(AtLeast(1)); |
| 199 | 204 |
| 200 EXPECT_EQ(NULL, service_->GetBackendForTest()); | 205 EXPECT_EQ(NULL, service_->GetBackendForTest()); |
| 201 EXPECT_FALSE(service_->sync_initialized()); | 206 EXPECT_FALSE(service_->sync_initialized()); |
| 202 | 207 |
| 203 JsController* js_controller = service_->GetJsController(); | 208 JsController* js_controller = service_->GetJsController(); |
| 204 js_controller->AddJsEventHandler(&event_handler); | 209 js_controller->AddJsEventHandler(&event_handler); |
| 205 // Since we're doing synchronous initialization, backend should be | 210 // Since we're doing synchronous initialization, backend should be |
| (...skipping 21 matching lines...) Expand all Loading... |
| 227 reply_handler.AsWeakHandle()); | 232 reply_handler.AsWeakHandle()); |
| 228 } | 233 } |
| 229 | 234 |
| 230 // This forces the sync thread to process the message and reply. | 235 // This forces the sync thread to process the message and reply. |
| 231 service_.reset(); | 236 service_.reset(); |
| 232 ui_loop_.RunAllPending(); | 237 ui_loop_.RunAllPending(); |
| 233 } | 238 } |
| 234 | 239 |
| 235 TEST_F(ProfileSyncServiceTest, | 240 TEST_F(ProfileSyncServiceTest, |
| 236 JsControllerProcessJsMessageBasicDelayedBackendInitialization) { | 241 JsControllerProcessJsMessageBasicDelayedBackendInitialization) { |
| 237 StartSyncServiceAndSetInitialSyncEnded(true, false, false, true); | 242 StartSyncServiceAndSetInitialSyncEnded(true, false, false, true, true); |
| 238 | 243 |
| 239 StrictMock<MockJsReplyHandler> reply_handler; | 244 StrictMock<MockJsReplyHandler> reply_handler; |
| 240 | 245 |
| 241 ListValue arg_list1; | 246 ListValue arg_list1; |
| 242 arg_list1.Append(Value::CreateBooleanValue(false)); | 247 arg_list1.Append(Value::CreateBooleanValue(false)); |
| 243 JsArgList args1(&arg_list1); | 248 JsArgList args1(&arg_list1); |
| 244 EXPECT_CALL(reply_handler, | 249 EXPECT_CALL(reply_handler, |
| 245 HandleJsReply("getNotificationState", HasArgs(args1))); | 250 HandleJsReply("getNotificationState", HasArgs(args1))); |
| 246 | 251 |
| 247 { | 252 { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 270 FilePath sync_file2 = temp_directory.AppendASCII("SyncData.sqlite3"); | 275 FilePath sync_file2 = temp_directory.AppendASCII("SyncData.sqlite3"); |
| 271 FilePath sync_file3 = temp_directory.AppendASCII("nonsense_file"); | 276 FilePath sync_file3 = temp_directory.AppendASCII("nonsense_file"); |
| 272 ASSERT_TRUE(file_util::CreateDirectory(temp_directory)); | 277 ASSERT_TRUE(file_util::CreateDirectory(temp_directory)); |
| 273 ASSERT_NE(-1, | 278 ASSERT_NE(-1, |
| 274 file_util::WriteFile(sync_file1, nonsense1, strlen(nonsense1))); | 279 file_util::WriteFile(sync_file1, nonsense1, strlen(nonsense1))); |
| 275 ASSERT_NE(-1, | 280 ASSERT_NE(-1, |
| 276 file_util::WriteFile(sync_file2, nonsense2, strlen(nonsense2))); | 281 file_util::WriteFile(sync_file2, nonsense2, strlen(nonsense2))); |
| 277 ASSERT_NE(-1, | 282 ASSERT_NE(-1, |
| 278 file_util::WriteFile(sync_file3, nonsense3, strlen(nonsense3))); | 283 file_util::WriteFile(sync_file3, nonsense3, strlen(nonsense3))); |
| 279 | 284 |
| 280 StartSyncServiceAndSetInitialSyncEnded(false, false, true, false); | 285 StartSyncServiceAndSetInitialSyncEnded(false, false, true, false, true); |
| 281 EXPECT_FALSE(service_->HasSyncSetupCompleted()); | 286 EXPECT_FALSE(service_->HasSyncSetupCompleted()); |
| 287 EXPECT_FALSE(service_->sync_initialized()); |
| 282 | 288 |
| 283 // Since we're doing synchronous initialization, backend should be | 289 // Since we're doing synchronous initialization, backend should be |
| 284 // initialized by this call. | 290 // initialized by this call. |
| 285 profile_->GetTokenService()->IssueAuthTokenForTest( | 291 profile_->GetTokenService()->IssueAuthTokenForTest( |
| 286 GaiaConstants::kSyncService, "token"); | 292 GaiaConstants::kSyncService, "token"); |
| 287 | 293 |
| 288 // Stop the service so we can read the new Sync Data files that were | 294 // Stop the service so we can read the new Sync Data files that were |
| 289 // created. | 295 // created. |
| 290 service_.reset(); | 296 service_.reset(); |
| 291 | 297 |
| 292 // This file should have been deleted when the whole directory was nuked. | 298 // This file should have been deleted when the whole directory was nuked. |
| 293 ASSERT_FALSE(file_util::PathExists(sync_file3)); | 299 ASSERT_FALSE(file_util::PathExists(sync_file3)); |
| 294 ASSERT_FALSE(file_util::PathExists(sync_file1)); | 300 ASSERT_FALSE(file_util::PathExists(sync_file1)); |
| 295 | 301 |
| 296 // This will still exist, but the text should have changed. | 302 // This will still exist, but the text should have changed. |
| 297 ASSERT_TRUE(file_util::PathExists(sync_file2)); | 303 ASSERT_TRUE(file_util::PathExists(sync_file2)); |
| 298 std::string file2text; | 304 std::string file2text; |
| 299 ASSERT_TRUE(file_util::ReadFileToString(sync_file2, &file2text)); | 305 ASSERT_TRUE(file_util::ReadFileToString(sync_file2, &file2text)); |
| 300 ASSERT_NE(file2text.compare(nonsense2), 0); | 306 ASSERT_NE(file2text.compare(nonsense2), 0); |
| 301 } | 307 } |
| 302 | 308 |
| 309 TEST_F(ProfileSyncServiceTest, CorruptDatabase) { |
| 310 const char* nonesense = "not a database"; |
| 311 |
| 312 FilePath temp_directory = profile_->GetPath().AppendASCII("Sync Data"); |
| 313 FilePath sync_db_file = temp_directory.AppendASCII("SyncData.sqlite3"); |
| 314 |
| 315 ASSERT_TRUE(file_util::CreateDirectory(temp_directory)); |
| 316 ASSERT_NE(-1, |
| 317 file_util::WriteFile(sync_db_file, nonesense, strlen(nonesense))); |
| 318 |
| 319 // Initialize with HasSyncSetupCompleted() set to true and InitialSyncEnded |
| 320 // false. This is to model the scenario that would result when opening the |
| 321 // sync database fails. |
| 322 StartSyncServiceAndSetInitialSyncEnded(false, true, true, true, false); |
| 323 |
| 324 // The backend is not ready. Ensure the PSS knows this. |
| 325 EXPECT_FALSE(service_->sync_initialized()); |
| 326 |
| 327 // Ensure we will be prepared to initialize a fresh DB next time. |
| 328 EXPECT_FALSE(service_->HasSyncSetupCompleted()); |
| 329 } |
| 330 |
| 303 } // namespace | 331 } // namespace |
| 304 | 332 |
| 305 } // namespace browser_sync | 333 } // namespace browser_sync |
| OLD | NEW |