tests: Add a callback_checker fixture
This commit is contained in:
parent
aba1556324
commit
359d4dd427
@ -41,7 +41,7 @@ from qutebrowser.browser.webkit import cookies
|
||||
from qutebrowser.misc import savemanager
|
||||
from qutebrowser.keyinput import modeman
|
||||
|
||||
from PyQt5.QtCore import PYQT_VERSION, QEvent, QSize, Qt
|
||||
from PyQt5.QtCore import PYQT_VERSION, pyqtSignal, QEvent, QSize, Qt, QObject
|
||||
from PyQt5.QtGui import QKeyEvent
|
||||
from PyQt5.QtWidgets import QWidget, QHBoxLayout, QVBoxLayout
|
||||
from PyQt5.QtNetwork import QNetworkCookieJar
|
||||
@ -72,6 +72,32 @@ 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)
|
||||
|
||||
def __init__(self, qtbot, parent=None):
|
||||
super().__init__(parent)
|
||||
self._qtbot = qtbot
|
||||
|
||||
def callback(self, result):
|
||||
"""Callback which can be passed to runJavaScript."""
|
||||
self.got_result.emit(result)
|
||||
|
||||
def check(self, expected):
|
||||
"""Wait until the JS result arrived and compare it."""
|
||||
with self._qtbot.waitSignal(self.got_result) as blocker:
|
||||
pass
|
||||
assert blocker.args == [expected]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def callback_checker(qtbot):
|
||||
return CallbackChecker(qtbot)
|
||||
|
||||
|
||||
class FakeStatusBar(QWidget):
|
||||
|
||||
"""Fake statusbar to test progressbar sizing."""
|
||||
|
@ -22,31 +22,9 @@
|
||||
|
||||
import pytest
|
||||
|
||||
from PyQt5.QtCore import QObject, pyqtSignal
|
||||
from PyQt5.QtWebKit import QWebSettings
|
||||
|
||||
|
||||
class WebEngineJSChecker(QObject):
|
||||
|
||||
"""Check if a JS value provided by a callback is the expected one."""
|
||||
|
||||
got_result = pyqtSignal(object)
|
||||
|
||||
def __init__(self, qtbot, parent=None):
|
||||
super().__init__(parent)
|
||||
self._qtbot = qtbot
|
||||
|
||||
def callback(self, result):
|
||||
"""Callback which can be passed to runJavaScript."""
|
||||
self.got_result.emit(result)
|
||||
|
||||
def check(self, expected):
|
||||
"""Wait until the JS result arrived and compare it."""
|
||||
with self._qtbot.waitSignal(self.got_result) as blocker:
|
||||
pass
|
||||
assert blocker.args == [expected]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('js_enabled, expected', [(True, 2.0), (False, None)])
|
||||
def test_simple_js_webkit(webview, js_enabled, expected):
|
||||
"""With QtWebKit, evaluateJavaScript works when JS is on."""
|
||||
@ -66,7 +44,8 @@ def test_element_js_webkit(webview, js_enabled, expected):
|
||||
|
||||
@pytest.mark.usefixtures('redirect_xdg_data')
|
||||
@pytest.mark.parametrize('js_enabled, expected', [(True, 2.0), (False, 2.0)])
|
||||
def test_simple_js_webengine(qtbot, webengineview, js_enabled, expected):
|
||||
def test_simple_js_webengine(callback_checker, webengineview, js_enabled,
|
||||
expected):
|
||||
"""With QtWebEngine, runJavaScript works even when JS is off."""
|
||||
# pylint: disable=no-name-in-module,useless-suppression
|
||||
# If we get there (because of the webengineview fixture) we can be certain
|
||||
@ -75,6 +54,5 @@ def test_simple_js_webengine(qtbot, webengineview, js_enabled, expected):
|
||||
webengineview.settings().setAttribute(QWebEngineSettings.JavascriptEnabled,
|
||||
js_enabled)
|
||||
|
||||
checker = WebEngineJSChecker(qtbot)
|
||||
webengineview.page().runJavaScript('1 + 1', checker.callback)
|
||||
checker.check(expected)
|
||||
webengineview.page().runJavaScript('1 + 1', callback_checker.callback)
|
||||
callback_checker.check(expected)
|
||||
|
Loading…
Reference in New Issue
Block a user