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 01:27:02 -08:00
|
|
|
|
|
|
|
var testCounter = 0;
|
2009-10-07 01:30:26 -07:00
|
|
|
SimpleTest.waitForExplicitFinish();
|
|
|
|
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
|
|
|
|
|
2009-11-21 01:27:02 -08:00
|
|
|
// Write a test file > 8192 characters
|
|
|
|
|
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 01:27:02 -08:00
|
|
|
var testData = "asdfblahqwer";
|
|
|
|
for (var i = 0; i < 10; i++) {
|
|
|
|
testData = testData + testData;
|
2009-10-07 01:30:26 -07:00
|
|
|
}
|
2009-11-21 01:27:02 -08:00
|
|
|
var testData2 = testData + "a";
|
|
|
|
var testData3 = testData + "as";
|
|
|
|
|
|
|
|
//Ensure we have different sizes of data for thoroughly testing data URI retrieval
|
|
|
|
is(testData.length % 3, 0, "Need to have data length % 3 == 0");
|
|
|
|
is(testData2.length % 3, 1, "Need to have data length % 3 == 1");
|
|
|
|
is(testData3.length % 3, 2, "Need to have data lenght % 3 == 2");
|
|
|
|
|
|
|
|
//Create UTF data that should be the same for UTF-16
|
|
|
|
var utf16Data = "\0a\0s\0d\0f\0b\0l\0a\0h\0q\0w\0e\0r";
|
|
|
|
for (var i = 0; i < 10; i++) {
|
|
|
|
utf16Data = utf16Data + utf16Data;
|
|
|
|
}
|
|
|
|
var utf16File = createFileWithData(utf16Data, "01");
|
2009-10-07 01:30:26 -07:00
|
|
|
|
2009-11-21 01:27:02 -08:00
|
|
|
//Create UTF data that should be the same for UTF-32
|
|
|
|
var utf32Data = "\0\0\0a\0\0\0s\0\0\0d\0\0\0f\0\0\0b\0\0\0l\0\0\0a\0\0\0h\0\0\0q\0\0\0w\0\0\0e\0\0\0r";
|
|
|
|
for (var i = 0; i < 10; i++) {
|
|
|
|
utf32Data = utf32Data + utf32Data;
|
2009-10-07 01:30:26 -07:00
|
|
|
}
|
2009-11-21 01:27:02 -08:00
|
|
|
var utf32File = createFileWithData(utf32Data, "001");
|
|
|
|
|
|
|
|
//Obtain a variety of encodings so we can test async return values
|
|
|
|
var file = createFileWithData(testData, "00");
|
|
|
|
var domFileData = file.getAsDataURL();
|
|
|
|
var domFileBinary = file.getAsBinary();
|
|
|
|
|
|
|
|
var domFileBinary2 = utf16File.getAsBinary();
|
|
|
|
var domFileBinary3 = utf32File.getAsBinary();
|
|
|
|
|
|
|
|
var request1 = new FileReader();
|
|
|
|
is(request1.readyState, FileReader.EMPTY, "correct initial readyState");
|
|
|
|
request1.onload = handleTextISO1;
|
|
|
|
request1.readAsText(file, "iso-8859-1");
|
|
|
|
is(request1.readyState, FileReader.LOADING, "correct loading readyState");
|
|
|
|
|
|
|
|
var request2 = new FileReader();
|
|
|
|
request2.onload = handleTextUTF8;
|
|
|
|
request2.readAsText(file);
|
|
|
|
|
|
|
|
var request3 = new FileReader();
|
|
|
|
request3.onload = handleTextUTF8;
|
|
|
|
request3.readAsText(file, "");
|
|
|
|
|
|
|
|
var request4 = new FileReader();
|
|
|
|
request4.onload = handleTextUTF8;
|
|
|
|
request4.readAsText(file, "UTF-8");
|
|
|
|
|
|
|
|
//Test a variety of encodings, and make sure they work properly
|
|
|
|
//Also, test a variety of the same calls with different numbers of arguments
|
|
|
|
var request5 = new FileReader();
|
|
|
|
request5.onload = handleTextUTF16;
|
|
|
|
request5.readAsText(utf16File, "UTF-16");
|
|
|
|
|
|
|
|
var request6 = new FileReader();
|
|
|
|
request6.onload = handleTextUTF32;
|
|
|
|
request6.readAsText(utf32File, "UTF-32");
|
|
|
|
|
|
|
|
//Test binary data accessor
|
|
|
|
var request7 = new FileReader();
|
|
|
|
is(request7.readyState, FileReader.EMPTY, "correct initial readyState");
|
|
|
|
request7.onload = handleDataBinary;
|
|
|
|
request7.readAsBinaryString(file);
|
|
|
|
is(request7.readyState, FileReader.LOADING, "correct loading readyState");
|
|
|
|
|
|
|
|
var request71 = new FileReader();
|
|
|
|
request71.onload = handleDataBinary16;
|
|
|
|
request71.readAsBinaryString(utf16File);
|
|
|
|
|
|
|
|
var request72 = new FileReader();
|
|
|
|
request72.onload = handleDataBinary32;
|
|
|
|
request72.readAsBinaryString(utf32File);
|
|
|
|
|
|
|
|
//Test data URI encoding on differing file sizes
|
|
|
|
//Testing data URI when length % 3 == 0
|
|
|
|
var request8 = new FileReader();
|
|
|
|
request8.onload = handleDataURI;
|
|
|
|
request8.readAsDataURL(file);
|
|
|
|
|
|
|
|
//Testing data URI when length % 3 == 1
|
|
|
|
var file2 = createFileWithData(testData2, "02");
|
|
|
|
var domFileData1 = file2.getAsDataURL();
|
|
|
|
|
|
|
|
var request9 = new FileReader();
|
|
|
|
request9.onload = handleDataURI1;
|
|
|
|
request9.readAsDataURL(file2);
|
|
|
|
|
|
|
|
//Testing data URI when length % 3 == 2
|
|
|
|
var file3 = createFileWithData(testData3, "03");
|
|
|
|
var domFileData2 = file3.getAsDataURL();
|
|
|
|
|
|
|
|
var request10 = new FileReader();
|
|
|
|
request10.onload = handleDataURI2;
|
|
|
|
request10.readAsDataURL(file3);
|
|
|
|
|
|
|
|
//Test asynchronous property of file access
|
|
|
|
var globalVar = 0;
|
|
|
|
var request105 = new FileReader();
|
|
|
|
request105.onload = incGlobalVar;
|
|
|
|
request105.readAsText(file, "");
|
|
|
|
is(globalVar, 0, "testing to make sure getAsText doesn't block subsequent execution");
|
|
|
|
|
|
|
|
//Create second file for testing cancelReads()
|
|
|
|
var dirSvc = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties);
|
|
|
|
var testFile4 = dirSvc.get("ProfD", Components.interfaces.nsIFile);
|
|
|
|
testFile4.append("testfile04");
|
|
|
|
var outStream4 = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
|
|
|
|
outStream4.init(testFile4, 0x02 | 0x08 | 0x20, 0666, 0);
|
|
|
|
outStream4.write(testData, testData.length);
|
|
|
|
outStream4.close();
|
2009-10-07 01:30:26 -07:00
|
|
|
|
2009-11-21 01:27:02 -08:00
|
|
|
var fileList = document.getElementById('fileList');
|
|
|
|
fileList.value = testFile4.path;
|
|
|
|
var file4 = fileList.files[0];
|
|
|
|
|
|
|
|
var request11 = new FileReader();
|
|
|
|
request11.onabort = handleCancel;
|
|
|
|
request11.readAsText(file4);
|
|
|
|
request11.abort();
|
|
|
|
|
|
|
|
//Test error handling - Note: currently throws exceptions
|
|
|
|
/*testFile4.permissions = 0;
|
|
|
|
var request12 = new FileReader();
|
|
|
|
request12.onerror = handleSecurityError;
|
|
|
|
request12.readAsText(file4, "");
|
|
|
|
|
|
|
|
testFile4.remove(false);
|
|
|
|
var request13 = new FileReader();
|
|
|
|
request13.onerror = handleNotFoundError;
|
|
|
|
request13.readAsText(file4, "");*/
|
|
|
|
|
|
|
|
//Corresponding callback functions
|
|
|
|
function incGlobalVar(fileAsText) {
|
|
|
|
globalVar++;
|
2009-10-07 01:30:26 -07:00
|
|
|
}
|
2009-11-21 01:27:02 -08:00
|
|
|
|
|
|
|
function handleCancel(event) {
|
|
|
|
var fileAsText = event.target.result;
|
|
|
|
var error = event.target.error;
|
|
|
|
is(error.code, FileError.ABORT_ERR, "error code set to CANCELED for canceled reads");
|
|
|
|
is(fileAsText, null, "file data should be null on canceled reads");
|
|
|
|
testHasRun();
|
2009-10-07 01:30:26 -07:00
|
|
|
}
|
|
|
|
|
2009-11-21 01:27:02 -08:00
|
|
|
function handleTextISO1(event) {
|
|
|
|
is(event.target.readyState, FileReader.DONE, "correct final readyState");
|
|
|
|
var fileAsText = event.target.result;
|
|
|
|
var error = event.target.error;
|
|
|
|
is(error, null, "error code set to null for successful data accesses");
|
|
|
|
is(testData.length, fileAsText.length, "iso-1 async length should match testdata");
|
|
|
|
is(testData, fileAsText, "iso-1 async string result should match testdata");
|
|
|
|
testHasRun();
|
|
|
|
}
|
2009-10-07 01:30:26 -07:00
|
|
|
|
2009-11-21 01:27:02 -08:00
|
|
|
function handleTextUTF8(event) {
|
|
|
|
var fileAsUTF8 = event.target.result;
|
|
|
|
var error = event.target.error;
|
|
|
|
is(error, null, "error code set to null for successful data accesses");
|
|
|
|
is(testData.length, fileAsUTF8.length, "UTF-8 async length should match testdata");
|
|
|
|
is(testData, fileAsUTF8, "UTF-8 async string result should match testdata");
|
|
|
|
testHasRun();
|
|
|
|
}
|
2009-10-07 01:30:26 -07:00
|
|
|
|
2009-11-21 01:27:02 -08:00
|
|
|
function handleTextUTF16(event) {
|
|
|
|
var fileAsUTF16 = event.target.result;
|
|
|
|
var error = event.target.error;
|
|
|
|
is(error, null, "error code set to SUCCESS for successful data accesses");
|
|
|
|
is(testData.length, fileAsUTF16.length, "UTF-16 async length should match testdata");
|
|
|
|
is(testData, fileAsUTF16, "UTF-16 async string result should match testdata");
|
|
|
|
testHasRun();
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleTextUTF32(event) {
|
|
|
|
var fileAsUTF32 = event.target.result;
|
|
|
|
var error = event.target.error;
|
|
|
|
is(error, null, "error code set to SUCCESS for successful data accesses");
|
|
|
|
is(testData.length, fileAsUTF32.length, "UTF-32 async length should match testdata");
|
|
|
|
is(testData, fileAsUTF32, "UTF-32 async string result should match testdata");
|
|
|
|
testHasRun();
|
|
|
|
}
|
|
|
|
|
|
|
|
//Tests dataURI.length % 3 == 0
|
|
|
|
function handleDataURI(event) {
|
|
|
|
var fileAsDataURI = event.target.result;
|
|
|
|
is(domFileData.length, fileAsDataURI.length, "data URI async length should match dom file data");
|
|
|
|
is(domFileData, fileAsDataURI, "data URI async string result should match dom file data");
|
|
|
|
testHasRun();
|
|
|
|
}
|
|
|
|
|
|
|
|
//Tests dataURI.length % 3 == 1
|
|
|
|
function handleDataURI1(event) {
|
|
|
|
var fileAsDataURI = event.target.result;
|
|
|
|
is(domFileData1.length, fileAsDataURI.length, "data URI async length should match dom file data1");
|
|
|
|
is(domFileData1, fileAsDataURI, "data URI async string result should match dom file data1");
|
|
|
|
testHasRun();
|
|
|
|
}
|
|
|
|
|
|
|
|
//Tests dataURI.length % 3 == 2
|
|
|
|
function handleDataURI2(event) {
|
|
|
|
var fileAsDataURI = event.target.result;
|
|
|
|
is(domFileData2.length, fileAsDataURI.length, "data URI async length should match dom file data2");
|
|
|
|
is(domFileData2, fileAsDataURI, "data URI async string result should match dom file data2");
|
|
|
|
testHasRun();
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleDataBinary(event) {
|
|
|
|
is(event.target.readyState, FileReader.DONE, "correct final readyState");
|
|
|
|
var fileAsBinary = event.target.result;
|
|
|
|
is(domFileBinary.length, fileAsBinary.length, "binary data async length should match dom file binary");
|
|
|
|
is(domFileBinary, fileAsBinary, "binary data async string result should match dom file binary");
|
|
|
|
testHasRun();
|
2009-10-07 01:30:26 -07:00
|
|
|
}
|
2009-11-21 01:27:02 -08:00
|
|
|
|
|
|
|
function handleDataBinary16(event) {
|
|
|
|
var fileAsBinary = event.target.result;
|
|
|
|
is(domFileBinary2.length, fileAsBinary.length, "binary data async length should match dom file binary16");
|
|
|
|
is(domFileBinary2, fileAsBinary, "binary data async string result should match dom file binary16");
|
|
|
|
testHasRun();
|
2009-10-07 01:30:26 -07:00
|
|
|
}
|
2009-11-21 01:27:02 -08:00
|
|
|
|
|
|
|
function handleDataBinary32(event) {
|
|
|
|
var fileAsBinary = event.target.result;
|
|
|
|
is(domFileBinary3.length, fileAsBinary.length, "binary data async length should match dom file binary32");
|
|
|
|
is(domFileBinary3, fileAsBinary, "binary data async string result should match dom file binary32");
|
|
|
|
testHasRun();
|
2009-10-07 01:30:26 -07:00
|
|
|
}
|
2009-11-21 01:27:02 -08:00
|
|
|
|
|
|
|
function handleSecurityError(event) {
|
|
|
|
var fileAsText = event.target.result;
|
|
|
|
var error = event.target.error;
|
|
|
|
is(error.code, FileError.SECURITY_ERR, "code for file security error should have value 18");
|
|
|
|
is(fileAsText, null, "file content should be null when error is encountered");
|
|
|
|
testHasRun();
|
2009-10-07 01:30:26 -07:00
|
|
|
}
|
2009-11-21 01:27:02 -08:00
|
|
|
|
|
|
|
function handleNotFoundError(event) {
|
|
|
|
var fileAsText = event.target.result;
|
|
|
|
var error = event.target.error;
|
|
|
|
is(error.code, FileError.NOT_FOUND_ERR, "code for file not found error should have value 8");
|
|
|
|
is(fileAsText, null, "file content should be null when error is encountered");
|
|
|
|
testHasRun();
|
2009-10-07 01:30:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function testHasRun() {
|
2009-11-21 01:27:02 -08:00
|
|
|
if (++testCounter == 13) SimpleTest.finish();
|
2009-10-07 01:30:26 -07:00
|
|
|
}
|
|
|
|
|
2009-11-21 01:27:02 -08:00
|
|
|
function createFileWithData(fileData, fileNum) {
|
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 01:27:02 -08:00
|
|
|
testFile.append("testfile" + 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];
|
|
|
|
}
|
|
|
|
|
|
|
|
</script>
|
|
|
|
</pre>
|
|
|
|
</body> </html>
|