test_filePosting.xul 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. /** Test for Bug 664783 **/
  24. var fileNum = 0;
  25. /**
  26. * Create a file which contains the given data.
  27. */
  28. function createFileWithData(fileData) {
  29. var testFile = Components.classes["@mozilla.org/file/directory_service;1"]
  30. .getService(Components.interfaces.nsIProperties)
  31. .get("ProfD", Components.interfaces.nsIFile);
  32. testFile.append("workerFilePosting" + fileNum++);
  33. var outStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
  34. .createInstance(Components.interfaces.nsIFileOutputStream);
  35. outStream.init(testFile, 0x02 | 0x08 | 0x20, // write, create, truncate
  36. 0666, 0);
  37. outStream.write(fileData, fileData.length);
  38. outStream.close();
  39. var fileList = document.getElementById('fileList');
  40. fileList.value = testFile.path;
  41. return fileList.files[0];
  42. }
  43. /**
  44. * Create a worker which posts the same file given. Used to test cloning of files.
  45. * Checks the size, type, name and path of the file posted from the worker to ensure it
  46. * is the same as the original.
  47. */
  48. function postFile(file) {
  49. var worker = new Worker("file_worker.js");
  50. worker.onerror = function(event) {
  51. ok(false, "Worker had an error: " + event.message);
  52. finish();
  53. };
  54. worker.onmessage = function(event) {
  55. is(event.data.size, file.size, "size of file posted from worker does not match file posted to worker.");
  56. is(event.data.type, file.type, "type of file posted from worker does not match file posted to worker.");
  57. is(event.data.name, file.name, "name of file posted from worker does not match file posted to worker.");
  58. finish();
  59. };
  60. worker.postMessage(file);
  61. waitForWorkerFinish();
  62. }
  63. // Empty file.
  64. postFile(createFileWithData(""));
  65. // Typical use case.
  66. postFile(createFileWithData("Hello"));
  67. ]]>
  68. </script>
  69. </window>