Skip to content

Commit 08f6b50

Browse files
committed
Background dialog test: wait for Qt events to be processed
1 parent 6361955 commit 08f6b50

2 files changed

Lines changed: 25 additions & 3 deletions

File tree

datalab/tests/features/images/background_dialog_test.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,26 @@
1414
import sigima.objects
1515
import sigima.params
1616
import sigima.proc.image as sigima_image
17-
from guidata.qthelpers import exec_dialog, qt_app_context
17+
from guidata.qthelpers import exec_dialog, qt_app_context, qt_wait_until
1818
from sigima.tests import vistools
1919
from sigima.tests.data import create_noisygauss_image
2020

2121
from datalab.env import execenv
2222
from datalab.widgets.imagebackground import ImageBackgroundDialog
2323

2424

25+
def wait_for_rect_coords(dlg: ImageBackgroundDialog) -> bool:
26+
"""Condition to wait for rectangle coordinates."""
27+
# Wait for rectangle coordinates to be set. This is necessary because
28+
# when executing the dialog in unattended mode, the rectangle
29+
# coordinates may not be set immediately (Qt events may not be processed).
30+
try:
31+
dlg.get_rect_coords()
32+
return True
33+
except ValueError:
34+
return False
35+
36+
2537
def test_image_background_selection() -> None:
2638
"""Image background selection test."""
2739
with qt_app_context():
@@ -30,6 +42,7 @@ def test_image_background_selection() -> None:
3042
dlg.resize(640, 480)
3143
dlg.setObjectName(dlg.objectName() + "_00") # to avoid timestamp suffix
3244
exec_dialog(dlg)
45+
qt_wait_until(lambda: wait_for_rect_coords(dlg))
3346
execenv.print(f"background: {dlg.get_background()}")
3447
execenv.print(f"rect coords: {dlg.get_rect_coords()}")
3548
# Check background value:
@@ -45,6 +58,7 @@ def test_image_offset_correction_with_background_dialog() -> None:
4558
dlg = ImageBackgroundDialog(i1)
4659
ok = exec_dialog(dlg)
4760
if ok:
61+
qt_wait_until(lambda: wait_for_rect_coords(dlg))
4862
param = sigima.objects.ROI2DParam()
4963
# pylint: disable=unbalanced-tuple-unpacking
5064
ix0, iy0, ix1, iy1 = i1.physical_to_indices(dlg.get_rect_coords())

datalab/widgets/imagebackground.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ def get_background(self) -> float:
8181
return self.__background
8282

8383
def get_rect_coords(self) -> tuple[float, float, float, float]:
84-
"""Get rectangle coordinates"""
85-
assert self.__rect_coords is not None, "Rectangle coordinates not set"
84+
"""Get rectangle coordinates
85+
86+
Returns:
87+
tuple: rectangle coordinates (x0, y0, x1, y1)
88+
89+
Raises:
90+
ValueError: if rectangle coordinates are not set
91+
"""
92+
if self.__rect_coords is None:
93+
raise ValueError("Rectangle coordinates not set")
8694
return self.__rect_coords

0 commit comments

Comments
 (0)