OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "webkit/fileapi/local_file_system_operation.h" | 5 #include "webkit/fileapi/local_file_system_operation.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
13 #include "base/string_number_conversions.h" | 13 #include "base/string_number_conversions.h" |
14 #include "googleurl/src/gurl.h" | 14 #include "googleurl/src/gurl.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 #include "webkit/blob/shareable_file_reference.h" | 16 #include "webkit/blob/shareable_file_reference.h" |
| 17 #include "webkit/fileapi/async_file_test_helper.h" |
17 #include "webkit/fileapi/file_system_context.h" | 18 #include "webkit/fileapi/file_system_context.h" |
18 #include "webkit/fileapi/file_system_file_util.h" | 19 #include "webkit/fileapi/file_system_file_util.h" |
19 #include "webkit/fileapi/file_system_mount_point_provider.h" | 20 #include "webkit/fileapi/file_system_mount_point_provider.h" |
20 #include "webkit/fileapi/file_system_quota_util.h" | 21 #include "webkit/fileapi/file_system_quota_util.h" |
21 #include "webkit/fileapi/file_system_util.h" | 22 #include "webkit/fileapi/file_system_util.h" |
22 #include "webkit/fileapi/file_util_helper.h" | |
23 #include "webkit/fileapi/local_file_system_test_helper.h" | 23 #include "webkit/fileapi/local_file_system_test_helper.h" |
24 #include "webkit/fileapi/mock_file_change_observer.h" | 24 #include "webkit/fileapi/mock_file_change_observer.h" |
25 #include "webkit/quota/mock_quota_manager.h" | 25 #include "webkit/quota/mock_quota_manager.h" |
26 #include "webkit/quota/quota_manager.h" | 26 #include "webkit/quota/quota_manager.h" |
27 | 27 |
28 using quota::QuotaManager; | 28 using quota::QuotaManager; |
29 using quota::QuotaManagerProxy; | 29 using quota::QuotaManagerProxy; |
30 using webkit_blob::ShareableFileReference; | 30 using webkit_blob::ShareableFileReference; |
31 | 31 |
32 namespace fileapi { | 32 namespace fileapi { |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 FileSystemURL URLForPath(const base::FilePath& path) const { | 104 FileSystemURL URLForPath(const base::FilePath& path) const { |
105 return test_helper_.CreateURL(path); | 105 return test_helper_.CreateURL(path); |
106 } | 106 } |
107 | 107 |
108 base::FilePath PlatformPath(const base::FilePath& virtual_path) { | 108 base::FilePath PlatformPath(const base::FilePath& virtual_path) { |
109 return test_helper_.GetLocalPath(virtual_path); | 109 return test_helper_.GetLocalPath(virtual_path); |
110 } | 110 } |
111 | 111 |
112 bool FileExists(const base::FilePath& virtual_path) { | 112 bool FileExists(const base::FilePath& virtual_path) { |
113 FileSystemURL url = test_helper_.CreateURL(virtual_path); | 113 FileSystemURL url = test_helper_.CreateURL(virtual_path); |
114 base::PlatformFileInfo file_info; | 114 return AsyncFileTestHelper::FileExists( |
115 base::FilePath platform_path; | 115 test_helper_.file_system_context(), url, |
116 scoped_ptr<FileSystemOperationContext> context(NewContext()); | 116 AsyncFileTestHelper::kDontCheckSize); |
117 base::PlatformFileError error = file_util()->GetFileInfo( | |
118 context.get(), url, &file_info, &platform_path); | |
119 return error == base::PLATFORM_FILE_OK && !file_info.is_directory; | |
120 } | 117 } |
121 | 118 |
122 bool DirectoryExists(const base::FilePath& virtual_path) { | 119 bool DirectoryExists(const base::FilePath& virtual_path) { |
123 FileSystemURL url = test_helper_.CreateURL(virtual_path); | 120 FileSystemURL url = test_helper_.CreateURL(virtual_path); |
124 scoped_ptr<FileSystemOperationContext> context(NewContext()); | 121 return AsyncFileTestHelper::DirectoryExists( |
125 return FileUtilHelper::DirectoryExists(context.get(), file_util(), url); | 122 test_helper_.file_system_context(), url); |
126 } | 123 } |
127 | 124 |
128 base::FilePath CreateUniqueFileInDir(const base::FilePath& virtual_dir_path) { | 125 base::FilePath CreateUniqueFileInDir(const base::FilePath& virtual_dir_path) { |
129 base::FilePath file_name = base::FilePath::FromUTF8Unsafe( | 126 base::FilePath file_name = base::FilePath::FromUTF8Unsafe( |
130 "tmpfile-" + base::IntToString(next_unique_path_suffix_++)); | 127 "tmpfile-" + base::IntToString(next_unique_path_suffix_++)); |
131 FileSystemURL url = test_helper_.CreateURL( | 128 FileSystemURL url = test_helper_.CreateURL( |
132 virtual_dir_path.Append(file_name)); | 129 virtual_dir_path.Append(file_name)); |
133 | 130 |
134 scoped_ptr<FileSystemOperationContext> context(NewContext()); | 131 scoped_ptr<FileSystemOperationContext> context(NewContext()); |
135 bool created; | 132 bool created; |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 base::PlatformFileError status, | 199 base::PlatformFileError status, |
203 const base::PlatformFileInfo& info, | 200 const base::PlatformFileInfo& info, |
204 const base::FilePath& platform_path, | 201 const base::FilePath& platform_path, |
205 const scoped_refptr<ShareableFileReference>& shareable_file_ref) { | 202 const scoped_refptr<ShareableFileReference>& shareable_file_ref) { |
206 info_ = info; | 203 info_ = info; |
207 path_ = platform_path; | 204 path_ = platform_path; |
208 status_ = status; | 205 status_ = status; |
209 shareable_file_ref_ = shareable_file_ref; | 206 shareable_file_ref_ = shareable_file_ref; |
210 } | 207 } |
211 | 208 |
212 static void DidGetUsageAndQuota(quota::QuotaStatusCode* status_out, | |
213 int64* usage_out, | |
214 int64* quota_out, | |
215 quota::QuotaStatusCode status, | |
216 int64 usage, | |
217 int64 quota) { | |
218 if (status_out) | |
219 *status_out = status; | |
220 | |
221 if (usage_out) | |
222 *usage_out = usage; | |
223 | |
224 if (quota_out) | |
225 *quota_out = quota; | |
226 } | |
227 | |
228 void GetUsageAndQuota(int64* usage, int64* quota) { | 209 void GetUsageAndQuota(int64* usage, int64* quota) { |
229 quota::QuotaStatusCode status = quota::kQuotaStatusUnknown; | 210 quota::QuotaStatusCode status = |
230 quota_manager_->GetUsageAndQuota( | 211 AsyncFileTestHelper::GetUsageAndQuota( |
231 test_helper_.origin(), | 212 quota_manager_, test_helper_.origin(), test_helper_.type(), |
232 test_helper_.storage_type(), | 213 usage, quota); |
233 base::Bind(&LocalFileSystemOperationTest::DidGetUsageAndQuota, | |
234 &status, usage, quota)); | |
235 MessageLoop::current()->RunUntilIdle(); | 214 MessageLoop::current()->RunUntilIdle(); |
236 ASSERT_EQ(quota::kQuotaStatusOk, status); | 215 ASSERT_EQ(quota::kQuotaStatusOk, status); |
237 } | 216 } |
238 | 217 |
239 void GenerateUniquePathInDir(const base::FilePath& dir, | 218 void GenerateUniquePathInDir(const base::FilePath& dir, |
240 base::FilePath* file_path, | 219 base::FilePath* file_path, |
241 int64* path_cost) { | 220 int64* path_cost) { |
242 int64 base_usage; | 221 int64 base_usage; |
243 GetUsageAndQuota(&base_usage, NULL); | 222 GetUsageAndQuota(&base_usage, NULL); |
244 *file_path = CreateUniqueFileInDir(dir); | 223 *file_path = CreateUniqueFileInDir(dir); |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
400 operation()->Move(URLForPath(src_file_path), URLForPath(dest_file_path), | 379 operation()->Move(URLForPath(src_file_path), URLForPath(dest_file_path), |
401 RecordStatusCallback()); | 380 RecordStatusCallback()); |
402 MessageLoop::current()->RunUntilIdle(); | 381 MessageLoop::current()->RunUntilIdle(); |
403 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); | 382 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); |
404 EXPECT_TRUE(FileExists(dest_file_path)); | 383 EXPECT_TRUE(FileExists(dest_file_path)); |
405 | 384 |
406 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count()); | 385 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count()); |
407 EXPECT_EQ(1, change_observer()->get_and_reset_remove_file_count()); | 386 EXPECT_EQ(1, change_observer()->get_and_reset_remove_file_count()); |
408 EXPECT_TRUE(change_observer()->HasNoChange()); | 387 EXPECT_TRUE(change_observer()->HasNoChange()); |
409 | 388 |
410 // Move is considered 'write' access (for both side), and won't be counted | 389 EXPECT_EQ(1, quota_manager_proxy()->notify_storage_accessed_count()); |
411 // as read access. | |
412 EXPECT_EQ(0, quota_manager_proxy()->notify_storage_accessed_count()); | |
413 } | 390 } |
414 | 391 |
415 TEST_F(LocalFileSystemOperationTest, TestMoveSuccessSrcFileAndNew) { | 392 TEST_F(LocalFileSystemOperationTest, TestMoveSuccessSrcFileAndNew) { |
416 base::FilePath src_dir_path(CreateUniqueDir()); | 393 base::FilePath src_dir_path(CreateUniqueDir()); |
417 base::FilePath src_file_path(CreateUniqueFileInDir(src_dir_path)); | 394 base::FilePath src_file_path(CreateUniqueFileInDir(src_dir_path)); |
418 base::FilePath dest_dir_path(CreateUniqueDir()); | 395 base::FilePath dest_dir_path(CreateUniqueDir()); |
419 base::FilePath dest_file_path(dest_dir_path.Append(FILE_PATH_LITERAL("NewFile"
))); | 396 base::FilePath dest_file_path(dest_dir_path.Append(FILE_PATH_LITERAL("NewFile"
))); |
420 | 397 |
421 operation()->Move(URLForPath(src_file_path), URLForPath(dest_file_path), | 398 operation()->Move(URLForPath(src_file_path), URLForPath(dest_file_path), |
422 RecordStatusCallback()); | 399 RecordStatusCallback()); |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
602 base::FilePath src_dir_path(CreateUniqueDir()); | 579 base::FilePath src_dir_path(CreateUniqueDir()); |
603 base::FilePath src_file_path(CreateUniqueFileInDir(src_dir_path)); | 580 base::FilePath src_file_path(CreateUniqueFileInDir(src_dir_path)); |
604 base::FilePath dest_dir_path(CreateUniqueDir()); | 581 base::FilePath dest_dir_path(CreateUniqueDir()); |
605 base::FilePath dest_file_path(CreateUniqueFileInDir(dest_dir_path)); | 582 base::FilePath dest_file_path(CreateUniqueFileInDir(dest_dir_path)); |
606 | 583 |
607 operation()->Copy(URLForPath(src_file_path), URLForPath(dest_file_path), | 584 operation()->Copy(URLForPath(src_file_path), URLForPath(dest_file_path), |
608 RecordStatusCallback()); | 585 RecordStatusCallback()); |
609 MessageLoop::current()->RunUntilIdle(); | 586 MessageLoop::current()->RunUntilIdle(); |
610 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); | 587 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); |
611 EXPECT_TRUE(FileExists(dest_file_path)); | 588 EXPECT_TRUE(FileExists(dest_file_path)); |
612 EXPECT_EQ(1, quota_manager_proxy()->notify_storage_accessed_count()); | 589 EXPECT_EQ(2, quota_manager_proxy()->notify_storage_accessed_count()); |
613 | 590 |
614 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count()); | 591 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count()); |
615 EXPECT_TRUE(change_observer()->HasNoChange()); | 592 EXPECT_TRUE(change_observer()->HasNoChange()); |
616 } | 593 } |
617 | 594 |
618 TEST_F(LocalFileSystemOperationTest, TestCopySuccessSrcFileAndNew) { | 595 TEST_F(LocalFileSystemOperationTest, TestCopySuccessSrcFileAndNew) { |
619 base::FilePath src_dir_path(CreateUniqueDir()); | 596 base::FilePath src_dir_path(CreateUniqueDir()); |
620 base::FilePath src_file_path(CreateUniqueFileInDir(src_dir_path)); | 597 base::FilePath src_file_path(CreateUniqueFileInDir(src_dir_path)); |
621 base::FilePath dest_dir_path(CreateUniqueDir()); | 598 base::FilePath dest_dir_path(CreateUniqueDir()); |
622 base::FilePath dest_file_path(dest_dir_path.Append(FILE_PATH_LITERAL("NewFile"
))); | 599 base::FilePath dest_file_path(dest_dir_path.Append( |
| 600 FILE_PATH_LITERAL("NewFile"))); |
623 | 601 |
624 operation()->Copy(URLForPath(src_file_path), URLForPath(dest_file_path), | 602 operation()->Copy(URLForPath(src_file_path), URLForPath(dest_file_path), |
625 RecordStatusCallback()); | 603 RecordStatusCallback()); |
626 MessageLoop::current()->RunUntilIdle(); | 604 MessageLoop::current()->RunUntilIdle(); |
627 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); | 605 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); |
628 EXPECT_TRUE(FileExists(dest_file_path)); | 606 EXPECT_TRUE(FileExists(dest_file_path)); |
629 EXPECT_EQ(1, quota_manager_proxy()->notify_storage_accessed_count()); | 607 EXPECT_EQ(2, quota_manager_proxy()->notify_storage_accessed_count()); |
630 | 608 |
631 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_from_count()); | 609 EXPECT_EQ(1, change_observer()->get_and_reset_create_file_from_count()); |
632 EXPECT_TRUE(change_observer()->HasNoChange()); | 610 EXPECT_TRUE(change_observer()->HasNoChange()); |
633 } | 611 } |
634 | 612 |
635 TEST_F(LocalFileSystemOperationTest, TestCopySuccessSrcDirAndOverwrite) { | 613 TEST_F(LocalFileSystemOperationTest, TestCopySuccessSrcDirAndOverwrite) { |
636 base::FilePath src_dir_path(CreateUniqueDir()); | 614 base::FilePath src_dir_path(CreateUniqueDir()); |
637 base::FilePath dest_dir_path(CreateUniqueDir()); | 615 base::FilePath dest_dir_path(CreateUniqueDir()); |
638 | 616 |
639 operation()->Copy(URLForPath(src_dir_path), URLForPath(dest_dir_path), | 617 operation()->Copy(URLForPath(src_dir_path), URLForPath(dest_dir_path), |
640 RecordStatusCallback()); | 618 RecordStatusCallback()); |
641 MessageLoop::current()->RunUntilIdle(); | 619 MessageLoop::current()->RunUntilIdle(); |
642 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); | 620 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); |
643 | 621 |
644 // Make sure we've overwritten but not copied the source under the |dest_dir|. | 622 // Make sure we've overwritten but not copied the source under the |dest_dir|. |
645 EXPECT_TRUE(DirectoryExists(dest_dir_path)); | 623 EXPECT_TRUE(DirectoryExists(dest_dir_path)); |
646 EXPECT_FALSE(DirectoryExists( | 624 EXPECT_FALSE(DirectoryExists( |
647 dest_dir_path.Append(VirtualPath::BaseName(src_dir_path)))); | 625 dest_dir_path.Append(VirtualPath::BaseName(src_dir_path)))); |
648 EXPECT_EQ(1, quota_manager_proxy()->notify_storage_accessed_count()); | 626 EXPECT_EQ(3, quota_manager_proxy()->notify_storage_accessed_count()); |
649 | 627 |
650 EXPECT_EQ(1, change_observer()->get_and_reset_remove_directory_count()); | 628 EXPECT_EQ(1, change_observer()->get_and_reset_remove_directory_count()); |
651 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count()); | 629 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count()); |
652 EXPECT_TRUE(change_observer()->HasNoChange()); | 630 EXPECT_TRUE(change_observer()->HasNoChange()); |
653 } | 631 } |
654 | 632 |
655 TEST_F(LocalFileSystemOperationTest, TestCopySuccessSrcDirAndNew) { | 633 TEST_F(LocalFileSystemOperationTest, TestCopySuccessSrcDirAndNew) { |
656 base::FilePath src_dir_path(CreateUniqueDir()); | 634 base::FilePath src_dir_path(CreateUniqueDir()); |
657 base::FilePath dest_parent_dir_path(CreateUniqueDir()); | 635 base::FilePath dest_parent_dir_path(CreateUniqueDir()); |
658 base::FilePath dest_child_dir_path(dest_parent_dir_path. | 636 base::FilePath dest_child_dir_path(dest_parent_dir_path. |
659 Append(FILE_PATH_LITERAL("NewDirectory"))); | 637 Append(FILE_PATH_LITERAL("NewDirectory"))); |
660 | 638 |
661 operation()->Copy(URLForPath(src_dir_path), URLForPath(dest_child_dir_path), | 639 operation()->Copy(URLForPath(src_dir_path), URLForPath(dest_child_dir_path), |
662 RecordStatusCallback()); | 640 RecordStatusCallback()); |
663 MessageLoop::current()->RunUntilIdle(); | 641 MessageLoop::current()->RunUntilIdle(); |
664 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); | 642 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); |
665 EXPECT_TRUE(DirectoryExists(dest_child_dir_path)); | 643 EXPECT_TRUE(DirectoryExists(dest_child_dir_path)); |
666 EXPECT_EQ(1, quota_manager_proxy()->notify_storage_accessed_count()); | 644 EXPECT_EQ(2, quota_manager_proxy()->notify_storage_accessed_count()); |
667 | 645 |
668 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count()); | 646 EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count()); |
669 EXPECT_TRUE(change_observer()->HasNoChange()); | 647 EXPECT_TRUE(change_observer()->HasNoChange()); |
670 } | 648 } |
671 | 649 |
672 TEST_F(LocalFileSystemOperationTest, TestCopySuccessSrcDirRecursive) { | 650 TEST_F(LocalFileSystemOperationTest, TestCopySuccessSrcDirRecursive) { |
673 base::FilePath src_dir_path(CreateUniqueDir()); | 651 base::FilePath src_dir_path(CreateUniqueDir()); |
674 base::FilePath child_dir_path(CreateUniqueDirInDir(src_dir_path)); | 652 base::FilePath child_dir_path(CreateUniqueDirInDir(src_dir_path)); |
675 base::FilePath grandchild_file_path( | 653 base::FilePath grandchild_file_path( |
676 CreateUniqueFileInDir(child_dir_path)); | 654 CreateUniqueFileInDir(child_dir_path)); |
(...skipping 528 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1205 EXPECT_FALSE(info().is_directory); | 1183 EXPECT_FALSE(info().is_directory); |
1206 EXPECT_EQ(PlatformPath(file_path), path()); | 1184 EXPECT_EQ(PlatformPath(file_path), path()); |
1207 EXPECT_TRUE(change_observer()->HasNoChange()); | 1185 EXPECT_TRUE(change_observer()->HasNoChange()); |
1208 | 1186 |
1209 // The FileSystemOpration implementation does not create a | 1187 // The FileSystemOpration implementation does not create a |
1210 // shareable file reference. | 1188 // shareable file reference. |
1211 EXPECT_EQ(NULL, shareable_file_ref()); | 1189 EXPECT_EQ(NULL, shareable_file_ref()); |
1212 } | 1190 } |
1213 | 1191 |
1214 } // namespace fileapi | 1192 } // namespace fileapi |
OLD | NEW |