gecko/content/base/test/test_fileapi_slice.html

406 lines
14 KiB
HTML
Raw Normal View History

<!DOCTYPE HTML>
<html>
<head>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=575946
-->
<title>Test for Bug 575946</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=575946">Mozilla Bug 575946</a>
<p id="display">
<canvas id=canvas width=1000 height=1000 hidden></canvas>
<canvas id=testcanvas hidden></canvas>
<input id="fileList" type="file"></input>
</p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
var fileNum = 1;
var testRanCounter = 0;
var expectedTestCount = 0;
SimpleTest.waitForExplicitFinish();
// Create files containing data we'll test with. We'll want long
// strings to ensure they span multiple buffers while loading
// Create a decent-sized image
cx = $("canvas").getContext('2d');
var s = cx.canvas.width;
var grad = cx.createLinearGradient(0, 0, s-1, s-1);
for (i = 0; i < 0.95; i += .1) {
grad.addColorStop(i, "white");
grad.addColorStop(i + .05, "black");
}
grad.addColorStop(1, "white");
cx.fillStyle = grad;
cx.fillRect(0, 0, s-1, s-1);
cx.fillStyle = "rgba(200, 0, 0, 0.9)";
cx.fillRect(.1 * s, .1 * s, .7 * s, .7 * s);
cx.strokeStyle = "rgba(0, 0, 130, 0.5)";
cx.lineWidth = .14 * s;
cx.beginPath();
cx.arc(.6 * s, .6 * s, .3 * s, 0, Math.PI*2, true);
cx.stroke();
cx.closePath();
cx.fillStyle = "rgb(0, 255, 0)";
cx.beginPath();
cx.arc(.1 * s, .8 * s, .1 * s, 0, Math.PI*2, true);
cx.fill();
cx.closePath();
var fileData =
atob(cx.canvas.toDataURL("image/png").substring("data:text/png;base64,".length + 1));
var memFile = cx.canvas.mozGetAsFile("image/png");
var fileFile = createFileWithData(fileData);
var size = fileData.length;
// This might fail if we dramatically improve the png encoder. If that happens
// please increase the complexity or size of the image generated above to ensure
// that we're testing with files that are large enough.
ok(size > 65536, "test data sufficiently large");
// Test that basic properties work
function testBasics(file, size, type) {
is(file.type, type, "mozGetAsFile type");
is(file.size, size, "file is correct size");
ok(file instanceof File, "file is a File");
ok(file instanceof Blob, "file is also a Blob");
var slice = file.slice(0, size);
is(slice.type, "", "full-size type");
is(slice.size, size, "full-size size");
ok(slice instanceof Blob, "slice is a Blob");
ok(!(slice instanceof File), "slice is not a File");
slice = file.slice(0, 1234);
is(slice.type, "", "sized type");
is(slice.size, 1234, "sized size");
ok(slice instanceof Blob, "slice is a Blob");
ok(!(slice instanceof File), "slice is not a File");
slice = file.slice(size-500, 1000);
is(slice.type, "", "end-sized type");
is(slice.size, 500, "end-sized size");
slice = file.slice(size+500, 1000);
is(slice.type, "", "sized type");
is(slice.size, 0, "sized size");
slice = file.slice(0, 0);
is(slice.type, "", "sized type");
is(slice.size, 0, "sized size");
slice = file.slice(1000, 0);
is(slice.type, "", "sized type");
is(slice.size, 0, "sized size");
slice = file.slice(0, size, "foo/bar");
is(slice.type, "foo/bar", "full-size foo/bar type");
is(slice.size, size, "full-size foo/bar size");
slice = file.slice(0, 5432, "foo/bar");
is(slice.type, "foo/bar", "sized foo/bar type");
is(slice.size, 5432, "sized foo/bar size");
is(slice.slice(0, 10).type, "", "slice-slice type");
is(slice.slice(0, 10).size, 10, "slice-slice size");
is(slice.slice(0, 10, "hello/world").type, "hello/world", "slice-slice hello/world type");
is(slice.slice(0, 10, "hello/world").size, 10, "slice-slice hello/world type");
}
testBasics(memFile, size, "image/png");
testBasics(fileFile, size, "");
// Test reading various slices
// Full file
testFile(memFile, fileData, "mem file");
testFile(fileFile, fileData, "file file");
// Simple slice
testFile(memFile.slice(0, 33000), fileData.substr(0, 33000), "mem file slice");
testFile(fileFile.slice(0, 33000), fileData.substr(0, 33000), "file file slice");
// Simple slice not starting at beginning
testFile(memFile.slice(1000, 33000), fileData.substr(1000, 33000), "mem file slice starting at non-zero");
testFile(fileFile.slice(1000, 33000), fileData.substr(1000, 33000), "file file slice starting at non-zero");
// Slice of slice
var memSlice = memFile.slice(0, 40000);
var fileSlice = fileFile.slice(0, 40000);
testFile(memSlice.slice(5000, 37000), fileData.substr(5000, 35000), "mem file slice slice");
testFile(fileSlice.slice(5000, 37000), fileData.substr(5000, 35000), "file file slice slice");
// ...of slice of slice
memSlice = memSlice.slice(5000, 37000).slice(400, 300);
fileSlice = fileSlice.slice(5000, 37000).slice(400, 300);
gc();
testFile(memSlice, fileData.substr(5400, 300), "mem file slice slice slice");
testFile(fileSlice, fileData.substr(5400, 300), "file file slice slice slice");
// empty slice
testFile(memFile.slice(4711, 0), "", "mem file empty slice");
testFile(fileFile.slice(4711, 0), "", "file file empty slice");
testFile(memFile.slice(0, 0), "", "mem file empty slice");
testFile(fileFile.slice(0, 0), "", "file file empty slice");
// slice at end
testFile(memFile.slice(size-1000, 1000), fileData.substr(size-1000, 1000), "mem file slice at end");
testFile(fileFile.slice(size-1000, 1000), fileData.substr(size-1000, 1000), "file file slice at end");
// slice across end
testFile(memFile.slice(size-500, 1000), fileData.substr(size-500, 500), "mem file slice across end");
testFile(fileFile.slice(size-500, 1000), fileData.substr(size-500, 500), "file file slice across end");
// slice past end
testFile(memFile.slice(size, 1000), "", "mem file slice past end");
testFile(fileFile.slice(size, 1000), "", "file file slice past end");
testFile(memFile.slice(size + 1000, 1000), "", "mem file slice past end");
testFile(fileFile.slice(size + 1000, 1000), "", "file file slice past end");
// Try loading directly from slice into an image
var testBinaryData = "";
for (var i = 0; i < 256; i++) {
testBinaryData += String.fromCharCode(i);
}
while (testBinaryData.length < 20000) {
testBinaryData += testBinaryData;
}
function imageLoadHandler(event) {
var origcanvas = $("canvas");
var testcanvas = $("testcanvas");
var image = event.target;
is(image.naturalWidth, origcanvas.width, "width correct");
is(image.naturalHeight, origcanvas.height, "height correct");
testcanvas.width = origcanvas.width;
testcanvas.height = origcanvas.height;
testcanvas.getContext("2d").drawImage(image, 0, 0);
is(testcanvas.toDataURL("image/png"), origcanvas.toDataURL("image/png"),
"correct image data");
testHasRun();
}
// image in the middle
var imgfile = createFileWithData(testBinaryData + fileData + testBinaryData);
is(imgfile.size, size + testBinaryData.length * 2, "correct file size");
var img = new Image;
img.src = URL.createObjectURL(imgfile.slice(testBinaryData.length, size));
img.onload = imageLoadHandler;
expectedTestCount++;
// image at start
var imgfile = createFileWithData(fileData + testBinaryData);
is(imgfile.size, size + testBinaryData.length, "correct file size");
var img = new Image;
img.src = URL.createObjectURL(imgfile.slice(0, size));
img.onload = imageLoadHandler;
expectedTestCount++;
// image at end
var imgfile = createFileWithData(testBinaryData + fileData);
is(imgfile.size, size + testBinaryData.length, "correct file size");
var img = new Image;
img.src = URL.createObjectURL(imgfile.slice(testBinaryData.length, size));
img.onload = imageLoadHandler;
expectedTestCount++;
// image past end
var imgfile = createFileWithData(testBinaryData + fileData);
is(imgfile.size, size + testBinaryData.length, "correct file size");
var img = new Image;
img.src = URL.createObjectURL(imgfile.slice(testBinaryData.length, size + 1000));
img.onload = imageLoadHandler;
expectedTestCount++;
// Utility functions
function testFile(file, contents, test) {
// Load file using FileReader
var r = new FileReader();
r.onload = getFileReaderLoadHandler(contents, contents.length, "FileReader.readAsBinaryString of " + test);
r.readAsBinaryString(file);
expectedTestCount++;
// Load file using URL.createObjectURL and XMLHttpRequest
var xhr = new XMLHttpRequest;
xhr.open("GET", URL.createObjectURL(file));
xhr.onload = getXHRLoadHandler(contents, contents.length, false,
"XMLHttpRequest load of " + test);
xhr.overrideMimeType('text/plain; charset=x-user-defined');
xhr.send();
expectedTestCount++;
// Send file to server using FormData and XMLHttpRequest
xhr = new XMLHttpRequest();
xhr.onload = function(event) {
checkMPSubmission(JSON.parse(event.target.responseText),
[{ name: "hello", value: "world"},
{ name: "myfile",
value: contents,
fileName: file.name || "",
contentType: file.type || "application/octet-stream" }]);
testHasRun();
}
xhr.open("POST", "../../html/content/test/form_submit_server.sjs");
var fd = new FormData;
fd.append("hello", "world");
fd.append("myfile", file);
xhr.send(fd);
expectedTestCount++;
// Send file to server using plain XMLHttpRequest
var xhr = new XMLHttpRequest;
xhr.open("POST", "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 " + test);
is(event.target.getResponseHeader("Result-Content-Length"),
file.size,
"request content-length in XMLHttpRequest send of " + test);
};
xhr.addEventListener("load",
getXHRLoadHandler(contents, contents.length, true,
"XMLHttpRequest send of " + test),
false);
xhr.overrideMimeType('text/plain; charset=x-user-defined');
xhr.send(file);
expectedTestCount++;
}
function getFileReaderLoadHandler(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();
}
}
function getXHRLoadHandler(expectedResult, expectedLength, statusWorking, testName) {
return function (event) {
is(event.target.readyState, 4,
"readyState in test " + testName);
if (statusWorking) {
is(event.target.status, 200,
"no error in test " + testName);
}
else {
todo(event.target.status, 200,
"no error in test " + testName);
}
is(convertXHRBinary(event.target.responseText), 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();
}
}
function convertXHRBinary(s) {
var res = "";
for (var i = 0; i < s.length; ++i) {
res += String.fromCharCode(s.charCodeAt(i) & 255);
}
return res;
}
function testHasRun() {
//alert(testRanCounter);
++testRanCounter;
if (testRanCounter == expectedTestCount) {
SimpleTest.finish();
}
}
function createFileWithData(fileData) {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
var dirSvc = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties);
var testFile = dirSvc.get("ProfD", Components.interfaces.nsIFile);
testFile.append("fileAPItestfile2-" + fileNum);
fileNum++;
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];
}
function gc() {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIDOMWindowUtils)
.garbageCollect();
}
function checkMPSubmission(sub, expected) {
function getPropCount(o) {
var x, l = 0;
for (x in o) ++l;
return l;
}
is(sub.length, expected.length,
"Correct number of items");
var 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");
is (getPropCount(sub[i].headers), 1,
"Wrong number of headers");
}
else {
is(sub[i].headers["Content-Disposition"],
"form-data; name=\"" + expected[i].name + "\"; filename=\"" +
expected[i].fileName + "\"",
"Correct name");
is(sub[i].headers["Content-Type"],
expected[i].contentType,
"Correct content type");
is (getPropCount(sub[i].headers), 2,
"Wrong number of headers");
}
is(sub[i].body,
expected[i].value,
"Correct value");
}
}
</script>
</pre>
</body> </html>