diff options
| author | Ruben Beltran del Rio <git@r.bdr.sh> | 2025-07-04 17:06:28 +0200 |
|---|---|---|
| committer | Ruben Beltran del Rio <git@r.bdr.sh> | 2025-07-04 17:06:28 +0200 |
| commit | c843d34f56c207abcf4b93e424125eea2dc601e0 (patch) | |
| tree | 415dc2d9be4be31e290e1dd1dedb0b630c5e8fc6 /MapUITests | |
| parent | ed10ac191df473c92c4fec495aafa7f569d108c8 (diff) | |
Upgrade to Swift 6
Diffstat (limited to 'MapUITests')
| -rw-r--r-- | MapUITests/MapUITests.swift | 273 | ||||
| -rw-r--r-- | MapUITests/SearchUITests.swift | 226 |
2 files changed, 489 insertions, 10 deletions
diff --git a/MapUITests/MapUITests.swift b/MapUITests/MapUITests.swift index 2f6c35d..540d928 100644 --- a/MapUITests/MapUITests.swift +++ b/MapUITests/MapUITests.swift @@ -3,34 +3,287 @@ import XCTest final class MapUITests: XCTestCase { override func setUpWithError() throws { - // Put setup code here. This method is called before the invocation of each test method in the class. - - // In UI tests it is usually best to stop immediately when a failure occurs. continueAfterFailure = false - - // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this. } override func tearDownWithError() throws { - // Put teardown code here. This method is called after the invocation of each test method in the class. + 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 testExample() throws { - // UI tests must launch the application that they test. + func testAppLaunchesSuccessfully() throws { let app = XCUIApplication() app.launch() - // Use XCTAssert and related functions to verify your tests produce the correct results. + // 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, *) { - // This measures how long it takes to launch your application. measure(metrics: [XCTApplicationLaunchMetric()]) { XCUIApplication().launch() } } } + } diff --git a/MapUITests/SearchUITests.swift b/MapUITests/SearchUITests.swift new file mode 100644 index 0000000..1891678 --- /dev/null +++ b/MapUITests/SearchUITests.swift @@ -0,0 +1,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") + } + +} |