| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/file_system_operation.h" | 5 #include "webkit/fileapi/file_system_operation.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" |
| 7 #include "base/logging.h" | 8 #include "base/logging.h" |
| 8 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/memory/scoped_temp_dir.h" | 10 #include "base/memory/scoped_temp_dir.h" |
| 10 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "webkit/fileapi/file_system_callback_dispatcher.h" | 13 #include "webkit/fileapi/file_system_callback_dispatcher.h" |
| 14 #include "webkit/fileapi/file_system_context.h" |
| 13 #include "webkit/fileapi/file_system_file_util.h" | 15 #include "webkit/fileapi/file_system_file_util.h" |
| 16 #include "webkit/fileapi/file_system_mount_point_provider.h" |
| 14 #include "webkit/fileapi/file_system_operation.h" | 17 #include "webkit/fileapi/file_system_operation.h" |
| 18 #include "webkit/fileapi/file_system_path_manager.h" |
| 19 #include "webkit/fileapi/file_system_util.h" |
| 15 | 20 |
| 16 namespace fileapi { | 21 namespace fileapi { |
| 17 | 22 |
| 18 const int kFileOperationStatusNotSet = 1; | 23 const int kFileOperationStatusNotSet = 1; |
| 19 const int kFileOperationSucceeded = 0; | 24 const int kFileOperationSucceeded = 0; |
| 20 | 25 |
| 21 static bool FileExists(FilePath path) { | 26 static bool FileExists(FilePath path) { |
| 22 return file_util::PathExists(path) && !file_util::DirectoryExists(path); | 27 return file_util::PathExists(path) && !file_util::DirectoryExists(path); |
| 23 } | 28 } |
| 24 | 29 |
| 25 class MockDispatcher; | 30 class MockDispatcher; |
| 26 | 31 |
| 27 // Test class for FileSystemOperation. Note that this just tests low-level | 32 // Test class for FileSystemOperation. Note that this just tests low-level |
| 28 // operations but doesn't test OpenFileSystem or any additional checks | 33 // operations but doesn't test OpenFileSystem or any additional checks |
| 29 // that require FileSystemContext (e.g. sandboxed paths, unlimited_storage | 34 // that require FileSystemContext (e.g. sandboxed paths, unlimited_storage |
| 30 // quota handling etc). | 35 // quota handling etc). |
| 31 // See SimpleFileSystem for more complete test environment for sandboxed | 36 // See SimpleFileSystem for more complete test environment for sandboxed |
| 32 // FileSystem. | 37 // FileSystem. |
| 33 class FileSystemOperationTest : public testing::Test { | 38 class FileSystemOperationTest : public testing::Test { |
| 34 public: | 39 public: |
| 35 FileSystemOperationTest() | 40 FileSystemOperationTest() |
| 36 : status_(kFileOperationStatusNotSet) { | 41 : status_(kFileOperationStatusNotSet) { |
| 37 EXPECT_TRUE(base_.CreateUniqueTempDir()); | 42 EXPECT_TRUE(base_.CreateUniqueTempDir()); |
| 38 } | 43 } |
| 39 | 44 |
| 40 FileSystemOperation* operation(); | 45 FileSystemOperation* operation(); |
| 41 | 46 |
| 47 void set_local_path(const FilePath& path) { local_path_ = path; } |
| 48 const FilePath& local_path() const { return local_path_; } |
| 42 void set_status(int status) { status_ = status; } | 49 void set_status(int status) { status_ = status; } |
| 43 int status() const { return status_; } | 50 int status() const { return status_; } |
| 44 void set_info(const base::PlatformFileInfo& info) { info_ = info; } | 51 void set_info(const base::PlatformFileInfo& info) { info_ = info; } |
| 45 const base::PlatformFileInfo& info() const { return info_; } | 52 const base::PlatformFileInfo& info() const { return info_; } |
| 46 void set_path(const FilePath& path) { path_ = path; } | 53 void set_path(const FilePath& path) { path_ = path; } |
| 47 const FilePath& path() const { return path_; } | 54 const FilePath& path() const { return path_; } |
| 48 void set_entries(const std::vector<base::FileUtilProxy::Entry>& entries) { | 55 void set_entries(const std::vector<base::FileUtilProxy::Entry>& entries) { |
| 49 entries_ = entries; | 56 entries_ = entries; |
| 50 } | 57 } |
| 51 const std::vector<base::FileUtilProxy::Entry>& entries() const { | 58 const std::vector<base::FileUtilProxy::Entry>& entries() const { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 63 | 70 |
| 64 GURL URLForPath(const FilePath& path) const { | 71 GURL URLForPath(const FilePath& path) const { |
| 65 // Only the path will actually get used. | 72 // Only the path will actually get used. |
| 66 return GURL("file://").Resolve(path.value()); | 73 return GURL("file://").Resolve(path.value()); |
| 67 } | 74 } |
| 68 | 75 |
| 69 // For post-operation status. | 76 // For post-operation status. |
| 70 int status_; | 77 int status_; |
| 71 base::PlatformFileInfo info_; | 78 base::PlatformFileInfo info_; |
| 72 FilePath path_; | 79 FilePath path_; |
| 80 FilePath local_path_; |
| 73 std::vector<base::FileUtilProxy::Entry> entries_; | 81 std::vector<base::FileUtilProxy::Entry> entries_; |
| 74 | 82 |
| 75 DISALLOW_COPY_AND_ASSIGN(FileSystemOperationTest); | 83 DISALLOW_COPY_AND_ASSIGN(FileSystemOperationTest); |
| 76 }; | 84 }; |
| 77 | 85 |
| 78 class MockDispatcher : public FileSystemCallbackDispatcher { | 86 class MockDispatcher : public FileSystemCallbackDispatcher { |
| 79 public: | 87 public: |
| 80 MockDispatcher(FileSystemOperationTest* test) : test_(test) { } | 88 MockDispatcher(FileSystemOperationTest* test) : test_(test) { } |
| 81 | 89 |
| 82 virtual void DidFail(base::PlatformFileError status) { | 90 virtual void DidFail(base::PlatformFileError status) { |
| 83 test_->set_status(status); | 91 test_->set_status(status); |
| 84 } | 92 } |
| 85 | 93 |
| 86 virtual void DidSucceed() { | 94 virtual void DidSucceed() { |
| 87 test_->set_status(kFileOperationSucceeded); | 95 test_->set_status(kFileOperationSucceeded); |
| 88 } | 96 } |
| 89 | 97 |
| 98 virtual void DidGetLocalPath(const FilePath& local_path) { |
| 99 test_->set_local_path(local_path); |
| 100 test_->set_status(kFileOperationSucceeded); |
| 101 } |
| 102 |
| 90 virtual void DidReadMetadata( | 103 virtual void DidReadMetadata( |
| 91 const base::PlatformFileInfo& info, | 104 const base::PlatformFileInfo& info, |
| 92 const FilePath& platform_path) { | 105 const FilePath& platform_path) { |
| 93 test_->set_info(info); | 106 test_->set_info(info); |
| 94 test_->set_path(platform_path); | 107 test_->set_path(platform_path); |
| 95 test_->set_status(kFileOperationSucceeded); | 108 test_->set_status(kFileOperationSucceeded); |
| 96 } | 109 } |
| 97 | 110 |
| 98 virtual void DidReadDirectory( | 111 virtual void DidReadDirectory( |
| 99 const std::vector<base::FileUtilProxy::Entry>& entries, | 112 const std::vector<base::FileUtilProxy::Entry>& entries, |
| (...skipping 19 matching lines...) Expand all Loading... |
| 119 base::MessageLoopProxy::CreateForCurrentThread(), | 132 base::MessageLoopProxy::CreateForCurrentThread(), |
| 120 NULL, | 133 NULL, |
| 121 FileSystemFileUtil::GetInstance()); | 134 FileSystemFileUtil::GetInstance()); |
| 122 operation->file_system_operation_context()->set_src_type( | 135 operation->file_system_operation_context()->set_src_type( |
| 123 kFileSystemTypeTemporary); | 136 kFileSystemTypeTemporary); |
| 124 operation->file_system_operation_context()->set_dest_type( | 137 operation->file_system_operation_context()->set_dest_type( |
| 125 kFileSystemTypeTemporary); | 138 kFileSystemTypeTemporary); |
| 126 GURL origin_url("fake://fake.foo/"); | 139 GURL origin_url("fake://fake.foo/"); |
| 127 operation->file_system_operation_context()->set_src_origin_url(origin_url); | 140 operation->file_system_operation_context()->set_src_origin_url(origin_url); |
| 128 operation->file_system_operation_context()->set_dest_origin_url(origin_url); | 141 operation->file_system_operation_context()->set_dest_origin_url(origin_url); |
| 142 |
| 129 return operation; | 143 return operation; |
| 130 } | 144 } |
| 131 | 145 |
| 132 TEST_F(FileSystemOperationTest, TestMoveFailureSrcDoesntExist) { | 146 TEST_F(FileSystemOperationTest, TestMoveFailureSrcDoesntExist) { |
| 133 GURL src(URLForRelativePath("a")); | 147 GURL src(URLForRelativePath("a")); |
| 134 GURL dest(URLForRelativePath("b")); | 148 GURL dest(URLForRelativePath("b")); |
| 135 operation()->Move(src, dest); | 149 operation()->Move(src, dest); |
| 136 MessageLoop::current()->RunAllPending(); | 150 MessageLoop::current()->RunAllPending(); |
| 137 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, status()); | 151 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, status()); |
| 138 } | 152 } |
| (...skipping 464 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 603 MessageLoop::current()->RunAllPending(); | 617 MessageLoop::current()->RunAllPending(); |
| 604 EXPECT_EQ(kFileOperationSucceeded, status()); | 618 EXPECT_EQ(kFileOperationSucceeded, status()); |
| 605 | 619 |
| 606 operation()->GetMetadata(URLForPath(file)); | 620 operation()->GetMetadata(URLForPath(file)); |
| 607 MessageLoop::current()->RunAllPending(); | 621 MessageLoop::current()->RunAllPending(); |
| 608 EXPECT_EQ(kFileOperationSucceeded, status()); | 622 EXPECT_EQ(kFileOperationSucceeded, status()); |
| 609 EXPECT_FALSE(info().is_directory); | 623 EXPECT_FALSE(info().is_directory); |
| 610 EXPECT_EQ(file, path()); | 624 EXPECT_EQ(file, path()); |
| 611 } | 625 } |
| 612 | 626 |
| 627 TEST_F(FileSystemOperationTest, TestGetLocalFilePathSuccess) { |
| 628 ScopedTempDir dir; |
| 629 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
| 630 operation()->GetLocalPath(URLForPath(dir.path())); |
| 631 MessageLoop::current()->RunAllPending(); |
| 632 EXPECT_EQ(kFileOperationSucceeded, status()); |
| 633 EXPECT_EQ(local_path().value(), dir.path().value()); |
| 634 |
| 635 FilePath file; |
| 636 file_util::CreateTemporaryFileInDir(dir.path(), &file); |
| 637 operation()->GetLocalPath(URLForPath(file)); |
| 638 MessageLoop::current()->RunAllPending(); |
| 639 EXPECT_EQ(kFileOperationSucceeded, status()); |
| 640 EXPECT_EQ(local_path().value(), file.value()); |
| 641 } |
| 642 |
| 613 TEST_F(FileSystemOperationTest, TestTypeMismatchErrors) { | 643 TEST_F(FileSystemOperationTest, TestTypeMismatchErrors) { |
| 614 ScopedTempDir dir; | 644 ScopedTempDir dir; |
| 615 ASSERT_TRUE(dir.CreateUniqueTempDir()); | 645 ASSERT_TRUE(dir.CreateUniqueTempDir()); |
| 616 operation()->FileExists(URLForPath(dir.path())); | 646 operation()->FileExists(URLForPath(dir.path())); |
| 617 MessageLoop::current()->RunAllPending(); | 647 MessageLoop::current()->RunAllPending(); |
| 618 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_FILE, status()); | 648 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_FILE, status()); |
| 619 | 649 |
| 620 FilePath file; | 650 FilePath file; |
| 621 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(dir.path(), &file)); | 651 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(dir.path(), &file)); |
| 622 operation()->DirectoryExists(URLForPath(file)); | 652 operation()->DirectoryExists(URLForPath(file)); |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 779 | 809 |
| 780 // Check that its length is now 3 and that it contains only bits of test data. | 810 // Check that its length is now 3 and that it contains only bits of test data. |
| 781 EXPECT_TRUE(file_util::GetFileInfo(file, &info)); | 811 EXPECT_TRUE(file_util::GetFileInfo(file, &info)); |
| 782 EXPECT_EQ(length, info.size); | 812 EXPECT_EQ(length, info.size); |
| 783 EXPECT_EQ(length, file_util::ReadFile(file, data, length)); | 813 EXPECT_EQ(length, file_util::ReadFile(file, data, length)); |
| 784 for (int i = 0; i < length; ++i) | 814 for (int i = 0; i < length; ++i) |
| 785 EXPECT_EQ(test_data[i], data[i]); | 815 EXPECT_EQ(test_data[i], data[i]); |
| 786 } | 816 } |
| 787 | 817 |
| 788 } // namespace fileapi | 818 } // namespace fileapi |
| OLD | NEW |