imported patch blobset5.patch
authorAndrea Marchesini <amarchesini@mozilla.com>
Wed, 28 Sep 2016 15:12:52 +0200
changeset 854706 167b34da447a4692c81211c6179f8265605e006b
parent 854705 4d0a808dc608a1534a7ed8f60ccd1116a76d20d6
child 854707 0328e4a2ca17b9cb93af1a5f448ed4b52a6b7b60
push id146892
push useramarchesini@mozilla.com
push dateWed, 28 Sep 2016 13:14:08 +0000
treeherdertry@0328e4a2ca17 [default view] [failures only]
milestone52.0a1
imported patch blobset5.patch
dom/xhr/tests/common_temporaryFileBlob.js
dom/xhr/tests/mochitest.ini
dom/xhr/tests/temporaryFileBlob.sjs
dom/xhr/tests/test_temporaryFileBlob.html
dom/xhr/tests/worker_temporaryFileBlob.js
new file mode 100644
--- /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');
+}
--- a/dom/xhr/tests/mochitest.ini
+++ b/dom/xhr/tests/mochitest.ini
@@ -1,11 +1,12 @@
 [DEFAULT]
 support-files =
   echo.sjs
+  temporaryFileBlob.sjs
   file_html_in_xhr.html
   file_html_in_xhr.sjs
   file_html_in_xhr2.html
   file_html_in_xhr3.html
   file_XHRDocURI.text
   file_XHRDocURI.text^headers^
   file_XHRDocURI.xml
   file_XHRDocURI.xml^headers^
@@ -51,16 +52,18 @@ support-files =
   xhr_implicit_cancel_worker.js
   relativeLoad_import.js
   relativeLoad_worker.js
   relativeLoad_worker2.js
   responseIdentical.sjs
   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
 [test_html_in_xhr.html]
 [test_sync_xhr_timer.xhtml]
 skip-if = toolkit == 'android'
 [test_xhr_abort_after_load.html]
 skip-if = toolkit == 'android'
@@ -96,8 +99,9 @@ skip-if = buildapp == 'b2g'
 [test_worker_xhr_parameters.html]
 skip-if = buildapp == 'b2g'
 [test_worker_xhr_responseURL.html]
 [test_worker_xhr_system.html]
 [test_worker_xhr_timeout.html]
 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]
new file mode 100644
--- /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);
+}
new file mode 100644
--- /dev/null
+++ b/dom/xhr/tests/test_temporaryFileBlob.html
@@ -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>
new file mode 100644
--- /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');
+  }
+}