| 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/basictypes.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/bind.h" | 6 #include "base/bind.h" |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/message_loop_proxy.h" | 9 #include "base/message_loop_proxy.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
| 12 #include "base/threading/thread.h" | 12 #include "base/threading/thread.h" |
| 13 #include "chrome/browser/sync_file_system/local_file_sync_service.h" | 13 #include "chrome/browser/sync_file_system/local_file_sync_service.h" |
| 14 #include "chrome/browser/sync_file_system/mock_local_change_processor.h" | 14 #include "chrome/browser/sync_file_system/mock_local_change_processor.h" |
| 15 #include "chrome/browser/sync_file_system/sync_file_system_test_util.h" | 15 #include "chrome/browser/sync_file_system/sync_file_system_test_util.h" |
| 16 #include "chrome/test/base/testing_profile.h" | 16 #include "chrome/test/base/testing_profile.h" |
| 17 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "webkit/fileapi/file_system_context.h" | 19 #include "webkit/fileapi/file_system_context.h" |
| 20 #include "webkit/fileapi/syncable/canned_syncable_file_system.h" | 20 #include "webkit/fileapi/syncable/canned_syncable_file_system.h" |
| 21 #include "webkit/fileapi/syncable/file_change.h" | 21 #include "webkit/fileapi/syncable/file_change.h" |
| 22 #include "webkit/fileapi/syncable/local_file_change_tracker.h" | 22 #include "webkit/fileapi/syncable/local_file_change_tracker.h" |
| 23 #include "webkit/fileapi/syncable/local_file_sync_context.h" | 23 #include "webkit/fileapi/syncable/local_file_sync_context.h" |
| 24 #include "webkit/fileapi/syncable/local_file_sync_status.h" | 24 #include "webkit/fileapi/syncable/local_file_sync_status.h" |
| 25 #include "webkit/fileapi/syncable/mock_sync_status_observer.h" | 25 #include "webkit/fileapi/syncable/mock_sync_status_observer.h" |
| 26 #include "webkit/fileapi/syncable/sync_file_metadata.h" | 26 #include "webkit/fileapi/syncable/sync_file_metadata.h" |
| 27 #include "webkit/fileapi/syncable/sync_status_code.h" | 27 #include "webkit/fileapi/syncable/sync_status_code.h" |
| 28 #include "webkit/fileapi/syncable/syncable_file_system_util.h" | 28 #include "webkit/fileapi/syncable/syncable_file_system_util.h" |
| 29 | 29 |
| 30 using fileapi::FileChange; | |
| 31 using fileapi::FileChangeList; | |
| 32 using fileapi::FileSystemURL; | 30 using fileapi::FileSystemURL; |
| 33 using fileapi::LocalFileSyncStatus; | 31 using fileapi::LocalFileSyncStatus; |
| 34 using fileapi::MockSyncStatusObserver; | 32 using fileapi::MockSyncStatusObserver; |
| 35 using fileapi::SyncFileMetadata; | 33 using fileapi::SyncFileMetadata; |
| 36 using fileapi::SyncFileType; | |
| 37 using fileapi::SyncStatusCallback; | 34 using fileapi::SyncStatusCallback; |
| 38 using fileapi::SyncStatusCode; | 35 using fileapi::SyncStatusCode; |
| 39 | 36 |
| 37 using sync_file_system::FileChange; |
| 38 using sync_file_system::FileChangeList; |
| 39 using sync_file_system::SyncFileType; |
| 40 |
| 40 using ::testing::_; | 41 using ::testing::_; |
| 41 using ::testing::AtLeast; | 42 using ::testing::AtLeast; |
| 42 using ::testing::InvokeWithoutArgs; | 43 using ::testing::InvokeWithoutArgs; |
| 43 using ::testing::StrictMock; | 44 using ::testing::StrictMock; |
| 44 | 45 |
| 45 namespace sync_file_system { | 46 namespace sync_file_system { |
| 46 | 47 |
| 47 namespace { | 48 namespace { |
| 48 | 49 |
| 49 const char kOrigin[] = "http://example.com"; | 50 const char kOrigin[] = "http://example.com"; |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 const int kTestFileDataSize = static_cast<int>(arraysize(kTestFileData) - 1); | 206 const int kTestFileDataSize = static_cast<int>(arraysize(kTestFileData) - 1); |
| 206 | 207 |
| 207 base::FilePath local_path; | 208 base::FilePath local_path; |
| 208 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(), | 209 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(), |
| 209 &local_path)); | 210 &local_path)); |
| 210 ASSERT_EQ(kTestFileDataSize, | 211 ASSERT_EQ(kTestFileDataSize, |
| 211 file_util::WriteFile(local_path, kTestFileData, kTestFileDataSize)); | 212 file_util::WriteFile(local_path, kTestFileData, kTestFileDataSize)); |
| 212 | 213 |
| 213 // Run PrepareForProcessRemoteChange for kFile. | 214 // Run PrepareForProcessRemoteChange for kFile. |
| 214 SyncFileMetadata expected_metadata; | 215 SyncFileMetadata expected_metadata; |
| 215 expected_metadata.file_type = fileapi::SYNC_FILE_TYPE_UNKNOWN; | 216 expected_metadata.file_type = SYNC_FILE_TYPE_UNKNOWN; |
| 216 expected_metadata.size = 0; | 217 expected_metadata.size = 0; |
| 217 PrepareForProcessRemoteChange(kFile, FROM_HERE, | 218 PrepareForProcessRemoteChange(kFile, FROM_HERE, |
| 218 fileapi::SYNC_STATUS_OK, | 219 fileapi::SYNC_STATUS_OK, |
| 219 expected_metadata); | 220 expected_metadata); |
| 220 | 221 |
| 221 // Run ApplyRemoteChange for kFile. | 222 // Run ApplyRemoteChange for kFile. |
| 222 FileChange change(FileChange::FILE_CHANGE_ADD_OR_UPDATE, | 223 FileChange change(FileChange::FILE_CHANGE_ADD_OR_UPDATE, |
| 223 fileapi::SYNC_FILE_TYPE_FILE); | 224 SYNC_FILE_TYPE_FILE); |
| 224 EXPECT_EQ(fileapi::SYNC_STATUS_OK, | 225 EXPECT_EQ(fileapi::SYNC_STATUS_OK, |
| 225 ApplyRemoteChange(change, local_path, kFile)); | 226 ApplyRemoteChange(change, local_path, kFile)); |
| 226 | 227 |
| 227 // Verify the file is synced. | 228 // Verify the file is synced. |
| 228 EXPECT_EQ(base::PLATFORM_FILE_OK, | 229 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 229 file_system_->VerifyFile(kFile, kTestFileData)); | 230 file_system_->VerifyFile(kFile, kTestFileData)); |
| 230 | 231 |
| 231 // Run PrepareForProcessRemoteChange for kDir. | 232 // Run PrepareForProcessRemoteChange for kDir. |
| 232 PrepareForProcessRemoteChange(kDir, FROM_HERE, | 233 PrepareForProcessRemoteChange(kDir, FROM_HERE, |
| 233 fileapi::SYNC_STATUS_OK, | 234 fileapi::SYNC_STATUS_OK, |
| 234 expected_metadata); | 235 expected_metadata); |
| 235 | 236 |
| 236 // Run ApplyRemoteChange for kDir. | 237 // Run ApplyRemoteChange for kDir. |
| 237 change = FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE, | 238 change = FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE, |
| 238 fileapi::SYNC_FILE_TYPE_DIRECTORY); | 239 SYNC_FILE_TYPE_DIRECTORY); |
| 239 EXPECT_EQ(fileapi::SYNC_STATUS_OK, | 240 EXPECT_EQ(fileapi::SYNC_STATUS_OK, |
| 240 ApplyRemoteChange(change, base::FilePath(), kDir)); | 241 ApplyRemoteChange(change, base::FilePath(), kDir)); |
| 241 | 242 |
| 242 // Verify the directory. | 243 // Verify the directory. |
| 243 EXPECT_EQ(base::PLATFORM_FILE_OK, | 244 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 244 file_system_->DirectoryExists(kDir)); | 245 file_system_->DirectoryExists(kDir)); |
| 245 | 246 |
| 246 // Run PrepareForProcessRemoteChange and ApplyRemoteChange for | 247 // Run PrepareForProcessRemoteChange and ApplyRemoteChange for |
| 247 // kDir once again for deletion. | 248 // kDir once again for deletion. |
| 248 expected_metadata.file_type = fileapi::SYNC_FILE_TYPE_DIRECTORY; | 249 expected_metadata.file_type = SYNC_FILE_TYPE_DIRECTORY; |
| 249 expected_metadata.size = 0; | 250 expected_metadata.size = 0; |
| 250 PrepareForProcessRemoteChange(kDir, FROM_HERE, | 251 PrepareForProcessRemoteChange(kDir, FROM_HERE, |
| 251 fileapi::SYNC_STATUS_OK, | 252 fileapi::SYNC_STATUS_OK, |
| 252 expected_metadata); | 253 expected_metadata); |
| 253 | 254 |
| 254 change = FileChange(FileChange::FILE_CHANGE_DELETE, | 255 change = FileChange(FileChange::FILE_CHANGE_DELETE, SYNC_FILE_TYPE_UNKNOWN); |
| 255 fileapi::SYNC_FILE_TYPE_UNKNOWN); | |
| 256 EXPECT_EQ(fileapi::SYNC_STATUS_OK, | 256 EXPECT_EQ(fileapi::SYNC_STATUS_OK, |
| 257 ApplyRemoteChange(change, base::FilePath(), kDir)); | 257 ApplyRemoteChange(change, base::FilePath(), kDir)); |
| 258 | 258 |
| 259 // Now the directory must have deleted. | 259 // Now the directory must have deleted. |
| 260 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, | 260 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
| 261 file_system_->DirectoryExists(kDir)); | 261 file_system_->DirectoryExists(kDir)); |
| 262 } | 262 } |
| 263 | 263 |
| 264 TEST_F(LocalFileSyncServiceTest, LocalChangeObserver) { | 264 TEST_F(LocalFileSyncServiceTest, LocalChangeObserver) { |
| 265 const FileSystemURL kFile(file_system_->URL("file")); | 265 const FileSystemURL kFile(file_system_->URL("file")); |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 342 // Retrieve the expected platform_path. | 342 // Retrieve the expected platform_path. |
| 343 base::PlatformFileInfo info; | 343 base::PlatformFileInfo info; |
| 344 base::FilePath platform_path; | 344 base::FilePath platform_path; |
| 345 EXPECT_EQ(base::PLATFORM_FILE_OK, | 345 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 346 file_system_->GetMetadata(kFile, &info, &platform_path)); | 346 file_system_->GetMetadata(kFile, &info, &platform_path)); |
| 347 | 347 |
| 348 // The local_change_processor's ApplyLocalChange should be called once | 348 // The local_change_processor's ApplyLocalChange should be called once |
| 349 // with ADD_OR_UPDATE change for TYPE_FILE. | 349 // with ADD_OR_UPDATE change for TYPE_FILE. |
| 350 StrictMock<MockLocalChangeProcessor> local_change_processor; | 350 StrictMock<MockLocalChangeProcessor> local_change_processor; |
| 351 const FileChange change(FileChange::FILE_CHANGE_ADD_OR_UPDATE, | 351 const FileChange change(FileChange::FILE_CHANGE_ADD_OR_UPDATE, |
| 352 fileapi::SYNC_FILE_TYPE_FILE); | 352 SYNC_FILE_TYPE_FILE); |
| 353 EXPECT_CALL(local_change_processor, | 353 EXPECT_CALL(local_change_processor, |
| 354 ApplyLocalChange(change, platform_path, kFile, _)) | 354 ApplyLocalChange(change, platform_path, kFile, _)) |
| 355 .WillOnce(MockStatusCallback(fileapi::SYNC_STATUS_OK)); | 355 .WillOnce(MockStatusCallback(fileapi::SYNC_STATUS_OK)); |
| 356 | 356 |
| 357 local_service_->ProcessLocalChange( | 357 local_service_->ProcessLocalChange( |
| 358 &local_change_processor, | 358 &local_change_processor, |
| 359 base::Bind(&OnSyncCompleted, FROM_HERE, run_loop.QuitClosure(), | 359 base::Bind(&OnSyncCompleted, FROM_HERE, run_loop.QuitClosure(), |
| 360 fileapi::SYNC_STATUS_OK, kFile)); | 360 fileapi::SYNC_STATUS_OK, kFile)); |
| 361 | 361 |
| 362 run_loop.Run(); | 362 run_loop.Run(); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 379 | 379 |
| 380 // Creates and then deletes a file. | 380 // Creates and then deletes a file. |
| 381 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_->CreateFile(kFile)); | 381 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_->CreateFile(kFile)); |
| 382 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_->Remove(kFile, false)); | 382 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_->Remove(kFile, false)); |
| 383 | 383 |
| 384 // The local_change_processor's ApplyLocalChange should be called once | 384 // The local_change_processor's ApplyLocalChange should be called once |
| 385 // with DELETE change for TYPE_FILE. | 385 // with DELETE change for TYPE_FILE. |
| 386 // The file will NOT exist in the remote side and the processor might | 386 // The file will NOT exist in the remote side and the processor might |
| 387 // return SYNC_FILE_ERROR_NOT_FOUND (as mocked). | 387 // return SYNC_FILE_ERROR_NOT_FOUND (as mocked). |
| 388 StrictMock<MockLocalChangeProcessor> local_change_processor; | 388 StrictMock<MockLocalChangeProcessor> local_change_processor; |
| 389 const FileChange change(FileChange::FILE_CHANGE_DELETE, | 389 const FileChange change(FileChange::FILE_CHANGE_DELETE, SYNC_FILE_TYPE_FILE); |
| 390 fileapi::SYNC_FILE_TYPE_FILE); | |
| 391 EXPECT_CALL(local_change_processor, ApplyLocalChange(change, _, kFile, _)) | 390 EXPECT_CALL(local_change_processor, ApplyLocalChange(change, _, kFile, _)) |
| 392 .WillOnce(MockStatusCallback(fileapi::SYNC_FILE_ERROR_NOT_FOUND)); | 391 .WillOnce(MockStatusCallback(fileapi::SYNC_FILE_ERROR_NOT_FOUND)); |
| 393 | 392 |
| 394 // The sync should succeed anyway. | 393 // The sync should succeed anyway. |
| 395 local_service_->ProcessLocalChange( | 394 local_service_->ProcessLocalChange( |
| 396 &local_change_processor, | 395 &local_change_processor, |
| 397 base::Bind(&OnSyncCompleted, FROM_HERE, run_loop.QuitClosure(), | 396 base::Bind(&OnSyncCompleted, FROM_HERE, run_loop.QuitClosure(), |
| 398 fileapi::SYNC_STATUS_OK, kFile)); | 397 fileapi::SYNC_STATUS_OK, kFile)); |
| 399 | 398 |
| 400 run_loop.Run(); | 399 run_loop.Run(); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 467 &changes)); | 466 &changes)); |
| 468 | 467 |
| 469 local_service_->ProcessLocalChange( | 468 local_service_->ProcessLocalChange( |
| 470 &local_change_processor, | 469 &local_change_processor, |
| 471 base::Bind(&OnSyncCompleted, FROM_HERE, run_loop.QuitClosure(), | 470 base::Bind(&OnSyncCompleted, FROM_HERE, run_loop.QuitClosure(), |
| 472 fileapi::SYNC_STATUS_OK, kPath)); | 471 fileapi::SYNC_STATUS_OK, kPath)); |
| 473 | 472 |
| 474 run_loop.Run(); | 473 run_loop.Run(); |
| 475 | 474 |
| 476 EXPECT_EQ(2U, changes.size()); | 475 EXPECT_EQ(2U, changes.size()); |
| 477 EXPECT_EQ(FileChange(FileChange::FILE_CHANGE_DELETE, | 476 EXPECT_EQ(FileChange(FileChange::FILE_CHANGE_DELETE, SYNC_FILE_TYPE_FILE), |
| 478 fileapi::SYNC_FILE_TYPE_FILE), | |
| 479 changes[0]); | 477 changes[0]); |
| 480 EXPECT_EQ(FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE, | 478 EXPECT_EQ(FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE, |
| 481 fileapi::SYNC_FILE_TYPE_DIRECTORY), | 479 SYNC_FILE_TYPE_DIRECTORY), |
| 482 changes[1]); | 480 changes[1]); |
| 483 | 481 |
| 484 file_system_->RemoveSyncStatusObserver(&status_observer); | 482 file_system_->RemoveSyncStatusObserver(&status_observer); |
| 485 | 483 |
| 486 // We have one more change for kOther. | 484 // We have one more change for kOther. |
| 487 EXPECT_EQ(1, GetNumChangesInTracker()); | 485 EXPECT_EQ(1, GetNumChangesInTracker()); |
| 488 } | 486 } |
| 489 | 487 |
| 490 TEST_F(LocalFileSyncServiceTest, ProcessLocalChange_GetLocalMetadata) { | 488 TEST_F(LocalFileSyncServiceTest, ProcessLocalChange_GetLocalMetadata) { |
| 491 const FileSystemURL kURL(file_system_->URL("foo")); | 489 const FileSystemURL kURL(file_system_->URL("foo")); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 521 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_->CreateFile(kURL)); | 519 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_->CreateFile(kURL)); |
| 522 file_system_->ClearChangeForURLInTracker(kURL); | 520 file_system_->ClearChangeForURLInTracker(kURL); |
| 523 | 521 |
| 524 EXPECT_EQ(0, GetNumChangesInTracker()); | 522 EXPECT_EQ(0, GetNumChangesInTracker()); |
| 525 | 523 |
| 526 fileapi::FileSystemURLSet urlset; | 524 fileapi::FileSystemURLSet urlset; |
| 527 file_system_->GetChangedURLsInTracker(&urlset); | 525 file_system_->GetChangedURLsInTracker(&urlset); |
| 528 EXPECT_TRUE(urlset.empty()); | 526 EXPECT_TRUE(urlset.empty()); |
| 529 | 527 |
| 530 const FileChange change(FileChange::FILE_CHANGE_ADD_OR_UPDATE, | 528 const FileChange change(FileChange::FILE_CHANGE_ADD_OR_UPDATE, |
| 531 fileapi::SYNC_FILE_TYPE_FILE); | 529 SYNC_FILE_TYPE_FILE); |
| 532 | 530 |
| 533 // Call RecordFakeLocalChange to add an ADD_OR_UPDATE change. | 531 // Call RecordFakeLocalChange to add an ADD_OR_UPDATE change. |
| 534 { | 532 { |
| 535 base::RunLoop run_loop; | 533 base::RunLoop run_loop; |
| 536 SyncStatusCode status = fileapi::SYNC_STATUS_UNKNOWN; | 534 SyncStatusCode status = fileapi::SYNC_STATUS_UNKNOWN; |
| 537 local_service_->RecordFakeLocalChange( | 535 local_service_->RecordFakeLocalChange( |
| 538 kURL, change, AssignAndQuitCallback(&run_loop, &status)); | 536 kURL, change, AssignAndQuitCallback(&run_loop, &status)); |
| 539 run_loop.Run(); | 537 run_loop.Run(); |
| 540 EXPECT_EQ(fileapi::SYNC_STATUS_OK, status); | 538 EXPECT_EQ(fileapi::SYNC_STATUS_OK, status); |
| 541 } | 539 } |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 707 all_origins.insert(kOrigin2); | 705 all_origins.insert(kOrigin2); |
| 708 all_origins.insert(kOrigin3); | 706 all_origins.insert(kOrigin3); |
| 709 while (!all_origins.empty()) { | 707 while (!all_origins.empty()) { |
| 710 ASSERT_TRUE(NextOriginToProcess(&origin)); | 708 ASSERT_TRUE(NextOriginToProcess(&origin)); |
| 711 ASSERT_TRUE(ContainsKey(all_origins, origin)); | 709 ASSERT_TRUE(ContainsKey(all_origins, origin)); |
| 712 all_origins.erase(origin); | 710 all_origins.erase(origin); |
| 713 } | 711 } |
| 714 } | 712 } |
| 715 | 713 |
| 716 } // namespace sync_file_system | 714 } // namespace sync_file_system |
| OLD | NEW |