SPB Git

Repository Public

SnapshotSupport.swift 2.1 KB 38 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Tests/EditorTests/SnapshotSupport.swift3// Description: Renders the editor off screen to a PNG (no window is shown, no focus is taken): visual evidence for the test report.4import AppKit5import Testing6import SPNotesDocument7@testable import SPNotesEditor89@MainActor func renderEditor(_ doc: NoteDocument, width: CGFloat = 820, dark: Bool = false, resources: AtomResources? = nil, to path: String) -> CGSize {10    _ = NSApplication.shared11    let (scroll, v) = NoteTextView.makeScrollable()12    let window = NSWindow(contentRect: CGRect(x: 0, y: 0, width: width, height: 600), styleMask: [.borderless], backing: .buffered, defer: false)13    window.appearance = NSAppearance(named: dark ? .darkAqua : .aqua)14    scroll.frame = window.contentView!.bounds; scroll.autoresizingMask = [.width, .height]15    window.contentView!.addSubview(scroll)16    v.resources = resources; v.load(doc)17    v.textLayoutManager?.ensureLayout(for: v.textLayoutManager!.documentRange)18    v.sizeToFit(); v.layoutSubtreeIfNeeded()19    let height = min(9000, max(200, v.frame.height))20    window.setContentSize(CGSize(width: width, height: height)); window.contentView!.layoutSubtreeIfNeeded()21    v.textLayoutManager?.ensureLayout(for: v.textLayoutManager!.documentRange)22    let host = window.contentView!23    guard let rep = host.bitmapImageRepForCachingDisplay(in: host.bounds) else { return .zero }24    let bg = NSView(frame: host.bounds); _ = bg25    host.wantsLayer = true; window.appearance?.performAsCurrentDrawingAppearance { host.layer?.backgroundColor = EditorTheme.background.cgColor }26    host.cacheDisplay(in: host.bounds, to: rep)27    try? rep.representation(using: .png, properties: [:])?.write(to: URL(fileURLWithPath: path))28    return host.bounds.size29}3031@MainActor @Test func snapshotOfEveryBlockType() throws {32    let out = ProcessInfo.processInfo.environment["SPNOTES_SNAPSHOT_DIR"] ?? NSTemporaryDirectory()33    let doc = try NoteDocument(json: Data(rich.utf8))34    let size = renderEditor(doc, to: out + "/editor-light.png")35    _ = renderEditor(doc, dark: true, to: out + "/editor-dark.png")36    #expect(size.height > 400)37}38
Files main
Menu

Theme