Files
hapi/ios/HapiTests/ImageZoomMathTests.swift
T
wushenghuaandHAPI 0d8abb7e2c feat(ios): zoom image attachments full screen
Tapping a user attachment or a generated image opens a pannable,
pinch-zoomable viewer. Zoom math (fit scale, clamping, double-tap
targets) is pure and unit tested so the gestures stay predictable.

via [HAPI](https://hapi.run)

Co-Authored-By: HAPI <noreply@hapi.run>
2026-12-05 12:09:07 +08:00

65 lines
2.0 KiB
Swift

import XCTest
@testable import Hapi
final class ImageZoomMathTests: XCTestCase {
func testScaleStaysWithinBounds() {
XCTAssertEqual(ImageZoomMath.clampedScale(0.2), 1)
XCTAssertEqual(ImageZoomMath.clampedScale(3), 3)
XCTAssertEqual(ImageZoomMath.clampedScale(99), 8)
}
func testFittedSizeUsesTheTighterAxis() {
let landscape = ImageZoomMath.fittedSize(
for: CGSize(width: 400, height: 200),
in: CGSize(width: 300, height: 300)
)
XCTAssertEqual(landscape.width, 300)
XCTAssertEqual(landscape.height, 150)
let portrait = ImageZoomMath.fittedSize(
for: CGSize(width: 200, height: 400),
in: CGSize(width: 300, height: 300)
)
XCTAssertEqual(portrait.width, 150)
XCTAssertEqual(portrait.height, 300)
}
func testOffsetIsClampedToTheScaledImageEdges() {
let image = CGSize(width: 400, height: 200)
let container = CGSize(width: 300, height: 300)
// At 1x the image fits horizontally, so horizontal panning is locked.
XCTAssertEqual(
ImageZoomMath.clampedOffset(
CGSize(width: 200, height: 0),
scale: 1,
imageSize: image,
container: container
),
.zero
)
// At 2x there is 150 pt of horizontal slack on each side.
XCTAssertEqual(
ImageZoomMath.clampedOffset(
CGSize(width: 500, height: 500),
scale: 2,
imageSize: image,
container: container
),
CGSize(width: 150, height: 0)
)
}
func testDegenerateSizesDoNotProduceNaN() {
XCTAssertEqual(
ImageZoomMath.fittedSize(for: .zero, in: CGSize(width: 300, height: 300)),
.zero
)
XCTAssertEqual(
ImageZoomMath.clampedOffset(.zero, scale: 2, imageSize: .zero, container: .zero),
.zero
)
}
}