dom/workers/test/test_fileReaderSync.xul

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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 /** Test for Bug 664783 **/
michael@0 29
michael@0 30 var fileNum = 0;
michael@0 31
michael@0 32 /**
michael@0 33 * Create a file which contains the given data and optionally adds the specified file extension.
michael@0 34 */
michael@0 35 function createFileWithData(fileData, /** optional */ extension) {
michael@0 36 var testFile = Components.classes["@mozilla.org/file/directory_service;1"]
michael@0 37 .getService(Components.interfaces.nsIProperties)
michael@0 38 .get("ProfD", Components.interfaces.nsIFile);
michael@0 39 var fileExtension = (extension == undefined) ? "" : "." + extension;
michael@0 40 testFile.append("workerFileReaderSync" + fileNum++ + fileExtension);
michael@0 41
michael@0 42 var outStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
michael@0 43 .createInstance(Components.interfaces.nsIFileOutputStream);
michael@0 44 outStream.init(testFile, 0x02 | 0x08 | 0x20, // write, create, truncate
michael@0 45 0666, 0);
michael@0 46 outStream.write(fileData, fileData.length);
michael@0 47 outStream.close();
michael@0 48
michael@0 49 var fileList = document.getElementById('fileList');
michael@0 50 fileList.value = testFile.path;
michael@0 51
michael@0 52 return fileList.files[0];
michael@0 53 }
michael@0 54
michael@0 55 function convertToUTF16(s) {
michael@0 56 res = "";
michael@0 57 for (var i = 0; i < s.length; ++i) {
michael@0 58 c = s.charCodeAt(i);
michael@0 59 res += String.fromCharCode(c & 255, c >>> 8);
michael@0 60 }
michael@0 61 return res;
michael@0 62 }
michael@0 63
michael@0 64 /**
michael@0 65 * Converts the given string to a data URL of the specified mime type.
michael@0 66 */
michael@0 67 function convertToDataURL(mime, s) {
michael@0 68 return "data:" + mime + ";base64," + btoa(s);
michael@0 69 }
michael@0 70
michael@0 71 /**
michael@0 72 * Create a worker to read a file containing fileData using FileReaderSync and
michael@0 73 * checks the return type against the expected type. Optionally set an encoding
michael@0 74 * for reading the file as text.
michael@0 75 */
michael@0 76 function readFileData(fileData, expectedText, /** optional */ encoding) {
michael@0 77 var worker = new Worker("fileReaderSync_worker.js");
michael@0 78
michael@0 79 worker.onmessage = function(event) {
michael@0 80 is(event.data.text, expectedText, "readAsText in worker returned incorrect result.");
michael@0 81 is(event.data.bin, fileData, "readAsBinaryString in worker returned incorrect result.");
michael@0 82 is(event.data.url, convertToDataURL("application/octet-stream", fileData), "readAsDataURL in worker returned incorrect result.");
michael@0 83 is(event.data.arrayBuffer.byteLength, fileData.length, "readAsArrayBuffer returned buffer of incorrect length.");
michael@0 84 finish();
michael@0 85 };
michael@0 86
michael@0 87 worker.onerror = function(event) {
michael@0 88 ok(false, "Worker had an error: " + event.data);
michael@0 89 finish();
michael@0 90 };
michael@0 91
michael@0 92 var params = {file: createFileWithData(fileData), encoding: encoding};
michael@0 93
michael@0 94 worker.postMessage(params);
michael@0 95
michael@0 96 waitForWorkerFinish();
michael@0 97 }
michael@0 98
michael@0 99 /**
michael@0 100 * Create a worker which reuses a FileReaderSync to read multiple files as DataURLs.
michael@0 101 */
michael@0 102 function reuseReaderForURL(files, expected) {
michael@0 103 var worker = new Worker("fileReaderSync_worker.js");
michael@0 104
michael@0 105 worker.onerror = function(event) {
michael@0 106 ok(false, "Worker had an error: " + event.data);
michael@0 107 finish();
michael@0 108 };
michael@0 109
michael@0 110 var k = 0;
michael@0 111 worker.onmessage = function(event) {
michael@0 112 is(event.data.url, expected[k], "readAsDataURL in worker returned incorrect result when reusing FileReaderSync.");
michael@0 113 k++;
michael@0 114 finish();
michael@0 115 };
michael@0 116
michael@0 117 for (var i = 0; i < files.length; ++i) {
michael@0 118 var params = {file: files[i], encoding: undefined};
michael@0 119 worker.postMessage(params);
michael@0 120 waitForWorkerFinish();
michael@0 121 }
michael@0 122 }
michael@0 123
michael@0 124 /**
michael@0 125 * Create a worker which reuses a FileReaderSync to read multiple files as text.
michael@0 126 */
michael@0 127 function reuseReaderForText(fileData, expected) {
michael@0 128 var worker = new Worker("fileReaderSync_worker.js");
michael@0 129
michael@0 130 worker.onerror = function(event) {
michael@0 131 ok(false, "Worker had an error: " + event.data);
michael@0 132 finish();
michael@0 133 };
michael@0 134
michael@0 135 var k = 0;
michael@0 136 worker.onmessage = function(event) {
michael@0 137 is(event.data.text, expected[k++], "readAsText in worker returned incorrect result when reusing FileReaderSync.");
michael@0 138 finish();
michael@0 139 };
michael@0 140
michael@0 141 for (var i = 0; i < fileData.length; ++i) {
michael@0 142 var params = {file: createFileWithData(fileData[i]), encoding: undefined};
michael@0 143 worker.postMessage(params);
michael@0 144 waitForWorkerFinish();
michael@0 145 }
michael@0 146 }
michael@0 147
michael@0 148
michael@0 149 /**
michael@0 150 * Creates a a worker which reads a file containing fileData as an ArrayBuffer.
michael@0 151 * Verifies that the ArrayBuffer when interpreted as a string matches the original data.
michael@0 152 */
michael@0 153 function readArrayBuffer(fileData) {
michael@0 154 var worker = new Worker("fileReaderSync_worker.js");
michael@0 155
michael@0 156 worker.onmessage = function(event) {
michael@0 157 var view = new Uint8Array(event.data.arrayBuffer);
michael@0 158 is(event.data.arrayBuffer.byteLength, fileData.length, "readAsArrayBuffer returned buffer of incorrect length.");
michael@0 159 is(String.fromCharCode.apply(String, view), fileData, "readAsArrayBuffer returned buffer containing incorrect data.");
michael@0 160 finish();
michael@0 161 };
michael@0 162
michael@0 163 worker.onerror = function(event) {
michael@0 164 ok(false, "Worker had an error: " + event.data);
michael@0 165 finish();
michael@0 166 };
michael@0 167
michael@0 168 var params = {file: createFileWithData(fileData), encoding: undefined};
michael@0 169
michael@0 170 worker.postMessage(params);
michael@0 171
michael@0 172 waitForWorkerFinish();
michael@0 173 }
michael@0 174
michael@0 175 // Empty file.
michael@0 176 readFileData("", "");
michael@0 177
michael@0 178 // Typical use case.
michael@0 179 readFileData("text", "text");
michael@0 180
michael@0 181 // Test reading UTF-16 characters.
michael@0 182 readFileData(convertToUTF16("text"), "text", "UTF-16");
michael@0 183
michael@0 184 // First read a file of type "text/plain", then read a file of type "application/octet-stream".
michael@0 185 reuseReaderForURL([createFileWithData("text", "txt"), createFileWithData("text")],
michael@0 186 [convertToDataURL("text/plain", "text"),
michael@0 187 convertToDataURL("application/octet-stream", "text")]);
michael@0 188
michael@0 189 // First read UTF-16 characters marked using BOM, then read UTF-8 characters.
michael@0 190 reuseReaderForText([convertToUTF16("\ufefftext"), "text"],
michael@0 191 ["text", "text"]);
michael@0 192
michael@0 193 // Reading data as ArrayBuffer.
michael@0 194 readArrayBuffer("");
michael@0 195 readArrayBuffer("text");
michael@0 196
michael@0 197 ]]>
michael@0 198 </script>
michael@0 199 </window>

mercurial