aboutsummaryrefslogtreecommitdiffstats
path: root/tests/wpt/web-platform-tests/web-animations/animation-node/animation-node-replace.html
blob: 35aa9d84d693309c9a2ce599de3be3d7d30a1579 (plain) (blame)
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
<!DOCTYPE html>
<meta charset=utf-8>
<title>AnimationNode replace() method tests</title>
<meta name="assert" content="Replaces this AnimationNode with the passed in nodes parameter">
<link rel="help" href="http://w3c.github.io/web-animations/#dom-animationnode-replace">
<link rel="help" href="http://w3c.github.io/web-animations/#remove-an-animation-node">
<link rel="help" href="http://w3c.github.io/web-animations/#insert-children">
<link rel="author" title="Sergey G. Grekhov" href="mailto:sgrekhov@unipro.ru">
<link rel="author" title="Aleksei Yu. Semenov" href="mailto:a.semenov@unipro.ru">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../testcommon.js"></script>
<link rel="stylesheet" href="/resources/testharness.css">
<body>
<div id="log"></div>
<script>
// Step 1. If there is no parent animation group, terminate these steps.
test(function() {
    var nodes = [newAnimation(createDiv(this)), new AnimationGroup([]), new AnimationSequence([])];
    nodes.forEach(function(node) {
        try {
            node.replace(null);
        } catch(e) {
            assert_unreached(type(node) + '.replace(null) throws unexpected exception: ' + e);
        }
    });
}, 'AnimationNode.replace(null) does nothing if node has no parent animation group');

test(function() {
    var nodes = [newAnimation(createDiv(this)), new AnimationGroup([]), new AnimationSequence([])];
    nodes.forEach(function(node) {
        try {
            node.replace(node);
        } catch(e) {
            assert_unreached(type(node) + '.replace(node) throws unexpected exception: ' + e);
        }
    });
}, 'AnimationNode.replace() does nothing if node has no parent animation group. ' +
    'HierarchyRequestError is not thrown if the node is replacing itself');

test(function() {
    var test = this;
    var nodes = [newAnimation(createDiv(this)), new AnimationGroup([]), new AnimationSequence([])];
    nodes.forEach(function(node1) {
        var node2 = newAnimation(createDiv(test));

        try {
            node1.replace(node2);
        } catch(e) {
            assert_unreached(type(node1) + '.replace() throws unexpected exception: ' + e);
        }
    });
}, 'AnimationNode.replace() does nothing if node has no parent animation group');

// Step 2. If any of the animation nodes in nodes is an inclusive ancestor
// of the parent animation group throw a HierarchyRequestError exception and terminate these steps.
test(function() {
    var test = this;
    var parents = [AnimationGroup, AnimationSequence];
    parents.forEach(function(parentCtor) {
        var nodes = [newAnimation(createDiv(test)), new AnimationGroup([]), new AnimationSequence([])];
        nodes.forEach(function(node) {
            var parent = new parentCtor([node]);

            assert_throws('HierarchyRequestError', function() {
                node.replace(node);
            }, 'HierarchyRequestError should be thrown if ' + type(node) +
                ' replaces itself');
            assert_equals(node.parent, parent, type(node) + '.replace() should not change ' +
                'parent attribute before throwing HierarchyRequestError');
            assert_array_equals(parent.children, [node], type(node) + '.replace() ' +
                'should not change parent children before throwing HierarchyRequestError');
        });
    });
}, 'HierarchyRequestError is thrown if the node replaces itself');

test(function() {
    var test = this;
    var parents = [AnimationGroup, AnimationSequence];
    parents.forEach(function(parentCtor) {
        var nodes = [newAnimation(createDiv(test)), new AnimationGroup([]), new AnimationSequence([])];
        nodes.forEach(function(node) {
            var parent = new parentCtor([node]);

            assert_throws('HierarchyRequestError', function() {
                node.replace(parent);
            }, 'HierarchyRequestError should be thrown if ' + type(node) +
                ' is replaced by its parent ' + parentCtor.name);
            assert_equals(node.parent, parent, type(node) + '.replace() should not change ' +
                'parent attribute before throwing HierarchyRequestError');
            assert_array_equals(parent.children, [node], type(node) + '.replace() ' +
                'should not change parent children before throwing HierarchyRequestError');
        });
    });
}, 'HierarchyRequestError is thrown if the node is replaced by its parent');

