aboutsummaryrefslogtreecommitdiff
path: root/MapUITests/MapUITests.swift
blob: 540d928374798f4987edd51fde9b849ea151d99d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import XCTest

final class MapUITests: XCTestCase {

  override func setUpWithError() throws {
    continueAfterFailure = false
  }

  override func tearDownWithError() throws {
    Task { @MainActor in
      // Close any test documents we may have created to avoid leaving them open
      let app = XCUIApplication()
      if app.state == .runningForeground {
        // Close all windows except the first one (if any existed before tests)
        let windowCount = app.windows.count
        if windowCount > 1 {
          for _ in 1..<windowCount {
            app.typeKey("w", modifierFlags: .command)
          }
        }
      }
    }
  }

  @MainActor
  private func createNewDocument(app: XCUIApplication) {
    // Create a new document to ensure we start with a clean slate
    app.typeKey("n", modifierFlags: .command)

    // Wait for the new document window to appear
    let newWindow = app.windows.element(boundBy: app.windows.count - 1)
    XCTAssertTrue(newWindow.waitForExistence(timeout: 3.0))
  }

  @MainActor
  func testAppLaunchesSuccessfully() throws {
    let app = XCUIApplication()
    app.launch()

    // Create new document to avoid interfering with existing files
    createNewDocument(app: app)

    // Verify the app window appears
    XCTAssertTrue(app.windows.firstMatch.waitForExistence(timeout: 5.0))

    // Verify the text editor is present
    XCTAssertTrue(app.textViews.firstMatch.exists)

    // Verify the evolution picker is present
    XCTAssertTrue(app.popUpButtons.firstMatch.exists)
  }

  @MainActor
  func testMapEditorTextInput() throws {
    let app = XCUIApplication()
    app.launch()

    // Create new document to avoid interfering with existing files
    createNewDocument(app: app)

    let textView = app.textViews.firstMatch
    XCTAssertTrue(textView.waitForExistence(timeout: 5.0))

    // Clear existing text and input new map content
    textView.click()
    textView.typeKey("a", modifierFlags: .command)  // Select all
    textView.typeText("Node A (10, 20)\nNode B (30, 40)\nNode A -> Node B")

    // Verify the text was entered
    XCTAssertTrue(textView.value as? String != nil)
  }

  @MainActor
  func testEvolutionPickerFunctionality() throws {
    let app = XCUIApplication()
    app.launch()

    // Create new document to avoid interfering with existing files
    createNewDocument(app: app)

    // Look specifically for the evolution picker in the toolbar, not just any popup
    let toolbar = app.toolbars.firstMatch
    XCTAssertTrue(toolbar.waitForExistence(timeout: 5.0))

    let evolutionPicker = toolbar.popUpButtons.firstMatch
    XCTAssertTrue(evolutionPicker.waitForExistence(timeout: 5.0))

    // Store the original selection to restore it later
    let originalSelection = evolutionPicker.value as? String

    // Click the evolution picker
    evolutionPicker.click()

    // Verify dropdown menu appears with options
    let menu = app.menus.firstMatch
    XCTAssertTrue(menu.waitForExistence(timeout: 2.0))

    // Check that there are menu items
    XCTAssertTrue(menu.menuItems.count > 0)

    // Test selecting a different option temporarily
    if menu.menuItems["Practice"].exists {
      menu.menuItems["Practice"].click()

      // Verify the selection changed
      XCTAssertEqual(evolutionPicker.value as? String, "Practice")

      // Restore original selection if we had one and it's different
      if let original = originalSelection, original != "Practice" {
        evolutionPicker.click()
        let restoreMenu = app.menus.firstMatch
        if restoreMenu.waitForExistence(timeout: 2.0) && restoreMenu.menuItems[original].exists {
          restoreMenu.menuItems[original].click()
        }
      }
    } else {
      // If Practice doesn't exist, just close the menu
      app.typeKey(XCUIKeyboardKey.escape, modifierFlags: [])
    }
  }

  @MainActor
  func testZoomSliderFunctionality() throws {
    let app = XCUIApplication()
    app.launch()

    // Create new document to avoid interfering with existing files
    createNewDocument(app: app)

    // Find the zoom slider
    let slider = app.sliders.firstMatch
    XCTAssertTrue(slider.waitForExistence(timeout: 5.0))

    // Get initial value and verify slider exists
    XCTAssertTrue(slider.exists, "Slider should exist")

    // Adjust the slider
    slider.adjust(toNormalizedSliderPosition: 0.8)

    // For zoom sliders, we can verify the change by checking if the adjustment was successful
    // Since macOS sliders sometimes don't expose their values properly in UI tests,
    // we'll just verify that the slider can be interacted with
    XCTAssertTrue(slider.exists, "Slider should still exist after adjustment")
  }

