Repository Public
1// ORIX Docs2// File: UITests/EditorUITests.swift3// Description: End-to-end tests that drive the real application through the accessibility interface: typing, editing keys, undo and redo,4// formatting, tables, notes, review, modes, saving and reopening, and recovery after the process is killed.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import XCTest910final class EditorUITests: XCTestCase {11 var app: XCUIApplication!1213 override func setUpWithError() throws {14 continueAfterFailure = false15 app = XCUIApplication()16 }1718 override func tearDownWithError() throws {19 if app.state != .notRunning { app.terminate() }20 }2122 func launch(_ arguments: [String] = ["-ORIXTemplate", "blank"]) {23 app.launchArguments = ["-ORIXUITest", "YES", "-ApplePersistenceIgnoreState", "YES", "-checkSpelling", "NO", "-AppleLanguages", "(en)"] + arguments24 app.launch()25 XCTAssertTrue(canvas.waitForExistence(timeout: 15), "the document canvas did not appear")26 canvas.click()27 }2829 var canvas: XCUIElement { app.descendants(matching: .any).matching(identifier: "document-canvas").firstMatch }30 var text: String { (canvas.value as? String) ?? "" }3132 func waitForText(_ predicate: @escaping (String) -> Bool, _ message: String, timeout: TimeInterval = 5) {33 let deadline = Date().addingTimeInterval(timeout)34 while Date() < deadline { if predicate(text) { return }; usleep(100_000) }35 XCTFail("\(message) — text is: \(text.prefix(300))")36 }3738 func testTypingReturnAndBackspace() {39 launch()40 app.typeText("Hello ORIX")41 waitForText({ $0.hasPrefix("Hello ORIX") }, "typed text missing")42 app.typeKey(.return, modifierFlags: [])43 app.typeText("Second line")44 waitForText({ $0.hasPrefix("Hello ORIX\nSecond line") }, "second paragraph missing")45 for _ in 0..<5 { app.typeKey(.delete, modifierFlags: []) }46 waitForText({ $0.hasPrefix("Hello ORIX\nSecond") && !$0.contains("line") }, "backspace did not delete")47 }4849 func testUnicodeTyping() {50 launch()51 app.typeText("Éte\u{0301} 👨👩👧 日本語")52 waitForText({ $0.contains("👨👩👧") && $0.contains("日本語") }, "unicode text missing")53 // Backspace removes the whole CJK character, then the next, never half of anything.54 app.typeKey(.delete, modifierFlags: [])55 waitForText({ $0.contains("日本") && !$0.contains("日本語") }, "one character should be gone")56 }5758 func testUndoRedoAndSelectAll() {59 launch()60 app.typeText("alpha")61 app.typeKey(.return, modifierFlags: [])62 app.typeText("beta")63 waitForText({ $0.hasPrefix("alpha\nbeta") }, "setup")64 app.typeKey("a", modifierFlags: .command)65 app.typeKey(.delete, modifierFlags: [])66 waitForText({ $0.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty }, "select all + delete should empty the document")67 app.typeKey("z", modifierFlags: .command)68 waitForText({ $0.hasPrefix("alpha\nbeta") }, "undo should restore the text")69 app.typeKey("z", modifierFlags: [.command, .shift])70 waitForText({ $0.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty }, "redo should delete again")71 }7273 func testArrowKeysAndWordDeletion() {74 launch()75 app.typeText("one two three")76 app.typeKey(.leftArrow, modifierFlags: .option)77 app.typeKey(.delete, modifierFlags: .option)78 waitForText({ $0.hasPrefix("one three") }, "option-delete should remove the previous word")79 app.typeKey(.leftArrow, modifierFlags: .command)80 app.typeText("zero ")81 waitForText({ $0.hasPrefix("zero one three") }, "command-left should go to the line start")82 app.typeKey(.rightArrow, modifierFlags: .command)83 app.typeText("!")84 waitForText({ $0.hasPrefix("zero one three!") }, "command-right should go to the line end")85 }8687 func testBoldShortcutTogglesToolbarState() {88 launch()89 app.typeText("plain ")90 app.typeKey("b", modifierFlags: .command)91 app.typeText("bold")92 let bold = app.buttons["cmd-bold"]93 XCTAssertTrue(bold.waitForExistence(timeout: 5))94 XCTAssertTrue(bold.isSelected, "the Bold button should be on while typing bold text")95 app.typeKey("b", modifierFlags: .command)96 XCTAssertFalse(app.buttons["cmd-bold"].isSelected)97 waitForText({ $0.hasPrefix("plain bold") }, "text")98 }99100 func testModesHideAndShowPanels() {101 launch(["-ORIXTemplate", "research-report"])102 app.buttons["mode-focus"].click()103 XCTAssertFalse(app.buttons["cmd-bold"].waitForExistence(timeout: 1.5), "Focus mode hides the format bar")104 app.typeKey(.escape, modifierFlags: [])105 XCTAssertTrue(app.buttons["cmd-bold"].waitForExistence(timeout: 5), "Escape leaves Focus mode")106 app.buttons["mode-review"].click()107 XCTAssertTrue(app.buttons["mode-review"].isSelected)108 }109110 func testTableMenuInsertsATableAndTabMovesBetweenCells() {111 launch()112 app.typeText("Before")113 app.typeKey(.return, modifierFlags: [])114 app.typeKey("t", modifierFlags: [.command, .option])115 let insert = app.sheets.buttons["Insert"]116 XCTAssertTrue(insert.waitForExistence(timeout: 5), "the insert-table sheet did not open")117 insert.click()118 canvas.click()119 app.menuBars.menuBarItems["Table"].click()120 XCTAssertTrue(app.menuItems["Add Row Below"].isEnabled, "the caret should be inside the new table")121 app.typeKey(.escape, modifierFlags: [])122 }123124 func testFootnoteAndTrackChanges() {125 launch()126 app.typeText("Claim")127 app.typeKey("f", modifierFlags: [.command, .option])128 app.typeText("Source of the claim")129 waitForText({ $0.contains("Source of the claim") }, "the caret should be in the footnote")130 app.typeKey(.escape, modifierFlags: [])131 waitForText({ $0.hasPrefix("Claim") }, "Escape returns to the body")132 app.typeKey("e", modifierFlags: [.command, .shift])133 app.typeText(" tracked")134 XCTAssertTrue(app.staticTexts["Tracking Changes"].waitForExistence(timeout: 5), "the tracking indicator should show")135 app.menuBars.menuBarItems["Review"].click()136 XCTAssertTrue(app.menuItems["Reject All Changes"].isEnabled)137 app.menuItems["Reject All Changes"].click()138 waitForText({ !$0.contains("tracked") }, "rejecting removes the tracked insertion")139 }140141 /// Creates a blank document file through the application itself and returns its location.142 func makeDocumentFile() -> URL {143 let url = FileManager.default.temporaryDirectory.appendingPathComponent("orix-uitest-\(UUID().uuidString).orixdoc")144 app.launchArguments = ["-ORIXUITest", "YES", "-ApplePersistenceIgnoreState", "YES", "-AppleLanguages", "(en)", "-ORIXTemplate", "blank", "-ORIXSaveTo", url.path]145 app.launch()146 let deadline = Date().addingTimeInterval(15)147 while Date() < deadline, !FileManager.default.fileExists(atPath: url.path) { usleep(200_000) }148 XCTAssertTrue(FileManager.default.fileExists(atPath: url.path), "the application did not write the document")149 sleep(1)150 app.terminate()151 return url152 }153154 func testSaveAndReopenKeepsTheText() throws {155 let url = makeDocumentFile()156 defer { try? FileManager.default.removeItem(at: url) }157 launch(["-ORIXOpen", url.path])158 app.typeText("Saved text 42")159 waitForText({ $0.contains("Saved text 42") }, "typing")160 app.typeKey("s", modifierFlags: .command)161 sleep(2)162 app.terminate()163 launch(["-ORIXOpen", url.path])164 waitForText({ $0.contains("Saved text 42") }, "the saved text should be there after reopening")165 }166167 func testAutosaveSurvivesAKilledProcess() throws {168 let url = makeDocumentFile()169 defer { try? FileManager.default.removeItem(at: url) }170 launch(["-ORIXOpen", url.path, "-ORIXAutosaveDelay", "2"])171 app.typeText("Unsaved but recoverable")172 waitForText({ $0.contains("Unsaved but recoverable") }, "typing")173 sleep(7) // autosave in place; no explicit save174 // Kill the process: it gets no chance to save.175 let pid = app.value(forKey: "processID") as? Int32 ?? 0176 XCTAssertTrue(pid > 0)177 kill(pid, SIGKILL)178 sleep(2)179 launch(["-ORIXOpen", url.path])180 waitForText({ $0.contains("Unsaved but recoverable") }, "autosaved text should be there after the process was killed", timeout: 8)181 }182}183