modules/libpr0n/test/mochitest/test_bug496292.html
author Benjamin Smedberg <benjamin@smedbergs.us>
Mon, 21 Jun 2010 11:41:42 -0400
changeset 47008 79ae7224e6273199d0db9638043e531942bc10fd
parent 34276 9e89c0d4e570640af0b78e6c18aa39f1960448ad
permissions -rw-r--r--
Merge mozilla-central into the "branch" for but 568691. The omnijar XPT/component/chrome registration was removed during this merge, and will be re-added to the component manager shortly.

<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=496292
-->
<head>
  <title>Test for Bug 496292</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=496292">Mozilla Bug 496292</a>
<p id="display"></p>
<div id="content" style="display: none">
<canvas id="canvas" width="100" height="100"> </canvas>
</div>
<pre id="test">
<script type="application/javascript">

netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

const Ci = Components.interfaces;
const Cc = Components.classes;

SimpleTest.waitForExplicitFinish();

var canvas = document.getElementById('canvas');
var first, second, third, ref;

RemoteCanvas = function(url) {
    this.url = url;
};

RemoteCanvas.CANVAS_WIDTH = 100;
RemoteCanvas.CANVAS_HEIGHT = 100;

RemoteCanvas.prototype.load = function(cb) {
    this.cb = cb;

    var windowWidth = window.innerWidth - 25;
    var iframe;
    iframe = document.createElement("iframe");
    iframe.id = "test-iframe-" + this.url;
    iframe.height = "10px";
    iframe.width = windowWidth + "px";
    iframe.style.visibility = "hidden";
    iframe.src = this.url;
    // Here is where the magic happens... add a listener to the
    // frame's onload event - it will call handleEvent
    iframe.addEventListener("load", this, true);
    //append to the end of the page
    window.document.body.appendChild(iframe);
};

RemoteCanvas.prototype.reload = function(cb, force) {
    this.cb = cb;
    window.frames[0].location.reload(force);
}

RemoteCanvas.prototype.handleEvent = function() {
    netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");

    // Look back up the iframe by id
    var ldrFrame = document.getElementById("test-iframe-" + this.url);
    // Get a reference to the window object you need for the canvas
    // drawWindow method
    var remoteWindow = ldrFrame.contentWindow;

    //Draw canvas
    canvas.style.width = RemoteCanvas.CANVAS_WIDTH + "px";
    canvas.style.height = RemoteCanvas.CANVAS_HEIGHT + "px";
    canvas.width = RemoteCanvas.CANVAS_WIDTH;
    canvas.height = RemoteCanvas.CANVAS_HEIGHT;
    var windowWidth = window.innerWidth - 25;
    var windowHeight = window.innerHeight;

    var ctx = canvas.getContext("2d");
    ctx.clearRect(0, 0,
                  RemoteCanvas.CANVAS_WIDTH,
                  RemoteCanvas.CANVAS_HEIGHT);
    ctx.save();
    ctx.scale(RemoteCanvas.CANVAS_WIDTH / windowWidth,
              RemoteCanvas.CANVAS_HEIGHT / windowHeight);
    ctx.drawWindow(remoteWindow,
                   0, 0,
                   windowWidth, windowHeight,
                   "rgb(0,0,0)");
    ctx.restore();
    this.cb();
};

function loadFirst()
{
  ref = canvas.toDataURL();

  var remoteCanvas = new RemoteCanvas("bug496292-iframe-1.html");
  remoteCanvas.load(checkFirst);
}

function checkFirst()
{
  first = canvas.toDataURL();
  is(first, ref, "The default Accept header used by image loader is correct");

  netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
  Cc["@mozilla.org/preferences-service;1"].
  getService(Ci.nsIPrefBranch).
  setCharPref("image.http.accept", "image/png");

  var remoteCanvas = new RemoteCanvas("bug496292-iframe-2.html");
  remoteCanvas.load(checkSecond);
}

function checkSecond()
{
  second = canvas.toDataURL();
  is(second, ref, "The modified Accept header used by image loader is correct");

  netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
  try {
    Cc["@mozilla.org/preferences-service;1"].
    getService(Ci.nsIPrefBranch).
    clearUserPref("image.http.accept");
  } catch (ex) { }

  var remoteCanvas = new RemoteCanvas("bug496292-iframe-1.html");
  remoteCanvas.load(checkThird);
}

function checkThird() {
  third = canvas.toDataURL();
  is(third, ref, "The Accept header used by image loader should be correctly reset");

  SimpleTest.finish();
}

var refCanvas = new RemoteCanvas("bug496292-iframe-ref.html");
refCanvas.load(loadFirst);

</script>
</pre>
</body>
</html>