diff options
author | kinuko@chromium.org <kinuko@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-02-07 10:49:08 +0000 |
---|---|---|
committer | kinuko@chromium.org <kinuko@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-02-07 10:49:08 +0000 |
commit | 57856df7f84ac730050c5a13906130081d0b54a2 (patch) | |
tree | 78c02c46ec985ffa00d51f394786bd0ce1683fcb /webkit/fileapi | |
parent | 69fd3626e4fa0664b257c6ce5705bc9352e6d09b (diff) | |
download | chromium_src-57856df7f84ac730050c5a13906130081d0b54a2.zip chromium_src-57856df7f84ac730050c5a13906130081d0b54a2.tar.gz chromium_src-57856df7f84ac730050c5a13906130081d0b54a2.tar.bz2 |
Introduce AsyncFileTestHelper for testing with async file/quota operations
Also gradually deprecate FileUtilHelper since its code is no longer
used in the actual code path (used only in unittests).
BUG=146215
TEST=content_unittests:LocalFileSystem*
TBR=jam@chromium.org
Review URL: https://codereview.chromium.org/12223006
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@181278 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit/fileapi')
-rw-r--r-- | webkit/fileapi/async_file_test_helper.cc | 235 | ||||
-rw-r--r-- | webkit/fileapi/async_file_test_helper.h | 90 | ||||
-rw-r--r-- | webkit/fileapi/local_file_system_cross_operation_unittest.cc | 167 | ||||
-rw-r--r-- | webkit/fileapi/local_file_system_operation_unittest.cc | 56 | ||||
-rw-r--r-- | webkit/fileapi/local_file_system_quota_unittest.cc | 34 | ||||
-rw-r--r-- | webkit/fileapi/local_file_system_test_helper.cc | 15 | ||||
-rw-r--r-- | webkit/fileapi/local_file_system_test_helper.h | 10 | ||||
-rw-r--r-- | webkit/fileapi/local_file_util_unittest.cc | 26 | ||||
-rw-r--r-- | webkit/fileapi/obfuscated_file_util_unittest.cc | 66 |
9 files changed, 412 insertions, 287 deletions
diff --git a/webkit/fileapi/async_file_test_helper.cc b/webkit/fileapi/async_file_test_helper.cc new file mode 100644 index 0000000..178efdc --- /dev/null +++ b/webkit/fileapi/async_file_test_helper.cc @@ -0,0 +1,235 @@ +// Copyright (c) 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/bind.h" +#include "base/run_loop.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "webkit/fileapi/async_file_test_helper.h" +#include "webkit/fileapi/file_system_context.h" +#include "webkit/fileapi/file_system_mount_point_provider.h" +#include "webkit/fileapi/file_system_url.h" +#include "webkit/fileapi/file_system_util.h" +#include "webkit/quota/quota_manager.h" + +namespace fileapi { + +namespace { + +typedef FileSystemOperation::FileEntryList FileEntryList; + +void AssignAndQuit(base::RunLoop* run_loop, + base::PlatformFileError* result_out, + base::PlatformFileError result) { + *result_out = result; + run_loop->Quit(); +} + +base::Callback<void(base::PlatformFileError)> +AssignAndQuitCallback(base::RunLoop* run_loop, + base::PlatformFileError* result) { + return base::Bind(&AssignAndQuit, run_loop, base::Unretained(result)); +} + +void GetMetadataCallback(base::RunLoop* run_loop, + base::PlatformFileError* result_out, + base::PlatformFileInfo* file_info_out, + base::FilePath* platform_path_out, + base::PlatformFileError result, + const base::PlatformFileInfo& file_info, + const base::FilePath& platform_path) { + *result_out = result; + if (file_info_out) + *file_info_out = file_info; + if (platform_path_out) + *platform_path_out = platform_path; + run_loop->Quit(); +} + +void ReadDirectoryCallback(base::RunLoop* run_loop, + base::PlatformFileError* result_out, + FileEntryList* entries_out, + base::PlatformFileError result, + const FileEntryList& entries, + bool has_more) { + *result_out = result; + *entries_out = entries; + if (result != base::PLATFORM_FILE_OK || !has_more) + run_loop->Quit(); +} + +void DidGetUsageAndQuota(quota::QuotaStatusCode* status_out, + int64* usage_out, + int64* quota_out, + quota::QuotaStatusCode status, + int64 usage, + int64 quota) { + if (status_out) + *status_out = status; + if (usage_out) + *usage_out = usage; + if (quota_out) + *quota_out = quota; +} + +} // namespace + +const int64 AsyncFileTestHelper::kDontCheckSize = -1; + +base::PlatformFileError AsyncFileTestHelper::Copy( + FileSystemContext* context, + const FileSystemURL& src, + const FileSystemURL& dest) { + DCHECK(context); + FileSystemOperation* operation = + context->CreateFileSystemOperation(dest, NULL); + EXPECT_TRUE(operation != NULL); + base::PlatformFileError result = base::PLATFORM_FILE_ERROR_FAILED; + base::RunLoop run_loop; + operation->Copy(src, dest, AssignAndQuitCallback(&run_loop, &result)); + run_loop.Run(); + return result; +} + +base::PlatformFileError AsyncFileTestHelper::Move( + FileSystemContext* context, + const FileSystemURL& src, + const FileSystemURL& dest) { + FileSystemOperation* operation = + context->CreateFileSystemOperation(dest, NULL); + EXPECT_TRUE(operation != NULL); + base::PlatformFileError result = base::PLATFORM_FILE_ERROR_FAILED; + base::RunLoop run_loop; + operation->Move(src, dest, AssignAndQuitCallback(&run_loop, &result)); + run_loop.Run(); + return result; +} + +base::PlatformFileError AsyncFileTestHelper::Remove( + FileSystemContext* context, + const FileSystemURL& url, + bool recursive) { + FileSystemOperation* operation = + context->CreateFileSystemOperation(url, NULL); + EXPECT_TRUE(operation != NULL); + base::PlatformFileError result = base::PLATFORM_FILE_ERROR_FAILED; + base::RunLoop run_loop; + operation->Remove(url, recursive, AssignAndQuitCallback(&run_loop, &result)); + run_loop.Run(); + return result; +} + +base::PlatformFileError AsyncFileTestHelper::ReadDirectory( + FileSystemContext* context, + const FileSystemURL& url, + FileEntryList* entries) { + DCHECK(entries); + entries->clear(); + base::PlatformFileError result = base::PLATFORM_FILE_ERROR_FAILED; + FileSystemOperation* operation = + context->CreateFileSystemOperation(url, NULL); + EXPECT_TRUE(operation != NULL); + base::RunLoop run_loop; + operation->ReadDirectory( + url, base::Bind(&ReadDirectoryCallback, &run_loop, &result, entries)); + run_loop.Run(); + return result; +} + +base::PlatformFileError AsyncFileTestHelper::CreateDirectory( + FileSystemContext* context, + const FileSystemURL& url) { + FileSystemOperation* operation = + context->CreateFileSystemOperation(url, NULL); + EXPECT_TRUE(operation != NULL); + base::PlatformFileError result = base::PLATFORM_FILE_ERROR_FAILED; + base::RunLoop run_loop; + operation->CreateDirectory(url, + false /* exclusive */, + false /* recursive */, + AssignAndQuitCallback(&run_loop, &result)); + run_loop.Run(); + return result; +} + +base::PlatformFileError AsyncFileTestHelper::CreateFile( + FileSystemContext* context, + const FileSystemURL& url) { + FileSystemOperation* operation = + context->CreateFileSystemOperation(url, NULL); + EXPECT_TRUE(operation != NULL); + base::RunLoop run_loop; + base::PlatformFileError result = base::PLATFORM_FILE_ERROR_FAILED; + operation->CreateFile(url, false /* exclusive */, + AssignAndQuitCallback(&run_loop, &result)); + run_loop.Run(); + return result; +} + +base::PlatformFileError AsyncFileTestHelper::TruncateFile( + FileSystemContext* context, + const FileSystemURL& url, + size_t size) { + FileSystemOperation* operation = + context->CreateFileSystemOperation(url, NULL); + EXPECT_TRUE(operation != NULL); + base::RunLoop run_loop; + base::PlatformFileError result = base::PLATFORM_FILE_ERROR_FAILED; + operation->Truncate(url, size, + AssignAndQuitCallback(&run_loop, &result)); + run_loop.Run(); + return result; +} + +base::PlatformFileError AsyncFileTestHelper::GetMetadata( + FileSystemContext* context, + const FileSystemURL& url, + base::PlatformFileInfo* file_info, + base::FilePath* platform_path) { + base::PlatformFileError result = base::PLATFORM_FILE_ERROR_FAILED; + base::RunLoop run_loop; + FileSystemOperation* operation = + context->CreateFileSystemOperation(url, NULL); + EXPECT_TRUE(operation != NULL); + operation->GetMetadata(url, base::Bind(&GetMetadataCallback, + &run_loop, &result, + file_info, platform_path)); + run_loop.Run(); + return result; +} + +bool AsyncFileTestHelper::FileExists( + FileSystemContext* context, + const FileSystemURL& url, + int64 expected_size) { + base::PlatformFileInfo file_info; + base::PlatformFileError result = GetMetadata(context, url, &file_info, NULL); + if (result != base::PLATFORM_FILE_OK || file_info.is_directory) + return false; + return expected_size == kDontCheckSize || file_info.size == expected_size; +} + +bool AsyncFileTestHelper::DirectoryExists( + FileSystemContext* context, + const FileSystemURL& url) { + base::PlatformFileInfo file_info; + base::PlatformFileError result = GetMetadata(context, url, &file_info, NULL); + return (result == base::PLATFORM_FILE_OK) && file_info.is_directory; +} + +quota::QuotaStatusCode AsyncFileTestHelper::GetUsageAndQuota( + quota::QuotaManager* quota_manager, + const GURL& origin, + FileSystemType type, + int64* usage, + int64* quota) { + quota::QuotaStatusCode status = quota::kQuotaStatusUnknown; + quota_manager->GetUsageAndQuota( + origin, + FileSystemTypeToQuotaStorageType(type), + base::Bind(&DidGetUsageAndQuota, &status, usage, quota)); + MessageLoop::current()->RunUntilIdle(); + return status; +} + +} // namespace fileapi diff --git a/webkit/fileapi/async_file_test_helper.h b/webkit/fileapi/async_file_test_helper.h new file mode 100644 index 0000000..9955b40 --- /dev/null +++ b/webkit/fileapi/async_file_test_helper.h @@ -0,0 +1,90 @@ +// Copyright (c) 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef WEBKIT_FILEAPI_ASYNC_FILE_TEST_HELPER_H_ +#define WEBKIT_FILEAPI_ASYNC_FILE_TEST_HELPER_H_ + +#include "base/basictypes.h" +#include "webkit/fileapi/file_system_operation.h" +#include "webkit/fileapi/file_system_types.h" +#include "webkit/quota/quota_status_code.h" + +namespace quota { +class QuotaManager; +} + +namespace fileapi { + +class FileSystemContext; +class FileSystemURL; + +// A helper class to perform async file operations in a synchronous way. +class AsyncFileTestHelper { + public: + typedef FileSystemOperation::FileEntryList FileEntryList; + + static const int64 kDontCheckSize; + + // Performs Copy from |src| to |dest| and returns the status code. + static base::PlatformFileError Copy(FileSystemContext* context, + const FileSystemURL& src, + const FileSystemURL& dest); + + // Performs Move from |src| to |dest| and returns the status code. + static base::PlatformFileError Move(FileSystemContext* context, + const FileSystemURL& src, + const FileSystemURL& dest); + + // Removes the given |url|. + static base::PlatformFileError Remove(FileSystemContext* context, + const FileSystemURL& url, + bool recursive); + + // Performs ReadDirectory on |url|. + static base::PlatformFileError ReadDirectory(FileSystemContext* context, + const FileSystemURL& url, + FileEntryList* entries); + + // Creates a directory at |url|. + static base::PlatformFileError CreateDirectory(FileSystemContext* context, + const FileSystemURL& url); + + // Creates a file at |url|. + static base::PlatformFileError CreateFile(FileSystemContext* context, + const FileSystemURL& url); + + // Truncates the file |url| to |size|. + static base::PlatformFileError TruncateFile(FileSystemContext* context, + const FileSystemURL& url, + size_t size); + + // Retrieves PlatformFileInfo for |url| and populates |file_info|. + static base::PlatformFileError GetMetadata(FileSystemContext* context, + const FileSystemURL& url, + base::PlatformFileInfo* file_info, + base::FilePath* platform_path); + + // Returns true if a file exists at |url| with |size|. If |size| is + // kDontCheckSize it doesn't check the file size (but just check its + // existence). + static bool FileExists(FileSystemContext* context, + const FileSystemURL& url, + int64 size); + + // Returns true if a directory exists at |url|. + static bool DirectoryExists(FileSystemContext* context, + const FileSystemURL& url); + + // Returns usage and quota. It's valid to pass NULL to |usage| and/or |quota|. + static quota::QuotaStatusCode GetUsageAndQuota( + quota::QuotaManager* quota_manager, + const GURL& origin, + FileSystemType type, + int64* usage, + int64* quota); +}; + +} // namespace fileapi + +#endif // WEBKIT_FILEAPI_ASYNC_FILE_TEST_HELPER_H_ diff --git a/webkit/fileapi/local_file_system_cross_operation_unittest.cc b/webkit/fileapi/local_file_system_cross_operation_unittest.cc index ad1d748..337a6a9 100644 --- a/webkit/fileapi/local_file_system_cross_operation_unittest.cc +++ b/webkit/fileapi/local_file_system_cross_operation_unittest.cc @@ -11,15 +11,15 @@ #include "base/message_loop.h" #include "base/run_loop.h" #include "base/stl_util.h" -#include "base/stringprintf.h" #include "testing/gtest/include/gtest/gtest.h" +#include "webkit/fileapi/async_file_test_helper.h" #include "webkit/fileapi/external_mount_points.h" #include "webkit/fileapi/file_system_context.h" #include "webkit/fileapi/file_system_mount_point_provider.h" +#include "webkit/fileapi/file_system_operation.h" #include "webkit/fileapi/file_system_task_runners.h" #include "webkit/fileapi/file_system_url.h" #include "webkit/fileapi/file_system_util.h" -#include "webkit/fileapi/local_file_system_operation.h" #include "webkit/fileapi/mock_file_system_options.h" #include "webkit/fileapi/test_file_set.h" #include "webkit/quota/mock_quota_manager.h" @@ -28,61 +28,8 @@ namespace fileapi { -namespace { - -const int64 kDontCheckSize = -1; typedef FileSystemOperation::FileEntryList FileEntryList; -void AssignAndQuit(base::RunLoop* run_loop, - base::PlatformFileError* result_out, - base::PlatformFileError result) { - *result_out = result; - run_loop->Quit(); -} - -base::Callback<void(base::PlatformFileError)> -AssignAndQuitCallback(base::RunLoop* run_loop, - base::PlatformFileError* result) { - return base::Bind(&AssignAndQuit, run_loop, base::Unretained(result)); -} - -void GetMetadataCallback(base::RunLoop* run_loop, - base::PlatformFileError* result_out, - base::PlatformFileInfo* file_info_out, - base::PlatformFileError result, - const base::PlatformFileInfo& file_info, - const base::FilePath& /* platform_path */) { - *result_out = result; - *file_info_out = file_info; - run_loop->Quit(); -} - -void ReadDirectoryCallback(base::RunLoop* run_loop, - base::PlatformFileError* result_out, - FileEntryList* entries_out, - base::PlatformFileError result, - const FileEntryList& entries, - bool has_more) { - *result_out = result; - *entries_out = entries; - if (result != base::PLATFORM_FILE_OK || !has_more) - run_loop->Quit(); -} - -void DidGetUsageAndQuota(quota::QuotaStatusCode* status_out, - int64* usage_out, - int64* quota_out, - quota::QuotaStatusCode status, - int64 usage, - int64 quota) { - if (status_out) - *status_out = status; - if (usage_out) - *usage_out = usage; - if (quota_out) - *quota_out = quota; -} - class CrossOperationTestHelper { public: CrossOperationTestHelper( @@ -165,26 +112,12 @@ class CrossOperationTestHelper { base::PlatformFileError Copy(const FileSystemURL& src, const FileSystemURL& dest) { - FileSystemOperation* operation = - file_system_context_->CreateFileSystemOperation(dest, NULL); - EXPECT_TRUE(operation != NULL); - base::PlatformFileError result = base::PLATFORM_FILE_ERROR_FAILED; - base::RunLoop run_loop; - operation->Copy(src, dest, AssignAndQuitCallback(&run_loop, &result)); - run_loop.Run(); - return result; + return AsyncFileTestHelper::Copy(file_system_context_, src, dest); } base::PlatformFileError Move(const FileSystemURL& src, const FileSystemURL& dest) { - FileSystemOperation* operation = - file_system_context_->CreateFileSystemOperation(dest, NULL); - EXPECT_TRUE(operation != NULL); - base::PlatformFileError result = base::PLATFORM_FILE_ERROR_FAILED; - base::RunLoop run_loop; - operation->Move(src, dest, AssignAndQuitCallback(&run_loop, &result)); - run_loop.Run(); - return result; + return AsyncFileTestHelper::Move(file_system_context_, src, dest); } base::PlatformFileError SetUpTestCaseFiles( @@ -243,101 +176,41 @@ class CrossOperationTestHelper { base::PlatformFileError ReadDirectory(const FileSystemURL& url, FileEntryList* entries) { - entries->clear(); - base::PlatformFileError result = base::PLATFORM_FILE_ERROR_FAILED; - FileSystemOperation* operation = - file_system_context_->CreateFileSystemOperation(url, NULL); - EXPECT_TRUE(operation != NULL); - base::RunLoop run_loop; - operation->ReadDirectory( - url, base::Bind(&ReadDirectoryCallback, &run_loop, &result, entries)); - run_loop.Run(); - return result; + return AsyncFileTestHelper::ReadDirectory( + file_system_context_, url, entries); } base::PlatformFileError CreateDirectory(const FileSystemURL& url) { - FileSystemOperation* operation = - file_system_context_->CreateFileSystemOperation(url, NULL); - EXPECT_TRUE(operation != NULL); - base::PlatformFileError result = base::PLATFORM_FILE_ERROR_FAILED; - base::RunLoop run_loop; - operation->CreateDirectory(url, - false /* exclusive */, - false /* recursive */, - AssignAndQuitCallback(&run_loop, &result)); - run_loop.Run(); - return result; + return AsyncFileTestHelper::CreateDirectory( + file_system_context_, url); } base::PlatformFileError CreateFile(const FileSystemURL& url, size_t size) { - base::PlatformFileError result = base::PLATFORM_FILE_ERROR_FAILED; - { - FileSystemOperation* operation = - file_system_context_->CreateFileSystemOperation(url, NULL); - EXPECT_TRUE(operation != NULL); - base::RunLoop run_loop; - operation->CreateFile(url, false /* exclusive */, - AssignAndQuitCallback(&run_loop, &result)); - run_loop.Run(); - } + base::PlatformFileError result = + AsyncFileTestHelper::CreateFile(file_system_context_, url); if (result != base::PLATFORM_FILE_OK) return result; - - { - FileSystemOperation* operation = - file_system_context_->CreateFileSystemOperation(url, NULL); - EXPECT_TRUE(operation != NULL); - base::RunLoop run_loop; - operation->Truncate(url, size, - AssignAndQuitCallback(&run_loop, &result)); - run_loop.Run(); - } - return result; + return AsyncFileTestHelper::TruncateFile(file_system_context_, url, size); } bool FileExists(const FileSystemURL& url, int64 expected_size) { - base::PlatformFileError result = base::PLATFORM_FILE_ERROR_FAILED; - base::PlatformFileInfo file_info; - base::RunLoop run_loop; - FileSystemOperation* operation = - file_system_context_->CreateFileSystemOperation(url, NULL); - EXPECT_TRUE(operation != NULL); - operation->GetMetadata(url, base::Bind(&GetMetadataCallback, - &run_loop, &result, &file_info)); - run_loop.Run(); - if (result != base::PLATFORM_FILE_OK || file_info.is_directory) - return false; - return expected_size == kDontCheckSize || file_info.size == expected_size; + return AsyncFileTestHelper::FileExists( + file_system_context_, url, expected_size); } bool DirectoryExists(const FileSystemURL& url) { - base::PlatformFileError result = base::PLATFORM_FILE_ERROR_FAILED; - base::PlatformFileInfo file_info; - base::RunLoop run_loop; - FileSystemOperation* operation = - file_system_context_->CreateFileSystemOperation(url, NULL); - EXPECT_TRUE(operation != NULL); - operation->GetMetadata(url, base::Bind(&GetMetadataCallback, - &run_loop, &result, &file_info)); - run_loop.Run(); - return (result == base::PLATFORM_FILE_OK) && file_info.is_directory; + return AsyncFileTestHelper::DirectoryExists(file_system_context_, url); } - GURL origin() const { return origin_; } - FileSystemType src_type() const { return src_type_; } - FileSystemType dest_type() const { return dest_type_; } - private: void GetUsageAndQuota(FileSystemType type, int64* usage, int64* quota) { - quota::QuotaStatusCode status = quota::kQuotaStatusUnknown; - quota_manager_->GetUsageAndQuota( - origin_, - FileSystemTypeToQuotaStorageType(type), - base::Bind(&DidGetUsageAndQuota, &status, usage, quota)); - MessageLoop::current()->RunUntilIdle(); + quota::QuotaStatusCode status = + AsyncFileTestHelper::GetUsageAndQuota( + quota_manager_, origin_, type, usage, quota); ASSERT_EQ(quota::kQuotaStatusOk, status); } + private: base::ScopedTempDir base_; const GURL origin_; @@ -352,8 +225,6 @@ class CrossOperationTestHelper { DISALLOW_COPY_AND_ASSIGN(CrossOperationTestHelper); }; -} // namespace - TEST(LocalFileSystemCrossOperationTest, CopySingleFile) { CrossOperationTestHelper helper(GURL("http://foo"), kFileSystemTypeTemporary, @@ -402,7 +273,7 @@ TEST(LocalFileSystemCrossOperationTest, MoveSingleFile) { ASSERT_EQ(base::PLATFORM_FILE_OK, helper.Move(src, dest)); // Verify. - ASSERT_FALSE(helper.FileExists(src, kDontCheckSize)); + ASSERT_FALSE(helper.FileExists(src, AsyncFileTestHelper::kDontCheckSize)); ASSERT_TRUE(helper.FileExists(dest, 10)); int64 src_new_usage = helper.GetSourceUsage(); diff --git a/webkit/fileapi/local_file_system_operation_unittest.cc b/webkit/fileapi/local_file_system_operation_unittest.cc index 8bc9972..6f2bd0d 100644 --- a/webkit/fileapi/local_file_system_operation_unittest.cc +++ b/webkit/fileapi/local_file_system_operation_unittest.cc @@ -14,12 +14,12 @@ #include "googleurl/src/gurl.h" #include "testing/gtest/include/gtest/gtest.h" #include "webkit/blob/shareable_file_reference.h" +#include "webkit/fileapi/async_file_test_helper.h" #include "webkit/fileapi/file_system_context.h" #include "webkit/fileapi/file_system_file_util.h" #include "webkit/fileapi/file_system_mount_point_provider.h" #include "webkit/fileapi/file_system_quota_util.h" #include "webkit/fileapi/file_system_util.h" -#include "webkit/fileapi/file_util_helper.h" #include "webkit/fileapi/local_file_system_test_helper.h" #include "webkit/fileapi/mock_file_change_observer.h" #include "webkit/quota/mock_quota_manager.h" @@ -111,18 +111,15 @@ class LocalFileSystemOperationTest bool FileExists(const base::FilePath& virtual_path) { FileSystemURL url = test_helper_.CreateURL(virtual_path); - base::PlatformFileInfo file_info; - base::FilePath platform_path; - scoped_ptr<FileSystemOperationContext> context(NewContext()); - base::PlatformFileError error = file_util()->GetFileInfo( - context.get(), url, &file_info, &platform_path); - return error == base::PLATFORM_FILE_OK && !file_info.is_directory; + return AsyncFileTestHelper::FileExists( + test_helper_.file_system_context(), url, + AsyncFileTestHelper::kDontCheckSize); } bool DirectoryExists(const base::FilePath& virtual_path) { FileSystemURL url = test_helper_.CreateURL(virtual_path); - scoped_ptr<FileSystemOperationContext> context(NewContext()); - return FileUtilHelper::DirectoryExists(context.get(), file_util(), url); + return AsyncFileTestHelper::DirectoryExists( + test_helper_.file_system_context(), url); } base::FilePath CreateUniqueFileInDir(const base::FilePath& virtual_dir_path) { @@ -209,29 +206,11 @@ class LocalFileSystemOperationTest shareable_file_ref_ = shareable_file_ref; } - static void DidGetUsageAndQuota(quota::QuotaStatusCode* status_out, - int64* usage_out, - int64* quota_out, - quota::QuotaStatusCode status, - int64 usage, - int64 quota) { - if (status_out) - *status_out = status; - - if (usage_out) - *usage_out = usage; - - if (quota_out) - *quota_out = quota; - } - void GetUsageAndQuota(int64* usage, int64* quota) { - quota::QuotaStatusCode status = quota::kQuotaStatusUnknown; - quota_manager_->GetUsageAndQuota( - test_helper_.origin(), - test_helper_.storage_type(), - base::Bind(&LocalFileSystemOperationTest::DidGetUsageAndQuota, - &status, usage, quota)); + quota::QuotaStatusCode status = + AsyncFileTestHelper::GetUsageAndQuota( + quota_manager_, test_helper_.origin(), test_helper_.type(), + usage, quota); MessageLoop::current()->RunUntilIdle(); ASSERT_EQ(quota::kQuotaStatusOk, status); } @@ -407,9 +386,7 @@ TEST_F(LocalFileSystemOperationTest, TestMoveSuccessSrcFileAndOverwrite) { EXPECT_EQ(1, change_observer()->get_and_reset_remove_file_count()); EXPECT_TRUE(change_observer()->HasNoChange()); - // Move is considered 'write' access (for both side), and won't be counted - // as read access. - EXPECT_EQ(0, quota_manager_proxy()->notify_storage_accessed_count()); + EXPECT_EQ(1, quota_manager_proxy()->notify_storage_accessed_count()); } TEST_F(LocalFileSystemOperationTest, TestMoveSuccessSrcFileAndNew) { @@ -609,7 +586,7 @@ TEST_F(LocalFileSystemOperationTest, TestCopySuccessSrcFileAndOverwrite) { MessageLoop::current()->RunUntilIdle(); EXPECT_EQ(base::PLATFORM_FILE_OK, status()); EXPECT_TRUE(FileExists(dest_file_path)); - EXPECT_EQ(1, quota_manager_proxy()->notify_storage_accessed_count()); + EXPECT_EQ(2, quota_manager_proxy()->notify_storage_accessed_count()); EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count()); EXPECT_TRUE(change_observer()->HasNoChange()); @@ -619,14 +596,15 @@ TEST_F(LocalFileSystemOperationTest, TestCopySuccessSrcFileAndNew) { base::FilePath src_dir_path(CreateUniqueDir()); base::FilePath src_file_path(CreateUniqueFileInDir(src_dir_path)); base::FilePath dest_dir_path(CreateUniqueDir()); - base::FilePath dest_file_path(dest_dir_path.Append(FILE_PATH_LITERAL("NewFile"))); + base::FilePath dest_file_path(dest_dir_path.Append( + FILE_PATH_LITERAL("NewFile"))); operation()->Copy(URLForPath(src_file_path), URLForPath(dest_file_path), RecordStatusCallback()); MessageLoop::current()->RunUntilIdle(); EXPECT_EQ(base::PLATFORM_FILE_OK, status()); EXPECT_TRUE(FileExists(dest_file_path)); - EXPECT_EQ(1, quota_manager_proxy()->notify_storage_accessed_count()); + EXPECT_EQ(2, quota_manager_proxy()->notify_storage_accessed_count()); EXPECT_EQ(1, change_observer()->get_and_reset_create_file_from_count()); EXPECT_TRUE(change_observer()->HasNoChange()); @@ -645,7 +623,7 @@ TEST_F(LocalFileSystemOperationTest, TestCopySuccessSrcDirAndOverwrite) { EXPECT_TRUE(DirectoryExists(dest_dir_path)); EXPECT_FALSE(DirectoryExists( dest_dir_path.Append(VirtualPath::BaseName(src_dir_path)))); - EXPECT_EQ(1, quota_manager_proxy()->notify_storage_accessed_count()); + EXPECT_EQ(3, quota_manager_proxy()->notify_storage_accessed_count()); EXPECT_EQ(1, change_observer()->get_and_reset_remove_directory_count()); EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count()); @@ -663,7 +641,7 @@ TEST_F(LocalFileSystemOperationTest, TestCopySuccessSrcDirAndNew) { MessageLoop::current()->RunUntilIdle(); EXPECT_EQ(base::PLATFORM_FILE_OK, status()); EXPECT_TRUE(DirectoryExists(dest_child_dir_path)); - EXPECT_EQ(1, quota_manager_proxy()->notify_storage_accessed_count()); + EXPECT_EQ(2, quota_manager_proxy()->notify_storage_accessed_count()); EXPECT_EQ(1, change_observer()->get_and_reset_create_directory_count()); EXPECT_TRUE(change_observer()->HasNoChange()); diff --git a/webkit/fileapi/local_file_system_quota_unittest.cc b/webkit/fileapi/local_file_system_quota_unittest.cc index 0e39102..8e0daed 100644 --- a/webkit/fileapi/local_file_system_quota_unittest.cc +++ b/webkit/fileapi/local_file_system_quota_unittest.cc @@ -16,9 +16,9 @@ #include "base/platform_file.h" #include "base/string_number_conversions.h" #include "testing/gtest/include/gtest/gtest.h" +#include "webkit/fileapi/async_file_test_helper.h" #include "webkit/fileapi/file_system_usage_cache.h" #include "webkit/fileapi/file_system_util.h" -#include "webkit/fileapi/file_util_helper.h" #include "webkit/fileapi/local_file_system_operation.h" #include "webkit/fileapi/local_file_system_test_helper.h" #include "webkit/quota/quota_manager.h" @@ -58,9 +58,6 @@ class LocalFileSystemQuotaTest virtual void SetUp() OVERRIDE; virtual void TearDown() OVERRIDE; - void OnGetUsageAndQuota( - quota::QuotaStatusCode status, int64 usage, int64 quota); - protected: FileSystemFileUtil* file_util() { return test_helper_.file_util(); @@ -93,27 +90,23 @@ class LocalFileSystemQuotaTest } void GetUsageAndQuotaFromQuotaManager() { - quota_manager_->GetUsageAndQuota( - test_helper_.origin(), test_helper_.storage_type(), - base::Bind(&LocalFileSystemQuotaTest::OnGetUsageAndQuota, - weak_factory_.GetWeakPtr())); + quota_status_ = AsyncFileTestHelper::GetUsageAndQuota( + quota_manager_, test_helper_.origin(), test_helper_.type(), + &usage_, "a_); MessageLoop::current()->RunUntilIdle(); } bool FileExists(const base::FilePath& virtual_path) { FileSystemURL url = test_helper_.CreateURL(virtual_path); - base::PlatformFileInfo file_info; - base::FilePath platform_path; - scoped_ptr<FileSystemOperationContext> context(NewContext()); - base::PlatformFileError error = file_util()->GetFileInfo( - context.get(), url, &file_info, &platform_path); - return error == base::PLATFORM_FILE_OK; + return AsyncFileTestHelper::FileExists( + test_helper_.file_system_context(), url, + AsyncFileTestHelper::kDontCheckSize); } bool DirectoryExists(const base::FilePath& virtual_path) { - FileSystemURL path = test_helper_.CreateURL(virtual_path); - scoped_ptr<FileSystemOperationContext> context(NewContext()); - return FileUtilHelper::DirectoryExists(context.get(), file_util(), path); + FileSystemURL url = test_helper_.CreateURL(virtual_path); + return AsyncFileTestHelper::DirectoryExists( + test_helper_.file_system_context(), url); } base::FilePath CreateUniqueFileInDir(const base::FilePath& virtual_dir_path) { @@ -210,13 +203,6 @@ LocalFileSystemOperation* LocalFileSystemQuotaTest::operation() { return test_helper_.NewOperation(); } -void LocalFileSystemQuotaTest::OnGetUsageAndQuota( - quota::QuotaStatusCode status, int64 usage, int64 quota) { - quota_status_ = status; - usage_ = usage; - quota_ = quota; -} - void LocalFileSystemQuotaTest::PrepareFileSet(const base::FilePath& virtual_path) { int64 usage = SizeByQuotaUtil(); child_dir_path_ = CreateUniqueDirInDir(virtual_path); diff --git a/webkit/fileapi/local_file_system_test_helper.cc b/webkit/fileapi/local_file_system_test_helper.cc index fe206f1..120f1fe 100644 --- a/webkit/fileapi/local_file_system_test_helper.cc +++ b/webkit/fileapi/local_file_system_test_helper.cc @@ -15,7 +15,6 @@ #include "webkit/fileapi/file_system_url.h" #include "webkit/fileapi/file_system_usage_cache.h" #include "webkit/fileapi/file_system_util.h" -#include "webkit/fileapi/file_util_helper.h" #include "webkit/fileapi/local_file_system_operation.h" #include "webkit/fileapi/mock_file_system_options.h" #include "webkit/fileapi/sandbox_mount_point_provider.h" @@ -124,20 +123,6 @@ FileSystemURL LocalFileSystemTestOriginHelper::CreateURL(const base::FilePath& p return file_system_context_->CreateCrackedFileSystemURL(origin_, type_, path); } -base::PlatformFileError LocalFileSystemTestOriginHelper::SameFileUtilCopy( - FileSystemOperationContext* context, - const FileSystemURL& src, - const FileSystemURL& dest) const { - return FileUtilHelper::Copy(context, file_util(), file_util(), src, dest); -} - -base::PlatformFileError LocalFileSystemTestOriginHelper::SameFileUtilMove( - FileSystemOperationContext* context, - const FileSystemURL& src, - const FileSystemURL& dest) const { - return FileUtilHelper::Move(context, file_util(), file_util(), src, dest); -} - int64 LocalFileSystemTestOriginHelper::GetCachedOriginUsage() const { return file_system_context_->GetQuotaUtil(type_)->GetOriginUsageOnFileThread( file_system_context_, origin_, type_); diff --git a/webkit/fileapi/local_file_system_test_helper.h b/webkit/fileapi/local_file_system_test_helper.h index 6dbebc6..f3f20f3 100644 --- a/webkit/fileapi/local_file_system_test_helper.h +++ b/webkit/fileapi/local_file_system_test_helper.h @@ -62,16 +62,6 @@ class LocalFileSystemTestOriginHelper { return CreateURL(base::FilePath::FromUTF8Unsafe(utf8)); } - // Helper methods for same-FileUtil copy/move. - base::PlatformFileError SameFileUtilCopy( - FileSystemOperationContext* context, - const FileSystemURL& src, - const FileSystemURL& dest) const; - base::PlatformFileError SameFileUtilMove( - FileSystemOperationContext* context, - const FileSystemURL& src, - const FileSystemURL& dest) const; - // This returns cached usage size returned by QuotaUtil. int64 GetCachedOriginUsage() const; diff --git a/webkit/fileapi/local_file_util_unittest.cc b/webkit/fileapi/local_file_util_unittest.cc index 0d7df03..7582687 100644 --- a/webkit/fileapi/local_file_util_unittest.cc +++ b/webkit/fileapi/local_file_util_unittest.cc @@ -12,6 +12,7 @@ #include "base/sys_string_conversions.h" #include "base/utf_string_conversions.h" #include "testing/gtest/include/gtest/gtest.h" +#include "webkit/fileapi/async_file_test_helper.h" #include "webkit/fileapi/file_system_context.h" #include "webkit/fileapi/file_system_file_util.h" #include "webkit/fileapi/file_system_operation_context.h" @@ -94,6 +95,10 @@ class LocalFileUtilTest : public testing::Test { return test_helper_; } + FileSystemContext* file_system_context() { + return test_helper_.file_system_context(); + } + private: scoped_ptr<LocalFileUtil> local_file_util_; base::ScopedTempDir data_dir_; @@ -218,15 +223,14 @@ TEST_F(LocalFileUtilTest, CopyFile) { EXPECT_TRUE(FileExists(from_file)); EXPECT_EQ(1020, GetSize(from_file)); - context.reset(NewContext()); ASSERT_EQ(base::PLATFORM_FILE_OK, - test_helper().SameFileUtilCopy(context.get(), - Path(from_file), Path(to_file1))); + AsyncFileTestHelper::Copy(file_system_context(), + Path(from_file), Path(to_file1))); context.reset(NewContext()); ASSERT_EQ(base::PLATFORM_FILE_OK, - test_helper().SameFileUtilCopy(context.get(), - Path(from_file), Path(to_file2))); + AsyncFileTestHelper::Copy(file_system_context(), + Path(from_file), Path(to_file2))); EXPECT_TRUE(FileExists(from_file)); EXPECT_EQ(1020, GetSize(from_file)); @@ -261,8 +265,8 @@ TEST_F(LocalFileUtilTest, CopyDirectory) { context.reset(NewContext()); ASSERT_EQ(base::PLATFORM_FILE_OK, - test_helper().SameFileUtilCopy(context.get(), - Path(from_dir), Path(to_dir))); + AsyncFileTestHelper::Copy(file_system_context(), + Path(from_dir), Path(to_dir))); EXPECT_TRUE(DirectoryExists(from_dir)); EXPECT_TRUE(FileExists(from_file)); @@ -289,8 +293,8 @@ TEST_F(LocalFileUtilTest, MoveFile) { context.reset(NewContext()); ASSERT_EQ(base::PLATFORM_FILE_OK, - test_helper().SameFileUtilMove(context.get(), - Path(from_file), Path(to_file))); + AsyncFileTestHelper::Move(file_system_context(), + Path(from_file), Path(to_file))); EXPECT_FALSE(FileExists(from_file)); EXPECT_TRUE(FileExists(to_file)); @@ -322,8 +326,8 @@ TEST_F(LocalFileUtilTest, MoveDirectory) { context.reset(NewContext()); ASSERT_EQ(base::PLATFORM_FILE_OK, - test_helper().SameFileUtilMove(context.get(), - Path(from_dir), Path(to_dir))); + AsyncFileTestHelper::Move(file_system_context(), + Path(from_dir), Path(to_dir))); EXPECT_FALSE(DirectoryExists(from_dir)); EXPECT_TRUE(DirectoryExists(to_dir)); diff --git a/webkit/fileapi/obfuscated_file_util_unittest.cc b/webkit/fileapi/obfuscated_file_util_unittest.cc index ef0f2bc..5c5159f 100644 --- a/webkit/fileapi/obfuscated_file_util_unittest.cc +++ b/webkit/fileapi/obfuscated_file_util_unittest.cc @@ -14,12 +14,12 @@ #include "base/message_loop.h" #include "base/platform_file.h" #include "testing/gtest/include/gtest/gtest.h" +#include "webkit/fileapi/async_file_test_helper.h" #include "webkit/fileapi/external_mount_points.h" #include "webkit/fileapi/file_system_context.h" #include "webkit/fileapi/file_system_operation_context.h" #include "webkit/fileapi/file_system_task_runners.h" #include "webkit/fileapi/file_system_usage_cache.h" -#include "webkit/fileapi/file_util_helper.h" #include "webkit/fileapi/local_file_system_test_helper.h" #include "webkit/fileapi/mock_file_change_observer.h" #include "webkit/fileapi/mock_file_system_options.h" @@ -209,11 +209,10 @@ class ObfuscatedFileUtilTest : public testing::Test { } void GetUsageFromQuotaManager() { - quota_manager_->GetUsageAndQuota( - origin(), test_helper_.storage_type(), - base::Bind(&ObfuscatedFileUtilTest::OnGetUsage, - weak_factory_.GetWeakPtr())); - MessageLoop::current()->RunUntilIdle(); + int64 quota = -1; + quota_status_ = AsyncFileTestHelper::GetUsageAndQuota( + quota_manager_, origin(), test_helper_.type(), + &usage_, "a); EXPECT_EQ(quota::kQuotaStatusOk, quota_status_); } @@ -241,8 +240,7 @@ class ObfuscatedFileUtilTest : public testing::Test { } bool DirectoryExists(const FileSystemURL& url) { - scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); - return FileUtilHelper::DirectoryExists(context.get(), ofu(), url); + return AsyncFileTestHelper::DirectoryExists(file_system_context(), url); } int64 usage() const { return usage_; } @@ -259,12 +257,6 @@ class ObfuscatedFileUtilTest : public testing::Test { return test_helper_.CreateURL(path); } - void OnGetUsage(quota::QuotaStatusCode status, int64 usage, int64 unused) { - EXPECT_EQ(quota::kQuotaStatusOk, status); - quota_status_ = status; - usage_ = usage; - } - void CheckFileAndCloseHandle( const FileSystemURL& url, base::PlatformFile file_handle) { scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); @@ -393,11 +385,10 @@ class ObfuscatedFileUtilTest : public testing::Test { std::set<base::FilePath::StringType>* files, std::set<base::FilePath::StringType>* directories) { scoped_ptr<FileSystemOperationContext> context; - context.reset(NewContext(NULL)); std::vector<base::FileUtilProxy::Entry> entries; EXPECT_EQ(base::PLATFORM_FILE_OK, - FileUtilHelper::ReadDirectory( - context.get(), ofu(), root_url, &entries)); + AsyncFileTestHelper::ReadDirectory( + file_system_context(), root_url, &entries)); EXPECT_EQ(0UL, entries.size()); files->clear(); @@ -440,8 +431,8 @@ class ObfuscatedFileUtilTest : public testing::Test { std::vector<base::FileUtilProxy::Entry> entries; context.reset(NewContext(NULL)); EXPECT_EQ(base::PLATFORM_FILE_OK, - FileUtilHelper::ReadDirectory( - context.get(), ofu(), root_url, &entries)); + AsyncFileTestHelper::ReadDirectory( + file_system_context(), root_url, &entries)); std::vector<base::FileUtilProxy::Entry>::iterator entry_iter; EXPECT_EQ(files.size() + directories.size(), entries.size()); EXPECT_TRUE(change_observer()->HasNoChange()); @@ -635,6 +626,10 @@ class ObfuscatedFileUtilTest : public testing::Test { return test_helper_; } + FileSystemContext* file_system_context() { + return test_helper_.file_system_context(); + } + private: base::ScopedTempDir data_dir_; MessageLoop message_loop_; @@ -1078,11 +1073,10 @@ TEST_F(ObfuscatedFileUtilTest, TestReadDirectoryOnFile) { ofu()->EnsureFileExists(context.get(), url, &created)); ASSERT_TRUE(created); - context.reset(NewContext(NULL)); std::vector<base::FileUtilProxy::Entry> entries; EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY, - FileUtilHelper::ReadDirectory( - context.get(), ofu(), url, &entries)); + AsyncFileTestHelper::ReadDirectory( + file_system_context(), url, &entries)); EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), url)); } @@ -1396,18 +1390,17 @@ TEST_F(ObfuscatedFileUtilTest, TestEnumerator) { FileSystemURL dest_url = CreateURLFromUTF8("destination dir"); EXPECT_FALSE(DirectoryExists(dest_url)); - context.reset(NewContext(NULL)); ASSERT_EQ(base::PLATFORM_FILE_OK, - test_helper().SameFileUtilCopy(context.get(), src_url, dest_url)); + AsyncFileTestHelper::Copy( + test_helper().file_system_context(), src_url, dest_url)); ValidateTestDirectory(dest_url, files, directories); EXPECT_TRUE(DirectoryExists(src_url)); EXPECT_TRUE(DirectoryExists(dest_url)); - context.reset(NewContext(NULL)); recursive = true; ASSERT_EQ(base::PLATFORM_FILE_OK, - FileUtilHelper::Delete(context.get(), ofu(), - dest_url, recursive)); + AsyncFileTestHelper::Remove( + file_system_context(), dest_url, recursive)); EXPECT_FALSE(DirectoryExists(dest_url)); } @@ -1636,24 +1629,21 @@ TEST_F(ObfuscatedFileUtilTest, TestIncompleteDirectoryReading) { EXPECT_TRUE(created); } - context.reset(NewContext(NULL)); std::vector<base::FileUtilProxy::Entry> entries; EXPECT_EQ(base::PLATFORM_FILE_OK, - FileUtilHelper::ReadDirectory( - context.get(), ofu(), empty_path, &entries)); + AsyncFileTestHelper::ReadDirectory( + file_system_context(), empty_path, &entries)); EXPECT_EQ(3u, entries.size()); - context.reset(NewContext(NULL)); base::FilePath local_path; EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath(context.get(), kPath[0], &local_path)); EXPECT_TRUE(file_util::Delete(local_path, false)); - context.reset(NewContext(NULL)); entries.clear(); EXPECT_EQ(base::PLATFORM_FILE_OK, - FileUtilHelper::ReadDirectory( - context.get(), ofu(), empty_path, &entries)); + AsyncFileTestHelper::ReadDirectory( + file_system_context(), empty_path, &entries)); EXPECT_EQ(ARRAYSIZE_UNSAFE(kPath) - 1, entries.size()); } @@ -2169,12 +2159,8 @@ TEST_F(ObfuscatedFileUtilTest, TestQuotaOnRemove) { ASSERT_EQ(1140, ComputeTotalFileSize()); ASSERT_EQ(base::PLATFORM_FILE_OK, - FileUtilHelper::Delete( - AllowUsageIncrease(-PathCost(dir) - - PathCost(dfile1) - - PathCost(dfile2) - - 1020 - 120)->context(), - ofu(), dir, true)); + AsyncFileTestHelper::Remove( + file_system_context(), dir, true /* recursive */)); ASSERT_EQ(0, ComputeTotalFileSize()); } |