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

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

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

Powered by Google App Engine
This is Rietveld 408576698