dom/workers/test/test_fileReadSlice.xul

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/workers/test/test_fileReadSlice.xul	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,94 @@
     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=664783
     1.9 +-->
    1.10 +<window title="Mozilla Bug 664783"
    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=664783"
    1.19 +     target="_blank">Mozilla Bug 664783</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 +  if (navigator.platform.startsWith("Win")) {
    1.32 +    SimpleTest.expectAssertions(0, 1);
    1.33 +  }
    1.34 +
    1.35 +  /** Test for Bug 664783 **/
    1.36 +
    1.37 +  var fileNum = 0;
    1.38 +
    1.39 +  /**
    1.40 +   * Create a file which contains the given data.
    1.41 +   */
    1.42 +  function createFileWithData(fileData) {
    1.43 +    var testFile = Components.classes["@mozilla.org/file/directory_service;1"]
    1.44 +                       .getService(Components.interfaces.nsIProperties)
    1.45 +                       .get("ProfD", Components.interfaces.nsIFile);
    1.46 +    testFile.append("workerReadSlice" + fileNum++);
    1.47 +
    1.48 +    var outStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
    1.49 +                        .createInstance(Components.interfaces.nsIFileOutputStream);
    1.50 +    outStream.init(testFile, 0x02 | 0x08 | 0x20, // write, create, truncate
    1.51 +                   0666, 0);
    1.52 +    outStream.write(fileData, fileData.length);
    1.53 +    outStream.close();
    1.54 +
    1.55 +    var fileList = document.getElementById('fileList');
    1.56 +    fileList.value = testFile.path;
    1.57 +
    1.58 +    return fileList.files[0];
    1.59 +  }
    1.60 +
    1.61 +  /**
    1.62 +   * Creates a worker which slices a blob to the given start and end offset and
    1.63 +   * reads the content as text.
    1.64 +   */
    1.65 +  function readSlice(blob, start, end, expectedText) {
    1.66 +    var worker = new Worker("fileReadSlice_worker.js");
    1.67 +
    1.68 +    worker.onerror = function(event) {
    1.69 +      ok(false, "Worker had an error: " + event.data);
    1.70 +      finish();
    1.71 +    };
    1.72 +
    1.73 +    worker.onmessage = function(event) {
    1.74 +      is(event.data, expectedText, "Text from sliced blob in worker is incorrect.");
    1.75 +      finish();
    1.76 +    };
    1.77 +
    1.78 +    var params = {blob: blob, start: start, end: end};
    1.79 +    worker.postMessage(params);
    1.80 +    waitForWorkerFinish();
    1.81 +  }
    1.82 +
    1.83 +  // Empty file.
    1.84 +  readSlice(createFileWithData(""), 0, 0, "");
    1.85 +
    1.86 +  // Typical use case.
    1.87 +  readSlice(createFileWithData("HelloBye"), 5, 8, "Bye");
    1.88 +
    1.89 +  // End offset too large.
    1.90 +  readSlice(createFileWithData("HelloBye"), 5, 9, "Bye");
    1.91 +
    1.92 +  // Start of file.
    1.93 +  readSlice(createFileWithData("HelloBye"), 0, 5, "Hello");
    1.94 +
    1.95 +  ]]>
    1.96 +  </script>
    1.97 +</window>

mercurial