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.
5 #include "chrome/browser/chromeos/drive/fake_file_system.h"
7 #include "base/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/google_apis/test_util.h"
12 #include "content/public/test/test_browser_thread_bundle.h"
13 #include "testing/gtest/include/gtest/gtest.h"
18 class FakeFileSystemTest : public ::testing::Test {
20 virtual void SetUp() OVERRIDE {
21 // Initialize FakeDriveService.
22 fake_drive_service_.reset(new FakeDriveService);
23 ASSERT_TRUE(fake_drive_service_->LoadResourceListForWapi(
24 "gdata/root_feed.json"));
25 ASSERT_TRUE(fake_drive_service_->LoadAccountMetadataForWapi(
26 "gdata/account_metadata.json"));
28 // Create a testee instance.
29 fake_file_system_.reset(new FakeFileSystem(fake_drive_service_.get()));
32 content::TestBrowserThreadBundle thread_bundle_;
33 scoped_ptr<FakeDriveService> fake_drive_service_;
34 scoped_ptr<FakeFileSystem> fake_file_system_;
37 TEST_F(FakeFileSystemTest, GetFileContentByPath) {
38 FileError initialize_error = FILE_ERROR_FAILED;
39 scoped_ptr<ResourceEntry> entry;
40 base::FilePath cache_file_path;
41 base::Closure cancel_download;
42 google_apis::test_util::TestGetContentCallback get_content_callback;
43 FileError completion_error = FILE_ERROR_FAILED;
45 const base::FilePath kDriveFile =
46 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt");
48 // For the first time, the file should be downloaded from the service.
49 fake_file_system_->GetFileContentByPath(
51 google_apis::test_util::CreateCopyResultCallback(
52 &initialize_error, &entry, &cache_file_path, &cancel_download),
53 get_content_callback.callback(),
54 google_apis::test_util::CreateCopyResultCallback(&completion_error));
55 base::RunLoop().RunUntilIdle();
57 EXPECT_EQ(FILE_ERROR_OK, initialize_error);
60 // No cache file is available yet.
61 EXPECT_TRUE(cache_file_path.empty());
63 // The download should be happened so the |get_content_callback|
64 // should have the actual data.
65 std::string content = get_content_callback.GetConcatenatedData();
66 EXPECT_EQ(26U, content.size());
67 EXPECT_EQ(FILE_ERROR_OK, completion_error);
69 initialize_error = FILE_ERROR_FAILED;
71 get_content_callback.mutable_data()->clear();
72 completion_error = FILE_ERROR_FAILED;
74 // For the second time, the cache file should be found.
75 fake_file_system_->GetFileContentByPath(
77 google_apis::test_util::CreateCopyResultCallback(
78 &initialize_error, &entry, &cache_file_path, &cancel_download),
79 get_content_callback.callback(),
80 google_apis::test_util::CreateCopyResultCallback(&completion_error));
81 base::RunLoop().RunUntilIdle();
83 EXPECT_EQ(FILE_ERROR_OK, initialize_error);
86 // Cache file should be available.
87 ASSERT_FALSE(cache_file_path.empty());
89 // There should be a cache file so no data should be downloaded.
90 EXPECT_TRUE(get_content_callback.data().empty());
91 EXPECT_EQ(FILE_ERROR_OK, completion_error);
93 // Make sure the cached file's content.
94 std::string cache_file_content;
96 base::ReadFileToString(cache_file_path, &cache_file_content));
97 EXPECT_EQ(content, cache_file_content);
100 TEST_F(FakeFileSystemTest, GetFileContentByPath_Directory) {
101 FileError initialize_error = FILE_ERROR_FAILED;
102 scoped_ptr<ResourceEntry> entry;
103 base::FilePath cache_file_path;
104 google_apis::test_util::TestGetContentCallback get_content_callback;
105 FileError completion_error = FILE_ERROR_FAILED;
106 base::Closure cancel_download;
108 fake_file_system_->GetFileContentByPath(
109 util::GetDriveMyDriveRootPath(),
110 google_apis::test_util::CreateCopyResultCallback(
111 &initialize_error, &entry, &cache_file_path, &cancel_download),
112 get_content_callback.callback(),
113 google_apis::test_util::CreateCopyResultCallback(&completion_error));
114 base::RunLoop().RunUntilIdle();
116 EXPECT_EQ(FILE_ERROR_NOT_A_FILE, completion_error);
119 TEST_F(FakeFileSystemTest, GetResourceEntryByPath) {
120 FileError error = FILE_ERROR_FAILED;
121 scoped_ptr<ResourceEntry> entry;
122 fake_file_system_->GetResourceEntryByPath(
123 util::GetDriveMyDriveRootPath().AppendASCII(
124 "Directory 1/Sub Directory Folder"),
125 google_apis::test_util::CreateCopyResultCallback(&error, &entry));
126 base::RunLoop().RunUntilIdle();
128 ASSERT_EQ(FILE_ERROR_OK, error);
130 EXPECT_EQ("folder:sub_dir_folder_resource_id", entry->resource_id());
133 TEST_F(FakeFileSystemTest, GetResourceEntryByPath_Root) {
134 FileError error = FILE_ERROR_FAILED;
135 scoped_ptr<ResourceEntry> entry;
136 fake_file_system_->GetResourceEntryByPath(
137 util::GetDriveMyDriveRootPath(),
138 google_apis::test_util::CreateCopyResultCallback(&error, &entry));
139 base::RunLoop().RunUntilIdle();
141 ASSERT_EQ(FILE_ERROR_OK, error);
143 EXPECT_TRUE(entry->file_info().is_directory());
144 EXPECT_EQ(fake_drive_service_->GetRootResourceId(), entry->resource_id());
145 EXPECT_EQ(util::kDriveMyDriveRootDirName, entry->title());
148 TEST_F(FakeFileSystemTest, GetResourceEntryByPath_Invalid) {
149 FileError error = FILE_ERROR_FAILED;
150 scoped_ptr<ResourceEntry> entry;
151 fake_file_system_->GetResourceEntryByPath(
152 util::GetDriveMyDriveRootPath().AppendASCII("Invalid File Name"),
153 google_apis::test_util::CreateCopyResultCallback(&error, &entry));
154 base::RunLoop().RunUntilIdle();
156 ASSERT_EQ(FILE_ERROR_NOT_FOUND, error);
160 } // namespace test_util