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" |
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
307 file_system2.TearDown(); | 307 file_system2.TearDown(); |
308 } | 308 } |
309 | 309 |
310 TEST_F(LocalFileSyncServiceTest, ProcessLocalChange_CreateFile) { | 310 TEST_F(LocalFileSyncServiceTest, ProcessLocalChange_CreateFile) { |
311 const FileSystemURL kFile(file_system_->URL("foo")); | 311 const FileSystemURL kFile(file_system_->URL("foo")); |
312 const char kTestFileData[] = "0123456789"; | 312 const char kTestFileData[] = "0123456789"; |
313 const int kTestFileDataSize = static_cast<int>(arraysize(kTestFileData) - 1); | 313 const int kTestFileDataSize = static_cast<int>(arraysize(kTestFileData) - 1); |
314 | 314 |
315 base::RunLoop run_loop; | 315 base::RunLoop run_loop; |
316 | 316 |
317 // We should get called OnSyncEnabled and OnWriteEnabled on kFile. | 317 // We should get called OnSyncEnabled on kFile. |
318 // (We quit the run loop when OnWriteEnabled is called on kFile) | |
319 StrictMock<MockSyncStatusObserver> status_observer; | 318 StrictMock<MockSyncStatusObserver> status_observer; |
320 EXPECT_CALL(status_observer, OnSyncEnabled(kFile)) | 319 EXPECT_CALL(status_observer, OnSyncEnabled(kFile)) |
321 .Times(AtLeast(1)); | 320 .Times(AtLeast(1)); |
322 EXPECT_CALL(status_observer, OnWriteEnabled(kFile)) | |
323 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); | |
324 file_system_->AddSyncStatusObserver(&status_observer); | 321 file_system_->AddSyncStatusObserver(&status_observer); |
325 | 322 |
326 // Creates and writes into a file. | 323 // Creates and writes into a file. |
327 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_->CreateFile(kFile)); | 324 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_->CreateFile(kFile)); |
328 EXPECT_EQ(kTestFileDataSize, | 325 EXPECT_EQ(kTestFileDataSize, |
329 file_system_->WriteString(kFile, std::string(kTestFileData))); | 326 file_system_->WriteString(kFile, std::string(kTestFileData))); |
330 | 327 |
331 // Retrieve the file metadata to set up the expected values. | 328 // Retrieve the file metadata to set up the expected values. |
332 base::PlatformFileInfo info; | 329 base::PlatformFileInfo info; |
333 FilePath platform_path; | 330 FilePath platform_path; |
(...skipping 12 matching lines...) Expand all Loading... |
346 // with ADD_OR_UPDATE change for TYPE_FILE. | 343 // with ADD_OR_UPDATE change for TYPE_FILE. |
347 StrictMock<MockLocalChangeProcessor> local_change_processor; | 344 StrictMock<MockLocalChangeProcessor> local_change_processor; |
348 const FileChange change(FileChange::FILE_CHANGE_ADD_OR_UPDATE, | 345 const FileChange change(FileChange::FILE_CHANGE_ADD_OR_UPDATE, |
349 fileapi::SYNC_FILE_TYPE_FILE); | 346 fileapi::SYNC_FILE_TYPE_FILE); |
350 EXPECT_CALL(local_change_processor, | 347 EXPECT_CALL(local_change_processor, |
351 ApplyLocalChange(change, platform_path, metadata, kFile, _)) | 348 ApplyLocalChange(change, platform_path, metadata, kFile, _)) |
352 .WillOnce(MockStatusCallback(fileapi::SYNC_STATUS_OK)); | 349 .WillOnce(MockStatusCallback(fileapi::SYNC_STATUS_OK)); |
353 | 350 |
354 local_service_->ProcessLocalChange( | 351 local_service_->ProcessLocalChange( |
355 &local_change_processor, | 352 &local_change_processor, |
356 base::Bind(&OnSyncCompleted, FROM_HERE, base::Bind(&base::DoNothing), | 353 base::Bind(&OnSyncCompleted, FROM_HERE, run_loop.QuitClosure(), |
357 fileapi::SYNC_STATUS_OK, kFile)); | 354 fileapi::SYNC_STATUS_OK, kFile)); |
358 | 355 |
359 run_loop.Run(); | 356 run_loop.Run(); |
360 | 357 |
361 file_system_->RemoveSyncStatusObserver(&status_observer); | 358 file_system_->RemoveSyncStatusObserver(&status_observer); |
362 | 359 |
363 EXPECT_EQ(0, GetNumChangesInTracker()); | 360 EXPECT_EQ(0, GetNumChangesInTracker()); |
364 } | 361 } |
365 | 362 |
366 TEST_F(LocalFileSyncServiceTest, ProcessLocalChange_CreateAndRemoveFile) { | 363 TEST_F(LocalFileSyncServiceTest, ProcessLocalChange_CreateAndRemoveFile) { |
367 const FileSystemURL kFile(file_system_->URL("foo")); | 364 const FileSystemURL kFile(file_system_->URL("foo")); |
368 | 365 |
369 base::RunLoop run_loop; | 366 base::RunLoop run_loop; |
370 | 367 |
371 // We should get called OnSyncEnabled and OnWriteEnabled on kFile. | 368 // We should get called OnSyncEnabled and OnWriteEnabled on kFile. |
372 // (We quit the run loop when OnWriteEnabled is called on kFile) | |
373 StrictMock<MockSyncStatusObserver> status_observer; | 369 StrictMock<MockSyncStatusObserver> status_observer; |
374 EXPECT_CALL(status_observer, OnSyncEnabled(kFile)) | 370 EXPECT_CALL(status_observer, OnSyncEnabled(kFile)) |
375 .Times(AtLeast(1)); | 371 .Times(AtLeast(1)); |
376 EXPECT_CALL(status_observer, OnWriteEnabled(kFile)) | |
377 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); | |
378 file_system_->AddSyncStatusObserver(&status_observer); | 372 file_system_->AddSyncStatusObserver(&status_observer); |
379 | 373 |
380 // Creates and then deletes a file. | 374 // Creates and then deletes a file. |
381 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_->CreateFile(kFile)); | 375 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_->CreateFile(kFile)); |
382 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_->Remove(kFile, false)); | 376 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_->Remove(kFile, false)); |
383 | 377 |
384 // The local_change_processor's ApplyLocalChange should be called once | 378 // The local_change_processor's ApplyLocalChange should be called once |
385 // with DELETE change for TYPE_FILE. | 379 // with DELETE change for TYPE_FILE. |
386 // The file will NOT exist in the remote side and the processor might | 380 // The file will NOT exist in the remote side and the processor might |
387 // return SYNC_FILE_ERROR_NOT_FOUND (as mocked). | 381 // return SYNC_FILE_ERROR_NOT_FOUND (as mocked). |
388 StrictMock<MockLocalChangeProcessor> local_change_processor; | 382 StrictMock<MockLocalChangeProcessor> local_change_processor; |
389 const FileChange change(FileChange::FILE_CHANGE_DELETE, | 383 const FileChange change(FileChange::FILE_CHANGE_DELETE, |
390 fileapi::SYNC_FILE_TYPE_FILE); | 384 fileapi::SYNC_FILE_TYPE_FILE); |
391 EXPECT_CALL(local_change_processor, ApplyLocalChange(change, _, _, kFile, _)) | 385 EXPECT_CALL(local_change_processor, ApplyLocalChange(change, _, _, kFile, _)) |
392 .WillOnce(MockStatusCallback(fileapi::SYNC_FILE_ERROR_NOT_FOUND)); | 386 .WillOnce(MockStatusCallback(fileapi::SYNC_FILE_ERROR_NOT_FOUND)); |
393 | 387 |
394 // The sync should succeed anyway. | 388 // The sync should succeed anyway. |
395 local_service_->ProcessLocalChange( | 389 local_service_->ProcessLocalChange( |
396 &local_change_processor, | 390 &local_change_processor, |
397 base::Bind(&OnSyncCompleted, FROM_HERE, base::Bind(&base::DoNothing), | 391 base::Bind(&OnSyncCompleted, FROM_HERE, run_loop.QuitClosure(), |
398 fileapi::SYNC_STATUS_OK, kFile)); | 392 fileapi::SYNC_STATUS_OK, kFile)); |
399 | 393 |
400 run_loop.Run(); | 394 run_loop.Run(); |
401 | 395 |
402 file_system_->RemoveSyncStatusObserver(&status_observer); | 396 file_system_->RemoveSyncStatusObserver(&status_observer); |
403 | 397 |
404 EXPECT_EQ(0, GetNumChangesInTracker()); | 398 EXPECT_EQ(0, GetNumChangesInTracker()); |
405 } | 399 } |
406 | 400 |
407 TEST_F(LocalFileSyncServiceTest, ProcessLocalChange_CreateAndRemoveDirectory) { | 401 TEST_F(LocalFileSyncServiceTest, ProcessLocalChange_CreateAndRemoveDirectory) { |
408 const FileSystemURL kDir(file_system_->URL("foo")); | 402 const FileSystemURL kDir(file_system_->URL("foo")); |
409 | 403 |
410 base::RunLoop run_loop; | 404 base::RunLoop run_loop; |
411 | 405 |
412 // OnSyncEnabled is expected to be called at least or more than once | 406 // OnSyncEnabled is expected to be called at least or more than once. |
413 // but OnWriteEnabled will never be called. | |
414 StrictMock<MockSyncStatusObserver> status_observer; | 407 StrictMock<MockSyncStatusObserver> status_observer; |
415 EXPECT_CALL(status_observer, OnSyncEnabled(kDir)).Times(AtLeast(1)); | 408 EXPECT_CALL(status_observer, OnSyncEnabled(kDir)).Times(AtLeast(1)); |
416 file_system_->AddSyncStatusObserver(&status_observer); | 409 file_system_->AddSyncStatusObserver(&status_observer); |
417 | 410 |
418 // Creates and then deletes a directory. | 411 // Creates and then deletes a directory. |
419 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_->CreateDirectory(kDir)); | 412 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_->CreateDirectory(kDir)); |
420 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_->Remove(kDir, false)); | 413 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_->Remove(kDir, false)); |
421 | 414 |
422 // The local_change_processor's ApplyLocalChange should never be called. | 415 // The local_change_processor's ApplyLocalChange should never be called. |
423 StrictMock<MockLocalChangeProcessor> local_change_processor; | 416 StrictMock<MockLocalChangeProcessor> local_change_processor; |
(...skipping 11 matching lines...) Expand all Loading... |
435 } | 428 } |
436 | 429 |
437 TEST_F(LocalFileSyncServiceTest, ProcessLocalChange_MultipleChanges) { | 430 TEST_F(LocalFileSyncServiceTest, ProcessLocalChange_MultipleChanges) { |
438 const FileSystemURL kPath(file_system_->URL("foo")); | 431 const FileSystemURL kPath(file_system_->URL("foo")); |
439 const FileSystemURL kOther(file_system_->URL("bar")); | 432 const FileSystemURL kOther(file_system_->URL("bar")); |
440 | 433 |
441 base::RunLoop run_loop; | 434 base::RunLoop run_loop; |
442 | 435 |
443 // We should get called OnSyncEnabled and OnWriteEnabled on kPath and | 436 // We should get called OnSyncEnabled and OnWriteEnabled on kPath and |
444 // OnSyncEnabled on kOther. | 437 // OnSyncEnabled on kOther. |
445 // (We quit the run loop when OnWriteEnabled is called on kPath) | |
446 StrictMock<MockSyncStatusObserver> status_observer; | 438 StrictMock<MockSyncStatusObserver> status_observer; |
447 EXPECT_CALL(status_observer, OnSyncEnabled(kPath)).Times(AtLeast(1)); | 439 EXPECT_CALL(status_observer, OnSyncEnabled(kPath)).Times(AtLeast(1)); |
448 EXPECT_CALL(status_observer, OnSyncEnabled(kOther)).Times(AtLeast(1)); | 440 EXPECT_CALL(status_observer, OnSyncEnabled(kOther)).Times(AtLeast(1)); |
449 EXPECT_CALL(status_observer, OnWriteEnabled(kPath)) | |
450 .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); | |
451 file_system_->AddSyncStatusObserver(&status_observer); | 441 file_system_->AddSyncStatusObserver(&status_observer); |
452 | 442 |
453 // Creates a file, delete the file and creates a directory with the same | 443 // Creates a file, delete the file and creates a directory with the same |
454 // name. | 444 // name. |
455 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_->CreateFile(kPath)); | 445 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_->CreateFile(kPath)); |
456 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_->Remove(kPath, false)); | 446 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_->Remove(kPath, false)); |
457 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_->CreateDirectory(kPath)); | 447 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_->CreateDirectory(kPath)); |
458 | 448 |
459 // Creates one more file. | 449 // Creates one more file. |
460 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_->CreateFile(kOther)); | 450 EXPECT_EQ(base::PLATFORM_FILE_OK, file_system_->CreateFile(kOther)); |
461 | 451 |
462 // The local_change_processor's ApplyLocalChange will be called | 452 // The local_change_processor's ApplyLocalChange will be called |
463 // twice for FILE_TYPE and FILE_DIRECTORY. | 453 // twice for FILE_TYPE and FILE_DIRECTORY. |
464 StrictMock<MockLocalChangeProcessor> local_change_processor; | 454 StrictMock<MockLocalChangeProcessor> local_change_processor; |
465 std::vector<FileChange> changes; | 455 std::vector<FileChange> changes; |
466 EXPECT_CALL(local_change_processor, ApplyLocalChange(_, _, _, kPath, _)) | 456 EXPECT_CALL(local_change_processor, ApplyLocalChange(_, _, _, kPath, _)) |
467 .Times(2) | 457 .Times(2) |
468 .WillOnce(MockStatusCallbackAndRecordChange(fileapi::SYNC_STATUS_OK, | 458 .WillOnce(MockStatusCallbackAndRecordChange(fileapi::SYNC_STATUS_OK, |
469 &changes)) | 459 &changes)) |
470 .WillOnce(MockStatusCallbackAndRecordChange(fileapi::SYNC_STATUS_OK, | 460 .WillOnce(MockStatusCallbackAndRecordChange(fileapi::SYNC_STATUS_OK, |
471 &changes)); | 461 &changes)); |
472 | 462 |
473 local_service_->ProcessLocalChange( | 463 local_service_->ProcessLocalChange( |
474 &local_change_processor, | 464 &local_change_processor, |
475 base::Bind(&OnSyncCompleted, FROM_HERE, base::Bind(&base::DoNothing), | 465 base::Bind(&OnSyncCompleted, FROM_HERE, run_loop.QuitClosure(), |
476 fileapi::SYNC_STATUS_OK, kPath)); | 466 fileapi::SYNC_STATUS_OK, kPath)); |
477 | 467 |
478 run_loop.Run(); | 468 run_loop.Run(); |
479 | 469 |
480 EXPECT_EQ(2U, changes.size()); | 470 EXPECT_EQ(2U, changes.size()); |
481 EXPECT_EQ(FileChange(FileChange::FILE_CHANGE_DELETE, | 471 EXPECT_EQ(FileChange(FileChange::FILE_CHANGE_DELETE, |
482 fileapi::SYNC_FILE_TYPE_FILE), | 472 fileapi::SYNC_FILE_TYPE_FILE), |
483 changes[0]); | 473 changes[0]); |
484 EXPECT_EQ(FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE, | 474 EXPECT_EQ(FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE, |
485 fileapi::SYNC_FILE_TYPE_DIRECTORY), | 475 fileapi::SYNC_FILE_TYPE_DIRECTORY), |
(...skipping 29 matching lines...) Expand all Loading... |
515 | 505 |
516 EXPECT_EQ(fileapi::SYNC_STATUS_OK, status); | 506 EXPECT_EQ(fileapi::SYNC_STATUS_OK, status); |
517 EXPECT_EQ(kTime, metadata.last_modified); | 507 EXPECT_EQ(kTime, metadata.last_modified); |
518 EXPECT_EQ(kSize, metadata.size); | 508 EXPECT_EQ(kSize, metadata.size); |
519 } | 509 } |
520 | 510 |
521 // TODO(kinuko): Add tests for multiple file changes and multiple | 511 // TODO(kinuko): Add tests for multiple file changes and multiple |
522 // FileSystemContexts. | 512 // FileSystemContexts. |
523 | 513 |
524 } // namespace sync_file_system | 514 } // namespace sync_file_system |
OLD | NEW |