Make unittests work without QtWebKit

This commit is contained in:
Florian Bruhin 2016-09-05 18:45:50 +02:00
parent 3b8537ff26
commit 7592345b6e
8 changed files with 71 additions and 42 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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