OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <vector> | 5 #include <vector> |
6 | 6 |
7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
11 #include "base/message_loop_proxy.h" | 11 #include "base/message_loop_proxy.h" |
12 #include "googleurl/src/gurl.h" | 12 #include "googleurl/src/gurl.h" |
13 #include "net/url_request/url_request.h" | 13 #include "net/url_request/url_request.h" |
14 #include "net/url_request/url_request_context.h" | 14 #include "net/url_request/url_request_context.h" |
15 #include "net/url_request/url_request_job.h" | 15 #include "net/url_request/url_request_job.h" |
16 #include "net/url_request/url_request_job_factory_impl.h" | 16 #include "net/url_request/url_request_job_factory_impl.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 #include "webkit/blob/blob_data.h" | 18 #include "webkit/blob/blob_data.h" |
19 #include "webkit/blob/blob_storage_controller.h" | 19 #include "webkit/blob/blob_storage_context.h" |
20 #include "webkit/blob/blob_url_request_job.h" | 20 #include "webkit/blob/blob_url_request_job.h" |
21 #include "webkit/blob/mock_blob_url_request_context.h" | 21 #include "webkit/blob/mock_blob_url_request_context.h" |
22 #include "webkit/fileapi/file_system_file_util.h" | 22 #include "webkit/fileapi/file_system_file_util.h" |
23 #include "webkit/fileapi/file_system_operation_context.h" | 23 #include "webkit/fileapi/file_system_operation_context.h" |
24 #include "webkit/fileapi/file_system_util.h" | 24 #include "webkit/fileapi/file_system_util.h" |
25 #include "webkit/fileapi/local_file_system_operation.h" | 25 #include "webkit/fileapi/local_file_system_operation.h" |
26 #include "webkit/fileapi/local_file_system_test_helper.h" | 26 #include "webkit/fileapi/local_file_system_test_helper.h" |
27 #include "webkit/fileapi/local_file_util.h" | 27 #include "webkit/fileapi/local_file_util.h" |
28 #include "webkit/fileapi/mock_file_change_observer.h" | 28 #include "webkit/fileapi/mock_file_change_observer.h" |
29 #include "webkit/quota/quota_manager.h" | 29 #include "webkit/quota/quota_manager.h" |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 MockBlobURLRequestContext url_request_context_; | 185 MockBlobURLRequestContext url_request_context_; |
186 | 186 |
187 private: | 187 private: |
188 MockFileChangeObserver change_observer_; | 188 MockFileChangeObserver change_observer_; |
189 ChangeObserverList change_observers_; | 189 ChangeObserverList change_observers_; |
190 | 190 |
191 DISALLOW_COPY_AND_ASSIGN(LocalFileSystemOperationWriteTest); | 191 DISALLOW_COPY_AND_ASSIGN(LocalFileSystemOperationWriteTest); |
192 }; | 192 }; |
193 | 193 |
194 TEST_F(LocalFileSystemOperationWriteTest, TestWriteSuccess) { | 194 TEST_F(LocalFileSystemOperationWriteTest, TestWriteSuccess) { |
195 const GURL blob_url("blob:success"); | 195 ScopedTextBlob blob(url_request_context_, "blob-id:success", |
196 ScopedTextBlob blob(url_request_context_, blob_url, "Hello, world!\n"); | 196 "Hello, world!\n"); |
197 | |
198 NewOperation()->Write( | 197 NewOperation()->Write( |
199 &url_request_context_, URLForPath(virtual_path_), blob_url, | 198 &url_request_context_, URLForPath(virtual_path_), |
| 199 blob.GetBlobDataHandle(), |
200 0, RecordWriteCallback()); | 200 0, RecordWriteCallback()); |
201 MessageLoop::current()->Run(); | 201 MessageLoop::current()->Run(); |
202 | 202 |
203 EXPECT_EQ(14, bytes_written()); | 203 EXPECT_EQ(14, bytes_written()); |
204 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); | 204 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); |
205 EXPECT_TRUE(complete()); | 205 EXPECT_TRUE(complete()); |
206 | 206 |
207 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count()); | 207 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count()); |
208 } | 208 } |
209 | 209 |
210 TEST_F(LocalFileSystemOperationWriteTest, TestWriteZero) { | 210 TEST_F(LocalFileSystemOperationWriteTest, TestWriteZero) { |
211 GURL blob_url("blob:zero"); | 211 ScopedTextBlob blob(url_request_context_, "blob_id:zero", ""); |
212 scoped_refptr<webkit_blob::BlobData> blob_data(new webkit_blob::BlobData()); | |
213 | |
214 url_request_context_.blob_storage_controller()->AddFinishedBlob( | |
215 blob_url, blob_data); | |
216 | |
217 NewOperation()->Write( | 212 NewOperation()->Write( |
218 &url_request_context_, URLForPath(virtual_path_), | 213 &url_request_context_, URLForPath(virtual_path_), |
219 blob_url, 0, RecordWriteCallback()); | 214 blob.GetBlobDataHandle(), 0, RecordWriteCallback()); |
220 MessageLoop::current()->Run(); | 215 MessageLoop::current()->Run(); |
221 | 216 |
222 url_request_context_.blob_storage_controller()->RemoveBlob(blob_url); | |
223 | |
224 EXPECT_EQ(0, bytes_written()); | 217 EXPECT_EQ(0, bytes_written()); |
225 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); | 218 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); |
226 EXPECT_TRUE(complete()); | 219 EXPECT_TRUE(complete()); |
227 | 220 |
228 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count()); | 221 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count()); |
229 } | 222 } |
230 | 223 |
231 TEST_F(LocalFileSystemOperationWriteTest, TestWriteInvalidBlobUrl) { | 224 TEST_F(LocalFileSystemOperationWriteTest, TestWriteInvalidBlobUrl) { |
| 225 // TODO: what should this test exactly now? |
| 226 /* |
| 227 scoped_ptr<webkit_blob::BlobDataHandle> invalid; |
232 NewOperation()->Write( | 228 NewOperation()->Write( |
233 &url_request_context_, URLForPath(virtual_path_), | 229 &url_request_context_, URLForPath(virtual_path_), |
234 GURL("blob:invalid"), 0, RecordWriteCallback()); | 230 invalid.Pass(), 0, RecordWriteCallback()); |
| 231 MessageLoop::current()->Run(); |
| 232 */ |
| 233 } |
| 234 |
| 235 TEST_F(LocalFileSystemOperationWriteTest, TestWriteInvalidFile) { |
| 236 ScopedTextBlob blob(url_request_context_, "blob_id:writeinvalidfile", |
| 237 "It\'ll not be written."); |
| 238 NewOperation()->Write( |
| 239 &url_request_context_, |
| 240 URLForPath(base::FilePath(FILE_PATH_LITERAL("nonexist"))), |
| 241 blob.GetBlobDataHandle(), 0, RecordWriteCallback()); |
235 MessageLoop::current()->Run(); | 242 MessageLoop::current()->Run(); |
236 | 243 |
237 EXPECT_EQ(0, bytes_written()); | 244 EXPECT_EQ(0, bytes_written()); |
238 EXPECT_EQ(base::PLATFORM_FILE_ERROR_FAILED, status()); | |
239 EXPECT_TRUE(complete()); | |
240 | |
241 EXPECT_EQ(0, change_observer()->get_and_reset_modify_file_count()); | |
242 } | |
243 | |
244 TEST_F(LocalFileSystemOperationWriteTest, TestWriteInvalidFile) { | |
245 GURL blob_url("blob:writeinvalidfile"); | |
246 ScopedTextBlob blob(url_request_context_, blob_url, "It\'ll not be written."); | |
247 | |
248 NewOperation()->Write( | |
249 &url_request_context_, | |
250 URLForPath(base::FilePath(FILE_PATH_LITERAL("nonexist"))), | |
251 blob_url, 0, RecordWriteCallback()); | |
252 MessageLoop::current()->Run(); | |
253 | |
254 EXPECT_EQ(0, bytes_written()); | |
255 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, status()); | 245 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, status()); |
256 EXPECT_TRUE(complete()); | 246 EXPECT_TRUE(complete()); |
257 | 247 |
258 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count()); | 248 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count()); |
259 } | 249 } |
260 | 250 |
261 TEST_F(LocalFileSystemOperationWriteTest, TestWriteDir) { | 251 TEST_F(LocalFileSystemOperationWriteTest, TestWriteDir) { |
262 base::FilePath virtual_dir_path(FILE_PATH_LITERAL("d")); | 252 base::FilePath virtual_dir_path(FILE_PATH_LITERAL("d")); |
263 NewOperation()->CreateDirectory( | 253 NewOperation()->CreateDirectory( |
264 URLForPath(virtual_dir_path), | 254 URLForPath(virtual_dir_path), |
265 true /* exclusive */, false /* recursive */, | 255 true /* exclusive */, false /* recursive */, |
266 base::Bind(&AssertStatusEq, base::PLATFORM_FILE_OK)); | 256 base::Bind(&AssertStatusEq, base::PLATFORM_FILE_OK)); |
267 | 257 ScopedTextBlob blob(url_request_context_, "blob_id:writedir", |
268 GURL blob_url("blob:writedir"); | |
269 ScopedTextBlob blob(url_request_context_, blob_url, | |
270 "It\'ll not be written, too."); | 258 "It\'ll not be written, too."); |
271 | |
272 NewOperation()->Write(&url_request_context_, URLForPath(virtual_dir_path), | 259 NewOperation()->Write(&url_request_context_, URLForPath(virtual_dir_path), |
273 blob_url, 0, RecordWriteCallback()); | 260 blob.GetBlobDataHandle(), 0, RecordWriteCallback()); |
274 MessageLoop::current()->Run(); | 261 MessageLoop::current()->Run(); |
275 | 262 |
276 EXPECT_EQ(0, bytes_written()); | 263 EXPECT_EQ(0, bytes_written()); |
277 // TODO(kinuko): This error code is platform- or fileutil- dependent | 264 // TODO(kinuko): This error code is platform- or fileutil- dependent |
278 // right now. Make it return PLATFORM_FILE_ERROR_NOT_A_FILE in every case. | 265 // right now. Make it return PLATFORM_FILE_ERROR_NOT_A_FILE in every case. |
279 EXPECT_TRUE(status() == base::PLATFORM_FILE_ERROR_NOT_A_FILE || | 266 EXPECT_TRUE(status() == base::PLATFORM_FILE_ERROR_NOT_A_FILE || |
280 status() == base::PLATFORM_FILE_ERROR_ACCESS_DENIED || | 267 status() == base::PLATFORM_FILE_ERROR_ACCESS_DENIED || |
281 status() == base::PLATFORM_FILE_ERROR_FAILED); | 268 status() == base::PLATFORM_FILE_ERROR_FAILED); |
282 EXPECT_TRUE(complete()); | 269 EXPECT_TRUE(complete()); |
283 | 270 |
284 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count()); | 271 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count()); |
285 } | 272 } |
286 | 273 |
287 TEST_F(LocalFileSystemOperationWriteTest, TestWriteFailureByQuota) { | 274 TEST_F(LocalFileSystemOperationWriteTest, TestWriteFailureByQuota) { |
288 GURL blob_url("blob:success"); | 275 ScopedTextBlob blob(url_request_context_, "blob_id:success", |
289 ScopedTextBlob blob(url_request_context_, blob_url, "Hello, world!\n"); | 276 "Hello, world!\n"); |
290 | 277 |
291 quota_manager_->set_quota(10); | 278 quota_manager_->set_quota(10); |
292 NewOperation()->Write( | 279 NewOperation()->Write( |
293 &url_request_context_, URLForPath(virtual_path_), blob_url, | 280 &url_request_context_, URLForPath(virtual_path_), |
| 281 blob.GetBlobDataHandle() |
294 0, RecordWriteCallback()); | 282 0, RecordWriteCallback()); |
295 MessageLoop::current()->Run(); | 283 MessageLoop::current()->Run(); |
296 | 284 |
297 EXPECT_EQ(10, bytes_written()); | 285 EXPECT_EQ(10, bytes_written()); |
298 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, status()); | 286 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, status()); |
299 EXPECT_TRUE(complete()); | 287 EXPECT_TRUE(complete()); |
300 | 288 |
301 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count()); | 289 EXPECT_EQ(1, change_observer()->get_and_reset_modify_file_count()); |
302 } | 290 } |
303 | 291 |
304 TEST_F(LocalFileSystemOperationWriteTest, TestImmediateCancelSuccessfulWrite) { | 292 TEST_F(LocalFileSystemOperationWriteTest, TestImmediateCancelSuccessfulWrite) { |
305 GURL blob_url("blob:success"); | 293 ScopedTextBlob blob(url_request_context_, "blob_id:success", |
306 ScopedTextBlob blob(url_request_context_, blob_url, "Hello, world!\n"); | 294 "Hello, world!\n"); |
307 | 295 |
308 FileSystemOperation* write_operation = NewOperation(); | 296 FileSystemOperation* write_operation = NewOperation(); |
309 write_operation->Write(&url_request_context_, URLForPath(virtual_path_), | 297 write_operation->Write(&url_request_context_, URLForPath(virtual_path_), |
310 blob_url, 0, RecordWriteCallback()); | 298 blob.GetBlobDataHandle(), |
| 299 0, RecordWriteCallback()); |
311 write_operation->Cancel(RecordCancelCallback()); | 300 write_operation->Cancel(RecordCancelCallback()); |
312 // We use RunAllPendings() instead of Run() here, because we won't dispatch | 301 // We use RunAllPendings() instead of Run() here, because we won't dispatch |
313 // callbacks after Cancel() is issued (so no chance to Quit) nor do we need | 302 // callbacks after Cancel() is issued (so no chance to Quit) nor do we need |
314 // to run another write cycle. | 303 // to run another write cycle. |
315 MessageLoop::current()->RunUntilIdle(); | 304 MessageLoop::current()->RunUntilIdle(); |
316 | 305 |
317 // Issued Cancel() before receiving any response from Write(), | 306 // Issued Cancel() before receiving any response from Write(), |
318 // so nothing should have happen. | 307 // so nothing should have happen. |
319 EXPECT_EQ(0, bytes_written()); | 308 EXPECT_EQ(0, bytes_written()); |
320 EXPECT_EQ(base::PLATFORM_FILE_ERROR_ABORT, status()); | 309 EXPECT_EQ(base::PLATFORM_FILE_ERROR_ABORT, status()); |
321 EXPECT_EQ(base::PLATFORM_FILE_OK, cancel_status()); | 310 EXPECT_EQ(base::PLATFORM_FILE_OK, cancel_status()); |
322 EXPECT_TRUE(complete()); | 311 EXPECT_TRUE(complete()); |
323 | 312 |
324 EXPECT_EQ(0, change_observer()->get_and_reset_modify_file_count()); | 313 EXPECT_EQ(0, change_observer()->get_and_reset_modify_file_count()); |
325 } | 314 } |
326 | 315 |
327 TEST_F(LocalFileSystemOperationWriteTest, TestImmediateCancelFailingWrite) { | 316 TEST_F(LocalFileSystemOperationWriteTest, TestImmediateCancelFailingWrite) { |
328 GURL blob_url("blob:writeinvalidfile"); | 317 ScopedTextBlob blob(url_request_context_, "blob_id:writeinvalidfile", |
329 ScopedTextBlob blob(url_request_context_, blob_url, "It\'ll not be written."); | 318 "It\'ll not be written."); |
330 | |
331 FileSystemOperation* write_operation = NewOperation(); | 319 FileSystemOperation* write_operation = NewOperation(); |
332 write_operation->Write( | 320 write_operation->Write( |
333 &url_request_context_, | 321 &url_request_context_, |
334 URLForPath(base::FilePath(FILE_PATH_LITERAL("nonexist"))), | 322 URLForPath(base::FilePath(FILE_PATH_LITERAL("nonexist"))), |
335 blob_url, 0, RecordWriteCallback()); | 323 blob.GetBlobDataHandle(), 0, RecordWriteCallback()); |
336 write_operation->Cancel(RecordCancelCallback()); | 324 write_operation->Cancel(RecordCancelCallback()); |
337 // We use RunAllPendings() instead of Run() here, because we won't dispatch | 325 // We use RunAllPendings() instead of Run() here, because we won't dispatch |
338 // callbacks after Cancel() is issued (so no chance to Quit) nor do we need | 326 // callbacks after Cancel() is issued (so no chance to Quit) nor do we need |
339 // to run another write cycle. | 327 // to run another write cycle. |
340 MessageLoop::current()->RunUntilIdle(); | 328 MessageLoop::current()->RunUntilIdle(); |
341 | 329 |
342 // Issued Cancel() before receiving any response from Write(), | 330 // Issued Cancel() before receiving any response from Write(), |
343 // so nothing should have happen. | 331 // so nothing should have happen. |
344 EXPECT_EQ(0, bytes_written()); | 332 EXPECT_EQ(0, bytes_written()); |
345 EXPECT_EQ(base::PLATFORM_FILE_ERROR_ABORT, status()); | 333 EXPECT_EQ(base::PLATFORM_FILE_ERROR_ABORT, status()); |
346 EXPECT_EQ(base::PLATFORM_FILE_OK, cancel_status()); | 334 EXPECT_EQ(base::PLATFORM_FILE_OK, cancel_status()); |
347 EXPECT_TRUE(complete()); | 335 EXPECT_TRUE(complete()); |
348 | 336 |
349 EXPECT_EQ(0, change_observer()->get_and_reset_modify_file_count()); | 337 EXPECT_EQ(0, change_observer()->get_and_reset_modify_file_count()); |
350 } | 338 } |
351 | 339 |
352 // TODO(ericu,dmikurube,kinuko): Add more tests for cancel cases. | 340 // TODO(ericu,dmikurube,kinuko): Add more tests for cancel cases. |
353 | 341 |
354 } // namespace fileapi | 342 } // namespace fileapi |
OLD | NEW |