- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / drive / file_system / open_file_operation_unittest.cc
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/file_system/open_file_operation.h"
6
7 #include <map>
8
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"
18
19 namespace drive {
20 namespace file_system {
21
22 class OpenFileOperationTest : public OperationTestBase {
23  protected:
24   virtual void SetUp() OVERRIDE {
25     OperationTestBase::SetUp();
26
27     operation_.reset(new OpenFileOperation(
28         blocking_task_runner(), observer(), scheduler(), metadata(), cache(),
29         temp_dir()));
30   }
31
32   scoped_ptr<OpenFileOperation> operation_;
33 };
34
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();
41
42   FileError error = FILE_ERROR_FAILED;
43   base::FilePath file_path;
44   base::Closure close_callback;
45   operation_->OpenFile(
46       file_in_root,
47       OPEN_FILE,
48       std::string(),  // mime_type
49       google_apis::test_util::CreateCopyResultCallback(
50           &error, &file_path, &close_callback));
51   test_util::RunBlockingPoolTask();
52
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);
58
59   ASSERT_FALSE(close_callback.is_null());
60   close_callback.Run();
61   EXPECT_EQ(
62       1U,
63       observer()->upload_needed_local_ids().count(src_entry.local_id()));
64 }
65
66 TEST_F(OpenFileOperationTest, OpenNonExistingFile) {
67   const base::FilePath file_in_root(
68       FILE_PATH_LITERAL("drive/root/not-exist.txt"));
69
70   FileError error = FILE_ERROR_FAILED;
71   base::FilePath file_path;
72   base::Closure close_callback;
73   operation_->OpenFile(
74       file_in_root,
75       OPEN_FILE,
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());
82 }
83
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));
89
90   FileError error = FILE_ERROR_FAILED;
91   base::FilePath file_path;
92   base::Closure close_callback;
93   operation_->OpenFile(
94       file_in_root,
95       CREATE_FILE,
96       std::string(),  // mime_type
97       google_apis::test_util::CreateCopyResultCallback(
98           &error, &file_path, &close_callback));
99   test_util::RunBlockingPoolTask();
100
101   EXPECT_EQ(FILE_ERROR_EXISTS, error);
102   EXPECT_TRUE(close_callback.is_null());
103 }
104
105 TEST_F(OpenFileOperationTest, CreateNonExistingFile) {
106   const base::FilePath file_in_root(
107       FILE_PATH_LITERAL("drive/root/not-exist.txt"));
108
109   FileError error = FILE_ERROR_FAILED;
110   base::FilePath file_path;
111   base::Closure close_callback;
112   operation_->OpenFile(
113       file_in_root,
114       CREATE_FILE,
115       std::string(),  // mime_type
116       google_apis::test_util::CreateCopyResultCallback(
117           &error, &file_path, &close_callback));
118   test_util::RunBlockingPoolTask();
119
120   EXPECT_EQ(1U, observer()->get_changed_paths().size());
121   EXPECT_TRUE(observer()->get_changed_paths().count(file_in_root.DirName()));
122
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.
128
129   ASSERT_FALSE(close_callback.is_null());
130   close_callback.Run();
131   EXPECT_EQ(
132       1U,
133       observer()->upload_needed_local_ids().count(GetLocalId(file_in_root)));
134 }
135
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();
142
143   FileError error = FILE_ERROR_FAILED;
144   base::FilePath file_path;
145   base::Closure close_callback;
146   operation_->OpenFile(
147       file_in_root,
148       OPEN_OR_CREATE_FILE,
149       std::string(),  // mime_type
150       google_apis::test_util::CreateCopyResultCallback(
151           &error, &file_path, &close_callback));
152   test_util::RunBlockingPoolTask();
153
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()));
157
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);
163
164   ASSERT_FALSE(close_callback.is_null());
165   close_callback.Run();
166   EXPECT_EQ(
167       1U,
168       observer()->upload_needed_local_ids().count(src_entry.local_id()));
169
170   bool success = false;
171   FileCacheEntry cache_entry;
172   base::PostTaskAndReplyWithResult(
173       blocking_task_runner(),
174       FROM_HERE,
175       base::Bind(&internal::FileCache::GetCacheEntry,
176                  base::Unretained(cache()),
177                  src_entry.local_id(),
178                  &cache_entry),
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());
184 }
185
186 TEST_F(OpenFileOperationTest, OpenOrCreateNonExistingFile) {
187   const base::FilePath file_in_root(
188       FILE_PATH_LITERAL("drive/root/not-exist.txt"));
189
190   FileError error = FILE_ERROR_FAILED;
191   base::FilePath file_path;
192   base::Closure close_callback;
193   operation_->OpenFile(
194       file_in_root,
195       OPEN_OR_CREATE_FILE,
196       std::string(),  // mime_type
197       google_apis::test_util::CreateCopyResultCallback(
198           &error, &file_path, &close_callback));
199   test_util::RunBlockingPoolTask();
200
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.
206
207   ASSERT_FALSE(close_callback.is_null());
208   close_callback.Run();
209   EXPECT_EQ(
210       1U,
211       observer()->upload_needed_local_ids().count(GetLocalId(file_in_root)));
212 }
213
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();
220
221   FileError error = FILE_ERROR_FAILED;
222   base::FilePath file_path;
223   base::Closure close_callback;
224   operation_->OpenFile(
225       file_in_root,
226       OPEN_FILE,
227       std::string(),  // mime_type
228       google_apis::test_util::CreateCopyResultCallback(
229           &error, &file_path, &close_callback));
230   test_util::RunBlockingPoolTask();
231
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);
237
238   // Open again.
239   error = FILE_ERROR_FAILED;
240   base::Closure close_callback2;
241   operation_->OpenFile(
242       file_in_root,
243       OPEN_FILE,
244       std::string(),  // mime_type
245       google_apis::test_util::CreateCopyResultCallback(
246           &error, &file_path, &close_callback2));
247   test_util::RunBlockingPoolTask();
248
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);
253
254   ASSERT_FALSE(close_callback.is_null());
255   ASSERT_FALSE(close_callback2.is_null());
256
257   close_callback.Run();
258
259   // There still remains a client opening the file, so it shouldn't be
260   // uploaded yet.
261   EXPECT_TRUE(observer()->upload_needed_local_ids().empty());
262
263   close_callback2.Run();
264
265   // Here, all the clients close the file, so it should be uploaded then.
266   EXPECT_EQ(
267       1U,
268       observer()->upload_needed_local_ids().count(src_entry.local_id()));
269 }
270
271 }  // namespace file_system
272 }  // namespace drive