dom/workers/test/test_fileReadSlice.xul

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     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=664783
     6 -->
     7 <window title="Mozilla Bug 664783"
     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"/>
    13   <!-- test results are displayed in the html:body -->
    14   <body xmlns="http://www.w3.org/1999/xhtml">
    15   <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=664783"
    16      target="_blank">Mozilla Bug 664783</a>
    18   <div id="content" style="display: none">
    19     <input id="fileList" type="file"></input>
    20   </div>
    22   </body>
    24   <!-- test code goes here -->
    25   <script type="application/javascript">
    26   <![CDATA[
    28   if (navigator.platform.startsWith("Win")) {
    29     SimpleTest.expectAssertions(0, 1);
    30   }
    32   /** Test for Bug 664783 **/
    34   var fileNum = 0;
    36   /**
    37    * Create a file which contains the given data.
    38    */
    39   function createFileWithData(fileData) {
    40     var testFile = Components.classes["@mozilla.org/file/directory_service;1"]
    41                        .getService(Components.interfaces.nsIProperties)
    42                        .get("ProfD", Components.interfaces.nsIFile);
    43     testFile.append("workerReadSlice" + fileNum++);
    45     var outStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
    46                         .createInstance(Components.interfaces.nsIFileOutputStream);
    47     outStream.init(testFile, 0x02 | 0x08 | 0x20, // write, create, truncate
    48                    0666, 0);
    49     outStream.write(fileData, fileData.length);
    50     outStream.close();
    52     var fileList = document.getElementById('fileList');
    53     fileList.value = testFile.path;
    55     return fileList.files[0];
    56   }
    58   /**
    59    * Creates a worker which slices a blob to the given start and end offset and
    60    * reads the content as text.
    61    */
    62   function readSlice(blob, start, end, expectedText) {
    63     var worker = new Worker("fileReadSlice_worker.js");
    65     worker.onerror = function(event) {
    66       ok(false, "Worker had an error: " + event.data);
    67       finish();
    68     };
    70     worker.onmessage = function(event) {
    71       is(event.data, expectedText, "Text from sliced blob in worker is incorrect.");
    72       finish();
    73     };
    75     var params = {blob: blob, start: start, end: end};
    76     worker.postMessage(params);
    77     waitForWorkerFinish();
    78   }
    80   // Empty file.
    81   readSlice(createFileWithData(""), 0, 0, "");
    83   // Typical use case.
    84   readSlice(createFileWithData("HelloBye"), 5, 8, "Bye");
    86   // End offset too large.
    87   readSlice(createFileWithData("HelloBye"), 5, 9, "Bye");
    89   // Start of file.
    90   readSlice(createFileWithData("HelloBye"), 0, 5, "Hello");
    92   ]]>
    93   </script>
    94 </window>

mercurial