From 36f19e0ec49ebbd4f7c387972e3c1bb86882abd7 Mon Sep 17 00:00:00 2001 From: Andrea Marchesini Date: Wed, 28 Sep 2016 14:39:25 +0200 Subject: [PATCH] Bug 1202006 - Memory Blob to Temporary File - part 5 - tests, r=smaug --- dom/xhr/tests/common_temporaryFileBlob.js | 100 ++++++++++++++++++++++ dom/xhr/tests/mochitest.ini | 4 + dom/xhr/tests/temporaryFileBlob.sjs | 33 +++++++ dom/xhr/tests/test_temporaryFileBlob.html | 40 +++++++++ dom/xhr/tests/worker_temporaryFileBlob.js | 29 +++++++ 5 files changed, 206 insertions(+) create mode 100644 dom/xhr/tests/common_temporaryFileBlob.js create mode 100644 dom/xhr/tests/temporaryFileBlob.sjs create mode 100644 dom/xhr/tests/test_temporaryFileBlob.html create mode 100644 dom/xhr/tests/worker_temporaryFileBlob.js diff --git a/dom/xhr/tests/common_temporaryFileBlob.js b/dom/xhr/tests/common_temporaryFileBlob.js new file mode 100644 index 000000000000..81a66238dab4 --- /dev/null +++ b/dom/xhr/tests/common_temporaryFileBlob.js @@ -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'); +} diff --git a/dom/xhr/tests/mochitest.ini b/dom/xhr/tests/mochitest.ini index 8a56774fe8cc..c25d6c443067 100644 --- a/dom/xhr/tests/mochitest.ini +++ b/dom/xhr/tests/mochitest.ini @@ -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] diff --git a/dom/xhr/tests/temporaryFileBlob.sjs b/dom/xhr/tests/temporaryFileBlob.sjs new file mode 100644 index 000000000000..ff3410c78c8a --- /dev/null +++ b/dom/xhr/tests/temporaryFileBlob.sjs @@ -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); +} diff --git a/dom/xhr/tests/test_temporaryFileBlob.html b/dom/xhr/tests/test_temporaryFileBlob.html new file mode 100644 index 000000000000..e4db56abdff8 --- /dev/null +++ b/dom/xhr/tests/test_temporaryFileBlob.html @@ -0,0 +1,40 @@ + + + + + Test for Bug 1202006 + + + + + + + + diff --git a/dom/xhr/tests/worker_temporaryFileBlob.js b/dom/xhr/tests/worker_temporaryFileBlob.js new file mode 100644 index 000000000000..db40f72a8b37 --- /dev/null +++ b/dom/xhr/tests/worker_temporaryFileBlob.js @@ -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'); + } +}