summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/web-animations/interfaces/KeyframeEffect/target.html
blob: 30b2ee6f0c8a3b5e371fd9996edfb2590c624d17 (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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<!DOCTYPE html>
<meta charset=utf-8>
<title>KeyframeEffect.target and .pseudoElement</title>
<link rel="help"
  href="https://drafts.csswg.org/web-animations/#dom-keyframeeffect-target">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../../testcommon.js"></script>
<style>
  .before::before {content: 'foo'; display: inline-block;}
  .after::after {content: 'bar'; display: inline-block;}
  .pseudoa::before, .pseudoc::before {margin-left: 10px;}
  .pseudob::before, .pseudoc::after {margin-left: 20px;}
</style>
<body>
<div id="log"></div>
<script>
'use strict';

const gKeyFrames = { 'marginLeft': ['0px', '100px'] };

test(t => {
  const div = createDiv(t);
  const effect = new KeyframeEffect(null, gKeyFrames, 100 * MS_PER_SEC);
  effect.target = div;

  const anim = new Animation(effect, document.timeline);
  anim.play();

  anim.currentTime = 50 * MS_PER_SEC;
  assert_equals(getComputedStyle(div).marginLeft, '50px',
                'Value at 50% progress');
}, 'Test setting target before constructing the associated animation');

test(t => {
  const div = createDiv(t);
  div.style.marginLeft = '10px';
  const effect = new KeyframeEffect(null, gKeyFrames, 100 * MS_PER_SEC);
  const anim = new Animation(effect, document.timeline);
  anim.play();

  anim.currentTime = 50 * MS_PER_SEC;
  assert_equals(getComputedStyle(div).marginLeft, '10px',
                'Value at 50% progress before setting new target');
  effect.target = div;
  assert_equals(getComputedStyle(div).marginLeft, '50px',
                'Value at 50% progress after setting new target');
}, 'Test setting target from null to a valid target');

test(t => {
  const div = createDiv(t);
  div.style.marginLeft = '10px';
  const anim = div.animate(gKeyFrames, 100 * MS_PER_SEC);

  anim.currentTime = 50 * MS_PER_SEC;
  assert_equals(getComputedStyle(div).marginLeft, '50px',
                'Value at 50% progress before clearing the target')

  anim.effect.target = null;
  assert_equals(getComputedStyle(div).marginLeft, '10px',
                'Value after clearing the target')
}, 'Test setting target from a valid target to null');

test(t => {
  const a = createDiv(t);
  const b = createDiv(t);
  a.style.marginLeft = '10px';
  b.style.marginLeft = '20px';
  const anim = a.animate(gKeyFrames, 100 * MS_PER_SEC);

  anim.currentTime = 50 * MS_PER_SEC;
  assert_equals(getComputedStyle(a).marginLeft, '50px',
                'Value of 1st element (currently targeted) before ' +
                'changing the effect target');
  assert_equals(getComputedStyle(b).marginLeft, '20px',
                'Value of 2nd element (currently not targeted) before ' +
                'changing the effect target');
  anim.effect.target = b;
  assert_equals(getComputedStyle(a).marginLeft, '10px',
                'Value of 1st element (currently not targeted) after ' +
                'changing the effect target');
  assert_equals(getComputedStyle(b).marginLeft, '50px',
                'Value of 2nd element (currently targeted) after ' +
                'changing the effect target');

  // This makes sure the animation property is changed correctly on new
  // targeted element.
  anim.currentTime = 75 * MS_PER_SEC;
  assert_equals(getComputedStyle(b).marginLeft, '75px',
                'Value of 2nd target (currently targeted) after ' +
                'changing the animation current time.');
}, 'Test setting target from a valid target to another target');

promise_test(async t => {
  const animation = createDiv(t).animate(
    { opacity: 0 },
    { duration: 1, fill: 'forwards' }
  );

  const foreignElement
    = document.createElementNS('http://example.org/test', 'test');
  document.body.appendChild(foreignElement);
  t.add_cleanup(() => {
    foreignElement.remove();
  });

  animation.effect.target = foreignElement;

  // Wait a frame to make sure nothing bad happens when the UA tries to update
  // style.
  await waitForNextFrame();
}, 'Target element can be set to a foreign element');

// Pseudo-element tests
// (testing target and pseudoElement in these cases)
// Since blink uses separate code paths for handling pseudo-element styles
// depending on whether content is set (putting the pseudo-element in the layout),
// we run tests on both cases.
for (const hasContent of [true, false]){
  test(t => {
    const d = createDiv(t);
    d.classList.add('pseudoa');
    if (hasContent) {
      d.classList.add('before');
    }

    const effect = new KeyframeEffect(null, gKeyFrames, 100 * MS_PER_SEC);
    const anim = new Animation(effect, document.timeline);
    anim.play();

    anim.currentTime = 50 * MS_PER_SEC;
    assert_equals(getComputedStyle(d, '::before').marginLeft, '10px',
                  'Value at 50% progress before setting new target');
    effect.target = d;
    effect.pseudoElement = '::before';

    assert_equals(effect.target, d, "Target element is set correctly");
    assert_equals(effect.pseudoElement, '::before', "Target pseudo-element set correctly");
    assert_equals(getComputedStyle(d, '::before').marginLeft, '50px',
                  'Value at 50% progress after setting new target');
  }, "Change target from null to " + (hasContent ? "an existing" : "a non-existing") +
     " pseudoElement setting target first.");

  test(t => {
    const d = createDiv(t);
    d.classList.add('pseudoa');
    if (hasContent) {
      d.classList.add('before');
    }

    const effect = new KeyframeEffect(null, gKeyFrames, 100 * MS_PER_SEC);
    const anim = new Animation(effect, document.timeline);
    anim.play();

    anim.currentTime = 50 * MS_PER_SEC;
    assert_equals(getComputedStyle(d, '::before').marginLeft, '10px',
                  'Value at 50% progress before setting new target');
    effect.pseudoElement = '::before';
    effect.target = d;

    assert_equals(effect.target, d, "Target element is set correctly");
    assert_equals(effect.pseudoElement, '::before', "Target pseudo-element set correctly");
    assert_equals(getComputedStyle(d, '::before').marginLeft, '50px',
                  'Value at 50% progress after setting new target');
  }, "Change target from null to " + (hasContent ? "an existing" : "a non-existing") +
     " pseudoElement setting pseudoElement first.");

  test(t => {
    const d = createDiv(t);
    d.classList.add('pseudoa');
    if (hasContent) {
      d.classList.add('before');
    }
    const anim = d.animate(gKeyFrames, {duration: 100 * MS_PER_SEC, pseudoElement: '::before'});

    anim.currentTime = 50 * MS_PER_SEC;
    anim.effect.pseudoElement = null;
    assert_equals(anim.effect.target, d,
                  "Animation targets specified element (target element)");
    assert_equals(anim.effect.pseudoElement, null,
                  "Animation targets specified element (null pseudo-selector)");
    assert_equals(getComputedStyle(d, '::before').marginLeft, '10px',
                  'Value of 1st element (currently not targeted) after ' +
                  'changing the effect target');
    assert_equals(getComputedStyle(d).marginLeft, '50px',
                  'Value of 2nd element (currently targeted) after ' +
                  'changing the effect target');
  }, "Change target from " + (hasContent ? "an existing" : "a non-existing") + " pseudo-element to the originating element.");

  for (const prevHasContent of [true, false]) {
    test(t => {
      const a = createDiv(t);
      a.classList.add('pseudoa');
      const b = createDiv(t);
      b.classList.add('pseudob');
      if (prevHasContent) {
        a.classList.add('before');
      }
      if (hasContent) {
        b.classList.add('before');
      }

      const anim = a.animate(gKeyFrames, {duration: 100 * MS_PER_SEC, pseudoElement: '::before'});

      anim.currentTime = 50 * MS_PER_SEC;
      anim.effect.target = b;
      assert_equals(anim.effect.target, b,
                    "Animation targets specified pseudo-element (target element)");
      assert_equals(anim.effect.pseudoElement, '::before',
                    "Animation targets specified pseudo-element (pseudo-selector)");
      assert_equals(getComputedStyle(a, '::before').marginLeft, '10px',
                    'Value of 1st element (currently not targeted) after ' +
                    'changing the effect target');
      assert_equals(getComputedStyle(b, '::before').marginLeft, '50px',
                    'Value of 2nd element (currently targeted) after ' +
                    'changing the effect target');
    }, "Change target from " + (prevHasContent ? "an existing" : "a non-existing") +
        " to a different " + (hasContent ? "existing" : "non-existing") +
        " pseudo-element by setting target.");

    test(t => {
      const d = createDiv(t);
      d.classList.add('pseudoc');
      if (prevHasContent) {
        d.classList.add('before');
      }
      if (hasContent) {
        d.classList.add('after');
      }

      const anim = d.animate(gKeyFrames, {duration: 100 * MS_PER_SEC, pseudoElement: '::before'});

      anim.currentTime = 50 * MS_PER_SEC;
      anim.effect.pseudoElement = '::after';
      assert_equals(anim.effect.target, d,
                    "Animation targets specified pseudo-element (target element)");
      assert_equals(anim.effect.pseudoElement, '::after',
                    "Animation targets specified pseudo-element (pseudo-selector)");
      assert_equals(getComputedStyle(d, '::before').marginLeft, '10px',
                    'Value of 1st element (currently not targeted) after ' +
                    'changing the effect target');
      assert_equals(getComputedStyle(d, '::after').marginLeft, '50px',
                    'Value of 2nd element (currently targeted) after ' +
                    'changing the effect target');
    }, "Change target from " + (prevHasContent ? "an existing" : "a non-existing") +
        " to a different " + (hasContent ? "existing" : "non-existing") +
        " pseudo-element by setting pseudoElement.");
  }
}

for (const pseudo of [
  '',
  'before',
  ':abc',
  '::abc',
  '::placeholder',
]) {
  test(t => {
    const effect = new KeyframeEffect(null, gKeyFrames, 100 * MS_PER_SEC);
    assert_throws_dom("SyntaxError", () => effect.pseudoElement = pseudo );
  }, `Changing pseudoElement to a non-null invalid pseudo-selector ` +
     `'${pseudo}' throws a SyntaxError`);
}

</script>
</body>