dom/workers/test/test_fileReadSlice.xul

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

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

mercurial