test(function() {
    var test = this;
    var parents = [AnimationGroup, AnimationSequence];
    parents.forEach(function(parentCtor) {
        var nodes = [newAnimation(createDiv(test)), new AnimationGroup([]), new AnimationSequence([])];
        nodes.forEach(function(node) {
            var parent1 = new parentCtor([node]);
            var parent2 = new parentCtor([parent1]);
            var parent3 = new parentCtor([parent2]);
            var parent4 = new parentCtor([parent3]);

            assert_throws('HierarchyRequestError', function() {
                node.replace(parent3);
            }, 'HierarchyRequestError should be thrown if ' + type(node) +
                ' is replaced by its inclusive ancestor' + parentCtor.name);
            assert_equals(node.parent, parent1, type(node) + '.replace() should not change ' +
                'parent attribute before throwing HierarchyRequestError');
            assert_array_equals(parent1.children, [node], type(node) + '.replace() ' +
                'should not change parent children before throwing HierarchyRequestError');
            assert_equals(parent3.parent, parent4, type(node) + '.replace() should not change ' +
                'parent attribute of replacement node before throwing HierarchyRequestError');
            assert_array_equals(parent4.children, [parent3], type(node) + '.replace() ' +
                'should not change replacement node parent children before throwing HierarchyRequestError');
        });
    });
}, 'HierarchyRequestError is thrown if the node is replaced by its inclusive ancestor');

test(function() {
    var test = this;
    var parents = [AnimationGroup, AnimationSequence];
    parents.forEach(function(parentCtor) {
        var nodes = [newAnimation(createDiv(test)), new AnimationGroup([]), new AnimationSequence([])];
        nodes.forEach(function(node1) {
            var parent1 = new parentCtor([node1]);
            var parent2 = new parentCtor([parent1]);
            var parent3 = new parentCtor([parent2]);
            var parent4 = new parentCtor([parent3]);
            var node2 = newAnimation(createDiv(test));
            var parent5 = new parentCtor([node2]);

            assert_throws('HierarchyRequestError', function() {
                node1.replace(node2, parent3);
            }, 'HierarchyRequestError should be thrown if ' + type(node1) +
                ' is replaced by its inclusive ancestor' + parentCtor.name);
            assert_equals(node1.parent, parent1, type(node1) + '.replace() should not change ' +
                'parent attribute before throwing HierarchyRequestError');
            assert_array_equals(parent1.children, [node1], type(node1) + '.replace() ' +
                'should not change parent children before throwing HierarchyRequestError');
            assert_equals(parent3.parent, parent4, type(node1) + '.replace() should not change ' +
                'parent attribute of replacement node before throwing HierarchyRequestError');
            assert_array_equals(parent4.children, [parent3], type(node1) + '.replace() ' +
                'should not change replacement node parent children before throwing HierarchyRequestError');
            assert_equals(node2.parent, parent5, type(node1) + '.replace() should not change ' +
                'parent attribute of replacement node before throwing HierarchyRequestError');
            assert_array_equals(parent5.children, [node2], type(node1) + '.replace() ' +
                'should not change replacement node parent children before throwing HierarchyRequestError');
        });
    });
}, 'HierarchyRequestError is thrown if node is replaced by its inclusive ancestor. ' +
    'Test several arguments in replace() call');

// Step 3. Let reference child be the next sibling of this animation node not in nodes.
// Step 4. Remove this animation node from its parent animation group.
test(function() {
    var test = this;
    var parents = [AnimationGroup, AnimationSequence];
    parents.forEach(function(parentCtor) {
        var nodes = [newAnimation(createDiv(test)), new AnimationGroup([]), new AnimationSequence([])];
        nodes.forEach(function(node) {
            var parent = new parentCtor([node]);

            node.replace();

            assert_array_equals(parent.children, [], type(node) +
                ' node should be removed from parent ' + parentCtor.name);
            assert_equals(node.parent, null, type(node) +
                ' node parent attribute should be updated');
        });
    });
}, 'AnimationNode.replace() without arguments removes the node from the parent ' +
    'animation group');

