/
animations_with_web_animations_integration_spec.ts
643 lines (554 loc) · 21 KB
/
animations_with_web_animations_integration_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
/**
* @license
* Copyright Google LLC 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 {animate, query, state, style, transition, trigger} from '@angular/animations';
import {AnimationDriver, ɵAnimationEngine, ɵWebAnimationsDriver, ɵWebAnimationsPlayer} from '@angular/animations/browser';
import {TransitionAnimationPlayer} from '@angular/animations/browser/src/render/transition_animation_engine';
import {AnimationGroupPlayer} from '@angular/animations/src/players/animation_group_player';
import {Component, ViewChild} from '@angular/core';
import {TestBed} from '@angular/core/testing';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {browserDetection} from '@angular/platform-browser/testing/src/browser_util';
(function() {
// these tests are only meant to be run within the DOM (for now)
// Buggy in Chromium 39, see https://github.com/angular/angular/issues/15793
if (isNode) return;
describe('animation integration tests using web animations', function() {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [{provide: AnimationDriver, useClass: ɵWebAnimationsDriver}],
imports: [BrowserAnimationsModule]
});
});
it('should compute (*) animation styles for a container that is being removed', () => {
@Component({
selector: 'ani-cmp',
template: `
<div @auto *ngIf="exp">
<div style="line-height:20px;">1</div>
<div style="line-height:20px;">2</div>
<div style="line-height:20px;">3</div>
<div style="line-height:20px;">4</div>
<div style="line-height:20px;">5</div>
</div>
`,
animations: [trigger(
'auto',
[
state('void', style({height: '0px'})), state('*', style({height: '*'})),
transition('* => *', animate(1000))
])]
})
class Cmp {
public exp: boolean = false;
}
TestBed.configureTestingModule({declarations: [Cmp]});
const engine = TestBed.inject(ɵAnimationEngine);
const fixture = TestBed.createComponent(Cmp);
const cmp = fixture.componentInstance;
cmp.exp = true;
fixture.detectChanges();
expect(engine.players.length).toEqual(1);
let webPlayer =
(engine.players[0] as TransitionAnimationPlayer).getRealPlayer() as ɵWebAnimationsPlayer;
expect(webPlayer.keyframes).toEqual([
new Map<string, string|number>([['height', '0px'], ['offset', 0]]),
new Map<string, string|number>([['height', '100px'], ['offset', 1]])
]);
webPlayer.finish();
if (!browserDetection.isOldChrome) {
cmp.exp = false;
fixture.detectChanges();
engine.flush();
expect(engine.players.length).toEqual(1);
webPlayer =
(engine.players[0] as TransitionAnimationPlayer).getRealPlayer() as ɵWebAnimationsPlayer;
expect(webPlayer.keyframes).toEqual([
new Map<string, string|number>([['height', '100px'], ['offset', 0]]),
new Map<string, string|number>([['height', '0px'], ['offset', 1]])
]);
}
});
it('should compute (!) animation styles for a container that is being inserted', () => {
@Component({
selector: 'ani-cmp',
template: `
<div @auto *ngIf="exp">
<div style="line-height:20px;">1</div>
<div style="line-height:20px;">2</div>
<div style="line-height:20px;">3</div>
<div style="line-height:20px;">4</div>
<div style="line-height:20px;">5</div>
</div>
`,
animations: [trigger(
'auto',
[transition(':enter', [style({height: '!'}), animate(1000, style({height: '120px'}))])])]
})
class Cmp {
public exp: boolean = false;
}
TestBed.configureTestingModule({declarations: [Cmp]});
const engine = TestBed.inject(ɵAnimationEngine);
const fixture = TestBed.createComponent(Cmp);
const cmp = fixture.componentInstance;
cmp.exp = true;
fixture.detectChanges();
engine.flush();
expect(engine.players.length).toEqual(1);
let webPlayer =
(engine.players[0] as TransitionAnimationPlayer).getRealPlayer() as ɵWebAnimationsPlayer;
expect(webPlayer.keyframes).toEqual([
new Map<string, string|number>([['height', '100px'], ['offset', 0]]),
new Map<string, string|number>([['height', '120px'], ['offset', 1]])
]);
});
it('should compute pre (!) and post (*) animation styles with different dom states', () => {
@Component({
selector: 'ani-cmp',
template: `
<div [@myAnimation]="exp" #parent>
<div *ngFor="let item of items" class="child" style="line-height:20px">
- {{ item }}
</div>
</div>
`,
animations: [trigger(
'myAnimation',
[transition('* => *', [style({height: '!'}), animate(1000, style({height: '*'}))])])]
})
class Cmp {
// TODO(issue/24571): remove '!'.
public exp!: number;
public items = [0, 1, 2, 3, 4];
}
TestBed.configureTestingModule({declarations: [Cmp]});
const engine = TestBed.inject(ɵAnimationEngine);
const fixture = TestBed.createComponent(Cmp);
const cmp = fixture.componentInstance;
cmp.exp = 1;
fixture.detectChanges();
engine.flush();
expect(engine.players.length).toEqual(1);
let player = engine.players[0];
let webPlayer = (player as TransitionAnimationPlayer).getRealPlayer() as ɵWebAnimationsPlayer;
expect(webPlayer.keyframes).toEqual([
new Map<string, string|number>([['height', '0px'], ['offset', 0]]),
new Map<string, string|number>([['height', '100px'], ['offset', 1]])
]);
// we destroy the player because since it has started and is
// at 0ms duration a height value of `0px` will be extracted
// from the element and passed into the follow-up animation.
player.destroy();
cmp.exp = 2;
cmp.items = [0, 1, 2, 6];
fixture.detectChanges();
engine.flush();
expect(engine.players.length).toEqual(1);
player = engine.players[0];
webPlayer = (player as TransitionAnimationPlayer).getRealPlayer() as ɵWebAnimationsPlayer;
expect(webPlayer.keyframes).toEqual([
new Map<string, string|number>([['height', '100px'], ['offset', 0]]),
new Map<string, string|number>([['height', '80px'], ['offset', 1]])
]);
});
it('should treat * styles as ! when a removal animation is being rendered', () => {
@Component({
selector: 'ani-cmp',
styles: [`
.box {
width: 500px;
overflow:hidden;
background:orange;
line-height:300px;
font-size:100px;
text-align:center;
}
`],
template: `
<button (click)="toggle()">Open / Close</button>
<hr />
<div *ngIf="exp" @slide class="box">
...
</div>
`,
animations: [trigger(
'slide',
[
state('void', style({height: '0px'})),
state('*', style({height: '*'})),
transition('* => *', animate('500ms')),
])]
})
class Cmp {
exp = false;
toggle() {
this.exp = !this.exp;
}
}
TestBed.configureTestingModule({declarations: [Cmp]});
const engine = TestBed.inject(ɵAnimationEngine);
const fixture = TestBed.createComponent(Cmp);
const cmp = fixture.componentInstance;
cmp.exp = true;
fixture.detectChanges();
let player = engine.players[0]!;
let webPlayer = (player as TransitionAnimationPlayer).getRealPlayer() as ɵWebAnimationsPlayer;
expect(webPlayer.keyframes).toEqual([
new Map<string, string|number>([['height', '0px'], ['offset', 0]]),
new Map<string, string|number>([['height', '300px'], ['offset', 1]]),
]);
player.finish();
cmp.exp = false;
fixture.detectChanges();
player = engine.players[0]!;
webPlayer = (player as TransitionAnimationPlayer).getRealPlayer() as ɵWebAnimationsPlayer;
expect(webPlayer.keyframes).toEqual([
new Map<string, string|number>([['height', '300px'], ['offset', 0]]),
new Map<string, string|number>([['height', '0px'], ['offset', 1]]),
]);
});
it('should treat * styles as ! for queried items that are collected in a container that is being removed',
() => {
@Component({
selector: 'my-app',
styles: [`
.list .outer {
overflow:hidden;
}
.list .inner {
box-sizing: border-box;
height: 50px;
}
`],
template: `
<button (click)="empty()">Empty</button>
<button (click)="middle()">Middle</button>
<button (click)="full()">Full</button>
<hr />
<div [@list]="exp" class="list">
<div *ngFor="let item of items" class="outer">
<div class="inner">
{{ item }}
</div>
</div>
</div>
`,
animations: [
trigger('list', [
transition(':enter', []),
transition('* => empty', [
query(':leave', [
animate(500, style({height: '0px'}))
])
]),
transition('* => full', [
query(':enter', [
style({height: '0px'}),
animate(500, style({height: '*'}))
])
]),
])
]
})
class Cmp {
items: any[] = [];
get exp() {
return this.items.length ? 'full' : 'empty';
}
empty() {
this.items = [];
}
full() {
this.items = [0, 1, 2, 3, 4];
}
}
TestBed.configureTestingModule({declarations: [Cmp]});
const engine = TestBed.inject(ɵAnimationEngine);
const fixture = TestBed.createComponent(Cmp);
const cmp = fixture.componentInstance;
cmp.empty();
fixture.detectChanges();
let player = engine.players[0]! as TransitionAnimationPlayer;
player.finish();
cmp.full();
fixture.detectChanges();
player = engine.players[0]! as TransitionAnimationPlayer;
let queriedPlayers =
((player as TransitionAnimationPlayer).getRealPlayer() as AnimationGroupPlayer).players;
expect(queriedPlayers.length).toEqual(5);
let i = 0;
for (i = 0; i < queriedPlayers.length; i++) {
let player = queriedPlayers[i] as ɵWebAnimationsPlayer;
expect(player.keyframes).toEqual([
new Map<string, string|number>([['height', '0px'], ['offset', 0]]),
new Map<string, string|number>([['height', '50px'], ['offset', 1]]),
]);
player.finish();
}
cmp.empty();
fixture.detectChanges();
player = engine.players[0]! as TransitionAnimationPlayer;
queriedPlayers =
((player as TransitionAnimationPlayer).getRealPlayer() as AnimationGroupPlayer).players;
expect(queriedPlayers.length).toEqual(5);
for (i = 0; i < queriedPlayers.length; i++) {
let player = queriedPlayers[i] as ɵWebAnimationsPlayer;
expect(player.keyframes).toEqual([
new Map<string, string|number>([['height', '50px'], ['offset', 0]]),
new Map<string, string|number>([['height', '0px'], ['offset', 1]]),
]);
}
});
it('should compute intermediate styles properly when an animation is cancelled', () => {
@Component({
selector: 'ani-cmp',
template: `
<div [@myAnimation]="exp" style="background-color: blue;">...</div>
`,
animations: [
trigger(
'myAnimation',
[
transition(
'* => a',
[
style({width: 0, height: 0}),
animate('1s', style({width: '300px', height: '600px'})),
]),
transition('* => b', [animate('1s', style({opacity: 0}))]),
]),
]
})
class Cmp {
// TODO(issue/24571): remove '!'.
public exp!: string;
}
TestBed.configureTestingModule({declarations: [Cmp]});
const engine = TestBed.inject(ɵAnimationEngine);
const fixture = TestBed.createComponent(Cmp);
const cmp = fixture.componentInstance;
cmp.exp = 'a';
fixture.detectChanges();
const player1 = engine.players[0]!;
const webPlayer1 =
(player1 as TransitionAnimationPlayer).getRealPlayer() as ɵWebAnimationsPlayer;
webPlayer1.setPosition(0.5);
cmp.exp = 'b';
fixture.detectChanges();
const player2 = engine.players[0]!;
const webPlayer2 =
(player2 as TransitionAnimationPlayer).getRealPlayer() as ɵWebAnimationsPlayer;
expect(approximate(parseFloat(webPlayer2.keyframes[0].get('width') as string), 150))
.toBeLessThan(0.05);
expect(approximate(parseFloat(webPlayer2.keyframes[0].get('height') as string), 300))
.toBeLessThan(0.05);
});
it('should compute intermediate styles properly for multiple queried elements when an animation is cancelled',
() => {
@Component({
selector: 'ani-cmp',
template: `
<div [@myAnimation]="exp">
<div *ngFor="let item of items" class="target"></div>
</div>
`,
animations: [
trigger(
'myAnimation',
[
transition(
'* => full', [query(
'.target',
[
style({width: 0, height: 0}),
animate('1s', style({width: '500px', height: '1000px'})),
])]),
transition('* => empty', [query('.target', [animate('1s', style({opacity: 0}))])]),
]),
]
})
class Cmp {
// TODO(issue/24571): remove '!'.
public exp!: string;
public items: any[] = [];
}
TestBed.configureTestingModule({declarations: [Cmp]});
const engine = TestBed.inject(ɵAnimationEngine);
const fixture = TestBed.createComponent(Cmp);
const cmp = fixture.componentInstance;
cmp.exp = 'full';
cmp.items = [0, 1, 2, 3, 4];
fixture.detectChanges();
let player = engine.players[0]!;
let groupPlayer =
(player as TransitionAnimationPlayer).getRealPlayer() as AnimationGroupPlayer;
let players = groupPlayer.players;
expect(players.length).toEqual(5);
for (let i = 0; i < players.length; i++) {
const p = players[i] as ɵWebAnimationsPlayer;
p.setPosition(0.5);
}
cmp.exp = 'empty';
cmp.items = [];
fixture.detectChanges();
player = engine.players[0];
groupPlayer = (player as TransitionAnimationPlayer).getRealPlayer() as AnimationGroupPlayer;
players = groupPlayer.players;
expect(players.length).toEqual(5);
for (let i = 0; i < players.length; i++) {
const p = players[i] as ɵWebAnimationsPlayer;
expect(approximate(parseFloat(p.keyframes[0].get('width') as string), 250))
.toBeLessThan(0.05);
expect(approximate(parseFloat(p.keyframes[0].get('height') as string), 500))
.toBeLessThan(0.05);
}
});
it('should apply the `display` and `position` styles as regular inline styles for the duration of the animation',
() => {
@Component({
selector: 'ani-cmp',
template: `
<div #elm [@myAnimation]="myAnimationExp" style="display:table; position:fixed"></div>
`,
animations: [
trigger(
'myAnimation',
[
state('go', style({display: 'inline-block'})),
transition(
'* => go',
[
style({display: 'inline', position: 'absolute', opacity: 0}),
animate('1s', style({display: 'inline', opacity: 1, position: 'static'})),
animate('1s', style({display: 'flexbox', opacity: 0})),
])
]),
]
})
class Cmp {
@ViewChild('elm', {static: true}) public element: any;
public myAnimationExp = '';
}
TestBed.configureTestingModule({declarations: [Cmp]});
const engine = TestBed.inject(ɵAnimationEngine);
const fixture = TestBed.createComponent(Cmp);
const cmp = fixture.componentInstance;
const elm = cmp.element.nativeElement;
expect(elm.style.getPropertyValue('display')).toEqual('table');
expect(elm.style.getPropertyValue('position')).toEqual('fixed');
cmp.myAnimationExp = 'go';
fixture.detectChanges();
expect(elm.style.getPropertyValue('display')).toEqual('inline');
expect(elm.style.getPropertyValue('position')).toEqual('absolute');
const player = engine.players.pop()!;
player.finish();
player.destroy();
expect(elm.style.getPropertyValue('display')).toEqual('inline-block');
expect(elm.style.getPropertyValue('position')).toEqual('fixed');
});
it('should set normalized style property values on animation end', () => {
@Component({
selector: 'ani-cmp',
template: `
<div #elm [@myAnimation]="myAnimationExp" style="width: 100%; font-size: 2rem"></div>
`,
animations: [
trigger(
'myAnimation',
[
state('go', style({width: 300, 'font-size': 14})),
transition('* => go', [animate('1s')])
]),
]
})
class Cmp {
@ViewChild('elm', {static: true}) public element: any;
public myAnimationExp = '';
}
TestBed.configureTestingModule({declarations: [Cmp]});
const engine = TestBed.inject(ɵAnimationEngine);
const fixture = TestBed.createComponent(Cmp);
const cmp = fixture.componentInstance;
const elm = cmp.element.nativeElement;
expect(elm.style.getPropertyValue('width')).toEqual('100%');
expect(elm.style.getPropertyValue('font-size')).toEqual('2rem');
cmp.myAnimationExp = 'go';
fixture.detectChanges();
const player = engine.players.pop()!;
player.finish();
player.destroy();
expect(elm.style.getPropertyValue('width')).toEqual('300px');
expect(elm.style.getPropertyValue('font-size')).toEqual('14px');
});
it('should apply correct state transitions for both CamelCase and kebab-case CSS properties',
() => {
@Component({
selector: 'ani-cmp',
template: `
<div id="camelCaseDiv" [@camelCaseTrigger]="status"></div>
<div id="kebab-case-div" [@kebab-case-trigger]="status"></div>
`,
animations: [
trigger(
'camelCaseTrigger',
[
state('active', style({
'backgroundColor': 'green',
})),
transition(
'inactive => active',
[
style({
'backgroundColor': 'red',
}),
animate(500),
]),
]),
trigger(
'kebab-case-trigger',
[
state('active', style({
'background-color': 'green',
})),
transition(
'inactive => active',
[
style({
'background-color': 'red',
}),
animate(500),
]),
]),
]
})
class Cmp {
public status: 'active'|'inactive' = 'inactive';
}
TestBed.configureTestingModule({declarations: [Cmp]});
const engine = TestBed.inject(ɵAnimationEngine);
const fixture = TestBed.createComponent(Cmp);
const cmp = fixture.componentInstance;
fixture.detectChanges();
cmp.status = 'active';
fixture.detectChanges();
engine.flush();
expect(engine.players.length).toEqual(2);
const [camelCaseWebPlayer, kebabCaseWebPlayer] = engine.players.map(
player => (player as TransitionAnimationPlayer).getRealPlayer() as ɵWebAnimationsPlayer);
[camelCaseWebPlayer, kebabCaseWebPlayer].forEach(webPlayer => {
expect(webPlayer.keyframes).toEqual([
new Map<string, string|number>([['backgroundColor', 'red'], ['offset', 0]]),
new Map<string, string|number>([['backgroundColor', 'green'], ['offset', 1]])
]);
});
});
});
})();
function approximate(value: number, target: number) {
return Math.abs(target - value) / value;
}