summaryrefslogtreecommitdiffstats
path: root/image/test/mochitest/test_animation_operators.html
blob: a03088b12fea82a10ef3b66574c3b5d373e949e1 (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=936720
-->
<head>
  <title>Test for Bug 936720</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="application/javascript" src="/tests/SimpleTest/WindowSnapshot.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=936720">Mozilla Bug 936720</a>
<pre id="test">
<script type="application/javascript">

/** Test for Bug 936720 **/

// Because there is no event telling us when an animated image finishes
// animating, tests for the operators used by animated GIFs and PNGs
// require that we poll until we get the correct result. A fixed timeout
// can easily result in intermittent failures on tests running in VMs.

// (Note that we do _not_ poll the reference, so it must not be animated.)

var gTests = [
  // IMPORTANT NOTE: For these tests, the test and reference are not
  // snapshotted in the same way.  The REFERENCE (second file) is
  // assumed to be complete when loaded, but we poll the TEST
  // (first file) until the test passes.

  // Tests of the allowed disposal operators for both GIF and APNG: keep, clear,
  // and restore previous.
  "== green-background.html?clear.gif green.png",
  "== green-background.html?clear.png green.png",
  "== keep.gif green.png",
  "== keep.png green.png",
  "== restore-previous.gif green.png",
  "== restore-previous.png green.png",

  // Tests of the blending/compositing operators that only APNG supports.
  "== over.png grey.png",
  "!= source.png grey.png",
  "== bug900200.png bug900200-ref.png",
  "== bug1319025.png bug1319025-ref.png",

  // Test of subframe updates.
  "== clear2.gif clear2-results.gif",
];

// Maintain a reference count of how many things we're waiting for until
// we can say the tests are done.
var gDelayCount = 0;
function AddFinishDependency()
  { ++gDelayCount; }
function RemoveFinishDependency()
  { if (--gDelayCount == 0) SimpleTest.finish(); }

// We record the maximum number of times we had to look at a test before
// it switched to the passing state (though we assume it's 10 to start
// rather than 0 so that we have a reasonable default).  Then we make a
// test "time out" if it takes more than gTimeoutFactor times that
// amount of time.  This allows us to report a test failure rather than
// making a test failure just show up as a timeout.
var gMaxPassingTries = 10;
var gTimeoutFactor = 10;

function takeSnapshot(iframe_element)
{
  return snapshotWindow(iframe_element.contentWindow, false);
}

function passes(op, shot1, shot2)
{
  var [correct, s1, s2] = compareSnapshots(shot1, shot2, op == "==");
  return correct;
}

function startTest(i)
{
  var testLine = gTests[i];
  var splitData = testLine.split(" ");
  var testData =
    { op: splitData[0], test: splitData[1], reference: splitData[2] };
  var tries = 0;

  // Maintain state specific to this test in the closure exposed to all
  // the functions nested inside this one.

  function startIframe(url)
  {
    var element = document.createElement("iframe");
    element.addEventListener("load", handleLoad, false);
    // Smaller than normal reftests, but enough for these.
    element.setAttribute("style", "width: 100px; height: 100px");
    element.setAttribute("frameborder", "0");
    element.setAttribute("scrolling", "no");
    element.src = url;
    document.body.appendChild(element);
    function handleLoad(event)
    {
      iframe.loaded = true;
      if (iframe == reference) {
        reference.snapshot = takeSnapshot(element);
      }
      var other = (iframe == test) ? reference : test;
      if (other.loaded) {
        setTimeout(checkTest, 100);
      }
    }
    function checkTest()
    {
      var test_snapshot = takeSnapshot(test.element);
      if (passes(testData.op, test_snapshot, reference.snapshot)) {
        if (tries > gMaxPassingTries) {
          gMaxPassingTries = tries;
        }
        report(true);
      } else {
        ++tries;
        if (tries > gMaxPassingTries * gTimeoutFactor) {
          info("Giving up after " + tries + " tries, " +
               "maxp=" + gMaxPassingTries +
               "fact=" + gTimeoutFactor);
          report(false);
        } else {
          // The animation might not have finished.  Try again in 100ms.
          setTimeout(checkTest, 100);
        }
      }
    }
    function report(result)
    {
      ok(result, "(" + i + ") " +
                 testData.op + " " + testData.test + " " + testData.reference);
      RemoveFinishDependency();
    }
    var iframe = { element: element, loaded: false };

    return iframe;
  }

  AddFinishDependency();
  var test = startIframe(testData.test);
  var reference = startIframe(testData.reference);
}

SimpleTest.waitForExplicitFinish();
SimpleTest.requestFlakyTimeout("untriaged");

// Run the tests.
for (var i = 0; i < gTests.length; ++i) {
  startTest(i);
}

</script>
</pre>
</body>
</html>