mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-21 01:05:45 +00:00
Bug 1305230 - Make test_blobconstructor promise based, r=qdot
This commit is contained in:
parent
9d15f50373
commit
f35e7cc013
142
dom/file/tests/common_blob.js
Normal file
142
dom/file/tests/common_blob.js
Normal file
@ -0,0 +1,142 @@
|
||||
function testBlob(file, contents, testName) {
|
||||
// Load file using FileReader
|
||||
return new Promise(resolve => {
|
||||
let r = new FileReader();
|
||||
r.onload = event => {
|
||||
is(event.target.readyState, FileReader.DONE,
|
||||
"[FileReader] readyState in test FileReader.readAsBinaryString of " + testName);
|
||||
is(event.target.error, null,
|
||||
"[FileReader] no error in test FileReader.readAsBinaryString of " + testName);
|
||||
// Do not use |is(event.target.result, contents, "...");| that may output raw binary data.
|
||||
is(event.target.result.length, contents.length,
|
||||
"[FileReader] Length of result in test FileReader.readAsBinaryString of " + testName);
|
||||
ok(event.target.result == contents,
|
||||
"[FileReader] Content of result in test FileReader.readAsBinaryString of " + testName);
|
||||
is(event.lengthComputable, true,
|
||||
"[FileReader] lengthComputable in test FileReader.readAsBinaryString of " + testName);
|
||||
is(event.loaded, contents.length,
|
||||
"[FileReader] Loaded length in test FileReader.readAsBinaryString of " + testName);
|
||||
is(event.total, contents.length,
|
||||
"[FileReader] Total length in test FileReader.readAsBinaryString of " + testName);
|
||||
resolve();
|
||||
}
|
||||
r.readAsBinaryString(file);
|
||||
})
|
||||
|
||||
// Load file using URL.createObjectURL and XMLHttpRequest
|
||||
.then(() => {
|
||||
return new Promise(resolve => {
|
||||
let xhr = new XMLHttpRequest();
|
||||
xhr.open("GET", URL.createObjectURL(file));
|
||||
xhr.onload = event => {
|
||||
XHRLoadHandler(event, resolve, contents, "XMLHttpRequest load of " + testName);
|
||||
};
|
||||
xhr.overrideMimeType('text/plain; charset=x-user-defined');
|
||||
xhr.send();
|
||||
});
|
||||
})
|
||||
|
||||
// Send file to server using FormData and XMLHttpRequest
|
||||
.then(() => {
|
||||
return new Promise(resolve => {
|
||||
let xhr = new XMLHttpRequest();
|
||||
xhr.onload = function(event) {
|
||||
checkMPSubmission(JSON.parse(event.target.responseText),
|
||||
[{ name: "hello", value: "world"},
|
||||
{ name: "myfile",
|
||||
value: contents,
|
||||
fileName: file.name || "blob",
|
||||
contentType: file.type || "application/octet-stream" }]);
|
||||
resolve();
|
||||
}
|
||||
xhr.open("POST", "../../../dom/html/test/form_submit_server.sjs");
|
||||
|
||||
let fd = new FormData;
|
||||
fd.append("hello", "world");
|
||||
fd.append("myfile", file);
|
||||
|
||||
xhr.send(fd);
|
||||
});
|
||||
})
|
||||
|
||||
// Send file to server using plain XMLHttpRequest
|
||||
.then(() => {
|
||||
return new Promise(resolve => {
|
||||
let xhr = new XMLHttpRequest();
|
||||
xhr.open("POST", "../../../dom/xhr/tests/file_XHRSendData.sjs");
|
||||
|
||||
xhr.onload = function (event) {
|
||||
is(event.target.getResponseHeader("Result-Content-Type"),
|
||||
file.type ? file.type : null,
|
||||
"request content-type in XMLHttpRequest send of " + testName);
|
||||
is(event.target.getResponseHeader("Result-Content-Length"),
|
||||
String(file.size),
|
||||
"request content-length in XMLHttpRequest send of " + testName);
|
||||
};
|
||||
|
||||
xhr.addEventListener("load", event => {
|
||||
XHRLoadHandler(event, resolve, contents, "XMLHttpRequest send of " + testName);
|
||||
});
|
||||
xhr.overrideMimeType('text/plain; charset=x-user-defined');
|
||||
xhr.send(file);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function convertXHRBinary(s) {
|
||||
let res = "";
|
||||
for (let i = 0; i < s.length; ++i) {
|
||||
res += String.fromCharCode(s.charCodeAt(i) & 255);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
function XHRLoadHandler(event, resolve, contents, testName) {
|
||||
is(event.target.readyState, 4, "[XHR] readyState in test " + testName);
|
||||
is(event.target.status, 200, "[XHR] no error in test " + testName);
|
||||
// Do not use |is(convertXHRBinary(event.target.responseText), contents, "...");| that may output raw binary data.
|
||||
let convertedData = convertXHRBinary(event.target.responseText);
|
||||
is(convertedData.length, contents.length, "[XHR] Length of result in test " + testName);
|
||||
ok(convertedData == contents, "[XHR] Content of result in test " + testName);
|
||||
is(event.lengthComputable, event.total != 0, "[XHR] lengthComputable in test " + testName);
|
||||
is(event.loaded, contents.length, "[XHR] Loaded length in test " + testName);
|
||||
is(event.total, contents.length, "[XHR] Total length in test " + testName);
|
||||
resolve();
|
||||
}
|
||||
|
||||
function checkMPSubmission(sub, expected) {
|
||||
function getPropCount(o) {
|
||||
let x, l = 0;
|
||||
for (x in o) ++l;
|
||||
return l;
|
||||
}
|
||||
|
||||
is(sub.length, expected.length,
|
||||
"Correct number of items");
|
||||
let i;
|
||||
for (i = 0; i < expected.length; ++i) {
|
||||
if (!("fileName" in expected[i])) {
|
||||
is(sub[i].headers["Content-Disposition"],
|
||||
"form-data; name=\"" + expected[i].name + "\"",
|
||||
"Correct name (A)");
|
||||
is (getPropCount(sub[i].headers), 1,
|
||||
"Wrong number of headers (A)");
|
||||
}
|
||||
else {
|
||||
is(sub[i].headers["Content-Disposition"],
|
||||
"form-data; name=\"" + expected[i].name + "\"; filename=\"" +
|
||||
expected[i].fileName + "\"",
|
||||
"Correct name (B)");
|
||||
is(sub[i].headers["Content-Type"],
|
||||
expected[i].contentType,
|
||||
"Correct content type (B)");
|
||||
is (getPropCount(sub[i].headers), 2,
|
||||
"Wrong number of headers (B)");
|
||||
}
|
||||
// Do not use |is(sub[i].body, expected[i].value, "...");| that may output raw binary data.
|
||||
is(sub[i].body.length, expected[i].value.length,
|
||||
"Length of correct value");
|
||||
ok(sub[i].body == expected[i].value,
|
||||
"Content of correct value");
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
[DEFAULT]
|
||||
support-files =
|
||||
common_blob.js
|
||||
create_file_objects.js
|
||||
file_blobURL_expiring.html
|
||||
file_mozfiledataurl_img.jpg
|
||||
|
@ -7,7 +7,7 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=721569
|
||||
<title>Test for Blob constructor (Bug 721569)</title>
|
||||
<script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
|
||||
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
|
||||
<script type="text/javascript" src="fileutils.js"></script>
|
||||
<script type="text/javascript" src="common_blob.js"></script>
|
||||
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
|
||||
</head>
|
||||
<body>
|
||||
@ -196,49 +196,49 @@ let testData =
|
||||
{start: 4, length: 8, contents: "DEFGHIJK"}]]
|
||||
];
|
||||
|
||||
let currentTest = null;
|
||||
let testCounter = 0;
|
||||
|
||||
function doTest(data) {
|
||||
testCounter++;
|
||||
|
||||
var [blobs, options, tests] = data;
|
||||
|
||||
function runTest(test) {
|
||||
|
||||
let blob;
|
||||
if (options !== undefined) {
|
||||
blob = new Blob(blobs, options);
|
||||
} else {
|
||||
blob = new Blob(blobs);
|
||||
function runTests() {
|
||||
if (!currentTest || currentTest[2].length == 0) {
|
||||
if (testData.length == 0) {
|
||||
SimpleTest.finish();
|
||||
return;
|
||||
}
|
||||
ok(blob, "Test " + testCounter + " got blob");
|
||||
ok(blob instanceof Blob, "Test " + testCounter + " blob is a Blob");
|
||||
ok(!(blob instanceof File), "Test " + testCounter + " blob is not a File");
|
||||
|
||||
let slice = blob.slice(test.start, test.start + test.length);
|
||||
ok(slice, "Test " + testCounter + " got slice");
|
||||
ok(slice instanceof Blob, "Test " + testCounter + " slice is a Blob");
|
||||
ok(!(slice instanceof File), "Test " + testCounter + " slice is not a File");
|
||||
is(slice.size, test.contents.length,
|
||||
"Test " + testCounter + " slice is correct size");
|
||||
|
||||
testFile(slice, test.contents, "Test " + testCounter);
|
||||
currentTest = testData.shift();
|
||||
++testCounter;
|
||||
}
|
||||
if (Array.isArray(tests)) {
|
||||
tests.forEach(runTest);
|
||||
|
||||
let [blobs, options] = currentTest;
|
||||
let test = currentTest[2].shift();
|
||||
|
||||
let blob;
|
||||
if (options !== undefined) {
|
||||
blob = new Blob(blobs, options);
|
||||
} else {
|
||||
try {
|
||||
let blob = new Blob(blobs, options);
|
||||
ok(false, "NOT REACHED");
|
||||
} catch (e) {
|
||||
is(e.name, tests, "Blob constructor should throw " + tests);
|
||||
}
|
||||
blob = new Blob(blobs);
|
||||
}
|
||||
SpecialPowers.gc();
|
||||
|
||||
ok(blob, "Test " + testCounter + " got blob");
|
||||
ok(blob instanceof Blob, "Test " + testCounter + " blob is a Blob");
|
||||
ok(!(blob instanceof File), "Test " + testCounter + " blob is not a File");
|
||||
|
||||
let slice = blob.slice(test.start, test.start + test.length);
|
||||
ok(slice, "Test " + testCounter + " got slice");
|
||||
ok(slice instanceof Blob, "Test " + testCounter + " slice is a Blob");
|
||||
ok(!(slice instanceof File), "Test " + testCounter + " slice is not a File");
|
||||
is(slice.size, test.contents.length, "Test " + testCounter + " slice is correct size");
|
||||
|
||||
testBlob(slice, test.contents, "Test " + testCounter).then(() => {
|
||||
SpecialPowers.gc();
|
||||
runTests();
|
||||
});
|
||||
}
|
||||
|
||||
SimpleTest.requestLongerTimeout(2);
|
||||
SimpleTest.waitForExplicitFinish();
|
||||
testData.forEach(doTest);
|
||||
runTests();
|
||||
|
||||
</script>
|
||||
</pre>
|
||||
|
Loading…
Reference in New Issue
Block a user