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 "content/public/test/test_browser_thread_bundle.h"
12 #include "google_apis/drive/test_util.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, GetFileContent) {
38 FileError initialize_error = FILE_ERROR_FAILED;
39 scoped_ptr<ResourceEntry> entry;
40 base::FilePath cache_file_path;
41 google_apis::test_util::TestGetContentCallback get_content_callback;
42 FileError completion_error = FILE_ERROR_FAILED;
44 const base::FilePath kDriveFile =
45 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt");
47 // For the first time, the file should be downloaded from the service.
48 base::Closure cancel_download = fake_file_system_->GetFileContent(
50 google_apis::test_util::CreateCopyResultCallback(
51 &initialize_error, &cache_file_path, &entry),
52 get_content_callback.callback(),
53 google_apis::test_util::CreateCopyResultCallback(&completion_error));
54 base::RunLoop().RunUntilIdle();
56 EXPECT_EQ(FILE_ERROR_OK, initialize_error);
59 // No cache file is available yet.
60 EXPECT_TRUE(cache_file_path.empty());
62 // The download should be happened so the |get_content_callback|
63 // should have the actual data.
64 std::string content = get_content_callback.GetConcatenatedData();
65 EXPECT_EQ(26U, content.size());
66 EXPECT_EQ(FILE_ERROR_OK, completion_error);
68 initialize_error = FILE_ERROR_FAILED;
70 get_content_callback.mutable_data()->clear();
71 completion_error = FILE_ERROR_FAILED;
73 // For the second time, the cache file should be found.
74 cancel_download = fake_file_system_->GetFileContent(
76 google_apis::test_util::CreateCopyResultCallback(
77 &initialize_error, &cache_file_path, &entry),
78 get_content_callback.callback(),
79 google_apis::test_util::CreateCopyResultCallback(&completion_error));
80 base::RunLoop().RunUntilIdle();
82 EXPECT_EQ(FILE_ERROR_OK, initialize_error);
85 // Cache file should be available.
86 ASSERT_FALSE(cache_file_path.empty());
88 // There should be a cache file so no data should be downloaded.
89 EXPECT_TRUE(get_content_callback.data().empty());
90 EXPECT_EQ(FILE_ERROR_OK, completion_error);
92 // Make sure the cached file's content.
93 std::string cache_file_content;
95 base::ReadFileToString(cache_file_path, &cache_file_content));
96 EXPECT_EQ(content, cache_file_content);
99 TEST_F(FakeFileSystemTest, GetFileContent_Directory) {
100 FileError initialize_error = FILE_ERROR_FAILED;
101 scoped_ptr<ResourceEntry> entry;
102 base::FilePath cache_file_path;
103 google_apis::test_util::TestGetContentCallback get_content_callback;
104 FileError completion_error = FILE_ERROR_FAILED;
105 base::Closure cancel_download = fake_file_system_->GetFileContent(
106 util::GetDriveMyDriveRootPath(),
107 google_apis::test_util::CreateCopyResultCallback(
108 &initialize_error, &cache_file_path, &entry),
109 get_content_callback.callback(),
110 google_apis::test_util::CreateCopyResultCallback(&completion_error));
111 base::RunLoop().RunUntilIdle();
113 EXPECT_EQ(FILE_ERROR_NOT_A_FILE, completion_error);
116 TEST_F(FakeFileSystemTest, GetResourceEntry) {
117 FileError error = FILE_ERROR_FAILED;
118 scoped_ptr<ResourceEntry> entry;
119 fake_file_system_->GetResourceEntry(
120 util::GetDriveMyDriveRootPath().AppendASCII(
121 "Directory 1/Sub Directory Folder"),
122 google_apis::test_util::CreateCopyResultCallback(&error, &entry));
123 base::RunLoop().RunUntilIdle();
125 ASSERT_EQ(FILE_ERROR_OK, error);
127 EXPECT_EQ("folder:sub_dir_folder_resource_id", entry->resource_id());
130 TEST_F(FakeFileSystemTest, GetResourceEntry_Root) {
131 FileError error = FILE_ERROR_FAILED;
132 scoped_ptr<ResourceEntry> entry;
133 fake_file_system_->GetResourceEntry(
134 util::GetDriveMyDriveRootPath(),
135 google_apis::test_util::CreateCopyResultCallback(&error, &entry));
136 base::RunLoop().RunUntilIdle();
138 ASSERT_EQ(FILE_ERROR_OK, error);
140 EXPECT_TRUE(entry->file_info().is_directory());
141 EXPECT_EQ(fake_drive_service_->GetRootResourceId(), entry->resource_id());
142 EXPECT_EQ(util::kDriveMyDriveRootDirName, entry->title());
145 TEST_F(FakeFileSystemTest, GetResourceEntry_Invalid) {
146 FileError error = FILE_ERROR_FAILED;
147 scoped_ptr<ResourceEntry> entry;
148 fake_file_system_->GetResourceEntry(
149 util::GetDriveMyDriveRootPath().AppendASCII("Invalid File Name"),
150 google_apis::test_util::CreateCopyResultCallback(&error, &entry));
151 base::RunLoop().RunUntilIdle();
153 ASSERT_EQ(FILE_ERROR_NOT_FOUND, error);
157 } // namespace test_util