tor-browser

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

browser_image.js (7229B)


      1 waitForExplicitFinish();
      2 requestLongerTimeout(2); // see bug 660123 -- this test is slow on Mac.
      3 
      4 // A hold on the current timer, so it doesn't get GCed out from
      5 // under us
      6 var gTimer;
      7 
      8 // Browsing to a new URL - pushing us into the bfcache - should cause
      9 // animations to stop, and resume when we return
     10 /* global yield */
     11 function testBFCache() {
     12  function theTest() {
     13    var abort = false;
     14    var chances, gImage, gFrames;
     15    gBrowser.selectedTab = BrowserTestUtils.addTab(
     16      gBrowser,
     17      TESTROOT + "image.html"
     18    );
     19    gBrowser.selectedBrowser.addEventListener(
     20      "pageshow",
     21      function () {
     22        var window = gBrowser.contentWindow;
     23        // If false, we are in an optimized build, and we abort this and
     24        // all further tests
     25        if (
     26          !actOnMozImage(window.document, "img1", function (image) {
     27            gImage = image;
     28            gFrames = gImage.framesNotified;
     29          })
     30        ) {
     31          gBrowser.removeCurrentTab();
     32          abort = true;
     33        }
     34        goer.next();
     35      },
     36      { capture: true, once: true }
     37    );
     38    yield;
     39    if (abort) {
     40      finish();
     41      yield; // optimized build
     42    }
     43 
     44    // Let animation run for a bit
     45    chances = 120;
     46    do {
     47      gTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
     48      gTimer.initWithCallback(
     49        function () {
     50          if (gImage.framesNotified >= 20) {
     51            goer.send(true);
     52          } else {
     53            chances--;
     54            goer.send(chances == 0); // maybe if we wait a bit, it will happen
     55          }
     56        },
     57        500,
     58        Ci.nsITimer.TYPE_ONE_SHOT
     59      );
     60    } while (!yield);
     61    is(chances > 0, true, "Must have animated a few frames so far");
     62 
     63    // Browse elsewhere; push our animating page into the bfcache
     64    gBrowser.loadURI(Services.io.newURI("about:blank"));
     65 
     66    // Wait a bit for page to fully load, then wait a while and
     67    // see that no animation occurs.
     68    gTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
     69    gTimer.initWithCallback(
     70      function () {
     71        gFrames = gImage.framesNotified;
     72        gTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
     73        gTimer.initWithCallback(
     74          function () {
     75            // Might have a few stray frames, until other page totally loads
     76            var additionalFrames = gImage.framesNotified - gFrames;
     77            is(
     78              additionalFrames == 0,
     79              true,
     80              "Must have not animated in bfcache! Got " +
     81                additionalFrames +
     82                " additional frames"
     83            );
     84            goer.next();
     85          },
     86          4000,
     87          Ci.nsITimer.TYPE_ONE_SHOT
     88        ); // 4 seconds - expect 40 frames
     89      },
     90      0,
     91      Ci.nsITimer.TYPE_ONE_SHOT
     92    ); // delay of 0 - wait for next event loop
     93    yield;
     94 
     95    // Go back
     96    gBrowser.goBack();
     97 
     98    chances = 120;
     99    do {
    100      gTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
    101      gTimer.initWithCallback(
    102        function () {
    103          if (gImage.framesNotified - gFrames >= 20) {
    104            goer.send(true);
    105          } else {
    106            chances--;
    107            goer.send(chances == 0); // maybe if we wait a bit, it will happen
    108          }
    109        },
    110        500,
    111        Ci.nsITimer.TYPE_ONE_SHOT
    112      );
    113    } while (!yield);
    114    is(chances > 0, true, "Must have animated once out of bfcache!");
    115 
    116    // Finally, check that the css background image has essentially the same
    117    // # of frames, implying that it animated at the same times as the regular
    118    // image. We can easily retrieve regular images through their HTML image
    119    // elements, which is what we did before. For the background image, we
    120    // create a regular image now, and read the current frame count.
    121    var doc = gBrowser.selectedBrowser.contentWindow.document;
    122    var div = doc.getElementById("background_div");
    123    div.innerHTML += '<img src="animated2.gif" id="img3">';
    124    actOnMozImage(doc, "img3", function (image) {
    125      is(
    126        Math.abs(image.framesNotified - gImage.framesNotified) /
    127          gImage.framesNotified <
    128          0.5,
    129        true,
    130        "Must have also animated the background image, and essentially the same # of frames. " +
    131          "Regular image got " +
    132          gImage.framesNotified +
    133          " frames but background image got " +
    134          image.framesNotified
    135      );
    136    });
    137 
    138    gBrowser.removeCurrentTab();
    139 
    140    nextTest();
    141  }
    142 
    143  var goer = theTest();
    144  goer.next();
    145 }
    146 
    147 // Check that imgContainers are shared on the same page and
    148 // between tabs
    149 function testSharedContainers() {
    150  function theTest() {
    151    var gImages = [];
    152    var gFrames;
    153 
    154    gBrowser.selectedTab = BrowserTestUtils.addTab(
    155      gBrowser,
    156      TESTROOT + "image.html"
    157    );
    158    gBrowser.selectedBrowser.addEventListener(
    159      "pageshow",
    160      function () {
    161        actOnMozImage(gBrowser.contentDocument, "img1", function (image) {
    162          gImages[0] = image;
    163          gFrames = image.framesNotified; // May in theory have frames from last test
    164          // in this counter - so subtract them out
    165        });
    166        goer.next();
    167      },
    168      { capture: true, once: true }
    169    );
    170    yield;
    171 
    172    // Load next tab somewhat later
    173    gTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
    174    gTimer.initWithCallback(
    175      function () {
    176        goer.next();
    177      },
    178      1500,
    179      Ci.nsITimer.TYPE_ONE_SHOT
    180    );
    181    yield;
    182 
    183    gBrowser.selectedTab = BrowserTestUtils.addTab(
    184      gBrowser,
    185      TESTROOT + "imageX2.html"
    186    );
    187    gBrowser.selectedBrowser.addEventListener(
    188      "pageshow",
    189      function () {
    190        [1, 2].forEach(function (i) {
    191          actOnMozImage(gBrowser.contentDocument, "img" + i, function (image) {
    192            gImages[i] = image;
    193          });
    194        });
    195        goer.next();
    196      },
    197      { capture: true, once: true }
    198    );
    199    yield;
    200 
    201    var chances = 120;
    202    do {
    203      gTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
    204      gTimer.initWithCallback(
    205        function () {
    206          if (gImages[0].framesNotified - gFrames >= 10) {
    207            goer.send(true);
    208          } else {
    209            chances--;
    210            goer.send(chances == 0); // maybe if we wait a bit, it will happen
    211          }
    212        },
    213        500,
    214        Ci.nsITimer.TYPE_ONE_SHOT
    215      );
    216    } while (!yield);
    217    is(
    218      chances > 0,
    219      true,
    220      "Must have been animating while showing several images"
    221    );
    222 
    223    // Check they all have the same frame counts
    224    var theFrames = null;
    225    [0, 1, 2].forEach(function (i) {
    226      var frames = gImages[i].framesNotified;
    227      if (theFrames == null) {
    228        theFrames = frames;
    229      } else {
    230        is(
    231          theFrames,
    232          frames,
    233          "Sharing the same imgContainer means *exactly* the same frame counts!"
    234        );
    235      }
    236    });
    237 
    238    gBrowser.removeCurrentTab();
    239    gBrowser.removeCurrentTab();
    240 
    241    nextTest();
    242  }
    243 
    244  var goer = theTest();
    245  goer.next();
    246 }
    247 
    248 var tests = [testBFCache, testSharedContainers];
    249 
    250 function nextTest() {
    251  if (!tests.length) {
    252    finish();
    253    return;
    254  }
    255  tests.shift()();
    256 }
    257 
    258 function test() {
    259  ignoreAllUncaughtExceptions();
    260  nextTest();
    261 }