test(function() {
    var test = this;
    var parents = [AnimationGroup, AnimationSequence];
    parents.forEach(function(parentCtor) {
        var nodes = [newAnimation(createDiv(test)), new AnimationGroup([]), new AnimationSequence([])];
        nodes.forEach(function(node1) {
            var node2 = newAnimation(createDiv(test));
            var parent = new parentCtor([node1]);

            node1.replace(node2);

            assert_array_equals(parent.children, [node2], type(node1) +
                ' node should be removed its parent group');
            assert_equals(node1.parent, null, type(node1) +
                ' node should be removed from its parent group');
            assert_equals(node1.previousSibling, null, type(node1) +
                ' node previousSibling attribute should be updated');
            assert_equals(node1.nextSibling, null, type(node1) +
                ' node nextSibling attribute should be updated');
        });
    });
}, 'AnimationNode.replace() removes the node from its parent animation group');

test(function() {
    var test = this;
    var parents = [AnimationGroup, AnimationSequence];
    parents.forEach(function(parentCtor) {
        var nodes = [newAnimation(createDiv(test)), new AnimationGroup([]), new AnimationSequence([])];
        nodes.forEach(function(node2) {
            var node1 = newAnimation(createDiv(test));
            var node3 = newAnimation(createDiv(test));
            var parent = new parentCtor([node1, node2, node3]);

            node2.replace(node3);

            assert_array_equals(parent.children, [node1,node3], type(node2) +
                ' node should be removed from parent ' + parentCtor.name);
            assert_equals(node2.parent, null, type(node2) +
                ' node parent attribute should be updated');
            assert_equals(node2.nextSibling, null, type(node2) +
                ' node nextSibling attribute should be updated');
            assert_equals(node2.previousSibling, null, type(node2) +
                ' node previousSibling attribute should be updated');
            assert_equals(node1.nextSibling, node3,
                'Sibling node nextSibling attribute should be updated');
            assert_equals(node3.previousSibling, node1,
                'Sibling node previousSibling attribute should be updated');
        });
    });
}, 'AnimationNode.replace(next sibling) removes the node from its parent ' +
    'animation group');

// Step 5. Insert nodes before reference child.
test(function() {
    var test = this;
    var parents = [AnimationGroup, AnimationSequence];
    parents.forEach(function(parentCtor) {
        var nodes = [newAnimation(createDiv(test)), new AnimationGroup([]), new AnimationSequence([])];
        nodes.forEach(function(node1) {
            var node2 = newAnimation(createDiv(test));
            var parent = new parentCtor([node1]);

            node1.replace(node2);

            assert_array_equals(parent.children, [node2], type(node1) +
                ' node should be replaced');
            assert_equals(node2.parent, parent,
                'Replacement node should be assigned to a parent group');
        });
    });
}, 'AnimationNode.replace() replaces node in the parent animation group');

test(function() {
    var test = this;
    var parents = [AnimationGroup, AnimationSequence];
    parents.forEach(function(parentCtor) {
        var nodes = [newAnimation(createDiv(test)), new AnimationGroup([]), new AnimationSequence([])];
        nodes.forEach(function(node4) {
            var node1 = newAnimation(createDiv(test));
            var node2 = newAnimation(createDiv(test));
            var node3 = newAnimation(createDiv(test));
            var parent1 = new parentCtor([node1, node2]);
            var parent2 = new parentCtor([node3, parent1, node4]);

            node4.replace(node1);

            assert_array_equals(parent1.children, [node2],
                'Replacement node should be removed from its previous position in the tree');
            assert_array_equals(parent2.children, [node3, parent1, node1],
                'Replacement node should be inserted into the new position');
            assert_equals(node1.parent, parent2, 'Inserted node parent group should be assigned');
            assert_equals(node1.previousSibling, parent1,
                'Inserted node previousSibling attribute should be updated');
            assert_equals(node1.nextSibling, null,
                'Inserted node nextSibling attribute should be updated');

            assert_equals(node4.parent, null, 'Node should be removed from its parent group');
            assert_equals(node4.previousSibling, null,
                'Replaced node previousSibling attribute should be updated');
            assert_equals(node4.nextSibling, null,
                'Replaced node nextSibling attribute should be updated');
        });
    });
}, 'Test AnimationNode.replace() replaces given node. The previous position ' +
    'of the replacement node is deeper in the tree than the current node');

