Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions beratools/gui/bt_gui_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ def __init__(self, parent=None):
# controls
self.tool_search = QtWidgets.QLineEdit()
self.tool_search.setPlaceholderText("Search...")
# the name screen readers and UI Automation (the pywinauto GUI tests) know the box by
self.tool_search.setAccessibleName("Search tools")

self.tags_model = _SearchProxyModel()
self.tree_model = QtGui.QStandardItemModel()
Expand Down
21 changes: 21 additions & 0 deletions tests/pytest_qt/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
pytest tests/pytest_qt/ -v -m gui_qt
"""

import atexit
import os
import shutil
import sys
import tempfile
from pathlib import Path
from unittest.mock import patch

Expand All @@ -20,6 +24,23 @@

sys.path.insert(0, Path(__file__).parents[2].as_posix())

# The GUI keeps its saved tool parameters, tool history and last folder in ~/.beratools, and
# fills the forms from them. The tests get an empty home folder of their own, so a value saved
# while using the GUI (skip_existing set to true, say) doesn't change the forms they check, and
# the tests don't change the user's settings. It must be set before beratools.gui is imported, as
# bt_gui_main reads the settings when imported. Path.home() reads USERPROFILE on Windows and HOME
# elsewhere.
# resolved: TEMP can be an 8.3 short path (C:/Users/ABCDEF~1/...), which Qt's dialogs report long
_TEST_HOME = str(Path(tempfile.mkdtemp(prefix="beratools_test_home_")).resolve())
os.environ["USERPROFILE"] = _TEST_HOME
os.environ["HOME"] = _TEST_HOME
atexit.register(shutil.rmtree, _TEST_HOME, ignore_errors=True)

# The widgets are made off screen, so the tests don't flash windows, or tooltips at the mouse (the
# CPU slider shows its value at the cursor when it changes), on the desktop. Set QT_QPA_PLATFORM
# (to "windows", say) to watch them on screen. It must be set before the QApplication is made.
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")

from beratools.gui.bt_data import BTData
from beratools.gui.bt_gui_main import MainWindow
from beratools.gui.tool_widgets import (
Expand Down
7 changes: 7 additions & 0 deletions tests/pytest_qt/test_main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,14 @@ def _get_saved(self, tool_api, variable=None):
saved_value = 42 if isinstance(target.default_value, int) else 42.0
target.set_value(saved_value)

# the form's required in/out files are empty, so get_widgets_arguments() rejects it;
# read the raw values instead since only the numeric round trip is under test
with (
patch.object(
main_window.tool_widget,
"get_widgets_arguments",
side_effect=main_window.tool_widget.get_widgets_values,
),
patch("beratools.gui.bt_gui_main.bt.add_tool_history", side_effect=_add_tool_history),
patch("beratools.gui.bt_gui_main.bt.save_tool_info"),
patch.object(bt_data.BTData, "get_saved_tool_params", autospec=True, side_effect=_get_saved),
Expand Down
2 changes: 1 addition & 1 deletion tests/pytest_qt/test_tool_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ def test_round_trip(self, make_tool_widget, testdata_dir, tmp_path):
"in_line": {"path": str(testdata_dir / "integration_aoi.gpkg"), "layer": "centerline"},
"in_footprint": {
"path": str(testdata_dir / "integration_aoi.gpkg"),
"layer": "footprint_rel",
"layer": "footprint_abs",
},
"n_samples": 15,
"offset": 30.0,
Expand Down
20 changes: 18 additions & 2 deletions tests/pywinauto/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,26 @@ def bera_app():
# ---------------------------------------------------------------------------


TOOL_SEARCH_NAME = "Search tools" # the accessible name of BTTreeView's search box


def tool_search_box(main, timeout=10):
"""The tool tree's search box, by its accessible name. Not the window's first Edit: which Edit
comes first depends on the layout (with the left side in splitters it is the tool form's first
input). Nor child_window(title=...): for an Edit, pywinauto matches the title against its text."""
end = time.time() + timeout
while True:
for edit in main.descendants(control_type="Edit"):
if edit.element_info.name == TOOL_SEARCH_NAME and edit.is_visible() and edit.is_enabled():
return edit
if time.time() > end:
raise TimeoutError(f"No visible, enabled Edit named {TOOL_SEARCH_NAME!r} in the window")
time.sleep(0.2)


def select_tool(main, tool_name: str):
"""Select a tool from the tree view using the search box."""
search = main.child_window(control_type="Edit", found_index=0)
search.wait("visible enabled", timeout=10)
search = tool_search_box(main)
search.set_edit_text("")
time.sleep(0.3)
search.set_edit_text(tool_name)
Expand Down
Loading