Bug 1400098 - Using common_fileReader.js for test_fileapi in workers, r=qdot

This commit is contained in:
Andrea Marchesini 2017-10-17 13:21:39 +02:00
Родитель de661f1d28
Коммит 72c79e182d
9 изменённых файлов: 183 добавлений и 670 удалений

Просмотреть файл

@ -1,94 +1,173 @@
function runTests(asciiFile, binaryFile, nonExistingFile, utf8TextFile,
utf16TextFile, emptyFile, dataUrlFile0, dataUrlFile1,
dataUrlFile2, testTextData) {
function test_setup() {
return new Promise(resolve => {
const minFileSize = 20000;
// Create strings containing data we'll test with. We'll want long
// strings to ensure they span multiple buffers while loading
let testTextData = "asd b\tlah\u1234w\u00a0r";
while (testTextData.length < minFileSize) {
testTextData = testTextData + testTextData;
}
let testASCIIData = "abcdef 123456\n";
while (testASCIIData.length < minFileSize) {
testASCIIData = testASCIIData + testASCIIData;
}
let testBinaryData = "";
for (let i = 0; i < 256; i++) {
testBinaryData += String.fromCharCode(i);
}
while (testBinaryData.length < minFileSize) {
testBinaryData = testBinaryData + testBinaryData;
}
let dataurldata0 = testBinaryData.substr(0, testBinaryData.length -
testBinaryData.length % 3);
let dataurldata1 = testBinaryData.substr(0, testBinaryData.length - 2 -
testBinaryData.length % 3);
let dataurldata2 = testBinaryData.substr(0, testBinaryData.length - 1 -
testBinaryData.length % 3);
//Set up files for testing
let openerURL = SimpleTest.getTestFileURL("fileapi_chromeScript.js");
let opener = SpecialPowers.loadChromeScript(openerURL);
opener.addMessageListener("files.opened", message => {
let [
asciiFile,
binaryFile,
nonExistingFile,
utf8TextFile,
utf16TextFile,
emptyFile,
dataUrlFile0,
dataUrlFile1,
dataUrlFile2,
] = message;
resolve({ blobs:{ asciiFile, binaryFile, nonExistingFile, utf8TextFile,
utf16TextFile, emptyFile, dataUrlFile0, dataUrlFile1,
dataUrlFile2 },
data: { text: testTextData,
ascii: testASCIIData,
binary: testBinaryData,
url0: dataurldata0,
url1: dataurldata1,
url2: dataurldata2, }});
});
opener.sendAsyncMessage("files.open", [
testASCIIData,
testBinaryData,
null,
convertToUTF8(testTextData),
convertToUTF16(testTextData),
"",
dataurldata0,
dataurldata1,
dataurldata2,
]);
});
}
function runTests(data) {
return test_basic()
.then(() => {
return test_readAsText(asciiFile, testASCIIData);
return test_readAsText(data.blobs.asciiFile, data.data.ascii);
})
.then(() => {
return test_readAsTextWithEncoding(asciiFile, testASCIIData,
testASCIIData.length, "");
return test_readAsTextWithEncoding(data.blobs.asciiFile, data.data.ascii,
data.data.ascii.length, "");
})
.then(() => {
return test_readAsTextWithEncoding(asciiFile, testASCIIData,
testASCIIData.length, "iso8859-1");
return test_readAsTextWithEncoding(data.blobs.asciiFile, data.data.ascii,
data.data.ascii.length, "iso8859-1");
})
.then(() => {
return test_readAsTextWithEncoding(utf8TextFile, testTextData,
convertToUTF8(testTextData).length,
return test_readAsTextWithEncoding(data.blobs.utf8TextFile, data.data.text,
convertToUTF8(data.data.text).length,
"utf8");
})
.then(() => {
return test_readAsTextWithEncoding(utf16TextFile, testTextData,
convertToUTF16(testTextData).length,
return test_readAsTextWithEncoding(data.blobs.utf16TextFile, data.data.text,
convertToUTF16(data.data.text).length,
"utf-16");
})
.then(() => {
return test_readAsBinaryString(binaryFile, testBinaryData);
return test_readAsBinaryString(data.blobs.binaryFile, data.data.binary);
})
.then(() => {
return test_readAsArrayBuffer(binaryFile, testBinaryData);
return test_readAsArrayBuffer(data.blobs.binaryFile, data.data.binary);
})
.then(() => {
return test_onlyResult()
})
.then(() => {
return test_readAsText(emptyFile, "");
return test_readAsText(data.blobs.emptyFile, "");
})
.then(() => {
return test_readAsTextWithEncoding(emptyFile, "", 0, "");
return test_readAsTextWithEncoding(data.blobs.emptyFile, "", 0, "");
})
.then(() => {
return test_readAsTextWithEncoding(emptyFile, "", 0, "utf8");
return test_readAsTextWithEncoding(data.blobs.emptyFile, "", 0, "utf8");
})
.then(() => {
return test_readAsTextWithEncoding(emptyFile, "", 0, "utf-16");
return test_readAsTextWithEncoding(data.blobs.emptyFile, "", 0, "utf-16");
})
.then(() => {
return test_readAsBinaryString(emptyFile, "");
return test_readAsBinaryString(data.blobs.emptyFile, "");
})
.then(() => {
return test_readAsArrayBuffer(emptyFile, "");
return test_readAsArrayBuffer(data.blobs.emptyFile, "");
})
.then(() => {
return test_readAsDataURL(emptyFile, convertToDataURL(""), 0);
return test_readAsDataURL(data.blobs.emptyFile, convertToDataURL(""), 0);
})
.then(() => {
return test_readAsTextTwice(asciiFile, testASCIIData);
return test_readAsTextTwice(data.blobs.asciiFile, data.data.ascii);
})
.then(() => {
return test_readAsBinaryStringTwice(binaryFile, testBinaryData);
return test_readAsBinaryStringTwice(data.blobs.binaryFile,
data.data.binary);
})
.then(() => {
return test_readAsDataURLTwice(binaryFile, convertToDataURL(testBinaryData),
testBinaryData.length);
return test_readAsDataURLTwice(data.blobs.binaryFile,
convertToDataURL(data.data.binary),
data.data.binary.length);
})
.then(() => {
return test_readAsArrayBufferTwice(binaryFile, testBinaryData);
return test_readAsArrayBufferTwice(data.blobs.binaryFile,
data.data.binary);
})
.then(() => {
return test_readAsArrayBufferTwice2(binaryFile, testBinaryData);
return test_readAsArrayBufferTwice2(data.blobs.binaryFile,
data.data.binary);
})
.then(() => {
return test_readAsDataURL_customLength(dataUrlFile0, convertToDataURL(dataurldata0),
dataurldata0.length, 0);
return test_readAsDataURL_customLength(data.blobs.dataUrlFile0,
convertToDataURL(data.data.url0),
data.data.url0.length, 0);
})
.then(() => {
return test_readAsDataURL_customLength(dataUrlFile1, convertToDataURL(dataurldata1),
dataurldata1.length, 1);
return test_readAsDataURL_customLength(data.blobs.dataUrlFile1,
convertToDataURL(data.data.url1),
data.data.url1.length, 1);
})
.then(() => {
return test_readAsDataURL_customLength(dataUrlFile2, convertToDataURL(dataurldata2),
dataurldata2.length, 2);
return test_readAsDataURL_customLength(data.blobs.dataUrlFile2,
convertToDataURL(data.data.url2),
data.data.url2.length, 2);
})
.then(() => {
return test_abort(asciiFile);
return test_abort(data.blobs.asciiFile);
})
.then(() => {
return test_abort_readAsX(asciiFile, testASCIIData);
return test_abort_readAsX(data.blobs.asciiFile, data.data.ascii);
})
.then(() => {
return test_nonExisting(nonExistingFile);
return test_nonExisting(data.blobs.nonExistingFile);
});
}
@ -133,13 +212,15 @@ function loadEventHandler_arrayBuffer(event, resolve, reader, data, testName) {
"array buffer contents in test " + testName);
u8v = null;
SpecialPowers.gc();
if ("SpecialPowers" in self) {
SpecialPowers.gc();
is(event.target.result.byteLength, data.length,
"array buffer size after gc in test " + testName);
u8v = new Uint8Array(event.target.result);
is(String.fromCharCode.apply(String, u8v), data,
"array buffer contents after gc in test " + testName);
is(event.target.result.byteLength, data.length,
"array buffer size after gc in test " + testName);
u8v = new Uint8Array(event.target.result);
is(String.fromCharCode.apply(String, u8v), data,
"array buffer contents after gc in test " + testName);
}
resolve();
}

Просмотреть файл

@ -2,6 +2,7 @@
support-files =
common_blob.js
create_file_objects.js
common_fileReader.js
file_blobURL_expiring.html
file_mozfiledataurl_img.jpg
file_mozfiledataurl_audio.ogg
@ -22,7 +23,8 @@ support-files = file_ipc_messagemanager_blob.html
[test_nonascii_blob_url.html]
[test_file_negative_date.html]
[test_fileapi.html]
support-files = common_fileReader.js
[test_fileapi_worker.html]
support-files = worker_fileReader.js
[test_fileapi_slice_realFile_1.html]
skip-if = (toolkit == 'android') # Android: Bug 775227
[test_fileapi_slice_realFile_2.html]

Просмотреть файл

@ -10,70 +10,13 @@
<body>
<script class="testbody" type="text/javascript">
const minFileSize = 20000;
SimpleTest.waitForExplicitFinish();
// Create strings containing data we'll test with. We'll want long
// strings to ensure they span multiple buffers while loading
var testTextData = "asd b\tlah\u1234w\u00a0r";
while (testTextData.length < minFileSize) {
testTextData = testTextData + testTextData;
}
var testASCIIData = "abcdef 123456\n";
while (testASCIIData.length < minFileSize) {
testASCIIData = testASCIIData + testASCIIData;
}
var testBinaryData = "";
for (var i = 0; i < 256; i++) {
testBinaryData += String.fromCharCode(i);
}
while (testBinaryData.length < minFileSize) {
testBinaryData = testBinaryData + testBinaryData;
}
var dataurldata0 = testBinaryData.substr(0, testBinaryData.length -
testBinaryData.length % 3);
var dataurldata1 = testBinaryData.substr(0, testBinaryData.length - 2 -
testBinaryData.length % 3);
var dataurldata2 = testBinaryData.substr(0, testBinaryData.length - 1 -
testBinaryData.length % 3);
//Set up files for testing
var openerURL = SimpleTest.getTestFileURL("fileapi_chromeScript.js");
var opener = SpecialPowers.loadChromeScript(openerURL);
opener.addMessageListener("files.opened", onFilesOpened);
opener.sendAsyncMessage("files.open", [
testASCIIData,
testBinaryData,
null,
convertToUTF8(testTextData),
convertToUTF16(testTextData),
"",
dataurldata0,
dataurldata1,
dataurldata2,
]);
function onFilesOpened(message) {
let [
asciiFile,
binaryFile,
nonExistingFile,
utf8TextFile,
utf16TextFile,
emptyFile,
dataUrlFile0,
dataUrlFile1,
dataUrlFile2,
] = message;
runTests(asciiFile, binaryFile, nonExistingFile, utf8TextFile, utf16TextFile,
emptyFile, dataUrlFile0, dataUrlFile1, dataUrlFile2, testTextData)
.then(SimpleTest.finish);
}
test_setup()
.then(data => {
return runTests(data);
})
.then(SimpleTest.finish);
</script>
</body>

Просмотреть файл

@ -0,0 +1,37 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Test for FileReader API in workers</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
<script type="text/javascript" src="common_fileReader.js"></script>
</head>
<body>
<script class="testbody" type="text/javascript">
SimpleTest.waitForExplicitFinish();
test_setup()
.then(data => {
let worker = new Worker('worker_fileReader.js');
worker.postMessage(data);
worker.onmessage = event => {
if (event.data.type == 'finish') {
SimpleTest.finish();
return;
}
if (event.data.type == 'check') {
ok(event.data.status, event.data.msg);
return;
}
ok(false, "Unknown message.");
}
});
</script>
</body>
</html>

Просмотреть файл

@ -0,0 +1,15 @@
importScripts('common_fileReader.js');
function ok(a, msg) {
postMessage({type:'check', msg, status: !!a});
}
function is(a, b, msg) {
ok(a === b, msg);
}
onmessage = event => {
runTests(event.data).then(() => {
postMessage({ type: 'finish' });
});
};

Просмотреть файл

@ -1,42 +0,0 @@
var { classes: Cc, interfaces: Ci, utils: Cu } = Components;
Cu.importGlobalProperties(["File"]);
var fileNum = 1;
function createFileWithData(fileData) {
var willDelete = fileData === null;
var dirSvc = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties);
var testFile = dirSvc.get("ProfD", Ci.nsIFile);
testFile.append("fileAPItestfile" + fileNum);
fileNum++;
var outStream = Cc["@mozilla.org/network/file-output-stream;1"].createInstance(Ci.nsIFileOutputStream);
outStream.init(testFile, 0x02 | 0x08 | 0x20, // write, create, truncate
0o666, 0);
if (willDelete) {
fileData = "some irrelevant test data\n";
}
outStream.write(fileData, fileData.length);
outStream.close();
return File.createFromNsIFile(testFile).then(function(domFile) {
if (willDelete) {
testFile.remove(/* recursive: */ false);
}
return domFile;
});
}
addMessageListener("files.open", function (message) {
let promises = [];
let list = [];
for (let fileData of message) {
promises.push(createFileWithData(fileData).then(domFile => {
list.push(domFile);
}));
}
Promise.all(promises).then(() => {
sendAsyncMessage("files.opened", list);
});
});

Просмотреть файл

@ -98,8 +98,6 @@ support-files =
worker_referrer.js
websocket_https.html
websocket_https_worker.js
worker_fileReader.js
fileapi_chromeScript.js
importScripts_3rdParty_worker.js
worker_bug1278777.js
worker_setTimeoutWith0.js
@ -229,8 +227,6 @@ scheme=https
[test_importScripts_3rdparty.html]
[test_sharedWorker_ports.html]
[test_sharedWorker_lifetime.html]
[test_fileReader.html]
skip-if = !debug # bug 1400098
[test_navigator_workers_hardwareConcurrency.html]
[test_bug1278777.html]
[test_setTimeoutWith0.html]

Просмотреть файл

@ -1,100 +0,0 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Test for FileReader in workers</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<script type="text/javascript">
const minFileSize = 20000;
SimpleTest.waitForExplicitFinish();
// Create strings containing data we'll test with. We'll want long
// strings to ensure they span multiple buffers while loading
var testTextData = "asd b\tlah\u1234w\u00a0r";
while (testTextData.length < minFileSize) {
testTextData = testTextData + testTextData;
}
var testASCIIData = "abcdef 123456\n";
while (testASCIIData.length < minFileSize) {
testASCIIData = testASCIIData + testASCIIData;
}
var testBinaryData = "";
for (var i = 0; i < 256; i++) {
testBinaryData += String.fromCharCode(i);
}
while (testBinaryData.length < minFileSize) {
testBinaryData = testBinaryData + testBinaryData;
}
var dataurldata0 = testBinaryData.substr(0, testBinaryData.length -
testBinaryData.length % 3);
var dataurldata1 = testBinaryData.substr(0, testBinaryData.length - 2 -
testBinaryData.length % 3);
var dataurldata2 = testBinaryData.substr(0, testBinaryData.length - 1 -
testBinaryData.length % 3);
//Set up files for testing
var openerURL = SimpleTest.getTestFileURL("fileapi_chromeScript.js");
var opener = SpecialPowers.loadChromeScript(openerURL);
opener.addMessageListener("files.opened", onFilesOpened);
opener.sendAsyncMessage("files.open", [
testASCIIData,
testBinaryData,
null,
convertToUTF8(testTextData),
convertToUTF16(testTextData),
"",
dataurldata0,
dataurldata1,
dataurldata2,
]);
function onFilesOpened(message) {
var worker = new Worker('worker_fileReader.js');
worker.postMessage({ blobs: message,
testTextData: testTextData,
testASCIIData: testASCIIData,
testBinaryData: testBinaryData,
dataurldata0: dataurldata0,
dataurldata1: dataurldata1,
dataurldata2: dataurldata2 });
worker.onmessage = function(e) {
var msg = e.data;
if (msg.type == 'finish') {
SimpleTest.finish();
return;
}
if (msg.type == 'check') {
ok(msg.status, msg.msg);
return;
}
ok(false, "Unknown message.");
}
}
function convertToUTF16(s) {
res = "";
for (var i = 0; i < s.length; ++i) {
c = s.charCodeAt(i);
res += String.fromCharCode(c & 255, c >>> 8);
}
return res;
}
function convertToUTF8(s) {
return unescape(encodeURIComponent(s));
}
</script>
</body>
</html>

Просмотреть файл

@ -1,419 +0,0 @@
var testRanCounter = 0;
var expectedTestCount = 0;
var testSetupFinished = false;
function ok(a, msg) {
postMessage({type: 'check', status: !!a, msg: msg });
}
function is(a, b, msg) {
ok(a === b, msg);
}
function finish() {
postMessage({type: 'finish'});
}
function convertToUTF16(s) {
res = "";
for (var i = 0; i < s.length; ++i) {
c = s.charCodeAt(i);
res += String.fromCharCode(c & 255, c >>> 8);
}
return res;
}
function convertToUTF8(s) {
return unescape(encodeURIComponent(s));
}
function convertToDataURL(s) {
return "data:application/octet-stream;base64," + btoa(s);
}
onmessage = function(message) {
is(FileReader.EMPTY, 0, "correct EMPTY value");
is(FileReader.LOADING, 1, "correct LOADING value");
is(FileReader.DONE, 2, "correct DONE value");
// List of blobs.
var asciiFile = message.data.blobs.shift();
var binaryFile = message.data.blobs.shift();
var nonExistingFile = message.data.blobs.shift();
var utf8TextFile = message.data.blobs.shift();
var utf16TextFile = message.data.blobs.shift();
var emptyFile = message.data.blobs.shift();
var dataUrlFile0 = message.data.blobs.shift();
var dataUrlFile1 = message.data.blobs.shift();
var dataUrlFile2 = message.data.blobs.shift();
// List of buffers for testing.
var testTextData = message.data.testTextData;
var testASCIIData = message.data.testASCIIData;
var testBinaryData = message.data.testBinaryData;
var dataurldata0 = message.data.dataurldata0;
var dataurldata1 = message.data.dataurldata1;
var dataurldata2 = message.data.dataurldata2;
// Test that plain reading works and fires events as expected, both
// for text and binary reading
var onloadHasRunText = false;
var onloadStartHasRunText = false;
r = new FileReader();
is(r.readyState, FileReader.EMPTY, "correct initial text readyState");
r.onload = getLoadHandler(testASCIIData, testASCIIData.length, "plain reading");
r.addEventListener("load", function() { onloadHasRunText = true });
r.addEventListener("loadstart", function() { onloadStartHasRunText = true });
r.readAsText(asciiFile);
is(r.readyState, FileReader.LOADING, "correct loading text readyState");
is(onloadHasRunText, false, "text loading must be async");
is(onloadStartHasRunText, true, "text loadstart should fire sync");
expectedTestCount++;
var onloadHasRunBinary = false;
var onloadStartHasRunBinary = false;
r = new FileReader();
is(r.readyState, FileReader.EMPTY, "correct initial binary readyState");
r.addEventListener("load", function() { onloadHasRunBinary = true });
r.addEventListener("loadstart", function() { onloadStartHasRunBinary = true });
r.readAsBinaryString(binaryFile);
r.onload = getLoadHandler(testBinaryData, testBinaryData.length, "binary reading");
is(r.readyState, FileReader.LOADING, "correct loading binary readyState");
is(onloadHasRunBinary, false, "binary loading must be async");
is(onloadStartHasRunBinary, true, "binary loadstart should fire sync");
expectedTestCount++;
var onloadHasRunArrayBuffer = false;
var onloadStartHasRunArrayBuffer = false;
r = new FileReader();
is(r.readyState, FileReader.EMPTY, "correct initial arrayBuffer readyState");
r.addEventListener("load", function() { onloadHasRunArrayBuffer = true });
r.addEventListener("loadstart", function() { onloadStartHasRunArrayBuffer = true });
r.readAsArrayBuffer(binaryFile);
r.onload = getLoadHandlerForArrayBuffer(testBinaryData, testBinaryData.length, "array buffer reading");
is(r.readyState, FileReader.LOADING, "correct loading arrayBuffer readyState");
is(onloadHasRunArrayBuffer, false, "arrayBuffer loading must be async");
is(onloadStartHasRunArrayBuffer, true, "arrayBuffer loadstart should fire sync");
expectedTestCount++;
// Test a variety of encodings, and make sure they work properly
r = new FileReader();
r.onload = getLoadHandler(testASCIIData, testASCIIData.length, "no encoding reading");
r.readAsText(asciiFile, "");
expectedTestCount++;
r = new FileReader();
r.onload = getLoadHandler(testASCIIData, testASCIIData.length, "iso8859 reading");
r.readAsText(asciiFile, "iso-8859-1");
expectedTestCount++;
r = new FileReader();
r.onload = getLoadHandler(testTextData,
convertToUTF8(testTextData).length,
"utf8 reading");
r.readAsText(utf8TextFile, "utf8");
expectedTestCount++;
r = new FileReader();
r.readAsText(utf16TextFile, "utf-16");
r.onload = getLoadHandler(testTextData,
convertToUTF16(testTextData).length,
"utf16 reading");
expectedTestCount++;
// Test get result without reading
r = new FileReader();
is(r.readyState, FileReader.EMPTY,
"readyState in test reader get result without reading");
is(r.error, null,
"no error in test reader get result without reading");
is(r.result, null,
"result in test reader get result without reading");
// Test loading an empty file works (and doesn't crash!)
r = new FileReader();
r.onload = getLoadHandler("", 0, "empty no encoding reading");
r.readAsText(emptyFile, "");
expectedTestCount++;
r = new FileReader();
r.onload = getLoadHandler("", 0, "empty utf8 reading");
r.readAsText(emptyFile, "utf8");
expectedTestCount++;
r = new FileReader();
r.onload = getLoadHandler("", 0, "empty utf16 reading");
r.readAsText(emptyFile, "utf-16");
expectedTestCount++;
r = new FileReader();
r.onload = getLoadHandler("", 0, "empty binary string reading");
r.readAsBinaryString(emptyFile);
expectedTestCount++;
r = new FileReader();
r.onload = getLoadHandlerForArrayBuffer("", 0, "empty array buffer reading");
r.readAsArrayBuffer(emptyFile);
expectedTestCount++;
r = new FileReader();
r.onload = getLoadHandler(convertToDataURL(""), 0, "empty binary string reading");
r.readAsDataURL(emptyFile);
expectedTestCount++;
// Test reusing a FileReader to read multiple times
r = new FileReader();
r.onload = getLoadHandler(testASCIIData,
testASCIIData.length,
"to-be-reused reading text")
var makeAnotherReadListener = function(event) {
r = event.target;
r.removeEventListener("load", makeAnotherReadListener);
r.onload = getLoadHandler(testASCIIData,
testASCIIData.length,
"reused reading text");
r.readAsText(asciiFile);
};
r.addEventListener("load", makeAnotherReadListener);
r.readAsText(asciiFile);
expectedTestCount += 2;
r = new FileReader();
r.onload = getLoadHandler(testBinaryData,
testBinaryData.length,
"to-be-reused reading binary")
var makeAnotherReadListener2 = function(event) {
r = event.target;
r.removeEventListener("load", makeAnotherReadListener2);
r.onload = getLoadHandler(testBinaryData,
testBinaryData.length,
"reused reading binary");
r.readAsBinaryString(binaryFile);
};
r.addEventListener("load", makeAnotherReadListener2);
r.readAsBinaryString(binaryFile);
expectedTestCount += 2;
r = new FileReader();
r.onload = getLoadHandler(convertToDataURL(testBinaryData),
testBinaryData.length,
"to-be-reused reading data url")
var makeAnotherReadListener3 = function(event) {
r = event.target;
r.removeEventListener("load", makeAnotherReadListener3);
r.onload = getLoadHandler(convertToDataURL(testBinaryData),
testBinaryData.length,
"reused reading data url");
r.readAsDataURL(binaryFile);
};
r.addEventListener("load", makeAnotherReadListener3);
r.readAsDataURL(binaryFile);
expectedTestCount += 2;
r = new FileReader();
r.onload = getLoadHandlerForArrayBuffer(testBinaryData,
testBinaryData.length,
"to-be-reused reading arrayBuffer")
var makeAnotherReadListener4 = function(event) {
r = event.target;
r.removeEventListener("load", makeAnotherReadListener4);
r.onload = getLoadHandlerForArrayBuffer(testBinaryData,
testBinaryData.length,
"reused reading arrayBuffer");
r.readAsArrayBuffer(binaryFile);
};
r.addEventListener("load", makeAnotherReadListener4);
r.readAsArrayBuffer(binaryFile);
expectedTestCount += 2;
// Test first reading as ArrayBuffer then read as something else
// (BinaryString) and doesn't crash
r = new FileReader();
r.onload = getLoadHandlerForArrayBuffer(testBinaryData,
testBinaryData.length,
"to-be-reused reading arrayBuffer")
var makeAnotherReadListener5 = function(event) {
r = event.target;
r.removeEventListener("load", makeAnotherReadListener5);
r.onload = getLoadHandler(testBinaryData,
testBinaryData.length,
"reused reading binary string");
r.readAsBinaryString(binaryFile);
};
r.addEventListener("load", makeAnotherReadListener5);
r.readAsArrayBuffer(binaryFile);
expectedTestCount += 2;
//Test data-URI encoding on differing file sizes
is(dataurldata0.length % 3, 0, "Want to test data with length % 3 == 0");
r = new FileReader();
r.onload = getLoadHandler(convertToDataURL(dataurldata0),
dataurldata0.length,
"dataurl reading, %3 = 0");
r.readAsDataURL(dataUrlFile0);
expectedTestCount++;
is(dataurldata1.length % 3, 1, "Want to test data with length % 3 == 1");
r = new FileReader();
r.onload = getLoadHandler(convertToDataURL(dataurldata1),
dataurldata1.length,
"dataurl reading, %3 = 1");
r.readAsDataURL(dataUrlFile1);
expectedTestCount++;
is(dataurldata2.length % 3, 2, "Want to test data with length % 3 == 2");
r = new FileReader();
r.onload = getLoadHandler(convertToDataURL(dataurldata2),
dataurldata2.length,
"dataurl reading, %3 = 2");
r.readAsDataURL(dataUrlFile2),
expectedTestCount++;
// Test abort()
var abortHasRun = false;
var loadEndHasRun = false;
r = new FileReader();
r.onabort = function (event) {
is(abortHasRun, false, "abort should only fire once");
is(loadEndHasRun, false, "loadend shouldn't have fired yet");
abortHasRun = true;
is(event.target.readyState, FileReader.DONE, "should be DONE while firing onabort");
is(event.target.error.name, "AbortError", "error set to AbortError for aborted reads");
is(event.target.result, null, "file data should be null on aborted reads");
}
r.onloadend = function (event) {
is(abortHasRun, true, "abort should fire before loadend");
is(loadEndHasRun, false, "loadend should only fire once");
loadEndHasRun = true;
is(event.target.readyState, FileReader.DONE, "should be DONE while firing onabort");
is(event.target.error.name, "AbortError", "error set to AbortError for aborted reads");
is(event.target.result, null, "file data should be null on aborted reads");
}
r.onload = function() { ok(false, "load should not fire for aborted reads") };
r.onerror = function() { ok(false, "error should not fire for aborted reads") };
r.onprogress = function() { ok(false, "progress should not fire for aborted reads") };
var abortThrew = false;
try {
r.abort();
} catch(e) {
abortThrew = true;
}
is(abortThrew, false, "abort() never throws");
is(abortHasRun, false, "abort() is a no-op unless loading");
r.readAsText(asciiFile);
r.abort();
is(abortHasRun, true, "1 abort should fire sync");
is(loadEndHasRun, true, "loadend should fire sync");
// Test calling readAsX to cause abort()
var reuseAbortHasRun = false;
r = new FileReader();
r.onabort = function (event) { reuseAbortHasRun = true; }
r.onload = function() { ok(true, "load should fire for aborted reads") };
var abortThrew = false;
try {
r.abort();
} catch(e) {
abortThrew = true;
}
is(abortThrew, false, "abort() never throws");
is(reuseAbortHasRun, false, "abort() is a no-op unless loading");
r.readAsText(asciiFile);
var readThrew = false;
try {
r.readAsText(asciiFile);
} catch(e) {
readThrew = true;
}
is(readThrew, true, "readAsText() must throw if loading");
is(reuseAbortHasRun, false, "2 abort should fire sync");
r.onload = getLoadHandler(testASCIIData, testASCIIData.length, "reuse-as-abort reading");
expectedTestCount++;
// Test reading from nonexistent files
r = new FileReader();
var didThrow = false;
r.onerror = function (event) {
is(event.target.readyState, FileReader.DONE, "should be DONE while firing onerror");
is(event.target.error.name, "NotFoundError", "error set to NotFoundError for nonexistent files");
is(event.target.result, null, "file data should be null on aborted reads");
testHasRun();
};
r.onload = function (event) {
is(false, "nonexistent file shouldn't load! (FIXME: bug 1122788)");
testHasRun();
};
try {
r.readAsDataURL(nonExistingFile);
expectedTestCount++;
} catch(ex) {
didThrow = true;
}
// Once this test passes, we should test that onerror gets called and
// that the FileReader object is in the right state during that call.
is(didThrow, false, "shouldn't throw when opening nonexistent file, should fire error instead");
function getLoadHandler(expectedResult, expectedLength, testName) {
return function (event) {
is(event.target.readyState, FileReader.DONE,
"readyState in test " + testName);
is(event.target.error, null,
"no error in test " + testName);
is(event.target.result, expectedResult,
"result in test " + testName);
is(event.lengthComputable, true,
"lengthComputable in test " + testName);
is(event.loaded, expectedLength,
"loaded in test " + testName);
is(event.total, expectedLength,
"total in test " + testName);
testHasRun();
}
}
function getLoadHandlerForArrayBuffer(expectedResult, expectedLength, testName) {
return function (event) {
is(event.target.readyState, FileReader.DONE,
"readyState in test " + testName);
is(event.target.error, null,
"no error in test " + testName);
is(event.lengthComputable, true,
"lengthComputable in test " + testName);
is(event.loaded, expectedLength,
"loaded in test " + testName);
is(event.total, expectedLength,
"total in test " + testName);
is(event.target.result.byteLength, expectedLength,
"array buffer size in test " + testName);
var u8v = new Uint8Array(event.target.result);
is(String.fromCharCode.apply(String, u8v), expectedResult,
"array buffer contents in test " + testName);
u8v = null;
is(event.target.result.byteLength, expectedLength,
"array buffer size after gc in test " + testName);
u8v = new Uint8Array(event.target.result);
is(String.fromCharCode.apply(String, u8v), expectedResult,
"array buffer contents after gc in test " + testName);
testHasRun();
}
}
function testHasRun() {
//alert(testRanCounter);
++testRanCounter;
if (testRanCounter == expectedTestCount) {
is(testSetupFinished, true, "test setup should have finished; check for exceptions");
is(onloadHasRunText, true, "onload text should have fired by now");
is(onloadHasRunBinary, true, "onload binary should have fired by now");
finish();
}
}
testSetupFinished = true;
}