@testable import BaseKit import SwiftUI import XCTest /// The sky's motion is the stylesheet's: its keyframes, eased, alternating. final class LookTests: XCTestCase { func testEaseInOutIsCSSs() { XCTAssertEqual(SkyPainter.easeInOut(0), 0) XCTAssertEqual(SkyPainter.easeInOut(1), 1) XCTAssertEqual(SkyPainter.easeInOut(0.5), 0.5, accuracy: 1e-4) // cubic-bezier(.42,0,.58,1) at 0.25 is about 0.129. XCTAssertEqual(SkyPainter.easeInOut(0.25), 0.129, accuracy: 0.002) } func testAlternatingProgress() { // sway 9s alternate: out on the first pass, back on the second. XCTAssertEqual(SkyPainter.progress(0, period: 9, delay: 0, reverse: false), 0, accuracy: 1e-9) XCTAssertEqual(SkyPainter.progress(4.5, period: 9, delay: 0, reverse: false), 0.5, accuracy: 1e-9) XCTAssertEqual(SkyPainter.progress(13.5, period: 9, delay: 0, reverse: false), 0.5, accuracy: 1e-9) XCTAssertEqual(SkyPainter.progress(12, period: 9, delay: 0, reverse: false), 2.0 / 3, accuracy: 1e-9) // A negative delay starts it part-way; alternate-reverse starts backwards. XCTAssertEqual(SkyPainter.progress(0, period: 15, delay: -5, reverse: false), 1.0 / 3, accuracy: 1e-9) XCTAssertEqual(SkyPainter.progress(0, period: 13, delay: -4, reverse: true), 1 - 4.0 / 13, accuracy: 1e-9) } func testKeyframesHitTheirStops() { let a = SkyPainter.pathA let start = SkyPainter.keyframe(a, 0), middle = SkyPainter.keyframe(a, 0.5), end = SkyPainter.keyframe(a, 1) XCTAssertEqual(start.tx, -0.18, accuracy: 1e-9); XCTAssertEqual(start.scale, 0.9, accuracy: 1e-9) XCTAssertEqual(middle.ty, -0.28, accuracy: 1e-9); XCTAssertEqual(middle.scale, 1.25, accuracy: 1e-9) XCTAssertEqual(end.tx, 0.36, accuracy: 1e-9); XCTAssertEqual(end.scale, 1, accuracy: 1e-9) } func testCSSGradientLine() { // 180deg runs top to bottom through the centre. let (s, e) = SkyPainter.cssLine(degrees: 180, in: CGRect(x: 0, y: 0, width: 100, height: 50)) XCTAssertEqual(s.x, 50, accuracy: 1e-9); XCTAssertEqual(s.y, 0, accuracy: 1e-9) XCTAssertEqual(e.x, 50, accuracy: 1e-9); XCTAssertEqual(e.y, 50, accuracy: 1e-9) // 165deg: down and a little right. let (_, e2) = SkyPainter.cssLine(degrees: 165, in: CGRect(x: 0, y: 0, width: 100, height: 100)) XCTAssertGreaterThan(e2.x, 50); XCTAssertGreaterThan(e2.y, 95) } func testIconsParse() { for icon in [BaseIcon.asking, .ready, .done, .paused, .stopped, .arrowOut, .close, .send] { let bounds = icon.path(in: CGRect(x: 0, y: 0, width: 24, height: 24)).boundingRect XCTAssertFalse(bounds.isEmpty && bounds.width == 0 && bounds.height == 0, "\(icon) drew nothing") XCTAssertTrue(CGRect(x: 0, y: 0, width: 24, height: 24).insetBy(dx: -1, dy: -1).contains(bounds), "\(icon) leaves its box: \(bounds)") } // The question mark's arc (a2.9 2.9 0 0 1 5.6 1) ends where the curve starts. let asking = BaseIcon.asking.path(in: CGRect(x: 0, y: 0, width: 24, height: 24)).boundingRect XCTAssertEqual(asking.minX, 9.2, accuracy: 0.05) XCTAssertEqual(asking.maxX, 14.8, accuracy: 0.6) // Paused's clock is a whole circle of radius 7.2 round the middle. let paused = BaseIcon.paused.path(in: CGRect(x: 0, y: 0, width: 24, height: 24)).boundingRect XCTAssertEqual(paused.minX, 4.8, accuracy: 0.05) XCTAssertEqual(paused.maxX, 19.2, accuracy: 0.05) XCTAssertEqual(paused.minY, 4.8, accuracy: 0.05) XCTAssertEqual(paused.maxY, 19.2, accuracy: 0.05) } func testAppearanceFromTheServer() { XCTAssertEqual(BaseAppearance(server: "dark").colorScheme, .dark) XCTAssertEqual(BaseAppearance(server: "light").colorScheme, .light) XCTAssertNil(BaseAppearance(server: "system").colorScheme) XCTAssertEqual(BaseAppearance(server: "sepia"), .system) XCTAssertEqual(BaseAppearance(server: nil), .system) } }