Repository Public
1// SP Notes iOS2// File: UITests/EditorUITests.swift3// Description: The note screen driven like a user, with the software keyboard: title and text, bold and checklist from the4// formatting bar, a table from the "+" menu, a long note (the caret stays above the bar), the "…" menu, history,5// statistics, the assistant sheet, then leaving and reopening the note. Screenshots are kept as attachments.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import XCTest1011final class EditorUITests: XCTestCase {12 private var app: XCUIApplication!13 private var padLeft = false14 private var isPad: Bool { UIDevice.current.userInterfaceIdiom == .pad }1516 override func setUp() {17 continueAfterFailure = false18 XCUIDevice.shared.orientation = isPad ? .landscapeLeft : .portrait19 }2021 override func tearDown() {22 XCUIDevice.shared.orientation = .portrait23 }2425 private func launch(appearance: String = "light") {26 app = XCUIApplication()27 app.launchEnvironment = ["SPNOTES_STORE": "memory", "SPNOTES_SESSION": "local", "SPNOTES_SEED": "1"]28 app.launchArguments = ["-appearance", appearance, "-lock.enabled", "NO", "-AppleLanguages", "(fr)", "-AppleLocale", "fr_CA"]29 app.launch()30 }3132 private func shot(_ name: String) {33 let attachment = XCTAttachment(screenshot: XCUIScreen.main.screenshot())34 attachment.name = "\(isPad ? "ipad" : "iphone")-editor-\(name)"35 attachment.lifetime = .keepAlways36 add(attachment)37 }3839 private func element(_ id: String) -> XCUIElement { app.descendants(matching: .any).matching(identifier: id).firstMatch }4041 /// Opens a new note from the shell. The shell belongs to another module: only its `new-note` control is assumed.42 private func openNewNote() throws {43 let newNote = element("new-note")44 try XCTSkipUnless(newNote.waitForExistence(timeout: 25), "the shell does not offer `new-note` yet: run again once the home screen exists")45 newNote.tap()46 XCTAssertTrue(element("note-editor").waitForExistence(timeout: 10), "the note screen shows the editor")47 }4849 private var editor: XCUIElement { element("note-editor") }50 private var titleField: XCUIElement { element("note-title") }5152 private func tapToolbar(_ id: String) {53 let button = element(id)54 XCTAssertTrue(button.waitForExistence(timeout: 5), "\(id) is in the formatting bar")55 if !button.isHittable { element("editor-toolbar").swipeLeft() }56 button.tap()57 }5859 private func leaveNote() {60 if isPad {61 // Three columns: another note of the list takes the place of this one.62 let other = app.staticTexts["Bienvenue dans SP Notes"].firstMatch63 if other.waitForExistence(timeout: 3) { other.tap(); padLeft = true }64 return65 }66 let back = app.navigationBars.buttons.element(boundBy: 0)67 XCTAssertTrue(back.waitForExistence(timeout: 5))68 back.tap()69 }7071 // MARK: writing, formatting, persistence7273 func testWriteFormatAndReopen() throws {74 launch()75 try openNewNote()7677 // Title, then Return moves to the text.78 XCTAssertTrue(titleField.waitForExistence(timeout: 5))79 titleField.tap()80 titleField.typeText("Réunion de projet\n")81 editor.typeText("Compte rendu de la rencontre ")82 XCTAssertTrue(element("editor-toolbar").waitForExistence(timeout: 5), "the formatting bar sits on the keyboard")83 tapToolbar("toolbar-bold")84 editor.typeText("important")85 tapToolbar("toolbar-bold")86 editor.typeText(" du matin.\n")87 tapToolbar("toolbar-checklist")88 editor.typeText("Envoyer le résumé\nRéserver la salle\n\n")89 shot("01-keyboard")9091 // A table from the "+" menu.92 tapToolbar("toolbar-plus.circle.fill")93 let table = app.buttons["Tableau"]94 XCTAssertTrue(table.waitForExistence(timeout: 5))95 shot("02-insert-menu")96 table.tap()97 XCTAssertTrue(editor.waitForExistence(timeout: 5))9899 // A long note: the caret line stays visible above the formatting bar while typing at the bottom.100 editor.typeText((1...14).map { "Point numéro \($0) de la discussion" }.joined(separator: "\n"))101 editor.typeText("\nDernière ligne tapée")102 shot("03-long-note-caret")103 let value = (editor.value as? String) ?? ""104 XCTAssertTrue(value.contains("Compte rendu de la rencontre important du matin."), "typed text is in the editor")105 XCTAssertTrue(value.contains("Dernière ligne tapée"))106107 // The "…" menu.108 let menu = element("note-menu")109 XCTAssertTrue(menu.waitForExistence(timeout: 5))110 menu.tap()111 XCTAssertTrue(element("menu-history").waitForExistence(timeout: 5))112 XCTAssertTrue(app.buttons["Exporter…"].exists)113 XCTAssertTrue(app.buttons["Rechercher dans la note"].exists)114 shot("04-menu")115116 // History: the restore point taken at the opening is listed on this device; the server section is honest offline.117 element("menu-history").tap()118 XCTAssertTrue(element("history-sheet").waitForExistence(timeout: 5))119 XCTAssertTrue(app.staticTexts["Sur cet appareil"].waitForExistence(timeout: 5) || app.staticTexts["SUR CET APPAREIL"].exists)120 shot("05-history")121 if element("history-local-row").waitForExistence(timeout: 3) {122 element("history-local-row").tap()123 XCTAssertTrue(element("history-restore").waitForExistence(timeout: 5))124 shot("06-history-version")125 app.navigationBars["Version"].buttons.element(boundBy: 0).tap()126 }127 XCTAssertTrue(element("history-close").waitForExistence(timeout: 5))128 element("history-close").tap()129130 // Statistics.131 menu.tap()132 element("menu-statistics").tap()133 XCTAssertTrue(element("statistics-sheet").waitForExistence(timeout: 5))134 shot("07-statistics")135 element("statistics-close").tap()136137 // Reminders sheet (nothing is saved here: the permission prompt belongs to the first saved reminder).138 menu.tap()139 element("menu-reminders").tap()140 XCTAssertTrue(element("reminders-sheet").waitForExistence(timeout: 5))141 shot("08-reminders")142 element("reminders-close").tap()143144 // Leave and reopen: the text is still there.145 leaveNote()146 // 2.0 tab structure: « Aujourd’hui » lists tasks first (lazy rows), the full list is under « Notes »147 if !isPad, app.tabBars.buttons["Notes"].waitForExistence(timeout: 5) { app.tabBars.buttons["Notes"].tap() }148 let row = app.staticTexts["Réunion de projet"].firstMatch149 XCTAssertTrue(row.waitForExistence(timeout: 8), "the note is listed under its title")150 if !isPad || padLeft {151 row.tap()152 XCTAssertTrue(editor.waitForExistence(timeout: 8))153 }154 let reopened = (editor.value as? String) ?? ""155 XCTAssertTrue(reopened.contains("Compte rendu de la rencontre important du matin."), "the text survived leaving the note")156 XCTAssertTrue(reopened.contains("Envoyer le résumé"))157 XCTAssertTrue(reopened.contains("Dernière ligne tapée"))158 XCTAssertEqual(titleField.value as? String, "Réunion de projet")159 shot("09-reopened")160 }161162 // MARK: assistant163164 func testAssistantSheet() throws {165 launch()166 try openNewNote()167 titleField.tap()168 titleField.typeText("Idées\n")169 editor.typeText("Trois pistes pour le projet : simplifier, mesurer, documenter.")170 tapToolbar("toolbar-sparkles")171 XCTAssertTrue(element("assistant-sheet").waitForExistence(timeout: 5))172 XCTAssertTrue(element("assistant-scope").waitForExistence(timeout: 5), "the sheet says what it works on")173 XCTAssertTrue(element("assistant-action-summarize").exists)174 shot("10-assistant")175176 // Without a session the assistant explains itself and the note is untouched.177 element("assistant-action-summarize").tap()178 XCTAssertTrue(element("assistant-failure").waitForExistence(timeout: 5))179 shot("11-assistant-unavailable")180 element("assistant-back").tap()181182 app.buttons["Questions"].tap()183 XCTAssertTrue(element("assistant-chat-field").waitForExistence(timeout: 5))184 shot("12-assistant-questions")185 element("assistant-close").tap()186 XCTAssertTrue(element("assistant-sheet").waitForNonExistence(timeout: 5))187 XCTAssertEqual((editor.value as? String)?.contains("Trois pistes pour le projet"), true, "nothing was written by the assistant")188 }189190 // MARK: dark appearance191192 func testDarkAppearance() throws {193 launch(appearance: "dark")194 try openNewNote()195 titleField.tap()196 titleField.typeText("Note sombre\n")197 editor.typeText("Un texte lisible sur fond sombre.\n")198 tapToolbar("toolbar-checklist")199 editor.typeText("Vérifier le contraste")200 shot("20-dark-keyboard")201 element("note-menu").tap()202 XCTAssertTrue(element("menu-history").waitForExistence(timeout: 5))203 shot("21-dark-menu")204 element("menu-attachments").tap()205 XCTAssertTrue(element("attachments-sheet").waitForExistence(timeout: 5))206 shot("22-dark-attachments")207 element("attachments-close").tap()208 }209}210