Bug 733553 - Tests for multipart streams via mochitest-plain using httpd.js. r=joe
authorAdam Dane [:hobophobe] <unusualtears@gmail.com>
Sat, 19 May 2012 14:42:23 -0500
changeset 96721 8f1c93b5b54933a4f88c9e252bc18c3462f36882
parent 96720 ed488f577c84d1213600d2459ad913de0952adb1
child 96722 0e2cc686b07b32b0e05f2b021a19230abf654df2
child 96725 ad9940bdbd6adeee32f67d6539755bfa991e5e4a
push id1439
push userlsblakk@mozilla.com
push dateMon, 04 Jun 2012 20:19:22 +0000
treeherdermozilla-aurora@ea74834dccd3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjoe
bugs733553
milestone15.0a1
Bug 733553 - Tests for multipart streams via mochitest-plain using httpd.js. r=joe
image/test/mochitest/Makefile.in
image/test/mochitest/bug733553-iframe.html
image/test/mochitest/bug733553.sjs
image/test/mochitest/test_bug733553.html
--- a/image/test/mochitest/Makefile.in
+++ b/image/test/mochitest/Makefile.in
@@ -81,16 +81,20 @@ include $(topsrcdir)/config/rules.mk
                 test_bug89419-1.html \
                 test_bug89419-2.html \
                 test_bug552605-1.html \
                 test_bug552605-2.html \
                 bug552605.sjs \
                 bug671906-iframe.html \
                 bug671906.sjs \
                 test_bug671906.html \
+                test_bug733553.html \
+                bug733553-iframe.html \
+                bug733553.sjs \
+                animated-gif2.gif \
 		test_error_events.html \
 		error-early.png \
 		test_drawDiscardedImage.html \
                 $(NULL)
 
 # Tests disabled due to intermittent orange
 # test_bug435296.html disabled - See bug 578591
 # test_bug478398.html disabled - See bug 579139
new file mode 100644
--- /dev/null
+++ b/image/test/mochitest/bug733553-iframe.html
@@ -0,0 +1,7 @@
+<html>
+<head>
+<title>Bug 733553 iframe</title>
+<body>
+<img src="bug733553.sjs" id="image1" />
+</body>
+</html>
new file mode 100644
--- /dev/null
+++ b/image/test/mochitest/bug733553.sjs
@@ -0,0 +1,77 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+var bodyPartIndex = 0;
+var bodyParts = [
+  ["red.png", "image/png"],
+  ["red.png", "image/png"], // Tests actually begin here
+  ["animated-gif2.gif", "image/gif"],
+  ["red.png", "image/png"],
+  ["lime100x100.svg", "image/svg+xml"],
+  ["big.png", "image/png"],
+  ["animated-gif2.gif", "image/gif"],
+  ["red.png", "image/png"],
+  ["lime100x100.svg", "image/svg+xml"]
+];
+var timer = Components.classes["@mozilla.org/timer;1"];
+var partTimer = timer.createInstance(Components.interfaces.nsITimer);
+
+function getFileAsInputStream(aFilename) {
+  var file = Components.classes["@mozilla.org/file/directory_service;1"]
+             .getService(Components.interfaces.nsIProperties)
+             .get("CurWorkD", Components.interfaces.nsIFile);
+
+  file.append("tests");
+  file.append("image");
+  file.append("test");
+  file.append("mochitest");
+  file.append(aFilename);
+
+  var fileStream = Components.classes['@mozilla.org/network/file-input-stream;1']
+                   .createInstance(Components.interfaces.nsIFileInputStream);
+  fileStream.init(file, 1, 0, false);
+  return fileStream;
+}
+
+function handleRequest(request, response)
+{
+  response.setHeader("Content-Type",
+                     "multipart/x-mixed-replace;boundary=BOUNDARYOMG", false);
+  response.setHeader("Cache-Control", "no-cache", false);
+  response.setStatusLine(request.httpVersion, 200, "OK");
+  // We're sending parts off in a delayed fashion, to let the tests occur.
+  response.processAsync();
+  response.write("--BOUNDARYOMG\r\n");
+  sendParts(response);
+}
+
+function sendParts(response) {
+  if (bodyParts.length > bodyPartIndex) {
+    partTimer.initWithCallback(getSendNextPart(response), 1000,
+                               Components.interfaces.nsITimer.TYPE_ONE_SHOT);
+  }
+  else {
+    sendClose(response);
+  }
+}
+
+function sendClose(response) {
+  response.write("--BOUNDARYOMG--\r\n");
+  response.finish();
+}
+
+function getSendNextPart(response) {
+  var part = bodyParts[bodyPartIndex++];
+  var nextPartHead = "Content-Type: " + part[1] + "\r\n\r\n";
+  var inputStream = getFileAsInputStream(part[0]);
+  return function () {
+    response.bodyOutputStream.write(nextPartHead, nextPartHead.length);
+    response.bodyOutputStream.writeFrom(inputStream, inputStream.available());
+    inputStream.close();
+    // Toss in the boundary, so the browser can know this part is complete
+    response.write("--BOUNDARYOMG\r\n");
+    sendParts(response);
+  }
+}
+
new file mode 100644
--- /dev/null
+++ b/image/test/mochitest/test_bug733553.html
@@ -0,0 +1,55 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=733553
+-->
+<head>
+  <title>Test for Bug 733553</title>
+  <script type="application/javascript" src="/MochiKit/MochiKit.js"></script>
+  <script type="application/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=733553">Mozilla Bug 733553</a>
+<p id="display"></p>
+<pre id="test">
+<script type="application/javascript">
+
+SimpleTest.waitForExplicitFinish();
+
+var testIndex = 0;
+var testParts = [
+  [1, "red.png"],
+  [40, "animated-gif2.gif"],
+  [1, "red.png"],
+  [100, "lime100x100.svg"],
+  [3000, "big.png"],
+  [40, "animated-gif2.gif"],
+  [1, "red.png"]
+];
+
+function initializeOnload() {
+  var iframeelem = document.getElementById('test-iframe');
+  var firstimg = iframeelem.contentDocument.getElementById('image1');
+  firstimg.addEventListener("load", imageLoad, false);
+}
+
+function imageLoad(aEvent) {
+  if (testParts.length > testIndex) {
+    var [width, fileName] = testParts[testIndex++];
+    is(aEvent.target.width, width,
+       "Test " + (testIndex - 1) + " " + fileName + " width not correct");
+  }
+  else {
+    aEvent.target.removeEventListener("load", arguments.callee, false);
+    SimpleTest.finish();
+  }
+}
+
+</script>
+</pre>
+<div id="content"> <!-- style="display: none" -->
+<iframe id="test-iframe" src="http://mochi.test:8888/tests/image/test/mochitest/bug733553-iframe.html" onload="initializeOnload()"></iframe>
+</div>
+</body>
+</html>