image/test/mochitest/test_removal_onload.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.

     1 <!DOCTYPE HTML>
     2 <html>
     3 <!--
     4 https://bugzilla.mozilla.org/show_bug.cgi?id=841579
     5 -->
     6 <head>
     7   <title>Test for Bug 841579</title>
     8   <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
     9   <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/WindowSnapshot.js"></script>
    10   <script type="application/javascript" src="imgutils.js"></script>
    11   <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css" />
    12 </head>
    13 <body>
    14 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=841579">Mozilla Bug 841579</a>
    15 <p id="display"></p>
    16 <div id="content">
    17 </div>
    18 <pre id="test">
    19 <script type="application/javascript;version=1.8">
    20 /** Test for Bug 841579**/
    22 SimpleTest.waitForExplicitFinish();
    24 const FAILURE_TIMEOUT = 120000; // Fail early after 120 seconds (2 minutes)
    26 const Cc = Components.classes;
    27 const Ci = Components.interfaces;
    28 const gContent = document.getElementById("content");
    30 var gImg;
    31 var gMyDecoderObserver;
    32 var gIsTestFinished = false;
    33 var gFiles;
    34 var gNotifications = 0;
    35 var gLoads = 0;
    37 function fileToLoad() {
    38   yield "red.png";
    39   yield "invalid.jpg";
    40   yield "lime100x100.svg";
    41   yield "bad.jpg";
    42   yield "rillybad.jpg";
    43 }
    45 function onSizeAvailable(aRequest) {
    46   ok(true, "AfterLoad.onSizeAvailable called for " + gImg.src);
    47 }
    48 function onLoadComplete(aRequest) {
    49   ok(true, "AfterLoad.onLoadComplete called for " + gImg.src);
    50   gLoads++;
    51   SimpleTest.executeSoon(function() {
    52     try {
    53       gContent.removeChild(gImg);
    54     } 
    55     catch (e) {} 
    56   });
    57 }
    58 function onDecodeComplete(aRequest) {
    59   ok(true, "AfterLoad.onDecodeComplete called for " + gImg.src);
    60 }
    62 function failTest() {
    63   ok(false, "timing out after " + FAILURE_TIMEOUT + "ms.  " +
    64             "currently displaying " + gImg.src);
    65   cleanUpAndFinish();
    66 }
    68 function onNotification()
    69 {
    70   gNotifications++;
    71   try {
    72     gImg.src = gFiles.next();
    73     gContent.appendChild(gImg);
    74   } catch(e) {
    75     cleanUpAndFinish();
    76   }
    77 }
    79 function cleanUpAndFinish() {
    80   // On the off chance that failTest and myOnStopFrame are triggered
    81   // back-to-back, use a flag to prevent multiple calls to SimpleTest.finish.
    82   if (gIsTestFinished) {
    83     return;
    84   }
    85   let imgLoadingContent = gImg.QueryInterface(Ci.nsIImageLoadingContent);
    86   imgLoadingContent.removeObserver(gMyDecoderObserver);
    87   // TODO: this isn't the case until post-bug 716140's refactorings
    88   // ok(gNotifications == gLoads, "Should be notified the same number of times as loads");
    89   SimpleTest.finish();
    90   gIsTestFinished = true;
    91 }
    93 function main() {
    94   gFiles = fileToLoad();
    95   gImg = new Image();
    96   gImg.onload = onNotification;
    97   gImg.onerror = onNotification;
    99   // Create, customize & attach decoder observer
   100   observer = new ImageDecoderObserverStub();
   101   observer.sizeAvailable = onSizeAvailable;
   102   observer.loadComplete = onLoadComplete;
   103   observer.decodeComplete = onDecodeComplete;
   104   gMyDecoderObserver =
   105     Cc["@mozilla.org/image/tools;1"].getService(Ci.imgITools)
   106       .createScriptedObserver(observer);
   107   let imgLoadingContent = gImg.QueryInterface(Ci.nsIImageLoadingContent);
   108   imgLoadingContent.addObserver(gMyDecoderObserver);
   110   // We want to test the cold loading behavior, so clear cache in case an
   111   // earlier test got our image in there already.
   112   clearImageCache();
   114   // kick off image-loading! myOnStopFrame handles the rest.
   115   gImg.setAttribute("src", gFiles.next());
   117   // In case something goes wrong, fail earlier than mochitest timeout,
   118   // and with more information.
   119   setTimeout(failTest, FAILURE_TIMEOUT);
   120 }
   122 window.onload = main;
   124 </script>
   125 </pre>
   126 </body>
   127 </html>

mercurial