fake_file_system_unittest.cc revision a93a17c8d99d686bd4a1511e5504e5e6cc9fcadf
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/file_util.h"
8#include "base/message_loop.h"
9#include "chrome/browser/chromeos/drive/file_system_util.h"
10#include "chrome/browser/google_apis/fake_drive_service.h"
11#include "chrome/browser/google_apis/test_util.h"
12#include "content/public/browser/browser_thread.h"
13#include "content/public/test/test_browser_thread.h"
14#include "testing/gtest/include/gtest/gtest.h"
15
16namespace drive {
17namespace test_util {
18
19class FakeFileSystemTest : public ::testing::Test {
20 protected:
21  FakeFileSystemTest()
22      : ui_thread_(content::BrowserThread::UI, &message_loop_) {
23  }
24
25  virtual void SetUp() OVERRIDE {
26    // Initialize FakeDriveService.
27    fake_drive_service_.reset(new google_apis::FakeDriveService);
28    fake_drive_service_->LoadResourceListForWapi(
29        "chromeos/gdata/root_feed.json");
30    fake_drive_service_->LoadAccountMetadataForWapi(
31        "chromeos/gdata/account_metadata.json");
32    fake_drive_service_->LoadAppListForDriveApi("chromeos/drive/applist.json");
33
34    // Create a testee instance.
35    fake_file_system_.reset(
36        new FakeFileSystem(fake_drive_service_.get()));
37    ASSERT_TRUE(fake_file_system_->InitializeForTesting());
38  }
39
40  MessageLoopForUI message_loop_;
41  content::TestBrowserThread ui_thread_;
42
43  scoped_ptr<google_apis::FakeDriveService> fake_drive_service_;
44  scoped_ptr<FakeFileSystem> fake_file_system_;
45};
46
47TEST_F(FakeFileSystemTest, GetResourceEntryById) {
48  FileError error = FILE_ERROR_FAILED;
49  scoped_ptr<ResourceEntry> entry;
50  base::FilePath file_path;
51
52  fake_file_system_->GetResourceEntryById(
53      "folder:sub_dir_folder_resource_id",
54      google_apis::test_util::CreateCopyResultCallback(
55          &error, &file_path, &entry));
56  google_apis::test_util::RunBlockingPoolTask();
57
58  ASSERT_EQ(FILE_ERROR_OK, error);
59  EXPECT_EQ(
60      util::GetDriveMyDriveRootPath().AppendASCII(
61          "Directory 1/Sub Directory Folder"),
62      file_path);
63  EXPECT_TRUE(entry);  // Just make sure something is returned.
64}
65
66TEST_F(FakeFileSystemTest,
67       GetResourceEntryById_PathCompatibleWithGetResourceEntryByPath) {
68  const std::string document_resource_id = "document:5_document_resource_id";
69
70  FileError error = FILE_ERROR_FAILED;
71  scoped_ptr<ResourceEntry> entry;
72  base::FilePath file_path;
73
74  // Get resource entry by resource id.
75  fake_file_system_->GetResourceEntryById(
76      document_resource_id,
77      google_apis::test_util::CreateCopyResultCallback(
78          &error, &file_path, &entry));
79  google_apis::test_util::RunBlockingPoolTask();
80
81  ASSERT_EQ(FILE_ERROR_OK, error);
82  ASSERT_TRUE(entry);
83  EXPECT_TRUE(entry->file_specific_info().is_hosted_document());
84
85  // Get resource entry by path given by GetResourceEntryById.
86  error = FILE_ERROR_FAILED;
87  entry.reset();
88  fake_file_system_->GetResourceEntryByPath(
89      file_path,
90      google_apis::test_util::CreateCopyResultCallback(&error, &entry));
91  google_apis::test_util::RunBlockingPoolTask();
92
93  ASSERT_EQ(FILE_ERROR_OK, error);
94  ASSERT_TRUE(entry);
95  EXPECT_EQ(document_resource_id, entry->resource_id());
96}
97
98TEST_F(FakeFileSystemTest, GetFileContentByPath) {
99  FileError initialize_error = FILE_ERROR_FAILED;
100  scoped_ptr<ResourceEntry> entry;
101  base::FilePath cache_file_path;
102  base::Closure cancel_download;
103  google_apis::test_util::TestGetContentCallback get_content_callback;
104  FileError completion_error = FILE_ERROR_FAILED;
105
106  const base::FilePath kDriveFile =
107      util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt");
108
109  // For the first time, the file should be downloaded from the service.
110  fake_file_system_->GetFileContentByPath(
111      kDriveFile,
112      google_apis::test_util::CreateCopyResultCallback(
113          &initialize_error, &entry, &cache_file_path, &cancel_download),
114      get_content_callback.callback(),
115      google_apis::test_util::CreateCopyResultCallback(&completion_error));
116  google_apis::test_util::RunBlockingPoolTask();
117
118  EXPECT_EQ(FILE_ERROR_OK, initialize_error);
119  EXPECT_TRUE(entry);
120
121  // No cache file is available yet.
122  EXPECT_TRUE(cache_file_path.empty());
123
124  // The download should be happened so the |get_content_callback|
125  // should have the actual data.
126  std::string content = get_content_callback.GetConcatenatedData();
127  EXPECT_EQ(10U, content.size());
128  EXPECT_EQ(FILE_ERROR_OK, completion_error);
129
130  initialize_error = FILE_ERROR_FAILED;
131  entry.reset();
132  get_content_callback.mutable_data()->clear();
133  completion_error = FILE_ERROR_FAILED;
134
135  // For the second time, the cache file should be found.
136  fake_file_system_->GetFileContentByPath(
137      kDriveFile,
138      google_apis::test_util::CreateCopyResultCallback(
139          &initialize_error, &entry, &cache_file_path, &cancel_download),
140      get_content_callback.callback(),
141      google_apis::test_util::CreateCopyResultCallback(&completion_error));
142  google_apis::test_util::RunBlockingPoolTask();
143
144  EXPECT_EQ(FILE_ERROR_OK, initialize_error);
145  EXPECT_TRUE(entry);
146
147  // Cache file should be available.
148  ASSERT_FALSE(cache_file_path.empty());
149
150  // There should be a cache file so no data should be downloaded.
151  EXPECT_TRUE(get_content_callback.data().empty());
152  EXPECT_EQ(FILE_ERROR_OK, completion_error);
153
154  // Make sure the cached file's content.
155  std::string cache_file_content;
156  ASSERT_TRUE(
157      file_util::ReadFileToString(cache_file_path, &cache_file_content));
158  EXPECT_EQ(content, cache_file_content);
159}
160
161TEST_F(FakeFileSystemTest, GetFileContentByPath_Directory) {
162  FileError initialize_error = FILE_ERROR_FAILED;
163  scoped_ptr<ResourceEntry> entry;
164  base::FilePath cache_file_path;
165  google_apis::test_util::TestGetContentCallback get_content_callback;
166  FileError completion_error = FILE_ERROR_FAILED;
167  base::Closure cancel_download;
168
169  fake_file_system_->GetFileContentByPath(
170      util::GetDriveMyDriveRootPath(),
171      google_apis::test_util::CreateCopyResultCallback(
172          &initialize_error, &entry, &cache_file_path, &cancel_download),
173      get_content_callback.callback(),
174      google_apis::test_util::CreateCopyResultCallback(&completion_error));
175  google_apis::test_util::RunBlockingPoolTask();
176
177  EXPECT_EQ(FILE_ERROR_NOT_A_FILE, completion_error);
178}
179
180TEST_F(FakeFileSystemTest, GetResourceEntryByPath) {
181  FileError error = FILE_ERROR_FAILED;
182  scoped_ptr<ResourceEntry> entry;
183  fake_file_system_->GetResourceEntryByPath(
184      util::GetDriveMyDriveRootPath().AppendASCII(
185          "Directory 1/Sub Directory Folder"),
186      google_apis::test_util::CreateCopyResultCallback(&error, &entry));
187  google_apis::test_util::RunBlockingPoolTask();
188
189  ASSERT_EQ(FILE_ERROR_OK, error);
190  ASSERT_TRUE(entry);
191  EXPECT_EQ("folder:sub_dir_folder_resource_id", entry->resource_id());
192}
193
194TEST_F(FakeFileSystemTest, GetResourceEntryByPath_Root) {
195  FileError error = FILE_ERROR_FAILED;
196  scoped_ptr<ResourceEntry> entry;
197  fake_file_system_->GetResourceEntryByPath(
198      util::GetDriveMyDriveRootPath(),
199      google_apis::test_util::CreateCopyResultCallback(&error, &entry));
200  google_apis::test_util::RunBlockingPoolTask();
201
202  ASSERT_EQ(FILE_ERROR_OK, error);
203  ASSERT_TRUE(entry);
204  EXPECT_TRUE(entry->file_info().is_directory());
205  EXPECT_EQ(fake_drive_service_->GetRootResourceId(), entry->resource_id());
206  EXPECT_EQ(util::kDriveMyDriveRootDirName, entry->title());
207}
208
209TEST_F(FakeFileSystemTest, GetResourceEntryByPath_Invalid) {
210  FileError error = FILE_ERROR_FAILED;
211  scoped_ptr<ResourceEntry> entry;
212  fake_file_system_->GetResourceEntryByPath(
213      util::GetDriveMyDriveRootPath().AppendASCII("Invalid File Name"),
214      google_apis::test_util::CreateCopyResultCallback(&error, &entry));
215  google_apis::test_util::RunBlockingPoolTask();
216
217  ASSERT_EQ(FILE_ERROR_NOT_FOUND, error);
218  ASSERT_FALSE(entry);
219}
220
221}  // namespace test_util
222}  // namespace drive
223