content/base/test/fileutils.js

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

michael@0 1 // Utility functions
michael@0 2 var testRanCounter = 0;
michael@0 3 var expectedTestCount = 0;
michael@0 4
michael@0 5 function testHasRun() {
michael@0 6 //alert(testRanCounter);
michael@0 7 ++testRanCounter;
michael@0 8 if (testRanCounter == expectedTestCount) {
michael@0 9 SimpleTest.finish();
michael@0 10 }
michael@0 11 }
michael@0 12
michael@0 13
michael@0 14 function testFile(file, contents, test) {
michael@0 15 SimpleTest.requestLongerTimeout(2);
michael@0 16
michael@0 17 // Load file using FileReader
michael@0 18 var r = new FileReader();
michael@0 19 r.onload = getFileReaderLoadHandler(contents, contents.length, "FileReader.readAsBinaryString of " + test);
michael@0 20 r.readAsBinaryString(file);
michael@0 21 expectedTestCount++;
michael@0 22
michael@0 23 // Load file using URL.createObjectURL and XMLHttpRequest
michael@0 24 var xhr = new XMLHttpRequest;
michael@0 25 xhr.open("GET", URL.createObjectURL(file));
michael@0 26 xhr.onload = getXHRLoadHandler(contents, contents.length, false,
michael@0 27 "XMLHttpRequest load of " + test);
michael@0 28 xhr.overrideMimeType('text/plain; charset=x-user-defined');
michael@0 29 xhr.send();
michael@0 30 expectedTestCount++;
michael@0 31
michael@0 32 // Send file to server using FormData and XMLHttpRequest
michael@0 33 xhr = new XMLHttpRequest();
michael@0 34 xhr.onload = function(event) {
michael@0 35 checkMPSubmission(JSON.parse(event.target.responseText),
michael@0 36 [{ name: "hello", value: "world"},
michael@0 37 { name: "myfile",
michael@0 38 value: contents,
michael@0 39 fileName: file.name || "blob",
michael@0 40 contentType: file.type || "application/octet-stream" }]);
michael@0 41 testHasRun();
michael@0 42 }
michael@0 43 xhr.open("POST", "../../html/content/test/form_submit_server.sjs");
michael@0 44 var fd = new FormData;
michael@0 45 fd.append("hello", "world");
michael@0 46 fd.append("myfile", file);
michael@0 47 xhr.send(fd);
michael@0 48 expectedTestCount++;
michael@0 49
michael@0 50 // Send file to server using plain XMLHttpRequest
michael@0 51 var xhr = new XMLHttpRequest;
michael@0 52 xhr.open("POST", "file_XHRSendData.sjs");
michael@0 53 xhr.onload = function (event) {
michael@0 54 is(event.target.getResponseHeader("Result-Content-Type"),
michael@0 55 file.type ? file.type : null,
michael@0 56 "request content-type in XMLHttpRequest send of " + test);
michael@0 57 is(event.target.getResponseHeader("Result-Content-Length"),
michael@0 58 file.size,
michael@0 59 "request content-length in XMLHttpRequest send of " + test);
michael@0 60 };
michael@0 61 xhr.addEventListener("load",
michael@0 62 getXHRLoadHandler(contents, contents.length, true,
michael@0 63 "XMLHttpRequest send of " + test),
michael@0 64 false);
michael@0 65 xhr.overrideMimeType('text/plain; charset=x-user-defined');
michael@0 66 xhr.send(file);
michael@0 67 expectedTestCount++;
michael@0 68 }
michael@0 69
michael@0 70 function getFileReaderLoadHandler(expectedResult, expectedLength, testName) {
michael@0 71 return function (event) {
michael@0 72 is(event.target.readyState, FileReader.DONE,
michael@0 73 "[FileReader] readyState in test " + testName);
michael@0 74 is(event.target.error, null,
michael@0 75 "[FileReader] no error in test " + testName);
michael@0 76 // Do not use |is(event.target.result, expectedResult, "...");| that may output raw binary data.
michael@0 77 is(event.target.result.length, expectedResult.length,
michael@0 78 "[FileReader] Length of result in test " + testName);
michael@0 79 ok(event.target.result == expectedResult,
michael@0 80 "[FileReader] Content of result in test " + testName);
michael@0 81 is(event.lengthComputable, true,
michael@0 82 "[FileReader] lengthComputable in test " + testName);
michael@0 83 is(event.loaded, expectedLength,
michael@0 84 "[FileReader] Loaded length in test " + testName);
michael@0 85 is(event.total, expectedLength,
michael@0 86 "[FileReader] Total length in test " + testName);
michael@0 87 testHasRun();
michael@0 88 }
michael@0 89 }
michael@0 90
michael@0 91 function getXHRLoadHandler(expectedResult, expectedLength, statusWorking, testName) {
michael@0 92 return function (event) {
michael@0 93 is(event.target.readyState, 4,
michael@0 94 "[XHR] readyState in test " + testName);
michael@0 95 if (statusWorking) {
michael@0 96 is(event.target.status, 200,
michael@0 97 "[XHR] no error in test " + testName);
michael@0 98 }
michael@0 99 else {
michael@0 100 todo_is(event.target.status, 200,
michael@0 101 "[XHR] no error in test " + testName);
michael@0 102 }
michael@0 103 // Do not use |is(convertXHRBinary(event.target.responseText), expectedResult, "...");| that may output raw binary data.
michael@0 104 var convertedData = convertXHRBinary(event.target.responseText);
michael@0 105 is(convertedData.length, expectedResult.length,
michael@0 106 "[XHR] Length of result in test " + testName);
michael@0 107 ok(convertedData == expectedResult,
michael@0 108 "[XHR] Content of result in test " + testName);
michael@0 109 is(event.lengthComputable, true,
michael@0 110 "[XHR] lengthComputable in test " + testName);
michael@0 111 is(event.loaded, expectedLength,
michael@0 112 "[XHR] Loaded length in test " + testName);
michael@0 113 is(event.total, expectedLength,
michael@0 114 "[XHR] Total length in test " + testName);
michael@0 115
michael@0 116 testHasRun();
michael@0 117 }
michael@0 118 }
michael@0 119
michael@0 120 function convertXHRBinary(s) {
michael@0 121 var res = "";
michael@0 122 for (var i = 0; i < s.length; ++i) {
michael@0 123 res += String.fromCharCode(s.charCodeAt(i) & 255);
michael@0 124 }
michael@0 125 return res;
michael@0 126 }
michael@0 127
michael@0 128 function testHasRun() {
michael@0 129 //alert(testRanCounter);
michael@0 130 ++testRanCounter;
michael@0 131 if (testRanCounter == expectedTestCount) {
michael@0 132 SimpleTest.finish();
michael@0 133 }
michael@0 134 }
michael@0 135
michael@0 136 function createFileWithData(fileData) {
michael@0 137 var dirSvc = SpecialPowers.Cc["@mozilla.org/file/directory_service;1"].getService(SpecialPowers.Ci.nsIProperties);
michael@0 138 var testFile = dirSvc.get("ProfD", SpecialPowers.Ci.nsIFile);
michael@0 139 testFile.append("fileAPItestfile2-" + fileNum);
michael@0 140 fileNum++;
michael@0 141 var outStream = SpecialPowers.Cc["@mozilla.org/network/file-output-stream;1"].createInstance(SpecialPowers.Ci.nsIFileOutputStream);
michael@0 142 outStream.init(testFile, 0x02 | 0x08 | 0x20, // write, create, truncate
michael@0 143 0666, 0);
michael@0 144 outStream.write(fileData, fileData.length);
michael@0 145 outStream.close();
michael@0 146
michael@0 147 var fileList = document.getElementById('fileList');
michael@0 148 SpecialPowers.wrap(fileList).value = testFile.path;
michael@0 149
michael@0 150 return fileList.files[0];
michael@0 151 }
michael@0 152
michael@0 153 function gc() {
michael@0 154 window.QueryInterface(SpecialPowers.Ci.nsIInterfaceRequestor)
michael@0 155 .getInterface(SpecialPowers.Ci.nsIDOMWindowUtils)
michael@0 156 .garbageCollect();
michael@0 157 }
michael@0 158
michael@0 159 function checkMPSubmission(sub, expected) {
michael@0 160 function getPropCount(o) {
michael@0 161 var x, l = 0;
michael@0 162 for (x in o) ++l;
michael@0 163 return l;
michael@0 164 }
michael@0 165
michael@0 166 is(sub.length, expected.length,
michael@0 167 "Correct number of items");
michael@0 168 var i;
michael@0 169 for (i = 0; i < expected.length; ++i) {
michael@0 170 if (!("fileName" in expected[i])) {
michael@0 171 is(sub[i].headers["Content-Disposition"],
michael@0 172 "form-data; name=\"" + expected[i].name + "\"",
michael@0 173 "Correct name (A)");
michael@0 174 is (getPropCount(sub[i].headers), 1,
michael@0 175 "Wrong number of headers (A)");
michael@0 176 }
michael@0 177 else {
michael@0 178 is(sub[i].headers["Content-Disposition"],
michael@0 179 "form-data; name=\"" + expected[i].name + "\"; filename=\"" +
michael@0 180 expected[i].fileName + "\"",
michael@0 181 "Correct name (B)");
michael@0 182 is(sub[i].headers["Content-Type"],
michael@0 183 expected[i].contentType,
michael@0 184 "Correct content type (B)");
michael@0 185 is (getPropCount(sub[i].headers), 2,
michael@0 186 "Wrong number of headers (B)");
michael@0 187 }
michael@0 188 // Do not use |is(sub[i].body, expected[i].value, "...");| that may output raw binary data.
michael@0 189 is(sub[i].body.length, expected[i].value.length,
michael@0 190 "Length of correct value");
michael@0 191 ok(sub[i].body == expected[i].value,
michael@0 192 "Content of correct value");
michael@0 193 }
michael@0 194 }
michael@0 195
michael@0 196 function testSlice(file, size, type, contents, fileType) {
michael@0 197 is(file.type, type, fileType + " file is correct type");
michael@0 198 is(file.size, size, fileType + " file is correct size");
michael@0 199 ok(file instanceof File, fileType + " file is a File");
michael@0 200 ok(file instanceof Blob, fileType + " file is also a Blob");
michael@0 201
michael@0 202 var slice = file.slice(0, size);
michael@0 203 ok(slice instanceof Blob, fileType + " fullsize slice is a Blob");
michael@0 204 ok(!(slice instanceof File), fileType + " fullsize slice is not a File");
michael@0 205
michael@0 206 slice = file.slice(0, 1234);
michael@0 207 ok(slice instanceof Blob, fileType + " sized slice is a Blob");
michael@0 208 ok(!(slice instanceof File), fileType + " sized slice is not a File");
michael@0 209
michael@0 210 slice = file.slice(0, size, "foo/bar");
michael@0 211 is(slice.type, "foo/bar", fileType + " fullsize slice foo/bar type");
michael@0 212
michael@0 213 slice = file.slice(0, 5432, "foo/bar");
michael@0 214 is(slice.type, "foo/bar", fileType + " sized slice foo/bar type");
michael@0 215
michael@0 216 is(slice.slice(0, 10).type, "", fileType + " slice-slice type");
michael@0 217 is(slice.slice(0, 10).size, 10, fileType + " slice-slice size");
michael@0 218 is(slice.slice(0, 10, "hello/world").type, "hello/world", fileType + " slice-slice hello/world type");
michael@0 219 is(slice.slice(0, 10, "hello/world").size, 10, fileType + " slice-slice hello/world size");
michael@0 220
michael@0 221 // Start, end, expected size
michael@0 222 var indexes = [[0, size, size],
michael@0 223 [0, 1234, 1234],
michael@0 224 [size-500, size, 500],
michael@0 225 [size-500, size+500, 500],
michael@0 226 [size+500, size+1500, 0],
michael@0 227 [0, 0, 0],
michael@0 228 [1000, 1000, 0],
michael@0 229 [size, size, 0],
michael@0 230 [undefined, undefined, size],
michael@0 231 [0, undefined, size],
michael@0 232 [100, undefined, size-100],
michael@0 233 [-100, undefined, 100],
michael@0 234 [100, -100, size-200],
michael@0 235 [-size-100, undefined, size],
michael@0 236 [-2*size-100, 500, 500],
michael@0 237 [0, -size-100, 0],
michael@0 238 [100, -size-100, 0],
michael@0 239 [50, -size+100, 50],
michael@0 240 [0, 33000, 33000],
michael@0 241 [1000, 34000, 33000],
michael@0 242 ];
michael@0 243
michael@0 244 for (var i = 0; i < indexes.length; ++i) {
michael@0 245 var sliceContents;
michael@0 246 var testName;
michael@0 247 if (indexes[i][0] == undefined) {
michael@0 248 slice = file.slice();
michael@0 249 sliceContents = contents.slice();
michael@0 250 testName = fileType + " slice()";
michael@0 251 }
michael@0 252 else if (indexes[i][1] == undefined) {
michael@0 253 slice = file.slice(indexes[i][0]);
michael@0 254 sliceContents = contents.slice(indexes[i][0]);
michael@0 255 testName = fileType + " slice(" + indexes[i][0] + ")";
michael@0 256 }
michael@0 257 else {
michael@0 258 slice = file.slice(indexes[i][0], indexes[i][1]);
michael@0 259 sliceContents = contents.slice(indexes[i][0], indexes[i][1]);
michael@0 260 testName = fileType + " slice(" + indexes[i][0] + ", " + indexes[i][1] + ")";
michael@0 261 }
michael@0 262 is(slice.type, "", testName + " type");
michael@0 263 is(slice.size, indexes[i][2], testName + " size");
michael@0 264 is(sliceContents.length, indexes[i][2], testName + " data size");
michael@0 265 testFile(slice, sliceContents, testName);
michael@0 266 }
michael@0 267
michael@0 268 // Slice of slice
michael@0 269 var slice = file.slice(0, 40000);
michael@0 270 testFile(slice.slice(5000, 42000), contents.slice(5000, 40000), "file slice slice");
michael@0 271
michael@0 272 // ...of slice of slice
michael@0 273 slice = slice.slice(5000, 42000).slice(400, 700);
michael@0 274 SpecialPowers.gc();
michael@0 275 testFile(slice, contents.slice(5400, 5700), "file slice slice slice");
michael@0 276 }
michael@0 277

mercurial