From 7592345b6e3b662b4374d6616098357d3866f3c9 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Mon, 5 Sep 2016 18:45:50 +0200 Subject: [PATCH] Make unittests work without QtWebKit --- tests/helpers/fixtures.py | 16 ++---- tests/unit/browser/webkit/test_history.py | 25 +++++++-- tests/unit/browser/webkit/test_mhtml.py | 2 +- .../unit/browser/webkit/test_qt_javascript.py | 8 ++- tests/unit/browser/webkit/test_webkitelem.py | 4 +- tests/unit/javascript/conftest.py | 53 +++++++++++-------- .../position_caret/test_position_caret.py | 3 ++ tests/unit/misc/test_sessions.py | 2 +- 8 files changed, 71 insertions(+), 42 deletions(-) diff --git a/tests/helpers/fixtures.py b/tests/helpers/fixtures.py index 2fdaf005f..e6d23ef3a 100644 --- a/tests/helpers/fixtures.py +++ b/tests/helpers/fixtures.py @@ -45,10 +45,6 @@ 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 -try: - from PyQt5 import QtWebEngineWidgets -except ImportError as e: - QtWebEngineWidgets = None class WinRegistryHelper: @@ -335,17 +331,15 @@ def qnam(qapp): @pytest.fixture def webengineview(): """Get a QWebEngineView if QtWebEngine is available.""" - if QtWebEngineWidgets is None: - pytest.skip("QtWebEngine unavailable") + QtWebEngineWidgets = pytest.importorskip('PyQt5.QtWebEngineWidgets') return QtWebEngineWidgets.QWebEngineView() @pytest.fixture def webpage(qnam): """Get a new QWebPage object.""" - from PyQt5.QtWebKitWidgets import QWebPage - - page = QWebPage() + QtWebKitWidgets = pytest.importorskip('PyQt5.QtWebKitWidgets') + page = QtWebKitWidgets.QWebPage() page.networkAccessManager().deleteLater() page.setNetworkAccessManager(qnam) return page @@ -354,9 +348,9 @@ def webpage(qnam): @pytest.fixture def webview(qtbot, webpage): """Get a new QWebView object.""" - from PyQt5.QtWebKitWidgets import QWebView + QtWebKitWidgets = pytest.importorskip('PyQt5.QtWebKitWidgets') - view = QWebView() + view = QtWebKitWidgets.QWebView() qtbot.add_widget(view) view.page().deleteLater() diff --git a/tests/unit/browser/webkit/test_history.py b/tests/unit/browser/webkit/test_history.py index bf9d96e5b..5598a00ef 100644 --- a/tests/unit/browser/webkit/test_history.py +++ b/tests/unit/browser/webkit/test_history.py @@ -26,10 +26,8 @@ import pytest import hypothesis from hypothesis import strategies from PyQt5.QtCore import QUrl -from PyQt5.QtWebKit import QWebHistoryInterface from qutebrowser.browser import history -from qutebrowser.browser.webkit import webkithistory from qutebrowser.utils import objreg @@ -368,6 +366,11 @@ def test_entry_str(entry, expected): @pytest.fixture def hist_interface(): + # pylint: disable=invalid-name + QtWebKit = pytest.importorskip('PyQt5.QtWebKit') + from qutebrowser.browser.webkit import webkithistory + QWebHistoryInterface = QtWebKit.QWebHistoryInterface + # pylint: enable=invalid-name entry = history.Entry(atime=0, url=QUrl('http://www.example.com/'), title='example') history_dict = {'http://www.example.com/': entry} @@ -389,17 +392,33 @@ def test_history_interface(qtbot, webview, hist_interface): @pytest.mark.parametrize('backend', ['webengine', 'webkit']) def test_init(backend, qapp, tmpdir, monkeypatch, fake_save_manager, fake_args): + if backend == 'webengine': + pytest.importorskip('PyQt5.QtWebEngineWidgets') + elif backend == 'webkit': + pytest.importorskip('PyQt5.QtWebKitWidgets') + else: + assert False + fake_args.backend = backend monkeypatch.setattr(history.standarddir, 'data', lambda: str(tmpdir)) history.init(qapp) hist = objreg.get('web-history') assert hist.parent() is qapp - default_interface = QWebHistoryInterface.defaultInterface() + + try: + from PyQt5.QtWebKit import QWebHistoryInterface + except ImportError: + QWebHistoryInterface = None if backend == 'webkit': + default_interface = QWebHistoryInterface.defaultInterface() assert default_interface._history is hist else: assert backend == 'webengine' + if QWebHistoryInterface is None: + default_interface = None + else: + default_interface = QWebHistoryInterface.defaultInterface() # For this to work, nothing can ever have called setDefaultInterface # before (so we need to test webengine before webkit) assert default_interface is None diff --git a/tests/unit/browser/webkit/test_mhtml.py b/tests/unit/browser/webkit/test_mhtml.py index fced28875..7ea3d999e 100644 --- a/tests/unit/browser/webkit/test_mhtml.py +++ b/tests/unit/browser/webkit/test_mhtml.py @@ -23,7 +23,7 @@ import re import pytest -from qutebrowser.browser.webkit import mhtml +mhtml = pytest.importorskip('qutebrowser.browser.webkit.mhtml') @pytest.fixture(autouse=True) diff --git a/tests/unit/browser/webkit/test_qt_javascript.py b/tests/unit/browser/webkit/test_qt_javascript.py index 5b69b2331..496fb181d 100644 --- a/tests/unit/browser/webkit/test_qt_javascript.py +++ b/tests/unit/browser/webkit/test_qt_javascript.py @@ -22,12 +22,13 @@ import pytest -from PyQt5.QtWebKit import QWebSettings - @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.""" + # If we get there (because of the webengineview fixture) we can be certain + # QtWebKit is available + from PyQt5.QtWebKit import QWebSettings webview.settings().setAttribute(QWebSettings.JavascriptEnabled, js_enabled) result = webview.page().mainFrame().evaluateJavaScript('1 + 1') assert result == expected @@ -36,6 +37,9 @@ def test_simple_js_webkit(webview, js_enabled, expected): @pytest.mark.parametrize('js_enabled, expected', [(True, 2.0), (False, 2.0)]) def test_element_js_webkit(webview, js_enabled, expected): """With QtWebKit, evaluateJavaScript on an element works with JS off.""" + # If we get there (because of the webengineview fixture) we can be certain + # QtWebKit is available + from PyQt5.QtWebKit import QWebSettings webview.settings().setAttribute(QWebSettings.JavascriptEnabled, js_enabled) elem = webview.page().mainFrame().documentElement() result = elem.evaluateJavaScript('1 + 1') diff --git a/tests/unit/browser/webkit/test_webkitelem.py b/tests/unit/browser/webkit/test_webkitelem.py index efe676c5a..34cb4b7e6 100644 --- a/tests/unit/browser/webkit/test_webkitelem.py +++ b/tests/unit/browser/webkit/test_webkitelem.py @@ -24,9 +24,9 @@ import collections.abc import operator import itertools -from PyQt5.QtCore import QRect, QPoint, QUrl -from PyQt5.QtWebKit import QWebElement import pytest +from PyQt5.QtCore import QRect, QPoint, QUrl +QWebElement = pytest.importorskip('PyQt5.QtWebKit').QWebElement from qutebrowser.browser import webelem from qutebrowser.browser.webkit import webkitelem diff --git a/tests/unit/javascript/conftest.py b/tests/unit/javascript/conftest.py index 6ab9deb6b..88cfecdc4 100644 --- a/tests/unit/javascript/conftest.py +++ b/tests/unit/javascript/conftest.py @@ -25,39 +25,48 @@ import logging import pytest import jinja2 -from PyQt5.QtWebKit import QWebSettings -from PyQt5.QtWebKitWidgets import QWebPage + +try: + from PyQt5.QtWebKit import QWebSettings + from PyQt5.QtWebKitWidgets import QWebPage +except ImportError: + # FIXME:qtwebengine Make these tests use the tab API + QWebSettings = None + QWebPage = None from qutebrowser.utils import utils -class TestWebPage(QWebPage): +if QWebPage is None: + TestWebPage = None +else: + class TestWebPage(QWebPage): - """QWebPage subclass which overrides some test methods. + """QWebPage subclass which overrides some test methods. - Attributes: - _logger: The logger used for alerts. - """ + Attributes: + _logger: The logger used for alerts. + """ - def __init__(self, parent=None): - super().__init__(parent) - self._logger = logging.getLogger('js-tests') + def __init__(self, parent=None): + super().__init__(parent) + self._logger = logging.getLogger('js-tests') - def javaScriptAlert(self, _frame, msg): - """Log javascript alerts.""" - self._logger.info("js alert: {}".format(msg)) + def javaScriptAlert(self, _frame, msg): + """Log javascript alerts.""" + self._logger.info("js alert: {}".format(msg)) - def javaScriptConfirm(self, _frame, msg): - """Fail tests on js confirm() as that should never happen.""" - pytest.fail("js confirm: {}".format(msg)) + def javaScriptConfirm(self, _frame, msg): + """Fail tests on js confirm() as that should never happen.""" + pytest.fail("js confirm: {}".format(msg)) - def javaScriptPrompt(self, _frame, msg, _default): - """Fail tests on js prompt() as that should never happen.""" - pytest.fail("js prompt: {}".format(msg)) + def javaScriptPrompt(self, _frame, msg, _default): + """Fail tests on js prompt() as that should never happen.""" + pytest.fail("js prompt: {}".format(msg)) - def javaScriptConsoleMessage(self, msg, line, source): - """Fail tests on js console messages as they're used for errors.""" - pytest.fail("js console ({}:{}): {}".format(source, line, msg)) + def javaScriptConsoleMessage(self, msg, line, source): + """Fail tests on js console messages as they're used for errors.""" + pytest.fail("js console ({}:{}): {}".format(source, line, msg)) class JSTester: diff --git a/tests/unit/javascript/position_caret/test_position_caret.py b/tests/unit/javascript/position_caret/test_position_caret.py index 2561362d6..ca5d75d2e 100644 --- a/tests/unit/javascript/position_caret/test_position_caret.py +++ b/tests/unit/javascript/position_caret/test_position_caret.py @@ -21,6 +21,9 @@ import pytest +# FIXME:qtwebengine Make these tests use the tab API +pytest.importorskip('PyQt5.QtWebKit') + from PyQt5.QtCore import Qt from PyQt5.QtWebKit import QWebSettings from PyQt5.QtWebKitWidgets import QWebPage diff --git a/tests/unit/misc/test_sessions.py b/tests/unit/misc/test_sessions.py index 30b170179..07e997344 100644 --- a/tests/unit/misc/test_sessions.py +++ b/tests/unit/misc/test_sessions.py @@ -26,7 +26,7 @@ import logging import pytest import yaml from PyQt5.QtCore import QUrl, QPoint, QByteArray, QObject -from PyQt5.QtWebKitWidgets import QWebView +QWebView = pytest.importorskip('PyQt5.QtWebKitWidgets').QWebView from qutebrowser.misc import sessions from qutebrowser.utils import objreg, qtutils