summaryrefslogtreecommitdiffstats
path: root/image/test/browser/browser_image.js
blob: 9527726c3d8aa267afd17c3810e55bcae7caf9dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
waitForExplicitFinish();
requestLongerTimeout(2); // see bug 660123 -- this test is slow on Mac.

// A hold on the current timer, so it doens't get GCed out from
// under us
var gTimer;

// Browsing to a new URL - pushing us into the bfcache - should cause
// 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 false, we are in an optimized build, and we abort this and
      // all further tests
      if (!actOnMozImage(window.document, "img1", function(image) {
        gImage = image;
        gFrames = gImage.framesNotified;
      })) {
        gBrowser.removeCurrentTab();
        abort = true;
      }
      goer.next();
    }, true);
    yield;
    if (abort) {
      finish();
      yield; // optimized build
    }

    // Let animation run for a bit
    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); // maybe if we wait a bit, it will happen
        }
      }, 500, Ci.nsITimer.TYPE_ONE_SHOT);
    } while (!(yield));
    is(chances > 0, true, "Must have animated a few frames so far");

    // Browse elsewhere; push our animating page into the bfcache
    gBrowser.loadURI("about:blank");

    // Wait a bit for page to fully load, then wait a while and
    // see that no animation occurs.
    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() {
        // Might have a few stray frames, until other page totally loads
        var additionalFrames = gImage.framesNotified  - gFrames;
        is(additionalFrames == 0, true, "Must have not animated in bfcache! Got " + additionalFrames + " additional frames");
        goer.next();
      }, 4000, Ci.nsITimer.TYPE_ONE_SHOT); // 4 seconds - expect 40 frames
    }, 0, Ci.nsITimer.TYPE_ONE_SHOT); // delay of 0 - wait for next event loop
    yield;

    // Go back
    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); // maybe if we wait a bit, it will happen
        }
      }, 500, Ci.nsITimer.TYPE_ONE_SHOT);
    } while (!(yield));
    is(chances > 0, true, "Must have animated once out of bfcache!");

    // 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.
    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. " +
         "Regular image got " + gImage.framesNotified + " frames but background image got " + image.framesNotified);
    });

    gBrowser.removeCurrentTab();

    nextTest();
  }

  var goer = theTest();
  goer.next();
}

// Check that imgContainers are shared on the same page and
// between tabs
function testSharedContainers() {
  function theTest() {
    var gImages = [];
    var gFrames;

    gBrowser.selectedTab = gBrowser.addTab(TESTROOT + "image.html");
    gBrowser.selectedBrowser.addEventListener("pageshow", function () {
      gBrowser.selectedBrowser.removeEventListener("pageshow", arguments.callee, true);
      actOnMozImage(gBrowser.contentDocument, "img1", function(image) {
        gImages[0] = image;
        gFrames = image.framesNotified; // May in theory have frames from last test
                                        // in this counter - so subtract them out
      });
      goer.next();
    }, true);
    yield;

    // Load next tab somewhat later
    gTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
    gTimer.initWithCallback(function() {
      goer.next();
    }, 1500, Ci.nsITimer.TYPE_ONE_SHOT);
    yield;

    gBrowser.selectedTab = gBrowser.addTab(TESTROOT + "imageX2.html");
    gBrowser.selectedBrowser.addEventListener("pageshow", function () {
      gBrowser.selectedBrowser.removeEventListener("pageshow", arguments.callee, true);
      [1,2].forEach(function(i) {
        actOnMozImage(gBrowser.contentDocument, "img"+i, function(image) {
          gImages[i] = image;
        });
      });
      goer.next();
    }, true);
    yield;

    var chances = 120;
    do {
      gTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
      gTimer.initWithCallback(function() {
        if (gImages[0].framesNotified - gFrames >= 10) {
          goer.send(true);
        } else {
          chances--;
          goer.send(chances == 0); // maybe if we wait a bit, it will happen
        }
      }, 500, Ci.nsITimer.TYPE_ONE_SHOT);
    } while (!(yield));
    is(chances > 0, true, "Must have been animating while showing several images");

    // Check they all have the same frame counts
    var theFrames = null;
    [0,1,2].forEach(function(i) {
      var frames = gImages[i].framesNotified;
      if (theFrames == null) {
        theFrames = frames;
      } else {
        is(theFrames, frames, "Sharing the same imgContainer means *exactly* the same frame counts!");
      }
    });

    gBrowser.removeCurrentTab();
    gBrowser.removeCurrentTab();

    nextTest();
  }

  var goer = theTest();
  goer.next();
}

var tests = [testBFCache, testSharedContainers];

function nextTest() {
  if (tests.length == 0) {
    finish();
    return;
  }
  tests.shift()();
}

function test() {
  ignoreAllUncaughtExceptions();
  nextTest();
}