- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / drive / file_system / update_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/update_operation.h"
6
7 #include "base/task_runner_util.h"
8 #include "chrome/browser/chromeos/drive/file_system/operation_test_base.h"
9 #include "chrome/browser/chromeos/drive/file_system_interface.h"
10 #include "chrome/browser/drive/fake_drive_service.h"
11 #include "chrome/browser/google_apis/gdata_wapi_parser.h"
12 #include "chrome/browser/google_apis/test_util.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace drive {
16 namespace file_system {
17
18 class UpdateOperationTest : public OperationTestBase {
19  protected:
20   virtual void SetUp() OVERRIDE {
21    OperationTestBase::SetUp();
22    operation_.reset(new UpdateOperation(blocking_task_runner(),
23                                         observer(),
24                                         scheduler(),
25                                         metadata(),
26                                         cache()));
27  }
28
29  scoped_ptr<UpdateOperation> operation_;
30 };
31
32 TEST_F(UpdateOperationTest, UpdateFileByLocalId_PersistentFile) {
33   const base::FilePath kFilePath(FILE_PATH_LITERAL("drive/root/File 1.txt"));
34   const std::string kResourceId("file:2_file_resource_id");
35   const std::string kMd5("3b4382ebefec6e743578c76bbd0575ce");
36
37   const base::FilePath kTestFile = temp_dir().Append(FILE_PATH_LITERAL("foo"));
38   const std::string kTestFileContent = "I'm being uploaded! Yay!";
39   google_apis::test_util::WriteStringToFile(kTestFile, kTestFileContent);
40
41   const std::string local_id = GetLocalId(kFilePath);
42   EXPECT_FALSE(local_id.empty());
43
44   // Pin the file so it'll be store in "persistent" directory.
45   FileError error = FILE_ERROR_FAILED;
46   cache()->PinOnUIThread(
47       local_id,
48       google_apis::test_util::CreateCopyResultCallback(&error));
49   test_util::RunBlockingPoolTask();
50   EXPECT_EQ(FILE_ERROR_OK, error);
51
52   // First store a file to cache.
53   error = FILE_ERROR_FAILED;
54   base::PostTaskAndReplyWithResult(
55       blocking_task_runner(),
56       FROM_HERE,
57       base::Bind(&internal::FileCache::Store,
58                  base::Unretained(cache()),
59                  local_id, kMd5, kTestFile,
60                  internal::FileCache::FILE_OPERATION_COPY),
61       google_apis::test_util::CreateCopyResultCallback(&error));
62   test_util::RunBlockingPoolTask();
63   EXPECT_EQ(FILE_ERROR_OK, error);
64
65   // Add the dirty bit.
66   error = FILE_ERROR_FAILED;
67   base::PostTaskAndReplyWithResult(
68       blocking_task_runner(),
69       FROM_HERE,
70       base::Bind(&internal::FileCache::MarkDirty,
71                  base::Unretained(cache()),
72                  local_id),
73       google_apis::test_util::CreateCopyResultCallback(&error));
74   test_util::RunBlockingPoolTask();
75   EXPECT_EQ(FILE_ERROR_OK, error);
76
77   int64 original_changestamp = fake_service()->largest_changestamp();
78
79   // The callback will be called upon completion of UpdateFileByLocalId().
80   error = FILE_ERROR_FAILED;
81   operation_->UpdateFileByLocalId(
82       local_id,
83       ClientContext(USER_INITIATED),
84       UpdateOperation::RUN_CONTENT_CHECK,
85       google_apis::test_util::CreateCopyResultCallback(&error));
86   test_util::RunBlockingPoolTask();
87   EXPECT_EQ(FILE_ERROR_OK, error);
88
89   // Check that the server has received an update.
90   EXPECT_LT(original_changestamp, fake_service()->largest_changestamp());
91
92   // Check that the file size is updated to that of the updated content.
93   google_apis::GDataErrorCode gdata_error = google_apis::GDATA_OTHER_ERROR;
94   scoped_ptr<google_apis::ResourceEntry> server_entry;
95   fake_service()->GetResourceEntry(
96       kResourceId,
97       google_apis::test_util::CreateCopyResultCallback(&gdata_error,
98                                                        &server_entry));
99   test_util::RunBlockingPoolTask();
100   EXPECT_EQ(google_apis::HTTP_SUCCESS, gdata_error);
101   EXPECT_EQ(static_cast<int64>(kTestFileContent.size()),
102             server_entry->file_size());
103
104   // Make sure that the cache is no longer dirty.
105   bool success = false;
106   FileCacheEntry cache_entry;
107   base::PostTaskAndReplyWithResult(
108       blocking_task_runner(),
109       FROM_HERE,
110       base::Bind(&internal::FileCache::GetCacheEntry,
111                  base::Unretained(cache()),
112                  local_id,
113                  &cache_entry),
114       google_apis::test_util::CreateCopyResultCallback(&success));
115   test_util::RunBlockingPoolTask();
116   ASSERT_TRUE(success);
117   EXPECT_FALSE(cache_entry.is_dirty());
118 }
119
120 TEST_F(UpdateOperationTest, UpdateFileByLocalId_NonexistentFile) {
121   FileError error = FILE_ERROR_OK;
122   operation_->UpdateFileByLocalId(
123       "nonexistent_local_id",
124       ClientContext(USER_INITIATED),
125       UpdateOperation::RUN_CONTENT_CHECK,
126       google_apis::test_util::CreateCopyResultCallback(&error));
127   test_util::RunBlockingPoolTask();
128   EXPECT_EQ(FILE_ERROR_NOT_FOUND, error);
129 }
130
131 TEST_F(UpdateOperationTest, UpdateFileByLocalId_Md5) {
132   const base::FilePath kFilePath(FILE_PATH_LITERAL("drive/root/File 1.txt"));
133   const std::string kResourceId("file:2_file_resource_id");
134   const std::string kMd5("3b4382ebefec6e743578c76bbd0575ce");
135
136   const base::FilePath kTestFile = temp_dir().Append(FILE_PATH_LITERAL("foo"));
137   const std::string kTestFileContent = "I'm being uploaded! Yay!";
138   google_apis::test_util::WriteStringToFile(kTestFile, kTestFileContent);
139
140   const std::string local_id = GetLocalId(kFilePath);
141   EXPECT_FALSE(local_id.empty());
142
143   // First store a file to cache.
144   FileError error = FILE_ERROR_FAILED;
145   base::PostTaskAndReplyWithResult(
146       blocking_task_runner(),
147       FROM_HERE,
148       base::Bind(&internal::FileCache::Store,
149                  base::Unretained(cache()),
150                  local_id, kMd5, kTestFile,
151                  internal::FileCache::FILE_OPERATION_COPY),
152       google_apis::test_util::CreateCopyResultCallback(&error));
153   test_util::RunBlockingPoolTask();
154   EXPECT_EQ(FILE_ERROR_OK, error);
155
156   // Add the dirty bit.
157   error = FILE_ERROR_FAILED;
158   base::PostTaskAndReplyWithResult(
159       blocking_task_runner(),
160       FROM_HERE,
161       base::Bind(&internal::FileCache::MarkDirty,
162                  base::Unretained(cache()),
163                  local_id),
164       google_apis::test_util::CreateCopyResultCallback(&error));
165   test_util::RunBlockingPoolTask();
166   EXPECT_EQ(FILE_ERROR_OK, error);
167
168   int64 original_changestamp = fake_service()->largest_changestamp();
169
170   // The callback will be called upon completion of UpdateFileByLocalId().
171   error = FILE_ERROR_FAILED;
172   operation_->UpdateFileByLocalId(
173       local_id,
174       ClientContext(USER_INITIATED),
175       UpdateOperation::RUN_CONTENT_CHECK,
176       google_apis::test_util::CreateCopyResultCallback(&error));
177   test_util::RunBlockingPoolTask();
178   EXPECT_EQ(FILE_ERROR_OK, error);
179
180   // Check that the server has received an update.
181   EXPECT_LT(original_changestamp, fake_service()->largest_changestamp());
182
183   // Check that the file size is updated to that of the updated content.
184   google_apis::GDataErrorCode gdata_error = google_apis::GDATA_OTHER_ERROR;
185   scoped_ptr<google_apis::ResourceEntry> server_entry;
186   fake_service()->GetResourceEntry(
187       kResourceId,
188       google_apis::test_util::CreateCopyResultCallback(&gdata_error,
189                                                        &server_entry));
190   test_util::RunBlockingPoolTask();
191   EXPECT_EQ(google_apis::HTTP_SUCCESS, gdata_error);
192   EXPECT_EQ(static_cast<int64>(kTestFileContent.size()),
193             server_entry->file_size());
194
195   // Make sure that the cache is no longer dirty.
196   bool success = false;
197   FileCacheEntry cache_entry;
198   base::PostTaskAndReplyWithResult(
199       blocking_task_runner(),
200       FROM_HERE,
201       base::Bind(&internal::FileCache::GetCacheEntry,
202                  base::Unretained(cache()),
203                  local_id,
204                  &cache_entry),
205       google_apis::test_util::CreateCopyResultCallback(&success));
206   test_util::RunBlockingPoolTask();
207   ASSERT_TRUE(success);
208   EXPECT_FALSE(cache_entry.is_dirty());
209
210   // Again mark the cache file dirty.
211   error = FILE_ERROR_FAILED;
212   base::PostTaskAndReplyWithResult(
213       blocking_task_runner(),
214       FROM_HERE,
215       base::Bind(&internal::FileCache::MarkDirty,
216                  base::Unretained(cache()),
217                  local_id),
218       google_apis::test_util::CreateCopyResultCallback(&error));
219   test_util::RunBlockingPoolTask();
220   EXPECT_EQ(FILE_ERROR_OK, error);
221
222   // And call UpdateFileByLocalId again.
223   // In this case, although the file is marked as dirty, but the content
224   // hasn't been changed. Thus, the actual uploading should be skipped.
225   original_changestamp = fake_service()->largest_changestamp();
226   error = FILE_ERROR_FAILED;
227   operation_->UpdateFileByLocalId(
228       local_id,
229       ClientContext(USER_INITIATED),
230       UpdateOperation::RUN_CONTENT_CHECK,
231       google_apis::test_util::CreateCopyResultCallback(&error));
232   test_util::RunBlockingPoolTask();
233   EXPECT_EQ(FILE_ERROR_OK, error);
234
235   EXPECT_EQ(original_changestamp, fake_service()->largest_changestamp());
236
237   // Make sure that the cache is no longer dirty.
238   success = false;
239   base::PostTaskAndReplyWithResult(
240       blocking_task_runner(),
241       FROM_HERE,
242       base::Bind(&internal::FileCache::GetCacheEntry,
243                  base::Unretained(cache()),
244                  local_id,
245                  &cache_entry),
246       google_apis::test_util::CreateCopyResultCallback(&success));
247   test_util::RunBlockingPoolTask();
248   ASSERT_TRUE(success);
249   EXPECT_FALSE(cache_entry.is_dirty());
250
251   // Once again mark the cache file dirty.
252   error = FILE_ERROR_FAILED;
253   base::PostTaskAndReplyWithResult(
254       blocking_task_runner(),
255       FROM_HERE,
256       base::Bind(&internal::FileCache::MarkDirty,
257                  base::Unretained(cache()),
258                  local_id),
259       google_apis::test_util::CreateCopyResultCallback(&error));
260   test_util::RunBlockingPoolTask();
261   EXPECT_EQ(FILE_ERROR_OK, error);
262
263   // And call UpdateFileByLocalId again.
264   // In this case, NO_CONTENT_CHECK is set, so the actual uploading should run
265   // no matter the content is changed or not.
266   original_changestamp = fake_service()->largest_changestamp();
267   error = FILE_ERROR_FAILED;
268   operation_->UpdateFileByLocalId(
269       local_id,
270       ClientContext(USER_INITIATED),
271       UpdateOperation::NO_CONTENT_CHECK,
272       google_apis::test_util::CreateCopyResultCallback(&error));
273   test_util::RunBlockingPoolTask();
274   EXPECT_EQ(FILE_ERROR_OK, error);
275
276   // Make sure that the server is receiving a change.
277   EXPECT_LE(original_changestamp, fake_service()->largest_changestamp());
278 }
279
280 }  // namespace file_system
281 }  // namespace drive