OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "ppapi/tests/test_file_io.h" | 5 #include "ppapi/tests/test_file_io.h" |
6 | 6 |
7 #include <stdio.h> | 7 #include <stdio.h> |
8 #include <string.h> | |
8 | 9 |
9 #include "ppapi/c/pp_errors.h" | 10 #include "ppapi/c/pp_errors.h" |
10 #include "ppapi/c/dev/ppb_file_io_dev.h" | 11 #include "ppapi/c/dev/ppb_file_io_dev.h" |
11 #include "ppapi/c/dev/ppb_testing_dev.h" | 12 #include "ppapi/c/dev/ppb_testing_dev.h" |
12 #include "ppapi/cpp/dev/file_io_dev.h" | 13 #include "ppapi/cpp/dev/file_io_dev.h" |
13 #include "ppapi/cpp/dev/file_ref_dev.h" | 14 #include "ppapi/cpp/dev/file_ref_dev.h" |
14 #include "ppapi/cpp/dev/file_system_dev.h" | 15 #include "ppapi/cpp/dev/file_system_dev.h" |
15 #include "ppapi/cpp/instance.h" | 16 #include "ppapi/cpp/instance.h" |
16 #include "ppapi/cpp/module.h" | 17 #include "ppapi/cpp/module.h" |
17 #include "ppapi/tests/test_utils.h" | 18 #include "ppapi/tests/test_utils.h" |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
76 } // namespace | 77 } // namespace |
77 | 78 |
78 bool TestFileIO::Init() { | 79 bool TestFileIO::Init() { |
79 return InitTestingInterface() && EnsureRunningOverHTTP(); | 80 return InitTestingInterface() && EnsureRunningOverHTTP(); |
80 } | 81 } |
81 | 82 |
82 void TestFileIO::RunTest() { | 83 void TestFileIO::RunTest() { |
83 RUN_TEST(Open); | 84 RUN_TEST(Open); |
84 RUN_TEST(ReadWriteSetLength); | 85 RUN_TEST(ReadWriteSetLength); |
85 RUN_TEST(TouchQuery); | 86 RUN_TEST(TouchQuery); |
87 RUN_TEST(AbortCalls); | |
88 // TODO(viettrungluu): add tests: | |
89 // - that PP_ERROR_PENDING is correctly returned | |
90 // - that operations respect the file open modes (flags) | |
86 } | 91 } |
87 | 92 |
88 std::string TestFileIO::TestOpen() { | 93 std::string TestFileIO::TestOpen() { |
89 TestCompletionCallback callback; | 94 TestCompletionCallback callback; |
90 | 95 |
91 pp::FileSystem_Dev file_system(instance_, PP_FILESYSTEMTYPE_LOCALTEMPORARY); | 96 pp::FileSystem_Dev file_system(instance_, PP_FILESYSTEMTYPE_LOCALTEMPORARY); |
92 pp::FileRef_Dev file_ref(file_system, "/file_open"); | 97 pp::FileRef_Dev file_ref(file_system, "/file_open"); |
93 int32_t rv = file_system.Open(1024, callback); | 98 int32_t rv = file_system.Open(1024, callback); |
94 if (rv == PP_ERROR_WOULDBLOCK) | 99 if (rv == PP_ERROR_WOULDBLOCK) |
95 rv = callback.WaitForResult(); | 100 rv = callback.WaitForResult(); |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
274 if (rv != PP_OK) | 279 if (rv != PP_OK) |
275 return ReportError("FileSystem::Query", rv); | 280 return ReportError("FileSystem::Query", rv); |
276 | 281 |
277 if ((info.size != 4) || | 282 if ((info.size != 4) || |
278 (info.type != PP_FILETYPE_REGULAR) || | 283 (info.type != PP_FILETYPE_REGULAR) || |
279 (info.system_type != PP_FILESYSTEMTYPE_LOCALTEMPORARY) || | 284 (info.system_type != PP_FILESYSTEMTYPE_LOCALTEMPORARY) || |
280 (info.last_access_time != last_access_time) || | 285 (info.last_access_time != last_access_time) || |
281 (info.last_modified_time != last_modified_time)) | 286 (info.last_modified_time != last_modified_time)) |
282 return "FileSystem::Query() has returned bad data."; | 287 return "FileSystem::Query() has returned bad data."; |
283 | 288 |
289 // Call |Query()| again, to make sure it works a second time. | |
290 rv = file_io.Query(&info, callback); | |
291 if (rv == PP_ERROR_WOULDBLOCK) | |
292 rv = callback.WaitForResult(); | |
293 if (rv != PP_OK) | |
294 return ReportError("FileSystem::Query", rv); | |
295 | |
284 PASS(); | 296 PASS(); |
285 } | 297 } |
298 | |
299 std::string TestFileIO::TestAbortCalls() { | |
brettw
2011/01/11 21:15:41
nice!
| |
300 TestCompletionCallback callback; | |
301 | |
302 pp::FileSystem_Dev file_system(instance_, PP_FILESYSTEMTYPE_LOCALTEMPORARY); | |
303 pp::FileRef_Dev file_ref(file_system, "/file_abort_calls"); | |
304 int32_t rv = file_system.Open(1024, callback); | |
305 if (rv == PP_ERROR_WOULDBLOCK) | |
306 rv = callback.WaitForResult(); | |
307 if (rv != PP_OK) | |
308 return ReportError("FileSystem::Open", rv); | |
309 | |
310 // First, create a file which to do ops on. | |
311 { | |
312 pp::FileIO_Dev file_io; | |
313 rv = file_io.Open(file_ref, | |
314 PP_FILEOPENFLAG_CREATE | PP_FILEOPENFLAG_WRITE, | |
315 callback); | |
316 if (rv == PP_ERROR_WOULDBLOCK) | |
317 rv = callback.WaitForResult(); | |
318 if (rv != PP_OK) | |
319 return ReportError("FileIO::Open", rv); | |
320 | |
321 // N.B.: Should write at least 3 bytes. | |
322 rv = WriteEntireBuffer(&file_io, 0, "foobarbazquux"); | |
323 if (rv != PP_OK) | |
324 return ReportError("FileIO::Write", rv); | |
325 } | |
326 | |
327 // Abort |Open()|. | |
328 { | |
329 callback.reset_run_count(); | |
330 rv = pp::FileIO_Dev().Open(file_ref, PP_FILEOPENFLAG_READ, callback); | |
331 if (callback.run_count() > 0) | |
332 return "FileIO::Open ran callback synchronously."; | |
333 if (rv == PP_ERROR_WOULDBLOCK) { | |
334 rv = callback.WaitForResult(); | |
335 if (rv != PP_ERROR_ABORTED) | |
336 return "FileIO::Open not aborted."; | |
337 } else if (rv != PP_OK) { | |
338 return ReportError("FileIO::Open", rv); | |
339 } | |
340 } | |
341 | |
342 // Abort |Query()|. | |
343 { | |
344 PP_FileInfo_Dev info = { 0 }; | |
345 { | |
346 pp::FileIO_Dev file_io; | |
347 rv = file_io.Open(file_ref, PP_FILEOPENFLAG_READ, callback); | |
348 if (rv == PP_ERROR_WOULDBLOCK) | |
349 rv = callback.WaitForResult(); | |
350 if (rv != PP_OK) | |
351 return ReportError("FileIO::Open", rv); | |
352 | |
353 callback.reset_run_count(); | |
354 rv = file_io.Query(&info, callback); | |
355 } // Destroy |file_io|. | |
356 if (rv == PP_ERROR_WOULDBLOCK) { | |
357 // Save a copy and make sure |info| doesn't get written to. | |
358 PP_FileInfo_Dev info_copy; | |
359 memcpy(&info_copy, &info, sizeof(info)); | |
360 rv = callback.WaitForResult(); | |
361 if (rv != PP_ERROR_ABORTED) | |
362 return "FileIO::Query not aborted."; | |
363 if (memcmp(&info_copy, &info, sizeof(info)) != 0) | |
364 return "FileIO::Query wrote data after resource destruction."; | |
365 } else if (rv != PP_OK) { | |
366 return ReportError("FileIO::Query", rv); | |
367 } | |
368 } | |
369 | |
370 // Abort |Touch()|. | |
371 { | |
372 { | |
373 pp::FileIO_Dev file_io; | |
374 rv = file_io.Open(file_ref, PP_FILEOPENFLAG_WRITE, callback); | |
375 if (rv == PP_ERROR_WOULDBLOCK) | |
376 rv = callback.WaitForResult(); | |
377 if (rv != PP_OK) | |
378 return ReportError("FileIO::Open", rv); | |
379 | |
380 callback.reset_run_count(); | |
381 rv = file_io.Touch(0, 0, callback); | |
382 } // Destroy |file_io|. | |
383 if (rv == PP_ERROR_WOULDBLOCK) { | |
384 rv = callback.WaitForResult(); | |
385 if (rv != PP_ERROR_ABORTED) | |
386 return "FileIO::Touch not aborted."; | |
387 } else if (rv != PP_OK) { | |
388 return ReportError("FileIO::Touch", rv); | |
389 } | |
390 } | |
391 | |
392 // Abort |Read()|. | |
393 { | |
394 char buf[3] = { 0 }; | |
395 { | |
396 pp::FileIO_Dev file_io; | |
397 rv = file_io.Open(file_ref, PP_FILEOPENFLAG_READ, callback); | |
398 if (rv == PP_ERROR_WOULDBLOCK) | |
399 rv = callback.WaitForResult(); | |
400 if (rv != PP_OK) | |
401 return ReportError("FileIO::Open", rv); | |
402 | |
403 callback.reset_run_count(); | |
404 rv = file_io.Read(0, buf, sizeof(buf), callback); | |
405 } // Destroy |file_io|. | |
406 if (rv == PP_ERROR_WOULDBLOCK) { | |
407 // Save a copy and make sure |buf| doesn't get written to. | |
408 char buf_copy[3]; | |
409 memcpy(&buf_copy, &buf, sizeof(buf)); | |
410 rv = callback.WaitForResult(); | |
411 if (rv != PP_ERROR_ABORTED) | |
412 return "FileIO::Read not aborted."; | |
413 if (memcmp(&buf_copy, &buf, sizeof(buf)) != 0) | |
414 return "FileIO::Read wrote data after resource destruction."; | |
415 } else if (rv != PP_OK) { | |
416 return ReportError("FileIO::Read", rv); | |
417 } | |
418 } | |
419 | |
420 // Abort |Write()|. | |
421 { | |
422 char buf[3] = { 0 }; | |
423 { | |
424 pp::FileIO_Dev file_io; | |
425 rv = file_io.Open(file_ref, PP_FILEOPENFLAG_READ, callback); | |
426 if (rv == PP_ERROR_WOULDBLOCK) | |
427 rv = callback.WaitForResult(); | |
428 if (rv != PP_OK) | |
429 return ReportError("FileIO::Open", rv); | |
430 | |
431 callback.reset_run_count(); | |
432 rv = file_io.Write(0, buf, sizeof(buf), callback); | |
433 } // Destroy |file_io|. | |
434 if (rv == PP_ERROR_WOULDBLOCK) { | |
435 rv = callback.WaitForResult(); | |
436 if (rv != PP_ERROR_ABORTED) | |
437 return "FileIO::Write not aborted."; | |
438 } else if (rv != PP_OK) { | |
439 return ReportError("FileIO::Write", rv); | |
440 } | |
441 } | |
442 | |
443 // Abort |SetLength()|. | |
444 { | |
445 { | |
446 pp::FileIO_Dev file_io; | |
447 rv = file_io.Open(file_ref, PP_FILEOPENFLAG_READ, callback); | |
448 if (rv == PP_ERROR_WOULDBLOCK) | |
449 rv = callback.WaitForResult(); | |
450 if (rv != PP_OK) | |
451 return ReportError("FileIO::Open", rv); | |
452 | |
453 callback.reset_run_count(); | |
454 rv = file_io.SetLength(3, callback); | |
455 } // Destroy |file_io|. | |
456 if (rv == PP_ERROR_WOULDBLOCK) { | |
457 rv = callback.WaitForResult(); | |
458 if (rv != PP_ERROR_ABORTED) | |
459 return "FileIO::SetLength not aborted."; | |
460 } else if (rv != PP_OK) { | |
461 return ReportError("FileIO::SetLength", rv); | |
462 } | |
463 } | |
464 | |
465 // Abort |Flush()|. | |
466 { | |
467 { | |
468 pp::FileIO_Dev file_io; | |
469 rv = file_io.Open(file_ref, PP_FILEOPENFLAG_READ, callback); | |
470 if (rv == PP_ERROR_WOULDBLOCK) | |
471 rv = callback.WaitForResult(); | |
472 if (rv != PP_OK) | |
473 return ReportError("FileIO::Open", rv); | |
474 | |
475 callback.reset_run_count(); | |
476 rv = file_io.Flush(callback); | |
477 } // Destroy |file_io|. | |
478 if (rv == PP_ERROR_WOULDBLOCK) { | |
479 rv = callback.WaitForResult(); | |
480 if (rv != PP_ERROR_ABORTED) | |
481 return "FileIO::Flush not aborted."; | |
482 } else if (rv != PP_OK) { | |
483 return ReportError("FileIO::Flush", rv); | |
484 } | |
485 } | |
486 | |
487 PASS(); | |
488 } | |
OLD | NEW |