1 // Copyright (c) 2013 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.
6 #include "base/file_util.h"
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/run_loop.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "webkit/browser/fileapi/async_file_test_helper.h"
11 #include "webkit/browser/fileapi/file_system_backend.h"
12 #include "webkit/browser/fileapi/file_system_context.h"
13 #include "webkit/browser/fileapi/file_system_operation_runner.h"
14 #include "webkit/browser/fileapi/file_system_url.h"
15 #include "webkit/browser/quota/quota_manager.h"
16 #include "webkit/common/fileapi/file_system_util.h"
22 typedef FileSystemOperation::FileEntryList FileEntryList
;
24 void AssignAndQuit(base::RunLoop
* run_loop
,
25 base::PlatformFileError
* result_out
,
26 base::PlatformFileError result
) {
31 base::Callback
<void(base::PlatformFileError
)>
32 AssignAndQuitCallback(base::RunLoop
* run_loop
,
33 base::PlatformFileError
* result
) {
34 return base::Bind(&AssignAndQuit
, run_loop
, base::Unretained(result
));
37 void GetMetadataCallback(base::RunLoop
* run_loop
,
38 base::PlatformFileError
* result_out
,
39 base::PlatformFileInfo
* file_info_out
,
40 base::PlatformFileError result
,
41 const base::PlatformFileInfo
& file_info
) {
44 *file_info_out
= file_info
;
48 void CreateSnapshotFileCallback(
49 base::RunLoop
* run_loop
,
50 base::PlatformFileError
* result_out
,
51 base::FilePath
* platform_path_out
,
52 base::PlatformFileError result
,
53 const base::PlatformFileInfo
& file_info
,
54 const base::FilePath
& platform_path
,
55 const scoped_refptr
<webkit_blob::ShareableFileReference
>& file_ref
) {
56 DCHECK(!file_ref
.get());
58 if (platform_path_out
)
59 *platform_path_out
= platform_path
;
63 void ReadDirectoryCallback(base::RunLoop
* run_loop
,
64 base::PlatformFileError
* result_out
,
65 FileEntryList
* entries_out
,
66 base::PlatformFileError result
,
67 const FileEntryList
& entries
,
70 *entries_out
= entries
;
71 if (result
!= base::PLATFORM_FILE_OK
|| !has_more
)
75 void DidGetUsageAndQuota(quota::QuotaStatusCode
* status_out
,
78 quota::QuotaStatusCode status
,
91 const int64
AsyncFileTestHelper::kDontCheckSize
= -1;
93 base::PlatformFileError
AsyncFileTestHelper::Copy(
94 FileSystemContext
* context
,
95 const FileSystemURL
& src
,
96 const FileSystemURL
& dest
) {
97 return CopyWithProgress(context
, src
, dest
, CopyProgressCallback());
100 base::PlatformFileError
AsyncFileTestHelper::CopyWithProgress(
101 FileSystemContext
* context
,
102 const FileSystemURL
& src
,
103 const FileSystemURL
& dest
,
104 const CopyProgressCallback
& progress_callback
) {
105 base::PlatformFileError result
= base::PLATFORM_FILE_ERROR_FAILED
;
106 base::RunLoop run_loop
;
107 context
->operation_runner()->Copy(
108 src
, dest
, FileSystemOperation::OPTION_NONE
, progress_callback
,
109 AssignAndQuitCallback(&run_loop
, &result
));
114 base::PlatformFileError
AsyncFileTestHelper::Move(
115 FileSystemContext
* context
,
116 const FileSystemURL
& src
,
117 const FileSystemURL
& dest
) {
118 base::PlatformFileError result
= base::PLATFORM_FILE_ERROR_FAILED
;
119 base::RunLoop run_loop
;
120 context
->operation_runner()->Move(
121 src
, dest
, FileSystemOperation::OPTION_NONE
,
122 AssignAndQuitCallback(&run_loop
, &result
));
127 base::PlatformFileError
AsyncFileTestHelper::Remove(
128 FileSystemContext
* context
,
129 const FileSystemURL
& url
,
131 base::PlatformFileError result
= base::PLATFORM_FILE_ERROR_FAILED
;
132 base::RunLoop run_loop
;
133 context
->operation_runner()->Remove(
134 url
, recursive
, AssignAndQuitCallback(&run_loop
, &result
));
139 base::PlatformFileError
AsyncFileTestHelper::ReadDirectory(
140 FileSystemContext
* context
,
141 const FileSystemURL
& url
,
142 FileEntryList
* entries
) {
143 base::PlatformFileError result
= base::PLATFORM_FILE_ERROR_FAILED
;
146 base::RunLoop run_loop
;
147 context
->operation_runner()->ReadDirectory(
148 url
, base::Bind(&ReadDirectoryCallback
, &run_loop
, &result
, entries
));
153 base::PlatformFileError
AsyncFileTestHelper::CreateDirectory(
154 FileSystemContext
* context
,
155 const FileSystemURL
& url
) {
156 base::PlatformFileError result
= base::PLATFORM_FILE_ERROR_FAILED
;
157 base::RunLoop run_loop
;
158 context
->operation_runner()->CreateDirectory(
160 false /* exclusive */,
161 false /* recursive */,
162 AssignAndQuitCallback(&run_loop
, &result
));
167 base::PlatformFileError
AsyncFileTestHelper::CreateFile(
168 FileSystemContext
* context
,
169 const FileSystemURL
& url
) {
170 base::PlatformFileError result
= base::PLATFORM_FILE_ERROR_FAILED
;
171 base::RunLoop run_loop
;
172 context
->operation_runner()->CreateFile(
173 url
, false /* exclusive */,
174 AssignAndQuitCallback(&run_loop
, &result
));
179 base::PlatformFileError
AsyncFileTestHelper::CreateFileWithData(
180 FileSystemContext
* context
,
181 const FileSystemURL
& url
,
184 base::ScopedTempDir dir
;
185 if (!dir
.CreateUniqueTempDir())
186 return base::PLATFORM_FILE_ERROR_FAILED
;
187 base::FilePath local_path
= dir
.path().AppendASCII("tmp");
188 if (buf_size
!= file_util::WriteFile(local_path
, buf
, buf_size
))
189 return base::PLATFORM_FILE_ERROR_FAILED
;
190 base::PlatformFileError result
= base::PLATFORM_FILE_ERROR_FAILED
;
191 base::RunLoop run_loop
;
192 context
->operation_runner()->CopyInForeignFile(
193 local_path
, url
, AssignAndQuitCallback(&run_loop
, &result
));
198 base::PlatformFileError
AsyncFileTestHelper::TruncateFile(
199 FileSystemContext
* context
,
200 const FileSystemURL
& url
,
202 base::RunLoop run_loop
;
203 base::PlatformFileError result
= base::PLATFORM_FILE_ERROR_FAILED
;
204 context
->operation_runner()->Truncate(
205 url
, size
, AssignAndQuitCallback(&run_loop
, &result
));
210 base::PlatformFileError
AsyncFileTestHelper::GetMetadata(
211 FileSystemContext
* context
,
212 const FileSystemURL
& url
,
213 base::PlatformFileInfo
* file_info
) {
214 base::PlatformFileError result
= base::PLATFORM_FILE_ERROR_FAILED
;
215 base::RunLoop run_loop
;
216 context
->operation_runner()->GetMetadata(
217 url
, base::Bind(&GetMetadataCallback
, &run_loop
, &result
,
223 base::PlatformFileError
AsyncFileTestHelper::GetPlatformPath(
224 FileSystemContext
* context
,
225 const FileSystemURL
& url
,
226 base::FilePath
* platform_path
) {
227 base::PlatformFileError result
= base::PLATFORM_FILE_ERROR_FAILED
;
228 base::RunLoop run_loop
;
229 context
->operation_runner()->CreateSnapshotFile(
230 url
, base::Bind(&CreateSnapshotFileCallback
, &run_loop
, &result
,
236 bool AsyncFileTestHelper::FileExists(
237 FileSystemContext
* context
,
238 const FileSystemURL
& url
,
239 int64 expected_size
) {
240 base::PlatformFileInfo file_info
;
241 base::PlatformFileError result
= GetMetadata(context
, url
, &file_info
);
242 if (result
!= base::PLATFORM_FILE_OK
|| file_info
.is_directory
)
244 return expected_size
== kDontCheckSize
|| file_info
.size
== expected_size
;
247 bool AsyncFileTestHelper::DirectoryExists(
248 FileSystemContext
* context
,
249 const FileSystemURL
& url
) {
250 base::PlatformFileInfo file_info
;
251 base::PlatformFileError result
= GetMetadata(context
, url
, &file_info
);
252 return (result
== base::PLATFORM_FILE_OK
) && file_info
.is_directory
;
255 quota::QuotaStatusCode
AsyncFileTestHelper::GetUsageAndQuota(
256 quota::QuotaManager
* quota_manager
,
261 quota::QuotaStatusCode status
= quota::kQuotaStatusUnknown
;
262 quota_manager
->GetUsageAndQuota(
264 FileSystemTypeToQuotaStorageType(type
),
265 base::Bind(&DidGetUsageAndQuota
, &status
, usage
, quota
));
266 base::RunLoop().RunUntilIdle();
270 } // namespace fileapi