Refactor CallbackChecker into test utils
This commit is contained in:
parent
155ee198cd
commit
d39dda38ce
@ -37,13 +37,14 @@ import pytest
|
|||||||
import py.path # pylint: disable=no-name-in-module
|
import py.path # pylint: disable=no-name-in-module
|
||||||
|
|
||||||
import helpers.stubs as stubsmod
|
import helpers.stubs as stubsmod
|
||||||
|
from helpers.utils import CallbackChecker
|
||||||
from qutebrowser.config import config, configdata, configtypes, configexc
|
from qutebrowser.config import config, configdata, configtypes, configexc
|
||||||
from qutebrowser.utils import objreg, standarddir
|
from qutebrowser.utils import objreg, standarddir
|
||||||
from qutebrowser.browser.webkit import cookies
|
from qutebrowser.browser.webkit import cookies
|
||||||
from qutebrowser.misc import savemanager, sql
|
from qutebrowser.misc import savemanager, sql
|
||||||
from qutebrowser.keyinput import modeman
|
from qutebrowser.keyinput import modeman
|
||||||
|
|
||||||
from PyQt5.QtCore import pyqtSignal, QEvent, QSize, Qt, QObject
|
from PyQt5.QtCore import QEvent, QSize, Qt
|
||||||
from PyQt5.QtGui import QKeyEvent
|
from PyQt5.QtGui import QKeyEvent
|
||||||
from PyQt5.QtWidgets import QWidget, QHBoxLayout, QVBoxLayout
|
from PyQt5.QtWidgets import QWidget, QHBoxLayout, QVBoxLayout
|
||||||
from PyQt5.QtNetwork import QNetworkCookieJar
|
from PyQt5.QtNetwork import QNetworkCookieJar
|
||||||
@ -78,34 +79,6 @@ class WinRegistryHelper:
|
|||||||
del objreg.window_registry[win_id]
|
del objreg.window_registry[win_id]
|
||||||
|
|
||||||
|
|
||||||
class CallbackChecker(QObject):
|
|
||||||
|
|
||||||
"""Check if a value provided by a callback is the expected one."""
|
|
||||||
|
|
||||||
got_result = pyqtSignal(object)
|
|
||||||
UNSET = object()
|
|
||||||
|
|
||||||
def __init__(self, qtbot, parent=None):
|
|
||||||
super().__init__(parent)
|
|
||||||
self._qtbot = qtbot
|
|
||||||
self._result = self.UNSET
|
|
||||||
|
|
||||||
def callback(self, result):
|
|
||||||
"""Callback which can be passed to runJavaScript."""
|
|
||||||
self._result = result
|
|
||||||
self.got_result.emit(result)
|
|
||||||
|
|
||||||
def check(self, expected):
|
|
||||||
"""Wait until the JS result arrived and compare it."""
|
|
||||||
if self._result is self.UNSET:
|
|
||||||
with self._qtbot.waitSignal(self.got_result, timeout=2000):
|
|
||||||
pass
|
|
||||||
self._assert_result(self._result, expected)
|
|
||||||
|
|
||||||
def _assert_result(self, result, expected):
|
|
||||||
assert result == expected
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def callback_checker(qtbot):
|
def callback_checker(qtbot):
|
||||||
return CallbackChecker(qtbot)
|
return CallbackChecker(qtbot)
|
||||||
|
@ -28,6 +28,7 @@ import contextlib
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from qutebrowser.utils import qtutils
|
from qutebrowser.utils import qtutils
|
||||||
|
from PyQt5.QtCore import QObject, pyqtSignal
|
||||||
|
|
||||||
|
|
||||||
qt58 = pytest.mark.skipif(
|
qt58 = pytest.mark.skipif(
|
||||||
@ -176,3 +177,28 @@ def abs_datapath():
|
|||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
def nop_contextmanager():
|
def nop_contextmanager():
|
||||||
yield
|
yield
|
||||||
|
|
||||||
|
|
||||||
|
class CallbackChecker(QObject):
|
||||||
|
|
||||||
|
"""Check if a value provided by a callback is the expected one."""
|
||||||
|
|
||||||
|
got_result = pyqtSignal(object)
|
||||||
|
UNSET = object()
|
||||||
|
|
||||||
|
def __init__(self, qtbot, parent=None):
|
||||||
|
super().__init__(parent)
|
||||||
|
self._qtbot = qtbot
|
||||||
|
self._result = self.UNSET
|
||||||
|
|
||||||
|
def callback(self, result):
|
||||||
|
"""Callback which can be passed to runJavaScript."""
|
||||||
|
self._result = result
|
||||||
|
self.got_result.emit(result)
|
||||||
|
|
||||||
|
def check(self, expected):
|
||||||
|
"""Wait until the JS result arrived and compare it."""
|
||||||
|
if self._result is self.UNSET:
|
||||||
|
with self._qtbot.waitSignal(self.got_result, timeout=2000):
|
||||||
|
pass
|
||||||
|
assert self._result == expected
|
||||||
|
@ -25,7 +25,7 @@ import logging
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
import jinja2
|
import jinja2
|
||||||
from tests.helpers.fixtures import CallbackChecker
|
from helpers.utils import CallbackChecker
|
||||||
from PyQt5.QtCore import QUrl
|
from PyQt5.QtCore import QUrl
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
Loading…
Reference in New Issue
Block a user