1 // Copyright 2014 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/files/file_util.h"
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/run_loop.h"
9 #include "content/public/test/async_file_test_helper.h"
10 #include "storage/browser/fileapi/file_system_backend.h"
11 #include "storage/browser/fileapi/file_system_context.h"
12 #include "storage/browser/fileapi/file_system_operation_runner.h"
13 #include "storage/browser/fileapi/file_system_url.h"
14 #include "storage/browser/quota/quota_manager.h"
15 #include "storage/common/fileapi/file_system_util.h"
16 #include "testing/gtest/include/gtest/gtest.h"
20 typedef storage::FileSystemOperation::FileEntryList FileEntryList
;
24 void AssignAndQuit(base::RunLoop
* run_loop
,
25 base::File::Error
* result_out
,
26 base::File::Error result
) {
31 base::Callback
<void(base::File::Error
)>
32 AssignAndQuitCallback(base::RunLoop
* run_loop
,
33 base::File::Error
* result
) {
34 return base::Bind(&AssignAndQuit
, run_loop
, base::Unretained(result
));
37 void GetMetadataCallback(base::RunLoop
* run_loop
,
38 base::File::Error
* result_out
,
39 base::File::Info
* file_info_out
,
40 base::File::Error result
,
41 const base::File::Info
& file_info
) {
44 *file_info_out
= file_info
;
48 void CreateSnapshotFileCallback(
49 base::RunLoop
* run_loop
,
50 base::File::Error
* result_out
,
51 base::FilePath
* platform_path_out
,
52 base::File::Error result
,
53 const base::File::Info
& file_info
,
54 const base::FilePath
& platform_path
,
55 const scoped_refptr
<storage::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::File::Error
* result_out
,
65 FileEntryList
* entries_out
,
66 base::File::Error result
,
67 const FileEntryList
& entries
,
70 entries_out
->insert(entries_out
->end(), entries
.begin(), entries
.end());
71 if (result
!= base::File::FILE_OK
|| !has_more
)
75 void DidGetUsageAndQuota(storage::QuotaStatusCode
* status_out
,
78 storage::QuotaStatusCode status
,
91 const int64
AsyncFileTestHelper::kDontCheckSize
= -1;
93 base::File::Error
AsyncFileTestHelper::Copy(
94 storage::FileSystemContext
* context
,
95 const storage::FileSystemURL
& src
,
96 const storage::FileSystemURL
& dest
) {
97 return CopyWithProgress(context
, src
, dest
, CopyProgressCallback());
100 base::File::Error
AsyncFileTestHelper::CopyWithProgress(
101 storage::FileSystemContext
* context
,
102 const storage::FileSystemURL
& src
,
103 const storage::FileSystemURL
& dest
,
104 const CopyProgressCallback
& progress_callback
) {
105 base::File::Error result
= base::File::FILE_ERROR_FAILED
;
106 base::RunLoop run_loop
;
107 context
->operation_runner()->Copy(
108 src
, dest
, storage::FileSystemOperation::OPTION_NONE
,
109 storage::FileSystemOperation::ERROR_BEHAVIOR_ABORT
, progress_callback
,
110 AssignAndQuitCallback(&run_loop
, &result
));
115 base::File::Error
AsyncFileTestHelper::Move(
116 storage::FileSystemContext
* context
,
117 const storage::FileSystemURL
& src
,
118 const storage::FileSystemURL
& dest
) {
119 base::File::Error result
= base::File::FILE_ERROR_FAILED
;
120 base::RunLoop run_loop
;
121 context
->operation_runner()->Move(src
,
123 storage::FileSystemOperation::OPTION_NONE
,
124 AssignAndQuitCallback(&run_loop
, &result
));
129 base::File::Error
AsyncFileTestHelper::Remove(
130 storage::FileSystemContext
* context
,
131 const storage::FileSystemURL
& url
,
133 base::File::Error result
= base::File::FILE_ERROR_FAILED
;
134 base::RunLoop run_loop
;
135 context
->operation_runner()->Remove(
136 url
, recursive
, AssignAndQuitCallback(&run_loop
, &result
));
141 base::File::Error
AsyncFileTestHelper::ReadDirectory(
142 storage::FileSystemContext
* context
,
143 const storage::FileSystemURL
& url
,
144 FileEntryList
* entries
) {
145 base::File::Error result
= base::File::FILE_ERROR_FAILED
;
148 base::RunLoop run_loop
;
149 context
->operation_runner()->ReadDirectory(
150 url
, base::Bind(&ReadDirectoryCallback
, &run_loop
, &result
, entries
));
155 base::File::Error
AsyncFileTestHelper::CreateDirectory(
156 storage::FileSystemContext
* context
,
157 const storage::FileSystemURL
& url
) {
158 base::File::Error result
= base::File::FILE_ERROR_FAILED
;
159 base::RunLoop run_loop
;
160 context
->operation_runner()->CreateDirectory(
162 false /* exclusive */,
163 false /* recursive */,
164 AssignAndQuitCallback(&run_loop
, &result
));
169 base::File::Error
AsyncFileTestHelper::CreateFile(
170 storage::FileSystemContext
* context
,
171 const storage::FileSystemURL
& url
) {
172 base::File::Error result
= base::File::FILE_ERROR_FAILED
;
173 base::RunLoop run_loop
;
174 context
->operation_runner()->CreateFile(
175 url
, false /* exclusive */,
176 AssignAndQuitCallback(&run_loop
, &result
));
181 base::File::Error
AsyncFileTestHelper::CreateFileWithData(
182 storage::FileSystemContext
* context
,
183 const storage::FileSystemURL
& url
,
186 base::ScopedTempDir dir
;
187 if (!dir
.CreateUniqueTempDir())
188 return base::File::FILE_ERROR_FAILED
;
189 base::FilePath local_path
= dir
.path().AppendASCII("tmp");
190 if (buf_size
!= base::WriteFile(local_path
, buf
, buf_size
))
191 return base::File::FILE_ERROR_FAILED
;
192 base::File::Error result
= base::File::FILE_ERROR_FAILED
;
193 base::RunLoop run_loop
;
194 context
->operation_runner()->CopyInForeignFile(
195 local_path
, url
, AssignAndQuitCallback(&run_loop
, &result
));
200 base::File::Error
AsyncFileTestHelper::TruncateFile(
201 storage::FileSystemContext
* context
,
202 const storage::FileSystemURL
& url
,
204 base::RunLoop run_loop
;
205 base::File::Error result
= base::File::FILE_ERROR_FAILED
;
206 context
->operation_runner()->Truncate(
207 url
, size
, AssignAndQuitCallback(&run_loop
, &result
));
212 base::File::Error
AsyncFileTestHelper::GetMetadata(
213 storage::FileSystemContext
* context
,
214 const storage::FileSystemURL
& url
,
215 base::File::Info
* file_info
) {
216 base::File::Error result
= base::File::FILE_ERROR_FAILED
;
217 base::RunLoop run_loop
;
218 context
->operation_runner()->GetMetadata(
219 url
, base::Bind(&GetMetadataCallback
, &run_loop
, &result
,
225 base::File::Error
AsyncFileTestHelper::GetPlatformPath(
226 storage::FileSystemContext
* context
,
227 const storage::FileSystemURL
& url
,
228 base::FilePath
* platform_path
) {
229 base::File::Error result
= base::File::FILE_ERROR_FAILED
;
230 base::RunLoop run_loop
;
231 context
->operation_runner()->CreateSnapshotFile(
232 url
, base::Bind(&CreateSnapshotFileCallback
, &run_loop
, &result
,
238 bool AsyncFileTestHelper::FileExists(storage::FileSystemContext
* context
,
239 const storage::FileSystemURL
& url
,
240 int64 expected_size
) {
241 base::File::Info file_info
;
242 base::File::Error result
= GetMetadata(context
, url
, &file_info
);
243 if (result
!= base::File::FILE_OK
|| file_info
.is_directory
)
245 return expected_size
== kDontCheckSize
|| file_info
.size
== expected_size
;
248 bool AsyncFileTestHelper::DirectoryExists(storage::FileSystemContext
* context
,
249 const storage::FileSystemURL
& url
) {
250 base::File::Info file_info
;
251 base::File::Error result
= GetMetadata(context
, url
, &file_info
);
252 return (result
== base::File::FILE_OK
) && file_info
.is_directory
;
255 storage::QuotaStatusCode
AsyncFileTestHelper::GetUsageAndQuota(
256 storage::QuotaManager
* quota_manager
,
258 storage::FileSystemType type
,
261 storage::QuotaStatusCode status
= storage::kQuotaStatusUnknown
;
262 quota_manager
->GetUsageAndQuota(
264 FileSystemTypeToQuotaStorageType(type
),
265 base::Bind(&DidGetUsageAndQuota
, &status
, usage
, quota
));
266 base::RunLoop().RunUntilIdle();
270 } // namespace storage