|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { createWaapiAdapter } from "./waapi"; |
| 3 | + |
| 4 | +class TestAnimation { |
| 5 | + constructor(public currentTime = 0) {} |
| 6 | + playState = "running"; |
| 7 | + playbackRate = 1; |
| 8 | + timeline: object | null = timeline; |
| 9 | + addEventListener = vi.fn(); |
| 10 | + pause = vi.fn(() => { |
| 11 | + this.playState = "paused"; |
| 12 | + }); |
| 13 | +} |
| 14 | +class TestCssAnimation extends TestAnimation {} |
| 15 | +class TestCssTransition extends TestAnimation {} |
| 16 | +const timeline = {}; |
| 17 | + |
| 18 | +describe("initial WAAPI baseline", () => { |
| 19 | + let items: TestAnimation[]; |
| 20 | + const create = (time = 300) => { |
| 21 | + const animation = new TestAnimation(time); |
| 22 | + items.push(animation); |
| 23 | + return animation; |
| 24 | + }; |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + items = []; |
| 28 | + vi.stubGlobal("document", { timeline, getAnimations: () => items }); |
| 29 | + vi.stubGlobal("CSSAnimation", TestCssAnimation); |
| 30 | + vi.stubGlobal("CSSTransition", TestCssTransition); |
| 31 | + vi.stubGlobal("Element", undefined); |
| 32 | + }); |
| 33 | + afterEach(() => vi.unstubAllGlobals()); |
| 34 | + |
| 35 | + it.each([0, 16, 90, 300, 1500])( |
| 36 | + "does not retain %d ms of initial wall-clock progress", |
| 37 | + (time) => { |
| 38 | + const animation = create(time); |
| 39 | + const adapter = createWaapiAdapter(); |
| 40 | + adapter.discover(); |
| 41 | + for (const t of [0, 2, 3.5, 2]) { |
| 42 | + adapter.seek({ time: t }); |
| 43 | + expect(animation.currentTime).toBe(t * 1000); |
| 44 | + expect(animation.playState).toBe("paused"); |
| 45 | + } |
| 46 | + }, |
| 47 | + ); |
| 48 | + |
| 49 | + it.each(["seek", "pause"])("also anchors when %s is called before discover", (method) => { |
| 50 | + const animation = create(); |
| 51 | + const adapter = createWaapiAdapter(); |
| 52 | + if (method === "pause") adapter.pause(); |
| 53 | + adapter.seek({ time: 2 }); |
| 54 | + expect(animation.currentTime).toBe(2000); |
| 55 | + }); |
| 56 | + |
| 57 | + it("rewinds an animation that finished before initial discovery", () => { |
| 58 | + const animation = create(80); |
| 59 | + animation.playState = "finished"; |
| 60 | + const adapter = createWaapiAdapter(); |
| 61 | + adapter.discover(); |
| 62 | + adapter.seek({ time: 0.04 }); |
| 63 | + expect(animation.currentTime).toBe(40); |
| 64 | + }); |
| 65 | + |
| 66 | + it("does not reinterpret an idle animation", () => { |
| 67 | + const animation = create(500); |
| 68 | + animation.playState = "idle"; |
| 69 | + const adapter = createWaapiAdapter(); |
| 70 | + adapter.discover(); |
| 71 | + adapter.seek({ time: 2 }); |
| 72 | + expect(animation.currentTime).toBe(2500); |
| 73 | + }); |
| 74 | + |
| 75 | + it("keeps the captured baseline across repeated discovery at composition zero", () => { |
| 76 | + const animation = create(300); |
| 77 | + const adapter = createWaapiAdapter(); |
| 78 | + adapter.discover(); |
| 79 | + animation.currentTime = 900; |
| 80 | + adapter.discover(); |
| 81 | + adapter.seek({ time: 2 }); |
| 82 | + expect(animation.currentTime).toBe(2000); |
| 83 | + }); |
| 84 | + |
| 85 | + it.each([0, 500])("preserves a paused authored offset of %d ms", (time) => { |
| 86 | + const animation = create(time); |
| 87 | + animation.playState = "paused"; |
| 88 | + const adapter = createWaapiAdapter(); |
| 89 | + adapter.discover(); |
| 90 | + adapter.seek({ time: 2 }); |
| 91 | + expect(animation.currentTime).toBe(2000 + time); |
| 92 | + }); |
| 93 | + |
| 94 | + it.each([TestCssAnimation, TestCssTransition])("leaves $name baselines unchanged", (Type) => { |
| 95 | + const animation = new Type(500); |
| 96 | + items.push(animation); |
| 97 | + const adapter = createWaapiAdapter(); |
| 98 | + adapter.discover(); |
| 99 | + adapter.seek({ time: 2 }); |
| 100 | + expect(animation.currentTime).toBe(2500); |
| 101 | + }); |
| 102 | + |
| 103 | + it.each([null, {}])("leaves an alternate or absent timeline unchanged", (otherTimeline) => { |
| 104 | + const animation = create(500); |
| 105 | + animation.timeline = otherTimeline; |
| 106 | + const adapter = createWaapiAdapter(); |
| 107 | + adapter.discover(); |
| 108 | + adapter.seek({ time: 2 }); |
| 109 | + expect(animation.currentTime).toBe(2500); |
| 110 | + }); |
| 111 | + |
| 112 | + it.each([-1, 0, 2])("does not reinterpret a playback rate of %d", (rate) => { |
| 113 | + const animation = create(500); |
| 114 | + animation.playbackRate = rate; |
| 115 | + const adapter = createWaapiAdapter(); |
| 116 | + adapter.discover(); |
| 117 | + adapter.seek({ time: 2 }); |
| 118 | + expect(animation.currentTime).toBe(2500); |
| 119 | + }); |
| 120 | + |
| 121 | + it("preserves late discovery and rediscovery relative to composition time", () => { |
| 122 | + const adapter = createWaapiAdapter(); |
| 123 | + create(100); |
| 124 | + adapter.discover(); |
| 125 | + adapter.seek({ time: 1 }); |
| 126 | + const late = create(0); |
| 127 | + adapter.seek({ time: 1.2 }); |
| 128 | + expect(late.currentTime).toBe(0); |
| 129 | + adapter.discover(); |
| 130 | + adapter.seek({ time: 2 }); |
| 131 | + expect(late.currentTime).toBe(800); |
| 132 | + }); |
| 133 | + |
| 134 | + it("can capture initial animations again after revert", () => { |
| 135 | + const adapter = createWaapiAdapter(); |
| 136 | + const animation = create(); |
| 137 | + adapter.discover(); |
| 138 | + adapter.seek({ time: 2 }); |
| 139 | + adapter.revert(); |
| 140 | + animation.currentTime = 300; |
| 141 | + animation.playState = "running"; |
| 142 | + adapter.discover(); |
| 143 | + adapter.seek({ time: 2 }); |
| 144 | + expect(animation.currentTime).toBe(2000); |
| 145 | + }); |
| 146 | +}); |
0 commit comments