|
| 1 | +import { test as base } from "rollwright"; |
| 2 | +import { expect } from "@playwright/test"; |
| 3 | + |
| 4 | +let test = base.extend({ |
| 5 | + advance: async ({ execute }, use) => { |
| 6 | + let advance = await execute(() => { |
| 7 | + let timestamp = 0; |
| 8 | + window.performance = { now: () => timestamp }; |
| 9 | + |
| 10 | + let callbacks = {}; |
| 11 | + let id = 0; |
| 12 | + window.requestAnimationFrame = (cb) => ((callbacks[id++] = cb), id); |
| 13 | + window.cancelAnimationFrame = (id) => delete callbacks[id]; |
| 14 | + |
| 15 | + function step(ms) { |
| 16 | + let cbs = callbacks; |
| 17 | + callbacks = {}; |
| 18 | + timestamp += ms; |
| 19 | + for (let k in cbs) cbs[k](timestamp); |
| 20 | + } |
| 21 | + |
| 22 | + return (steps, ms) => { |
| 23 | + for (let i = 0; i < steps; i++) step(ms); |
| 24 | + }; |
| 25 | + }); |
| 26 | + |
| 27 | + await use((steps, ms) => |
| 28 | + execute((advance, steps, ms) => advance(steps, ms), advance, steps, ms), |
| 29 | + ); |
| 30 | + }, |
| 31 | +}); |
| 32 | + |
| 33 | +test("interpolation with ideal timer condition", async ({ execute, advance }) => { |
| 34 | + let snapshots = await execute(async () => { |
| 35 | + let { motion } = await import("./motion.js"); |
| 36 | + let { spring } = await import("./spring.js"); |
| 37 | + let snapshots = []; |
| 38 | + let ctl = new AbortController(); |
| 39 | + motion(spring(0, 10), (x) => snapshots.push(x), { signal: ctl.signal }); |
| 40 | + return snapshots; |
| 41 | + }); |
| 42 | + |
| 43 | + await advance(1, 0); |
| 44 | + await advance(5, 1000 / 60); |
| 45 | + |
| 46 | + expect(await snapshots.jsonValue()).toEqual([ |
| 47 | + 0, 0.47241496285986795, 1.1901835641718526, 2.0130615470443205, 2.8566207208531145, |
| 48 | + 3.6720468997166984, |
| 49 | + ]); |
| 50 | +}); |
| 51 | + |
| 52 | +test("eventual interpolation completeness", async ({ execute, advance }) => { |
| 53 | + let snapshots = await execute(async () => { |
| 54 | + let { motion } = await import("./motion.js"); |
| 55 | + let { spring } = await import("./spring.js"); |
| 56 | + let snapshots = []; |
| 57 | + let ctl = new AbortController(); |
| 58 | + motion(spring(0, 10), (x) => snapshots.push(x), { signal: ctl.signal }); |
| 59 | + return snapshots; |
| 60 | + }); |
| 61 | + |
| 62 | + await advance(1, 0); |
| 63 | + await advance(69, 1000 / 60); |
| 64 | + |
| 65 | + expect((await snapshots.jsonValue()).at(-1)).toBe(10); |
| 66 | +}); |
| 67 | + |
| 68 | +test("interpolation with inconsistent timer condition", async ({ execute, advance }) => { |
| 69 | + let snapshots = await execute(async () => { |
| 70 | + let { motion } = await import("./motion.js"); |
| 71 | + let { spring } = await import("./spring.js"); |
| 72 | + let snapshots = []; |
| 73 | + let ctl = new AbortController(); |
| 74 | + motion(spring(0, 10), (x) => snapshots.push(x), { signal: ctl.signal }); |
| 75 | + return snapshots; |
| 76 | + }); |
| 77 | + |
| 78 | + await advance(1, 0); |
| 79 | + expect(await snapshots.jsonValue()).toEqual([0]); |
| 80 | + |
| 81 | + await advance(1, (1000 / 60) * 11); |
| 82 | + expect(await snapshots.jsonValue()).toEqual([0]); |
| 83 | + |
| 84 | + await advance(3, 0.1); |
| 85 | + expect(await snapshots.jsonValue()).toEqual([ |
| 86 | + 0, 0.0028344897771590463, 0.0056689795543180925, 0.008503469331477139, |
| 87 | + ]); |
| 88 | + |
| 89 | + await advance(10, 999); |
| 90 | + expect(await snapshots.jsonValue()).toEqual([ |
| 91 | + 0, 0.0028344897771590463, 0.0056689795543180925, 0.008503469331477139, |
| 92 | + ]); |
| 93 | + |
| 94 | + await advance(2, 36); |
| 95 | + expect(await snapshots.jsonValue()).toEqual([ |
| 96 | + 0, 0.0028344897771590463, 0.0056689795543180925, 0.008503469331477139, 1.3218440414314472, |
| 97 | + 3.117557098089461, |
| 98 | + ]); |
| 99 | +}); |
| 100 | + |
| 101 | +test("explicit motion cancellation", async ({ execute, advance }) => { |
| 102 | + let ctl = await execute(() => new AbortController()); |
| 103 | + |
| 104 | + let snapshots = await execute(async (ctl) => { |
| 105 | + let { motion } = await import("./motion.js"); |
| 106 | + let { spring } = await import("./spring.js"); |
| 107 | + let snapshots = []; |
| 108 | + motion(spring(-10, -100), (x) => snapshots.push(x), { signal: ctl.signal }); |
| 109 | + return snapshots; |
| 110 | + }, ctl); |
| 111 | + |
| 112 | + await advance(1, 0); |
| 113 | + await advance(3, 1000 / 60); |
| 114 | + |
| 115 | + await execute((ctl) => ctl.abort(), ctl); |
| 116 | + await advance(2, 1000 / 60); |
| 117 | + |
| 118 | + expect(await snapshots.jsonValue()).toEqual([ |
| 119 | + -10, -14.251734665738812, -20.711652077546674, -28.117553923398887, |
| 120 | + ]); |
| 121 | +}); |
0 commit comments