OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "base/bind.h" | |
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" | |
17 | |
18 namespace content { | |
19 | |
20 typedef storage::FileSystemOperation::FileEntryList FileEntryList; | |
21 | |
22 namespace { | |
23 | |
24 void AssignAndQuit(base::RunLoop* run_loop, | |
25 base::File::Error* result_out, | |
26 base::File::Error result) { | |
27 *result_out = result; | |
28 run_loop->Quit(); | |
29 } | |
30 | |
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)); | |
35 } | |
36 | |
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) { | |
42 *result_out = result; | |
43 if (file_info_out) | |
44 *file_info_out = file_info; | |
45 run_loop->Quit(); | |
46 } | |
47 | |
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()); | |
57 *result_out = result; | |
58 if (platform_path_out) | |
59 *platform_path_out = platform_path; | |
60 run_loop->Quit(); | |
61 } | |
62 | |
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, | |
68 bool has_more) { | |
69 *result_out = result; | |
70 entries_out->insert(entries_out->end(), entries.begin(), entries.end()); | |
71 if (result != base::File::FILE_OK || !has_more) | |
72 run_loop->Quit(); | |
73 } | |
74 | |
75 void DidGetUsageAndQuota(storage::QuotaStatusCode* status_out, | |
76 int64_t* usage_out, | |
77 int64_t* quota_out, | |
78 storage::QuotaStatusCode status, | |
79 int64_t usage, | |
80 int64_t quota) { | |
81 if (status_out) | |
82 *status_out = status; | |
83 if (usage_out) | |
84 *usage_out = usage; | |
85 if (quota_out) | |
86 *quota_out = quota; | |
87 } | |
88 | |
89 } // namespace | |
90 | |
91 const int64_t AsyncFileTestHelper::kDontCheckSize = -1; | |
92 | |
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()); | |
98 } | |
99 | |
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)); | |
111 run_loop.Run(); | |
112 return result; | |
113 } | |
114 | |
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, | |
122 dest, | |
123 storage::FileSystemOperation::OPTION_NONE, | |
124 AssignAndQuitCallback(&run_loop, &result)); | |
125 run_loop.Run(); | |
126 return result; | |
127 } | |
128 | |
129 base::File::Error AsyncFileTestHelper::Remove( | |
130 storage::FileSystemContext* context, | |
131 const storage::FileSystemURL& url, | |
132 bool recursive) { | |
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)); | |
137 run_loop.Run(); | |
138 return result; | |
139 } | |
140 | |
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; | |
146 DCHECK(entries); | |
147 entries->clear(); | |
148 base::RunLoop run_loop; | |
149 context->operation_runner()->ReadDirectory( | |
150 url, base::Bind(&ReadDirectoryCallback, &run_loop, &result, entries)); | |
151 run_loop.Run(); | |
152 return result; | |
153 } | |
154 | |
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( | |
161 url, | |
162 false /* exclusive */, | |
163 false /* recursive */, | |
164 AssignAndQuitCallback(&run_loop, &result)); | |
165 run_loop.Run(); | |
166 return result; | |
167 } | |
168 | |
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)); | |
177 run_loop.Run(); | |
178 return result; | |
179 } | |
180 | |
181 base::File::Error AsyncFileTestHelper::CreateFileWithData( | |
182 storage::FileSystemContext* context, | |
183 const storage::FileSystemURL& url, | |
184 const char* buf, | |
185 int buf_size) { | |
186 base::ScopedTempDir dir; | |
187 if (!dir.CreateUniqueTempDir()) | |
188 return base::File::FILE_ERROR_FAILED; | |
189 base::FilePath local_path = dir.GetPath().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)); | |
196 run_loop.Run(); | |
197 return result; | |
198 } | |
199 | |
200 base::File::Error AsyncFileTestHelper::TruncateFile( | |
201 storage::FileSystemContext* context, | |
202 const storage::FileSystemURL& url, | |
203 size_t size) { | |
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)); | |
208 run_loop.Run(); | |
209 return result; | |
210 } | |
211 | |
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, storage::FileSystemOperation::GET_METADATA_FIELD_IS_DIRECTORY | | |
220 storage::FileSystemOperation::GET_METADATA_FIELD_SIZE | | |
221 storage::FileSystemOperation::GET_METADATA_FIELD_LAST_MODIFIED, | |
222 base::Bind(&GetMetadataCallback, &run_loop, &result, file_info)); | |
223 run_loop.Run(); | |
224 return result; | |
225 } | |
226 | |
227 base::File::Error AsyncFileTestHelper::GetPlatformPath( | |
228 storage::FileSystemContext* context, | |
229 const storage::FileSystemURL& url, | |
230 base::FilePath* platform_path) { | |
231 base::File::Error result = base::File::FILE_ERROR_FAILED; | |
232 base::RunLoop run_loop; | |
233 context->operation_runner()->CreateSnapshotFile( | |
234 url, base::Bind(&CreateSnapshotFileCallback, &run_loop, &result, | |
235 platform_path)); | |
236 run_loop.Run(); | |
237 return result; | |
238 } | |
239 | |
240 bool AsyncFileTestHelper::FileExists(storage::FileSystemContext* context, | |
241 const storage::FileSystemURL& url, | |
242 int64_t expected_size) { | |
243 base::File::Info file_info; | |
244 base::File::Error result = GetMetadata(context, url, &file_info); | |
245 if (result != base::File::FILE_OK || file_info.is_directory) | |
246 return false; | |
247 return expected_size == kDontCheckSize || file_info.size == expected_size; | |
248 } | |
249 | |
250 bool AsyncFileTestHelper::DirectoryExists(storage::FileSystemContext* context, | |
251 const storage::FileSystemURL& url) { | |
252 base::File::Info file_info; | |
253 base::File::Error result = GetMetadata(context, url, &file_info); | |
254 return (result == base::File::FILE_OK) && file_info.is_directory; | |
255 } | |
256 | |
257 storage::QuotaStatusCode AsyncFileTestHelper::GetUsageAndQuota( | |
258 storage::QuotaManager* quota_manager, | |
259 const GURL& origin, | |
260 storage::FileSystemType type, | |
261 int64_t* usage, | |
262 int64_t* quota) { | |
263 storage::QuotaStatusCode status = storage::kQuotaStatusUnknown; | |
264 quota_manager->GetUsageAndQuota( | |
265 origin, | |
266 FileSystemTypeToQuotaStorageType(type), | |
267 base::Bind(&DidGetUsageAndQuota, &status, usage, quota)); | |
268 base::RunLoop().RunUntilIdle(); | |
269 return status; | |
270 } | |
271 | |
272 } // namespace content | |
OLD | NEW |