test_fileReadSlice.xul 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?xml version="1.0"?>
  2. <?xml-stylesheet type="text/css" href="chrome://global/skin"?>
  3. <?xml-stylesheet type="text/css" href="/tests/SimpleTest/test.css"?>
  4. <!--
  5. https://bugzilla.mozilla.org/show_bug.cgi?id=664783
  6. -->
  7. <window title="Mozilla Bug 664783"
  8. xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
  9. <script type="application/javascript"
  10. src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
  11. <script type="application/javascript" src="dom_worker_helper.js"/>
  12. <!-- test results are displayed in the html:body -->
  13. <body xmlns="http://www.w3.org/1999/xhtml">
  14. <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=664783"
  15. target="_blank">Mozilla Bug 664783</a>
  16. <div id="content" style="display: none">
  17. <input id="fileList" type="file"></input>
  18. </div>
  19. </body>
  20. <!-- test code goes here -->
  21. <script type="application/javascript">
  22. <![CDATA[
  23. if (navigator.platform.startsWith("Win")) {
  24. SimpleTest.expectAssertions(0, 1);
  25. }
  26. /** Test for Bug 664783 **/
  27. var fileNum = 0;
  28. /**
  29. * Create a file which contains the given data.
  30. */
  31. function createFileWithData(fileData) {
  32. var testFile = Components.classes["@mozilla.org/file/directory_service;1"]
  33. .getService(Components.interfaces.nsIProperties)
  34. .get("ProfD", Components.interfaces.nsIFile);
  35. testFile.append("workerReadSlice" + fileNum++);
  36. var outStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
  37. .createInstance(Components.interfaces.nsIFileOutputStream);
  38. outStream.init(testFile, 0x02 | 0x08 | 0x20, // write, create, truncate
  39. 0666, 0);
  40. outStream.write(fileData, fileData.length);
  41. outStream.close();
  42. var fileList = document.getElementById('fileList');
  43. fileList.value = testFile.path;
  44. return fileList.files[0];
  45. }
  46. /**
  47. * Creates a worker which slices a blob to the given start and end offset and
  48. * reads the content as text.
  49. */
  50. function readSlice(blob, start, end, expectedText) {
  51. var worker = new Worker("fileReadSlice_worker.js");
  52. worker.onerror = function(event) {
  53. ok(false, "Worker had an error: " + event.message);
  54. finish();
  55. };
  56. worker.onmessage = function(event) {
  57. is(event.data, expectedText, "Text from sliced blob in worker is incorrect.");
  58. finish();
  59. };
  60. var params = {blob: blob, start: start, end: end};
  61. worker.postMessage(params);
  62. waitForWorkerFinish();
  63. }
  64. // Empty file.
  65. readSlice(createFileWithData(""), 0, 0, "");
  66. // Typical use case.
  67. readSlice(createFileWithData("HelloBye"), 5, 8, "Bye");
  68. // End offset too large.
  69. readSlice(createFileWithData("HelloBye"), 5, 9, "Bye");
  70. // Start of file.
  71. readSlice(createFileWithData("HelloBye"), 0, 5, "Hello");
  72. ]]>
  73. </script>
  74. </window>