1
- import pytest
2
1
import time
3
2
3
+ import pytest
4
+
4
5
from pytestqt .qt_compat import QtCore , Signal
5
6
6
7
7
8
class Signaller (QtCore .QObject ):
8
-
9
9
signal = Signal ()
10
10
signal_2 = Signal ()
11
11
@@ -24,7 +24,7 @@ def explicit_wait(qtbot, signal, timeout, multiple, raising, raises):
24
24
Explicit wait for the signal using blocker API.
25
25
"""
26
26
func = qtbot .waitSignals if multiple else qtbot .waitSignal
27
- blocker = func (signal , timeout , raising = raising )
27
+ blocker = func (signal , timeout , raising = raising )
28
28
assert not blocker .signal_triggered
29
29
if raises :
30
30
with pytest .raises (qtbot .SignalTimeoutError ):
@@ -38,7 +38,7 @@ def context_manager_wait(qtbot, signal, timeout, multiple, raising, raises):
38
38
"""
39
39
Waiting for signal using context manager API.
40
40
"""
41
- func = qtbot .waitSignals if multiple else qtbot .waitSignal
41
+ func = qtbot .waitSignals if multiple else qtbot .waitSignal
42
42
if raises :
43
43
with pytest .raises (qtbot .SignalTimeoutError ):
44
44
with func (signal , timeout , raising = raising ) as blocker :
@@ -66,24 +66,20 @@ def context_manager_wait(qtbot, signal, timeout, multiple, raising, raises):
66
66
(context_manager_wait , 2000 , 500 , False , True ),
67
67
]
68
68
)
69
- def test_signal_triggered (qtbot , wait_function , emit_delay , timeout ,
69
+ def test_signal_triggered (qtbot , single_shot , wait_function , emit_delay , timeout ,
70
70
expected_signal_triggered , raising ):
71
71
"""
72
72
Testing for a signal in different conditions, ensuring we are obtaining
73
73
the expected results.
74
74
"""
75
75
signaller = Signaller ()
76
-
77
- timer = QtCore .QTimer ()
78
- timer .setSingleShot (True )
79
- timer .timeout .connect (signaller .signal .emit )
80
- timer .start (emit_delay )
76
+ single_shot (signaller .signal , emit_delay )
81
77
82
78
start_time = time .time ()
83
79
raises = raising and not expected_signal_triggered
84
80
85
81
blocker = wait_function (qtbot , signaller .signal , timeout , raising = raising ,
86
- raises = raises , multiple = False )
82
+ raises = raises , multiple = False )
87
83
88
84
# Check that event loop exited.
89
85
assert not blocker ._loop .isRunning ()
@@ -113,31 +109,27 @@ def test_signal_triggered(qtbot, wait_function, emit_delay, timeout,
113
109
(context_manager_wait , 2000 , 2000 , 500 , False , False ),
114
110
(context_manager_wait , 500 , 2000 , 1000 , False , False ),
115
111
(context_manager_wait , 2000 , 500 , 1000 , False , False ),
112
+ (context_manager_wait , 2000 , 500 , 1000 , False , True ),
113
+ (context_manager_wait , 500 , 2000 , 1000 , False , True ),
116
114
]
117
115
)
118
- def test_signal_triggered_multiple (qtbot , wait_function , emit_delay_1 ,
116
+ def test_signal_triggered_multiple (qtbot , single_shot , wait_function ,
117
+ emit_delay_1 ,
119
118
emit_delay_2 , timeout ,
120
119
expected_signal_triggered , raising ):
121
120
"""
122
121
Testing for a signal in different conditions, ensuring we are obtaining
123
122
the expected results.
124
123
"""
125
124
signaller = Signaller ()
126
-
127
- timer = QtCore .QTimer ()
128
- timer .setSingleShot (True )
129
- timer .timeout .connect (signaller .signal .emit )
130
- timer .start (emit_delay_1 )
131
-
132
- timer2 = QtCore .QTimer ()
133
- timer2 .setSingleShot (True )
134
- timer2 .timeout .connect (signaller .signal_2 .emit )
135
- timer2 .start (emit_delay_2 )
125
+ single_shot (signaller .signal , emit_delay_1 )
126
+ single_shot (signaller .signal_2 , emit_delay_2 )
136
127
137
128
raises = raising and not expected_signal_triggered
138
129
start_time = time .time ()
139
130
blocker = wait_function (qtbot , [signaller .signal , signaller .signal_2 ],
140
- timeout , multiple = True , raising = raising , raises = raises )
131
+ timeout , multiple = True , raising = raising ,
132
+ raises = raises )
141
133
142
134
# Check that event loop exited.
143
135
assert not blocker ._loop .isRunning ()
@@ -175,3 +167,24 @@ def test_explicit_emit_multiple(qtbot):
175
167
signaller .signal_2 .emit ()
176
168
177
169
assert waiting .signal_triggered
170
+
171
+
172
+ @pytest .yield_fixture
173
+ def single_shot ():
174
+ """
175
+ Fixture that provides a callback with signature: (signal, delay) that
176
+ triggers that signal once after the given delay in ms.
177
+
178
+ The fixture is responsible for cleaning up after the timers.
179
+ """
180
+ def shoot (signal , delay ):
181
+ timer = QtCore .QTimer ()
182
+ timer .setSingleShot (True )
183
+ timer .timeout .connect (signal .emit )
184
+ timer .start (delay )
185
+ timers .append (timer )
186
+
187
+ timers = []
188
+ yield shoot
189
+ for t in timers :
190
+ t .stop ()
0 commit comments