import CoreGraphics import RealityDriverSupport import Testing struct DriverFrameWritesTests { @Test func positionOnlyDoesNotRewriteAnAcceptedPublishedSize() throws { let target = CGRect(x: 160, y: 57, width: 890, height: 846) var position = CGPoint(x: 20, y: 58) var sizes: [CGSize] = [] try DriverFrameWrites.apply( target: target, publishedSize: target.size, readSize: { CGSize(width: 890, height: 845) }, readPosition: { position }, writeSize: { sizes.append($0) }, writePosition: { position = $0 }) #expect(sizes.isEmpty) #expect(position == target.origin) } @Test func repairsThePositionAfterTheLastResizeMovesIt() throws { let target = CGRect(x: 20, y: 58, width: 890, height: 846) var size = CGSize(width: 600, height: 400) var position = CGPoint(x: 80, y: 108) var steps: [String] = [] try DriverFrameWrites.apply( target: target, publishedSize: size, readSize: { size }, readPosition: { position }, writeSize: { steps.append("size") size = steps.count == 1 ? CGSize(width: 890, height: 845) : $0 position.y += 1 }, writePosition: { steps.append("position") position = $0 }) #expect(steps == ["size", "position", "size", "position"]) #expect(position == target.origin) #expect(size == target.size) } @Test func unchangedPublishedFrameNeedsNoWrites() throws { let target = CGRect(x: 20, y: 58, width: 890, height: 846) try DriverFrameWrites.apply( target: target, publishedSize: target.size, readSize: { Issue.record("An unchanged size should not be read"); return target.size }, readPosition: { target.origin }, writeSize: { _ in Issue.record("Unexpected size write") }, writePosition: { _ in Issue.record("Unexpected position write") }) } @Test func readAndWriteFailuresAreNotHidden() { enum Failure: Error { case refused } let target = CGRect(x: 20, y: 58, width: 890, height: 846) #expect(throws: Failure.self) { try DriverFrameWrites.apply( target: target, publishedSize: target.size, readSize: { target.size }, readPosition: { throw Failure.refused }, writeSize: { _ in }, writePosition: { _ in }) } #expect(throws: Failure.self) { try DriverFrameWrites.apply( target: target, publishedSize: .zero, readSize: { target.size }, readPosition: { target.origin }, writeSize: { _ in throw Failure.refused }, writePosition: { _ in }) } } }