summaryrefslogtreecommitdiff
path: root/dom/animation/test/css-animations/file_document-get-animations.html
blob: abe02d7fc34cc2a0c73ae6f8bf04bf36b8ccb54a (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
267
268
269
270
271
272
273
274
275
276
<!doctype html>
<meta charset=utf-8>
<script src="../testcommon.js"></script>
<style>
@keyframes animLeft {
  to { left: 100px }
}
@keyframes animTop {
  to { top: 100px }
}
@keyframes animBottom {
  to { bottom: 100px }
}
@keyframes animRight {
  to { right: 100px }
}
</style>
<body>
<script>
'use strict';

test(function(t) {
  assert_equals(document.getAnimations().length, 0,
    'getAnimations returns an empty sequence for a document'
    + ' with no animations');
}, 'getAnimations for non-animated content');

test(function(t) {
  var div = addDiv(t);

  // Add an animation
  div.style.animation = 'animLeft 100s';
  assert_equals(document.getAnimations().length, 1,
                'getAnimations returns a running CSS Animation');

  // Add another animation
  div.style.animation = 'animLeft 100s, animTop 100s';
  assert_equals(document.getAnimations().length, 2,
                'getAnimations returns two running CSS Animations');

  // Remove both
  div.style.animation = '';
  assert_equals(document.getAnimations().length, 0,
                'getAnimations returns no running CSS Animations');
}, 'getAnimations for CSS Animations');

test(function(t) {
  var div = addDiv(t);
  div.style.animation = 'animLeft 100s, animTop 100s, animRight 100s, ' +
                        'animBottom 100s';

  var animations = document.getAnimations();
  assert_equals(animations.length, 4,
                'getAnimations returns all running CSS Animations');
  assert_equals(animations[0].animationName, 'animLeft',
                'Order of first animation returned');
  assert_equals(animations[1].animationName, 'animTop',
                'Order of second animation returned');
  assert_equals(animations[2].animationName, 'animRight',
                'Order of third animation returned');
  assert_equals(animations[3].animationName, 'animBottom',
                'Order of fourth animation returned');
}, 'Order of CSS Animations - within an element');

test(function(t) {
  var div1 = addDiv(t, { style: 'animation: animLeft 100s' });
  var div2 = addDiv(t, { style: 'animation: animLeft 100s' });
  var div3 = addDiv(t, { style: 'animation: animLeft 100s' });
  var div4 = addDiv(t, { style: 'animation: animLeft 100s' });

  var animations = document.getAnimations();
  assert_equals(animations.length, 4,
                'getAnimations returns all running CSS Animations');
  assert_equals(animations[0].effect.target, div1,
                'Order of first animation returned');
  assert_equals(animations[1].effect.target, div2,
                'Order of second animation returned');
  assert_equals(animations[2].effect.target, div3,
                'Order of third animation returned');
  assert_equals(animations[3].effect.target, div4,
                'Order of fourth animation returned');

  // Order should be depth-first pre-order so add some depth as follows:
  //
  //      <parent>
  //       /  |
  //      2   3
  //    /  \
  //   1   4
  //
  // Which should give: 2, 1, 4, 3
  div2.appendChild(div1);
  div2.appendChild(div4);
  animations = document.getAnimations();
  assert_equals(animations[0].effect.target, div2,
                'Order of first animation returned after tree surgery');
  assert_equals(animations[1].effect.target, div1,
                'Order of second animation returned after tree surgery');
  assert_equals(animations[2].effect.target, div4,
                'Order of third animation returned after tree surgery');
  assert_equals(animations[3].effect.target, div3,
                'Order of fourth animation returned after tree surgery');

}, 'Order of CSS Animations - across elements');

test(function(t) {
  var div1 = addDiv(t, { style: 'animation: animLeft 100s, animTop 100s' });
  var div2 = addDiv(t, { style: 'animation: animBottom 100s' });

  var expectedResults = [ [ div1, 'animLeft' ],
                          [ div1, 'animTop' ],
                          [ div2, 'animBottom' ] ];
  var animations = document.getAnimations();
  assert_equals(animations.length, expectedResults.length,
                'getAnimations returns all running CSS Animations');
  animations.forEach(function(anim, i) {
    assert_equals(anim.effect.target, expectedResults[i][0],
                  'Target of animation in position ' + i);
    assert_equals(anim.animationName, expectedResults[i][1],
                  'Name of animation in position ' + i);
  });

  // Modify tree structure and animation list
  div2.appendChild(div1);
  div1.style.animation = 'animLeft 100s, animRight 100s, animTop 100s';

  expectedResults = [ [ div2, 'animBottom' ],
                      [ div1, 'animLeft' ],
                      [ div1, 'animRight' ],
                      [ div1, 'animTop' ] ];
  animations = document.getAnimations();
  assert_equals(animations.length, expectedResults.length,
                'getAnimations returns all running CSS Animations after ' +
                'making changes');
  animations.forEach(function(anim, i) {
    assert_equals(anim.effect.target, expectedResults[i][0],
                  'Target of animation in position ' + i + ' after changes');
    assert_equals(anim.animationName, expectedResults[i][1],
                  'Name of animation in position ' + i + ' after changes');
  });
}, 'Order of CSS Animations - across and within elements');

test(function(t) {
  var div = addDiv(t, { style: 'animation: animLeft 100s, animTop 100s' });
  var animLeft = document.getAnimations()[0];
  assert_equals(animLeft.animationName, 'animLeft',
                'Originally, animLeft animation comes first');

  // Disassociate animLeft from markup and restart
  div.style.animation = 'animTop 100s';
  animLeft.play();

  var animations = document.getAnimations();
  assert_equals(animations.length, 2,
                'getAnimations returns markup-bound and free animations');
  assert_equals(animations[0].animationName, 'animTop',
                'Markup-bound animations come first');
  assert_equals(animations[1], animLeft, 'Free animations come last');
}, 'Order of CSS Animations - markup-bound vs free animations');

test(function(t) {
  var div = addDiv(t, { style: 'animation: animLeft 100s, animTop 100s' });
  var animLeft = document.getAnimations()[0];
  var animTop  = document.getAnimations()[1];

  // Disassociate both animations from markup and restart in opposite order
  div.style.animation = '';
  animTop.play();
  animLeft.play();

  var animations = document.getAnimations();
  assert_equals(animations.length, 2,
                'getAnimations returns free animations');
  assert_equals(animations[0], animTop,
                'Free animations are returned in the order they are started');
  assert_equals(animations[1], animLeft,
                'Animations started later are returned later');

  // Restarting an animation should have no effect
  animTop.cancel();
  animTop.play();
  assert_equals(document.getAnimations()[0], animTop,
                'After restarting, the ordering of free animations' +
                ' does not change');
}, 'Order of CSS Animations - free animations');

test(function(t) {
  // Add an animation first
  var div = addDiv(t, { style: 'animation: animLeft 100s' });
  div.style.top = '0px';
  div.style.transition = 'all 100s';
  flushComputedStyle(div);

  // *Then* add a transition
  div.style.top = '100px';
  flushComputedStyle(div);

  // Although the transition was added later, it should come first in the list
  var animations = document.getAnimations();
  assert_equals(animations.length, 2,
                'Both CSS animations and transitions are returned');
  assert_class_string(animations[0], 'CSSTransition', 'Transition comes first');
  assert_class_string(animations[1], 'CSSAnimation', 'Animation comes second');
}, 'Order of CSS Animations and CSS Transitions');

test(function(t) {
  var div = addDiv(t, { style: 'animation: animLeft 100s forwards' });
  div.getAnimations()[0].finish();
  assert_equals(document.getAnimations().length, 1,
                'Forwards-filling CSS animations are returned');
}, 'Finished but filling CSS Animations are returned');

test(function(t) {
  var div = addDiv(t, { style: 'animation: animLeft 100s' });
  div.getAnimations()[0].finish();
  assert_equals(document.getAnimations().length, 0,
                'Non-filling finished CSS animations are not returned');
}, 'Finished but not filling CSS Animations are not returned');

test(function(t) {
  var div = addDiv(t, { style: 'animation: animLeft 100s 100s' });
  assert_equals(document.getAnimations().length, 1,
                'Yet-to-start CSS animations are returned');
}, 'Yet-to-start CSS Animations are returned');

test(function(t) {
  var div = addDiv(t, { style: 'animation: animLeft 100s' });
  div.getAnimations()[0].cancel();
  assert_equals(document.getAnimations().length, 0,
                'CSS animations cancelled by the API are not returned');
}, 'CSS Animations cancelled via the API are not returned');

test(function(t) {
  var div = addDiv(t, { style: 'animation: animLeft 100s' });
  var anim = div.getAnimations()[0];
  anim.cancel();
  anim.play();
  assert_equals(document.getAnimations().length, 1,
                'CSS animations cancelled and restarted by the API are ' +
                'returned');
}, 'CSS Animations cancelled and restarted via the API are returned');

test(function(t) {
  addStyle(t, { '#parent::after': 'animation: animLeft 10s;',
                '#parent::before': 'animation: animRight 10s;' });
  // create two divs with these arrangement:
  //       parent
  //     ::before,
  //     ::after
  //        |
  //       child
  var parent = addDiv(t, { 'id': 'parent' });
  var child = addDiv(t);
  parent.appendChild(child);
  [parent, child].forEach((div) => {
    div.setAttribute('style', 'animation: animBottom 10s');
  });

  var anims = document.getAnimations();
  assert_equals(anims.length, 4,
                'CSS animations on both pseudo-elements and elements ' +
                'are returned');
  assert_equals(anims[0].effect.target, parent,
                'The animation targeting the parent element comes first');
  assert_equals(anims[1].effect.target.type, '::before',
                'The animation targeting the ::before element comes second');
  assert_equals(anims[2].effect.target.type, '::after',
                'The animation targeting the ::after element comes third');
  assert_equals(anims[3].effect.target, child,
                'The animation targeting the child element comes last');
}, 'CSS Animations targetting (pseudo-)elements should have correct order ' +
   'after sorting');

done();
</script>
</body>