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_system.h" | 5 #include "chrome/browser/chromeos/drive/file_system.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 (*counter)++; | 57 (*counter)++; |
58 if (*counter >= expected_counter) | 58 if (*counter >= expected_counter) |
59 message_loop->Quit(); | 59 message_loop->Quit(); |
60 } | 60 } |
61 | 61 |
62 } // namespace | 62 } // namespace |
63 | 63 |
64 class FileSystemTest : public testing::Test { | 64 class FileSystemTest : public testing::Test { |
65 protected: | 65 protected: |
66 FileSystemTest() | 66 FileSystemTest() |
67 : ui_thread_(content::BrowserThread::UI, &message_loop_), | 67 : ui_thread_(content::BrowserThread::UI, &message_loop_) { |
68 // |root_feed_changestamp_| should be set to the largest changestamp in | |
69 // about resource feed. But we fake it by some non-zero positive | |
70 // increasing value. See |LoadFeed()|. | |
71 root_feed_changestamp_(1) { | |
72 } | 68 } |
73 | 69 |
74 virtual void SetUp() OVERRIDE { | 70 virtual void SetUp() OVERRIDE { |
75 profile_.reset(new TestingProfile); | 71 profile_.reset(new TestingProfile); |
76 | 72 |
77 // The fake object will be manually deleted in TearDown(). | 73 // The fake object will be manually deleted in TearDown(). |
78 fake_drive_service_.reset(new google_apis::FakeDriveService); | 74 fake_drive_service_.reset(new google_apis::FakeDriveService); |
79 fake_drive_service_->LoadResourceListForWapi( | 75 fake_drive_service_->LoadResourceListForWapi( |
80 "chromeos/gdata/root_feed.json"); | 76 "chromeos/gdata/root_feed.json"); |
81 fake_drive_service_->LoadAccountMetadataForWapi( | 77 fake_drive_service_->LoadAccountMetadataForWapi( |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 | 125 |
130 virtual void TearDown() OVERRIDE { | 126 virtual void TearDown() OVERRIDE { |
131 ASSERT_TRUE(file_system_); | 127 ASSERT_TRUE(file_system_); |
132 file_system_.reset(); | 128 file_system_.reset(); |
133 scheduler_.reset(); | 129 scheduler_.reset(); |
134 fake_drive_service_.reset(); | 130 fake_drive_service_.reset(); |
135 cache_.reset(); | 131 cache_.reset(); |
136 profile_.reset(NULL); | 132 profile_.reset(NULL); |
137 } | 133 } |
138 | 134 |
139 // Loads test json file as root ("/drive") element. | 135 // Loads the full resource list via FakeDriveService. |
140 bool LoadRootFeedDocument() { | 136 bool LoadFullResourceList() { |
141 FileError error = FILE_ERROR_FAILED; | 137 FileError error = FILE_ERROR_FAILED; |
142 file_system_->change_list_loader()->LoadIfNeeded( | 138 file_system_->change_list_loader()->LoadIfNeeded( |
143 DirectoryFetchInfo(), | 139 DirectoryFetchInfo(), |
144 google_apis::test_util::CreateCopyResultCallback(&error)); | 140 google_apis::test_util::CreateCopyResultCallback(&error)); |
145 google_apis::test_util::RunBlockingPoolTask(); | 141 google_apis::test_util::RunBlockingPoolTask(); |
146 return error == FILE_ERROR_OK; | 142 return error == FILE_ERROR_OK; |
147 } | 143 } |
148 | 144 |
149 // Gets resource entry by path synchronously. | 145 // Gets resource entry by path synchronously. |
150 scoped_ptr<ResourceEntry> GetResourceEntryByPathSync( | 146 scoped_ptr<ResourceEntry> GetResourceEntryByPathSync( |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 | 328 |
333 scoped_ptr<internal::FileCache, test_util::DestroyHelperForTests> cache_; | 329 scoped_ptr<internal::FileCache, test_util::DestroyHelperForTests> cache_; |
334 scoped_ptr<FileSystem> file_system_; | 330 scoped_ptr<FileSystem> file_system_; |
335 scoped_ptr<google_apis::FakeDriveService> fake_drive_service_; | 331 scoped_ptr<google_apis::FakeDriveService> fake_drive_service_; |
336 scoped_ptr<JobScheduler> scheduler_; | 332 scoped_ptr<JobScheduler> scheduler_; |
337 scoped_ptr<internal::ResourceMetadata, test_util::DestroyHelperForTests> | 333 scoped_ptr<internal::ResourceMetadata, test_util::DestroyHelperForTests> |
338 resource_metadata_; | 334 resource_metadata_; |
339 scoped_ptr<FakeFreeDiskSpaceGetter> fake_free_disk_space_getter_; | 335 scoped_ptr<FakeFreeDiskSpaceGetter> fake_free_disk_space_getter_; |
340 scoped_ptr<StrictMock<MockCacheObserver> > mock_cache_observer_; | 336 scoped_ptr<StrictMock<MockCacheObserver> > mock_cache_observer_; |
341 scoped_ptr<StrictMock<MockDirectoryChangeObserver> > mock_directory_observer_; | 337 scoped_ptr<StrictMock<MockDirectoryChangeObserver> > mock_directory_observer_; |
342 | |
343 int root_feed_changestamp_; | |
344 }; | 338 }; |
345 | 339 |
346 TEST_F(FileSystemTest, DuplicatedAsyncInitialization) { | 340 TEST_F(FileSystemTest, DuplicatedAsyncInitialization) { |
347 // "Fast fetch" will fire an OnirectoryChanged event. | 341 // "Fast fetch" will fire an OnirectoryChanged event. |
348 EXPECT_CALL(*mock_directory_observer_, OnDirectoryChanged( | 342 EXPECT_CALL(*mock_directory_observer_, OnDirectoryChanged( |
349 Eq(base::FilePath(FILE_PATH_LITERAL("drive"))))).Times(1); | 343 Eq(base::FilePath(FILE_PATH_LITERAL("drive"))))).Times(1); |
350 | 344 |
351 int counter = 0; | 345 int counter = 0; |
352 const GetResourceEntryCallback& callback = base::Bind( | 346 const GetResourceEntryCallback& callback = base::Bind( |
353 &AsyncInitializationCallback, &counter, 2, &message_loop_); | 347 &AsyncInitializationCallback, &counter, 2, &message_loop_); |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
541 scoped_ptr<ResourceEntryVector> entries( | 535 scoped_ptr<ResourceEntryVector> entries( |
542 ReadDirectoryByPathSync( | 536 ReadDirectoryByPathSync( |
543 base::FilePath::FromUTF8Unsafe("drive/root/Directory 1"))); | 537 base::FilePath::FromUTF8Unsafe("drive/root/Directory 1"))); |
544 // The non root directory should also be read correctly. | 538 // The non root directory should also be read correctly. |
545 // There was a bug (crbug.com/181487), which broke this behavior. | 539 // There was a bug (crbug.com/181487), which broke this behavior. |
546 // Make sure this is fixed. | 540 // Make sure this is fixed. |
547 ASSERT_TRUE(entries); | 541 ASSERT_TRUE(entries); |
548 EXPECT_EQ(3U, entries->size()); | 542 EXPECT_EQ(3U, entries->size()); |
549 } | 543 } |
550 | 544 |
551 TEST_F(FileSystemTest, CachedFeedLoadingThenServerFeedLoading) { | 545 TEST_F(FileSystemTest, LoadFileSystemFromUpToDateCache) { |
552 ASSERT_TRUE(SetUpTestFileSystem(USE_SERVER_TIMESTAMP)); | 546 ASSERT_TRUE(SetUpTestFileSystem(USE_SERVER_TIMESTAMP)); |
553 | 547 |
554 // Kicks loading of cached file system and query for server update. | 548 // Kicks loading of cached file system and query for server update. |
555 EXPECT_TRUE(ReadDirectoryByPathSync(util::GetDriveMyDriveRootPath())); | 549 EXPECT_TRUE(ReadDirectoryByPathSync(util::GetDriveMyDriveRootPath())); |
556 | 550 |
557 // SetUpTestFileSystem and "account_metadata.json" have the same changestamp, | 551 // SetUpTestFileSystem and "account_metadata.json" have the same |
558 // so no request for new feeds (i.e., call to GetResourceList) should happen. | 552 // changestamp (i.e. the local metadata is up-to-date), so no request for |
| 553 // new resource list (i.e., call to GetResourceList) should happen. |
559 EXPECT_EQ(1, fake_drive_service_->about_resource_load_count()); | 554 EXPECT_EQ(1, fake_drive_service_->about_resource_load_count()); |
560 EXPECT_EQ(0, fake_drive_service_->resource_list_load_count()); | 555 EXPECT_EQ(0, fake_drive_service_->resource_list_load_count()); |
561 | 556 |
562 // Since the file system has verified that it holds the latest snapshot, | 557 // Since the file system has verified that it holds the latest snapshot, |
563 // it should change its state to "loaded", which admits periodic refresh. | 558 // it should change its state to "loaded", which admits periodic refresh. |
564 // To test it, call CheckForUpdates and verify it does try to check updates. | 559 // To test it, call CheckForUpdates and verify it does try to check updates. |
565 file_system_->CheckForUpdates(); | 560 file_system_->CheckForUpdates(); |
566 google_apis::test_util::RunBlockingPoolTask(); | 561 google_apis::test_util::RunBlockingPoolTask(); |
567 EXPECT_EQ(2, fake_drive_service_->about_resource_load_count()); | 562 EXPECT_EQ(2, fake_drive_service_->about_resource_load_count()); |
568 } | 563 } |
569 | 564 |
570 TEST_F(FileSystemTest, OfflineCachedFeedLoading) { | 565 TEST_F(FileSystemTest, LoadFileSystemFromCacheWhileOffline) { |
571 ASSERT_TRUE(SetUpTestFileSystem(USE_OLD_TIMESTAMP)); | 566 ASSERT_TRUE(SetUpTestFileSystem(USE_OLD_TIMESTAMP)); |
572 | 567 |
573 // Make GetResourceList fail for simulating offline situation. This will leave | 568 // Make GetResourceList fail for simulating offline situation. This will |
574 // the file system "loaded from cache, but not synced with server" state. | 569 // leave the file system "loaded from cache, but not synced with server" |
| 570 // state. |
575 fake_drive_service_->set_offline(true); | 571 fake_drive_service_->set_offline(true); |
576 | 572 |
577 // Kicks loading of cached file system and query for server update. | 573 // Kicks loading of cached file system and query for server update. |
578 EXPECT_TRUE(ReadDirectoryByPathSync(util::GetDriveMyDriveRootPath())); | 574 EXPECT_TRUE(ReadDirectoryByPathSync(util::GetDriveMyDriveRootPath())); |
579 // Loading of about resource should not happen as it's offline. | 575 // Loading of about resource should not happen as it's offline. |
580 EXPECT_EQ(0, fake_drive_service_->about_resource_load_count()); | 576 EXPECT_EQ(0, fake_drive_service_->about_resource_load_count()); |
581 | 577 |
582 // Tests that cached data can be loaded even if the server is not reachable. | 578 // Tests that cached data can be loaded even if the server is not reachable. |
583 EXPECT_TRUE(EntryExists(base::FilePath( | 579 EXPECT_TRUE(EntryExists(base::FilePath( |
584 FILE_PATH_LITERAL("drive/root/File1")))); | 580 FILE_PATH_LITERAL("drive/root/File1")))); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
639 ASSERT_TRUE(SetUpTestFileSystem(USE_OLD_TIMESTAMP)); | 635 ASSERT_TRUE(SetUpTestFileSystem(USE_OLD_TIMESTAMP)); |
640 file_system_->CheckForUpdates(); | 636 file_system_->CheckForUpdates(); |
641 | 637 |
642 // If an entry is not found, parent directory's resource list is fetched. | 638 // If an entry is not found, parent directory's resource list is fetched. |
643 EXPECT_FALSE(GetResourceEntryByPathSync(base::FilePath( | 639 EXPECT_FALSE(GetResourceEntryByPathSync(base::FilePath( |
644 FILE_PATH_LITERAL("drive/root/Dir1/NonExistentFile")))); | 640 FILE_PATH_LITERAL("drive/root/Dir1/NonExistentFile")))); |
645 EXPECT_EQ(1, fake_drive_service_->directory_load_count()); | 641 EXPECT_EQ(1, fake_drive_service_->directory_load_count()); |
646 } | 642 } |
647 | 643 |
648 TEST_F(FileSystemTest, CreateDirectoryByImplicitLoad) { | 644 TEST_F(FileSystemTest, CreateDirectoryByImplicitLoad) { |
649 // Intentionally *not* calling LoadRootFeedDocument(), for testing that | 645 // Intentionally *not* calling LoadFullResourceList(), for testing that |
650 // CreateDirectory ensures feed loading before it runs. | 646 // CreateDirectory ensures the resource list is loaded before it runs. |
651 | 647 |
652 base::FilePath existing_directory( | 648 base::FilePath existing_directory( |
653 FILE_PATH_LITERAL("drive/root/Directory 1")); | 649 FILE_PATH_LITERAL("drive/root/Directory 1")); |
654 FileError error = FILE_ERROR_FAILED; | 650 FileError error = FILE_ERROR_FAILED; |
655 file_system_->CreateDirectory( | 651 file_system_->CreateDirectory( |
656 existing_directory, | 652 existing_directory, |
657 true, // is_exclusive | 653 true, // is_exclusive |
658 false, // is_recursive | 654 false, // is_recursive |
659 google_apis::test_util::CreateCopyResultCallback(&error)); | 655 google_apis::test_util::CreateCopyResultCallback(&error)); |
660 google_apis::test_util::RunBlockingPoolTask(); | 656 google_apis::test_util::RunBlockingPoolTask(); |
661 | 657 |
662 // It should fail because is_exclusive is set to true. | 658 // It should fail because is_exclusive is set to true. |
663 EXPECT_EQ(FILE_ERROR_EXISTS, error); | 659 EXPECT_EQ(FILE_ERROR_EXISTS, error); |
664 } | 660 } |
665 | 661 |
666 TEST_F(FileSystemTest, PinAndUnpin) { | 662 TEST_F(FileSystemTest, PinAndUnpin) { |
667 ASSERT_TRUE(LoadRootFeedDocument()); | 663 ASSERT_TRUE(LoadFullResourceList()); |
668 | 664 |
669 base::FilePath file_path(FILE_PATH_LITERAL("drive/root/File 1.txt")); | 665 base::FilePath file_path(FILE_PATH_LITERAL("drive/root/File 1.txt")); |
670 | 666 |
671 // Get the file info. | 667 // Get the file info. |
672 scoped_ptr<ResourceEntry> entry(GetResourceEntryByPathSync(file_path)); | 668 scoped_ptr<ResourceEntry> entry(GetResourceEntryByPathSync(file_path)); |
673 ASSERT_TRUE(entry); | 669 ASSERT_TRUE(entry); |
674 | 670 |
675 // Pin the file. | 671 // Pin the file. |
676 FileError error = FILE_ERROR_FAILED; | 672 FileError error = FILE_ERROR_FAILED; |
677 EXPECT_CALL(*mock_cache_observer_, | 673 EXPECT_CALL(*mock_cache_observer_, |
(...skipping 21 matching lines...) Expand all Loading... |
699 int64 bytes_used; | 695 int64 bytes_used; |
700 file_system_->GetAvailableSpace( | 696 file_system_->GetAvailableSpace( |
701 google_apis::test_util::CreateCopyResultCallback( | 697 google_apis::test_util::CreateCopyResultCallback( |
702 &error, &bytes_total, &bytes_used)); | 698 &error, &bytes_total, &bytes_used)); |
703 google_apis::test_util::RunBlockingPoolTask(); | 699 google_apis::test_util::RunBlockingPoolTask(); |
704 EXPECT_EQ(GG_LONGLONG(6789012345), bytes_used); | 700 EXPECT_EQ(GG_LONGLONG(6789012345), bytes_used); |
705 EXPECT_EQ(GG_LONGLONG(9876543210), bytes_total); | 701 EXPECT_EQ(GG_LONGLONG(9876543210), bytes_total); |
706 } | 702 } |
707 | 703 |
708 TEST_F(FileSystemTest, RefreshDirectory) { | 704 TEST_F(FileSystemTest, RefreshDirectory) { |
709 ASSERT_TRUE(LoadRootFeedDocument()); | 705 ASSERT_TRUE(LoadFullResourceList()); |
710 | 706 |
711 // We'll notify the directory change to the observer. | 707 // We'll notify the directory change to the observer. |
712 EXPECT_CALL(*mock_directory_observer_, | 708 EXPECT_CALL(*mock_directory_observer_, |
713 OnDirectoryChanged(Eq(util::GetDriveMyDriveRootPath()))).Times(1); | 709 OnDirectoryChanged(Eq(util::GetDriveMyDriveRootPath()))).Times(1); |
714 | 710 |
715 FileError error = FILE_ERROR_FAILED; | 711 FileError error = FILE_ERROR_FAILED; |
716 file_system_->RefreshDirectory( | 712 file_system_->RefreshDirectory( |
717 util::GetDriveMyDriveRootPath(), | 713 util::GetDriveMyDriveRootPath(), |
718 google_apis::test_util::CreateCopyResultCallback(&error)); | 714 google_apis::test_util::CreateCopyResultCallback(&error)); |
719 google_apis::test_util::RunBlockingPoolTask(); | 715 google_apis::test_util::RunBlockingPoolTask(); |
720 EXPECT_EQ(FILE_ERROR_OK, error); | 716 EXPECT_EQ(FILE_ERROR_OK, error); |
721 } | 717 } |
722 | 718 |
723 TEST_F(FileSystemTest, OpenAndCloseFile) { | 719 TEST_F(FileSystemTest, OpenAndCloseFile) { |
724 ASSERT_TRUE(LoadRootFeedDocument()); | 720 ASSERT_TRUE(LoadFullResourceList()); |
725 | 721 |
726 // The transfered file is cached and the change of "offline available" | 722 // The transfered file is cached and the change of "offline available" |
727 // attribute is notified. | 723 // attribute is notified. |
728 EXPECT_CALL(*mock_directory_observer_, OnDirectoryChanged( | 724 EXPECT_CALL(*mock_directory_observer_, OnDirectoryChanged( |
729 Eq(base::FilePath(FILE_PATH_LITERAL("drive/root"))))).Times(1); | 725 Eq(base::FilePath(FILE_PATH_LITERAL("drive/root"))))).Times(1); |
730 | 726 |
731 const base::FilePath kFileInRoot(FILE_PATH_LITERAL("drive/root/File 1.txt")); | 727 const base::FilePath kFileInRoot(FILE_PATH_LITERAL("drive/root/File 1.txt")); |
732 scoped_ptr<ResourceEntry> entry(GetResourceEntryByPathSync(kFileInRoot)); | 728 scoped_ptr<ResourceEntry> entry(GetResourceEntryByPathSync(kFileInRoot)); |
733 const int64 file_size = entry->file_info().size(); | 729 const int64 file_size = entry->file_info().size(); |
734 const std::string& file_resource_id = | 730 const std::string& file_resource_id = |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
806 kFileInRoot, | 802 kFileInRoot, |
807 google_apis::test_util::CreateCopyResultCallback(&error)); | 803 google_apis::test_util::CreateCopyResultCallback(&error)); |
808 google_apis::test_util::RunBlockingPoolTask(); | 804 google_apis::test_util::RunBlockingPoolTask(); |
809 | 805 |
810 // It must fail. | 806 // It must fail. |
811 EXPECT_EQ(FILE_ERROR_NOT_FOUND, error); | 807 EXPECT_EQ(FILE_ERROR_NOT_FOUND, error); |
812 } | 808 } |
813 | 809 |
814 TEST_F(FileSystemTest, MarkCacheFileAsMountedAndUnmounted) { | 810 TEST_F(FileSystemTest, MarkCacheFileAsMountedAndUnmounted) { |
815 fake_free_disk_space_getter_->set_fake_free_disk_space(kLotsOfSpace); | 811 fake_free_disk_space_getter_->set_fake_free_disk_space(kLotsOfSpace); |
816 ASSERT_TRUE(LoadRootFeedDocument()); | 812 ASSERT_TRUE(LoadFullResourceList()); |
817 | 813 |
818 base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt")); | 814 base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt")); |
819 scoped_ptr<ResourceEntry> entry(GetResourceEntryByPathSync(file_in_root)); | 815 scoped_ptr<ResourceEntry> entry(GetResourceEntryByPathSync(file_in_root)); |
820 ASSERT_TRUE(entry); | 816 ASSERT_TRUE(entry); |
821 | 817 |
822 // Write to cache. | 818 // Write to cache. |
823 FileError error = FILE_ERROR_FAILED; | 819 FileError error = FILE_ERROR_FAILED; |
824 cache_->StoreOnUIThread( | 820 cache_->StoreOnUIThread( |
825 entry->resource_id(), | 821 entry->resource_id(), |
826 entry->file_specific_info().file_md5(), | 822 entry->file_specific_info().file_md5(), |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
863 entry->resource_id(), | 859 entry->resource_id(), |
864 entry->file_specific_info().file_md5(), | 860 entry->file_specific_info().file_md5(), |
865 google_apis::test_util::CreateCopyResultCallback(&success, &cache_entry)); | 861 google_apis::test_util::CreateCopyResultCallback(&success, &cache_entry)); |
866 google_apis::test_util::RunBlockingPoolTask(); | 862 google_apis::test_util::RunBlockingPoolTask(); |
867 | 863 |
868 EXPECT_TRUE(success); | 864 EXPECT_TRUE(success); |
869 EXPECT_FALSE(cache_entry.is_mounted()); | 865 EXPECT_FALSE(cache_entry.is_mounted()); |
870 } | 866 } |
871 | 867 |
872 } // namespace drive | 868 } // namespace drive |
OLD | NEW |