fake_file_system_unittest.cc revision d0247b1b59f9c528cb6df88b4f2b9afaf80d181e
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/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"
14
15namespace drive {
16namespace test_util {
17
18class FakeFileSystemTest : public ::testing::Test {
19 protected:
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"));
27
28    // Create a testee instance.
29    fake_file_system_.reset(new FakeFileSystem(fake_drive_service_.get()));
30  }
31
32  content::TestBrowserThreadBundle thread_bundle_;
33  scoped_ptr<FakeDriveService> fake_drive_service_;
34  scoped_ptr<FakeFileSystem> fake_file_system_;
35};
36
37TEST_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;
44
45  const base::FilePath kDriveFile =
46      util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt");
47
48  // For the first time, the file should be downloaded from the service.
49  fake_file_system_->GetFileContentByPath(
50      kDriveFile,
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();
56
57  EXPECT_EQ(FILE_ERROR_OK, initialize_error);
58  EXPECT_TRUE(entry);
59
60  // No cache file is available yet.
61  EXPECT_TRUE(cache_file_path.empty());
62
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);
68
69  initialize_error = FILE_ERROR_FAILED;
70  entry.reset();
71  get_content_callback.mutable_data()->clear();
72  completion_error = FILE_ERROR_FAILED;
73
74  // For the second time, the cache file should be found.
75  fake_file_system_->GetFileContentByPath(
76      kDriveFile,
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();
82
83  EXPECT_EQ(FILE_ERROR_OK, initialize_error);
84  EXPECT_TRUE(entry);
85
86  // Cache file should be available.
87  ASSERT_FALSE(cache_file_path.empty());
88
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);
92
93  // Make sure the cached file's content.
94  std::string cache_file_content;
95  ASSERT_TRUE(
96      base::ReadFileToString(cache_file_path, &cache_file_content));
97  EXPECT_EQ(content, cache_file_content);
98}
99
100TEST_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;
107
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();
115
116  EXPECT_EQ(FILE_ERROR_NOT_A_FILE, completion_error);
117}
118
119TEST_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();
127
128  ASSERT_EQ(FILE_ERROR_OK, error);
129  ASSERT_TRUE(entry);
130  EXPECT_EQ("folder:sub_dir_folder_resource_id", entry->resource_id());
131}
132
133TEST_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();
140
141  ASSERT_EQ(FILE_ERROR_OK, error);
142  ASSERT_TRUE(entry);
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());
146}
147
148TEST_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();
155
156  ASSERT_EQ(FILE_ERROR_NOT_FOUND, error);
157  ASSERT_FALSE(entry);
158}
159
160}  // namespace test_util
161}  // namespace drive
162