From 2c00466859c3674ca4e2389962892fb9b53309f4 Mon Sep 17 00:00:00 2001 From: Benjamin Kindle Date: Wed, 2 Dec 2020 17:17:08 -0500 Subject: [PATCH] fix(animations): implement getPosition in browser animation builder Forward `getPosition` to `animation_group_player`. --- .../src/players/animation_group_player.ts | 13 +++--- .../test/animation_group_player_spec.ts | 42 +++++++++++++++++++ .../animations/src/animation_builder.ts | 2 +- 3 files changed, 50 insertions(+), 7 deletions(-) create mode 100644 packages/animations/test/animation_group_player_spec.ts diff --git a/packages/animations/src/players/animation_group_player.ts b/packages/animations/src/players/animation_group_player.ts index a5fa06ced5ea59..d774e807256931 100644 --- a/packages/animations/src/players/animation_group_player.ts +++ b/packages/animations/src/players/animation_group_player.ts @@ -148,12 +148,13 @@ export class AnimationGroupPlayer implements AnimationPlayer { } getPosition(): number { - let min = 0; - this.players.forEach(player => { - const p = player.getPosition(); - min = Math.min(p, min); - }); - return min; + const longestPlayer = + this.players.reduce((longestSoFar: AnimationPlayer|undefined, player: AnimationPlayer) => { + const newPlayerIsLongest = + longestSoFar === undefined || player.totalTime > longestSoFar!.totalTime; + return newPlayerIsLongest ? player : longestSoFar; + }, undefined); + return longestPlayer != undefined ? longestPlayer.getPosition() / longestPlayer.totalTime : 0; } beforeDestroy(): void { diff --git a/packages/animations/test/animation_group_player_spec.ts b/packages/animations/test/animation_group_player_spec.ts new file mode 100644 index 00000000000000..856e71b415a80b --- /dev/null +++ b/packages/animations/test/animation_group_player_spec.ts @@ -0,0 +1,42 @@ +/** + * @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 {fakeAsync} from '@angular/core/testing'; +import {NoopAnimationPlayer} from '../src/animations'; +import {AnimationGroupPlayer} from '../src/players/animation_group_player'; + +{ + describe('AnimationGroupPlayer', function() { + it('should getPosition of an empty group', fakeAsync(() => { + const players: NoopAnimationPlayer[] = []; + const groupPlayer = new AnimationGroupPlayer(players); + expect(groupPlayer.getPosition()).toBe(0); + })); + + it('should getPosition of a single player in a group', fakeAsync(() => { + const player = new NoopAnimationPlayer(5, 5); + player.getPosition = () => 2; + const players = [player]; + const groupPlayer = new AnimationGroupPlayer(players); + expect(groupPlayer.getPosition()).toBe(0.2); + })); + + it('should getPosition based on the longest player in the group', fakeAsync(() => { + const longestPlayer = new NoopAnimationPlayer(5, 5); + longestPlayer.getPosition = () => 2; + const players = [ + new NoopAnimationPlayer(1, 4), + new NoopAnimationPlayer(4, 1), + new NoopAnimationPlayer(7, 0), + longestPlayer, + new NoopAnimationPlayer(1, 1), + ]; + const groupPlayer = new AnimationGroupPlayer(players); + expect(groupPlayer.getPosition()).toBe(0.2); + })); + }); +} diff --git a/packages/platform-browser/animations/src/animation_builder.ts b/packages/platform-browser/animations/src/animation_builder.ts index 2e0080104c931a..80e767c461317e 100644 --- a/packages/platform-browser/animations/src/animation_builder.ts +++ b/packages/platform-browser/animations/src/animation_builder.ts @@ -111,7 +111,7 @@ export class RendererAnimationPlayer implements AnimationPlayer { } getPosition(): number { - return 0; + return this._renderer.engine.players[+this.id].getPosition(); } public totalTime = 0;