image/test/mochitest/test_bug496292.html

branch
TOR_BUG_9701
changeset 14
925c144e1f1f
equal deleted inserted replaced
-1:000000000000 0:ce5903f2a66b
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
21 SimpleTest.waitForExplicitFinish();
22
23 var canvas = document.getElementById('canvas');
24 var first, second, third, ref;
25
26 RemoteCanvas = function(url) {
27 this.url = url;
28 };
29
30 RemoteCanvas.CANVAS_WIDTH = 100;
31 RemoteCanvas.CANVAS_HEIGHT = 100;
32
33 RemoteCanvas.prototype.load = function(cb) {
34 this.cb = cb;
35
36 var windowWidth = window.innerWidth - 25;
37 var iframe;
38 iframe = document.createElement("iframe");
39 iframe.id = "test-iframe-" + this.url;
40 iframe.height = "10px";
41 iframe.width = windowWidth + "px";
42 iframe.style.visibility = "hidden";
43 iframe.src = this.url;
44 // Here is where the magic happens... add a listener to the
45 // frame's onload event - it will call handleEvent
46 iframe.addEventListener("load", this, true);
47 //append to the end of the page
48 window.document.body.appendChild(iframe);
49 };
50
51 RemoteCanvas.prototype.reload = function(cb, force) {
52 this.cb = cb;
53 window.frames[0].location.reload(force);
54 }
55
56 RemoteCanvas.prototype.handleEvent = function() {
57 // Look back up the iframe by id
58 var ldrFrame = document.getElementById("test-iframe-" + this.url);
59 // Get a reference to the window object you need for the canvas
60 // SpecialPowers.snapshotRect method
61 var remoteWindow = ldrFrame.contentWindow;
62
63 //Draw canvas
64 canvas.style.width = RemoteCanvas.CANVAS_WIDTH + "px";
65 canvas.style.height = RemoteCanvas.CANVAS_HEIGHT + "px";
66 canvas.width = RemoteCanvas.CANVAS_WIDTH;
67 canvas.height = RemoteCanvas.CANVAS_HEIGHT;
68 var windowWidth = window.innerWidth - 25;
69 var windowHeight = window.innerHeight;
70
71 var rect = { left: 0, top: 0, width: windowWidth, height: windowHeight };
72 var snapshot = SpecialPowers.snapshotRect(remoteWindow, rect, "rgb(0,0,0)");
73
74 var ctx = canvas.getContext("2d");
75 ctx.clearRect(0, 0,
76 RemoteCanvas.CANVAS_WIDTH,
77 RemoteCanvas.CANVAS_HEIGHT);
78 ctx.save();
79 ctx.scale(RemoteCanvas.CANVAS_WIDTH / windowWidth,
80 RemoteCanvas.CANVAS_HEIGHT / windowHeight);
81 ctx.drawImage(snapshot, 0, 0);
82 ctx.restore();
83 this.cb();
84 };
85
86 function loadFirst()
87 {
88 ref = canvas.toDataURL();
89
90 var remoteCanvas = new RemoteCanvas("bug496292-iframe-1.html");
91 remoteCanvas.load(checkFirst);
92 }
93
94 function checkFirst()
95 {
96 first = canvas.toDataURL();
97 is(first, ref, "The default Accept header used by image loader is correct");
98
99 SpecialPowers.setCharPref("image.http.accept", "image/png");
100
101 var remoteCanvas = new RemoteCanvas("bug496292-iframe-2.html");
102 remoteCanvas.load(checkSecond);
103 }
104
105 function checkSecond()
106 {
107 second = canvas.toDataURL();
108 is(second, ref, "The modified Accept header used by image loader is correct");
109
110 try {
111 SpecialPowers.clearUserPref("image.http.accept");
112 } catch (ex) { }
113
114 var remoteCanvas = new RemoteCanvas("bug496292-iframe-1.html");
115 remoteCanvas.load(checkThird);
116 }
117
118 function checkThird() {
119 third = canvas.toDataURL();
120 is(third, ref, "The Accept header used by image loader should be correctly reset");
121
122 SimpleTest.finish();
123 }
124
125 var refCanvas = new RemoteCanvas("bug496292-iframe-ref.html");
126 refCanvas.load(loadFirst);
127
128 </script>
129 </pre>
130 </body>
131 </html>

mercurial