2009-10-07 01:30:26 -07:00
|
|
|
<!DOCTYPE HTML>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<!--
|
|
|
|
https://bugzilla.mozilla.org/show_bug.cgi?id=414796
|
|
|
|
-->
|
|
|
|
<title>Test for Bug 414796</title>
|
|
|
|
<script type="text/javascript" src="/MochiKit/packed.js"></script>
|
|
|
|
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
|
|
|
|
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
|
|
|
|
</head>
|
|
|
|
|
|
|
|
<body>
|
|
|
|
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=414796">Mozilla Bug 414796</a>
|
|
|
|
<p id="display">
|
|
|
|
<input id="fileList" type="file"></input>
|
|
|
|
</p>
|
|
|
|
<div id="content" style="display: none">
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<pre id="test">
|
|
|
|
<script class="testbody" type="text/javascript">
|
2009-11-21 03:56:31 -08:00
|
|
|
const minFileSize = 20000;
|
|
|
|
var fileNum = 1;
|
|
|
|
var testRanCounter = 0;
|
|
|
|
var expectedTestCount = 0;
|
2009-10-07 01:30:26 -07:00
|
|
|
SimpleTest.waitForExplicitFinish();
|
|
|
|
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
|
|
|
|
|
2009-11-16 18:01:40 -08:00
|
|
|
is(FileReader.EMPTY, 0, "correct EMPTY value");
|
|
|
|
is(FileReader.LOADING, 1, "correct LOADING value");
|
|
|
|
is(FileReader.DONE, 2, "correct DONE value");
|
|
|
|
|
2009-11-21 03:56:31 -08:00
|
|
|
// 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;
|
2009-10-07 01:30:26 -07:00
|
|
|
}
|
2009-11-21 01:27:02 -08:00
|
|
|
|
2009-11-21 03:56:31 -08:00
|
|
|
var testASCIIData = "abcdef 123456\n";
|
|
|
|
while (testASCIIData.length < minFileSize) {
|
|
|
|
testASCIIData = testASCIIData + testASCIIData;
|
2009-10-07 01:30:26 -07:00
|
|
|
}
|
|
|
|
|
2009-11-21 03:56:31 -08:00
|
|
|
var testBinaryData = "";
|
|
|
|
for (var i = 0; i < 256; i++) {
|
|
|
|
testBinaryData += String.fromCharCode(i);
|
2009-11-21 01:27:02 -08:00
|
|
|
}
|
2009-11-21 03:56:31 -08:00
|
|
|
while (testBinaryData.length < minFileSize) {
|
|
|
|
testBinaryData = testBinaryData + testBinaryData;
|
2009-11-21 01:27:02 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-21 03:56:31 -08:00
|
|
|
//Set up files for testing
|
|
|
|
var asciiFile = createFileWithData(testASCIIData);
|
|
|
|
var binaryFile = createFileWithData(testBinaryData);
|
2009-11-21 01:27:02 -08:00
|
|
|
|
2009-11-21 03:56:31 -08:00
|
|
|
var fileList = document.getElementById('fileList');
|
|
|
|
fileList.value = "/none/existing/path/fileAPI/testing";
|
|
|
|
var nonExistingFile = fileList.files[0];
|
|
|
|
|
|
|
|
// 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 }, false);
|
|
|
|
r.addEventListener("loadstart", function() { onloadStartHasRunText = true }, false);
|
|
|
|
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 }, false);
|
|
|
|
r.addEventListener("loadstart", function() { onloadStartHasRunBinary = true }, false);
|
|
|
|
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++;
|
|
|
|
|
|
|
|
|
|
|
|
// 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(createFileWithData(convertToUTF8(testTextData)), "utf8");
|
|
|
|
expectedTestCount++;
|
|
|
|
|
|
|
|
r = new FileReader();
|
|
|
|
r.readAsText(createFileWithData(convertToUTF16(testTextData)), "utf-16");
|
|
|
|
r.onload = getLoadHandler(testTextData,
|
|
|
|
convertToUTF16(testTextData).length,
|
|
|
|
"utf16 reading");
|
|
|
|
expectedTestCount++;
|
|
|
|
|
|
|
|
r = new FileReader();
|
|
|
|
r.onload = getLoadHandler(testTextData,
|
|
|
|
convertToUTF32(testTextData).length,
|
|
|
|
"utf32 reading");
|
|
|
|
r.readAsText(createFileWithData(convertToUTF32(testTextData)), "UTF-32");
|
|
|
|
expectedTestCount++;
|
|
|
|
|
|
|
|
|
2009-11-22 11:05:12 -08:00
|
|
|
// Test reusing a FileReader to read multiple times
|
|
|
|
r = new FileReader();
|
|
|
|
r.onload = getLoadHandler(testASCIIData,
|
|
|
|
testASCIIData.length,
|
2009-11-22 22:23:23 -08:00
|
|
|
"to-be-reused reading text")
|
2009-11-22 11:05:12 -08:00
|
|
|
var makeAnotherReadListener = function(event) {
|
|
|
|
r = event.target;
|
|
|
|
r.removeEventListener("load", makeAnotherReadListener, false);
|
2009-11-22 22:23:23 -08:00
|
|
|
r.onload = getLoadHandler(testASCIIData,
|
|
|
|
testASCIIData.length,
|
|
|
|
"reused reading text");
|
|
|
|
r.readAsText(asciiFile);
|
|
|
|
};
|
|
|
|
r.addEventListener("load", makeAnotherReadListener, false);
|
|
|
|
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, false);
|
2009-11-22 11:05:12 -08:00
|
|
|
r.onload = getLoadHandler(testBinaryData,
|
|
|
|
testBinaryData.length,
|
2009-11-22 22:23:23 -08:00
|
|
|
"reused reading binary");
|
2009-11-22 11:05:12 -08:00
|
|
|
r.readAsBinaryString(binaryFile);
|
|
|
|
};
|
2009-11-22 22:23:23 -08:00
|
|
|
r.addEventListener("load", makeAnotherReadListener2, false);
|
|
|
|
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, false);
|
|
|
|
r.onload = getLoadHandler(convertToDataURL(testBinaryData),
|
|
|
|
testBinaryData.length,
|
|
|
|
"reused reading data url");
|
|
|
|
r.readAsDataURL(binaryFile);
|
|
|
|
};
|
|
|
|
r.addEventListener("load", makeAnotherReadListener3, false);
|
|
|
|
r.readAsDataURL(binaryFile);
|
2009-11-22 11:05:12 -08:00
|
|
|
expectedTestCount += 2;
|
|
|
|
|
|
|
|
|
2009-11-21 03:56:31 -08:00
|
|
|
//Test data-URI encoding on differing file sizes
|
|
|
|
dataurldata = testBinaryData.substr(0, testBinaryData.length -
|
|
|
|
testBinaryData.length % 3);
|
|
|
|
is(dataurldata.length % 3, 0, "Want to test data with length % 3 == 0");
|
|
|
|
r = new FileReader();
|
|
|
|
r.onload = getLoadHandler(convertToDataURL(dataurldata),
|
|
|
|
dataurldata.length,
|
|
|
|
"dataurl reading, %3 = 0");
|
|
|
|
r.readAsDataURL(createFileWithData(dataurldata));
|
|
|
|
expectedTestCount++;
|
|
|
|
|
|
|
|
dataurldata = testBinaryData.substr(0, testBinaryData.length - 2 -
|
|
|
|
testBinaryData.length % 3);
|
|
|
|
is(dataurldata.length % 3, 1, "Want to test data with length % 3 == 1");
|
|
|
|
r = new FileReader();
|
|
|
|
r.onload = getLoadHandler(convertToDataURL(dataurldata),
|
|
|
|
dataurldata.length,
|
|
|
|
"dataurl reading, %3 = 1");
|
|
|
|
r.readAsDataURL(createFileWithData(dataurldata));
|
|
|
|
expectedTestCount++;
|
|
|
|
|
|
|
|
dataurldata = testBinaryData.substr(0, testBinaryData.length - 1 -
|
|
|
|
testBinaryData.length % 3);
|
|
|
|
is(dataurldata.length % 3, 2, "Want to test data with length % 3 == 2");
|
|
|
|
r = new FileReader();
|
|
|
|
r.onload = getLoadHandler(convertToDataURL(dataurldata),
|
|
|
|
dataurldata.length,
|
|
|
|
"dataurl reading, %3 = 2");
|
|
|
|
r.readAsDataURL(createFileWithData(dataurldata));
|
|
|
|
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.code, FileError.ABORT_ERR, "error code set to ABORT for aborted reads");
|
|
|
|
is(event.target.result, null, "file data should be null on aborted reads");
|
2009-10-07 01:30:26 -07:00
|
|
|
}
|
2009-11-21 03:56:31 -08:00
|
|
|
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.code, FileError.ABORT_ERR, "error code set to ABORT for aborted reads");
|
|
|
|
is(event.target.result, null, "file data should be null on aborted reads");
|
2009-10-07 01:30:26 -07:00
|
|
|
}
|
2009-11-21 03:56:31 -08:00
|
|
|
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") };
|
|
|
|
r.abort();
|
|
|
|
is(abortHasRun, false, "abort() is a no-op unless loading");
|
|
|
|
r.readAsText(asciiFile);
|
|
|
|
r.abort();
|
|
|
|
is(abortHasRun, true, "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) {
|
|
|
|
is(reuseAbortHasRun, false, "abort should only fire once");
|
|
|
|
reuseAbortHasRun = true;
|
|
|
|
is(event.target.readyState, FileReader.DONE, "should be DONE while firing onabort");
|
|
|
|
is(event.target.error.code, FileError.ABORT_ERR, "error code set to ABORT for aborted reads");
|
|
|
|
is(event.target.result, null, "file data should be null on aborted reads");
|
2009-10-07 01:30:26 -07:00
|
|
|
}
|
2009-11-21 03:56:31 -08:00
|
|
|
r.onload = function() { ok(false, "load should not fire for aborted reads") };
|
|
|
|
r.abort();
|
|
|
|
is(reuseAbortHasRun, false, "abort() is a no-op unless loading");
|
|
|
|
r.readAsText(asciiFile);
|
|
|
|
r.readAsText(asciiFile);
|
|
|
|
is(reuseAbortHasRun, true, "abort should fire sync");
|
|
|
|
r.onload = getLoadHandler(testASCIIData, testASCIIData.length, "reuse-as-abort reading");
|
|
|
|
expectedTestCount++;
|
|
|
|
|
|
|
|
|
|
|
|
// Test reading from non-existing files
|
|
|
|
r = new FileReader();
|
|
|
|
var didThrow = false;
|
|
|
|
try {
|
|
|
|
r.readAsDataURL(nonExistingFile);
|
|
|
|
} catch(ex) {
|
|
|
|
didThrow = true;
|
2009-10-07 01:30:26 -07:00
|
|
|
}
|
2009-11-21 03:56:31 -08:00
|
|
|
// Once this test passes, we shoud test that onerror gets called and
|
|
|
|
// that the FileReader object is in the right state during that call.
|
|
|
|
todo(!didThrow, "shouldn't throw when opening non-existing 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,
|
|
|
|
"lengthComputable in test " + testName);
|
|
|
|
is(event.total, expectedLength,
|
|
|
|
"lengthComputable in test " + testName);
|
|
|
|
testHasRun();
|
|
|
|
}
|
2009-10-07 01:30:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function testHasRun() {
|
2009-11-21 03:56:31 -08:00
|
|
|
//alert(testRanCounter);
|
|
|
|
++testRanCounter;
|
|
|
|
if (testRanCounter == expectedTestCount) {
|
|
|
|
is(onloadHasRunText, true, "onload text should have fired by now");
|
|
|
|
is(onloadHasRunBinary, true, "onload binary should have fired by now");
|
|
|
|
SimpleTest.finish();
|
|
|
|
}
|
2009-10-07 01:30:26 -07:00
|
|
|
}
|
|
|
|
|
2009-11-21 03:56:31 -08:00
|
|
|
function createFileWithData(fileData) {
|
2009-10-07 01:30:26 -07:00
|
|
|
var dirSvc = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties);
|
|
|
|
var testFile = dirSvc.get("ProfD", Components.interfaces.nsIFile);
|
2009-11-21 03:56:31 -08:00
|
|
|
testFile.append("fileAPItestfile" + fileNum);
|
|
|
|
fileNum++;
|
2009-10-07 01:30:26 -07:00
|
|
|
var outStream = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
|
|
|
|
outStream.init(testFile, 0x02 | 0x08 | 0x20, // write, create, truncate
|
|
|
|
0666, 0);
|
|
|
|
outStream.write(fileData, fileData.length);
|
|
|
|
outStream.close();
|
|
|
|
|
|
|
|
var fileList = document.getElementById('fileList');
|
|
|
|
fileList.value = testFile.path;
|
|
|
|
|
|
|
|
return fileList.files[0];
|
|
|
|
}
|
|
|
|
|
2009-11-21 03:56:31 -08:00
|
|
|
function convertToUTF16(s) {
|
|
|
|
res = "";
|
|
|
|
for (var i = 0; i < s.length; ++i) {
|
|
|
|
c = s.charCodeAt(i);
|
|
|
|
res += String.fromCharCode(c >>> 8, c & 255);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
function convertToUTF32(s) {
|
|
|
|
res = "";
|
|
|
|
for (var i = 0; i < s.length; ++i) {
|
|
|
|
c = s.charCodeAt(i);
|
|
|
|
res += "\0\0" + String.fromCharCode(c >>> 8, c & 255);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
function convertToUTF8(s) {
|
|
|
|
return unescape(encodeURIComponent(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
function convertToDataURL(s) {
|
|
|
|
return "data:application/octet-stream;base64," + btoa(s);
|
|
|
|
}
|
|
|
|
|
2009-10-07 01:30:26 -07:00
|
|
|
</script>
|
|
|
|
</pre>
|
|
|
|
</body> </html>
|