| 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, true); | 80 StartSyncServiceAndSetInitialSyncEnded(true, true, false, 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) { | |
| 89 if (!service_.get()) { | 88 if (!service_.get()) { |
| 90 // Set bootstrap to true and it will provide a logged in user for test | 89 // Set bootstrap to true and it will provide a logged in user for test |
| 91 service_.reset(new TestProfileSyncService(&factory_, | 90 service_.reset(new TestProfileSyncService(&factory_, |
| 92 profile_.get(), | 91 profile_.get(), |
| 93 "test", true, NULL)); | 92 "test", true, NULL)); |
| 94 if (!set_initial_sync_ended) | 93 if (!set_initial_sync_ended) |
| 95 service_->dont_set_initial_sync_ended_on_init(); | 94 service_->dont_set_initial_sync_ended_on_init(); |
| 96 if (synchronous_sync_configuration) | 95 if (synchronous_sync_configuration) |
| 97 service_->set_synchronous_sync_configuration(); | 96 service_->set_synchronous_sync_configuration(); |
| 98 if (!sync_setup_completed) | 97 if (!sync_setup_completed) |
| 99 profile_->GetPrefs()->SetBoolean(prefs::kSyncHasSetupCompleted, false); | 98 profile_->GetPrefs()->SetBoolean(prefs::kSyncHasSetupCompleted, false); |
| 100 | 99 |
| 101 if (expect_create_dtm) { | 100 // Register the bookmark data type. |
| 102 // Register the bookmark data type. | 101 EXPECT_CALL(factory_, CreateDataTypeManager(_, _)). |
| 103 EXPECT_CALL(factory_, CreateDataTypeManager(_, _)). | 102 WillOnce(ReturnNewDataTypeManager()); |
| 104 WillOnce(ReturnNewDataTypeManager()); | |
| 105 } else { | |
| 106 EXPECT_CALL(factory_, CreateDataTypeManager(_, _)).Times(0); | |
| 107 } | |
| 108 | 103 |
| 109 if (issue_auth_token) { | 104 if (issue_auth_token) { |
| 110 profile_->GetTokenService()->IssueAuthTokenForTest( | 105 profile_->GetTokenService()->IssueAuthTokenForTest( |
| 111 GaiaConstants::kSyncService, "token"); | 106 GaiaConstants::kSyncService, "token"); |
| 112 } | 107 } |
| 113 service_->Initialize(); | 108 service_->Initialize(); |
| 114 } | 109 } |
| 115 } | 110 } |
| 116 | 111 |
| 117 MessageLoop ui_loop_; | 112 MessageLoop ui_loop_; |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 EXPECT_TRUE(service_->GetBackendForTest() != NULL); | 185 EXPECT_TRUE(service_->GetBackendForTest() != NULL); |
| 191 | 186 |
| 192 JsController* js_controller = service_->GetJsController(); | 187 JsController* js_controller = service_->GetJsController(); |
| 193 StrictMock<MockJsEventHandler> event_handler; | 188 StrictMock<MockJsEventHandler> event_handler; |
| 194 js_controller->AddJsEventHandler(&event_handler); | 189 js_controller->AddJsEventHandler(&event_handler); |
| 195 js_controller->RemoveJsEventHandler(&event_handler); | 190 js_controller->RemoveJsEventHandler(&event_handler); |
| 196 } | 191 } |
| 197 | 192 |
| 198 TEST_F(ProfileSyncServiceTest, | 193 TEST_F(ProfileSyncServiceTest, |
| 199 JsControllerHandlersDelayedBackendInitialization) { | 194 JsControllerHandlersDelayedBackendInitialization) { |
| 200 StartSyncServiceAndSetInitialSyncEnded(true, false, false, true, true); | 195 StartSyncServiceAndSetInitialSyncEnded(true, false, false, true); |
| 201 | 196 |
| 202 StrictMock<MockJsEventHandler> event_handler; | 197 StrictMock<MockJsEventHandler> event_handler; |
| 203 EXPECT_CALL(event_handler, HandleJsEvent(_, _)).Times(AtLeast(1)); | 198 EXPECT_CALL(event_handler, HandleJsEvent(_, _)).Times(AtLeast(1)); |
| 204 | 199 |
| 205 EXPECT_EQ(NULL, service_->GetBackendForTest()); | 200 EXPECT_EQ(NULL, service_->GetBackendForTest()); |
| 206 EXPECT_FALSE(service_->sync_initialized()); | 201 EXPECT_FALSE(service_->sync_initialized()); |
| 207 | 202 |
| 208 JsController* js_controller = service_->GetJsController(); | 203 JsController* js_controller = service_->GetJsController(); |
| 209 js_controller->AddJsEventHandler(&event_handler); | 204 js_controller->AddJsEventHandler(&event_handler); |
| 210 // Since we're doing synchronous initialization, backend should be | 205 // Since we're doing synchronous initialization, backend should be |
| (...skipping 21 matching lines...) Expand all Loading... |
| 232 reply_handler.AsWeakHandle()); | 227 reply_handler.AsWeakHandle()); |
| 233 } | 228 } |
| 234 | 229 |
| 235 // This forces the sync thread to process the message and reply. | 230 // This forces the sync thread to process the message and reply. |
| 236 service_.reset(); | 231 service_.reset(); |
| 237 ui_loop_.RunAllPending(); | 232 ui_loop_.RunAllPending(); |
| 238 } | 233 } |
| 239 | 234 |
| 240 TEST_F(ProfileSyncServiceTest, | 235 TEST_F(ProfileSyncServiceTest, |
| 241 JsControllerProcessJsMessageBasicDelayedBackendInitialization) { | 236 JsControllerProcessJsMessageBasicDelayedBackendInitialization) { |
| 242 StartSyncServiceAndSetInitialSyncEnded(true, false, false, true, true); | 237 StartSyncServiceAndSetInitialSyncEnded(true, false, false, true); |
| 243 | 238 |
| 244 StrictMock<MockJsReplyHandler> reply_handler; | 239 StrictMock<MockJsReplyHandler> reply_handler; |
| 245 | 240 |
| 246 ListValue arg_list1; | 241 ListValue arg_list1; |
| 247 arg_list1.Append(Value::CreateBooleanValue(false)); | 242 arg_list1.Append(Value::CreateBooleanValue(false)); |
| 248 JsArgList args1(&arg_list1); | 243 JsArgList args1(&arg_list1); |
| 249 EXPECT_CALL(reply_handler, | 244 EXPECT_CALL(reply_handler, |
| 250 HandleJsReply("getNotificationState", HasArgs(args1))); | 245 HandleJsReply("getNotificationState", HasArgs(args1))); |
| 251 | 246 |
| 252 { | 247 { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 275 FilePath sync_file2 = temp_directory.AppendASCII("SyncData.sqlite3"); | 270 FilePath sync_file2 = temp_directory.AppendASCII("SyncData.sqlite3"); |
| 276 FilePath sync_file3 = temp_directory.AppendASCII("nonsense_file"); | 271 FilePath sync_file3 = temp_directory.AppendASCII("nonsense_file"); |
| 277 ASSERT_TRUE(file_util::CreateDirectory(temp_directory)); | 272 ASSERT_TRUE(file_util::CreateDirectory(temp_directory)); |
| 278 ASSERT_NE(-1, | 273 ASSERT_NE(-1, |
| 279 file_util::WriteFile(sync_file1, nonsense1, strlen(nonsense1))); | 274 file_util::WriteFile(sync_file1, nonsense1, strlen(nonsense1))); |
| 280 ASSERT_NE(-1, | 275 ASSERT_NE(-1, |
| 281 file_util::WriteFile(sync_file2, nonsense2, strlen(nonsense2))); | 276 file_util::WriteFile(sync_file2, nonsense2, strlen(nonsense2))); |
| 282 ASSERT_NE(-1, | 277 ASSERT_NE(-1, |
| 283 file_util::WriteFile(sync_file3, nonsense3, strlen(nonsense3))); | 278 file_util::WriteFile(sync_file3, nonsense3, strlen(nonsense3))); |
| 284 | 279 |
| 285 StartSyncServiceAndSetInitialSyncEnded(false, false, true, false, true); | 280 StartSyncServiceAndSetInitialSyncEnded(false, false, true, false); |
| 286 EXPECT_FALSE(service_->HasSyncSetupCompleted()); | 281 EXPECT_FALSE(service_->HasSyncSetupCompleted()); |
| 287 EXPECT_FALSE(service_->sync_initialized()); | |
| 288 | 282 |
| 289 // Since we're doing synchronous initialization, backend should be | 283 // Since we're doing synchronous initialization, backend should be |
| 290 // initialized by this call. | 284 // initialized by this call. |
| 291 profile_->GetTokenService()->IssueAuthTokenForTest( | 285 profile_->GetTokenService()->IssueAuthTokenForTest( |
| 292 GaiaConstants::kSyncService, "token"); | 286 GaiaConstants::kSyncService, "token"); |
| 293 | 287 |
| 294 // Stop the service so we can read the new Sync Data files that were | 288 // Stop the service so we can read the new Sync Data files that were |
| 295 // created. | 289 // created. |
| 296 service_.reset(); | 290 service_.reset(); |
| 297 | 291 |
| 298 // This file should have been deleted when the whole directory was nuked. | 292 // This file should have been deleted when the whole directory was nuked. |
| 299 ASSERT_FALSE(file_util::PathExists(sync_file3)); | 293 ASSERT_FALSE(file_util::PathExists(sync_file3)); |
| 300 ASSERT_FALSE(file_util::PathExists(sync_file1)); | 294 ASSERT_FALSE(file_util::PathExists(sync_file1)); |
| 301 | 295 |
| 302 // This will still exist, but the text should have changed. | 296 // This will still exist, but the text should have changed. |
| 303 ASSERT_TRUE(file_util::PathExists(sync_file2)); | 297 ASSERT_TRUE(file_util::PathExists(sync_file2)); |
| 304 std::string file2text; | 298 std::string file2text; |
| 305 ASSERT_TRUE(file_util::ReadFileToString(sync_file2, &file2text)); | 299 ASSERT_TRUE(file_util::ReadFileToString(sync_file2, &file2text)); |
| 306 ASSERT_NE(file2text.compare(nonsense2), 0); | 300 ASSERT_NE(file2text.compare(nonsense2), 0); |
| 307 } | 301 } |
| 308 | 302 |
| 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 | |
| 331 } // namespace | 303 } // namespace |
| 332 | 304 |
| 333 } // namespace browser_sync | 305 } // namespace browser_sync |
| OLD | NEW |