B2G/gecko/image/test/browser/browser_image.js
// animations to stop, and resume when we return
animations to stop, and resume when we return
animations to stop and resume when we return
function testBFCache() {
  function theTest() {
    var abort = false;
    var chances, gImage, gFrames;
    gBrowser.selectedTab = gBrowser.addTab(TESTROOT + "image.html");
    gBrowser.selectedBrowser.addEventListener("pageshow", function () {
      gBrowser.selectedBrowser.removeEventListener("pageshow", arguments.callee, true);
      var window = gBrowser.contentWindow;
      if (!actOnMozImage(window.document, "img1", function(image) {
        gImage = image;
        gFrames = gImage.framesNotified;
      })) {
        gBrowser.removeCurrentTab();
        abort = true;
      }
      goer.next();
    }, true);
    yield;
    if (abort) {
      finish();
      yield; 
    }
    chances = 120;
    do {
      gTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
      gTimer.initWithCallback(function() {
        if (gImage.framesNotified >= 20) {
          goer.send(true);
        } else {
          chances--;
          goer.send(chances == 0); 
        }
      }, 500, Ci.nsITimer.TYPE_ONE_SHOT);
    } while (!(yield));
    is(chances > 0, true, "Must have animated a few frames so far");
    gBrowser.loadURI("about:blank");
    gTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
    gTimer.initWithCallback(function() {
      gFrames = gImage.framesNotified;
      gTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
      gTimer.initWithCallback(function() {
        is(gImage.framesNotified == gFrames, true, "Must have not animated in bfcache!");
        goer.next();
      }, 4000, Ci.nsITimer.TYPE_ONE_SHOT); 
    }, 0, Ci.nsITimer.TYPE_ONE_SHOT); 
    yield;
    gBrowser.goBack();
    chances = 120;
    do {
      gTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
      gTimer.initWithCallback(function() {
        if (gImage.framesNotified - gFrames >= 20) {
          goer.send(true);
        } else {
          chances--;
          goer.send(chances == 0); 
        }
      }, 500, Ci.nsITimer.TYPE_ONE_SHOT);
    } while (!(yield));
    is(chances > 0, true, "Must have animated once out of bfcache!");
    var doc = gBrowser.selectedBrowser.contentWindow.document;
    var div = doc.getElementById("background_div");
    div.innerHTML += '<img src="animated2.gif" id="img3">';
    actOnMozImage(doc, "img3", function(image) {
      is(Math.abs(image.framesNotified - gImage.framesNotified)/gImage.framesNotified < 0.5, true,
         "Must have also animated the background image, and essentially the same # of frames");
    });
    gBrowser.removeCurrentTab();
    nextTest();
  }
  var goer = theTest();
  goer.next();
}
function testBFCache ( ) { function theTest ( ) { var abort = false ; var chances , gImage , gFrames ; gBrowser . selectedTab = gBrowser . addTab ( TESTROOT + "image.html" ) ; gBrowser . selectedBrowser . addEventListener ( "pageshow" , function ( ) { gBrowser . selectedBrowser . removeEventListener ( "pageshow" , arguments . callee , true ) ; var window = gBrowser . contentWindow ; if ( ! actOnMozImage ( window . document , "img1" , function ( image ) { gImage = image ; gFrames = gImage . framesNotified ; } ) ) { gBrowser . removeCurrentTab ( ) ; abort = true ; } goer . next ( ) ; } , true ) ; yield ; if ( abort ) { finish ( ) ; yield ; } chances = 120 ; do { gTimer = Cc [ "@mozilla.org/timer;1" ] . createInstance ( Ci . nsITimer ) ; gTimer . initWithCallback ( function ( ) { if ( gImage . framesNotified >= 20 ) { goer . send ( true ) ; } else { chances -- ; goer . send ( chances == 0 ) ; } } , 500 , Ci . nsITimer . TYPE_ONE_SHOT ) ; } while ( ! ( yield ) ) ; is ( chances > 0 , true , "Must have animated a few frames so far" ) ; gBrowser . loadURI ( "about:blank" ) ; gTimer = Cc [ "@mozilla.org/timer;1" ] . createInstance ( Ci . nsITimer ) ; gTimer . initWithCallback ( function ( ) { gFrames = gImage . framesNotified ; gTimer = Cc [ "@mozilla.org/timer;1" ] . createInstance ( Ci . nsITimer ) ; gTimer . initWithCallback ( function ( ) { is ( gImage . framesNotified == gFrames , true , "Must have not animated in bfcache!" ) ; goer . next ( ) ; } , 4000 , Ci . nsITimer . TYPE_ONE_SHOT ) ; } , 0 , Ci . nsITimer . TYPE_ONE_SHOT ) ; yield ; gBrowser . goBack ( ) ; chances = 120 ; do { gTimer = Cc [ "@mozilla.org/timer;1" ] . createInstance ( Ci . nsITimer ) ; gTimer . initWithCallback ( function ( ) { if ( gImage . framesNotified - gFrames >= 20 ) { goer . send ( true ) ; } else { chances -- ; goer . send ( chances == 0 ) ; } } , 500 , Ci . nsITimer . TYPE_ONE_SHOT ) ; } while ( ! ( yield ) ) ; is ( chances > 0 , true , "Must have animated once out of bfcache!" ) ; var doc = gBrowser . selectedBrowser . contentWindow . document ; var div = doc . getElementById ( "background_div" ) ; div . innerHTML += '<img src="animated2.gif" id="img3">' ; actOnMozImage ( doc , "img3" , function ( image ) { is ( Math . abs ( image . framesNotified - gImage . framesNotified ) / gImage . framesNotified < 0.5 , true , "Must have also animated the background image, and essentially the same # of frames" ) ; } ) ; gBrowser . removeCurrentTab ( ) ; nextTest ( ) ; } var goer = theTest ( ) ; goer . next ( ) ; }
animations to stop, and resume when we return
animations to stop and resume when we return
// If false, we are in an optimized build, and we abort this and // all further tests // optimized build // Let animation run for a bit // maybe if we wait a bit, it will happen // Browse elsewhere; push our animating page into the bfcache // Wait a bit for page to fully load, then wait a while and // see that no animation occurs. // Might have a few stray frames, until other page totally loads // 4 seconds - expect 40 frames // delay of 0 - wait for next event loop // Go back // maybe if we wait a bit, it will happen // Finally, check that the css background image has essentially the same // # of frames, implying that it animated at the same times as the regular // image. We can easily retrieve regular images through their HTML image // elements, which is what we did before. For the background image, we // create a regular image now, and read the current frame count.