aboutsummaryrefslogtreecommitdiff
path: root/MapUITests/SearchUITests.swift
blob: 189167856e3323b9963bd56368179fee8524b42b (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
// Copyright (C) 2024 Rubén Beltrán del Río

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see https://map.tranquil.systems.
import XCTest

final class SearchUITests: 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 testSearchBarAppearance() 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))

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

    // Verify search bar appears (SearchBar uses a TextField with placeholder "Search")
    let searchField = app.textFields["Search"]
    XCTAssertTrue(searchField.waitForExistence(timeout: 2.0))

    // Verify search navigation buttons appear - the search bar should contain multiple buttons
    // We'll check for the Done button which should be most reliable
    let doneButton = app.buttons["Done"]
    XCTAssertTrue(doneButton.waitForExistence(timeout: 2.0))
  }

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

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

    // Add test content with searchable terms
    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 C (50, 60)\nNode A -> Node B")

    // Open search
    app.typeKey("f", modifierFlags: .command)

    let searchField = app.textFields["Search"]
    XCTAssertTrue(searchField.waitForExistence(timeout: 2.0))

    // Search for "Node"
    searchField.typeText("Node")

    // Verify search results are highlighted (this is visual, hard to test directly)
    // We can at least verify the search field contains our text
    XCTAssertEqual(searchField.value as? String, "Node")
  }

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

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

    // Add test content with multiple instances of search term
    let textView = app.textViews.firstMatch
    XCTAssertTrue(textView.waitForExistence(timeout: 5.0))

    textView.click()
    textView.typeKey("a", modifierFlags: .command)
    textView.typeText("Test Test Test\nAnother Test line\nFinal Test here")

    // Open search
    app.typeKey("f", modifierFlags: .command)

    let searchField = app.textFields["Search"]
    XCTAssertTrue(searchField.waitForExistence(timeout: 2.0))

    // Search for "Test"
    searchField.typeText("Test")

    // Try navigation with chevron buttons
    let nextButton = app.buttons.containing(.image, identifier: "chevron.right").firstMatch
    let previousButton = app.buttons.containing(.image, identifier: "chevron.left").firstMatch

    if nextButton.waitForExistence(timeout: 2.0) {
      nextButton.click()
    }

    if previousButton.waitForExistence(timeout: 2.0) {
      previousButton.click()
    }
  }

  @MainActor
  func testSearchDismissal() 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))

    // Open search
    window.typeKey("f", modifierFlags: .command)

    let searchField = app.textFields["Search"]
    XCTAssertTrue(searchField.waitForExistence(timeout: 2.0))

    // Type something
    searchField.typeText("test")

    // Dismiss with Escape
    searchField.typeKey(XCUIKeyboardKey.escape, modifierFlags: [])

    // Verify search bar is dismissed
    XCTAssertFalse(searchField.waitForExistence(timeout: 1.0))
  }

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

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

    // Add test 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)")

    // Open search
    app.typeKey("f", modifierFlags: .command)

    let searchField = app.textFields["Search"]
    XCTAssertTrue(searchField.waitForExistence(timeout: 2.0))

    // Search for something that doesn't exist
    searchField.typeText("XYZ123")

    // Verify search field still exists and contains our search term
    XCTAssertEqual(searchField.value as? String, "XYZ123")
  }

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

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

    // Add test content with mixed case
    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 C (50, 60)")

    // Open search
    app.typeKey("f", modifierFlags: .command)

    let searchField = app.textFields["Search"]
    XCTAssertTrue(searchField.waitForExistence(timeout: 2.0))

    // Search for "node" (lowercase)
    searchField.typeText("node")

    // The search should be case-insensitive based on the implementation
    XCTAssertEqual(searchField.value as? String, "node")
  }

}