|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | + |
| 3 | +/** |
| 4 | + * @file sheetRelease.test.ts |
| 5 | + * @input vitest, resolveSheetRelease and the UIKit constants table |
| 6 | + * @output Holds the release rule to UIKit's own releases: 47 drags of a |
| 7 | + * UISheetPresentationController `[.medium(), .large()]` sheet read from |
| 8 | + * UIKit (offset and velocity of the sheet interaction's last drag update |
| 9 | + * before the lift, the detent it settled on, the damping of the spring it |
| 10 | + * attached); plus the rule's own edges |
| 11 | + * @position Testing; pure, no DOM |
| 12 | + */ |
| 13 | + |
| 14 | +import {describe, expect, it} from 'vitest'; |
| 15 | +import {sheet, springs} from '../motion/uikit'; |
| 16 | +import {resolveSheetRelease} from './sheetRelease'; |
| 17 | + |
| 18 | +/** |
| 19 | + * Measured on the iOS 26.2 simulator (iPhone 17 Pro, 402 × 874 pt), in |
| 20 | + * UIKit's own interaction offsets: the sheet's top at the large detent is at |
| 21 | + * 62 pt, at the medium detent 404.32 pt, and gone at 874 pt. |
| 22 | + * [case, offset at the lift (pt), velocity (pt/s, + = down), outcome, ζ of |
| 23 | + * the release spring (null: a translate release, whose spring was not read)] |
| 24 | + */ |
| 25 | +const UIKIT_RELEASES: ReadonlyArray< |
| 26 | + [string, number, number, 'large' | 'medium' | 'dismissed', number | null] |
| 27 | +> = [ |
| 28 | + ['off-300.v0', 120.99, -12.5, 'large', 1], |
| 29 | + ['off-300.v500', 122.65, -481.4, 'large', 1], |
| 30 | + ['off-300.v1500', 116.65, -1495.6, 'large', 0.8], |
| 31 | + ['off-300.v3000', 129.32, -2903.5, 'large', 0.8], |
| 32 | + ['off-230.v0', 184.32, 0, 'large', 1], |
| 33 | + ['off-230.v500', 188.65, -472, 'large', 1], |
| 34 | + ['off-230.v1500', 199.65, -1590, 'large', 0.8], |
| 35 | + ['off-230.v3000', 225.65, -4037.7, 'large', 0.8], |
| 36 | + ['off-170.v0', 244.32, 0, 'medium', 1], |
| 37 | + ['off-170.v500', 250.99, -494.8, 'large', 1], |
| 38 | + ['off-170.v1500', 282.99, -2323, 'large', 0.8], |
| 39 | + ['off-170.v3000', 258.65, -3292.1, 'large', 0.8], |
| 40 | + ['off-120.v0', 294.32, 0, 'medium', 1], |
| 41 | + ['off-120.v500', 300.65, -466, 'medium', 1], |
| 42 | + ['off-120.v1500', 296.32, -2209.6, 'large', 0.8], |
| 43 | + ['off-120.v3000', 356.32, -686.1, 'medium', 1], |
| 44 | + ['off-60.v0', 354.32, 0, 'medium', 1], |
| 45 | + ['off-60.v500', 365.65, -493.5, 'medium', 1], |
| 46 | + ['off-60.v1500', 368.32, -574.9, 'medium', 1], |
| 47 | + ['off60.v0', 454.32, 0, 'medium', null], |
| 48 | + ['off60.v500', 446.99, 481.5, 'medium', null], |
| 49 | + ['off60.v1500', 428.32, 450.6, 'medium', null], |
| 50 | + ['off120.v0', 514.32, 0, 'medium', null], |
| 51 | + ['off120.v500', 503.65, 453.3, 'medium', null], |
| 52 | + ['off120.v1500', 512.32, 1969.9, 'dismissed', null], |
| 53 | + ['off120.v3000', 476.32, 1139.7, 'dismissed', null], |
| 54 | + ['off200.v0', 594.32, 0, 'medium', null], |
| 55 | + ['off200.v500', 583.65, 494.5, 'medium', null], |
| 56 | + ['off200.v1500', 579.32, 1400, 'dismissed', null], |
| 57 | + ['off200.v3000', 579.32, 3094.6, 'dismissed', null], |
| 58 | + ['off300.v0', 694.32, 0, 'dismissed', null], |
| 59 | + ['off300.v500', 687.65, 478.4, 'dismissed', null], |
| 60 | + ['off300.v1500', 666.65, 1489.2, 'dismissed', null], |
| 61 | + ['off300.v3000', 679.32, 3008.2, 'dismissed', null], |
| 62 | + ['up.end356.v900', 351.65, -926.5, 'medium', 1], |
| 63 | + ['up.end356.v1000', 352.65, -996.7, 'medium', 1], |
| 64 | + ['down.end500.v1000', 501.99, 991, 'medium', null], |
| 65 | + ['down.end500.v1200', 500.65, 1212.4, 'dismissed', null], |
| 66 | + ['up.long.v400', 277.65, -381.2, 'medium', 1], |
| 67 | + ['down.end600.v200', 590.32, 210.5, 'medium', null], |
| 68 | + ['zeta.towardLarge.v720', 160.32, -726.6, 'large', 1], |
| 69 | + ['zeta.towardLarge.v760', 160.65, -736.9, 'large', 1], |
| 70 | + ['zeta.towardLarge.v800', 167.65, -746.9, 'large', 1], |
| 71 | + ['zeta.towardLarge.v840', 161.32, -837.8, 'large', 1], |
| 72 | + ['zeta.awayFromTarget.v760', 224.32, 801.4, 'medium', 1], |
| 73 | + ['zeta.awayFromTarget.v840', 224.32, 1147.3, 'medium', 0.8], |
| 74 | + ['zeta.awayFromTarget.v1000', 208.32, 76, 'large', 1], |
| 75 | +]; |
| 76 | + |
| 77 | +const LARGE = 62; |
| 78 | +const MEDIUM = 404.32; |
| 79 | +const GONE = 874; |
| 80 | +const MEDIUM_GONE_BOUNDARY = (MEDIUM + GONE) / 2; |
| 81 | + |
| 82 | +/** |
| 83 | + * The releases the rule does not reproduce, and why. The first two land on |
| 84 | + * opposite sides of the medium / dismissed boundary less than 1 pt from it |
| 85 | + * and from each other: one needs a projection time of at least 0.1145 s, the |
| 86 | + * other at most 0.1120 s, so no single value separates them (the noise of the |
| 87 | + * velocity reading). The third's last drag update read 1140 pt/s for a |
| 88 | + * 3000 pt/s injection (its siblings read ~3000): an under-read, not a rule. |
| 89 | + */ |
| 90 | +const KNOWN_MISSES: Record<string, 'boundary' | 'under-read'> = { |
| 91 | + 'down.end500.v1200': 'boundary', |
| 92 | + 'off200.v500': 'boundary', |
| 93 | + 'off120.v3000': 'under-read', |
| 94 | +}; |
| 95 | + |
| 96 | +describe('resolveSheetRelease against UIKit', () => { |
| 97 | + for (const [name, offset, velocity, outcome, zeta] of UIKIT_RELEASES) { |
| 98 | + it(`${name}: ${velocity} pt/s from ${offset} pt → ${outcome}`, () => { |
| 99 | + const release = resolveSheetRelease({ |
| 100 | + detents: [LARGE, MEDIUM], |
| 101 | + dismissedOffset: GONE, |
| 102 | + position: offset, |
| 103 | + velocity, |
| 104 | + }); |
| 105 | + const landed = |
| 106 | + release.kind === 'dismiss' |
| 107 | + ? 'dismissed' |
| 108 | + : release.index === 0 |
| 109 | + ? 'large' |
| 110 | + : 'medium'; |
| 111 | + const miss = KNOWN_MISSES[name]; |
| 112 | + if (miss === 'boundary') { |
| 113 | + expect(landed).not.toBe(outcome); |
| 114 | + expect(Math.abs(release.projected - MEDIUM_GONE_BOUNDARY)).toBeLessThan( |
| 115 | + 1, |
| 116 | + ); |
| 117 | + } else if (miss === 'under-read') { |
| 118 | + expect(landed).not.toBe(outcome); |
| 119 | + } else { |
| 120 | + expect(landed).toBe(outcome); |
| 121 | + } |
| 122 | + if (zeta != null) { |
| 123 | + expect(release.spring).toBe( |
| 124 | + zeta === 1 ? springs.sheetDetent : springs.sheetReleaseFast, |
| 125 | + ); |
| 126 | + } |
| 127 | + }); |
| 128 | + } |
| 129 | + |
| 130 | + it('reproduces all but the three releases it names', () => { |
| 131 | + const misses = UIKIT_RELEASES.filter(([, offset, velocity, outcome]) => { |
| 132 | + const release = resolveSheetRelease({ |
| 133 | + detents: [LARGE, MEDIUM], |
| 134 | + dismissedOffset: GONE, |
| 135 | + position: offset, |
| 136 | + velocity, |
| 137 | + }); |
| 138 | + const landed = |
| 139 | + release.kind === 'dismiss' |
| 140 | + ? 'dismissed' |
| 141 | + : release.index === 0 |
| 142 | + ? 'large' |
| 143 | + : 'medium'; |
| 144 | + return landed !== outcome; |
| 145 | + }).map(([name]) => name); |
| 146 | + expect(misses.sort()).toEqual(Object.keys(KNOWN_MISSES).sort()); |
| 147 | + }); |
| 148 | +}); |
| 149 | + |
| 150 | +describe('resolveSheetRelease', () => { |
| 151 | + it('projects linearly: offset plus the velocity times the projection time', () => { |
| 152 | + const release = resolveSheetRelease({ |
| 153 | + detents: [0, 300], |
| 154 | + dismissedOffset: 640, |
| 155 | + position: 100, |
| 156 | + velocity: 500, |
| 157 | + }); |
| 158 | + expect(release.projected).toBeCloseTo( |
| 159 | + 100 + 500 * sheet.release.projectionSeconds, |
| 160 | + 9, |
| 161 | + ); |
| 162 | + }); |
| 163 | + |
| 164 | + it('at rest, dismisses only past halfway from the shortest detent to gone', () => { |
| 165 | + const at = (position: number) => |
| 166 | + resolveSheetRelease({ |
| 167 | + detents: [0, 300], |
| 168 | + dismissedOffset: 640, |
| 169 | + position, |
| 170 | + velocity: 0, |
| 171 | + }).kind; |
| 172 | + expect(at(469)).toBe('detent'); |
| 173 | + expect(at(471)).toBe('dismiss'); |
| 174 | + }); |
| 175 | + |
| 176 | + it('settles against the motion when the projection says so', () => { |
| 177 | + // Just above the shortest detent, moving up slowly: back down to it. |
| 178 | + const release = resolveSheetRelease({ |
| 179 | + detents: [0, 300], |
| 180 | + dismissedOffset: 640, |
| 181 | + position: 250, |
| 182 | + velocity: -200, |
| 183 | + }); |
| 184 | + expect(release).toMatchObject({kind: 'detent', index: 1, offset: 300}); |
| 185 | + }); |
| 186 | + |
| 187 | + it('never dismisses a sheet a drag may not dismiss', () => { |
| 188 | + const release = resolveSheetRelease({ |
| 189 | + detents: [0, 300], |
| 190 | + dismissedOffset: null, |
| 191 | + position: 600, |
| 192 | + velocity: 3000, |
| 193 | + }); |
| 194 | + expect(release).toMatchObject({kind: 'detent', index: 1, offset: 300}); |
| 195 | + }); |
| 196 | + |
| 197 | + it('picks the spring by speed alone, from sheet.release.fastSpeed', () => { |
| 198 | + const spring = (velocity: number, position = 150) => |
| 199 | + resolveSheetRelease({ |
| 200 | + detents: [0, 300], |
| 201 | + dismissedOffset: 640, |
| 202 | + position, |
| 203 | + velocity, |
| 204 | + }).spring; |
| 205 | + const fast = sheet.release.fastSpeed; |
| 206 | + expect(spring(fast - 1)).toBe(springs.sheetDetent); |
| 207 | + expect(spring(-(fast - 1))).toBe(springs.sheetDetent); |
| 208 | + expect(spring(fast)).toBe(springs.sheetReleaseFast); |
| 209 | + expect(spring(-fast)).toBe(springs.sheetReleaseFast); |
| 210 | + // Dismissals: the swipe spring when fast, the detent spring when slow. |
| 211 | + expect(spring(fast, 600)).toBe(springs.sheetDismissSwipe); |
| 212 | + expect(spring(0, 600)).toBe(springs.sheetDetent); |
| 213 | + }); |
| 214 | + |
| 215 | + it('needs a detent', () => { |
| 216 | + expect(() => |
| 217 | + resolveSheetRelease({ |
| 218 | + detents: [], |
| 219 | + dismissedOffset: 640, |
| 220 | + position: 0, |
| 221 | + velocity: 0, |
| 222 | + }), |
| 223 | + ).toThrow(RangeError); |
| 224 | + }); |
| 225 | +}); |
0 commit comments