1 // Copyright 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "chrome/browser/chromeos/drive/fake_file_system.h" 6 7 #include "base/files/file_util.h" 8 #include "base/run_loop.h" 9 #include "chrome/browser/chromeos/drive/file_system_util.h" 10 #include "chrome/browser/drive/fake_drive_service.h" 11 #include "chrome/browser/drive/test_util.h" 12 #include "content/public/test/test_browser_thread_bundle.h" 13 #include "google_apis/drive/test_util.h" 14 #include "testing/gtest/include/gtest/gtest.h" 15 16 namespace drive { 17 namespace test_util { 18 19 class FakeFileSystemTest : public ::testing::Test { 20 protected: 21 virtual void SetUp() OVERRIDE { 22 // Initialize FakeDriveService. 23 fake_drive_service_.reset(new FakeDriveService); 24 ASSERT_TRUE(SetUpTestEntries(fake_drive_service_.get())); 25 26 // Create a testee instance. 27 fake_file_system_.reset(new FakeFileSystem(fake_drive_service_.get())); 28 } 29 30 content::TestBrowserThreadBundle thread_bundle_; 31 scoped_ptr<FakeDriveService> fake_drive_service_; 32 scoped_ptr<FakeFileSystem> fake_file_system_; 33 }; 34 35 TEST_F(FakeFileSystemTest, GetFileContent) { 36 FileError initialize_error = FILE_ERROR_FAILED; 37 scoped_ptr<ResourceEntry> entry; 38 base::FilePath cache_file_path; 39 google_apis::test_util::TestGetContentCallback get_content_callback; 40 FileError completion_error = FILE_ERROR_FAILED; 41 42 const base::FilePath kDriveFile = 43 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt"); 44 45 // For the first time, the file should be downloaded from the service. 46 base::Closure cancel_download = fake_file_system_->GetFileContent( 47 kDriveFile, 48 google_apis::test_util::CreateCopyResultCallback( 49 &initialize_error, &cache_file_path, &entry), 50 get_content_callback.callback(), 51 google_apis::test_util::CreateCopyResultCallback(&completion_error)); 52 base::RunLoop().RunUntilIdle(); 53 54 EXPECT_EQ(FILE_ERROR_OK, initialize_error); 55 EXPECT_TRUE(entry); 56 57 // No cache file is available yet. 58 EXPECT_TRUE(cache_file_path.empty()); 59 60 // The download should be happened so the |get_content_callback| 61 // should have the actual data. 62 std::string content = get_content_callback.GetConcatenatedData(); 63 EXPECT_EQ(26U, content.size()); 64 EXPECT_EQ(FILE_ERROR_OK, completion_error); 65 66 initialize_error = FILE_ERROR_FAILED; 67 entry.reset(); 68 get_content_callback.mutable_data()->clear(); 69 completion_error = FILE_ERROR_FAILED; 70 71 // For the second time, the cache file should be found. 72 cancel_download = fake_file_system_->GetFileContent( 73 kDriveFile, 74 google_apis::test_util::CreateCopyResultCallback( 75 &initialize_error, &cache_file_path, &entry), 76 get_content_callback.callback(), 77 google_apis::test_util::CreateCopyResultCallback(&completion_error)); 78 base::RunLoop().RunUntilIdle(); 79 80 EXPECT_EQ(FILE_ERROR_OK, initialize_error); 81 EXPECT_TRUE(entry); 82 83 // Cache file should be available. 84 ASSERT_FALSE(cache_file_path.empty()); 85 86 // There should be a cache file so no data should be downloaded. 87 EXPECT_TRUE(get_content_callback.data().empty()); 88 EXPECT_EQ(FILE_ERROR_OK, completion_error); 89 90 // Make sure the cached file's content. 91 std::string cache_file_content; 92 ASSERT_TRUE( 93 base::ReadFileToString(cache_file_path, &cache_file_content)); 94 EXPECT_EQ(content, cache_file_content); 95 } 96 97 TEST_F(FakeFileSystemTest, GetFileContent_Directory) { 98 FileError initialize_error = FILE_ERROR_FAILED; 99 scoped_ptr<ResourceEntry> entry; 100 base::FilePath cache_file_path; 101 google_apis::test_util::TestGetContentCallback get_content_callback; 102 FileError completion_error = FILE_ERROR_FAILED; 103 base::Closure cancel_download = fake_file_system_->GetFileContent( 104 util::GetDriveMyDriveRootPath(), 105 google_apis::test_util::CreateCopyResultCallback( 106 &initialize_error, &cache_file_path, &entry), 107 get_content_callback.callback(), 108 google_apis::test_util::CreateCopyResultCallback(&completion_error)); 109 base::RunLoop().RunUntilIdle(); 110 111 EXPECT_EQ(FILE_ERROR_NOT_A_FILE, completion_error); 112 } 113 114 TEST_F(FakeFileSystemTest, GetResourceEntry) { 115 FileError error = FILE_ERROR_FAILED; 116 scoped_ptr<ResourceEntry> entry; 117 fake_file_system_->GetResourceEntry( 118 util::GetDriveMyDriveRootPath().AppendASCII( 119 "Directory 1/Sub Directory Folder"), 120 google_apis::test_util::CreateCopyResultCallback(&error, &entry)); 121 base::RunLoop().RunUntilIdle(); 122 123 ASSERT_EQ(FILE_ERROR_OK, error); 124 ASSERT_TRUE(entry); 125 EXPECT_EQ("sub_dir_folder_resource_id", entry->resource_id()); 126 } 127 128 TEST_F(FakeFileSystemTest, GetResourceEntry_Root) { 129 FileError error = FILE_ERROR_FAILED; 130 scoped_ptr<ResourceEntry> entry; 131 fake_file_system_->GetResourceEntry( 132 util::GetDriveMyDriveRootPath(), 133 google_apis::test_util::CreateCopyResultCallback(&error, &entry)); 134 base::RunLoop().RunUntilIdle(); 135 136 ASSERT_EQ(FILE_ERROR_OK, error); 137 ASSERT_TRUE(entry); 138 EXPECT_TRUE(entry->file_info().is_directory()); 139 EXPECT_EQ(fake_drive_service_->GetRootResourceId(), entry->resource_id()); 140 EXPECT_EQ(util::kDriveMyDriveRootDirName, entry->title()); 141 } 142 143 TEST_F(FakeFileSystemTest, GetResourceEntry_Invalid) { 144 FileError error = FILE_ERROR_FAILED; 145 scoped_ptr<ResourceEntry> entry; 146 fake_file_system_->GetResourceEntry( 147 util::GetDriveMyDriveRootPath().AppendASCII("Invalid File Name"), 148 google_apis::test_util::CreateCopyResultCallback(&error, &entry)); 149 base::RunLoop().RunUntilIdle(); 150 151 ASSERT_EQ(FILE_ERROR_NOT_FOUND, error); 152 ASSERT_FALSE(entry); 153 } 154 155 } // namespace test_util 156 } // namespace drive 157