tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_fileReadSlice.xhtml (2756B)


      1 <?xml version="1.0"?>
      2 <?xml-stylesheet type="text/css" href="chrome://global/skin"?>
      3 <?xml-stylesheet type="text/css" href="chrome://mochikit/content/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 src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
     10  <script type="application/javascript" src="dom_worker_helper.js"/>
     11 
     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 
     17  <div id="content" style="display: none">
     18    <input id="fileList" type="file"></input>
     19  </div>
     20 
     21  </body>
     22 
     23  <!-- test code goes here -->
     24  <script type="application/javascript">
     25  <![CDATA[
     26 
     27  if (navigator.platform.startsWith("Win")) {
     28    SimpleTest.expectAssertions(0, 1);
     29  }
     30 
     31  /** Test for Bug 664783 */
     32 
     33  var fileNum = 0;
     34 
     35  /**
     36   * Create a file which contains the given data.
     37   */
     38  function createFileWithData(fileData) {
     39    var testFile = Cc["@mozilla.org/file/directory_service;1"]
     40                       .getService(Ci.nsIProperties)
     41                       .get("ProfD", Ci.nsIFile);
     42    testFile.append("workerReadSlice" + fileNum++);
     43 
     44    var outStream = Cc["@mozilla.org/network/file-output-stream;1"]
     45                        .createInstance(Ci.nsIFileOutputStream);
     46    outStream.init(testFile, 0x02 | 0x08 | 0x20, // write, create, truncate
     47                   0o666, 0);
     48    outStream.write(fileData, fileData.length);
     49    outStream.close();
     50 
     51    var fileList = document.getElementById('fileList');
     52    fileList.value = testFile.path;
     53 
     54    return fileList.files[0];
     55  }
     56 
     57  /**
     58   * Creates a worker which slices a blob to the given start and end offset and
     59   * reads the content as text.
     60   */
     61  function readSlice(blob, start, end, expectedText) {
     62    var worker = new Worker("fileReadSlice_worker.js");
     63 
     64    worker.onerror = function(event) {
     65      ok(false, "Worker had an error: " + event.message);
     66      finish();
     67    };
     68 
     69    worker.onmessage = function(event) {
     70      is(event.data, expectedText, "Text from sliced blob in worker is incorrect.");
     71      finish();
     72    };
     73 
     74    var params = {blob, start, end};
     75    worker.postMessage(params);
     76    waitForWorkerFinish();
     77  }
     78 
     79  // Empty file.
     80  readSlice(createFileWithData(""), 0, 0, "");
     81 
     82  // Typical use case.
     83  readSlice(createFileWithData("HelloBye"), 5, 8, "Bye");
     84 
     85  // End offset too large.
     86  readSlice(createFileWithData("HelloBye"), 5, 9, "Bye");
     87 
     88  // Start of file.
     89  readSlice(createFileWithData("HelloBye"), 0, 5, "Hello");
     90 
     91  ]]>
     92  </script>
     93 </window>