1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
from unittest.mock import Mock, patch
import pytest
from qtpy.QtCore import QMutex, QThread, QTimer
from superqt.utils import qdebounced
class _TestThread(QThread):
def __init__(self) -> None:
super().__init__()
self.mutex = QMutex()
def run(self):
self.mutex.lock()
@pytest.mark.disable_qthread_start
def test_disable_qthread(qapp):
t = _TestThread()
t.mutex.lock()
t.start()
assert not t.isRunning()
t.mutex.unlock()
def test_qthread_running(qtbot):
t = _TestThread()
t.mutex.lock()
t.start()
assert t.isRunning()
t.mutex.unlock()
qtbot.waitUntil(t.isFinished, timeout=2000)
@pytest.mark.disable_qtimer_start
def test_disable_qtimer(qtbot):
t = QTimer()
t.setInterval(1000)
t.start()
assert not t.isActive()
# As qtbot uses a QTimer in waitUntil, we also test if timer disable does not break it
th = _TestThread()
th.mutex.lock()
th.start()
assert th.isRunning()
th.mutex.unlock()
qtbot.waitUntil(th.isFinished, timeout=2000)
assert not th.isRunning()
@pytest.mark.usefixtures('_disable_throttling')
@patch('qtpy.QtCore.QTimer.start')
def test_disable_throttle(start_mock):
mock = Mock()
@qdebounced(timeout=50)
def f() -> str:
mock()
f()
start_mock.assert_not_called()
mock.assert_called_once()
def test_lack_disable_throttle(monkeypatch):
"""This is test showing that if we do not use disable_throttling then timer is started"""
mock = Mock()
start_mock = Mock()
active_mock = Mock(return_value=True)
monkeypatch.setattr('qtpy.QtCore.QTimer.start', start_mock)
monkeypatch.setattr('qtpy.QtCore.QTimer.isActive', active_mock)
@qdebounced(timeout=50)
def f() -> str:
mock()
f()
start_mock.assert_called_once()
mock.assert_not_called()
|