summaryrefslogtreecommitdiffstats
path: root/image/test/mochitest/test_animated_css_image.html
blob: ca4a47915fb970411e3a263b9b751881f323aa0b (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<!doctype html>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<script src="/tests/SimpleTest/WindowSnapshot.js"></script>
<!--
  scrolling=no is just paranoia to ensure that we don't get invalidations
  due to scrollbars
-->
<iframe scrolling="no" id="iframe"></iframe>
<script>
SimpleTest.waitForExplicitFinish();

// We hit an optimized path in WebRender that doesn't cause a repaint on the
// main thread:
//
// https://searchfox.org/mozilla-central/rev/b7f3977978922d44c7d92ae01c0d4cc2baca7bc2/layout/style/ImageLoader.cpp#553
//
// So our assertions and polling need to be a bit weaker on WR.
const kUsingWebRender = SpecialPowers.DOMWindowUtils.layerManagerType.startsWith("WebRender");

let iframe = document.getElementById("iframe");
let blankSnapshot;

async function assertAnimates(html, getExpectedRepaintedElement) {
  const kExpectEqual = true;
  const kNumRetries = kUsingWebRender ? 600 : 30;

  info("testing: " + html);

  {
    let load = new Promise(resolve => {
      iframe.addEventListener("load", resolve, { once: true });
    });
    iframe.srcdoc = html;
    await load;
  }

  // This ensures the MozAfterPaint events come through as expected.
  await SimpleTest.promiseFocus(iframe.contentWindow);

  let initial = await snapshotWindow(iframe.contentWindow);

  let repaintedElement = getExpectedRepaintedElement(iframe.contentDocument);
  if (!kUsingWebRender) {
    // Ensure the painted state is clear before we start polling.
    SpecialPowers.DOMWindowUtils.checkAndClearPaintedState(repaintedElement);
  }

  {
    let [equal, s1 /* , s2, differentPixels, maxDiff */] = compareSnapshots(initial, blankSnapshot, kExpectEqual);
    ok(!equal, "Initial snapshot shouldn't be blank");
    info(s1);
  }

  let foundDifferent = false;
  let foundInitialAgain = false;
  for (let i = 0; i < kNumRetries; ++i) {
    let current = await snapshotWindow(iframe.contentWindow);
    let [equal, /* s1 */, s2 /* , differentPixels, maxDiff */ ] = compareSnapshots(initial, current, kExpectEqual);
    if (!foundDifferent && !equal) {
      ok(true, `Found different image after ${i} retries`);
      ok(kUsingWebRender || SpecialPowers.DOMWindowUtils.checkAndClearPaintedState(repaintedElement), "Should've repainted the expected element");
      info(s2);
      foundDifferent = true;
    }

    // Ensure that we go back to the initial state (animated1.gif) is an
    // infinite gif.
    if (foundDifferent && equal) {
      ok(true, `Found same image again after ${i} retries`);
      ok(kUsingWebRender || SpecialPowers.DOMWindowUtils.checkAndClearPaintedState(repaintedElement), "Should've repainted the expected element");
      foundInitialAgain = true;
      break;
    }

    await new Promise(resolve => {
      if (kUsingWebRender) {
        requestAnimationFrame(() => {
          requestAnimationFrame(resolve);
        });
      } else {
        iframe.contentWindow.addEventListener("MozAfterPaint", resolve, { once: true });
      }
    });
  }

  ok(foundDifferent && foundInitialAgain, `Should've found a different snapshot and then an equal one, after ${kNumRetries} retries`);
}

const kTests = [
  // Sanity test: background-image on a regular element.
  {
    html: `
      <!doctype html>
      <style>
        div {
          width: 100px;
          height: 100px;
          background-image: url(animated1.gif);
        }
      </style>
      <div></div>
    `,
    element(doc) {
      return doc.querySelector("div");
    },
  },

  // bug 1627585: content: url()
  {
    html: `
      <!doctype html>
      <style>
        div::before {
          content: url(animated1.gif);
        }
      </style>
      <div></div>
    `,
    element(doc) {
      return doc.querySelector("div");
    },
  },

  // bug 1627585: content: url() (on an element directly)
  {
    html: `
      <!doctype html>
      <style>
        div {
          content: url(animated1.gif);
        }
      </style>
      <div></div>
    `,
    element(doc) {
      return doc.querySelector("div");
    },
  },

  // bug 1625571: background propagated to canvas.
  {
    html: `
      <!doctype html>
      <style>
        body {
          background-image: url(animated1.gif);
        }
      </style>
    `,
    element(doc) {
      return doc.documentElement;
    },
  },

  // bug 1719375: CSS animation in SVG image.
  {
    html: `
      <!doctype html>
      <style>
        div {
          width: 100px;
          height: 100px;
          background-image: url(animated1.svg);
        }
      </style>
      <div></div>
    `,
    element(doc) {
      return doc.querySelector("div");
    },
  },

  // bug 1730834: stopped window.
  {
    html: `
      <!doctype html>
      <style>
        div {
          width: 100px;
          height: 100px;
        }
      </style>
      <body onload="window.stop(); document.querySelector('div').style.backgroundImage = 'url(animated1.gif)';">
        <div></div>
      </body>
    `,
    element(doc) {
      return doc.querySelector("div");
    },
  },

  // bug 1731138: Animated mask
  {
    html: `
      <!doctype html>
      <style>
        div {
          width: 100px;
          height: 100px;
          background-color: lime;
          mask-clip: border-box;
          mask-size: 100% 100%;
          mask-image: url(animatedMask.gif);
      }
      </style>
      <div></div>
    `,
    element(doc) {
      return doc.querySelector("div");
    },
  },
];

onload = async function() {
  // First snapshot the blank window.
  blankSnapshot = await snapshotWindow(iframe.contentWindow);

  for (let { html, element } of kTests)
    await assertAnimates(html, element);

  SimpleTest.finish();
}
</script>