test_bug496292.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <!DOCTYPE HTML>
  2. <html>
  3. <!--
  4. https://bugzilla.mozilla.org/show_bug.cgi?id=496292
  5. -->
  6. <head>
  7. <title>Test for Bug 496292</title>
  8. <script type="application/javascript" src="/MochiKit/MochiKit.js"></script>
  9. <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  10. <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  11. </head>
  12. <body>
  13. <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=496292">Mozilla Bug 496292</a>
  14. <p id="display"></p>
  15. <div id="content" style="display: none">
  16. <canvas id="canvas" width="100" height="100"> </canvas>
  17. </div>
  18. <pre id="test">
  19. <script type="application/javascript">
  20. SimpleTest.waitForExplicitFinish();
  21. var canvas = document.getElementById('canvas');
  22. var first, second, third, ref;
  23. RemoteCanvas = function(url) {
  24. this.url = url;
  25. };
  26. RemoteCanvas.CANVAS_WIDTH = 100;
  27. RemoteCanvas.CANVAS_HEIGHT = 100;
  28. RemoteCanvas.prototype.load = function(cb) {
  29. this.cb = cb;
  30. var windowWidth = window.innerWidth - 25;
  31. var iframe;
  32. iframe = document.createElement("iframe");
  33. iframe.id = "test-iframe-" + this.url;
  34. iframe.height = "10px";
  35. iframe.width = windowWidth + "px";
  36. iframe.style.visibility = "hidden";
  37. iframe.src = this.url;
  38. // Here is where the magic happens... add a listener to the
  39. // frame's onload event - it will call handleEvent
  40. iframe.addEventListener("load", this, true);
  41. //append to the end of the page
  42. window.document.body.appendChild(iframe);
  43. };
  44. RemoteCanvas.prototype.reload = function(cb, force) {
  45. this.cb = cb;
  46. window.frames[0].location.reload(force);
  47. }
  48. RemoteCanvas.prototype.handleEvent = function() {
  49. // Look back up the iframe by id
  50. var ldrFrame = document.getElementById("test-iframe-" + this.url);
  51. // Get a reference to the window object you need for the canvas
  52. // SpecialPowers.snapshotRect method
  53. var remoteWindow = ldrFrame.contentWindow;
  54. //Draw canvas
  55. canvas.style.width = RemoteCanvas.CANVAS_WIDTH + "px";
  56. canvas.style.height = RemoteCanvas.CANVAS_HEIGHT + "px";
  57. canvas.width = RemoteCanvas.CANVAS_WIDTH;
  58. canvas.height = RemoteCanvas.CANVAS_HEIGHT;
  59. var windowWidth = window.innerWidth - 25;
  60. var windowHeight = window.innerHeight;
  61. var rect = { left: 0, top: 0, width: windowWidth, height: windowHeight };
  62. var snapshot = SpecialPowers.snapshotRect(remoteWindow, rect, "rgb(0,0,0)");
  63. var ctx = canvas.getContext("2d");
  64. ctx.clearRect(0, 0,
  65. RemoteCanvas.CANVAS_WIDTH,
  66. RemoteCanvas.CANVAS_HEIGHT);
  67. ctx.save();
  68. ctx.scale(RemoteCanvas.CANVAS_WIDTH / windowWidth,
  69. RemoteCanvas.CANVAS_HEIGHT / windowHeight);
  70. ctx.drawImage(snapshot, 0, 0);
  71. ctx.restore();
  72. this.cb();
  73. };
  74. function loadFirst()
  75. {
  76. ref = canvas.toDataURL();
  77. var remoteCanvas = new RemoteCanvas("bug496292-iframe-1.html");
  78. remoteCanvas.load(checkFirst);
  79. }
  80. function checkFirst()
  81. {
  82. first = canvas.toDataURL();
  83. is(first, ref, "The default Accept header used by image loader is correct");
  84. SpecialPowers.setCharPref("image.http.accept", "image/png");
  85. SpecialPowers.pushPrefEnv({"set": [["image.http.accept", "image/png"]]}, function() {
  86. var remoteCanvas = new RemoteCanvas("bug496292-iframe-2.html");
  87. remoteCanvas.load(checkSecond);
  88. });
  89. }
  90. function checkSecond()
  91. {
  92. second = canvas.toDataURL();
  93. is(second, ref, "The modified Accept header used by image loader is correct");
  94. SpecialPowers.pushPrefEnv({"clear": [["image.http.accept"]]}, function() {
  95. var remoteCanvas = new RemoteCanvas("bug496292-iframe-1.html");
  96. remoteCanvas.load(checkThird);
  97. });
  98. }
  99. function checkThird() {
  100. third = canvas.toDataURL();
  101. is(third, ref, "The Accept header used by image loader should be correctly reset");
  102. SimpleTest.finish();
  103. }
  104. var refCanvas = new RemoteCanvas("bug496292-iframe-ref.html");
  105. refCanvas.load(loadFirst);
  106. </script>
  107. </pre>
  108. </body>
  109. </html>