layout/generic/test/test_bug507902.html

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 <!DOCTYPE HTML>
michael@0 2 <html>
michael@0 3 <!--
michael@0 4 https://bugzilla.mozilla.org/show_bug.cgi?id=507902
michael@0 5 -->
michael@0 6 <head>
michael@0 7 <title>Test for Bug 507902</title>
michael@0 8 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
michael@0 9 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
michael@0 10 </head>
michael@0 11 <body>
michael@0 12 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=507902">Mozilla Bug 507902</a>
michael@0 13
michael@0 14 <iframe id="testFrameElem"></iframe>
michael@0 15
michael@0 16 <pre id="test">
michael@0 17 <script class="testbody" type="text/javascript">
michael@0 18
michael@0 19 //
michael@0 20 // Mochitest to test nsImageFrame icons
michael@0 21 //
michael@0 22 // The 'loading' icon should be displayed up until we have enough image
michael@0 23 // data to determine the frame size.
michael@0 24 //
michael@0 25 // The 'broken' icon should be displayed when the URL is invalid (either
michael@0 26 // a bad server or a file that fails to be sniffed to an appropriate
michael@0 27 // mimetype).
michael@0 28 //
michael@0 29
michael@0 30 // Boilerplate
michael@0 31 gWindowUtils = SpecialPowers.getDOMWindowUtils(window);
michael@0 32
michael@0 33 // URL + paths
michael@0 34 //
michael@0 35 // We have a separate copy of the icons in the test directory to
michael@0 36 // avoid any firefox caching mechanisms that might affect the
michael@0 37 // behavior of the load.
michael@0 38 var us = window.location.href;
michael@0 39 var baseURL = us.substring(0, us.lastIndexOf('/') + 1);
michael@0 40 var loadIconFilename = "file_LoadingImageReference.png";
michael@0 41 var imageFilename = "file_Dolske.png";
michael@0 42 var brokenIconFilename = "file_BrokenImageReference.png";
michael@0 43 var serverFilename = "file_IconTestServer.sjs";
michael@0 44 var serverContinueFlag = "?continue=true";
michael@0 45 var bogusFilename = "oneuponatimewhendolskewasyoung.png";
michael@0 46
michael@0 47 // Our test image element, inside a div, inside an iframe
michael@0 48 var testFrameElem = document.getElementById("testFrameElem");
michael@0 49 var innerDoc = testFrameElem.contentWindow.document;
michael@0 50 var divContainer = innerDoc.createElement("div");
michael@0 51 divContainer.style.cssFloat = "left";
michael@0 52 innerDoc.body.appendChild(divContainer);
michael@0 53 var testImageElem = new Image();
michael@0 54 divContainer.appendChild(testImageElem);
michael@0 55 var pingImage = new Image();
michael@0 56
michael@0 57 // Set up the canvases
michael@0 58 var canvases = {};
michael@0 59 var canvasNames = [ "brokenIconTest", "brokenIconReference",
michael@0 60 "loadingIconTest", "loadingIconReference",
michael@0 61 "loadedTest", "loadedReference" ];
michael@0 62 var windowElem = document.documentElement;
michael@0 63 for (var i in canvasNames) {
michael@0 64 var can = document.createElement("canvas");
michael@0 65 can.setAttribute("width", windowElem.getAttribute("width"));
michael@0 66 can.setAttribute("height", windowElem.getAttribute("height"));
michael@0 67 canvases[canvasNames[i]] = can;
michael@0 68
michael@0 69 // When the image frame has no idea how to size itself, it sizes itself
michael@0 70 // to dimensions capable of displaying the alt feedback icons. However, if
michael@0 71 // the image has been loaded before, something (I don't know what) seems to
michael@0 72 // remember the size of the last successful image for that URL. So when we
michael@0 73 // create a new image frame for that URL, it uses that size until it hears
michael@0 74 // something different. This happens through a refresh (not sure if this is
michael@0 75 // desired behavior). This means that if you refresh the test, the "loading"
michael@0 76 // icon for the test image will appear with a border that stretches further
michael@0 77 // right and down, because that URL previously displayed an image with larger
michael@0 78 // dimensions. This causes the verify stage to fail. To allow for
michael@0 79 // successful test refreshes (only useful for people, not automated tests),
michael@0 80 // we add a clipping region so that we see the left and top borders, along
michael@0 81 // with the image, but not the bottom and right borders.
michael@0 82
michael@0 83 if ((i > 1) && (i < 4)) {
michael@0 84 var ctx = can.getContext("2d");
michael@0 85 ctx.beginPath();
michael@0 86 ctx.rect(0,0, 30, 30);
michael@0 87 ctx.clip();
michael@0 88 }
michael@0 89
michael@0 90 }
michael@0 91
michael@0 92 // Stage 1 - Load the reference image for the broken icon
michael@0 93 function loadBrokenIconReference() {
michael@0 94
michael@0 95 // Debugging - Let's see if setting onload after src is a problem
michael@0 96 testImageElem.onload = function(event) { dump("test_bug507902.html DEBUG - uh oh, placeholder onload 1 called\n");};
michael@0 97
michael@0 98 // Debug - Figure out if we're getting an onerror instead of onload
michael@0 99 testImageElem.onerror = function(event) {dump("test_bug507902.html DEBUG - Got onerror for testImageElem!\n");};
michael@0 100
michael@0 101 testImageElem.src = baseURL + brokenIconFilename;
michael@0 102 stageTransition();
michael@0 103 }
michael@0 104
michael@0 105 // Stage 2 - Draw the reference image for the broken icon to a canvas
michael@0 106 function drawBrokenIconReference() {
michael@0 107
michael@0 108 enableBorderAndPad();
michael@0 109 drawWindowToCanvas("brokenIconReference");
michael@0 110 disableBorderAndPad();
michael@0 111
michael@0 112 stageTransition();
michael@0 113 }
michael@0 114
michael@0 115 // Stage 3 - Load the reference image for the loading icon
michael@0 116 function loadLoadingIconReference() {
michael@0 117
michael@0 118 // Debugging - Let's see if setting onload after src is a problem
michael@0 119 testImageElem.onload = function(event) { dump("test_bug507902.html DEBUG - uh oh, placeholder onload 3 called\n");};
michael@0 120
michael@0 121 testImageElem.src = baseURL + loadIconFilename;
michael@0 122 stageTransition();
michael@0 123 }
michael@0 124
michael@0 125 // Stage 4 - Draw the reference image for the loading icon to a canvas
michael@0 126 function drawLoadingIconReference() {
michael@0 127
michael@0 128 enableBorderAndPad();
michael@0 129 drawWindowToCanvas("loadingIconReference");
michael@0 130 disableBorderAndPad();
michael@0 131
michael@0 132 stageTransition();
michael@0 133 }
michael@0 134
michael@0 135 // Stage 5 - Try to load a broken image
michael@0 136 function loadBrokenImage() {
michael@0 137 resetImage();
michael@0 138 testImageElem.src = baseURL + bogusFilename;
michael@0 139 stageTransition();
michael@0 140 }
michael@0 141
michael@0 142 // Stage 6 - Draw the screen to a canvas. This should hopefully
michael@0 143 // be the broken icon.
michael@0 144 function drawBrokenIcon() {
michael@0 145 drawWindowToCanvas("brokenIconTest");
michael@0 146 stageTransition();
michael@0 147 }
michael@0 148
michael@0 149 // Stage 7 - Load the reference image for the test image
michael@0 150 function loadImageReference() {
michael@0 151 resetImage();
michael@0 152
michael@0 153 // Debugging - Let's see if setting onload after src is a problem
michael@0 154 testImageElem.onload = function(event) { dump("test_bug507902.html DEBUG - uh oh, placeholder onload 7 called\n");};
michael@0 155
michael@0 156 testImageElem.src = baseURL + imageFilename;
michael@0 157 stageTransition();
michael@0 158 }
michael@0 159
michael@0 160 // Stage 8 - Draw the reference image for the test image to a canvas
michael@0 161 function drawImageReference() {
michael@0 162 drawWindowToCanvas("loadedReference");
michael@0 163 stageTransition();
michael@0 164 }
michael@0 165
michael@0 166 // Stage 9 - Start a load of the test image from the delay-generating server
michael@0 167 function startServerLoad() {
michael@0 168
michael@0 169 // Reset the image
michael@0 170 resetImage();
michael@0 171
michael@0 172 // Debugging info so we can figure out the hang
michael@0 173 dump("test_bug507902.html DEBUG - starting server load\n");
michael@0 174
michael@0 175 // Load the image
michael@0 176 testImageElem.src = baseURL + serverFilename;
michael@0 177 stageTransition();
michael@0 178 }
michael@0 179
michael@0 180 // Stage 10 - Draw the screen to a canvas. This should hopefully be the loading
michael@0 181 // icon.
michael@0 182 function drawLoadingIcon() {
michael@0 183
michael@0 184 // Debugging info so we can figure out the hang
michael@0 185 dump("test_bug507902.html DEBUG - drawing loading icon\n");
michael@0 186
michael@0 187 drawWindowToCanvas("loadingIconTest");
michael@0 188 stageTransition();
michael@0 189 }
michael@0 190
michael@0 191 // Stage 11 - Tell the server to continue.
michael@0 192 function signalServerContinue() {
michael@0 193
michael@0 194 // Debugging info so we can figure out the hang
michael@0 195 dump("test_bug507902.html DEBUG - signaling server to continue\n");
michael@0 196
michael@0 197 pingImage.src = baseURL + serverFilename + serverContinueFlag;
michael@0 198 stageTransition();
michael@0 199 }
michael@0 200
michael@0 201 // Stage 12 - Draw the screen to a canvas. This should hopefully be the loaded
michael@0 202 // test image.
michael@0 203 function drawLoadedImage() {
michael@0 204 drawWindowToCanvas("loadedTest");
michael@0 205 stageTransition();
michael@0 206 }
michael@0 207
michael@0 208
michael@0 209 // Stage 13 - Verify That the appropriate canvases match
michael@0 210 function verifyCanvases() {
michael@0 211
michael@0 212 // Verify the broken icon
michael@0 213 ok(canvasesAreEqual("brokenIconTest", "brokenIconReference"),
michael@0 214 "Window drawn on broken load should match broken icon reference");
michael@0 215
michael@0 216 // Verify the loading icon
michael@0 217 ok(canvasesAreEqual("loadingIconTest", "loadingIconReference"),
michael@0 218 "Window drawn mid-load should match loading icon reference");
michael@0 219
michael@0 220 // Verify the loaded image
michael@0 221 ok(canvasesAreEqual("loadedTest", "loadedReference"),
michael@0 222 "Window drawn post-load should match reference image");
michael@0 223
michael@0 224 stageTransition();
michael@0 225 }
michael@0 226
michael@0 227 // We have a bunch of different things that need to happen in order
michael@0 228 // with different transitions. We make a "stage table" here where
michael@0 229 // each entry contains the stage function ('fn') and a transition
michael@0 230 // ('trans'), which can be one of the following:
michael@0 231 // "instant" - Just calls the next stage directly
michael@0 232 // "onload" - Sets the next stage as an onload event for the image element
michael@0 233 // "onerror" - Sets the next stage as an onerror event for the image element
michael@0 234 // integer - Sets the next stage to be called after the given timeout duration
michael@0 235 // "finish" - Finish the test
michael@0 236 var testStages = [
michael@0 237 { "fn" : loadBrokenIconReference, "trans" : "onload"},
michael@0 238 { "fn" : drawBrokenIconReference, "trans" : "instant"},
michael@0 239 { "fn" : loadLoadingIconReference, "trans" : "onload" },
michael@0 240 { "fn" : drawLoadingIconReference, "trans" : "instant" },
michael@0 241 { "fn" : loadBrokenImage, "trans" : "onerror" },
michael@0 242 { "fn" : drawBrokenIcon, "trans" : "instant" },
michael@0 243 { "fn" : loadImageReference, "trans" : "onload" },
michael@0 244 { "fn" : drawImageReference, "trans" : "instant" },
michael@0 245 // XXXbholley - We use a timeout here because resetting the
michael@0 246 // image doesn't seem to be quite synchronous. If we make
michael@0 247 // this transition "instant", then the drawImage call draws
michael@0 248 // an empty (0,0,0,0) rect to the canvas and we're left with
michael@0 249 // whatever was there before. I don't know of any good event
michael@0 250 // mechanism to figure out when the image frame is bootstrapped
michael@0 251 // enough to display the loading image, so I did trial-and-error
michael@0 252 // with timeouts. 50ms seems to be enough time for things to work
michael@0 253 // reliably, so *= 6 for good measure.
michael@0 254 { "fn" : startServerLoad, "trans" : 300 },
michael@0 255 { "fn" : drawLoadingIcon, "trans" : "instant" },
michael@0 256 { "fn" : signalServerContinue, "trans" : "onload" },
michael@0 257 { "fn" : drawLoadedImage, "trans" : "instant" },
michael@0 258 { "fn" : verifyCanvases, "trans" : "finish" } ];
michael@0 259 var currentStage = 0;
michael@0 260
michael@0 261 // Transition function called at the end of each stage
michael@0 262 function stageTransition() {
michael@0 263
michael@0 264 // Debugging info so we can figure out the hang
michael@0 265 dump("test_bug507902.html DEBUG - Current Stage: " + currentStage + "\n");
michael@0 266
michael@0 267 // What's our transition?
michael@0 268 var trans = testStages[currentStage++].trans;
michael@0 269
michael@0 270 // If the transition is finish, stop now before we access out of bounds
michael@0 271 if (trans == "finish") {
michael@0 272 makeCanvasesVisible(); // Useful for debugging
michael@0 273 SimpleTest.finish();
michael@0 274 return;
michael@0 275 }
michael@0 276
michael@0 277 // Otherwise, get the next function
michael@0 278 var nextfn = testStages[currentStage].fn;
michael@0 279
michael@0 280 // Switch based on transition
michael@0 281 switch (trans) {
michael@0 282
michael@0 283 // Continue right away
michael@0 284 case "instant":
michael@0 285 nextfn();
michael@0 286 break;
michael@0 287
michael@0 288 // Continue after we get an onload event on the test image
michael@0 289 case "onload":
michael@0 290 testImageElem.onload = function(event) {testImageElem.onload = undefined; nextfn();};
michael@0 291 break;
michael@0 292
michael@0 293 // Continue after we get an onerror event on the test image
michael@0 294 case "onerror":
michael@0 295 testImageElem.onerror = function(event) {testImageElem.onerror = undefined; nextfn();};
michael@0 296 break;
michael@0 297
michael@0 298 // Timeout
michael@0 299 default:
michael@0 300 setTimeout(nextfn, trans);
michael@0 301 break
michael@0 302 }
michael@0 303 }
michael@0 304
michael@0 305 // Lots if asynchronous behavior here
michael@0 306 SimpleTest.waitForExplicitFinish();
michael@0 307
michael@0 308 // Catch somebody's eye
michael@0 309 dump("This test is failing intermittently, see bug 510001 - If you see orange here, please paste the following debugging output on the bug!\n");
michael@0 310
michael@0 311 // Kick off the test by invoking the first stage. The stages call each other
michael@0 312 testStages[0].fn();
michael@0 313
michael@0 314
michael@0 315 // We need to get rid of the old image element and make a new one. If we
michael@0 316 // don't, the "current/pending" machinery will display the old image until
michael@0 317 // the new one is loaded, so we won't see the loading icon.
michael@0 318 function resetImage() {
michael@0 319 divContainer.removeChild(testImageElem);
michael@0 320 testImageElem = null;
michael@0 321 testImageElem = new Image();
michael@0 322 divContainer.appendChild(testImageElem);
michael@0 323 }
michael@0 324
michael@0 325 //
michael@0 326 // Makes the canvases visible. Called before the tests finish. This is useful for
michael@0 327 // debugging.
michael@0 328 //
michael@0 329 function makeCanvasesVisible() {
michael@0 330 for (var i = 0; i < canvasNames.length - 1; i += 2) {
michael@0 331 var title = document.createElement("h3");
michael@0 332 title.innerHTML = canvasNames[i] + ", " + canvasNames[i+1] + ":";
michael@0 333 document.body.appendChild(title);
michael@0 334 var myDiv = document.createElement("div");
michael@0 335 myDiv.appendChild(canvases[canvasNames[i]]);
michael@0 336 myDiv.appendChild(canvases[canvasNames[i+1]]);
michael@0 337 document.body.appendChild(myDiv);
michael@0 338 }
michael@0 339 }
michael@0 340
michael@0 341 //
michael@0 342 // Enables and disables bordering/padding to mimic the look of alt feedback icons
michael@0 343 //
michael@0 344 function enableBorderAndPad() {
michael@0 345 divContainer.style.border = "1px";
michael@0 346 divContainer.style.borderStyle = "inset";
michael@0 347 testImageElem.style.padding = "3px";
michael@0 348 }
michael@0 349
michael@0 350 function disableBorderAndPad() {
michael@0 351 testImageElem.style.padding = 0;
michael@0 352 divContainer.style.border = "0px";
michael@0 353 divContainer.style.borderStyle = "";
michael@0 354 }
michael@0 355
michael@0 356 //
michael@0 357 // Helper canvas methods. This is mostly copped directly from the reftest framework
michael@0 358 //
michael@0 359
michael@0 360 function drawWindowToCanvas(canvasName) {
michael@0 361 var win = testFrameElem.contentWindow;
michael@0 362 var ctx = canvases[canvasName].getContext("2d");
michael@0 363 // drawWindow always draws one canvas pixel for each CSS pixel in the source
michael@0 364 // window, so scale the drawing to show the zoom (making each canvas pixel be one
michael@0 365 // device pixel instead)
michael@0 366 ctx.drawWindow(win, win.scrollX, win.scrollY,
michael@0 367 Math.ceil(canvases[canvasName].width),
michael@0 368 Math.ceil(canvases[canvasName].height),
michael@0 369 "rgb(255,255,255)");
michael@0 370 }
michael@0 371
michael@0 372 function canvasesAreEqual(canvas1Name, canvas2Name) {
michael@0 373 var c1 = canvases[canvas1Name];
michael@0 374 var c2 = canvases[canvas2Name];
michael@0 375 var differences = gWindowUtils.compareCanvases(c1, c2, {});
michael@0 376 return (differences == 0);
michael@0 377 }
michael@0 378
michael@0 379 </script>
michael@0 380 </pre>
michael@0 381 </body>
michael@0 382 </html>

mercurial