Index: chrome/browser/media_galleries/fileapi/async_file_util_test_helper.cc |
diff --git a/chrome/browser/media_galleries/fileapi/async_file_util_test_helper.cc b/chrome/browser/media_galleries/fileapi/async_file_util_test_helper.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..7e8497ccca2bfbcd626bb544461882ba97116457 |
--- /dev/null |
+++ b/chrome/browser/media_galleries/fileapi/async_file_util_test_helper.cc |
@@ -0,0 +1,299 @@ |
+// Copyright 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 "chrome/browser/media_galleries/fileapi/async_file_util_test_helper.h" |
+ |
+#include "base/bind.h" |
+#include "base/files/file_path.h" |
+#include "base/platform_file.h" |
+#include "base/run_loop.h" |
+#include "webkit/common/blob/scoped_file.h" |
+#include "webkit/common/blob/shareable_file_reference.h" |
+ |
+using fileapi::AsyncFileUtil; |
+using fileapi::FileSystemOperationContext; |
+using fileapi::FileSystemURL; |
+ |
+namespace chrome { |
+ |
+namespace { |
+ |
+// Needed as base::Bind only supports functions of up to arity 7. |
+struct CreateSnapshotResults { |
+ scoped_refptr<webkit_blob::ShareableFileReference>* file_ref; |
+ base::PlatformFileError* error; |
+ base::PlatformFileInfo* file_info; |
+ base::FilePath* platform_path; |
+}; |
+ |
+void CreateOrOpenTestCallback( |
+ base::RunLoop* run_loop, |
+ base::PlatformFileError* error_result, |
+ base::PlatformFile* platform_file_result, |
+ bool* created_result, |
+ base::PlatformFileError error, |
+ base::PassPlatformFile pass_platform_file, |
+ bool created) { |
+ DCHECK(error_result); |
+ DCHECK(platform_file_result); |
+ DCHECK(created_result); |
+ *error_result = error; |
+ *platform_file_result = pass_platform_file.ReleaseValue(); |
+ *created_result = created; |
+ run_loop->Quit(); |
+} |
+ |
+void CreateSnapshotTestCallback( |
+ base::RunLoop* run_loop, |
+ CreateSnapshotResults* results, |
+ base::PlatformFileError error, |
+ const base::PlatformFileInfo& file_info, |
+ const base::FilePath& platform_path, |
+ const scoped_refptr<webkit_blob::ShareableFileReference>& file_ref) { |
+ DCHECK(results->file_ref); |
+ DCHECK(results->error); |
+ DCHECK(results->file_info); |
+ DCHECK(results->platform_path); |
+ *(results->error) = error; |
+ *(results->file_info) = file_info; |
+ *(results->platform_path) = platform_path; |
+ *(results->file_ref) = file_ref; |
+ run_loop->Quit(); |
+} |
+ |
+void EnsureFileExistsTestCallback( |
+ base::RunLoop* run_loop, |
+ base::PlatformFileError* error_result, |
+ bool* created_result, |
+ base::PlatformFileError error, |
+ bool created) { |
+ DCHECK(error_result); |
+ DCHECK(created_result); |
+ *error_result = error; |
+ *created_result = created; |
+ run_loop->Quit(); |
+} |
+ |
+void GetFileInfoTestCallback( |
+ base::RunLoop* run_loop, |
+ base::PlatformFileError* error_result, |
+ base::PlatformFileInfo* file_info_result, |
+ base::FilePath* platform_path_result, |
+ base::PlatformFileError error, |
+ const base::PlatformFileInfo& file_info, |
+ const base::FilePath& platform_path) { |
+ DCHECK(error_result); |
+ DCHECK(file_info_result); |
+ DCHECK(platform_path_result); |
+ *error_result = error; |
+ *file_info_result = file_info; |
+ *platform_path_result = platform_path; |
+ run_loop->Quit(); |
+} |
+ |
+void ReadDirectoryTestCallback( |
+ base::RunLoop* run_loop, |
+ base::PlatformFileError* error_result, |
+ AsyncFileUtil::EntryList* file_list_result, |
+ bool* has_more_result, |
+ base::PlatformFileError error, |
+ const AsyncFileUtil::EntryList& file_list, |
+ bool has_more) { |
+ DCHECK(error_result); |
+ DCHECK(file_list_result); |
+ DCHECK(has_more_result); |
+ *error_result = error; |
+ *file_list_result = file_list; |
+ *has_more_result = has_more; |
+ run_loop->Quit(); |
+} |
+ |
+void StatusTestCallback( |
+ base::RunLoop* run_loop, |
+ base::PlatformFileError* error_result, |
+ base::PlatformFileError error) { |
+ DCHECK(error_result); |
+ *error_result = error; |
+ run_loop->Quit(); |
+} |
+ |
+} // namespace |
+ |
+AsyncFileUtilTestHelper::AsyncFileUtilTestHelper(AsyncFileUtil* async_file_util) |
+ : async_file_util_(async_file_util) { |
+} |
+ |
+AsyncFileUtilTestHelper::~AsyncFileUtilTestHelper() {} |
+ |
+base::PlatformFileError AsyncFileUtilTestHelper::CreateOrOpen( |
+ FileSystemOperationContext* context, |
+ const FileSystemURL& url, |
+ int file_flags, |
+ base::PlatformFile* file_handle, |
+ bool* created) { |
+ DCHECK(context); |
+ base::PlatformFileError result; |
+ base::RunLoop run_loop; |
+ async_file_util_->CreateOrOpen( |
+ context, |
+ url, |
+ file_flags, |
+ base::Bind( |
+ &CreateOrOpenTestCallback, |
+ &run_loop, |
+ &result, |
+ file_handle, |
+ created)); |
+ run_loop.Run(); |
+ return result; |
+} |
+ |
+base::PlatformFileError AsyncFileUtilTestHelper::EnsureFileExists( |
+ FileSystemOperationContext* context, |
+ const FileSystemURL& url, |
+ bool* created) { |
+ DCHECK(context); |
+ base::PlatformFileError result; |
+ base::RunLoop run_loop; |
+ async_file_util_->EnsureFileExists( |
+ context, |
+ url, |
+ base::Bind(&EnsureFileExistsTestCallback, &run_loop, &result, created)); |
+ run_loop.Run(); |
+ return result; |
+} |
+ |
+base::PlatformFileError AsyncFileUtilTestHelper::CreateDirectory( |
+ FileSystemOperationContext* context, |
+ const FileSystemURL& url, |
+ bool exclusive, |
+ bool recursive) { |
+ DCHECK(context); |
+ base::PlatformFileError result; |
+ base::RunLoop run_loop; |
+ async_file_util_->CreateDirectory(context, url, exclusive, recursive, |
+ base::Bind(&StatusTestCallback, |
+ &run_loop, &result)); |
+ run_loop.Run(); |
+ return result; |
+} |
+ |
+base::PlatformFileError AsyncFileUtilTestHelper::GetFileInfo( |
+ FileSystemOperationContext* context, |
+ const FileSystemURL& url, |
+ base::PlatformFileInfo* file_info, |
+ base::FilePath* platform_path) { |
+ DCHECK(context); |
+ base::PlatformFileError result; |
+ base::RunLoop run_loop; |
+ async_file_util_->GetFileInfo( |
+ context, |
+ url, |
+ base::Bind(&GetFileInfoTestCallback, &run_loop, &result, file_info, |
+ platform_path)); |
+ run_loop.Run(); |
+ return result; |
+} |
+ |
+base::PlatformFileError AsyncFileUtilTestHelper::Touch( |
+ FileSystemOperationContext* context, |
+ const FileSystemURL& url, |
+ const base::Time& last_access_time, |
+ const base::Time& last_modified_time) { |
+ DCHECK(context); |
+ base::PlatformFileError result; |
+ base::RunLoop run_loop; |
+ async_file_util_->Touch(context, url, last_access_time, last_modified_time, |
+ base::Bind(&StatusTestCallback, &run_loop, &result)); |
+ run_loop.Run(); |
+ return result; |
+} |
+ |
+base::PlatformFileError AsyncFileUtilTestHelper::Truncate( |
+ FileSystemOperationContext* context, |
+ const FileSystemURL& url, |
+ int64 length) { |
+ DCHECK(context); |
+ base::PlatformFileError result; |
+ base::RunLoop run_loop; |
+ async_file_util_->Truncate(context, url, length, |
+ base::Bind(&StatusTestCallback, &run_loop, |
+ &result)); |
+ run_loop.Run(); |
+ return result; |
+} |
+ |
+base::PlatformFileError AsyncFileUtilTestHelper::CopyOrMoveFile( |
+ FileSystemOperationContext* context, |
+ const FileSystemURL& src_url, |
+ const FileSystemURL& dest_url, |
+ bool copy) { |
+ DCHECK(context); |
+ base::PlatformFileError result; |
+ base::RunLoop run_loop; |
+ if (copy) { |
+ async_file_util_->CopyFileLocal(context, src_url, dest_url, |
+ base::Bind(&StatusTestCallback, &run_loop, |
+ &result)); |
+ } else { |
+ async_file_util_->MoveFileLocal(context, src_url, dest_url, |
+ base::Bind(&StatusTestCallback, &run_loop, |
+ &result)); |
+ } |
+ run_loop.Run(); |
+ return result; |
+} |
+ |
+base::PlatformFileError AsyncFileUtilTestHelper::CopyInForeignFile( |
+ FileSystemOperationContext* context, |
+ const base::FilePath& src_file_path, |
+ const FileSystemURL& dest_url) { |
+ DCHECK(context); |
+ base::PlatformFileError result; |
+ base::RunLoop run_loop; |
+ async_file_util_->CopyInForeignFile(context, src_file_path, dest_url, |
+ base::Bind(&StatusTestCallback, &run_loop, |
+ &result)); |
+ run_loop.Run(); |
+ return result; |
+} |
+ |
+base::PlatformFileError AsyncFileUtilTestHelper::ReadDirectorySync( |
+ FileSystemOperationContext* context, |
+ const FileSystemURL& url, |
+ AsyncFileUtil::EntryList* file_list, |
+ bool* has_more) { |
+ DCHECK(context); |
+ base::PlatformFileError result; |
+ base::RunLoop run_loop; |
+ async_file_util_->ReadDirectory( |
+ context, |
+ url, |
+ base::Bind(&ReadDirectoryTestCallback, &run_loop, &result, file_list, |
+ has_more)); |
+ run_loop.Run(); |
+ return result; |
+} |
+ |
+scoped_refptr<webkit_blob::ShareableFileReference> |
+AsyncFileUtilTestHelper::CreateSnapshotFileSharable( |
+ FileSystemOperationContext* context, |
+ const FileSystemURL& url, |
+ base::PlatformFileError* error, |
+ base::PlatformFileInfo* file_info, |
+ base::FilePath* platform_path) { |
+ DCHECK(context); |
+ scoped_refptr<webkit_blob::ShareableFileReference> file_ref_result; |
+ CreateSnapshotResults results = |
+ { &file_ref_result, error, file_info, platform_path }; |
+ base::RunLoop run_loop; |
+ async_file_util_->CreateSnapshotFile( |
+ context, |
+ url, |
+ base::Bind(&CreateSnapshotTestCallback, &run_loop, &results)); |
+ run_loop.Run(); |
+ return file_ref_result; |
+} |
+ |
+} // namespace fileapi |