test_fileSubWorker.xul 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 and optionally adds the specified file extension.
  27. */
  28. function createFileWithData(fileData, /** optional */ extension) {
  29. var testFile = Components.classes["@mozilla.org/file/directory_service;1"]
  30. .getService(Components.interfaces.nsIProperties)
  31. .get("ProfD", Components.interfaces.nsIFile);
  32. var fileExtension = (extension == undefined) ? "" : "." + extension;
  33. testFile.append("workerSubWorker" + fileNum++ + fileExtension);
  34. var outStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
  35. .createInstance(Components.interfaces.nsIFileOutputStream);
  36. outStream.init(testFile, 0x02 | 0x08 | 0x20, // write, create, truncate
  37. 0666, 0);
  38. outStream.write(fileData, fileData.length);
  39. outStream.close();
  40. var fileList = document.getElementById('fileList');
  41. fileList.value = testFile.path;
  42. return fileList.files[0];
  43. }
  44. /**
  45. * Create a worker to access file properties.
  46. */
  47. function accessFileProperties(file, expectedSize, expectedType) {
  48. var worker = new Worker("fileSubWorker_worker.js");
  49. worker.onerror = function(event) {
  50. ok(false, "Worker had an error: " + event.message);
  51. finish();
  52. };
  53. worker.onmessage = function(event) {
  54. if (event.data == undefined) {
  55. ok(false, "Worker had an error.");
  56. } else {
  57. is(event.data.size, expectedSize, "size proproperty accessed from worker is not the same as on main thread.");
  58. is(event.data.type, expectedType, "type proproperty accessed from worker is incorrect.");
  59. is(event.data.name, file.name, "name proproperty accessed from worker is incorrect.");
  60. }
  61. finish();
  62. };
  63. worker.postMessage(file);
  64. waitForWorkerFinish();
  65. }
  66. // Empty file.
  67. accessFileProperties(createFileWithData(""), 0, "");
  68. // Typical use case.
  69. accessFileProperties(createFileWithData("Hello"), 5, "");
  70. // Longish file.
  71. var text = "";
  72. for (var i = 0; i < 10000; ++i) {
  73. text += "long";
  74. }
  75. accessFileProperties(createFileWithData(text), 40000, "");
  76. // Type detection based on extension.
  77. accessFileProperties(createFileWithData("text", "txt"), 4, "text/plain");
  78. ]]>
  79. </script>
  80. </window>