test(function() {
    var test = this;
    var parents = [AnimationGroup, AnimationSequence];
    parents.forEach(function(parentCtor) {
        var nodes = [newAnimation(createDiv(test)), new AnimationGroup([]), new AnimationSequence([])];
        nodes.forEach(function(node2) {
            var node1 = newAnimation(createDiv(test));
            var node3 = newAnimation(createDiv(test));
            var node4 = newAnimation(createDiv(test));
            var parent1 = new parentCtor([node1, node2]);
            var parent2 = new parentCtor([node3, parent1, node4]);

            node2.replace(node4);

            assert_array_equals(parent1.children, [node1, node4],
                'Replacement node should be inserted to the new position');
            assert_array_equals(parent2.children, [node3, parent1],
                'Replacement node should be removed from its previous position in the tree');
            assert_equals(node4.parent, parent1, 'Inserted node parent group should be changed');
            assert_equals(node4.previousSibling, node1,
                'Inserted node previousSibling attribute should be updated');
            assert_equals(node1.nextSibling, node4,
                'Inserted node sibling nextSibling attribute should be updated');

            assert_equals(node2.parent, null, 'Replaced node parent group should be changed');
            assert_equals(node2.previousSibling, null,
                'Replaced node previousSibling attribute should be updated');
            assert_equals(node2.nextSibling, null, 'Replaced node nextSibling attribute ' +
                'should be updated');
        });
    });
}, 'Test AnimationNode.replace() replaces given node. The previous position ' +
    'of the replacement node is shallower in the tree than current node, but is not an ancestor');

test(function() {
    var test = this;
    var parents = [AnimationGroup, AnimationSequence];
    parents.forEach(function(parentCtor) {
        var nodes = [newAnimation(createDiv(test)), new AnimationGroup([]), new AnimationSequence([])];
        nodes.forEach(function(node2) {
            var node1 = newAnimation(createDiv(test));
            var node3 = newAnimation(createDiv(test));
            var node4 = newAnimation(createDiv(test));
            var parent = new parentCtor([node1, node2]);

            node2.replace(node3, node4);

            assert_array_equals(parent.children, [node1, node3, node4], type(node2) + '.replace() ' +
                'should replace the current node by ones passed in arguments');
            assert_equals(node1.nextSibling, node3, 'nextSibling attribute should be updated');
            assert_equals(node3.previousSibling, node1, 'Inserted node previousSibling attribute ' +
                'should be updated');
            assert_equals(node3.nextSibling, node4, 'Inserted node nextSibling attribute ' +
                'should be updated');
            assert_equals(node3.parent, parent, 'Parent group of the inserted node should be changed');
            assert_equals(node4.previousSibling, node3, 'Inserted node previousSibling attribute ' +
                'should be updated');
            assert_equals(node4.nextSibling, null, 'Inserted node nextSibling attribute ' +
                'should be updated');
            assert_equals(node4.parent, parent, 'Parent group of the second inserted node ' +
                'should be changed');
            assert_equals(node2.parent, null, 'Replaced node parent group should be changed');
            assert_equals(node2.previousSibling, null,
                'Replaced node previousSibling attribute should be updated');
            assert_equals(node2.nextSibling, null, 'Replaced node nextSibling attribute ' +
                'should be updated');
        });
    });
}, 'Test AnimationNode.replace() replaces given node. Test several arguments');

