Skip to content

Commit c189419

Browse files
committed
fix(core): a CSS animation in a later clip shows the right frame after a seek
1 parent 984ad28 commit c189419

8 files changed

Lines changed: 447 additions & 62 deletions

File tree

‎packages/core/src/runtime/adapters/css.test.ts‎

Lines changed: 276 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,40 @@
1-
import { afterEach, describe, it, expect, vi } from "vitest";
1+
import { afterEach, beforeEach, describe, it, expect, vi } from "vitest";
22
import { createCssAdapter } from "./css";
33

4+
// jsdom has no Animation subclasses; the adapter tells them apart with instanceof CSSAnimation.
5+
class FakeAnimation {}
6+
class FakeCSSAnimation extends FakeAnimation {}
7+
class FakeCSSTransition extends FakeAnimation {}
8+
9+
const makeAnimation = (target: Element, kind: typeof FakeAnimation = FakeCSSAnimation) =>
10+
Object.assign(new kind(), {
11+
currentTime: 0,
12+
pause: vi.fn(),
13+
play: vi.fn(),
14+
effect: { target },
15+
}) as unknown as Animation;
16+
17+
// The browser answers the same list per element and document-wide; returns a setter to replace it.
18+
const mockLiveAnimations = (el: HTMLElement, initial: Animation[]) => {
19+
let live = initial;
20+
el.getAnimations = () => live;
21+
document.getAnimations = () => live;
22+
return (next: Animation[]) => {
23+
live = next;
24+
};
25+
};
26+
427
describe("css adapter", () => {
28+
beforeEach(() => {
29+
vi.stubGlobal("CSSAnimation", FakeCSSAnimation);
30+
vi.stubGlobal("CSSTransition", FakeCSSTransition);
31+
});
32+
33+
afterEach(() => {
34+
Reflect.deleteProperty(document, "getAnimations");
35+
vi.unstubAllGlobals();
36+
});
37+
538
it("has correct name", () => {
639
expect(createCssAdapter().name).toBe("css");
740
});
@@ -70,7 +103,7 @@ describe("css adapter", () => {
70103
vi.restoreAllMocks();
71104
});
72105

73-
it("pause restores base play state", () => {
106+
it("pause keeps the fallback pose of an element with no live animation; play restores base", () => {
74107
const el = document.createElement("div");
75108
el.style.animationName = "spin";
76109
el.style.animationPlayState = "running";
@@ -86,13 +119,31 @@ describe("css adapter", () => {
86119
adapter.seek({ time: 1 });
87120
expect(el.style.animationPlayState).toBe("paused");
88121

122+
// A render seeks, pauses, then shows the clip: the browser's new animation starts from this pose.
89123
adapter.pause();
124+
expect(el.style.animationPlayState).toBe("paused");
125+
expect(el.style.animationDelay).toBe("-1s");
126+
127+
adapter.play?.();
90128
expect(el.style.animationPlayState).toBe("running");
129+
expect(el.style.animationDelay).toBe("");
91130

92131
document.body.removeChild(el);
93132
vi.restoreAllMocks();
94133
});
95134

135+
it("does not scan document animations when no element has a CSS animation", () => {
136+
const getAnimations = vi.fn(() => []);
137+
document.getAnimations = getAnimations;
138+
139+
const adapter = createCssAdapter();
140+
adapter.discover();
141+
adapter.seek({ time: 1 });
142+
adapter.pause();
143+
144+
expect(getAnimations).not.toHaveBeenCalled();
145+
});
146+
96147
it("revert clears entries", () => {
97148
const adapter = createCssAdapter();
98149
adapter.revert!();
@@ -110,8 +161,8 @@ describe("css adapter", () => {
110161
return { animationName: "spin" } as CSSStyleDeclaration;
111162
});
112163

113-
const animation = { currentTime: 0, pause: vi.fn(), play: vi.fn() } as unknown as Animation;
114-
(el as HTMLElement & { getAnimations?: () => Animation[] }).getAnimations = () => [animation];
164+
const animation = makeAnimation(el);
165+
mockLiveAnimations(el, [animation]);
115166

116167
const adapter = createCssAdapter();
117168
adapter.discover();
@@ -126,30 +177,232 @@ describe("css adapter", () => {
126177
vi.restoreAllMocks();
127178
});
128179

129-
it("does not rescan element animations on every seek", () => {
180+
describe("after the browser replaces an element's CSSAnimation", () => {
130181
const el = document.createElement("div");
131-
el.style.animationName = "spin";
132-
document.body.appendChild(el);
182+
el.setAttribute("data-start", "1");
183+
el.style.animationName = "slide";
133184

134-
vi.spyOn(window, "getComputedStyle").mockImplementation(() => {
135-
return { animationName: "spin" } as CSSStyleDeclaration;
185+
const setup = (initial: Animation[]) => {
186+
document.body.appendChild(el);
187+
vi.spyOn(window, "getComputedStyle").mockImplementation(() => {
188+
return { animationName: "slide" } as CSSStyleDeclaration;
189+
});
190+
const replace = mockLiveAnimations(el, initial);
191+
const adapter = createCssAdapter();
192+
adapter.discover();
193+
return { adapter, replace };
194+
};
195+
196+
afterEach(() => {
197+
el.remove();
198+
el.style.removeProperty("animation-delay");
199+
el.style.removeProperty("animation-play-state");
200+
vi.restoreAllMocks();
136201
});
137202

138-
const animation = { currentTime: 0, pause: vi.fn(), play: vi.fn() } as unknown as Animation;
139-
const getAnimations = vi.fn(() => [animation]);
140-
(el as HTMLElement & { getAnimations?: () => Animation[] }).getAnimations = getAnimations;
203+
it("seek drives the live animation, not the one seen at discover", () => {
204+
const stale = makeAnimation(el);
205+
const current = makeAnimation(el);
206+
const { adapter, replace } = setup([stale]);
141207

142-
const adapter = createCssAdapter();
143-
adapter.discover();
144-
adapter.seek({ time: 1 });
145-
adapter.seek({ time: 2 });
146-
adapter.seek({ time: 3 });
208+
replace([current]);
209+
adapter.seek({ time: 3 });
147210

148-
expect(getAnimations).toHaveBeenCalledTimes(1);
149-
expect(animation.currentTime).toBe(3000);
211+
expect(current.currentTime).toBe(2000);
212+
expect(current.pause).toHaveBeenCalled();
213+
expect(stale.currentTime).toBe(0);
214+
});
150215

151-
document.body.removeChild(el);
152-
vi.restoreAllMocks();
216+
it("seeks from the pass's shared page list without a scan of its own", () => {
217+
const current = makeAnimation(el);
218+
const { adapter } = setup([]);
219+
const getAnimations = vi.spyOn(document, "getAnimations");
220+
221+
adapter.seek({ time: 3, pageAnimations: () => [current] });
222+
223+
expect(getAnimations).not.toHaveBeenCalled();
224+
expect(current.currentTime).toBe(2000);
225+
});
226+
227+
it("pause takes the seek pass's page list when given one and scans without it", () => {
228+
const current = makeAnimation(el);
229+
const { adapter } = setup([current]);
230+
const getAnimations = vi.spyOn(document, "getAnimations");
231+
232+
adapter.pause({ pageAnimations: () => [current] });
233+
expect(getAnimations).not.toHaveBeenCalled();
234+
expect(current.pause).toHaveBeenCalledTimes(1);
235+
236+
adapter.pause();
237+
expect(getAnimations).toHaveBeenCalledTimes(1);
238+
expect(current.pause).toHaveBeenCalledTimes(2);
239+
});
240+
241+
it("seeks back into a finished no-fill animation after it left the page's list", () => {
242+
const finished = makeAnimation(el);
243+
const { adapter, replace } = setup([finished]);
244+
245+
adapter.seek({ time: 1.5 });
246+
replace([]);
247+
adapter.seek({ time: 1.25 });
248+
249+
expect(finished.currentTime).toBe(250);
250+
expect(el.style.animationDelay).toBe("");
251+
});
252+
253+
it("never writes to an animation the browser cancelled, and falls back to the inline pose", () => {
254+
const cancelled = makeAnimation(el);
255+
const { adapter, replace } = setup([cancelled]);
256+
257+
adapter.seek({ time: 1.5 });
258+
replace([]);
259+
Object.assign(cancelled, { playState: "idle", currentTime: null });
260+
adapter.seek({ time: 2 });
261+
262+
expect(cancelled.currentTime).toBeNull();
263+
expect(el.style.animationDelay).toBe("-1s");
264+
});
265+
266+
it("forgets a cancelled animation instead of checking it on every seek", () => {
267+
const { adapter, replace } = setup([]);
268+
const playStateReads: Array<() => string> = [];
269+
for (let showing = 0; showing < 3; showing++) {
270+
const shown = makeAnimation(el);
271+
const readPlayState = vi.fn(() => "idle");
272+
Object.defineProperty(shown, "playState", { get: readPlayState });
273+
playStateReads.push(readPlayState);
274+
replace([shown]);
275+
adapter.seek({ time: 1.5 });
276+
replace([]);
277+
adapter.seek({ time: 2 });
278+
adapter.seek({ time: 2.5 });
279+
}
280+
281+
for (const readPlayState of playStateReads) expect(readPlayState).toHaveBeenCalledTimes(1);
282+
});
283+
284+
it("forgets every known animation on revert", () => {
285+
const finished = makeAnimation(el);
286+
const { adapter, replace } = setup([finished]);
287+
288+
adapter.seek({ time: 1.5 });
289+
adapter.revert?.();
290+
adapter.discover();
291+
replace([]);
292+
adapter.seek({ time: 1.25 });
293+
294+
expect(finished.currentTime).toBe(500);
295+
});
296+
297+
it("rediscover keeps a remembered handle for an element still in the page", () => {
298+
const finished = makeAnimation(el);
299+
const { adapter, replace } = setup([finished]);
300+
301+
adapter.seek({ time: 1.5 });
302+
replace([]);
303+
adapter.discover();
304+
adapter.seek({ time: 1.25 });
305+
306+
expect(finished.currentTime).toBe(250);
307+
expect(el.style.animationDelay).toBe("");
308+
});
309+
310+
it("rediscover forgets the handles of an element no longer in the page", () => {
311+
const finished = makeAnimation(el);
312+
const { adapter, replace } = setup([finished]);
313+
314+
adapter.seek({ time: 1.5 });
315+
replace([]);
316+
el.remove();
317+
adapter.discover();
318+
document.body.appendChild(el);
319+
adapter.discover();
320+
adapter.seek({ time: 1.25 });
321+
322+
expect(finished.currentTime).toBe(500);
323+
expect(el.style.animationDelay).toBe("-0.25s");
324+
});
325+
326+
it("leaves the element's pseudo-element animations alone, as el.getAnimations() does", () => {
327+
const own = makeAnimation(el);
328+
const before = Object.assign(makeAnimation(el), {
329+
effect: { target: el, pseudoElement: "::before" },
330+
});
331+
const { adapter } = setup([own, before]);
332+
333+
adapter.seek({ time: 3 });
334+
335+
expect(own.currentTime).toBe(2000);
336+
expect(before.currentTime).toBe(0);
337+
});
338+
339+
it("leaves CSS transitions and script animations on the element to the WAAPI adapter", () => {
340+
const own = makeAnimation(el);
341+
const others = [makeAnimation(el, FakeCSSTransition), makeAnimation(el, FakeAnimation)];
342+
const { adapter } = setup([own, ...others]);
343+
344+
adapter.seek({ time: 3 });
345+
adapter.play?.();
346+
adapter.pause();
347+
348+
expect(own.currentTime).toBe(2000);
349+
for (const other of others) {
350+
expect(other.currentTime).toBe(0);
351+
expect(other.pause).not.toHaveBeenCalled();
352+
expect(other.play).not.toHaveBeenCalled();
353+
}
354+
});
355+
356+
it("seeks every animation where the browser has no CSSAnimation to tell them apart", () => {
357+
vi.stubGlobal("CSSAnimation", undefined);
358+
const animation = makeAnimation(el, FakeAnimation);
359+
const { adapter } = setup([animation]);
360+
361+
adapter.seek({ time: 3 });
362+
363+
expect(animation.currentTime).toBe(2000);
364+
});
365+
366+
it("play and pause act on the live animation", () => {
367+
const stale = makeAnimation(el);
368+
const current = makeAnimation(el);
369+
const { adapter, replace } = setup([stale]);
370+
371+
replace([current]);
372+
adapter.play?.();
373+
adapter.pause();
374+
375+
expect(current.play).toHaveBeenCalled();
376+
expect(current.pause).toHaveBeenCalled();
377+
expect(stale.play).not.toHaveBeenCalled();
378+
expect(stale.pause).not.toHaveBeenCalled();
379+
});
380+
381+
it("drops the fallback's inline delay once a live animation exists", () => {
382+
const current = makeAnimation(el);
383+
const { adapter, replace } = setup([]);
384+
385+
adapter.seek({ time: 3 });
386+
expect(el.style.animationDelay).toBe("-2s");
387+
388+
replace([current]);
389+
adapter.seek({ time: 4 });
390+
391+
expect(el.style.animationDelay).toBe("");
392+
expect(el.style.animationPlayState).toBe("");
393+
expect(current.currentTime).toBe(3000);
394+
});
395+
396+
it("rediscover does not read a fallback seek's inline delay back as authored", () => {
397+
const { adapter } = setup([]);
398+
399+
adapter.seek({ time: 3 });
400+
adapter.discover();
401+
adapter.pause();
402+
403+
expect(el.style.animationDelay).toBe("");
404+
expect(el.style.animationPlayState).toBe("");
405+
});
153406
});
154407

155408
it("play resumes WAAPI animations and restores inline styles", () => {
@@ -162,8 +415,8 @@ describe("css adapter", () => {
162415
return { animationName: "spin" } as CSSStyleDeclaration;
163416
});
164417

165-
const animation = { currentTime: 0, pause: vi.fn(), play: vi.fn() } as unknown as Animation;
166-
(el as HTMLElement & { getAnimations?: () => Animation[] }).getAnimations = () => [animation];
418+
const animation = makeAnimation(el);
419+
mockLiveAnimations(el, [animation]);
167420

168421
const adapter = createCssAdapter();
169422
adapter.discover();

0 commit comments

Comments
 (0)