Refactor CallbackChecker into test utils

This commit is contained in:
Jay Kamat 2017-11-10 12:27:00 -05:00
parent 155ee198cd
commit d39dda38ce
No known key found for this signature in database
GPG Key ID: 5D2E399600F4F7B5
3 changed files with 29 additions and 30 deletions

View File

@ -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)

View File

@ -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

View File

@ -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: