dom/workers/test/test_file.xul

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/workers/test/test_file.xul	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,99 @@
     1.4 +<?xml version="1.0"?>
     1.5 +<?xml-stylesheet type="text/css" href="chrome://global/skin"?>
     1.6 +<?xml-stylesheet type="text/css" href="/tests/SimpleTest/test.css"?>
     1.7 +<!--
     1.8 +https://bugzilla.mozilla.org/show_bug.cgi?id=123456
     1.9 +-->
    1.10 +<window title="Mozilla Bug 123456"
    1.11 +        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
    1.12 +  <script type="application/javascript"
    1.13 +          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
    1.14 +  <script type="application/javascript" src="dom_worker_helper.js"/>
    1.15 +
    1.16 +  <!-- test results are displayed in the html:body -->
    1.17 +  <body xmlns="http://www.w3.org/1999/xhtml">
    1.18 +  <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=123456"
    1.19 +     target="_blank">Mozilla Bug 123456</a>
    1.20 +
    1.21 +  <div id="content" style="display: none">
    1.22 +    <input id="fileList" type="file"></input>
    1.23 +  </div>
    1.24 +
    1.25 +  </body>
    1.26 +
    1.27 +  <!-- test code goes here -->
    1.28 +  <script type="application/javascript">
    1.29 +  <![CDATA[
    1.30 +
    1.31 +  /** Test for Bug 123456 **/
    1.32 +
    1.33 +  var fileNum = 0;
    1.34 +
    1.35 +  /**
    1.36 +   * Create a file which contains the given data and optionally adds the specified file extension.
    1.37 +   */
    1.38 +  function createFileWithData(fileData, /** optional */ extension) {
    1.39 +    var testFile = Components.classes["@mozilla.org/file/directory_service;1"]
    1.40 +                       .getService(Components.interfaces.nsIProperties)
    1.41 +                       .get("ProfD", Components.interfaces.nsIFile);
    1.42 +    var fileExtension = (extension == undefined) ? "" : "." + extension;
    1.43 +    testFile.append("workerFile" + fileNum++ + fileExtension);
    1.44 +
    1.45 +    var outStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
    1.46 +                        .createInstance(Components.interfaces.nsIFileOutputStream);
    1.47 +    outStream.init(testFile, 0x02 | 0x08 | 0x20, // write, create, truncate
    1.48 +                   0666, 0);
    1.49 +    outStream.write(fileData, fileData.length);
    1.50 +    outStream.close();
    1.51 +
    1.52 +    var fileList = document.getElementById('fileList');
    1.53 +    fileList.value = testFile.path;
    1.54 +
    1.55 +    return fileList.files[0];
    1.56 +  }
    1.57 +
    1.58 +  /**
    1.59 +   * Create a worker to access file properties.
    1.60 +   */
    1.61 +  function accessFileProperties(file, expectedSize, expectedType) {
    1.62 +    waitForWorkerFinish();
    1.63 +
    1.64 +    var worker = new Worker("file_worker.js");
    1.65 +
    1.66 +    worker.onerror = function(event) {
    1.67 +      ok(false, "Worker had an error: " + event.data);
    1.68 +      finish();
    1.69 +    };
    1.70 +
    1.71 +    worker.onmessage = function(event) {
    1.72 +      is(event.data.size, expectedSize, "size proproperty accessed from worker is not the same as on main thread.");
    1.73 +      is(event.data.type, expectedType, "type proproperty accessed from worker is incorrect.");
    1.74 +      is(event.data.name, file.name, "name proproperty accessed from worker is incorrect.");
    1.75 +      is(event.data.path, file.path, "path proproperty accessed from worker is incorrect.");
    1.76 +      is(event.data.lastModifiedDate.toString(), file.lastModifiedDate.toString(), "lastModifiedDate proproperty accessed from worker is incorrect.");
    1.77 +      is(event.data.mozFullPath, file.mozFullPath, "mozFullPath proproperty accessed from worker is not the same as on main thread.");
    1.78 +      finish();
    1.79 +    };
    1.80 +
    1.81 +    worker.postMessage(file);
    1.82 +  }
    1.83 +
    1.84 +  // Empty file.
    1.85 +  accessFileProperties(createFileWithData(""), 0, "");
    1.86 +
    1.87 +  // Typical use case.
    1.88 +  accessFileProperties(createFileWithData("Hello"), 5, "");
    1.89 +
    1.90 +  // Longish file.
    1.91 +  var text = "";
    1.92 +  for (var i = 0; i < 10000; ++i) {
    1.93 +    text += "long";
    1.94 +  }
    1.95 +  accessFileProperties(createFileWithData(text), 40000, "");
    1.96 +
    1.97 +  // Type detection based on extension.
    1.98 +  accessFileProperties(createFileWithData("text", "txt"), 4, "text/plain");
    1.99 +
   1.100 +  ]]>
   1.101 +  </script>
   1.102 +</window>

mercurial