import CoreGraphics import XCTest @testable import CallsKit /// Portrait video (2026-10-21): which way up a picture is, and whether it /// fills its place or is shown whole. final class VideoShapeTests: XCTestCase { private func size(_ w: CGFloat, _ h: CGFloat) -> CGSize { CGSize(width: w, height: h) } // MARK: The classifier func testThresholds() { XCTAssertEqual(VideoShape.of(size(720, 1280)), .portrait) XCTAssertEqual(VideoShape.of(size(1280, 720)), .landscape) XCTAssertEqual(VideoShape.of(size(640, 480)), .landscape, "4:3 is landscape") XCTAssertEqual(VideoShape.of(size(480, 640)), .portrait) XCTAssertEqual(VideoShape.of(size(500, 500)), .square) // h > w × 1.1 is portrait; exactly 1.1 is not yet. XCTAssertEqual(VideoShape.of(size(1000, 1100)), .square) XCTAssertEqual(VideoShape.of(size(1000, 1101)), .portrait) XCTAssertEqual(VideoShape.of(size(1101, 1000)), .landscape) XCTAssertEqual(VideoShape.of(size(0, 0)), .landscape, "nothing known is today's landscape") } func testRotationIsApplied() { // An iPhone held upright sends its 1280 × 720 buffer turned 90°. XCTAssertEqual(VideoShape.displayed(width: 1280, height: 720, rotation: 90), size(720, 1280)) XCTAssertEqual(VideoShape.displayed(width: 1280, height: 720, rotation: 270), size(720, 1280)) XCTAssertEqual(VideoShape.displayed(width: 1280, height: 720, rotation: 180), size(1280, 720)) XCTAssertEqual(VideoShape.displayed(width: 1280, height: 720, rotation: 0), size(1280, 720)) XCTAssertEqual(VideoShape.of(VideoShape.displayed(width: 1280, height: 720, rotation: 90)), .portrait) } func testHysteresisHoldsAShapeThroughTheBand() { var reading = VideoShapeReading() XCTAssertEqual(reading.read(size(720, 1280)), .portrait) // Still taller than wide, inside the band: stays portrait. XCTAssertEqual(reading.read(size(1000, 1050)), .portrait) XCTAssertEqual(reading.read(size(1000, 1001)), .portrait) // No taller than wide: let go, to what it now is. XCTAssertEqual(reading.read(size(1000, 1000)), .square) // Back up into the band is not enough to be portrait again. XCTAssertEqual(reading.read(size(1000, 1080)), .square) XCTAssertEqual(reading.read(size(1000, 1200)), .portrait) } func testNearSquareDoesNotFlap() { // A picture jittering about 1.1 from square never changes once. var reading = VideoShapeReading() var changes = 0 var last = reading.read(size(1000, 1000)) for step in 0..<40 { let h: CGFloat = step.isMultiple(of: 2) ? 1095 : 1105 let now = reading.read(size(1000, h)) if now != last { changes += 1 } last = now } XCTAssertEqual(changes, 1, "into portrait once, then held there") } func testARotationMidCallSwitchesOnce() { // A phone turned: landscape, the steps a resolution ladder may report // on the way, then upright, with no flapping between. var reading = VideoShapeReading() let sizes = [ size(1280, 720), size(960, 540), size(1280, 720), // landscape, adapting size(720, 1280), size(540, 960), size(720, 1280), // turned upright ] var shapes: [VideoShape] = [] for s in sizes { let shape = reading.read(s) if shapes.last != shape { shapes.append(shape) } } XCTAssertEqual(shapes, [.landscape, .portrait]) } // MARK: Fill or fit func testCrop() { XCTAssertEqual(VideoPlacement.crop(video: size(1280, 720), in: size(1600, 900)), 0, accuracy: 1e-6) // 16:9 in 16:10 cuts a tenth of its width. XCTAssertEqual(VideoPlacement.crop(video: size(16, 9), in: size(16, 10)), 0.1, accuracy: 1e-6) // 9:16 in 16:10: all but 9/16 × 10/16 of its height goes. XCTAssertEqual( VideoPlacement.crop(video: size(9, 16), in: size(16, 10)), 1 - (9.0 / 16) * (10.0 / 16), accuracy: 1e-6) } /// The decision table in VideoPlacement's comment, row by row. func testDecisionTable() { let mac = size(1440, 900) // 16:10 let phone = size(393, 852) // an upright iPhone let tile = size(320, 180) // a grid or filmstrip tile, 16:9 let phoneTile = size(99, 132) // the iPhone's filmstrip, 3:4 func fits(_ v: CGSize, _ b: CGSize, screen: Bool = false) -> Bool { var reading = VideoShapeReading() return VideoPlacement.fits(video: v, shape: reading.read(v), in: b, screen: screen) } // A screen is always fitted. XCTAssertTrue(fits(size(1920, 1200), mac, screen: true)) XCTAssertTrue(fits(size(1440, 900), mac, screen: true)) // The same shape fills, however much it crops. XCTAssertFalse(fits(size(1280, 720), mac)) XCTAssertFalse(fits(size(720, 1280), phone)) XCTAssertFalse(fits(size(2560, 1080), tile), "21:9 in 16:9 crops 25% and still fills") // Another shape with a small crop fills. XCTAssertFalse(fits(size(1000, 1000), size(1100, 1000)), "square in a near-square place") XCTAssertFalse(fits(size(1050, 1000), size(1000, 1080))) // Another shape with a big crop is fitted. XCTAssertTrue(fits(size(720, 1280), mac), "Farshad's iPhone on a Mac window") XCTAssertTrue(fits(size(720, 1280), tile), "and in a grid or filmstrip tile") XCTAssertTrue(fits(size(1280, 720), phone), "a Mac on an upright phone") XCTAssertTrue(fits(size(1280, 720), phoneTile), "and in the phone's 3:4 filmstrip") XCTAssertTrue(fits(size(720, 720), mac), "a square picture in a 16:10 window") // The 15% line: 4:3 into 16:9 (25%) is the same shape and fills. XCTAssertFalse(fits(size(640, 480), tile)) // A portrait 3:4 into a square-ish 1.08:1 place cuts 31%: fitted. XCTAssertTrue(fits(size(480, 640), size(1080, 1000))) // Nothing known yet: fill, as 0.4.10. XCTAssertFalse(VideoPlacement.fits(video: .zero, shape: .portrait, in: mac, screen: false)) } func testPlacedPictureFitsCentredAndFillsWithHeadroom() { var picture = PlacedPicture() let mac = size(1600, 1000) // Before the first frame: 16:9, filling. XCTAssertFalse(picture.place(in: mac).fitted) picture.update(video: size(720, 1280)) let placed = picture.place(in: mac) XCTAssertTrue(placed.fitted) XCTAssertEqual(placed.frame.height, 1000, accuracy: 1e-6, "the whole height shown") XCTAssertEqual(placed.frame.width, 562.5, accuracy: 1e-6) XCTAssertEqual(placed.frame.midX, 800, accuracy: 1e-6, "centred") // Turned back to landscape: fills again, cropped with headroom. picture.update(video: size(1280, 720)) let filled = picture.place(in: mac) XCTAssertFalse(filled.fitted) XCTAssertGreaterThanOrEqual(filled.frame.width, 1600) XCTAssertGreaterThanOrEqual(filled.frame.height, 1000 - 1e-6) } // MARK: The self view func testAnUprightSelfFloatsUpright() { let mac = size(1200, 800) let landscape = PiPLayout.size(in: mac, touch: false) XCTAssertGreaterThan(landscape.width, landscape.height) let upright = PiPLayout.size(in: mac, touch: false, shape: .portrait) XCTAssertGreaterThan(upright.height, upright.width) XCTAssertEqual(upright.width / upright.height, 9.0 / 16, accuracy: 0.02) // A phone: its own picture's shape wins over the screen's. let phone = size(393, 852) XCTAssertGreaterThan(PiPLayout.size(in: phone, touch: true).height, PiPLayout.size(in: phone, touch: true).width) let turned = PiPLayout.size(in: phone, touch: true, shape: .landscape) XCTAssertGreaterThan(turned.width, turned.height) } // MARK: The backdrop's picture func testBackdropPictureTakesThePicturesShape() { XCTAssertTrue(BackdropSampler.size(for: size(1280, 720)) == (64, 36)) XCTAssertTrue(BackdropSampler.size(for: size(720, 1280)) == (36, 64)) XCTAssertTrue(BackdropSampler.size(for: size(500, 500)) == (48, 48)) } func testBackdropPictureIsTurnedUpright() { // 3 × 2, each pixel's red its index: turned 90° clockwise it is 2 × 3 // with the bottom-left pixel first. let w = 3 let h = 2 var rgba = [UInt8](repeating: 255, count: w * h * 4) for i in 0..<(w * h) { rgba[i * 4] = UInt8(i) } let turned = BackdropSampler.rotate(rgba, width: w, height: h, rotation: 90) XCTAssertEqual(turned.width, 2) XCTAssertEqual(turned.height, 3) XCTAssertEqual(stride(from: 0, to: 6, by: 1).map { turned.pixels[$0 * 4] }, [3, 0, 4, 1, 5, 2]) let back = BackdropSampler.rotate(turned.pixels, width: 2, height: 3, rotation: 270) XCTAssertEqual(back.pixels, rgba) let half = BackdropSampler.rotate(rgba, width: w, height: h, rotation: 180) XCTAssertEqual(stride(from: 0, to: 6, by: 1).map { half.pixels[$0 * 4] }, [5, 4, 3, 2, 1, 0]) } /// The cost of several fitted tiles' backdrops: each takes a real /// 1280 × 720 frame through WebRTC's scale, the conversion, the blur and /// the CGImage, twice a second. Printed for the record; held well under /// a frame's time for the whole budget at once. func testSeveralBackdropsAreCheap() { let source = RTCI420Buffer(width: 1280, height: 720) let upright = RTCVideoFrame(buffer: source, rotation: ._90, timeStampNs: 0) let level = RTCVideoFrame(buffer: source, rotation: ._0, timeStampNs: 0) _ = BackdropSampler.picture(of: upright) let runs = 20 let start = Date() for _ in 0..