|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | + |
| 3 | +/** |
| 4 | + * @file wheelDrum.test.ts |
| 5 | + * @input wheelDrum.ts and wheelPhysics.ts, and a UIPickerView's cell |
| 6 | + * transforms read at rest |
| 7 | + * @output Proves the drum puts rows where UIKit's cylinder does, and that a |
| 8 | + * baked motion stays within tolerance, never sweeps a parked row across the |
| 9 | + * front, and leaves rows that never show without animations |
| 10 | + * @position Unit coverage for DateInput/wheelDrum.ts |
| 11 | + */ |
| 12 | + |
| 13 | +import {describe, expect, it} from 'vitest'; |
| 14 | +import { |
| 15 | + BAKE_TOLERANCE, |
| 16 | + PARKED_ANGLE, |
| 17 | + drumGeometry, |
| 18 | + isRowVisible, |
| 19 | + rowAngle, |
| 20 | + rowAtPoint, |
| 21 | + rowDistance, |
| 22 | + rowKeyframes, |
| 23 | + rowOrigin, |
| 24 | + rowTransform, |
| 25 | + sampleCurve, |
| 26 | + stripKeyframes, |
| 27 | + stripTranslation, |
| 28 | +} from './wheelDrum'; |
| 29 | +import {planRelease, spinCurve, type WheelLayout} from './wheelPhysics'; |
| 30 | + |
| 31 | +const MINUTES: WheelLayout = {rowHeight: 32, rowCount: 60}; |
| 32 | +const LOOPING: WheelLayout = {...MINUTES, looping: true}; |
| 33 | +const GEOMETRY = drumGeometry(32); |
| 34 | + |
| 35 | +/** Where CSS puts a row's centre: rotateX(−θ) about an origin R behind it. */ |
| 36 | +function cylinderPosition(angle: number, radius: number) { |
| 37 | + return {y: radius * Math.sin(angle), z: radius * (Math.cos(angle) - 1)}; |
| 38 | +} |
| 39 | + |
| 40 | +describe('the drum at rest', () => { |
| 41 | + it('turns rows ±1 to ±4 as UIPickerView does', () => { |
| 42 | + // UIKit's cell transforms: rotations of 18.85, 37.8, 56.7 and 75.7° |
| 43 | + // (18.92° a row; its depths fit R = 97.2, which gives 18.86°). |
| 44 | + const measured = [18.85, 37.8, 56.7, 75.7]; |
| 45 | + measured.forEach((degrees, i) => { |
| 46 | + const angle = rowAngle( |
| 47 | + rowDistance(31 + i + 1, 31 * 32, MINUTES), |
| 48 | + GEOMETRY, |
| 49 | + ); |
| 50 | + expect(Math.abs((angle * 180) / Math.PI - degrees)).toBeLessThan(0.3); |
| 51 | + const above = rowAngle( |
| 52 | + rowDistance(31 - i - 1, 31 * 32, MINUTES), |
| 53 | + GEOMETRY, |
| 54 | + ); |
| 55 | + expect(above).toBeCloseTo(-angle, 12); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + it('moves rows onto the cylinder where UIKit’s cells sit', () => { |
| 60 | + // UIKit's cells, relative to the band: depth −5.2, −20.4, −43.9, −73.2 |
| 61 | + // pt; along y 32k minus 0.41, 4.23, 14.5, 33.6 pt (at a rest offset a |
| 62 | + // fraction of a point off the row). |
| 63 | + const depth = [-5.2, -20.4, -43.9, -73.2]; |
| 64 | + const pull = [0.41, 4.23, 14.5, 33.6]; |
| 65 | + depth.forEach((z, i) => { |
| 66 | + const k = i + 1; |
| 67 | + const position = cylinderPosition( |
| 68 | + rowAngle(k * 32, GEOMETRY), |
| 69 | + GEOMETRY.radius, |
| 70 | + ); |
| 71 | + expect(Math.abs(position.z - z)).toBeLessThan(0.5); |
| 72 | + expect(Math.abs(position.y - (32 * k - pull[i]))).toBeLessThan(0.4); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + it('writes the turn as one rotation about an origin R behind the row', () => { |
| 77 | + expect(rowOrigin(GEOMETRY)).toBe('50% 50% -97.2px'); |
| 78 | + expect(rowTransform(32 / 97.2)).toBe('rotateX(-0.32922rad)'); |
| 79 | + }); |
| 80 | + |
| 81 | + it('parks rows past a quarter turn instead of turning them round', () => { |
| 82 | + const angle = rowAngle(400, GEOMETRY); |
| 83 | + expect(angle).toBe(PARKED_ANGLE); |
| 84 | + expect(isRowVisible(angle)).toBe(false); |
| 85 | + expect(isRowVisible(rowAngle(140, GEOMETRY))).toBe(true); |
| 86 | + }); |
| 87 | + |
| 88 | + it('scales the whole drum with the row height', () => { |
| 89 | + const geometry = drumGeometry(28); |
| 90 | + expect(geometry.radius).toBeCloseTo(85.05, 2); |
| 91 | + expect(geometry.height).toBe(189); |
| 92 | + expect(rowAngle(28, geometry)).toBeCloseTo(rowAngle(32, GEOMETRY), 12); |
| 93 | + }); |
| 94 | + |
| 95 | + it('shows each row of a looping wheel once, at the copy nearest the band', () => { |
| 96 | + // Minute 0 at an offset on minute 58 sits two rows below the band. |
| 97 | + expect(rowDistance(0, 58 * 32, LOOPING)).toBe(64); |
| 98 | + expect(rowDistance(58, 0, LOOPING)).toBe(-64); |
| 99 | + expect(rowDistance(30, 0, LOOPING)).toBe(-960); |
| 100 | + }); |
| 101 | +}); |
| 102 | + |
| 103 | +describe('which row is under a point', () => { |
| 104 | + it('inverts the cylinder', () => { |
| 105 | + const y = GEOMETRY.radius * Math.sin(32 / GEOMETRY.radius); |
| 106 | + expect(rowAtPoint(y, 31 * 32, MINUTES, GEOMETRY)).toBe(32); |
| 107 | + expect(rowAtPoint(-y, 31 * 32, MINUTES, GEOMETRY)).toBe(30); |
| 108 | + expect(rowAtPoint(0, 31 * 32, MINUTES, GEOMETRY)).toBe(31); |
| 109 | + }); |
| 110 | + |
| 111 | + it('finds nothing past the drum or past the rows', () => { |
| 112 | + expect( |
| 113 | + rowAtPoint(GEOMETRY.radius + 1, 31 * 32, MINUTES, GEOMETRY), |
| 114 | + ).toBeNull(); |
| 115 | + expect(rowAtPoint(-40, 0, MINUTES, GEOMETRY)).toBeNull(); |
| 116 | + expect(rowAtPoint(-40, 0, LOOPING, GEOMETRY)).toBe(59); |
| 117 | + }); |
| 118 | +}); |
| 119 | + |
| 120 | +describe('a baked motion', () => { |
| 121 | + const coast = planRelease(1110, 1016.2, MINUTES).curve; |
| 122 | + |
| 123 | + it('stays within the tolerance between samples', () => { |
| 124 | + const samples = sampleCurve(coast); |
| 125 | + expect(samples[0].t).toBe(0); |
| 126 | + expect(samples[samples.length - 1].t).toBe(coast.duration); |
| 127 | + expect(samples.length).toBeLessThan(400); |
| 128 | + for (let i = 1; i < samples.length; i++) { |
| 129 | + const a = samples[i - 1]; |
| 130 | + const b = samples[i]; |
| 131 | + for (const f of [0.1, 0.33, 0.5, 0.66, 0.9]) { |
| 132 | + const t = a.t + (b.t - a.t) * f; |
| 133 | + const linear = a.value + (b.value - a.value) * f; |
| 134 | + expect(Math.abs(coast.value(t) - linear)).toBeLessThanOrEqual( |
| 135 | + BAKE_TOLERANCE * 1.5, |
| 136 | + ); |
| 137 | + } |
| 138 | + } |
| 139 | + }); |
| 140 | + |
| 141 | + it('animates only the rows that show during the motion', () => { |
| 142 | + const samples = sampleCurve(coast); |
| 143 | + // Rows 30 to 55 show at some point between row 34.7 and row 50. |
| 144 | + expect( |
| 145 | + rowKeyframes(10, samples, coast.duration, MINUTES, GEOMETRY), |
| 146 | + ).toBeNull(); |
| 147 | + expect( |
| 148 | + rowKeyframes(59, samples, coast.duration, MINUTES, GEOMETRY), |
| 149 | + ).toBeNull(); |
| 150 | + const row40 = rowKeyframes(40, samples, coast.duration, MINUTES, GEOMETRY); |
| 151 | + expect(row40).not.toBeNull(); |
| 152 | + expect(row40![0].offset).toBe(0); |
| 153 | + expect(row40![row40!.length - 1].offset).toBe(1); |
| 154 | + // It ends parked above the band, where row 40 sits when row 50 is on it. |
| 155 | + expect(row40![row40!.length - 1].transform).toBe( |
| 156 | + rowTransform(-PARKED_ANGLE), |
| 157 | + ); |
| 158 | + const row50 = rowKeyframes(50, samples, coast.duration, MINUTES, GEOMETRY); |
| 159 | + expect(row50![row50!.length - 1].transform).toBe(rowTransform(0)); |
| 160 | + }); |
| 161 | + |
| 162 | + it('steps a looping row to its other copy while it is out of sight', () => { |
| 163 | + // From minute 58 across 0 to minute 3: minute 30 changes sides behind |
| 164 | + // the drum and must never be drawn on the front on the way. |
| 165 | + const spin = spinCurve(58 * 32, 63 * 32); |
| 166 | + const samples = sampleCurve(spin); |
| 167 | + const frames = rowKeyframes(28, samples, spin.duration, LOOPING, GEOMETRY); |
| 168 | + expect(frames).toBeNull(); |
| 169 | + const minute1 = rowKeyframes(1, samples, spin.duration, LOOPING, GEOMETRY)!; |
| 170 | + for (const frame of minute1) { |
| 171 | + expect(frame.transform).not.toContain('NaN'); |
| 172 | + } |
| 173 | + // Minute 33 is at the back the whole time: no keyframes. Minute 0 comes |
| 174 | + // into view from below and passes the band. |
| 175 | + const minute0 = rowKeyframes(0, samples, spin.duration, LOOPING, GEOMETRY)!; |
| 176 | + expect(minute0[0].transform).toBe(rowTransform(rowAngle(64, GEOMETRY))); |
| 177 | + expect(minute0[minute0.length - 1].transform).toBe( |
| 178 | + rowTransform(rowAngle(-96, GEOMETRY)), |
| 179 | + ); |
| 180 | + }); |
| 181 | + |
| 182 | + it('jumps a parked looping row with two keyframes at the same progress', () => { |
| 183 | + // A long looping spin: row 0 goes once round the back. |
| 184 | + const spin = spinCurve(0, 40 * 32); |
| 185 | + const samples = sampleCurve(spin); |
| 186 | + const frames = rowKeyframes(0, samples, spin.duration, LOOPING, GEOMETRY)!; |
| 187 | + const steps = frames.filter( |
| 188 | + (frame, i) => i > 0 && frame.offset === frames[i - 1].offset, |
| 189 | + ); |
| 190 | + expect(steps.length).toBe(1); |
| 191 | + for (let i = 1; i < frames.length; i++) { |
| 192 | + expect(frames[i].offset).toBeGreaterThanOrEqual(frames[i - 1].offset); |
| 193 | + } |
| 194 | + }); |
| 195 | + |
| 196 | + it('translates the band strip with the offset, folding a looping strip', () => { |
| 197 | + expect(stripTranslation(992, MINUTES)).toBe(-992); |
| 198 | + expect(stripTranslation(992, LOOPING)).toBe(-(992 + 1920)); |
| 199 | + expect(stripTranslation(-32, LOOPING)).toBe(-(1888 + 1920)); |
| 200 | + const spin = spinCurve(58 * 32, 63 * 32); |
| 201 | + const frames = stripKeyframes(sampleCurve(spin), spin.duration, LOOPING); |
| 202 | + const steps = frames.filter( |
| 203 | + (frame, i) => i > 0 && frame.offset === frames[i - 1].offset, |
| 204 | + ); |
| 205 | + expect(steps.length).toBe(1); |
| 206 | + }); |
| 207 | +}); |
0 commit comments