Bug 1202006 - Memory Blob to Temporary File - part 5 - tests, r=smaug

This commit is contained in:
Andrea Marchesini 2016-09-29 07:13:41 +02:00
parent c69e585e9c
commit a509619370
5 changed files with 206 additions and 0 deletions

View File

@ -0,0 +1,100 @@
var data = new Array(256).join("1234567890ABCDEF");
function createXHR() {
var xhr = new XMLHttpRequest();
xhr.open("POST", "temporaryFileBlob.sjs");
xhr.responseType = 'blob';
xhr.send({toString: function() { return data; }});
return xhr;
}
function test_simple() {
info("Simple test");
var xhr = createXHR();
xhr.onloadend = function() {
ok(xhr.response instanceof Blob, "We have a blob!");
is(xhr.response.size, data.length, "Data length matches");
var fr = new FileReader();
fr.readAsText(xhr.response);
fr.onload = function() {
is(fr.result, data, "Data content matches");
next();
}
}
}
function test_abort() {
info("Aborting during onloading");
var xhr = createXHR();
xhr.onprogress = function() {
xhr.abort();
}
xhr.onloadend = function() {
ok(!xhr.response, "We should not have a Blob!");
next();
}
}
function test_reuse() {
info("Reuse test");
var xhr = createXHR();
var count = 0;
xhr.onloadend = function() {
ok(xhr.response instanceof Blob, "We have a blob!");
is(xhr.response.size, data.length, "Data length matches");
var fr = new FileReader();
fr.readAsText(xhr.response);
fr.onload = function() {
is(fr.result, data, "Data content matches");
if (++count > 2) {
next();
return;
}
xhr.open("POST", "temporaryFileBlob.sjs");
xhr.responseType = 'blob';
xhr.send({toString: function() { return data; }});
}
}
}
function test_worker_generic(test) {
var w = new Worker('worker_temporaryFileBlob.js');
w.onmessage = function(e) {
if (e.data.type == 'info') {
info(e.data.msg);
} else if (e.data.type == 'check') {
ok(e.data.what, e.data.msg);
} else if (e.data.type == 'finish') {
next();
} else {
ok(false, 'Something wrong happened');
}
}
w.postMessage(test);
}
function test_worker() {
info("XHR in workers");
test_worker_generic('simple');
}
function test_worker_abort() {
info("XHR in workers");
test_worker_generic('abort');
}
function test_worker_reuse() {
info("XHR in workers");
test_worker_generic('reuse');
}

View File

@ -1,6 +1,7 @@
[DEFAULT]
support-files =
echo.sjs
temporaryFileBlob.sjs
file_html_in_xhr.html
file_html_in_xhr.sjs
file_html_in_xhr2.html
@ -56,6 +57,8 @@ support-files =
subdir/relativeLoad_sub_worker.js
subdir/relativeLoad_sub_worker2.js
subdir/relativeLoad_sub_import.js
common_temporaryFileBlob.js
worker_temporaryFileBlob.js
[test_xhr_overridemimetype_throws_on_invalid_state.html]
skip-if = buildapp == 'b2g' # Requires webgl support
@ -101,3 +104,4 @@ skip-if = buildapp == 'b2g'
skip-if = (os == "win") || (os == "mac") || toolkit == 'android' #bug 798220
[test_relativeLoad.html]
skip-if = buildapp == 'b2g' # b2g(Failed to load script: relativeLoad_import.js) b2g-debug(Failed to load script: relativeLoad_import.js) b2g-desktop(Failed to load script: relativeLoad_import.js)
[test_temporaryFileBlob.html]

View File

@ -0,0 +1,33 @@
const CC = Components.Constructor;
const BinaryInputStream = CC("@mozilla.org/binaryinputstream;1",
"nsIBinaryInputStream",
"setInputStream");
const BinaryOutputStream = CC("@mozilla.org/binaryoutputstream;1",
"nsIBinaryOutputStream",
"setOutputStream");
const Timer = CC("@mozilla.org/timer;1",
"nsITimer",
"initWithCallback");
function handleRequest(request, response) {
var bodyStream = new BinaryInputStream(request.bodyInputStream);
var bodyBytes = [];
while ((bodyAvail = bodyStream.available()) > 0)
Array.prototype.push.apply(bodyBytes, bodyStream.readByteArray(bodyAvail));
var bos = new BinaryOutputStream(response.bodyOutputStream);
response.processAsync();
var part = bodyBytes.splice(0, 256);
bos.writeByteArray(part, part.length);
response.timer1 = new Timer(function(timer) {
bos.writeByteArray(bodyBytes, bodyBytes.length);
}, 1000, Components.interfaces.nsITimer.TYPE_ONE_SHOT);
response.timer2 = new Timer(function(timer) {
response.finish();
}, 2000, Components.interfaces.nsITimer.TYPE_ONE_SHOT);
}

View File

@ -0,0 +1,40 @@
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Test for Bug 1202006</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="application/javascript" src="common_temporaryFileBlob.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<script type="application/javascript">
var tests = [
// from common_temporaryFileBlob.js:
test_simple,
test_reuse,
test_abort,
test_worker,
test_worker_reuse,
test_worker_abort,
];
function next() {
if (!tests.length) {
SimpleTest.finish();
return;
}
var test = tests.shift();
test();
}
SpecialPowers.pushPrefEnv({ "set" : [[ "dom.blob.memoryToTemporaryFile", 1 ]] },
next);
SimpleTest.waitForExplicitFinish();
</script>
</body>
</html>

View File

@ -0,0 +1,29 @@
importScripts('common_temporaryFileBlob.js');
function info(msg) {
postMessage({type: 'info', msg: msg});
}
function ok(a, msg) {
postMessage({type: 'check', what: !!a, msg: msg});
}
function is(a, b, msg) {
ok(a === b, msg);
}
function next() {
postMessage({type: 'finish'});
}
onmessage = function(e) {
if (e.data == 'simple') {
test_simple();
} else if (e.data == 'abort') {
test_abort();
} else if (e.data == 'reuse') {
test_reuse();
} else {
ok(false, 'Something wrong happened');
}
}