|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | + |
| 3 | +/** |
| 4 | + * @file zoomStage.test.ts |
| 5 | + * @input vitest, the zoom stage geometry and the UIKit zoom constants |
| 6 | + * @output Checks the zoom stage against UIKit's measured zooming: the |
| 7 | + * overshoot law past the limits (and its inverse), the settle's duration |
| 8 | + * law and eased curve, Photos' aspect-fill double tap and maximum zoom, the |
| 9 | + * point kept under the fingers, the resting place for a scale, the rubber |
| 10 | + * band of a drag and the transform kept across a resize |
| 11 | + * @position Testing; zoomStage.ts without a browser |
| 12 | + */ |
| 13 | + |
| 14 | +import {describe, expect, it} from 'vitest'; |
| 15 | +import {cubicBezier} from '../motion/bezier'; |
| 16 | +import {rubberBand} from '../motion/rubberBand'; |
| 17 | +import {zoomMedia} from '../motion/uikit/zoomMedia'; |
| 18 | +import { |
| 19 | + IDENTITY, |
| 20 | + doubleTapScaleFor, |
| 21 | + doubleTapTarget, |
| 22 | + draggedTransform, |
| 23 | + isZoomed, |
| 24 | + maxScaleFor, |
| 25 | + overshootScale, |
| 26 | + pannedBy, |
| 27 | + pinchScaleFor, |
| 28 | + rawForDragged, |
| 29 | + releaseTarget, |
| 30 | + restRange, |
| 31 | + restingTransform, |
| 32 | + settleCurves, |
| 33 | + settleSeconds, |
| 34 | + transformForResize, |
| 35 | + transformKeepingPoint, |
| 36 | + type ZoomGeometry, |
| 37 | + type ZoomTransform, |
| 38 | +} from './zoomStage'; |
| 39 | + |
| 40 | +/** |
| 41 | + * The lab's stage: a 402 × 874 screen showing a 3000 × 2000 photo fitted |
| 42 | + * 402 × 268 at its vertical centre, on a 3× screen. |
| 43 | + */ |
| 44 | +function landscape(overrides: Partial<ZoomGeometry> = {}): ZoomGeometry { |
| 45 | + const width = 402; |
| 46 | + const height = 874; |
| 47 | + const fit = {x: 0, y: (874 - 268) / 2, width: 402, height: 268}; |
| 48 | + const doubleTapScale = doubleTapScaleFor(width, height, 402, 268); |
| 49 | + return { |
| 50 | + width, |
| 51 | + height, |
| 52 | + fit, |
| 53 | + minScale: 1, |
| 54 | + maxScale: maxScaleFor(3000, 402, 3, doubleTapScale), |
| 55 | + doubleTapScale, |
| 56 | + ...overrides, |
| 57 | + }; |
| 58 | +} |
| 59 | + |
| 60 | +/** Where the photo point (u, v) of the fitted box shows on the stage. */ |
| 61 | +function shown( |
| 62 | + geometry: ZoomGeometry, |
| 63 | + transform: ZoomTransform, |
| 64 | + u: number, |
| 65 | + v: number, |
| 66 | +) { |
| 67 | + return { |
| 68 | + x: geometry.fit.x + transform.x + u * transform.scale, |
| 69 | + y: geometry.fit.y + transform.y + v * transform.scale, |
| 70 | + }; |
| 71 | +} |
| 72 | + |
| 73 | +describe('the overshoot past the zoom limits (UIKit lab, iOS 26.2)', () => { |
| 74 | + const geometry = landscape({minScale: 1, maxScale: 4}); |
| 75 | + |
| 76 | + it('follows the pinch 1:1 inside the limits', () => { |
| 77 | + for (const scale of [1, 1.5, 2.25, 4]) { |
| 78 | + expect(overshootScale(geometry, scale)).toBe(scale); |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + it('gives way past the maximum as measured', () => { |
| 83 | + // Pinch 4.5 / 5 / 5.5 / 6 past a maximum of 4. |
| 84 | + const measured = [ |
| 85 | + [4.5, 4.444], |
| 86 | + [5, 4.8], |
| 87 | + [5.5, 5.091], |
| 88 | + [6, 5.333], |
| 89 | + ]; |
| 90 | + for (const [pinch, zoom] of measured) { |
| 91 | + expect(overshootScale(geometry, pinch)).toBeCloseTo(zoom, 3); |
| 92 | + } |
| 93 | + }); |
| 94 | + |
| 95 | + it('gives way past the minimum as measured', () => { |
| 96 | + const measured = [ |
| 97 | + [0.85, 0.87], |
| 98 | + [0.7, 0.769], |
| 99 | + [0.474, 0.655], |
| 100 | + [0.323, 0.596], |
| 101 | + ]; |
| 102 | + for (const [pinch, zoom] of measured) { |
| 103 | + expect(overshootScale(geometry, pinch)).toBeCloseTo(zoom, 3); |
| 104 | + } |
| 105 | + }); |
| 106 | + |
| 107 | + it('never reaches twice the maximum or half the minimum', () => { |
| 108 | + expect(overshootScale(geometry, 1e6)).toBeLessThan(8); |
| 109 | + expect(overshootScale(geometry, 1e-6)).toBeGreaterThan(0.5); |
| 110 | + }); |
| 111 | + |
| 112 | + it('inverts, so a pinch can continue from a photo already past a limit', () => { |
| 113 | + for (const pinch of [0.3, 0.6, 0.95, 1, 2, 4, 4.4, 7, 20]) { |
| 114 | + expect( |
| 115 | + pinchScaleFor(geometry, overshootScale(geometry, pinch)), |
| 116 | + ).toBeCloseTo(pinch, 9); |
| 117 | + } |
| 118 | + }); |
| 119 | +}); |
| 120 | + |
| 121 | +describe('the settle (UIScrollView zoom animation)', () => { |
| 122 | + it('lasts 0.1 s + 0.3 s × |ln(zoom ratio)|, at most 0.4 s, as measured', () => { |
| 123 | + expect(settleSeconds(5.333, 4)).toBeCloseTo(0.186, 3); |
| 124 | + expect(settleSeconds(4.8, 4)).toBeCloseTo(0.155, 3); |
| 125 | + expect(settleSeconds(0.596, 1)).toBeCloseTo(0.255, 3); |
| 126 | + expect(settleSeconds(1, 2.5)).toBeCloseTo(0.375, 3); |
| 127 | + expect(settleSeconds(2.5, 1)).toBeCloseTo(0.375, 3); |
| 128 | + expect(settleSeconds(1, 4)).toBe(0.4); |
| 129 | + // A move at the same zoom (a keyboard step). |
| 130 | + expect(settleSeconds(3, 3)).toBeCloseTo(0.1, 9); |
| 131 | + }); |
| 132 | + |
| 133 | + it('runs along the curve the theme sets for zoomMedia.settle.curve', () => { |
| 134 | + const from: ZoomTransform = {x: 0, y: 0, scale: 1}; |
| 135 | + const to: ZoomTransform = {x: -400, y: -100, scale: 3}; |
| 136 | + // A theme's linear value for the role: a quarter of the time is a |
| 137 | + // quarter of the way (easeInEaseOut would be about 13 %). |
| 138 | + const curves = settleCurves(from, to, [0, 0, 1, 1]); |
| 139 | + const t = curves.duration / 4; |
| 140 | + expect(curves.x.at(t)).toBeCloseTo(-100, 3); |
| 141 | + expect(curves.scale.at(t)).toBeCloseTo(1.5, 3); |
| 142 | + }); |
| 143 | + |
| 144 | + it('moves zoom and offset together on easeInEaseOut', () => { |
| 145 | + const from: ZoomTransform = {x: 0, y: 0, scale: 1}; |
| 146 | + const to: ZoomTransform = {x: -400, y: -100, scale: 3}; |
| 147 | + const curves = settleCurves(from, to); |
| 148 | + const ease = cubicBezier([0.42, 0, 0.58, 1]); |
| 149 | + expect(zoomMedia.settle.curve).toEqual([0.42, 0, 0.58, 1]); |
| 150 | + expect(curves.duration).toBe(0.4); |
| 151 | + for (const p of [0, 0.1, 0.25, 0.5, 0.75, 1]) { |
| 152 | + const t = p * curves.duration; |
| 153 | + expect(curves.scale.at(t)).toBeCloseTo(1 + 2 * ease(p), 9); |
| 154 | + expect(curves.x.at(t)).toBeCloseTo(-400 * ease(p), 9); |
| 155 | + expect(curves.y.at(t)).toBeCloseTo(-100 * ease(p), 9); |
| 156 | + } |
| 157 | + // Ease in and out: at rest at both ends, fastest in the middle. |
| 158 | + expect(curves.x.velocity(0)).toBeCloseTo(0, 0); |
| 159 | + expect(curves.x.velocity(curves.duration)).toBeCloseTo(0, 0); |
| 160 | + expect(Math.abs(curves.x.velocity(curves.duration / 2))).toBeGreaterThan( |
| 161 | + 1000, |
| 162 | + ); |
| 163 | + }); |
| 164 | +}); |
| 165 | + |
| 166 | +describe("Photos' double tap and maximum zoom (iOS 26.2)", () => { |
| 167 | + it('zooms until the photo fills the screen (aspect fill)', () => { |
| 168 | + // Measured: landscape 402 × 268 → 3.261; portrait 402 × 603 → 1.449. |
| 169 | + expect(doubleTapScaleFor(402, 874, 402, 268)).toBeCloseTo(3.261, 3); |
| 170 | + expect(doubleTapScaleFor(402, 874, 402, 603)).toBeCloseTo(1.449, 3); |
| 171 | + }); |
| 172 | + |
| 173 | + it('falls back to 2× when the photo already fills the stage', () => { |
| 174 | + expect(doubleTapScaleFor(400, 300, 400, 300)).toBe(2); |
| 175 | + }); |
| 176 | + |
| 177 | + it('allows 6 times the zoom that shows one image pixel per device pixel', () => { |
| 178 | + // Measured: 14.93 for 3000 × 2000 and 9.95 for 2000 × 3000, fitted 402 pt |
| 179 | + // wide at 3×. |
| 180 | + expect(maxScaleFor(3000, 402, 3, 3.261)).toBeCloseTo(14.93, 2); |
| 181 | + expect(maxScaleFor(2000, 402, 3, 1.449)).toBeCloseTo(9.95, 2); |
| 182 | + // Never below the double tap's zoom. |
| 183 | + expect(maxScaleFor(200, 402, 3, 3.261)).toBe(3.261); |
| 184 | + }); |
| 185 | + |
| 186 | + it('brings a centre tap to the centre, at the aspect-fill zoom', () => { |
| 187 | + const geometry = landscape(); |
| 188 | + const target = doubleTapTarget(geometry, IDENTITY, {x: 201, y: 437}); |
| 189 | + expect(target.scale).toBeCloseTo(874 / 268, 9); |
| 190 | + // The photo point under the tap (its centre) ends at the screen centre, |
| 191 | + // and the photo exactly fills the screen's height. |
| 192 | + const centre = shown(geometry, target, 201, 134); |
| 193 | + expect(centre.x).toBeCloseTo(201, 9); |
| 194 | + expect(centre.y).toBeCloseTo(437, 9); |
| 195 | + expect(geometry.fit.y + target.y).toBeCloseTo(0, 9); |
| 196 | + }); |
| 197 | + |
| 198 | + it('brings an off-centre tap to the centre', () => { |
| 199 | + const geometry = landscape(); |
| 200 | + // A third of the way across the photo. |
| 201 | + const target = doubleTapTarget(geometry, IDENTITY, {x: 134, y: 437}); |
| 202 | + expect(shown(geometry, target, 134, 134).x).toBeCloseTo(201, 9); |
| 203 | + }); |
| 204 | + |
| 205 | + it('stops at the edge for a tap near it', () => { |
| 206 | + const geometry = landscape(); |
| 207 | + const target = doubleTapTarget(geometry, IDENTITY, {x: 20, y: 437}); |
| 208 | + // The photo's left edge rests on the screen's left edge. |
| 209 | + expect(geometry.fit.x + target.x).toBeCloseTo(0, 9); |
| 210 | + }); |
| 211 | + |
| 212 | + it('returns to the fitted size when zoomed', () => { |
| 213 | + const geometry = landscape(); |
| 214 | + const zoomed = doubleTapTarget(geometry, IDENTITY, {x: 201, y: 437}); |
| 215 | + expect(isZoomed(geometry, zoomed)).toBe(true); |
| 216 | + expect(doubleTapTarget(geometry, zoomed, {x: 50, y: 50})).toEqual(IDENTITY); |
| 217 | + }); |
| 218 | +}); |
| 219 | + |
| 220 | +describe('the point under the fingers', () => { |
| 221 | + it('stays under the centroid as it moves and the zoom changes', () => { |
| 222 | + const geometry = landscape(); |
| 223 | + const start: ZoomTransform = {x: -300, y: -100, scale: 2.5}; |
| 224 | + const anchor = {x: 150, y: 400}; |
| 225 | + const u = (anchor.x - geometry.fit.x - start.x) / start.scale; |
| 226 | + const v = (anchor.y - geometry.fit.y - start.y) / start.scale; |
| 227 | + const centroid = {x: 190, y: 360}; |
| 228 | + const next = transformKeepingPoint(geometry, start, anchor, centroid, 4.2); |
| 229 | + const point = shown(geometry, next, u, v); |
| 230 | + expect(point.x).toBeCloseTo(centroid.x, 9); |
| 231 | + expect(point.y).toBeCloseTo(centroid.y, 9); |
| 232 | + expect(next.scale).toBe(4.2); |
| 233 | + }); |
| 234 | + |
| 235 | + it('settles a release past the maximum about the last centroid', () => { |
| 236 | + const geometry = landscape({maxScale: 4}); |
| 237 | + const centroid = {x: 220, y: 430}; |
| 238 | + // Released at 5.333 with the content point under the centroid at u, v. |
| 239 | + const released: ZoomTransform = {x: -700, y: -500, scale: 5.333}; |
| 240 | + const target = releaseTarget(geometry, released, centroid); |
| 241 | + expect(target.scale).toBe(4); |
| 242 | + const u = (centroid.x - geometry.fit.x - released.x) / released.scale; |
| 243 | + const v = (centroid.y - geometry.fit.y - released.y) / released.scale; |
| 244 | + const point = shown(geometry, target, u, v); |
| 245 | + expect(point.x).toBeCloseTo(centroid.x, 9); |
| 246 | + expect(point.y).toBeCloseTo(centroid.y, 9); |
| 247 | + }); |
| 248 | + |
| 249 | + it('keeps a release inside the limits where it is', () => { |
| 250 | + const geometry = landscape(); |
| 251 | + const released: ZoomTransform = {x: -100, y: -50, scale: 2}; |
| 252 | + expect(releaseTarget(geometry, released, {x: 0, y: 0})).toBe(released); |
| 253 | + }); |
| 254 | + |
| 255 | + it('settles a release past the minimum to the fitted size, centred', () => { |
| 256 | + const geometry = landscape(); |
| 257 | + const released: ZoomTransform = {x: 40, y: 30, scale: 0.6}; |
| 258 | + expect(releaseTarget(geometry, released, {x: 180, y: 420})).toEqual( |
| 259 | + IDENTITY, |
| 260 | + ); |
| 261 | + }); |
| 262 | +}); |
| 263 | + |
| 264 | +describe('the resting place', () => { |
| 265 | + it('centres the photo on an axis where it is smaller than the stage', () => { |
| 266 | + const geometry = landscape(); |
| 267 | + // 2× is 804 × 536: wider than the screen, shorter than it. |
| 268 | + const range = restRange(geometry, 2); |
| 269 | + expect(range.x).toEqual({min: 402 - 804, max: 0}); |
| 270 | + expect(range.y.min).toBe(range.y.max); |
| 271 | + expect(geometry.fit.y + range.y.min + 536 / 2).toBeCloseTo(874 / 2, 9); |
| 272 | + }); |
| 273 | + |
| 274 | + it('keeps a zoomed photo with no gap at either edge', () => { |
| 275 | + const geometry = landscape(); |
| 276 | + expect(restingTransform(geometry, {x: 50, y: 0, scale: 2}).x).toBe(0); |
| 277 | + expect(restingTransform(geometry, {x: -900, y: 0, scale: 2}).x).toBe( |
| 278 | + 402 - 804, |
| 279 | + ); |
| 280 | + }); |
| 281 | + |
| 282 | + it('pans by keyboard steps inside those bounds', () => { |
| 283 | + const geometry = landscape(); |
| 284 | + const from: ZoomTransform = { |
| 285 | + x: -10, |
| 286 | + y: restRange(geometry, 2).y.min, |
| 287 | + scale: 2, |
| 288 | + }; |
| 289 | + expect(pannedBy(geometry, from, 50, 0).x).toBe(0); |
| 290 | + expect(pannedBy(geometry, from, -50, 0).x).toBe(-60); |
| 291 | + expect(pannedBy(geometry, from, 0, 50).y).toBe(from.y); |
| 292 | + }); |
| 293 | +}); |
| 294 | + |
| 295 | +describe('the rubber band of a drag', () => { |
| 296 | + const geometry = landscape(); |
| 297 | + |
| 298 | + it('follows the finger inside the bounds', () => { |
| 299 | + const raw: ZoomTransform = {x: -200, y: 0, scale: 2}; |
| 300 | + expect(draggedTransform(geometry, raw).x).toBe(-200); |
| 301 | + }); |
| 302 | + |
| 303 | + it("gives way past an edge with UIScrollView's rubber band over the stage width", () => { |
| 304 | + const raw: ZoomTransform = {x: 120, y: 0, scale: 2}; |
| 305 | + expect(draggedTransform(geometry, raw).x).toBeCloseTo( |
| 306 | + rubberBand(120, 402, 0.55), |
| 307 | + 9, |
| 308 | + ); |
| 309 | + const far: ZoomTransform = {x: 402 - 804 - 300, y: 0, scale: 2}; |
| 310 | + expect(draggedTransform(geometry, far).x).toBeCloseTo( |
| 311 | + 402 - 804 + rubberBand(-300, 402, 0.55), |
| 312 | + 9, |
| 313 | + ); |
| 314 | + }); |
| 315 | + |
| 316 | + it('does not move the photo on an axis where it fits', () => { |
| 317 | + const raw: ZoomTransform = {x: -200, y: 500, scale: 2}; |
| 318 | + expect(draggedTransform(geometry, raw).y).toBe( |
| 319 | + restRange(geometry, 2).y.min, |
| 320 | + ); |
| 321 | + }); |
| 322 | + |
| 323 | + it('inverts, so a finger landing on a banded photo picks it up where it shows', () => { |
| 324 | + const raw: ZoomTransform = {x: 150, y: 0, scale: 2}; |
| 325 | + const banded = draggedTransform(geometry, raw); |
| 326 | + expect(rawForDragged(geometry, banded).x).toBeCloseTo(150, 9); |
| 327 | + }); |
| 328 | +}); |
| 329 | + |
| 330 | +describe('a resize while zoomed (rotation)', () => { |
| 331 | + it('keeps the photo point at the centre and its size on screen', () => { |
| 332 | + const before = landscape(); |
| 333 | + const transform: ZoomTransform = {x: -500, y: -150, scale: 3}; |
| 334 | + const u = (201 - before.fit.x - transform.x) / transform.scale / 402; |
| 335 | + const v = (437 - before.fit.y - transform.y) / transform.scale / 268; |
| 336 | + // Landscape orientation: 874 × 402, the photo fitted 603 × 402. |
| 337 | + const after: ZoomGeometry = { |
| 338 | + width: 874, |
| 339 | + height: 402, |
| 340 | + fit: {x: (874 - 603) / 2, y: 0, width: 603, height: 402}, |
| 341 | + minScale: 1, |
| 342 | + maxScale: maxScaleFor(3000, 603, 3, 874 / 603), |
| 343 | + doubleTapScale: 874 / 603, |
| 344 | + }; |
| 345 | + const next = transformForResize(before, after, transform); |
| 346 | + // The same on-screen size: 402 × 3 = 1206 px wide either way. |
| 347 | + expect(603 * next.scale).toBeCloseTo(402 * 3, 9); |
| 348 | + const centre = shown(after, next, u * 603, v * 402); |
| 349 | + expect(centre.x).toBeCloseTo(437, 6); |
| 350 | + expect(centre.y).toBeCloseTo(201, 6); |
| 351 | + }); |
| 352 | + |
| 353 | + it('stays at the fitted size when not zoomed', () => { |
| 354 | + const before = landscape(); |
| 355 | + const after = landscape({width: 500}); |
| 356 | + expect(transformForResize(before, after, IDENTITY)).toEqual(IDENTITY); |
| 357 | + }); |
| 358 | +}); |
0 commit comments