SPB Git

Repository Public

EditorRenderingTests.swift 5.1 KB 96 lines swift

1// SP Notes iOS2// File: Tests/EditorRenderingTests.swift3// Description: Renders the editor with a document covering every block to PNG files (light and dark) so the drawing of markers,4//              boxes, atoms and marks can be inspected; asserts the layout produced content.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Testing9import UIKit10import SPNotesCore11@testable import SPNotes1213@MainActor14@Suite("Rendu de l’éditeur") struct EditorRenderingTests {15    @Test(arguments: [UIUserInterfaceStyle.light, .dark])16    func rendersEveryBlock(style: UIUserInterfaceStyle) throws {17        let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 393, height: 1500))18        window.overrideUserInterfaceStyle = style19        let host = UIViewController()20        window.rootViewController = host21        window.makeKeyAndVisible()22        host.view.backgroundColor = UIColor(named: "Background")23        let editor = EditorController()24        editor.textView.frame = host.view.bounds25        editor.textView.isScrollEnabled = false26        host.view.addSubview(editor.textView)27        editor.load(try NoteDocument(json: Data(sampleDocumentJSON.utf8)))28        editor.relayout(force: true)29        host.view.layoutIfNeeded()30        editor.textView.layoutIfNeeded()31        RunLoop.main.run(until: Date().addingTimeInterval(0.3))32        let size = editor.textView.sizeThatFits(CGSize(width: 393, height: CGFloat.greatestFiniteMagnitude))33        #expect(size.height > 600)34        let image = UIGraphicsImageRenderer(bounds: host.view.bounds).image { _ in host.view.drawHierarchy(in: host.view.bounds, afterScreenUpdates: true) }35        let url = URL(fileURLWithPath: "/tmp/spnotes-editor-\(style == .dark ? "dark" : "light").png")36        try image.pngData()?.write(to: url)37        window.isHidden = true38    }3940    /// 2.0: the seeded note « Blocs 3.0 » (every block of SPEC §2, with its files on the device) drawn like the app shows it.41    /// Written to /tmp/spnotes-blocs-<light|dark|a11y>.png to be looked at.42    @Test(.serialized, arguments: ["light", "dark", "a11y"])43    func rendersTheBlocksNote(variant: String) async throws {44        let app = try makeApp()45        #expect(await SampleData.seed(into: app))46        let page = try #require(try await app.store.page(id: SampleData.blocksNoteID))47        let document = try NoteDocument(json: page.content)48        #expect(Set(NodeType.nextBlocks).isSubset(of: Set(document.lines.map(\.type))))49        #expect(document.lines.contains { $0.type == "blocFutur" } && document.citedReferences().count == 2)5051        let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 393, height: 852))52        window.windowScene = UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }.first53        window.overrideUserInterfaceStyle = variant == "dark" ? .dark : .light54        if variant == "a11y" { window.traitOverrides.preferredContentSizeCategory = .accessibilityLarge }55        let host = UIViewController()56        window.rootViewController = host57        window.makeKeyAndVisible()58        host.overrideUserInterfaceStyle = window.overrideUserInterfaceStyle59        host.view.backgroundColor = UIColor(named: "Background")60        let model = NoteEditorModel(noteId: SampleData.blocksNoteID, app: app)61        let editor = model.controller62        editor.textView.frame = host.view.bounds63        editor.textView.contentInsetAdjustmentBehavior = .never64        host.view.addSubview(editor.textView)65        await model.load()66        await model.cardResolver.settle()67        editor.relayout(force: true)68        await model.cardResolver.settle()69        host.view.layoutIfNeeded()70        editor.textView.layoutIfNeeded()71        try await Task.sleep(for: .milliseconds(200))72        let size = editor.textView.sizeThatFits(CGSize(width: 393, height: CGFloat.greatestFiniteMagnitude))73        #expect(size.height > 2000)74        #expect(editor.textView.traitCollection.userInterfaceStyle == (variant == "dark" ? .dark : .light))75        // one picture per screen of the note, top to bottom76        var offset: CGFloat = 0, index = 077        while offset < size.height, index < 14 {78            editor.textView.setContentOffset(CGPoint(x: 0, y: offset), animated: false)79            editor.textView.layoutIfNeeded()80            try await Task.sleep(for: .milliseconds(60))81            let image = UIGraphicsImageRenderer(bounds: host.view.bounds).image { _ in host.view.drawHierarchy(in: host.view.bounds, afterScreenUpdates: true) }82            try image.pngData()?.write(to: URL(fileURLWithPath: "/tmp/spnotes-blocs-\(variant)-\(index).png"))83            offset += 80084            index += 185        }86        // every block atom has its bitmap, and loading + resolving the cards left the note clean87        var missing = 088        editor.storage.enumerateAttribute(.attachment, in: NSRange(location: 0, length: editor.storage.length)) { value, _, _ in89            if let atom = value as? AtomAttachment, atom.image == nil || atom.bounds.height < 10 { missing += 1 }90        }91        #expect(missing == 0)92        #expect(model.saveState == .saved)93        window.isHidden = true94    }95}96
Files main
Menu

Theme