Files
cbb-refresh-verify-795/tests/button_simulation.py
T

78 lines
2.6 KiB
Python

"""Real Qt click helpers; external native writers stay outside these UI tests."""
from contextlib import contextmanager
from PySide6.QtCore import QPoint, QTimer, Qt
from PySide6.QtWidgets import QApplication, QCheckBox, QDialogButtonBox, QFileDialog, QMessageBox
def press(qtbot, button):
assert button.isVisible(), button.text()
assert button.isEnabled(), button.text()
if isinstance(button, QCheckBox):
qtbot.mouseClick(button, Qt.LeftButton, pos=QPoint(8, button.height()//2))
else:
qtbot.mouseClick(button, Qt.LeftButton)
def tab(qtbot, window, index):
qtbot.mouseClick(window.section_tabs, Qt.LeftButton,
pos=window.section_tabs.tabRect(index).center())
assert window.section_stack.currentIndex() == index
@contextmanager
def modal_click(qtbot, standard=None, *, file=None, cancel_file=False):
"""Answer the real nested dialog by clicking its actual button."""
timer = QTimer()
deadline = QTimer()
deadline.setSingleShot(True)
seen, errors = [], []
prepared = False
def answer():
nonlocal prepared
dialog = QApplication.activeModalWidget()
try:
if standard is not None and isinstance(dialog, QMessageBox):
button = dialog.button(standard)
if button is None:
return
elif isinstance(dialog, QFileDialog) and (file is not None or cancel_file):
box = dialog.findChild(QDialogButtonBox)
if not cancel_file and not prepared:
dialog.setDirectory(str(file.parent))
dialog.selectFile(str(file))
prepared = True
return
button = box.button(QDialogButtonBox.Cancel if cancel_file else QDialogButtonBox.Open)
else:
return
if button is None or not button.isEnabled():
return
seen.append(dialog.windowTitle())
timer.stop()
press(qtbot, button)
except Exception as error:
errors.append(str(error))
timer.stop()
if dialog is not None:
dialog.reject()
def timeout():
errors.append('The expected modal button did not become available')
dialog = QApplication.activeModalWidget()
if dialog is not None:
dialog.reject()
timer.timeout.connect(answer)
deadline.timeout.connect(timeout)
timer.start(10)
deadline.start(4000)
try:
yield seen
finally:
timer.stop()
deadline.stop()
assert not errors, errors
assert len(seen) == 1, seen