test_filehandle_progress_events.html 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <!--
  2. Any copyright is dedicated to the Public Domain.
  3. http://creativecommons.org/publicdomain/zero/1.0/
  4. -->
  5. <html>
  6. <head>
  7. <title>File Handle Test</title>
  8. <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  9. <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  10. <script type="text/javascript;version=1.7">
  11. function testSteps()
  12. {
  13. const name = window.location.pathname;
  14. var testBuffer = getRandomBuffer(100000);
  15. let request = indexedDB.open(name, 1);
  16. request.onerror = errorHandler;
  17. request.onsuccess = grabEventAndContinueHandler;
  18. let event = yield undefined;
  19. let db = event.target.result;
  20. db.onerror = errorHandler;
  21. request = db.createMutableFile("test.txt");
  22. request.onerror = errorHandler;
  23. request.onsuccess = grabEventAndContinueHandler;
  24. event = yield undefined;
  25. let mutableFile = event.target.result;
  26. mutableFile.onerror = errorHandler;
  27. let fileHandle = mutableFile.open("readwrite");
  28. let sum = 0;
  29. request = fileHandle.write(testBuffer);
  30. request.onprogress = function(event) {
  31. let loaded = event.loaded;
  32. let total = event.total;
  33. ok(loaded >= 0 && loaded <= total, "Correct loaded progress");
  34. is(total, testBuffer.byteLength, "Correct total progress");
  35. sum += event.loaded - sum;
  36. }
  37. request.onsuccess = grabEventAndContinueHandler;
  38. event = yield undefined;
  39. is(sum, testBuffer.byteLength, "Correct loaded progress sum");
  40. sum = 0;
  41. fileHandle.location = 0;
  42. request = fileHandle.readAsArrayBuffer(testBuffer.byteLength);
  43. request.onprogress = function(event) {
  44. let loaded = event.loaded;
  45. let total = event.total;
  46. ok(loaded >= 0 && loaded <= total, "Correct loaded progress");
  47. is(total, testBuffer.byteLength, "Correct total progress");
  48. sum += event.loaded - sum;
  49. }
  50. request.onsuccess = grabEventAndContinueHandler;
  51. event = yield undefined;
  52. is(sum, testBuffer.byteLength, "Correct loaded progress sum");
  53. finishTest();
  54. yield undefined;
  55. }
  56. </script>
  57. <script type="text/javascript;version=1.7" src="file.js"></script>
  58. <script type="text/javascript;version=1.7" src="helpers.js"></script>
  59. </head>
  60. <body onload="runTest();"></body>
  61. </html>