|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | + |
| 3 | +/** |
| 4 | + * @file contextMenuPressCurve.test.ts |
| 5 | + * @input vitest, the context-menu constants, the press feedback curves, an |
| 6 | + * independent RK4 integration, and per-frame presentation sizes of UIKit's |
| 7 | + * press platter measured on iOS 26.2 |
| 8 | + * @output The press feedback against UIKit: the size UIKit asks for, the |
| 9 | + * closed-form curve against a numerical integration of the same spring, |
| 10 | + * continuity where the ramp changes, and the visible size of a long press |
| 11 | + * and of a press lifted at 0.25 s against the measured frames |
| 12 | + * @position Unit tests of packages/core/src/ContextMenu/contextMenuPressCurve.ts |
| 13 | + */ |
| 14 | + |
| 15 | +import {afterEach, describe, expect, it, vi} from 'vitest'; |
| 16 | +import {solveSpring, toSpring} from '../motion/spring'; |
| 17 | +import {contextMenu, registerContextMenuTable} from '../motion/uikit'; |
| 18 | +import { |
| 19 | + PRESS_FEEDBACK_START_MS, |
| 20 | + pressFeedbackCurve, |
| 21 | + pressFeedbackMotion, |
| 22 | + pressFeedbackTarget, |
| 23 | +} from './contextMenuPressCurve'; |
| 24 | +import { |
| 25 | + UIKIT_EARLY_LIFT, |
| 26 | + UIKIT_EARLY_LIFT_AT, |
| 27 | + UIKIT_LONG_PRESS, |
| 28 | + UIKIT_PRESS_VIEW_WIDTH, |
| 29 | +} from './__tests__/uikitPressFrames'; |
| 30 | + |
| 31 | +const press = contextMenu.press; |
| 32 | + |
| 33 | +/** The visible scale of a held press `t` ms after the touch. */ |
| 34 | +function held(t: number): number { |
| 35 | + const curve = pressFeedbackCurve(); |
| 36 | + return t <= PRESS_FEEDBACK_START_MS |
| 37 | + ? 1 |
| 38 | + : curve.at((t - PRESS_FEEDBACK_START_MS) / 1000); |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * The visible scale of a press lifted at `lift` ms: the held curve until the |
| 43 | + * screen shows the lift, then the feedback spring back to 1 from where the |
| 44 | + * curve was (what the animator plays on `fail`). |
| 45 | + */ |
| 46 | +function liftedAt(lift: number): (t: number) => number { |
| 47 | + const curve = pressFeedbackCurve(); |
| 48 | + const turn = lift + press.returnLatencyMs; |
| 49 | + const s = (turn - PRESS_FEEDBACK_START_MS) / 1000; |
| 50 | + const back = solveSpring(toSpring(press.feedbackSpring), { |
| 51 | + from: curve.at(s), |
| 52 | + to: 1, |
| 53 | + velocity: curve.velocity(s), |
| 54 | + }); |
| 55 | + return t => (t < turn ? held(t) : back.value((t - turn) / 1000)); |
| 56 | +} |
| 57 | + |
| 58 | +function errors( |
| 59 | + measured: ReadonlyArray<readonly [number, number]>, |
| 60 | + model: (t: number) => number, |
| 61 | +): {rms: number; max: number} { |
| 62 | + const points = measured.map( |
| 63 | + ([t, value]) => Math.abs(model(t) - value) * UIKIT_PRESS_VIEW_WIDTH, |
| 64 | + ); |
| 65 | + return { |
| 66 | + rms: Math.sqrt(points.reduce((sum, e) => sum + e * e, 0) / points.length), |
| 67 | + max: Math.max(...points), |
| 68 | + }; |
| 69 | +} |
| 70 | + |
| 71 | +describe('pressFeedbackTarget', () => { |
| 72 | + it('is 1 until the onset, then ramps to 1.04 at the commit', () => { |
| 73 | + expect(pressFeedbackTarget(0)).toBe(1); |
| 74 | + expect(pressFeedbackTarget(press.feedbackOnsetMs)).toBe(1); |
| 75 | + expect(pressFeedbackTarget(press.commitMs)).toBeCloseTo(1.04, 10); |
| 76 | + }); |
| 77 | + |
| 78 | + it('continues on the slower ramp from the same size, and stops when the menu displays', () => { |
| 79 | + expect(pressFeedbackTarget(press.commitMs + 1e-9)).toBeCloseTo(1.04, 8); |
| 80 | + expect(pressFeedbackTarget(press.menuDisplayMs)).toBeCloseTo( |
| 81 | + 1 + (press.feedbackRateAfterCommit * press.menuDisplayMs) / 1000, |
| 82 | + 10, |
| 83 | + ); |
| 84 | + expect(pressFeedbackTarget(5000)).toBe( |
| 85 | + pressFeedbackTarget(press.menuDisplayMs), |
| 86 | + ); |
| 87 | + }); |
| 88 | +}); |
| 89 | + |
| 90 | +describe('pressFeedbackCurve', () => { |
| 91 | + it('matches a numerical integration of the feedback spring chasing the ramp', () => { |
| 92 | + const {stiffness, damping, mass} = toSpring(press.feedbackSpring); |
| 93 | + const curve = pressFeedbackCurve(); |
| 94 | + const target = (s: number) => |
| 95 | + pressFeedbackTarget(press.feedbackOnsetMs + s * 1000); |
| 96 | + let x = 1; |
| 97 | + let v = 0; |
| 98 | + const h = 1e-4; |
| 99 | + let worst = 0; |
| 100 | + for (let step = 0; step * h < curve.duration - h; step++) { |
| 101 | + const s = step * h; |
| 102 | + if (step % 50 === 0) { |
| 103 | + worst = Math.max(worst, Math.abs(curve.at(s) - x)); |
| 104 | + } |
| 105 | + const f = (px: number, pv: number, ps: number): [number, number] => [ |
| 106 | + pv, |
| 107 | + (stiffness * (target(ps) - px) - damping * pv) / mass, |
| 108 | + ]; |
| 109 | + const [a1, b1] = f(x, v, s); |
| 110 | + const [a2, b2] = f(x + (h / 2) * a1, v + (h / 2) * b1, s + h / 2); |
| 111 | + const [a3, b3] = f(x + (h / 2) * a2, v + (h / 2) * b2, s + h / 2); |
| 112 | + const [a4, b4] = f(x + h * a3, v + h * b3, s + h); |
| 113 | + x += (h / 6) * (a1 + 2 * a2 + 2 * a3 + a4); |
| 114 | + v += (h / 6) * (b1 + 2 * b2 + 2 * b3 + b4); |
| 115 | + } |
| 116 | + expect(worst).toBeLessThan(1e-6); |
| 117 | + }); |
| 118 | + |
| 119 | + it('is continuous in value and velocity where the ramp changes', () => { |
| 120 | + const curve = pressFeedbackCurve(); |
| 121 | + for (const ms of [press.commitMs, press.menuDisplayMs]) { |
| 122 | + const s = (ms - press.feedbackOnsetMs) / 1000; |
| 123 | + expect(curve.at(s + 1e-7)).toBeCloseTo(curve.at(s - 1e-7), 6); |
| 124 | + expect(curve.velocity(s + 1e-7)).toBeCloseTo(curve.velocity(s - 1e-7), 4); |
| 125 | + } |
| 126 | + }); |
| 127 | + |
| 128 | + it('starts from rest at 1 and rests on the last size', () => { |
| 129 | + const curve = pressFeedbackCurve(); |
| 130 | + expect(curve.at(0)).toBe(1); |
| 131 | + expect(curve.velocity(0)).toBe(0); |
| 132 | + expect(curve.at(curve.duration)).toBeCloseTo( |
| 133 | + pressFeedbackTarget(press.menuDisplayMs), |
| 134 | + 3, |
| 135 | + ); |
| 136 | + expect(Math.abs(curve.velocity(curve.duration))).toBeLessThan(1e-3); |
| 137 | + }); |
| 138 | + |
| 139 | + it('continues from a press still moving', () => { |
| 140 | + const curve = pressFeedbackCurve({from: 1.02, velocity: -0.05}); |
| 141 | + expect(curve.at(0)).toBeCloseTo(1.02, 10); |
| 142 | + expect(curve.velocity(0)).toBeCloseTo(-0.05, 10); |
| 143 | + }); |
| 144 | +}); |
| 145 | + |
| 146 | +describe('the press feedback against UIKit (iOS 26.2, measured frames)', () => { |
| 147 | + it('a long press: the visible size follows UIKit’s within 0.05 pt rms on a 200 pt view', () => { |
| 148 | + const {rms, max} = errors(UIKIT_LONG_PRESS, held); |
| 149 | + expect(rms).toBeLessThan(0.05); |
| 150 | + expect(max).toBeLessThan(0.12); |
| 151 | + }); |
| 152 | + |
| 153 | + it('a press lifted at 0.25 s: it overshoots on the way back as UIKit’s does, within 0.05 pt rms', () => { |
| 154 | + const {rms, max} = errors(UIKIT_EARLY_LIFT, liftedAt(UIKIT_EARLY_LIFT_AT)); |
| 155 | + expect(rms).toBeLessThan(0.05); |
| 156 | + expect(max).toBeLessThan(0.12); |
| 157 | + // The measured peak after the lift: 1.00504 at 332.8 ms. |
| 158 | + expect(liftedAt(UIKIT_EARLY_LIFT_AT)(332.8)).toBeCloseTo(1.00504, 4); |
| 159 | + }); |
| 160 | +}); |
| 161 | + |
| 162 | +describe('pressFeedbackMotion: the feedback is a motion role a theme retunes', () => { |
| 163 | + // The touch code registers the table before its first motion. |
| 164 | + registerContextMenuTable(); |
| 165 | + |
| 166 | + afterEach(() => { |
| 167 | + vi.restoreAllMocks(); |
| 168 | + document.body.innerHTML = ''; |
| 169 | + }); |
| 170 | + |
| 171 | + function themed(values: Record<string, string>) { |
| 172 | + vi.spyOn(window, 'getComputedStyle').mockImplementation( |
| 173 | + (element: Element) => |
| 174 | + ({ |
| 175 | + getPropertyValue: (name: string) => |
| 176 | + element.hasAttribute('data-astryx-theme') |
| 177 | + ? (values[name] ?? '') |
| 178 | + : '', |
| 179 | + }) as CSSStyleDeclaration, |
| 180 | + ); |
| 181 | + const scope = document.createElement('div'); |
| 182 | + scope.setAttribute('data-astryx-theme', 'test'); |
| 183 | + const child = document.createElement('div'); |
| 184 | + scope.append(child); |
| 185 | + document.body.append(scope); |
| 186 | + return child; |
| 187 | + } |
| 188 | + |
| 189 | + it('reads the table’s spring, onset and latency where no theme sets them', () => { |
| 190 | + const element = themed({}); |
| 191 | + expect(pressFeedbackMotion(element)).toEqual({ |
| 192 | + spring: contextMenu.press.feedbackSpring, |
| 193 | + onsetMs: contextMenu.press.feedbackOnsetMs, |
| 194 | + latencyMs: contextMenu.press.feedbackLatencyMs, |
| 195 | + }); |
| 196 | + }); |
| 197 | + |
| 198 | + it('takes a theme’s spring and latency; the onset, the commit and the menu’s moment are not roles', () => { |
| 199 | + const element = themed({ |
| 200 | + '--motion-context-menu-press-feedback-spring': '0.5s 0', |
| 201 | + '--motion-context-menu-press-feedback-latency-ms': '80ms', |
| 202 | + '--motion-context-menu-press-feedback-onset-ms': '300ms', |
| 203 | + '--motion-context-menu-press-commit-ms': '100ms', |
| 204 | + }); |
| 205 | + const motion = pressFeedbackMotion(element); |
| 206 | + expect(motion).toEqual({ |
| 207 | + spring: {duration: 0.5, bounce: 0}, |
| 208 | + onsetMs: contextMenu.press.feedbackOnsetMs, |
| 209 | + latencyMs: 80, |
| 210 | + }); |
| 211 | + const curve = pressFeedbackCurve({motion}); |
| 212 | + expect(curve.at(0)).toBe(1); |
| 213 | + expect(curve.at(0.3)).toBeGreaterThan(1); |
| 214 | + }); |
| 215 | + |
| 216 | + it('starts its ramp at the onset it is given and reaches the commit continuously', () => { |
| 217 | + // Nothing until the onset; the ramp then reaches UIKit's commit |
| 218 | + // continuously. |
| 219 | + expect(pressFeedbackTarget(250, 300)).toBe(1); |
| 220 | + expect(pressFeedbackTarget(400, 300)).toBeCloseTo(1.016, 6); |
| 221 | + expect(pressFeedbackTarget(400.001, 300)).toBeCloseTo(1.016, 4); |
| 222 | + }); |
| 223 | +}); |
0 commit comments