test(function() {
    var test = this;
    var parents = [AnimationGroup, AnimationSequence];
    parents.forEach(function(parentCtor) {
        var nodes = [newAnimation(createDiv(test)), new AnimationGroup([]), new AnimationSequence([])];
        nodes.forEach(function(node2) {
            var node1 = newAnimation(createDiv(test));
            var node3 = newAnimation(createDiv(test));
            var node4 = newAnimation(createDiv(test));
            var parent = new parentCtor([node1, node2]);

            node2.replace(node3, node4, node3, node4);

            assert_array_equals(parent.children, [node1, node3, node4], type(node2) + '.replace() ' +
                'should replace the current node by ones passed in arguments');
            assert_equals(node1.nextSibling, node3, 'nextSibling attribute should be updated');
            assert_equals(node3.previousSibling, node1, 'Inserted node previousSibling attribute ' +
                'should be updated');
            assert_equals(node3.nextSibling, node4, 'Inserted node nextSibling attribute ' +
                'should be updated');
            assert_equals(node3.parent, parent, 'Parent group of the inserted node should be changed');
            assert_equals(node4.previousSibling, node3, 'Inserted node previousSibling attribute ' +
                'should be updated');
            assert_equals(node4.nextSibling, null, 'Inserted node nextSibling attribute ' +
                'should be updated');
            assert_equals(node4.parent, parent, 'Parent group of the second inserted node ' +
                'should be changed');
            assert_equals(node2.parent, null, 'Replaced node parent group should be changed');
            assert_equals(node2.previousSibling, null,
                'Replaced node previousSibling attribute should be updated');
            assert_equals(node2.nextSibling, null, 'Replaced node nextSibling attribute ' +
                'should be updated');
        });
    });
}, 'Test AnimationNode.replace() replaces given node by several nodes, duplicate nodes are ignored');

test(function() {
    var test = this;
    var parents = [AnimationGroup, AnimationSequence];
    parents.forEach(function(parentCtor) {
        var nodes = [newAnimation(createDiv(test)), new AnimationGroup([]), new AnimationSequence([])];
        nodes.forEach(function(node2) {
            var node1 = newAnimation(createDiv(test));
            var node3 = newAnimation(createDiv(test));
            var node4 = newAnimation(createDiv(test));
            var parent = new parentCtor([node1, node2]);

            node2.replace(node3, node4, node3);

            assert_array_equals(parent.children, [node1, node4, node3], type(node2) + '.replace() ' +
                'should replace the current node by ones passed in arguments');
            assert_equals(node1.nextSibling, node4, 'nextSibling attribute should be updated');
            assert_equals(node4.previousSibling, node1, 'Inserted node previousSibling attribute ' +
                'should be updated');
            assert_equals(node4.nextSibling, node3, 'Inserted node nextSibling attribute ' +
                'should be updated');
            assert_equals(node4.parent, parent, 'Parent group of the inserted node should be changed');
            assert_equals(node3.previousSibling, node4, 'Inserted node previousSibling attribute ' +
                'should be updated');
            assert_equals(node3.nextSibling, null, 'Inserted node nextSibling attribute ' +
                'should be updated');
            assert_equals(node3.parent, parent, 'Parent group of the second inserted node ' +
                'should be changed');
            assert_equals(node2.parent, null, 'Replaced node parent group should be changed');
            assert_equals(node2.previousSibling, null,
                'Replaced node previousSibling attribute should be updated');
            assert_equals(node2.nextSibling, null, 'Replaced node nextSibling attribute ' +
                'should be updated');
        });
    });
}, 'Test AnimationNode.replace() replaces given node by several nodes, check replacement order');

test(function() {
    var test = this;
    var parents = [AnimationGroup, AnimationSequence];
    parents.forEach(function(parentCtor) {
        var nodes = [newAnimation(createDiv(test)), new AnimationGroup([]), new AnimationSequence([])];
        nodes.forEach(function(node1) {
            var node2 = newAnimation(createDiv(test));
            var parent = new parentCtor([node1]);
            var player = document.timeline.play(node2);

            assert_equals(player.source, node2, 'The node should be associated with its player');
            node1.replace(node2);
            assert_equals(player.source, null, 'The node should be disassociated from its player');
        });
    });
}, 'Test AnimationNode.replace() disassociates the inserted node from the player, ' +
    'if node is directly associated with a player');
</script>
</body>