image/test/mochitest/test_bug496292.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/image/test/mochitest/test_bug496292.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,131 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<!--
     1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=496292
     1.8 +-->
     1.9 +<head>
    1.10 +  <title>Test for Bug 496292</title>
    1.11 +  <script type="application/javascript" src="/MochiKit/MochiKit.js"></script>
    1.12 +  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
    1.13 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
    1.14 +</head>
    1.15 +<body>
    1.16 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=496292">Mozilla Bug 496292</a>
    1.17 +<p id="display"></p>
    1.18 +<div id="content" style="display: none">
    1.19 +<canvas id="canvas" width="100" height="100"> </canvas>
    1.20 +</div>
    1.21 +<pre id="test">
    1.22 +<script type="application/javascript">
    1.23 +
    1.24 +SimpleTest.waitForExplicitFinish();
    1.25 +
    1.26 +var canvas = document.getElementById('canvas');
    1.27 +var first, second, third, ref;
    1.28 +
    1.29 +RemoteCanvas = function(url) {
    1.30 +    this.url = url;
    1.31 +};
    1.32 +
    1.33 +RemoteCanvas.CANVAS_WIDTH = 100;
    1.34 +RemoteCanvas.CANVAS_HEIGHT = 100;
    1.35 +
    1.36 +RemoteCanvas.prototype.load = function(cb) {
    1.37 +    this.cb = cb;
    1.38 +
    1.39 +    var windowWidth = window.innerWidth - 25;
    1.40 +    var iframe;
    1.41 +    iframe = document.createElement("iframe");
    1.42 +    iframe.id = "test-iframe-" + this.url;
    1.43 +    iframe.height = "10px";
    1.44 +    iframe.width = windowWidth + "px";
    1.45 +    iframe.style.visibility = "hidden";
    1.46 +    iframe.src = this.url;
    1.47 +    // Here is where the magic happens... add a listener to the
    1.48 +    // frame's onload event - it will call handleEvent
    1.49 +    iframe.addEventListener("load", this, true);
    1.50 +    //append to the end of the page
    1.51 +    window.document.body.appendChild(iframe);
    1.52 +};
    1.53 +
    1.54 +RemoteCanvas.prototype.reload = function(cb, force) {
    1.55 +    this.cb = cb;
    1.56 +    window.frames[0].location.reload(force);
    1.57 +}
    1.58 +
    1.59 +RemoteCanvas.prototype.handleEvent = function() {
    1.60 +    // Look back up the iframe by id
    1.61 +    var ldrFrame = document.getElementById("test-iframe-" + this.url);
    1.62 +    // Get a reference to the window object you need for the canvas
    1.63 +    // SpecialPowers.snapshotRect method
    1.64 +    var remoteWindow = ldrFrame.contentWindow;
    1.65 +
    1.66 +    //Draw canvas
    1.67 +    canvas.style.width = RemoteCanvas.CANVAS_WIDTH + "px";
    1.68 +    canvas.style.height = RemoteCanvas.CANVAS_HEIGHT + "px";
    1.69 +    canvas.width = RemoteCanvas.CANVAS_WIDTH;
    1.70 +    canvas.height = RemoteCanvas.CANVAS_HEIGHT;
    1.71 +    var windowWidth = window.innerWidth - 25;
    1.72 +    var windowHeight = window.innerHeight;
    1.73 +
    1.74 +    var rect = { left: 0, top: 0, width: windowWidth, height: windowHeight };
    1.75 +    var snapshot = SpecialPowers.snapshotRect(remoteWindow, rect, "rgb(0,0,0)");
    1.76 +
    1.77 +    var ctx = canvas.getContext("2d");
    1.78 +    ctx.clearRect(0, 0,
    1.79 +                  RemoteCanvas.CANVAS_WIDTH,
    1.80 +                  RemoteCanvas.CANVAS_HEIGHT);
    1.81 +    ctx.save();
    1.82 +    ctx.scale(RemoteCanvas.CANVAS_WIDTH / windowWidth,
    1.83 +              RemoteCanvas.CANVAS_HEIGHT / windowHeight);
    1.84 +    ctx.drawImage(snapshot, 0, 0);
    1.85 +    ctx.restore();
    1.86 +    this.cb();
    1.87 +};
    1.88 +
    1.89 +function loadFirst()
    1.90 +{
    1.91 +  ref = canvas.toDataURL();
    1.92 +
    1.93 +  var remoteCanvas = new RemoteCanvas("bug496292-iframe-1.html");
    1.94 +  remoteCanvas.load(checkFirst);
    1.95 +}
    1.96 +
    1.97 +function checkFirst()
    1.98 +{
    1.99 +  first = canvas.toDataURL();
   1.100 +  is(first, ref, "The default Accept header used by image loader is correct");
   1.101 +
   1.102 +  SpecialPowers.setCharPref("image.http.accept", "image/png");
   1.103 +
   1.104 +  var remoteCanvas = new RemoteCanvas("bug496292-iframe-2.html");
   1.105 +  remoteCanvas.load(checkSecond);
   1.106 +}
   1.107 +
   1.108 +function checkSecond()
   1.109 +{
   1.110 +  second = canvas.toDataURL();
   1.111 +  is(second, ref, "The modified Accept header used by image loader is correct");
   1.112 +
   1.113 +  try {
   1.114 +    SpecialPowers.clearUserPref("image.http.accept");
   1.115 +  } catch (ex) { }
   1.116 +
   1.117 +  var remoteCanvas = new RemoteCanvas("bug496292-iframe-1.html");
   1.118 +  remoteCanvas.load(checkThird);
   1.119 +}
   1.120 +
   1.121 +function checkThird() {
   1.122 +  third = canvas.toDataURL();
   1.123 +  is(third, ref, "The Accept header used by image loader should be correctly reset");
   1.124 +
   1.125 +  SimpleTest.finish();
   1.126 +}
   1.127 +
   1.128 +var refCanvas = new RemoteCanvas("bug496292-iframe-ref.html");
   1.129 +refCanvas.load(loadFirst);
   1.130 +
   1.131 +</script>
   1.132 +</pre>
   1.133 +</body>
   1.134 +</html>

mercurial