Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(455)

Side by Side Diff: webkit/browser/fileapi/local_file_system_operation_unittest.cc

Issue 21370003: Rename fileapi::LocalFileSystemOperation to FileSystemOperationImpl. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 "webkit/browser/fileapi/local_file_system_operation.h"
6
7 #include "base/bind.h"
8 #include "base/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/strings/stringprintf.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "url/gurl.h"
17 #include "webkit/browser/fileapi/async_file_test_helper.h"
18 #include "webkit/browser/fileapi/file_system_context.h"
19 #include "webkit/browser/fileapi/file_system_file_util.h"
20 #include "webkit/browser/fileapi/file_system_operation_context.h"
21 #include "webkit/browser/fileapi/file_system_operation_runner.h"
22 #include "webkit/browser/fileapi/mock_file_change_observer.h"
23 #include "webkit/browser/fileapi/sandbox_file_system_backend.h"
24 #include "webkit/browser/fileapi/sandbox_file_system_test_helper.h"
25 #include "webkit/browser/quota/mock_quota_manager.h"
26 #include "webkit/browser/quota/quota_manager.h"
27 #include "webkit/common/blob/shareable_file_reference.h"
28 #include "webkit/common/fileapi/file_system_util.h"
29
30 using quota::QuotaManager;
31 using quota::QuotaManagerProxy;
32 using webkit_blob::ShareableFileReference;
33
34 namespace fileapi {
35
36 namespace {
37
38 const int kFileOperationStatusNotSet = 1;
39
40 void AssertFileErrorEq(const tracked_objects::Location& from_here,
41 base::PlatformFileError expected,
42 base::PlatformFileError actual) {
43 ASSERT_EQ(expected, actual) << from_here.ToString();
44 }
45
46 } // namespace (anonymous)
47
48 // Test class for LocalFileSystemOperation.
49 class LocalFileSystemOperationTest
50 : public testing::Test,
51 public base::SupportsWeakPtr<LocalFileSystemOperationTest> {
52 public:
53 LocalFileSystemOperationTest()
54 : status_(kFileOperationStatusNotSet) {}
55
56 protected:
57 virtual void SetUp() OVERRIDE {
58 EXPECT_TRUE(base_.CreateUniqueTempDir());
59 change_observers_ = MockFileChangeObserver::CreateList(&change_observer_);
60
61 base::FilePath base_dir = base_.path().AppendASCII("filesystem");
62 quota_manager_ =
63 new quota::MockQuotaManager(false /* is_incognito */,
64 base_dir,
65 base::MessageLoopProxy::current().get(),
66 base::MessageLoopProxy::current().get(),
67 NULL /* special storage policy */);
68 quota_manager_proxy_ = new quota::MockQuotaManagerProxy(
69 quota_manager(), base::MessageLoopProxy::current().get());
70 sandbox_file_system_.SetUp(base_dir, quota_manager_proxy_.get());
71 sandbox_file_system_.AddFileChangeObserver(&change_observer_);
72 }
73
74 virtual void TearDown() OVERRIDE {
75 // Let the client go away before dropping a ref of the quota manager proxy.
76 quota_manager_proxy()->SimulateQuotaManagerDestroyed();
77 quota_manager_ = NULL;
78 quota_manager_proxy_ = NULL;
79 sandbox_file_system_.TearDown();
80 }
81
82 FileSystemOperationRunner* operation_runner() {
83 return sandbox_file_system_.operation_runner();
84 }
85
86 int status() const { return status_; }
87 const base::PlatformFileInfo& info() const { return info_; }
88 const base::FilePath& path() const { return path_; }
89 const std::vector<DirectoryEntry>& entries() const {
90 return entries_;
91 }
92
93 const ShareableFileReference* shareable_file_ref() const {
94 return shareable_file_ref_.get();
95 }
96
97 quota::MockQuotaManager* quota_manager() {
98 return static_cast<quota::MockQuotaManager*>(quota_manager_.get());
99 }
100
101 quota::MockQuotaManagerProxy* quota_manager_proxy() {
102 return static_cast<quota::MockQuotaManagerProxy*>(
103 quota_manager_proxy_.get());
104 }
105
106 FileSystemFileUtil* file_util() {
107 return sandbox_file_system_.file_util();
108 }
109
110 MockFileChangeObserver* change_observer() {
111 return &change_observer_;
112 }
113
114 scoped_ptr<FileSystemOperationContext> NewContext() {
115 FileSystemOperationContext* context =
116 sandbox_file_system_.NewOperationContext();
117 // Grant enough quota for all test cases.
118 context->set_allowed_bytes_growth(1000000);
119 return make_scoped_ptr(context);
120 }
121
122 FileSystemURL URLForPath(const std::string& path) const {
123 return sandbox_file_system_.CreateURLFromUTF8(path);
124 }
125
126 base::FilePath PlatformPath(const std::string& path) {
127 return sandbox_file_system_.GetLocalPath(
128 base::FilePath::FromUTF8Unsafe(path));
129 }
130
131 bool FileExists(const std::string& path) {
132 return AsyncFileTestHelper::FileExists(
133 sandbox_file_system_.file_system_context(), URLForPath(path),
134 AsyncFileTestHelper::kDontCheckSize);
135 }
136
137 bool DirectoryExists(const std::string& path) {
138 return AsyncFileTestHelper::DirectoryExists(
139 sandbox_file_system_.file_system_context(), URLForPath(path));
140 }
141
142 FileSystemURL CreateFile(const std::string& path) {
143 FileSystemURL url = URLForPath(path);
144 bool created = false;
145 EXPECT_EQ(base::PLATFORM_FILE_OK,
146 file_util()->EnsureFileExists(NewContext().get(),
147 url, &created));
148 EXPECT_TRUE(created);
149 return url;
150 }
151
152 FileSystemURL CreateDirectory(const std::string& path) {
153 FileSystemURL url = URLForPath(path);
154 EXPECT_EQ(base::PLATFORM_FILE_OK,
155 file_util()->CreateDirectory(NewContext().get(), url,
156 false /* exclusive */, true));
157 return url;
158 }
159
160 int64 GetFileSize(const std::string& path) {
161 base::PlatformFileInfo info;
162 EXPECT_TRUE(file_util::GetFileInfo(PlatformPath(path), &info));
163 return info.size;
164 }
165
166 // Callbacks for recording test results.
167 FileSystemOperation::StatusCallback RecordStatusCallback() {
168 return base::Bind(&LocalFileSystemOperationTest::DidFinish, AsWeakPtr());
169 }
170
171 FileSystemOperation::ReadDirectoryCallback
172 RecordReadDirectoryCallback() {
173 return base::Bind(&LocalFileSystemOperationTest::DidReadDirectory,
174 AsWeakPtr());
175 }
176
177 FileSystemOperation::GetMetadataCallback RecordMetadataCallback() {
178 return base::Bind(&LocalFileSystemOperationTest::DidGetMetadata,
179 AsWeakPtr());
180 }
181
182 FileSystemOperation::SnapshotFileCallback RecordSnapshotFileCallback() {
183 return base::Bind(&LocalFileSystemOperationTest::DidCreateSnapshotFile,
184 AsWeakPtr());
185 }
186
187 void DidFinish(base::PlatformFileError status) {
188 status_ = status;
189 }
190
191 void DidReadDirectory(
192 base::PlatformFileError status,
193 const std::vector<DirectoryEntry>& entries,
194 bool /* has_more */) {
195 entries_ = entries;
196 status_ = status;
197 }
198
199 void DidGetMetadata(base::PlatformFileError status,
200 const base::PlatformFileInfo& info) {
201 info_ = info;
202 status_ = status;
203 }
204
205 void DidCreateSnapshotFile(
206 base::PlatformFileError status,
207 const base::PlatformFileInfo& info,
208 const base::FilePath& platform_path,
209 const scoped_refptr<ShareableFileReference>& shareable_file_ref) {
210 info_ = info;
211 path_ = platform_path;
212 status_ = status;
213 shareable_file_ref_ = shareable_file_ref;
214 }
215
216 int64 GetDataSizeOnDisk() {
217 return sandbox_file_system_.ComputeCurrentOriginUsage() -
218 sandbox_file_system_.ComputeCurrentDirectoryDatabaseUsage();
219 }
220
221 void GetUsageAndQuota(int64* usage, int64* quota) {
222 quota::QuotaStatusCode status =
223 AsyncFileTestHelper::GetUsageAndQuota(quota_manager_.get(),
224 sandbox_file_system_.origin(),
225 sandbox_file_system_.type(),
226 usage,
227 quota);
228 base::MessageLoop::current()->RunUntilIdle();
229 ASSERT_EQ(quota::kQuotaStatusOk, status);
230 }
231
232 int64 ComputePathCost(const FileSystemURL& url) {
233 int64 base_usage;
234 GetUsageAndQuota(&base_usage, NULL);
235
236 AsyncFileTestHelper::CreateFile(
237 sandbox_file_system_.file_system_context(), url);
238 operation_runner()->Remove(url, false /* recursive */,
239 base::Bind(&AssertFileErrorEq, FROM_HERE,
240 base::PLATFORM_FILE_OK));
241 base::MessageLoop::current()->RunUntilIdle();
242 change_observer()->ResetCount();
243
244 int64 total_usage;
245 GetUsageAndQuota(&total_usage, NULL);
246 return total_usage - base_usage;
247 }
248
249 void GrantQuotaForCurrentUsage() {
250 int64 usage;
251 GetUsageAndQuota(&usage, NULL);
252 quota_manager()->SetQuota(sandbox_file_system_.origin(),
253 sandbox_file_system_.storage_type(),
254 usage);
255 }
256
257 int64 GetUsage() {
258 int64 usage = 0;
259 GetUsageAndQuota(&usage, NULL);
260 return usage;
261 }
262
263 void AddQuota(int64 quota_delta) {
264 int64 quota;
265 GetUsageAndQuota(NULL, &quota);
266 quota_manager()->SetQuota(sandbox_file_system_.origin(),
267 sandbox_file_system_.storage_type(),
268 quota + quota_delta);
269 }
270
271 base::MessageLoop message_loop_;
272 scoped_refptr<QuotaManager> quota_manager_;
273 scoped_refptr<QuotaManagerProxy> quota_manager_proxy_;
274
275 // Common temp base for nondestructive uses.
276 base::ScopedTempDir base_;
277
278 SandboxFileSystemTestHelper sandbox_file_system_;
279
280 // For post-operation status.
281 int status_;
282 base::PlatformFileInfo info_;
283 base::FilePath path_;
284 std::vector<DirectoryEntry> entries_;
285 scoped_refptr<ShareableFileReference> shareable_file_ref_;
286
287 MockFileChangeObserver change_observer_;
288 ChangeObserverList change_observers_;
289
290 DISALLOW_COPY_AND_ASSIGN(LocalFileSystemOperationTest);
291 };
292
293 TEST_F(LocalFileSystemOperationTest, TestMoveFailureSrcDoesntExist) {
294 change_observer()->ResetCount();
295 operation_runner()->Move(URLForPath("a"), URLForPath("b"),
296 RecordStatusCallback());
297 base::MessageLoop::current()->RunUntilIdle();
298 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, status());
299 EXPECT_TRUE(change_observer()->HasNoChange());
300 }
301
302 TEST_F(LocalFileSystemOperationTest, TestMoveFailureContainsPath) {
303 FileSystemURL src_dir(CreateDirectory("src"));
304 FileSystemURL dest_dir(CreateDirectory("src/dest"));
305
306 operation_runner()->Move(src_dir, dest_dir, RecordStatusCallback());
307 base::MessageLoop::current()->RunUntilIdle();
308 EXPECT_EQ(base::PLATFORM_FILE_ERROR_INVALID_OPERATION, status());
309 EXPECT_TRUE(change_observer()->HasNoChange());
310 }
311
312 TEST_F(LocalFileSystemOperationTest, TestMoveFailureSrcDirExistsDestFile) {
313 // Src exists and is dir. Dest is a file.
314 FileSystemURL src_dir(CreateDirectory("src"));
315 FileSystemURL dest_dir(CreateDirectory("dest"));
316 FileSystemURL dest_file(CreateFile("dest/file"));
317
318 operation_runner()->Move(src_dir, dest_file, RecordStatusCallback());
319 base::MessageLoop::current()->RunUntilIdle();
320 EXPECT_EQ(base::PLATFORM_FILE_ERROR_INVALID_OPERATION, status());
321 EXPECT_TRUE(change_observer()->HasNoChange());
322 }
323
324 TEST_F(LocalFileSystemOperationTest,
325 TestMoveFailureSrcFileExistsDestNonEmptyDir) {
326 // Src exists and is a directory. Dest is a non-empty directory.
327 FileSystemURL src_dir(CreateDirectory("src"));
328 FileSystemURL dest_dir(CreateDirectory("dest"));
329 FileSystemURL dest_file(CreateFile("dest/file"));
330
331 operation_runner()->Move(src_dir, dest_dir, RecordStatusCallback());
332 base::MessageLoop::current()->RunUntilIdle();
333 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY, status());
334 EXPECT_TRUE(change_observer()->HasNoChange());
335 }
336
337 TEST_F(LocalFileSystemOperationTest, TestMoveFailureSrcFileExistsDestDir) {
338 // Src exists and is a file. Dest is a directory.
339 FileSystemURL src_dir(CreateDirectory("src"));
340 FileSystemURL src_file(CreateFile("src/file"));
341 FileSystemURL dest_dir(CreateDirectory("dest"));
342
343 operation_runner()->Move(src_file, dest_dir, RecordStatusCallback());
344 base::MessageLoop::current()->RunUntilIdle();
345 EXPECT_EQ(base::PLATFORM_FILE_ERROR_INVALID_OPERATION, status());
346 EXPECT_TRUE(change_observer()->HasNoChange());
347 }
348
349 TEST_F(LocalFileSystemOperationTest, TestMoveFailureDestParentDoesntExist) {
350 // Dest. parent path does not exist.
351 FileSystemURL src_dir(CreateDirectory("src"));
352 operation_runner()->Move(src_dir, URLForPath("nonexistent/deset"),
353 RecordStatusCallback());
354 base::MessageLoop::current()->RunUntilIdle();
355 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, status());
356 EXPECT_TRUE(change_observer()->HasNoChange());
357 }
358
359 TEST_F(LocalFileSystemOperationTest, TestMoveSuccessSrcFileAndOverwrite) {
360 FileSystemURL src_file(CreateFile("src"));
361 FileSystemURL dest_file(CreateFile("dest"));
362
363 operation_runner()->Move(src_file, dest_file, RecordStatusCallback());
364 base::MessageLoop::current()->RunUntilIdle();
365 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
366 EXPECT_TRUE(FileExists("dest"));
367
368 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
369 EXPECT_EQ(1, change_observer()->get_and_reset_remove_file_count());
370 EXPECT_TRUE(change_observer()->HasNoChange());
371
372 EXPECT_EQ(1, quota_manager_proxy()->notify_storage_accessed_count());
373 }
374
375 TEST_F(LocalFileSystemOperationTest, TestMoveSuccessSrcFileAndNew) {
376 FileSystemURL src_file(CreateFile("src"));
377
378 operation_runner()->Move(src_file, URLForPath("new"), RecordStatusCallback());
379 base::MessageLoop::current()->RunUntilIdle();
380 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
381 EXPECT_TRUE(FileExists("new"));
382
383 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_from_count());
384 EXPECT_EQ(1, change_observer()->get_and_reset_remove_file_count());
385 EXPECT_TRUE(change_observer()->HasNoChange());
386 }
387
388 TEST_F(LocalFileSystemOperationTest, TestMoveSuccessSrcDirAndOverwrite) {
389 FileSystemURL src_dir(CreateDirectory("src"));
390 FileSystemURL dest_dir(CreateDirectory("dest"));
391
392 operation_runner()->Move(src_dir, dest_dir, RecordStatusCallback());
393 base::MessageLoop::current()->RunUntilIdle();
394 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
395 EXPECT_FALSE(DirectoryExists("src"));
396
397 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
398 EXPECT_EQ(2, change_observer()->get_and_reset_remove_directory_count());
399 EXPECT_TRUE(change_observer()->HasNoChange());
400
401 // Make sure we've overwritten but not moved the source under the |dest_dir|.
402 EXPECT_TRUE(DirectoryExists("dest"));
403 EXPECT_FALSE(DirectoryExists("dest/src"));
404 }
405
406 TEST_F(LocalFileSystemOperationTest, TestMoveSuccessSrcDirAndNew) {
407 FileSystemURL src_dir(CreateDirectory("src"));
408 FileSystemURL dest_dir(CreateDirectory("dest"));
409
410 operation_runner()->Move(src_dir, URLForPath("dest/new"),
411 RecordStatusCallback());
412 base::MessageLoop::current()->RunUntilIdle();
413 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
414 EXPECT_FALSE(DirectoryExists("src"));
415 EXPECT_TRUE(DirectoryExists("dest/new"));
416
417 EXPECT_EQ(1, change_observer()->get_and_reset_remove_directory_count());
418 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
419 EXPECT_TRUE(change_observer()->HasNoChange());
420 }
421
422 TEST_F(LocalFileSystemOperationTest, TestMoveSuccessSrcDirRecursive) {
423 FileSystemURL src_dir(CreateDirectory("src"));
424 CreateDirectory("src/dir");
425 CreateFile("src/dir/sub");
426
427 FileSystemURL dest_dir(CreateDirectory("dest"));
428
429 operation_runner()->Move(src_dir, dest_dir, RecordStatusCallback());
430 base::MessageLoop::current()->RunUntilIdle();
431 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
432 EXPECT_TRUE(DirectoryExists("dest/dir"));
433 EXPECT_TRUE(FileExists("dest/dir/sub"));
434
435 EXPECT_EQ(3, change_observer()->get_and_reset_remove_directory_count());
436 EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count());
437 EXPECT_EQ(1, change_observer()->get_and_reset_remove_file_count());
438 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_from_count());
439 EXPECT_TRUE(change_observer()->HasNoChange());
440 }
441
442 TEST_F(LocalFileSystemOperationTest, TestCopyFailureSrcDoesntExist) {
443 operation_runner()->Copy(URLForPath("a"), URLForPath("b"),
444 RecordStatusCallback());
445 base::MessageLoop::current()->RunUntilIdle();
446 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, status());
447 EXPECT_TRUE(change_observer()->HasNoChange());
448 }
449
450 TEST_F(LocalFileSystemOperationTest, TestCopyFailureContainsPath) {
451 FileSystemURL src_dir(CreateDirectory("src"));
452 FileSystemURL dest_dir(CreateDirectory("src/dir"));
453
454 operation_runner()->Copy(src_dir, dest_dir, RecordStatusCallback());
455 base::MessageLoop::current()->RunUntilIdle();
456 EXPECT_EQ(base::PLATFORM_FILE_ERROR_INVALID_OPERATION, status());
457 EXPECT_TRUE(change_observer()->HasNoChange());
458 }
459
460 TEST_F(LocalFileSystemOperationTest, TestCopyFailureSrcDirExistsDestFile) {
461 // Src exists and is dir. Dest is a file.
462 FileSystemURL src_dir(CreateDirectory("src"));
463 FileSystemURL dest_dir(CreateDirectory("dest"));
464 FileSystemURL dest_file(CreateFile("dest/file"));
465
466 operation_runner()->Copy(src_dir, dest_file, RecordStatusCallback());
467 base::MessageLoop::current()->RunUntilIdle();
468 EXPECT_EQ(base::PLATFORM_FILE_ERROR_INVALID_OPERATION, status());
469 EXPECT_TRUE(change_observer()->HasNoChange());
470 }
471
472 TEST_F(LocalFileSystemOperationTest,
473 TestCopyFailureSrcFileExistsDestNonEmptyDir) {
474 // Src exists and is a directory. Dest is a non-empty directory.
475 FileSystemURL src_dir(CreateDirectory("src"));
476 FileSystemURL dest_dir(CreateDirectory("dest"));
477 FileSystemURL dest_file(CreateFile("dest/file"));
478
479 operation_runner()->Copy(src_dir, dest_dir, RecordStatusCallback());
480 base::MessageLoop::current()->RunUntilIdle();
481 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY, status());
482 EXPECT_TRUE(change_observer()->HasNoChange());
483 }
484
485 TEST_F(LocalFileSystemOperationTest, TestCopyFailureSrcFileExistsDestDir) {
486 // Src exists and is a file. Dest is a directory.
487 FileSystemURL src_file(CreateFile("src"));
488 FileSystemURL dest_dir(CreateDirectory("dest"));
489
490 operation_runner()->Copy(src_file, dest_dir, RecordStatusCallback());
491 base::MessageLoop::current()->RunUntilIdle();
492 EXPECT_EQ(base::PLATFORM_FILE_ERROR_INVALID_OPERATION, status());
493 EXPECT_TRUE(change_observer()->HasNoChange());
494 }
495
496 TEST_F(LocalFileSystemOperationTest, TestCopyFailureDestParentDoesntExist) {
497 // Dest. parent path does not exist.
498 FileSystemURL src_dir(CreateDirectory("src"));
499
500 operation_runner()->Copy(src_dir, URLForPath("nonexistent/dest"),
501 RecordStatusCallback());
502 base::MessageLoop::current()->RunUntilIdle();
503 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, status());
504 EXPECT_TRUE(change_observer()->HasNoChange());
505 }
506
507 TEST_F(LocalFileSystemOperationTest, TestCopyFailureByQuota) {
508 FileSystemURL src_dir(CreateDirectory("src"));
509 FileSystemURL src_file(CreateFile("src/file"));
510 FileSystemURL dest_dir(CreateDirectory("dest"));
511 operation_runner()->Truncate(src_file, 6, RecordStatusCallback());
512 base::MessageLoop::current()->RunUntilIdle();
513 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
514 EXPECT_EQ(6, GetFileSize("src/file"));
515
516 FileSystemURL dest_file(URLForPath("dest/file"));
517 int64 dest_path_cost = ComputePathCost(dest_file);
518 GrantQuotaForCurrentUsage();
519 AddQuota(6 + dest_path_cost - 1);
520
521 operation_runner()->Copy(src_file, dest_file, RecordStatusCallback());
522 base::MessageLoop::current()->RunUntilIdle();
523 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, status());
524 EXPECT_FALSE(FileExists("dest/file"));
525 }
526
527 TEST_F(LocalFileSystemOperationTest, TestCopySuccessSrcFileAndOverwrite) {
528 FileSystemURL src_file(CreateFile("src"));
529 FileSystemURL dest_file(CreateFile("dest"));
530
531 operation_runner()->Copy(src_file, dest_file, RecordStatusCallback());
532 base::MessageLoop::current()->RunUntilIdle();
533 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
534 EXPECT_TRUE(FileExists("dest"));
535 EXPECT_EQ(2, quota_manager_proxy()->notify_storage_accessed_count());
536
537 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
538 EXPECT_TRUE(change_observer()->HasNoChange());
539 }
540
541 TEST_F(LocalFileSystemOperationTest, TestCopySuccessSrcFileAndNew) {
542 FileSystemURL src_file(CreateFile("src"));
543
544 operation_runner()->Copy(src_file, URLForPath("new"),
545 RecordStatusCallback());
546 base::MessageLoop::current()->RunUntilIdle();
547 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
548 EXPECT_TRUE(FileExists("new"));
549 EXPECT_EQ(2, quota_manager_proxy()->notify_storage_accessed_count());
550
551 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_from_count());
552 EXPECT_TRUE(change_observer()->HasNoChange());
553 }
554
555 TEST_F(LocalFileSystemOperationTest, TestCopySuccessSrcDirAndOverwrite) {
556 FileSystemURL src_dir(CreateDirectory("src"));
557 FileSystemURL dest_dir(CreateDirectory("dest"));
558
559 operation_runner()->Copy(src_dir, dest_dir, RecordStatusCallback());
560 base::MessageLoop::current()->RunUntilIdle();
561 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
562
563 // Make sure we've overwritten but not copied the source under the |dest_dir|.
564 EXPECT_TRUE(DirectoryExists("dest"));
565 EXPECT_FALSE(DirectoryExists("dest/src"));
566 EXPECT_GE(quota_manager_proxy()->notify_storage_accessed_count(), 3);
567
568 EXPECT_EQ(1, change_observer()->get_and_reset_remove_directory_count());
569 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
570 EXPECT_TRUE(change_observer()->HasNoChange());
571 }
572
573 TEST_F(LocalFileSystemOperationTest, TestCopySuccessSrcDirAndNew) {
574 FileSystemURL src_dir(CreateDirectory("src"));
575 FileSystemURL dest_dir_new(URLForPath("dest"));
576
577 operation_runner()->Copy(src_dir, dest_dir_new, RecordStatusCallback());
578 base::MessageLoop::current()->RunUntilIdle();
579 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
580 EXPECT_TRUE(DirectoryExists("dest"));
581 EXPECT_GE(quota_manager_proxy()->notify_storage_accessed_count(), 2);
582
583 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
584 EXPECT_TRUE(change_observer()->HasNoChange());
585 }
586
587 TEST_F(LocalFileSystemOperationTest, TestCopySuccessSrcDirRecursive) {
588 FileSystemURL src_dir(CreateDirectory("src"));
589 CreateDirectory("src/dir");
590 CreateFile("src/dir/sub");
591
592 FileSystemURL dest_dir(CreateDirectory("dest"));
593
594 operation_runner()->Copy(src_dir, dest_dir, RecordStatusCallback());
595 base::MessageLoop::current()->RunUntilIdle();
596
597 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
598 EXPECT_TRUE(DirectoryExists("dest/dir"));
599 EXPECT_TRUE(FileExists("dest/dir/sub"));
600
601 // For recursive copy we may record multiple read access.
602 EXPECT_GE(quota_manager_proxy()->notify_storage_accessed_count(), 1);
603
604 EXPECT_EQ(2, change_observer()->get_and_reset_create_directory_count());
605 EXPECT_EQ(1, change_observer()->get_and_reset_remove_directory_count());
606 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_from_count());
607 EXPECT_TRUE(change_observer()->HasNoChange());
608 }
609
610 TEST_F(LocalFileSystemOperationTest, TestCopyInForeignFileSuccess) {
611 base::FilePath src_local_disk_file_path;
612 file_util::CreateTemporaryFile(&src_local_disk_file_path);
613 const char test_data[] = "foo";
614 int data_size = ARRAYSIZE_UNSAFE(test_data);
615 file_util::WriteFile(src_local_disk_file_path, test_data, data_size);
616
617 FileSystemURL dest_dir(CreateDirectory("dest"));
618
619 int64 before_usage;
620 GetUsageAndQuota(&before_usage, NULL);
621
622 // Check that the file copied and corresponding usage increased.
623 operation_runner()->CopyInForeignFile(src_local_disk_file_path,
624 URLForPath("dest/file"),
625 RecordStatusCallback());
626 base::MessageLoop::current()->RunUntilIdle();
627
628 EXPECT_EQ(1, change_observer()->create_file_count());
629 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
630 EXPECT_TRUE(FileExists("dest/file"));
631 int64 after_usage;
632 GetUsageAndQuota(&after_usage, NULL);
633 EXPECT_GT(after_usage, before_usage);
634
635 // Compare contents of src and copied file.
636 char buffer[100];
637 EXPECT_EQ(data_size, file_util::ReadFile(PlatformPath("dest/file"),
638 buffer, data_size));
639 for (int i = 0; i < data_size; ++i)
640 EXPECT_EQ(test_data[i], buffer[i]);
641 }
642
643 TEST_F(LocalFileSystemOperationTest, TestCopyInForeignFileFailureByQuota) {
644 base::FilePath src_local_disk_file_path;
645 file_util::CreateTemporaryFile(&src_local_disk_file_path);
646 const char test_data[] = "foo";
647 file_util::WriteFile(src_local_disk_file_path, test_data,
648 ARRAYSIZE_UNSAFE(test_data));
649
650 FileSystemURL dest_dir(CreateDirectory("dest"));
651
652 GrantQuotaForCurrentUsage();
653 operation_runner()->CopyInForeignFile(src_local_disk_file_path,
654 URLForPath("dest/file"),
655 RecordStatusCallback());
656 base::MessageLoop::current()->RunUntilIdle();
657
658 EXPECT_FALSE(FileExists("dest/file"));
659 EXPECT_EQ(0, change_observer()->create_file_count());
660 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, status());
661 }
662
663 TEST_F(LocalFileSystemOperationTest, TestCreateFileFailure) {
664 // Already existing file and exclusive true.
665 FileSystemURL file(CreateFile("file"));
666 operation_runner()->CreateFile(file, true, RecordStatusCallback());
667 base::MessageLoop::current()->RunUntilIdle();
668 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, status());
669 EXPECT_TRUE(change_observer()->HasNoChange());
670 }
671
672 TEST_F(LocalFileSystemOperationTest, TestCreateFileSuccessFileExists) {
673 // Already existing file and exclusive false.
674 FileSystemURL file(CreateFile("file"));
675 operation_runner()->CreateFile(file, false, RecordStatusCallback());
676 base::MessageLoop::current()->RunUntilIdle();
677 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
678 EXPECT_TRUE(FileExists("file"));
679
680 // The file was already there; did nothing.
681 EXPECT_TRUE(change_observer()->HasNoChange());
682 }
683
684 TEST_F(LocalFileSystemOperationTest, TestCreateFileSuccessExclusive) {
685 // File doesn't exist but exclusive is true.
686 operation_runner()->CreateFile(URLForPath("new"), true,
687 RecordStatusCallback());
688 base::MessageLoop::current()->RunUntilIdle();
689 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
690 EXPECT_TRUE(FileExists("new"));
691 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
692 }
693
694 TEST_F(LocalFileSystemOperationTest, TestCreateFileSuccessFileDoesntExist) {
695 // Non existing file.
696 operation_runner()->CreateFile(URLForPath("nonexistent"), false,
697 RecordStatusCallback());
698 base::MessageLoop::current()->RunUntilIdle();
699 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
700 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_count());
701 }
702
703 TEST_F(LocalFileSystemOperationTest,
704 TestCreateDirFailureDestParentDoesntExist) {
705 // Dest. parent path does not exist.
706 operation_runner()->CreateDirectory(
707 URLForPath("nonexistent/dir"), false, false,
708 RecordStatusCallback());
709 base::MessageLoop::current()->RunUntilIdle();
710 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, status());
711 EXPECT_TRUE(change_observer()->HasNoChange());
712 }
713
714 TEST_F(LocalFileSystemOperationTest, TestCreateDirFailureDirExists) {
715 // Exclusive and dir existing at path.
716 FileSystemURL dir(CreateDirectory("dir"));
717 operation_runner()->CreateDirectory(dir, true, false,
718 RecordStatusCallback());
719 base::MessageLoop::current()->RunUntilIdle();
720 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, status());
721 EXPECT_TRUE(change_observer()->HasNoChange());
722 }
723
724 TEST_F(LocalFileSystemOperationTest, TestCreateDirFailureFileExists) {
725 // Exclusive true and file existing at path.
726 FileSystemURL file(CreateFile("file"));
727 operation_runner()->CreateDirectory(file, true, false,
728 RecordStatusCallback());
729 base::MessageLoop::current()->RunUntilIdle();
730 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, status());
731 EXPECT_TRUE(change_observer()->HasNoChange());
732 }
733
734 TEST_F(LocalFileSystemOperationTest, TestCreateDirSuccess) {
735 // Dir exists and exclusive is false.
736 FileSystemURL dir(CreateDirectory("dir"));
737 operation_runner()->CreateDirectory(dir, false, false,
738 RecordStatusCallback());
739 base::MessageLoop::current()->RunUntilIdle();
740 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
741 EXPECT_TRUE(change_observer()->HasNoChange());
742
743 // Dir doesn't exist.
744 operation_runner()->CreateDirectory(URLForPath("new"), false, false,
745 RecordStatusCallback());
746 base::MessageLoop::current()->RunUntilIdle();
747 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
748 EXPECT_TRUE(DirectoryExists("new"));
749 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
750 }
751
752 TEST_F(LocalFileSystemOperationTest, TestCreateDirSuccessExclusive) {
753 // Dir doesn't exist.
754 operation_runner()->CreateDirectory(URLForPath("new"), true, false,
755 RecordStatusCallback());
756 base::MessageLoop::current()->RunUntilIdle();
757 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
758 EXPECT_TRUE(DirectoryExists("new"));
759 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count());
760 EXPECT_TRUE(change_observer()->HasNoChange());
761 }
762
763 TEST_F(LocalFileSystemOperationTest, TestExistsAndMetadataFailure) {
764 operation_runner()->GetMetadata(URLForPath("nonexistent"),
765 RecordMetadataCallback());
766 base::MessageLoop::current()->RunUntilIdle();
767 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, status());
768
769 operation_runner()->FileExists(URLForPath("nonexistent"),
770 RecordStatusCallback());
771 base::MessageLoop::current()->RunUntilIdle();
772 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, status());
773
774 operation_runner()->DirectoryExists(URLForPath("nonexistent"),
775 RecordStatusCallback());
776 base::MessageLoop::current()->RunUntilIdle();
777 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, status());
778 EXPECT_TRUE(change_observer()->HasNoChange());
779 }
780
781 TEST_F(LocalFileSystemOperationTest, TestExistsAndMetadataSuccess) {
782 FileSystemURL dir(CreateDirectory("dir"));
783 FileSystemURL file(CreateFile("dir/file"));
784 int read_access = 0;
785
786 operation_runner()->DirectoryExists(dir, RecordStatusCallback());
787 base::MessageLoop::current()->RunUntilIdle();
788 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
789 ++read_access;
790
791 operation_runner()->GetMetadata(dir, RecordMetadataCallback());
792 base::MessageLoop::current()->RunUntilIdle();
793 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
794 EXPECT_TRUE(info().is_directory);
795 ++read_access;
796
797 operation_runner()->FileExists(file, RecordStatusCallback());
798 base::MessageLoop::current()->RunUntilIdle();
799 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
800 ++read_access;
801
802 operation_runner()->GetMetadata(file, RecordMetadataCallback());
803 base::MessageLoop::current()->RunUntilIdle();
804 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
805 EXPECT_FALSE(info().is_directory);
806 ++read_access;
807
808 EXPECT_EQ(read_access,
809 quota_manager_proxy()->notify_storage_accessed_count());
810 EXPECT_TRUE(change_observer()->HasNoChange());
811 }
812
813 TEST_F(LocalFileSystemOperationTest, TestTypeMismatchErrors) {
814 FileSystemURL dir(CreateDirectory("dir"));
815 operation_runner()->FileExists(dir, RecordStatusCallback());
816 base::MessageLoop::current()->RunUntilIdle();
817 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_FILE, status());
818
819 FileSystemURL file(CreateFile("file"));
820 operation_runner()->DirectoryExists(file, RecordStatusCallback());
821 base::MessageLoop::current()->RunUntilIdle();
822 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY, status());
823 }
824
825 TEST_F(LocalFileSystemOperationTest, TestReadDirFailure) {
826 // Path doesn't exist
827 operation_runner()->ReadDirectory(URLForPath("nonexistent"),
828 RecordReadDirectoryCallback());
829 base::MessageLoop::current()->RunUntilIdle();
830 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, status());
831
832 // File exists.
833 FileSystemURL file(CreateFile("file"));
834 operation_runner()->ReadDirectory(file, RecordReadDirectoryCallback());
835 base::MessageLoop::current()->RunUntilIdle();
836 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY, status());
837 EXPECT_TRUE(change_observer()->HasNoChange());
838 }
839
840 TEST_F(LocalFileSystemOperationTest, TestReadDirSuccess) {
841 // parent_dir
842 // | |
843 // child_dir child_file
844 // Verify reading parent_dir.
845 FileSystemURL parent_dir(CreateDirectory("dir"));
846 FileSystemURL child_dir(CreateDirectory("dir/child_dir"));
847 FileSystemURL child_file(CreateFile("dir/child_file"));
848
849 operation_runner()->ReadDirectory(parent_dir, RecordReadDirectoryCallback());
850 base::MessageLoop::current()->RunUntilIdle();
851 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
852 EXPECT_EQ(2u, entries().size());
853
854 for (size_t i = 0; i < entries().size(); ++i) {
855 if (entries()[i].is_directory)
856 EXPECT_EQ(FILE_PATH_LITERAL("child_dir"), entries()[i].name);
857 else
858 EXPECT_EQ(FILE_PATH_LITERAL("child_file"), entries()[i].name);
859 }
860 EXPECT_EQ(1, quota_manager_proxy()->notify_storage_accessed_count());
861 EXPECT_TRUE(change_observer()->HasNoChange());
862 }
863
864 TEST_F(LocalFileSystemOperationTest, TestRemoveFailure) {
865 // Path doesn't exist.
866 operation_runner()->Remove(URLForPath("nonexistent"), false /* recursive */,
867 RecordStatusCallback());
868 base::MessageLoop::current()->RunUntilIdle();
869 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, status());
870
871 // It's an error to try to remove a non-empty directory if recursive flag
872 // is false.
873 // parent_dir
874 // | |
875 // child_dir child_file
876 // Verify deleting parent_dir.
877 FileSystemURL parent_dir(CreateDirectory("dir"));
878 FileSystemURL child_dir(CreateDirectory("dir/child_dir"));
879 FileSystemURL child_file(CreateFile("dir/child_file"));
880
881 operation_runner()->Remove(parent_dir, false /* recursive */,
882 RecordStatusCallback());
883 base::MessageLoop::current()->RunUntilIdle();
884 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY, status());
885 EXPECT_TRUE(change_observer()->HasNoChange());
886 }
887
888 TEST_F(LocalFileSystemOperationTest, TestRemoveSuccess) {
889 FileSystemURL empty_dir(CreateDirectory("empty_dir"));
890 EXPECT_TRUE(DirectoryExists("empty_dir"));
891 operation_runner()->Remove(empty_dir, false /* recursive */,
892 RecordStatusCallback());
893 base::MessageLoop::current()->RunUntilIdle();
894 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
895 EXPECT_FALSE(DirectoryExists("empty_dir"));
896
897 EXPECT_EQ(1, change_observer()->get_and_reset_remove_directory_count());
898 EXPECT_TRUE(change_observer()->HasNoChange());
899 }
900
901 TEST_F(LocalFileSystemOperationTest, TestRemoveSuccessRecursive) {
902 // Removing a non-empty directory with recursive flag == true should be ok.
903 // parent_dir
904 // | |
905 // child_dir child_files
906 // |
907 // child_files
908 //
909 // Verify deleting parent_dir.
910 FileSystemURL parent_dir(CreateDirectory("dir"));
911 for (int i = 0; i < 8; ++i)
912 CreateFile(base::StringPrintf("dir/file-%d", i));
913 FileSystemURL child_dir(CreateDirectory("dir/child_dir"));
914 for (int i = 0; i < 8; ++i)
915 CreateFile(base::StringPrintf("dir/child_dir/file-%d", i));
916
917 operation_runner()->Remove(parent_dir, true /* recursive */,
918 RecordStatusCallback());
919 base::MessageLoop::current()->RunUntilIdle();
920 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
921 EXPECT_FALSE(DirectoryExists("parent_dir"));
922
923 EXPECT_EQ(2, change_observer()->get_and_reset_remove_directory_count());
924 EXPECT_EQ(16, change_observer()->get_and_reset_remove_file_count());
925 EXPECT_TRUE(change_observer()->HasNoChange());
926 }
927
928 TEST_F(LocalFileSystemOperationTest, TestTruncate) {
929 FileSystemURL file(CreateFile("file"));
930 base::FilePath platform_path = PlatformPath("file");
931
932 char test_data[] = "test data";
933 int data_size = static_cast<int>(sizeof(test_data));
934 EXPECT_EQ(data_size,
935 file_util::WriteFile(platform_path, test_data, data_size));
936
937 // Check that its length is the size of the data written.
938 operation_runner()->GetMetadata(file, RecordMetadataCallback());
939 base::MessageLoop::current()->RunUntilIdle();
940 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
941 EXPECT_FALSE(info().is_directory);
942 EXPECT_EQ(data_size, info().size);
943
944 // Extend the file by truncating it.
945 int length = 17;
946 operation_runner()->Truncate(file, length, RecordStatusCallback());
947 base::MessageLoop::current()->RunUntilIdle();
948 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
949
950 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
951 EXPECT_TRUE(change_observer()->HasNoChange());
952
953 // Check that its length is now 17 and that it's all zeroes after the test
954 // data.
955 EXPECT_EQ(length, GetFileSize("file"));
956 char data[100];
957 EXPECT_EQ(length, file_util::ReadFile(platform_path, data, length));
958 for (int i = 0; i < length; ++i) {
959 if (i < static_cast<int>(sizeof(test_data)))
960 EXPECT_EQ(test_data[i], data[i]);
961 else
962 EXPECT_EQ(0, data[i]);
963 }
964
965 // Shorten the file by truncating it.
966 length = 3;
967 operation_runner()->Truncate(file, length, RecordStatusCallback());
968 base::MessageLoop::current()->RunUntilIdle();
969 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
970
971 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
972 EXPECT_TRUE(change_observer()->HasNoChange());
973
974 // Check that its length is now 3 and that it contains only bits of test data.
975 EXPECT_EQ(length, GetFileSize("file"));
976 EXPECT_EQ(length, file_util::ReadFile(platform_path, data, length));
977 for (int i = 0; i < length; ++i)
978 EXPECT_EQ(test_data[i], data[i]);
979
980 // Truncate is not a 'read' access. (Here expected access count is 1
981 // since we made 1 read access for GetMetadata.)
982 EXPECT_EQ(1, quota_manager_proxy()->notify_storage_accessed_count());
983 }
984
985 TEST_F(LocalFileSystemOperationTest, TestTruncateFailureByQuota) {
986 FileSystemURL dir(CreateDirectory("dir"));
987 FileSystemURL file(CreateFile("dir/file"));
988
989 GrantQuotaForCurrentUsage();
990 AddQuota(10);
991
992 operation_runner()->Truncate(file, 10, RecordStatusCallback());
993 base::MessageLoop::current()->RunUntilIdle();
994 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
995 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count());
996 EXPECT_TRUE(change_observer()->HasNoChange());
997
998 EXPECT_EQ(10, GetFileSize("dir/file"));
999
1000 operation_runner()->Truncate(file, 11, RecordStatusCallback());
1001 base::MessageLoop::current()->RunUntilIdle();
1002 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, status());
1003 EXPECT_TRUE(change_observer()->HasNoChange());
1004
1005 EXPECT_EQ(10, GetFileSize("dir/file"));
1006 }
1007
1008 TEST_F(LocalFileSystemOperationTest, TestTouchFile) {
1009 FileSystemURL file(CreateFile("file"));
1010 base::FilePath platform_path = PlatformPath("file");
1011
1012 base::PlatformFileInfo info;
1013 EXPECT_TRUE(file_util::GetFileInfo(platform_path, &info));
1014 EXPECT_FALSE(info.is_directory);
1015 EXPECT_EQ(0, info.size);
1016 const base::Time last_modified = info.last_modified;
1017 const base::Time last_accessed = info.last_accessed;
1018
1019 const base::Time new_modified_time = base::Time::UnixEpoch();
1020 const base::Time new_accessed_time = new_modified_time +
1021 base::TimeDelta::FromHours(77);
1022 ASSERT_NE(last_modified, new_modified_time);
1023 ASSERT_NE(last_accessed, new_accessed_time);
1024
1025 operation_runner()->TouchFile(file, new_accessed_time, new_modified_time,
1026 RecordStatusCallback());
1027 base::MessageLoop::current()->RunUntilIdle();
1028 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
1029 EXPECT_TRUE(change_observer()->HasNoChange());
1030
1031 EXPECT_TRUE(file_util::GetFileInfo(platform_path, &info));
1032 // We compare as time_t here to lower our resolution, to avoid false
1033 // negatives caused by conversion to the local filesystem's native
1034 // representation and back.
1035 EXPECT_EQ(new_modified_time.ToTimeT(), info.last_modified.ToTimeT());
1036 EXPECT_EQ(new_accessed_time.ToTimeT(), info.last_accessed.ToTimeT());
1037 }
1038
1039 TEST_F(LocalFileSystemOperationTest, TestCreateSnapshotFile) {
1040 FileSystemURL dir(CreateDirectory("dir"));
1041
1042 // Create a file for the testing.
1043 operation_runner()->DirectoryExists(dir, RecordStatusCallback());
1044 FileSystemURL file(CreateFile("dir/file"));
1045 operation_runner()->FileExists(file, RecordStatusCallback());
1046 base::MessageLoop::current()->RunUntilIdle();
1047 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
1048
1049 // See if we can get a 'snapshot' file info for the file.
1050 // Since LocalFileSystemOperation assumes the file exists in the local
1051 // directory it should just returns the same metadata and platform_path
1052 // as the file itself.
1053 operation_runner()->CreateSnapshotFile(file, RecordSnapshotFileCallback());
1054 base::MessageLoop::current()->RunUntilIdle();
1055 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
1056 EXPECT_FALSE(info().is_directory);
1057 EXPECT_EQ(PlatformPath("dir/file"), path());
1058 EXPECT_TRUE(change_observer()->HasNoChange());
1059
1060 // The FileSystemOpration implementation does not create a
1061 // shareable file reference.
1062 EXPECT_EQ(NULL, shareable_file_ref());
1063 }
1064
1065 TEST_F(LocalFileSystemOperationTest,
1066 TestMoveSuccessSrcDirRecursiveWithQuota) {
1067 FileSystemURL src(CreateDirectory("src"));
1068 int src_path_cost = GetUsage();
1069
1070 FileSystemURL dest(CreateDirectory("dest"));
1071 FileSystemURL child_file1(CreateFile("src/file1"));
1072 FileSystemURL child_file2(CreateFile("src/file2"));
1073 FileSystemURL child_dir(CreateDirectory("src/dir"));
1074 FileSystemURL grandchild_file1(CreateFile("src/dir/file1"));
1075 FileSystemURL grandchild_file2(CreateFile("src/dir/file2"));
1076
1077 int total_path_cost = GetUsage();
1078 EXPECT_EQ(0, GetDataSizeOnDisk());
1079
1080 operation_runner()->Truncate(
1081 child_file1, 5000,
1082 base::Bind(&AssertFileErrorEq, FROM_HERE, base::PLATFORM_FILE_OK));
1083 operation_runner()->Truncate(
1084 child_file2, 400,
1085 base::Bind(&AssertFileErrorEq, FROM_HERE, base::PLATFORM_FILE_OK));
1086 operation_runner()->Truncate(
1087 grandchild_file1, 30,
1088 base::Bind(&AssertFileErrorEq, FROM_HERE, base::PLATFORM_FILE_OK));
1089 operation_runner()->Truncate(
1090 grandchild_file2, 2,
1091 base::Bind(&AssertFileErrorEq, FROM_HERE, base::PLATFORM_FILE_OK));
1092 base::MessageLoop::current()->RunUntilIdle();
1093
1094 const int64 all_file_size = 5000 + 400 + 30 + 2;
1095 EXPECT_EQ(all_file_size, GetDataSizeOnDisk());
1096 EXPECT_EQ(all_file_size + total_path_cost, GetUsage());
1097
1098 operation_runner()->Move(
1099 src, dest,
1100 base::Bind(&AssertFileErrorEq, FROM_HERE, base::PLATFORM_FILE_OK));
1101 base::MessageLoop::current()->RunUntilIdle();
1102
1103 EXPECT_FALSE(DirectoryExists("src/dir"));
1104 EXPECT_FALSE(FileExists("src/dir/file2"));
1105 EXPECT_TRUE(DirectoryExists("dest/dir"));
1106 EXPECT_TRUE(FileExists("dest/dir/file2"));
1107
1108 EXPECT_EQ(all_file_size, GetDataSizeOnDisk());
1109 EXPECT_EQ(all_file_size + total_path_cost - src_path_cost,
1110 GetUsage());
1111 }
1112
1113 TEST_F(LocalFileSystemOperationTest,
1114 TestCopySuccessSrcDirRecursiveWithQuota) {
1115 FileSystemURL src(CreateDirectory("src"));
1116 FileSystemURL dest1(CreateDirectory("dest1"));
1117 FileSystemURL dest2(CreateDirectory("dest2"));
1118
1119 int64 usage = GetUsage();
1120 FileSystemURL child_file1(CreateFile("src/file1"));
1121 FileSystemURL child_file2(CreateFile("src/file2"));
1122 FileSystemURL child_dir(CreateDirectory("src/dir"));
1123 int64 child_path_cost = GetUsage() - usage;
1124 usage += child_path_cost;
1125
1126 FileSystemURL grandchild_file1(CreateFile("src/dir/file1"));
1127 FileSystemURL grandchild_file2(CreateFile("src/dir/file2"));
1128 int64 total_path_cost = GetUsage();
1129 int64 grandchild_path_cost = total_path_cost - usage;
1130
1131 EXPECT_EQ(0, GetDataSizeOnDisk());
1132
1133 operation_runner()->Truncate(
1134 child_file1, 8000,
1135 base::Bind(&AssertFileErrorEq, FROM_HERE, base::PLATFORM_FILE_OK));
1136 operation_runner()->Truncate(
1137 child_file2, 700,
1138 base::Bind(&AssertFileErrorEq, FROM_HERE, base::PLATFORM_FILE_OK));
1139 operation_runner()->Truncate(
1140 grandchild_file1, 60,
1141 base::Bind(&AssertFileErrorEq, FROM_HERE, base::PLATFORM_FILE_OK));
1142 operation_runner()->Truncate(
1143 grandchild_file2, 5,
1144 base::Bind(&AssertFileErrorEq, FROM_HERE, base::PLATFORM_FILE_OK));
1145 base::MessageLoop::current()->RunUntilIdle();
1146
1147 const int64 child_file_size = 8000 + 700;
1148 const int64 grandchild_file_size = 60 + 5;
1149 const int64 all_file_size = child_file_size + grandchild_file_size;
1150 int64 expected_usage = all_file_size + total_path_cost;
1151
1152 usage = GetUsage();
1153 EXPECT_EQ(all_file_size, GetDataSizeOnDisk());
1154 EXPECT_EQ(expected_usage, usage);
1155
1156 // Copy src to dest1.
1157 operation_runner()->Copy(
1158 src, dest1,
1159 base::Bind(&AssertFileErrorEq, FROM_HERE, base::PLATFORM_FILE_OK));
1160 base::MessageLoop::current()->RunUntilIdle();
1161
1162 expected_usage += all_file_size + child_path_cost + grandchild_path_cost;
1163 EXPECT_TRUE(DirectoryExists("src/dir"));
1164 EXPECT_TRUE(FileExists("src/dir/file2"));
1165 EXPECT_TRUE(DirectoryExists("dest1/dir"));
1166 EXPECT_TRUE(FileExists("dest1/dir/file2"));
1167
1168 EXPECT_EQ(2 * all_file_size, GetDataSizeOnDisk());
1169 EXPECT_EQ(expected_usage, GetUsage());
1170
1171 // Copy src/dir to dest2.
1172 operation_runner()->Copy(
1173 child_dir, dest2,
1174 base::Bind(&AssertFileErrorEq, FROM_HERE, base::PLATFORM_FILE_OK));
1175 base::MessageLoop::current()->RunUntilIdle();
1176
1177 expected_usage += grandchild_file_size + grandchild_path_cost;
1178 usage = GetUsage();
1179 EXPECT_EQ(2 * child_file_size + 3 * grandchild_file_size,
1180 GetDataSizeOnDisk());
1181 EXPECT_EQ(expected_usage, usage);
1182 }
1183
1184 } // namespace fileapi
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698