import XCTest @testable import DocsKit /// Spec §1, "Page centring": the web's clamp in window points at the six /// widths, both zooms, inspector open and closed, the 1,108pt boundary, /// and the paddings swapped right to left. final class DocsDeskLayoutTests: XCTestCase { /// The page's width in window points: 612 × 4/3 × zoom. private func page(_ zoom: CGFloat) -> CGFloat { DocsDeskLayout.paperWidth * 4 / 3 * zoom } func testTheReserveIsTheInspectorAndItsGaps() { XCTAssertEqual(DocsDeskLayout.reserve, 276) } func testClosedThePageIsPlainlyCentred() { for zoom in [1.0, 1.5] as [CGFloat] { for width in [1800, 1440, 1280, 1108, 1000, 920] as [CGFloat] { XCTAssertEqual(DocsDeskLayout.inspectorPadding(width: width, page: page(zoom), inspector: false), 16) let x = DocsDeskLayout.pageX(width: width, page: page(zoom), inspector: nil) XCTAssertEqual(x, max(16, (width - page(zoom)) / 2), accuracy: 0.001, "width \(width) zoom \(zoom)") } } } /// E = clamp(16, 16 + P + 2R − W, R) and the page centred in [16, W − E]. func testOpenFollowsTheWebsClamp() { let expected: [(CGFloat, CGFloat, CGFloat, CGFloat)] = [ // width, zoom, E, page's left edge (1800, 1, 16, 492), (1440, 1, 16, 312), (1280, 1, 104, 188), (1108, 1, 276, 16), (1000, 1, 276, 16), (920, 1, 276, 16), (1800, 1.5, 16, 288), (1440, 1.5, 276, 16), (1280, 1.5, 276, 16), (1108, 1.5, 276, 16), (1000, 1.5, 276, 16), (920, 1.5, 276, 16), ] for (width, zoom, e, x) in expected { XCTAssertEqual(DocsDeskLayout.inspectorPadding(width: width, page: page(zoom), inspector: true), e, accuracy: 0.001, "E at \(width) zoom \(zoom)") XCTAssertEqual(DocsDeskLayout.pageX(width: width, page: page(zoom), inspector: .right), x, accuracy: 0.001, "x at \(width) zoom \(zoom)") } } /// At W = P + 16 + R the page just clears the inspector (16pt apart); /// a point narrower and it starts to run under it. func testTheBoundaryAtZoom100IsOneThousandOneHundredAndEight() { let p = page(1) let boundary = p + 16 + DocsDeskLayout.reserve XCTAssertEqual(boundary, 1108, accuracy: 0.001) let x = DocsDeskLayout.pageX(width: boundary, page: p, inspector: .right) let inspectorLeft = boundary - DocsDeskLayout.inspectorInset - DocsDeskLayout.inspectorWidth XCTAssertEqual(inspectorLeft - (x + p), 16, accuracy: 0.001) let narrower = DocsDeskLayout.pageX(width: boundary - 1, page: p, inspector: .right) XCTAssertGreaterThan(narrower + p, boundary - 1 - DocsDeskLayout.inspectorInset - DocsDeskLayout.inspectorWidth - 16) XCTAssertEqual(narrower, 16, "the page's leading edge stays at 16; it never shrinks") } /// Right to left the inspector is on the left and the paddings swap. func testRightToLeftMirrors() { for (width, zoom) in [(1800, 1), (1440, 1), (1280, 1), (1108, 1), (1800, 1.5)] as [(CGFloat, CGFloat)] { let ltr = DocsDeskLayout.pageX(width: width, page: page(zoom), inspector: .right) let rtl = DocsDeskLayout.pageX(width: width, page: page(zoom), inspector: .left) XCTAssertEqual(rtl, width - ltr - page(zoom), accuracy: 0.001, "width \(width)") } } }