| 1 | import XCTest |
| 2 | @testable import DocsKit |
| 3 | |
| 4 | /// Spec §1, "Page centring": the web's clamp in window points at the six |
| 5 | /// widths, both zooms, inspector open and closed, the 1,108pt boundary, |
| 6 | /// and the paddings swapped right to left. |
| 7 | final class DocsDeskLayoutTests: XCTestCase { |
| 8 | /// The page's width in window points: 612 × 4/3 × zoom. |
| 9 | private func page(_ zoom: CGFloat) -> CGFloat { DocsDeskLayout.paperWidth * 4 / 3 * zoom } |
| 10 | |
| 11 | func testTheReserveIsTheInspectorAndItsGaps() { |
| 12 | XCTAssertEqual(DocsDeskLayout.reserve, 276) |
| 13 | } |
| 14 | |
| 15 | func testClosedThePageIsPlainlyCentred() { |
| 16 | for zoom in [1.0, 1.5] as [CGFloat] { |
| 17 | for width in [1800, 1440, 1280, 1108, 1000, 920] as [CGFloat] { |
| 18 | XCTAssertEqual(DocsDeskLayout.inspectorPadding(width: width, page: page(zoom), inspector: false), 16) |
| 19 | let x = DocsDeskLayout.pageX(width: width, page: page(zoom), inspector: nil) |
| 20 | XCTAssertEqual(x, max(16, (width - page(zoom)) / 2), accuracy: 0.001, "width \(width) zoom \(zoom)") |
| 21 | } |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | /// E = clamp(16, 16 + P + 2R − W, R) and the page centred in [16, W − E]. |
| 26 | func testOpenFollowsTheWebsClamp() { |
| 27 | let expected: [(CGFloat, CGFloat, CGFloat, CGFloat)] = [ |
| 28 | // width, zoom, E, page's left edge |
| 29 | (1800, 1, 16, 492), (1440, 1, 16, 312), (1280, 1, 104, 188), (1108, 1, 276, 16), (1000, 1, 276, 16), (920, 1, 276, 16), |
| 30 | (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), |
| 31 | ] |
| 32 | for (width, zoom, e, x) in expected { |
| 33 | XCTAssertEqual(DocsDeskLayout.inspectorPadding(width: width, page: page(zoom), inspector: true), e, accuracy: 0.001, "E at \(width) zoom \(zoom)") |
| 34 | XCTAssertEqual(DocsDeskLayout.pageX(width: width, page: page(zoom), inspector: .right), x, accuracy: 0.001, "x at \(width) zoom \(zoom)") |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | /// At W = P + 16 + R the page just clears the inspector (16pt apart); |
| 39 | /// a point narrower and it starts to run under it. |
| 40 | func testTheBoundaryAtZoom100IsOneThousandOneHundredAndEight() { |
| 41 | let p = page(1) |
| 42 | let boundary = p + 16 + DocsDeskLayout.reserve |
| 43 | XCTAssertEqual(boundary, 1108, accuracy: 0.001) |
| 44 | let x = DocsDeskLayout.pageX(width: boundary, page: p, inspector: .right) |
| 45 | let inspectorLeft = boundary - DocsDeskLayout.inspectorInset - DocsDeskLayout.inspectorWidth |
| 46 | XCTAssertEqual(inspectorLeft - (x + p), 16, accuracy: 0.001) |
| 47 | let narrower = DocsDeskLayout.pageX(width: boundary - 1, page: p, inspector: .right) |
| 48 | XCTAssertGreaterThan(narrower + p, boundary - 1 - DocsDeskLayout.inspectorInset - DocsDeskLayout.inspectorWidth - 16) |
| 49 | XCTAssertEqual(narrower, 16, "the page's leading edge stays at 16; it never shrinks") |
| 50 | } |
| 51 | |
| 52 | /// Right to left the inspector is on the left and the paddings swap. |
| 53 | func testRightToLeftMirrors() { |
| 54 | for (width, zoom) in [(1800, 1), (1440, 1), (1280, 1), (1108, 1), (1800, 1.5)] as [(CGFloat, CGFloat)] { |
| 55 | let ltr = DocsDeskLayout.pageX(width: width, page: page(zoom), inspector: .right) |
| 56 | let rtl = DocsDeskLayout.pageX(width: width, page: page(zoom), inspector: .left) |
| 57 | XCTAssertEqual(rtl, width - ltr - page(zoom), accuracy: 0.001, "width \(width)") |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |