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_cache.h"
15 #include "chrome/browser/chromeos/drive/file_errors.h"
16 #include "chrome/browser/chromeos/drive/file_system/operation_test_base.h"
17 #include "google_apis/drive/test_util.h"
18 #include "testing/gtest/include/gtest/gtest.h"
21 namespace file_system
{
23 class OpenFileOperationTest
: public OperationTestBase
{
25 virtual void SetUp() OVERRIDE
{
26 OperationTestBase::SetUp();
28 operation_
.reset(new OpenFileOperation(
29 blocking_task_runner(), observer(), scheduler(), metadata(), cache(),
33 scoped_ptr
<OpenFileOperation
> operation_
;
36 TEST_F(OpenFileOperationTest
, OpenExistingFile
) {
37 const base::FilePath
file_in_root(
38 FILE_PATH_LITERAL("drive/root/File 1.txt"));
39 ResourceEntry src_entry
;
40 ASSERT_EQ(FILE_ERROR_OK
, GetLocalResourceEntry(file_in_root
, &src_entry
));
41 const int64 file_size
= src_entry
.file_info().size();
43 FileError error
= FILE_ERROR_FAILED
;
44 base::FilePath file_path
;
45 base::Closure close_callback
;
49 std::string(), // mime_type
50 google_apis::test_util::CreateCopyResultCallback(
51 &error
, &file_path
, &close_callback
));
52 test_util::RunBlockingPoolTask();
54 EXPECT_EQ(FILE_ERROR_OK
, error
);
55 ASSERT_TRUE(base::PathExists(file_path
));
56 int64 local_file_size
;
57 ASSERT_TRUE(base::GetFileSize(file_path
, &local_file_size
));
58 EXPECT_EQ(file_size
, local_file_size
);
60 ASSERT_FALSE(close_callback
.is_null());
64 observer()->upload_needed_local_ids().count(src_entry
.local_id()));
67 TEST_F(OpenFileOperationTest
, OpenNonExistingFile
) {
68 const base::FilePath
file_in_root(
69 FILE_PATH_LITERAL("drive/root/not-exist.txt"));
71 FileError error
= FILE_ERROR_FAILED
;
72 base::FilePath file_path
;
73 base::Closure close_callback
;
77 std::string(), // mime_type
78 google_apis::test_util::CreateCopyResultCallback(
79 &error
, &file_path
, &close_callback
));
80 test_util::RunBlockingPoolTask();
81 EXPECT_EQ(FILE_ERROR_NOT_FOUND
, error
);
82 EXPECT_TRUE(close_callback
.is_null());
85 TEST_F(OpenFileOperationTest
, CreateExistingFile
) {
86 const base::FilePath
file_in_root(
87 FILE_PATH_LITERAL("drive/root/File 1.txt"));
88 ResourceEntry src_entry
;
89 ASSERT_EQ(FILE_ERROR_OK
, GetLocalResourceEntry(file_in_root
, &src_entry
));
91 FileError error
= FILE_ERROR_FAILED
;
92 base::FilePath file_path
;
93 base::Closure close_callback
;
97 std::string(), // mime_type
98 google_apis::test_util::CreateCopyResultCallback(
99 &error
, &file_path
, &close_callback
));
100 test_util::RunBlockingPoolTask();
102 EXPECT_EQ(FILE_ERROR_EXISTS
, error
);
103 EXPECT_TRUE(close_callback
.is_null());
106 TEST_F(OpenFileOperationTest
, CreateNonExistingFile
) {
107 const base::FilePath
file_in_root(
108 FILE_PATH_LITERAL("drive/root/not-exist.txt"));
110 FileError error
= FILE_ERROR_FAILED
;
111 base::FilePath file_path
;
112 base::Closure close_callback
;
113 operation_
->OpenFile(
116 std::string(), // mime_type
117 google_apis::test_util::CreateCopyResultCallback(
118 &error
, &file_path
, &close_callback
));
119 test_util::RunBlockingPoolTask();
121 EXPECT_EQ(1U, observer()->get_changed_paths().size());
122 EXPECT_TRUE(observer()->get_changed_paths().count(file_in_root
.DirName()));
124 EXPECT_EQ(FILE_ERROR_OK
, error
);
125 ASSERT_TRUE(base::PathExists(file_path
));
126 int64 local_file_size
;
127 ASSERT_TRUE(base::GetFileSize(file_path
, &local_file_size
));
128 EXPECT_EQ(0, local_file_size
); // Should be an empty file.
130 ASSERT_FALSE(close_callback
.is_null());
131 close_callback
.Run();
134 observer()->upload_needed_local_ids().count(GetLocalId(file_in_root
)));
137 TEST_F(OpenFileOperationTest
, OpenOrCreateExistingFile
) {
138 const base::FilePath
file_in_root(
139 FILE_PATH_LITERAL("drive/root/File 1.txt"));
140 ResourceEntry src_entry
;
141 ASSERT_EQ(FILE_ERROR_OK
, GetLocalResourceEntry(file_in_root
, &src_entry
));
142 const int64 file_size
= src_entry
.file_info().size();
144 FileError error
= FILE_ERROR_FAILED
;
145 base::FilePath file_path
;
146 base::Closure close_callback
;
147 operation_
->OpenFile(
150 std::string(), // mime_type
151 google_apis::test_util::CreateCopyResultCallback(
152 &error
, &file_path
, &close_callback
));
153 test_util::RunBlockingPoolTask();
155 // Notified because 'available offline' status of the existing file changes.
156 EXPECT_EQ(1U, observer()->get_changed_paths().size());
157 EXPECT_TRUE(observer()->get_changed_paths().count(file_in_root
.DirName()));
159 EXPECT_EQ(FILE_ERROR_OK
, error
);
160 ASSERT_TRUE(base::PathExists(file_path
));
161 int64 local_file_size
;
162 ASSERT_TRUE(base::GetFileSize(file_path
, &local_file_size
));
163 EXPECT_EQ(file_size
, local_file_size
);
165 ASSERT_FALSE(close_callback
.is_null());
166 close_callback
.Run();
169 observer()->upload_needed_local_ids().count(src_entry
.local_id()));
171 bool success
= false;
172 FileCacheEntry cache_entry
;
173 base::PostTaskAndReplyWithResult(
174 blocking_task_runner(),
176 base::Bind(&internal::FileCache::GetCacheEntry
,
177 base::Unretained(cache()),
178 src_entry
.local_id(),
180 google_apis::test_util::CreateCopyResultCallback(&success
));
181 test_util::RunBlockingPoolTask();
182 EXPECT_TRUE(success
);
183 EXPECT_TRUE(cache_entry
.is_present());
184 EXPECT_TRUE(cache_entry
.is_dirty());
187 TEST_F(OpenFileOperationTest
, OpenOrCreateNonExistingFile
) {
188 const base::FilePath
file_in_root(
189 FILE_PATH_LITERAL("drive/root/not-exist.txt"));
191 FileError error
= FILE_ERROR_FAILED
;
192 base::FilePath file_path
;
193 base::Closure close_callback
;
194 operation_
->OpenFile(
197 std::string(), // mime_type
198 google_apis::test_util::CreateCopyResultCallback(
199 &error
, &file_path
, &close_callback
));
200 test_util::RunBlockingPoolTask();
202 EXPECT_EQ(FILE_ERROR_OK
, error
);
203 ASSERT_TRUE(base::PathExists(file_path
));
204 int64 local_file_size
;
205 ASSERT_TRUE(base::GetFileSize(file_path
, &local_file_size
));
206 EXPECT_EQ(0, local_file_size
); // Should be an empty file.
208 ASSERT_FALSE(close_callback
.is_null());
209 close_callback
.Run();
212 observer()->upload_needed_local_ids().count(GetLocalId(file_in_root
)));
215 TEST_F(OpenFileOperationTest
, OpenFileTwice
) {
216 const base::FilePath
file_in_root(
217 FILE_PATH_LITERAL("drive/root/File 1.txt"));
218 ResourceEntry src_entry
;
219 ASSERT_EQ(FILE_ERROR_OK
, GetLocalResourceEntry(file_in_root
, &src_entry
));
220 const int64 file_size
= src_entry
.file_info().size();
222 FileError error
= FILE_ERROR_FAILED
;
223 base::FilePath file_path
;
224 base::Closure close_callback
;
225 operation_
->OpenFile(
228 std::string(), // mime_type
229 google_apis::test_util::CreateCopyResultCallback(
230 &error
, &file_path
, &close_callback
));
231 test_util::RunBlockingPoolTask();
233 EXPECT_EQ(FILE_ERROR_OK
, error
);
234 ASSERT_TRUE(base::PathExists(file_path
));
235 int64 local_file_size
;
236 ASSERT_TRUE(base::GetFileSize(file_path
, &local_file_size
));
237 EXPECT_EQ(file_size
, local_file_size
);
240 error
= FILE_ERROR_FAILED
;
241 base::Closure close_callback2
;
242 operation_
->OpenFile(
245 std::string(), // mime_type
246 google_apis::test_util::CreateCopyResultCallback(
247 &error
, &file_path
, &close_callback2
));
248 test_util::RunBlockingPoolTask();
250 EXPECT_EQ(FILE_ERROR_OK
, error
);
251 ASSERT_TRUE(base::PathExists(file_path
));
252 ASSERT_TRUE(base::GetFileSize(file_path
, &local_file_size
));
253 EXPECT_EQ(file_size
, local_file_size
);
255 ASSERT_FALSE(close_callback
.is_null());
256 ASSERT_FALSE(close_callback2
.is_null());
258 close_callback
.Run();
260 // There still remains a client opening the file, so it shouldn't be
262 EXPECT_TRUE(observer()->upload_needed_local_ids().empty());
264 close_callback2
.Run();
266 // Here, all the clients close the file, so it should be uploaded then.
269 observer()->upload_needed_local_ids().count(src_entry
.local_id()));
272 } // namespace file_system