summaryrefslogtreecommitdiffstats
path: root/dom/base/test/file_domwindowutils_animation.html
blob: c8b6f2a99dc089b1895dff0e9365e4f639fbd49b (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
224
225
226
227
228
229
230
231
232
233
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>DOMWindowUtils test with animation</title>
  <script src="/tests/SimpleTest/paint_listener.js"></script>
  <script src="/tests/dom/animation/test/testcommon.js"></script>
</head>
<body>
<script type="application/javascript">

const SimpleTest = window.opener.SimpleTest;
const utils = SpecialPowers.getDOMWindowUtils(window);
const next = window.opener.next;
const is = window.opener.is;
const ok = window.opener.ok;

function addStyle(rules) {
  const extraStyle = document.createElement("style");
  document.head.appendChild(extraStyle);
  rules.forEach(rule => {
    extraStyle.sheet.insertRule(rule, extraStyle.sheet.cssRules.length);
  });
}

function deleteStyle() {
  document.head.querySelector("style").remove();
}


let propertyIndex = 0;

function test_getUnanimatedComputedStyle() {
  const name = `--property-${++propertyIndex}`;
  CSS.registerProperty({
    name,
    syntax: "<length>",
    initialValue: "2px",
    inherits: false,
  });
  [
    {
      property: "opacity",
      keyframes: [1, 0],
      expectedInitialStyle: "1",
      expectedDuringTransitionStyle: "0",
      isDiscrete: false,
    },
    {
      property: name,
      keyframes: ["1px", "10px"],
      expectedInitialStyle: "2px",
      expectedDuringTransitionStyle: "2px",
      isDiscrete: true,
    },
    {
      property: "clear",
      keyframes: ["left", "inline-end"],
      expectedInitialStyle: "none",
      expectedDuringTransitionStyle: "inline-end",
      isDiscrete: true,
    },
  ].forEach(testcase => {
    const { property, keyframes, expectedInitialStyle,
            expectedDuringTransitionStyle, isDiscrete } = testcase;

    [null, "unset", "initial", "inherit"].forEach(initialStyle => {
      const scriptAnimation = target => {
        return target.animate({ [property]: keyframes }, 1000);
      }
      checkUnanimatedComputedStyle(property, initialStyle, null,
                                   expectedInitialStyle, expectedInitialStyle,
                                   scriptAnimation, "script animation");

      const cssAnimationStyle = `@keyframes cssanimation {`
                                + ` from { ${property}: ${ keyframes[0] }; }`
                                + ` to { ${property}: ${ keyframes[1] }; } }`;
      addStyle([cssAnimationStyle]);
      const cssAnimation = target => {
        target.style.animation = "cssanimation 1s";
        return target.getAnimations()[0];
      }
      checkUnanimatedComputedStyle(property, initialStyle, null,
                                   expectedInitialStyle, expectedInitialStyle,
                                   cssAnimation, "CSS Animations");
      deleteStyle();

      // We don't support discrete animations for CSS Transitions yet.
      // (bug 1320854)
      if (!isDiscrete) {
        const cssTransition = target => {
          target.style[property] = keyframes[0];
          target.style.transition =
            `${ property } 1s`;
          window.getComputedStyle(target)[property];
          target.style[property] = keyframes[1];
          return target.getAnimations()[0];
        }
        checkUnanimatedComputedStyle(property, initialStyle, null,
                                     expectedInitialStyle,
                                     expectedDuringTransitionStyle,
                                     cssTransition, "CSS Transitions");
      }

      addStyle([cssAnimationStyle,
                ".pseudo::before { content: '' }",
                ".animation::before { animation: cssanimation 1s }"]);
      const pseudoAnimation = target => {
        target.classList.add("animation");
        return target.getAnimations({ subtree: true })[0];
      }
      checkUnanimatedComputedStyle(property, initialStyle, "::before",
                                   expectedInitialStyle, expectedInitialStyle,
                                   pseudoAnimation, "Animation at pseudo");
      deleteStyle();
    });
  });

  const div = document.createElement("div");
  document.body.appendChild(div);

  SimpleTest.doesThrow(
    () => utils.getUnanimatedComputedStyle(div, null, "background", utils.FLUSH_NONE),
    "NS_ERROR_INVALID_ARG",
    "Shorthand property should throw");

  SimpleTest.doesThrow(
    () => utils.getUnanimatedComputedStyle(div, null, "invalid", utils.FLUSH_NONE),
    "NS_ERROR_INVALID_ARG",
    "Invalid property should throw");

  SimpleTest.doesThrow(
    () => utils.getUnanimatedComputedStyle(null, null, "opacity", utils.FLUSH_NONE),
    "NS_ERROR_INVALID_ARG",
    "Null element should throw");

  SimpleTest.doesThrow(
    () => utils.getUnanimatedComputedStyle(div, null, "opacity", utils.FLUSH_LAYOUT),
    "NS_ERROR_INVALID_ARG",
    "FLUSH_LAYOUT option should throw");

  SimpleTest.doesThrow(
    () => utils.getUnanimatedComputedStyle(div, null, "opacity", utils.FLUSH_DISPLAY),
    "NS_ERROR_INVALID_ARG",
    "FLUSH_DISPLAY option should throw");

  SimpleTest.doesThrow(
    () => utils.getUnanimatedComputedStyle(div, "::before", "opacity", utils.FLUSH_NONE),
    "NS_ERROR_FAILURE",
    "Non-existent pseudo should throw");

  // Flush styles since getUnanimatedComputedStyle flushes pending styles even
  // with FLUSH_NONE option if the element hasn't yet styled.
  getComputedStyle(div).opacity;

  div.style.opacity = "0";
  is(utils.getUnanimatedComputedStyle(div, null, "opacity", utils.FLUSH_NONE),
     "1",
     "getUnanimatedComputedStyle with FLUSH_NONE should not flush pending styles");

  is(utils.getUnanimatedComputedStyle(div, null, "opacity", utils.FLUSH_STYLE),
     "0",
     "getUnanimatedComputedStyle with FLUSH_STYLE should flush pending styles");

  div.remove();

  test_needsFlushWithThrottledAnimations();
}

function checkUnanimatedComputedStyle(property, initialStyle, pseudoType,
                                      expectedBeforeAnimation,
                                      expectedDuringAnimation,
                                      animate, animationType) {
  const div = document.createElement("div");
  document.body.appendChild(div);

  if (initialStyle) {
    div.style[property] = initialStyle;
  }
  if (pseudoType) {
    div.classList.add("pseudo");
  }

  is(utils.getUnanimatedComputedStyle(div, pseudoType, property, utils.FLUSH_STYLE),
     expectedBeforeAnimation,
     `'${ property }' property with '${ initialStyle }' style `
     + `should be '${ expectedBeforeAnimation }' `
     + `before animating by ${ animationType }`);

  const animation = animate(div);
  animation.currentTime = 500;
  is(utils.getUnanimatedComputedStyle(div, pseudoType, property, utils.FLUSH_STYLE),
     expectedDuringAnimation,
     `'${ property }' property with '${ initialStyle }' style `
     + `should be '${ expectedDuringAnimation }' `
     + `even while animating by ${ animationType }`);

  div.remove();
}

function test_needsFlushWithThrottledAnimations() {
  const div = document.createElement("div");
  div.style = "width: 100px; height: 100px; background-color: blue;";
  document.body.appendChild(div);

  const animation = div.animate({ opacity: [ 0, 1 ] },
                                { duration: 100000, iterations: Infinity });
  waitForAnimationReadyToRestyle(animation).then(() => {
    ok(SpecialPowers.wrap(animation).isRunningOnCompositor,
        "Opacity animation should run on the compositor");

    // FIXME! Bug 1442861: We should make sure needsFlush() returns true
    // before flusing layout.
    //ok(utils.needsFlush(SpecialPowers.Ci.nsIDOMWindowUtils.FLUSH_STYLE),
    //   "needsFlush should return true if there is an animation on the compositor");

    // Flush layout.
    document.documentElement.getBoundingClientRect();

    ok(!utils.needsFlush(SpecialPowers.Ci.nsIDOMWindowUtils.FLUSH_STYLE),
        "needsFlush should return false after flushing layout");

    div.remove();
    next();
    window.close();
  });
}

window.addEventListener("load", test_getUnanimatedComputedStyle);

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