  @MainActor
  func testMenuBarCommands() throws {
    let app = XCUIApplication()
    app.launch()

    // Create new document first to have a window available
    createNewDocument(app: app)

    // Test File menu
    let fileMenu = app.menuBars.menuBarItems["File"]
    if fileMenu.exists {
      fileMenu.click()

      // Look for New Document command
      let newDocumentItem = app.menus.menuItems["New Document"]
      if newDocumentItem.exists {
        XCTAssertTrue(newDocumentItem.isEnabled)
      }

      // Press Escape to close menu instead of clicking on window
      app.typeKey(XCUIKeyboardKey.escape, modifierFlags: [])
    }
  }

  @MainActor
  func testKeyboardShortcuts() throws {
    let app = XCUIApplication()
    app.launch()

    // Create new document to avoid interfering with existing files
    createNewDocument(app: app)

    let window = app.windows.firstMatch
    XCTAssertTrue(window.waitForExistence(timeout: 5.0))

    // Test zoom in shortcut (Cmd+Plus)
    window.typeKey("+", modifierFlags: .command)

    // Test zoom out shortcut (Cmd+Minus)
    window.typeKey("-", modifierFlags: .command)

    // Test search shortcut (Cmd+F)
    window.typeKey("f", modifierFlags: .command)

    // Check if search bar appears
    let searchField = app.textFields["Search"]
    if searchField.waitForExistence(timeout: 2.0) {
      XCTAssertTrue(searchField.exists)

      // Type in search field
      searchField.typeText("Node")

      // Press Escape to close search
      searchField.typeKey(XCUIKeyboardKey.escape, modifierFlags: [])
    }
  }

  @MainActor
  func testSplitViewResizing() throws {
    let app = XCUIApplication()
    app.launch()

    // Create new document to avoid interfering with existing files
    createNewDocument(app: app)

    let window = app.windows.firstMatch
    XCTAssertTrue(window.waitForExistence(timeout: 5.0))

    // Find split view divider
    let splitViews = app.splitGroups
    if splitViews.count > 0 {
      let splitView = splitViews.firstMatch
      XCTAssertTrue(splitView.exists)

      // Test that we can interact with the split view
      // Note: Actual dragging of split view dividers is complex in UI tests
      // This test mainly verifies the split view structure exists
    }
  }

  @MainActor
  func testDocumentSaveLoad() throws {
    let app = XCUIApplication()
    app.launch()

    // Create new document to avoid interfering with existing files
    createNewDocument(app: app)

    let textView = app.textViews.firstMatch
    XCTAssertTrue(textView.waitForExistence(timeout: 5.0))

    // Enter test content
    textView.click()
    textView.typeKey("a", modifierFlags: .command)
    textView.typeText("Test Map Content\nNode A (10, 20)")

    // Test Save command (Cmd+S) - this will open save dialog for new document
    app.typeKey("s", modifierFlags: .command)

    // If save dialog appears, dismiss it to avoid saving test files
    let saveDialog = app.sheets.firstMatch
    if saveDialog.waitForExistence(timeout: 2.0) {
      let cancelButton = saveDialog.buttons["Cancel"]
      if cancelButton.exists {
        cancelButton.click()
      }
    }
  }

  @MainActor
  func testMapRenderingArea() throws {
    let app = XCUIApplication()
    app.launch()

    // Create new document to avoid interfering with existing files
    createNewDocument(app: app)

    // Add some map content
    let textView = app.textViews.firstMatch
    XCTAssertTrue(textView.waitForExistence(timeout: 5.0))

    textView.click()
    textView.typeKey("a", modifierFlags: .command)
    textView.typeText("Node A (10, 20)\nNode B (30, 40)\nNode A -> Node B")

    // Verify the map rendering area exists
    let scrollViews = app.scrollViews
    XCTAssertTrue(scrollViews.count > 0)

    // The map rendering should be in a scroll view
    let mapScrollView = scrollViews.element(boundBy: scrollViews.count - 1)
    XCTAssertTrue(mapScrollView.exists)
  }

  @MainActor
  func testLaunchPerformance() throws {
    if #available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 7.0, *) {
      measure(metrics: [XCTApplicationLaunchMetric()]) {
        XCUIApplication().launch()
      }
    }
  }

}