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