Cotton code
85 lines · 4.0 KB · SwiftRaw
#if os(iOS)import SwiftUIimport UIKitimport WebKitimport XCTest@testable import CottonUI@MainActorfinal class ViewerWebGroundTests: XCTestCase {    func testLoadedBodyPaintReachesTheHead() async throws {        let loadedPaint = try await loaded("body { background: rgb(24, 40, 56) }")        let paint = try XCTUnwrap(loadedPaint)        XCTAssertEqual(paint.red, 24.0 / 255, accuracy: 0.01)        XCTAssertEqual(paint.green, 40.0 / 255, accuracy: 0.01)        XCTAssertEqual(paint.blue, 56.0 / 255, accuracy: 0.01)    }    func testHTMLPaintIsTheCanvasWhenTheBodyIsTransparent() async throws {        let loadedPaint = try await loaded("html { background: rgb(229, 238, 246) } body { background: transparent }")        let paint = try XCTUnwrap(loadedPaint)        XCTAssertEqual(paint.red, 229.0 / 255, accuracy: 0.01)        XCTAssertEqual(paint.green, 238.0 / 255, accuracy: 0.01)        XCTAssertEqual(paint.blue, 246.0 / 255, accuracy: 0.01)    }    func testPartialBodyPaintRetainsItsAlpha() async throws {        let loadedPaint = try await loaded("body { background: rgba(0, 0, 0, 0.5) }")        let paint = try XCTUnwrap(loadedPaint)        XCTAssertEqual(paint.alpha, 0.5, accuracy: 0.01)        XCTAssertEqual(paint.red, 0, accuracy: 0.01)    }    func testAPageWithoutPaintKeepsTheViewerGround() async throws {        let paint = try await loaded("")        XCTAssertNil(paint)    }    func testTranslucentPaintAndInkFollowTheCanvas() throws {        let white = ThemeColor(red: 1, green: 1, blue: 1)        let halfBlack = ThemeColor(red: 0, green: 0, blue: 0, alpha: 0.5)        let mixed = try XCTUnwrap(ViewerWebGround.composite(halfBlack, over: white))        XCTAssertEqual(mixed, ThemeColor(red: 0.5, green: 0.5, blue: 0.5))        XCTAssertNil(ViewerWebGround.composite(halfBlack.withAlpha(0), over: white))        let theme = Theme.paper        XCTAssertEqual(ViewerWebGround.ink(on: white, theme: theme), theme.palette.resolved(.light).ink)        XCTAssertEqual(ViewerWebGround.ink(on: halfBlack, theme: theme), theme.palette.resolved(.light).paper)    }    /// The real sealed pane's navigation delegate, a local synthetic page,    /// and a visible WebKit canvas: no network and no personal records.    private func loaded(_ css: String) async throws -> ThemeColor? {        let file = FileManager.default.temporaryDirectory.appendingPathComponent("viewer-ground-\(UUID()).html")        try "<!doctype html><html><head><style>\(css)</style></head><body>synthetic page</body></html>".write(to: file, atomically: true, encoding: .utf8)        defer { try? FileManager.default.removeItem(at: file) }        let compiled = await ViewerSeal.rules()        let rules = try XCTUnwrap(compiled)        let configuration = WKWebViewConfiguration()        configuration.websiteDataStore = .nonPersistent()        configuration.userContentController.add(rules)        let web = WKWebView(frame: CGRect(x: 0, y: 0, width: 402, height: 874), configuration: configuration)        web.isOpaque = false        web.backgroundColor = .clear        web.scrollView.backgroundColor = .clear        let controller = UIViewController()        controller.view = web        let scene = try XCTUnwrap(UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }.first)        let window = UIWindow(windowScene: scene)        window.rootViewController = controller        window.makeKeyAndVisible()        defer { window.isHidden = true }        let finished = expectation(description: "loaded page reports its canvas")        var result: ThemeColor?        let delegate = ViewerSealedWebView.Coordinator(file: file) { result = $0; finished.fulfill() }        web.navigationDelegate = delegate        web.loadFileURL(file, allowingReadAccessTo: file)        await fulfillment(of: [finished], timeout: 10)        withExtendedLifetime(delegate) {}        return result    }}private extension ThemeColor {    func withAlpha(_ alpha: Double) -> ThemeColor { ThemeColor(red: red, green: green, blue: blue, alpha: alpha) }}#endif