SPB Git

Repository Public

EditorBlocksUITests.swift 6.2 KB 124 lines swift

1// SP Notes iOS2// File: UITests/EditorBlocksUITests.swift3// Description: The 2.0 editor seen like a user (iPhone): the seeded note « Blocs 3.0 » with a card for every block, two block4//              sheets opened by a tap, the slash menu, and the formatting bar morphing with the block of the caret.5//              Screenshots are kept as attachments and written to /tmp/spnote-next/shots-ios/editor-*.png.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import XCTest1011final class EditorBlocksUITests: XCTestCase {12    private var app: XCUIApplication!13    private var appearance = "light"14    /// Same value as `SampleData.blocksNoteID` in the app.15    private let blocksNoteID = "5eed0000-0000-4000-8000-000000000020"1617    override func setUp() {18        continueAfterFailure = false19        XCUIDevice.shared.orientation = .portrait20    }2122    private func launch(appearance: String, open: String? = nil) {23        self.appearance = appearance24        app = XCUIApplication()25        var environment = ["SPNOTES_STORE": "memory", "SPNOTES_SESSION": "local", "SPNOTES_SEED": "1"]26        if let open { environment["SPNOTES_OPEN"] = open }27        app.launchEnvironment = environment28        app.launchArguments = ["-appearance", appearance, "-lock.enabled", "NO", "-AppleLanguages", "(fr)", "-AppleLocale", "fr_CA"]29        app.launch()30    }3132    private func shot(_ name: String) {33        let screenshot = XCUIScreen.main.screenshot()34        let attachment = XCTAttachment(screenshot: screenshot)35        attachment.name = "editor-\(name)-iphone-\(appearance)"36        attachment.lifetime = .keepAlways37        add(attachment)38        let directory = URL(fileURLWithPath: "/tmp/spnote-next/shots-ios", isDirectory: true)39        try? FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)40        try? screenshot.pngRepresentation.write(to: directory.appendingPathComponent("editor-\(name)-iphone-\(appearance).png"))41    }4243    private func element(_ id: String) -> XCUIElement { app.descendants(matching: .any).matching(identifier: id).firstMatch }44    private var editor: XCUIElement { element("note-editor") }4546    private func blocksNote(_ appearance: String) throws {47        try XCTSkipIf(UIDevice.current.userInterfaceIdiom == .pad, "iPhone scenario")48        launch(appearance: appearance, open: "note/\(blocksNoteID)")49        XCTAssertTrue(editor.waitForExistence(timeout: 40), "the note « Blocs 3.0 » opens")50        XCTAssertTrue(app.staticTexts["Blocs 3.0"].firstMatch.waitForExistence(timeout: 5) || element("note-title").waitForExistence(timeout: 5))51        sleep(2) // card data resolves (thumbnails, referenced block)52        shot("blocs-1")53        // a tap on the first card (the Mermaid diagram) opens its sheet54        editor.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.45)).tap()55        XCTAssertTrue(element("block-sheet").waitForExistence(timeout: 8), "the diagram sheet opens on a tap")56        XCTAssertTrue(element("mermaid-code").waitForExistence(timeout: 5))57        shot("sheet-mermaid")58        element("block-sheet-cancel").tap()59        XCTAssertTrue(element("block-sheet").waitForNonExistence(timeout: 5))60        // down the note, one screen at a time61        for index in 2...6 {62            editor.swipeUp(velocity: .slow)63            usleep(700_000)64            shot("blocs-\(index)")65            if index == 2 {66                // the chart is on this screen: its sheet has the live Swift Charts preview and the data grid67                editor.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.35)).tap()68                if element("block-sheet").waitForExistence(timeout: 6) {69                    shot("sheet-2")70                    element("block-sheet-cancel").tap()71                    XCTAssertTrue(element("block-sheet").waitForNonExistence(timeout: 5))72                }73            }74        }75    }7677    func testBlocksNoteLight() throws { try blocksNote("light") }78    func testBlocksNoteDark() throws { try blocksNote("dark") }7980    private func slashAndMorph(_ appearance: String) throws {81        try XCTSkipIf(UIDevice.current.userInterfaceIdiom == .pad, "iPhone scenario")82        launch(appearance: appearance)83        let newNote = element("new-note")84        try XCTSkipUnless(newNote.waitForExistence(timeout: 40), "the shell does not offer `new-note`")85        newNote.tap()86        XCTAssertTrue(editor.waitForExistence(timeout: 10))87        let title = element("note-title")88        XCTAssertTrue(title.waitForExistence(timeout: 5))89        title.tap()90        title.typeText("Essai des blocs\n")91        XCTAssertTrue(element("editor-toolbar").waitForExistence(timeout: 8), "the formatting bar is above the keyboard")92        // text: marks, lists, quote, link93        editor.typeText("Un paragraphe")94        XCTAssertTrue(element("toolbar-bold").exists)95        shot("bar-text")96        // slash menu at the start of an empty paragraph97        editor.typeText("\n/")98        XCTAssertTrue(element("slash-menu").waitForExistence(timeout: 5), "typing / opens the menu")99        shot("slash")100        editor.typeText("tâ")101        XCTAssertTrue(element("slash-tasks").waitForExistence(timeout: 5), "the menu filters as you type")102        shot("slash-filter")103        element("slash-tasks").tap()104        XCTAssertTrue(element("slash-menu").waitForNonExistence(timeout: 5))105        // a task item: the bar now offers the due date and the priority106        editor.typeText("Relire le chapitre")107        XCTAssertTrue(element("toolbar-calendar").waitForExistence(timeout: 5), "the bar morphed for a task")108        XCTAssertTrue(element("toolbar-flag").exists)109        shot("bar-task")110        // Markdown shortcut: a heading111        editor.typeText("\n\n## Titre\n")112        // code block from the slash menu: language, no marks113        editor.typeText("/code")114        XCTAssertTrue(element("slash-code").waitForExistence(timeout: 5))115        element("slash-code").tap()116        editor.typeText("let total = 42 // somme")117        XCTAssertTrue(element("toolbar-bold").waitForNonExistence(timeout: 5), "no marks in a code block")118        shot("bar-code")119    }120121    func testSlashMenuAndMorphingBarLight() throws { try slashAndMorph("light") }122    func testSlashMenuAndMorphingBarDark() throws { try slashAndMorph("dark") }123}124
Files main
Menu

Theme