tor-browser

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

test_fileSlice.xhtml (3445B)


      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  /** Test for Bug 664783 */
     28 
     29  var fileNum = 0;
     30 
     31  /**
     32   * Create a file which contains the given data and optionally adds the specified file extension.
     33   */
     34  function createFileWithData(fileData, /** optional */ extension) {
     35    var testFile = Cc["@mozilla.org/file/directory_service;1"]
     36                       .getService(Ci.nsIProperties)
     37                       .get("ProfD", Ci.nsIFile);
     38    var fileExtension = (extension == undefined) ? "" : "." + extension;
     39    testFile.append("workerSlice" + fileNum++ + fileExtension);
     40 
     41    var outStream = Cc["@mozilla.org/network/file-output-stream;1"]
     42                        .createInstance(Ci.nsIFileOutputStream);
     43    outStream.init(testFile, 0x02 | 0x08 | 0x20, // write, create, truncate
     44                   0o666, 0);
     45    outStream.write(fileData, fileData.length);
     46    outStream.close();
     47 
     48    var fileList = document.getElementById('fileList');
     49    fileList.value = testFile.path;
     50 
     51    return fileList.files[0];
     52  }
     53 
     54  /**
     55   * Starts a worker which slices the blob to the given start offset and optional end offset and
     56   * content type. It then verifies that the size and type of the sliced blob is correct.
     57   */
     58  function createSlice(blob, start, expectedLength, /** optional */ end, /** optional */ contentType) {
     59    var worker = new Worker("fileSlice_worker.js");
     60 
     61    worker.onerror = function(event) {
     62      ok(false, "Worker had an error: " + event.message);
     63      finish();
     64    };
     65 
     66    worker.onmessage = function(event) {
     67      is(event.data.size, expectedLength, "size property of slice is incorrect.");
     68      is(event.data.type, contentType ? contentType : blob.type, "type property of slice is incorrect.");
     69      finish();
     70    };
     71 
     72    var params = {blob, start, end, contentType};
     73    worker.postMessage(params);
     74    waitForWorkerFinish();
     75  }
     76 
     77  // Empty file.
     78  createSlice(createFileWithData(""), 0, 0, 0);
     79 
     80  // Typical use case.
     81  createSlice(createFileWithData("Hello"), 1, 1, 2);
     82 
     83  // Longish file.
     84  var text = "";
     85  for (var i = 0; i < 10000; ++i) {
     86    text += "long";
     87  }
     88  createSlice(createFileWithData(text), 2000, 2000, 4000);
     89 
     90  // Slice to different type.
     91  createSlice(createFileWithData("text", "txt"), 0, 2, 2, "image/png");
     92 
     93  // Length longer than blob.
     94  createSlice(createFileWithData("text"), 0, 4, 20);
     95 
     96  // Start longer than blob.
     97  createSlice(createFileWithData("text"), 20, 0, 4);
     98 
     99  // No optional arguments
    100  createSlice(createFileWithData("text"), 0, 4);
    101  createSlice(createFileWithData("text"), 2, 2);
    102 
    103  ]]>
    104  </script>
    105 </window>