tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_synchronized_animation.html (3474B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <!--
      4 https://bugzilla.mozilla.org/show_bug.cgi?id=867758
      5 -->
      6 <head>
      7  <title>Test for Bug 867758</title>
      8  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      9  <script src="/tests/SimpleTest/WindowSnapshot.js"></script>
     10  <script type="application/javascript" src="imgutils.js"></script>
     11  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
     12 </head>
     13 <body>
     14 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=867758">Mozilla Bug 867758</a>
     15 <p id="display"></p>
     16 <div id="content">
     17 </div>
     18 <pre id="test">
     19 <script type="application/javascript">
     20 /** Test for Bug 867758*/
     21 
     22 SimpleTest.requestFlakyTimeout("Early failure timeout");
     23 SimpleTest.waitForExplicitFinish();
     24 
     25 const FAILURE_TIMEOUT = 120000; // Fail early after 120 seconds (2 minutes)
     26 
     27 const Cc = SpecialPowers.Cc;
     28 const Ci = SpecialPowers.Ci;
     29 const gContent = document.getElementById("content");
     30 
     31 var gDispatched = false;
     32 var gRanEvent = false;
     33 var gObserver;
     34 var gImg1;
     35 var gImg2;
     36 var gFirstImageLoaded = false;
     37 var gOuter;
     38 var gFinished = false;
     39 var gFirstRequest = null;
     40 
     41 function cleanUpAndFinish() {
     42  if (gFinished) {
     43    return;
     44  }
     45  var imgLoadingContent = SpecialPowers.wrap(gImg1);
     46  imgLoadingContent.removeObserver(gOuter);
     47 
     48  imgLoadingContent = SpecialPowers.wrap(gImg2);
     49  imgLoadingContent.removeObserver(gOuter);
     50 
     51  SimpleTest.finish();
     52 
     53  gFinished = true;
     54 }
     55 
     56 function frameUpdate(aRequest) {
     57  if (!gDispatched) {
     58    Promise.resolve().then(function() { 
     59      gRanEvent = true;
     60    });
     61    gDispatched = true;
     62    gFirstRequest = aRequest;
     63  } else if (aRequest != gFirstRequest) {
     64    ok(!gRanEvent, "Should not have run event before all frame update events occurred!");
     65    cleanUpAndFinish();
     66  }
     67 }
     68 
     69 function failTest() {
     70  ok(false, "timing out after " + FAILURE_TIMEOUT + "ms.  ");
     71  cleanUpAndFinish();
     72 }
     73 
     74 function waitForLoadAndTest(image) {
     75  return () => {
     76    // Draw the image into a canvas to ensure it's decoded.
     77    var canvas = document.createElement('canvas');
     78    var context = canvas.getContext('2d');
     79    context.drawImage(image, 0, 0);
     80 
     81    // Attach the observer.
     82    var imgLoadingContent = SpecialPowers.wrap(image);
     83    imgLoadingContent.addObserver(gOuter);
     84 
     85    // If the other image already loaded, add both images to the document, which
     86    // begins the real test.
     87    if (gFirstImageLoaded) {
     88      gContent.appendChild(gImg1);
     89      gContent.appendChild(gImg2);
     90    } else {
     91      gFirstImageLoaded = true;
     92    }
     93  };
     94 }
     95 
     96 function main() {
     97  gImg1 = new Image();
     98  gImg2 = new Image();
     99 
    100  // Create and customize decoder observer
    101  var obs = new ImageDecoderObserverStub();
    102  obs.frameUpdate = frameUpdate;
    103 
    104  gOuter = Cc["@mozilla.org/image/tools;1"].getService(Ci.imgITools).createScriptedObserver(SpecialPowers.wrapCallbackObject(obs));
    105 
    106  // We want to test the cold loading behavior, so clear cache in case an
    107  // earlier test got our image in there already.
    108  clearAllImageCaches();
    109 
    110  // These are two copies of the same image; hence, they have the same frame rate.
    111  gImg1.src = "animated1.gif";
    112  gImg2.src = "animated2.gif";
    113 
    114  // Wait for each image to load.
    115  gImg1.addEventListener('load', waitForLoadAndTest(gImg1));
    116  gImg2.addEventListener('load', waitForLoadAndTest(gImg2));
    117 
    118  // In case something goes wrong, fail earlier than mochitest timeout,
    119  // and with more information.
    120  setTimeout(failTest, FAILURE_TIMEOUT);
    121 }
    122 
    123 window.onload = main;
    124 
    125 </script>
    126 </pre>
    127 </body>
    128 </html>