plupload
Version:
Plupload is a JavaScript API for dealing with file uploads it supports features like multiple file selection, file type filtering, request chunking, client side image scaling and it uses different runtimes to achieve this such as HTML 5, Silverlight and F
364 lines (278 loc) • 8 kB
HTML
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>FileReader</title>
<script src="../loader.js"></script>
<script type="text/javascript" src="XMLHttpRequest/image-b64.js"></script>
<script type="text/javascript">
QUnit.config.reorder = false;
QUnit.config.testTimeout = 15000;
module("FileReader", {
setup: function() {
o.extend(this, {
x: o.Exceptions,
DOM: jQuery('#qunit-fixture'),
runtimeOptions: {
runtime_order: "html5,flash,silverlight,html4",
container: "qunit-fixture",
swf_url: "../../bin/flash/Moxie.swf",
xap_url: "../../bin/silverlight/Moxie.xap"
}
});
},
teardown: function() {
}
});
test("Test Error event: try to load a resource having a wrong format", function(assert) {
var fr = new o.FileReader()
, x = this.x
, order = []
;
fr.onloadstart = function() {
order.push('loadstart');
};
fr.onerror = function() {
start();
order.push('error');
assert.ok(true, "Error event fired.");
assert.equal(this.readyState, o.FileReader.DONE, "readyState switched to DONE.");
assert.deepEqual(this.error, new x.DOMException(x.DOMException.NOT_FOUND_ERR),
"Resource cannot be found or has an invalid format.");
};
fr.onloadend = function() {
order.push('loadend');
assert.ok(true, "LoadEnd event fired.");
assert.deepEqual(order, ['loadstart', 'error', 'loadend'], "Events fired in proper order: LoadStart, Error, LoadEnd.");
this.destroy();
};
stop();
fr.readAsDataURL("this is not a blob");
});
test("Load detached blob", function(assert) {
var fr = new o.FileReader()
, x = this.x
, blob
, order = []
;
blob = new o.Blob(null, {
type: 'image/jpeg',
data: o.atob(imgB64)
});
fr.onloadstart = function() {
order.push('loadstart');
};
fr.onload = function() {
start();
order.push('load');
assert.ok(true, "Load event fired.");
assert.equal(this.readyState, o.FileReader.DONE, "readyState switched to DONE.");
};
fr.onloadend = function() {
order.push('loadend');
assert.ok(true, "LoadEnd event fired.");
assert.deepEqual(order, ['loadstart', 'load', 'loadend'], "Events fired in proper order: LoadStart, Load, LoadEnd.");
this.destroy();
};
stop();
fr.readAsDataURL(blob);
});
o.each("html5,flash,silverlight,html4".split(','), function(runtime) {
test("readAsDataURL(), runtime: " + runtime, function(assert) {
var self = this
, url = o.resolveUrl("XMLHttpRequest/poster.jpg")
, xhr = new o.XMLHttpRequest()
, startTime = (new Date).getTime()
, endTime
, progressRate = 0
, events = {
loadstart: 0,
//loadend: 0,
load: 0,
progress: 0,
timeout: 0,
abort: 0,
error: 0
}
;
xhr.open('get', url);
xhr.responseType = 'blob';
xhr.onload = function() {
var blob = xhr.response;
var fr = new o.FileReader();
start();
o.each(events, function(times, e) {
if (e === 'progress') {
var prevTime = startTime;
fr.onprogress = function(e) {
var time = (new Date).getTime();
progressRate += time - prevTime;
prevTime = time;
events.progress++;
};
} else {
fr['on' + e] = function() {
events[e]++;
};
}
});
fr.onloadend = function() {
xhr.destroy();
start();
assert.equal(events.loadstart, 1, "LoadStart fired");
assert.ok(events.progress, "Progress fired " + events.progress + " times; average rate: " + Math.round(progressRate / events.progress) + "ms");
assert.ok(true, "LoadEnd fired");
var img = new o.Image();
img.onload = function() {
start();
ok(true, "Resulting dataUrl contains proper image.");
img.destroy();
fr.destroy();
};
img.onerror = function() {
start();
assert.ok(false, "Resulting dataUrl contains proper image.");
img.destroy();
fr.destroy();
};
stop();
img.load(fr.result, o.extend({}, self.runtimeOptions));
};
stop();
fr.readAsDataURL(blob);
};
xhr.bind("Error RuntimeError", function() {
start();
assert.ok(true, "Runtime not supported.");
});
stop();
xhr.send(null, o.extend({}, this.runtimeOptions, { runtime_order: runtime }));
});
});
o.each("html5,flash,silverlight,html4".split(','), function(runtime) {
test("readAsText(), runtime: " + runtime, function(assert) {
var self = this
, url = o.resolveUrl("XMLHttpRequest/json.php")
, xhr = new o.XMLHttpRequest()
, startTime = (new Date).getTime()
, endTime
, progressRate = 0
, events = {
loadstart: 0,
//loadend: 0,
load: 0,
progress: 0,
timeout: 0,
abort: 0,
error: 0
}
;
xhr.open('get', url);
xhr.responseType = 'blob';
xhr.onload = function() {
var blob = xhr.response;
var fr = new o.FileReader();
start();
o.each(events, function(times, e) {
if (e === 'progress') {
var prevTime = startTime;
fr.onprogress = function(e) {
var time = (new Date).getTime();
progressRate += time - prevTime;
prevTime = time;
events.progress++;
};
} else {
fr['on' + e] = function() {
events[e]++;
};
}
});
fr.onloadend = function() {
xhr.destroy();
start();
assert.equal(events.loadstart, 1, "LoadStart fired");
assert.ok(events.progress, "Progress fired " + events.progress + " times; average rate: " + Math.round(progressRate / events.progress) + "ms");
assert.ok(true, "LoadEnd fired");
try {
var json = JSON.parse(this.result);
assert.ok(true, "Result succesfully parsed as JSON");
assert.equal(json.info, "Some JSON here", "Parsed text is readable.");
assert.equal(json.utf, "სიურპრიზი", "Non-latin characters processed properly.");
} catch (ex) {
assert.ok(false, "Result succesfully parsed as JSON (Err.: "+ex.message+")");
}
};
stop();
fr.readAsText(blob);
};
xhr.bind("Error RuntimeError", function() {
start();
assert.ok(true, "Runtime not supported.");
});
stop();
xhr.send(null, o.extend({}, this.runtimeOptions, { runtime_order: runtime }));
});
});
// re-check for #97
o.each("html5,flash,silverlight,html4".split(','), function(runtime) {
test("Instances are independent, runtime: " + runtime, function(assert) {
var self = this
, url = o.resolveUrl("XMLHttpRequest/json.php")
, xhr = new o.XMLHttpRequest()
;
xhr.open('get', url);
xhr.responseType = 'blob';
xhr.onload = function() {
var blob = xhr.response;
var fr1, fr2;
fr1 = new o.FileReader();
start();
fr1.onload = function() {
var result1, result2;
fr2 = new o.FileReader();
fr2.onload = function() {
try {
result1 = JSON.parse(fr1.result);
} catch (ex) {
assert.ok(false, "Result 1 succesfully parsed as JSON (Err.: "+ex.message+")");
return;
}
assert.equal(result1.info, "Some JSON here", "Structure of the result is what we expected.");
assert.equal(fr1.result, fr2.result, "Both read results are identical.");
fr1.destroy();
assert.ok(!fr1.result, "First FileReader object is now destroyed.");
try {
result2 = JSON.parse(fr2.result);
} catch (ex) {
assert.ok(false, "Result 2 succesfully parsed as JSON (Err.: "+ex.message+")");
return;
}
assert.equal(result2.info, "Some JSON here", "Second FileReader still exists and is not empty.");
};
fr2.onloadend = function() {
start();
xhr.destroy();
fr1.destroy();
fr2.destroy();
};
fr2.readAsText(blob);
};
stop();
fr1.readAsText(blob);
};
xhr.bind("Error RuntimeError", function() {
start();
assert.ok(true, "Runtime not supported.");
});
stop();
xhr.send(null, o.extend({}, this.runtimeOptions, { runtime_order: runtime }));
});
});
</script>
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture" style="position: relative; top: 0 !important; left: 0 !important; width: 100%; height: 9px;"></div>
</body>
</html>