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 "chrome/browser/chromeos/drive/file_cache.h" | 5 #include "chrome/browser/chromeos/drive/file_cache.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 25 matching lines...) Expand all Loading... |
36 out_cache_entries->push_back(cache_entry); | 36 out_cache_entries->push_back(cache_entry); |
37 } | 37 } |
38 | 38 |
39 // Called upon completion of Iterate(). | 39 // Called upon completion of Iterate(). |
40 void OnIterateCompleted(bool* out_is_called) { | 40 void OnIterateCompleted(bool* out_is_called) { |
41 *out_is_called = true; | 41 *out_is_called = true; |
42 } | 42 } |
43 | 43 |
44 } // namespace | 44 } // namespace |
45 | 45 |
46 class FileCacheTest : public testing::Test { | 46 // Tests FileCache methods from UI thread. |
| 47 class FileCacheTestOnUIThread : public testing::Test { |
47 protected: | 48 protected: |
48 FileCacheTest() | 49 FileCacheTestOnUIThread() |
49 : ui_thread_(content::BrowserThread::UI, &message_loop_), | 50 : ui_thread_(content::BrowserThread::UI, &message_loop_), |
50 expected_error_(FILE_ERROR_OK), | 51 expected_error_(FILE_ERROR_OK), |
51 expected_cache_state_(0), | 52 expected_cache_state_(0), |
52 expected_sub_dir_type_(FileCache::CACHE_TYPE_META), | 53 expected_sub_dir_type_(FileCache::CACHE_TYPE_META), |
53 expected_success_(true) { | 54 expected_success_(true) { |
54 } | 55 } |
55 | 56 |
56 virtual void SetUp() OVERRIDE { | 57 virtual void SetUp() OVERRIDE { |
57 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 58 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
58 fake_free_disk_space_getter_.reset(new FakeFreeDiskSpaceGetter); | 59 fake_free_disk_space_getter_.reset(new FakeFreeDiskSpaceGetter); |
(...skipping 445 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
504 scoped_ptr<FakeFreeDiskSpaceGetter> fake_free_disk_space_getter_; | 505 scoped_ptr<FakeFreeDiskSpaceGetter> fake_free_disk_space_getter_; |
505 scoped_ptr<StrictMock<MockCacheObserver> > mock_cache_observer_; | 506 scoped_ptr<StrictMock<MockCacheObserver> > mock_cache_observer_; |
506 | 507 |
507 FileError expected_error_; | 508 FileError expected_error_; |
508 int expected_cache_state_; | 509 int expected_cache_state_; |
509 FileCache::CacheSubDirectoryType expected_sub_dir_type_; | 510 FileCache::CacheSubDirectoryType expected_sub_dir_type_; |
510 bool expected_success_; | 511 bool expected_success_; |
511 std::string expected_file_extension_; | 512 std::string expected_file_extension_; |
512 }; | 513 }; |
513 | 514 |
514 TEST_F(FileCacheTest, GetCacheFilePath) { | 515 TEST_F(FileCacheTestOnUIThread, GetCacheFilePath) { |
515 // Use alphanumeric characters for resource id. | 516 // Use alphanumeric characters for resource id. |
516 std::string resource_id("pdf:1a2b"); | 517 std::string resource_id("pdf:1a2b"); |
517 std::string md5("abcdef0123456789"); | 518 std::string md5("abcdef0123456789"); |
518 TestGetCacheFilePath(resource_id, md5, | 519 TestGetCacheFilePath(resource_id, md5, |
519 resource_id + base::FilePath::kExtensionSeparator + md5); | 520 resource_id + base::FilePath::kExtensionSeparator + md5); |
520 | 521 |
521 // Use non-alphanumeric characters for resource id, including '.' which is an | 522 // Use non-alphanumeric characters for resource id, including '.' which is an |
522 // extension separator, to test that the characters are escaped and unescaped | 523 // extension separator, to test that the characters are escaped and unescaped |
523 // correctly, and '.' doesn't mess up the filename format and operations. | 524 // correctly, and '.' doesn't mess up the filename format and operations. |
524 resource_id = "pdf:`~!@#$%^&*()-_=+[{|]}\\;',<.>/?"; | 525 resource_id = "pdf:`~!@#$%^&*()-_=+[{|]}\\;',<.>/?"; |
525 std::string escaped_resource_id = util::EscapeCacheFileName(resource_id); | 526 std::string escaped_resource_id = util::EscapeCacheFileName(resource_id); |
526 std::string escaped_md5 = util::EscapeCacheFileName(md5); | 527 std::string escaped_md5 = util::EscapeCacheFileName(md5); |
527 TestGetCacheFilePath( | 528 TestGetCacheFilePath( |
528 resource_id, md5, escaped_resource_id + | 529 resource_id, md5, escaped_resource_id + |
529 base::FilePath::kExtensionSeparator + escaped_md5); | 530 base::FilePath::kExtensionSeparator + escaped_md5); |
530 } | 531 } |
531 | 532 |
532 TEST_F(FileCacheTest, StoreToCacheSimple) { | 533 TEST_F(FileCacheTestOnUIThread, StoreToCacheSimple) { |
533 std::string resource_id("pdf:1a2b"); | 534 std::string resource_id("pdf:1a2b"); |
534 std::string md5("abcdef0123456789"); | 535 std::string md5("abcdef0123456789"); |
535 | 536 |
536 // Store an existing file. | 537 // Store an existing file. |
537 TestStoreToCache( | 538 TestStoreToCache( |
538 resource_id, md5, | 539 resource_id, md5, |
539 google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), | 540 google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), |
540 FILE_ERROR_OK, test_util::TEST_CACHE_STATE_PRESENT, | 541 FILE_ERROR_OK, test_util::TEST_CACHE_STATE_PRESENT, |
541 FileCache::CACHE_TYPE_TMP); | 542 FileCache::CACHE_TYPE_TMP); |
542 | 543 |
(...skipping 10 matching lines...) Expand all Loading... |
553 resource_id, md5, | 554 resource_id, md5, |
554 google_apis::test_util::GetTestFilePath("chromeos/gdata/empty_feed.json"), | 555 google_apis::test_util::GetTestFilePath("chromeos/gdata/empty_feed.json"), |
555 FILE_ERROR_OK, test_util::TEST_CACHE_STATE_PRESENT, | 556 FILE_ERROR_OK, test_util::TEST_CACHE_STATE_PRESENT, |
556 FileCache::CACHE_TYPE_TMP); | 557 FileCache::CACHE_TYPE_TMP); |
557 | 558 |
558 // Verify that there's only one file with name <resource_id>, i.e. previously | 559 // Verify that there's only one file with name <resource_id>, i.e. previously |
559 // cached file with the different md5 should be deleted. | 560 // cached file with the different md5 should be deleted. |
560 EXPECT_EQ(1U, CountCacheFiles(resource_id, md5)); | 561 EXPECT_EQ(1U, CountCacheFiles(resource_id, md5)); |
561 } | 562 } |
562 | 563 |
563 TEST_F(FileCacheTest, LocallyModifiedSimple) { | 564 TEST_F(FileCacheTestOnUIThread, LocallyModifiedSimple) { |
564 std::string resource_id("pdf:1a2b"); | 565 std::string resource_id("pdf:1a2b"); |
565 std::string md5("abcdef0123456789"); | 566 std::string md5("abcdef0123456789"); |
566 | 567 |
567 const int kDirtyCacheState = | 568 const int kDirtyCacheState = |
568 test_util::TEST_CACHE_STATE_PRESENT | | 569 test_util::TEST_CACHE_STATE_PRESENT | |
569 test_util::TEST_CACHE_STATE_DIRTY | | 570 test_util::TEST_CACHE_STATE_DIRTY | |
570 test_util::TEST_CACHE_STATE_PERSISTENT; | 571 test_util::TEST_CACHE_STATE_PERSISTENT; |
571 | 572 |
572 EXPECT_CALL(*mock_cache_observer_, OnCacheCommitted(resource_id)).Times(1); | 573 EXPECT_CALL(*mock_cache_observer_, OnCacheCommitted(resource_id)).Times(1); |
573 TestStoreLocallyModifiedToCache( | 574 TestStoreLocallyModifiedToCache( |
574 resource_id, md5, | 575 resource_id, md5, |
575 google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), | 576 google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), |
576 FILE_ERROR_OK, kDirtyCacheState, FileCache::CACHE_TYPE_PERSISTENT); | 577 FILE_ERROR_OK, kDirtyCacheState, FileCache::CACHE_TYPE_PERSISTENT); |
577 } | 578 } |
578 | 579 |
579 TEST_F(FileCacheTest, GetFromCacheSimple) { | 580 TEST_F(FileCacheTestOnUIThread, GetFromCacheSimple) { |
580 std::string resource_id("pdf:1a2b"); | 581 std::string resource_id("pdf:1a2b"); |
581 std::string md5("abcdef0123456789"); | 582 std::string md5("abcdef0123456789"); |
582 // First store a file to cache. | 583 // First store a file to cache. |
583 TestStoreToCache( | 584 TestStoreToCache( |
584 resource_id, md5, | 585 resource_id, md5, |
585 google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), | 586 google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), |
586 FILE_ERROR_OK, test_util::TEST_CACHE_STATE_PRESENT, | 587 FILE_ERROR_OK, test_util::TEST_CACHE_STATE_PRESENT, |
587 FileCache::CACHE_TYPE_TMP); | 588 FileCache::CACHE_TYPE_TMP); |
588 | 589 |
589 // Then try to get the existing file from cache. | 590 // Then try to get the existing file from cache. |
590 TestGetFileFromCacheByResourceIdAndMd5( | 591 TestGetFileFromCacheByResourceIdAndMd5( |
591 resource_id, md5, FILE_ERROR_OK, md5); | 592 resource_id, md5, FILE_ERROR_OK, md5); |
592 | 593 |
593 // Get file from cache with same resource id as existing file but different | 594 // Get file from cache with same resource id as existing file but different |
594 // md5. | 595 // md5. |
595 TestGetFileFromCacheByResourceIdAndMd5( | 596 TestGetFileFromCacheByResourceIdAndMd5( |
596 resource_id, "9999", FILE_ERROR_NOT_FOUND, md5); | 597 resource_id, "9999", FILE_ERROR_NOT_FOUND, md5); |
597 | 598 |
598 // Get file from cache with different resource id from existing file but same | 599 // Get file from cache with different resource id from existing file but same |
599 // md5. | 600 // md5. |
600 resource_id = "document:1a2b"; | 601 resource_id = "document:1a2b"; |
601 TestGetFileFromCacheByResourceIdAndMd5( | 602 TestGetFileFromCacheByResourceIdAndMd5( |
602 resource_id, md5, FILE_ERROR_NOT_FOUND, md5); | 603 resource_id, md5, FILE_ERROR_NOT_FOUND, md5); |
603 } | 604 } |
604 | 605 |
605 TEST_F(FileCacheTest, RemoveFromCacheSimple) { | 606 TEST_F(FileCacheTestOnUIThread, RemoveFromCacheSimple) { |
606 // Use alphanumeric characters for resource id. | 607 // Use alphanumeric characters for resource id. |
607 std::string resource_id("pdf:1a2b"); | 608 std::string resource_id("pdf:1a2b"); |
608 std::string md5("abcdef0123456789"); | 609 std::string md5("abcdef0123456789"); |
609 // First store a file to cache. | 610 // First store a file to cache. |
610 TestStoreToCache( | 611 TestStoreToCache( |
611 resource_id, md5, | 612 resource_id, md5, |
612 google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), | 613 google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), |
613 FILE_ERROR_OK, test_util::TEST_CACHE_STATE_PRESENT, | 614 FILE_ERROR_OK, test_util::TEST_CACHE_STATE_PRESENT, |
614 FileCache::CACHE_TYPE_TMP); | 615 FileCache::CACHE_TYPE_TMP); |
615 | 616 |
616 // Then try to remove existing file from cache. | 617 // Then try to remove existing file from cache. |
617 TestRemoveFromCache(resource_id, FILE_ERROR_OK); | 618 TestRemoveFromCache(resource_id, FILE_ERROR_OK); |
618 | 619 |
619 // Repeat using non-alphanumeric characters for resource id, including '.' | 620 // Repeat using non-alphanumeric characters for resource id, including '.' |
620 // which is an extension separator. | 621 // which is an extension separator. |
621 resource_id = "pdf:`~!@#$%^&*()-_=+[{|]}\\;',<.>/?"; | 622 resource_id = "pdf:`~!@#$%^&*()-_=+[{|]}\\;',<.>/?"; |
622 TestStoreToCache( | 623 TestStoreToCache( |
623 resource_id, md5, | 624 resource_id, md5, |
624 google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), | 625 google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), |
625 FILE_ERROR_OK, test_util::TEST_CACHE_STATE_PRESENT, | 626 FILE_ERROR_OK, test_util::TEST_CACHE_STATE_PRESENT, |
626 FileCache::CACHE_TYPE_TMP); | 627 FileCache::CACHE_TYPE_TMP); |
627 | 628 |
628 TestRemoveFromCache(resource_id, FILE_ERROR_OK); | 629 TestRemoveFromCache(resource_id, FILE_ERROR_OK); |
629 } | 630 } |
630 | 631 |
631 TEST_F(FileCacheTest, PinAndUnpin) { | 632 TEST_F(FileCacheTestOnUIThread, PinAndUnpin) { |
632 std::string resource_id("pdf:1a2b"); | 633 std::string resource_id("pdf:1a2b"); |
633 std::string md5("abcdef0123456789"); | 634 std::string md5("abcdef0123456789"); |
634 EXPECT_CALL(*mock_cache_observer_, OnCachePinned(resource_id, md5)).Times(2); | 635 EXPECT_CALL(*mock_cache_observer_, OnCachePinned(resource_id, md5)).Times(2); |
635 EXPECT_CALL(*mock_cache_observer_, OnCacheUnpinned(resource_id, md5)) | 636 EXPECT_CALL(*mock_cache_observer_, OnCacheUnpinned(resource_id, md5)) |
636 .Times(1); | 637 .Times(1); |
637 | 638 |
638 // First store a file to cache. | 639 // First store a file to cache. |
639 TestStoreToCache( | 640 TestStoreToCache( |
640 resource_id, md5, | 641 resource_id, md5, |
641 google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), | 642 google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
681 resource_id = "not-in-cache:1a2b"; | 682 resource_id = "not-in-cache:1a2b"; |
682 // Because unpinning will fail, OnCacheUnpinned() won't be run. | 683 // Because unpinning will fail, OnCacheUnpinned() won't be run. |
683 EXPECT_CALL(*mock_cache_observer_, OnCacheUnpinned(resource_id, md5)) | 684 EXPECT_CALL(*mock_cache_observer_, OnCacheUnpinned(resource_id, md5)) |
684 .Times(0); | 685 .Times(0); |
685 | 686 |
686 TestUnpin(resource_id, md5, FILE_ERROR_NOT_FOUND, | 687 TestUnpin(resource_id, md5, FILE_ERROR_NOT_FOUND, |
687 test_util::TEST_CACHE_STATE_NONE, | 688 test_util::TEST_CACHE_STATE_NONE, |
688 FileCache::CACHE_TYPE_TMP /* non-applicable */); | 689 FileCache::CACHE_TYPE_TMP /* non-applicable */); |
689 } | 690 } |
690 | 691 |
691 TEST_F(FileCacheTest, StoreToCachePinned) { | 692 TEST_F(FileCacheTestOnUIThread, StoreToCachePinned) { |
692 std::string resource_id("pdf:1a2b"); | 693 std::string resource_id("pdf:1a2b"); |
693 std::string md5("abcdef0123456789"); | 694 std::string md5("abcdef0123456789"); |
694 EXPECT_CALL(*mock_cache_observer_, OnCachePinned(resource_id, md5)).Times(1); | 695 EXPECT_CALL(*mock_cache_observer_, OnCachePinned(resource_id, md5)).Times(1); |
695 | 696 |
696 // Pin a non-existent file. | 697 // Pin a non-existent file. |
697 TestPin(resource_id, md5, FILE_ERROR_OK, | 698 TestPin(resource_id, md5, FILE_ERROR_OK, |
698 test_util::TEST_CACHE_STATE_PINNED, | 699 test_util::TEST_CACHE_STATE_PINNED, |
699 FileCache::CACHE_TYPE_TMP); | 700 FileCache::CACHE_TYPE_TMP); |
700 | 701 |
701 // Store an existing file to a previously pinned file. | 702 // Store an existing file to a previously pinned file. |
702 TestStoreToCache( | 703 TestStoreToCache( |
703 resource_id, md5, | 704 resource_id, md5, |
704 google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), | 705 google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), |
705 FILE_ERROR_OK, | 706 FILE_ERROR_OK, |
706 test_util::TEST_CACHE_STATE_PRESENT | | 707 test_util::TEST_CACHE_STATE_PRESENT | |
707 test_util::TEST_CACHE_STATE_PINNED | | 708 test_util::TEST_CACHE_STATE_PINNED | |
708 test_util::TEST_CACHE_STATE_PERSISTENT, | 709 test_util::TEST_CACHE_STATE_PERSISTENT, |
709 FileCache::CACHE_TYPE_PERSISTENT); | 710 FileCache::CACHE_TYPE_PERSISTENT); |
710 | 711 |
711 // Store a non-existent file to a previously pinned and stored file. | 712 // Store a non-existent file to a previously pinned and stored file. |
712 TestStoreToCache(resource_id, md5, base::FilePath("./non_existent.json"), | 713 TestStoreToCache(resource_id, md5, base::FilePath("./non_existent.json"), |
713 FILE_ERROR_FAILED, | 714 FILE_ERROR_FAILED, |
714 test_util::TEST_CACHE_STATE_PRESENT | | 715 test_util::TEST_CACHE_STATE_PRESENT | |
715 test_util::TEST_CACHE_STATE_PINNED | | 716 test_util::TEST_CACHE_STATE_PINNED | |
716 test_util::TEST_CACHE_STATE_PERSISTENT, | 717 test_util::TEST_CACHE_STATE_PERSISTENT, |
717 FileCache::CACHE_TYPE_PERSISTENT); | 718 FileCache::CACHE_TYPE_PERSISTENT); |
718 } | 719 } |
719 | 720 |
720 TEST_F(FileCacheTest, GetFromCachePinned) { | 721 TEST_F(FileCacheTestOnUIThread, GetFromCachePinned) { |
721 std::string resource_id("pdf:1a2b"); | 722 std::string resource_id("pdf:1a2b"); |
722 std::string md5("abcdef0123456789"); | 723 std::string md5("abcdef0123456789"); |
723 EXPECT_CALL(*mock_cache_observer_, OnCachePinned(resource_id, md5)).Times(1); | 724 EXPECT_CALL(*mock_cache_observer_, OnCachePinned(resource_id, md5)).Times(1); |
724 | 725 |
725 // Pin a non-existent file. | 726 // Pin a non-existent file. |
726 TestPin(resource_id, md5, FILE_ERROR_OK, | 727 TestPin(resource_id, md5, FILE_ERROR_OK, |
727 test_util::TEST_CACHE_STATE_PINNED, | 728 test_util::TEST_CACHE_STATE_PINNED, |
728 FileCache::CACHE_TYPE_TMP); | 729 FileCache::CACHE_TYPE_TMP); |
729 | 730 |
730 // Get the non-existent pinned file from cache. | 731 // Get the non-existent pinned file from cache. |
731 TestGetFileFromCacheByResourceIdAndMd5( | 732 TestGetFileFromCacheByResourceIdAndMd5( |
732 resource_id, md5, FILE_ERROR_NOT_FOUND, md5); | 733 resource_id, md5, FILE_ERROR_NOT_FOUND, md5); |
733 | 734 |
734 // Store an existing file to the previously pinned non-existent file. | 735 // Store an existing file to the previously pinned non-existent file. |
735 TestStoreToCache( | 736 TestStoreToCache( |
736 resource_id, md5, | 737 resource_id, md5, |
737 google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), | 738 google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), |
738 FILE_ERROR_OK, | 739 FILE_ERROR_OK, |
739 test_util::TEST_CACHE_STATE_PRESENT | | 740 test_util::TEST_CACHE_STATE_PRESENT | |
740 test_util::TEST_CACHE_STATE_PINNED | | 741 test_util::TEST_CACHE_STATE_PINNED | |
741 test_util::TEST_CACHE_STATE_PERSISTENT, | 742 test_util::TEST_CACHE_STATE_PERSISTENT, |
742 FileCache::CACHE_TYPE_PERSISTENT); | 743 FileCache::CACHE_TYPE_PERSISTENT); |
743 | 744 |
744 // Get the previously pinned and stored file from cache. | 745 // Get the previously pinned and stored file from cache. |
745 TestGetFileFromCacheByResourceIdAndMd5( | 746 TestGetFileFromCacheByResourceIdAndMd5( |
746 resource_id, md5, FILE_ERROR_OK, md5); | 747 resource_id, md5, FILE_ERROR_OK, md5); |
747 } | 748 } |
748 | 749 |
749 TEST_F(FileCacheTest, RemoveFromCachePinned) { | 750 TEST_F(FileCacheTestOnUIThread, RemoveFromCachePinned) { |
750 // Use alphanumeric characters for resource_id. | 751 // Use alphanumeric characters for resource_id. |
751 std::string resource_id("pdf:1a2b"); | 752 std::string resource_id("pdf:1a2b"); |
752 std::string md5("abcdef0123456789"); | 753 std::string md5("abcdef0123456789"); |
753 EXPECT_CALL(*mock_cache_observer_, OnCachePinned(resource_id, md5)).Times(1); | 754 EXPECT_CALL(*mock_cache_observer_, OnCachePinned(resource_id, md5)).Times(1); |
754 | 755 |
755 // Store a file to cache, and pin it. | 756 // Store a file to cache, and pin it. |
756 TestStoreToCache( | 757 TestStoreToCache( |
757 resource_id, md5, | 758 resource_id, md5, |
758 google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), | 759 google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), |
759 FILE_ERROR_OK, test_util::TEST_CACHE_STATE_PRESENT, | 760 FILE_ERROR_OK, test_util::TEST_CACHE_STATE_PRESENT, |
(...skipping 19 matching lines...) Expand all Loading... |
779 FileCache::CACHE_TYPE_TMP); | 780 FileCache::CACHE_TYPE_TMP); |
780 TestPin(resource_id, md5, FILE_ERROR_OK, | 781 TestPin(resource_id, md5, FILE_ERROR_OK, |
781 test_util::TEST_CACHE_STATE_PRESENT | | 782 test_util::TEST_CACHE_STATE_PRESENT | |
782 test_util::TEST_CACHE_STATE_PINNED | | 783 test_util::TEST_CACHE_STATE_PINNED | |
783 test_util::TEST_CACHE_STATE_PERSISTENT, | 784 test_util::TEST_CACHE_STATE_PERSISTENT, |
784 FileCache::CACHE_TYPE_PERSISTENT); | 785 FileCache::CACHE_TYPE_PERSISTENT); |
785 | 786 |
786 TestRemoveFromCache(resource_id, FILE_ERROR_OK); | 787 TestRemoveFromCache(resource_id, FILE_ERROR_OK); |
787 } | 788 } |
788 | 789 |
789 TEST_F(FileCacheTest, DirtyCacheSimple) { | 790 TEST_F(FileCacheTestOnUIThread, DirtyCacheSimple) { |
790 std::string resource_id("pdf:1a2b"); | 791 std::string resource_id("pdf:1a2b"); |
791 std::string md5("abcdef0123456789"); | 792 std::string md5("abcdef0123456789"); |
792 EXPECT_CALL(*mock_cache_observer_, OnCacheCommitted(resource_id)).Times(1); | 793 EXPECT_CALL(*mock_cache_observer_, OnCacheCommitted(resource_id)).Times(1); |
793 | 794 |
794 // First store a file to cache. | 795 // First store a file to cache. |
795 TestStoreToCache( | 796 TestStoreToCache( |
796 resource_id, md5, | 797 resource_id, md5, |
797 google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), | 798 google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), |
798 FILE_ERROR_OK, test_util::TEST_CACHE_STATE_PRESENT, | 799 FILE_ERROR_OK, test_util::TEST_CACHE_STATE_PRESENT, |
799 FileCache::CACHE_TYPE_TMP); | 800 FileCache::CACHE_TYPE_TMP); |
(...skipping 11 matching lines...) Expand all Loading... |
811 test_util::TEST_CACHE_STATE_DIRTY | | 812 test_util::TEST_CACHE_STATE_DIRTY | |
812 test_util::TEST_CACHE_STATE_PERSISTENT, | 813 test_util::TEST_CACHE_STATE_PERSISTENT, |
813 FileCache::CACHE_TYPE_PERSISTENT); | 814 FileCache::CACHE_TYPE_PERSISTENT); |
814 | 815 |
815 // Clear dirty state of the file. | 816 // Clear dirty state of the file. |
816 TestClearDirty(resource_id, md5, FILE_ERROR_OK, | 817 TestClearDirty(resource_id, md5, FILE_ERROR_OK, |
817 test_util::TEST_CACHE_STATE_PRESENT, | 818 test_util::TEST_CACHE_STATE_PRESENT, |
818 FileCache::CACHE_TYPE_TMP); | 819 FileCache::CACHE_TYPE_TMP); |
819 } | 820 } |
820 | 821 |
821 TEST_F(FileCacheTest, DirtyCachePinned) { | 822 TEST_F(FileCacheTestOnUIThread, DirtyCachePinned) { |
822 std::string resource_id("pdf:1a2b"); | 823 std::string resource_id("pdf:1a2b"); |
823 std::string md5("abcdef0123456789"); | 824 std::string md5("abcdef0123456789"); |
824 EXPECT_CALL(*mock_cache_observer_, OnCachePinned(resource_id, md5)).Times(1); | 825 EXPECT_CALL(*mock_cache_observer_, OnCachePinned(resource_id, md5)).Times(1); |
825 EXPECT_CALL(*mock_cache_observer_, OnCacheCommitted(resource_id)).Times(1); | 826 EXPECT_CALL(*mock_cache_observer_, OnCacheCommitted(resource_id)).Times(1); |
826 | 827 |
827 // First store a file to cache and pin it. | 828 // First store a file to cache and pin it. |
828 TestStoreToCache( | 829 TestStoreToCache( |
829 resource_id, md5, | 830 resource_id, md5, |
830 google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), | 831 google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), |
831 FILE_ERROR_OK, test_util::TEST_CACHE_STATE_PRESENT, | 832 FILE_ERROR_OK, test_util::TEST_CACHE_STATE_PRESENT, |
(...skipping 22 matching lines...) Expand all Loading... |
854 | 855 |
855 // Clear dirty state of the file. | 856 // Clear dirty state of the file. |
856 TestClearDirty(resource_id, md5, FILE_ERROR_OK, | 857 TestClearDirty(resource_id, md5, FILE_ERROR_OK, |
857 test_util::TEST_CACHE_STATE_PRESENT | | 858 test_util::TEST_CACHE_STATE_PRESENT | |
858 test_util::TEST_CACHE_STATE_PINNED | | 859 test_util::TEST_CACHE_STATE_PINNED | |
859 test_util::TEST_CACHE_STATE_PERSISTENT, | 860 test_util::TEST_CACHE_STATE_PERSISTENT, |
860 FileCache::CACHE_TYPE_PERSISTENT); | 861 FileCache::CACHE_TYPE_PERSISTENT); |
861 } | 862 } |
862 | 863 |
863 // Test is disabled because it is flaky (http://crbug.com/134146) | 864 // Test is disabled because it is flaky (http://crbug.com/134146) |
864 TEST_F(FileCacheTest, PinAndUnpinDirtyCache) { | 865 TEST_F(FileCacheTestOnUIThread, PinAndUnpinDirtyCache) { |
865 std::string resource_id("pdf:1a2b"); | 866 std::string resource_id("pdf:1a2b"); |
866 std::string md5("abcdef0123456789"); | 867 std::string md5("abcdef0123456789"); |
867 EXPECT_CALL(*mock_cache_observer_, OnCachePinned(resource_id, md5)).Times(1); | 868 EXPECT_CALL(*mock_cache_observer_, OnCachePinned(resource_id, md5)).Times(1); |
868 EXPECT_CALL(*mock_cache_observer_, OnCacheUnpinned(resource_id, md5)) | 869 EXPECT_CALL(*mock_cache_observer_, OnCacheUnpinned(resource_id, md5)) |
869 .Times(1); | 870 .Times(1); |
870 | 871 |
871 // First store a file to cache and mark it as dirty. | 872 // First store a file to cache and mark it as dirty. |
872 TestStoreToCache( | 873 TestStoreToCache( |
873 resource_id, md5, | 874 resource_id, md5, |
874 google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), | 875 google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), |
(...skipping 30 matching lines...) Expand all Loading... |
905 TestUnpin(resource_id, md5, FILE_ERROR_OK, | 906 TestUnpin(resource_id, md5, FILE_ERROR_OK, |
906 test_util::TEST_CACHE_STATE_PRESENT | | 907 test_util::TEST_CACHE_STATE_PRESENT | |
907 test_util::TEST_CACHE_STATE_DIRTY | | 908 test_util::TEST_CACHE_STATE_DIRTY | |
908 test_util::TEST_CACHE_STATE_PERSISTENT, | 909 test_util::TEST_CACHE_STATE_PERSISTENT, |
909 FileCache::CACHE_TYPE_PERSISTENT); | 910 FileCache::CACHE_TYPE_PERSISTENT); |
910 | 911 |
911 // Verify dirty file still exist at the same pathname. | 912 // Verify dirty file still exist at the same pathname. |
912 EXPECT_TRUE(file_util::PathExists(dirty_path)); | 913 EXPECT_TRUE(file_util::PathExists(dirty_path)); |
913 } | 914 } |
914 | 915 |
915 TEST_F(FileCacheTest, DirtyCacheRepetitive) { | 916 TEST_F(FileCacheTestOnUIThread, DirtyCacheRepetitive) { |
916 std::string resource_id("pdf:1a2b"); | 917 std::string resource_id("pdf:1a2b"); |
917 std::string md5("abcdef0123456789"); | 918 std::string md5("abcdef0123456789"); |
918 EXPECT_CALL(*mock_cache_observer_, OnCacheCommitted(resource_id)).Times(3); | 919 EXPECT_CALL(*mock_cache_observer_, OnCacheCommitted(resource_id)).Times(3); |
919 | 920 |
920 // First store a file to cache. | 921 // First store a file to cache. |
921 TestStoreToCache( | 922 TestStoreToCache( |
922 resource_id, md5, | 923 resource_id, md5, |
923 google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), | 924 google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), |
924 FILE_ERROR_OK, test_util::TEST_CACHE_STATE_PRESENT, | 925 FILE_ERROR_OK, test_util::TEST_CACHE_STATE_PRESENT, |
925 FileCache::CACHE_TYPE_TMP); | 926 FileCache::CACHE_TYPE_TMP); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
970 TestClearDirty(resource_id, md5, FILE_ERROR_OK, | 971 TestClearDirty(resource_id, md5, FILE_ERROR_OK, |
971 test_util::TEST_CACHE_STATE_PRESENT, | 972 test_util::TEST_CACHE_STATE_PRESENT, |
972 FileCache::CACHE_TYPE_TMP); | 973 FileCache::CACHE_TYPE_TMP); |
973 | 974 |
974 // Again, clear dirty state of the file, which is no longer dirty. | 975 // Again, clear dirty state of the file, which is no longer dirty. |
975 TestClearDirty(resource_id, md5, FILE_ERROR_INVALID_OPERATION, | 976 TestClearDirty(resource_id, md5, FILE_ERROR_INVALID_OPERATION, |
976 test_util::TEST_CACHE_STATE_PRESENT, | 977 test_util::TEST_CACHE_STATE_PRESENT, |
977 FileCache::CACHE_TYPE_TMP); | 978 FileCache::CACHE_TYPE_TMP); |
978 } | 979 } |
979 | 980 |
980 TEST_F(FileCacheTest, DirtyCacheInvalid) { | 981 TEST_F(FileCacheTestOnUIThread, DirtyCacheInvalid) { |
981 std::string resource_id("pdf:1a2b"); | 982 std::string resource_id("pdf:1a2b"); |
982 std::string md5("abcdef0123456789"); | 983 std::string md5("abcdef0123456789"); |
983 | 984 |
984 // Mark a non-existent file dirty. | 985 // Mark a non-existent file dirty. |
985 TestMarkDirty(resource_id, md5, FILE_ERROR_NOT_FOUND, | 986 TestMarkDirty(resource_id, md5, FILE_ERROR_NOT_FOUND, |
986 test_util::TEST_CACHE_STATE_NONE, | 987 test_util::TEST_CACHE_STATE_NONE, |
987 FileCache::CACHE_TYPE_TMP); | 988 FileCache::CACHE_TYPE_TMP); |
988 | 989 |
989 // Clear dirty state of a non-existent file. | 990 // Clear dirty state of a non-existent file. |
990 TestClearDirty(resource_id, md5, FILE_ERROR_NOT_FOUND, | 991 TestClearDirty(resource_id, md5, FILE_ERROR_NOT_FOUND, |
(...skipping 24 matching lines...) Expand all Loading... |
1015 resource_id, md5, | 1016 resource_id, md5, |
1016 google_apis::test_util::GetTestFilePath( | 1017 google_apis::test_util::GetTestFilePath( |
1017 "chromeos/gdata/empty_feed.json"), | 1018 "chromeos/gdata/empty_feed.json"), |
1018 FILE_ERROR_IN_USE, | 1019 FILE_ERROR_IN_USE, |
1019 test_util::TEST_CACHE_STATE_PRESENT | | 1020 test_util::TEST_CACHE_STATE_PRESENT | |
1020 test_util::TEST_CACHE_STATE_DIRTY | | 1021 test_util::TEST_CACHE_STATE_DIRTY | |
1021 test_util::TEST_CACHE_STATE_PERSISTENT, | 1022 test_util::TEST_CACHE_STATE_PERSISTENT, |
1022 FileCache::CACHE_TYPE_PERSISTENT); | 1023 FileCache::CACHE_TYPE_PERSISTENT); |
1023 } | 1024 } |
1024 | 1025 |
1025 TEST_F(FileCacheTest, RemoveFromDirtyCache) { | 1026 TEST_F(FileCacheTestOnUIThread, RemoveFromDirtyCache) { |
1026 std::string resource_id("pdf:1a2b"); | 1027 std::string resource_id("pdf:1a2b"); |
1027 std::string md5("abcdef0123456789"); | 1028 std::string md5("abcdef0123456789"); |
1028 EXPECT_CALL(*mock_cache_observer_, OnCachePinned(resource_id, md5)).Times(1); | 1029 EXPECT_CALL(*mock_cache_observer_, OnCachePinned(resource_id, md5)).Times(1); |
1029 EXPECT_CALL(*mock_cache_observer_, OnCacheCommitted(resource_id)).Times(1); | 1030 EXPECT_CALL(*mock_cache_observer_, OnCacheCommitted(resource_id)).Times(1); |
1030 | 1031 |
1031 // Store a file to cache, pin it, mark it dirty and commit it. | 1032 // Store a file to cache, pin it, mark it dirty and commit it. |
1032 TestStoreToCache( | 1033 TestStoreToCache( |
1033 resource_id, md5, | 1034 resource_id, md5, |
1034 google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), | 1035 google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), |
1035 FILE_ERROR_OK, test_util::TEST_CACHE_STATE_PRESENT, | 1036 FILE_ERROR_OK, test_util::TEST_CACHE_STATE_PRESENT, |
(...skipping 13 matching lines...) Expand all Loading... |
1049 test_util::TEST_CACHE_STATE_PRESENT | | 1050 test_util::TEST_CACHE_STATE_PRESENT | |
1050 test_util::TEST_CACHE_STATE_PINNED | | 1051 test_util::TEST_CACHE_STATE_PINNED | |
1051 test_util::TEST_CACHE_STATE_DIRTY | | 1052 test_util::TEST_CACHE_STATE_DIRTY | |
1052 test_util::TEST_CACHE_STATE_PERSISTENT, | 1053 test_util::TEST_CACHE_STATE_PERSISTENT, |
1053 FileCache::CACHE_TYPE_PERSISTENT); | 1054 FileCache::CACHE_TYPE_PERSISTENT); |
1054 | 1055 |
1055 // Try to remove the file. Since file is dirty, it should not be removed. | 1056 // Try to remove the file. Since file is dirty, it should not be removed. |
1056 TestRemoveFromCache(resource_id, FILE_ERROR_OK); | 1057 TestRemoveFromCache(resource_id, FILE_ERROR_OK); |
1057 } | 1058 } |
1058 | 1059 |
1059 TEST_F(FileCacheTest, MountUnmount) { | 1060 TEST_F(FileCacheTestOnUIThread, MountUnmount) { |
1060 std::string resource_id("pdf:1a2b"); | 1061 std::string resource_id("pdf:1a2b"); |
1061 std::string md5("abcdef0123456789"); | 1062 std::string md5("abcdef0123456789"); |
1062 | 1063 |
1063 // First store a file to cache in the tmp subdir. | 1064 // First store a file to cache in the tmp subdir. |
1064 TestStoreToCache( | 1065 TestStoreToCache( |
1065 resource_id, md5, | 1066 resource_id, md5, |
1066 google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), | 1067 google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), |
1067 FILE_ERROR_OK, test_util::TEST_CACHE_STATE_PRESENT, | 1068 FILE_ERROR_OK, test_util::TEST_CACHE_STATE_PRESENT, |
1068 FileCache::CACHE_TYPE_TMP); | 1069 FileCache::CACHE_TYPE_TMP); |
1069 | 1070 |
(...skipping 19 matching lines...) Expand all Loading... |
1089 TestMarkAsUnmounted(resource_id, md5, file_path, | 1090 TestMarkAsUnmounted(resource_id, md5, file_path, |
1090 FILE_ERROR_OK, | 1091 FILE_ERROR_OK, |
1091 test_util::TEST_CACHE_STATE_PRESENT, | 1092 test_util::TEST_CACHE_STATE_PRESENT, |
1092 FileCache::CACHE_TYPE_TMP); | 1093 FileCache::CACHE_TYPE_TMP); |
1093 EXPECT_TRUE(CacheEntryExists(resource_id, md5)); | 1094 EXPECT_TRUE(CacheEntryExists(resource_id, md5)); |
1094 | 1095 |
1095 // Try to remove the file. | 1096 // Try to remove the file. |
1096 TestRemoveFromCache(resource_id, FILE_ERROR_OK); | 1097 TestRemoveFromCache(resource_id, FILE_ERROR_OK); |
1097 } | 1098 } |
1098 | 1099 |
1099 TEST_F(FileCacheTest, Iterate) { | 1100 TEST_F(FileCacheTestOnUIThread, Iterate) { |
1100 const std::vector<test_util::TestCacheResource> cache_resources( | 1101 const std::vector<test_util::TestCacheResource> cache_resources( |
1101 test_util::GetDefaultTestCacheResources()); | 1102 test_util::GetDefaultTestCacheResources()); |
1102 // Set mock expectations. | 1103 // Set mock expectations. |
1103 for (size_t i = 0; i < cache_resources.size(); ++i) { | 1104 for (size_t i = 0; i < cache_resources.size(); ++i) { |
1104 if (cache_resources[i].is_pinned) { | 1105 if (cache_resources[i].is_pinned) { |
1105 EXPECT_CALL(*mock_cache_observer_, | 1106 EXPECT_CALL(*mock_cache_observer_, |
1106 OnCachePinned(cache_resources[i].resource_id, | 1107 OnCachePinned(cache_resources[i].resource_id, |
1107 cache_resources[i].md5)).Times(1); | 1108 cache_resources[i].md5)).Times(1); |
1108 } | 1109 } |
1109 if (cache_resources[i].is_dirty) { | 1110 if (cache_resources[i].is_dirty) { |
(...skipping 20 matching lines...) Expand all Loading... |
1130 EXPECT_EQ("dirty:existing", resource_ids[0]); | 1131 EXPECT_EQ("dirty:existing", resource_ids[0]); |
1131 EXPECT_EQ("dirty_and_pinned:existing", resource_ids[1]); | 1132 EXPECT_EQ("dirty_and_pinned:existing", resource_ids[1]); |
1132 EXPECT_EQ("pinned:existing", resource_ids[2]); | 1133 EXPECT_EQ("pinned:existing", resource_ids[2]); |
1133 EXPECT_EQ("pinned:non-existent", resource_ids[3]); | 1134 EXPECT_EQ("pinned:non-existent", resource_ids[3]); |
1134 EXPECT_EQ("tmp:`~!@#$%^&*()-_=+[{|]}\\;',<.>/?", resource_ids[4]); | 1135 EXPECT_EQ("tmp:`~!@#$%^&*()-_=+[{|]}\\;',<.>/?", resource_ids[4]); |
1135 EXPECT_EQ("tmp:resource_id", resource_ids[5]); | 1136 EXPECT_EQ("tmp:resource_id", resource_ids[5]); |
1136 | 1137 |
1137 ASSERT_EQ(6U, cache_entries.size()); | 1138 ASSERT_EQ(6U, cache_entries.size()); |
1138 } | 1139 } |
1139 | 1140 |
1140 | 1141 TEST_F(FileCacheTestOnUIThread, ClearAll) { |
1141 TEST_F(FileCacheTest, ClearAll) { | |
1142 std::string resource_id("pdf:1a2b"); | 1142 std::string resource_id("pdf:1a2b"); |
1143 std::string md5("abcdef0123456789"); | 1143 std::string md5("abcdef0123456789"); |
1144 | 1144 |
1145 // Store an existing file. | 1145 // Store an existing file. |
1146 TestStoreToCache( | 1146 TestStoreToCache( |
1147 resource_id, md5, | 1147 resource_id, md5, |
1148 google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), | 1148 google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), |
1149 FILE_ERROR_OK, test_util::TEST_CACHE_STATE_PRESENT, | 1149 FILE_ERROR_OK, test_util::TEST_CACHE_STATE_PRESENT, |
1150 FileCache::CACHE_TYPE_TMP); | 1150 FileCache::CACHE_TYPE_TMP); |
1151 | 1151 |
1152 // Verify that there's only one cached file. | 1152 // Verify that there's only one cached file. |
1153 EXPECT_EQ(1U, CountCacheFiles(resource_id, md5)); | 1153 EXPECT_EQ(1U, CountCacheFiles(resource_id, md5)); |
1154 | 1154 |
1155 // Clear cache. | 1155 // Clear cache. |
1156 bool success = false; | 1156 bool success = false; |
1157 cache_->ClearAllOnUIThread( | 1157 cache_->ClearAllOnUIThread( |
1158 google_apis::test_util::CreateCopyResultCallback(&success)); | 1158 google_apis::test_util::CreateCopyResultCallback(&success)); |
1159 google_apis::test_util::RunBlockingPoolTask(); | 1159 google_apis::test_util::RunBlockingPoolTask(); |
1160 EXPECT_TRUE(success); | 1160 EXPECT_TRUE(success); |
1161 | 1161 |
1162 // Verify that all the cache is removed. | 1162 // Verify that all the cache is removed. |
1163 expected_error_ = FILE_ERROR_OK; | 1163 expected_error_ = FILE_ERROR_OK; |
1164 VerifyRemoveFromCache(FILE_ERROR_OK, resource_id, md5); | 1164 VerifyRemoveFromCache(FILE_ERROR_OK, resource_id, md5); |
1165 EXPECT_EQ(0U, CountCacheFiles(resource_id, md5)); | 1165 EXPECT_EQ(0U, CountCacheFiles(resource_id, md5)); |
1166 } | 1166 } |
1167 | 1167 |
1168 TEST_F(FileCacheTest, StoreToCacheNoSpace) { | 1168 TEST_F(FileCacheTestOnUIThread, StoreToCacheNoSpace) { |
1169 fake_free_disk_space_getter_->set_default_value(0); | 1169 fake_free_disk_space_getter_->set_default_value(0); |
1170 | 1170 |
1171 std::string resource_id("pdf:1a2b"); | 1171 std::string resource_id("pdf:1a2b"); |
1172 std::string md5("abcdef0123456789"); | 1172 std::string md5("abcdef0123456789"); |
1173 | 1173 |
1174 // Try to store an existing file. | 1174 // Try to store an existing file. |
1175 TestStoreToCache( | 1175 TestStoreToCache( |
1176 resource_id, md5, | 1176 resource_id, md5, |
1177 google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), | 1177 google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), |
1178 FILE_ERROR_NO_SPACE, | 1178 FILE_ERROR_NO_SPACE, |
(...skipping 19 matching lines...) Expand all Loading... |
1198 pool->GetSequencedTaskRunner(pool->GetSequenceToken()), | 1198 pool->GetSequencedTaskRunner(pool->GetSequenceToken()), |
1199 NULL /* free_disk_space_getter */)); | 1199 NULL /* free_disk_space_getter */)); |
1200 | 1200 |
1201 bool success = true; | 1201 bool success = true; |
1202 cache->RequestInitialize( | 1202 cache->RequestInitialize( |
1203 google_apis::test_util::CreateCopyResultCallback(&success)); | 1203 google_apis::test_util::CreateCopyResultCallback(&success)); |
1204 google_apis::test_util::RunBlockingPoolTask(); | 1204 google_apis::test_util::RunBlockingPoolTask(); |
1205 EXPECT_FALSE(success); | 1205 EXPECT_FALSE(success); |
1206 } | 1206 } |
1207 | 1207 |
1208 TEST_F(FileCacheTest, UpdatePinnedCache) { | 1208 TEST_F(FileCacheTestOnUIThread, UpdatePinnedCache) { |
1209 std::string resource_id("pdf:1a2b"); | 1209 std::string resource_id("pdf:1a2b"); |
1210 std::string md5("abcdef0123456789"); | 1210 std::string md5("abcdef0123456789"); |
1211 std::string md5_modified("aaaaaa0000000000"); | 1211 std::string md5_modified("aaaaaa0000000000"); |
1212 | 1212 |
1213 // Store an existing file. | 1213 // Store an existing file. |
1214 TestStoreToCache( | 1214 TestStoreToCache( |
1215 resource_id, md5, | 1215 resource_id, md5, |
1216 google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), | 1216 google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), |
1217 FILE_ERROR_OK, | 1217 FILE_ERROR_OK, |
1218 test_util::TEST_CACHE_STATE_PRESENT, | 1218 test_util::TEST_CACHE_STATE_PRESENT, |
(...skipping 13 matching lines...) Expand all Loading... |
1232 TestStoreToCache( | 1232 TestStoreToCache( |
1233 resource_id, md5_modified, | 1233 resource_id, md5_modified, |
1234 google_apis::test_util::GetTestFilePath("chromeos/gdata/empty_feed.json"), | 1234 google_apis::test_util::GetTestFilePath("chromeos/gdata/empty_feed.json"), |
1235 FILE_ERROR_OK, | 1235 FILE_ERROR_OK, |
1236 test_util::TEST_CACHE_STATE_PRESENT | | 1236 test_util::TEST_CACHE_STATE_PRESENT | |
1237 test_util::TEST_CACHE_STATE_PINNED | | 1237 test_util::TEST_CACHE_STATE_PINNED | |
1238 test_util::TEST_CACHE_STATE_PERSISTENT, | 1238 test_util::TEST_CACHE_STATE_PERSISTENT, |
1239 FileCache::CACHE_TYPE_PERSISTENT); | 1239 FileCache::CACHE_TYPE_PERSISTENT); |
1240 } | 1240 } |
1241 | 1241 |
| 1242 // Tests FileCache methods working with the blocking task runner. |
| 1243 class FileCacheTest : public testing::Test { |
| 1244 protected: |
| 1245 FileCacheTest() : ui_thread_(content::BrowserThread::UI, &message_loop_) {} |
| 1246 |
| 1247 virtual void SetUp() OVERRIDE { |
| 1248 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 1249 fake_free_disk_space_getter_.reset(new FakeFreeDiskSpaceGetter); |
| 1250 |
| 1251 cache_.reset(new FileCache(temp_dir_.path(), |
| 1252 message_loop_.message_loop_proxy(), |
| 1253 fake_free_disk_space_getter_.get())); |
| 1254 |
| 1255 bool success = false; |
| 1256 cache_->RequestInitialize( |
| 1257 google_apis::test_util::CreateCopyResultCallback(&success)); |
| 1258 message_loop_.RunUntilIdle(); |
| 1259 ASSERT_TRUE(success); |
| 1260 } |
| 1261 |
| 1262 virtual void TearDown() OVERRIDE { |
| 1263 cache_.reset(); |
| 1264 } |
| 1265 |
| 1266 base::MessageLoopForUI message_loop_; |
| 1267 content::TestBrowserThread ui_thread_; |
| 1268 base::ScopedTempDir temp_dir_; |
| 1269 |
| 1270 scoped_ptr<FileCache, test_util::DestroyHelperForTests> cache_; |
| 1271 scoped_ptr<FakeFreeDiskSpaceGetter> fake_free_disk_space_getter_; |
| 1272 }; |
| 1273 |
| 1274 TEST_F(FileCacheTest, FreeDiskSpaceIfNeededFor) { |
| 1275 base::FilePath src_file; |
| 1276 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(), &src_file)); |
| 1277 |
| 1278 // Store a file as a 'temporary' file and remember the path. |
| 1279 const std::string resource_id_tmp = "id_tmp", md5_tmp = "md5_tmp"; |
| 1280 ASSERT_EQ(FILE_ERROR_OK, |
| 1281 cache_->Store(resource_id_tmp, md5_tmp, src_file, |
| 1282 FileCache::FILE_OPERATION_COPY)); |
| 1283 base::FilePath tmp_path; |
| 1284 ASSERT_EQ(FILE_ERROR_OK, |
| 1285 cache_->GetFile(resource_id_tmp, md5_tmp, &tmp_path)); |
| 1286 |
| 1287 // Store a file as a pinned file and remember the path. |
| 1288 const std::string resource_id_pinned = "id_pinned", md5_pinned = "md5_pinned"; |
| 1289 ASSERT_EQ(FILE_ERROR_OK, |
| 1290 cache_->Store(resource_id_pinned, md5_pinned, src_file, |
| 1291 FileCache::FILE_OPERATION_COPY)); |
| 1292 ASSERT_EQ(FILE_ERROR_OK, cache_->Pin(resource_id_pinned, md5_pinned)); |
| 1293 base::FilePath pinned_path; |
| 1294 ASSERT_EQ(FILE_ERROR_OK, |
| 1295 cache_->GetFile(resource_id_pinned, md5_pinned, &pinned_path)); |
| 1296 |
| 1297 // Call FreeDiskSpaceIfNeededFor(). |
| 1298 fake_free_disk_space_getter_->set_default_value(test_util::kLotsOfSpace); |
| 1299 fake_free_disk_space_getter_->PushFakeValue(0); |
| 1300 const int64 kNeededBytes = 1; |
| 1301 EXPECT_TRUE(cache_->FreeDiskSpaceIfNeededFor(kNeededBytes)); |
| 1302 |
| 1303 // Only 'temporary' file gets removed. |
| 1304 FileCacheEntry entry; |
| 1305 EXPECT_FALSE(cache_->GetCacheEntry(resource_id_tmp, md5_tmp, &entry)); |
| 1306 EXPECT_FALSE(file_util::PathExists(tmp_path)); |
| 1307 |
| 1308 EXPECT_TRUE(cache_->GetCacheEntry(resource_id_pinned, md5_pinned, &entry)); |
| 1309 EXPECT_TRUE(file_util::PathExists(pinned_path)); |
| 1310 |
| 1311 // Returns false when disk space cannot be freed. |
| 1312 fake_free_disk_space_getter_->set_default_value(0); |
| 1313 EXPECT_FALSE(cache_->FreeDiskSpaceIfNeededFor(kNeededBytes)); |
| 1314 } |
| 1315 |
1242 } // namespace internal | 1316 } // namespace internal |
1243 } // namespace drive | 1317 } // namespace drive |
OLD | NEW |