test_file.xul 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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=123456
  6. -->
  7. <window title="Mozilla Bug 123456"
  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=123456"
  15. target="_blank">Mozilla Bug 123456</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 123456 **/
  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("workerFile" + 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. waitForWorkerFinish();
  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, expectedSize, "size proproperty accessed from worker is not the same as on main thread.");
  56. is(event.data.type, expectedType, "type proproperty accessed from worker is incorrect.");
  57. is(event.data.name, file.name, "name proproperty accessed from worker is incorrect.");
  58. is(event.data.lastModifiedDate.toString(), file.lastModifiedDate.toString(), "lastModifiedDate proproperty accessed from worker is incorrect.");
  59. finish();
  60. };
  61. worker.postMessage(file);
  62. }
  63. // Empty file.
  64. accessFileProperties(createFileWithData(""), 0, "");
  65. // Typical use case.
  66. accessFileProperties(createFileWithData("Hello"), 5, "");
  67. // Longish file.
  68. var text = "";
  69. for (var i = 0; i < 10000; ++i) {
  70. text += "long";
  71. }
  72. accessFileProperties(createFileWithData(text), 40000, "");
  73. // Type detection based on extension.
  74. accessFileProperties(createFileWithData("text", "txt"), 4, "text/plain");
  75. ]]>
  76. </script>
  77. </window>