forked from angular/angular
/
dom_animation_engine_spec.ts
702 lines (555 loc) · 25.9 KB
/
dom_animation_engine_spec.ts
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {AnimationEvent, NoopAnimationPlayer, animate, keyframes, state, style, transition, trigger} from '@angular/animations';
import {el} from '@angular/platform-browser/testing/src/browser_util';
import {buildAnimationKeyframes} from '../../src/dsl/animation_timeline_visitor';
import {buildTrigger} from '../../src/dsl/animation_trigger';
import {AnimationStyleNormalizer, NoopAnimationStyleNormalizer} from '../../src/dsl/style_normalization/animation_style_normalizer';
import {DomAnimationEngine} from '../../src/render/dom_animation_engine';
import {MockAnimationDriver, MockAnimationPlayer} from '../../testing/src/mock_animation_driver';
function makeTrigger(name: string, steps: any) {
const triggerData = trigger(name, steps);
const triggerInstance = buildTrigger(triggerData.name, triggerData.definitions);
return triggerInstance;
}
export function main() {
const driver = new MockAnimationDriver();
// these tests are only mean't to be run within the DOM
if (typeof Element == 'undefined') return;
describe('DomAnimationEngine', () => {
let element: any;
beforeEach(() => {
MockAnimationDriver.log = [];
element = el('<div></div>');
});
function makeEngine(normalizer: AnimationStyleNormalizer = null) {
return new DomAnimationEngine(driver, normalizer || new NoopAnimationStyleNormalizer());
}
describe('trigger registration', () => {
it('should ignore and not throw an error if the same trigger is registered twice', () => {
const engine = makeEngine();
engine.registerTrigger(trigger('trig', []));
expect(() => { engine.registerTrigger(trigger('trig', [])); }).not.toThrow();
});
});
describe('property setting', () => {
it('should invoke a transition based on a property change', () => {
const engine = makeEngine();
const trig = trigger('myTrigger', [
transition('* => *', [style({height: '0px'}), animate(1000, style({height: '100px'}))])
]);
engine.registerTrigger(trig);
expect(engine.queuedPlayers.length).toEqual(0);
engine.setProperty(element, 'myTrigger', 'value');
expect(engine.queuedPlayers.length).toEqual(1);
const player = MockAnimationDriver.log.pop() as MockAnimationPlayer;
expect(player.keyframes).toEqual([
{height: '0px', offset: 0}, {height: '100px', offset: 1}
]);
});
it('should always invoke an animation even if the property change is not matched', () => {
const engine = makeEngine();
const trig = trigger(
'myTrigger',
[transition(
'yes => no', [style({height: '0px'}), animate(1000, style({height: '100px'}))])]);
engine.registerTrigger(trig);
expect(engine.queuedPlayers.length).toEqual(0);
engine.setProperty(element, 'myTrigger', 'no');
expect(engine.queuedPlayers.length).toEqual(1);
expect(engine.queuedPlayers.pop() instanceof NoopAnimationPlayer).toBe(true);
engine.flush();
engine.setProperty(element, 'myTrigger', 'yes');
expect(engine.queuedPlayers.length).toEqual(1);
expect(engine.queuedPlayers.pop() instanceof NoopAnimationPlayer).toBe(true);
});
it('should not initialize the animation until the engine has been flushed', () => {
const engine = makeEngine();
engine.registerTrigger(trigger(
'trig', [transition('* => something', [animate(1000, style({color: 'gold'}))])]));
engine.setProperty(element, 'trig', 'something');
const player = engine.queuedPlayers.pop() as MockAnimationPlayer;
let initialized = false;
player.onInit(() => initialized = true);
expect(initialized).toBe(false);
engine.flush();
expect(initialized).toBe(true);
});
it('should not queue an animation if the property value has not changed at all', () => {
const engine = makeEngine();
const trig = trigger('myTrigger', [
transition('* => *', [style({height: '0px'}), animate(1000, style({height: '100px'}))])
]);
engine.registerTrigger(trig);
expect(engine.queuedPlayers.length).toEqual(0);
engine.setProperty(element, 'myTrigger', 'abc');
expect(engine.queuedPlayers.length).toEqual(1);
engine.setProperty(element, 'myTrigger', 'abc');
expect(engine.queuedPlayers.length).toEqual(1);
});
it('should throw an error if an animation property without a matching trigger is changed',
() => {
const engine = makeEngine();
expect(() => {
engine.setProperty(element, 'myTrigger', 'no');
}).toThrowError(/The provided animation trigger "myTrigger" has not been registered!/);
});
});
describe('event listeners', () => {
it('should listen to the onStart operation for the animation', () => {
const engine = makeEngine();
const trig = trigger('myTrigger', [
transition('* => *', [style({height: '0px'}), animate(1000, style({height: '100px'}))])
]);
let count = 0;
engine.registerTrigger(trig);
engine.listen(element, 'myTrigger', 'start', () => count++);
engine.setProperty(element, 'myTrigger', 'value');
expect(count).toEqual(0);
engine.flush();
expect(count).toEqual(1);
});
it('should listen to the onDone operation for the animation', () => {
const engine = makeEngine();
const trig = trigger('myTrigger', [
transition('* => *', [style({height: '0px'}), animate(1000, style({height: '100px'}))])
]);
let count = 0;
engine.registerTrigger(trig);
engine.listen(element, 'myTrigger', 'done', () => count++);
engine.setProperty(element, 'myTrigger', 'value');
expect(count).toEqual(0);
engine.flush();
expect(count).toEqual(0);
const player = engine.activePlayers.pop();
player.finish();
expect(count).toEqual(1);
});
it('should throw an error when an event is listened to that isn\'t supported', () => {
const engine = makeEngine();
const trig = trigger('myTrigger', []);
engine.registerTrigger(trig);
expect(() => { engine.listen(element, 'myTrigger', 'explode', () => {}); })
.toThrowError(
/The provided animation trigger event "explode" for the animation trigger "myTrigger" is not supported!/);
});
it('should throw an error when an event is listened for a trigger that doesn\'t exist', () => {
const engine = makeEngine();
expect(() => { engine.listen(element, 'myTrigger', 'explode', () => {}); })
.toThrowError(
/Unable to listen on the animation trigger event "explode" because the animation trigger "myTrigger" doesn\'t exist!/);
});
it('should throw an error when an undefined event is listened for', () => {
const engine = makeEngine();
const trig = trigger('myTrigger', []);
engine.registerTrigger(trig);
expect(() => { engine.listen(element, 'myTrigger', '', () => {}); })
.toThrowError(
/Unable to listen on the animation trigger "myTrigger" because the provided event is undefined!/);
});
it('should retain event listeners and call them for sucessive animation state changes',
() => {
const engine = makeEngine();
const trig = trigger(
'myTrigger',
[transition(
'* => *', [style({height: '0px'}), animate(1000, style({height: '100px'}))])]);
engine.registerTrigger(trig);
let count = 0;
engine.listen(element, 'myTrigger', 'start', () => count++);
engine.setProperty(element, 'myTrigger', '123');
engine.flush();
expect(count).toEqual(1);
engine.setProperty(element, 'myTrigger', '456');
engine.flush();
expect(count).toEqual(2);
});
it('should only fire event listener changes for when the corresponding trigger changes state',
() => {
const engine = makeEngine();
const trig1 = trigger(
'myTrigger1',
[transition(
'* => 123', [style({height: '0px'}), animate(1000, style({height: '100px'}))])]);
engine.registerTrigger(trig1);
const trig2 = trigger(
'myTrigger2',
[transition(
'* => 123', [style({width: '0px'}), animate(1000, style({width: '100px'}))])]);
engine.registerTrigger(trig2);
let count = 0;
engine.listen(element, 'myTrigger1', 'start', () => count++);
engine.setProperty(element, 'myTrigger1', '123');
engine.flush();
expect(count).toEqual(1);
engine.setProperty(element, 'myTrigger2', '123');
engine.flush();
expect(count).toEqual(1);
});
it('should allow a listener to be deregistered', () => {
const engine = makeEngine();
const trig = trigger(
'myTrigger',
[transition(
'* => 123', [style({height: '0px'}), animate(1000, style({height: '100px'}))])]);
engine.registerTrigger(trig);
let count = 0;
const deregisterFn = engine.listen(element, 'myTrigger', 'start', () => count++);
engine.setProperty(element, 'myTrigger', '123');
engine.flush();
expect(count).toEqual(1);
deregisterFn();
engine.setProperty(element, 'myTrigger', '456');
engine.flush();
expect(count).toEqual(1);
});
it('should trigger a listener callback with an AnimationEvent argument', () => {
const engine = makeEngine();
engine.registerTrigger(trigger(
'myTrigger',
[transition(
'* => *', [style({height: '0px'}), animate(1234, style({height: '100px'}))])]));
// we do this so that the next transition has a starting value that isnt null
engine.setProperty(element, 'myTrigger', '123');
engine.flush();
let capture: AnimationEvent = null;
engine.listen(element, 'myTrigger', 'start', (e) => capture = e);
engine.listen(element, 'myTrigger', 'done', (e) => capture = e);
engine.setProperty(element, 'myTrigger', '456');
engine.flush();
expect(capture).toEqual({
element,
triggerName: 'myTrigger',
phaseName: 'start',
fromState: '123',
toState: '456',
totalTime: 1234
});
capture = null;
const player = engine.activePlayers.pop();
player.finish();
expect(capture).toEqual({
element,
triggerName: 'myTrigger',
phaseName: 'done',
fromState: '123',
toState: '456',
totalTime: 1234
});
});
});
describe('instructions', () => {
it('should animate a transition instruction', () => {
const engine = makeEngine();
const trig = makeTrigger('something', [
state('on', style({height: 100})), state('off', style({height: 0})),
transition('on => off', animate(9876))
]);
const instruction = trig.matchTransition('on', 'off');
expect(MockAnimationDriver.log.length).toEqual(0);
engine.animateTransition(element, instruction);
expect(MockAnimationDriver.log.length).toEqual(1);
});
it('should animate a timeline instruction', () => {
const engine = makeEngine();
const timelines =
buildAnimationKeyframes([style({height: 100}), animate(1000, style({height: 0}))]);
expect(MockAnimationDriver.log.length).toEqual(0);
engine.animateTimeline(element, timelines);
expect(MockAnimationDriver.log.length).toEqual(1);
});
it('should animate an array of animation instructions', () => {
const engine = makeEngine();
const instructions = buildAnimationKeyframes([
style({height: 100}), animate(1000, style({height: 0})),
animate(1000, keyframes([style({width: 0}), style({width: 1000})]))
]);
expect(MockAnimationDriver.log.length).toEqual(0);
engine.animateTimeline(element, instructions);
expect(MockAnimationDriver.log.length).toBeGreaterThan(0);
});
});
describe('removals / insertions', () => {
it('should allow text nodes to be removed through the engine', () => {
const engine = makeEngine();
const node = document.createTextNode('hello');
element.appendChild(node);
let called = false;
engine.onRemove(node, () => called = true);
expect(called).toBeTruthy();
});
it('should allow text nodes to be inserted through the engine', () => {
const engine = makeEngine();
const node = document.createTextNode('hello');
let called = false;
engine.onInsert(node, () => called = true);
expect(called).toBeTruthy();
});
});
describe('transition operations', () => {
it('should persist the styles on the element as actual styles once the animation is complete',
() => {
const engine = makeEngine();
const trig = makeTrigger('something', [
state('on', style({height: '100px'})), state('off', style({height: '0px'})),
transition('on => off', animate(9876))
]);
const instruction = trig.matchTransition('on', 'off');
const player = engine.animateTransition(element, instruction);
expect(element.style.height).not.toEqual('0px');
player.finish();
expect(element.style.height).toEqual('0px');
});
it('should remove all existing state styling from an element when a follow-up transition occurs on the same trigger',
() => {
const engine = makeEngine();
const trig = makeTrigger('something', [
state('a', style({height: '100px'})), state('b', style({height: '500px'})),
state('c', style({width: '200px'})), transition('* => *', animate(9876))
]);
const instruction1 = trig.matchTransition('a', 'b');
const player1 = engine.animateTransition(element, instruction1);
player1.finish();
expect(element.style.height).toEqual('500px');
const instruction2 = trig.matchTransition('b', 'c');
const player2 = engine.animateTransition(element, instruction2);
expect(element.style.height).not.toEqual('500px');
player2.finish();
expect(element.style.width).toEqual('200px');
expect(element.style.height).not.toEqual('500px');
});
it('should allow two animation transitions with different triggers to animate in parallel',
() => {
const engine = makeEngine();
const trig1 = makeTrigger('something1', [
state('a', style({width: '100px'})), state('b', style({width: '200px'})),
transition('* => *', animate(1000))
]);
const trig2 = makeTrigger('something2', [
state('x', style({height: '500px'})), state('y', style({height: '1000px'})),
transition('* => *', animate(2000))
]);
let doneCount = 0;
function doneCallback() { doneCount++; }
const instruction1 = trig1.matchTransition('a', 'b');
const instruction2 = trig2.matchTransition('x', 'y');
const player1 = engine.animateTransition(element, instruction1);
player1.onDone(doneCallback);
expect(doneCount).toEqual(0);
const player2 = engine.animateTransition(element, instruction2);
player2.onDone(doneCallback);
expect(doneCount).toEqual(0);
player1.finish();
expect(doneCount).toEqual(1);
player2.finish();
expect(doneCount).toEqual(2);
expect(element.style.width).toEqual('200px');
expect(element.style.height).toEqual('1000px');
});
it('should cancel a previously running animation when a follow-up transition kicks off on the same trigger',
() => {
const engine = makeEngine();
const trig = makeTrigger('something', [
state('x', style({opacity: 0})), state('y', style({opacity: .5})),
state('z', style({opacity: 1})), transition('* => *', animate(1000))
]);
const instruction1 = trig.matchTransition('x', 'y');
const instruction2 = trig.matchTransition('y', 'z');
expect(parseFloat(element.style.opacity)).not.toEqual(.5);
const player1 = engine.animateTransition(element, instruction1);
const player2 = engine.animateTransition(element, instruction2);
expect(parseFloat(element.style.opacity)).toEqual(.5);
player2.finish();
expect(parseFloat(element.style.opacity)).toEqual(1);
player1.finish();
expect(parseFloat(element.style.opacity)).toEqual(1);
});
it('should pass in the previously running players into the follow-up transition player when cancelled',
() => {
const engine = makeEngine();
const trig = makeTrigger('something', [
state('x', style({opacity: 0})), state('y', style({opacity: .5})),
state('z', style({opacity: 1})), transition('* => *', animate(1000))
]);
const instruction1 = trig.matchTransition('x', 'y');
const instruction2 = trig.matchTransition('y', 'z');
const instruction3 = trig.matchTransition('z', 'x');
const player1 = engine.animateTransition(element, instruction1);
engine.flush();
player1.setPosition(0.5);
const player2 = <MockAnimationPlayer>engine.animateTransition(element, instruction2);
expect(player2.previousPlayers).toEqual([player1]);
player2.finish();
const player3 = <MockAnimationPlayer>engine.animateTransition(element, instruction3);
expect(player3.previousPlayers).toEqual([]);
});
it('should cancel all existing players if a removal animation is set to occur', () => {
const engine = makeEngine();
const trig = makeTrigger('something', [
state('m', style({opacity: 0})), state('n', style({opacity: 1})),
transition('* => *', animate(1000))
]);
let doneCount = 0;
function doneCallback() { doneCount++; }
const instruction1 = trig.matchTransition('m', 'n');
const instructions2 =
buildAnimationKeyframes([style({height: 0}), animate(1000, style({height: 100}))]);
const instruction3 = trig.matchTransition('n', 'void');
const player1 = engine.animateTransition(element, instruction1);
player1.onDone(doneCallback);
const player2 = engine.animateTimeline(element, instructions2);
player2.onDone(doneCallback);
engine.flush();
expect(doneCount).toEqual(0);
const player3 = engine.animateTransition(element, instruction3);
expect(doneCount).toEqual(2);
});
it('should only persist styles that exist in the final state styles and not the last keyframe',
() => {
const engine = makeEngine();
const trig = makeTrigger('something', [
state('0', style({width: '0px'})), state('1', style({width: '100px'})),
transition('* => *', [animate(1000, style({height: '200px'}))])
]);
const instruction = trig.matchTransition('0', '1');
const player = engine.animateTransition(element, instruction);
expect(element.style.width).not.toEqual('100px');
player.finish();
expect(element.style.height).not.toEqual('200px');
expect(element.style.width).toEqual('100px');
});
it('should default to using styling from the `*` state if a matching state is not found',
() => {
const engine = makeEngine();
const trig = makeTrigger('something', [
state('a', style({opacity: 0})), state('*', style({opacity: .5})),
transition('* => *', animate(1000))
]);
const instruction = trig.matchTransition('a', 'z');
engine.animateTransition(element, instruction).finish();
expect(parseFloat(element.style.opacity)).toEqual(.5);
});
it('should treat `void` as `void`', () => {
const engine = makeEngine();
const trig = makeTrigger('something', [
state('a', style({opacity: 0})), state('void', style({opacity: .8})),
transition('* => *', animate(1000))
]);
const instruction = trig.matchTransition('a', 'void');
engine.animateTransition(element, instruction).finish();
expect(parseFloat(element.style.opacity)).toEqual(.8);
});
});
describe('timeline operations', () => {
it('should not destroy timeline-based animations after they have finished', () => {
const engine = makeEngine();
const log: string[] = [];
function capture(value: string) {
return () => { log.push(value); };
}
const instructions =
buildAnimationKeyframes([style({height: 0}), animate(1000, style({height: 500}))]);
const player = engine.animateTimeline(element, instructions);
player.onDone(capture('done'));
player.onDestroy(capture('destroy'));
expect(log).toEqual([]);
player.finish();
expect(log).toEqual(['done']);
player.destroy();
expect(log).toEqual(['done', 'destroy']);
});
});
describe('style normalizer', () => {
it('should normalize the style values that are animateTransitioned within an a transition animation',
() => {
const engine = makeEngine(new SuffixNormalizer('-normalized'));
const trig = makeTrigger('something', [
state('on', style({height: 100})), state('off', style({height: 0})),
transition('on => off', animate(9876))
]);
const instruction = trig.matchTransition('on', 'off');
const player = <MockAnimationPlayer>engine.animateTransition(element, instruction);
expect(player.keyframes).toEqual([
{'height-normalized': '100-normalized', offset: 0},
{'height-normalized': '0-normalized', offset: 1}
]);
});
it('should normalize the style values that are animateTransitioned within an a timeline animation',
() => {
const engine = makeEngine(new SuffixNormalizer('-normalized'));
const instructions = buildAnimationKeyframes([
style({width: '333px'}),
animate(1000, style({width: '999px'})),
]);
const player = <MockAnimationPlayer>engine.animateTimeline(element, instructions);
expect(player.keyframes).toEqual([
{'width-normalized': '333px-normalized', offset: 0},
{'width-normalized': '999px-normalized', offset: 1}
]);
});
it('should throw an error when normalization fails within a transition animation', () => {
const engine = makeEngine(new ExactCssValueNormalizer({left: '100px'}));
const trig = makeTrigger('something', [
state('a', style({left: '0px', width: '200px'})),
state('b', style({left: '100px', width: '100px'})), transition('a => b', animate(9876))
]);
const instruction = trig.matchTransition('a', 'b');
let errorMessage = '';
try {
engine.animateTransition(element, instruction);
} catch (e) {
errorMessage = e.toString();
}
expect(errorMessage).toMatch(/Unable to animate due to the following errors:/);
expect(errorMessage).toMatch(/- The CSS property `left` is not allowed to be `0px`/);
expect(errorMessage).toMatch(/- The CSS property `width` is not allowed/);
});
});
describe('view operations', () => {
it('should perform insert operations immediately ', () => {
const engine = makeEngine();
let container = <any>el('<div></div>');
let child1 = <any>el('<div></div>');
let child2 = <any>el('<div></div>');
engine.onInsert(container, () => container.appendChild(child1));
engine.onInsert(container, () => container.appendChild(child2));
expect(container.contains(child1)).toBe(true);
expect(container.contains(child2)).toBe(true);
});
});
});
}
class SuffixNormalizer extends AnimationStyleNormalizer {
constructor(private _suffix: string) { super(); }
normalizePropertyName(propertyName: string, errors: string[]): string {
return propertyName + this._suffix;
}
normalizeStyleValue(
userProvidedProperty: string, normalizedProperty: string, value: string|number,
errors: string[]): string {
return value + this._suffix;
}
}
class ExactCssValueNormalizer extends AnimationStyleNormalizer {
constructor(private _allowedValues: {[propName: string]: any}) { super(); }
normalizePropertyName(propertyName: string, errors: string[]): string {
if (!this._allowedValues[propertyName]) {
errors.push(`The CSS property \`${propertyName}\` is not allowed`);
}
return propertyName;
}
normalizeStyleValue(
userProvidedProperty: string, normalizedProperty: string, value: string|number,
errors: string[]): string {
const expectedValue = this._allowedValues[userProvidedProperty];
if (expectedValue != value) {
errors.push(`The CSS property \`${userProvidedProperty}\` is not allowed to be \`${value}\``);
}
return expectedValue;
}
}