|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +""" |
| 4 | +GUI tests for the ParameterEditorWindow using PyAutoGUI. |
| 5 | +
|
| 6 | +This module contains automated GUI tests for the Tkinter-based parameter editor. |
| 7 | +Tests verify that the GUI initializes correctly and displays expected elements. |
| 8 | +
|
| 9 | +This file is part of ArduPilot Methodic Configurator. https://github.com/ArduPilot/MethodicConfigurator |
| 10 | +
|
| 11 | +SPDX-FileCopyrightText: 2024-2025 Amilcar do Carmo Lucas <amilcar.lucas@iav.de> |
| 12 | +
|
| 13 | +SPDX-License-Identifier: GPL-3.0-or-later |
| 14 | +""" |
| 15 | + |
| 16 | +import tkinter as tk |
| 17 | +from tkinter import ttk |
| 18 | + |
| 19 | +import pytest |
| 20 | + |
| 21 | +from ardupilot_methodic_configurator.configuration_manager import ConfigurationManager |
| 22 | +from ardupilot_methodic_configurator.frontend_tkinter_parameter_editor import ParameterEditorWindow, show_about_window |
| 23 | + |
| 24 | + |
| 25 | +class TestParameterEditorWindow: |
| 26 | + """Test cases for ParameterEditorWindow GUI initialization.""" |
| 27 | + |
| 28 | + def test_pyautogui_setup(self, gui_test_environment) -> None: |
| 29 | + """Test that PyAutoGUI is properly configured for testing.""" |
| 30 | + # The gui_test_environment fixture handles all the assertions |
| 31 | + |
| 32 | + def test_basic_gui_creation(self, test_config_manager: ConfigurationManager) -> None: |
| 33 | + """Test basic GUI creation without running mainloop.""" |
| 34 | + # Create window but intercept mainloop |
| 35 | + original_mainloop = None |
| 36 | + window = None |
| 37 | + |
| 38 | + def mock_mainloop(self) -> None: # pylint: disable=unused-argument |
| 39 | + """Mock mainloop to prevent blocking.""" |
| 40 | + |
| 41 | + try: |
| 42 | + # Patch mainloop to prevent blocking |
| 43 | + original_mainloop = tk.Tk.mainloop |
| 44 | + tk.Tk.mainloop = mock_mainloop |
| 45 | + |
| 46 | + # Create the window |
| 47 | + window = ParameterEditorWindow(test_config_manager) |
| 48 | + |
| 49 | + # Basic checks |
| 50 | + assert window.root is not None |
| 51 | + assert hasattr(window, "configuration_manager") |
| 52 | + assert window.configuration_manager is test_config_manager |
| 53 | + |
| 54 | + finally: |
| 55 | + # Restore original mainloop |
| 56 | + if original_mainloop: |
| 57 | + tk.Tk.mainloop = original_mainloop |
| 58 | + |
| 59 | + # Clean up window |
| 60 | + if window and window.root: |
| 61 | + window.root.destroy() |
| 62 | + |
| 63 | + @pytest.mark.skip(reason="GUI test requires display - run manually in GUI environment") |
| 64 | + def test_full_gui_with_pyautogui(self, test_config_manager: ConfigurationManager) -> None: # pylint: disable=unused-argument |
| 65 | + """Full GUI test with PyAutoGUI - requires display.""" |
| 66 | + # This test would run the full GUI and use PyAutoGUI to interact with it |
| 67 | + # For now, it's skipped as it requires a display environment |
| 68 | + |
| 69 | + # Example of what the test could do: |
| 70 | + # 1. Start GUI in separate thread |
| 71 | + # 2. Use PyAutoGUI to locate window |
| 72 | + # 3. Take screenshots |
| 73 | + # 4. Simulate mouse/keyboard interactions |
| 74 | + # 5. Verify GUI behavior |
| 75 | + |
| 76 | + pytest.skip("Full GUI test requires display environment") |
| 77 | + |
| 78 | + def test_display_usage_popup_window(self, mocker) -> None: |
| 79 | + """Test that the usage popup window can be created.""" |
| 80 | + # Create a mock parent window |
| 81 | + parent = tk.Tk() |
| 82 | + parent.withdraw() # Hide the parent window |
| 83 | + |
| 84 | + try: |
| 85 | + # Mock the UsagePopupWindow.display method to avoid actually showing the window |
| 86 | + mock_display = mocker.patch( |
| 87 | + "ardupilot_methodic_configurator.frontend_tkinter_parameter_editor.UsagePopupWindow.display" |
| 88 | + ) |
| 89 | + |
| 90 | + # Call the method |
| 91 | + ParameterEditorWindow._ParameterEditorWindow__display_usage_popup_window(parent) # pylint: disable=protected-access |
| 92 | + |
| 93 | + # Verify that UsagePopupWindow.display was called |
| 94 | + mock_display.assert_called_once() |
| 95 | + args = mock_display.call_args[0] |
| 96 | + |
| 97 | + # Check that the correct arguments were passed |
| 98 | + assert len(args) >= 5 # parent, window, title, key, size |
| 99 | + assert "How to use the parameter file editor and uploader window" in args[2] # title |
| 100 | + assert args[3] == "parameter_editor" # key |
| 101 | + assert args[4] == "690x360" # size |
| 102 | + |
| 103 | + finally: |
| 104 | + parent.destroy() |
| 105 | + |
| 106 | + def test_show_about_window(self, mocker) -> None: # pylint: disable=too-many-locals |
| 107 | + """Test that the about window can be created.""" |
| 108 | + # Create a mock root window |
| 109 | + root = tk.Tk() |
| 110 | + root.withdraw() # Hide the root window |
| 111 | + |
| 112 | + try: |
| 113 | + # Mock webbrowser.open to avoid actually opening URLs |
| 114 | + mocker.patch("ardupilot_methodic_configurator.frontend_tkinter_parameter_editor.webbrowser_open") |
| 115 | + |
| 116 | + # Call the function |
| 117 | + show_about_window(root, "1.0.0") |
| 118 | + |
| 119 | + # Find the about window (it should be a Toplevel child of root) |
| 120 | + about_windows = [child for child in root.winfo_children() if isinstance(child, tk.Toplevel)] |
| 121 | + |
| 122 | + # There should be exactly one about window |
| 123 | + assert len(about_windows) == 1 |
| 124 | + about_window = about_windows[0] |
| 125 | + |
| 126 | + # Check window properties |
| 127 | + assert about_window.title() == "About" |
| 128 | + # Check that geometry contains the expected size (position may vary) |
| 129 | + geometry = about_window.geometry() |
| 130 | + assert "650x340" in geometry |
| 131 | + |
| 132 | + # Check that the window contains the expected content |
| 133 | + # Find all labels in the window (using ttk.Label) |
| 134 | + def find_labels(widget) -> list: |
| 135 | + labels = [] |
| 136 | + # Check for both tk.Label and ttk.Label |
| 137 | + if isinstance(widget, (tk.Label, ttk.Label)): |
| 138 | + labels.append(widget) |
| 139 | + for child in widget.winfo_children(): |
| 140 | + labels.extend(find_labels(child)) |
| 141 | + return labels |
| 142 | + |
| 143 | + labels = find_labels(about_window) |
| 144 | + assert len(labels) > 0 |
| 145 | + |
| 146 | + # Check that at least one label contains version information |
| 147 | + version_found = False |
| 148 | + for label in labels: |
| 149 | + text = label.cget("text") |
| 150 | + if "ArduPilot Methodic Configurator Version: 1.0.0" in text: |
| 151 | + version_found = True |
| 152 | + break |
| 153 | + assert version_found, "Version information not found in about window" |
| 154 | + |
| 155 | + # Check that buttons are created |
| 156 | + def find_buttons(widget) -> list: |
| 157 | + buttons = [] |
| 158 | + # Check for both tk.Button and ttk.Button |
| 159 | + if isinstance(widget, (tk.Button, ttk.Button)): |
| 160 | + buttons.append(widget) |
| 161 | + for child in widget.winfo_children(): |
| 162 | + buttons.extend(find_buttons(child)) |
| 163 | + return buttons |
| 164 | + |
| 165 | + buttons = find_buttons(about_window) |
| 166 | + expected_buttons = ["User Manual", "Support Forum", "Report a Bug", "Licenses", "Source Code"] |
| 167 | + button_texts = [btn.cget("text") for btn in buttons] |
| 168 | + |
| 169 | + for expected_text in expected_buttons: |
| 170 | + assert expected_text in button_texts, f"Button '{expected_text}' not found" |
| 171 | + |
| 172 | + # Clean up the about window |
| 173 | + about_window.destroy() |
| 174 | + |
| 175 | + finally: |
| 176 | + root.destroy() |
0 commit comments