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/file_system/open_file_operation.h"
9 #include "base/file_util.h"
10 #include "base/files/file_path.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/task_runner_util.h"
13 #include "chrome/browser/chromeos/drive/drive.pb.h"
14 #include "chrome/browser/chromeos/drive/file_errors.h"
15 #include "chrome/browser/chromeos/drive/file_system/operation_test_base.h"
16 #include "chrome/browser/google_apis/test_util.h"
17 #include "testing/gtest/include/gtest/gtest.h"
20 namespace file_system {
22 class OpenFileOperationTest : public OperationTestBase {
24 virtual void SetUp() OVERRIDE {
25 OperationTestBase::SetUp();
27 operation_.reset(new OpenFileOperation(
28 blocking_task_runner(), observer(), scheduler(), metadata(), cache(),
32 scoped_ptr<OpenFileOperation> operation_;
35 TEST_F(OpenFileOperationTest, OpenExistingFile) {
36 const base::FilePath file_in_root(
37 FILE_PATH_LITERAL("drive/root/File 1.txt"));
38 ResourceEntry src_entry;
39 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry));
40 const int64 file_size = src_entry.file_info().size();
42 FileError error = FILE_ERROR_FAILED;
43 base::FilePath file_path;
44 base::Closure close_callback;
48 std::string(), // mime_type
49 google_apis::test_util::CreateCopyResultCallback(
50 &error, &file_path, &close_callback));
51 test_util::RunBlockingPoolTask();
53 EXPECT_EQ(FILE_ERROR_OK, error);
54 ASSERT_TRUE(base::PathExists(file_path));
55 int64 local_file_size;
56 ASSERT_TRUE(file_util::GetFileSize(file_path, &local_file_size));
57 EXPECT_EQ(file_size, local_file_size);
59 ASSERT_FALSE(close_callback.is_null());
63 observer()->upload_needed_local_ids().count(src_entry.local_id()));
66 TEST_F(OpenFileOperationTest, OpenNonExistingFile) {
67 const base::FilePath file_in_root(
68 FILE_PATH_LITERAL("drive/root/not-exist.txt"));
70 FileError error = FILE_ERROR_FAILED;
71 base::FilePath file_path;
72 base::Closure close_callback;
76 std::string(), // mime_type
77 google_apis::test_util::CreateCopyResultCallback(
78 &error, &file_path, &close_callback));
79 test_util::RunBlockingPoolTask();
80 EXPECT_EQ(FILE_ERROR_NOT_FOUND, error);
81 EXPECT_TRUE(close_callback.is_null());
84 TEST_F(OpenFileOperationTest, CreateExistingFile) {
85 const base::FilePath file_in_root(
86 FILE_PATH_LITERAL("drive/root/File 1.txt"));
87 ResourceEntry src_entry;
88 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry));
90 FileError error = FILE_ERROR_FAILED;
91 base::FilePath file_path;
92 base::Closure close_callback;
96 std::string(), // mime_type
97 google_apis::test_util::CreateCopyResultCallback(
98 &error, &file_path, &close_callback));
99 test_util::RunBlockingPoolTask();
101 EXPECT_EQ(FILE_ERROR_EXISTS, error);
102 EXPECT_TRUE(close_callback.is_null());
105 TEST_F(OpenFileOperationTest, CreateNonExistingFile) {
106 const base::FilePath file_in_root(
107 FILE_PATH_LITERAL("drive/root/not-exist.txt"));
109 FileError error = FILE_ERROR_FAILED;
110 base::FilePath file_path;
111 base::Closure close_callback;
112 operation_->OpenFile(
115 std::string(), // mime_type
116 google_apis::test_util::CreateCopyResultCallback(
117 &error, &file_path, &close_callback));
118 test_util::RunBlockingPoolTask();
120 EXPECT_EQ(1U, observer()->get_changed_paths().size());
121 EXPECT_TRUE(observer()->get_changed_paths().count(file_in_root.DirName()));
123 EXPECT_EQ(FILE_ERROR_OK, error);
124 ASSERT_TRUE(base::PathExists(file_path));
125 int64 local_file_size;
126 ASSERT_TRUE(file_util::GetFileSize(file_path, &local_file_size));
127 EXPECT_EQ(0, local_file_size); // Should be an empty file.
129 ASSERT_FALSE(close_callback.is_null());
130 close_callback.Run();
133 observer()->upload_needed_local_ids().count(GetLocalId(file_in_root)));
136 TEST_F(OpenFileOperationTest, OpenOrCreateExistingFile) {
137 const base::FilePath file_in_root(
138 FILE_PATH_LITERAL("drive/root/File 1.txt"));
139 ResourceEntry src_entry;
140 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry));
141 const int64 file_size = src_entry.file_info().size();
143 FileError error = FILE_ERROR_FAILED;
144 base::FilePath file_path;
145 base::Closure close_callback;
146 operation_->OpenFile(
149 std::string(), // mime_type
150 google_apis::test_util::CreateCopyResultCallback(
151 &error, &file_path, &close_callback));
152 test_util::RunBlockingPoolTask();
154 // Notified because 'available offline' status of the existing file changes.
155 EXPECT_EQ(1U, observer()->get_changed_paths().size());
156 EXPECT_TRUE(observer()->get_changed_paths().count(file_in_root.DirName()));
158 EXPECT_EQ(FILE_ERROR_OK, error);
159 ASSERT_TRUE(base::PathExists(file_path));
160 int64 local_file_size;
161 ASSERT_TRUE(file_util::GetFileSize(file_path, &local_file_size));
162 EXPECT_EQ(file_size, local_file_size);
164 ASSERT_FALSE(close_callback.is_null());
165 close_callback.Run();
168 observer()->upload_needed_local_ids().count(src_entry.local_id()));
170 bool success = false;
171 FileCacheEntry cache_entry;
172 base::PostTaskAndReplyWithResult(
173 blocking_task_runner(),
175 base::Bind(&internal::FileCache::GetCacheEntry,
176 base::Unretained(cache()),
177 src_entry.local_id(),
179 google_apis::test_util::CreateCopyResultCallback(&success));
180 test_util::RunBlockingPoolTask();
181 EXPECT_TRUE(success);
182 EXPECT_TRUE(cache_entry.is_present());
183 EXPECT_TRUE(cache_entry.is_dirty());
186 TEST_F(OpenFileOperationTest, OpenOrCreateNonExistingFile) {
187 const base::FilePath file_in_root(
188 FILE_PATH_LITERAL("drive/root/not-exist.txt"));
190 FileError error = FILE_ERROR_FAILED;
191 base::FilePath file_path;
192 base::Closure close_callback;
193 operation_->OpenFile(
196 std::string(), // mime_type
197 google_apis::test_util::CreateCopyResultCallback(
198 &error, &file_path, &close_callback));
199 test_util::RunBlockingPoolTask();
201 EXPECT_EQ(FILE_ERROR_OK, error);
202 ASSERT_TRUE(base::PathExists(file_path));
203 int64 local_file_size;
204 ASSERT_TRUE(file_util::GetFileSize(file_path, &local_file_size));
205 EXPECT_EQ(0, local_file_size); // Should be an empty file.
207 ASSERT_FALSE(close_callback.is_null());
208 close_callback.Run();
211 observer()->upload_needed_local_ids().count(GetLocalId(file_in_root)));
214 TEST_F(OpenFileOperationTest, OpenFileTwice) {
215 const base::FilePath file_in_root(
216 FILE_PATH_LITERAL("drive/root/File 1.txt"));
217 ResourceEntry src_entry;
218 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry));
219 const int64 file_size = src_entry.file_info().size();
221 FileError error = FILE_ERROR_FAILED;
222 base::FilePath file_path;
223 base::Closure close_callback;
224 operation_->OpenFile(
227 std::string(), // mime_type
228 google_apis::test_util::CreateCopyResultCallback(
229 &error, &file_path, &close_callback));
230 test_util::RunBlockingPoolTask();
232 EXPECT_EQ(FILE_ERROR_OK, error);
233 ASSERT_TRUE(base::PathExists(file_path));
234 int64 local_file_size;
235 ASSERT_TRUE(file_util::GetFileSize(file_path, &local_file_size));
236 EXPECT_EQ(file_size, local_file_size);
239 error = FILE_ERROR_FAILED;
240 base::Closure close_callback2;
241 operation_->OpenFile(
244 std::string(), // mime_type
245 google_apis::test_util::CreateCopyResultCallback(
246 &error, &file_path, &close_callback2));
247 test_util::RunBlockingPoolTask();
249 EXPECT_EQ(FILE_ERROR_OK, error);
250 ASSERT_TRUE(base::PathExists(file_path));
251 ASSERT_TRUE(file_util::GetFileSize(file_path, &local_file_size));
252 EXPECT_EQ(file_size, local_file_size);
254 ASSERT_FALSE(close_callback.is_null());
255 ASSERT_FALSE(close_callback2.is_null());
257 close_callback.Run();
259 // There still remains a client opening the file, so it shouldn't be
261 EXPECT_TRUE(observer()->upload_needed_local_ids().empty());
263 close_callback2.Run();
265 // Here, all the clients close the file, so it should be uploaded then.
268 observer()->upload_needed_local_ids().count(src_entry.local_id()));
271 } // namespace file_system