summaryrefslogtreecommitdiffstats
path: root/image/test/mochitest/test_animated_css_image.html
blob: c9c77ff44405d62a82a87c13981f16a3b8352a42 (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
<!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 === "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;
    },
  }
];

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>