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(src
,
109 storage::FileSystemOperation::OPTION_NONE
,
111 AssignAndQuitCallback(&run_loop
, &result
));
116 base::File::Error
AsyncFileTestHelper::Move(
117 storage::FileSystemContext
* context
,
118 const storage::FileSystemURL
& src
,
119 const storage::FileSystemURL
& dest
) {
120 base::File::Error result
= base::File::FILE_ERROR_FAILED
;
121 base::RunLoop run_loop
;
122 context
->operation_runner()->Move(src
,
124 storage::FileSystemOperation::OPTION_NONE
,
125 AssignAndQuitCallback(&run_loop
, &result
));
130 base::File::Error
AsyncFileTestHelper::Remove(
131 storage::FileSystemContext
* context
,
132 const storage::FileSystemURL
& url
,
134 base::File::Error result
= base::File::FILE_ERROR_FAILED
;
135 base::RunLoop run_loop
;
136 context
->operation_runner()->Remove(
137 url
, recursive
, AssignAndQuitCallback(&run_loop
, &result
));
142 base::File::Error
AsyncFileTestHelper::ReadDirectory(
143 storage::FileSystemContext
* context
,
144 const storage::FileSystemURL
& url
,
145 FileEntryList
* entries
) {
146 base::File::Error result
= base::File::FILE_ERROR_FAILED
;
149 base::RunLoop run_loop
;
150 context
->operation_runner()->ReadDirectory(
151 url
, base::Bind(&ReadDirectoryCallback
, &run_loop
, &result
, entries
));
156 base::File::Error
AsyncFileTestHelper::CreateDirectory(
157 storage::FileSystemContext
* context
,
158 const storage::FileSystemURL
& url
) {
159 base::File::Error result
= base::File::FILE_ERROR_FAILED
;
160 base::RunLoop run_loop
;
161 context
->operation_runner()->CreateDirectory(
163 false /* exclusive */,
164 false /* recursive */,
165 AssignAndQuitCallback(&run_loop
, &result
));
170 base::File::Error
AsyncFileTestHelper::CreateFile(
171 storage::FileSystemContext
* context
,
172 const storage::FileSystemURL
& url
) {
173 base::File::Error result
= base::File::FILE_ERROR_FAILED
;
174 base::RunLoop run_loop
;
175 context
->operation_runner()->CreateFile(
176 url
, false /* exclusive */,
177 AssignAndQuitCallback(&run_loop
, &result
));
182 base::File::Error
AsyncFileTestHelper::CreateFileWithData(
183 storage::FileSystemContext
* context
,
184 const storage::FileSystemURL
& url
,
187 base::ScopedTempDir dir
;
188 if (!dir
.CreateUniqueTempDir())
189 return base::File::FILE_ERROR_FAILED
;
190 base::FilePath local_path
= dir
.path().AppendASCII("tmp");
191 if (buf_size
!= base::WriteFile(local_path
, buf
, buf_size
))
192 return base::File::FILE_ERROR_FAILED
;
193 base::File::Error result
= base::File::FILE_ERROR_FAILED
;
194 base::RunLoop run_loop
;
195 context
->operation_runner()->CopyInForeignFile(
196 local_path
, url
, AssignAndQuitCallback(&run_loop
, &result
));
201 base::File::Error
AsyncFileTestHelper::TruncateFile(
202 storage::FileSystemContext
* context
,
203 const storage::FileSystemURL
& url
,
205 base::RunLoop run_loop
;
206 base::File::Error result
= base::File::FILE_ERROR_FAILED
;
207 context
->operation_runner()->Truncate(
208 url
, size
, AssignAndQuitCallback(&run_loop
, &result
));
213 base::File::Error
AsyncFileTestHelper::GetMetadata(
214 storage::FileSystemContext
* context
,
215 const storage::FileSystemURL
& url
,
216 base::File::Info
* file_info
) {
217 base::File::Error result
= base::File::FILE_ERROR_FAILED
;
218 base::RunLoop run_loop
;
219 context
->operation_runner()->GetMetadata(
220 url
, base::Bind(&GetMetadataCallback
, &run_loop
, &result
,
226 base::File::Error
AsyncFileTestHelper::GetPlatformPath(
227 storage::FileSystemContext
* context
,
228 const storage::FileSystemURL
& url
,
229 base::FilePath
* platform_path
) {
230 base::File::Error result
= base::File::FILE_ERROR_FAILED
;
231 base::RunLoop run_loop
;
232 context
->operation_runner()->CreateSnapshotFile(
233 url
, base::Bind(&CreateSnapshotFileCallback
, &run_loop
, &result
,
239 bool AsyncFileTestHelper::FileExists(storage::FileSystemContext
* context
,
240 const storage::FileSystemURL
& url
,
241 int64 expected_size
) {
242 base::File::Info file_info
;
243 base::File::Error result
= GetMetadata(context
, url
, &file_info
);
244 if (result
!= base::File::FILE_OK
|| file_info
.is_directory
)
246 return expected_size
== kDontCheckSize
|| file_info
.size
== expected_size
;
249 bool AsyncFileTestHelper::DirectoryExists(storage::FileSystemContext
* context
,
250 const storage::FileSystemURL
& url
) {
251 base::File::Info file_info
;
252 base::File::Error result
= GetMetadata(context
, url
, &file_info
);
253 return (result
== base::File::FILE_OK
) && file_info
.is_directory
;
256 storage::QuotaStatusCode
AsyncFileTestHelper::GetUsageAndQuota(
257 storage::QuotaManager
* quota_manager
,
259 storage::FileSystemType type
,
262 storage::QuotaStatusCode status
= storage::kQuotaStatusUnknown
;
263 quota_manager
->GetUsageAndQuota(
265 FileSystemTypeToQuotaStorageType(type
),
266 base::Bind(&DidGetUsageAndQuota
, &status
, usage
, quota
));
267 base::RunLoop().RunUntilIdle();
271 } // namespace storage