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" |
11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
13 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "base/message_loop.h" | 15 #include "base/message_loop.h" |
16 #include "base/threading/sequenced_worker_pool.h" | 16 #include "base/threading/sequenced_worker_pool.h" |
17 #include "chrome/browser/chromeos/drive/change_list_loader.h" | 17 #include "chrome/browser/chromeos/drive/change_list_loader.h" |
18 #include "chrome/browser/chromeos/drive/drive.pb.h" | 18 #include "chrome/browser/chromeos/drive/drive.pb.h" |
19 #include "chrome/browser/chromeos/drive/fake_free_disk_space_getter.h" | 19 #include "chrome/browser/chromeos/drive/fake_free_disk_space_getter.h" |
20 #include "chrome/browser/chromeos/drive/file_system_util.h" | 20 #include "chrome/browser/chromeos/drive/file_system_util.h" |
21 #include "chrome/browser/chromeos/drive/job_scheduler.h" | 21 #include "chrome/browser/chromeos/drive/job_scheduler.h" |
22 #include "chrome/browser/chromeos/drive/mock_directory_change_observer.h" | 22 #include "chrome/browser/chromeos/drive/mock_directory_change_observer.h" |
23 #include "chrome/browser/chromeos/drive/mock_file_cache_observer.h" | 23 #include "chrome/browser/chromeos/drive/mock_file_cache_observer.h" |
| 24 #include "chrome/browser/chromeos/drive/sync_client.h" |
24 #include "chrome/browser/chromeos/drive/test_util.h" | 25 #include "chrome/browser/chromeos/drive/test_util.h" |
25 #include "chrome/browser/google_apis/drive_api_parser.h" | 26 #include "chrome/browser/google_apis/drive_api_parser.h" |
26 #include "chrome/browser/google_apis/fake_drive_service.h" | 27 #include "chrome/browser/google_apis/fake_drive_service.h" |
27 #include "chrome/browser/google_apis/test_util.h" | 28 #include "chrome/browser/google_apis/test_util.h" |
28 #include "chrome/test/base/testing_profile.h" | 29 #include "chrome/test/base/testing_profile.h" |
29 #include "content/public/browser/browser_thread.h" | 30 #include "content/public/browser/browser_thread.h" |
30 #include "content/public/test/test_browser_thread.h" | 31 #include "content/public/test/test_browser_thread.h" |
31 #include "testing/gmock/include/gmock/gmock.h" | 32 #include "testing/gmock/include/gmock/gmock.h" |
32 #include "testing/gtest/include/gtest/gtest.h" | 33 #include "testing/gtest/include/gtest/gtest.h" |
33 | 34 |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 | 113 |
113 file_system_.reset(new FileSystem(profile_.get(), | 114 file_system_.reset(new FileSystem(profile_.get(), |
114 cache_.get(), | 115 cache_.get(), |
115 fake_drive_service_.get(), | 116 fake_drive_service_.get(), |
116 scheduler_.get(), | 117 scheduler_.get(), |
117 resource_metadata_.get(), | 118 resource_metadata_.get(), |
118 blocking_task_runner_)); | 119 blocking_task_runner_)); |
119 file_system_->AddObserver(mock_directory_observer_.get()); | 120 file_system_->AddObserver(mock_directory_observer_.get()); |
120 file_system_->Initialize(); | 121 file_system_->Initialize(); |
121 | 122 |
| 123 // Disable delaying so that the sync starts immediately. |
| 124 file_system_->sync_client_for_testing()->set_delay_for_testing( |
| 125 base::TimeDelta::FromSeconds(0)); |
| 126 |
122 FileError error = FILE_ERROR_FAILED; | 127 FileError error = FILE_ERROR_FAILED; |
123 resource_metadata_->Initialize( | 128 resource_metadata_->Initialize( |
124 google_apis::test_util::CreateCopyResultCallback(&error)); | 129 google_apis::test_util::CreateCopyResultCallback(&error)); |
125 google_apis::test_util::RunBlockingPoolTask(); | 130 google_apis::test_util::RunBlockingPoolTask(); |
126 ASSERT_EQ(FILE_ERROR_OK, error); | 131 ASSERT_EQ(FILE_ERROR_OK, error); |
127 } | 132 } |
128 | 133 |
129 virtual void TearDown() OVERRIDE { | 134 virtual void TearDown() OVERRIDE { |
130 ASSERT_TRUE(file_system_); | 135 ASSERT_TRUE(file_system_); |
131 file_system_.reset(); | 136 file_system_.reset(); |
(...skipping 524 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
656 true, // is_exclusive | 661 true, // is_exclusive |
657 false, // is_recursive | 662 false, // is_recursive |
658 google_apis::test_util::CreateCopyResultCallback(&error)); | 663 google_apis::test_util::CreateCopyResultCallback(&error)); |
659 google_apis::test_util::RunBlockingPoolTask(); | 664 google_apis::test_util::RunBlockingPoolTask(); |
660 | 665 |
661 // It should fail because is_exclusive is set to true. | 666 // It should fail because is_exclusive is set to true. |
662 EXPECT_EQ(FILE_ERROR_EXISTS, error); | 667 EXPECT_EQ(FILE_ERROR_EXISTS, error); |
663 } | 668 } |
664 | 669 |
665 TEST_F(FileSystemTest, PinAndUnpin) { | 670 TEST_F(FileSystemTest, PinAndUnpin) { |
| 671 // Pinned file gets synced and it results in entry state changes. |
| 672 EXPECT_CALL(*mock_directory_observer_, OnDirectoryChanged( |
| 673 Eq(base::FilePath(FILE_PATH_LITERAL("drive/root"))))).Times(AtLeast(1)); |
| 674 |
666 ASSERT_TRUE(LoadFullResourceList()); | 675 ASSERT_TRUE(LoadFullResourceList()); |
667 | 676 |
668 base::FilePath file_path(FILE_PATH_LITERAL("drive/root/File 1.txt")); | 677 base::FilePath file_path(FILE_PATH_LITERAL("drive/root/File 1.txt")); |
669 | 678 |
670 // Get the file info. | 679 // Get the file info. |
671 scoped_ptr<ResourceEntry> entry(GetResourceEntryByPathSync(file_path)); | 680 scoped_ptr<ResourceEntry> entry(GetResourceEntryByPathSync(file_path)); |
672 ASSERT_TRUE(entry); | 681 ASSERT_TRUE(entry); |
673 | 682 |
674 // Pin the file. | 683 // Pin the file. |
675 FileError error = FILE_ERROR_FAILED; | 684 FileError error = FILE_ERROR_FAILED; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
718 google_apis::test_util::RunBlockingPoolTask(); | 727 google_apis::test_util::RunBlockingPoolTask(); |
719 EXPECT_EQ(FILE_ERROR_OK, error); | 728 EXPECT_EQ(FILE_ERROR_OK, error); |
720 } | 729 } |
721 | 730 |
722 TEST_F(FileSystemTest, OpenAndCloseFile) { | 731 TEST_F(FileSystemTest, OpenAndCloseFile) { |
723 ASSERT_TRUE(LoadFullResourceList()); | 732 ASSERT_TRUE(LoadFullResourceList()); |
724 | 733 |
725 // The transfered file is cached and the change of "offline available" | 734 // The transfered file is cached and the change of "offline available" |
726 // attribute is notified. | 735 // attribute is notified. |
727 EXPECT_CALL(*mock_directory_observer_, OnDirectoryChanged( | 736 EXPECT_CALL(*mock_directory_observer_, OnDirectoryChanged( |
728 Eq(base::FilePath(FILE_PATH_LITERAL("drive/root"))))).Times(1); | 737 Eq(base::FilePath(FILE_PATH_LITERAL("drive/root"))))).Times(AtLeast(1)); |
729 | 738 |
730 const base::FilePath kFileInRoot(FILE_PATH_LITERAL("drive/root/File 1.txt")); | 739 const base::FilePath kFileInRoot(FILE_PATH_LITERAL("drive/root/File 1.txt")); |
731 scoped_ptr<ResourceEntry> entry(GetResourceEntryByPathSync(kFileInRoot)); | 740 scoped_ptr<ResourceEntry> entry(GetResourceEntryByPathSync(kFileInRoot)); |
732 const std::string& file_resource_id = entry->resource_id(); | 741 const std::string& file_resource_id = entry->resource_id(); |
733 const std::string& md5 = entry->file_specific_info().md5(); | 742 const std::string& md5 = entry->file_specific_info().md5(); |
734 | 743 |
735 // A dirty file is created on close. | |
736 EXPECT_CALL(*mock_cache_observer_, OnCacheCommitted(file_resource_id)) | |
737 .Times(1); | |
738 | |
739 // Open kFileInRoot ("drive/root/File 1.txt"). | 744 // Open kFileInRoot ("drive/root/File 1.txt"). |
740 FileError error = FILE_ERROR_FAILED; | 745 FileError error = FILE_ERROR_FAILED; |
741 base::FilePath file_path; | 746 base::FilePath file_path; |
742 file_system_->OpenFile( | 747 file_system_->OpenFile( |
743 kFileInRoot, | 748 kFileInRoot, |
744 google_apis::test_util::CreateCopyResultCallback(&error, &file_path)); | 749 google_apis::test_util::CreateCopyResultCallback(&error, &file_path)); |
745 google_apis::test_util::RunBlockingPoolTask(); | 750 google_apis::test_util::RunBlockingPoolTask(); |
746 const base::FilePath opened_file_path = file_path; | 751 const base::FilePath opened_file_path = file_path; |
747 | 752 |
748 // Verify that the file was properly opened. | 753 // Verify that the file was properly opened. |
(...skipping 22 matching lines...) Expand all Loading... |
771 EXPECT_TRUE(cache_entry.is_persistent()); | 776 EXPECT_TRUE(cache_entry.is_persistent()); |
772 | 777 |
773 base::FilePath cache_file_path; | 778 base::FilePath cache_file_path; |
774 cache_->GetFileOnUIThread(file_resource_id, md5, | 779 cache_->GetFileOnUIThread(file_resource_id, md5, |
775 google_apis::test_util::CreateCopyResultCallback( | 780 google_apis::test_util::CreateCopyResultCallback( |
776 &error, &cache_file_path)); | 781 &error, &cache_file_path)); |
777 google_apis::test_util::RunBlockingPoolTask(); | 782 google_apis::test_util::RunBlockingPoolTask(); |
778 EXPECT_EQ(FILE_ERROR_OK, error); | 783 EXPECT_EQ(FILE_ERROR_OK, error); |
779 EXPECT_EQ(cache_file_path, opened_file_path); | 784 EXPECT_EQ(cache_file_path, opened_file_path); |
780 | 785 |
| 786 // Write a new content. |
| 787 const std::string kNewContent = kExpectedContent + kExpectedContent; |
| 788 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(cache_file_path, |
| 789 kNewContent)); |
| 790 |
781 // Close kFileInRoot ("drive/root/File 1.txt"). | 791 // Close kFileInRoot ("drive/root/File 1.txt"). |
782 file_system_->CloseFile( | 792 file_system_->CloseFile( |
783 kFileInRoot, | 793 kFileInRoot, |
784 google_apis::test_util::CreateCopyResultCallback(&error)); | 794 google_apis::test_util::CreateCopyResultCallback(&error)); |
785 google_apis::test_util::RunBlockingPoolTask(); | 795 google_apis::test_util::RunBlockingPoolTask(); |
786 | 796 |
787 // Verify that the file was properly closed. | 797 // Verify that the file was properly closed. |
788 EXPECT_EQ(FILE_ERROR_OK, error); | 798 EXPECT_EQ(FILE_ERROR_OK, error); |
789 | 799 |
790 // Verify that the cache state was changed as expected. | 800 // Verify that the file was synced as expected. |
791 EXPECT_TRUE(GetCacheEntryFromOriginThread(file_resource_id, md5, | 801 google_apis::GDataErrorCode gdata_error = google_apis::GDATA_FILE_ERROR; |
792 &cache_entry)); | 802 scoped_ptr<google_apis::ResourceEntry> gdata_entry; |
793 EXPECT_TRUE(cache_entry.is_present()); | 803 fake_drive_service_->GetResourceEntry( |
794 EXPECT_TRUE(cache_entry.is_dirty()); | 804 file_resource_id, |
795 EXPECT_TRUE(cache_entry.is_persistent()); | 805 google_apis::test_util::CreateCopyResultCallback( |
| 806 &gdata_error, &gdata_entry)); |
| 807 google_apis::test_util::RunBlockingPoolTask(); |
| 808 EXPECT_EQ(gdata_error, google_apis::HTTP_SUCCESS); |
| 809 ASSERT_TRUE(gdata_entry); |
| 810 EXPECT_EQ(static_cast<int>(kNewContent.size()), gdata_entry->file_size()); |
796 | 811 |
797 // Try to close the same file twice. | 812 // Try to close the same file twice. |
798 file_system_->CloseFile( | 813 file_system_->CloseFile( |
799 kFileInRoot, | 814 kFileInRoot, |
800 google_apis::test_util::CreateCopyResultCallback(&error)); | 815 google_apis::test_util::CreateCopyResultCallback(&error)); |
801 google_apis::test_util::RunBlockingPoolTask(); | 816 google_apis::test_util::RunBlockingPoolTask(); |
802 | 817 |
803 // It must fail. | 818 // It must fail. |
804 EXPECT_EQ(FILE_ERROR_NOT_FOUND, error); | 819 EXPECT_EQ(FILE_ERROR_NOT_FOUND, error); |
805 } | 820 } |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
855 entry->resource_id(), | 870 entry->resource_id(), |
856 entry->file_specific_info().md5(), | 871 entry->file_specific_info().md5(), |
857 google_apis::test_util::CreateCopyResultCallback(&success, &cache_entry)); | 872 google_apis::test_util::CreateCopyResultCallback(&success, &cache_entry)); |
858 google_apis::test_util::RunBlockingPoolTask(); | 873 google_apis::test_util::RunBlockingPoolTask(); |
859 | 874 |
860 EXPECT_TRUE(success); | 875 EXPECT_TRUE(success); |
861 EXPECT_FALSE(cache_entry.is_mounted()); | 876 EXPECT_FALSE(cache_entry.is_mounted()); |
862 } | 877 } |
863 | 878 |
864 } // namespace drive | 879 } // namespace drive |
OLD | NEW |