oficina / base Public

Clone
git clone https://amadan.net/oficina/base

Plain git — no account needed to clone.

Download

Download this file

1@testable import BaseKit
2import SwiftUI
3import XCTest
4
5/// The sky's motion is the stylesheet's: its keyframes, eased, alternating.
6final class LookTests: XCTestCase {
7 func testEaseInOutIsCSSs() {
8 XCTAssertEqual(SkyPainter.easeInOut(0), 0)
9 XCTAssertEqual(SkyPainter.easeInOut(1), 1)
10 XCTAssertEqual(SkyPainter.easeInOut(0.5), 0.5, accuracy: 1e-4)
11 // cubic-bezier(.42,0,.58,1) at 0.25 is about 0.129.
12 XCTAssertEqual(SkyPainter.easeInOut(0.25), 0.129, accuracy: 0.002)
13 }
14
15 func testAlternatingProgress() {
16 // sway 9s alternate: out on the first pass, back on the second.
17 XCTAssertEqual(SkyPainter.progress(0, period: 9, delay: 0, reverse: false), 0, accuracy: 1e-9)
18 XCTAssertEqual(SkyPainter.progress(4.5, period: 9, delay: 0, reverse: false), 0.5, accuracy: 1e-9)
19 XCTAssertEqual(SkyPainter.progress(13.5, period: 9, delay: 0, reverse: false), 0.5, accuracy: 1e-9)
20 XCTAssertEqual(SkyPainter.progress(12, period: 9, delay: 0, reverse: false), 2.0 / 3, accuracy: 1e-9)
21 // A negative delay starts it part-way; alternate-reverse starts backwards.
22 XCTAssertEqual(SkyPainter.progress(0, period: 15, delay: -5, reverse: false), 1.0 / 3, accuracy: 1e-9)
23 XCTAssertEqual(SkyPainter.progress(0, period: 13, delay: -4, reverse: true), 1 - 4.0 / 13, accuracy: 1e-9)
24 }
25
26 func testKeyframesHitTheirStops() {
27 let a = SkyPainter.pathA
28 let start = SkyPainter.keyframe(a, 0), middle = SkyPainter.keyframe(a, 0.5), end = SkyPainter.keyframe(a, 1)
29 XCTAssertEqual(start.tx, -0.18, accuracy: 1e-9); XCTAssertEqual(start.scale, 0.9, accuracy: 1e-9)
30 XCTAssertEqual(middle.ty, -0.28, accuracy: 1e-9); XCTAssertEqual(middle.scale, 1.25, accuracy: 1e-9)
31 XCTAssertEqual(end.tx, 0.36, accuracy: 1e-9); XCTAssertEqual(end.scale, 1, accuracy: 1e-9)
32 }
33
34 func testCSSGradientLine() {
35 // 180deg runs top to bottom through the centre.
36 let (s, e) = SkyPainter.cssLine(degrees: 180, in: CGRect(x: 0, y: 0, width: 100, height: 50))
37 XCTAssertEqual(s.x, 50, accuracy: 1e-9); XCTAssertEqual(s.y, 0, accuracy: 1e-9)
38 XCTAssertEqual(e.x, 50, accuracy: 1e-9); XCTAssertEqual(e.y, 50, accuracy: 1e-9)
39 // 165deg: down and a little right.
40 let (_, e2) = SkyPainter.cssLine(degrees: 165, in: CGRect(x: 0, y: 0, width: 100, height: 100))
41 XCTAssertGreaterThan(e2.x, 50); XCTAssertGreaterThan(e2.y, 95)
42 }
43
44 func testIconsParse() {
45 for icon in [BaseIcon.asking, .ready, .done, .paused, .stopped, .arrowOut, .close, .send] {
46 let bounds = icon.path(in: CGRect(x: 0, y: 0, width: 24, height: 24)).boundingRect
47 XCTAssertFalse(bounds.isEmpty && bounds.width == 0 && bounds.height == 0, "\(icon) drew nothing")
48 XCTAssertTrue(CGRect(x: 0, y: 0, width: 24, height: 24).insetBy(dx: -1, dy: -1).contains(bounds), "\(icon) leaves its box: \(bounds)")
49 }
50 // The question mark's arc (a2.9 2.9 0 0 1 5.6 1) ends where the curve starts.
51 let asking = BaseIcon.asking.path(in: CGRect(x: 0, y: 0, width: 24, height: 24)).boundingRect
52 XCTAssertEqual(asking.minX, 9.2, accuracy: 0.05)
53 XCTAssertEqual(asking.maxX, 14.8, accuracy: 0.6)
54 // Paused's clock is a whole circle of radius 7.2 round the middle.
55 let paused = BaseIcon.paused.path(in: CGRect(x: 0, y: 0, width: 24, height: 24)).boundingRect
56 XCTAssertEqual(paused.minX, 4.8, accuracy: 0.05)
57 XCTAssertEqual(paused.maxX, 19.2, accuracy: 0.05)
58 XCTAssertEqual(paused.minY, 4.8, accuracy: 0.05)
59 XCTAssertEqual(paused.maxY, 19.2, accuracy: 0.05)
60 }
61
62 func testAppearanceFromTheServer() {
63 XCTAssertEqual(BaseAppearance(server: "dark").colorScheme, .dark)
64 XCTAssertEqual(BaseAppearance(server: "light").colorScheme, .light)
65 XCTAssertNil(BaseAppearance(server: "system").colorScheme)
66 XCTAssertEqual(BaseAppearance(server: "sepia"), .system)
67 XCTAssertEqual(BaseAppearance(server: nil), .system)
68 }
69}
70