oficina / sheets Public

Clone
git clone https://amadan.net/oficina/sheets

Plain git — no account needed to clone.

Download

Download this file

1import XCTest
2
3@MainActor final class NativeUITests: XCTestCase {
4 func testCreateAndEditFormula() {
5 continueAfterFailure = false
6 let app = XCUIApplication()
7 app.launch()
8 defer { app.terminate() }
9 let create = app.buttons["New spreadsheet"]
10 XCTAssertTrue(create.waitForExistence(timeout: 15))
11 create.tap()
12 let formula = app.textFields["Cell contents or formula"]
13 XCTAssertTrue(formula.waitForExistence(timeout: 10), "Creating a sheet must navigate to its native editor")
14 formula.tap()
15 formula.typeText("=2+3")
16 app.buttons["Save"].tap()
17 XCTAssertTrue(app.buttons["A1, 5"].waitForExistence(timeout: 10), "The server-calculated value must appear")
18 let saved = NSPredicate(format: "exists == false")
19 XCTAssertEqual(XCTWaiter.wait(for: [XCTNSPredicateExpectation(predicate: saved, object: app.buttons["Save"])], timeout: 5), .completed)
20 app.buttons["A1, 5"].tap()
21 let rawFormula = NSPredicate(format: "value == %@", "=2+3")
22 XCTAssertEqual(XCTWaiter.wait(for: [XCTNSPredicateExpectation(predicate: rawFormula, object: formula)], timeout: 5), .completed, "Selecting a cell must expose its formula rather than its display value")
23 formula.coordinate(withNormalizedOffset: CGVector(dx: 0.95, dy: 0.5)).tap()
24 formula.typeText(String(repeating: XCUIKeyboardKey.delete.rawValue, count: 4))
25 formula.typeText("=7*6")
26 app.buttons["Save"].tap()
27 XCTAssertTrue(app.buttons["A1, 42"].waitForExistence(timeout: 10))
28 XCTAssertEqual(XCTWaiter.wait(for: [XCTNSPredicateExpectation(predicate: saved, object: app.buttons["Save"])], timeout: 5), .completed)
29 app.buttons["A1, 42"].tap()
30 let finalFormula = NSPredicate(format: "value == %@", "=7*6")
31 XCTAssertEqual(XCTWaiter.wait(for: [XCTNSPredicateExpectation(predicate: finalFormula, object: formula)], timeout: 5), .completed)
32
33 // Exercise commands through the actual native menus, preserving A1 as the
34 // fixture's independent server-persistence sentinel.
35 menu(app, "Edit", command: "Fill down")
36 XCTAssertTrue(app.buttons["A2, 42"].waitForExistence(timeout: 10))
37 select(app, "A1:A2")
38 menu(app, "Edit", command: "Copy")
39 select(app, "B1")
40 menu(app, "Edit", command: "Paste")
41 XCTAssertTrue(app.buttons["B1, 42"].waitForExistence(timeout: 10))
42 XCTAssertTrue(app.buttons["B2, 42"].waitForExistence(timeout: 10))
43 select(app, "B1:B2")
44 openMenu(app, "Format")
45 app.buttons["Number format"].tap()
46 app.buttons["Number"].tap()
47 XCTAssertTrue(app.buttons["B1, 42.00"].waitForExistence(timeout: 10))
48 XCTAssertTrue(app.buttons["B2, 42.00"].waitForExistence(timeout: 10))
49 menu(app, "Edit", command: "Undo my last value edit")
50 XCTAssertTrue(app.buttons["B2, Empty"].waitForExistence(timeout: 10))
51 select(app, "B2")
52 XCTAssertEqual(XCTWaiter.wait(for: [XCTNSPredicateExpectation(predicate: NSPredicate(format: "value == %@ OR value == %@", "", "Cell contents or formula"), object: formula)], timeout: 10), .completed)
53 menu(app, "File", command: "Version history")
54 let restore = app.buttons["Restore before this edit"].firstMatch
55 XCTAssertTrue(restore.waitForExistence(timeout: 10))
56 restore.tap()
57 app.buttons["Done"].tap()
58 // Canonical value undo clears the sparse cell row, including its format;
59 // restoring the input therefore returns the default number display.
60 XCTAssertTrue(app.buttons["B2, 42"].waitForExistence(timeout: 10), "History Restore must restore the pasted formula")
61 select(app, "A1")
62 let screenshot = XCTAttachment(screenshot: app.screenshot())
63 screenshot.name = "Sheets native formula editing"
64 screenshot.lifetime = .keepAlways
65 add(screenshot)
66 }
67
68 private func select(_ app: XCUIApplication, _ reference: String) {
69 let field = app.textFields["Cell or range, for example A1:B5"]
70 XCTAssertTrue(field.waitForExistence(timeout: 5))
71 field.coordinate(withNormalizedOffset: CGVector(dx: 0.95, dy: 0.5)).tap()
72 field.typeText(String(repeating: XCUIKeyboardKey.delete.rawValue, count: (field.value as? String ?? "").count))
73 field.typeText(reference + "\n")
74 }
75
76 // Edits are optimistic (2026-11-26): a refused one keeps its value on
77 // its cell, marked, offers Retry, and blocks nothing else.
78 func testFailedCellEditCanBeRetriedWithoutLosingDraft() {
79 continueAfterFailure = false
80 let app = XCUIApplication()
81 app.launch()
82 defer { app.terminate() }
83 XCTAssertTrue(app.buttons["New spreadsheet"].waitForExistence(timeout: 15))
84 app.buttons["New spreadsheet"].tap()
85 let formula = app.textFields["Cell contents or formula"]
86 XCTAssertTrue(formula.waitForExistence(timeout: 10))
87 formula.tap()
88 // Only this exact synthetic input triggers one refusal in the fixture.
89 formula.typeText("=6*7")
90 app.buttons["Save"].tap()
91 // "Retry" reads "Try again" in English (Mac.strings).
92 let retry = app.buttons["Try again"]
93 XCTAssertTrue(retry.waitForExistence(timeout: 10))
94 XCTAssertTrue(app.buttons["A1, =6*7"].exists, "the refused value stays on its cell")
95 XCTAssertTrue(formula.isEnabled, "a refused edit must not lock the formula bar")
96 app.buttons["A2, Empty"].tap()
97 XCTAssertEqual(app.textFields["Cell or range, for example A1:B5"].value as? String, "A2")
98 retry.tap()
99 XCTAssertTrue(app.buttons["A1, 42"].waitForExistence(timeout: 10))
100 XCTAssertEqual(XCTWaiter.wait(for: [XCTNSPredicateExpectation(predicate: NSPredicate(format: "exists == false"), object: retry)], timeout: 10), .completed)
101 let screenshot = XCTAttachment(screenshot: app.screenshot())
102 screenshot.name = "Sheets native retried edit"; screenshot.lifetime = .keepAlways; add(screenshot)
103 }
104
105 func testExportWorkbookOpensNativeDestinationAndCancels() {
106 continueAfterFailure = false
107 let app = XCUIApplication()
108 app.launch()
109 defer { app.terminate() }
110 let workbook = app.buttons["Native review workbook"]
111 XCTAssertTrue(workbook.waitForExistence(timeout: 15))
112 workbook.tap()
113 XCTAssertTrue(app.textFields["Cell contents or formula"].waitForExistence(timeout: 10))
114 menu(app, "File", command: "Export workbook")
115 let export = app.buttons["Save workbook…"]
116 XCTAssertTrue(export.waitForExistence(timeout: 10))
117 export.tap()
118 let picker = app.otherElements["Browse View (Picker)"]
119 XCTAssertTrue(picker.waitForExistence(timeout: 15), "A native file destination picker must open after download")
120 let screenshot = XCTAttachment(screenshot: app.screenshot())
121 screenshot.name = "Sheets native export destination"
122 screenshot.lifetime = .keepAlways
123 add(screenshot)
124 cancelDestinationPicker(picker)
125 XCTAssertTrue(export.waitForExistence(timeout: 10))
126 app.buttons["Done"].tap()
127 }
128
129 // The system destination picker has no single "Cancel" on iOS 27
130 // (2026-11-26). On iPad the sidebar and the file browser each have one, so
131 // an unscoped query matches two. On iPhone the picker opens inside a
132 // location ("On My iPhone") with a back button, and Cancel appears only at
133 // the browse root. So: use the browser's own bar, and go back to the root
134 // first if there's no Cancel there yet.
135 private func cancelDestinationPicker(_ picker: XCUIElement) {
136 let bar = picker.navigationBars.matching(identifier: "FullDocumentManagerViewControllerNavigationBar")
137 let cancel = bar.buttons["Cancel"].firstMatch
138 let back = bar.buttons["BackButton"].firstMatch
139 var steps = 0
140 while !cancel.waitForExistence(timeout: 2), back.exists, steps < 3 {
141 back.tap()
142 steps += 1
143 }
144 XCTAssertTrue(cancel.waitForExistence(timeout: 5), "The destination picker must offer Cancel")
145 cancel.tap()
146 }
147
148 private func openMenu(_ app: XCUIApplication, _ name: String) {
149 if app.buttons["Sheet actions"].exists { app.buttons["Sheet actions"].tap() }
150 let menu = app.buttons[name]
151 XCTAssertTrue(menu.waitForExistence(timeout: 5), "Missing native menu: \(name)")
152 menu.tap()
153 }
154
155 private func menu(_ app: XCUIApplication, _ name: String, command: String) {
156 openMenu(app, name)
157 let action = app.buttons[command]
158 XCTAssertTrue(action.waitForExistence(timeout: 5), "Missing native command: \(command)")
159 action.tap()
160 }
161}
162