From d39dda38cef502cbda6a572284c30714bdffa5b0 Mon Sep 17 00:00:00 2001 From: Jay Kamat Date: Fri, 10 Nov 2017 12:27:00 -0500 Subject: [PATCH] Refactor CallbackChecker into test utils --- tests/helpers/fixtures.py | 31 ++----------------------------- tests/helpers/utils.py | 26 ++++++++++++++++++++++++++ tests/unit/javascript/conftest.py | 2 +- 3 files changed, 29 insertions(+), 30 deletions(-) diff --git a/tests/helpers/fixtures.py b/tests/helpers/fixtures.py index 6fe64a2d2..8caf6fcf5 100644 --- a/tests/helpers/fixtures.py +++ b/tests/helpers/fixtures.py @@ -37,13 +37,14 @@ import pytest import py.path # pylint: disable=no-name-in-module import helpers.stubs as stubsmod +from helpers.utils import CallbackChecker from qutebrowser.config import config, configdata, configtypes, configexc from qutebrowser.utils import objreg, standarddir from qutebrowser.browser.webkit import cookies from qutebrowser.misc import savemanager, sql 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.QtWidgets import QWidget, QHBoxLayout, QVBoxLayout from PyQt5.QtNetwork import QNetworkCookieJar @@ -78,34 +79,6 @@ class WinRegistryHelper: 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 def callback_checker(qtbot): return CallbackChecker(qtbot) diff --git a/tests/helpers/utils.py b/tests/helpers/utils.py index e6e3d37c8..45141f7dc 100644 --- a/tests/helpers/utils.py +++ b/tests/helpers/utils.py @@ -28,6 +28,7 @@ import contextlib import pytest from qutebrowser.utils import qtutils +from PyQt5.QtCore import QObject, pyqtSignal qt58 = pytest.mark.skipif( @@ -176,3 +177,28 @@ def abs_datapath(): @contextlib.contextmanager def nop_contextmanager(): 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 diff --git a/tests/unit/javascript/conftest.py b/tests/unit/javascript/conftest.py index b6b0980d5..103c9cf4d 100644 --- a/tests/unit/javascript/conftest.py +++ b/tests/unit/javascript/conftest.py @@ -25,7 +25,7 @@ import logging import pytest import jinja2 -from tests.helpers.fixtures import CallbackChecker +from helpers.utils import CallbackChecker from PyQt5.QtCore import QUrl try: